Skip to content

Commit

Permalink
Feature #3038 nc_dim_log (#3039)
Browse files Browse the repository at this point in the history
* Per #3038, add debug and warning log messages to tell the user which NetCDF dimension indices are being used.

* Per #3038, fix a few SonarQube code smells to reduce the overall number of them for the #3039 PR.
  • Loading branch information
JohnHalleyGotway authored Dec 17, 2024
1 parent c7e4345 commit 0600e02
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 24 deletions.
13 changes: 10 additions & 3 deletions src/libcode/vx_data2d_nc_cf/data2d_nc_cf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ LongArray MetNcCFDataFile::collect_time_offsets(VarInfo &vinfo) {

int time_count = time_offsets.n_elements();
if (0 < time_count)
mlog << Debug(7) << method_name << " Found " << time_count
mlog << Debug(7) << method_name << "Found " << time_count
<< (time_count==1 ? " time" : " times") << " between "
<< unix_to_yyyymmdd_hhmmss(_file->ValidTime[0]) << " and "
<< unix_to_yyyymmdd_hhmmss(_file->ValidTime[time_dim_size-1]) << "\n";
Expand Down Expand Up @@ -638,11 +638,11 @@ long MetNcCFDataFile::convert_time_to_offset(long time_value) {
}

if (found)
mlog << Debug(7) << method_name << " Found "
mlog << Debug(7) << method_name << "Found "
<< unix_to_yyyymmdd_hhmmss(time_value)
<< " at index " << time_offset << " from time value\n";
else if (found_value)
mlog << Debug(7) << method_name << " Found " << time_value
mlog << Debug(7) << method_name << "Found " << time_value
<< " at index " << time_offset << " from time value\n";
else
mlog << Warning << "\n" << method_name << time_value
Expand All @@ -668,6 +668,13 @@ long MetNcCFDataFile::convert_value_to_offset(double z_value, string z_dim_name)
}
}

// Log the dimension value to index conversion
if(z_offset != (long) bad_data_int) {
mlog << Debug(7) << method_name << "Found \""
<< z_dim_name << "\" dimension value of \"" << z_value
<< "\" at dimension index " << z_offset << ".\n";
}

