Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature #2525 fill value at dataplane #2557

Merged
merged 18 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 84 additions & 70 deletions scripts/python/examples/read_ascii_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,82 +4,96 @@

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

print("Python Script:\t" + repr(sys.argv[0]))
def log(msg):
dataplane.log_msg(msg)

def set_dataplane_attrs():
# attrs is a dictionary which contains attributes describing the dataplane.
# attrs should have 9 items, each of data type string:
# 'name': data name
# 'long_name': descriptive name
# 'valid': valid time (format = 'yyyymmdd_hhmmss')
# 'init': init time (format = 'yyyymmdd_hhmmss')
# 'lead': lead time (format = 'hhmmss')
# 'accum': accumulation time (format = 'hhmmss')
# 'level': vertilcal level
# 'units': units of the data
# 'grid': contains the grid information
# - a grid name (G212)
# - a gridded data file name
# - MET specific grid string, "lambert 185 129 12.19 -133.459 -95 40.635 6371.2 25 25 N"
# - a dictionary for the grid information

valid_time = '20050807_120000'
init_time = '20050807_000000'
lead_time = '120000'
accum_time = '120000'
v_level = 'Surface'
units = 'None'

grid_lambert_conformal = {
'type': 'Lambert Conformal',
'hemisphere': 'N',

'name': 'FooGrid',

'scale_lat_1': 25.0,
'scale_lat_2': 25.0,

'lat_pin': 12.19,
'lon_pin': -135.459,

'x_pin': 0.0,
'y_pin': 0.0,

'lon_orient': -95.0,

'd_km': 40.635,
'r_km': 6371.2,

'nx': 185,
'ny': 129,
}

long_name = data_name + "_word"
return dataplane.set_dataplane_attrs(data_name, valid_time, init_time,
lead_time, accum_time, v_level, units,
grid_lambert_conformal, long_name)

log("Python Script:\t" + repr(sys.argv[0]))

##
## input file specified on the command line
## load the data into the numpy array
##
##
## input file specified on the command line
## load the data into the numpy array
##

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)
dataplane.quit("read_ascii_numpy.py -> Must specify exactly one input file and a name for the data.")

# Read the input file as the first argument
input_file = os.path.expandvars(sys.argv[1])
data_name = sys.argv[2]

try:
print("Input File:\t" + repr(input_file))
print("Data Name:\t" + repr(data_name))
# read_2d_text_input() reads n by m text data and returns 2D numpy array
met_data = dataplane.read_2d_text_input(input_file)
print("Data Shape:\t" + repr(met_data.shape))
print("Data Type:\t" + repr(met_data.dtype))
except NameError:
met_data = None
print("Can't find the input file")

# attrs is a dictionary which contains attributes describing the dataplane.
# attrs should have 9 items, each of data type string:
# 'name': data name
# 'long_name': descriptive name
# 'valid': valid time (format = 'yyyymmdd_hhmmss')
# 'init': init time (format = 'yyyymmdd_hhmmss')
# 'lead': lead time (format = 'hhmmss')
# 'accum': accumulation time (format = 'hhmmss')
# 'level': vertilcal level
# 'units': units of the data
# 'grid': contains the grid information
# - a grid name (G212)
# - a gridded data file name
# - MET specific grid string, "lambert 185 129 12.19 -133.459 -95 40.635 6371.2 25 25 N"
# - a dictionary for the grid information

valid_time = '20050807_120000'
init_time = '20050807_000000'
lead_time = '120000'
accum_time = '120000'
v_level = 'Surface'
units = 'None'

grid_lambert_conformal = {
'type': 'Lambert Conformal',
'hemisphere': 'N',

'name': 'FooGrid',

'scale_lat_1': 25.0,
'scale_lat_2': 25.0,

'lat_pin': 12.19,
'lon_pin': -135.459,

'x_pin': 0.0,
'y_pin': 0.0,

'lon_orient': -95.0,

'd_km': 40.635,
'r_km': 6371.2,

'nx': 185,
'ny': 129,
}

long_name = data_name + "_word"
attrs = dataplane.set_dataplane_attrs(data_name, valid_time, init_time,
lead_time, accum_time, v_level, units,
grid_lambert_conformal, long_name)

print("Attributes:\t" + repr(attrs))
log("Input File:\t" + repr(input_file))
log("Data Name:\t" + repr(data_name))
if os.path.exists(input_file):
# read_2d_text_input() reads n by m text data and returns 2D numpy array
met_data = dataplane.read_2d_text_input(input_file)
if met_data is None:
dataplane.quit(f" Fail to build met_data from {input_file}")
else:
log("Data Shape:\t" + repr(met_data.shape))
log("Data Type:\t" + repr(met_data.dtype))
else:
dataplane.quit(f"input {input_file} does exist!!!")
except:
import traceback
traceback.print_exc()
dataplane.quit(f"Unknown error with {sys.argv[0]}: ")

attrs = set_dataplane_attrs()
log("Attributes:\t" + repr(attrs))

# Sets fill_value if it exists
#attrs['fill_value'] = 255 # for letter.txt
35 changes: 24 additions & 11 deletions scripts/python/examples/read_ascii_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,41 @@

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

print("Python Script:\t" + repr(sys.argv[0]))
def log(msg):
dataplane.log_msg(msg)

log("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) != 3:
print("ERROR: read_ascii_xarray.py -> Must specify exactly one input file and a name for the data.")
sys.exit(1)
dataplane.quit("read_ascii_xarray.py -> Must specify exactly one input file and a name for the data.")

# Read the input file as the first argument
input_file = os.path.expandvars(sys.argv[1])
data_name = sys.argv[2]

try:
print("Input File:\t" + repr(input_file))
print("Data Name:\t" + repr(data_name))
# read_2d_text_input() reads n by m text data and returns 2D numpy array
met_data = dataplane.read_2d_text_input(input_file)
print("Data Shape:\t" + repr(met_data.shape))
print("Data Type:\t" + repr(met_data.dtype))
except NameError:
log("Input File:\t" + repr(input_file))
log("Data Name:\t" + repr(data_name))
if os.path.exists(input_file):
# read_2d_text_input() reads n by m text data and returns 2D numpy array
met_data = dataplane.read_2d_text_input(input_file)
if met_data is None:
dataplane.quit(f" Fail to build met_data from {input_file}")
else:
log("Data Shape:\t" + repr(met_data.shape))
log("Data Type:\t" + repr(met_data.dtype))
else:
dataplane.quit(f"input {input_file} does exist!!!")
except:
met_data = None
print("Can't read the input file")
import traceback
traceback.print_exc()
dataplane.quit(f"Unknown error with {sys.argv[0]}: ")

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

Expand Down Expand Up @@ -105,5 +115,8 @@
# Delete the met_data variable, and reset it to be the Xarray object
del met_data

# Sets fill_value/min_value/max_value if it exists
#ds.attrs['fill_value'] = 255

# Create met_data and specify attrs because XR doesn't persist them.
met_data = xr.DataArray(ds.fcst, attrs=ds.attrs)
1 change: 1 addition & 0 deletions scripts/python/met/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
pythonmetscriptsdir = $(pkgdatadir)/python/met

pythonmetscripts_DATA = \
logger.py \
dataplane.py \
mprbase.py \
point.py
Expand Down
1 change: 1 addition & 0 deletions scripts/python/met/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ top_builddir = @top_builddir@
top_srcdir = @top_srcdir@
pythonmetscriptsdir = $(pkgdatadir)/python/met
pythonmetscripts_DATA = \
logger.py \
dataplane.py \
mprbase.py \
point.py
Expand Down
Loading