Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Commit

Permalink
The ncf2cbh scripts now account for the nhm_ids to map the CBH files …
Browse files Browse the repository at this point in the history
…when they are created.

This fixes a big bug in their creation.
  • Loading branch information
markstro-usgs committed Oct 22, 2019
1 parent a64fd1a commit 04f72a9
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 28 deletions.
30 changes: 26 additions & 4 deletions ncf2cbh/ncf2cbh.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from netCDF4 import num2date
import datetime
import sys
import numpy as np
import csv

def read(nc_fn):
nc_fid = Dataset(nc_fn, 'r')
Expand Down Expand Up @@ -48,10 +50,21 @@ def read(nc_fn):
# full path required for nc_fn
def run(dir, nc_fn):
var_names, base_date, nts, vals = read(nc_fn)

# read the mapping
nhm_id = np.zeros(109951, dtype = np.int)
ii = 0
nhm_id_file = dir + 'nhm_id'
with open(nhm_id_file) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
nhm_id[ii] = int(row[0])
ii = ii + 1

# Write CBH files.
for name in var_names:
v = vals[name]
v2 = np.zeros(109951)
nfeats = len(v[0])
fn2 = dir + name + ".cbh"
current_date = base_date
Expand All @@ -66,14 +79,23 @@ def run(dir, nc_fn):
+ str(current_date.day) + ' 0 0 0')
for jj in range(nfeats):
if name == 'prcp':
v[ii, jj] = v[ii, jj] / 25.4
v2[jj] = v[ii, nhm_id[jj]-1] / 25.4
elif name == 'tmax':
v[ii, jj] = v[ii, jj] * 9 / 5 + 32
v2[jj] = v[ii, nhm_id[jj]-1] * 9 / 5 + 32
elif name == 'tmin':
v[ii, jj] = v[ii, jj] * 9 / 5 + 32
v2[jj] = v[ii, nhm_id[jj]-1] * 9 / 5 + 32
else:
"don't know how to convert units"
fp.write(' ' + str(v[ii,jj]))
for jj in range(nfeats):
if name == 'prcp':
fp.write(' ' + '{:.2f}'.format(v2[jj]))
elif name == 'tmax':
fp.write(' ' + '{:.1f}'.format(v2[jj]))
elif name == 'tmin':
fp.write(' ' + '{:.1f}'.format(v2[jj]))
else:
fp.write(' ' + '{:.2f}'.format(v2[jj]))

fp.write('\n')
current_date += datetime.timedelta(days=1)

Expand Down
93 changes: 72 additions & 21 deletions ncf2cbh/yeti_ncf_2_cbh.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
from netCDF4 import Dataset # http://code.google.com/p/netcdf4-python/

#from netCDF4 import Dataset # http://code.google.com/p/netcdf4-python/
import datetime
from prms_utils import ncf_reader
import sys
import numpy as np
import csv

nhm_id_file = "/work/markstro/operat/setup/test/NHM-PRMS_CONUS/input/nhm_id"

def main(dir, nc_fn):
def main(dir, nc_fn, yr_str):
var_names, base_date, nts, vals = ncf_reader.read(nc_fn)

# read the mapping
nhm_id = np.zeros(109951, dtype = np.int)
ii = 0
with open(nhm_id_file) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
nhm_id[ii] = int(row[0])
ii = ii + 1

print(nhm_id)

# Write CBH files.
for name in var_names:
v = vals[name]
v2 = np.zeros(109951)
nfeats = len(v[0])
fn2 = dir + name + ".cbh"
fn2 = dir + yr_str + "_" + name + ".cbh"
current_date = base_date
print("base_date", str(base_date))
print('writing ' + fn2)
Expand All @@ -24,28 +40,63 @@ def main(dir, nc_fn):
fp.write(str(current_date.year) + ' ' + str(current_date.month) + ' '
+ str(current_date.day) + ' 0 0 0')
for jj in range(nfeats):
# if name == 'prcp':
# v[ii, jj] = v[ii, jj] / 25.4
# elif name == 'tmax':
# v[ii, jj] = v[ii, jj] * 9 / 5 + 32
# elif name == 'tmin':
# v[ii, jj] = v[ii, jj] * 9 / 5 + 32
# else:
# "don't know how to convert units"
fp.write(' ' + str(v[ii,jj]))
if name == 'prcp':
v2[jj] = v[ii, nhm_id[jj]-1] / 25.4
elif name == 'tmax':
v2[jj] = v[ii, nhm_id[jj]-1] * 9 / 5 + 32
elif name == 'tmin':
v2[jj] = v[ii, nhm_id[jj]-1] * 9 / 5 + 32
else:
"don't know how to convert units"
for jj in range(nfeats):
if name == 'prcp':
fp.write(' ' + '{:.2f}'.format(v2[jj]))
elif name == 'tmax':
fp.write(' ' + '{:.1f}'.format(v2[jj]))
elif name == 'tmin':
fp.write(' ' + '{:.1f}'.format(v2[jj]))
else:
fp.write(' ' + '{:.2f}'.format(v2[jj]))

