[643] | 1 | #include "temporal_filter.hpp" |
---|
| 2 | #include "functor_type.hpp" |
---|
| 3 | #include "calendar_util.hpp" |
---|
| 4 | |
---|
| 5 | namespace xios |
---|
| 6 | { |
---|
[1440] | 7 | static func::CFunctor* createFunctor(const std::string& opId, bool ignoreMissingValue, CArray<double, 1>& tmpData); |
---|
[1158] | 8 | |
---|
[643] | 9 | CTemporalFilter::CTemporalFilter(CGarbageCollector& gc, const std::string& opId, |
---|
| 10 | const CDate& initDate, const CDuration samplingFreq, const CDuration samplingOffset, const CDuration opFreq, |
---|
[1440] | 11 | bool ignoreMissingValue /*= false*/) |
---|
[643] | 12 | : CFilter(gc, 1, this) |
---|
[1440] | 13 | , functor(createFunctor(opId, ignoreMissingValue, tmpData)) |
---|
[1158] | 14 | , isOnceOperation(functor->timeType() == func::CFunctor::once) |
---|
| 15 | , isInstantOperation(functor->timeType() == func::CFunctor::instant) |
---|
[1278] | 16 | , samplingFreq(samplingFreq) |
---|
| 17 | , samplingOffset(samplingOffset) |
---|
[643] | 18 | , opFreq(opFreq) |
---|
[1302] | 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) |
---|
[1406] | 24 | , nextSamplingDate(initDate + (this->samplingOffset + initDate.getRelCalendar().getTimeStep())) |
---|
[1302] | 25 | , nbOperationDates(1) |
---|
| 26 | // , nextOperationDate(initDate + opFreq + this->samplingOffset) |
---|
[643] | 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 | { |
---|
[1158] | 37 | bool usePacket, outputResult, copyLess; |
---|
| 38 | if (isOnceOperation) |
---|
| 39 | usePacket = outputResult = copyLess = isFirstOperation; |
---|
| 40 | else |
---|
| 41 | { |
---|
| 42 | usePacket = (data[0]->date >= nextSamplingDate); |
---|
[1302] | 43 | // outputResult = (data[0]->date + samplingFreq > nextOperationDate); |
---|
| 44 | outputResult = (data[0]->date > initDate + nbOperationDates*opFreq - samplingFreq + offsetMonth + offsetAllButMonth); |
---|
[1158] | 45 | copyLess = (isInstantOperation && usePacket && outputResult); |
---|
| 46 | } |
---|
| 47 | |
---|
[643] | 48 | if (usePacket) |
---|
| 49 | { |
---|
[1158] | 50 | if (!copyLess) |
---|
| 51 | { |
---|
| 52 | if (!tmpData.numElements()) |
---|
| 53 | tmpData.resize(data[0]->data.numElements()); |
---|
[643] | 54 | |
---|
[1158] | 55 | (*functor)(data[0]->data); |
---|
| 56 | } |
---|
[643] | 57 | |
---|
[1285] | 58 | nextSamplingDate = nextSamplingDate + samplingFreq; |
---|
[643] | 59 | } |
---|
| 60 | |
---|
| 61 | if (outputResult) |
---|
| 62 | { |
---|
[1302] | 63 | nbOperationDates ++; |
---|
[1158] | 64 | if (!copyLess) |
---|
| 65 | { |
---|
| 66 | functor->final(); |
---|
[643] | 67 | |
---|
[1158] | 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]; |
---|
[643] | 77 | |
---|
[1285] | 78 | isFirstOperation = false; |
---|
[1302] | 79 | // nextOperationDate = initDate + samplingFreq + nbOperationDates*opFreq - samplingFreq + offsetMonth + offsetAllButMonth; |
---|
[643] | 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | return packet; |
---|
| 84 | } |
---|
[1158] | 85 | |
---|
[1358] | 86 | bool CTemporalFilter::mustAutoTrigger() const |
---|
| 87 | { |
---|
| 88 | return true; |
---|
| 89 | } |
---|
| 90 | |
---|
[1158] | 91 | bool CTemporalFilter::isDataExpected(const CDate& date) const |
---|
| 92 | { |
---|
[1302] | 93 | // return isOnceOperation ? isFirstOperation : (date >= nextSamplingDate || date + samplingFreq > nextOperationDate); |
---|
| 94 | return isOnceOperation ? isFirstOperation : (date >= nextSamplingDate || date > initDate + nbOperationDates*opFreq - samplingFreq + offsetMonth + offsetAllButMonth); |
---|
[1158] | 95 | } |
---|
| 96 | |
---|
[1440] | 97 | static func::CFunctor* createFunctor(const std::string& opId, bool ignoreMissingValue, CArray<double, 1>& tmpData) |
---|
[1158] | 98 | { |
---|
| 99 | func::CFunctor* functor = NULL; |
---|
| 100 | |
---|
[1440] | 101 | double defaultValue = std::numeric_limits<double>::quiet_NaN(); |
---|
[1158] | 102 | |
---|
| 103 | #define DECLARE_FUNCTOR(MType, mtype) \ |
---|
| 104 | if (opId.compare(#mtype) == 0) \ |
---|
| 105 | { \ |
---|
| 106 | if (ignoreMissingValue) \ |
---|
| 107 | { \ |
---|
| 108 | functor = new func::C##MType(tmpData, defaultValue); \ |
---|
| 109 | } \ |
---|
| 110 | else \ |
---|
| 111 | { \ |
---|
| 112 | functor = new func::C##MType(tmpData); \ |
---|
| 113 | } \ |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | #include "functor_type.conf" |
---|
| 117 | |
---|
| 118 | if (!functor) |
---|
| 119 | ERROR("createFunctor(const std::string& opId, ...)", |
---|
| 120 | << "\"" << opId << "\" is not a valid operation."); |
---|
| 121 | |
---|
| 122 | return functor; |
---|
| 123 | } |
---|
[643] | 124 | } // namespace xios |
---|