Example #1
0
    def collect_source_model_data(self, source_model):
        """
        Parse source model file and collect information about source ids,
        source types and tectonic region types available in it. That
        information is used then for :meth:`validate_filters` and
        :meth:`validate_uncertainty_value`.
        """
        all_source_types = set('{%s}%sSource' % (self.NRML, tagname)
                               for tagname in self.SOURCE_TYPES)
        sourcetype_slice = slice(len('{%s}' % self.NRML), -len('Source'))

        fh = self._get_source_model(source_model)
        eventstream = iterparse(fh)
        while True:
            try:
                _, node = next(eventstream)
            except StopIteration:
                break
            except etree.ParseError as exc:
                raise ParsingError(source_model, str(exc))
            if node.tag not in all_source_types:
                continue
            self.tectonic_region_types.add(node.attrib['tectonicRegion'])
            source_id = node.attrib['id']
            source_type = node.tag[sourcetype_slice]
            self.source_ids.add(source_id)
            self.source_types.add(source_type)
            node.clear()
Example #2
0
def read_lazy(source, lazytags):
    """
    Convert a NRML file into a validated LiteralNode object. The
    tree is lazy, i.e. you access nodes by iterating on them.

    :param source:
        a file name or file object open for reading
    :param lazytags:
       the name of nodes which subnodes must be read lazily
    :returns:
       a list of nodes; some of them will contain lazy subnodes
    """
    nodes = []
    try:
        for _, el in iterparse(source, remove_comments=True):
            tag = striptag(el.tag)
            if tag in nodefactory:  # NRML tag
                nodes.append(
                    node_from_elem(el, nodefactory[tag], lazy=lazytags))
                el.clear()  # save memory
    except:
        etype, exc, tb = sys.exc_info()
        msg = str(exc)
        if str(source) not in msg:
            msg = '%s in %s' % (msg, source)
        raise_(etype, msg, tb)
    return nodes
Example #3
0
def read_lazy(source, lazytags):
    """
    Convert a NRML file into a validated LiteralNode object. The
    tree is lazy, i.e. you access nodes by iterating on them.

    :param source:
        a file name or file object open for reading
    :param lazytags:
       the name of nodes which subnodes must be read lazily
    :returns:
       a list of nodes; some of them will contain lazy subnodes
    """
    nodes = []
    try:
        for _, el in iterparse(source, remove_comments=True):
            tag = striptag(el.tag)
            if tag in nodefactory:  # NRML tag
                nodes.append(node_from_elem(el, nodefactory[tag], lazy=lazytags))
                el.clear()  # save memory
    except:
        etype, exc, tb = sys.exc_info()
        msg = str(exc)
        if str(source) not in msg:
            msg = "%s in %s" % (msg, source)
        raise_(etype, msg, tb)
    return nodes
Example #4
0
    def collect_source_model_data(self, source_model):
        """
        Parse source model file and collect information about source ids,
        source types and tectonic region types available in it. That
        information is used then for :meth:`validate_filters` and
        :meth:`validate_uncertainty_value`.
        """
        all_source_types = set("{%s}%sSource" % (self.NRML, tagname) for tagname in self.SOURCE_TYPES)
        sourcetype_slice = slice(len("{%s}" % self.NRML), -len("Source"))

        fh = self._get_source_model(source_model)
        eventstream = iterparse(fh)
        while True:
            try:
                _, node = next(eventstream)
            except StopIteration:
                break
            except etree.ParseError as exc:
                raise ParsingError(source_model, str(exc))
            if node.tag not in all_source_types:
                continue
            self.tectonic_region_types.add(node.attrib["tectonicRegion"])
            source_id = node.attrib["id"]
            source_type = node.tag[sourcetype_slice]
            self.source_ids.add(source_id)
            self.source_types.add(source_type)
            node.clear()
def LossMapParser(input_file):

    values = []
    meta_info = {}

    for _, element in iterparse(input_file):
        if element.tag == "%slossMap" % xmlNRML:
            meta_info = parse_metadata(element)
        elif element.tag == "%snode" % xmlNRML:
            value = parse_single_loss_node(element)
            values.append(value)
        else:
            continue
    return values
def LossCurveParser(input_file):

    refs = []
    longitude = []
    latitude = []
    losses = []
    poes = []
    meta_info = {}
    for _, element in iterparse(input_file):
        if element.tag == '%slossCurves' % xmlNRML:
            meta_info = parse_metadata(element)
        elif element.tag == '%slossCurve' % xmlNRML:
            lon, lat, ref, poe, loss = parse_single_loss_curve(element)
            longitude.append(lon)
            latitude.append(lat)
            refs.append(ref)
            poes.append(poe)
            losses.append(loss)
        else:
            continue
    longitude = np.array(longitude)
    latitude = np.array(latitude)
    return refs, longitude, latitude, poes, losses
