Skip to content

Commit

Permalink
Removed future imports since they were used to transition from python…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
georgemccabe committed Sep 19, 2022
1 parent 8ebd1fd commit c086ca3
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 139 deletions.
72 changes: 35 additions & 37 deletions scripts/python/derive_WRF_semilatlon.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import print_function

import os
import sys
import numpy as np
Expand All @@ -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")

###########################################

##
Expand Down
35 changes: 16 additions & 19 deletions scripts/python/read_ascii_mpr.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import print_function

import pandas as pd
import os
import sys
Expand All @@ -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")

########################################################################
30 changes: 14 additions & 16 deletions scripts/python/read_ascii_numpy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import print_function

import numpy as np
import os
import sys
Expand All @@ -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")

###########################################

##
Expand Down
30 changes: 14 additions & 16 deletions scripts/python/read_ascii_numpy_grid.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import print_function

import numpy as np
import os
import sys
Expand All @@ -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")

###########################################

##
Expand Down
68 changes: 33 additions & 35 deletions scripts/python/read_ascii_point.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import print_function

import pandas as pd
import os
import sys
Expand All @@ -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)

########################################################################
30 changes: 14 additions & 16 deletions scripts/python/read_ascii_xarray.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import print_function

import numpy as np
import os
import sys
Expand All @@ -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")

###########################################

##
Expand Down

0 comments on commit c086ca3

Please sign in to comment.