source: XIOS/dev/branch_openmp/src/filter/source_filter.cpp @ 1642

Last change on this file since 1642 was 1642, checked in by yushan, 5 years ago

dev on ADA. add flag switch _usingEP/_usingMPI

File size: 4.3 KB
RevLine 
[638]1#include "source_filter.hpp"
2#include "grid.hpp"
3#include "exception.hpp"
[756]4#include "calendar_util.hpp"
[1018]5#include <limits>
[638]6
7namespace xios
8{
[1642]9  CSourceFilter::CSourceFilter(CGarbageCollector& gc, CGrid* grid,
10                               bool compression /*= true*/, bool mask /*= false*/,
[1018]11                               const CDuration offset /*= NoneDu*/, bool manualTrigger /*= false*/,
12                               bool hasMissingValue /*= false*/,
13                               double defaultValue /*= 0.0*/)
[1006]14    : COutputPin(gc, manualTrigger)
15    , grid(grid)
[1460]16    , compression(compression)
[1642]17    , mask(mask)
[756]18    , offset(offset)
[1018]19    , hasMissingValue(hasMissingValue), defaultValue(defaultValue)
[638]20  {
21    if (!grid)
22      ERROR("CSourceFilter::CSourceFilter(CGrid* grid)",
23            "Impossible to construct a source filter without providing a grid.");
24  }
25
26  template <int N>
[643]27  void CSourceFilter::streamData(CDate date, const CArray<double, N>& data)
[638]28  {
[756]29    date = date + offset; // this is a temporary solution, it should be part of a proper temporal filter
30
[638]31    CDataPacketPtr packet(new CDataPacket);
[643]32    packet->date = date;
33    packet->timestamp = date;
[638]34    packet->status = CDataPacket::NO_ERROR;
35
[1460]36    packet->data.resize(grid->storeIndex_client.numElements());   
37   
38    if (compression)
39    {
40      packet->data = defaultValue;
41      grid->uncompressField(data, packet->data);   
42    }
43    else
[1642]44    {
45      if (mask)
46        grid->maskField(data, packet->data);
47      else
48        grid->inputField(data, packet->data);
49    }
[1018]50    // Convert missing values to NaN
51    if (hasMissingValue)
52    {
[1205]53      const double nanValue = std::numeric_limits<double>::quiet_NaN();
54      const size_t nbData = packet->data.numElements();
[1018]55      for (size_t idx = 0; idx < nbData; ++idx)
56      {
57        if (defaultValue == packet->data(idx))
58          packet->data(idx) = nanValue;
59      }
60    }
61
[1006]62    onOutputReady(packet);
[638]63  }
64
[643]65  template void CSourceFilter::streamData<1>(CDate date, const CArray<double, 1>& data);
66  template void CSourceFilter::streamData<2>(CDate date, const CArray<double, 2>& data);
67  template void CSourceFilter::streamData<3>(CDate date, const CArray<double, 3>& data);
[932]68  template void CSourceFilter::streamData<4>(CDate date, const CArray<double, 4>& data);
69  template void CSourceFilter::streamData<5>(CDate date, const CArray<double, 5>& data);
70  template void CSourceFilter::streamData<6>(CDate date, const CArray<double, 6>& data);
71  template void CSourceFilter::streamData<7>(CDate date, const CArray<double, 7>& data);
[638]72
[643]73  void CSourceFilter::streamDataFromServer(CDate date, const std::map<int, CArray<double, 1> >& data)
[638]74  {
[756]75    date = date + offset; // this is a temporary solution, it should be part of a proper temporal filter
76
[638]77    CDataPacketPtr packet(new CDataPacket);
[643]78    packet->date = date;
79    packet->timestamp = date;
[638]80    packet->status = CDataPacket::NO_ERROR;
[1460]81   
[988]82    if (data.size() != grid->storeIndex_fromSrv.size())
[643]83      ERROR("CSourceFilter::streamDataFromServer(CDate date, const std::map<int, CArray<double, 1> >& data)",
[638]84            << "Incoherent data received from servers,"
[988]85            << " expected " << grid->storeIndex_fromSrv.size() << " chunks but " << data.size() << " were given.");
[638]86
87    packet->data.resize(grid->storeIndex_client.numElements());
88    std::map<int, CArray<double, 1> >::const_iterator it, itEnd = data.end();
89    for (it = data.begin(); it != itEnd; it++)
[1460]90    {     
[988]91      CArray<int,1>& index = grid->storeIndex_fromSrv[it->first];
[638]92      for (int n = 0; n < index.numElements(); n++)
93        packet->data(index(n)) = it->second(n);
94    }
95
[1205]96    // Convert missing values to NaN
97    if (hasMissingValue)
98    {
99      const double nanValue = std::numeric_limits<double>::quiet_NaN();
100      const size_t nbData = packet->data.numElements();
101      for (size_t idx = 0; idx < nbData; ++idx)
102      {
103        if (defaultValue == packet->data(idx))
104          packet->data(idx) = nanValue;
105      }
106    }
107
[1006]108    onOutputReady(packet);
[638]109  }
110
[643]111  void CSourceFilter::signalEndOfStream(CDate date)
[638]112  {
[1328]113    date = date + offset; // this is a temporary solution, it should be part of a proper temporal filter
114
[638]115    CDataPacketPtr packet(new CDataPacket);
[643]116    packet->date = date;
117    packet->timestamp = date;
[638]118    packet->status = CDataPacket::END_OF_STREAM;
[1006]119    onOutputReady(packet);
[638]120  }
121} // namespace xios
Note: See TracBrowser for help on using the repository browser.