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 1319 no pickle #1699

Merged
merged 43 commits into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from 41 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
ebddb56
Start on write netcdf pickle alternative.
Jan 26, 2021
0fdbfdd
Write dataplane array.
Jan 26, 2021
6d46603
Start on read of netcdf as pickle alternative.
Jan 26, 2021
6fe4245
Create attribute variables.
Feb 2, 2021
644db21
Use global attributes for met_info attrs.
Feb 3, 2021
6594062
Add grid structure.
Feb 4, 2021
c6667e3
Read metadata back into met_info.attrs.
Feb 4, 2021
1e6eb9e
Convert grid.nx and grid.ny to int.
Feb 4, 2021
e005585
Rename _name key to name.
Feb 4, 2021
ab986ca
Removed pickle write.
Feb 4, 2021
760b690
Fixed write_pickle_dataplane to work for both numpy and xarray.
Feb 5, 2021
791ebf0
Use items() to iterate of key, value attrs.
Feb 5, 2021
c5f17e8
Write temporary text file.
Feb 13, 2021
d6142e8
Renamed scripts.
Feb 17, 2021
b39ca28
Changed script names in Makefile.am.
Feb 17, 2021
7cc2d77
Replaced pickle with tmp_nc.
Feb 17, 2021
df0db18
Fixed wrapper script names.
Feb 17, 2021
044c704
Test for attrs in met_in.met_data.
Feb 17, 2021
d798e9d
Initial version of read_tmp_point module.
Feb 18, 2021
8116e75
Added read_tmp_point.py to install list.
Feb 18, 2021
7b57715
Start on Python3_Script::read_tmp_point.
Feb 18, 2021
5502da9
Write MPR tmp ascii file.
Feb 18, 2021
961b4fc
Renamed to read_tmp_ascii to use for point point and MPR.
Feb 18, 2021
4c0963d
Renamed to read_tmp_ascii to use for point point and MPR.
Feb 18, 2021
91122be
Define Python3_Script::import_read_tmp_ascii_py.
Feb 19, 2021
fef8484
Call Python3_Script::import_read_tmp_ascii_py.
Feb 19, 2021
93e9762
Append MET_BASE/wrappers to sys.path.
Feb 20, 2021
44d8328
Finished implementation of Python3_Script::import_read_tmp_ascii_py.
Feb 20, 2021
3953aba
Call Python3_Script::read_tmp_ascii in python_handler.
Feb 20, 2021
25961d6
Revised python3_script::read_tmp_ascii with call to run, PyRun_String.
Feb 22, 2021
794e8fb
Return PyObject* from Python3_Script::run.
Feb 22, 2021
d569cfb
Restored call to run_python_string for now.
Feb 22, 2021
f21b2e6
Added python3_script::import_read_tmp_ascii.
Mar 5, 2021
ba85a46
Merge branch 'develop' into feature_1319_no_pickle
Mar 5, 2021
31ae2e4
Restored read_tmp_ascii call.
Mar 5, 2021
f8becb9
Added lookup into ascii module.
Mar 5, 2021
b0c8813
Return PyObject* from read_tmp_ascii.
Mar 7, 2021
bd9ed77
Put point_data in global namespace.
Mar 7, 2021
b358bed
Remove temporary ascii file.
Mar 7, 2021
4506173
Added tmp_ascii_path.
Mar 7, 2021
d6ed4b9
Removed read_obs_from_pickle.
Mar 7, 2021
6568493
Replaced tmp netcdf _name attribute with name_str.
Mar 12, 2021
22f5e98
Append user script path to system path.
Mar 15, 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
8 changes: 5 additions & 3 deletions met/data/wrappers/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ wrappersdir = $(pkgdatadir)/wrappers
wrappers_DATA = \
generic_python.py \
generic_pickle.py \
read_pickle_dataplane.py \
write_pickle_dataplane.py \
read_tmp_dataplane.py \
write_tmp_dataplane.py \
write_pickle_mpr.py \
write_pickle_point.py
read_tmp_ascii.py \
write_pickle_point.py \
write_tmp_point.py

EXTRA_DIST = ${wrappers_DATA}

Expand Down
15 changes: 0 additions & 15 deletions met/data/wrappers/read_pickle_dataplane.py

This file was deleted.

49 changes: 49 additions & 0 deletions met/data/wrappers/read_tmp_ascii.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
Module Name: read_tmp_ascii.py

Read MET Point Observations from a text file created by write_tmp_point.py script
or MET Matched Pairs from a text file created by write_tmp_mpr.py script

Point observation format:
Message_Type, Station_ID, Valid_Time, Lat, Lon, Elevation,
GRIB_Code or Variable_Name, Level, Height, QC_String, Observation_Value

