source: XIOS/dev/dev_ym/XIOS_COUPLING/src/filter/file_writer_store_filter.cpp @ 2230

Last change on this file since 2230 was 2143, checked in by yushan, 3 years ago

Big commit on graph functionality. Add buildWorkflowGraph function for filters

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 3.7 KB
Line 
1#include "file_writer_store_filter.hpp"
2#include "exception.hpp"
3#include "field.hpp"
4#include "file.hpp"
5#include "context.hpp"
6#include "workflow_graph.hpp"
7
8namespace xios
9{
10  CFileWriterStoreFilter::CFileWriterStoreFilter(CGarbageCollector& gc, CField* field)
11    : CInputPin(gc, 1)
12    , field_(field), graphEnabled(false)
13
14  {
15    CContext* context = CContext::getCurrent();
16
17    if (!field) ERROR("CFileWriterStoreFilter::CFileWriterStoreFilter(CField* field)", "The field cannot be null.");
18    file_ = field->getFileOut() ;
19    grid_= field->getGrid() ;
20    freqWrite_ = file_->output_freq ;
21    lastWrite_ = context->getCalendar()->getInitDate();
22    if (!file_->isEmptyZone() && (field->getGrid()->doGridHaveDataToWrite() || file_->type == CFile::type_attr::one_file))
23         needToWrite_=true ;
24    else needToWrite_=false;
25    lastFileSplit_ = file_->getLastSplit() ;
26    nstep_ = file_->record_offset.isEmpty() ? 0 : file_->record_offset; // record_offset < 0 ==> no output (debugging)
27    if (!field->scale_factor.isEmpty()) { scaleFactor_ = field->scale_factor ; hasScaleFactor_ = true ; }
28    if (!field->add_offset.isEmpty()) { addOffset_ = field->add_offset ; hasAddOffset_ = true ; }
29    if (!field->prec.isEmpty() && field->prec == 2) hasRounding_ = true ;
30    if (!field->default_value.isEmpty()) {hasDefaultValue_=true ; defaultValue_ = field->default_value ;}
31  }
32
33  void CFileWriterStoreFilter::onInputReady(std::vector<CDataPacketPtr> data)
34  {
35    const CDate currentWrite = lastWrite_ + freqWrite_;
36    if (needToWrite_)
37    {
38      file_->checkWriteFile();
39      if (file_->getLastSplit() != lastFileSplit_)
40      {
41        nstep_ = 0 ;
42        lastFileSplit_ = file_->getLastSplit() ;
43      }
44      nstep_ = nstep_+1;
45     
46      CArray<double,1> dataIn = data[0]->data ;
47      CArray<double,1> fieldData ;
48
49      if (hasAddOffset_ || hasScaleFactor_ || hasRounding_) dataIn = data[0]->data ;
50      else dataIn.reference(data[0]->data) ;
51
52      if (hasAddOffset_)   dataIn = dataIn - addOffset_ ;
53      if (hasScaleFactor_) dataIn = dataIn / scaleFactor_;
54      if (hasRounding_)    dataIn = round(dataIn);
55
56      if (hasDefaultValue_)
57      {
58        size_t nbData=dataIn.numElements() ;
59        for (size_t idx = 0; idx < nbData; ++idx) if ( NumTraits<double>::isNan(dataIn(idx)) ) dataIn(idx)=defaultValue_ ;
60      }
61
62      if (field_->getUseCompressedOutput()) fieldData.reference(dataIn) ;
63      else
64      {
65        fieldData.resize(grid_->getWorkflowToFullConnector()->getDstSize());
66        if (hasDefaultValue_) grid_->getWorkflowToFullConnector()->transfer(dataIn, fieldData, defaultValue_ ) ;
67        else grid_->getWorkflowToFullConnector()->transfer(dataIn, fieldData ) ;
68      } 
69      nstep_ = file_->getDataOutput()->writeFieldData(field_, fieldData, lastWrite_,currentWrite, nstep_);
70      if(this->graphEnabled)
71      {
72       
73        this->graphPackage->filterId = CWorkflowGraph::getNodeSize();
74        if(!data[0]->graphPackage) data[0]->graphPackage = new CGraphDataPackage;
75        data[0]->graphPackage->currentField = this->graphPackage->inFields[0];
76        std::rotate(this->graphPackage->inFields.begin(), this->graphPackage->inFields.begin() + 1, this->graphPackage->inFields.end());
77     
78        CWorkflowGraph::addNode("File Writer Store filter", 5, true, 1, data[0]);
79     
80        CWorkflowGraph::addEdge(data[0]->graphPackage->fromFilter, this->graphPackage->filterId, data[0]);
81        data[0]->graphPackage->fromFilter = this->graphPackage->filterId;
82
83
84      }
85    }
86
87    lastWrite_ = currentWrite ;
88
89  }
90
91  bool CFileWriterStoreFilter::mustAutoTrigger() const
92  {
93    return true;
94  }
95
96  bool CFileWriterStoreFilter::isDataExpected(const CDate& date) const
97  {
98    return true;
99  }
100} // namespace xios
Note: See TracBrowser for help on using the repository browser.