From 8818d62df766dd5211cfbf3d7a9de89ad2f013a2 Mon Sep 17 00:00:00 2001 From: Howard Soh Date: Mon, 18 Sep 2023 15:17:56 -0600 Subject: [PATCH] #2231 Renamed to metadata_map to ugrid_metadata_map. Allow to override ugrid_metadata_map --- data/config/UGridConfig_default | 6 +- src/libcode/vx_data2d_ugrid/data2d_ugrid.cc | 45 +-- src/libcode/vx_data2d_ugrid/data2d_ugrid.h | 1 + src/libcode/vx_data2d_ugrid/ugrid_file.cc | 193 ++++++----- src/libcode/vx_data2d_ugrid/ugrid_file.h | 6 +- src/libcode/vx_data2d_ugrid/var_info_ugrid.cc | 328 +++++++++--------- src/libcode/vx_data2d_ugrid/var_info_ugrid.h | 2 +- 7 files changed, 290 insertions(+), 291 deletions(-) diff --git a/data/config/UGridConfig_default b/data/config/UGridConfig_default index 0fd8b355c7..27f5133570 100644 --- a/data/config/UGridConfig_default +++ b/data/config/UGridConfig_default @@ -2,14 +2,16 @@ // // Unstructured Grid configuration file. // -// For additional information, please see the MET User's Guide. +// UGridConfig_user at the current working directory overrides the default. +// For additional or updated information, please see the MET User's Guide. // //////////////////////////////////////////////////////////////////////////////// // // Default mapping for Metadata. +// The first values are for MPAS and the second values are for LFric. // -metadata_map = [ +ugrid_metadata_map = [ { key = "dim_face"; val = "nCells,nMesh2d_face"; }, { key = "dim_node"; val = "nVertices,nMesh2d_node"; }, { key = "dim_edge"; val = "nEdges,nMesh2d_edge"; }, diff --git a/src/libcode/vx_data2d_ugrid/data2d_ugrid.cc b/src/libcode/vx_data2d_ugrid/data2d_ugrid.cc index 3a1582d0e6..f4d842e213 100644 --- a/src/libcode/vx_data2d_ugrid/data2d_ugrid.cc +++ b/src/libcode/vx_data2d_ugrid/data2d_ugrid.cc @@ -85,27 +85,6 @@ void MetUGridDataFile::ugrid_init_from_scratch() { return; } -//////////////////////////////////////////////////////////////////////// -/* -NcVarInfo *MetUGridDataFile::find_first_data_var() { - NcVarInfo *first_data_var = nullptr; - // Store the name of the first data variable - for (int i = 0; i < _file->Nvars; ++i) { - if (is_nc_unit_time(_file->Var[i].units_att.c_str()) || - is_nc_unit_longitude(_file->Var[i].units_att.c_str()) || - is_nc_unit_latitude(_file->Var[i].units_att.c_str()) || - _file->get_time_var_info() == &_file->Var[i] - ) continue; - - if (strcmp(_file->Var[i].name.c_str(), ugrid_lat_var_name) != 0 && - strcmp(_file->Var[i].name.c_str(), ugrid_lon_var_name) != 0) { - first_data_var = &(_file->Var[i]); - break; - } - } - return first_data_var; -} -*/ //////////////////////////////////////////////////////////////////////// void MetUGridDataFile::close() { @@ -211,7 +190,7 @@ cout << " FIXME MetUGridDataFile::data_plane(VarInfo &vinfo, DataPlane &plane) LongArray dimension = vinfo_nc->dimension(); BoolArray is_offset = vinfo_nc->is_offset(); - data_var = _file->find_var_name(vinfo_nc->req_name().c_str()); + data_var = _file->find_by_name(vinfo_nc->req_name().c_str()); /* if (nullptr != data_var) { time_dim_slot = data_var->t_slot; @@ -361,7 +340,7 @@ cout << " FIXME MetUGridDataFile::data_plane(VarInfo &vinfo, DataPlane &plane) //////////////////////////////////////////////////////////////////////// int MetUGridDataFile::data_plane_array(VarInfo &vinfo, - DataPlaneArray &plane_array) { + DataPlaneArray &plane_array) { int n_rec = 0; DataPlane plane; bool status = false; @@ -371,14 +350,6 @@ int MetUGridDataFile::data_plane_array(VarInfo &vinfo, // Initialize plane_array.clear(); - VarInfoUGrid *vinfo_nc = (VarInfoUGrid *)&vinfo; - if ( vinfo_nc->req_name() == na_str ) { - // Store the name of the first data variable -cout << " FIXME MetUGridDataFile::data_plane_array() calling find_first_data_var()\n"; -// NcVarInfo *data_var = find_first_data_var(); -// if (nullptr != data_var) vinfo_nc->set_req_name(data_var->name.c_str()); - } - LongArray time_offsets = collect_time_offsets(vinfo); if (0 < time_offsets.n_elements()) { LevelInfo level = vinfo.level(); @@ -420,7 +391,7 @@ LongArray MetUGridDataFile::collect_time_offsets(VarInfo &vinfo) { = "MetUGridDataFile::collect_time_offsets(VarInfo &) -> "; LongArray time_offsets; - NcVarInfo *info = _file->find_var_name(vinfo_nc->req_name().c_str()); + NcVarInfo *info = _file->find_by_name(vinfo_nc->req_name().c_str()); // Check for variable not found if(!info) { @@ -622,7 +593,7 @@ LongArray MetUGridDataFile::collect_time_offsets(VarInfo &vinfo) { int MetUGridDataFile::index(VarInfo &vinfo){ - if( nullptr == _file->find_var_name( vinfo.name().c_str() ) ) return -1; + if( nullptr == _file->find_by_name( vinfo.name().c_str() ) ) return -1; if( ( vinfo.valid() != 0 && _file->ValidTime[0] != vinfo.valid() ) || ( vinfo.init() != 0 && _file->InitTime != vinfo.init() ) || @@ -704,3 +675,11 @@ long MetUGridDataFile::convert_value_to_offset(double z_value, string z_dim_name } //////////////////////////////////////////////////////////////////////// + + +void MetUGridDataFile::set_map_config_file(ConcatString filename) { + _file->set_map_config_file(filename); +} + + +//////////////////////////////////////////////////////////////////////// diff --git a/src/libcode/vx_data2d_ugrid/data2d_ugrid.h b/src/libcode/vx_data2d_ugrid/data2d_ugrid.h index 473254b660..7a3b1af17c 100644 --- a/src/libcode/vx_data2d_ugrid/data2d_ugrid.h +++ b/src/libcode/vx_data2d_ugrid/data2d_ugrid.h @@ -100,6 +100,7 @@ class MetUGridDataFile : public Met2dDataFile { bool open (const char * filename); bool open_metadata(const char * filename); + void set_map_config_file(ConcatString filename); void close (); diff --git a/src/libcode/vx_data2d_ugrid/ugrid_file.cc b/src/libcode/vx_data2d_ugrid/ugrid_file.cc index 2c6a2c8dd1..153ef46dff 100644 --- a/src/libcode/vx_data2d_ugrid/ugrid_file.cc +++ b/src/libcode/vx_data2d_ugrid/ugrid_file.cc @@ -35,7 +35,8 @@ using namespace netCDF; //////////////////////////////////////////////////////////////////////// -static const char *default_config_filename = +static const char *def_user_config = "UGridConfig_user""; +static const char *def_config_filename = "MET_BASE/config/UGridConfig_default"; array DIM_KEYS = { @@ -71,7 +72,6 @@ UGridFile::UGridFile() //////////////////////////////////////////////////////////////////////// - UGridFile::~UGridFile() { close(); @@ -172,7 +172,9 @@ bool UGridFile::open(const char * filepath) // Close any open files and clear out the associated members close(); - read_config(); + ConcatString config_file = def_config_filename + if (file_exists(default_user_config.c_str())) config_file = def_user_config; + read_config(config_file); if (_ncFile) { delete _ncFile; @@ -188,49 +190,7 @@ bool UGridFile::open(const char * filepath) return false; } - // Pull out the variables - - NcDim dim; - int meta_count = 0; - int max_dim_count = 0; - ConcatString att_value; - StringArray var_names; - - Nvars = get_var_names(_ncFile, &var_names); - for (int j=0; j max_dim_count) max_dim_count = dim_count; - - Var[j].Dims = new NcDim * [dim_count]; - - // parse the variable attributes - get_att_str( Var[j], long_name_att_name, Var[j].long_name_att ); - get_att_str( Var[j], units_att_name, Var[j].units_att ); - - } // for j - - // done - - return true; + return get_var_info(); } @@ -579,12 +539,10 @@ std::string UGridFile::find_metadata_name(std::string &key, StringArray &availab //////////////////////////////////////////////////////////////////////// -NcVarInfo* UGridFile::find_var_name(const char * var_name) const +NcVarInfo* UGridFile::find_by_name(const char * var_name) const { - cout << "UGridFile::find_var_name() Nvars=" << Nvars << ", " << var_name << "\n"; for (int i = 0; i < Nvars; i++) { - cout << "UGridFile::find_var_name() " << Var[i].name << " VS " << var_name << "\n"; if (Var[i].name == var_name) return &Var[i]; } @@ -600,6 +558,8 @@ double UGridFile::getData(NcVar * var, const LongArray & a) const clock_t start_clock = clock(); static const string method_name = "UGridFile::getData(NcVar *, const LongArray &) -> "; + +/* if (!args_ok(a)) { mlog << Error << "\n" << method_name @@ -628,6 +588,7 @@ double UGridFile::getData(NcVar * var, const LongArray & a) const exit ( 1 ); } } +*/ bool status = false; double d = bad_data_double; @@ -636,7 +597,8 @@ double UGridFile::getData(NcVar * var, const LongArray & a) const double missing_value = get_var_missing_value(var); get_var_fill_value(var, fill_value); - status = get_nc_data(var, &d, a); + //status = get_nc_data(var, &d, a); + status = get_nc_data(var, a); if (!status) { @@ -665,40 +627,28 @@ 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); - } +// 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 - bool found = false; - NcVarInfo *var = (NcVarInfo *)nullptr; - - for (int j = 0; j < Nvars; ++j) - { - if (Var[j].var == v) - { - found = true; - var = Var + j; - break; - } - } + NcVarInfo *var = find_by_name(GET_NC_NAME_P(v).c_str()); - if (!found) - { + if (nullptr == var) { mlog << Error << "\n" << method_name << "variable " << (GET_NC_NAME_P(v)) << " not found!\n\n"; exit(1); @@ -723,9 +673,6 @@ bool UGridFile::getData(NcVar * v, const LongArray & a, DataPlane & plane) const plane.set_size(nx, ny); // get the data - int *i; - short *s; - float *f; const int plane_size = nx * ny; double *d = new double[plane_size]; @@ -760,9 +707,7 @@ bool UGridFile::getData(const char *var_name, const LongArray &a, DataPlane &plane, NcVarInfo *&info) const { - info = find_var_name(var_name); - if (info == nullptr) - return false; + info = find_by_name(var_name); bool found = getData(info->var, a, plane); @@ -804,6 +749,63 @@ StringArray UGridFile::get_metadata_names(std::string &key) { } +//////////////////////////////////////////////////////////////////////// + +bool UGridFile::get_var_info() { + + // Pull out the variables + if (Var) { + delete [] Var; + Var = (NcVarInfo *)nullptr; + } + + NcDim dim; + int meta_count = 0; + int max_dim_count = 0; + ConcatString att_value; + StringArray var_names; + + Nvars = get_var_names(_ncFile, &var_names); + for (int j=0; j max_dim_count) max_dim_count = dim_count; + + Var[j].Dims = new NcDim * [dim_count]; + + // parse the variable attributes + get_att_str( Var[j], long_name_att_name, Var[j].long_name_att ); + get_att_str( Var[j], units_att_name, Var[j].units_att ); + + } // for j + + // done + + return true; +} + //////////////////////////////////////////////////////////////////////// @@ -818,12 +820,12 @@ int UGridFile::lead_time() const //////////////////////////////////////////////////////////////////////// -void UGridFile::read_config() { +void UGridFile::read_config(ConcatString config_filename) { const char *method_name = "UGridFile::read_config()"; MetConfig conf; // Read the default config file - conf.read(replace_path(default_config_filename).c_str()); + conf.read(replace_path(config_filename).c_str()); metadata_map = parse_conf_metadata_map(&conf); metadata_names.clear(); @@ -832,7 +834,7 @@ void UGridFile::read_config() { metadata_names.add(it->second); } - mlog << Debug(4) << method_name + mlog << Debug(6) << method_name << " map size: " << metadata_map.size() << ", dims_vars_count = " << metadata_names.n() << "\n"; } @@ -899,4 +901,17 @@ void UGridFile::read_netcdf_grid() } +//////////////////////////////////////////////////////////////////////// + +void UGridFile::set_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"; + +} + //////////////////////////////////////////////////////////////////////// diff --git a/src/libcode/vx_data2d_ugrid/ugrid_file.h b/src/libcode/vx_data2d_ugrid/ugrid_file.h index 846f60c0e5..7453d757ab 100644 --- a/src/libcode/vx_data2d_ugrid/ugrid_file.h +++ b/src/libcode/vx_data2d_ugrid/ugrid_file.h @@ -45,6 +45,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 close(); @@ -101,7 +103,7 @@ class UGridFile { bool getData(const char *, const LongArray &, DataPlane &, NcVarInfo *&) const; - NcVarInfo* find_var_name(const char * var_name) const; + NcVarInfo* find_by_name(const char * var_name) const; private: @@ -155,7 +157,7 @@ class UGridFile { std::string find_metadata_name(std::string &key, StringArray &available_names); StringArray get_metadata_names(std::string &key); - void read_config(); + void read_config(ConcatString config_filename); void read_netcdf_grid(); }; diff --git a/src/libcode/vx_data2d_ugrid/var_info_ugrid.cc b/src/libcode/vx_data2d_ugrid/var_info_ugrid.cc index 7b1da7e5ab..15ee2950df 100644 --- a/src/libcode/vx_data2d_ugrid/var_info_ugrid.cc +++ b/src/libcode/vx_data2d_ugrid/var_info_ugrid.cc @@ -97,9 +97,9 @@ void VarInfoUGrid::assign(const VarInfoUGrid &v) { // Copy clear_dimension(); - for(i=0; i