Skip to content

Commit

Permalink
Per #1700, lots of little changes to make the python scripts consiste…
Browse files Browse the repository at this point in the history
…nt, updating the write*.py functions to add the user script directory to the system path, and remove extraneous log messages.
  • Loading branch information
JohnHalleyGotway committed Apr 23, 2021
1 parent bf6e760 commit 6125e8b
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 24 deletions.
6 changes: 3 additions & 3 deletions met/data/wrappers/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ wrappers_DATA = \
generic_python.py \
generic_pickle.py \
read_tmp_dataplane.py \
write_tmp_dataplane.py \
write_pickle_mpr.py \
read_tmp_ascii.py \
write_tmp_point.py
write_tmp_dataplane.py \
write_tmp_point.py \
write_tmp_mpr.py

EXTRA_DIST = ${wrappers_DATA}

Expand Down
1 change: 0 additions & 1 deletion met/data/wrappers/read_tmp_ascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def read_tmp_ascii(filename):
Returns:
(list of lists): point data
"""
print('read_tmp_ascii:' + filename)
f = open(filename, 'r')
lines = f.readlines()
f.close()
Expand Down
4 changes: 4 additions & 0 deletions met/data/wrappers/write_tmp_dataplane.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
import importlib.util
import netCDF4 as nc

print("Python Script:\t" + repr(sys.argv[0]))
print("User Command:\t" + repr(' '.join(sys.argv[2:])))
print("Temporary File:\t" + repr(sys.argv[1]))

netcdf_filename = sys.argv[1]
pyembed_module_name = sys.argv[2]
sys.argv = sys.argv[2:]
Expand Down
27 changes: 17 additions & 10 deletions met/data/wrappers/write_tmp_mpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# Adapted from a script provided by George McCabe
# Adapted by Randy Bullock
#
# usage: /path/to/python write_pickle_mpr.py \
# pickle_output_filename <user_python_script>.py <args>
# usage: /path/to/python write_tmp_mpr.py \
# tmp_output_filename <user_python_script>.py <args>
#
########################################################################

Expand All @@ -13,21 +13,28 @@
import pickle
import importlib.util

print('Python Script:\t', sys.argv[0])
print('User Command:\t', sys.argv[2:])
print('Write Pickle:\t', sys.argv[1])

pickle_filename = sys.argv[1]
print("Python Script:\t" + repr(sys.argv[0]))
print("User Command:\t" + repr(' '.join(sys.argv[2:])))
print("Temporary File:\t" + repr(sys.argv[1]))

tmp_filename = sys.argv[1]
pyembed_module_name = sys.argv[2]
sys.argv = sys.argv[2:]

# append user script dir to system path
pyembed_dir, pyembed_file = os.path.split(pyembed_module_name)
if pyembed_dir:
sys.path.insert(0, pyembed_dir)

if not pyembed_module_name.endswith('.py'):
pyembed_module_name += '.py'

user_base = os.path.basename(pyembed_module_name).replace('.py','')

spec = importlib.util.spec_from_file_location(user_base, pyembed_module_name)
met_in = importlib.util.module_from_spec(spec)
spec.loader.exec_module(met_in)

print(met_in)

pickle.dump( met_in.mpr_data, open( pickle_filename, "wb" ) )
f = open(tmp_filename, 'w')
for line in met_in.mpr_data:
f.write(str(line) + '\n')
17 changes: 12 additions & 5 deletions met/data/wrappers/write_tmp_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,30 @@
# Adapted by Randy Bullock
#
# usage: /path/to/python write_tmp_point.py \
# tmp_ascii_output_filename <user_python_script>.py <args>
# tmp_output_filename <user_python_script>.py <args>
#
########################################################################

import os
import sys
import importlib.util

print('Python Script:\t', sys.argv[0])
print('User Command:\t', sys.argv[2:])
print('Write Temporary Ascii:\t', sys.argv[1])
print("Python Script:\t" + repr(sys.argv[0]))
print("User Command:\t" + repr(' '.join(sys.argv[2:])))
print("Temporary File:\t" + repr(sys.argv[1]))

tmp_filename = sys.argv[1]

pyembed_module_name = sys.argv[2]
sys.argv = sys.argv[2:]

# append user script dir to system path
pyembed_dir, pyembed_file = os.path.split(pyembed_module_name)
if pyembed_dir:
sys.path.insert(0, pyembed_dir)

if not pyembed_module_name.endswith('.py'):
pyembed_module_name += '.py'

user_base = os.path.basename(pyembed_module_name).replace('.py','')

spec = importlib.util.spec_from_file_location(user_base, pyembed_module_name)
Expand Down
2 changes: 1 addition & 1 deletion met/scripts/python/read_ascii_mpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

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

##
## input file specified on the command line
Expand Down
2 changes: 1 addition & 1 deletion met/scripts/python/read_ascii_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

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

##
## input file specified on the command line
Expand Down
2 changes: 1 addition & 1 deletion met/scripts/python/read_ascii_numpy_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

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

##
## input file specified on the command line
Expand Down
2 changes: 1 addition & 1 deletion met/scripts/python/read_ascii_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

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

##
## input file specified on the command line
Expand Down
2 changes: 1 addition & 1 deletion met/scripts/python/read_ascii_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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

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

##
## input file specified on the command line
Expand Down

0 comments on commit 6125e8b

Please sign in to comment.