Skip to content

Commit

Permalink
#1720 Added grid_str to open()
Browse files Browse the repository at this point in the history
  • Loading branch information
Howard Soh committed Jul 25, 2024
1 parent 07fac42 commit 40133cd
Show file tree
Hide file tree
Showing 18 changed files with 52 additions and 28 deletions.
10 changes: 5 additions & 5 deletions src/libcode/vx_data2d_factory/data2d_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ MetPythonDataFile * p = nullptr;

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

Met2dDataFile * Met2dDataFileFactory::new_met_2d_data_file(const char *filename) {
Met2dDataFile * Met2dDataFileFactory::new_met_2d_data_file(const char *filename, ConcatString attr_grid) {
GrdFileType type;
Met2dDataFile *mtddf = (Met2dDataFile *) nullptr;

Expand All @@ -179,7 +179,7 @@ Met2dDataFile * Met2dDataFileFactory::new_met_2d_data_file(const char *filename)
if(mtddf &&
type != FileType_Python_Numpy &&
type != FileType_Python_Xarray) {
if(!(mtddf->open(filename))) {
if(!(mtddf->open(filename, attr_grid))) {
mlog << Error << "\nMet2dDataFileFactory::new_met_2d_data_file() -> "
<< "error opening file \"" << filename << "\"\n\n";
exit(1);
Expand All @@ -191,7 +191,7 @@ Met2dDataFile * Met2dDataFileFactory::new_met_2d_data_file(const char *filename)

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

Met2dDataFile * Met2dDataFileFactory::new_met_2d_data_file(const char *filename, GrdFileType type)
Met2dDataFile * Met2dDataFileFactory::new_met_2d_data_file(const char *filename, GrdFileType type, ConcatString attr_grid)

{

Expand Down Expand Up @@ -223,7 +223,7 @@ Met2dDataFile * Met2dDataFileFactory::new_met_2d_data_file(const char *filename,
if(mtddf &&
type != FileType_Python_Numpy &&
type != FileType_Python_Xarray) {
if(!(mtddf->open(filename))) {
if(!(mtddf->open(filename, attr_grid))) {
mlog << Error << "\nMet2dDataFileFactory::new_met_2d_data_file() -> "
<< "error opening file \"" << filename << "\"\n\n";
exit(1);
Expand All @@ -235,7 +235,7 @@ Met2dDataFile * Met2dDataFileFactory::new_met_2d_data_file(const char *filename,
// Otherwise determine the type from the file name
//
else {
mtddf = new_met_2d_data_file(filename);
mtddf = new_met_2d_data_file(filename, attr_grid);
}

return mtddf;
Expand Down
4 changes: 2 additions & 2 deletions src/libcode/vx_data2d_factory/data2d_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class Met2dDataFileFactory
{
public:
static Met2dDataFile *new_met_2d_data_file(GrdFileType type);
static Met2dDataFile *new_met_2d_data_file(const char *filename);
static Met2dDataFile *new_met_2d_data_file(const char *filename, GrdFileType type);
static Met2dDataFile *new_met_2d_data_file(const char *filename, ConcatString attr_grid="");
static Met2dDataFile *new_met_2d_data_file(const char *filename, GrdFileType type, ConcatString attr_grid="");
};

///////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion src/libcode/vx_data2d_grib/data2d_grib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ return;
////////////////////////////////////////////////////////////////////////


bool MetGrib1DataFile::open(const char * _filename)
bool MetGrib1DataFile::open(const char * _filename, ConcatString grid_str)

{

Expand Down
2 changes: 1 addition & 1 deletion src/libcode/vx_data2d_grib/data2d_grib.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class MetGrib1DataFile : public Met2dDataFile {
// do stuff
//

bool open(const char * filename);
bool open(const char * filename, ConcatString grid_str="");

void close();

Expand Down
2 changes: 1 addition & 1 deletion src/libcode/vx_data2d_grib2/data2d_grib2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void MetGrib2DataFile::close() {

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

bool MetGrib2DataFile::open(const char * _filename) {
bool MetGrib2DataFile::open(const char * _filename, ConcatString grid_str) {
Filename = _filename;
if( nullptr == (FileGrib2 = met_fopen(Filename.c_str(), "r")) ){
mlog << Error << "\nMetGrib2DataFile::open() -> "
Expand Down
2 changes: 1 addition & 1 deletion src/libcode/vx_data2d_grib2/data2d_grib2.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class MetGrib2DataFile : public Met2dDataFile {
// do stuff
//

bool open (const char * filename);
bool open (const char * filename, ConcatString grid_str="");

void close ();

Expand Down
4 changes: 2 additions & 2 deletions src/libcode/vx_data2d_nc_cf/data2d_nc_cf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ void MetNcCFDataFile::close() {

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

bool MetNcCFDataFile::open(const char * _filename) {
bool MetNcCFDataFile::open(const char * _filename, ConcatString attr_grid) {

close();

_file = new NcCfFile;

if(!_file->open(_filename)) {
if(!_file->open(_filename, attr_grid)) {
mlog << Error << "\nMetNcCFDataFile::open(const char *) -> "
<< "unable to open NetCDF file \"" << _filename << "\"\n\n";
close();
Expand Down
2 changes: 1 addition & 1 deletion src/libcode/vx_data2d_nc_cf/data2d_nc_cf.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class MetNcCFDataFile : public Met2dDataFile {
// do stuff
//

bool open (const char * filename);
bool open (const char * filename, ConcatString grid_str="");

void close ();

Expand Down
25 changes: 20 additions & 5 deletions src/libcode/vx_data2d_nc_cf/nc_cf_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "vx_cal.h"
#include "vx_log.h"

#include "data2d_utils.h"
#include "nc_cf_file.h"

using namespace std;
Expand Down Expand Up @@ -169,7 +170,7 @@ void NcCfFile::close()
////////////////////////////////////////////////////////////////////////


bool NcCfFile::open(const char * filepath)
bool NcCfFile::open(const char * filepath, ConcatString grid_str)
{
unixtime ut;
int sec_per_unit;
Expand All @@ -189,6 +190,8 @@ bool NcCfFile::open(const char * filepath)
// FIXME: Commented out with NetCDF4 enabling
// NcError err(NcError::silent_nonfatal);

_grid_str = grid_str;

// Open the file
_ncFile = open_ncfile(filepath);

Expand Down Expand Up @@ -438,7 +441,12 @@ bool NcCfFile::open(const char * filepath)
StringArray dimNames;
string var_x_dim_name, var_y_dim_name;
if (IS_VALID_NC_P(_xDim)) var_x_dim_name = GET_NC_NAME_P(_xDim);
cout << " DEBUG HS var_x_dim_name = " << var_x_dim_name << "\n";
cout << " DEBUG HS IS_VALID_NC_P(_yDim) = " << IS_VALID_NC_P(_yDim) << "\n";
if (IS_VALID_NC_P(_yDim)) cout << " DEBUG HS _yDim->getName() = " << _yDim->getName() << "\n";
if (IS_VALID_NC_P(_yDim)) cout << " DEBUG HS GET_NC_NAME_P(_yDim) = " << GET_NC_NAME_P(_yDim) << "\n";
if (IS_VALID_NC_P(_yDim)) var_y_dim_name = GET_NC_NAME_P(_yDim);
cout << " DEBUG HS var_y_dim_name = " << var_y_dim_name << "\n";
for (int j=0; j<Nvars; ++j) {

int dim_count = Var[j].Ndims;
Expand Down Expand Up @@ -1359,6 +1367,7 @@ void NcCfFile::read_netcdf_grid()
NcVar *data_var = nullptr;
NcVar *tmp_data_var = nullptr;
IntArray var_index_list;
const char *method_name = "NcCfFile::read_netcdf_grid() -> ";

for (int i = 0; i < Nvars; ++i)
{
Expand Down Expand Up @@ -1458,11 +1467,17 @@ void NcCfFile::read_netcdf_grid()
status = true;
}

if (_grid_str.nonempty()) {
status = build_grid_by_grid_string(_grid_str, grid, method_name);
mlog << Debug(3) << method_name
<< "Override the grid by set_attr_grid=\"" << _grid_str << "\"\n";
}

if (!status ||
!((_xDim && _yDim) ||
(x_dim_var_name.nonempty() && y_dim_var_name.nonempty())))
{
mlog << Error << "\nNcCfFile::read_netcdf_grid() -> "
mlog << Error << "\n" << method_name
<< "Couldn't figure out projection from information in netCDF file.\n\n";
exit(1);
}
Expand Down Expand Up @@ -3406,7 +3421,7 @@ LatLonData NcCfFile::get_data_from_lat_lon_vars(NcVar *lat_var, NcVar *lon_var,
<< i-1 << "]=" << lat_values[i-1] << " lat["
<< i << "]=" << lat_values[i] << " "
<< fabs(curr_delta - dlat) << " > " << degree_tolerance << "\n";
mlog << Error << "\n" << method_name << " -> "
mlog << Warning << "\n" << method_name << " -> "
<< "MET can only process Latitude/Longitude files where the latitudes are evenly spaced (dlat="
<< dlat <<", delta[" << i << "]=" << curr_delta << ")\n\n";
sanity_check_failed = true;
Expand All @@ -3426,15 +3441,15 @@ LatLonData NcCfFile::get_data_from_lat_lon_vars(NcVar *lat_var, NcVar *lon_var,
<< i-1 << "]=" << lon_values[i-1] << " lon["
<< i << "]=" << lon_values[i] << " "
<< fabs(curr_delta - dlon) << " > " << degree_tolerance << "\n";
mlog << Error << "\n" << method_name << " -> "
mlog << Warning << "\n" << method_name << " -> "
<< "MET can only process Latitude/Longitude files where the longitudes are evenly spaced (dlon="
<< dlon <<", delta[" << i << "]=" << curr_delta << ")\n\n";
sanity_check_failed = true;
break;
}
}

if (sanity_check_failed) {
if (sanity_check_failed && _grid_str.empty()) {
mlog << Error << "\n" << method_name << " -> "
<< "Please check the input data is the lat/lon projection\n\n";
exit(1);
Expand Down
11 changes: 10 additions & 1 deletion src/libcode/vx_data2d_nc_cf/nc_cf_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class NcCfFile {
NcCfFile();
~NcCfFile();

bool open(const char * filename);
bool open(const char * filename, ConcatString attr_grid="");

void close();

Expand Down Expand Up @@ -117,11 +117,14 @@ class NcCfFile {
NcVarInfo* find_var_name(const char * var_name) const;
NcVarInfo* find_var_by_dim_name(const char *dim_name) const;

//void set_attr_grid(ConcatString grid_str);

private:

static const double DELTA_TOLERANCE;

netCDF::NcFile * _ncFile; // allocated
ConcatString _grid_str;

//
// dimensions
Expand Down Expand Up @@ -198,6 +201,12 @@ class NcCfFile {
////////////////////////////////////////////////////////////////////////


//inline void NcCfFile::set_attr_grid(ConcatString grid_str) { attr_grid = grid_str; };


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


#endif /* __NC_CF_FILE_H__ */


Expand Down
2 changes: 1 addition & 1 deletion src/libcode/vx_data2d_nc_met/data2d_nc_met.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void MetNcMetDataFile::close() {

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

bool MetNcMetDataFile::open(const char * _filename) {
bool MetNcMetDataFile::open(const char * _filename, ConcatString grid_str) {

close();

Expand Down
2 changes: 1 addition & 1 deletion src/libcode/vx_data2d_nc_met/data2d_nc_met.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class MetNcMetDataFile : public Met2dDataFile {
// do stuff
//

bool open (const char * filename);
bool open (const char * filename, ConcatString grid_str="");

void close ();

Expand Down
2 changes: 1 addition & 1 deletion src/libcode/vx_data2d_nc_wrf/data2d_nc_wrf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void MetNcWrfDataFile::close() {

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

bool MetNcWrfDataFile::open(const char * _filename) {
bool MetNcWrfDataFile::open(const char * _filename, ConcatString grid_str) {

close();

Expand Down
2 changes: 1 addition & 1 deletion src/libcode/vx_data2d_nc_wrf/data2d_nc_wrf.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class MetNcWrfDataFile : public Met2dDataFile {
// do stuff
//

bool open (const char * filename);
bool open (const char * filename, ConcatString grid_str="");

void close ();

Expand Down
2 changes: 1 addition & 1 deletion src/libcode/vx_data2d_python/data2d_python.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ return;
////////////////////////////////////////////////////////////////////////


bool MetPythonDataFile::open(const char * cur_command)
bool MetPythonDataFile::open(const char * cur_command, ConcatString grid_str)

{

Expand Down
2 changes: 1 addition & 1 deletion src/libcode/vx_data2d_python/data2d_python.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class MetPythonDataFile : public Met2dDataFile {
// do stuff
//

bool open(const char * cur_command);
bool open(const char * cur_command, ConcatString grid_str="");

void close();

Expand Down
2 changes: 1 addition & 1 deletion src/libcode/vx_data2d_ugrid/data2d_ugrid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void MetUGridDataFile::close() {

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

bool MetUGridDataFile::open(const char * _filename) {
bool MetUGridDataFile::open(const char * _filename, ConcatString grid_str) {

close();

Expand Down
2 changes: 1 addition & 1 deletion src/libcode/vx_data2d_ugrid/data2d_ugrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class MetUGridDataFile : public Met2dDataFile {
// do stuff
//

bool open (const char * filename);
bool open (const char * filename, ConcatString grid_str="");
bool open_metadata(const char * filename);
ConcatString coordinate_file() const;
void set_ugrid_configs(ConcatString dataset_name, double max_distance_km,
Expand Down

0 comments on commit 40133cd

Please sign in to comment.