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

Last change on this file since 854 was 854, checked in by ymipsl, 6 years ago

Correct bug in output using freq_op != 1ts and Gregorian calendar.

YM

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