// Unmarshal unmarshals the SeisComPML in b and initialises all
// the objects referenced by ID in the SeisComPML e.g., PreferredOrigin,
// PreferredMagnitude etc.
//
// Support SC3ML versions are 0.7, 0.8, 0.9
// Any other versions will result in a error.
Unmarshal unmarshals the SeisComPML in b and initialises all
the objects referenced by ID in the SeisComPML e.g., PreferredOrigin,
PreferredMagnitude etc.

Support SC3ML versions are 0.7, 0.8, 0.9
Any other versions will result in a error.
// Unmarshal unmarshals the SeisComPML in b and initialises all // the objects referenced by ID in the SeisComPML e . g . PreferredOrigin // PreferredMagnitude etc . // // Support SC3ML versions are 0 . 7 0 . 8 0 . 9 // Any other versions will result in a error .
func Unmarshal(b []byte, s *Seiscomp) error {
	switch {
	case bytes.Contains(b, sc3ml07):
	case bytes.Contains(b, sc3ml08):
	case bytes.Contains(b, sc3ml09):
	default:
		return errors.New("unsupported SC3ML version.")
	}
	if err := xml.Unmarshal(b, s); err != nil {
		return err
	}
	var picks = make(map[string]Pick)
	for k, v := range s.EventParameters.Picks {
		picks[v.PublicID] = s.EventParameters.Picks[k]
	}
	var arrivals = make(map[string]Arrival)
	for i := range s.EventParameters.Origins {
		for _, v := range s.EventParameters.Origins[i].Arrivals {
			arrivals[v.PickID] = v
		}
	}
	var amplitudes = make(map[string]Amplitude)
	for k, v := range s.EventParameters.Amplitudes {
		a := s.EventParameters.Amplitudes[k]
		pk := arrivals[v.PickID]
		a.Distance = pk.Distance
		a.Azimuth = pk.Azimuth
		amplitudes[v.PublicID] = a
	}
	for i := range s.EventParameters.Origins {
		for k, v := range s.EventParameters.Origins[i].Arrivals {
			s.EventParameters.Origins[i].Arrivals[k].Pick = picks[v.PickID]
		}
		var stationMagnitudes = make(map[string]StationMagnitude)
		for k, v := range s.EventParameters.Origins[i].StationMagnitudes {
			s.EventParameters.Origins[i].StationMagnitudes[k].Amplitude = amplitudes[v.AmplitudeID]
			stationMagnitudes[v.PublicID] = s.EventParameters.Origins[i].StationMagnitudes[k]
		}
		for j := range s.EventParameters.Origins[i].Magnitudes {
			for k, v := range s.EventParameters.Origins[i].Magnitudes[j].StationMagnitudeContributions {
				s.EventParameters.Origins[i].Magnitudes[j].StationMagnitudeContributions[k].StationMagnitude = stationMagnitudes[v.StationMagnitudeID]
			}
		}
	}
	for i := range s.EventParameters.Events {
		for k, v := range s.EventParameters.Origins {
			if v.PublicID == s.EventParameters.Events[i].PreferredOriginID {
				s.EventParameters.Events[i].PreferredOrigin = s.EventParameters.Origins[k]
			}
			for _, mag := range v.Magnitudes {
				if mag.PublicID == s.EventParameters.Events[i].PreferredMagnitudeID {
					s.EventParameters.Events[i].PreferredMagnitude = mag
				}
			}
		}
	}
	if len(s.EventParameters.Events) != 1 {
		return nil
	}
	var by bytes.Buffer
	by.Write(b)
	d := xml.NewDecoder(&by)
	var tk xml.Token
	var err error
	for {
		tk, err = d.Token()
		if tk == nil {
			break
		}
		if err != nil {
			return err
		}
		switch se := tk.(type) {
		case xml.StartElement:
			if se.Name.Local == "creationInfo" {
				var c CreationInfo
				err = d.DecodeElement(&c, &se)
				if err != nil {
					return err
				}
				if c.ModificationTime.After(s.EventParameters.Events[0].ModificationTime) {
					s.EventParameters.Events[0].ModificationTime = c.ModificationTime
				}
				if c.CreationTime.After(s.EventParameters.Events[0].ModificationTime) {
					s.EventParameters.Events[0].ModificationTime = c.CreationTime
				}
			}
		}
	}
	return nil
}
func Unmarshal ( b [ ] byte , s * Seiscomp ) error { switch { case bytes . Contains ( b , sc3ml07 ) : case bytes . Contains ( b , sc3ml08 ) : case bytes . Contains ( b , sc3ml09 ) : default : return errors . New ( " " ) } if err := xml . Unmarshal ( b , s ) ; err != nil { return err } var picks = make ( map [ string ] Pick ) for k , v := range s . EventParameters . Picks { picks [ v . PublicID ] = s . EventParameters . Picks [ k ] } var arrivals = make ( map [ string ] Arrival ) for i := range s . EventParameters . Origins { for _ , v := range s . EventParameters . Origins [ i ] . Arrivals { arrivals [ v . PickID ] = v } } var amplitudes = make ( map [ string ] Amplitude ) for k , v := range s . EventParameters . Amplitudes { a := s . EventParameters . Amplitudes [ k ] pk := arrivals [ v . PickID ] a . Distance = pk . Distance a . Azimuth = pk . Azimuth amplitudes [ v . PublicID ] = a } for i := range s . EventParameters . Origins { for k , v := range s . EventParameters . Origins [ i ] . Arrivals { s . EventParameters . Origins [ i ] . Arrivals [ k ] . Pick = picks [ v . PickID ] } var stationMagnitudes = make ( map [ string ] StationMagnitude ) for k , v := range s . EventParameters . Origins [ i ] . StationMagnitudes { s . EventParameters . Origins [ i ] . StationMagnitudes [ k ] . Amplitude = amplitudes [ v . AmplitudeID ] stationMagnitudes [ v . PublicID ] = s . EventParameters . Origins [ i ] . StationMagnitudes [ k ] } for j := range s . EventParameters . Origins [ i ] . Magnitudes { for k , v := range s . EventParameters . Origins [ i ] . Magnitudes [ j ] . StationMagnitudeContributions { s . EventParameters . Origins [ i ] . Magnitudes [ j ] . StationMagnitudeContributions [ k ] . StationMagnitude = stationMagnitudes [ v . StationMagnitudeID ] } } } for i := range s . EventParameters . Events { for k , v := range s . EventParameters . Origins { if v . PublicID == s . EventParameters . Events [ i ] . PreferredOriginID { s . EventParameters . Events [ i ] . PreferredOrigin = s . EventParameters . Origins [ k ] } for _ , mag := range v . Magnitudes { if mag . PublicID == s . EventParameters . Events [ i ] . PreferredMagnitudeID { s . EventParameters . Events [ i ] . PreferredMagnitude = mag } } } } if len ( s . EventParameters . Events ) != 1 { return nil } var by bytes . Buffer by . Write ( b ) d := xml . NewDecoder ( & by ) var tk xml . Token var err error for { tk , err = d . Token ( ) if tk == nil { break } if err != nil { return err } switch se := tk . ( type ) { case xml . StartElement : if se . Name . Local == " " { var c CreationInfo err = d . DecodeElement ( & c , & se ) if err != nil { return err } if c . ModificationTime . After ( s . EventParameters . Events [ 0 ] . ModificationTime ) { s . EventParameters . Events [ 0 ] . ModificationTime = c . ModificationTime } if c . CreationTime . After ( s . EventParameters . Events [ 0 ] . ModificationTime ) { s . EventParameters . Events [ 0 ] . ModificationTime = c . CreationTime } } } } return nil }
Unmarshal unmarshals the SeisComPML in b and initialises all
the objects referenced by ID in the SeisComPML e.g., PreferredOrigin,
PreferredMagnitude etc.
Unmarshal unmarshals the SeisComPML in b and initialises all the objects referenced by ID in the SeisComPML e . g . PreferredOrigin PreferredMagnitude etc .
// add distance and azimuth from the arrival with the matching PickID. // set the preferred origin. // set the preferred mag which can come from any origin // set the most recent modified time as long as there is only one event. // XML token parse the entire SC3ML, looking for creationInfo. // assumes all objects in the SC3ML are associated, or have been associated, // with the event somehow. // Read tokens from the XML document in a stream.