@@ -218,6 +218,9 @@ def add_sectoral_demands(context: "Context") -> dict[str, pd.DataFrame]:
218218 df_dmds .sort_values (["year" , "node" , "variable" , "value" ], inplace = True )
219219
220220 df_dmds ["time" ] = "year"
221+
222+ # Filter to only include basins that exist after basin filtering
223+ df_dmds = df_dmds [df_dmds ["node" ].isin (context .valid_basins )]
221224
222225 # Write final interpolated values as csv
223226 # df2_f.to_csv('final_interpolated_values.csv')
@@ -240,6 +243,9 @@ def add_sectoral_demands(context: "Context") -> dict[str, pd.DataFrame]:
240243 )
241244 df_m = df_m [["year" , "pid" , "variable" , "value" , "month" ]]
242245 df_m .columns = pd .Index (["year" , "node" , "variable" , "value" , "time" ])
246+
247+ # Filter monthly data to only include valid basins
248+ df_m = df_m [df_m ["node" ].isin (context .valid_basins )]
243249
244250 # remove yearly parts from df_dms
245251 df_dmds = df_dmds [
@@ -769,13 +775,11 @@ def read_water_availability(context: "Context") -> Sequence[pd.DataFrame]:
769775 "water" , "delineation" , f"basins_by_region_simpl_{ context .regions } .csv"
770776 )
771777 df_x = pd .read_csv (PATH )
778+
779+ # Filter to only include valid basins
780+ df_x = df_x [df_x ["BCU_name" ].isin (context .valid_basins )]
772781
773782 if "year" in context .time :
774- # path for reading basin delineation file
775- PATH = package_data_path (
776- "water" , "delineation" , f"basins_by_region_simpl_{ context .regions } .csv"
777- )
778- df_x = pd .read_csv (PATH )
779783 # Adding freshwater supply constraints
780784 # Reading data, the data is spatially and temprally aggregated from GHMs
781785 path1 = package_data_path (
@@ -786,6 +790,14 @@ def read_water_availability(context: "Context") -> Sequence[pd.DataFrame]:
786790 # Read rcp 2.6 data
787791 df_sw = pd .read_csv (path1 )
788792 df_sw .drop (["Unnamed: 0" ], axis = 1 , inplace = True )
793+
794+ # Filter columns to only include valid basins
795+ # The columns are years, so we need to filter rows based on the original basin order
796+ # First, get the indices of valid basins from the original full list
797+ full_basin_df = pd .read_csv (PATH ) # Read full basin list again
798+ valid_indices = full_basin_df [full_basin_df ["BCU_name" ].isin (context .valid_basins )].index
799+ df_sw = df_sw .iloc [valid_indices ] # Keep only rows for valid basins
800+ df_sw .reset_index (drop = True , inplace = True )
789801
790802 df_sw .index = df_x ["BCU_name" ].index
791803 df_sw = df_sw .stack ().reset_index ()
@@ -811,6 +823,11 @@ def read_water_availability(context: "Context") -> Sequence[pd.DataFrame]:
811823 # Read groundwater data
812824 df_gw = pd .read_csv (path1 )
813825 df_gw .drop (["Unnamed: 0" ], axis = 1 , inplace = True )
826+
827+ # Filter to only include valid basins (same as df_sw)
828+ df_gw = df_gw .iloc [valid_indices ] # Use same valid_indices from above
829+ df_gw .reset_index (drop = True , inplace = True )
830+
814831 df_gw .index = df_x ["BCU_name" ].index
815832 df_gw = df_gw .stack ().reset_index ()
816833 df_gw .columns = pd .Index (["Region" , "years" , "value" ])
@@ -834,6 +851,12 @@ def read_water_availability(context: "Context") -> Sequence[pd.DataFrame]:
834851 )
835852 df_sw = pd .read_csv (path1 )
836853 df_sw .drop (["Unnamed: 0" ], axis = 1 , inplace = True )
854+
855+ # Filter to only include valid basins
856+ full_basin_df = pd .read_csv (PATH ) # Read full basin list again
857+ valid_indices = full_basin_df [full_basin_df ["BCU_name" ].isin (context .valid_basins )].index
858+ df_sw = df_sw .iloc [valid_indices ]
859+ df_sw .reset_index (drop = True , inplace = True )
837860
838861 df_sw .index = df_x ["BCU_name" ].index
839862 df_sw = df_sw .stack ().reset_index ()
@@ -857,6 +880,10 @@ def read_water_availability(context: "Context") -> Sequence[pd.DataFrame]:
857880 )
858881 df_gw = pd .read_csv (path1 )
859882 df_gw .drop (["Unnamed: 0" ], axis = 1 , inplace = True )
883+
884+ # Filter to only include valid basins (same as df_sw)
885+ df_gw = df_gw .iloc [valid_indices ] # Use same valid_indices from above
886+ df_gw .reset_index (drop = True , inplace = True )
860887
861888 df_gw .index = df_x ["BCU_name" ].index
862889 df_gw = df_gw .stack ().reset_index ()
0 commit comments