Version Date
1.0.0 2021/02/18 David Fillmore Initial version
"""

__author__ = 'David Fillmore'
__version__ = '1.0.0'
__email__ = 'met_help@ucar.edu'

import argparse

point_data = None

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

Returns:
(list of lists): point 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

if __name__ == '__main__':
"""
Parse command line arguments
"""
parser = argparse.ArgumentParser()
parser.add_argument('--filename', type=str)
args = parser.parse_args()

data = read_tmp_ascii(args.filename)
36 changes: 36 additions & 0 deletions met/data/wrappers/read_tmp_dataplane.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
########################################################################
#
# Reads temporary file into memory.
#
# usage: /path/to/python read_tmp_dataplane.py dataplane.tmp
#
########################################################################

import sys
import numpy as np
import netCDF4 as nc

print('Python Script:\t', sys.argv[0])
met_info = {}

netcdf_filename = sys.argv[1]
print('Read NetCDF:\t', netcdf_filename)

# read NetCDF file
ds = nc.Dataset(netcdf_filename, 'r')
met_data = ds['met_data'][:]
met_attrs = {}
grid = {}
for attr, attr_val in ds.__dict__.items():
if 'grid' in attr:
grid_attr = attr.split('.')[1]
grid[grid_attr] = attr_val
else:
met_attrs[attr] = attr_val
grid['nx'], grid['ny'] = int(grid['nx']), int(grid['ny'])
met_attrs['grid'] = grid
met_attrs['name'] = met_attrs['_name']
del met_attrs['_name']
met_info['met_data'] = met_data
met_info['attrs'] = met_attrs
print(met_info)
5 changes: 4 additions & 1 deletion met/data/wrappers/write_pickle_mpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
print('Write Pickle:\t', sys.argv[1])

pickle_filename = sys.argv[1]
tmp_filename = pickle_filename + '.txt'

pyembed_module_name = sys.argv[2]
sys.argv = sys.argv[2:]
Expand All @@ -28,6 +29,8 @@
met_in = importlib.util.module_from_spec(spec)
spec.loader.exec_module(met_in)

print(met_in)
f = open(tmp_filename, 'w')
for line in met_in.mpr_data:
f.write(str(line) + '\n')

pickle.dump( met_in.mpr_data, open( pickle_filename, "wb" ) )
5 changes: 5 additions & 0 deletions met/data/wrappers/write_pickle_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
print('Write Pickle:\t', sys.argv[1])

pickle_filename = sys.argv[1]
tmp_filename = pickle_filename + '.txt'

pyembed_module_name = sys.argv[2]
sys.argv = sys.argv[2:]
Expand All @@ -28,4 +29,8 @@
met_in = importlib.util.module_from_spec(spec)
spec.loader.exec_module(met_in)

f = open(tmp_filename, 'w')
for line in met_in.point_data:
f.write(str(line) + '\n')

pickle.dump( met_in.point_data, open( pickle_filename, "wb" ) )
63 changes: 63 additions & 0 deletions met/data/wrappers/write_tmp_dataplane.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
########################################################################
#
# Adapted from a script provided by George McCabe
# Adapted by Randy Bullock
#
# usage: /path/to/python write_tmp_dataplane.py \
# tmp_output_filename <user_python_script>.py <args>
#
########################################################################

import os
import sys
import importlib.util
import netCDF4 as nc

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

netcdf_filename = sys.argv[1]

print('Write NetCDF:\t', netcdf_filename)

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

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)

met_info = {'met_data': met_in.met_data}
if hasattr(met_in.met_data, 'attrs') and met_in.met_data.attrs:
attrs = met_in.met_data.attrs
else:
attrs = met_in.attrs
met_info['attrs'] = attrs

print('write_tmp_dataplane')
print(met_info)

# write NetCDF file
ds = nc.Dataset(netcdf_filename, 'w')

nx, ny = met_in.met_data.shape
ds.createDimension('x', nx)
ds.createDimension('y', ny)
dp = ds.createVariable('met_data', met_in.met_data.dtype, ('x', 'y'))
dp[:] = met_in.met_data

for attr, attr_val in met_info['attrs'].items():
print(attr, attr_val, type(attr_val))
if attr == 'name':
setattr(ds, '_name', attr_val)
if type(attr_val) == str:
setattr(ds, attr, attr_val)
if type(attr_val) == dict:
for key in attr_val:
setattr(ds, attr + '.' + key, attr_val[key])
ds.close()
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,30 @@
# Adapted from a script provided by George McCabe
# Adapted by Randy Bullock
#
# usage: /path/to/python write_pickle_dataplane.py \
# pickle_output_filename <user_python_script>.py <args>
# usage: /path/to/python write_tmp_point.py \
# tmp_ascii_output_filename <user_python_script>.py <args>
#
########################################################################

import os
import sys
import pickle
import importlib.util
import xarray as xr

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

pickle_filename = sys.argv[1]
tmp_filename = sys.argv[1]

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

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)

if isinstance(met_in.met_data, xr.DataArray):
met_info = { 'attrs': met_in.met_data.attrs, 'met_data': met_in.met_data }
else:
met_info = { 'attrs': met_in.attrs, 'met_data': met_in.met_data }

print(met_info)

pickle.dump( met_info, open( pickle_filename, "wb" ) )
f = open(tmp_filename, 'w')
for line in met_in.point_data:
f.write(str(line) + '\n')
Loading