Skip to content

Commit

Permalink
Per #1954, ci-run-unit add unit tests to demonstrate the newly added …
Browse files Browse the repository at this point in the history
…semilatlon functionality. Still need to update the docs.
  • Loading branch information
JohnHalleyGotway committed Sep 15, 2022
1 parent 092bd34 commit a3bc30c
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 2 deletions.
41 changes: 41 additions & 0 deletions internal/test_unit/xml/unit_python.xml
Original file line number Diff line number Diff line change
Expand Up @@ -587,4 +587,45 @@
</output>
</test>

<test name="python_plot_data_plane_SEMILATLON_ZONAL_MEAN">
<exec>&MET_BIN;/plot_data_plane</exec>
<param> \
PYTHON_NUMPY \
&OUTPUT_DIR;/python/wrfout_d01_2008-08-08_12:00:00_PLEV_ZONAL_MEAN.ps \
'name="&MET_BASE;/python/derive_WRF_semilatlon.py &DATA_DIR_MODEL;/p_interp/wrfout_d01_2008-08-08_12:00:00_PLEV TT lat";' \
-title "WRF Zonal Mean" \
-v 1
</param>
<output>
<ps>&OUTPUT_DIR;/python/wrfout_d01_2008-08-08_12:00:00_PLEV_ZONAL_MEAN.ps</ps>
</output>
</test>

<test name="python_pcp_combine_SEMILATLON_MERIDIONAL_MEAN">
<exec>&MET_BIN;/pcp_combine</exec>
<param> \
-add PYTHON_NUMPY \
'name="&MET_BASE;/python/derive_WRF_semilatlon.py &DATA_DIR_MODEL;/p_interp/wrfout_d01_2008-08-08_12:00:00_PLEV TT lon";' \
&OUTPUT_DIR;/python/wrfout_d01_2008-08-08_12:00:00_PLEV_MERIDIONAL_MEAN.nc \
-name "TT_MERIDIONAL_MEAN" -v 1
</param>
<output>
<ps>&OUTPUT_DIR;/python/wrfout_d01_2008-08-08_12:00:00_PLEV_MERIDIONAL_MEAN.nc</ps>
</output>
</test>

<test name="python_plot_data_plane_SEMILATLON_MERIDIONAL_MEAN">
<exec>&MET_BIN;/plot_data_plane</exec>
<param> \
&OUTPUT_DIR;/python/wrfout_d01_2008-08-08_12:00:00_PLEV_MERIDIONAL_MEAN.nc \
&OUTPUT_DIR;/python/wrfout_d01_2008-08-08_12:00:00_PLEV_MERIDIONAL_MEAN.ps \
'name="TT_MERIDIONAL_MEAN"; level="(*,*)";' \
-title "WRF Meridional Mean" \
-v 1
</param>
<output>
<ps>&OUTPUT_DIR;/python/wrfout_d01_2008-08-08_12:00:00_PLEV_MERIDIONAL_MEAN.ps</ps>
</output>
</test>

</met_test>
3 changes: 2 additions & 1 deletion scripts/python/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ pythonscripts_DATA = \
read_ascii_xarray.py \
read_ascii_point.py \
read_ascii_mpr.py \
read_met_point_obs.py
read_met_point_obs.py \
derive_WRF_semilatlon.py

EXTRA_DIST = ${pythonscripts_DATA}

Expand Down
3 changes: 2 additions & 1 deletion scripts/python/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ pythonscripts_DATA = \
read_ascii_xarray.py \
read_ascii_point.py \
read_ascii_mpr.py \
read_met_point_obs.py
read_met_point_obs.py \
derive_WRF_semilatlon.py

EXTRA_DIST = ${pythonscripts_DATA}
MAINTAINERCLEANFILES = Makefile.in
Expand Down
103 changes: 103 additions & 0 deletions scripts/python/derive_WRF_semilatlon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
from __future__ import print_function

import os
import sys
import numpy as np
import datetime as dt
from netCDF4 import Dataset,chartostring

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

##
## input file specified on the command line
## 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:
print("Must specify exactly one input file, variable name, and summary axis (lat, lon, latlon).")
sys.exit(1)

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

##
## create the metadata dictionary
##

init = dt.datetime.strptime(getattr(f, "START_DATE"), "%Y-%m-%d_%H:%M:%S")
valid_ref = dt.datetime.strptime(getattr(f.variables["Time"], "units"), "hours since %Y-%m-%d %H:%M:%S")
add_hours = float(f.variables["Time"][:])
valid = valid_ref + dt.timedelta(hours=add_hours)
lead, rem = divmod((valid-init).total_seconds(), 3600)
accum = "00"

# Use the first column of lats

if axis == "lon":
lats = list()
lons = list(np.float64(f.variables["XLONG"][0,0,:]))
elif axis == "lat":
lats = list(np.float64(f.variables["XLAT"][0,:,0]))
lons = list()
elif axis == "latlon":
lats = list(np.float64(f.variables["XLONG"][0,0,:]))
lons = list(np.float64(f.variables["XLAT"][0,0,:]))

levels = list(pvals)
times = list()

attrs = {
'valid': valid.strftime("%Y%m%d_%H%M%S"),
'init': init.strftime("%Y%m%d_%H%M%S"),
'lead': str(int(lead)),
'accum': accum,

'name': var_name,
'long_name': str(getattr(f.variables[var_name], "description")),
'level': axis + "_mean",
'units': str(getattr(f.variables[var_name], "units")),

'grid': {
'type' : "SemiLatLon",
'name' : axis + "_mean",
'lats' : lats,
'lons' : lons,
'levels' : levels,
'times' : times
}
}

print("Attributes: " + repr(attrs))

0 comments on commit a3bc30c

Please sign in to comment.