source: XIOS/dev/dev_olga/src/filter/temporal_filter.cpp @ 1654

Last change on this file since 1654 was 1653, checked in by oabramkina, 5 years ago

Developments for visualization of XIOS workflow.

Branch is spawned from trunk r1649.

Boost library is used for producing Graphviz DOT files. Current results: a DOT file representing a static workflow. For a complete proof of concept, DOT files for each timestamp should be generated. The necessary information has been collected by XIOS, it only requires rearranging the information for graphing (changes in classes CWorkflowGraph and CGraphviz).

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