I have and XML file (please see attached).
I made a python script to try to obtain information from the XML to later do some plots.
The aim of this code is:
a. To iterate over the XML file to find the </event>
and then </origin>
and then <quality>
to reach <azimuthalGap>348.000</azimuthalGap>
b. To save each azimutalGap in the gaps=[]
variable
c. To plot an Histograms of azimutalGap.
So far I tried the code:
import xml.etree.ElementTree as ET
def parse_azimuthal_gaps(xml_file):
gaps = []
# Parse the XML file
tree = ET.parse(xml_file)
root = tree.getroot()
# Iterate through each event
for event in root.findall(".//event"):
origin = event.find(".//origin")
if origin is not None:
# Find the azimuthal gap (if present)
azimuthal_gap = origin.find(".//quality/azimuthalGap")
if azimuthal_gap is not None:
gap_value = azimuthal_gap.text
if gap_value is not None:
try:
gaps.append(float(gap_value))
except ValueError:
continue # Skip if not a valid float
return gaps
# Parse azimuthal gaps from the ISC XML file
gaps = parse_azimuthal_gaps("SCB_earthquakes.xml")
print("Azimuthal Gaps:", gaps[:10]) # Print the first 10 gaps for inspection
But I always get empty gaps varible.
The input file has this format:
</event>
<event publicID="smi:ISC/evid=602401243">
<preferredOriginID>smi:ISC/origid=601808754</preferredOriginID>
<description>
<text>Peru-Bolivia border region</text>
<type>Flinn-Engdahl region</type>
</description>
<type>earthquake</type>
<typeCertainty>known</typeCertainty>
<comment>
<text>Event reviewed by the ISC</text>
</comment>
<creationInfo>
<agencyID>ISC</agencyID>
<author>ISC</author>
</creationInfo>
<origin publicID="smi:ISC/origid=601808754">
<time>
<value>2011-01-23T09:13:37.30Z</value>
<uncertainty>0.96</uncertainty>
</time>
<latitude>
<value>-11.9240</value>
</latitude>
<longitude>
<value>-68.9133</value>
</longitude>
<depth>
<value>58000.0</value>
</depth>
<depthType>operator assigned</depthType>
<quality>
<usedPhaseCount>8</usedPhaseCount>
<associatedStationCount>6</associatedStationCount>
<standardError>0.4800</standardError>
<azimuthalGap>353.000</azimuthalGap>
<minimumDistance>4.260</minimumDistance>
<maximumDistance>5.070</maximumDistance>
</quality>
<creationInfo>
<author>SCB</author>
<agencyID>SCB</agencyID>
</creationInfo>
<originUncertainty>
<preferredDescription>uncertainty ellipse</preferredDescription>
<minHorizontalUncertainty>20399.9996185303</minHorizontalUncertainty>
<maxHorizontalUncertainty>96000</maxHorizontalUncertainty>
<azimuthMaxHorizontalUncertainty>83.0</azimuthMaxHorizontalUncertainty>
</originUncertainty>
<arrival publicID="smi:ISC/pickid=637614753/hypid=601808754">
<pickID>smi:ISC/pickid=637614753</pickID>
<phase>Pn</phase>
<azimuth>170.165</azimuth>
<distance>4.404</distance>
<timeResidual>3.6</timeResidual>
</arrival>
<arrival publicID="smi:ISC/pickid=637614754/hypid=601808754">
<pickID>smi:ISC/pickid=637614754</pickID>
<phase>Sn</phase>
<azimuth>170.165</azimuth>
<distance>4.404</distance>
<timeResidual>4.6</timeResidual>
</arrival>
</origin>
<pick publicID="smi:ISC/pickid=637614753">
<time>
<value>2011-01-23T09:14:46.10Z</value>
</time>
<waveformID networkCode="IR" stationCode="LPAZ"></waveformID>
<onset>impulsive</onset>
<polarity>positive</polarity>
<phaseHint>Pn</phaseHint>
</pick>
<pick publicID="smi:ISC/pickid=637614754">
<time>
<value>2011-01-23T09:15:37.60Z</value>
</time>
<waveformID networkCode="IR" stationCode="LPAZ"></waveformID>
<onset>emergent</onset>
<phaseHint>Sn</phaseHint>
</pick>
<magnitude publicID="smi:ISC/magid=602398394">
<mag>
<value>3.80</value>
<uncertainty>0.40</uncertainty>
</mag>
<type>Ml</type>
<originID>smi:ISC/origid=601808754</originID>
<stationCount>2</stationCount>
<creationInfo>
<author>SCB</author>
</creationInfo>
</magnitude>
<preferredMagnitudeID>smi:ISC/magid=602398394</preferredMagnitudeID>
Will you have any idea to improve the code?
Tonino
If you have a list of events, you can create the interested gaps list with root.findall()
.
import xml.etree.ElementTree as ET
root = ET.parse("your_file.xml").getroot()
gaps = root.findall(".//azimuthalGap")
print([x.text for x in gaps])
Output:
['353.000', '453.000', …]
For the 3. point of your question, you will find a answer here.