def site_model_from_xml(regolith_polygon_filename, eqrm_dir='..'): import sys # imports important system function for use in Python if not eqrm_dir + '/eqrm_code/' in sys.path: # adds the EQRM code subdirectory to the PATH sys.path.append(eqrm_dir + '/eqrm_code/') from eqrm_code.xml_interface import Xml_Interface from scipy import array doc = Xml_Interface(filename=regolith_polygon_filename) xml_source_model = doc['site_class_polygons'][0] # opened the xml source_polygon_filename site_polygons = [] polygon_site_classes = [] xml_polygons = doc['site_class_polygon'] # get a list of xml polygons for xml_polygon in xml_polygons: boundary = xml_polygon.array site_class = xml_polygon.attributes['site_class'] # make the polygon an array polygon = array(boundary) site_polygons.append(polygon) polygon_site_classes.append(site_class) doc.unlink() return site_polygons, polygon_site_classes
def site_model_from_xml(regolith_polygon_filename, eqrm_dir='..'): import sys # imports important system function for use in Python if not eqrm_dir+'/eqrm_code/' in sys.path: # adds the EQRM code subdirectory to the PATH sys.path.append(eqrm_dir+'/eqrm_code/') from eqrm_code.xml_interface import Xml_Interface from scipy import array doc=Xml_Interface(filename=regolith_polygon_filename) xml_source_model=doc['site_class_polygons'][0] # opened the xml source_polygon_filename site_polygons=[] polygon_site_classes=[] xml_polygons = doc['site_class_polygon'] # get a list of xml polygons for xml_polygon in xml_polygons: boundary = xml_polygon.array site_class=xml_polygon.attributes['site_class'] # make the polygon an array polygon=array(boundary) site_polygons.append(polygon) polygon_site_classes.append(site_class) doc.unlink() return site_polygons,polygon_site_classes
def source_model_from_xml(filename): doc = Xml_Interface(filename=filename) xml_source_model = doc['source_model_zone'][0] magnitude_type = xml_source_model.attributes['magnitude_type'] source_zone_polygons = [] xml_polygons = doc['zone'] for i, xml_polygon in enumerate(xml_polygons): try: polygon_name = xml_polygon.attributes['name'] except KeyError: polygon_name = str(i) geometry = xml_polygon['geometry'][0] boundary = geometry['boundary'][0].array recurrence_models, event_gen = get_recurrence_elements(xml_polygon) area = float(xml_polygon.attributes['area']) event_type = xml_polygon.attributes['event_type'] generation_min_mag = float(event_gen.attributes[ 'generation_min_mag']) exclude = [] for exclusion_zone in xml_polygon['excludes']: exclude.append(exclusion_zone.array) # print 'LAMBDAMIN 1 ',A_min source_zone_polygon = Source_Zone( boundary, exclude, [RecurrenceModel(**rm.attributes) for rm in recurrence_models], generation_min_mag, event_type, polygon_name) source_zone_polygons.append(source_zone_polygon) doc.unlink() return Source_Model(source_zone_polygons, magnitude_type)
class Test_Xml_Interface(unittest.TestCase): def setUp(self): # correctly formed old-style XML str1 = '\n'.join([ '<Event magnitude_type="Mw">', ' <polygon>', ' <boundary>', '-20 120', '-30 120', '-20 120', ' </boundary>', ' <depth distribution="constant" mean="10"></depth>', ' </polygon>', ' <polygon>', ' <boundary>', '-30 121', '-35 122', '-30 135', '-30 121', ' </boundary>', ' <exclude>', '-32 121', '-34 122', '-32 121', ' </exclude>', ' <exclude>', '-35 121', '-36 122', '-35 121', ' </exclude>', ' <depth distribution="constant" mean="5" />', ' </polygon>', '</Event>' ]) self.xml = Xml_Interface(string=str1) # similar to above but simplified, wrong name for top-level tag str2 = '\n'.join([ '<EventX magnitude_type="Mw">', ' <polygon>', ' <depth distribution="constant" mean="10"></depth>', ' </polygon>', ' <polygon>', ' <depth distribution="constant" mean="5" />', ' </polygon>', '</EventX>' ]) self.xml2 = Xml_Interface(string=str2) def tearDown(self): self.xml.unlink() self.xml2.unlink() def test_attributes(self): assert self.xml['Event'][0].attributes['magnitude_type'] == 'Mw' def test_getting_polygons(self): assert len(self.xml['polygon']) == 2 def test_getting_excludes(self): assert len(self.xml['polygon'][0]['exclude']) == 0 assert len(self.xml['polygon'][1]['exclude']) == 2 def test_getting_array(self): exclude_array = self.xml['polygon'][1]['exclude'][0].array expected_exclude_array = [[-32.0, 121], [-34, 122], [-32, 121]] assert allclose(asarray(exclude_array), asarray(expected_exclude_array)) def Xtest_build_zone_xml(self): # Looking into something different root = build_xml() def test_top_level_tag_name(self): """Test getting some info about the XML document. Mainly to test various operations we will use. """ # test getting name of top-level tag # NOTE: There *must* be a better way to do this!? top_tag = self.xml.xml_node.documentElement.nodeName msg = "Expected top-level tag of 'Event', got '%s'" % top_tag self.failUnlessEqual(top_tag, 'Event', msg)
class Test_Xml_Interface(unittest.TestCase): def setUp(self): # correctly formed old-style XML str1 = '\n'.join(['<Event magnitude_type="Mw">', ' <polygon>', ' <boundary>', '-20 120', '-30 120', '-20 120', ' </boundary>', ' <depth distribution="constant" mean="10"></depth>', ' </polygon>', ' <polygon>', ' <boundary>', '-30 121', '-35 122', '-30 135', '-30 121', ' </boundary>', ' <exclude>', '-32 121', '-34 122', '-32 121', ' </exclude>', ' <exclude>', '-35 121', '-36 122', '-35 121', ' </exclude>', ' <depth distribution="constant" mean="5" />', ' </polygon>', '</Event>']) self.xml = Xml_Interface(string=str1) # similar to above but simplified, wrong name for top-level tag str2 = '\n'.join(['<EventX magnitude_type="Mw">', ' <polygon>', ' <depth distribution="constant" mean="10"></depth>', ' </polygon>', ' <polygon>', ' <depth distribution="constant" mean="5" />', ' </polygon>', '</EventX>']) self.xml2 = Xml_Interface(string=str2) def tearDown(self): self.xml.unlink() self.xml2.unlink() def test_attributes(self): assert self.xml['Event'][0].attributes['magnitude_type']=='Mw' def test_getting_polygons(self): assert len(self.xml['polygon'])==2 def test_getting_excludes(self): assert len(self.xml['polygon'][0]['exclude'])==0 assert len(self.xml['polygon'][1]['exclude'])==2 def test_getting_array(self): exclude_array = self.xml['polygon'][1]['exclude'][0].array expected_exclude_array=[[-32.0,121],[-34,122],[-32,121]] assert allclose(asarray(exclude_array),asarray(expected_exclude_array)) def Xtest_build_zone_xml(self): # Looking into something different root = build_xml() def test_top_level_tag_name(self): """Test getting some info about the XML document. Mainly to test various operations we will use. """ # test getting name of top-level tag # NOTE: There *must* be a better way to do this!? top_tag = self.xml.xml_node.documentElement.nodeName msg = "Expected top-level tag of 'Event', got '%s'" % top_tag self.failUnlessEqual(top_tag, 'Event', msg)