コード例 #1
0
ファイル: rate_grids.py プロジェクト: g-weatherill/hmtk
    def from_model_files(cls,
                         limits,
                         input_model,
                         investigation_time=1.0,
                         simple_mesh_spacing=1.0,
                         complex_mesh_spacing=5.0,
                         mfd_width=0.1,
                         area_discretisation=10.0):
        """
        Reads the hazard model from a file
        :param list limits:
             Grid configuration [west, east, xspc, south, north, yspc,
                                 upper, lower, zspc]
        :param str input_model:
            Path to input source model
        :param float investigation_time:
            Investigation time of Poisson model
        :param float simple_mesh_spacing:
            Rupture mesh spacing of simple fault (km)
        :param float complex_mesh_spacing:
            Rupture mesh spacing of complex fault (km)
        :param float mfd_width:
            Spacing (in magnitude units) of MFD
        :param float area_discretisation:
            Spacing of discretisation of area source (km)
        """
        converter = SourceConverter(investigation_time, simple_mesh_spacing,
                                    complex_mesh_spacing, mfd_width,
                                    area_discretisation)

        parser = SourceModelParser(converter)
        sources = parser.parse_sources(input_model)
        return cls(limits, sources, area_discretisation)
コード例 #2
0
def read_area_source(area_source_file, discretisation=200.):
    """Calls OpenQuake parsers to read area source model
    from source_mode.xml type file, convert to point sources
    and write to a new nrml source model file.
    :params area_source_file:
        nrml format file of the area source
    :params discretisation:
        Grid size (km) for the area source discretisation, 
        which defines the distance between resulting point
        sources.
    """
    converter = SourceConverter(50,
                                10,
                                width_of_mfd_bin=0.1,
                                area_source_discretization=discretisation)
    parser = SourceModelParser(converter)
    print[method
          for method in dir(parser)]  # if callable(getattr(parser, method))]
    try:
        sources = parser.parse_sources(area_source_file)
    except AttributeError:  # Handle version 2.1 and above
        sources = []
        groups = parser.parse_src_groups(area_source_file)
        for group in groups:
            for source in group:
                sources.append(source)
    for source in sources:
        print source.polygon
    return sources
コード例 #3
0
 def test_is_writeable(self):
     parser = SourceModelParser(SourceConverter(50., 1., 10, 0.1, 10.))
     [sf, cf] = map(copy.deepcopy, parser.parse_sources(ALT_MFDS))
     # there are a SimpleFaultSource and a CharacteristicFaultSource
     fd, fname = tempfile.mkstemp(suffix='.xml')
     with os.fdopen(fd, 'w'):
         write_source_model(fname, [sf, cf], 'Test Source Model')
コード例 #4
0
def get_source_model(source_file,
                     inv_time=50.0,
                     simple_mesh_spacing=1.0,
                     complex_mesh_spacing=10.0,
                     mfd_spacing=0.1,
                     area_discretisation=10.0):
    conv = SourceConverter(inv_time, simple_mesh_spacing, complex_mesh_spacing,
                           mfd_spacing, area_discretisation)
    return parse_source_model(source_file, conv)
コード例 #5
0
 def check_round_trip(self, fname):
     parser = SourceModelParser(SourceConverter(50., 1., 10, 0.1, 10.))
     sources = parser.parse_sources(fname)
     fd, name = tempfile.mkstemp(suffix='.xml')
     with os.fdopen(fd, 'w'):
         write_source_model(name, sources, 'Test Source Model')
     if open(name).read() != open(fname).read():
         raise Exception('Different files: %s %s' % (name, fname))
     os.remove(name)
コード例 #6
0
 def read(self, nrml_file, validate=False,
         simple_fault_spacing=1.0, complex_mesh_spacing=5.0,
         mfd_spacing=0.1):
     """
     Build the source model from nrml format
     """
     self.source_file = nrml_file
     if validate:
         converter = SourceConverter(1.0, simple_fault_spacing,
                                     complex_mesh_spacing,
                                     mfd_spacing,
                                     10.0)
     src_nodes = nrml.read(nrml_file).sourceModel
     sources = []
     for no, src_node in enumerate(src_nodes, 1):
         if validate:
             print("Validating Source %s" % src_node.attrib["id"])
             converter.convert_node(src_node)
         sources.append(src_node)
     return SourceModel(sources)
