1

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

1
  • what about, try for event in root.findall('origin'): for origin in event.findall('quality'): azimuthal_gap = origin.find('azimuthalGap').text Commented Dec 6, 2024 at 4:45

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

With a simpler method:

with open("SCB_earthquakes.xml", "r") as infile:
    lines = infile.read().split("\n")
    for line in lines:
        if '<azimuthalGap>' in line:
            gaps.append(line.split('<azimuthalGap>')[1].split('<')[0])

2 Comments

Consider parsing XML with DOM methods and not as a generic text file. Will save you headaches!
@Parfait Yes, DOM method is maybe better in case of a more complicated xml, but now it seems simple this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.