Skip to content

Commit

Permalink
#1815 Support @ for pinterp
Browse files Browse the repository at this point in the history
  • Loading branch information
Howard Soh committed Jun 27, 2022
1 parent 4f1343f commit 9541e84
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 42 deletions.
15 changes: 13 additions & 2 deletions src/libcode/vx_data2d_nc_pinterp/data2d_nc_pinterp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,20 @@ bool MetNcPinterpDataFile::data_plane(VarInfo &vinfo, DataPlane &plane) {
plane.clear();

// Read the data
PinterpNc->get_nc_var_info(vinfo_nc->req_name().c_str(), info);
LongArray dimension = vinfo_nc->dimension();
int dim_count = dimension.n_elements();
for (int k=0; k<dim_count; k++) {
if (dimension[k] == vx_data2d_dim_by_value) {
dimension[k] = get_index_for_dim(PinterpNc->Var,
GET_NC_NAME(get_nc_dim(info->var, k)),
vinfo_nc->dim_value(k), PinterpNc->Nvars,
(k == info->t_slot));
}
}

status = PinterpNc->data(vinfo_nc->req_name().c_str(),
vinfo_nc->dimension(),
plane, pressure, info);
dimension, plane, pressure, info);

// Check that the times match those requested
if(status) {
Expand Down
60 changes: 41 additions & 19 deletions src/libcode/vx_data2d_nc_pinterp/pinterp_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static const char pressure_var_wrf_interp_name [] = "LEV";
static const char pa_units_str [] = "Pa";
static const char hpa_units_str [] = "hPa";

static const string init_time_att_name = "START_DATE";
static const string start_time_att_name = "START_DATE";

static const int max_pinterp_args = 30;

Expand All @@ -73,7 +73,6 @@ static bool is_bad_data_pinterp(double);

static bool is_accumulation(const char *);


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


Expand Down Expand Up @@ -281,7 +280,7 @@ else {
}

ConcatString att_value;
get_global_att(Nc, init_time_att_name, att_value);
get_global_att(Nc, start_time_att_name, att_value);

InitTime = parse_init_time(att_value.c_str());

Expand Down Expand Up @@ -796,13 +795,15 @@ return ( true );


bool PinterpFile::data(const char * var_name, const LongArray & a, DataPlane & plane,
double & pressure, NcVarInfo *&info) const
double & pressure, NcVarInfo *&info) const {

{
int j, time_index;
bool found = false;

int j, time_index;
bool found = false;

if (NULL != info) found = true;
else found = get_nc_var_info(var_name, info);
/*
for (j=0; j<Nvars; ++j) {
if ( Var[j].name == var_name ) {
Expand All @@ -812,45 +813,66 @@ for (j=0; j<Nvars; ++j) {
}
}
*/

if ( !found ) return ( false );
if ( !found ) return ( false );

found = data(Var[j].var, a, plane, pressure);
found = data(info->var, a, plane, pressure);

//
// store the times
//

time_index = a[Var[j].t_slot];
time_index = a[info->t_slot];

plane.set_init ( InitTime );
plane.set_valid ( valid_time(time_index) );
plane.set_lead ( lead_time(time_index) );
plane.set_init ( InitTime );
plane.set_valid ( valid_time(time_index) );
plane.set_lead ( lead_time(time_index) );

//
// since Pinterp files only contain WRF-ARW output, it is always a
// a runtime accumulation
//

if ( is_accumulation(var_name) ) {
if ( is_accumulation(var_name) ) {

plane.set_accum ( lead_time(time_index) );
plane.set_accum ( lead_time(time_index) );

} else {
} else {

plane.set_accum ( 0 );
plane.set_accum ( 0 );

}
}

//
// done
//

return ( found );
return ( found );

}


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

bool PinterpFile::get_nc_var_info(const char *var_name, NcVarInfo *&info) const {
bool found = false;

if (NULL == info) {
for (int j=0; j<Nvars; ++j) {

if ( Var[j].name == var_name ) {
found = true;
info = &Var[j];
break;
}

}
}

return found;
}

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


Expand Down
1 change: 1 addition & 0 deletions src/libcode/vx_data2d_nc_pinterp/pinterp_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class PinterpFile {
bool data(const char *, const LongArray &, DataPlane &,
double & pressure, NcVarInfo *&) const;

bool get_nc_var_info(const char *var_name, NcVarInfo *&info) const;
};


Expand Down
74 changes: 56 additions & 18 deletions src/libcode/vx_data2d_nc_pinterp/var_info_nc_pinterp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ void VarInfoNcPinterp::init_from_scratch() {

clear();

return;
}

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -90,10 +89,11 @@ void VarInfoNcPinterp::assign(const VarInfoNcPinterp &v) {
VarInfo::assign(v);

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

return;
}

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -104,9 +104,8 @@ void VarInfoNcPinterp::clear() {
VarInfo::clear();

// Initialize
Dimension.clear();
clear_dimension();

return;
}

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -117,15 +116,27 @@ void VarInfoNcPinterp::dump(ostream &out) const {
out << "VarInfoNcPinterp::dump():\n"
<< " Dimension:\n";
Dimension.dump(out);
out << " Is_offset:\n";
Is_offset.dump(out);
out << " Dim_value:\n";
Dim_value.dump(out);

return;
}

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

void VarInfoNcPinterp::add_dimension(int dim) {
void VarInfoNcPinterp::add_dimension(int dim, bool as_offset, double dim_value) {
Dimension.add(dim);
return;
Is_offset.add(as_offset);
Dim_value.add(dim_value);
}

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

void VarInfoNcPinterp::clear_dimension() {
Dimension.clear();
Is_offset.clear();
Dim_value.clear();
}

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -140,6 +151,7 @@ void VarInfoNcPinterp::set_dimension(int i_dim, int dim) {
void VarInfoNcPinterp::set_magic(const ConcatString &nstr, const ConcatString &lstr) {
ConcatString tmp_str;
char *ptr = (char *) 0, *ptr2 = (char *) 0, *ptr3 = (char *) 0, *save_ptr = (char *) 0;
const char *method_name = "VarInfoNcPinterp::set_magic() -> ";

// Store the magic string
VarInfo::set_magic(nstr, lstr);
Expand All @@ -152,10 +164,10 @@ void VarInfoNcPinterp::set_magic(const ConcatString &nstr, const ConcatString &l
if(strchr(lstr.c_str(), '(') == NULL) {
Level.set_req_name("0,*,*");
Level.set_name("0,*,*");
Dimension.clear();
Dimension.add(0);
Dimension.add(vx_data2d_star);
Dimension.add(vx_data2d_star);
clear_dimension();
add_dimension(0);
add_dimension(vx_data2d_star);
add_dimension(vx_data2d_star);
}
// Parse the level specification
else {
Expand All @@ -171,28 +183,32 @@ void VarInfoNcPinterp::set_magic(const ConcatString &nstr, const ConcatString &l
Level.set_name(ptr);

// If dimensions are specified, clear the default value
if(strchr(ptr, ',') != NULL) Dimension.clear();
if(strchr(ptr, ',') != NULL) clear_dimension();

// Parse the dimensions
bool as_offset = true;
while((ptr2 = strtok_r(ptr, ",", &save_ptr)) != NULL) {

// Check for wildcards
if(strchr(ptr2, '*') != NULL) Dimension.add(vx_data2d_star);
if(strchr(ptr2, '*') != NULL) add_dimension(vx_data2d_star);
else {

as_offset = (*ptr2 != '@');
if (!as_offset) ptr2++;

// Check for a range of levels
if((ptr3 = strchr(ptr2, '-')) != NULL) {

// Check if a range has already been supplied
if(Dimension.has(range_flag)) {
mlog << Error << "\nVarInfoNcPinterp::set_magic() -> "
mlog << Error << "\n" << method_name
<< "only one dimension can have a range for NetCDF variable \""
<< MagicStr << "\".\n\n";
exit(1);
}
// Store the dimension of the range and limits
else {
Dimension.add(range_flag);
add_dimension(range_flag);
Level.set_lower(atoi(ptr2));
Level.set_upper(atoi(++ptr3));

Expand All @@ -202,7 +218,29 @@ void VarInfoNcPinterp::set_magic(const ConcatString &nstr, const ConcatString &l
}
// Single level
else {
Dimension.add(atoi(ptr2));
int level = 0;
double level_value = bad_data_double;
if (is_number(ptr2)) {
if (as_offset) level = atoi(ptr2);
else {
level = vx_data2d_dim_by_value;
level_value = atof(ptr2);
}
}
else if (is_datestring(ptr2)) {
unixtime unix_time = timestring_to_unix(ptr2);
level = vx_data2d_dim_by_value;
level_value = unix_time;
as_offset = false;
}
else {
mlog << Error << "\n" << method_name
<< "trouble parsing NetCDF dimension value \""
<< ptr2 << "\"!\n\n";
exit(1);
}
if (as_offset) add_dimension(level, as_offset);
else add_dimension(level, as_offset, level_value);
}
}

Expand Down
21 changes: 18 additions & 3 deletions src/libcode/vx_data2d_nc_pinterp/var_info_nc_pinterp.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

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

typedef CRC_Array<bool> BoolArray;

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

//
// List of Pinterp precipitation variable names
// Taken from the WRF version 3.2 Registry.EM file
Expand Down Expand Up @@ -184,9 +188,12 @@ class VarInfoNcPinterp : public VarInfo
//

LongArray Dimension; // Dimension values for extracting 2D field
BoolArray Is_offset; // boolean for Dimension value (true: offset, false: value to be an offset (false for value)
NumArray Dim_value; // Dimension values as float for extracting 2D field

void init_from_scratch();
void assign(const VarInfoNcPinterp &);
void clear_dimension();

public:
VarInfoNcPinterp();
Expand All @@ -202,8 +209,12 @@ class VarInfoNcPinterp : public VarInfo
//

GrdFileType file_type() const;
const LongArray & dimension() const;
int dimension(int i) const;
const LongArray & dimension() const;
int dimension(int i) const;
const NumArray & dim_value() const;
double dim_value(int i) const;
const BoolArray & is_offset() const;
bool is_offset(int i) const;
int n_dimension() const;

//
Expand All @@ -213,7 +224,7 @@ class VarInfoNcPinterp : public VarInfo
void set_magic(const ConcatString &, const ConcatString &);
void set_dict(Dictionary &);

void add_dimension(int dim);
void add_dimension(int dim, bool as_index=true, double dim_value=bad_data_double);
void set_dimension(int i_dim, int dim);

//
Expand All @@ -235,6 +246,10 @@ inline GrdFileType VarInfoNcPinterp::file_type() const { return(FileT
inline const LongArray & VarInfoNcPinterp::dimension() const { return(Dimension); }
inline int VarInfoNcPinterp::dimension(int i) const { return(Dimension[i]); }
inline int VarInfoNcPinterp::n_dimension() const { return(Dimension.n_elements()); }
inline const NumArray & VarInfoNcPinterp::dim_value() const { return(Dim_value); }
inline double VarInfoNcPinterp::dim_value(int i) const { return(Dim_value[i]); }
inline const BoolArray & VarInfoNcPinterp::is_offset() const { return(Is_offset); }
inline bool VarInfoNcPinterp::is_offset(int i) const { return(Is_offset[i]); }

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

Expand Down

0 comments on commit 9541e84

Please sign in to comment.