Skip to content

Commit

Permalink
Per #2924, update logic for handling RegridInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnHalleyGotway committed Sep 16, 2024
1 parent c71c72e commit fa719a1
Show file tree
Hide file tree
Showing 18 changed files with 158 additions and 111 deletions.
3 changes: 2 additions & 1 deletion src/basic/vx_config/config_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ struct InterpInfo {
void clear();
void validate(); // Ensure that width and method are accordant
bool operator==(const InterpInfo &) const;
InterpInfo &operator=(const InterpInfo &a) noexcept; // SoanrQube findings
InterpInfo &operator=(const InterpInfo &a) noexcept; // SonarQube findings
};

////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -329,6 +329,7 @@ struct RegridInfo {
void validate(); // ensure that width and method are accordant
void validate_point(); // ensure that width and method are accordant
RegridInfo &operator=(const RegridInfo &a) noexcept; // SoanrQube findings
ConcatString get_str() const;
};

////////////////////////////////////////////////////////////////////////
Expand Down
123 changes: 67 additions & 56 deletions src/basic/vx_config/config_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,13 @@ RegridInfo &RegridInfo::operator=(const RegridInfo &a) noexcept {
return *this;
}

///////////////////////////////////////////////////////////////////////////////

ConcatString RegridInfo::get_str() const {
ConcatString cs(interpmthd_to_string(method));
cs << "(" << width << ")";
return cs;
}

///////////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -1332,12 +1339,9 @@ BootInfo parse_conf_boot(Dictionary *dict) {
return info;
}


///////////////////////////////////////////////////////////////////////////////

