source: XIOS/dev/dev_ym/XIOS_COUPLING/src/filter/store_filter.cpp @ 1853

Last change on this file since 1853 was 1761, checked in by ymipsl, 5 years ago

implementing first guess for service functionnalities.

YM

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