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

Last change on this file since 1756 was 1704, checked in by yushan, 5 years ago

Introducing the new graph functionality. Attribute build_workflow_graph=.TRUE. is used in the field definition section in the xml file to enable the workflow graph of the field and other fields referecing to it. A more detailed document will be available soon on the graph fuctionality.

File size: 5.2 KB
RevLine 
[638]1#include "source_filter.hpp"
2#include "grid.hpp"
3#include "exception.hpp"
[756]4#include "calendar_util.hpp"
[1158]5#include <limits>
[1704]6#include "workflow_graph.hpp"
[638]7
8namespace xios
9{
[1637]10  CSourceFilter::CSourceFilter(CGarbageCollector& gc, CGrid* grid,
11                               bool compression /*= true*/, bool mask /*= false*/,
[1158]12                               const CDuration offset /*= NoneDu*/, bool manualTrigger /*= false*/,
13                               bool hasMissingValue /*= false*/,
14                               double defaultValue /*= 0.0*/)
[1021]15    : COutputPin(gc, manualTrigger)
16    , grid(grid)
[1241]17    , compression(compression)
[1637]18    , mask(mask)
[756]19    , offset(offset)
[1158]20    , hasMissingValue(hasMissingValue), defaultValue(defaultValue)
[638]21  {
22    if (!grid)
23      ERROR("CSourceFilter::CSourceFilter(CGrid* grid)",
24            "Impossible to construct a source filter without providing a grid.");
25  }
26
[1704]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
[638]51  template <int N>
[643]52  void CSourceFilter::streamData(CDate date, const CArray<double, N>& data)
[638]53  {
[756]54    date = date + offset; // this is a temporary solution, it should be part of a proper temporal filter
55
[638]56    CDataPacketPtr packet(new CDataPacket);
[643]57    packet->date = date;
58    packet->timestamp = date;
[638]59    packet->status = CDataPacket::NO_ERROR;
60
[1250]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
[1637]69    {
70      if (mask)
71        grid->maskField(data, packet->data);
72      else
73        grid->inputField(data, packet->data);
74    }
[1158]75    // Convert missing values to NaN
76    if (hasMissingValue)
77    {
[1201]78      const double nanValue = std::numeric_limits<double>::quiet_NaN();
79      const size_t nbData = packet->data.numElements();
[1158]80      for (size_t idx = 0; idx < nbData; ++idx)
81      {
82        if (defaultValue == packet->data(idx))
83          packet->data(idx) = nanValue;
84      }
85    }
86
[1704]87    if(CXios::isClient) buildGraph(packet);
88   
89
90
[1021]91    onOutputReady(packet);
[638]92  }
93
[643]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);
[932]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);
[638]101
[643]102  void CSourceFilter::streamDataFromServer(CDate date, const std::map<int, CArray<double, 1> >& data)
[638]103  {
[756]104    date = date + offset; // this is a temporary solution, it should be part of a proper temporal filter
105
[638]106    CDataPacketPtr packet(new CDataPacket);
[643]107    packet->date = date;
108    packet->timestamp = date;
[638]109    packet->status = CDataPacket::NO_ERROR;
[1249]110   
[1021]111    if (data.size() != grid->storeIndex_fromSrv.size())
[643]112      ERROR("CSourceFilter::streamDataFromServer(CDate date, const std::map<int, CArray<double, 1> >& data)",
[638]113            << "Incoherent data received from servers,"
[1021]114            << " expected " << grid->storeIndex_fromSrv.size() << " chunks but " << data.size() << " were given.");
[638]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++)
[1249]119    {     
[1021]120      CArray<int,1>& index = grid->storeIndex_fromSrv[it->first];
[638]121      for (int n = 0; n < index.numElements(); n++)
122        packet->data(index(n)) = it->second(n);
123    }
124
[1201]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
[1021]137    onOutputReady(packet);
[638]138  }
139
[643]140  void CSourceFilter::signalEndOfStream(CDate date)
[638]141  {
[1210]142    date = date + offset; // this is a temporary solution, it should be part of a proper temporal filter
143
[638]144    CDataPacketPtr packet(new CDataPacket);
[643]145    packet->date = date;
146    packet->timestamp = date;
[638]147    packet->status = CDataPacket::END_OF_STREAM;
[1021]148    onOutputReady(packet);
[638]149  }
150} // namespace xios
Note: See TracBrowser for help on using the repository browser.