1 | #include "temporal_filter.hpp" |
---|
2 | #include "functor_type.hpp" |
---|
3 | #include "calendar_util.hpp" |
---|
4 | #include "workflow_graph.hpp" |
---|
5 | |
---|
6 | namespace 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 | |
---|
69 | CDataPacketPtr CTemporalFilter::apply(std::vector<CDataPacketPtr> data) |
---|
70 | { |
---|
71 | buildWorkflowGraph(data); |
---|
72 | |
---|
73 | CDataPacketPtr packet; |
---|
74 | |
---|
75 | if (data[0]->status != CDataPacket::END_OF_STREAM) |
---|
76 | { |
---|
77 | bool usePacket, outputResult, copyLess; |
---|
78 | if (isOnceOperation) |
---|
79 | usePacket = outputResult = copyLess = isFirstOperation; |
---|
80 | else |
---|
81 | { |
---|
82 | usePacket = (data[0]->date >= nextSamplingDate); |
---|
83 | outputResult = (data[0]->date > initDate + nbOperationDates*opFreq - samplingFreq + offsetMonth + offsetAllButMonth); |
---|
84 | copyLess = (isInstantOperation && usePacket && outputResult); |
---|
85 | } |
---|
86 | |
---|
87 | if (usePacket) |
---|
88 | { |
---|
89 | nbSamplingDates ++; |
---|
90 | if (!copyLess) |
---|
91 | { |
---|
92 | if (!tmpData.numElements()) |
---|
93 | tmpData.resize(data[0]->data.numElements()); |
---|
94 | |
---|
95 | (*functor)(data[0]->data); |
---|
96 | } |
---|
97 | |
---|
98 | nextSamplingDate = ((initDate + offsetMonth) + nbSamplingDates * samplingFreq) + offsetAllButMonth + initDate.getRelCalendar().getTimeStep(); |
---|
99 | } |
---|
100 | |
---|
101 | if (outputResult) |
---|
102 | { |
---|
103 | nbOperationDates ++; |
---|
104 | if (!copyLess) |
---|
105 | { |
---|
106 | functor->final(); |
---|
107 | |
---|
108 | packet = CDataPacketPtr(new CDataPacket); |
---|
109 | packet->date = data[0]->date; |
---|
110 | packet->timestamp = data[0]->timestamp; |
---|
111 | packet->status = data[0]->status; |
---|
112 | packet->data.resize(tmpData.numElements()); |
---|
113 | packet->data = tmpData; |
---|
114 | packet->graphPackage = data[0]->graphPackage; |
---|
115 | } |
---|
116 | else |
---|
117 | packet = data[0]; |
---|
118 | |
---|
119 | isFirstOperation = false; |
---|
120 | graphCycleCompleted = true; |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | return packet; |
---|
125 | } |
---|
126 | |
---|
127 | bool CTemporalFilter::mustAutoTrigger() const |
---|
128 | { |
---|
129 | return true; |
---|
130 | } |
---|
131 | |
---|
132 | bool CTemporalFilter::isDataExpected(const CDate& date) const |
---|
133 | { |
---|
134 | // return isOnceOperation ? isFirstOperation : (date >= nextSamplingDate || date + samplingFreq > nextOperationDate); |
---|
135 | return isOnceOperation ? isFirstOperation : (date >= nextSamplingDate || date > initDate + nbOperationDates*opFreq - samplingFreq + offsetMonth + offsetAllButMonth); |
---|
136 | } |
---|
137 | |
---|
138 | static func::CFunctor* createFunctor(const std::string& opId, bool ignoreMissingValue, CArray<double, 1>& tmpData) |
---|
139 | { |
---|
140 | func::CFunctor* functor = NULL; |
---|
141 | |
---|
142 | double defaultValue = std::numeric_limits<double>::quiet_NaN(); |
---|
143 | |
---|
144 | #define DECLARE_FUNCTOR(MType, mtype) \ |
---|
145 | if (opId.compare(#mtype) == 0) \ |
---|
146 | { \ |
---|
147 | if (ignoreMissingValue) \ |
---|
148 | { \ |
---|
149 | functor = new func::C##MType(tmpData, defaultValue); \ |
---|
150 | } \ |
---|
151 | else \ |
---|
152 | { \ |
---|
153 | functor = new func::C##MType(tmpData); \ |
---|
154 | } \ |
---|
155 | } |
---|
156 | |
---|
157 | #include "functor_type.conf" |
---|
158 | |
---|
159 | if (!functor) |
---|
160 | ERROR("createFunctor(const std::string& opId, ...)", |
---|
161 | << "\"" << opId << "\" is not a valid operation."); |
---|
162 | |
---|
163 | return functor; |
---|
164 | } |
---|
165 | } // namespace xios |
---|