source: XIOS/dev/XIOS_DEV_CMIP6/src/filter/store_filter.cpp @ 1358

Last change on this file since 1358 was 1358, checked in by rlacroix, 6 years ago

Support reentrant workflows and workflows with temporal integration for fields read from files.

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