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

Last change on this file since 1606 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
RevLine 
[643]1#include "temporal_filter.hpp"
2#include "functor_type.hpp"
3#include "calendar_util.hpp"
4
5namespace xios
6{
[1440]7  static func::CFunctor* createFunctor(const std::string& opId, bool ignoreMissingValue, CArray<double, 1>& tmpData);
[1158]8
[643]9  CTemporalFilter::CTemporalFilter(CGarbageCollector& gc, const std::string& opId,
10                                   const CDate& initDate, const CDuration samplingFreq, const CDuration samplingOffset, const CDuration opFreq,
[1440]11                                   bool ignoreMissingValue /*= false*/)
[643]12    : CFilter(gc, 1, this)
[1440]13    , functor(createFunctor(opId, ignoreMissingValue, tmpData))
[1158]14    , isOnceOperation(functor->timeType() == func::CFunctor::once)
15    , isInstantOperation(functor->timeType() == func::CFunctor::instant)
[1278]16    , samplingFreq(samplingFreq)
17    , samplingOffset(samplingOffset)
[643]18    , opFreq(opFreq)
[1472]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)
[1302]23    , initDate(initDate)
[1523]24//    , nextSamplingDate(initDate + (this->samplingOffset + initDate.getRelCalendar().getTimeStep()))
25    , nextSamplingDate(initDate + offsetMonth + ( offsetAllButMonth + initDate.getRelCalendar().getTimeStep()))
[1302]26    , nbOperationDates(1)
[1473]27    , nbSamplingDates(0)
[1302]28//    , nextOperationDate(initDate + opFreq + this->samplingOffset)
[643]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    {
[1158]39      bool usePacket, outputResult, copyLess;
40      if (isOnceOperation)
41        usePacket = outputResult = copyLess = isFirstOperation;
42      else
43      {
44        usePacket = (data[0]->date >= nextSamplingDate);
[1302]45//        outputResult = (data[0]->date + samplingFreq > nextOperationDate);
46        outputResult = (data[0]->date  > initDate + nbOperationDates*opFreq - samplingFreq + offsetMonth + offsetAllButMonth);
[1158]47        copyLess = (isInstantOperation && usePacket && outputResult);
48      }
49
[643]50      if (usePacket)
51      {
[1473]52        nbSamplingDates ++;
[1158]53        if (!copyLess)
54        {
55          if (!tmpData.numElements())
56            tmpData.resize(data[0]->data.numElements());
[643]57
[1158]58          (*functor)(data[0]->data);
59        }
[643]60
[1473]61        nextSamplingDate = ((initDate + offsetMonth) + nbSamplingDates * samplingFreq) + offsetAllButMonth + initDate.getRelCalendar().getTimeStep();
[643]62      }
63
64      if (outputResult)
65      {
[1302]66        nbOperationDates ++;
[1158]67        if (!copyLess)
68        {
69          functor->final();
[643]70
[1158]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];
[643]80
[1285]81        isFirstOperation = false;
[1302]82//        nextOperationDate = initDate + samplingFreq + nbOperationDates*opFreq - samplingFreq + offsetMonth + offsetAllButMonth;
[643]83      }
84    }
85
86    return packet;
87  }
[1158]88
[1358]89  bool CTemporalFilter::mustAutoTrigger() const
90  {
91    return true;
92  }
93
[1158]94  bool CTemporalFilter::isDataExpected(const CDate& date) const
95  {
[1302]96//    return isOnceOperation ? isFirstOperation : (date >= nextSamplingDate || date + samplingFreq > nextOperationDate);
97    return isOnceOperation ? isFirstOperation : (date >= nextSamplingDate || date > initDate + nbOperationDates*opFreq - samplingFreq + offsetMonth + offsetAllButMonth);
[1158]98  }
99
[1440]100  static func::CFunctor* createFunctor(const std::string& opId, bool ignoreMissingValue, CArray<double, 1>& tmpData)
[1158]101  {
102    func::CFunctor* functor = NULL;
103
[1440]104    double defaultValue = std::numeric_limits<double>::quiet_NaN();
[1158]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  }
[643]127} // namespace xios
Note: See TracBrowser for help on using the repository browser.