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

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

Big commit on graph functionality. Add buildWorkflowGraph function for filters

File size: 5.4 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  {
33  }
34
35  void CTemporalFilter::buildWorkflowGraph(std::vector<CDataPacketPtr> data)
36  {
37    if(this->graphEnabled )
38    {
39      if(!data[0]->graphPackage)
40      {
41        data[0]->graphPackage = new CGraphDataPackage;
42      }
43     
44      if(graphCycleCompleted)
45      { 
46        this->graphPackage->filterId = CWorkflowGraph::getNodeSize();
47        CWorkflowGraph::addNode("Temporal filter", 3, false, 0, data[0]);
48        graphCycleCompleted = false;
49      }
50     
51      data[0]->graphPackage->currentField = this->graphPackage->inFields[0];
52      std::rotate(this->graphPackage->inFields.begin(), this->graphPackage->inFields.begin() + 1, this->graphPackage->inFields.end());
53     
54      CWorkflowGraph::addEdge(data[0]->graphPackage->fromFilter, this->graphPackage->filterId, data[0]);
55      data[0]->graphPackage->fromFilter = this->graphPackage->filterId;
56      this->graphPackage->sourceFilterIds.push_back(data[0]->graphPackage->fromFilter); 
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
61  }
62  CDataPacketPtr CTemporalFilter::apply(std::vector<CDataPacketPtr> data)
63  {
64    buildWorkflowGraph(data);
65 
66    CDataPacketPtr packet;
67
68    if (data[0]->status != CDataPacket::END_OF_STREAM)
69    {
70      bool usePacket, outputResult, copyLess;
71      if (isOnceOperation)
72        usePacket = outputResult = copyLess = isFirstOperation;
73      else
74      {
75        usePacket = (data[0]->date >= nextSamplingDate);
76        outputResult = (data[0]->date  > initDate + nbOperationDates*opFreq - samplingFreq + offsetMonth + offsetAllButMonth);
77        copyLess = (isInstantOperation && usePacket && outputResult);
78      }
79
80      if (usePacket)
81      {
82        nbSamplingDates ++;
83        if (!copyLess)
84        {
85          if (!tmpData.numElements())
86            tmpData.resize(data[0]->data.numElements());
87
88          (*functor)(data[0]->data);
89        }
90
91        nextSamplingDate = ((initDate + offsetMonth) + nbSamplingDates * samplingFreq) + offsetAllButMonth + initDate.getRelCalendar().getTimeStep();
92      }
93
94      if (outputResult)
95      {
96        nbOperationDates ++;
97        if (!copyLess)
98        {
99          functor->final();
100
101          packet = CDataPacketPtr(new CDataPacket);
102          packet->date = data[0]->date;
103          packet->timestamp = data[0]->timestamp;
104          packet->status = data[0]->status;
105          packet->data.resize(tmpData.numElements());
106          packet->data = tmpData;
107          packet->graphPackage = data[0]->graphPackage;
108        }
109        else
110          packet = data[0];
111
112        isFirstOperation = false;
113        graphCycleCompleted = true;
114     }
115    }
116
117    return packet;
118  }
119
120  bool CTemporalFilter::mustAutoTrigger() const
121  {
122    return true;
123  }
124
125  bool CTemporalFilter::isDataExpected(const CDate& date) const
126  {
127//    return isOnceOperation ? isFirstOperation : (date >= nextSamplingDate || date + samplingFreq > nextOperationDate);
128    return isOnceOperation ? isFirstOperation : (date >= nextSamplingDate || date > initDate + nbOperationDates*opFreq - samplingFreq + offsetMonth + offsetAllButMonth);
129  }
130
131  static func::CFunctor* createFunctor(const std::string& opId, bool ignoreMissingValue, CArray<double, 1>& tmpData)
132  {
133    func::CFunctor* functor = NULL;
134
135    double defaultValue = std::numeric_limits<double>::quiet_NaN();
136
137#define DECLARE_FUNCTOR(MType, mtype) \
138    if (opId.compare(#mtype) == 0) \
139    { \
140      if (ignoreMissingValue) \
141      { \
142        functor = new func::C##MType(tmpData, defaultValue); \
143      } \
144      else \
145      { \
146        functor = new func::C##MType(tmpData); \
147      } \
148    }
149
150#include "functor_type.conf"
151
152    if (!functor)
153      ERROR("createFunctor(const std::string& opId, ...)",
154            << "\"" << opId << "\" is not a valid operation.");
155
156    return functor;
157  }
158} // namespace xios
Note: See TracBrowser for help on using the repository browser.