XIOS  1.0
Xml I/O Server
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Définitions de type Énumérations Valeurs énumérées Amis Macros
temporal_filter.cpp
Aller à la documentation de ce fichier.
1 #include "temporal_filter.hpp"
2 #include "functor_type.hpp"
3 #include "calendar_util.hpp"
4 
5 namespace xios
6 {
7  static func::CFunctor* createFunctor(const std::string& opId, bool ignoreMissingValue, CArray<double, 1>& tmpData);
8 
9  CTemporalFilter::CTemporalFilter(CGarbageCollector& gc, const std::string& opId,
10  const CDate& initDate, const CDuration samplingFreq, const CDuration samplingOffset, const CDuration opFreq,
11  bool ignoreMissingValue /*= false*/)
12  : CFilter(gc, 1, this)
13  , functor(createFunctor(opId, ignoreMissingValue, tmpData))
14  , isOnceOperation(functor->timeType() == func::CFunctor::once)
15  , isInstantOperation(functor->timeType() == func::CFunctor::instant)
16  , samplingFreq(samplingFreq)
17  , samplingOffset(samplingOffset)
18  , opFreq(opFreq)
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)
24 // , nextSamplingDate(initDate + (this->samplingOffset + initDate.getRelCalendar().getTimeStep()))
25  , nextSamplingDate(initDate + offsetMonth + ( offsetAllButMonth + initDate.getRelCalendar().getTimeStep()))
26  , nbOperationDates(1)
27  , nbSamplingDates(0)
28 // , nextOperationDate(initDate + opFreq + this->samplingOffset)
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  {
39  bool usePacket, outputResult, copyLess;
40  if (isOnceOperation)
41  usePacket = outputResult = copyLess = isFirstOperation;
42  else
43  {
44  usePacket = (data[0]->date >= nextSamplingDate);
45 // outputResult = (data[0]->date + samplingFreq > nextOperationDate);
46  outputResult = (data[0]->date > initDate + nbOperationDates*opFreq - samplingFreq + offsetMonth + offsetAllButMonth);
47  copyLess = (isInstantOperation && usePacket && outputResult);
48  }
49 
50  if (usePacket)
51  {
52  nbSamplingDates ++;
53  if (!copyLess)
54  {
55  if (!tmpData.numElements())
56  tmpData.resize(data[0]->data.numElements());
57 
58  (*functor)(data[0]->data);
59  }
60 
62  }
63 
64  if (outputResult)
65  {
67  if (!copyLess)
68  {
69  functor->final();
70 
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];
80 
81  isFirstOperation = false;
82 // nextOperationDate = initDate + samplingFreq + nbOperationDates*opFreq - samplingFreq + offsetMonth + offsetAllButMonth;
83  }
84  }
85 
86  return packet;
87  }
88 
90  {
91  return true;
92  }
93 
94  bool CTemporalFilter::isDataExpected(const CDate& date) const
95  {
96 // return isOnceOperation ? isFirstOperation : (date >= nextSamplingDate || date + samplingFreq > nextOperationDate);
98  }
99 
100  static func::CFunctor* createFunctor(const std::string& opId, bool ignoreMissingValue, CArray<double, 1>& tmpData)
101  {
102  func::CFunctor* functor = NULL;
103 
104  double defaultValue = std::numeric_limits<double>::quiet_NaN();
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  }
127 } // namespace xios
A generic filter with an input pin and an output pin.
Definition: filter.hpp:15
static func::CFunctor * createFunctor(const std::string &opId, bool ignoreMissingValue, CArray< double, 1 > &tmpData)
CDate nextSamplingDate
The date of the next sampling.
Last packet of the stream, does not have data.
Definition: data_packet.hpp:21
const CDuration opFreq
The operation frequency, i.e.
std::shared_ptr< CDataPacket > CDataPacketPtr
Definition: data_packet.hpp:45
const CCalendar & getRelCalendar(void) const
Definition: date.cpp:145
int nbOperationDates
The number of times an operation is performed.
const boost::scoped_ptr< func::CFunctor > functor
The functor corresponding to the temporal operation.
const CDuration samplingFreq
The sampling frequency, i.e.
CTemporalFilter(CGarbageCollector &gc, const std::string &opId, const CDate &initDate, const CDuration samplingFreq, const CDuration samplingOffset, const CDuration opFreq, bool ignoreMissingValue=false)
Constructs a temporal filter wrapping the specified temporal operation.
#define xios(arg)
A packet corresponds to a timestamped array of data.
Definition: data_packet.hpp:14
const CDuration offsetMonth
The month duration of samplingOffset.
const bool isOnceOperation
True if the operation should be computed just once.
virtual CDataPacketPtr apply(std::vector< CDataPacketPtr > data)
Applies the temporal operation to the input data and returns the result when it is ready...
bool isFirstOperation
True before the first operation was been computed.
CATCH CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar *scalarDestination, CScalar *scalarSource, CReduceScalarToScalar *algo ERROR)("CScalarAlgorithmReduceScalar::CScalarAlgorithmReduceScalar(CScalar* scalarDestination, CScalar* scalarSource, CReduceScalarToScalar* algo)",<< "Operation must be defined."<< "Scalar source "<< scalarSource->getId()<< std::endl<< "Scalar destination "<< scalarDestination->getId())
virtual bool mustAutoTrigger() const
Tests if the filter must auto-trigger.
const CDuration & getTimeStep(void) const
Accesseurs ///.
Definition: calendar.cpp:154
void resize(int extent)
Definition: array_new.hpp:320
CDuration offsetAllButMonth
All but the month duration of samplingOffset.
CArray< double, 1 > tmpData
The array of data used for temporary storage.
virtual bool isDataExpected(const CDate &date) const
Tests whether data is expected for the specified date.
A basic garbage collector which ensures no old packets linger in the filter graph.
////////////////////// Déclarations ////////////////////// ///
Definition: functor.hpp:14
const bool isInstantOperation
True if the operation is instant.