from __future__ import print_function import os import sys 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) == 2: # Read the input file as the first argument input_file = os.path.expandvars(sys.argv[1]) try: # Print some output to verify that this script ran print("Input File: " + repr(input_file)) lines = [line.rstrip('\n') for line in open(input_file)] nlon = int(lines[0].split()[1]) nlat = int(lines[1].split()[1]) lon_ll = float(lines[2].split()[1]) lat_ll = float(lines[3].split()[1]) dlat = float(lines[4].split()[1]) dlon = dlat bad_data = float(lines[5].split()[1]) met_data = np.loadtxt(input_file, skiprows=6) met_data[met_data == bad_data] = -9999 met_data = met_data.copy() print("Data Shape: " + repr(met_data.shape)) print("Data Type: " + repr(met_data.dtype)) except NameError: print("Can't find the input file") else: print("Must specify exactly one input file.") sys.exit(1) ########################################### ## ## parse time info from file name ## valid = dt.datetime.strptime(os.path.basename(input_file), "AWS_CMORPH_crain-%Y%m%d%H.txt") init = valid - dt.timedelta(hours=1) ## ## create the metadata dictionary ## attrs = { 'valid': valid.strftime("%Y%m%d_%H%M%S"), 'init': init.strftime("%Y%m%d_%H%M%S"), 'lead': '00', 'accum': '01', # 1-hourly 'name': 'APCP_01', 'long_name': '1-Hourly Accumulated Precip', 'level': 'Surface', 'units': 'mm', 'grid': { 'name': 'CMORPH', 'type' : 'LatLon', 'lat_ll' : lat_ll, 'lon_ll' : lon_ll, 'delta_lat' : dlat, 'delta_lon' : dlon, 'Nlat' : nlat, 'Nlon' : nlon, } } print("Attributes: " + repr(attrs))