Skip to content

Commit

Permalink
Per #2781, added init function for nc_point_obs to take an input file…
Browse files Browse the repository at this point in the history
…name. Also raise TypeError exception from nc_point_obs.read_data() if input file cannot be read
  • Loading branch information
georgemccabe committed May 8, 2024
1 parent 67703e3 commit 51db09f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
5 changes: 3 additions & 2 deletions scripts/python/examples/read_met_point_obs_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
print("Input File:\t" + repr(input_file))

# Read MET point observation NetCDF file
point_obs = nc_point_obs()
if not point_obs.read_data(input_file):
try:
point_obs = nc_point_obs(input_file)
except TypeError:
print(f"ERROR: Could not read MET point data file {input_file}")
sys.exit(1)

Expand Down
16 changes: 8 additions & 8 deletions scripts/python/met/point_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def get_string_array(nc_group, var_name):

class nc_point_obs(met_point_obs):

def __init__(self, nc_filename=None):
if nc_filename:
self.read_data(nc_filename)

# args should be string, list, or dictionary
def get_nc_filename(self, args):
nc_filename = None
Expand All @@ -70,17 +74,14 @@ def get_nc_filename(self, args):
def read_data(self, nc_filename):
method_name = f"{self.__class__.__name__}.read_data()"
if not nc_filename:
print(f"ERROR: {method_name} The input NetCDF filename is missing")
return False
raise TypeError(f"{method_name} The input NetCDF filename is missing")
if not os.path.exists(nc_filename):
print(f"ERROR: {method_name} input NetCDF file ({nc_filename}) does not exist")
return False
raise TypeError(f"{method_name} input NetCDF file ({nc_filename}) does not exist")

try:
dataset = nc.Dataset(nc_filename, 'r')
dataset = nc.Dataset(nc_filename, 'r')
except OSError:
print(f"ERROR: {method_name} Could not open NetCDF file ({nc_filename}")
return False
raise TypeError(f"{method_name} Could not open NetCDF file ({nc_filename}")

attr_name = 'use_var_id'
use_var_id_str = dataset.getncattr(attr_name) if attr_name in dataset.ncattrs() else "false"
Expand Down Expand Up @@ -131,7 +132,6 @@ def read_data(self, nc_filename):
self.obs_vid = np.array(nc_var[:])

self.obs_qty_table = met_point_nc_tools.get_string_array(dataset, 'obs_qty_table')
return True

def save_ncfile(self, nc_filename):
met_data = self.get_point_data()
Expand Down

0 comments on commit 51db09f

Please sign in to comment.