@@ -206,7 +206,7 @@ def downscale(self, distribution, inputs, region_masks):
206206
207207 return out
208208
209- def disaggregate_source (self , downscaled ):
209+ def disaggregate_source (self ):
210210 """Disaggregate water demand by source (runoff, groundwater, desal)"""
211211
212212 print ('Disaggregating Source' )
@@ -221,11 +221,7 @@ def disaggregate_source(self, downscaled):
221221 # apply shares to the gridded region masks
222222 gridded_shares = get_shares .generate_gridded_shares (shares_df )
223223
224- # disaggregate water supply source for each downscaled demand in each grid cell
225- downscaled_disaggregated_sw = (downscaled * gridded_shares ['runoff' ]).compute ()
226- downscaled_disaggregated_gw = (downscaled * gridded_shares ['groundwater' ]).compute ()
227-
228- return gridded_shares , downscaled_disaggregated_sw , downscaled_disaggregated_gw
224+ return gridded_shares
229225
230226
231227 def run_model (self ):
@@ -238,6 +234,17 @@ def run_model(self):
238234 self ._load_region_masks ()
239235 self ._load_inputs ()
240236
237+ # disaggregate water supply source
238+ if self .source_disaggregation :
239+ gshares = self .disaggregate_source ()
240+
241+ # store the disaggregated results
242+ self .griddedshares = gshares
243+
244+ if self .output_dir is not None :
245+ filename = os .path .join (self .output_dir , f'gridded_runoff_shares.nc' )
246+ gshares ['runoff' ].to_netcdf (filename )
247+
241248 for supersector , rules in self .downscaling_rules .items ():
242249
243250 print (f'Downscaling { supersector } ' )
@@ -284,16 +291,6 @@ def run_model(self):
284291 downscaled .to_dataset (dim = 'sector' ).to_netcdf (filename , encoding = encoding )
285292 downscaled = xr .open_dataset (filename ).to_array (dim = 'sector' ) # hopefully this keeps dask happy
286293
287- # disaggregate water supply source and update the objects
288- if self .source_disaggregation :
289- gshares , sw , gw = self .disaggregate_source (downscaled )
290-
291- # store the disaggregated results
292- self .griddedshares = gshares
293- self .disaggregated_sw = sw
294- self .disaggregated_gw = gw
295-
296-
297294 if self .temporal_config is not None :
298295 # calculate the monthly distributions (share of annual) for each year
299296
@@ -315,7 +312,7 @@ def run_model(self):
315312 if self .output_dir is not None :
316313 filename = os .path .join (self .output_dir , f'{ supersector } _{ self .demand_type } _monthly.nc' )
317314 encoding = {sector : {'zlib' : True , 'complevel' : 5 } for sector in downscaled .sector .data }
318- downscaled .to_dataset (dim = 'sector' ).to_netcdf (filename , encoding = encoding )
315+ downscaled .to_dataset (dim = 'sector' ).compute (). to_netcdf (filename , encoding = encoding )
319316 downscaled = xr .open_dataset (filename ).to_array (dim = 'sector' ) # hopefully this keeps dask happy
320317
321318 self .outputs .update (downscaled .to_dataset (dim = 'sector' ))
0 commit comments