コード例 #7
0
    def read(self, input_shapefile, validate=False,
            simple_fault_spacing=1.0, complex_mesh_spacing=5.0,
            mfd_spacing=0.1):
        """
        Build the source model from nrml format
        """
        reader = shapefile.Reader(input_shapefile)
        fields = [field[0] for field in reader.fields[1:]]
        shapes = reader.shapes()
        records = reader.records()
        sources = []
        if validate:
            converter = SourceConverter(1.0, simple_fault_spacing,
                                        complex_mesh_spacing,
                                        mfd_spacing,
                                        10.0)

        for iloc in range(0, reader.numRecords):
            # Build record dictionary
            record = record_to_dict(records[iloc], fields)
            shape = shapes[iloc]
            if "pointSource" in record["sourcetype"]:
                src = build_point_source_from_shp(shape, record)
            elif "areaSource" in record["sourcetype"]:
                src = build_area_source_from_shp(shape, record)
            elif "simpleFaultSource" in record["sourcetype"]:
                src = build_simple_fault_source_from_shp(shape, record)
            elif "complexFaultSource" in record["sourcetype"]:
                src = build_complex_fault_source_from_shp(shape, record)
            elif "characteristicFaultSource" in record["sourcetype"]:
                print "Characteristic Fault Source Not Yet Supported - Sorry!"
            else:
                pass
            if validate:
                print "Validating Source %s" % src.attrib["id"]
                _ = converter.convert_node(src)
            sources.append(src)
        return SourceModel(sources)
コード例 #8
0
ファイル: area2pt_source.py プロジェクト: alexgorb/NSHA2018
def area2pt_source(area_source_file, discretisation=200.):
    """Calls OpenQuake parsers to read area source model
    from source_mode.xml type file, convert to point sources
    and write to a new nrml source model file.
    :params area_source_file:
        nrml format file of the area source
    :params discretisation:
        Grid size (km) for the area source discretisation, 
        which defines the distance between resulting point
        sources.
    """
    converter = SourceConverter(50,
                                10,
                                width_of_mfd_bin=0.1,
                                area_source_discretization=discretisation)
    parser = SourceModelParser(converter)
    print[method
          for method in dir(parser)]  # if callable(getattr(parser, method))]
    try:
        sources = parser.parse_sources(area_source_file)
    except AttributeError:  # Handle version 2.1 and above
        sources = []
        groups = parser.parse_src_groups(area_source_file)
        for group in groups:
            for source in group:
                sources.append(source)
    name = 'test_point_model'
    new_pt_sources = {}
    for source in sources:
        pt_sources = area_to_point_sources(source)
        for pt in pt_sources:
            pt.source_id = pt.source_id.replace(':', '')
            pt.name = pt.name.replace(':', '_')
            try:
                new_pt_sources[pt.tectonic_region_type].append(pt)
            except KeyError:
                new_pt_sources[pt.tectonic_region_type] = [pt]
        # print [method for method in dir(pt) if callable(getattr(pt, method))]
        #  print [attribute for attribute in dir(pt)]
    nrml_pt_file = area_source_file[:-4] + '_pts.xml'
    source_group_list = []
    id = 0
    for trt, sources in new_pt_sources.iteritems():
        source_group = SourceGroup(trt, sources=sources, id=id)
        id += 1
        source_group_list.append(source_group)
    write_source_model(nrml_pt_file, source_group_list, name='Leonard2008')
コード例 #9
0
def read_simplefault_source(simplefault_source_file):
    """Read nrml source model into simpmle fault objects
    """
    converter = SourceConverter(50, 2, width_of_mfd_bin=0.1,
                                area_source_discretization=200.)
    parser = SourceModelParser(converter)
    try:
        sources = parser.parse_sources(simplefault_source_file)
    except AttributeError: # Handle version 2.1 and above
        sources = []
        groups = parser.parse_src_groups(simplefault_source_file)
        for group in groups:
            for source in group:
                sources.append(source)
    for fault in sources:
        print fault.mfd.max_mag
    return sources
コード例 #10
0
def read_pt_source(pt_source_file):
    """Read nrml source model into pt source objects
    """
    converter = SourceConverter(50,
                                10,
                                width_of_mfd_bin=0.1,
                                area_source_discretization=200.)
    parser = SourceModelParser(converter)
    try:
        sources = parser.parse_sources(pt_source_file)
    except AttributeError:  # Handle version 2.1 and above
        sources = []
        groups = parser.parse_src_groups(pt_source_file)
        for group in groups:
            for source in group:
                sources.append(source)
    return sources