Skip to content

Commit

Permalink
#2231 Added ugrid_max_distance_km
Browse files Browse the repository at this point in the history
  • Loading branch information
Howard Soh committed Sep 20, 2023
1 parent d33c387 commit 5a05eaa
Show file tree
Hide file tree
Showing 17 changed files with 170 additions and 158 deletions.
7 changes: 4 additions & 3 deletions src/basic/vx_config/config_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -675,14 +675,15 @@ static const char conf_key_trunc_factor[] = "gaussian_trunc_factor";
static const char conf_key_eclv_points[] = "eclv_points";
static const char conf_key_var_name_map[] = "var_name_map";
static const char conf_key_metadata_map[] = "metadata_map";
static const char conf_key_ugrid_map_config[] = "ugrid_map_config";
static const char conf_key_ugrid_metadata_map[]= "ugrid_metadata_map";
static const char conf_key_obs_to_qc_map[] = "obs_to_qc_map";
static const char conf_key_missing_thresh[] = "missing_thresh";
static const char conf_key_control_id[] = "control_id";
static const char conf_key_ens_member_ids[] = "ens_member_ids";
static const char conf_key_seeps_p1_thresh[] = "seeps_p1_thresh";
static const char conf_key_ugrid_metadata_file[] = "ugrid_metadata_file";
static const char conf_key_ugrid_coordinates_file[] = "ugrid_coordinates_file";
static const char conf_key_ugrid_max_distance_km[] = "ugrid_max_distance_km";
static const char conf_key_ugrid_metadata_map[] = "ugrid_metadata_map";
static const char conf_key_ugrid_user_map_config[] = "ugrid_user_map_config";

