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 1700 python #1765

Merged
merged 16 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
050f24b
Per #1700, no real change, removing extra newline.
JohnHalleyGotway Apr 22, 2021
b4d297a
Merge branch 'develop' into feature_1700_python
JohnHalleyGotway Apr 22, 2021
94279ac
Per #1700, move global_python.h from vx_data2d_python over to the vx_…
JohnHalleyGotway Apr 22, 2021
bf6e760
Per #1700, no code changes. Just removing commented out code.
JohnHalleyGotway Apr 23, 2021
6125e8b
Per #1700, lots of little changes to make the python scripts consiste…
JohnHalleyGotway Apr 23, 2021
88226b7
Per #1700, rename generic_python.py to set_python_env.py. Still actua…
JohnHalleyGotway Apr 23, 2021
8a82ad6
Per #1700 remove the pickle import.
JohnHalleyGotway Apr 23, 2021
acf9bd0
Per #1700, work in progress. Replaced pickle file with tmp file.
JohnHalleyGotway Apr 23, 2021
9c31d61
Per #1700, work in progress. Replaced pickle file with tmp file.
JohnHalleyGotway Apr 23, 2021
f969442
Per #1700, update read_tmp_ascii.py to work for both ascii2nc and sta…
JohnHalleyGotway Apr 23, 2021
f765966
Per #1700, getting closer. Work in progress. Just need to get user-py…
JohnHalleyGotway Apr 23, 2021
8237705
Per #1700, removing extraneous cout.
JohnHalleyGotway Apr 23, 2021
c6f61fb
Per #1700, fix logic in PyLineDataFile::do_tmp_ascii() to get stat_an…
JohnHalleyGotway Apr 25, 2021
124a44b
Per #1700, just comments.
JohnHalleyGotway Apr 25, 2021
e28d653
Per #1700, replace references to pickle with user_python
JohnHalleyGotway Apr 25, 2021
624d1bb
Per #1700, update documentation to replace pickle with temp files.
JohnHalleyGotway Apr 25, 2021
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
9 changes: 4 additions & 5 deletions met/data/wrappers/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ SUBDIRS =
wrappersdir = $(pkgdatadir)/wrappers

wrappers_DATA = \
generic_python.py \
generic_pickle.py \
set_python_env.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
10 changes: 0 additions & 10 deletions met/data/wrappers/generic_pickle.py

This file was deleted.

17 changes: 8 additions & 9 deletions met/data/wrappers/read_tmp_ascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
Message_Type, Station_ID, Valid_Time, Lat, Lon, Elevation,
GRIB_Code or Variable_Name, Level, Height, QC_String, Observation_Value

MPR format: See documentation of the MPR line type

Version Date
1.0.0 2021/02/18 David Fillmore Initial version
"""
Expand All @@ -18,25 +20,22 @@

import argparse

point_data = None

def read_tmp_ascii(filename):
"""
Arguments:
filename (string): temporary file created by write_tmp_point.py
filename (string): temporary file created by write_tmp_point.py or write_tmp_mpr.py

Returns:
(list of lists): point data
(list of lists): point or mpr data
"""
print('read_tmp_ascii:' + filename)
f = open(filename, 'r')
lines = f.readlines()
f.close()

global point_data
point_data = [eval(line.strip('\n')) for line in lines]

return point_data
global ascii_data
ascii_data = [eval(line.strip('\n')) for line in lines]
return ascii_data

if __name__ == '__main__':
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
########################################################################
#
# When MET_PYTHON_EXE is not defined, this script initializes
# MET's python runtime environment.
# This script initializes MET's python runtime environment.
#
########################################################################

import os
import sys
import numpy as np
import pickle
33 changes: 0 additions & 33 deletions met/data/wrappers/write_pickle_mpr.py

This file was deleted.

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
39 changes: 39 additions & 0 deletions met/data/wrappers/write_tmp_mpr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
########################################################################
#
# Adapted from a script provided by George McCabe
# Adapted by Randy Bullock
#
# usage: /path/to/python write_tmp_mpr.py \
# tmp_output_filename <user_python_script>.py <args>
#
########################################################################

import os
import sys
import importlib.util

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)

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
6 changes: 3 additions & 3 deletions met/docs/Users_Guide/appendixF.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ The types of Python embedding supported in MET are described below. In all cases

Setting this environment variable triggers slightly different processing logic in MET. Rather than executing the user-specified script with compiled Python instance directly, MET does the following:

1. Wrap the user's Python script and arguments with a wrapper script (write_pickle_mpr.py, write_pickle_point.py, or write_pickle_dataplane.py) and specify the name of a temporary file to be written.
1. Wrap the user's Python script and arguments with a wrapper script (write_tmp_mpr.py, write_tmp_point.py, or write_tmp_dataplane.py) and specify the name of a temporary file to be written.

2. Use a system call to the **MET_PYTHON_EXE** Python instance to execute these commands and write the resulting data objects to a temporary Python pickle file.
2. Use a system call to the **MET_PYTHON_EXE** Python instance to execute these commands and write the resulting data objects to a temporary ASCII or NetCDF file.

3. Use the compiled Python instance to read data from that temporary pickle file.
3. Use the compiled Python instance to run a wrapper script (read_tmp_ascii.py or read_tmp_dataplane.py) to read data from that temporary file.

With this approach, users should be able to execute Python scripts in their own custom environments.

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
Loading