Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
anom = d1 - d2
if amin is None:
amin = -1 * np.max(np.abs(anom))
if amax is None:
amax = np.max(np.abs(anom))
if cmap is None:
cmap = cmap_discretize('cm.winter')
if amap is None:
amap = cmap_discretize('cm.RdBu')
f, axarr = plt.subplots(3, 3, figsize=(13.5, 9), dpi=150)
f.tight_layout()
for layer in pyrange(3):
plt.sca(axarr[layer, 0])
sub_plot_pcolor(lons, lats, np.ma.masked_where(mask, d1[layer]),
vmin=vmin, vmax=vmax, units=units,
ncolors=9, title='{} (A)'.format(t1),
cmap=cmap, cbar_location='right',)
plt.ylabel('Layer {}'.format(layer))
plt.sca(axarr[layer, 1])
sub_plot_pcolor(lons, lats, np.ma.masked_where(mask, d2[layer]),
vmin=vmin, vmax=vmax,
ncolors=9, title='{} (B)'.format(t2), units=units,
cbar_location='right', cmap=cmap)
plt.sca(axarr[layer, 2])
sub_plot_pcolor(lons, lats, np.ma.masked_where(mask, anom[layer]),
ncolors=9, cmap=amap,
title='Difference (A-B)', units=units,
else:
raise ValueError('Unknown timesegment options \
{0}'.format(options['time_segment']))
print("Number of files: {0}".format(len(segment_dates) - 1))
assert len(segment_dates) == num_segments + 1
# Make sure the first and last dates are start/end_date
segment_dates[0] = start_date
segment_dates[-1] = end_date + timedelta(minutes=1)
# ---------------------------------------------------------------- #
# ---------------------------------------------------------------- #
# Setup Segments
segments = deque()
for num in pyrange(num_segments):
# Segment time bounds
t0 = segment_dates[num]
t1 = segment_dates[num + 1]
# Get segment inds
i0 = bisect_left(vic_datelist, t0)
i1 = bisect_left(vic_datelist, t1)
# Make segment filename (with path)
if options['time_segment'] == 'day':
filename = "{0}.{1}.nc".format(options['out_file_prefix'],
t0.strftime('%Y-%m-%d'))
elif options['time_segment'] == 'month':
filename = "{0}.{1}.nc".format(options['out_file_prefix'],
t0.strftime('%Y-%m'))
elif options['time_segment'] == 'year':
calendar=options['calendar'])
elif options['time_segment'] == 'month':
num_segments = (end_date.year - start_date.year) * 12 \
+ end_date.month - start_date.month + 1
month = start_date.month
year = start_date.year
for i in pyrange(num_segments + 1):
segment_dates.append(datetime(year, month, 1))
month += 1
if month == 13:
month = 1
year += 1
elif options['time_segment'] == 'year':
num_segments = end_date.year - start_date.year + 1
year = start_date.year
for i in pyrange(num_segments + 1):
segment_dates.append(datetime(year, 1, 1))
year += 1
elif options['time_segment'] == 'decade':
num_segments = (end_date.year - start_date.year) / 10 + 1
year = start_date.year
for i in pyrange(num_segments + 1):
segment_dates.append(datetime(year, 1, 1))
year += 10
elif options['time_segment'] == 'all':
num_segments = 1
segment_dates = [start_date, end_date]
else:
raise ValueError('Unknown timesegment options \
{0}'.format(options['time_segment']))
print("Number of files: {0}".format(len(segment_dates) - 1))
assert len(segment_dates) == num_segments + 1
out_dict[var][ymask, xmask] = fill_val
elif mydict[var].ndim == 2:
steps = mydict[var].shape[1]
out_dict[var] = np.ma.zeros((steps, ysize, xsize),
dtype=dtype)
for i in pyrange(steps):
out_dict[var][i, yi, xi] = mydict[var][:, i]
out_dict[var][:, ymask, xmask] = fill_val
elif mydict[var].ndim == 3:
j = mydict[var].shape[1]
k = mydict[var].shape[2]
out_dict[var] = np.ma.zeros((j, k, ysize, xsize),
dtype=dtype)
for jj in pyrange(j):
for kk in pyrange(k):
out_dict[var][jj, kk, yi, xi] = mydict[var][:, jj, kk]
for y, x in pyzip(ymask, xmask):
out_dict[var][:, :, y, x] = fill_val
out_dict[var] = np.ma.masked_values(out_dict[var], fill_val)
out_dicts[name] = out_dict
# Merge information from veglib and veg and transfer to veg_dict
if veglib_dict and veg_dict:
# Check for any missing Cv areas that will become bare soil
var = 'Cv'
bare = 1 - out_dicts['veg_dict'][var].sum(axis=0)
bare[bare < 0.0] = 0.0
elif mydict[var].ndim == 2:
steps = mydict[var].shape[1]
out_dict[var] = np.ma.zeros((steps, ysize, xsize),
dtype=dtype)
for i in pyrange(steps):
out_dict[var][i, yi, xi] = mydict[var][:, i]
out_dict[var][:, ymask, xmask] = fill_val
elif mydict[var].ndim == 3:
j = mydict[var].shape[1]
k = mydict[var].shape[2]
out_dict[var] = np.ma.zeros((j, k, ysize, xsize),
dtype=dtype)
for jj in pyrange(j):
for kk in pyrange(k):
out_dict[var][jj, kk, yi, xi] = mydict[var][:, jj, kk]
for y, x in pyzip(ymask, xmask):
out_dict[var][:, :, y, x] = fill_val
out_dict[var] = np.ma.masked_values(out_dict[var], fill_val)
out_dicts[name] = out_dict
# Merge information from veglib and veg and transfer to veg_dict
if veglib_dict and veg_dict:
# Check for any missing Cv areas that will become bare soil
var = 'Cv'
bare = 1 - out_dicts['veg_dict'][var].sum(axis=0)
bare[bare < 0.0] = 0.0
def nc_add_data_standard(self, points):
ys = points.get_ys()
xs = points.get_xs()
for p in points:
for name in self.three_dim_vars:
data = points.get_data(name, self.slice)
self.f.variables[name][:, ys, xs] = data
for name in self.four_dim_vars:
varshape = self.f.variables[name].shape[1]
for i in pyrange(varshape):
sn = name + str(i)
self.f.variables[name][:, i, ys,
xs] = p.df[sn].values[self.slice]
def nc_add_data_to_array(self, point):
for name in self.three_dim_vars:
self.data[name][:, point.y, point.x] = \
point.df[name].values[self.slice]
for name in self.four_dim_vars:
varshape = self.f.variables[name].shape[1]
for i in pyrange(varshape):
subname = name + str(i)
self.data[name][:, i, point.y,
point.x] = point.df[subname].values[self.slice]