source: XIOS/trunk/src/filter/source_filter.cpp @ 756

Last change on this file since 756 was 756, checked in by rlacroix, 9 years ago

Support freq_offset for fields in files in read mode.

In theory the first record of a file being read is available at timestep 0, this allows to delay the availability of the data to a later timestep.

Use a temporary solution for now until this can be properly integrated in a temporal filter.

File size: 2.4 KB
Line 
1#include "source_filter.hpp"
2#include "grid.hpp"
3#include "exception.hpp"
4#include "calendar_util.hpp"
5
6namespace xios
7{
8  CSourceFilter::CSourceFilter(CGrid* grid, const CDuration offset /*= NoneDu*/)
9    : grid(grid)
10    , offset(offset)
11  {
12    if (!grid)
13      ERROR("CSourceFilter::CSourceFilter(CGrid* grid)",
14            "Impossible to construct a source filter without providing a grid.");
15  }
16
17  template <int N>
18  void CSourceFilter::streamData(CDate date, const CArray<double, N>& data)
19  {
20    date = date + offset; // this is a temporary solution, it should be part of a proper temporal filter
21
22    CDataPacketPtr packet(new CDataPacket);
23    packet->date = date;
24    packet->timestamp = date;
25    packet->status = CDataPacket::NO_ERROR;
26
27    packet->data.resize(grid->storeIndex_client.numElements());
28    grid->inputField(data, packet->data);
29
30    deliverOuput(packet);
31  }
32
33  template void CSourceFilter::streamData<1>(CDate date, const CArray<double, 1>& data);
34  template void CSourceFilter::streamData<2>(CDate date, const CArray<double, 2>& data);
35  template void CSourceFilter::streamData<3>(CDate date, const CArray<double, 3>& data);
36
37  void CSourceFilter::streamDataFromServer(CDate date, const std::map<int, CArray<double, 1> >& data)
38  {
39    date = date + offset; // this is a temporary solution, it should be part of a proper temporal filter
40
41    CDataPacketPtr packet(new CDataPacket);
42    packet->date = date;
43    packet->timestamp = date;
44    packet->status = CDataPacket::NO_ERROR;
45
46    if (data.size() != grid->storeIndex_toSrv.size())
47      ERROR("CSourceFilter::streamDataFromServer(CDate date, const std::map<int, CArray<double, 1> >& data)",
48            << "Incoherent data received from servers,"
49            << " expected " << grid->storeIndex_toSrv.size() << " chunks but " << data.size() << " were given.");
50
51    packet->data.resize(grid->storeIndex_client.numElements());
52    std::map<int, CArray<double, 1> >::const_iterator it, itEnd = data.end();
53    for (it = data.begin(); it != itEnd; it++)
54    {
55      CArray<int,1>& index = grid->storeIndex_toSrv[it->first];
56
57      for (int n = 0; n < index.numElements(); n++)
58        packet->data(index(n)) = it->second(n);
59    }
60
61    deliverOuput(packet);
62  }
63
64  void CSourceFilter::signalEndOfStream(CDate date)
65  {
66    CDataPacketPtr packet(new CDataPacket);
67    packet->date = date;
68    packet->timestamp = date;
69    packet->status = CDataPacket::END_OF_STREAM;
70    deliverOuput(packet);
71  }
72} // namespace xios
Note: See TracBrowser for help on using the repository browser.