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