source: XIOS/dev/dev_trunk_omp/src/filter/source_filter.cpp @ 1689

Last change on this file since 1689 was 1689, checked in by yushan, 2 years ago

dev for graph. up to date with trunk at r1684

File size: 5.2 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  {
22    if (!grid)
23      ERROR("CSourceFilter::CSourceFilter(CGrid* grid)",
24            "Impossible to construct a source filter without providing a grid.");
25  }
26
27  void CSourceFilter::buildGraph(CDataPacketPtr packet)
28  {
29    bool building_graph = this->tag ? packet->timestamp >= this->field->field_graph_start && packet->timestamp <= this->field->field_graph_end : false;
30    if(building_graph)
31    {
32      this->filterID = InvalidableObject::filterIdGenerator++; 
33      packet->src_filterID=this->filterID;
34      packet->field = this->field;
35      packet->distance = 1;
36     
37   
38      CWorkflowGraph::allocNodeEdge();
39
40      CWorkflowGraph::addNode(this->filterID, "Source Filter ", 1, 1, 0, packet);
41      (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].attributes = this->field->record4graphXiosAttributes();
42      (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].field_id = this->field->getId();
43      (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].distance = 1;
44
45      CWorkflowGraph::build_begin = true;
46    }
47
48  }
49
50
51  template <int N>
52  void CSourceFilter::streamData(CDate date, const CArray<double, N>& data)
53  {
54    date = date + offset; // this is a temporary solution, it should be part of a proper temporal filter
55
56    CDataPacketPtr packet(new CDataPacket);
57    packet->date = date;
58    packet->timestamp = date;
59    packet->status = CDataPacket::NO_ERROR;
60
61    packet->data.resize(grid->storeIndex_client.numElements());   
62   
63    if (compression)
64    {
65      packet->data = defaultValue;
66      grid->uncompressField(data, packet->data);   
67    }
68    else
69    {
70      if (mask)
71        grid->maskField(data, packet->data);
72      else
73        grid->inputField(data, packet->data);
74    }
75    // Convert missing values to NaN
76    if (hasMissingValue)
77    {
78      const double nanValue = std::numeric_limits<double>::quiet_NaN();
79      const size_t nbData = packet->data.numElements();
80      for (size_t idx = 0; idx < nbData; ++idx)
81      {
82        if (defaultValue == packet->data(idx))
83          packet->data(idx) = nanValue;
84      }
85    }
86
87    if(CXios::isClient) buildGraph(packet);
88   
89
90
91    onOutputReady(packet);
92  }
93
94  template void CSourceFilter::streamData<1>(CDate date, const CArray<double, 1>& data);
95  template void CSourceFilter::streamData<2>(CDate date, const CArray<double, 2>& data);
96  template void CSourceFilter::streamData<3>(CDate date, const CArray<double, 3>& data);
97  template void CSourceFilter::streamData<4>(CDate date, const CArray<double, 4>& data);
98  template void CSourceFilter::streamData<5>(CDate date, const CArray<double, 5>& data);
99  template void CSourceFilter::streamData<6>(CDate date, const CArray<double, 6>& data);
100  template void CSourceFilter::streamData<7>(CDate date, const CArray<double, 7>& data);
101
102  void CSourceFilter::streamDataFromServer(CDate date, const std::map<int, CArray<double, 1> >& data)
103  {
104    date = date + offset; // this is a temporary solution, it should be part of a proper temporal filter
105
106    CDataPacketPtr packet(new CDataPacket);
107    packet->date = date;
108    packet->timestamp = date;
109    packet->status = CDataPacket::NO_ERROR;
110   
111    if (data.size() != grid->storeIndex_fromSrv.size())
112      ERROR("CSourceFilter::streamDataFromServer(CDate date, const std::map<int, CArray<double, 1> >& data)",
113            << "Incoherent data received from servers,"
114            << " expected " << grid->storeIndex_fromSrv.size() << " chunks but " << data.size() << " were given.");
115
116    packet->data.resize(grid->storeIndex_client.numElements());
117    std::map<int, CArray<double, 1> >::const_iterator it, itEnd = data.end();
118    for (it = data.begin(); it != itEnd; it++)
119    {     
120      CArray<int,1>& index = grid->storeIndex_fromSrv[it->first];
121      for (int n = 0; n < index.numElements(); n++)
122        packet->data(index(n)) = it->second(n);
123    }
124
125    // Convert missing values to NaN
126    if (hasMissingValue)
127    {
128      const double nanValue = std::numeric_limits<double>::quiet_NaN();
129      const size_t nbData = packet->data.numElements();
130      for (size_t idx = 0; idx < nbData; ++idx)
131      {
132        if (defaultValue == packet->data(idx))
133          packet->data(idx) = nanValue;
134      }
135    }
136
137    onOutputReady(packet);
138  }
139
140  void CSourceFilter::signalEndOfStream(CDate date)
141  {
142    date = date + offset; // this is a temporary solution, it should be part of a proper temporal filter
143
144    CDataPacketPtr packet(new CDataPacket);
145    packet->date = date;
146    packet->timestamp = date;
147    packet->status = CDataPacket::END_OF_STREAM;
148    onOutputReady(packet);
149  }
150} // namespace xios
Note: See TracBrowser for help on using the repository browser.