from __future__ import print_function import os import sys import re import numpy as np import datetime as dt from netCDF4 import Dataset,chartostring ########################################### ## ## input file specified on the command line ## load the data into the numpy array ## if len(sys.argv) == 3: # Store the input file and variable name input_file = os.path.expandvars(sys.argv[1]) var_name = sys.argv[2] try: # Print some output to verify that this script ran print("Input File: " + repr(input_file)) print("Variable Name: " + repr(var_name)) # Read input file f = Dataset(input_file, 'r') # Read data and update bad data value data = np.float64(f.variables[var_name][:]) data[data < -999] = -9999 met_data = data.copy() print("Data Shape: " + repr(met_data.shape)) print("Data Type: " + repr(met_data.dtype)) except NameError: print("Trouble reading input file: " + input_file) else: print("Must specify exactly one input file and the variable name.") sys.exit(1) ########################################### ## ## create the metadata dictionary ## # Parse valid time from the file name found = 0 for token in os.path.basename(input_file).split('_'): if(re.search("[0-9]{8,8}", token)): valid = dt.datetime.strptime(token[0:8],"%Y%m%d") found = 1 print("Valid Date: " + valid.strftime("%Y%m%d_%H%M%S")) if(found == 0): print("Can't determine valid time!") sys.exit(1) attrs = { 'valid': valid.strftime("%Y%m%d_%H%M%S"), 'init': valid.strftime("%Y%m%d_%H%M%S"), 'lead': '00', 'accum': '00', 'name': var_name, 'long_name': 'UNKNOWN', 'level': 'Surface', 'units': 'UNKNOWN', 'grid': { 'name': 'Global 1/4 Degree', 'type' : 'LatLon', 'lat_ll' : -90.0, 'lon_ll' : -180.0, 'delta_lat' : 0.25, 'delta_lon' : 0.25, 'Nlat' : 720, 'Nlon' : 1440, } } print("Attributes: " + repr(attrs))