[643] | 1 | #include "temporal_filter.hpp" |
---|
| 2 | #include "functor_type.hpp" |
---|
| 3 | #include "calendar_util.hpp" |
---|
[1704] | 4 | #include "workflow_graph.hpp" |
---|
| 5 | #include "file.hpp" |
---|
[643] | 6 | |
---|
| 7 | namespace xios |
---|
| 8 | { |
---|
[1440] | 9 | static func::CFunctor* createFunctor(const std::string& opId, bool ignoreMissingValue, CArray<double, 1>& tmpData); |
---|
[1158] | 10 | |
---|
[643] | 11 | CTemporalFilter::CTemporalFilter(CGarbageCollector& gc, const std::string& opId, |
---|
| 12 | const CDate& initDate, const CDuration samplingFreq, const CDuration samplingOffset, const CDuration opFreq, |
---|
[1440] | 13 | bool ignoreMissingValue /*= false*/) |
---|
[643] | 14 | : CFilter(gc, 1, this) |
---|
[1440] | 15 | , functor(createFunctor(opId, ignoreMissingValue, tmpData)) |
---|
[1158] | 16 | , isOnceOperation(functor->timeType() == func::CFunctor::once) |
---|
| 17 | , isInstantOperation(functor->timeType() == func::CFunctor::instant) |
---|
[1278] | 18 | , samplingFreq(samplingFreq) |
---|
| 19 | , samplingOffset(samplingOffset) |
---|
[643] | 20 | , opFreq(opFreq) |
---|
[1472] | 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) |
---|
[1302] | 25 | , initDate(initDate) |
---|
[1523] | 26 | , nextSamplingDate(initDate + offsetMonth + ( offsetAllButMonth + initDate.getRelCalendar().getTimeStep())) |
---|
[1302] | 27 | , nbOperationDates(1) |
---|
[1473] | 28 | , nbSamplingDates(0) |
---|
[1302] | 29 | // , nextOperationDate(initDate + opFreq + this->samplingOffset) |
---|
[643] | 30 | , isFirstOperation(true) |
---|
[1704] | 31 | , temp_op(opId) |
---|
[643] | 32 | { |
---|
| 33 | } |
---|
| 34 | |
---|
[1704] | 35 | |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | bool CTemporalFilter::buildGraph(std::vector<CDataPacketPtr> data) |
---|
| 40 | { |
---|
| 41 | bool building_graph=this->tag ? data[0]->timestamp >= this->start_graph && data[0]->timestamp <= this->end_graph : false; |
---|
| 42 | |
---|
| 43 | if(building_graph) |
---|
| 44 | { |
---|
| 45 | if(this->filterIDoutputs.size()==0) this->filterID = InvalidableObject::filterIdGenerator++; |
---|
| 46 | int edgeID = InvalidableObject::edgeIdGenerator++; |
---|
| 47 | |
---|
| 48 | // std::cout<<"CTemporalFilter::apply filter tag = "<<this->tag<<" start = "<<this->start_graph<<" end = "<<this->end_graph<<std::endl; |
---|
| 49 | |
---|
| 50 | CWorkflowGraph::allocNodeEdge(); |
---|
| 51 | |
---|
| 52 | if(this->filterIDoutputs.size()==0) |
---|
| 53 | { |
---|
| 54 | CWorkflowGraph::addNode(this->filterID, "Temporal Filter\\n("+this->temp_op+")", 5, 1, 0, data[0]); |
---|
| 55 | (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].transform_type = this->temp_op; |
---|
| 56 | (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].inputs_complete = false ; |
---|
| 57 | (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].clusterID = 1 ; |
---|
| 58 | (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].distance = (data[0]->distance); |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].attributes = this->field->record4graphXiosAttributes(); |
---|
| 62 | if(this->field->file) (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].attributes += "</br>file attributes : </br>" +this->field->file->record4graphXiosAttributes(); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | if(CWorkflowGraph::build_begin) |
---|
| 66 | { |
---|
| 67 | |
---|
| 68 | CWorkflowGraph::addEdge(edgeID, this->filterID, data[0]); |
---|
| 69 | |
---|
| 70 | (*CWorkflowGraph::mapFilters_ptr_with_info)[data[0]->src_filterID].filter_filled = 0 ; |
---|
| 71 | (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].expected_entry_nb += 1 ; |
---|
| 72 | (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].distance = max(data[0]->distance+1, (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].distance); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | this->filterIDoutputs.push_back(data[0]->src_filterID); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | return building_graph; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | |
---|
[643] | 83 | CDataPacketPtr CTemporalFilter::apply(std::vector<CDataPacketPtr> data) |
---|
| 84 | { |
---|
[1704] | 85 | bool BG = buildGraph(data); |
---|
| 86 | |
---|
[1876] | 87 | CDataPacketPtr packet=0; |
---|
[643] | 88 | |
---|
| 89 | if (data[0]->status != CDataPacket::END_OF_STREAM) |
---|
| 90 | { |
---|
[1158] | 91 | bool usePacket, outputResult, copyLess; |
---|
| 92 | if (isOnceOperation) |
---|
| 93 | usePacket = outputResult = copyLess = isFirstOperation; |
---|
| 94 | else |
---|
| 95 | { |
---|
| 96 | usePacket = (data[0]->date >= nextSamplingDate); |
---|
[1302] | 97 | outputResult = (data[0]->date > initDate + nbOperationDates*opFreq - samplingFreq + offsetMonth + offsetAllButMonth); |
---|
[1158] | 98 | copyLess = (isInstantOperation && usePacket && outputResult); |
---|
| 99 | } |
---|
| 100 | |
---|
[643] | 101 | if (usePacket) |
---|
| 102 | { |
---|
[1473] | 103 | nbSamplingDates ++; |
---|
[1158] | 104 | if (!copyLess) |
---|
| 105 | { |
---|
| 106 | if (!tmpData.numElements()) |
---|
| 107 | tmpData.resize(data[0]->data.numElements()); |
---|
[643] | 108 | |
---|
[1158] | 109 | (*functor)(data[0]->data); |
---|
| 110 | } |
---|
[643] | 111 | |
---|
[1473] | 112 | nextSamplingDate = ((initDate + offsetMonth) + nbSamplingDates * samplingFreq) + offsetAllButMonth + initDate.getRelCalendar().getTimeStep(); |
---|
[643] | 113 | } |
---|
| 114 | |
---|
| 115 | if (outputResult) |
---|
| 116 | { |
---|
[1302] | 117 | nbOperationDates ++; |
---|
[1158] | 118 | if (!copyLess) |
---|
| 119 | { |
---|
| 120 | functor->final(); |
---|
[643] | 121 | |
---|
[1158] | 122 | packet = CDataPacketPtr(new CDataPacket); |
---|
| 123 | packet->date = data[0]->date; |
---|
| 124 | packet->timestamp = data[0]->timestamp; |
---|
| 125 | packet->status = data[0]->status; |
---|
| 126 | packet->data.resize(tmpData.numElements()); |
---|
| 127 | packet->data = tmpData; |
---|
| 128 | } |
---|
| 129 | else |
---|
| 130 | packet = data[0]; |
---|
[643] | 131 | |
---|
[1285] | 132 | isFirstOperation = false; |
---|
[1704] | 133 | |
---|
| 134 | packet->field = this->field; |
---|
| 135 | |
---|
| 136 | if(BG) |
---|
| 137 | { |
---|
| 138 | packet->src_filterID=this->filterID; |
---|
| 139 | packet->distance = data[0]->distance+1; |
---|
| 140 | this->filterIDoutputs.clear(); |
---|
| 141 | CWorkflowGraph::build_begin=true; |
---|
| 142 | (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].inputs_complete = true ; |
---|
| 143 | } |
---|
[643] | 144 | } |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | return packet; |
---|
| 148 | } |
---|
[1158] | 149 | |
---|
[1358] | 150 | bool CTemporalFilter::mustAutoTrigger() const |
---|
| 151 | { |
---|
| 152 | return true; |
---|
| 153 | } |
---|
| 154 | |
---|
[1158] | 155 | bool CTemporalFilter::isDataExpected(const CDate& date) const |
---|
| 156 | { |
---|
[1302] | 157 | // return isOnceOperation ? isFirstOperation : (date >= nextSamplingDate || date + samplingFreq > nextOperationDate); |
---|
| 158 | return isOnceOperation ? isFirstOperation : (date >= nextSamplingDate || date > initDate + nbOperationDates*opFreq - samplingFreq + offsetMonth + offsetAllButMonth); |
---|
[1158] | 159 | } |
---|
| 160 | |
---|
[1440] | 161 | static func::CFunctor* createFunctor(const std::string& opId, bool ignoreMissingValue, CArray<double, 1>& tmpData) |
---|
[1158] | 162 | { |
---|
| 163 | func::CFunctor* functor = NULL; |
---|
| 164 | |
---|
[1440] | 165 | double defaultValue = std::numeric_limits<double>::quiet_NaN(); |
---|
[1158] | 166 | |
---|
| 167 | #define DECLARE_FUNCTOR(MType, mtype) \ |
---|
| 168 | if (opId.compare(#mtype) == 0) \ |
---|
| 169 | { \ |
---|
| 170 | if (ignoreMissingValue) \ |
---|
| 171 | { \ |
---|
| 172 | functor = new func::C##MType(tmpData, defaultValue); \ |
---|
| 173 | } \ |
---|
| 174 | else \ |
---|
| 175 | { \ |
---|
| 176 | functor = new func::C##MType(tmpData); \ |
---|
| 177 | } \ |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | #include "functor_type.conf" |
---|
| 181 | |
---|
| 182 | if (!functor) |
---|
| 183 | ERROR("createFunctor(const std::string& opId, ...)", |
---|
| 184 | << "\"" << opId << "\" is not a valid operation."); |
---|
| 185 | |
---|
| 186 | return functor; |
---|
| 187 | } |
---|
[643] | 188 | } // namespace xios |
---|