generated from ut-issl/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBatchEstimator.py
1182 lines (948 loc) · 45.5 KB
/
BatchEstimator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""@brief Python code that reads GPS dataset and calculates TLE """
##
# @mainpage Batch estimate from GPS to calculate TLE
#
# Author: Joshua Critchley-Marrows (ISSL/USYD) 2023-03-01
# Modified: Joshua Critchley-Marrows (ISSL/USYD) 2023-03-01
# Imports
import numpy as np
import pandas as pd
from numpy import float64
import pyatmos
import spiceypy as spice
from pyatmos.jb2008.spaceweather import read_sw_jb2008
import numdifftools as nd
import scipy as sp
import pyshtools as pysh
from sgp4.api import Satrec
from sgp4.conveniences import jday_datetime
from astropy.coordinates.builtin_frames.utils import get_polar_motion
from astropy.time import Time
# Global Constants
DEBUG = 1 # mode of operation (0 = normal, 1 = debug)
MAX_OBS_NUM = 50000 # maximum number of GPS observations
R_EARTH = 6378137 # Earth radius [m]
GRAV = 6.6743e-11 # gravitational constant [m3/kg/s2]
M_EARTH = 5.972e24 # mass of Earth [kg]
F_EARTH = 0.00335279499764807 # Earth flattening constant
MIN_VIS_SATS = 5 # minimum number of visible GPS satellites to include measurment
GPS_LEAP_SECONDS = 18 # number of leap seconds
SIGMA_GPS = 10 # GPS receiver noise
INPUT_MULIPLIER = 4.5 # data editing input multiplier
USE_DATA_EDITING = 0 # data editing setting (0 = off, 1 = on)
NSTATES = 3
class BatchEstimator:
def __init__(self):
"""!@brief Initializes the program."""
# Load the SPICE kernels
spice.furnsh('batch_estimator.tm')
# Update celestrack file
# self.swfile = pyatmos.download_sw_jb2008()
# Assume BSTAR
self.bstar = 0.59575e-3 # TLE bstar [R_EARTH-1]
# Read spherical harmonic file
self.clm = pysh.datasets.Earth.EGM2008(2)
self.nstates = NSTATES
if DEBUG:
print("Initializing program.")
def read_data_header(self,line):
"""!@brief Reads data header and returns to the list of parameters
[time,GPS_TIME_week,GPS_TIME_msec,TLM_RECEIVED_TI,VISIBLE_SAT,
POS_ECEF_X_m,POS_ECEF_Y_m,POS_ECEF_Z_m,VEL_ECEF_X_M/s,
VEL_ECEF_Y_m/s,VEL_ECEF_Z_m/s]
@param line line containing the GPS data header
@return header locations
"""
header = line.split(',')
header_loc = np.empty(11,dtype=int)
header_loc.fill(-1)
for i in range(len(header)):
if header[i] == 'Time':
header_loc[0] = i
elif header[i] == 'ORBIT.COMPO.GPS_R.GPS_TIME_week':
header_loc[1] = i
elif header[i] == 'ORBIT.COMPO.GPS_R.GPS_TIME_msec':
header_loc[2] = i
elif header[i] == 'ORBIT.COMPO.GPS_R.TLM_RECEIVED_TI':
header_loc[3] = i
elif header[i] == 'ORBIT.COMPO.GPS_R.VISIBLE_SAT':
header_loc[4] = i
elif header[i] == 'ORBIT.COMPO.GPS_R.POS_ECEF_m.X':
header_loc[5] = i
elif header[i] == 'ORBIT.COMPO.GPS_R.POS_ECEF_m.Y':
header_loc[6] = i
elif header[i] == 'ORBIT.COMPO.GPS_R.POS_ECEF_m.Z':
header_loc[7] = i
elif header[i] == 'ORBIT.COMPO.GPS_R.VEL_ECEF_m_s.X':
header_loc[8] = i
elif header[i] == 'ORBIT.COMPO.GPS_R.VEL_ECEF_m_s.Y':
header_loc[9] = i
elif header[i] == 'ORBIT.COMPO.GPS_R.VEL_ECEF_m_s.Z':
header_loc[10] = i
return header_loc
def read_gps_data(self,filename):
"""!@brief Reads the GPS data set and retrieves GPS-related data.
Assigns to arrays
@param filename File path to data set
@return Read success (1) or failure (0)
"""
# Set constants
GPS_EPOCH = pd.Timestamp('1980-01-06 00:00:00.0000',unit='ns',tz='utc')
# Open the gps data file
gps_data = open(filename)
data_set = gps_data.readline()
# Check the header locations
header_loc = self.read_data_header(data_set)
while np.sum(header_loc) == -11:
data_set = gps_data.readline()
header_loc = self.read_data_header(data_set)
if ~data_set:
return 0
# Assign GPS dataset arrays
self.rcv_time = np.empty((MAX_OBS_NUM,),dtype=object)
self.gps_week = np.empty(MAX_OBS_NUM,dtype=float64)
self.gps_msec = np.empty(MAX_OBS_NUM,dtype=float64)
self.tlm_received = np.empty(MAX_OBS_NUM,dtype=float64)
self.nsats = np.empty(MAX_OBS_NUM,dtype=int)
self.states = np.empty((6,MAX_OBS_NUM),dtype=float64)
self.obs_num = 0
# Read GPS datasets from each line
data_set = gps_data.readline()
while data_set:
data_set = data_set.split(',')
# Check GPS dataset validity
if int(float(data_set[header_loc[4]])) < MIN_VIS_SATS or \
int(float(data_set[header_loc[4]])) > 64 or \
int(float(data_set[header_loc[1]])) > 3000 or \
int(float(data_set[header_loc[2]])) > 6.048e8:
data_set = gps_data.readline()
continue
# Check telemetry validity
if self.obs_num != 0:
if (np.float64(data_set[header_loc[3]]) - self.tlm_received[self.obs_num-1]) > 1E7:
self.obs_num = self.obs_num - 1
data_set = gps_data.readline()
continue
# Time of observation
self.rcv_time[self.obs_num] = data_set[header_loc[0]]
# GPS Time of Week
self.gps_week[self.obs_num] = data_set[header_loc[1]]
# GPS Milliseconds
self.gps_msec[self.obs_num] = data_set[header_loc[2]]
# GPS Time Telemetry Receiver
self.tlm_received[self.obs_num] = data_set[header_loc[3]]
# GPS Number of Visible Satellites
self.nsats[self.obs_num] = int(float(data_set[header_loc[4]]))
# GPS Positions in ECEF [m]
self.states[0,self.obs_num] = data_set[header_loc[5]]
self.states[1,self.obs_num] = data_set[header_loc[6]]
self.states[2,self.obs_num] = data_set[header_loc[7]]
# GPS Velocities in ECEF [m/s]
self.states[3,self.obs_num] = data_set[header_loc[8]]
self.states[4,self.obs_num] = data_set[header_loc[9]]
self.states[5,self.obs_num] = data_set[header_loc[10]]
# Move to the next line
self.obs_num = self.obs_num + 1
data_set = gps_data.readline()
if self.obs_num == 0: return 0
# Reduce data sets to minimum number
self.rcv_time = self.rcv_time[:self.obs_num]
self.rcv_time = pd.to_datetime(self.rcv_time,yearfirst=True,unit='ns',utc=True)
self.gps_week = self.gps_week[:self.obs_num]
self.gps_msec = self.gps_msec[:self.obs_num]
self.tlm_received = self.tlm_received[:self.obs_num]
self.nsats = self.nsats[:self.obs_num]
self.states = self.states[:,:self.obs_num]
# Calculate gps weeks since epoch
timeSinceGpsEpoch = (self.rcv_time[0] - GPS_EPOCH)
weekPeriod = np.floor(timeSinceGpsEpoch.days / 7 / 2048)
if weekPeriod*2048 > self.gps_week[0]:
self.gps_week = self.gps_week + weekPeriod*2048
# Convert GPS week and msec to UTC measurement time
self.time = GPS_EPOCH + pd.to_timedelta(self.gps_week,unit='W') + \
pd.to_timedelta(self.gps_msec,unit='ms') - \
pd.to_timedelta(GPS_LEAP_SECONDS,unit='s')
return 1
def earth_gravity(self,time,r,model):
"""!@brief Determines the Earth's gravitational force and Jacobian matrix
@para time Time value to derive the desired values (datetime)
@para r Position of the satellite in ECI (x,y,z) [m]
@para model Treat Earth using spherical harmonics ('spherical') or as a point mass
@return a Earth gravitation acceleration (a_x,a_y,a_z) [m/s2]
@return dadr Earth gravitation Jacobian in terms of position ((3,3))
"""
J2 = 0.001082 # J2 perturbation non-dimensional
J2 = J2*R_EARTH**2*GRAV*M_EARTH
# Calculate distance
rr = np.linalg.norm(r)
if 'spherical' == model:
# Transform to fixed frame
et = spice.datetime2et(time)
rot = spice.pxform('J2000','ITRF93',et)
r_f = rot@r
long,lat,_ = spice.recgeo(r_f,R_EARTH,F_EARTH)
# Calculate gravitational acceleration
a_sph = pysh.gravmag.MakeGravGridPoint(self.clm.coeffs,self.clm.gm,self.clm.r0,\
rr,lat*180/np.pi,long*180/np.pi)
a = np.array((a_sph[0]*np.sin(a_sph[1])*np.cos(a_sph[2]),
a_sph[0]*np.sin(a_sph[1])*np.sin(a_sph[2]),
a_sph[0]*np.cos(a_sph[1])))
dcm = [[-np.sin(lat)*np.cos(long), -np.sin(long), -np.cos(lat)*np.cos(long)],
[-np.sin(lat)*np.sin(long), np.cos(long), -np.cos(lat)*np.sin(long)],
[np.cos(lat), 0, -np.sin(lat)]]
a = -rot.T@dcm@a
# Calculate Jacobian
dadr = self.spherical_gravity_jacobian(r_f)
elif 'J2' == model:
# Calculate terms
x = r[0]; y = r[1]; z = r[2]
term0 = 6*z**2 - 3/2*(x**2 + y**2)
term1 = 3*z**2 - 9/2*(x**2 + y**2)
# Calculate acceleration
a = -GRAV*M_EARTH*r/rr**3
a[0] = a[0] + J2*x/rr**7*term0
a[1] = a[1] + J2*y/rr**7*term0
a[2] = a[2] + J2*z/rr**7*term1
# Calculate Jacobian
dadr = -GRAV*M_EARTH*(np.ones((3,3))/rr**3 -\
3*r.reshape((3,1))@r.reshape((3,1)).T/rr**5)
dadr[0,0] = dadr[0,0] + J2*(rr**2-7*x**2)/rr**9*term0 - 3*J2*x**2/rr**7
dadr[0,1] = dadr[0,1] - 7*J2*x*y/rr**9*term0 - 3*J2*x*y/rr**7
dadr[0,2] = dadr[0,2] - 7*J2*x*z/rr**9*term0 + 12*J2*x*z/rr**7
dadr[1,0] = dadr[1,0] - 7*J2*x*y/rr**9*term0 - 3*J2*x*y/rr**7
dadr[1,1] = dadr[1,1] + J2*(rr**2-7*y**2)/rr**9*term0 - 3*J2*y**2/rr**7
dadr[1,2] = dadr[1,2] - 7*J2*y*z/rr**9*term0 + 12*J2*y*z/rr**7
dadr[2,0] = dadr[2,0] - 7*J2*x*z/rr**9*term1 - 9*J2*x*z/rr**7
dadr[2,1] = dadr[2,1] - 7*J2*y*z/rr**9*term1 - 9*J2*y*z/rr**7
dadr[2,2] = dadr[2,2] + J2*(rr**2-7*z**2)/rr**9*term1 + 6*J2*z**2/rr**7
return a,dadr
else:
# Calculate acceleration
a = -GRAV*M_EARTH*r/rr**3
# Calculate Jacobian
dadr = -GRAV*M_EARTH*(np.ones((3,3))/rr**3 -\
3*r.reshape((3,1))@r.reshape((3,1)).T/rr**5)
return a,dadr
def find_atmospheric_density(self,time,r,model):
"""!@brief Finds the atmospheric density at a certain position
@para time Time value to derive the desired values (datetime)
@para r Position of the satellite in ECI (x,y,z) [m]
@para model Use either JB2008 ('jb2008') or exponential model
@return rho Atmospheric density [kg/m3]
"""
# Calculate transformation matrix
et = spice.datetime2et(time)
rot = spice.pxform('J2000','ITRF93',et)
r = rot@r
# Calculate lat, long, alt
long,lat,alt = spice.recgeo(r.reshape(-1),R_EARTH,F_EARTH)
lat = lat*180/np.pi; long = long*180/np.pi; alt = alt/1000
# Estimate atmospheric density (assume spherical Earth) (use exponential model)
if model == 'jb2008':
swdata = read_sw_jb2008(self.swfile)
jb08 = pyatmos.jb2008(time,(lat,long,alt),swdata)
rho = jb08.rho
else:
expom = pyatmos.expo(alt)
rho = expom.rho
return rho
def atmospheric_drag(self,time,r,v,model):
"""!@brief Determines the Earth's atmospheric drag and Jacobians
@para time Time value to derive the desired values (datetime)
@para r Position of the satellite in ECI (x,y,z) [m]
@para v Velocity of the atellite in ECI (v_x,v_y,v_z) [m]
@para model Treat Earth using spherical harmonics ('spherical') or as a point mass
@return a Earth drag acceleration (a_x,a_y,a_z) [m/s2]
@return dadr Earth drag Jacobian in terms of position ((3,3))
@return dadv Earth drag Jacobian in terms of velocity ((3,3))
"""
# Define constants
RHO_0 = 0.1570 # reference air density [kg/m2/R_EARTH]
if model == 'ignore':
return np.zeros(3),np.zeros((3,3)), np.zeros((3,3))
# Calculate gravitational density
rho = self.find_atmospheric_density(time,r.reshape((3,1)),model)
# Relative velocity of spacecraft relative to Earth atmosphere (assume relative to
# Earth rotation)
et = spice.datetime2et(time)
rot = spice.sxform('ITRF93','J2000',et)
_,omega = spice.xf2rav(rot)
v_r = v - np.cross(omega,r)
# Calculate atmospheric drag
a = -rho/(RHO_0*R_EARTH)*self.bstar*v_r*np.linalg.norm(v_r)
# Estimate drag derivative
fun = lambda rr: self.find_atmospheric_density(time,rr,'expo')
drhodr = nd.Jacobian(fun)
drhodr = drhodr(r.reshape((3,1)))
# Calculate Jacobian
X = [[0.,-omega[2],omega[1]],
[omega[2],0.,-omega[0]],
[-omega[1],omega[0],0.]]
v_r = v_r.reshape((3,1))
dadv = -rho/(RHO_0*R_EARTH)*(v_r@v_r.T/np.linalg.norm(v_r) + \
np.linalg.norm(v_r)*np.ones((3,1)))
dadr = -rho/(RHO_0*R_EARTH)*np.linalg.norm(v_r)*v_r@drhodr - \
dadv@X
return a,dadr,dadv
def estimate_batch_orbit(self,model_atmos,model_grav):
"""!@brief Calculates the batch orbit estimates using SVD from a set of position and
velocity measurements
Reference:
Gill, E., Montenbruck, O. (2000). Satellite orbits: models,
methods, and applications. Germany: Springer Berlin Heidelberg.
@param model_atmos atmospheric model setting for atmospheric density ('expo','j2008','ignore')
@param model_grav gravitational potential setting ('j2','spherical','point')
@return success result or return error code of integration tool
"""
# Set model parameters to only those working - TODO
model_atmos = 'ignore'
if model_grav == 'spherical': model_grav = 'j2'
# Assume initial position and velocity as GPS estimate
et = spice.datetime2et(self.time[0])
rot = spice.sxform('ITRF93','J2000',et)
x0_original = [email protected][:,0]
# Set up estimation matrices
# self.obs_num = 1
H = np.empty((6*self.obs_num,6))
dz = np.empty((6*self.obs_num,1))
self.x0 = x0_original
dx = np.ones((6,1))
# Build the estimation operation
iter_count = 0
while np.linalg.norm(dx) > 1e-5 and iter_count < 10:
for i in range(self.obs_num):
# Compute state and transformation
et = spice.datetime2et(self.time[i])
rot = spice.sxform('ITRF93','J2000',et)
# Retrieve measurement
z = [email protected](self.states[:,i],(6,1))
# Set current time
dt = pd.Timedelta(self.time[i] - self.time[0]).total_seconds()
# Calculate propagated state x approximate by numerical integration
def xfun(t,x,t0):
time = t0 + pd.to_timedelta(t, unit='sec')
a_e,_ = self.earth_gravity(time,x[0:3],model_grav)
a_atm,_,_ = self.atmospheric_drag(time,x[0:3],x[3:6],model_atmos)
a = a_e + a_atm
return np.concatenate((x[3:6], a))
integ = sp.integrate.ode(xfun).set_integrator('vode')
integ.set_initial_value(self.x0,0).set_f_params(self.time[0])
x = integ.integrate(dt).reshape((6,1))
if integ.successful() != 1:
return integ.get_return_code()
# Calculate acting forces
_,dadr_e = self.earth_gravity(self.time[0],x0_original[0:3],model_grav)
_,dadr_atm,dadv_atm = self.atmospheric_drag(self.time[i],x0_original[0:3],x0_original[3:6],model_atmos)
# Calculate F matrix
F = np.block([[np.zeros((3,3)), np.eye(3)],
[dadr_e + dadr_atm, dadv_atm]])
# Calculate Phi approximate by numerical integration
def Phifun(t,Phi,F):
Phi = Phi.reshape((6,6))
dPhi = F@Phi
return dPhi.reshape(-1)
dt = pd.Timedelta(self.time[i] - self.time[0]).total_seconds()
Phi0 = np.eye(6).reshape(-1)
integ = sp.integrate.ode(Phifun).set_integrator('vode')
integ.set_initial_value(Phi0,0).set_f_params(F)
Phi = integ.integrate(dt).reshape((6,6))
if integ.successful() != 1:
return integ.get_return_code()
# Contribute this to H and z vectors
dz[i*6:(i*6+6),:] = z - x
H[i*6:(i*6+6),:] = Phi
# Calculate SVD estimation
U,D,V = sp.linalg.svd(H,full_matrices=False)
D = np.diag(D)
dx = [email protected](D)@U.T@dz
self.x0 = self.x0 + dx.reshape(-1)
iter_count = iter_count + 1
self.t0 = self.time[0]
self.state_to_kepler()
self.state_to_kepler_sgp4()
self.write_to_tle()
return 1
def estimate_batch_orbit_sgp4(self):
"""!@brief Calculates the batch orbit estimates using SVD from a set of position and
velocity measurements, where the SGP4 propagator is used.
@return success result or return error code of integration tool
"""
# Set optimisation parameters
tol = 1e-3
delta_perc = 0.001
max_iter = 100
# Define solver function that will need to equal zero for Kepler
# to be exact to sgp4 TLE
def sgpsolver(para,tle,time):
# Set basic TLE
s_tle = tle.split('\n')[0]
t_tle = tle.split('\n')[1]
t_tle = t_tle[:8] + '%8.4f' % para[2] + t_tle[16:]
t_tle = t_tle[:17] + '%8.4f' % para[3] + t_tle[25:]
ecc = '%.7f' % para[1]
t_tle = t_tle[:26] + '' + ecc.lstrip('0').lstrip('.') + t_tle[33:]
t_tle = t_tle[:34] + '%8.4f' % para[4] + t_tle[42:]
t_tle = t_tle[:43] + '%8.4f' % para[5] + t_tle[51:]
t_tle = t_tle[:52] + '%11.8f' % para[0] + t_tle[63:]
# Set up satellite object
sat = Satrec.twoline2rv(s_tle, t_tle)
# Run SGP4 propagation with time difference as zero
jd,fr = jday_datetime(time)
_,r,v = sat.sgp4(jd,fr)
# Calculate state
x = np.zeros(6)
x[0:3] = np.array(r)*1e3; x[3:6] = np.array(v)*1e3
return x
# Initialise Kepler parameters
jd,fr = jday_datetime(self.time[0])
rot = self.teme2itrf(jd, fr).T
self.x0 = [email protected][:,0]
self.t0 = self.time[0]
self.state_to_kepler()
self.write_to_tle()
# Build the estimation operation
para = self.para.copy()
deltay = 1.0
iter_count = 0
E_R = 200000
while np.linalg.norm(deltay) > tol and max_iter > iter_count:
# Remove outliers
thresh = INPUT_MULIPLIER*E_R
valid = np.ones(self.obs_num)
x_err = np.empty(self.obs_num)
obs_num = 0
if USE_DATA_EDITING:
for i in range(self.obs_num):
# Transform measured state to the ECI/J2000 frame
jd,fr = jday_datetime(self.time[i])
rot = self.teme2itrf(jd, fr).T
x = [email protected](self.states[:,i],(6,1))
# Calculate propagated state x
f = sgpsolver(para,self.tle_str,self.time[i])
x_err[i] = np.linalg.norm(x[0:3] - f[0:3].reshape((3,1)))
if (x_err[i]/SIGMA_GPS) > thresh:
valid[i] = 0
else:
obs_num = obs_num + 1
E_R = np.sqrt(np.sum(np.square(x_err))/obs_num)
else:
obs_num = self.obs_num
# Generate measurement vector and matrix
ns = self.nstates
H = np.empty((ns*obs_num,6))
dz = np.empty((ns*obs_num,1))
pos = 0
for i in range(self.obs_num):
if valid[i]:
# Transform measured state to the ECI/J2000 frame
jd,fr = jday_datetime(self.time[i])
rot = self.teme2itrf(jd, fr).T
x = [email protected](self.states[:,i],(6,1))
# Calculate measurement vector
f = sgpsolver(para,self.tle_str,self.time[i])
dz[pos*ns:(pos*ns+ns),:] = x[0:ns] - f[0:ns].reshape((ns,1))
# Calculate linealized SGP4 matrix
M = np.zeros((ns,6))
for j in range(0,6):
delta_para = para.copy()
delta_para[j] = para[j] + delta_perc*para[j]
deltaf = sgpsolver(delta_para,self.tle_str,self.time[i])
M[:,j] = (deltaf[0:ns] - f[0:ns])/(delta_perc*para[j])
H[pos*ns:(pos*ns+ns),:] = M
pos = pos + 1
# Calculate SVD estimation
U,D,V = sp.linalg.svd(H,full_matrices=False)
D = np.diag(D)
deltay = [email protected](D)@U.T@dz
para = para + deltay.reshape(-1)
# Check if all terms are within bounds
if para[1] > 1 or para[1] < 0: para[1] = self.para[1]
para[2] = para[2] % 360; para[3] = para[3] % 360
para[4] = para[4] % 360; para[5] = para[5] % 360
iter_count = iter_count + 1
self.t0 = self.time[0]
self.para = para
self.write_to_tle()
return 1
def estimate_batch_orbit_sgp4_bstar(self):
"""!@brief Calculates the batch orbit estimates using SVD from a set of position and
velocity measurements, where the SGP4 propagator is used. Optimises all six
orbital elements as well as the bstar drag term.
@return success result or return error code of integration tool
"""
# Set optimisation parameters
tol = 1e-3
delta_perc = 0.001
max_iter = 100
# Define solver function that will need to equal zero for Kepler
# to be exact to sgp4 TLE
def sgpsolver(para,tle,time):
# Set basic TLE
s_tle = tle.split('\n')[0]
s_tle = s_tle[:53] + self.convert_sci(para[6]) + s_tle[61:]
t_tle = tle.split('\n')[1]
t_tle = t_tle[:8] + '%8.4f' % para[2] + t_tle[16:]
t_tle = t_tle[:17] + '%8.4f' % para[3] + t_tle[25:]
ecc = '%.7f' % para[1]
t_tle = t_tle[:26] + '' + ecc.lstrip('0').lstrip('.') + t_tle[33:]
t_tle = t_tle[:34] + '%8.4f' % para[4] + t_tle[42:]
t_tle = t_tle[:43] + '%8.4f' % para[5] + t_tle[51:]
t_tle = t_tle[:52] + '%11.8f' % para[0] + t_tle[63:]
# Set up satellite object
sat = Satrec.twoline2rv(s_tle, t_tle)
# Run SGP4 propagation with time difference as zero
jd,fr = jday_datetime(time)
_,r,v = sat.sgp4(jd,fr)
# Calculate tolerance
x = np.zeros(6)
x[0:3] = np.array(r)*1e3; x[3:6] = np.array(v)*1e3
return x
# Initialise Kepler parameters
jd,fr = jday_datetime(self.time[0])
rot = self.teme2itrf(jd, fr).T
self.x0 = [email protected][:,0]
self.t0 = self.time[0]
self.state_to_kepler()
self.write_to_tle()
# Set up estimation matrices
H = np.empty((6*self.obs_num,7))
dz = np.empty((6*self.obs_num,1))
# Build the estimation operation
y = np.array(self.para.copy())
y = np.append(y,self.bstar)
deltay = 1.0
iter_count = 0
while np.linalg.norm(deltay) > tol and max_iter > iter_count:
for i in range(self.obs_num):
# Transform measured state to the ECI/J2000 frame
jd,fr = jday_datetime(self.time[i])
rot = self.teme2itrf(jd, fr).T
x = [email protected](self.states[:,i],(6,1))
# Calculate propagated state x approximate by numerical integration
M = np.zeros((6,7))
f = sgpsolver(y,self.tle_str,self.time[i])
for j in range(0,7):
delta_y = y.copy()
delta_y[j] = y[j] + delta_perc*y[j]
deltaf = sgpsolver(delta_y,self.tle_str,self.time[i])
M[:,j] = (deltaf - f)/(delta_perc*y[j])
# Contribute this to H and z vectors
dz[i*6:(i*6+6),:] = x - f.reshape((6,1))
H[i*6:(i*6+6),:] = M
# Calculate SVD estimation
U,D,V = sp.linalg.svd(H,full_matrices=False)
D = np.diag(D)
deltay = [email protected](D)@U.T@dz
y = y + deltay.reshape(-1)
# Check if all terms are within bounds
if y[1] > 1 or y[1] < 0: y[1] = self.para[1]
y[2] = y[2] % 360; y[3] = y[3] % 360
y[4] = y[4] % 360; y[5] = y[5] % 360
iter_count = iter_count + 1
if y[6] < 0:
self.estimate_batch_orbit_sgp4()
else:
self.t0 = self.time[0]
self.para = y[0:6]
self.bstar = y[6]
self.write_to_tle()
return 1
def state_to_kepler(self):
"""!@brief Transform ECI position and velocity to Kepler orbital parameters.
"""
mu = GRAV*M_EARTH
# Calculate position and velocity norm
r = self.x0[0:3]
v = self.x0[3:6]
rr = np.linalg.norm(r)
vv = np.linalg.norm(v)
# Calculate angular momentum and node vector
h = np.cross(r,v)
k = [0.,0.,1.]
N = np.cross(k,h)
# Calculate eccentricity vector
ee = ((vv**2 - mu/rr)*r - np.dot(r,v)*v)/mu
ecc = np.linalg.norm(ee)
# Calculate energy
energy = vv**2/2 - mu/rr
# Determine semimajor axis and mean motion
a_major = -mu/2/energy
n_motion = np.sqrt(mu/a_major**3)
n_motion = n_motion/2/np.pi*60*60*24
# Determine inclination
inc = np.arccos(h[2]/np.linalg.norm(h))
inc = inc*180./np.pi
# Determine right ascension of ascending node
raan = np.arccos(N[0]/np.linalg.norm(N))
if N[1] < 0: raan = 2*np.pi - raan
raan = raan*180/np.pi
# Determine argument of perigee
aop = np.arccos(np.dot(N,ee)/np.linalg.norm(N)/ecc)
if ee[2] < 0: aop = 2*np.pi - aop
aop = aop*180./np.pi
# Determine true anomaly
true_anom = np.arccos(np.dot(ee,r)/ecc/rr)
if np.dot(r,v) < 0: true_anom = 2*np.pi - true_anom
# Determine eccentric anomaly
E_anom = 2*np.arctan2(((1+ecc)/(1-ecc))**(-1/2)*np.sin(true_anom/2),\
np.cos(true_anom/2))
# Determine mean anomaly at epoch
M_anom = E_anom - ecc*np.sin(E_anom)
M_anom = M_anom*180/np.pi
# Set parameter
# [n_motion, ecc, inc, raan, aop, M_anom]
self.para = [n_motion, ecc, inc, raan, aop, M_anom]
return
def state_to_kepler_sgp4(self):
"""!@brief Transform ECI position and velocity to Kepler orbital parameters by the SGP4
definition.
Reference: Lee, Byoung-Sun & Park, Jae-Woo. (2003). Estimation of the SGP4 Drag Term
From Two Osculating Orbit States. Journal of Astronomy and Space Sciences. 20. 11-20.
10.5140/JASS.2003.20.1.011.
"""
# Set constants
tol = 1e-5
delta_perc = 0.001
max_iter = 100
# Define solver function that will need to equal zero for Kepler
# to be exact to sgp4 TLE
def sgpsolver(para,tle,time):
# Set basic TLE
s_tle = tle.split('\n')[0]
t_tle = tle.split('\n')[1]
t_tle = t_tle[:8] + '%8.4f' % para[2] + t_tle[16:]
t_tle = t_tle[:17] + '%8.4f' % para[3] + t_tle[25:]
ecc = '%.7f' % para[1]
t_tle = t_tle[:26] + '' + ecc.lstrip('0').lstrip('.') + t_tle[33:]
t_tle = t_tle[:34] + '%8.4f' % para[4] + t_tle[42:]
t_tle = t_tle[:43] + '%8.4f' % para[5] + t_tle[51:]
t_tle = t_tle[:52] + '%11.8f' % para[0] + t_tle[63:]
# Set up satellite object
jd,fr = jday_datetime(time)
sat = Satrec.twoline2rv(s_tle, t_tle)
# Run SGP4 propagation with time difference as zero
_,r,v = sat.sgp4(jd,fr)
# Calculate tolerance
x = np.zeros(6)
x[0:3] = np.array(r)*1e3; x[3:6] = np.array(v)*1e3
return x
# Set up iterative solver loop
para = self.para.copy()
deltay = 1.0
iter_count = 0
while np.linalg.norm(deltay) > tol and max_iter > iter_count:
# Build Jacobian using forward difference quotient
M = np.zeros((6,6))
f = sgpsolver(para,self.tle_str,self.t0)
for i in range(0,6):
delta_para = para.copy()
delta_para[i] = para[i] + delta_perc*para[i]
deltaf = sgpsolver(delta_para,self.tle_str,self.t0)
M[:,i] = (deltaf - f)/(delta_perc*para[i])
# Calculate iteration
Minv = np.linalg.inv(M)
deltax = self.x0 - f
deltay = Minv@deltax
para = para + deltay
# Check if all terms are within bounds
if para[1] > 1 or para[1] < 0: para[1] = self.para[1]
para[2] = para[2] % 360; para[3] = para[3] % 360
para[4] = para[4] % 360; para[5] = para[5] % 360
iter_count = iter_count + 1
# Set parameter
# [n_motion, ecc, inc, raan, aop, M_anom]
self.para = para
return
def read_tle(self,tle_str):
"""!@brief Read and stores the parameters extracted from a TLE string.
@para tle TLE string
@return Sucessful operation boolean
"""
# Split tle lines
tle = tle_str.split('\n')
# Check tle is first row
if tle[0][0:1] != '1': return 0
# Read first line contents
self.tle_sat_cat_num = int(tle[0][2:7])
self.tle_classification = tle[0][7:8]
self.tle_int_desig = tle[0][9:17]
year = pd.to_datetime(tle[0][18:20],format="%y")
doy = pd.to_timedelta(np.double(tle[0][20:32]),unit='D')
self.t0 = year + doy - pd.Timedelta(days=1)
self.tle_ndot = np.double(tle[0][33:43])
self.tle_nddot = np.double('.' + tle[0][44:50].strip() \
+ 'e' + tle[0][50:52])
self.bstar = np.double(tle[0][53:54] + '.' + tle[0][54:59].strip() \
+ 'e' + tle[0][59:61])
self.tle_eph_type = tle[0][62:63]
self.ele_set_no = int(tle[0][64:68])
# Check tle is second row
if tle[1][0:1] != '2': return 0
# Read second line contents
inc = np.double(tle[1][8:16])
raan = np.double(tle[1][17:25])
ecc = np.double('.' + tle[1][26:33].strip())
aop = np.double(tle[1][33:42])
M_anom = np.double(tle[1][43:51])
n_motion = np.double(tle[1][52:63])
self.tle_rev_num = int(tle[1][63:68])
self.para = [n_motion, ecc, inc, raan, aop, M_anom]
# Store string parameter in class
self.tle_str = tle_str
return 1
def convert_sci(self,n):
"""!@brief Convert scientific notation-based number to TLE format 'XXXXX...X+X'
@para n Number of significant figures
@return String contain TLE format based scientific notation-based number
"""
sign = '-' if n < 0 else ' '
if n == 0.0: return ' 00000+0'
ppow = np.floor(np.log10(np.abs(n)))
if ppow < -9: return ' 00000+0'
pow_str = '%1d' % (np.abs(ppow)-1)
pow_sign = '-' if ppow < 0 else '+'
dec = (np.abs(n)/10**ppow)*10**-1
dec_str = '%5.5f' % dec
return sign + dec_str.lstrip('0').lstrip('.') + pow_sign + pow_str
def calc_checksum(self,line):
"""!@brief Calculate checksum of the line by the summation of all digits, modulo 10
@para line Line to calculate checksum over
@return checksum
"""
checksum = 0
for ch in line:
if ch.isdigit() == True: checksum += int(ch)
elif ch == '-': checksum += 1
return checksum % 10
def write_to_tle(self):
"""!@brief Write stored parameters to a tle string
@return TLE string
"""
# Write first line of tle
tle = '1'
tle = tle + ' %05d' % self.tle_sat_cat_num
tle = tle + self.tle_classification
tle = tle + ' ' + self.tle_int_desig
tle = tle + ' ' + self.t0.strftime('%y')
doy = self.t0.dayofyear + self.t0.hour / 24.0 + self.t0.minute / (24.0*60.0) + \
(self.t0.second + np.double(self.t0.strftime('.%f'))) / (24.0*60.0*60.0)
tle = tle + '%012.8f' % doy
sign = ' -' if self.tle_ndot < 0 else ' '
ndot = '%8.8f' % self.tle_ndot
tle = tle + sign + ndot.lstrip('0')
tle = tle + ' ' + self.convert_sci(self.tle_nddot)
tle = tle + ' ' + self.convert_sci(self.bstar)
tle = tle + ' ' + self.tle_eph_type
tle = tle + ' %4d' % self.ele_set_no
checksum = self.calc_checksum(tle)
tle = tle + '%1d' % checksum
tle = tle +'\n'
# Write second line of tle
tle_2 = '2'
tle_2 = tle_2 + ' %05d' % self.tle_sat_cat_num
tle_2 = tle_2 + ' %8.4f' % self.para[2]
tle_2 = tle_2 + ' %8.4f' % self.para[3]
ecc = '%.7f' % self.para[1]
tle_2 = tle_2 + ' ' + ecc.lstrip('0').lstrip('.')
tle_2 = tle_2 + ' %8.4f' % self.para[4]
tle_2 = tle_2 + ' %8.4f' % self.para[5]
tle_2 = tle_2 + ' %11.8f' % self.para[0]
rev_num = '%5d' % self.tle_rev_num
tle_2 = tle_2 + rev_num.ljust(5)
checksum = self.calc_checksum(tle_2)
tle_2 = tle_2 + '%1d' % checksum
tle_2 = tle_2 +'\n'
tle = tle + tle_2
self.tle_str = tle
return tle
def spherical_gravity_jacobian(self,x):
"""!@brief Calculate the spherical harmonic Jacobian
Reference: Lundberg, J.B., & Schutz, B.E. (1988). Recursion formulas of Legendre functions
for use with nonsingular geopotential models. Journal of Guidance Control and
Dynamics, 11, 31-38.
@para x Position of the satellite in ECEF coordinates (x,y,z) [m]
@return Spherical harmonic Jacobian matrix
"""
# Read coefficients
Cnm = self.clm.coeffs[0,:,:]
Snm = self.clm.coeffs[1,:,:]
if self.clm.normalization == '4pi':
nor = 4*np.pi
else:
nor = 1
degree = self.clm.lmax
Cnm = Cnm*nor; Snm = Snm*nor
# Derive new spherical coordinates
r = np.linalg.norm(x);
s = x[0]/r; t = x[1]/r; u = x[2]/r
# Calculate r_m and i_m
# [r_0 r_1 ... r_n], [i_0 i_1 ... i_n]
r_m = np.ones(degree+1); i_m = np.zeros(degree+1)
r_m[1] = s; i_m[1] = t
for m in range(2,degree+1):
r_m[m] = s*r_m[m-1] - t*i_m[m-1]
i_m[m] = s*i_m[m-1] + t*r_m[m-1]
# Calculate Anm terms
# [[A_00 A_01 ... A_0n2 ]
# [A_10 A_11 ... A_1n2 ]
# [... ... ... ... ]
# [A_n20 A_n21 ... A_n2n2]]
Anm = np.zeros((degree+3,degree+3))
cosphi = np.sqrt(1 - x[2]**2/r**2)
Anm[1,1] = np.sqrt(3)*cosphi
for n in range(2,degree+3):
for m in range(1,n+1):
if m == n: