from __future__ import print_function import os import sys import re import numpy as np import datetime as dt ########################################### ## ## input file specified on the command line ## load the data into the numpy array ## if len(sys.argv) == 3: # Store the input file and record number input_file = os.path.expandvars(sys.argv[1]) rec_num = int(sys.argv[2]) try: # Print some output to verify that this script ran print("Input File: " + repr(input_file)) print("Record Num: " + repr(rec_num)) # Check bounds if(rec_num > 18): print("Record number cannot exceed 18.") sys.exit(1) # Read input file data = np.float64(np.fromfile(input_file, 'f').reshape(19, 181, 360)) # Read data and update bad data value met_data = data[rec_num].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 record number.") sys.exit(1) ########################################### ## ## create the metadata dictionary ## for token in os.path.basename(input_file).replace('-', '_').split('_'): if(re.search("[0-9]{8,8}", token)): ymd = dt.datetime.strptime(token[0:8],"%Y%m%d") elif(re.search("[0-9]{2,2}z", token)): hh = int(token.replace("z", "")) elif(re.search("[0-9]+day.bin", token)): day = int(token.replace("day.bin", "")) init = ymd + dt.timedelta(hours=hh) valid = init + dt.timedelta(days=day) lead, rem = divmod((valid-init).total_seconds(), 3600) attrs = { 'valid': valid.strftime("%Y%m%d_%H%M%S"), 'init': init.strftime("%Y%m%d_%H%M%S"), 'lead': str(int(lead)), 'accum': '00', 'name': 'UNKNOWN', 'long_name': 'UNKNOWN', 'level': 'Surface', 'units': 'UNKNOWN', 'grid': { 'name': 'Global 1 Degree', 'type' : 'LatLon', 'lat_ll' : -90.0, 'lon_ll' : -180.0, 'delta_lat' : 1.0, 'delta_lon' : 1.0, 'Nlat' : 181, 'Nlon' : 360, } } print("Attributes: " + repr(attrs))