source: XIOS/dev/dev_oa/src/filter/source_filter.cpp @ 1967

Last change on this file since 1967 was 1967, checked in by oabramkina, 4 years ago

dev_oa: bugfix on tiled domains. Previosly only the first ts was written.

File size: 6.8 KB
Line 
1#include "source_filter.hpp"
2#include "grid.hpp"
3#include "exception.hpp"
4#include "calendar_util.hpp"
5#include <limits>
6#include "workflow_graph.hpp"
7
8namespace xios
9{
10  CSourceFilter::CSourceFilter(CGarbageCollector& gc, CGrid* grid,
11                               bool compression /*= true*/, bool mask /*= false*/,
12                               const CDuration offset /*= NoneDu*/, bool manualTrigger /*= false*/,
13                               bool hasMissingValue /*= false*/,
14                               double defaultValue /*= 0.0*/)
15    : COutputPin(gc, manualTrigger)
16    , grid(grid)
17    , compression(compression)
18    , mask(mask)
19    , offset(offset)
20    , hasMissingValue(hasMissingValue), defaultValue(defaultValue)
21    , ntiles(0)
22    , storedTileData()
23  {
24    if (!grid)
25      ERROR("CSourceFilter::CSourceFilter(CGrid* grid)",
26            "Impossible to construct a source filter without providing a grid.");
27  }
28
29  void CSourceFilter::buildGraph(CDataPacketPtr packet)
30  {
31    bool filter_interval=false;
32    if (this->field)
33    {
34      if(this->field->field_graph_start == -1 && this->field->field_graph_end == -1) filter_interval = true;
35      else filter_interval = packet->timestamp >= this->field->field_graph_start && packet->timestamp <= this->field->field_graph_end;
36    }
37    bool building_graph = this->tag ? filter_interval : false;
38    if(building_graph)
39    {
40      this->filterID = InvalidableObject::filterIdGenerator++; 
41      packet->src_filterID=this->filterID;
42      packet->field = this->field;
43      packet->distance = 1;
44     
45      CWorkflowGraph::allocNodeEdge();
46     
47      CWorkflowGraph::addNode(this->filterID, "Source Filter ", 1, 1, 0, packet);
48      (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].attributes = this->field->record4graphXiosAttributes();
49      (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].field_id = this->field->getId();
50      (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].distance = 1;
51
52      CWorkflowGraph::build_begin = true;
53    }
54
55  }
56
57  template <int N>
58  void CSourceFilter::streamTile(CDate date, const CArray<double, N>& tileData, int tileId)
59  {
60    if (ntiles==0)
61    {
62      const double nanValue = std::numeric_limits<double>::quiet_NaN();
63      storedTileData.resize(grid->storeIndex_client.numElements());
64      storedTileData = nanValue;
65    }
66    grid->copyTile(tileData, storedTileData, tileId);
67    ++ntiles;
68    if (ntiles==grid->getNTiles())
69    {
70      // Data entering workflow will be exactly of size ni*nj for a grid 2d or ni*nj*n for a grid 3d
71      streamData(date, storedTileData);
72      ntiles = 0;
73    }
74  }
75
76  template <int N>
77  void CSourceFilter::streamData(CDate date, const CArray<double, N>& data)
78  {
79    date = date + offset; // this is a temporary solution, it should be part of a proper temporal filter
80
81    CDataPacketPtr packet(new CDataPacket);
82    packet->date = date;
83    packet->timestamp = date;
84    packet->status = CDataPacket::NO_ERROR;
85
86    packet->data.resize(grid->storeIndex_client.numElements());   
87   
88    if (compression)
89    {
90      packet->data = defaultValue;
91      grid->uncompressField(data, packet->data);   
92    }
93    else
94    {
95      if (mask)
96        grid->maskField(data, packet->data);
97      else
98        grid->inputField(data, packet->data);
99    }
100    // Convert missing values to NaN
101    if (hasMissingValue)
102    {
103      const double nanValue = std::numeric_limits<double>::quiet_NaN();
104      const size_t nbData = packet->data.numElements();
105      for (size_t idx = 0; idx < nbData; ++idx)
106      {
107        if (defaultValue == packet->data(idx))
108          packet->data(idx) = nanValue;
109      }
110    }
111
112    if(CXios::isClient) buildGraph(packet);
113   
114
115
116    onOutputReady(packet);
117  }
118
119  template void CSourceFilter::streamData<1>(CDate date, const CArray<double, 1>& data);
120  template void CSourceFilter::streamData<2>(CDate date, const CArray<double, 2>& data);
121  template void CSourceFilter::streamData<3>(CDate date, const CArray<double, 3>& data);
122  template void CSourceFilter::streamData<4>(CDate date, const CArray<double, 4>& data);
123  template void CSourceFilter::streamData<5>(CDate date, const CArray<double, 5>& data);
124  template void CSourceFilter::streamData<6>(CDate date, const CArray<double, 6>& data);
125  template void CSourceFilter::streamData<7>(CDate date, const CArray<double, 7>& data);
126
127  template void CSourceFilter::streamTile<1>(CDate date, const CArray<double, 1>& data, int ntile);
128  template void CSourceFilter::streamTile<2>(CDate date, const CArray<double, 2>& data, int ntile);
129  template void CSourceFilter::streamTile<3>(CDate date, const CArray<double, 3>& data, int ntile);
130  template void CSourceFilter::streamTile<4>(CDate date, const CArray<double, 4>& data, int ntile);
131  template void CSourceFilter::streamTile<5>(CDate date, const CArray<double, 5>& data, int ntile);
132  template void CSourceFilter::streamTile<6>(CDate date, const CArray<double, 6>& data, int ntile);
133  template void CSourceFilter::streamTile<7>(CDate date, const CArray<double, 7>& data, int ntile);
134
135  void CSourceFilter::streamDataFromServer(CDate date, const std::map<int, CArray<double, 1> >& data)
136  {
137    date = date + offset; // this is a temporary solution, it should be part of a proper temporal filter
138
139    CDataPacketPtr packet(new CDataPacket);
140    packet->date = date;
141    packet->timestamp = date;
142    packet->status = CDataPacket::NO_ERROR;
143   
144    if (data.size() != grid->storeIndex_fromSrv.size())
145      ERROR("CSourceFilter::streamDataFromServer(CDate date, const std::map<int, CArray<double, 1> >& data)",
146            << "Incoherent data received from servers,"
147            << " expected " << grid->storeIndex_fromSrv.size() << " chunks but " << data.size() << " were given.");
148
149    packet->data.resize(grid->storeIndex_client.numElements());
150    std::map<int, CArray<double, 1> >::const_iterator it, itEnd = data.end();
151    for (it = data.begin(); it != itEnd; it++)
152    {     
153      CArray<int,1>& index = grid->storeIndex_fromSrv[it->first];
154      for (int n = 0; n < index.numElements(); n++)
155        packet->data(index(n)) = it->second(n);
156    }
157
158    // Convert missing values to NaN
159    if (hasMissingValue)
160    {
161      const double nanValue = std::numeric_limits<double>::quiet_NaN();
162      const size_t nbData = packet->data.numElements();
163      for (size_t idx = 0; idx < nbData; ++idx)
164      {
165        if (defaultValue == packet->data(idx))
166          packet->data(idx) = nanValue;
167      }
168    }
169    if(CXios::isClient) buildGraph(packet);
170    onOutputReady(packet);
171  }
172
173  void CSourceFilter::signalEndOfStream(CDate date)
174  {
175    date = date + offset; // this is a temporary solution, it should be part of a proper temporal filter
176
177    CDataPacketPtr packet(new CDataPacket);
178    packet->date = date;
179    packet->timestamp = date;
180    packet->status = CDataPacket::END_OF_STREAM; 
181    onOutputReady(packet);
182  }
183} // namespace xios
Note: See TracBrowser for help on using the repository browser.