source: XIOS/dev/dev_ym/XIOS_COUPLING/src/filter/temporal_filter.cpp @ 2193

Last change on this file since 2193 was 2193, checked in by yushan, 3 years ago

workflow graph : enable unary and binary arithmetic filters

File size: 5.6 KB
Line 
1#include "temporal_filter.hpp"
2#include "functor_type.hpp"
3#include "calendar_util.hpp"
4#include "workflow_graph.hpp"
5
6namespace xios
7{
8  static func::CFunctor* createFunctor(const std::string& opId, bool ignoreMissingValue, CArray<double, 1>& tmpData);
9
10  CTemporalFilter::CTemporalFilter(CGarbageCollector& gc, const std::string& opId,
11                                   const CDate& initDate, const CDuration samplingFreq, const CDuration samplingOffset, const CDuration opFreq,
12                                   bool ignoreMissingValue /*= false*/)
13    : CFilter(gc, 1, this)
14    , functor(createFunctor(opId, ignoreMissingValue, tmpData))
15    , isOnceOperation(functor->timeType() == func::CFunctor::once)
16    , isInstantOperation(functor->timeType() == func::CFunctor::instant)
17    , samplingFreq(samplingFreq)
18    , samplingOffset(samplingOffset)
19    , opFreq(opFreq)
20    , offsetMonth(0, this->samplingOffset.month, 0, 0, 0, 0, 0)
21    , offsetAllButMonth(this->samplingOffset.year, 0 , this->samplingOffset.day,
22                        this->samplingOffset.hour, this->samplingOffset.minute,
23                        this->samplingOffset.second, this->samplingOffset.timestep)
24    , initDate(initDate)
25//    , nextSamplingDate(initDate + (this->samplingOffset + initDate.getRelCalendar().getTimeStep()))
26    , nextSamplingDate(initDate + offsetMonth + ( offsetAllButMonth + initDate.getRelCalendar().getTimeStep()))
27    , nbOperationDates(1)
28    , nbSamplingDates(0)
29//    , nextOperationDate(initDate + opFreq + this->samplingOffset)
30    , isFirstOperation(true)
31    , graphCycleCompleted(true)
32    , temporalOperation(opId)
33  {
34  }
35
36  std::string CTemporalFilter::getTemporalOperation()
37  {
38    return this->temporalOperation;
39  }
40
41  void CTemporalFilter::buildWorkflowGraph(std::vector<CDataPacketPtr> data)
42  {
43    if(this->graphEnabled )
44    {
45      if(!data[0]->graphPackage)
46      {
47        data[0]->graphPackage = new CGraphDataPackage;
48      }
49     
50      if(graphCycleCompleted)
51      { 
52        this->graphPackage->filterId = CWorkflowGraph::getNodeSize();
53        CWorkflowGraph::addNode("Temporal filter \\n("+getTemporalOperation()+")", 3, false, 0, data[0]);
54        graphCycleCompleted = false;
55      }
56     
57      data[0]->graphPackage->currentField = this->graphPackage->inFields[0];
58      std::rotate(this->graphPackage->inFields.begin(), this->graphPackage->inFields.begin() + 1, this->graphPackage->inFields.end());
59     
60      CWorkflowGraph::addEdge(data[0]->graphPackage->fromFilter, this->graphPackage->filterId, data[0]);
61      data[0]->graphPackage->fromFilter = this->graphPackage->filterId;
62      // this->graphPackage->sourceFilterIds.push_back(data[0]->graphPackage->fromFilter);
63      data[0]->graphPackage->currentField = this->graphPackage->inFields[0];
64      std::rotate(this->graphPackage->inFields.begin(), this->graphPackage->inFields.begin() + 1, this->graphPackage->inFields.end());
65    }
66
67  }
68  CDataPacketPtr CTemporalFilter::apply(std::vector<CDataPacketPtr> data)
69  {
70    buildWorkflowGraph(data);
71 
72    CDataPacketPtr packet;
73
74    if (data[0]->status != CDataPacket::END_OF_STREAM)
75    {
76      bool usePacket, outputResult, copyLess;
77      if (isOnceOperation)
78        usePacket = outputResult = copyLess = isFirstOperation;
79      else
80      {
81        usePacket = (data[0]->date >= nextSamplingDate);
82        outputResult = (data[0]->date  > initDate + nbOperationDates*opFreq - samplingFreq + offsetMonth + offsetAllButMonth);
83        copyLess = (isInstantOperation && usePacket && outputResult);
84      }
85
86      if (usePacket)
87      {
88        nbSamplingDates ++;
89        if (!copyLess)
90        {
91          if (!tmpData.numElements())
92            tmpData.resize(data[0]->data.numElements());
93
94          (*functor)(data[0]->data);
95        }
96
97        nextSamplingDate = ((initDate + offsetMonth) + nbSamplingDates * samplingFreq) + offsetAllButMonth + initDate.getRelCalendar().getTimeStep();
98      }
99
100      if (outputResult)
101      {
102        nbOperationDates ++;
103        if (!copyLess)
104        {
105          functor->final();
106
107          packet = CDataPacketPtr(new CDataPacket);
108          packet->date = data[0]->date;
109          packet->timestamp = data[0]->timestamp;
110          packet->status = data[0]->status;
111          packet->data.resize(tmpData.numElements());
112          packet->data = tmpData;
113          packet->graphPackage = data[0]->graphPackage;
114        }
115        else
116          packet = data[0];
117
118        isFirstOperation = false;
119        graphCycleCompleted = true;
120     }
121    }
122
123    return packet;
124  }
125
126  bool CTemporalFilter::mustAutoTrigger() const
127  {
128    return true;
129  }
130
131  bool CTemporalFilter::isDataExpected(const CDate& date) const
132  {
133//    return isOnceOperation ? isFirstOperation : (date >= nextSamplingDate || date + samplingFreq > nextOperationDate);
134    return isOnceOperation ? isFirstOperation : (date >= nextSamplingDate || date > initDate + nbOperationDates*opFreq - samplingFreq + offsetMonth + offsetAllButMonth);
135  }
136
137  static func::CFunctor* createFunctor(const std::string& opId, bool ignoreMissingValue, CArray<double, 1>& tmpData)
138  {
139    func::CFunctor* functor = NULL;
140
141    double defaultValue = std::numeric_limits<double>::quiet_NaN();
142
143#define DECLARE_FUNCTOR(MType, mtype) \
144    if (opId.compare(#mtype) == 0) \
145    { \
146      if (ignoreMissingValue) \
147      { \
148        functor = new func::C##MType(tmpData, defaultValue); \
149      } \
150      else \
151      { \
152        functor = new func::C##MType(tmpData); \
153      } \
154    }
155
156#include "functor_type.conf"
157
158    if (!functor)
159      ERROR("createFunctor(const std::string& opId, ...)",
160            << "\"" << opId << "\" is not a valid operation.");
161
162    return functor;
163  }
164} // namespace xios
Note: See TracBrowser for help on using the repository browser.