/** * @file SingleTileThresholds.cc */ //------------------------------------------------------------------ #include #include #include #include #include #include #include #include const std::string SingleTileThresholds::_tag = "TileData"; //------------------------------------------------------------------ SingleTileThresholds::SingleTileThresholds(void) : _ok(false), _bias(-99.99), _coldstart(true), _motherTile(false), _generatingTime(0), _obsValue(-99.99), _fcstValue(-99.99) { } //------------------------------------------------------------------ SingleTileThresholds:: SingleTileThresholds(const std::string &xml, const std::vector &fields, int &tileIndex) : _ok(true) { // read the thresholds array vector vstring; if (TaXml::readStringArray(xml, FieldThresh::_tag, vstring)) { LOG(ERROR) << "Reading tag as array using " << FieldThresh::_tag; _ok = false; } if (_ok) { if (vstring.size() != fields.size()) { LOG(ERROR) << "Inconsistent number of fields " << fields.size() << " " << vstring.size(); _ok = false; } else { for (size_t i=0; i &fieldthresh, bool fromMother) : _ok(true), _thresh(fieldthresh), _bias(-99.99), _coldstart(true), _motherTile(fromMother), _generatingTime(0), _obsValue(-99.99), _fcstValue(-99.99) { } //------------------------------------------------------------------ SingleTileThresholds:: SingleTileThresholds(const std::vector &fieldthresh, double bias, const time_t &generatingTime, double obsValue, double fcstValue, bool fromMother) : _ok(true), _thresh(fieldthresh), _bias(bias), _coldstart(false), _motherTile(fromMother), _generatingTime(generatingTime), _obsValue(obsValue), _fcstValue(fcstValue) { } //------------------------------------------------------------------ SingleTileThresholds::~SingleTileThresholds() { } //------------------------------------------------------------------ bool SingleTileThresholds::update(const TileThreshInfo &item) { if (_thresh.size() != item.getThresh().size()) { LOG(ERROR) << "Sizes don't match " << item.getThresh().size() << " " << _thresh.size(); return false; } for (size_t i=0; i<_thresh.size(); ++i) { if (!_thresh[i].fieldMatch(item.getIthName(i), true)) { return false; } _thresh[i].setThresh(item.getIthThresh(i)); } _coldstart = item.getColdstart(); _motherTile = item.getMotherTile(); _generatingTime = item.getGeneratingTime(); _bias = item.getBias(); _obsValue = item.getObs(); _fcstValue = item.getFcst(); return true; } //------------------------------------------------------------------ bool SingleTileThresholds::filterFields(const std::vector &fieldNames) { vector newThresh; for (size_t i=0; i &filterFields) { bool ret = true; for (size_t i=0; i &coldstartThresh) { if (_coldstart) { // already set return true; } if (t - _generatingTime <= maxSecondsBeforeColdstart) { // not too old return true; } if (_thresh.size() != coldstartThresh.size()) { LOG(ERROR) << "Sizes don't match " << coldstartThresh.size() << " " << _thresh.size(); return false; } bool good = true; for (size_t i=0; i<_thresh.size(); ++i) { if (!_thresh[i].fieldMatch(coldstartThresh[i], true)) { good = false; } } if (!good) { return false; } _bias = -99.99; _coldstart = true; // Note: mother tile is not changed here _generatingTime = 0; _thresh = coldstartThresh; _obsValue = -99.99; _fcstValue = -99.99; return true; } //------------------------------------------------------------------ void SingleTileThresholds::print(int leadTime, int tileIndex, const TileInfo &info,bool verbose) const { printf(" lt:%08d tile[%3d(%s)] ", leadTime, tileIndex, info.latlonDebugString(tileIndex).c_str()); for (size_t i=0; i<_thresh.size(); ++i) { printf("%s ", _thresh[i].sprint().c_str()); } printf("bias:%10.8lf ", _bias); if (_motherTile) { printf("Mother "); } else { printf(" "); } if (_coldstart) { printf("Coldstart\n"); } else { printf("ObsTime:%s", DateTime::strn(_generatingTime).c_str()); if (verbose) { printf(" ObsValue:%.8lf", _obsValue); printf(" FcstValue:%.8lf\n", _fcstValue); } else { printf("\n"); } } } //------------------------------------------------------------------ void SingleTileThresholds::logDebug(int leadTime, int tileIndex, bool verbose) const { LOG(DEBUG) << " lt:" << leadTime << " tileIndex:" << tileIndex; string s= ""; for (size_t i=0; i<_thresh.size(); ++i) { s += _thresh[i].sprint().c_str(); } LOG(DEBUG) << " " << s; LOG(DEBUG) << " bias:" << _bias; if (_motherTile) { LOG(DEBUG) << " fromMotherTile"; } if (_coldstart) { LOG(DEBUG) << " Coldstart"; } else { LOG(DEBUG) << " ObsTime: " << DateTime::strn(_generatingTime); if (verbose) { LOG(DEBUG) << " ObsValue:" << _obsValue; LOG(DEBUG) << " FcstValue:" << _fcstValue; } } } //------------------------------------------------------------------ std::string SingleTileThresholds::sprint(int leadTime, int tileIndex, const TileInfo &info, bool verbose) const { char buf[10000]; sprintf(buf, "lt:%08d tile[%3d(%s)] ", leadTime, tileIndex, info.latlonDebugString(tileIndex).c_str()); for (size_t i=0; i<_thresh.size(); ++i) { sprintf(buf+strlen(buf), "%s ", _thresh[i].sprint().c_str()); } sprintf(buf+strlen(buf), "bias:%10.8lf ", _bias); if (_motherTile) { sprintf(buf + strlen(buf), "fromMother "); } else { sprintf(buf + strlen(buf), " "); } if (_coldstart) { sprintf(buf+strlen(buf), "Coldstart"); } else { sprintf(buf+strlen(buf), "ObsTime:%s", DateTime::strn(_generatingTime).c_str()); if (verbose) { sprintf(buf+strlen(buf), " ObsValue:%.8lf", _obsValue); sprintf(buf+strlen(buf), " FcstValue:%.8lf", _fcstValue); } } string s = buf; return s; } //------------------------------------------------------------------ bool SingleTileThresholds::namesOk(const std::vector &names, bool printErrors) const { if (names.size() != _thresh.size()) { if (printErrors) { LOG(ERROR) << "Uneven sizes " << names.size() << " " << _thresh.size(); } return false; } for (size_t i=0; i(i); } } LOG(ERROR) << "Name not found " << fieldName; return -1; } //------------------------------------------------------------------ bool SingleTileThresholds::getIthThreshold(int i, double &thresh) const { if (i >= 0 && i < static_cast(_thresh.size())) { thresh = _thresh[i].getThresh(); return true; } else { return false; } } //------------------------------------------------------------------ bool SingleTileThresholds::getIthName(int i, std::string &name) const { if (i >= 0 && i < static_cast(_thresh.size())) { name = _thresh[i].getField(); return true; } else { return false; } } //------------------------------------------------------------------ bool SingleTileThresholds::_replaceValue(const std::string &fieldName, const SingleTileThresholds &filtMap) { for (size_t i=0; i<_thresh.size(); ++i) { if (_thresh[i].fieldMatch(fieldName)) { for (size_t j=0; j