Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"but received `type(hydrobasins) = {}`".format(type(hydrobasins))
)
missing_columns = (pd.Index(['HYBAS_ID', 'DIST_MAIN', 'NEXT_DOWN', 'geometry'])
.difference(hydrobasins.columns))
assert missing_columns.empty, (
"Couldn't find the column(s) {} in the hydrobasins dataset."
.format(", ".join(missing_columns))
)
hydrobasins = hydrobasins.set_index("HYBAS_ID")
meta = hydrobasins[hydrobasins.columns.difference(('geometry',))]
shapes = hydrobasins['geometry']
maybe_progressbar = make_optional_progressbar(
show_progress, "Determine upstream basins per plant", len(plants)
)
plant_basins = []
for p in maybe_progressbar(plants.itertuples()):
hid = find_basin(shapes, p.lon, p.lat)
plant_basins.append((hid, find_upstream_basins(meta, hid)))
plant_basins = pd.DataFrame(plant_basins, columns=['hid', 'upstream'], index=plants.index)
unique_basins = pd.Index(plant_basins['upstream'].sum()).unique().rename("hid")
return Basins(plant_basins, meta.loc[unique_basins], shapes.loc[unique_basins])
def shift_and_aggregate_runoff_for_plants(basins, runoff, flowspeed=1, show_progress=True):
inflow = xr.DataArray(np.zeros((len(basins.plants), runoff.indexes["time"].size)),
[('plant', basins.plants.index),
('time' , runoff.coords["time"])])
maybe_progressbar = make_optional_progressbar(
show_progress, "Shift and aggregate runoff by plant", len(basins.plants)
)
for ppl in maybe_progressbar(basins.plants.itertuples()):
inflow_plant = inflow.loc[dict(plant=ppl.Index)]
distances = (basins.meta.loc[ppl.upstream, "DIST_MAIN"] -
basins.meta.at[ppl.hid, "DIST_MAIN"])
nhours = (distances * (flowspeed * 3.6) + 0.5).astype(int)
for b in ppl.upstream:
inflow_plant += runoff.sel(hid=b).shift(time=nhours.at[b])
return inflow
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)
maybe_progressbar = make_optional_progressbar(show_progress, prefix, len(yearmonths))
for ym in maybe_progressbar(yearmonths):
with xr.open_dataset(cutout.datasetfn(ym)) as ds:
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'])