Skip to content

Commit

Permalink
#2285 Renamed load_txt to read_2d_text_input. Removed get_grid_metada…
Browse files Browse the repository at this point in the history
…ta and added create_header_attrs
  • Loading branch information
Howard Soh committed Apr 13, 2023
1 parent 236d59c commit 632dcf1
Show file tree
Hide file tree
Showing 4 changed files with 270 additions and 151 deletions.
63 changes: 61 additions & 2 deletions scripts/python/examples/read_ascii_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,67 @@
input_file = os.path.expandvars(sys.argv[1])
data_name = sys.argv[2]

met_data = dataplane.load_txt(input_file, data_name)
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 = dataplane.get_grid_metadata(data_name)
# attrs is a dictionary which contains header data for the dataplane.
# attrs should have 9 members with string type data:
# '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.create_header_attrs(data_name, valid_time, init_time,
lead_time, accum_time, v_level, units,
grid_lambert_conformal, long_name)

print("Attributes:\t" + repr(attrs))
45 changes: 40 additions & 5 deletions scripts/python/examples/read_ascii_numpy_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,53 @@
##

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)
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]

met_data = dataplane.load_txt(input_file, data_name)
try:
# Print some output to verify that this script ran
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:
print("Can't find the input file")

# attrs is a dictionary which contains header data for the dataplane.
# attrs should have 9 members with string type data:
# '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'

## create the metadata dictionary from the environment variable,
## Default env_name = 'PYTHON_GRID'
grid_info = os.path.expandvars(os.getenv('PYTHON_GRID'))

attrs = dataplane.get_grid_metadata_from_env(data_name)
long_name = data_name + "_word"
attrs = dataplane.create_header_attrs(data_name, valid_time, init_time,
lead_time, accum_time, v_level, units,
grid_info, long_name)

print("Attributes:\t" + repr(attrs))
63 changes: 61 additions & 2 deletions scripts/python/examples/read_ascii_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,74 @@
input_file = os.path.expandvars(sys.argv[1])
data_name = sys.argv[2]

met_data = dataplane.load_txt(input_file, data_name)
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 read the input file")

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

##
## create the metadata dictionary
##

attrs = dataplane.get_grid_metadata(data_name)
# attrs is a dictionary which contains header data for the dataplane.
# attrs should have 9 members with string type data:
# '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.create_header_attrs(data_name, valid_time, init_time,
lead_time, accum_time, v_level, units,
grid_lambert_conformal)

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

Expand Down
Loading

0 comments on commit 632dcf1

Please sign in to comment.