[1935] | 1 | #include "file_writer_store_filter.hpp" |
---|
| 2 | #include "exception.hpp" |
---|
| 3 | #include "field.hpp" |
---|
[1961] | 4 | #include "file.hpp" |
---|
| 5 | #include "context.hpp" |
---|
[1935] | 6 | |
---|
| 7 | namespace xios |
---|
| 8 | { |
---|
| 9 | CFileWriterStoreFilter::CFileWriterStoreFilter(CGarbageCollector& gc, CField* field) |
---|
| 10 | : CInputPin(gc, 1) |
---|
[1961] | 11 | , field_(field) |
---|
| 12 | |
---|
[1935] | 13 | { |
---|
[1961] | 14 | CContext* context = CContext::getCurrent(); |
---|
| 15 | |
---|
| 16 | if (!field) ERROR("CFileWriterStoreFilter::CFileWriterStoreFilter(CField* field)", "The field cannot be null."); |
---|
| 17 | file_ = field->getFileOut() ; |
---|
| 18 | grid_= field->getGrid() ; |
---|
| 19 | freqWrite_ = file_->output_freq ; |
---|
| 20 | lastWrite_ = context->getCalendar()->getInitDate(); |
---|
| 21 | if (!file_->isEmptyZone() && (field->getGrid()->doGridHaveDataToWrite() || file_->type == CFile::type_attr::one_file)) |
---|
| 22 | needToWrite_=true ; |
---|
| 23 | else needToWrite_=false; |
---|
| 24 | lastFileSplit_ = file_->getLastSplit() ; |
---|
[1962] | 25 | nstep_ = file_->record_offset.isEmpty() ? 0 : file_->record_offset; // record_offset < 0 ==> no output (debugging) |
---|
[1961] | 26 | if (!field->scale_factor.isEmpty()) { scaleFactor_ = field->scale_factor ; hasScaleFactor_ = true ; } |
---|
| 27 | if (!field->add_offset.isEmpty()) { addOffset_ = field->add_offset ; hasAddOffset_ = true ; } |
---|
| 28 | if (!field->prec.isEmpty() && field->prec == 2) hasRounding_ = true ; |
---|
| 29 | if (!field->default_value.isEmpty()) {hasDefaultValue_=true ; defaultValue_ = field->default_value ;} |
---|
[1935] | 30 | } |
---|
| 31 | |
---|
| 32 | void CFileWriterStoreFilter::onInputReady(std::vector<CDataPacketPtr> data) |
---|
| 33 | { |
---|
[1961] | 34 | const CDate currentWrite = lastWrite_ + freqWrite_; |
---|
| 35 | if (needToWrite_) |
---|
| 36 | { |
---|
| 37 | file_->checkWriteFile(); |
---|
| 38 | if (file_->getLastSplit() != lastFileSplit_) |
---|
| 39 | { |
---|
| 40 | nstep_ = 0 ; |
---|
| 41 | lastFileSplit_ = file_->getLastSplit() ; |
---|
| 42 | } |
---|
| 43 | nstep_ = nstep_+1; |
---|
| 44 | |
---|
| 45 | CArray<double,1> dataIn = data[0]->data ; |
---|
| 46 | CArray<double,1> fieldData ; |
---|
| 47 | |
---|
| 48 | if (hasAddOffset_ || hasScaleFactor_ || hasRounding_) dataIn = data[0]->data ; |
---|
| 49 | else dataIn.reference(data[0]->data) ; |
---|
| 50 | |
---|
| 51 | if (hasAddOffset_) dataIn = dataIn - addOffset_ ; |
---|
| 52 | if (hasScaleFactor_) dataIn = dataIn / scaleFactor_; |
---|
| 53 | if (hasRounding_) dataIn = round(dataIn); |
---|
| 54 | |
---|
| 55 | if (hasDefaultValue_) |
---|
| 56 | { |
---|
| 57 | size_t nbData=dataIn.numElements() ; |
---|
| 58 | for (size_t idx = 0; idx < nbData; ++idx) if ( NumTraits<double>::isNan(dataIn(idx)) ) dataIn(idx)=defaultValue_ ; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | if (field_->getUseCompressedOutput()) fieldData.reference(dataIn) ; |
---|
| 62 | else |
---|
| 63 | { |
---|
| 64 | fieldData.resize(grid_->getWorkflowToFullConnector()->getDstSize()); |
---|
| 65 | if (hasDefaultValue_) grid_->getWorkflowToFullConnector()->transfer(dataIn, fieldData, defaultValue_ ) ; |
---|
| 66 | else grid_->getWorkflowToFullConnector()->transfer(dataIn, fieldData ) ; |
---|
| 67 | } |
---|
| 68 | nstep_ = file_->getDataOutput()->writeFieldData(field_, fieldData, lastWrite_,currentWrite, nstep_); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | lastWrite_ = currentWrite ; |
---|
| 72 | |
---|
[1935] | 73 | } |
---|
| 74 | |
---|
| 75 | bool CFileWriterStoreFilter::mustAutoTrigger() const |
---|
| 76 | { |
---|
| 77 | return true; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | bool CFileWriterStoreFilter::isDataExpected(const CDate& date) const |
---|
| 81 | { |
---|
| 82 | return true; |
---|
| 83 | } |
---|
| 84 | } // namespace xios |
---|