-
Notifications
You must be signed in to change notification settings - Fork 0
/
aprefCreateBLs.py
271 lines (251 loc) · 9.49 KB
/
aprefCreateBLs.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
#!/usr/bin/env python3
"""
This is a modified version of createBLs.py v2.10. It is designed to work on
SNXEPO.SNX.NONCON.AUS, the SINEX file of APREF stations that have less than
2 years of data. It is in GDA2020 and has Type B uncertainties added
NAME:
createBLsAPREF.py
PURPOSE:
Form baselines from the stations in the APREF SINEX file and create DynaML
formatted files.
EXPLANATION:
The code takes SNXEPO.SNX.NONCON.AUS and returns a pair of DynaML
formatted station measurement files for input into DynAdjust
USAGE:
createBLsAPREF.py YYYYMMDD
INPUT:
SNXEPO.SNX.NONCON.AUS
OUTPUT:
aprefYYYYMMDD_stn.xml
aprefYYYYMMDD_msr.xml
HISTORY:
0.01 2019-03-25 Craig Harrison
- Written
"""
import sys
import os
import datetime
import re
from glob import glob
from numpy import matrix, zeros, copy
# Set some variables
refFrame = 'GDA2020'
epoch = '01.01.2020'
inputFile = 'SNXEPO.SNX.NONCON.AUS'
aprefVer = sys.argv[1]
# Get the starting epoch of the solution epoch data from inputFile
epochs = {}
go = False
for line in open(inputFile):
if '-SOLUTION/EPOCHS' in line:
break
if go and '*Code' not in line:
stat = line[1:5] + line[11:13].strip()
epochs[stat] = line[16:18] + line[19:22]
if int(epochs[stat]) < 94000:
epochs[stat] = '20' + epochs[stat]
else:
epochs[stat] = '19' + epochs[stat]
if '+SOLUTION/EPOCHS' in line:
go = True
# Read in the discontinuity information for renaming purposes
yearDOY = {}
go = False
for line in open('soln.snx'):
if '-SOLUTION/DISCONTINUITY' in line:
break
if go and line[0:1] != '*':
cols = line.rstrip().split()
if cols[6] == 'P' and cols[4] != cols[5]:
segment = cols[0] + cols[2]
try:
epochs[segment]
yearDOY[segment] = line[16:18] + line[19:22]
if int(yearDOY[segment]) < 94000:
yearDOY[segment] = '20' + yearDOY[segment]
else:
yearDOY[segment] = '19' + yearDOY[segment]
if yearDOY[segment] == '2000000':
yearDOY[segment] = '1900001'
except KeyError:
pass
if '+SOLUTION/DISCONTINUITY' in line:
go = True
go = False
# Determine which stations have discontinuities
discontStats = set()
for key, value in yearDOY.items():
stat = key[0:4]
discontStats.add(stat)
# Open the output files
stn = open('apref' + aprefVer + '_stn.xml', 'w')
msr = open('apref' + aprefVer + '_msr.xml', 'w')
# Write headers
stn.write('<?xml version="1.0"?>\n')
stn.write('<DnaXmlFormat type="Station File" referenceframe="GDA2020" ' +
'epoch="01.01.2020" ' +
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
'xsi:noNamespaceSchemaLocation="DynaML.xsd">\n')
msr.write('<?xml version="1.0"?>\n')
msr.write('<DnaXmlFormat type="Measurement File" referenceframe="GDA2020" ' +
'epoch="01.01.2020" ' +
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
'xsi:noNamespaceSchemaLocation="DynaML.xsd">\n')
# Open the SINEX file and read in all lines
snxFile = open(inputFile)
lines = snxFile.readlines()
# Create lists to hold the site ID, station coordinate estimate, and the VCV
# matrix lines
estimateLines = []
matrixLines = []
goE = 0
goM = 0
for line in lines:
if re.match('\+SOLUTION/ESTIMATE', line):
goE = 1
if re.match('\+SOLUTION/MATRIX_ESTIMATE', line):
goM = 1
if goE:
if not re.match('\+|\*|\-', line):
estimateLines.append(line)
if goM:
if not re.match('\+|\*|\-', line):
(a, b) = divmod(int(line[:6].lstrip()) - 1, 3)
if a % 2 == 0:
matrixLines.append(line)
if re.match('\-SOLUTION/ESTIMATE', line):
goE = 0
if re.match('\-SOLUTION/MATRIX_ESTIMATE', line):
goM = 0
# Create a list of dictionaries to hold the station names and their coordinates
data = []
estimateLines.reverse()
while estimateLines:
col = estimateLines.pop().rstrip().split()
if col[1][0:3] != 'VEL':
source = {}
if col[2].upper() in discontStats:
segment = col[2] + col[4]
stat = col[2].upper() + '_' + yearDOY[segment]
else:
stat = col[2].upper()
source['site'] = stat
source['x'] = float(col[8])
col = estimateLines.pop().rstrip().split()
source['y'] = float(col[8])
col = estimateLines.pop().rstrip().split()
source['z'] = float(col[8])
data.append(source)
# Create the variance-covariance matrix. In the SINEX file it is given as a
# upper triangular matrix, but it is immediately converted to a lower
# trianguler matrix
vcvXVL = matrix(zeros((6*len(data), 6*len(data))))
for line in matrixLines:
cols = line.rstrip().split()
row = int(cols[0]) - 1
col = int(cols[1]) - 1
for i in range(2, len(cols)):
vcvXVL[col+i-2, row] = float(cols[i])
vcvL = matrix(zeros((3*len(data), 3*len(data))))
for i in range(len(vcvXVL)):
(a, b) = divmod(i, 6)
row = i - 3 * a
for j in range(len(vcvXVL)):
(a, b) = divmod(j, 6)
col = j - 3 * a
if col < len(vcvL) and row < len(vcvL):
vcvL[row, col] = float(vcvXVL[i, j])
vcvU = copy(vcvL.transpose())
vcv = vcvU + vcvL
# Create the design matrix
desMatrix = matrix(zeros((3*(len(data)-1), 3*len(data))))
for i in range(len(data)-1):
desMatrix[3*i, 0] = -1
desMatrix[3*i+1, 1] = -1
desMatrix[3*i+2, 2] = -1
desMatrix[3*i, 3*(i+1)] = 1
desMatrix[3*i+1, 3*(i+1)+1] = 1
desMatrix[3*i+2, 3*(i+1)+2] = 1
# Create the matrix of observed antenna positions
coords = matrix(zeros((3*len(data), 1)))
for i in range(len(data)):
coords[3*i, 0] = data[i]['x']
coords[3*i+1, 0] = data[i]['y']
coords[3*i+2, 0] = data[i]['z']
# Calculate the deltas and the corresponding VCV matrix
deltas = desMatrix * coords
delVCV = desMatrix * vcv * desMatrix.transpose()
# Loop over the sites and write the station data to the output XML file
for i in range(len(data)):
stn.write('\t<DnaStation>\n')
stn.write('\t\t<Name>%s</Name>\n'%(data[i]['site']))
stn.write('\t\t<Constraints>FFF</Constraints>\n')
stn.write('\t\t<Type>XYZ</Type>\n')
stn.write('\t\t<StationCoord>\n')
stn.write('\t\t\t<Name>%s</Name>\n'%(data[i]['site']))
stn.write('\t\t\t<XAxis>%20.14e</XAxis>\n'%(data[i]['x']))
stn.write('\t\t\t<YAxis>%20.14e</YAxis>\n'%(data[i]['y']))
stn.write('\t\t\t<Height>%20.14e</Height>\n'%(data[i]['z']))
stn.write('\t\t\t<HemisphereZone></HemisphereZone>\n')
stn.write('\t\t</StationCoord>\n')
stn.write('\t\t<Description></Description>\n')
stn.write('\t</DnaStation>\n')
# Create an array of the stations minus the core station
coreStat = data[0]['site']
nonCoreStns = []
for i in range(1, len(data)):
nonCoreStns.append(data[i]['site'])
numCovar = len(nonCoreStns) - 1
# Loop over the non-core stations and write the measurement data to the output
# XML file
msr.write('\t<!--Type X GNSS baseline cluster (full correlations)-->\n')
msr.write('\t<DnaMeasurement>\n')
msr.write('\t\t<Type>X</Type>\n')
msr.write('\t\t<Ignore/>\n')
msr.write('\t\t<ReferenceFrame>%s</ReferenceFrame>\n'%(refFrame))
msr.write('\t\t<Epoch>%s</Epoch>\n'%(epoch))
msr.write('\t\t<Vscale>1.000</Vscale>\n')
msr.write('\t\t<Pscale>1.000</Pscale>\n')
msr.write('\t\t<Lscale>1.000</Lscale>\n')
msr.write('\t\t<Hscale>1.000</Hscale>\n')
msr.write('\t\t<Total>%s</Total>\n'%(len(nonCoreStns)))
for i in range(len(nonCoreStns)):
msr.write('\t\t<First>%s</First>\n'%(coreStat))
msr.write('\t\t<Second>%s</Second>\n'%(nonCoreStns[i]))
msr.write('\t\t<GPSBaseline>\n')
msr.write('\t\t\t<X>%20.14e</X>\n'%(deltas[3*i, 0]))
msr.write('\t\t\t<Y>%20.14e</Y>\n'%(deltas[3*i+1, 0]))
msr.write('\t\t\t<Z>%20.14e</Z>\n'%(deltas[3*i+2, 0]))
msr.write('\t\t\t<SigmaXX>%20.14e</SigmaXX>\n'%(delVCV[3*i, 3*i]))
msr.write('\t\t\t<SigmaXY>%20.14e</SigmaXY>\n'%(delVCV[3*i+1, 3*i]))
msr.write('\t\t\t<SigmaXZ>%20.14e</SigmaXZ>\n'%(delVCV[3*i+2, 3*i]))
msr.write('\t\t\t<SigmaYY>%20.14e</SigmaYY>\n'%(delVCV[3*i+1, 3*i+1]))
msr.write('\t\t\t<SigmaYZ>%20.14e</SigmaYZ>\n'%(delVCV[3*i+2, 3*i+1]))
msr.write('\t\t\t<SigmaZZ>%20.14e</SigmaZZ>\n'%(delVCV[3*i+2, 3*i+2]))
for j in range(numCovar):
msr.write('\t\t\t<GPSCovariance>\n')
msr.write('\t\t\t\t<m11>%20.14e</m11>\n'% \
(delVCV[3*(i+1)+3*j, 3*i]))
msr.write('\t\t\t\t<m12>%20.14e</m12>\n'% \
(delVCV[3*(i+1)+3*j+1, 3*i]))
msr.write('\t\t\t\t<m13>%20.14e</m13>\n'% \
(delVCV[3*(i+1)+3*j+2, 3*i]))
msr.write('\t\t\t\t<m21>%20.14e</m21>\n'% \
(delVCV[3*(i+1)+3*j, 3*i+1]))
msr.write('\t\t\t\t<m22>%20.14e</m22>\n'% \
(delVCV[3*(i+1)+3*j+1, 3*i+1]))
msr.write('\t\t\t\t<m23>%20.14e</m23>\n'% \
(delVCV[3*(i+1)+3*j+2, 3*i+1]))
msr.write('\t\t\t\t<m31>%20.14e</m31>\n'% \
(delVCV[3*(i+1)+3*j, 3*i+2]))
msr.write('\t\t\t\t<m32>%20.14e</m32>\n'% \
(delVCV[3*(i+1)+3*j+1, 3*i+2]))
msr.write('\t\t\t\t<m33>%20.14e</m33>\n'% \
(delVCV[3*(i+1)+3*j+2, 3*i+2]))
msr.write('\t\t\t</GPSCovariance>\n')
numCovar -= 1
msr.write('\t\t</GPSBaseline>\n')
msr.write('\t\t<Source>%s</Source>\n'%os.path.basename(inputFile))
msr.write('\t</DnaMeasurement>\n')
stn.write('</DnaXmlFormat>\n')
msr.write('</DnaXmlFormat>\n')