[643] | 1 | #include "temporal_filter.hpp" |
---|
| 2 | #include "functor_type.hpp" |
---|
| 3 | #include "calendar_util.hpp" |
---|
| 4 | |
---|
| 5 | namespace xios |
---|
| 6 | { |
---|
| 7 | CTemporalFilter::CTemporalFilter(CGarbageCollector& gc, const std::string& opId, |
---|
| 8 | const CDate& initDate, const CDuration samplingFreq, const CDuration samplingOffset, const CDuration opFreq, |
---|
| 9 | bool ignoreMissingValue /*= false*/, double missingValue /*= 0.0*/) |
---|
| 10 | : CFilter(gc, 1, this) |
---|
| 11 | , samplingFreq(samplingFreq) |
---|
| 12 | , opFreq(opFreq) |
---|
| 13 | , nextSamplingDate(initDate + samplingOffset + initDate.getRelCalendar().getTimeStep()) |
---|
[1121] | 14 | , nextOperationDate(initDate + samplingOffset + opFreq) |
---|
[643] | 15 | , isFirstOperation(true) |
---|
| 16 | { |
---|
[1018] | 17 | double defaultValue = ignoreMissingValue ? std::numeric_limits<double>::quiet_NaN() : missingValue; |
---|
| 18 | |
---|
[643] | 19 | #define DECLARE_FUNCTOR(MType, mtype) \ |
---|
| 20 | if (opId.compare(#mtype) == 0) \ |
---|
| 21 | { \ |
---|
| 22 | if (ignoreMissingValue) \ |
---|
| 23 | { \ |
---|
[1018] | 24 | functor.reset(new func::C##MType(tmpData, defaultValue)); \ |
---|
[643] | 25 | } \ |
---|
| 26 | else \ |
---|
| 27 | { \ |
---|
| 28 | functor.reset(new func::C##MType(tmpData)); \ |
---|
| 29 | } \ |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | #include "functor_type.conf" |
---|
| 33 | |
---|
| 34 | if (!functor) |
---|
| 35 | ERROR("CTemporalFilter::CTemporalFilter(CGarbageCollector& gc, const std::string& opId, ...)", |
---|
| 36 | << "\"" << opId << "\" is not a valid operation."); |
---|
| 37 | |
---|
| 38 | isOnceOperation = (functor->timeType() == func::CFunctor::once); |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | CDataPacketPtr CTemporalFilter::apply(std::vector<CDataPacketPtr> data) |
---|
| 42 | { |
---|
| 43 | CDataPacketPtr packet; |
---|
| 44 | |
---|
| 45 | if (data[0]->status != CDataPacket::END_OF_STREAM) |
---|
| 46 | { |
---|
| 47 | const bool usePacket = isOnceOperation ? isFirstOperation : (data[0]->date >= nextSamplingDate); |
---|
| 48 | if (usePacket) |
---|
| 49 | { |
---|
| 50 | if (!tmpData.numElements()) |
---|
| 51 | tmpData.resize(data[0]->data.numElements()); |
---|
| 52 | |
---|
| 53 | (*functor)(data[0]->data); |
---|
| 54 | |
---|
| 55 | nextSamplingDate = nextSamplingDate + samplingFreq; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | const bool outputResult = isOnceOperation ? isFirstOperation : (data[0]->date + samplingFreq > nextOperationDate); |
---|
| 59 | if (outputResult) |
---|
| 60 | { |
---|
| 61 | functor->final(); |
---|
| 62 | |
---|
| 63 | packet = CDataPacketPtr(new CDataPacket); |
---|
| 64 | packet->date = data[0]->date; |
---|
| 65 | packet->timestamp = data[0]->timestamp; |
---|
| 66 | packet->status = data[0]->status; |
---|
| 67 | packet->data.resize(tmpData.numElements()); |
---|
| 68 | packet->data = tmpData; |
---|
| 69 | |
---|
| 70 | isFirstOperation = false; |
---|
[854] | 71 | nextOperationDate = nextOperationDate + samplingFreq + opFreq - samplingFreq; |
---|
[643] | 72 | } |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | return packet; |
---|
| 76 | } |
---|
[1119] | 77 | |
---|
| 78 | bool CTemporalFilter::isDataExpected(const CDate& date) const |
---|
| 79 | { |
---|
| 80 | return isOnceOperation ? isFirstOperation : (date >= nextSamplingDate || date + samplingFreq > nextOperationDate); |
---|
| 81 | } |
---|
[643] | 82 | } // namespace xios |
---|