From c086ca333c3260871b5fe68e734b025921fe851c Mon Sep 17 00:00:00 2001 From: George McCabe <23407799+georgemccabe@users.noreply.github.com> Date: Mon, 19 Sep 2022 10:48:59 -0600 Subject: [PATCH] Removed future imports since they were used to transition from python 2 to 3. Rearranged logic to be more readible by checking error conditions and exiting if they are not met instead of putting all of the logic to run inside the if statement --- scripts/python/derive_WRF_semilatlon.py | 72 ++++++++++++------------- scripts/python/read_ascii_mpr.py | 35 ++++++------ scripts/python/read_ascii_numpy.py | 30 +++++------ scripts/python/read_ascii_numpy_grid.py | 30 +++++------ scripts/python/read_ascii_point.py | 68 ++++++++++++----------- scripts/python/read_ascii_xarray.py | 30 +++++------ 6 files changed, 126 insertions(+), 139 deletions(-) diff --git a/scripts/python/derive_WRF_semilatlon.py b/scripts/python/derive_WRF_semilatlon.py index 4a9bc74f31..d42cfc66c3 100644 --- a/scripts/python/derive_WRF_semilatlon.py +++ b/scripts/python/derive_WRF_semilatlon.py @@ -1,5 +1,3 @@ -from __future__ import print_function - import os import sys import numpy as np @@ -13,44 +11,44 @@ ## load the data into the numpy array ## -if len(sys.argv) == 4: - # Read the input file as the first argument - input_file = os.path.expandvars(sys.argv[1]) - var_name = sys.argv[2] - axis = sys.argv[3] - - try: - # Print some output to verify that this script ran - print("Input File: " + repr(input_file)) - print("Variable: " + repr(var_name)) - print("Axis: " + repr(axis)) - - # Read input file - f = Dataset(input_file, 'r') - - data = np.float64(f.variables[var_name][0,:,:,:]) - data[data > 1.0e30] = np.nan - - pvals = list(np.float64(f.variables["pressure"][:])) - - if axis == "lon": - met_data = np.nanmean(data[::-1], axis=1).copy() - elif axis == "lat": - met_data = np.nanmean(data[::-1], axis=2).transpose().copy() - elif axis == "latlon": - met_data = np.nanmean(data[::-1], axis=1).copy() - else: - print("ERROR: Unsupported axis type: " + axis) - sys.exit(1) - - print("Data Shape: " + repr(met_data.shape)) - print("Data Type: " + repr(met_data.dtype)) - except NameError: - print("Trouble reading data from input file") -else: +if len(sys.argv) != 4: print("Must specify exactly one input file, variable name, and summary axis (lat, lon, latlon).") sys.exit(1) +# Read the input file as the first argument +input_file = os.path.expandvars(sys.argv[1]) +var_name = sys.argv[2] +axis = sys.argv[3] + +try: + # Print some output to verify that this script ran + print("Input File: " + repr(input_file)) + print("Variable: " + repr(var_name)) + print("Axis: " + repr(axis)) + + # Read input file + f = Dataset(input_file, 'r') + + data = np.float64(f.variables[var_name][0,:,:,:]) + data[data > 1.0e30] = np.nan + + pvals = list(np.float64(f.variables["pressure"][:])) + + if axis == "lon": + met_data = np.nanmean(data[::-1], axis=1).copy() + elif axis == "lat": + met_data = np.nanmean(data[::-1], axis=2).transpose().copy() + elif axis == "latlon": + met_data = np.nanmean(data[::-1], axis=1).copy() + else: + print("ERROR: Unsupported axis type: " + axis) + sys.exit(1) + + print("Data Shape: " + repr(met_data.shape)) + print("Data Type: " + repr(met_data.dtype)) +except NameError: + print("Trouble reading data from input file") + ########################################### ## diff --git a/scripts/python/read_ascii_mpr.py b/scripts/python/read_ascii_mpr.py index 4a8c988405..fa71b8e6d2 100755 --- a/scripts/python/read_ascii_mpr.py +++ b/scripts/python/read_ascii_mpr.py @@ -1,5 +1,3 @@ -from __future__ import print_function - import pandas as pd import os import sys @@ -13,24 +11,23 @@ ## load the data into the numpy array ## -if len(sys.argv) == 2: - # Read the input file as the first argument - input_file = os.path.expandvars(sys.argv[1]) - try: - print("Input File:\t" + repr(input_file)) - - # Read MPR lines, skipping the header row and first column. - mpr_data = pd.read_csv(input_file, header=None, - delim_whitespace=True, keep_default_na=False, - skiprows=1, usecols=range(1,37), - dtype=str).values.tolist() - print("Data Length:\t" + repr(len(mpr_data))) - print("Data Type:\t" + repr(type(mpr_data))) - except NameError: - print("Can't find the input file") -else: +if len(sys.argv) != 2: print("ERROR: read_ascii_point.py -> Must specify exactly one input file.") sys.exit(1) -######################################################################## +# Read the input file as the first argument +input_file = os.path.expandvars(sys.argv[1]) +try: + print("Input File:\t" + repr(input_file)) + + # Read MPR lines, skipping the header row and first column. + mpr_data = pd.read_csv(input_file, header=None, + delim_whitespace=True, keep_default_na=False, + skiprows=1, usecols=range(1,37), + dtype=str).values.tolist() + print("Data Length:\t" + repr(len(mpr_data))) + print("Data Type:\t" + repr(type(mpr_data))) +except NameError: + print("Can't find the input file") +######################################################################## diff --git a/scripts/python/read_ascii_numpy.py b/scripts/python/read_ascii_numpy.py index b7a1e081b4..6d129afc1c 100755 --- a/scripts/python/read_ascii_numpy.py +++ b/scripts/python/read_ascii_numpy.py @@ -1,5 +1,3 @@ -from __future__ import print_function - import numpy as np import os import sys @@ -13,23 +11,23 @@ ## load the data into the numpy array ## -if len(sys.argv) == 3: - # Read the input file as the first argument - input_file = os.path.expandvars(sys.argv[1]) - data_name = sys.argv[2] - try: - # Print some output to verify that this script ran - print("Input File:\t" + repr(input_file)) - print("Data Name:\t" + repr(data_name)) - met_data = np.loadtxt(input_file) - print("Data Shape:\t" + repr(met_data.shape)) - print("Data Type:\t" + repr(met_data.dtype)) - except NameError: - print("Can't find the input file") -else: +if len(sys.argv) != 3: print("ERROR: read_ascii_numpy.py -> Must specify exactly one input file and a name for the data.") sys.exit(1) +# Read the input file as the first argument +input_file = os.path.expandvars(sys.argv[1]) +data_name = sys.argv[2] +try: + # Print some output to verify that this script ran + print("Input File:\t" + repr(input_file)) + print("Data Name:\t" + repr(data_name)) + met_data = np.loadtxt(input_file) + print("Data Shape:\t" + repr(met_data.shape)) + print("Data Type:\t" + repr(met_data.dtype)) +except NameError: + print("Can't find the input file") + ########################################### ## diff --git a/scripts/python/read_ascii_numpy_grid.py b/scripts/python/read_ascii_numpy_grid.py index fa72cbff6b..3e4cc25f69 100755 --- a/scripts/python/read_ascii_numpy_grid.py +++ b/scripts/python/read_ascii_numpy_grid.py @@ -1,5 +1,3 @@ -from __future__ import print_function - import numpy as np import os import sys @@ -13,23 +11,23 @@ ## load the data into the numpy array ## -if len(sys.argv) == 3: - # Read the input file as the first argument - input_file = os.path.expandvars(sys.argv[1]) - data_name = sys.argv[2] - try: - # Print some output to verify that this script ran - print("Input File:\t" + repr(input_file)) - print("Data Name:\t" + repr(data_name)) - met_data = np.loadtxt(input_file) - print("Data Shape:\t" + repr(met_data.shape)) - print("Data Type:\t" + repr(met_data.dtype)) - except NameError: - print("Can't find the input file") -else: +if len(sys.argv) != 3: print("ERROR: read_ascii_numpy.py -> Must specify exactly one input file and a name for the data.") sys.exit(1) +# Read the input file as the first argument +input_file = os.path.expandvars(sys.argv[1]) +data_name = sys.argv[2] +try: + # Print some output to verify that this script ran + print("Input File:\t" + repr(input_file)) + print("Data Name:\t" + repr(data_name)) + met_data = np.loadtxt(input_file) + print("Data Shape:\t" + repr(met_data.shape)) + print("Data Type:\t" + repr(met_data.dtype)) +except NameError: + print("Can't find the input file") + ########################################### ## diff --git a/scripts/python/read_ascii_point.py b/scripts/python/read_ascii_point.py index cfd37d272a..ee7d2b7b9f 100755 --- a/scripts/python/read_ascii_point.py +++ b/scripts/python/read_ascii_point.py @@ -1,5 +1,3 @@ -from __future__ import print_function - import pandas as pd import os import sys @@ -8,40 +6,40 @@ print("Python Script:\t" + repr(sys.argv[0])) - ## - ## input file specified on the command line - ## load the data into the numpy array - ## - -if len(sys.argv) == 2: - # Read the input file as the first argument - input_file = os.path.expandvars(sys.argv[1]) - try: - print("Input File:\t" + repr(input_file)) - - # Read and format the input 11-column observations: - # (1) string: Message_Type - # (2) string: Station_ID - # (3) string: Valid_Time(YYYYMMDD_HHMMSS) - # (4) numeric: Lat(Deg North) - # (5) numeric: Lon(Deg East) - # (6) numeric: Elevation(msl) - # (7) string: Var_Name(or GRIB_Code) - # (8) numeric: Level - # (9) numeric: Height(msl or agl) - # (10) string: QC_String - # (11) numeric: Observation_Value - - point_data = pd.read_csv(input_file, header=None, delim_whitespace=True, keep_default_na=False, - names=['typ', 'sid', 'vld', 'lat', 'lon', 'elv', 'var', 'lvl', 'hgt', 'qc', 'obs'], - dtype={'typ':'str', 'sid':'str', 'vld':'str', 'var':'str', 'qc':'str'}).values.tolist() - print("Data Length:\t" + repr(len(point_data))) - print("Data Type:\t" + repr(type(point_data))) - except NameError: - print("Can't find the input file") -else: +## +## input file specified on the command line +## load the data into the numpy array +## + +if len(sys.argv) != 2: print("ERROR: read_ascii_point.py -> Must specify exactly one input file.") sys.exit(1) -######################################################################## +# Read the input file as the first argument +input_file = os.path.expandvars(sys.argv[1]) +try: + print("Input File:\t" + repr(input_file)) + + # Read and format the input 11-column observations: + # (1) string: Message_Type + # (2) string: Station_ID + # (3) string: Valid_Time(YYYYMMDD_HHMMSS) + # (4) numeric: Lat(Deg North) + # (5) numeric: Lon(Deg East) + # (6) numeric: Elevation(msl) + # (7) string: Var_Name(or GRIB_Code) + # (8) numeric: Level + # (9) numeric: Height(msl or agl) + # (10) string: QC_String + # (11) numeric: Observation_Value + + point_data = pd.read_csv(input_file, header=None, delim_whitespace=True, keep_default_na=False, + names=['typ', 'sid', 'vld', 'lat', 'lon', 'elv', 'var', 'lvl', 'hgt', 'qc', 'obs'], + dtype={'typ':'str', 'sid':'str', 'vld':'str', 'var':'str', 'qc':'str'}).values.tolist() + print("Data Length:\t" + repr(len(point_data))) + print("Data Type:\t" + repr(type(point_data))) +except NameError: + print("Can't find the input file") + sys.exit(1) +######################################################################## diff --git a/scripts/python/read_ascii_xarray.py b/scripts/python/read_ascii_xarray.py index 53f8574d48..6e906863a7 100755 --- a/scripts/python/read_ascii_xarray.py +++ b/scripts/python/read_ascii_xarray.py @@ -1,5 +1,3 @@ -from __future__ import print_function - import numpy as np import os import sys @@ -14,23 +12,23 @@ ## load the data into the numpy array ## -if len(sys.argv) == 3: - # Read the input file as the first argument - input_file = os.path.expandvars(sys.argv[1]) - data_name = sys.argv[2] - try: - # Print some output to verify that this script ran - print("Input File:\t" + repr(input_file)) - print("Data Name:\t" + repr(data_name)) - met_data = np.loadtxt(input_file) - print("Data Shape:\t" + repr(met_data.shape)) - print("Data Type:\t" + repr(met_data.dtype)) - except NameError: - print("Can't find the input file") -else: +if len(sys.argv) != 3: print("ERROR: read_ascii_xarray.py -> Must specify exactly one input file and a name for the data.") sys.exit(1) +# Read the input file as the first argument +input_file = os.path.expandvars(sys.argv[1]) +data_name = sys.argv[2] +try: + # Print some output to verify that this script ran + print("Input File:\t" + repr(input_file)) + print("Data Name:\t" + repr(data_name)) + met_data = np.loadtxt(input_file) + print("Data Shape:\t" + repr(met_data.shape)) + print("Data Type:\t" + repr(met_data.dtype)) +except NameError: + print("Can't find the input file") + ########################################### ##