if (!found && 0 < z_dim_name.length()) {
NcVarInfo *var_info = find_var_info_by_dim_name(_file->Var, z_dim_name, _file->Nvars);
if (var_info) {
Expand Down
84 changes: 64 additions & 20 deletions src/libcode/vx_data2d_nc_cf/var_info_nc_cf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ using namespace std;
///////////////////////////////////////////////////////////////////////////////

static bool is_grib_code_abbr_match(const ConcatString &, int);
static void check_dim_offset(const char *);

///////////////////////////////////////////////////////////////////////////////
//
Expand Down Expand Up @@ -80,7 +81,7 @@ VarInfo *VarInfoNcCF::clone() const {

VarInfoNcCF *ret = new VarInfoNcCF(*this);

return (VarInfo *)ret;
return ret;
}

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -98,14 +99,13 @@ void VarInfoNcCF::init_from_scratch() {
///////////////////////////////////////////////////////////////////////////////

void VarInfoNcCF::assign(const VarInfoNcCF &v) {
int i;

// First call the parent's assign
VarInfo::assign(v);

// Copy
clear_dimension();
for(i=0; i<v.n_dimension(); i++) {
for(int i=0; i<v.n_dimension(); i++) {
add_dimension(v.dimension(i), v.is_offset(i), v.dim_value(i));
}

Expand Down Expand Up @@ -223,6 +223,13 @@ void VarInfoNcCF::set_magic(const ConcatString &nstr, const ConcatString &lstr)
// Store the dimension of the range and limits
*ptr3++ = 0;
add_dimension(range_flag, as_offset);

// Check for integer dimension offsets
if(as_offset) {
check_dim_offset(ptr2);
check_dim_offset(ptr3);
}

Level.set_lower(as_offset ? atoi(ptr2) : atof(ptr2));
Level.set_upper(as_offset ? atoi(ptr3) : atof(ptr3));

Expand Down Expand Up @@ -257,28 +264,47 @@ void VarInfoNcCF::set_magic(const ConcatString &nstr, const ConcatString &lstr)
<< MagicStr << "\".\n\n";
exit(1);
}
if (datestring_start && datestring_end) as_offset = false;

unixtime time_lower = datestring_start
? timestring_to_unix(ptr2)
: (as_offset ? atoi(ptr2) : atof(ptr2));
unixtime time_upper = datestring_end
? timestring_to_unix(ptr3)
: (as_offset ? atoi(ptr3) : atof(ptr3));

// Parse the lower and upper time limits
unixtime time_lower = 0;
unixtime time_upper = 0;

if (datestring_start && datestring_end) {
as_offset = false;
time_lower = timestring_to_unix(ptr2);
time_upper = timestring_to_unix(ptr3);
}
else if (as_offset) {

// Check for integer dimension offsets
check_dim_offset(ptr2);
check_dim_offset(ptr3);

time_lower = (unixtime) atoi(ptr2);
time_upper = (unixtime) atoi(ptr3);
}
else {
time_lower = (unixtime) nint(atof(ptr2));
time_upper = (unixtime) nint(atof(ptr3));
}

if (ptr_inc != nullptr) {
if (as_offset) increment = atoi(ptr_inc);
if (as_offset) {
increment = atoi(ptr_inc);
}
else {
increment = is_float(ptr_inc)
? atof(ptr_inc) : timestring_to_sec(ptr_inc);
? nint(atof(ptr_inc))
: timestring_to_sec(ptr_inc);
mlog << Debug(7) << method_name
<< " increment: \"" << ptr_inc << "\" to "
<< "increment: \"" << ptr_inc << "\" to "
<< increment << " seconds.\n";
}
}

add_dimension(range_flag, as_offset);
Level.set_lower(time_lower);
Level.set_upper(time_upper);
Level.set_lower((double) time_lower);
Level.set_upper((double) time_upper);
Level.set_increment(increment);

// Assume time level type for a range of levels
Expand All @@ -293,11 +319,14 @@ void VarInfoNcCF::set_magic(const ConcatString &nstr, const ConcatString &lstr)
if (is_datestring(ptr2)) {
unixtime unix_time = timestring_to_unix(ptr2);
level = vx_data2d_dim_by_value;
level_value = unix_time;
level_value = (double) unix_time;
as_offset = false;
}
else if (is_number(ptr2)) {
if (as_offset) level = atoi(ptr2);
if (as_offset) {
check_dim_offset(ptr2);
level = atoi(ptr2);
}
else {
level = vx_data2d_dim_by_value;
level_value = atof(ptr2);
Expand All @@ -306,7 +335,7 @@ void VarInfoNcCF::set_magic(const ConcatString &nstr, const ConcatString &lstr)
else if (is_datestring(ptr2)) {
unixtime unix_time = timestring_to_unix(ptr2);
level = vx_data2d_dim_by_value;
level_value = unix_time;
level_value = (double) unix_time;
as_offset = false;
}
else {
Expand Down Expand Up @@ -455,7 +484,7 @@ bool VarInfoNcCF::is_wind_direction() const {
//
///////////////////////////////////////////////////////////////////////////////

bool is_grib_code_abbr_match(const ConcatString &str, int grib_code) {
static bool is_grib_code_abbr_match(const ConcatString &str, int grib_code) {
ConcatString abbr_str;
bool match = false;

Expand All @@ -476,3 +505,18 @@ bool is_grib_code_abbr_match(const ConcatString &str, int grib_code) {
}

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

static void check_dim_offset(const char *ptr) {

if(!is_eq(atof(ptr), (double) atoi(ptr))) {
mlog << Warning << "\ncheck_dim_offset() -> "
<< "Found non-integer NetCDF dimension index ("
<< ptr << " != " << atoi(ptr) << ").\n"
<< "Did you intend to use \"@" << ptr
<< "\" to specify the value for that dimension instead?\n\n";
}

return;
}

///////////////////////////////////////////////////////////////////////////////
2 changes: 1 addition & 1 deletion src/libcode/vx_data2d_nc_cf/var_info_nc_cf.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class VarInfoNcCF : public VarInfo

inline GrdFileType VarInfoNcCF::file_type() const { return FileType_NcCF; }
inline const LongArray & VarInfoNcCF::dimension() const { return Dimension; }
inline int VarInfoNcCF::dimension(int i) const { return Dimension[i]; }
inline int VarInfoNcCF::dimension(int i) const { return (int) Dimension[i]; }
inline int VarInfoNcCF::n_dimension() const { return Dimension.n_elements();}
inline const NumArray & VarInfoNcCF::dim_value() const { return Dim_value; }
inline double VarInfoNcCF::dim_value(int i) const { return Dim_value[i]; }
Expand Down

0 comments on commit 0600e02

Please sign in to comment.