Пример #1
0
 def __getitem__(self, indices):
     if isinstance(indices, int):  # single asset
         a = self.array[indices]
         values = {lt: a[lt] for lt in self.loss_types}
         if 'occupants' in self.array.dtype.names:
             values['occupants_' + str(self.time_event)] = a['occupants']
         return riskmodels.Asset(
             a['idx'],
             self.taxonomies[a['taxonomy']],
             number=a['number'],
             location=(a['lon'], a['lat']),
             values=values,
             area=a['area'],
             deductibles={lt[self.D:]: a[lt]
                          for lt in self.deduc},
             insurance_limits={lt[self.I:]: a[lt]
                               for lt in self.i_lim},
             retrofitteds={lt[self.R:]: a[lt]
                           for lt in self.retro},
             calc=self.cc,
             ordinal=indices)
     new = object.__new__(self.__class__)
     new.time_event = self.time_event
     new.array = self.array[indices]
     new.taxonomies = self.taxonomies
     return new
Пример #2
0
 def __getitem__(self, aid):
     a = self.array[aid]
     values = {
         lt: a['value-' + lt]
         for lt in self.loss_types if lt != 'occupants'
     }
     if 'occupants' in self.array.dtype.names:
         values['occupants_' + str(self.time_event)] = a['occupants']
     return riskmodels.Asset(
         a['idx'],
         self.taxonomies[aid],
         number=a['number'],
         location=(
             valid.longitude(a['lon']),  # round coordinates
             valid.latitude(a['lat'])),
         values=values,
         area=a['area'],
         deductibles={lt[self.D:]: a[lt]
                      for lt in self.deduc},
         insurance_limits={lt[self.I:]: a[lt]
                           for lt in self.i_lim},
         retrofitteds={lt[self.R:]: a[lt]
                       for lt in self.retro},
         calc=self.cc,
         ordinal=aid)
Пример #3
0
def asset(ref,
          value,
          deductibles=None,
          insurance_limits=None,
          retrofitteds=None):
    return riskmodels.Asset(ref, 'taxonomy', 1, (0, 0), dict(structural=value),
                            1, deductibles, insurance_limits, retrofitteds)
Пример #4
0
def get_exposure(oqparam):
    """
    Read the full exposure in memory and build a list of
    :class:`openquake.risklib.riskmodels.Asset` instances.
    If you don't want to keep everything in memory, use
    get_exposure_lazy instead (for experts only).

    :param oqparam:
        an :class:`openquake.commonlib.oqvalidation.OqParam` instance
    :returns:
        an :class:`Exposure` instance
    """
    out_of_region = 0
    if oqparam.region_constraint:
        region = wkt.loads(oqparam.region_constraint)
    else:
        region = None
    all_cost_types = set(oqparam.all_cost_types)
    fname = oqparam.inputs['exposure']
    exposure, assets_node, cc = get_exposure_lazy(fname, all_cost_types)
    relevant_cost_types = all_cost_types - set(['occupants'])
    asset_refs = set()
    ignore_missing_costs = set(oqparam.ignore_missing_costs)

    for idx, asset in enumerate(assets_node):
        values = {}
        deductibles = {}
        insurance_limits = {}
        retrofitteds = {}
        with context(fname, asset):
            asset_id = asset['id'].encode('utf8')
            if asset_id in asset_refs:
                raise DuplicatedID(asset_id)
            asset_refs.add(asset_id)
            exposure.asset_refs.append(asset_id)
            taxonomy = asset['taxonomy']
            if 'damage' in oqparam.calculation_mode:
                # calculators of 'damage' kind require the 'number'
                # if it is missing a KeyError is raised
                number = asset.attrib['number']
            else:
                # some calculators ignore the 'number' attribute;
                # if it is missing it is considered 1, since we are going
                # to multiply by it
                try:
                    number = asset['number']
                except KeyError:
                    number = 1
                else:
                    if 'occupants' in all_cost_types:
                        values['occupants_None'] = number
            location = asset.location['lon'], asset.location['lat']
            if region and not geometry.Point(*location).within(region):
                out_of_region += 1
                continue
        try:
            costs = asset.costs
        except NameError:
            costs = LiteralNode('costs', [])
        try:
            occupancies = asset.occupancies
        except NameError:
            occupancies = LiteralNode('occupancies', [])
        for cost in costs:
            with context(fname, cost):
                cost_type = cost['type']
                if cost_type in relevant_cost_types:
                    values[cost_type] = cost['value']
                    retrovalue = cost.attrib.get('retrofitted')
                    if retrovalue is not None:
                        retrofitteds[cost_type] = retrovalue
                    if oqparam.insured_losses:
                        deductibles[cost_type] = cost['deductible']
                        insurance_limits[cost_type] = cost['insuranceLimit']

        # check we are not missing a cost type
        missing = relevant_cost_types - set(values)
        if missing and missing <= ignore_missing_costs:
            logging.warn('Ignoring asset %s, missing cost type(s): %s',
                         asset_id, ', '.join(missing))
            for cost_type in missing:
                values[cost_type] = None
        elif missing and 'damage' not in oqparam.calculation_mode:
            # missing the costs is okay for damage calculators
            with context(fname, asset):
                raise ValueError("Invalid Exposure. "
                                 "Missing cost %s for asset %s" %
                                 (missing, asset_id))
        tot_occupants = 0
        for occupancy in occupancies:
            with context(fname, occupancy):
                exposure.time_events.add(occupancy['period'])
                occupants = 'occupants_%s' % occupancy['period']
                values[occupants] = occupancy['occupants']
                tot_occupants += values[occupants]
        if occupancies:  # store average occupants
            values['occupants_None'] = tot_occupants / len(occupancies)
        area = float(asset.attrib.get('area', 1))
        ass = riskmodels.Asset(idx, taxonomy, number, location, values, area,
                               deductibles, insurance_limits, retrofitteds, cc)
        exposure.assets.append(ass)
        exposure.taxonomies.add(taxonomy)
    if region:
        logging.info(
            'Read %d assets within the region_constraint '
            'and discarded %d assets outside the region', len(exposure.assets),
            out_of_region)
        if len(exposure.assets) == 0:
            raise RuntimeError('Could not find any asset within the region!')
    else:
        logging.info('Read %d assets', len(exposure.assets))

    # sanity check
    values = any(len(ass.values) + ass.number for ass in exposure.assets)
    assert values, 'Could not find any value??'
    return exposure
Пример #5
0
def asset(values, deductibles=None, insurance_limits=None, retrofitteds=None):
    return riskmodels.Asset('a1', 'taxonomy', 1, (0, 0), values, 1,
                            deductibles, insurance_limits, retrofitteds)