Skip to content

Commit

Permalink
Per #1019, update library code to handle the independent writing of p…
Browse files Browse the repository at this point in the history
…oint observation descriptions. Previously, if units were present then descriptions (usually empty ones) were added. Now, units and descriptions and handled independently.
  • Loading branch information
JohnHalleyGotway committed Jan 13, 2025
1 parent ef4d982 commit 6202314
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 26 deletions.
12 changes: 8 additions & 4 deletions src/basic/vx_util/observation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ Observation::Observation(const string &header_type, const string &station_id,
const string &quality_flag,
const int var_code, const double pressure_level_hpa,
const double height_m, const double value,
const string &var_name, const string &var_units) :
const string &var_name,
const string &var_units,
const string &var_desc) :
_headerType(header_type),
_stationId(station_id),
_validTime(valid_time),
Expand All @@ -47,6 +49,7 @@ Observation::Observation(const string &header_type, const string &station_id,
_qualityFlag(quality_flag),
_varName(var_name),
_varUnits(var_units),
_varDesc(var_desc),
varCode(var_code),
hdrIndex(0),
_pressureLevel(pressure_level_hpa),
Expand Down Expand Up @@ -150,13 +153,14 @@ _varName = pyobject_as_string(list[6]);

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

// TODO: Consider enhancing the Python point observation format
// to include specifying units.

_varUnits = "";

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

_varDesc = "";

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

_pressureLevel = pyobject_as_double(list[7]);

////////////////////////
Expand Down
12 changes: 10 additions & 2 deletions src/basic/vx_util/observation.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class Observation
const int grib_code, const double pressure_level_hpa,
const double height_m, const double value,
const std::string &var_name = "",
const std::string &var_units = "");
const std::string &var_units = "",
const std::string &var_desc = "");

Observation(const std::string &header_type, const std::string &station_id,
const time_t start_time, const time_t end_time,
Expand All @@ -55,7 +56,8 @@ class Observation
const int grib_code, const double pressure_level_hpa,
const double height_m, const double value,
const std::string &var_name = "",
const std::string &var_units = "");
const std::string &var_units = "",
const std::string &var_desc = "");

////////////////////////
#ifdef ENABLE_PYTHON
Expand Down Expand Up @@ -167,6 +169,11 @@ class Observation
return _varUnits;
}

std::string getVarDesc() const
{
return _varDesc;
}

bool hasSameHeader(Observation &other) const;
bool hasSameHeader(Observation *other) const;

