source: XIOS/dev/dev_trunk_graph/src/filter/temporal_filter.cpp @ 2020

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

Graph intermedia commit to a tmp branch

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