docs: 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.warning('There are no ruptures in the datastore')
          return []
      name, ext = export.keyfunc(ekey)
      agg_losses = dstore['losses_by_event']
      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)]
                + 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)
      events = dstore['events'].value
      events_by_rupid = collections.defaultdict(list)
      for event in events:
          rupid = event['eid'] // TWO32
          events_by_rupid[rupid].append(event)
      year_of = year_dict(events['eid'], oq.investigation_time, oq.ses_seed)
      rup_data = {}
      event_by_eid = {}  # eid -> event
      # populate rup_data and event_by_eid
      # TODO: avoid reading the events twice
      for rgetter in getters.gen_rupture_getters(dstore):
          ruptures = rgetter.get_ruptures()
          for ebr in ruptures:
              for event in events_by_rupid[ebr.serial]:
                  event_by_eid[event['eid']] = event
          if has_rup_data:
              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'] = eid = event['eid']
          rec['year'] = year_of[eid]
          if rup_data:
              rec['rup_id'] = rup_id = event['eid'] // TWO32
              (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'])
      dest = dstore.build_fname('elt', '', 'csv')
      writer.save(elt, dest)
      return writer.getsaved()