source: XIOS/trunk/src/filter/temporal_filter.cpp @ 1122

Last change on this file since 1122 was 1122, checked in by rlacroix, 7 years ago

CTemporalFilter: Improve const-correctness.

File size: 3.0 KB
Line 
1#include "temporal_filter.hpp"
2#include "functor_type.hpp"
3#include "calendar_util.hpp"
4
5namespace xios
6{
7  static func::CFunctor* createFunctor(const std::string& opId, bool ignoreMissingValue, double missingValue, CArray<double, 1>& tmpData);
8
9  CTemporalFilter::CTemporalFilter(CGarbageCollector& gc, const std::string& opId,
10                                   const CDate& initDate, const CDuration samplingFreq, const CDuration samplingOffset, const CDuration opFreq,
11                                   bool ignoreMissingValue /*= false*/, double missingValue /*= 0.0*/)
12    : CFilter(gc, 1, this)
13    , functor(createFunctor(opId, ignoreMissingValue, missingValue, tmpData))
14    , samplingFreq(samplingFreq)
15    , opFreq(opFreq)
16    , nextSamplingDate(initDate + samplingOffset + initDate.getRelCalendar().getTimeStep())
17    , nextOperationDate(initDate + samplingOffset + opFreq)
18    , isFirstOperation(true)
19    , isOnceOperation(functor->timeType() == func::CFunctor::once)
20  {
21  }
22
23  CDataPacketPtr CTemporalFilter::apply(std::vector<CDataPacketPtr> data)
24  {
25    CDataPacketPtr packet;
26
27    if (data[0]->status != CDataPacket::END_OF_STREAM)
28    {
29      const bool usePacket = isOnceOperation ? isFirstOperation : (data[0]->date >= nextSamplingDate);
30      if (usePacket)
31      {
32        if (!tmpData.numElements())
33          tmpData.resize(data[0]->data.numElements());
34
35        (*functor)(data[0]->data);
36
37        nextSamplingDate = nextSamplingDate + samplingFreq;
38      }
39
40      const bool outputResult = isOnceOperation ? isFirstOperation : (data[0]->date + samplingFreq > nextOperationDate);
41      if (outputResult)
42      {
43        functor->final();
44
45        packet = CDataPacketPtr(new CDataPacket);
46        packet->date = data[0]->date;
47        packet->timestamp = data[0]->timestamp;
48        packet->status = data[0]->status;
49        packet->data.resize(tmpData.numElements());
50        packet->data = tmpData;
51
52        isFirstOperation = false;
53        nextOperationDate = nextOperationDate + samplingFreq + opFreq - samplingFreq;
54      }
55    }
56
57    return packet;
58  }
59
60  bool CTemporalFilter::isDataExpected(const CDate& date) const
61  {
62    return isOnceOperation ? isFirstOperation : (date >= nextSamplingDate || date + samplingFreq > nextOperationDate);
63  }
64
65  static func::CFunctor* createFunctor(const std::string& opId, bool ignoreMissingValue, double missingValue, CArray<double, 1>& tmpData)
66  {
67    func::CFunctor* functor = NULL;
68
69    double defaultValue = ignoreMissingValue ? std::numeric_limits<double>::quiet_NaN() : missingValue;
70
71#define DECLARE_FUNCTOR(MType, mtype) \
72    if (opId.compare(#mtype) == 0) \
73    { \
74      if (ignoreMissingValue) \
75      { \
76        functor = new func::C##MType(tmpData, defaultValue); \
77      } \
78      else \
79      { \
80        functor = new func::C##MType(tmpData); \
81      } \
82    }
83
84#include "functor_type.conf"
85
86    if (!functor)
87      ERROR("createFunctor(const std::string& opId, ...)",
88            << "\"" << opId << "\" is not a valid operation.");
89
90    return functor;
91  }
92} // namespace xios
Note: See TracBrowser for help on using the repository browser.