-
Notifications
You must be signed in to change notification settings - Fork 1
/
iris.go
149 lines (134 loc) · 4.12 KB
/
iris.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package source
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"math"
"regexp"
"strings"
"time"
)
const IrisID ID = "IRIS"
type Iris struct {
FdsnWs
}
func NewIris() Iris {
return Iris{
NewFdsnWs("IRIS", "https://service.iris.edu/fdsnws/event/1/query", IrisID),
}
}
func (s Iris) Transform(r io.Reader) ([]EarthquakeData, error) {
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(r)
if err != nil {
return nil, fmt.Errorf("cannot read from buffer: %v", err)
}
if buf.Len() <= 0 {
return []EarthquakeData{}, nil
}
var eventsRes IrisResponse
err = xml.Unmarshal(buf.Bytes(), &eventsRes)
if err != nil {
return nil, fmt.Errorf("cannot unmarshal event data: %v", err)
}
maxFeatures := 10
if featuresLen := len(eventsRes.EventParameters.Event); featuresLen < maxFeatures {
maxFeatures = featuresLen
}
features := eventsRes.EventParameters.Event[:maxFeatures]
events := make([]EarthquakeData, 0, len(features))
for _, feature := range features {
data := EarthquakeData{
Mag: feature.Magnitude.Mag.Value,
MagType: strings.ToLower(feature.Magnitude.Type),
Depth: math.Abs(feature.Origin.Depth.Value / 1000),
Time: time.Time(feature.Origin.Time.Value).UTC(),
Lat: feature.Origin.Latitude.Value,
Lon: feature.Origin.Longitude.Value,
Location: feature.Description.Text,
DetailsURL: irisDetailsURL(feature.PublicID),
SourceID: s.SourceID,
EventID: feature.Origin.ContributorEventId,
}
events = append(events, data)
}
return events, nil
}
func irisDetailsURL(publicIDPath string) string {
r, _ := regexp.Compile("eventid=([0-9]+)")
parts := r.FindStringSubmatch(publicIDPath)
if len(parts) != 2 {
return ""
}
return fmt.Sprintf("https://ds.iris.edu/ds/nodes/dmc/tools/event/%s", parts[1])
}
type IrisResponse struct {
XMLName xml.Name `xml:"quakeml"`
Text string `xml:",chardata"`
Q string `xml:"q,attr"`
Iris string `xml:"iris,attr"`
Xmlns string `xml:"xmlns,attr"`
Xsi string `xml:"xsi,attr"`
SchemaLocation string `xml:"schemaLocation,attr"`
EventParameters struct {
Text string `xml:",chardata"`
PublicID string `xml:"publicID,attr"`
Event []struct {
Text string `xml:",chardata"`
PublicID string `xml:"publicID,attr"`
Type string `xml:"type"`
Description struct {
Chardata string `xml:",chardata"`
Iris string `xml:"iris,attr"`
FEcode string `xml:"FEcode,attr"`
Type string `xml:"type"`
Text string `xml:"text"`
} `xml:"description"`
PreferredMagnitudeID string `xml:"preferredMagnitudeID"`
PreferredOriginID string `xml:"preferredOriginID"`
Origin struct {
Text string `xml:",chardata"`
Iris string `xml:"iris,attr"`
PublicID string `xml:"publicID,attr"`
ContributorOriginId string `xml:"contributorOriginId,attr"`
Contributor string `xml:"contributor,attr"`
ContributorEventId string `xml:"contributorEventId,attr"`
Catalog string `xml:"catalog,attr"`
Time struct {
Text string `xml:",chardata"`
Value fdsnwsTime `xml:"value"`
} `xml:"time"`
CreationInfo struct {
Text string `xml:",chardata"`
Author string `xml:"author"`
} `xml:"creationInfo"`
Latitude struct {
Text string `xml:",chardata"`
Value float64 `xml:"value"`
} `xml:"latitude"`
Longitude struct {
Text string `xml:",chardata"`
Value float64 `xml:"value"`
} `xml:"longitude"`
Depth struct {
Text string `xml:",chardata"`
Value float64 `xml:"value"`
} `xml:"depth"`
} `xml:"origin"`
Magnitude struct {
Text string `xml:",chardata"`
PublicID string `xml:"publicID,attr"`
Mag struct {
Text string `xml:",chardata"`
Value float64 `xml:"value"`
} `xml:"mag"`
Type string `xml:"type"`
CreationInfo struct {
Text string `xml:",chardata"`
Author string `xml:"author"`
} `xml:"creationInfo"`
} `xml:"magnitude"`
} `xml:"event"`
} `xml:"eventParameters"`
}