fp.write('\n')
current_date += datetime.timedelta(days=1)


if __name__ == '__main__':
dir = '/work/markstro/operat/setup/test/cbh_update/gridMet/'
nc_fn = dir + '2019_climate_2019_09_13.nc'

argc = len(sys.argv) - 1
# print(argc)
dir = '/work/markstro/operat/setup/init_file/updated_cbh_20191018/'

ft = [('1979','_climate_2019_10_08.nc'),('1980','_climate_2019_10_08.nc'),
('1981','_climate_2019_10_09.nc'),('1982','_climate_2019_10_10.nc'),
('1983','_climate_2019_10_11.nc'),('1984','_climate_2019_10_12.nc'),
('1985','_climate_2019_10_13.nc'),('1986','_climate_2019_10_14.nc'),
('1987','_climate_2019_10_16.nc'),('1988','_climate_2019_10_17.nc'),
('1989','_climate_2019_10_18.nc'),('1990','_climate_2019_10_08.nc'),
('1991','_climate_2019_10_09.nc'),('1992','_climate_2019_10_10.nc'),
('1993','_climate_2019_10_11.nc'),('1994','_climate_2019_10_12.nc'),
('1995','_climate_2019_10_13.nc'),('1996','_climate_2019_10_14.nc'),
('1997','_climate_2019_10_15.nc'),('1998','_climate_2019_10_16.nc'),
('1999','_climate_2019_10_17.nc'),('2000','_climate_2019_10_08.nc'),
('2001','_climate_2019_10_09.nc'),('2002','_climate_2019_10_10.nc'),
('2003','_climate_2019_10_11.nc'),('2004','_climate_2019_10_12.nc'),
('2005','_climate_2019_10_13.nc'),('2006','_climate_2019_10_14.nc'),
('2007','_climate_2019_10_15.nc'),('2008','_climate_2019_10_16.nc'),
('2009','_climate_2019_10_17.nc'),('2010','_climate_2019_10_08.nc'),
('2011','_climate_2019_10_09.nc'),('2012','_climate_2019_10_10.nc'),
('2013','_climate_2019_10_11.nc'),('2014','_climate_2019_10_12.nc'),
('2015','_climate_2019_10_13.nc'),('2016','_climate_2019_10_14.nc'),
('2017','_climate_2019_10_15.nc'),('2018','_climate_2019_10_16.nc'),
('2019','_climate_2019_10_17.nc')
]

for f in ft:
nc_fn = dir + f[0] + f[1]

if argc == 1:
print('setting dir = ' + sys.argv[1])
dir = sys.argv[1]
# argc = len(sys.argv) - 1
# # print(argc)
#
# if argc == 1:
# print('setting dir = ' + sys.argv[1])
# dir = sys.argv[1]

main(dir, nc_fn)
main(dir, nc_fn, f[0])
diff
7 changes: 4 additions & 3 deletions out2ncf/prms_outputs3_ncf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# Wed Sep 04 09:43:53 MDT 2019
#
# This script runs against a CSV file and writes the
# contents into a set of netcdf files, one for each variable
# contents into a set of netcdf files, one for each variable. It converts the
# whole CSV file to a netcdf file. If the files are big, this will be very slow.

#from prms_utils import csv_reader
import numpy as np
Expand Down Expand Up @@ -114,7 +115,7 @@ def main(dir):
with open(json_file, "r") as read_file:
cntl = json.load(read_file)

# Read the PRMS outputk
# Read the PRMS output
var_names = cntl["output_variables"].keys()
val_list = []
dim_list = set()
Expand Down Expand Up @@ -160,7 +161,7 @@ def main(dir):
# nsegments = len(seg_lat_vals)

# write the ncf file
ofn = str(dir) + "/out/" + str(end_date) + "_" + var_name + "_out.nc"
ofn = str(dir) + "/output/" + str(end_date) + "_" + var_name + "_out.nc"
# print('writing netcdf file ' + cntl['ncf_file_name'])
print('writing netcdf file ' + ofn)
ncf = Dataset(ofn, 'w', format='NETCDF4_CLASSIC')
Expand Down

0 comments on commit 04f72a9

Please sign in to comment.