Skip to content

Commit

Permalink
added a function to import lon/lat/depth/mag from QuakeML files from ISC
Browse files Browse the repository at this point in the history
  • Loading branch information
mthielma committed Feb 28, 2024
1 parent 170a280 commit f689349
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Glob = "c27321d9-0574-5035-807b-f59d2c89b15c"
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"
Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59"
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
LightXML = "9c8b4983-aa76-5018-a973-4c85ecc9e179"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MeshIO = "7269a6da-0436-5bbc-96c2-40638cbb6118"
NearestNeighbors = "b8a86587-4115-5ab1-83bc-aa920d37bbce"
Expand Down Expand Up @@ -59,4 +60,4 @@ GMT = "5752ebe1-31b9-557e-87aa-f909b540aa54"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test","GMT"]
test = ["Test", "GMT"]
42 changes: 40 additions & 2 deletions src/data_import.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#
# Author: Marcel Thielmann, 05/2021

export Screenshot_To_GeoData, Screenshot_To_CartData, Screenshot_To_UTMData
using LightXML

export Screenshot_To_GeoData, Screenshot_To_CartData, Screenshot_To_UTMData, GetLonLatDepthMag_QuakeML

# import CSV data using standard library functions
# here we assume that the data is indeed comma separated and that comments are preceded with a "#"
Expand Down Expand Up @@ -309,7 +311,43 @@ function Screenshot_To_UTMData(filename::String, Corner_LowerLeft, Corner_UpperR

# first create a GeoData struct
Data_UTM = Screenshot_To_GeoData(filename, Corner_LowerLeft, Corner_UpperRight; Corner_LowerRight=Corner_LowerRight, Corner_UpperLeft=Corner_UpperLeft, Cartesian=false, UTM=true, UTMzone=UTMzone, isnorth=isnorth, fieldname=fieldname)

return Data_UTM
end

"""
Data = GetLonLatDepthMag_QuakeML(filename::String)
Extracts longitude, latitude, depth and magnitude from a QuakeML file that has been e.g. downloaded from ISC. The data is then returned in GeoData format.
"""
function GetLonLatDepthMag_QuakeML(filename::String)

Check warning on line 322 in src/data_import.jl

View check run for this annotation

Codecov / codecov/patch

src/data_import.jl#L322

Added line #L322 was not covered by tests
# The QuakeML format consists of a tree with quite a lot of branches, so we have to traverse it to quite some extent to get the desired values
# using LightXML: extension???
xdoc = parse_file(filename); # parse the whole file
xroot =root(xdoc);
catalogues = get_elements_by_tagname(xroot,"eventParameters");
catalogue = catalogues[1];
events = get_elements_by_tagname(catalogue,"event"); # now those are all events
num_events = size(events,1);

Check warning on line 330 in src/data_import.jl

View check run for this annotation

Codecov / codecov/patch

src/data_import.jl#L325-L330

Added lines #L325 - L330 were not covered by tests

# allocate, lat,lon,depth,magnitude
lon = zeros(num_events,1);
lat = zeros(num_events,1);
depth = zeros(num_events,1);
mag = zeros(num_events,1);

Check warning on line 336 in src/data_import.jl

View check run for this annotation

Codecov / codecov/patch

src/data_import.jl#L333-L336

Added lines #L333 - L336 were not covered by tests

# now loop over the events and assign the respective values
for ievent = 1:num_events
tmp_event = events[ievent];
origin = get_elements_by_tagname(events[ievent], "origin");
magnitude = get_elements_by_tagname(events[ievent], "magnitude");

Check warning on line 342 in src/data_import.jl

View check run for this annotation

Codecov / codecov/patch

src/data_import.jl#L339-L342

Added lines #L339 - L342 were not covered by tests

# this is a bit dirty, if you find a better/cleaner way, be my guest...
lon[ievent] = parse(Float64,string(collect(child_nodes(collect(child_elements(get_elements_by_tagname(origin[1], "longitude")[1]))[1]))[1]))
lat[ievent] = parse(Float64,string(collect(child_nodes(collect(child_elements(get_elements_by_tagname(origin[1], "latitude")[1]))[1]))[1]))
depth[ievent] = parse(Float64,string(collect(child_nodes(collect(child_elements(get_elements_by_tagname(origin[1], "depth")[1]))[1]))[1]))
mag[ievent] = parse(Float64,string(collect(child_nodes(get_elements_by_tagname(get_elements_by_tagname(magnitude[1],"mag")[1],"value")[1]))[1]));
end

Check warning on line 349 in src/data_import.jl

View check run for this annotation

Codecov / codecov/patch

src/data_import.jl#L345-L349

Added lines #L345 - L349 were not covered by tests

Data_ISC = GeoData(lon,lat,depth,(Magnitude=mag,Depth=depth));
return Data_ISC

Check warning on line 352 in src/data_import.jl

View check run for this annotation

Codecov / codecov/patch

src/data_import.jl#L351-L352

Added lines #L351 - L352 were not covered by tests
end

0 comments on commit f689349

Please sign in to comment.