source: XIOS/dev/XIOS_DEV_CMIP6/src/filter/temporal_filter.cpp @ 1523

Last change on this file since 1523 was 1523, checked in by aclsce, 6 years ago

Modified to allow to run monthly simulations by using offsetMonth and offsetAllButMonth instead of samplingOffset.

File size: 4.3 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, 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*/)
12    : CFilter(gc, 1, this)
13    , functor(createFunctor(opId, ignoreMissingValue, tmpData))
14    , isOnceOperation(functor->timeType() == func::CFunctor::once)
15    , isInstantOperation(functor->timeType() == func::CFunctor::instant)
16    , samplingFreq(samplingFreq)
17    , samplingOffset(samplingOffset)
18    , opFreq(opFreq)
19    , offsetMonth(0, this->samplingOffset.month, 0, 0, 0, 0, 0)
20    , offsetAllButMonth(this->samplingOffset.year, 0 , this->samplingOffset.day,
21                        this->samplingOffset.hour, this->samplingOffset.minute,
22                        this->samplingOffset.second, this->samplingOffset.timestep)
23    , initDate(initDate)
24//    , nextSamplingDate(initDate + (this->samplingOffset + initDate.getRelCalendar().getTimeStep()))
25    , nextSamplingDate(initDate + offsetMonth + ( offsetAllButMonth + initDate.getRelCalendar().getTimeStep()))
26    , nbOperationDates(1)
27    , nbSamplingDates(0)
28//    , nextOperationDate(initDate + opFreq + this->samplingOffset)
29    , isFirstOperation(true)
30  {
31  }
32
33  CDataPacketPtr CTemporalFilter::apply(std::vector<CDataPacketPtr> data)
34  {
35    CDataPacketPtr packet;
36
37    if (data[0]->status != CDataPacket::END_OF_STREAM)
38    {
39      bool usePacket, outputResult, copyLess;
40      if (isOnceOperation)
41        usePacket = outputResult = copyLess = isFirstOperation;
42      else
43      {
44        usePacket = (data[0]->date >= nextSamplingDate);
45//        outputResult = (data[0]->date + samplingFreq > nextOperationDate);
46        outputResult = (data[0]->date  > initDate + nbOperationDates*opFreq - samplingFreq + offsetMonth + offsetAllButMonth);
47        copyLess = (isInstantOperation && usePacket && outputResult);
48      }
49
50      if (usePacket)
51      {
52        nbSamplingDates ++;
53        if (!copyLess)
54        {
55          if (!tmpData.numElements())
56            tmpData.resize(data[0]->data.numElements());
57
58          (*functor)(data[0]->data);
59        }
60
61        nextSamplingDate = ((initDate + offsetMonth) + nbSamplingDates * samplingFreq) + offsetAllButMonth + initDate.getRelCalendar().getTimeStep();
62      }
63
64      if (outputResult)
65      {
66        nbOperationDates ++;
67        if (!copyLess)
68        {
69          functor->final();
70
71          packet = CDataPacketPtr(new CDataPacket);
72          packet->date = data[0]->date;
73          packet->timestamp = data[0]->timestamp;
74          packet->status = data[0]->status;
75          packet->data.resize(tmpData.numElements());
76          packet->data = tmpData;
77        }
78        else
79          packet = data[0];
80
81        isFirstOperation = false;
82//        nextOperationDate = initDate + samplingFreq + nbOperationDates*opFreq - samplingFreq + offsetMonth + offsetAllButMonth;
83      }
84    }
85
86    return packet;
87  }
88
89  bool CTemporalFilter::mustAutoTrigger() const
90  {
91    return true;
92  }
93
94  bool CTemporalFilter::isDataExpected(const CDate& date) const
95  {
96//    return isOnceOperation ? isFirstOperation : (date >= nextSamplingDate || date + samplingFreq > nextOperationDate);
97    return isOnceOperation ? isFirstOperation : (date >= nextSamplingDate || date > initDate + nbOperationDates*opFreq - samplingFreq + offsetMonth + offsetAllButMonth);
98  }
99
100  static func::CFunctor* createFunctor(const std::string& opId, bool ignoreMissingValue, CArray<double, 1>& tmpData)
101  {
102    func::CFunctor* functor = NULL;
103
104    double defaultValue = std::numeric_limits<double>::quiet_NaN();
105
106#define DECLARE_FUNCTOR(MType, mtype) \
107    if (opId.compare(#mtype) == 0) \
108    { \
109      if (ignoreMissingValue) \
110      { \
111        functor = new func::C##MType(tmpData, defaultValue); \
112      } \
113      else \
114      { \
115        functor = new func::C##MType(tmpData); \
116      } \
117    }
118
119#include "functor_type.conf"
120
121    if (!functor)
122      ERROR("createFunctor(const std::string& opId, ...)",
123            << "\"" << opId << "\" is not a valid operation.");
124
125    return functor;
126  }
127} // namespace xios
Note: See TracBrowser for help on using the repository browser.