source: XIOS/dev/dev_trunk_omp/src/filter/store_filter.cpp @ 1668

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

MARK: branch merged with trunk @1663. static graph OK with EP

File size: 4.3 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  }
24
25  CConstDataPacketPtr CStoreFilter::getPacket(Time timestamp)
26  {
27    CTimer timer("CStoreFilter::getPacket");
28    CConstDataPacketPtr packet;
29    const double timeout = CXios::recvFieldTimeout;
30
31    do
32    {
33      if (canBeTriggered())
34        trigger(timestamp);
35
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)
48    {
49      std::map<Time, CDataPacketPtr>::const_iterator it ;
50      #pragma omp critical (_output)
51      {
52        info(0)<<"Impossible to get the packet with timestamp = " << timestamp<<std::endl<<"Available timestamp are : "<<std::endl ;
53      }
54      for(it=packets.begin();it!=packets.end();++it)
55      {
56        #pragma omp critical (_output)
57        {
58          info(0)<<it->first<<"  ";
59        }
60      }
61      #pragma omp critical (_output)
62      { 
63        info(0)<<std::endl ;
64      }
65      ERROR("CConstDataPacketPtr CStoreFilter::getPacket(Time timestamp) const",
66            << "Impossible to get the packet with timestamp = " << timestamp);
67    }
68    return packet;
69  }
70
71  template <int N>
72  CDataPacket::StatusCode CStoreFilter::getData(Time timestamp, CArray<double, N>& data)
73  {
74    CConstDataPacketPtr packet = getPacket(timestamp);
75
76    if (packet->status == CDataPacket::NO_ERROR)
77      grid->outputField(packet->data, data);
78
79    return packet->status;
80  }
81
82  template CDataPacket::StatusCode CStoreFilter::getData<1>(Time timestamp, CArray<double, 1>& data);
83  template CDataPacket::StatusCode CStoreFilter::getData<2>(Time timestamp, CArray<double, 2>& data);
84  template CDataPacket::StatusCode CStoreFilter::getData<3>(Time timestamp, CArray<double, 3>& data);
85  template CDataPacket::StatusCode CStoreFilter::getData<4>(Time timestamp, CArray<double, 4>& data);
86  template CDataPacket::StatusCode CStoreFilter::getData<5>(Time timestamp, CArray<double, 5>& data);
87  template CDataPacket::StatusCode CStoreFilter::getData<6>(Time timestamp, CArray<double, 6>& data);
88  template CDataPacket::StatusCode CStoreFilter::getData<7>(Time timestamp, CArray<double, 7>& data);
89
90  void CStoreFilter::onInputReady(std::vector<CDataPacketPtr> data)
91  {
92
93    CDataPacketPtr packet;
94    if (detectMissingValues)
95    {
96      const size_t nbData = data[0]->data.numElements();
97
98      packet = CDataPacketPtr(new CDataPacket);
99      packet->date = data[0]->date;
100      packet->timestamp = data[0]->timestamp;
101      packet->status = data[0]->status;
102      packet->data.resize(nbData);
103      packet->data = data[0]->data;
104
105      for (size_t idx = 0; idx < nbData; ++idx)
106      {
107        if (NumTraits<double>::isNan(packet->data(idx)))
108          packet->data(idx) = missingValue;
109      }
110
111    }
112
113    else
114    {
115      packet = data[0];
116    }
117
118    packets.insert(std::make_pair(packet->timestamp, packet));
119    // The packet is always destroyed by the garbage collector
120    // so we register but never unregister
121    gc.registerObject(this, packet->timestamp);
122
123  }
124
125  bool CStoreFilter::mustAutoTrigger() const
126  {
127    return false;
128  }
129
130  bool CStoreFilter::isDataExpected(const CDate& date) const
131  {
132    return true;
133  }
134
135  void CStoreFilter::invalidate(Time timestamp)
136  {
137    CInputPin::invalidate(timestamp);
138    packets.erase(packets.begin(), packets.lower_bound(timestamp));
139  }
140
141  int CStoreFilter::getFilterId(void)
142  {
143    return filterId;
144  }
145
146} // namespace xios
Note: See TracBrowser for help on using the repository browser.