Expand Down Expand Up @@ -216,6 +223,7 @@ class Observation
std::string _qualityFlag;
std::string _varName;
std::string _varUnits;
std::string _varDesc;
int varCode;
long hdrIndex;
double _pressureLevel;
Expand Down
8 changes: 5 additions & 3 deletions src/libcode/vx_nc_obs/nc_obs_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -342,16 +342,18 @@ void NetcdfObsVars::create_obs_vars (NcFile *f_out) {

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

void NetcdfObsVars::create_obs_name_vars (NcFile *f_out, const int var_count, const int unit_count) {
void NetcdfObsVars::create_obs_name_vars (NcFile *f_out, const int var_count,
const int unit_count, const int desc_count) {
const string method_name = " create_other_vars()";

if (var_count > 0) {
NcDim var_dim = create_var_obs_var(f_out, var_count);
if (unit_count > 0) {
unit_var = add_var(f_out, nc_var_unit, ncChar, var_dim, strl2_dim, deflate_level);
desc_var = add_var(f_out, nc_var_desc, ncChar, var_dim, strl3_dim, deflate_level);

add_att(&unit_var, "long_name", "variable units");
}
if (desc_count > 0) {
desc_var = add_var(f_out, nc_var_desc, ncChar, var_dim, strl3_dim, deflate_level);
add_att(&desc_var, "long_name", "variable descriptions");
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libcode/vx_nc_obs/nc_obs_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ struct NetcdfObsVars {
void create_dimensions(netCDF::NcFile *f_out);
void create_hdr_vars (netCDF::NcFile *f_out, const int hdr_count);
void create_obs_vars (netCDF::NcFile *f_out);
void create_obs_name_vars (netCDF::NcFile *f_out, const int var_count, const int unit_count);
void create_obs_name_vars (netCDF::NcFile *f_out, const int var_count, const int unit_count, const int desc_count);
void create_table_vars (netCDF::NcFile *f_out, MetPointHeader &hdr_data, NcDataBuffer &data_buffer);
void create_pb_hdrs (netCDF::NcFile *f_out, const int hdr_count);
netCDF::NcDim create_var_obs_var (netCDF::NcFile *f_out, int var_count);
Expand Down
7 changes: 4 additions & 3 deletions src/libcode/vx_nc_obs/nc_point_obs_out.cc
Original file line number Diff line number Diff line change
Expand Up @@ -597,10 +597,11 @@ bool MetNcPointObsOut::write_to_netcdf(StringArray obs_names, StringArray obs_un
int var_count = obs_names.n_elements();
if (var_count > 0) {
int unit_count = obs_units.n();
obs_vars.create_obs_name_vars (obs_nc, var_count, unit_count);
int desc_count = obs_descs.n();
obs_vars.create_obs_name_vars (obs_nc, var_count, unit_count, desc_count);
obs_vars.write_obs_var_names(obs_names);
if( unit_count > 0 ) obs_vars.write_obs_var_units(obs_units);
if( obs_descs.n() > 0 ) obs_vars.write_obs_var_descriptions(obs_descs);
if(unit_count > 0) obs_vars.write_obs_var_units(obs_units);
if(desc_count > 0) obs_vars.write_obs_var_descriptions(obs_descs);
mlog << Debug(7) << method_name << var_count
<< " variable names were saved\n";
}
Expand Down
7 changes: 5 additions & 2 deletions src/libcode/vx_summary/summary_key.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ SummaryKey::SummaryKey(const string &header_type,
const double lat, const double lon, const double elev,
const int var_code,
const double height, const double pressure_level,
const string &var_name, const string &var_units) :
const string &var_name,
const string &var_units,
const string &var_desc) :
_headerType(header_type),
_stationId(station_id),
_latitude(lat),
Expand All @@ -42,7 +44,8 @@ SummaryKey::SummaryKey(const string &header_type,
_height(height),
_pressureLevel(pressure_level),
_varName(var_name),
_varUnits(var_units)
_varUnits(var_units),
_varDesc(var_desc)
{
}

Expand Down
13 changes: 12 additions & 1 deletion src/libcode/vx_summary/summary_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class SummaryKey
const int var_code,
const double height_m, const double pressure_level,
const std::string &var_name = "",
const std::string &var_units = "");
const std::string &var_units = "",
const std::string &var_desc = "");

virtual ~SummaryKey();

Expand Down Expand Up @@ -102,6 +103,11 @@ class SummaryKey
return _varUnits;
}

std::string getVarDesc() const
{
return _varDesc;
}

///////////////
// Operators //
///////////////
Expand Down Expand Up @@ -153,6 +159,10 @@ class SummaryKey
if (_varUnits != other._varUnits)
return _varUnits < other._varUnits;

// Var desc
if (_varDesc != other._varDesc)
return _varDesc < other._varDesc;

// Grib code

return _varCode < other._varCode;
Expand All @@ -175,6 +185,7 @@ class SummaryKey
double _pressureLevel;
std::string _varName;
std::string _varUnits;
std::string _varDesc;

};

Expand Down
26 changes: 20 additions & 6 deletions src/libcode/vx_summary/summary_obs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,15 @@ bool SummaryObs::summarizeObs(const TimeSummaryInfo &summary_info)
// Save the summary information
const char *var_name = 0;
const char *var_units = 0;
const char *var_desc = 0;

//_dataSummarized = true;
TimeSummaryInfo inputSummaryInfo = summary_info;

// Initialize the list of summary observations
StringArray summary_vnames;
StringArray summary_vunits;
StringArray summary_vdescs;
StringArray printted_var_names;
//vector< Observation > summary_obs;

Expand Down Expand Up @@ -215,7 +217,8 @@ bool SummaryObs::summarizeObs(const TimeSummaryInfo &summary_info)
<< "SummaryObs::summarizeObs() Filtered variable ["
<< curr_obs->getVarName() << "] (id: "
<< curr_obs->getVarCode() << ", units: "
<< curr_obs->getVarUnits() << ")\n";
<< curr_obs->getVarUnits() << ", desc: "
<< curr_obs->getVarDesc() << ")\n";
printted_var_names.add(curr_obs->getVarName().c_str());
}
}
Expand All @@ -234,14 +237,17 @@ bool SummaryObs::summarizeObs(const TimeSummaryInfo &summary_info)
curr_obs->getHeight(),
curr_obs->getPressureLevel(),
curr_obs->getVarName(),
curr_obs->getVarUnits());
curr_obs->getVarUnits(),
curr_obs->getVarDesc());

