/** * @file SpdbGenBasedMetadata.cc */ //------------------------------------------------------------------ #include #include #include #include #include #include //------------------------------------------------------------------ SpdbGenBasedMetadata::SpdbGenBasedMetadata(void): _fixedValuesSet(false), _genTime(0), _hasFixedField(false), _thresholdsSet(false) { } //------------------------------------------------------------------ SpdbGenBasedMetadata::SpdbGenBasedMetadata(const time_t &genTime, const std::vector <Hours, const std::string &threshField, double threshColdstartThresh, const std::string &fixedField, double fixedThresh, const TileInfo &tiling, const std::vector &obarThresh) : _fixedValuesSet(true), _genTime(genTime), _threshField(threshField), _threshColdstartThresh(threshColdstartThresh), _hasFixedField(true), _fixedField(fixedField), _fixedThresh(fixedThresh), _tiling(tiling), _thresholdsSet(true) { for (size_t i=0; i(ltHours[i]*3600.0); _leadSeconds.push_back(lts); MultiObarThreshTileThresholds m(_threshColdstartThresh, _tiling, obarThresh); _thresholdsAtLead.push_back(m); } } //------------------------------------------------------------------ SpdbGenBasedMetadata::SpdbGenBasedMetadata(const time_t &genTime, const std::vector <Hours, const std::string &threshField, double threshColdstartThresh, const TileInfo &tiling, const std::vector &obarThresh) : _fixedValuesSet(true), _genTime(genTime), _threshField(threshField), _threshColdstartThresh(threshColdstartThresh), _hasFixedField(false), _fixedField("None"), _fixedThresh(0.0), _tiling(tiling), _thresholdsSet(true) { for (size_t i=0; i(ltHours[i]*3600.0); _leadSeconds.push_back(lts); MultiObarThreshTileThresholds m(_threshColdstartThresh, _tiling, obarThresh); _thresholdsAtLead.push_back(m); } } //------------------------------------------------------------------ SpdbGenBasedMetadata::~SpdbGenBasedMetadata() { } //------------------------------------------------------------------ int SpdbGenBasedMetadata::getNumObarThresh(void) const { if (_thresholdsAtLead.empty()) { return 0; } else { return _thresholdsAtLead[0].getNumObarThresh(); } } //------------------------------------------------------------------ std::vector SpdbGenBasedMetadata::getObarThresh(void) const { if (_thresholdsAtLead.empty()) { std::vector ret; return ret; } else { return _thresholdsAtLead[0].getObarThresh(); } } //------------------------------------------------------------------ std::vector SpdbGenBasedMetadata::getTileThresholds(int leadSeconds, int obarThreshIndex) const { vector ret; if (!_thresholdsSet) { return ret; } int ltIndex = _leadIndex(leadSeconds); if (ltIndex < 0) { return ret; } return _thresholdsAtLead[ltIndex].getTileThresholds(obarThreshIndex); } //------------------------------------------------------------------ std::vector SpdbGenBasedMetadata::getTileThreshInfo(int leadSeconds, int obarThreshIndex) const { vector ret; if (!_thresholdsSet) { return ret; } int ltIndex = _leadIndex(leadSeconds); if (ltIndex < 0) { return ret; } return _thresholdsAtLead[ltIndex].getTileThreshInfo(_genTime, leadSeconds, obarThreshIndex); } //------------------------------------------------------------------ void SpdbGenBasedMetadata::update(const TileThreshInfoGenBased &item, int obarThreshIndex) { int lt = item.getLeadTime(); int i = _leadIndex(lt); if (i < 0) { LOG(ERROR) << "Bad lead time " << lt; return; } if (_thresholdsAtLead[i].update(item, obarThreshIndex)) { if (!_thresholdsSet) { LOG(DEBUG) << "Setting first lead time values for gen time " << DateTime::strn(_genTime); _thresholdsSet = true; } } else { LOG(ERROR) << "Could not update for obsIndex " << obarThreshIndex; } } //------------------------------------------------------------------ bool SpdbGenBasedMetadata::fromXml(const std::string &xml) { if (!_fixedValuesSetFromXml(xml)) { return false; } if (!_genFromXml(xml)) { return false; } if (!_leadsFromXml(xml)) { return false; } if (!_threshFieldFromXml(xml)) { return false; } if (!_fixedFieldFromXml(xml)) { return false; } if (!_tilingFromXml(xml)) { return false; } if (!_thresholdsFromXml(xml)) { return false; } if (!_thresholdsSetFromXml(xml)) { return false; } return true; } //------------------------------------------------------------------ std::string SpdbGenBasedMetadata::toXml(void) const { string ret = _fixedValuesSetToXml(); ret += _genToXml(); ret += _leadsToXml(); ret += _threshFieldToXml(); ret += _fixedFieldToXml(); ret += _tiling.toXml(); ret += _thresholdsToXml(); ret += _thresholdsSetToXml(); return ret; } //------------------------------------------------------------------ bool SpdbGenBasedMetadata::getTiledGrid(int leadTime, double obarThresh, double centerWeight, double edgeWeight, int nptSmooth, Grid2d &item, bool motherOnly) const { int ltIndex = _leadIndex(leadTime); if (ltIndex < 0) { LOG(ERROR) << "Lead time not found in state " << leadTime; return false; } if (_thresholdsAtLead[ltIndex].getTiledGrid(_threshField, obarThresh, _tiling, centerWeight, edgeWeight, nptSmooth, item, motherOnly)) { return true; } else { LOG(ERROR) << "Setting tiled grids for lead time " << ltIndex; return false; } } //------------------------------------------------------------------ void SpdbGenBasedMetadata::setThresh(int ltSec, int obarThreshIndex, int tileIndex, double value) { int i = _leadIndex(ltSec); if (i < 0) { LOG(ERROR) << "Setting tiled grids for lead time " << ltSec; return; } _thresholdsAtLead[i].setThresh(obarThreshIndex, tileIndex, value, false); } //------------------------------------------------------------------ void SpdbGenBasedMetadata::setThreshColdstart(int ltSec, int obarThreshIndex, int tileIndex) { int i = _leadIndex(ltSec); if (i < 0) { LOG(ERROR) << "Setting tiled grids for lead time " << ltSec; return; } _thresholdsAtLead[i].setThresh(obarThreshIndex, tileIndex, _threshColdstartThresh, true); } //------------------------------------------------------------------ void SpdbGenBasedMetadata::printState(const time_t &t, const time_t &twritten, const std::vector &leadSec, const std::vector &tiles, bool verbose) const { if (leadSec.empty() && tiles.empty()) { printState(t, twritten, verbose); return; } printf("---------Threshold/bias information %s ---written %s, for %s----------\n", DateTime::strn(t).c_str(), DateTime::strn(twritten).c_str(), _threshField.c_str()); printf("\nLeadtimes:"); vector wantedLt; for (size_t i=0; i<_leadSeconds.size(); ++i) { bool good = false; if (leadSec.empty()) { good = true; } else { if (find(leadSec.begin(), leadSec.end(), _leadSeconds[i]) != leadSec.end()) { good = true; } } if (good) { printf(" %d", _leadSeconds[i]); wantedLt.push_back(i); } } printf("\n"); _tiling.print(verbose); for (size_t i=0; i vstring; if (TaXml::readStringArray(str, "lt", vstring)) { LOG(ERROR) << "No XML string array with key lt"; return false; } _leadSeconds.clear(); for (size_t i=0; i vstring; if (TaXml::readStringArray(xml, MultiObarThreshTileThresholds::_tag, vstring)) { LOG(ERROR) << "Reading tag as array " << MultiObarThreshTileThresholds::_tag; return false; } if (vstring.size() != _leadSeconds.size()) { LOG(ERROR) << "Inconsistent tiling " << "input:" << _leadSeconds.size() << " xml:" << vstring.size(); return false; } // for every element, // parse it for (size_t i=0; i::const_iterator i; i = find(_leadSeconds.begin(), _leadSeconds.end(), leadTime); if (i == _leadSeconds.end()) { return -1; } else { return (int)(i - _leadSeconds.begin()); } }