source: XIOS/trunk/src/filter/store_filter.cpp @ 1852

Last change on this file since 1852 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.4 KB
Line 
1#include "store_filter.hpp"
2#include "context.hpp"
3#include "grid.hpp"
4#include "timer.hpp"
5#include "file.hpp"
6
7namespace xios
8{
9  CStoreFilter::CStoreFilter(CGarbageCollector& gc, CContext* context, CGrid* grid,
10                             bool detectMissingValues /*= false*/, double missingValue /*= 0.0*/)
11    : CInputPin(gc, 1)
12    , gc(gc)
13    , context(context)
14    , grid(grid)
15    , detectMissingValues(detectMissingValues)
16    , missingValue(missingValue)
17  {
18    if (!context)
19      ERROR("CStoreFilter::CStoreFilter(CContext* context, CGrid* grid)",
20            "Impossible to construct a store filter without providing a context.");
21    if (!grid)
22      ERROR("CStoreFilter::CStoreFilter(CContext* context, CGrid* grid)",
23            "Impossible to construct a store filter without providing a grid.");
24  }
25
26  CConstDataPacketPtr CStoreFilter::getPacket(Time timestamp)
27  {
28    CTimer timer("CStoreFilter::getPacket");
29    CConstDataPacketPtr packet;
30    const double timeout = CXios::recvFieldTimeout;
31
32    do
33    {
34      if (canBeTriggered())
35        trigger(timestamp);
36
37      timer.resume();
38
39      std::map<Time, CDataPacketPtr>::const_iterator it = packets.find(timestamp);
40      if (it != packets.end())
41        packet = it->second;
42      else // if the packet is not available yet, check if it can be received
43        context->checkBuffersAndListen();
44
45      timer.suspend();
46    } while (!packet && timer.getCumulatedTime() < timeout);
47
48    if (!packet)
49    {
50      std::map<Time, CDataPacketPtr>::const_iterator it ;
51      info(0)<<"Impossible to get the packet with timestamp = " << timestamp<<std::endl<<"Available timestamp are : "<<std::endl ;
52      for(it=packets.begin();it!=packets.end();++it) info(0)<<it->first<<"  ";
53      info(0)<<std::endl ;
54      ERROR("CConstDataPacketPtr CStoreFilter::getPacket(Time timestamp) const",
55            << "Impossible to get the packet with timestamp = " << timestamp);
56    }
57    return packet;
58  }
59
60  template <int N>
61  CDataPacket::StatusCode CStoreFilter::getData(Time timestamp, CArray<double, N>& data)
62  {
63    CConstDataPacketPtr packet = getPacket(timestamp);
64
65    if (packet->status == CDataPacket::NO_ERROR)
66      grid->outputField(packet->data, data);
67
68    return packet->status;
69  }
70
71  template CDataPacket::StatusCode CStoreFilter::getData<1>(Time timestamp, CArray<double, 1>& data);
72  template CDataPacket::StatusCode CStoreFilter::getData<2>(Time timestamp, CArray<double, 2>& data);
73  template CDataPacket::StatusCode CStoreFilter::getData<3>(Time timestamp, CArray<double, 3>& data);
74  template CDataPacket::StatusCode CStoreFilter::getData<4>(Time timestamp, CArray<double, 4>& data);
75  template CDataPacket::StatusCode CStoreFilter::getData<5>(Time timestamp, CArray<double, 5>& data);
76  template CDataPacket::StatusCode CStoreFilter::getData<6>(Time timestamp, CArray<double, 6>& data);
77  template CDataPacket::StatusCode CStoreFilter::getData<7>(Time timestamp, CArray<double, 7>& data);
78
79  void CStoreFilter::buildGraph(std::vector<CDataPacketPtr> data)
80  {
81    bool building_graph = this->tag ? data[0]->timestamp >= this->start_graph && data[0]->timestamp <= this->end_graph : false;
82
83    if(building_graph)
84    {
85      this->filterID = InvalidableObject::filterIdGenerator++;
86      int edgeID = InvalidableObject::edgeIdGenerator++;
87
88      CWorkflowGraph::allocNodeEdge();
89 
90      CWorkflowGraph::addNode(this->filterID, "Store Filter", 7, 0, 1, data[0]);
91      (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].distance = ++(data[0]->distance);
92      (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].attributes = this->field->record4graphXiosAttributes();
93      if(this->field->file) (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].attributes += "</br>file attributes : </br>" +this->field->file->record4graphXiosAttributes();
94
95      // if(CXios::isClient) std::cout<<"CStoreFilter::apply filter tag = "<<this->tag<<std::endl;
96
97      if(CXios::isClient && CWorkflowGraph::build_begin) 
98      {
99        CWorkflowGraph::addEdge(edgeID, this->filterID, data[0]);;
100        (*CWorkflowGraph::mapFilters_ptr_with_info)[data[0]->src_filterID].filter_filled = 0;
101      }
102      else CWorkflowGraph::build_begin = true;
103    }
104  }
105
106  void CStoreFilter::onInputReady(std::vector<CDataPacketPtr> data)
107  {
108    buildGraph(data);
109
110    CDataPacketPtr packet;
111    if (detectMissingValues)
112    {
113      const size_t nbData = data[0]->data.numElements();
114
115      packet = CDataPacketPtr(new CDataPacket);
116      packet->date = data[0]->date;
117      packet->timestamp = data[0]->timestamp;
118      packet->status = data[0]->status;
119      packet->data.resize(nbData);
120      packet->data = data[0]->data;
121
122      for (size_t idx = 0; idx < nbData; ++idx)
123      {
124        if (NumTraits<double>::isNan(packet->data(idx)))
125          packet->data(idx) = missingValue;
126      }
127
128    }
129
130    else
131    {
132      packet = data[0];
133    }
134
135    packets.insert(std::make_pair(packet->timestamp, packet));
136    // The packet is always destroyed by the garbage collector
137    // so we register but never unregister
138    gc.registerObject(this, packet->timestamp);
139
140  }
141
142  bool CStoreFilter::mustAutoTrigger() const
143  {
144    return false;
145  }
146
147  bool CStoreFilter::isDataExpected(const CDate& date) const
148  {
149    return true;
150  }
151
152  void CStoreFilter::invalidate(Time timestamp)
153  {
154    CInputPin::invalidate(timestamp);
155    packets.erase(packets.begin(), packets.lower_bound(timestamp));
156  }
157} // namespace xios
Note: See TracBrowser for help on using the repository browser.