// Collect variable names
var_name = curr_obs->getVarName().c_str();
var_units = curr_obs->getVarUnits().c_str();
var_desc = curr_obs->getVarDesc().c_str();
if (0 < m_strlen(var_name) && !summary_vnames.has(var_name)) {
summary_vnames.add(var_name);
summary_vunits.add(var_units);
summary_vdescs.add(var_desc);
}
// If this is a new key, create a new NumArray
if (summary_values.find(summary_key) == summary_values.end()) {
Expand Down Expand Up @@ -289,7 +295,8 @@ bool SummaryObs::summarizeObs(const TimeSummaryInfo &summary_info)
<< curr_values->first.getElevation() << ", "
<< curr_values->first.getVarName() << ", "
<< curr_values->first.getVarCode() << ", "
<< curr_values->first.getVarUnits() << "\n";
<< curr_values->first.getVarUnits() << ", "
<< curr_values->first.getVarDesc() << "\n";
continue;
}
}
Expand All @@ -310,7 +317,8 @@ bool SummaryObs::summarizeObs(const TimeSummaryInfo &summary_info)
curr_values->first.getHeight(),
calc->calcSummary(*curr_values->second),
curr_values->first.getVarName(),
curr_values->first.getVarUnits()));
curr_values->first.getVarUnits(),
curr_values->first.getVarDesc()));
summaryCount++;
} /* endfor - calc */

Expand All @@ -331,6 +339,7 @@ bool SummaryObs::summarizeObs(const TimeSummaryInfo &summary_info)
if (!obs_names.has(summary_vnames[idx])) {
obs_names.add(summary_vnames[idx]);
obs_units.add(summary_vunits[idx]);
obs_descs.add(summary_vdescs[idx]);
}
}

Expand Down Expand Up @@ -511,9 +520,11 @@ bool SummaryObs::addObservationObj(const Observation &obs)

const ConcatString var_name = obs.getVarName();
const ConcatString var_units = obs.getVarUnits();
const ConcatString var_desc = obs.getVarDesc();
if (0 < var_name.length() && !obs_names.has(var_name)) {
obs_names.add(var_name);
obs_units.add(var_units);
obs_descs.add(var_desc);
}
result = true;
return result;
Expand All @@ -529,13 +540,16 @@ bool SummaryObs::addObservation(
const string &quality_flag,
const int var_code, const double pressure_level_hpa,
const double height_m, const double value,
const string &var_name, const string &var_units)
const string &var_name,
const string &var_units,
const string &var_desc)
{
return addObservationObj(
Observation(header_type, station_id, valid_time,
latitude, longitude, elevation,
quality_flag, var_code, pressure_level_hpa,
height_m, value, var_name, var_units));
height_m, value,
var_name, var_units, var_desc));
}

