Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 27 additions & 23 deletions tonic/models/vic/netcdf2vic.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@
def _run(args):

config = read_config(args.config)
files = config['options']['files']
var_keys = config['options']['var_keys']
output = config['options']['output']
binary_mult = config['options']['binary_mult']
binary_type = config['options']['binary_type'],
paths = config['options']['paths']
out_prefix = config['options']['out_prefix']
verbose = config['options']['verbose']

mask = read_netcdf(paths['mask_path'], nc_vars=['mask'])['mask']
files = [config['Basics']['files']]
var_keys = [config['Basics']['var_keys']]
output = config['Paths']['ASCIIoutPath']
#binary_mult = config['Basics']['binary_mult']
#binary_type = config['Basics']['binary_type'],
paths = config['Paths']
out_prefix = config['Basics']['out_prefix']
verbose = config['Basics']['verbose']
mask = read_netcdf(paths['mask_path'], variables=['mask'])[0]['mask']
yi, xi = np.nonzero(mask)
print('found {0} points in mask file.'.format(len(yi)))
print(mask)
print('found {0} points in mask fqile.'.format(len(xi)))
#x = read_netcdf(os.path.join(paths['in_path'], 'pr_1979.nc'))
#print(x)

xlist = []
ylist = []
Expand All @@ -37,25 +39,27 @@ def _run(args):

for i, fname in enumerate(files):
d = read_netcdf(os.path.join(paths['in_path'], fname),
verbose=verbose)

verbose=verbose)[0]
print(i)
if i == 0:

# find point locations
xs = d['xc']
ys = d['yc']
xs = d['lon']
ys = d['lat']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should just specify these values in the configuration file.

posinds = np.nonzero(xs > 180)
xs[posinds] -= 360
print('adjusted xs lon minimum')


for y, x in pyzip(yi, xi):
active_flag = False
for key in var_keys:
if (d[key][:, y, x].all() is np.ma.masked) \
or (mask[y, x] == 0):
active_flag = True
if not active_flag:
point = (ys[y, x], xs[y, x])
if not active_flag:
point = (ys[y], xs[x])
print(point)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was required because my original code was assuming the xs/ys were 2d. If we used meshgrid c. line 49, we could insure that the xs/ys variables were 2d.

xlist.append(x)
ylist.append(y)
pointlist.append(point)
Expand All @@ -70,11 +74,11 @@ def _run(args):
for j, key in enumerate(var_keys):
data[:, j] = d[key][:, y, x]

if output['Binary']:
write_binary(data * binary_mult, point, binary_type,
out_prefix, paths['BinaryoutPath'], append)
if output['ASCII']:
write_ascii(data, point, out_prefix, paths['ASCIIoutPath'],
#if output['Binary']:
# write_binary(data * binary_mult, point, binary_type,
# out_prefix, paths['BinaryoutPath'], append)
#if output['ASCII']:
write_ascii(data, point, out_prefix, paths['ASCIIoutPath'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why this was changed but we should make sure the example configuration file works with both Binary and ASCII output.

append)
return
# -------------------------------------------------------------------- #
Expand All @@ -86,7 +90,7 @@ def write_ascii(array, point, out_prefix, path, append, verbose=False):
"""
Write an array to standard VIC ASCII output.
"""
fname = out_prefix + ('%1.3f' % point[0]) + '_' + ('%1.3f' % point[1])
fname = out_prefix + ('%1.5f' % point[0]) + '_' + ('%1.5f' % point[1])
out_file = os.path.join(path, fname)
if append:
f = open(out_file, 'a')
Expand Down