def export_ruptures_csv(ekey, dstore): """ :param ekey: export key, i.e. a pair (datastore key, fmt) :param dstore: datastore object """ oq = dstore['oqparam'] if 'scenario' in oq.calculation_mode: return [] dest = dstore.export_path('ruptures.csv') header = ('rupid multiplicity mag centroid_lon centroid_lat centroid_depth' ' trt strike dip rake boundary').split() csm_info = dstore['csm_info'] grp_trt = csm_info.grp_trt() rows = [] ruptures_by_grp = calc.get_ruptures_by_grp(dstore) for grp_id, trt in sorted(grp_trt.items()): rup_data = calc.RuptureData(trt, csm_info.get_gsims(grp_id)) for r in rup_data.to_array(ruptures_by_grp.get(grp_id, [])): rows.append((r['rup_id'], r['multiplicity'], r['mag'], r['lon'], r['lat'], r['depth'], trt, r['strike'], r['dip'], r['rake'], r['boundary'])) rows.sort() # by rupture serial writers.write_csv(dest, rows, header=header, sep='\t', comment='investigation_time=%s' % oq.investigation_time) return [dest]
def export_agg_losses_ebr(ekey, dstore): """ :param ekey: export key, i.e. a pair (datastore key, fmt) :param dstore: datastore object """ if 'ruptures' not in dstore: logging.warn('There are no ruptures in the datastore') return [] name, ext = export.keyfunc(ekey) agg_losses = dstore[name] has_rup_data = 'ruptures' in dstore extra_list = [('magnitude', F32), ('centroid_lon', F32), ('centroid_lat', F32), ('centroid_depth', F32)] if has_rup_data else [] oq = dstore['oqparam'] lti = oq.lti dtlist = ([('event_id', U64), ('rup_id', U32), ('year', U32), ('rlzi', U16)] + extra_list + oq.loss_dt_list()) elt_dt = numpy.dtype(dtlist) elt = numpy.zeros(len(agg_losses), elt_dt) writer = writers.CsvWriter(fmt=writers.FIVEDIGITS) the_events = dstore['events'].value all_events = group_array(the_events, 'grp_id') rup_data = {} event_by_eid = {} # eid -> event # populate rup_data and event_by_eid ruptures_by_grp = calc.get_ruptures_by_grp(dstore) # TODO: avoid reading the events twice for grp_id, events in all_events.items(): for event in events: event_by_eid[event['eid']] = event if has_rup_data: ruptures = ruptures_by_grp.get(grp_id, []) rup_data.update(get_rup_data(ruptures)) for r, row in enumerate(agg_losses): rec = elt[r] event = event_by_eid[row['eid']] rec['event_id'] = event['eid'] rec['year'] = event['year'] rec['rlzi'] = row['rlzi'] if rup_data: rec['rup_id'] = rup_id = event['rup_id'] (rec['magnitude'], rec['centroid_lon'], rec['centroid_lat'], rec['centroid_depth']) = rup_data[rup_id] for lt, i in lti.items(): rec[lt] = row['loss'][i] elt.sort(order=['year', 'event_id', 'rlzi']) dest = dstore.build_fname('agg_losses', 'all', 'csv') writer.save(elt, dest) return writer.getsaved()
def export_ruptures_xml(ekey, dstore): """ :param ekey: export key, i.e. a pair (datastore key, fmt) :param dstore: datastore object """ fmt = ekey[-1] oq = dstore['oqparam'] mesh = get_mesh(dstore['sitecol']) ruptures_by_grp = {} for grp_id, ruptures in calc.get_ruptures_by_grp(dstore).items(): ruptures_by_grp[grp_id] = [ebr.export(mesh) for ebr in ruptures] dest = dstore.export_path('ses.' + fmt) writer = hazard_writers.SESXMLWriter(dest) writer.serialize(ruptures_by_grp, oq.investigation_time) return [dest]
def execute(self): """ Run the calculator and aggregate the results """ if self.oqparam.number_of_logic_tree_samples: logging.warn('The event based risk calculator with sampling is ' 'EXPERIMENTAL, UNTESTED and SLOW') if self.oqparam.ground_motion_fields: logging.warn('To store the ground motion fields change ' 'calculation_mode = event_based') if self.oqparam.hazard_curves_from_gmfs: logging.warn('To compute the hazard curves change ' 'calculation_mode = event_based') if 'all_loss_ratios' in self.datastore: EbrPostCalculator(self).run(close=False) return self.csm_info = self.datastore['csm_info'] with self.monitor('reading ruptures', autoflush=True): ruptures_by_grp = (self.precalc.result if self.precalc else calc.get_ruptures_by_grp(self.datastore.parent)) # the ordering of the ruptures is essential for repeatibility for grp in ruptures_by_grp: ruptures_by_grp[grp].sort(key=operator.attrgetter('serial')) num_rlzs = 0 allres = [] source_models = self.csm_info.source_models self.sm_by_grp = self.csm_info.get_sm_by_grp() num_events = sum(ebr.multiplicity for grp in ruptures_by_grp for ebr in ruptures_by_grp[grp]) self.get_eps = riskinput.make_epsilon_getter( len(self.assetcol), num_events, self.oqparam.asset_correlation, self.oqparam.master_seed, self.oqparam.ignore_covs or not self.riskmodel.covs) self.assets_by_site = self.assetcol.assets_by_site() self.start = 0 for i, args in enumerate(self.gen_args(ruptures_by_grp)): ires = self.start_tasks(*args) allres.append(ires) ires.rlz_slice = slice(num_rlzs, num_rlzs + ires.num_rlzs) num_rlzs += ires.num_rlzs for sg in source_models[i].src_groups: sg.eff_ruptures = ires.num_ruptures.get(sg.id, 0) num_events = self.save_results(allres, num_rlzs) return num_events # {sm_id: #events}