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/fdsn_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,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.Format("2006-01-02T15:04:05"), 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(time.RFC3339Nano), latitude, longitude, depth, eventID, magType, magnitude, loc, eventType)
b.WriteString(s)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/fdsn-ws/fdsn_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestTimeParse(t *testing.T) {
t.Error(err)
}

if err := tm.UnmarshalText([]byte("2015-01-12T12:12:12-09:00")); err == nil {
if err := tm.UnmarshalText([]byte("2015-01-12T12:12:12.invalid")); err == nil {
t.Error("expected an error for invalid time string.")
}
}
Expand Down
96 changes: 51 additions & 45 deletions cmd/fdsn-ws/fdsn_station.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ var (
fdsnStations fdsnStationObj
emptyDateTime = time.Date(9999, 1, 1, 0, 0, 0, 0, time.UTC)
errNotModified = fmt.Errorf("Not modified.")
s3Bucket string
s3Meta string
stationXMLBucket string
stationXMLKey string
)

func initStationTemplate() {
Expand All @@ -134,18 +134,18 @@ func initStationTemplate() {

func initStationXML() {
var err error
s3Bucket = os.Getenv("STATION_XML_BUCKET")
s3Meta = os.Getenv("STATION_XML_META_KEY")
stationXMLBucket = os.Getenv("STATION_XML_BUCKET")
stationXMLKey = os.Getenv("STATION_XML_META_KEY")

// Prepare the data source for station.
// If there's no local file available then we'll have to download first.
by := bytes.NewBuffer(nil)
modified := zeroDateTime
var s os.FileInfo
if s, err = os.Stat("etc/" + s3Meta); err == nil {
log.Println("Loading fdsn station xml file ", "etc/"+s3Meta)
if s, err = os.Stat("etc/" + stationXMLKey); err == nil {
log.Println("Loading fdsn station xml file ", "etc/"+stationXMLKey)
var f *os.File
if f, err = os.Open("etc/" + s3Meta); err == nil {
if f, err = os.Open("etc/" + stationXMLKey); err == nil {

if _, err = io.Copy(by, f); err != nil {
log.Println("Error copying station xml file", err)
Expand Down Expand Up @@ -656,7 +656,7 @@ func (r *FDSNStationXML) doFilter(params []fdsnStationV1Search) bool {
// If this node meets at least one criteria, then we pass all the met criterion to next level.

func (n *NetworkType) doFilter(params []fdsnStationV1Search) bool {
n.TotalNumberStations = len(n.Station)
n.TotalNumberStations = CounterType(len(n.Station))
matchedParams := make([]fdsnStationV1Search, 0)
resultStations := make([]StationType, 0)

Expand Down Expand Up @@ -699,15 +699,15 @@ func (n *NetworkType) doFilter(params []fdsnStationV1Search) bool {
}
}

n.SelectedNumberStations = len(resultStations)
n.SelectedNumberStations = CounterType(len(resultStations))
n.Station = resultStations

// this node only valid if the children matches any query
return n.SelectedNumberStations > 0
}

func (s *StationType) doFilter(params []fdsnStationV1Search) bool {
s.TotalNumberChannels = len(s.Channel)
s.TotalNumberChannels = CounterType(len(s.Channel))
resultChannels := make([]ChannelType, 0)

matchedParams := make([]fdsnStationV1Search, 0)
Expand Down Expand Up @@ -835,39 +835,31 @@ func (v fdsnStationV1Search) validStartEnd(start, end time.Time, level int) bool
return true
}

func (v fdsnStationV1Search) validLatLng(latitude *LatitudeType, longitude *LongitudeType) bool {
if v.MinLatitude != math.MaxFloat64 && (latitude == nil || latitude.Value < v.MinLatitude) {
// request to check latitude:
// 1. this node doesn't have latitude -> check failed
// 2. the value fall outside the range -> check failed
// (similar logics apply for cases below)
func (v fdsnStationV1Search) validLatLng(latitude LatitudeType, longitude LongitudeType) bool {
if v.MinLatitude != math.MaxFloat64 && latitude.Value < v.MinLatitude {
return false
}

if v.MaxLatitude != math.MaxFloat64 && (latitude == nil || latitude.Value > v.MaxLatitude) {
if v.MaxLatitude != math.MaxFloat64 && latitude.Value > v.MaxLatitude {
return false
}

if v.MinLongitude != math.MaxFloat64 && (longitude == nil || longitude.Value < v.MinLongitude) {
if v.MinLongitude != math.MaxFloat64 && longitude.Value < v.MinLongitude {
return false
}

if v.MaxLongitude != math.MaxFloat64 && (longitude == nil || longitude.Value > v.MaxLongitude) {
if v.MaxLongitude != math.MaxFloat64 && longitude.Value > v.MaxLongitude {
return false
}

return true
}

func (v fdsnStationV1Search) validBounding(latitude *LatitudeType, longitude *LongitudeType) bool {
func (v fdsnStationV1Search) validBounding(latitude LatitudeType, longitude LongitudeType) bool {
if v.Latitude == math.MaxFloat64 {
// not using bounding circle
return true
}
if latitude == nil || longitude == nil {
// requested bounding circle, but this node doesn't have lat/lon
return false
}
d, _, err := wgs84.DistanceBearing(v.Latitude, v.Longitude, latitude.Value, longitude.Value)
if err != nil {
log.Printf("Error checking bounding:%s\n", err.Error())
Expand All @@ -887,28 +879,43 @@ func (v fdsnStationV1Search) validBounding(latitude *LatitudeType, longitude *Lo

// Download station XML from S3
func downloadStationXML(since time.Time) (by *bytes.Buffer, modified time.Time, err error) {
s3Client, err := s3.NewWithMaxRetries(100)
if err != nil {
return
}
var tp time.Time
by = bytes.NewBuffer(nil)

tp, err := s3Client.LastModified(s3Bucket, s3Meta, "")
if err != nil {
return
}
if stationXMLBucket != "" {
var s3Client s3.S3
s3Client, err = s3.NewWithMaxRetries(100)
if err != nil {
return
}

if !tp.After(since) {
return nil, zeroDateTime, errNotModified
}
tp, err = s3Client.LastModified(stationXMLBucket, stationXMLKey, "")
if err != nil {
return
}

log.Println("Downloading fdsn station xml file from S3: ", s3Bucket+"/"+s3Meta)
if !tp.After(since) {
return nil, zeroDateTime, errNotModified
}

by = bytes.NewBuffer(nil)
err = s3Client.Get(s3Bucket, s3Meta, "", by)
if err != nil {
return
}
log.Println("Downloading fdsn station xml file from S3: ", stationXMLBucket+"/"+stationXMLKey)

err = s3Client.Get(stationXMLBucket, stationXMLKey, "", by)
if err != nil {
return
}
} else {
// load from local to make debugging easier.
// s3Meta be the path to station xml
var f *os.File
f, err = os.Open(stationXMLKey)
if err != nil {
return
}
defer f.Close()
_, err = by.ReadFrom(f)
tp = time.Now()
}
modified = tp
log.Println("Download complete.")
return
Expand Down Expand Up @@ -977,7 +984,7 @@ func levelValue(level string) (int, error) {
case "response":
return STATION_LEVEL_RESPONSE, nil
default:
return -1, fmt.Errorf("Invalid level value.")
return -1, fmt.Errorf("invalid level value")
}
}

Expand Down Expand Up @@ -1020,14 +1027,13 @@ func (d xsdDateTime) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
return xml.Attr{Name: name, Value: string(t)}, nil
}

// For format=text
// For format=text - now outputs RFC3339Nano with Z timezone
func (d xsdDateTime) MarshalFormatText() string {
if time.Time(d).Equal(zeroDateTime) || time.Time(d).Equal(emptyDateTime) {
return ""
}

b, _ := d.MarshalText()
return string(b)
return time.Time(d).UTC().Format(time.RFC3339Nano)
}

func contains(slice []string, value string) bool {
Expand Down
13 changes: 8 additions & 5 deletions cmd/fdsn-ws/fdsn_station_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,9 @@ func TestStartEnd(t *testing.T) {
}

func TestFormatText(t *testing.T) {
setup(t)
defer teardown()

var e fdsnStationV1Search
var err error
var v url.Values = make(map[string][]string)
Expand All @@ -373,7 +376,7 @@ func TestFormatText(t *testing.T) {
c.doFilter([]fdsnStationV1Search{e})
b := c.marshalText(STATION_LEVEL_CHANNEL)
exp := `#Network | Station | Location | Channel | Latitude | Longitude | Elevation | Depth | Azimuth | Dip | SensorDescription | Scale | ScaleFreq | ScaleUnits | SampleRate | StartTime | EndTime
NZ|ARAZ|10|EHZ|-38.627690|176.120060|420.000000|0.000000|0.000000|-90.000000|Short Period Seismometer|74574725.120000|15.000000|m/s|100.000000|2011-06-20T04:00:01|
NZ|ARAZ|10|EHZ|-38.627690|176.120060|420.000000|0.000000|0.000000|-90.000000|Short Period Seismometer|74574725.120000|15.000000|m/s|100.000000|2011-06-20T04:00:01Z|
`
if b.String() != exp {
t.Errorf("Incorrect text result.")
Expand All @@ -388,7 +391,7 @@ NZ|ARAZ|10|EHZ|-38.627690|176.120060|420.000000|0.000000|0.000000|-90.000000|Sho
c.doFilter([]fdsnStationV1Search{e})
b = c.marshalText(STATION_LEVEL_NETWORK)
exp = `#Network | Description | StartTime | EndTime | TotalStations
NZ|New Zealand National Seismograph Network|1884-02-01T00:00:00||2
NZ|New Zealand National Seismograph Network|1884-02-01T00:00:00Z||2
`
if b.String() != exp {
t.Errorf("Incorrect text result.")
Expand All @@ -403,7 +406,7 @@ NZ|New Zealand National Seismograph Network|1884-02-01T00:00:00||2
c.doFilter([]fdsnStationV1Search{e})
b = c.marshalText(STATION_LEVEL_STATION)
exp = `#Network | Station | Latitude | Longitude | Elevation | SiteName | StartTime | EndTime
NZ|ARAZ|-38.627690|176.120060|420.000000|Aratiatia Landcorp Farm|2007-05-20T23:00:00|
NZ|ARAZ|-38.627690|176.120060|420.000000|Aratiatia Landcorp Farm|2007-05-20T23:00:00Z|
`
if b.String() != exp {
t.Errorf("Incorrect text result.")
Expand Down Expand Up @@ -457,8 +460,8 @@ NZ ARA* * EHE* 2001-01-01T00:00:00 *
NZ ARH? * EHN* 2001-01-01T00:00:00 *`
expected := strings.TrimSpace(`
#Network | Station | Latitude | Longitude | Elevation | SiteName | StartTime | EndTime
NZ|ARAZ|-38.627690|176.120060|420.000000|Aratiatia Landcorp Farm|2007-05-20T23:00:00|
NZ|ARHZ|-39.263100|176.995900|270.000000|Aropaoanui|2010-03-11T00:00:00|`)
NZ|ARAZ|-38.627690|176.120060|420.000000|Aratiatia Landcorp Farm|2007-05-20T23:00:00Z|
NZ|ARHZ|-39.263100|176.995900|270.000000|Aropaoanui|2010-03-11T00:00:00Z|`)

route := wt.Request{ID: wt.L(), URL: "/fdsnws/station/1/query", Method: "POST", PostBody: []byte(body), Content: "text/plain"}

Expand Down
Loading
Loading