@@ -183,27 +183,30 @@ def from_footprints(cls, path, description=None, ref_raster=None, centroids=None
183183
184184 LOGGER .info ('Commencing to iterate over netCDF files.' )
185185
186- haz = cls ()
187- for file_name in file_names :
188- if any (fo in file_name for fo in files_omit ):
189- LOGGER .info ("Omitting file %s" , file_name )
190- continue
191- new_haz = cls ._read_one_nc (file_name , centroids , intensity_thres )
192- if new_haz is not None :
193- haz .append (new_haz )
194-
186+ files_to_read = [file for file in file_names if file not in files_omit ]
187+ LOGGER .info (
188+ "Omitting files %s" , [
189+ file for file in file_names if file in files_omit ])
190+ hazard_list = [
191+ cls ._read_one_nc (
192+ file_name ,
193+ centroids ,
194+ intensity_thres ) for file_name in files_to_read ]
195+ haz = cls .concat ([haz for haz in hazard_list if haz is not None ])
196+
197+ # Fix values after concatenation
195198 haz .event_id = np .arange (1 , len (haz .event_id ) + 1 )
196199 haz .frequency = np .divide (
197200 np .ones_like (haz .date ),
198201 np .max ([(last_year (haz .date ) - first_year (haz .date )), 1 ])
199202 )
200203
204+ if description is None :
205+ description = "WISC historical hazard set."
201206 haz .tag = TagHazard (
202207 HAZ_TYPE , 'Hazard set not saved by default' ,
203- description = 'WISC historical hazard set.'
208+ description = description ,
204209 )
205- if description is not None :
206- haz .tag .description = description
207210
208211 if combine_threshold is not None :
209212 LOGGER .info ('Combining events with small difference in date.' )
@@ -214,8 +217,8 @@ def from_footprints(cls, path, description=None, ref_raster=None, centroids=None
214217 haz ._combine_events (event_ids )
215218 return haz
216219
217- @staticmethod
218- def _read_one_nc (file_name , centroids , intensity_thres ):
220+ @classmethod
221+ def _read_one_nc (cls , file_name , centroids , intensity_thres ):
219222 """Read a single WISC footprint. Assumes a time dimension of length 1.
220223 Omits a footprint if another file with the same timestamp has already
221224 been read.
@@ -253,17 +256,16 @@ def _read_one_nc(file_name, centroids, intensity_thres):
253256
254257 # fill in values from netCDF
255258 ssi_wisc = np .array ([float (ncdf .ssi )])
256- new_haz = StormEurope (ssi_wisc = ssi_wisc )
257- new_haz .event_name = [ncdf .storm_name ]
258- new_haz .date = np .array ([datetime64_to_ordinal (ncdf .time .data [0 ])])
259- new_haz .intensity = sparse .csr_matrix (stacked )
260-
261- # fill in default values
262- new_haz .centroids = centroids
263- new_haz .event_id = np .array ([1 ])
264- new_haz .frequency = np .array ([1 ])
265- new_haz .fraction = sparse .csr_matrix (new_haz .intensity .shape )
266- new_haz .orig = np .array ([True ])
259+ intensity = sparse .csr_matrix (stacked )
260+ new_haz = cls (ssi_wisc = ssi_wisc ,
261+ intensity = intensity ,
262+ event_name = [ncdf .storm_name ],
263+ date = np .array ([datetime64_to_ordinal (ncdf .time .data [0 ])]),
264+ # fill in default values
265+ centroids = centroids ,
266+ event_id = np .array ([1 ]),
267+ frequency = np .array ([1 ]),
268+ orig = np .array ([True ]),)
267269
268270 ncdf .close ()
269271 return new_haz
@@ -316,10 +318,6 @@ def from_cosmoe_file(cls, fp_file, run_datetime, event_date=None,
316318 """
317319 intensity_thres = cls .intensity_thres if intensity_thres is None else intensity_thres
318320
319- haz = cls ()
320- # create centroids
321- haz .centroids = cls ._centroids_from_nc (fp_file )
322-
323321 # read intensity from file
324322 ncdf = xr .open_dataset (fp_file )
325323 ncdf = ncdf .assign_coords (date = ('time' ,ncdf ["time" ].dt .floor ("D" ).values ))
@@ -350,35 +348,38 @@ def from_cosmoe_file(cls, fp_file, run_datetime, event_date=None,
350348 stacked = stacked .fillna (0 )
351349
352350 # fill in values from netCDF
353- haz .intensity = sparse .csr_matrix (stacked .VMAX_10M .T )
354- haz .event_id = np .arange (stacked .date_ensemble .size ) + 1
355-
356- # fill in default values
357- haz .units = 'm/s'
358- haz .fraction = haz .intensity .copy ().tocsr ()
359- haz .fraction .data .fill (1 )
360- haz .orig = np .ones_like (haz .event_id )* False
361- haz .orig [(stacked .epsd_1 == 0 ).values ] = True
362- haz .date = np .repeat (
351+ intensity = sparse .csr_matrix (stacked .VMAX_10M .T )
352+ event_id = np .arange (stacked .date_ensemble .size ) + 1
353+ date = np .repeat (
363354 np .array (datetime64_to_ordinal (considered_dates )),
364355 np .unique (ncdf .epsd_1 ).size
365356 )
366- haz .event_name = [date_i + '_ens' + str (ens_i )
367- for date_i , ens_i
368- in zip (date_to_str (haz .date ), stacked .epsd_1 .values + 1 )]
369- haz .frequency = np .divide (
370- np .ones_like (haz .event_id ),
371- np .unique (ncdf .epsd_1 ).size )
372- if not description :
357+ orig = np .full_like (event_id , False )
358+ orig [(stacked .epsd_1 == 0 ).values ] = True
359+ if description is None :
373360 description = (model_name +
374361 ' weather forecast windfield ' +
375362 'for run startet at ' +
376363 run_datetime .strftime ('%Y%m%d%H' ))
377364
378- haz .tag = TagHazard (
379- HAZ_TYPE , 'Hazard set not saved, too large to pickle' ,
380- description = description
365+ # Create Hazard
366+ haz = cls (
367+ intensity = intensity ,
368+ event_id = event_id ,
369+ centroids = cls ._centroids_from_nc (fp_file ),
370+ # fill in default values
371+ orig = orig ,
372+ date = date ,
373+ event_name = [date_i + '_ens' + str (ens_i )
374+ for date_i , ens_i
375+ in zip (date_to_str (date ), stacked .epsd_1 .values + 1 )],
376+ frequency = np .divide (
377+ np .ones_like (event_id ),
378+ np .unique (ncdf .epsd_1 ).size ),
379+ description = description ,
380+ file_name = "Hazard set not saved, too large to pickle" ,
381381 )
382+
382383 # close netcdf file
383384 ncdf .close ()
384385 haz .check ()
@@ -440,7 +441,6 @@ def from_icon_grib(cls, run_datetime, event_date=None, model_name='icon-eu-eps',
440441 # pylint: disable=protected-access
441442 intensity_thres = cls .intensity_thres if intensity_thres is None else intensity_thres
442443
443- haz = cls ()
444444 if not (run_datetime .hour == 0 or run_datetime .hour == 12 ):
445445 LOGGER .warning ('The event definition is inaccuratly implemented '
446446 'for starting times, which are not 00H or 12H.' )
@@ -450,7 +450,6 @@ def from_icon_grib(cls, run_datetime, event_date=None, model_name='icon-eu-eps',
450450
451451 # create centroids
452452 nc_centroids_file = download_icon_centroids_file (model_name , grib_dir )
453- haz .centroids = haz ._centroids_from_nc (nc_centroids_file )
454453
455454 # read intensity from files
456455 for ind_i , file_i in enumerate (file_names ):
@@ -490,36 +489,32 @@ def from_icon_grib(cls, run_datetime, event_date=None, model_name='icon-eu-eps',
490489 stacked = stacked .where (stacked > intensity_thres )
491490 stacked = stacked .fillna (0 )
492491
493-
494- # fill in values from netCDF
495- haz .intensity = sparse .csr_matrix (stacked .gust .T )
496- haz .event_id = np .arange (stacked .date_ensemble .size )+ 1
497-
498- # fill in default values
499- haz .units = 'm/s'
500- haz .fraction = haz .intensity .copy ().tocsr ()
501- haz .fraction .data .fill (1 )
502- haz .orig = np .ones_like (haz .event_id )* False
503- haz .orig [(stacked .number == 1 ).values ] = True
504-
505- haz .date = np .repeat (
492+ event_id = np .arange (stacked .date_ensemble .size ) + 1
493+ date = np .repeat (
506494 np .array (datetime64_to_ordinal (considered_dates )),
507495 np .unique (stacked .number ).size
508- )
509- haz .event_name = [date_i + '_ens' + str (ens_i )
510- for date_i , ens_i in zip (date_to_str (haz .date ), stacked .number .values )]
511- haz .frequency = np .divide (
512- np .ones_like (haz .event_id ),
513- np .unique (stacked .number ).size )
514- if not description :
515- description = ('icon weather forecast windfield ' +
516- 'for run startet at ' +
496+ )
497+ orig = np .full_like (event_id , False )
498+ orig [(stacked .number == 1 ).values ] = True
499+ if description is None :
500+ description = ('icon weather forecast windfield for run started at ' +
517501 run_datetime .strftime ('%Y%m%d%H' ))
518502
519- haz .tag = TagHazard (
520- HAZ_TYPE , 'Hazard set not saved, too large to pickle' ,
521- description = description
522- )
503+ # Create Hazard
504+ haz = cls (
505+ intensity = sparse .csr_matrix (stacked .gust .T ),
506+ centroids = cls ._centroids_from_nc (nc_centroids_file ),
507+ event_id = event_id ,
508+ date = date ,
509+ orig = orig ,
510+ event_name = [date_i + '_ens' + str (ens_i )
511+ for date_i , ens_i
512+ in zip (date_to_str (date ), stacked .number .values )],
513+ frequency = np .divide (
514+ np .ones_like (event_id ),
515+ np .unique (stacked .number ).size ),
516+ description = description ,
517+ file_name = "Hazard set not saved, too large to pickle" )
523518 haz .check ()
524519
525520 # delete generated .grib2 and .4cc40.idx files
@@ -831,33 +826,28 @@ def generate_prob_storms(self, reg_id=528, spatial_shift=4, ssi_args=None,
831826 ** kwargs )
832827
833828 LOGGER .info ('Generating new StormEurope instance' )
834- new_haz = StormEurope (ssi_full_area = ssi )
835- new_haz .intensity = sparse .csr_matrix (intensity_prob )
836-
837- # don't use synthetic dates; just repeat the historic dates
838- new_haz .date = np .repeat (self .date , N_PROB_EVENTS )
839-
840- # subsetting centroids
841- new_haz .centroids = self .centroids .select (sel_cen = sel_cen )
842-
843- # construct new event ids
844829 base = np .repeat ((self .event_id * 100 ), N_PROB_EVENTS )
845830 synth_id = np .tile (np .arange (N_PROB_EVENTS ), self .size )
846- new_haz .event_id = base + synth_id
847-
848- # frequency still based on the historic number of years
849- new_haz .frequency = np .divide (np .repeat (self .frequency , N_PROB_EVENTS ),
850- N_PROB_EVENTS )
851-
852- new_haz .tag = TagHazard (
853- HAZ_TYPE , 'Hazard set not saved by default' ,
854- description = 'WISC probabilistic hazard set according to Schwierz et al.'
831+ event_id = base + synth_id
832+ new_haz = StormEurope (
833+ ssi_full_area = ssi ,
834+ intensity = sparse .csr_matrix (intensity_prob ),
835+ # don't use synthetic dates; just repeat the
836+ # historic dates
837+ date = np .repeat (self .date , N_PROB_EVENTS ),
838+ # subsetting centroids
839+ centroids = self .centroids .select (sel_cen = sel_cen ),
840+ # construct new event ids
841+
842+ event_id = event_id ,
843+ # frequency still based on the historic number of
844+ # years
845+ frequency = np .divide (np .repeat (self .frequency , N_PROB_EVENTS ),
846+ N_PROB_EVENTS ),
847+ file_name = 'Hazard set not saved by default' ,
848+ description = 'WISC probabilistic hazard set according to Schwierz et al.' ,
849+ orig = (event_id % 100 == 0 ),
855850 )
856-
857- new_haz .fraction = new_haz .intensity .copy ().tocsr ()
858- new_haz .fraction .data .fill (1 )
859- new_haz .orig = (new_haz .event_id % 100 == 0 )
860-
861851 new_haz .check ()
862852
863853 return new_haz
0 commit comments