Skip to content

Commit

Permalink
Per #2279, switch MaskSID::sid_list from a vector of pairs to a simpl…
Browse files Browse the repository at this point in the history
…er map named sid_map.
  • Loading branch information
JohnHalleyGotway committed Oct 14, 2024
1 parent 13fe94a commit 02302be
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 66 deletions.
8 changes: 3 additions & 5 deletions src/basic/vx_config/config_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,8 @@ struct MaskSID {
// Boolean for non-default weights
bool has_weights;

// Vector of SID name and corresponding weights
std::vector<std::pair<std::string,double>> sid_list;
// Mapping of SID name to weight value
std::map<std::string,double> sid_map;

void clear();
bool operator==(const MaskSID &) const;
Expand All @@ -447,9 +447,7 @@ struct MaskSID {
void add(const std::string &);
void add_css(const std::string &);
bool has(const std::string &) const ;
bool has(const std::string &,
std::pair<std::string,double> *&item_ptr) const;
void sort();
bool has(const std::string &, double &) const;
};

////////////////////////////////////////////////////////////////////////
Expand Down
68 changes: 32 additions & 36 deletions src/basic/vx_config/config_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -606,16 +606,16 @@ StringArray parse_conf_message_type(Dictionary *dict, bool error_out) {
void MaskSID::clear() {
name.clear();
has_weights = false;
sid_list.clear();
sid_map.clear();
}

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

bool MaskSID::operator==(const MaskSID &v) const {
bool match = true;

if(!(name == v.name ) ||
!(sid_list == v.sid_list)) {
if(!(name == v.name ) ||
!(sid_map == v.sid_map)) {
match = false;
}

Expand All @@ -628,15 +628,15 @@ MaskSID &MaskSID::operator=(const MaskSID &a) noexcept {
if(this != &a) {
name = a.name;
has_weights = a.has_weights;
sid_list = a.sid_list;
sid_map = a.sid_map;
}
return *this;
}

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

int MaskSID::n() const {
return sid_list.size();
return (int) sid_map.size();
}

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -653,8 +653,8 @@ void MaskSID::add(const string &text) {
has_weights = true;
}

// Store the station ID, weight pair
sid_list.push_back(pair<string,double>(sid,weight));
// Add station ID map entry
if(sid_map.count(sid) == 0) sid_map[sid] = weight;

return;
}
Expand All @@ -671,49 +671,36 @@ void MaskSID::add_css(const string &text) {
///////////////////////////////////////////////////////////////////////////////

bool MaskSID::has(const string &s) const {
pair<string,double> *item_ptr = nullptr;

return has(s, item_ptr);
return sid_map.count(s) > 0;
}

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

bool MaskSID::has(const string &s, pair<string,double> *&item_ptr) const {
bool match = false;
bool MaskSID::has(const string &s, double &weight) const {
bool found = false;

for(auto item : sid_list) {
if(s == item.first) {
match = true;
item_ptr = &item;
break;
}
if(sid_map.count(s) == 0) {
weight = bad_data_double;
}
else {
found = true;
weight = sid_map.at(s);
}

return match;
}

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

void MaskSID::sort() {

std::sort(sid_list.begin(), sid_list.end());

return;
return found;
}

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

StringArray parse_conf_sid_list(Dictionary *dict, const char *conf_key) {
MaskSID cur;
StringArray sid_sa;
const char *method_name = "parse_conf_sid_list() -> ";

StringArray sa(parse_conf_string_array(dict, conf_key, method_name));

// Append to the list of station ID's
for(int i=0; i<sa.n(); i++) {
cur = parse_sid_mask(string(sa[i]));
for(auto item : cur.sid_list) sid_sa.add(item.first);
sid_sa.add(parse_sid_mask_as_list(string(sa[i])));
}

mlog << Debug(4) << method_name
Expand Down Expand Up @@ -808,8 +795,8 @@ MaskSID parse_sid_mask(const ConcatString &mask_sid_str) {
// One elements means no colon was specified
if(sa.n() == 1) {
mask_sid.add_css(sa[0]);
mask_sid.name = (mask_sid.sid_list.size() == 1 ?
mask_sid.sid_list[0].first : "MASK_SID");
mask_sid.name = (mask_sid.sid_map.size() == 1 ?
mask_sid.sid_map.begin()->first : "MASK_SID");
}
// Two elements means one colon was specified
else if(sa.n() == 2) {
Expand All @@ -825,12 +812,21 @@ MaskSID parse_sid_mask(const ConcatString &mask_sid_str) {

}

// Sort the mask_sid's
mask_sid.sort();

return mask_sid;
}

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

StringArray parse_sid_mask_as_list(const ConcatString &mask_sid_str) {

MaskSID ms = parse_sid_mask(mask_sid_str);

StringArray sa;
for(const auto &pair : ms.sid_map) sa.add(pair.first);

return sa;
}

///////////////////////////////////////////////////////////////////////////////
//
// Code for MaskLatLon struct
Expand Down
1 change: 1 addition & 0 deletions src/basic/vx_config/config_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ extern StringArray parse_conf_sid_list(
Dictionary *dict,
const char *);
extern MaskSID parse_sid_mask(const ConcatString &);
extern StringArray parse_sid_mask_as_list(const ConcatString &);
extern std::vector<MaskLatLon>
parse_conf_llpnt_mask(Dictionary *dict);
extern StringArray parse_conf_obs_qty_inc(Dictionary *dict);
Expand Down
9 changes: 5 additions & 4 deletions src/libcode/vx_analysis_util/stat_job.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1808,13 +1808,14 @@ void STATAnalysisJob::set_mask_sid(const char *c) {
mlog << Debug(1)
<< "Station ID Mask: " << mask_sid_str << "\n";

MaskSID ms = parse_sid_mask(mask_sid_str);
for(auto item : ms.sid_list) mask_sid.add(item.first);
MaskSID ms;
ms = parse_sid_mask(mask_sid_str);
for(const auto &pair : ms.sid_map) mask_sid.add(pair.first);

// List the length of the station ID mask
mlog << Debug(2)
<< "Parsed Station ID Mask: " << ms.name
<< " containing " << mask_sid.n() << " stations\n";
<< "Parsed Station ID Mask (" << ms.name
<< ") containing " << mask_sid.n() << " stations\n";

return;
}
Expand Down
6 changes: 3 additions & 3 deletions src/libcode/vx_statistics/pair_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -801,9 +801,9 @@ void PairBase::set_point_weight(const PointWeightType wgt_flag) {
// Loop through the point observations
for(int i_obs=0; i_obs<n_obs; i_obs++) {

pair<string,double> *item_ptr = nullptr;
if(mask_sid_ptr->has(sid_sa[i_obs], item_ptr)) {
wgt_na.set(i_obs, item_ptr->second);
double wgt;
if(mask_sid_ptr->has(sid_sa[i_obs], wgt)) {
wgt_na.set(i_obs, wgt);
}
else {
mlog << Warning << "\n" << method_name
Expand Down
2 changes: 1 addition & 1 deletion src/libcode/vx_statistics/pair_data_ensemble.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,7 @@ void VxPairDataEnsemble::add_point_obs(float *hdr_arr, int *hdr_typ_arr,
const char *hdr_sid_str,
unixtime hdr_ut,
const char *obs_qty, float *obs_arr,
Grid &gr, const char *var_name) {
const Grid &gr, const char *var_name) {

// Check the observation VarInfo file type
if(obs_info->file_type() != FileType_Gb1) {
Expand Down
2 changes: 1 addition & 1 deletion src/libcode/vx_statistics/pair_data_ensemble.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class VxPairDataEnsemble : public VxPairBase {
void set_skip_const(bool);

void add_point_obs(float *, int *, const char *, const char *,
unixtime, const char *, float *, Grid &,
unixtime, const char *, float *, const Grid &,
const char *);
void add_ens(int, bool mn, Grid &);
};
Expand Down
2 changes: 1 addition & 1 deletion src/libcode/vx_statistics/pair_data_point.cc
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ void VxPairDataPoint::set_size(int types, int masks, int interps) {
void VxPairDataPoint::add_point_obs(float *hdr_arr, const char *hdr_typ_str,
const char *hdr_sid_str, unixtime hdr_ut,
const char *obs_qty, float *obs_arr,
Grid &gr, const char *var_name) {
const Grid &gr, const char *var_name) {

// Increment the number of tries count
n_try++;
Expand Down
3 changes: 2 additions & 1 deletion src/libcode/vx_statistics/pair_data_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ class VxPairDataPoint : public VxPairBase {
void set_seeps_thresh(const SingleThresh &p1_thresh);

void add_point_obs(float *, const char *, const char *, unixtime,
const char *, float *, Grid &, const char *);
const char *, float *, const Grid &,
const char *);
};


Expand Down
6 changes: 3 additions & 3 deletions src/tools/other/ascii2nc/ascii2nc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -749,12 +749,12 @@ void set_mask_sid(const StringArray & a) {
<< "Station ID Mask: " << a[0] << "\n";

MaskSID ms = parse_sid_mask(a[0]);
for(auto item : ms.sid_list) mask_sid.add(item.first);
for(const auto &pair : ms.sid_map) mask_sid.add(pair.first);

// List the length of the station ID mask
mlog << Debug(2)
<< "Parsed Station ID Mask: " << ms.name
<< " containing " << mask_sid.n() << " stations\n";
<< "Parsed Station ID Mask (" << ms.name
<< ") containing " << mask_sid.n() << " stations\n";
}

////////////////////////////////////////////////////////////////////////
Expand Down
6 changes: 3 additions & 3 deletions src/tools/other/ioda2nc/ioda2nc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1561,11 +1561,11 @@ void set_mask_sid(const StringArray & a) {
mlog << Debug(1) << "Station ID Mask: " << a[0] << "\n";

MaskSID ms = parse_sid_mask(a[0]);
for(auto item : ms.sid_list) mask_sid.add(item.first);
for(const auto &pair : ms.sid_map) mask_sid.add(pair.first);

// List the length of the station ID mask
mlog << Debug(2) << "Parsed Station ID Mask: " << ms.name
<< " containing " << mask_sid.n() << " stations\n";
mlog << Debug(2) << "Parsed Station ID Mask (" << ms.name
<< ") containing " << mask_sid.n() << " stations\n";
}

////////////////////////////////////////////////////////////////////////
Expand Down
3 changes: 1 addition & 2 deletions src/tools/other/ioda2nc/ioda2nc_conf_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ void IODA2NCConfInfo::process_config() {
// Conf: station_id
sa = conf.lookup_string_array(conf_key_station_id);
for(i=0; i<sa.n(); i++) {
MaskSID ms = parse_sid_mask(replace_path(sa[i]));
for(auto item : ms.sid_list) station_id.add(item.first);
station_id.add(parse_sid_mask_as_list(replace_path(sa[i])));
}

// Conf: beg_ds and end_ds
Expand Down
8 changes: 4 additions & 4 deletions src/tools/other/madis2nc/madis2nc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3742,13 +3742,13 @@ void set_mask_sid(const StringArray & a) {
mlog << Debug(1)
<< "Station ID Mask: " << a[0] << "\n";

MaskSID ms = parse_sid_mask(a[0]);
for(auto item : ms.sid_list) mask_sid.add(item.first);
MaskSID ms;
for(const auto &pair : ms.sid_map) mask_sid.add(pair.first);

// List the length of the station ID mask
mlog << Debug(2)
<< "Parsed Station ID Mask: " << ms.name
<< " containing " << mask_sid.n() << " stations\n";
<< "Parsed Station ID Mask (" << ms.name
<< ") containing " << mask_sid.n() << " stations\n";
}

////////////////////////////////////////////////////////////////////////
Expand Down
3 changes: 1 addition & 2 deletions src/tools/other/pb2nc/pb2nc_conf_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ void PB2NCConfInfo::process_config() {
// Conf: station_id
sa = conf.lookup_string_array(conf_key_station_id);
for(i=0; i<sa.n(); i++) {
MaskSID ms = parse_sid_mask(replace_path(sa[i]));
for(auto item : ms.sid_list) station_id.add(item.first);
station_id.add(parse_sid_mask_as_list(replace_path(sa[i])));
}

// Conf: beg_ds and end_ds
Expand Down

0 comments on commit 02302be

Please sign in to comment.