source: XIOS/dev/branch_yushan/src/filter/source_filter.cpp @ 1056

Last change on this file since 1056 was 1056, checked in by yushan, 6 years ago

one file mode OK. Tested with test_client

File size: 2.8 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
29    grid->inputField(data, packet->data);
30
31    deliverOuput(packet);
32
33  }
34
35  template void CSourceFilter::streamData<1>(CDate date, const CArray<double, 1>& data);
36  template void CSourceFilter::streamData<2>(CDate date, const CArray<double, 2>& data);
37  template void CSourceFilter::streamData<3>(CDate date, const CArray<double, 3>& data);
38  template void CSourceFilter::streamData<4>(CDate date, const CArray<double, 4>& data);
39  template void CSourceFilter::streamData<5>(CDate date, const CArray<double, 5>& data);
40  template void CSourceFilter::streamData<6>(CDate date, const CArray<double, 6>& data);
41  template void CSourceFilter::streamData<7>(CDate date, const CArray<double, 7>& data);
42
43  void CSourceFilter::streamDataFromServer(CDate date, const std::map<int, CArray<double, 1> >& data)
44  {
45    date = date + offset; // this is a temporary solution, it should be part of a proper temporal filter
46
47    CDataPacketPtr packet(new CDataPacket);
48    packet->date = date;
49    packet->timestamp = date;
50    packet->status = CDataPacket::NO_ERROR;
51
52    if (data.size() != grid->storeIndex_toSrv.size())
53      ERROR("CSourceFilter::streamDataFromServer(CDate date, const std::map<int, CArray<double, 1> >& data)",
54            << "Incoherent data received from servers,"
55            << " expected " << grid->storeIndex_toSrv.size() << " chunks but " << data.size() << " were given.");
56
57    packet->data.resize(grid->storeIndex_client.numElements());
58    std::map<int, CArray<double, 1> >::const_iterator it, itEnd = data.end();
59    for (it = data.begin(); it != itEnd; it++)
60    {
61      CArray<int,1>& index = grid->storeIndex_toSrv[it->first];
62
63      for (int n = 0; n < index.numElements(); n++)
64        packet->data(index(n)) = it->second(n);
65    }
66
67    deliverOuput(packet);
68  }
69
70  void CSourceFilter::signalEndOfStream(CDate date)
71  {
72    CDataPacketPtr packet(new CDataPacket);
73    packet->date = date;
74    packet->timestamp = date;
75    packet->status = CDataPacket::END_OF_STREAM;
76    deliverOuput(packet);
77  }
78} // namespace xios
Note: See TracBrowser for help on using the repository browser.