//
// Entries to override file metadata
Expand Down
43 changes: 15 additions & 28 deletions src/basic/vx_config/config_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ ConcatString parse_conf_string(Dictionary *dict, const char *conf_key,
const char *method_name = "parse_conf_string() -> ";

if(!dict) {
mlog << Error << "\n" << method_name << "empty dictionary!\n\n";
mlog << Error << "\n" << method_name << "empty dictionary! (" << conf_key <<")\n\n";
exit(1);
}

Expand Down Expand Up @@ -1126,13 +1126,6 @@ map<ConcatString,StringArray> parse_conf_metadata_map(Dictionary *dict) {

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

map<ConcatString,StringArray> parse_conf_ugrid_metadata_map(Dictionary *dict) {
const char *method_name = "parse_conf_ugrid_metadata_map() -> ";
return parse_conf_key_values_map(dict, conf_key_ugrid_metadata_map, method_name);
}

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

map<ConcatString,ConcatString> parse_conf_obs_name_map(Dictionary *dict) {
const char *method_name = "parse_conf_obs_name_map() -> ";
return parse_conf_key_value_map(dict, conf_key_obs_name_map);
Expand Down Expand Up @@ -2269,36 +2262,30 @@ WaveletType parse_conf_wavelet_type(Dictionary *dict) {

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

ConcatString parse_conf_ugrid_map_config(Dictionary *dict) {
ConcatString s;

if(!dict) {
mlog << Error << "\nparse_conf_ugrid_map_config() -> "
<< "empty dictionary!\n\n";
exit(1);
}
double parse_conf_ugrid_max_distance_km(Dictionary *dict) {
return dict ? dict->lookup_double(conf_key_ugrid_max_distance_km, false) : bad_data_double;
}

s = dict->lookup_string(conf_key_ugrid_map_config);
///////////////////////////////////////////////////////////////////////////////

return s;
ConcatString parse_conf_ugrid_coordinates_file(Dictionary *dict) {
return dict ? dict->lookup_string(conf_key_ugrid_coordinates_file, false) : "";
}

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

ConcatString parse_conf_ugrid_metadata_file(Dictionary *dict) {
ConcatString s;

if(!dict) {
mlog << Error << "\nparse_conf_ugrid_metadata_file() -> "
<< "empty dictionary!\n\n";
exit(1);
}
map<ConcatString,StringArray> parse_conf_ugrid_metadata_map(Dictionary *dict) {
const char *method_name = "parse_conf_ugrid_metadata_map() -> ";
return parse_conf_key_values_map(dict, conf_key_ugrid_metadata_map, method_name);
}

s = dict->lookup_string(conf_key_ugrid_metadata_file);
///////////////////////////////////////////////////////////////////////////////

return s;
ConcatString parse_conf_ugrid_user_map_config(Dictionary *dict) {
return dict ? dict->lookup_string(conf_key_ugrid_user_map_config, false) : "";
}


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


Expand Down
7 changes: 4 additions & 3 deletions src/basic/vx_config/config_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ extern std::map<ConcatString,ConcatString>
parse_conf_obs_name_map(Dictionary *dict);
extern std::map<ConcatString,StringArray>
parse_conf_obs_to_qc_map(Dictionary *dict);
extern std::map<ConcatString,StringArray> parse_conf_ugrid_metadata_map(Dictionary *dict);
extern std::map<ConcatString,UserFunc_1Arg>
parse_conf_key_convert_map(
Dictionary *dict, const char *conf_key_map_name, const char *caller=nullptr);
Expand All @@ -86,8 +85,10 @@ extern void parse_conf_range_int(Dictionary *dict, int &beg, int &end
extern void parse_conf_range_double(Dictionary *dict, double &beg, double &end);
extern StringArray parse_conf_ens_member_ids(Dictionary *dict);
extern NormalizeType parse_conf_normalize(Dictionary *dict);
extern ConcatString parse_conf_ugrid_map_config(Dictionary *dict);
extern ConcatString parse_conf_ugrid_metadata_file(Dictionary *dict);
extern double parse_conf_ugrid_max_distance_km(Dictionary *dict);
extern ConcatString parse_conf_ugrid_user_map_config(Dictionary *dict);
extern ConcatString parse_conf_ugrid_coordinates_file(Dictionary *dict);
extern std::map<ConcatString,StringArray> parse_conf_ugrid_metadata_map(Dictionary *dict);

extern void check_mask_names(const StringArray &);

Expand Down
9 changes: 7 additions & 2 deletions src/libcode/vx_data2d_ugrid/data2d_ugrid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -676,9 +676,14 @@ long MetUGridDataFile::convert_value_to_offset(double z_value, string z_dim_name

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

void MetUGridDataFile::set_max_distance_km(double max_distance) {
_file->set_max_distance_km(max_distance);
}

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

void MetUGridDataFile::set_map_config_file(ConcatString filename) {
_file->set_map_config_file(filename);
void MetUGridDataFile::set_user_map_config_file(ConcatString filename) {
_file->set_user_map_config_file(filename);
}


Expand Down
3 changes: 2 additions & 1 deletion src/libcode/vx_data2d_ugrid/data2d_ugrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ class MetUGridDataFile : public Met2dDataFile {

bool open (const char * filename);
bool open_metadata(const char * filename);
void set_map_config_file(ConcatString filename);
void set_max_distance_km(double max_distance);
void set_user_map_config_file(ConcatString filename);

void close ();

Expand Down
91 changes: 24 additions & 67 deletions src/libcode/vx_data2d_ugrid/ugrid_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ using namespace netCDF;

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

static const char *def_user_config = "UGridConfig_user"";
static const char *def_user_config = "UGridConfig_user";
static const char *def_config_filename =
"MET_BASE/config/UGridConfig_default";

Expand Down Expand Up @@ -100,6 +100,7 @@ void UGridFile::init_from_scratch()
_lonVar = (NcVar *)nullptr;
_xCoordVar = (NcVar *)nullptr;
_yCoordVar = (NcVar *)nullptr;
max_distance_km = bad_data_double;

// Close any existing file

Expand Down Expand Up @@ -172,8 +173,8 @@ bool UGridFile::open(const char * filepath)

// Close any open files and clear out the associated members
close();
ConcatString config_file = def_config_filename
if (file_exists(default_user_config.c_str())) config_file = def_user_config;
ConcatString config_file = file_exists(def_user_config)
? def_user_config : def_config_filename;
read_config(config_file);

if (_ncFile) {
Expand Down Expand Up @@ -445,13 +446,7 @@ void UGridFile::dump(ostream & out, int depth) const
grid.dump(out, depth + 1);

out << prefix << "\n";

out << prefix << "Nc = " << (_ncFile ? "ok" : "(nul)") << "\n";

out << prefix << "\n";

out << prefix << "Ndims = " << _numDims << "\n";

out << prefix << "\n";

out << prefix << "face_dim = " << (_faceDim ? GET_NC_NAME_P(_faceDim) : "(nul)") << "\n";
Expand Down Expand Up @@ -528,8 +523,6 @@ std::string UGridFile::find_metadata_name(std::string &key, StringArray &availab
for (int idx=0; idx<meta_names.n(); idx++) {
if (available_names.has(meta_names[idx])) {
meta_name = meta_names[idx];
//mlog << Debug(6) << "UGridFile::find_metadata_name --> found " << meta_name
// << " for " << key << "\n";
break;
}
}
Expand Down Expand Up @@ -559,37 +552,6 @@ double UGridFile::getData(NcVar * var, const LongArray & a) const
static const string method_name
= "UGridFile::getData(NcVar *, const LongArray &) -> ";

/*
if (!args_ok(a))
{
mlog << Error << "\n" << method_name
<< "bad arguments:\n";
a.dump(cerr);
exit(1);
}
int dim_count = get_dim_count(var);
if (dim_count != a.n_elements())
{
mlog << Error << "\n" << method_name
<< "needed " << (dim_count) << " arguments for variable "
<< (GET_NC_NAME_P(var)) << ", got " << (a.n_elements()) << "\n\n";
exit(1);
}
for (int k=0; k<dim_count; k++) {
int dim_size = var->getDim(k).getSize();
if (dim_size < a[k]) {
mlog << Error << "\n" << method_name
<< "offset (" << a[k] << ") at " << k
<< "th dimension (" << long(dim_size) << ") is too big for variable \""
<< GET_NC_NAME_P(var) << "\"\n\n";
exit ( 1 );
}
}
*/

bool status = false;
double d = bad_data_double;

Expand Down Expand Up @@ -627,23 +589,6 @@ bool UGridFile::getData(NcVar * v, const LongArray & a, DataPlane & plane) const
static const string method_name
= "UGridFile::getData(NcVar *, const LongArray &, DataPlane &) const -> ";

// if (!args_ok(a))
// {
// mlog << Error << "\n" << method_name
// << "bad arguments:\n";
// a.dump(cerr);
// exit(1);
// }
//
// int dim_count = get_dim_count(v);
// if (dim_count != a.n_elements())
// {
// mlog << Error << "\n" << method_name
// << "needed " << (dim_count) << " arguments for variable "
// << (GET_NC_NAME_P(v)) << ", got " << (a.n_elements()) << "\n\n";
// exit(1);
// }

// find varinfo's

NcVarInfo *var = find_by_name(GET_NC_NAME_P(v).c_str());
Expand Down Expand Up @@ -780,9 +725,6 @@ bool UGridFile::get_var_info() {
for (int j=0; j<Nvars; ++j) {
if (metadata_names.has(var_names[j]) && !meta_time_names.has(var_names[j])) continue;

//mlog << Debug(7) << method_name
// << "adding data variable " << var_names[j] << "\n";

NcVar v = get_var(_ncFile, var_names[j].c_str());

Var[j].var = new NcVar(v);
Expand Down Expand Up @@ -882,13 +824,13 @@ void UGridFile::read_netcdf_grid()
}
if (get_var_units(_lonVar, units_value)) {
if (units_value == "rad" || units_value == "radian") {
mlog << Debug(6) << method_name << " convert lont (" <<units_value << ") to degree\n";
mlog << Debug(6) << method_name << " convert lon (" <<units_value << ") to degree\n";
for (int idx=0; idx<face_count; idx++) _lon[idx] /= rad_per_deg;
}
}
//for (int idx=0; idx<face_count; idx++) cout << method_name << " lon/lat: " << _lon[idx] << ", " << _lat[idx] << "\n";

grid_data.set_points(face_count, _lon, _lat);
grid_data.max_distance_km = max_distance_km;

grid.set(grid_data);

Expand All @@ -903,14 +845,29 @@ void UGridFile::read_netcdf_grid()

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

void UGridFile::set_map_config_file(ConcatString filename) {
void UGridFile::set_max_distance_km(double max_distance) {

max_distance_km = max_distance;
if (grid.is_set()) {
UnstructuredData D;
D.copy_from(grid.info().us);
D.max_distance_km = max_distance;
grid.set(D);
}

}

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

void UGridFile::set_user_map_config_file(ConcatString filename) {

if (file_exists(filename.c_str())) {
read_config(filename.c_str());
get_var_info();
}
else mlog << Warning << "\nUGridFile::set_map_config_file() The UGrid metadata mapping configuration file \""
<< filename << " does not exist. Use the default mapping\n\n";
else mlog << Warning << "\nUGridFile::set_user_map_config_file()"
<< " The UGrid metadata mapping configuration file \""
<< filename << "\" does not exist. Use the default mapping\n\n";

}

Expand Down
4 changes: 3 additions & 1 deletion src/libcode/vx_data2d_ugrid/ugrid_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class UGridFile {
bool open(const char *filename);
bool open_metadata(const char *filename);
bool get_var_info();
void set_map_config_file(ConcatString filename);
void set_max_distance_km(double max_distance);
void set_user_map_config_file(ConcatString filename);

void close();

Expand Down Expand Up @@ -114,6 +115,7 @@ class UGridFile {

std::map<ConcatString,StringArray> metadata_map;
StringArray metadata_names;
double max_distance_km;

//
// dimensions
Expand Down
34 changes: 20 additions & 14 deletions src/libcode/vx_grid/grid_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,22 @@ void UnstructuredData::clear() {

name = (const char *) nullptr;

Nface = Nnode = Nedge = 0;
max_distance_km = bad_data_double; // disable max_distance

clear_data();

}


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

void UnstructuredData::clear_data() {

Nface = 0;
//distance = -1.; // disable distance
pointLonLat.clear();
lat_checksum = lon_checksum = 0.;

pointLonLat.clear();
if (kdtree) { delete kdtree; kdtree = nullptr; }

}
Expand All @@ -325,9 +336,10 @@ void UnstructuredData::dump() const {

mlog << Debug(grid_debug_level)
<< "\nUnstructured Grid Data:\n"
<< " Nface: " << Nface << "\n"
<< " lat_checksum: " << lat_checksum << "\n"
<< " lon_checksum: " << lon_checksum << "\n"
<< " Nface: " << Nface << "\n"
<< " lat_checksum: " << lat_checksum << "\n"
<< " lon_checksum: " << lon_checksum << "\n"
<< " max_distance_km: " << max_distance_km << "\n"
;
}

Expand Down Expand Up @@ -733,17 +745,11 @@ void GridInfo::set(const UnstructuredData & data)

clear();

/*
us = new UnstructuredData();
memcpy(D, &data, sizeof(data));
us->Nface = data.Nface;
us->copy_data(data.pointLonLat);
//us = new UnstructuredData(data);
*/

UnstructuredData *D = new UnstructuredData;

//D->Nface = data.Nface;
D->Nedge = data.Nedge;
D->Nnode = data.Nnode;
D->max_distance_km = data.max_distance_km;
D->set_points(data.Nface, data.pointLonLat);
us = D;
D = (UnstructuredData *)nullptr;
Expand Down
Loading

0 comments on commit 5a05eaa

Please sign in to comment.