Skip to content

Commit

Permalink
Per issue #2232, modified setup_nc_file to use new flags: multiple_da…
Browse files Browse the repository at this point in the history
…ta_sources, unique_variable_names to append VAR# var names if variables are not unique. This fixes the issues of trying to write the same variable name the output file. SL
  • Loading branch information
Seth Linden committed Oct 26, 2022
1 parent 5fcae49 commit 7d22fac
Showing 1 changed file with 77 additions and 66 deletions.
143 changes: 77 additions & 66 deletions src/tools/other/grid_diag/grid_diag.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// 003 08/20/21 Halley Gotway Bugfix #1886 for integer overflow.
// 004 07/06/22 Howard Soh METplus-Internal #19 Rename main to met_main
// 005 10/03/22 Prestopnik MET #2227 Remove using namespace std and netCDF from header files
// 006 10/26/22 Linden MET #2232 Refine the Grid-Diag output variable names when specifying two input data sources
//
////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -162,8 +163,6 @@ void process_command_line(int argc, char **argv) {
conf_info.read_config(default_config_file.c_str(),
config_file.c_str());

cout << "CHECK1" << endl;

// Determine the number of data fields
conf_info.set_n_data();

Expand All @@ -181,8 +180,6 @@ void process_command_line(int argc, char **argv) {
// Process the input data file lists
for(int i=0; i<data_files.size(); i++) {

cout << "CHECK2, inside loop over data files" << endl;

// Parse the data file list
data_files[i] = parse_file_list(data_files[i]);

Expand Down Expand Up @@ -215,13 +212,9 @@ void process_command_line(int argc, char **argv) {

} // end for i

cout << "CHECK3" << endl;

// Process the configuration
conf_info.process_config(file_types);

cout << "CHECK4" << endl;

// Determine the verification grid
grid = parse_vx_grid(conf_info.data_info[0]->regrid(),
&data_grid, &data_grid);
Expand Down Expand Up @@ -498,70 +491,66 @@ void setup_joint_histograms(void) {
////////////////////////////////////////////////////////////////////////

void setup_nc_file(void) {
ConcatString cs, i_var_str;

// Create NetCDF file
nc_out = open_ncfile(out_file.c_str(), true);

if(IS_INVALID_NC_P(nc_out)) {
ConcatString cs, i_var_str, j_var_str;
// Create NetCDF file
nc_out = open_ncfile(out_file.c_str(), true);
if(IS_INVALID_NC_P(nc_out)) {
mlog << Error << "\nsetup_nc_file() -> "
<< "trouble opening output NetCDF file "
<< out_file << "\n\n";
exit(1);
}

// Add global attributes
write_netcdf_global(nc_out, out_file.c_str(), program_name,
NULL, NULL, conf_info.desc.c_str());
add_att(nc_out, "mask_grid", (conf_info.mask_grid_name.nonempty() ?
(string)conf_info.mask_grid_name :
na_str));
add_att(nc_out, "mask_poly", (conf_info.mask_poly_name.nonempty() ?
(string)conf_info.mask_poly_name :
na_str));

// Add time range information to the global attributes
add_att(nc_out, "init_beg", (string)unix_to_yyyymmdd_hhmmss(init_beg));
add_att(nc_out, "init_end", (string)unix_to_yyyymmdd_hhmmss(init_end));
add_att(nc_out, "valid_beg", (string)unix_to_yyyymmdd_hhmmss(valid_beg));
add_att(nc_out, "valid_end", (string)unix_to_yyyymmdd_hhmmss(valid_end));
add_att(nc_out, "lead_beg", (string)sec_to_hhmmss(lead_beg));
add_att(nc_out, "lead_end", (string)sec_to_hhmmss(lead_end));

// Write the grid size, mask size, and series length
write_nc_var_int("grid_size", "number of grid points", grid.nxy());
write_nc_var_int("mask_size", "number of mask points", conf_info.mask_area.count());
write_nc_var_int("n_series", "length of series", n_series);

// Compression level
int deflate_level = compress_level;
if(deflate_level < 0) deflate_level = conf_info.conf.nc_compression();

for(int i_var=0; i_var < conf_info.get_n_data(); i_var++) {

<< "trouble opening output NetCDF file "
<< out_file << "\n\n";
exit(1);
}
// Add global attributes
write_netcdf_global(nc_out, out_file.c_str(), program_name,
NULL, NULL, conf_info.desc.c_str());
add_att(nc_out, "mask_grid", (conf_info.mask_grid_name.nonempty() ?
(string)conf_info.mask_grid_name :
na_str));
add_att(nc_out, "mask_poly", (conf_info.mask_poly_name.nonempty() ?
(string)conf_info.mask_poly_name :
na_str));

// Add time range information to the global attributes
add_att(nc_out, "init_beg", (string)unix_to_yyyymmdd_hhmmss(init_beg));
add_att(nc_out, "init_end", (string)unix_to_yyyymmdd_hhmmss(init_end));
add_att(nc_out, "valid_beg", (string)unix_to_yyyymmdd_hhmmss(valid_beg));
add_att(nc_out, "valid_end", (string)unix_to_yyyymmdd_hhmmss(valid_end));
add_att(nc_out, "lead_beg", (string)sec_to_hhmmss(lead_beg));
add_att(nc_out, "lead_end", (string)sec_to_hhmmss(lead_end));
// Write the grid size, mask size, and series length
write_nc_var_int("grid_size", "number of grid points", grid.nxy());
write_nc_var_int("mask_size", "number of mask points", conf_info.mask_area.count());
write_nc_var_int("n_series", "length of series", n_series);

// Compression level
int deflate_level = compress_level;
if(deflate_level < 0) deflate_level = conf_info.conf.nc_compression();
for(int i_var=0; i_var < conf_info.get_n_data(); i_var++) {
i_var_str << cs_erase << "VAR" << i_var;

VarInfo *data_info = conf_info.data_info[i_var];


// This is the section we need to fix SL

cout << "multiple_data_sources = " << multiple_data_sources << endl;
cout << "unique_variable_names = " << unique_variable_names << endl;
VarInfo *data_info = conf_info.data_info[i_var];

// Set variable NetCDF name
ConcatString var_name = data_info->name_attr();
cout << "Initial var_name = " << var_name << endl;
var_name.add("_");
var_name.add(data_info->level_attr());
cout << "Output var_name = " << var_name << endl;


if(multiple_data_sources && !unique_variable_names) {
var_name.add("_");
var_name.add(i_var_str);
}

// Define histogram dimensions
NcDim var_dim = add_dim(nc_out, var_name,
(long) data_info->n_bins());
data_var_dims.push_back(var_dim);

// Define histogram bins
ConcatString var_min_name = var_name;
ConcatString var_max_name = var_name;
Expand All @@ -588,53 +577,75 @@ void setup_nc_file(void) {
cs << cs_erase << "Midpoint value of " << var_name << " bin";
add_var_att_local(&var_mid, "long_name", cs);
add_var_att_local(&var_mid, "units", data_info->units_attr());

// Write bin values
var_min.putVar(bin_mins[i_var_str].data());
var_max.putVar(bin_maxs[i_var_str].data());
var_mid.putVar(bin_mids[i_var_str].data());
}

// Define histograms
for(int i_var=0; i_var < conf_info.get_n_data(); i_var++) {

i_var_str << cs_erase << "VAR" << i_var;

VarInfo *data_info = conf_info.data_info[i_var];

// Set variable NetCDF name
ConcatString var_name = data_info->name_attr();
var_name.add("_");
var_name.add(data_info->level_attr());


if(multiple_data_sources && !unique_variable_names) {
var_name.add("_");
var_name.add(i_var_str);
}

ConcatString hist_name("hist_");
hist_name.add(var_name);
NcDim var_dim = data_var_dims[i_var];
NcVar hist_var = add_var(nc_out, hist_name, ncInt64, var_dim,
deflate_level);
hist_vars.push_back(hist_var);

// Add variable attributes
cs << cs_erase << "Histogram of " << var_name << " values";
add_var_att_local(&hist_var, "long_name", cs);
}

// Define joint histograms
for(int i_var=0; i_var < conf_info.get_n_data(); i_var++) {

i_var_str << cs_erase << "VAR" << i_var;

VarInfo *data_info = conf_info.data_info[i_var];

for(int j_var=i_var+1; j_var<conf_info.get_n_data(); j_var++) {

j_var_str << cs_erase << "VAR" << j_var;

VarInfo *joint_info = conf_info.data_info[j_var];

ConcatString hist_name("hist_");
hist_name.add(data_info->name_attr());
hist_name.add("_");
hist_name.add(data_info->level_attr());

if(multiple_data_sources && !unique_variable_names) {
hist_name.add("_");
hist_name.add(i_var_str);
}

hist_name.add("_");
hist_name.add(joint_info->name_attr());
hist_name.add("_");
hist_name.add(joint_info->level_attr());

if(multiple_data_sources && !unique_variable_names) {
hist_name.add("_");
hist_name.add(j_var_str);
}

NcDim var_dim = data_var_dims[i_var];
NcDim joint_dim = data_var_dims[j_var];
vector<NcDim> dims;
Expand Down

0 comments on commit 7d22fac

Please sign in to comment.