Skip to content

Commit

Permalink
Fix Readme bug.
Browse files Browse the repository at this point in the history
Replace the SGP4 library
  • Loading branch information
shibingli committed Aug 10, 2022
1 parent 7c684da commit 50fd6f8
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 33 deletions.
34 changes: 4 additions & 30 deletions entity/gp.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package entity

import (
"github.com/SharkEzz/sgp4"
"strings"
"github.com/shibingli/spacetrack/internal/satellite"
"time"
)

type GP struct {
Expand Down Expand Up @@ -48,32 +48,6 @@ type GP struct {
TLELine2 string `json:"TLE_LINE2,omitempty" xml:"TLE_LINE2,omitempty" bson:"tle_line_2,omitempty"`
}

func (g *GP) TLE() (*sgp4.TLE, error) {
name := g.TLELine0
names := strings.Fields(name)

if len(names) > 1 {
name = strings.Join(names[1:], " ")
}

tle, err := sgp4.NewTLE(name, g.TLELine1, g.TLELine2)
if err != nil {
return nil, err
}

return tle, nil
}

func (g *GP) SGP4() (*sgp4.SGP4, error) {
tle, err := g.TLE()
if err != nil {
return nil, err
}

sgp, err := sgp4.NewSGP4(tle)
if err != nil {
return nil, err
}

return sgp, nil
func (g *GP) SGP4Position(utcTime time.Time) *satellite.LLA {
return satellite.LatLonAlt(g.TLELine1, g.TLELine2, utcTime)
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ module github.com/shibingli/spacetrack

go 1.18

require github.com/SharkEzz/sgp4 v1.0.1
require github.com/joshuaferrara/go-satellite v0.0.0-20220611180459-512638c64e5b
require github.com/pkg/errors v0.9.1 // indirect
12 changes: 10 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
github.com/SharkEzz/sgp4 v1.0.1 h1:Ws8mAUpvSzZ3ocbacb9FaWoqDYh5wj6xIjHARhXXVzY=
github.com/SharkEzz/sgp4 v1.0.1/go.mod h1:Ij16kwC0HZob8vkFmHb5YjVyu2BmyUflL0RFpNim0dQ=
github.com/joshuaferrara/go-satellite v0.0.0-20220611180459-512638c64e5b h1:JlltDRgni6FuoFwluvoZCrE6cmpojccO4WsqeYlFJLE=
github.com/joshuaferrara/go-satellite v0.0.0-20220611180459-512638c64e5b/go.mod h1:msW2QeN9IsnRyvuK8OBAzBwn6DHwXpiAiqBk8dbLfrU=
github.com/onsi/ginkgo v1.2.1-0.20160509182050-5437a97bf824 h1:MbMqwlWoESqhGm4Sslfdyeq7Ww8R9ppeKS5DcO3xDI0=
github.com/onsi/ginkgo v1.2.1-0.20160509182050-5437a97bf824/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v0.0.0-20160516222431-c73e51675ad2 h1:38zSYUaJJkzreBjLz7tx4AUTVjnFI7EQBnlRoWt4QFA=
github.com/onsi/gomega v0.0.0-20160516222431-c73e51675ad2/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
gopkg.in/yaml.v2 v2.0.0-20160301204022-a83829b6f129 h1:RBgb9aPUbZ9nu66ecQNIBNsA7j3mB5h8PNDIfhPjaJg=
gopkg.in/yaml.v2 v2.0.0-20160301204022-a83829b6f129/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
58 changes: 58 additions & 0 deletions internal/satellite/satellite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package satellite

import (
goSatellite "github.com/joshuaferrara/go-satellite"
"time"
)

func ParseTLE(line1, line2 string) (sat goSatellite.Satellite) {
return goSatellite.TLEToSat(line1, line2, goSatellite.GravityWGS84)
}

func Propagate(sat goSatellite.Satellite, t time.Time) (position, velocity goSatellite.Vector3) {
return goSatellite.Propagate(sat, t.Year(), int(t.Month()), t.Day(), t.Hour(), t.Minute(), t.Second())
}

func TLEPropagate(line1, line2 string, t time.Time) (position, velocity goSatellite.Vector3) {
tle := ParseTLE(line1, line2)
return Propagate(tle, t)
}

func ECIToECEF(eciCoords goSatellite.Vector3, t time.Time) (ecfCoords goSatellite.Vector3) {
return goSatellite.ECIToECEF(eciCoords, GSTimeFromDate(t))
}

func ECIToLLA(eciCoords goSatellite.Vector3, t time.Time) (altitude, velocity float64, ret goSatellite.LatLong) {
return goSatellite.ECIToLLA(eciCoords, GSTimeFromDate(t))
}

func LatLongDeg(rat goSatellite.LatLong) (deg goSatellite.LatLong) {
return goSatellite.LatLongDeg(rat)
}

type LLA struct {
Latitude float64
Longitude float64
Altitude float64
}

func LatLonAlt(line1, line2 string, t time.Time) (deg *LLA) {

pos, _ := TLEPropagate(line1, line2, t)

alt, _, ret := ECIToLLA(pos, t)

latLong := LatLongDeg(ret)

lla := &LLA{
Latitude: latLong.Latitude,
Longitude: latLong.Longitude,
Altitude: alt,
}

return lla
}

func GSTimeFromDate(t time.Time) float64 {
return goSatellite.GSTimeFromDate(t.Year(), int(t.Month()), t.Day(), t.Hour(), t.Minute(), t.Second())
}

0 comments on commit 50fd6f8

Please sign in to comment.