RegridInfo parse_conf_regrid(Dictionary *dict, Dictionary *default_dict, bool error_out) {
Dictionary *regrid_dict = nullptr;
Dictionary *regrid_default = nullptr;
RegridInfo parse_conf_regrid(Dictionary *dict, RegridInfo *default_info, bool error_out) {
RegridInfo info;

if(!dict) {
Expand All @@ -1347,15 +1351,10 @@ RegridInfo parse_conf_regrid(Dictionary *dict, Dictionary *default_dict, bool er
}

// Conf: regrid
regrid_dict = dict->lookup_dictionary(conf_key_regrid, false);

// Dictionary with default settings
if(default_dict) {
regrid_default = default_dict->lookup_dictionary(conf_key_regrid, false);
}
Dictionary *regrid_dict = dict->lookup_dictionary(conf_key_regrid, false);

// Check that the regrid dictionary is present
if(!regrid_dict && !regrid_default) {
if(!regrid_dict && !default_info) {
if(error_out) {
mlog << Error << "\nparse_conf_regrid() -> "
<< "can't find the \"regrid\" dictionary!\n\n";
Expand All @@ -1369,8 +1368,7 @@ RegridInfo parse_conf_regrid(Dictionary *dict, Dictionary *default_dict, bool er
// Conf: to_grid (optional) as an integer or string
const DictionaryEntry * entry = nullptr;

if(regrid_dict) entry = regrid_dict->lookup(conf_key_to_grid, false);
if(!entry && regrid_default) entry = regrid_default->lookup(conf_key_to_grid, false);
if(regrid_dict) entry = regrid_dict->lookup(conf_key_to_grid, false);

// to_grid found
if(entry) {
Expand All @@ -1395,6 +1393,11 @@ RegridInfo parse_conf_regrid(Dictionary *dict, Dictionary *default_dict, bool er
exit(1);
}
}
// Use default RegridInfo
else if(default_info){
info.name = default_info->name;
info.enable = default_info->enable;
}
// to_grid not found
else {
info.name = "";
Expand All @@ -1406,77 +1409,82 @@ RegridInfo parse_conf_regrid(Dictionary *dict, Dictionary *default_dict, bool er
if(regrid_dict && regrid_dict->lookup(conf_key_vld_thresh, false)) {
v_dbl = regrid_dict->lookup_double(conf_key_vld_thresh);
}
else if(regrid_default && regrid_default->lookup(conf_key_vld_thresh, false)) {
v_dbl = regrid_default->lookup_double(conf_key_vld_thresh);
// Use default RegridInfo
else if(default_info) {
v_dbl = default_info->vld_thresh;
}
info.vld_thresh = (is_bad_data(v_dbl) ? default_vld_thresh : v_dbl);

// Conf: method (required)
int v_int = bad_data_int;
if(regrid_dict && regrid_dict->lookup(conf_key_method, false)) {
v_int = regrid_dict->lookup_int(conf_key_method);
info.method = int_to_interpmthd(regrid_dict->lookup_int(conf_key_method));
}
else if(regrid_default && regrid_default->lookup(conf_key_method, false)) {
v_int = regrid_default->lookup_int(conf_key_method);
// Use default RegridInfo
else if(default_info) {
info.method = default_info->method;
}
info.method = int_to_interpmthd(v_int);

// Conf: width (required)
v_int = bad_data_int;
if(regrid_dict && regrid_dict->lookup(conf_key_width, false)) {
v_int = regrid_dict->lookup_int(conf_key_width);
info.width = regrid_dict->lookup_int(conf_key_width);
}
else if(regrid_default && regrid_default->lookup(conf_key_width, false)) {
v_int = regrid_default->lookup_int(conf_key_width);
// Use default RegridInfo
else if(default_info) {
info.width = default_info->width;
}
info.width = v_int;

// Conf: shape (optional)
v_int = bad_data_int;
if(regrid_dict && regrid_dict->lookup(conf_key_shape, false)) {
v_int = regrid_dict->lookup_int(conf_key_shape);
info.shape = int_to_gridtemplate(regrid_dict->lookup_int(conf_key_shape));
}
else if(regrid_default && regrid_default->lookup(conf_key_shape, false)) {
v_int = regrid_default->lookup_int(conf_key_shape);
// Use default RegridInfo
else if(default_info) {
info.shape = default_info->shape;
}
// Use global default
else {
info.shape = GridTemplateFactory::GridTemplates::Square;
}
// Default is square
info.shape = (is_bad_data(v_int) ?
GridTemplateFactory::GridTemplates::Square :
int_to_gridtemplate(v_int));

// Conf: gaussian_dx (optional)
v_dbl = bad_data_double;
if(regrid_dict && regrid_dict->lookup(conf_key_gaussian_dx, false)) {
v_dbl = regrid_dict->lookup_double(conf_key_gaussian_dx);
info.gaussian.dx = regrid_dict->lookup_double(conf_key_gaussian_dx);
}
else if(regrid_default && regrid_default->lookup(conf_key_gaussian_dx, false)) {
v_dbl = regrid_default->lookup_double(conf_key_gaussian_dx);
// Use default RegridInfo
else if(default_info) {
info.gaussian.dx = default_info->gaussian.dx;
}
// Use global default
else {
info.gaussian.dx = default_gaussian_dx;
}
info.gaussian.dx = (is_bad_data(v_dbl) ? default_gaussian_dx : v_dbl);

// Conf: gaussian_radius (optional)
v_dbl = bad_data_double;
if(regrid_dict && regrid_dict->lookup(conf_key_gaussian_radius, false)) {
v_dbl = regrid_dict->lookup_double(conf_key_gaussian_radius);
info.gaussian.radius = regrid_dict->lookup_double(conf_key_gaussian_radius);
}
else if(regrid_default && regrid_default->lookup(conf_key_gaussian_radius, false)) {
v_dbl = regrid_default->lookup_double(conf_key_gaussian_radius);
// Use default RegridInfo
else if(default_info) {
info.gaussian.radius = default_info->gaussian.radius;
}
// Use global default
else {
info.gaussian.radius = default_gaussian_radius;
}
info.gaussian.radius = (is_bad_data(v_dbl) ?
default_gaussian_radius :
v_dbl);

// Conf: gaussian_trunc_factor (optional)
v_dbl = bad_data_double;
if(regrid_dict && regrid_dict->lookup(conf_key_trunc_factor, false)) {
v_dbl = regrid_dict->lookup_double(conf_key_trunc_factor);
}
else if(regrid_default && regrid_default->lookup(conf_key_trunc_factor, false)) {
v_dbl = regrid_default->lookup_double(conf_key_trunc_factor);
// Use default RegridInfo
else if(default_info) {
info.gaussian.trunc_factor = default_info->gaussian.trunc_factor;
}
// Use global default
else {
info.gaussian.trunc_factor = default_trunc_factor;
}
info.gaussian.trunc_factor = (is_bad_data(v_dbl) ?
default_trunc_factor :
v_dbl);

if(info.method == InterpMthd::Gaussian ||
info.method == InterpMthd::MaxGauss) {
Expand All @@ -1491,24 +1499,27 @@ RegridInfo parse_conf_regrid(Dictionary *dict, Dictionary *default_dict, bool er
if(regrid_dict && regrid_dict->lookup(conf_key_convert, false)) {
info.convert_fx.set(regrid_dict->lookup(conf_key_convert));
}
else if(regrid_default && regrid_default->lookup(conf_key_convert, false)) {
info.convert_fx.set(regrid_default->lookup(conf_key_convert));
// Use default RegridInfo
else if(default_info) {
info.convert_fx = default_info->convert_fx;
}

// Conf: censor_thresh (optional)
if(regrid_dict && regrid_dict->lookup(conf_key_censor_thresh, false)) {
info.censor_thresh = regrid_dict->lookup_thresh_array(conf_key_censor_thresh);
}
else if(regrid_default && regrid_default->lookup(conf_key_censor_thresh, false)) {
info.censor_thresh = regrid_default->lookup_thresh_array(conf_key_censor_thresh);
// Use default RegridInfo
else if(default_info) {
info.censor_thresh = default_info->censor_thresh;
}

// Conf: censor_val (optional)
if(regrid_dict && regrid_dict->lookup(conf_key_censor_val, false)) {
info.censor_val = regrid_dict->lookup_num_array(conf_key_censor_val);
}
else if(regrid_default && regrid_default->lookup(conf_key_censor_val, false)) {
info.censor_val = regrid_default->lookup_num_array(conf_key_censor_val);
// Use default RegridInfo
else if(default_info) {
info.censor_val = default_info->censor_val;
}

// Validate the settings
Expand Down
2 changes: 1 addition & 1 deletion src/basic/vx_config/config_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ extern std::map<ConcatString,UserFunc_1Arg>
extern BootInfo parse_conf_boot(Dictionary *dict);
extern RegridInfo parse_conf_regrid(
Dictionary *dict,
Dictionary *default_dict=nullptr,
RegridInfo *default_info=nullptr,
bool error_out=default_dictionary_error_out);
extern InterpInfo parse_conf_interp(Dictionary *dict, const char *);
extern NbrhdInfo parse_conf_nbrhd(Dictionary *dict, const char *);
Expand Down
54 changes: 33 additions & 21 deletions src/libcode/vx_data2d/var_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ void VarInfo::assign(const VarInfo &v) {
nBins = v.nBins;
Range = v.Range;

DefaultRegrid = v.DefaultRegrid;
Regrid = v.Regrid;

SetAttrName = v.SetAttrName;
Expand Down Expand Up @@ -176,6 +177,7 @@ void VarInfo::clear() {
nBins = 0;
Range.clear();

DefaultRegrid.clear();
Regrid.clear();

SetAttrName.clear();
Expand Down Expand Up @@ -215,26 +217,29 @@ void VarInfo::dump(ostream &out) const {

// Dump out the contents
out << "VarInfo::dump():\n"
<< " MagicStr = " << MagicStr.contents() << "\n"
<< " ReqName = " << ReqName.contents() << "\n"
<< " Name = " << Name.contents() << "\n"
<< " LongName = " << LongName.contents() << "\n"
<< " Units = " << Units.contents() << "\n"
<< " PFlag = " << PFlag << "\n"
<< " PName = " << PName.contents() << "\n"
<< " PUnits = " << PUnits.contents() << "\n"
<< " PAsScalar = " << PAsScalar << "\n"
<< " UVIndex = " << UVIndex << "\n"
<< " Init = " << init_str << " (" << Init << ")\n"
<< " Valid = " << valid_str << " (" << Valid << ")\n"
<< " Ensemble = " << Ensemble.contents() << "\n"
<< " Lead = " << lead_str << " (" << Lead << ")\n"
<< " ConvertFx = " << (ConvertFx.is_set() ? "IsSet" : "(nul)") << "\n"
<< " CensorThresh = " << CensorThresh.get_str() << "\n"
<< " CensorVal = " << CensorVal.serialize() << "\n"
<< " nBins = " << nBins << "\n"
<< " Range = " << Range.serialize() << "\n"
<< " Regrid = " << interpmthd_to_string(Regrid.method) << "\n";
<< " MagicStr = " << MagicStr.contents() << "\n"
<< " ReqName = " << ReqName.contents() << "\n"
<< " Name = " << Name.contents() << "\n"
<< " LongName = " << LongName.contents() << "\n"
<< " Units = " << Units.contents() << "\n"
<< " PFlag = " << PFlag << "\n"
<< " PName = " << PName.contents() << "\n"
<< " PUnits = " << PUnits.contents() << "\n"
<< " PAsScalar = " << PAsScalar << "\n"
<< " UVIndex = " << UVIndex << "\n"
<< " Init = " << init_str << " (" << Init << ")\n"
<< " Valid = " << valid_str << " (" << Valid << ")\n"
<< " Ensemble = " << Ensemble.contents() << "\n"
<< " Lead = " << lead_str << " (" << Lead << ")\n"
<< " ConvertFx = " << (ConvertFx.is_set() ? "IsSet" : "(nul)") << "\n"
<< " CensorThresh = " << CensorThresh.get_str() << "\n"
<< " CensorVal = " << CensorVal.serialize() << "\n"
<< " nBins = " << nBins << "\n"
<< " Range = " << Range.serialize() << "\n"
<< " DefaultRegrid = " << interpmthd_to_string(DefaultRegrid.method)
<< "(" << DefaultRegrid.width << ")\n"
<< " Regrid = " << interpmthd_to_string(Regrid.method)
<< "(" << Regrid.width << ")\n";

Level.dump(out);

Expand Down Expand Up @@ -425,6 +430,13 @@ void VarInfo::set_range(const NumArray &a) {

///////////////////////////////////////////////////////////////////////////////

void VarInfo::set_default_regrid(const RegridInfo &ri) {
DefaultRegrid = ri;
return;
}

///////////////////////////////////////////////////////////////////////////////

void VarInfo::set_regrid(const RegridInfo &ri) {
Regrid = ri;
return;
Expand Down Expand Up @@ -528,7 +540,7 @@ void VarInfo::set_dict(Dictionary &dict) {
if(dict.last_lookup_status()) set_range(na);

// Parse regrid, if present
Regrid = parse_conf_regrid(&dict, nullptr, false);
Regrid = parse_conf_regrid(&dict, &DefaultRegrid, false);

// Parse set_attr strings
SetAttrName =
Expand Down
4 changes: 3 additions & 1 deletion src/libcode/vx_data2d/var_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class VarInfo
int nBins; // Number of pdf bins
NumArray Range; // Range of pdf bins

RegridInfo Regrid; // Regridding logic
RegridInfo DefaultRegrid; // Default regridding logic
RegridInfo Regrid; // Regridding logic

// Options to override metadata
ConcatString SetAttrName;
Expand Down Expand Up @@ -189,6 +190,7 @@ class VarInfo
void set_n_bins(const int &);
void set_range(const NumArray &);

void set_default_regrid(const RegridInfo &);
void set_regrid(const RegridInfo &);

void set_level_info_grib(Dictionary & dict);
Expand Down
3 changes: 2 additions & 1 deletion src/libcode/vx_statistics/apply_mask.cc
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,8 @@ DataPlane parse_geog_data(Dictionary *dict, const Grid &vx_grid,
regrid_info = parse_conf_regrid(dict);
mlog << Debug(2)
<< "Regridding geography mask data " << info->magic_str()
<< " to the verification grid.\n";
<< " to the verification grid using "
<< regrid_info.get_str() << ".\n";
dp = met_regrid(dp, mtddf->grid(), vx_grid, regrid_info);
}
}
Expand Down
Loading

0 comments on commit fa719a1

Please sign in to comment.