Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/fdsn-ws/etc/fdsn-station-test.xml

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions cmd/fdsn-ws/fdsn_dataselect.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func fdsnDataMetricsV1Handler(r *http.Request, h http.Header, b *bytes.Buffer) e

switch r.Method {
case "POST":
defer r.Body.Close()
defer func() { _ = r.Body.Close() }()
if err := fdsn.ParseDataSelectPost(r.Body, &params); err != nil {
return fdsnError{StatusError: weft.StatusError{Code: http.StatusBadRequest, Err: err}, url: r.URL.String(), timestamp: tm}
}
Expand Down Expand Up @@ -144,7 +144,7 @@ func fdsnDataselectV1Handler(r *http.Request, w http.ResponseWriter) (int64, err

switch r.Method {
case "POST":
defer r.Body.Close()
defer func() { _ = r.Body.Close() }()
if err := fdsn.ParseDataSelectPost(r.Body, &params); err != nil {
return 0, fdsnError{StatusError: weft.StatusError{Code: http.StatusBadRequest, Err: err}, url: r.URL.String(), timestamp: tm}
}
Expand Down Expand Up @@ -181,7 +181,7 @@ func fdsnDataselectV1Handler(r *http.Request, w http.ResponseWriter) (int64, err

for _, v := range params {
//flick gtHalfHour to true if the request is longer than half an hour
gtHalfHour = gtHalfHour || v.EndTime.Sub(time.Time(v.StartTime.Time)) > time.Minute*30
gtHalfHour = gtHalfHour || v.EndTime.Sub(v.StartTime.Time) > time.Minute*30

d, err := v.Regexp()
if err != nil {
Expand Down Expand Up @@ -303,7 +303,7 @@ func fdsnDataselectVersion(r *http.Request, h http.Header, b *bytes.Buffer) erro
}

h.Set("Content-Type", "text/plain")
_, err = b.WriteString("1.1")
_, err = b.WriteString(dataselectVersion)

return err
}
Expand Down
70 changes: 33 additions & 37 deletions cmd/fdsn-ws/fdsn_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,29 @@ var eventAbbreviations = map[string]string{
// supported query parameters for the event service from http://www.fdsn.org/webservices/FDSN-WS-Specifications-1.1.pdf
type fdsnEventV1 struct {
// required
StartTime Time `schema:"starttime"` // limit to events on or after the specified start time.
EndTime Time `schema:"endtime"` // limit to events on or before the specified end time.
MinLatitude float64 `schema:"minlatitude"` // limit to events with a latitude larger than or equal to the specified minimum.
MaxLatitude float64 `schema:"maxlatitude"` // limit to events with a latitude smaller than or equal to the specified maximum.
MinLongitude float64 `schema:"minlongitude"` // limit to events with a longitude larger than or equal to the specified minimum.
MaxLongitude float64 `schema:"maxlongitude"` // limit to events with a longitude smaller than or equal to the specified maximum.
MinDepth float64 `schema:"mindepth"` // limit to events with depth more than the specified minimum.
MaxDepth float64 `schema:"maxdepth"` // limit to events with depth less than the specified maximum.
MinMagnitude float64 `schema:"minmagnitude"` // limit to events with a magnitude larger than the specified minimum.
MaxMagnitude float64 `schema:"maxmagnitude"` // limit to events with a magnitude smaller than the specified maximum.
OrderBy string `schema:"orderby"` // order the result by time or magnitude with the following possibilities: time, time-asc, magnitude, magnitude-asc
StartTime fdsn.WsDateTime `schema:"starttime"` // limit to events on or after the specified start time.
EndTime fdsn.WsDateTime `schema:"endtime"` // limit to events on or before the specified end time.
MinLatitude float64 `schema:"minlatitude"` // limit to events with a latitude larger than or equal to the specified minimum.
MaxLatitude float64 `schema:"maxlatitude"` // limit to events with a latitude smaller than or equal to the specified maximum.
MinLongitude float64 `schema:"minlongitude"` // limit to events with a longitude larger than or equal to the specified minimum.
MaxLongitude float64 `schema:"maxlongitude"` // limit to events with a longitude smaller than or equal to the specified maximum.
MinDepth float64 `schema:"mindepth"` // limit to events with depth more than the specified minimum.
MaxDepth float64 `schema:"maxdepth"` // limit to events with depth less than the specified maximum.
MinMagnitude float64 `schema:"minmagnitude"` // limit to events with a magnitude larger than the specified minimum.
MaxMagnitude float64 `schema:"maxmagnitude"` // limit to events with a magnitude smaller than the specified maximum.
OrderBy string `schema:"orderby"` // order the result by time or magnitude with the following possibilities: time, time-asc, magnitude, magnitude-asc

// supported optionals
Latitude float64 `schema:"latitude"`
Longitude float64 `schema:"longitude"`
MinRadius float64 `schema:"minradius"`
MaxRadius float64 `schema:"maxradius"`
PublicID string `schema:"eventid"` // select a specific event by ID; event identifiers are data center specific.
UpdatedAfter Time `schema:"updatedafter"` // Limit to events updated after the specified time.
Format string `schema:"format"`
NoData int `schema:"nodata"` // Select status code for “no data”, either ‘204’ (default) or ‘404’.
EventType string `schema:"eventtype"`
eventTypeSlice []interface{} // interal use only. holds matched eventtypes
}

type Time struct {
time.Time
Latitude float64 `schema:"latitude"`
Longitude float64 `schema:"longitude"`
MinRadius float64 `schema:"minradius"`
MaxRadius float64 `schema:"maxradius"`
PublicID string `schema:"eventid"` // select a specific event by ID; event identifiers are data center specific.
UpdatedAfter fdsn.WsDateTime `schema:"updatedafter"` // Limit to events updated after the specified time.
Format string `schema:"format"`
NoData int `schema:"nodata"` // Select status code for “no data”, either ‘204’ (default) or ‘404’.
EventType string `schema:"eventtype"`
eventTypeSlice []interface{} // interal use only. holds matched eventtypes
}

var fdsnEventWadlFile []byte
Expand Down Expand Up @@ -230,11 +226,11 @@ func parseEventV1(v url.Values) (fdsnEventV1, error) {
}

if e.Format != "xml" && e.Format != "text" {
return e, errors.New("Invalid format.")
return e, errors.New("invalid format")
}

if e.NoData != 204 && e.NoData != 404 {
return e, errors.New("nodata must be 204 or 404.")
return e, errors.New("nodata must be 204 or 404")
}

// geometry bounds checking
Expand Down Expand Up @@ -276,17 +272,17 @@ func parseEventV1(v url.Values) (fdsnEventV1, error) {
}

if e.MaxRadius < 0 || e.MaxRadius > 180.0 {
err = fmt.Errorf("invalid maxradius value.")
err = fmt.Errorf("invalid maxradius value")
return e, err
}

if e.MinRadius < 0 || e.MinRadius > 180.0 {
err = fmt.Errorf("invalid minradius value.")
err = fmt.Errorf("invalid minradius value")
return e, err
}

if e.MinRadius > e.MaxRadius {
err = fmt.Errorf("minradius or maxradius range error.")
err = fmt.Errorf("minradius or maxradius range error")
return e, err
}
}
Expand Down Expand Up @@ -451,19 +447,19 @@ func (e *fdsnEventV1) filter() (q string, args []interface{}) {
i++
}

if !e.StartTime.Time.IsZero() {
if !e.StartTime.IsZero() {
q = fmt.Sprintf("%s origintime >= $%d AND", q, i)
args = append(args, e.StartTime.Time)
i++
}

if !e.EndTime.Time.IsZero() {
if !e.EndTime.IsZero() {
q = fmt.Sprintf("%s origintime <= $%d AND", q, i)
args = append(args, e.EndTime.Time)
i++
}

if !e.UpdatedAfter.Time.IsZero() {
if !e.UpdatedAfter.IsZero() {
q = fmt.Sprintf("%s modificationtime >= $%d AND", q, i)
args = append(args, e.UpdatedAfter.Time)
i++
Expand Down Expand Up @@ -531,7 +527,7 @@ func fdsnEventV1Handler(r *http.Request, h http.Header, b *bytes.Buffer) error {
if err != nil {
return fdsnError{StatusError: weft.StatusError{Code: http.StatusInternalServerError, Err: err}, url: r.URL.String(), timestamp: tm}
}
defer rows.Close()
defer func() { _ = rows.Close() }()

b.WriteString(`<?xml version="1.0" encoding="UTF-8"?>
<q:quakeml xmlns:q="http://quakeml.org/xmlns/quakeml/1.2" xmlns="http://quakeml.org/xmlns/bed/1.2">
Expand All @@ -556,7 +552,7 @@ func fdsnEventV1Handler(r *http.Request, h http.Header, b *bytes.Buffer) error {
if err != nil {
return fdsnError{StatusError: weft.StatusError{Code: http.StatusInternalServerError, Err: err}, url: r.URL.String(), timestamp: tm}
}
defer rows.Close()
defer func() { _ = rows.Close() }()

b.WriteString("#EventID | Time | Latitude | Longitude | Depth/km | Author | Catalog | Contributor | ContributorID | MagType | Magnitude | MagAuthor | EventLocationName | EventType\n")

Expand All @@ -572,7 +568,7 @@ func fdsnEventV1Handler(r *http.Request, h http.Header, b *bytes.Buffer) error {
if l, err := wgs84.ClosestNZ(latitude, longitude); err == nil {
loc = l.Description()
}
s := fmt.Sprintf("%s|%s|%.3f|%.3f|%.1f|GNS|GNS|GNS|%s|%s|%.1f|GNS|%s|%s\n", eventID, tm.UTC().Format(time.RFC3339Nano), latitude, longitude, depth, eventID, magType, magnitude, loc, eventType)
s := fmt.Sprintf("%s|%s|%.3f|%.3f|%.1f|GNS|GNS|GNS|%s|%s|%.1f|GNS|%s|%s\n", eventID, tm.UTC().Format(fdsn.WsMarshalTimeFormat), latitude, longitude, depth, eventID, magType, magnitude, loc, eventType)
b.WriteString(s)
}

Expand All @@ -591,7 +587,7 @@ func fdsnEventVersion(r *http.Request, h http.Header, b *bytes.Buffer) error {
}

h.Set("Content-Type", "text/plain")
_, err = b.WriteString("1.1")
_, err = b.WriteString(eventVersion)

return err
}
Expand Down
43 changes: 12 additions & 31 deletions cmd/fdsn-ws/fdsn_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"net/url"
"reflect"
"testing"
"time"

"github.com/GeoNet/fdsn/internal/fdsn"
)

func TestEventV1Query(t *testing.T) {
Expand Down Expand Up @@ -54,13 +55,11 @@ func TestEventV1Query(t *testing.T) {
EventType: "*", // default value
}

ex.StartTime.Time, err = time.Parse(time.RFC3339Nano, "2015-01-12T12:12:12.000000000Z")
if err != nil {
if err = ex.StartTime.UnmarshalText([]byte("2015-01-12T12:12:12.000000")); err != nil {
t.Error(err)
}

ex.EndTime.Time, err = time.Parse(time.RFC3339Nano, "2015-02-12T12:12:12.000000000Z")
if err != nil {
if err = ex.EndTime.UnmarshalText([]byte("2015-02-12T12:12:12.000000")); err != nil {
t.Error(err)
}

Expand Down Expand Up @@ -103,26 +102,6 @@ func TestEventV1OrderBy(t *testing.T) {
}
}

func TestTimeParse(t *testing.T) {
var tm Time

if err := tm.UnmarshalText([]byte("2015-01-12T12:12:12.999999")); err != nil {
t.Error(err)
}

if err := tm.UnmarshalText([]byte("2015-01-12T12:12:12")); err != nil {
t.Error(err)
}

if err := tm.UnmarshalText([]byte("2015-01-12")); err != nil {
t.Error(err)
}

if err := tm.UnmarshalText([]byte("2015-01-12T12:12:12.invalid")); err == nil {
t.Error("expected an error for invalid time string.")
}
}

func TestEventQuery(t *testing.T) {
setup(t)
defer teardown()
Expand Down Expand Up @@ -334,14 +313,16 @@ func TestEventAbbreviations(t *testing.T) {
t.Errorf("expected 2.2 for maxmag %f.\n", e.MaxMagnitude)
}

tm, _ := time.Parse(time.RFC3339Nano, "2016-09-04T00:00:00.000000000Z")
if !e.StartTime.Equal(tm) {
t.Errorf("start parameter error: %s", e.StartTime.Format(time.RFC3339Nano))
var tm fdsn.WsDateTime

_ = tm.UnmarshalText([]byte("2016-09-04T00:00:00.000000"))
if !e.StartTime.Equal(tm.Time) {
t.Errorf("start parameter error: %v", e.StartTime)
}

tm, _ = time.Parse(time.RFC3339Nano, "2016-09-05T00:00:00.000000000Z")
if !e.EndTime.Equal(tm) {
t.Errorf("end parameter error: %s", e.EndTime.Format(time.RFC3339Nano))
_ = tm.UnmarshalText([]byte("2016-09-05T00:00:00"))
if !e.EndTime.Equal(tm.Time) {
t.Errorf("end parameter error: %v", e.EndTime)
}
}

Expand Down
Loading
Loading