-
Notifications
You must be signed in to change notification settings - Fork 3
/
processGNSSMeas.py
415 lines (374 loc) · 19.2 KB
/
processGNSSMeas.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
# -*- coding: utf-8 -*-
import GnssThresholds
import GnssConstants
import numpy as np
import Time
import datetime
from math import sin,cos,sqrt,pi
class GpsLoc():
def __init__(self,rfile):
self.rfile = rfile
with open(self.rfile,'w') as f:
f.writelines('')
@staticmethod
def blhxyz(geod,a0,b0):
'''
input:
geod tuple or list(B,L,H)(3) 单位:(弧度)
output:
x tuple or list(X,Y,Z)(3)
'''
xyz = [0,0,0]
if a0 == 0 or b0 == 0:
a = 6378137.0
b = 298.257223563 # alpha = (a-b)/a
else:
a = a0
b = b0
if b <= 6000000:
b = a - a / b
e2 = 1 - (b ** 2) / (a ** 2)
W = sqrt(1 - e2 * sin(geod[0]) **2)
N = a / W
xyz[0] = (N + geod[2]) * cos(geod[0]) * cos(geod[1])
xyz[1] = (N + geod[2]) * cos(geod[0]) * sin(geod[1])
xyz[2] = (N * (1 - e2) + geod[2]) * sin(geod[0])
return xyz
def loc2rfile(self,fixdata):
with open(self.rfile,'a+') as f:
blh = [float(i)*pi/180 for i in fixdata[1:4]]
utctime = datetime.datetime.fromtimestamp(float(fixdata[-1])/1e3).strftime("%Y/%m/%d %H:%M:%S.%f")[:-3]
xyz = GpsLoc.blhxyz(blh,0,0)
f.writelines("%s %15.4f %15.4f %15.4f\n" % (utctime,*xyz))
class GnssRaw():
def __init__(self):
self._prepself()
def clear(self):
self._prepself()
def _prepself(self):
self._prepClock()
self._prepMeas()
self.gnssRaw = {}
self.gnssRaw.update(self.gnssClockFields)
self.gnssRaw.update(self.gnssMeasurementFields)
def _prepClock(self):
clockfields = ['TimeNanos',
'TimeUncertaintyNanos',
'LeapSecond',
'FullBiasNanos',
'BiasUncertaintyNanos',
'DriftNanosPerSecond',
'DriftUncertaintyNanosPerSecond',
'HardwareClockDiscontinuityCount',
'BiasNanos']
self.gnssClockFields = {i:None for i in clockfields}
def _prepMeas(self):
measurementfields = ['Cn0DbHz',
'ConstellationType',
'MultipathIndicator',
'PseudorangeRateMetersPerSecond',
'PseudorangeRateUncertaintyMetersPerSecond',
'ReceivedSvTimeNanos',
'ReceivedSvTimeUncertaintyNanos',
'State',
'Svid',
'AccumulatedDeltaRangeMeters',
'AccumulatedDeltaRangeUncertaintyMeters',
'CarrierFrequencyHz']
self.gnssMeasurementFields = {i:None for i in measurementfields}
class AdrMconstBias():
timeLimit = 10 * 60
sysType = [1,3,5,6]
def __init__(self):
'''
checkCycleSlip:
systerm: GPS GLS BDS GAL
satNumber: 1-50
-1 Not initialized
0 valid data
1 cycle-slip
'''
self.checkTime = {isys : {isat : np.inf for isat in range(GnssConstants.MAXSAT)} for isys in AdrMconstBias.sysType}
self.constBias = {isys : {isat : np.nan for isat in range(GnssConstants.MAXSAT)} for isys in AdrMconstBias.sysType}
self.refPrM = {isys : {isat : np.nan for isat in range(GnssConstants.MAXSAT)} for isys in AdrMconstBias.sysType}
self.refClkDCount = 0
@staticmethod
def wavelength(ifreq,isys,isvd,freqv=None):
if ifreq == 0:
return np.nan
if ifreq == 1 and isys == 1:
return GnssConstants.LIGHTSPEED / GnssConstants.GPS_L1
if ifreq != 1 and isys == 1:
return GnssConstants.LIGHTSPEED / GnssConstants.GPS_L5
if ifreq == 1 and isys == 3:
# gls_freq = round((freqv - GnssConstants.GLS_L1) / GnssConstants.GLS_dL1) * GnssConstants.GLS_dL1 + GnssConstants.GLS_L1
gls_freq = GnssConstants.GLS_L1 + GnssConstants.GLS_IFREQ[isvd] * GnssConstants.GLS_dL1
return GnssConstants.LIGHTSPEED / gls_freq
if ifreq != 1 and isys == 3:
#gls_freq = round((freqv - GnssConstants.GLS_L2) / GnssConstants.GLS_dL2) * GnssConstants.GLS_dL2 + GnssConstants.GLS_L2
# gls_freq = GnssConstants.GLS_L2 + GnssConstants.GLS_IFREQ[isvd] * GnssConstants.GLS_dL2
return np.nan # GnssConstants.LIGHTSPEED / GnssConstants.GLS_L2
if ifreq == 1 and isys == 5:
return GnssConstants.LIGHTSPEED / GnssConstants.BDS_B1
if ifreq != 1 and isys == 5:
return GnssConstants.LIGHTSPEED / GnssConstants.BDS_B2
if ifreq == 1 and isys == 6:
return GnssConstants.LIGHTSPEED / GnssConstants.GAL_E1
if ifreq != 1 and isys == 6:
return GnssConstants.LIGHTSPEED / GnssConstants.GAL_E5a
@staticmethod
def getRealMeasWaveL(freqNum,conType,freqValue):
return np.array([AdrMconstBias.wavelength(ifreq,isys,freqv) for ifreq,isys,freqv in zip(freqNum,conType,freqValue)])
@staticmethod
def getStandardWaveL(freqNum,conType,sVids):
return np.array([AdrMconstBias.wavelength(ifreq,isys,isvd) for ifreq,isys,isvd in zip(freqNum,conType,sVids)])
def getDelPr(self,gnssMeas):
nmeas = len(gnssMeas.Svid)
delPrM = []
bClockDis = (gnssMeas.ClkDCount - self.refClkDCount) != 0
self.refClkDCount = gnssMeas.ClkDCount
for i in range(nmeas):
isys = gnssMeas.ConstellationType[i]
isvd = gnssMeas.Svid[i]
if bClockDis or (self.refPrM[isys][isvd] == np.nan):
self.refPrM[isys][isvd] = gnssMeas.PrM[i]
delPrM.append(np.nan)
else:
delPrM.append(gnssMeas.PrM[i] - self.refPrM[isys][isvd])
return np.array(delPrM)
def processAdrM(self,gnssMeas):
nmeas = len(gnssMeas.Svid)
waveLen = AdrMconstBias.getStandardWaveL(gnssMeas.freqNum,gnssMeas.ConstellationType,gnssMeas.CarrierFrequencyHz)
constBias = []
for i in range(nmeas):
isys = gnssMeas.ConstellationType[i]
isvd = gnssMeas.Svid[i]
if (gnssMeas.AdrState[i] & 2**0): # Valid Adr Meas
ltime = (gnssMeas.fctTime - self.checkTime[isys][isvd]) > AdrMconstBias.timeLimit
# update time-tag
self.checkTime[isys][isvd] = gnssMeas.fctTime
# check if failed time-tag or no-valued Bias
if ltime or np.isnan(self.constBias[isys][isvd]):
bias = gnssMeas.PrM[i] - gnssMeas.AdrM[i]
self.constBias[isys][isvd] = bias
constBias.append(bias)
else:
constBias.append(self.constBias[isys][isvd])
elif (gnssMeas.AdrState[i] & 2**4): # Cycle-slip Adr Meas
# update time-tag
self.checkTime[isys][isvd] = gnssMeas.fctTime
bias = gnssMeas.PrM[i] - gnssMeas.AdrM[i]
self.constBias[isys][isvd] = bias
constBias.append(bias)
else:
if not np.isnan(self.constBias[isys][isvd]):
# delete time-tag
self.checkTime[isys][isvd] = np.inf
self.constBias[isys][isvd] = np.nan
constBias.append(np.nan)
return (gnssMeas.AdrM + np.array(constBias)) / waveLen
class GnssMeas():
LogFile = './Log.txt'
def __init__(self):
self.iepoch = 1
self.ConstComp = AdrMconstBias()
def filterValid(self,gnssRaw):
# remove fields corresponding to measurements that are invalid
# check ReceivedSvTimeUncertaintyNanos, PseudorangeRateUncertaintyMetersPerSecond
# for now keep only Svid with towUnc<0.5 microseconds and prrUnc < 10 mps
sMsg = ''
iTowUnc = gnssRaw['ReceivedSvTimeUncertaintyNanos'] > GnssThresholds.MAXTOWUNCNS
iPrrUnc = gnssRaw['PseudorangeRateUncertaintyMetersPerSecond'] > GnssThresholds.MAXPRRUNCMPS
iBad = iTowUnc | iPrrUnc
if np.any(iBad):
numBad = np.sum(iBad)
if numBad >= len(iBad):
raise Exception("Removing all measurements in gnssRaw.")
gnssRaw = {i:gnssRaw[i][iBad==0] for i in gnssRaw.keys()}
sMsg += 'Removed %d bad meas inside ProcessGnssMeas > FilterValid because:\n' % numBad
if np.any(iTowUnc):
sMsg += 'towUnc > %.0f ns\n' % GnssThresholds.MAXTOWUNCNS
if np.any(iPrrUnc):
sMsg += 'prrUnc > %.0f m/s\n' % GnssThresholds.MAXPRRUNCMPS
print(sMsg)
with open(GnssMeas.LogFile,'a+') as f:
f.writelines(sMsg)
return gnssRaw
def checkGpsTimeRollover(self,tRxSecondes,prSeconds,Type):
if Type == 'day':
const_t = GnssConstants.DAYSEC
iRollover = prSeconds > const_t / 2
elif Type == 'week':
const_t = GnssConstants.WEEKSEC
iRollover = prSeconds > const_t / 2
if np.any(iRollover):
print('WARNING: %s rollover detected in time tags. Adjusting ...\n' % Type)
prS = prSeconds[iRollover]
delS = np.round(prS / const_t) * const_t
prS -= delS
maxBiasSeconds = 10
if np.any(prS>maxBiasSeconds):
raise Exception("Failed to correct %s rollover\n" % Type)
else:
prSeconds[iRollover] = prS
tRxSecondes[iRollover] = tRxSecondes[iRollover] - delS
print("Corrected %s rollover\n" % Type)
prSeconds[tRxSecondes == 0] = np.nan
return tRxSecondes,prSeconds
def _getDelPrMinusAdrM(self):
#/* However, it is expected that the data is only accurate when:
# * 'accumulated delta range state' == GPS_ADR_STATE_VALID.
#*/
# define GPS_ADR_STATE_UNKNOWN 0
# define GPS_ADR_STATE_VALID (1<<0)
# define GPS_ADR_STATE_RESET (1<<1)
# define GPS_ADR_STATE_CYCLE_SLIP (1<<2)
nmeas = len(self.Svid)
DelPrMinusAdrM = np.array([np.nan]*nmeas)
iValid = self.AdrState & (2**0)
iReset = self.AdrState & (2**1)
self.AdrM[iValid==0] = np.nan
for i in range(nmeas):
DelPrM0 = 0 # to store initial offset from AdrM
if (not np.isinf(self.AdrM[i])) and (self.AdrM[i] != 0) and (not np.isinf(self.DelPrM[i])) and (iReset[i] == 0):
# reinitialize after NaNs or AdrM zero or AdrState reset
if np.isnan(DelPrM0):
DelPrM0 = self.DelPrM[i] - self.AdrM[i]
else:
DelPrM0 = np.nan
DelPrMinusAdrM[i] = self.DelPrM[i] - DelPrM0 - self.AdrM[i]
return DelPrMinusAdrM
def _getPhaseMeas(self):
return self.ConstComp.processAdrM(self)
def _getDelPr(self):
return self.ConstComp.getDelPr(self)
def _prepData(self,nmeas,gnssRaw):
self.ClkDCount = 0
self.HwDscDelS = 0
# self.Svid = 0
# epoch 1 base-Data
if self.iepoch == 1:
self.fullBiasNanos = gnssRaw['FullBiasNanos'][0]
self.AzDeg = np.array([np.nan]*nmeas)
self.ElDeg = np.array([np.nan]*nmeas)
self.tRxSeconds = np.array([np.nan]*nmeas)
self.tTxSeconds = np.array([np.nan]*nmeas)
self.PrM = np.array([np.nan]*nmeas)
self.PrSigmaM = np.array([np.nan]*nmeas)
self.DelPrM = np.array([np.nan]*nmeas)
self.PrrMps = np.array([np.nan]*nmeas)
self.PrrSigmaMps= np.array([np.nan]*nmeas)
self.AdrM = np.array([np.nan]*nmeas)
self.AdrSigmaM = np.array([np.nan]*nmeas)
self.AdrState = np.zeros(nmeas)
self.Cn0DbHz = np.array([np.nan]*nmeas)
self.ConstellationType = np.array([np.nan]*nmeas)
self.CarrierFrequencyHz = np.array([np.nan]*nmeas)
self.freqNum = np.zeros(nmeas)
def freqSign(self,gnssRaw):
# gps sys
gpsfreq = gnssRaw['CarrierFrequencyHz'] * (gnssRaw['ConstellationType'] == 1)
self.freqNum[np.abs(gpsfreq - GnssConstants.GPS_L1) < 100] = 1
self.freqNum[np.abs(gpsfreq - GnssConstants.GPS_L5) < 100] = 5
# gls sys
glsfreq = gnssRaw['CarrierFrequencyHz'] * (gnssRaw['ConstellationType'] == 3)
# self.freqNum[glsfreq!=0] = 1
self.freqNum[
np.abs(np.round((glsfreq - GnssConstants.GLS_L1) / GnssConstants.GLS_dL1) -
(gnssRaw['CarrierFrequencyHz'] - GnssConstants.GLS_L1) / GnssConstants.GLS_dL1) < 0.0002
] = 1
self.freqNum[np.round((glsfreq - GnssConstants.GLS_L1) / GnssConstants.GLS_dL1) > 100] = 0
# self.freqNum[
# np.abs(np.round((glsfreq - GnssConstants.GLS_L2) / GnssConstants.GLS_dL2) -
# (gnssRaw['CarrierFrequencyHz'] - GnssConstants.GLS_L2) / GnssConstants.GLS_dL2) < 0.0002
# ] = 2
# bds sys
bdsfreq = gnssRaw['CarrierFrequencyHz'] * (gnssRaw['ConstellationType'] == 5)
self.freqNum[np.abs(bdsfreq - GnssConstants.BDS_B1) < 100] = 1
self.freqNum[np.abs(bdsfreq - GnssConstants.BDS_B2) < 100] = 2
# gal sys
galfreq = gnssRaw['CarrierFrequencyHz'] * (gnssRaw['ConstellationType'] == 6)
self.freqNum[np.abs(galfreq - GnssConstants.GAL_E1) < 100] = 1
self.freqNum[np.abs(galfreq - GnssConstants.GAL_E5a) < 100] = 5
def process(self,gnssRaw):
# clean invalid data in gnssRaw
gnssRaw = self.filterValid(gnssRaw)
# gnssRaw : dict
allRxMilliseconds = gnssRaw['allRxMillis'].astype(np.float64)
self.FctSeconds = np.unique(allRxMilliseconds) * 1e-3
self.Svid = gnssRaw['Svid']
nmeas = len(self.Svid)
self._prepData(nmeas,gnssRaw)
# GPS week number
weekNumber = np.floor( - gnssRaw['FullBiasNanos'] * 1e-9 / GnssConstants.WEEKSEC)
# GPS day number
# dayNumber = np.floor( - gnssRaw['FullBiasNanos']*1e-9 / GnssConstants.DAYSEC)
# GPS time of week
towSeconds = (gnssRaw['TimeNanos'] - gnssRaw['BiasNanos']) * 1e-9
# compute time of measurement relative to start of week
# subtract big longs (i.e. time from 1980) before casting time of week as double
weekNumberNanos = np.int64(weekNumber * GnssConstants.WEEKSEC * 1e9)
# compute tRxNanos using gnssRaw.FullBiasNanos(1), so that
# tRxNanos includes rx clock drift since the first epoch:
tRxNanos = gnssRaw['TimeNanos'] - self.fullBiasNanos - weekNumberNanos
gnssRaw['State'] = gnssRaw['State'].astype(np.int)
State = gnssRaw['State'][0]
if not ((State & 2**3) or (State & 2**7)) :
raise Exception('gnssRaw.State[0] must have bits 0 and (3 or 7) before calling Process')
if not(np.all(tRxNanos >= 0)):
raise Exception('tRxNanos should be >= 0')
tRxGnssSeconds = tRxNanos - gnssRaw['TimeOffsetNanos'] - gnssRaw['BiasNanos']
tRxSeconds = np.zeros(tRxGnssSeconds.shape)
# tRx at positions of GPS
gpspos = ((gnssRaw['ConstellationType'] == 1) * (gnssRaw['State'] & 2**3))!=0
tRxSeconds[gpspos] = np.mod(tRxGnssSeconds[gpspos],GnssConstants.WEEKSEC*1e9)*1e-9
# tRx at position of BDS
bdspos = ((gnssRaw['ConstellationType'] == 5) * (gnssRaw['State'] & 2**3))!=0
tRxSeconds[bdspos] = np.mod(tRxGnssSeconds[bdspos],GnssConstants.WEEKSEC*1e9)*1e-9 - 14
# tRx at position of GEO
galpos = ((gnssRaw['ConstellationType'] == 6) * (gnssRaw['State'] & 2**3))!=0
tRxSeconds[galpos] = np.mod(tRxGnssSeconds[galpos],GnssConstants.MILLISEC*1e9)*1e-9
# tRx at positions of GLO
glspos = ((gnssRaw['ConstellationType'] == 3) * (gnssRaw['State'] & 2**7))!=0
utctime = Time.Gps2Utc(weekNumber,towSeconds,allRxMilliseconds*1e-3)
leapsecs = Time.LeapSeconds(utctime)
tRxSeconds[glspos] = np.mod(tRxGnssSeconds[glspos],GnssConstants.DAYSEC*1e9)*1e-9 + (3*60*60 - leapsecs[glspos])
tTxSeconds = (gnssRaw['ReceivedSvTimeNanos'] + gnssRaw['TimeOffsetNanos'])*1e-9
prSeconds = tRxSeconds - tTxSeconds
tRxSeconds[glspos==0],prSeconds[glspos==0] = self.checkGpsTimeRollover(tRxSeconds[glspos==0],prSeconds[glspos==0],'week')
tRxSeconds[glspos],prSeconds[glspos] = self.checkGpsTimeRollover(tRxSeconds[glspos],prSeconds[glspos],'day')
PrM = prSeconds * GnssConstants.LIGHTSPEED
PrSigmaM = gnssRaw['ReceivedSvTimeUncertaintyNanos'] * 1e-9 * GnssConstants.LIGHTSPEED
PrrMps = gnssRaw['PseudorangeRateMetersPerSecond']
PrrSigmaMps = gnssRaw['PseudorangeRateUncertaintyMetersPerSecond']
AdrM = gnssRaw['AccumulatedDeltaRangeMeters']
AdrSigmaM = gnssRaw["AccumulatedDeltaRangeUncertaintyMeters"]
AdrState = gnssRaw['AccumulatedDeltaRangeState']
Cn0DbHz = gnssRaw['Cn0DbHz']
ConstellationType = gnssRaw['ConstellationType']
CarrierFrequencyHz = gnssRaw['CarrierFrequencyHz']
self.fctTime = weekNumber[0] * GnssConstants.WEEKSEC + tRxSeconds[gpspos][0]
self.GpsTime = Time.Fct2Ymdhms(np.array([self.fctTime]))[0]
self.tRxSeconds = tRxSeconds
self.tTxSeconds = tTxSeconds
self.PrM = PrM
self.PrSigmaM = PrSigmaM
self.PrrMps = PrrMps
self.PrrSigmaMps= PrrSigmaMps
self.AdrM = AdrM
self.AdrSigmaM = AdrSigmaM
self.AdrState = AdrState
self.Cn0DbHz = Cn0DbHz
self.ConstellationType = ConstellationType
self.CarrierFrequencyHz = CarrierFrequencyHz
self.ClkDCount = gnssRaw['HardwareClockDiscontinuityCount'][0]
if gnssRaw['HardwareClockDiscontinuityCount'][0] != gnssRaw['HardwareClockDiscontinuityCount'][-1]:
raise Exception('HardwareClockDiscontinuityCount changed within the same epoch')
self.freqSign(gnssRaw)
self.DelPrM = self._getDelPr()
# self.PhaseMeas = self._getPhaseMeas()
# self.PhaseMeas = self.AdrM / AdrMconstBias.getStandardWaveL(self.freqNum,self.ConstellationType,self.CarrierFrequencyHz)
self.PhaseMeas = self.AdrM / AdrMconstBias.getStandardWaveL(self.freqNum,self.ConstellationType,self.Svid)
self.PhaseMeas[(self.AdrState & 2**0) == 0] = np.nan
self.iepoch += 1
return None