NCAR / watershed_tools

Methods for creating watershed discretizations for use in hydrological modeling or analysis. Examples use the SUMMA modeling Framework.
GNU General Public License v3.0
11 stars 5 forks source link

Issue during small HRU elimination (geospatial_analysis.py) #10

Open DaveCasson opened 2 years ago

DaveCasson commented 2 years ago

When moving from default "median" delineations, to defining a number of bins. The error below is produced during the elimination of small hrus. It appears that the boolean array is passed to the pandas.at It expects a scalar value, or index value.

eliminating .... 0%| | 0/21 [00:00<?, ?it/s]Traceback (most recent call last): File "/Users/drc858/GitHub/watershed_tools/discretize/generate_HRUs.py", line 238, in ga.eliminate_small_hrus_neighbor(hru_vector, hru_threshold_type, hru_threshold, gruNo_fieldname, gruId_fieldname, File "../functions/geospatial_analysis.py", line 835, in eliminate_small_hrus_neighbor in_gpd_disv.at[in_gpd_disv[hruName_field]==target_hruName,field] = gru_df.loc[larg_nbhd_idx,field] File "/Users/drc858/PycharmProjects/watershed_tools/lib/python3.8/site-packages/pandas/core/indexing.py", line 2273, in setitem return super().setitem(key, value) File "/Users/drc858/PycharmProjects/watershed_tools/lib/python3.8/site-packages/pandas/core/indexing.py", line 2228, in setitem self.obj._set_value(*key, value=value, takeable=self._takeable) File "/Users/drc858/PycharmProjects/watershed_tools/lib/python3.8/site-packages/pandas/core/frame.py", line 3871, in _set_value loc = self.index.get_loc(index) File "/Users/drc858/PycharmProjects/watershed_tools/lib/python3.8/site-packages/pandas/core/indexes/range.py", line 388, in get_loc self._check_indexing_error(key) File "/Users/drc858/PycharmProjects/watershed_tools/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 5637, in _check_indexing_error raise InvalidIndexError(key) pandas.errors.InvalidIndexError: 0 False 1 False 2 False 3 True 4 False ...
162 False 163 False 164 False 165 False 166 False Name: HRU_ID, Length: 167, dtype: bool 0%| | 0/21 [00:00<?, ?it/s]

DaveCasson commented 2 years ago

While not very elegant, this is a fix that returns the correct index value:

for field in fieldname_list: index_array = in_gpd_disv[in_gpd_disv[hruName_field]==target_hruName].index.values in_gpd_disv.at[index_array[0],field] = gru_df.loc[larg_nbhd_idx,field] index_array = in_gpd_disv[in_gpd_disv[hruName_field] == target_hruName].index.values in_gpd_disv.at[index_array[0],hruName_field] = gru_df.loc[larg_nbhd_idx,hruName_field]

andywood commented 2 years ago

We'll need to work this in, since we have only so far supported a median delineation for continuous factors (elevation, radiation). Presumably this worked in that case. Next step would be a PR with a case that allows for splitting the basin further (including new control file entries).