diff --git a/scripts/python/examples/read_met_point_obs_pandas.py b/scripts/python/examples/read_met_point_obs_pandas.py index 51613ae3b6..bf7bb5527b 100644 --- a/scripts/python/examples/read_met_point_obs_pandas.py +++ b/scripts/python/examples/read_met_point_obs_pandas.py @@ -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) diff --git a/scripts/python/met/point_nc.py b/scripts/python/met/point_nc.py index d3dfc4220c..c0773e65f7 100644 --- a/scripts/python/met/point_nc.py +++ b/scripts/python/met/point_nc.py @@ -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 @@ -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" @@ -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()