@@ -128,6 +128,20 @@ def reorder_stations(dataset, index_mapping):
128128 new_station_index = len (all_data .number_of_stations )
129129 # Expand all of the previous data to contain the new station. Fill with nan values for all previous times.
130130 all_data = all_data .reindex ({'number_of_stations' : np .arange (new_station_index + 1 )}, fill_value = np .nan )
131+ for var in all_data .data_vars :
132+ if 'number_of_stations' in all_data [var ].dims :
133+ if var in ['station_event_fraction' , 'station_power_ratio' , 'station_active' , 'event_contributing_stations' ]:
134+ val_to_fill = 0
135+ else :
136+ val_to_fill = new_station [var ].data .item ()
137+ if all_data [var ].data .dtype != 'object' :
138+ filled_data = np .nan_to_num (all_data [var ].data , copy = True , nan = val_to_fill )
139+ else :
140+ filled_data = all_data [var ].data .copy ().astype (new_station [var ].data .dtype )
141+ for i in np .ndindex (filled_data .shape ):
142+ if filled_data [i ] == np .array ([np .nan ]).astype (new_station [var ].data .dtype )[0 ]:
143+ filled_data [i ] = val_to_fill
144+ all_data [var ].data = filled_data
131145 # Update the dimension coordinate to reflect the new station's addition
132146 all_data ['number_of_stations' ] = ('number_of_stations' , np .arange (new_station_index + 1 ))
133147 # Add the new station's information to the global list of known stations
@@ -146,6 +160,18 @@ def reorder_stations(dataset, index_mapping):
146160 temp_station_id = len (new_file .number_of_stations )
147161 # fill the new row with nan values
148162 new_file = new_file .reindex ({'number_of_stations' : np .arange (temp_station_id + 1 )}, fill_value = np .nan )
163+ for var in new_file .data_vars :
164+ if var == 'number_of_stations' :
165+ continue
166+ if 'number_of_stations' in new_file [var ].dims :
167+ if var in ['station_event_fraction' , 'station_power_ratio' , 'station_active' , 'event_contributing_stations' ]:
168+ val_to_fill = 0
169+ filled_data = np .nan_to_num (new_file [var ].data , copy = True , nan = val_to_fill )
170+ new_file [var ].data = filled_data
171+ else :
172+ new_file [var ].data [temp_station_id ] = all_data [var ].isel (number_of_stations = old_station_id , number_of_files = - 1 ).data .item ()
173+ val_to_fill = all_data [var ].isel (number_of_stations = old_station_id ).data
174+ val_to_fill = val_to_fill [0 ]
149175 new_file ['number_of_stations' ] = ('number_of_stations' , np .arange (temp_station_id + 1 ))
150176 # add the dead station to the list of stations from this file so that it is the same length as the all_data dataset
151177 stations_in_file .append (temp_station_id )
0 commit comments