Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if layout is not None:
if isinstance(layout, xr.DataArray):
layout = layout.reindex_like(cutout.meta).stack(spatial=('y', 'x')).values
else:
assert layout.shape == cutout.shape
matrix = (layout.reshape((1,-1))
if matrix is None
else sp.sparse.csr_matrix(matrix).dot(spdiag(layout.ravel())))
if matrix is not None:
matrix = sp.sparse.csr_matrix(matrix)
if index is None:
index = pd.RangeIndex(matrix.shape[0])
aggregate_func = aggregate_matrix
aggregate_kwds = dict(matrix=matrix, index=index)
else:
aggregate_func = aggregate_sum
aggregate_kwds = {}
results = []
yearmonths = cutout.coords['year-month'].to_index()
if isinstance(show_progress, string_types):
prefix = show_progress
else:
func_name = (convert_func.__name__[len('convert_'):]
if convert_func.__name__.startswith('convert_')
else convert_func.__name__)
prefix = 'Convert and aggregate `{}`: '.format(func_name)
if 'view' in cutout.meta.attrs:
ds = ds.sel(**cutout.meta.attrs['view'])
da = convert_func(ds, **convert_kwds)
results.append(aggregate_func(da, **aggregate_kwds).load())
if 'time' in results[0].coords:
results = xr.concat(results, dim='time')
else:
results = sum(results)
if capacity_factor:
assert aggregate_func is aggregate_sum, \
"The arguments `matrix`, `shapes` and `layout` are incompatible with capacity_factor"
results /= len(cutout.meta['time'])
if per_unit or return_capacity:
assert aggregate_func is aggregate_matrix, \
"One of `matrix`, `shapes` and `layout` must be given for `per_unit`"
capacity = xr.DataArray(np.asarray(matrix.sum(axis=1)).reshape(-1), [index])
if per_unit:
results = (results / capacity).fillna(0.)
if return_capacity:
return results, capacity
else:
return results