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