source: XIOS/dev/dev_olga/src/filter/store_filter.cpp @ 1654

Last change on this file since 1654 was 1654, checked in by oabramkina, 3 years ago

Corrections for workflow visualization.

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