"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/metgm/MetGmTimeTag.cc" between
fimex-1.4.1.tar.gz and fimex-1.4.2.tar.gz

About: Fimex is a the File Interpolation, Manipulation and EXtraction library for gridded geospatial data. It converts between different, extensible dataformats (currently netcdf, NcML, grib1/2 and felt).

MetGmTimeTag.cc  (fimex-1.4.1):MetGmTimeTag.cc  (fimex-1.4.2)
/* /*
* Fimex * Fimex
* *
* (C) Copyright 2011, met.no * (C) Copyright 2011-2019, met.no
* *
* Project Info: https://wiki.met.no/fimex/start * Project Info: https://wiki.met.no/fimex/start
* *
* This library is free software; you can redistribute it and/or modify it * This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by * under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or * the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This library is distributed in the hope that it will be useful, but * This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
skipping to change at line 27 skipping to change at line 27
* *
* You should have received a copy of the GNU Lesser General Public * You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software * License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA. * USA.
*/ */
// internals // internals
// //
#include "MetGmTimeTag.h" #include "MetGmTimeTag.h"
#include "MetGmCommentAttributeParser.h"
#include "MetGmGroup1Ptr.h" #include "MetGmGroup1Ptr.h"
#include "MetGmGroup3Ptr.h" #include "MetGmGroup3Ptr.h"
// fimex // fimex
// //
#include "fimex/CDM.h" #include "fimex/CDM.h"
#include "fimex/CDMReader.h" #include "fimex/CDMReader.h"
#include "fimex/CDMReaderUtils.h" #include "fimex/CDMReaderUtils.h"
#include "fimex/CDMVariable.h" #include "fimex/CDMVariable.h"
#include "fimex/Data.h" #include "fimex/Data.h"
skipping to change at line 50 skipping to change at line 51
// standard // standard
// //
#include <memory> #include <memory>
#include <numeric> #include <numeric>
namespace MetNoFimex { namespace MetNoFimex {
std::shared_ptr<MetGmTimeTag> MetGmTimeTag::createMetGmTimeTagGlobal(const CDMRe ader_p pCdmReader) std::shared_ptr<MetGmTimeTag> MetGmTimeTag::createMetGmTimeTagGlobal(const CDMRe ader_p pCdmReader)
{ {
if(!pCdmReader.get()) if (!pCdmReader)
throw CDMException("createMetGmTimeTag: pCdmReader is null"); throw CDMException("createMetGmTimeTag: pCdmReader is null");
const CDM& cdmRef = pCdmReader->getCDM(); const CDM& cdmRef = pCdmReader->getCDM();
if( !(cdmRef.hasDimension("time") && cdmRef.hasVariable("time")) ) if( !(cdmRef.hasDimension("time") && cdmRef.hasVariable("time")) )
throw CDMException("createMetGmTimeTag: cdm model doesn't have the 'time '"); throw CDMException("createMetGmTimeTag: cdm model doesn't have the 'time '");
std::shared_ptr<MetGmTimeTag> TTag = std::shared_ptr<MetGmTimeTag>(new MetGm std::shared_ptr<MetGmTimeTag> TTag(new MetGmTimeTag);
TimeTag());
TTag->init(pCdmReader); TTag->init(pCdmReader);
return TTag; return TTag;
} }
std::shared_ptr<MetGmTimeTag> MetGmTimeTag::createMetGmTimeTagForWriting(const C DMReader_p pCdmReader, const CDMVariable* pVariable) std::shared_ptr<MetGmTimeTag> MetGmTimeTag::createMetGmTimeTagForWriting(const C DMReader_p pCdmReader, const CDMVariable* pVariable)
{ {
std::shared_ptr<MetGmTimeTag> TTag; std::shared_ptr<MetGmTimeTag> TTag;
const CoordinateSystem_cp_v coordSys = listCoordinateSystems(pCdmReader); CoordinateSystem_cp cs = findCompleteCoordinateSystemFor(listCoordinateSyste
const CDM& cdmRef = pCdmReader->getCDM(); ms(pCdmReader), pVariable->getName());
if (cs && cs->isSimpleSpatialGridded() && cs->getTimeAxis()) {
CoordinateSystem_cp cs = findCompleteCoordinateSystemFor(coordSys, pVariable TTag = std::shared_ptr<MetGmTimeTag>(new MetGmTimeTag);
->getName());
if (cs.get()) {
if(cs->isSimpleSpatialGridded()) {
CoordinateAxis_cp tAxis = cs->getTimeAxis();
if(!tAxis.get()) {
return std::shared_ptr<MetGmTimeTag>();
}
TTag = std::shared_ptr<MetGmTimeTag>(new MetGmTimeTag);
if(tAxis->getAxisType() != CoordinateAxis::Time) CoordinateAxis_cp tAxis = cs->getTimeAxis();
throw CDMException("time axis not found"); if (tAxis->getAxisType() != CoordinateAxis::Time)
throw CDMException("time axis not found");
DataPtr data = pCdmReader->getData(tAxis->getName()); DataPtr data = pCdmReader->getData(tAxis->getName());
TTag->nT_ = data->size(); TTag->nT_ = data->size();
const CDMAttribute& tUnitAttribute(cdmRef.getAttribute("time", std:: const CDM& cdmRef = pCdmReader->getCDM();
string("units"))); const CDMAttribute& tUnitAttribute(cdmRef.getAttribute("time", std::stri
const std::string t_unit = tUnitAttribute.getStringValue(); ng("units")));
const TimeUnit kilde_tu(t_unit); const std::string t_unit = tUnitAttribute.getStringValue();
const TimeUnit kilde_tu(t_unit);
const shared_array<double> tArray = data->asDouble(); const shared_array<double> tArray = data->asDouble();
for(size_t index = 0; index < data->size(); ++index) { for (size_t index = 0; index < data->size(); ++index) {
time_t t = kilde_tu.unitTime2epochSeconds(tArray[index]); time_t t = kilde_tu.unitTime2epochSeconds(tArray[index]);
TTag->points_.push_back(t); TTag->points_.push_back(t);
} }
TTag->extractStartDateTime(); TTag->extractStartDateTime();
TTag->extractAnalysisDateTime(pCdmReader); TTag->extractAnalysisDateTime(pCdmReader);
if(TTag->hasNegativeTimePoints()) if (TTag->hasNegativeTimePoints())
throw CDMException("negative values on the time axis not support throw CDMException("negative values on the time axis not supported")
ed"); ;
if(TTag->hasNonEquidistantTimePoints()) if (TTag->hasNonEquidistantTimePoints())
throw CDMException("time points at time axis are not equidistant throw CDMException("time points at time axis are not equidistant [us
[use extractor to split file on boundaries]"); e extractor to split file on boundaries]");
if(TTag->points_.size() <= 1) { if (TTag->points_.size() <= 1) {
TTag->dT_ = 3600; TTag->dT_ = 3600;
} else { } else {
TTag->dT_ = TTag->points_.at(1) - TTag->points_.at(0); TTag->dT_ = TTag->points_.at(1) - TTag->points_.at(0);
}
} }
} else {
} }
return TTag; return TTag;
} }
std::shared_ptr<MetGmTimeTag> MetGmTimeTag::createMetGmTimeTagForReading(const s td::shared_ptr<MetGmGroup1Ptr> pGroup1, std::shared_ptr<MetGmTimeTag> MetGmTimeTag::createMetGmTimeTagForReading(const s td::shared_ptr<MetGmGroup1Ptr> pGroup1,
const s td::shared_ptr<MetGmGroup3Ptr> pGroup3) const s td::shared_ptr<MetGmGroup3Ptr> pGroup3)
{ {
std::shared_ptr<MetGmTimeTag> TTag = std::shared_ptr<MetGmTimeTag>(new MetGm TimeTag); std::shared_ptr<MetGmTimeTag> TTag = std::shared_ptr<MetGmTimeTag>(new MetGm TimeTag);
if(pGroup3->p_id() == 0) { if(pGroup3->p_id() == 0) {
skipping to change at line 150 skipping to change at line 138
TTag->start_t = pGroup1->startTime(); TTag->start_t = pGroup1->startTime();
for(size_t step = 0; step < TTag->nT_; ++step) { for(size_t step = 0; step < TTag->nT_; ++step) {
TTag->points_.push_back(TTag->start_t + step * TTag->dT_); TTag->points_.push_back(TTag->start_t + step * TTag->dT_);
TTag->pointsAsTimePoints_.push_back(make_time_from_timet(TTag->point s_.at(step))); TTag->pointsAsTimePoints_.push_back(make_time_from_timet(TTag->point s_.at(step)));
TTag->pointsAsDouble_.push_back(TTag->points_.at(step)); TTag->pointsAsDouble_.push_back(TTag->points_.at(step));
} }
} }
return TTag; return TTag;
} }
void MetGmTimeTag::extractAnalysisDateTime(const CDMReader_p pCdmReader) void MetGmTimeTag::extractAnalysisDateTime(const CDMReader_p pCdmReader)
{ {
const CDM& cdmRef = pCdmReader->getCDM(); const CDM& cdmRef = pCdmReader->getCDM();
CDMAttribute metgmAnalysisDateTimeAttribute; CDMAttribute metgmAnalysisDateTimeAttribute;
FimexTime analysisTime; FimexTime analysisTime;
std::vector<std::string> refVarNames = cdmRef.findVariables("standard_na std::vector<std::string> refVarNames = cdmRef.findVariables("standard_name",
me", "forecast_reference_time"); "forecast_reference_time");
if(!refVarNames.empty()) { // we have to honour if there is forecast tim if (!refVarNames.empty()) { // we have to honour if there is forecast time i
e in CDM model we get n CDM model we get
analysisTime = getUniqueForecastReferenceTimeFT(pCdmReader); analysisTime = getUniqueForecastReferenceTimeFT(pCdmReader);
} else if(cdmRef.getAttribute(cdmRef.globalAttributeNS(), ANALYSIS_DATE_ } else if (cdmRef.getAttribute(cdmRef.globalAttributeNS(), ANALYSIS_DATE_TIM
TIME, metgmAnalysisDateTimeAttribute)) { E, metgmAnalysisDateTimeAttribute)) {
analysisTime = string2FimexTime(metgmAnalysisDateTimeAttribute.getSt analysisTime = string2FimexTime(metgmAnalysisDateTimeAttribute.getString
ringValue()); Value());
} else { } else {
analysisTime = make_fimextime_utc_now(); analysisTime = make_fimextime_utc_now();
}
const TimeUnit tu("seconds since 1970-01-01 00:00:00");
double unit_time = tu.fimexTime2unitTime(analysisTime);
analysis_t = tu.unitTime2epochSeconds(unit_time);
}
bool MetGmTimeTag::hasNegativeTimePoints()
{
return std::find_if(points_.begin(), points_.end(), [](float f) { return
f < 0; }) != points_.end();
} }
bool MetGmTimeTag::hasNonEquidistantTimePoints() const TimeUnit tu("seconds since 1970-01-01 00:00:00");
{ double unit_time = tu.fimexTime2unitTime(analysisTime);
if(nT() <= 1) analysis_t = tu.unitTime2epochSeconds(unit_time);
return false; }
std::deque<double> adjDiff(points_.size()); bool MetGmTimeTag::hasNegativeTimePoints()
std::adjacent_difference(points_.begin(), points_.end(), adjDiff.begin() {
); return std::find_if(points_.begin(), points_.end(), [](float f) { return f <
adjDiff.pop_front(); // remove first element 0; }) != points_.end();
}
std::deque<double>::iterator it = std::unique_copy(adjDiff.begin(), adjD bool MetGmTimeTag::hasNonEquidistantTimePoints()
iff.end(), adjDiff.begin()); {
std::sort(adjDiff.begin(), it); if (nT() <= 1)
return false;
adjDiff.resize(it - adjDiff.begin()); std::deque<double> adjDiff(points_.size());
std::adjacent_difference(points_.begin(), points_.end(), adjDiff.begin());
adjDiff.pop_front(); // remove first element
return adjDiff.size() != 1; std::deque<double>::iterator it = std::unique_copy(adjDiff.begin(), adjDiff.
} end(), adjDiff.begin());
std::sort(adjDiff.begin(), it);
void MetGmTimeTag::extractTimePoints(const CDMReader_p pCdmReader) adjDiff.resize(it - adjDiff.begin());
{
const CDM& cdmRef = pCdmReader->getCDM();
const CDMVariable& tVar = cdmRef.getVariable("time");
DataPtr tData = pCdmReader->getData(tVar.getName()); return adjDiff.size() != 1;
}
const CDMAttribute& tUnitAttribute(cdmRef.getAttribute("time", std::stri void MetGmTimeTag::extractTimePoints(const CDMReader_p pCdmReader)
ng("units"))); {
const std::string t_unit = tUnitAttribute.getStringValue(); const CDM& cdmRef = pCdmReader->getCDM();
const TimeUnit kilde_tu(t_unit); const CDMVariable& tVar = cdmRef.getVariable("time");
const shared_array<double> tArray = tData->asDouble(); DataPtr tData = pCdmReader->getData(tVar.getName());
for(size_t index = 0; index < tData->size(); ++index) { const CDMAttribute& tUnitAttribute(cdmRef.getAttribute("time", std::string("
time_t t = kilde_tu.unitTime2epochSeconds(tArray[index]); units")));
points_.push_back(t); const std::string t_unit = tUnitAttribute.getStringValue();
} const TimeUnit kilde_tu(t_unit);
if(hasNegativeTimePoints()) const shared_array<double> tArray = tData->asDouble();
throw CDMException("negative values on the time axis not supported")
;
if(hasNonEquidistantTimePoints()) for (size_t index = 0; index < tData->size(); ++index) {
throw CDMException("time points at time axis are not equidistant [us time_t t = kilde_tu.unitTime2epochSeconds(tArray[index]);
e extractor to split file on boundaries]"); points_.push_back(t);
} }
void MetGmTimeTag::extractStartDateTime() if (hasNegativeTimePoints())
{ throw CDMException("negative values on the time axis not supported");
start_t = points_.at(0);
}
void MetGmTimeTag::init(const CDMReader_p pCdmReader) if (hasNonEquidistantTimePoints())
{ throw CDMException("time points at time axis are not equidistant [use ex
const CDM& cdmRef = pCdmReader->getCDM(); tractor to split file on boundaries]");
const CDMDimension& tDim = cdmRef.getDimension("time"); }
if(!tDim.isUnlimited()) void MetGmTimeTag::extractStartDateTime()
throw CDMException("in given cdm model 'time'' is not unlimited dime {
nsion"); start_t = points_.at(0);
}
void MetGmTimeTag::init(const CDMReader_p pCdmReader)
{
const CDM& cdmRef = pCdmReader->getCDM();
const CDMDimension& tDim = cdmRef.getDimension("time");
nT_ = tDim.getLength(); if (!tDim.isUnlimited())
throw CDMException("in given cdm model 'time'' is not unlimited dimensio
n");
extractTimePoints(pCdmReader); nT_ = tDim.getLength();
if(points_.size() <= 1) extractTimePoints(pCdmReader);
dT_ = 3600;
else
dT_ = points_.at(1) - points_.at(0);
extractStartDateTime(); if (points_.size() <= 1)
dT_ = 3600;
else
dT_ = points_.at(1) - points_.at(0);
extractAnalysisDateTime(pCdmReader); extractStartDateTime();
}
extractAnalysisDateTime(pCdmReader);
} }
} // namespace MetNoFimex
 End of changes. 41 change blocks. 
129 lines changed or deleted 112 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)