Example #7
0
    def __iter__(self):
        """
        Parse the document iteratively.
        """
        for event, element in iterparse(self._source, events=("start", "end")):

            # exposure metadata
            if event == "start" and element.tag == "%sexposureModel" % NRML:
                desc = element.find("%sdescription" % NRML)
                if desc is not None:
                    desc = desc.text
                else:
                    desc = ""

                self._meta = ExposureMetadata(
                    exposure_id=element.get("id"),
                    description=desc,
                    taxonomy_source=element.get("taxonomySource"),
                    asset_category=str(element.get("category")),
                    conversions=Conversions(
                        cost_types=[],
                        area_type=None,
                        area_unit=None,
                        deductible_is_absolute=True,
                        insurance_limit_is_absolute=True,
                    ),
                )

            # conversions
            if event == "start" and element.tag == "%sarea" % NRML:
                self._meta.conversions.area_type = element.get("type")
                self._meta.conversions.area_unit = element.get("unit")
            elif event == "start" and element.tag == "%sdeductible" % NRML:
                self._meta.conversions.deductible_is_absolute = not (element.get("isAbsolute", "false") == "false")
            elif event == "start" and element.tag == "%sinsuranceLimit" % NRML:
                self._meta.conversions.insurance_limit_is_absolute = not (element.get("isAbsolute", "false") == "false")
            elif event == "start" and element.tag == "%scostType" % NRML:
                self._meta.conversions.cost_types.append(
                    CostType(
                        name=element.get("name"),
                        conversion_type=element.get("type"),
                        unit=element.get("unit"),
                        retrofitted_type=element.get("retrofittedType"),
                        retrofitted_unit=element.get("retrofittedUnit"),
                    )
                )

            # asset data
            elif event == "end" and element.tag == "%sasset" % NRML:
                if element.get("area") is not None:
                    area = float(element.get("area"))
                else:
                    area = None

                if element.get("number") is not None:
                    number = float(element.get("number"))
                else:
                    number = None

                point_elem = element.find("%slocation" % NRML)

                site_data = AssetData(
                    exposure_metadata=self._meta,
                    site=Site(float(point_elem.get("lon")), float(point_elem.get("lat"))),
                    asset_ref=element.get("id"),
                    taxonomy=element.get("taxonomy"),
                    area=area,
                    number=number,
                    costs=_to_costs(element),
                    occupancy=_to_occupancy(element),
                )
                del element

                yield site_data
Example #8
0
    def __iter__(self):
        """
        Parse the document iteratively.
        """
        for event, element in iterparse(self._source, events=('start', 'end')):

            # exposure metadata
            if event == 'start' and element.tag == '%sexposureModel' % NRML:
                desc = element.find('%sdescription' % NRML)
                if desc is not None:
                    desc = desc.text
                else:
                    desc = ""

                self._meta = ExposureMetadata(
                    exposure_id=element.get('id'),
                    description=desc,
                    taxonomy_source=element.get('taxonomySource'),
                    asset_category=str(element.get('category')),
                    conversions=Conversions(cost_types=[],
                                            area_type=None,
                                            area_unit=None,
                                            deductible_is_absolute=True,
                                            insurance_limit_is_absolute=True))

            # conversions
            if event == 'start' and element.tag == "%sarea" % NRML:
                self._meta.conversions.area_type = element.get('type')
                self._meta.conversions.area_unit = element.get('unit')
            elif event == 'start' and element.tag == "%sdeductible" % NRML:
                self._meta.conversions.deductible_is_absolute = not (
                    element.get('isAbsolute', "false") == "false")
            elif event == 'start' and element.tag == "%sinsuranceLimit" % NRML:
                self._meta.conversions.insurance_limit_is_absolute = not (
                    element.get('isAbsolute', "false") == "false")
            elif event == 'start' and element.tag == "%scostType" % NRML:
                self._meta.conversions.cost_types.append(
                    CostType(name=element.get('name'),
                             conversion_type=element.get('type'),
                             unit=element.get('unit'),
                             retrofitted_type=element.get('retrofittedType'),
                             retrofitted_unit=element.get('retrofittedUnit')))

            # asset data
            elif event == 'end' and element.tag == '%sasset' % NRML:
                if element.get('area') is not None:
                    area = float(element.get('area'))
                else:
                    area = None

                if element.get('number') is not None:
                    number = float(element.get('number'))
                else:
                    number = None

                point_elem = element.find('%slocation' % NRML)

                site_data = AssetData(exposure_metadata=self._meta,
                                      site=Site(float(point_elem.get("lon")),
                                                float(point_elem.get("lat"))),
                                      asset_ref=element.get('id'),
                                      taxonomy=element.get('taxonomy'),
                                      area=area,
                                      number=number,
                                      costs=_to_costs(element),
                                      occupancy=_to_occupancy(element))
                del element

                yield site_data