Ejemplo n.º 1
0
// TestStationsProto creates a Protobuf file of Stations.
func TestStationsProto(t *testing.T) {
	var networks meta.NetworkList

	if err := meta.LoadList("../network/networks.csv", &networks); err != nil {
		t.Error(err)
	}

	var net = make(map[string]*delta.Network)

	for _, v := range networks {
		n := delta.Network{
			Code:        v.Code,
			External:    v.External,
			Description: v.Description,
			Restricted:  v.Restricted,
		}

		net[v.Code] = &n
	}

	var stations meta.StationList
	if err := meta.LoadList("../network/stations.csv", &stations); err != nil {
		t.Error(err)
	}

	if len(stations) == 0 {
		t.Error("zero length stations list.")
	}

	var s delta.Stations
	s.Stations = make(map[string]*delta.Station)

	for _, v := range stations {
		pt := delta.Point{
			Longitude: v.Longitude,
			Latitude:  v.Latitude,
			Elevation: v.Elevation,
			Datum:     v.Datum,
		}

		sp := delta.Span{
			Start: v.Start.Unix(),
			End:   v.End.Unix(),
		}

		st := delta.Station{
			Code:    v.Code,
			Name:    v.Name,
			Network: net[v.Network],
			Point:   &pt,
			Span:    &sp,
		}

		s.Stations[st.Code] = &st
	}

	b, err := proto.Marshal(&s)
	if err != nil {
		t.Error(err)
	}

	t.Log(s)

	if err := os.MkdirAll(apiDir, 0777); err != nil {
		t.Error(err)
	}

	if err := ioutil.WriteFile(apiDir+"/stations.pb", b, 0644); err != nil {
		t.Error(err)
	}
}
Ejemplo n.º 2
0
// TestStationsProto creates:
// Protobuf file of Stations
// JSON file of Stations
// GeoJSON of Site for each sensor type.
func TestStationsProto(t *testing.T) {
	var networks meta.NetworkList

	if err := meta.LoadList("../network/networks.csv", &networks); err != nil {
		t.Error(err)
	}

	var net = make(map[string]*delta.Network)

	for _, v := range networks {
		n := delta.Network{
			Code:        v.Code,
			External:    v.External,
			Description: v.Description,
			Restricted:  v.Restricted,
		}

		net[v.Code] = &n
	}

	var stations meta.StationList
	if err := meta.LoadList("../network/stations.csv", &stations); err != nil {
		t.Error(err)
	}

	if len(stations) == 0 {
		t.Error("zero length stations list.")
	}

	var s delta.Stations
	s.Stations = make(map[string]*delta.Station)

	for _, v := range stations {
		pt := delta.Point{
			Longitude: v.Longitude,
			Latitude:  v.Latitude,
			Elevation: v.Elevation,
			Datum:     v.Datum,
		}

		sp := delta.Span{
			Start: v.Start.Unix(),
			End:   v.End.Unix(),
		}

		st := delta.Station{
			Code:    v.Code,
			Name:    v.Name,
			Network: net[v.Network],
			Point:   &pt,
			Span:    &sp,
		}

		s.Stations[st.Code] = &st
	}

	var sites meta.SiteList
	if err := meta.LoadList("../network/sites.csv", &sites); err != nil {
		t.Error(err)
	}

	for _, v := range sites {
		pt := delta.Point{
			Longitude: v.Longitude,
			Latitude:  v.Latitude,
			Elevation: v.Elevation,
			Datum:     v.Datum,
		}
		sp := delta.Span{
			Start: v.Start.Unix(),
			End:   v.End.Unix(),
		}
		si := delta.Site{
			Location: v.Location,
			Survey:   v.Survey,
			Span:     &sp,
			Point:    &pt,
		}

		if _, ok := s.Stations[v.Station]; ok {
			if s.Stations[v.Station].Sites == nil {
				s.Stations[v.Station].Sites = make(map[string]*delta.Site)
			}
			s.Stations[v.Station].Sites[si.Location] = &si
		}
	}

	var installed meta.InstalledSensorList
	if err := meta.LoadList("../install/sensors.csv", &installed); err != nil {
		t.Fatal(err)
	}

	for _, v := range installed {
		st := resp.SensorModels[v.Model]

		e := delta.Equipment{
			Make:   v.Make,
			Model:  v.Model,
			Serial: v.Serial,
			Type:   st.Type,
		}
		sp := delta.Span{
			Start: v.Start.Unix(),
			End:   v.End.Unix(),
		}
		o := delta.Orientation{
			Dip:     v.Dip,
			Azimuth: v.Azimuth,
		}
		off := delta.Offset{
			North:    v.North,
			East:     v.East,
			Vertical: v.Vertical,
		}
		sc := delta.Scale{
			Bias:   v.Bias,
			Factor: v.Factor,
		}
		is := delta.InstalledSensor{
			Equipment:   &e,
			Span:        &sp,
			Orientation: &o,
			Offset:      &off,
			Scale:       &sc,
		}

		if _, ok := s.Stations[v.Station]; ok {
			if _, ok := s.Stations[v.Station].Sites[v.Location]; ok {
				s.Stations[v.Station].Sites[v.Location].InstalledSensor = append(s.Stations[v.Station].Sites[v.Location].InstalledSensor, &is)
			}
		}
	}

	// strong motion recorders that are a sensor and a datalogger as a package.
	var recorders meta.InstalledRecorderList
	if err := meta.LoadList("../install/recorders.csv", &recorders); err != nil {
		t.Fatal(err)
	}

	for _, v := range recorders {
		st, ok := resp.SensorModels[v.InstalledSensor.Model]
		if !ok {
			// in resp/auto.go some models have " SENSOR" appended.
			st, ok = resp.SensorModels[v.InstalledSensor.Model+" SENSOR"]
		}

		e := delta.Equipment{
			Make:   v.Make,
			Model:  v.Model,
			Serial: v.Serial,
			Type:   st.Type,
		}
		sp := delta.Span{
			Start: v.Start.Unix(),
			End:   v.End.Unix(),
		}
		o := delta.Orientation{
			Dip:     v.Dip,
			Azimuth: v.Azimuth,
		}
		off := delta.Offset{
			North:    v.North,
			East:     v.East,
			Vertical: v.Vertical,
		}
		sc := delta.Scale{
			Bias:   v.Bias,
			Factor: v.Factor,
		}
		is := delta.InstalledSensor{
			Equipment:   &e,
			Span:        &sp,
			Orientation: &o,
			Offset:      &off,
			Scale:       &sc,
		}

		if _, ok := s.Stations[v.Station]; ok {
			if _, ok := s.Stations[v.Station].Sites[v.Location]; ok {
				s.Stations[v.Station].Sites[v.Location].InstalledSensor = append(s.Stations[v.Station].Sites[v.Location].InstalledSensor, &is)
			}
		}
	}

	// protobuf of all station information

	b, err := proto.Marshal(&s)
	if err != nil {
		t.Error(err)
	}

	if err := os.MkdirAll(apiDir, 0777); err != nil {
		t.Error(err)
	}

	if err := ioutil.WriteFile(apiDir+"/stations.pb", b, 0644); err != nil {
		t.Error(err)
	}

	// json of all station information

	b, err = json.Marshal(&s)
	if err != nil {
		t.Error(err)
	}

	if err := os.MkdirAll(apiDir, 0777); err != nil {
		t.Error(err)
	}

	if err := ioutil.WriteFile(apiDir+"/stations.json", b, 0644); err != nil {
		t.Error(err)
	}

	// GeoJSON files of site for each sensor type.

	var out = make(map[string]*bytes.Buffer)

	for _, v := range resp.SensorModels {
		if v.Type != "" {
			out[v.Type] = &bytes.Buffer{}
		}
	}

	for k := range out {
		out[k].WriteString(`{"type": "FeatureCollection","features": [`)
	}

	for _, station := range s.Stations {
		if station.GetSpan() == nil || station.GetPoint() == nil || station.GetNetwork() == nil || station.Network.Restricted {
			continue
		}
		if station.Network.External != "NZ" || station.Network.Code == "XX" {
			continue
		}

		for _, site := range station.Sites {
			for _, v := range site.InstalledSensor {
				_, ok := out[v.Equipment.Type]

				switch {
				case v.Equipment.Type == "":
					t.Logf("%s.%s no sensor model for %s, site not classified.",
						station.Code, site.Location, v.Equipment.Model)
				case ok:
					writeProps(station, site, v, out[v.Equipment.Type])
				case !ok:
					t.Logf("unclassified type %s", v.Equipment.Type)
				}
			}
		}
	}

	for k := range out {
		out[k].WriteString(`]}`)

		fn := strings.Replace(strings.ToLower(k), " ", "", -1)
		fn = apiDir + "/" + fn + ".geojson"

		if err := ioutil.WriteFile(fn, out[k].Bytes(), 0644); err != nil {
			t.Error(err)
		}
	}
}