////////////////////////////////////////////////////////////////////////
7 changes: 5 additions & 2 deletions src/libcode/vx_summary/summary_obs.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class SummaryObs
const int var_code, const double pressure_level_hpa,
const double height_m, const double value,
const std::string &var_name = "",
const std::string &var_units = "");
const std::string &var_units = "",
const std::string &var_desc = "");
std::vector< Observation > getObservations();
std::vector< Observation > getSummaries();
long countHeaders();
Expand All @@ -54,7 +55,7 @@ class SummaryObs
void setSummaryInfo(const TimeSummaryInfo &summary_info);
StringArray getObsNames();
StringArray getObsUnits();

StringArray getObsDescs();

protected:

Expand All @@ -73,6 +74,7 @@ class SummaryObs
std::vector< Observation > summaries;
StringArray obs_names;
StringArray obs_units;
StringArray obs_descs;

///////////////////////
// Protected methods //
Expand Down Expand Up @@ -258,6 +260,7 @@ inline std::vector< Observation > SummaryObs::getObservations() { return observa
inline std::vector< Observation > SummaryObs::getSummaries() { return summaries; }
inline StringArray SummaryObs::getObsNames() { return obs_names; }
inline StringArray SummaryObs::getObsUnits() { return obs_units; }
inline StringArray SummaryObs::getObsDescs() { return obs_descs; }
inline void SummaryObs::setSummaryInfo(const TimeSummaryInfo &summary_info)
{ summaryInfo = summary_info; }
inline TimeSummaryInfo SummaryObs::getSummaryInfo() { return summaryInfo; }
Expand Down
25 changes: 23 additions & 2 deletions src/tools/other/ascii2nc/file_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,12 @@ bool FileHandler::summarizeObs(const TimeSummaryInfo &summary_info)
_summaryInfo = summary_info;
StringArray summary_vnames = summary_obs.getObsNames();
StringArray summary_vunits = summary_obs.getObsUnits();
StringArray summary_vdescs = summary_obs.getObsDescs();
for (int idx=0; idx<summary_vnames.n(); idx++) {
if (!obs_names.has(summary_vnames[idx])) {
obs_names.add(summary_vnames[idx]);
obs_units.add(summary_vunits[idx]);
obs_descs.add(summary_vdescs[idx]);
}
}
return result;
Expand Down Expand Up @@ -322,9 +324,11 @@ bool FileHandler::_addObservations(const Observation &obs)
else {
ConcatString var_name = obs.getVarName();
ConcatString var_units = obs.getVarUnits();
ConcatString var_desc = obs.getVarDesc();
if (var_name.nonempty() && !obs_names.has(var_name)) {
obs_names.add(var_name);
obs_units.add(var_units);
obs_descs.add(var_desc);
}
}

Expand All @@ -335,8 +339,25 @@ bool FileHandler::_addObservations(const Observation &obs)

bool FileHandler::_writeObservations()
{
StringArray descs;
nc_point_obs.write_to_netcdf(obs_names, obs_units, descs);
StringArray units_sa;
StringArray descs_sa;

// Do not write all empty strings
if(!obs_units.all_empty()) units_sa = obs_units;
if(!obs_descs.all_empty()) descs_sa = obs_descs;

// JHG
cout << "JHG obs_names...";
obs_names.dump(cout);
cout << "\n";
cout << "JHG units_sa...";
units_sa.dump(cout);
cout << "\n";
cout << "JHG descs_sa...";
descs_sa.dump(cout);
cout << "\n";

nc_point_obs.write_to_netcdf(obs_names, units_sa, descs_sa);

return true;
}
Expand Down
1 change: 1 addition & 0 deletions src/tools/other/ascii2nc/file_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class FileHandler
bool use_var_id;
StringArray obs_names;
StringArray obs_units;
StringArray obs_descs;

bool do_monitor;
int start_time, end_time;
Expand Down

0 comments on commit 6202314

Please sign in to comment.