Skip to content

Commit

Permalink
Per #2781, added function to convert MET NetCDF point observation dat…
Browse files Browse the repository at this point in the history
…a to pandas so it can be read and modified in a python embedding script. Added example python embedding script
  • Loading branch information
georgemccabe committed Apr 24, 2024
1 parent 2a26d59 commit c722bdc
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
43 changes: 43 additions & 0 deletions scripts/python/examples/pyembed_met_point_nc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import os
import sys

from met.point_nc import nc_point_obs

print(f"Python Script:\t{sys.argv[0]}")

arg_cnt = len(sys.argv)
if len(sys.argv) != 2:
print("ERROR: read_met_point.py -> Specify only 1 input file")
sys.exit(1)

# Read the input file as the first argument
input_file = os.path.expandvars(sys.argv[1])
try:
print("Input File:\t" + repr(input_file))

# Read and format the input 11-column observations:
# (1) string: Message_Type
# (2) string: Station_ID
# (3) string: Valid_Time(YYYYMMDD_HHMMSS)
# (4) numeric: Lat(Deg North)
# (5) numeric: Lon(Deg East)
# (6) numeric: Elevation(msl)
# (7) string: Var_Name(or GRIB_Code)
# (8) numeric: Level
# (9) numeric: Height(msl or agl)
# (10) string: QC_String
# (11) numeric: Observation_Value

# Read 11 column text input data by using pandas package
point_obs = nc_point_obs()
if not point_obs.read_data(input_file):
print(f"ERROR: Could not read MET point data file {input_file}")
sys.exit(1)

df = point_obs.to_pandas()
point_data = df.values.tolist()
print(f" point_data: Data Length:\t{len(point_data)}")
print(f" point_data: Data Type:\t{type(point_data)}")
except FileNotFoundError:
print(f"The input file {input_file} does not exist")
sys.exit(1)
18 changes: 18 additions & 0 deletions scripts/python/met/point_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
'''

import sys
import os

import numpy as np
import netCDF4 as nc
import pandas as pd

from met.point import met_point_obs, met_point_tools

Expand Down Expand Up @@ -274,6 +276,22 @@ def write_nc_data(nc_dataset, point_obs):
print(f' === ERROR at {method_name} type(nc_dataset)={type(nc_dataset)} type(point_obs)={type(point_obs)}')
raise

def to_pandas(self):
return pd.DataFrame({
'typ': [self.hdr_typ_table[i] for i in self.hdr_typ],
'sid': [self.hdr_sid_table[i] for i in self.hdr_sid],
'vld': [self.hdr_vld_table[i] for i in self.hdr_vld],
'lat': self.hdr_lat,
'lon': self.hdr_lon,
'elv': self.hdr_elv,
'var': [self.obs_var_table[i] if self.use_var_id else f'{i}'
for i in self.obs_vid],
'lvl': self.obs_lvl,
'hgt': self.obs_hgt,
'qc': [np.nan if np.ma.is_masked(i) else self.obs_qty_table[i]
for i in self.obs_qty],
'obs': self.obs_val,
})

def main(argv):
if len(argv) != 1 and argv[1] != ARG_PRINT_DATA:
Expand Down

0 comments on commit c722bdc

Please sign in to comment.