-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynaDiff.py
364 lines (300 loc) · 12.2 KB
/
dynaDiff.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
#!/usr/bin/env python3
# ----------------------------------------------------------------------
# DynaDiff.py
# ----------------------------------------------------------------------
# Author: Nicholas Gowans
# Date: 17 May 2019
# Purpose: To compare difference between common stations in two
# DynAdjust *xyz files.
#
# ----------------------------------------------------------------------
# Usage: Cmd:\>python DynaDiff.py <xyz_file_1> <xyz_file_2>
# ----------------------------------------------------------------------
# Notes: Updated 30 SEP 2019:
# - Reads coordinate info based on DynAdjust coordinate type option
# - Print error message if .xyz files are missing essential coord types
# - Write point differences shapefile
#
# Updated 02 OCT 2019:
# - Altered read_switch trigger for different coord_type formats
# - Incorporated read_xyz function from Josh Batchelor
# ----------------------------------------------------------------------
import os
import math
import geodepy.convert as gc
import shapefile
dateUpdated = '20190210'
mandatory_coord_types = 'ENzPLHh'
xyz1_file = os.sys.argv[1]
xyz2_file = os.sys.argv[2]
output_rpt_file = '{:s}-{:s}_DynaDiff.rpt'.format(xyz2_file, xyz1_file)
output_csv_file = '{:s}-{:s}_DynaDiff.csv'.format(xyz2_file, xyz1_file)
def shortname(filename):
"""returns the string truncated at the first period"""
count = 0
len_fn = len(filename)
for l in filename:
count += 1
if l == '.':
short = filename[:count - 1]
break
# no period in file name
if count == len_fn:
short = filename
return short
# pyshp truncates shapefile names at the first period.
# Provide a meaningful name, with no periods.
short1 = shortname(xyz1_file)
short2 = shortname(xyz2_file)
shp_file = '{:s}-{:s}_p'.format(short1, short2)
# open output files, and print header information
rep_fh = open(output_rpt_file, 'w')
csv_fh = open(output_csv_file, 'w')
headerStr = '-'*80 + '\n'
headerStr += 'DynaDiff.py Report File\n'
headerStr += '-'*30 + '\n'
headerStr += 'Coordinates differences computed by File 2 minus File 1\n'
headerStr += 'File 1: ' + xyz1_file + '\n'
headerStr += 'File 2: ' + xyz2_file + '\n'
headerStr += 'Version: ' + dateUpdated + '\n'
headerStr += '-'*80 + '\n'
print(headerStr, file=rep_fh)
csvHeaderStr = 'Station,Latitude,Longitude,dE,dN,Dist,Brg,dZ,dOHGT,dEHGT,dSD_E,dSD_N,dSD_U'
print(csvHeaderStr, file=csv_fh)
# initialisations
coords1 = {}
coords2 = {}
# ----------------------------------------------------------------------
# Read in First and Second xyz files
# ----------------------------------------------------------------------
def read_xyz(xyz_file, read_switch=False):
coords = {}
print()
print(' Reading {:s}'.format(xyz_file))
with open(xyz_file, 'r') as xyz_fh:
for line in xyz_fh:
# skip empty lines
if line == '':
continue
if line == '\n':
continue
if read_switch is False:
if line[:35] == 'Version: ':
vers = line[35:].strip()
if line[:35] == 'File name: ':
file = line[35:].strip()
if line[:35] == 'Reference frame: ':
ref_frame = line[35:].strip()
if line[:35] == 'Epoch: ':
epoch = line[35:].strip()
if line[:35] == 'Geoid model: ':
geoid = line[35:].strip()
if line[:35] == 'Station coordinate types: ':
coord_types = line[35:].strip()
for l in mandatory_coord_types:
if l not in coord_types:
print('*' * 20)
print(' Warning! Mandatory coordinate types not present in {:s}'.format(xyz_file))
print(' .xyz file must contain coord types ENzPLHh')
print('')
print('Exiting...')
exit()
if line[87:88] == '-':
read_switch = True
continue
if read_switch:
stn = line[0:20].strip()
results = line[25:].split()
r_count = 0
for ct in coord_types:
if ct == 'E':
E = float(results[r_count])
if ct == 'N':
N = float(results[r_count])
if ct == 'z':
z = int(results[r_count])
if ct == 'P':
P = gc.hp2dec(float(results[r_count]))
if ct == 'L':
L = gc.hp2dec(float(results[r_count]))
if ct == 'H':
H = float(results[r_count])
if ct == 'h':
h = float(results[r_count])
# Cartesian coords disabled for now
# if ct == 'X':
# X = float(results[r_count])
# if ct == 'Y':
# Y = float(results[r_count])
# if ct == 'Z':
# Z = float(results[r_count])
r_count += 1
# Don't forget about the qualities
SE = float(results[r_count])
SN = float(results[r_count + 1])
SU = float(results[r_count + 2])
coords[stn] = {'E': E,
'N': N,
'Z': z,
'LAT': P,
'LON': L,
'OHGT': H,
'EHGT': h,
'SE': SE,
'SN': SN,
'SU': SU
}
return vers, file, ref_frame, epoch, geoid, coord_types, coords
vers1, file1, ref_frame1, epoch1, geoid1, coord_types1, coords1 = read_xyz(xyz1_file)
vers2, file2, ref_frame2, epoch2, geoid2, coord_types2, coords2 = read_xyz(xyz2_file)
print()
print(' Read:')
print(' - {:8d} stations in {:s}'.format(len(coords1), xyz1_file))
print(' - {:8d} stations in {:s}'.format(len(coords2), xyz2_file))
# ----------------------------------------------------------------------
# Print warnings
# ----------------------------------------------------------------------
warn_str = ''
if vers1 != vers2:
warn_str = warn_str + ' * Different version of DynAdjust used between adjustments.\n'
if ref_frame1 != ref_frame2:
warn_str = warn_str + ' * Reference frame differs between adjustments.\n'
if epoch1 != epoch2:
warn_str = warn_str + ' * Epoch differs between adjustments.\n'
if geoid1 != geoid2:
warn_str = warn_str + ' * Geoid model differs between adjustments.\n'
if coord_types1 != coord_types2:
warn_str = warn_str + ' * Different coordinate types printed between adjustments.\n'
print()
print('-'*20, file=rep_fh)
print('WARNINGS:', file=rep_fh)
print('-'*20, file=rep_fh)
if warn_str != '':
rep_fh.write(warn_str)
else:
rep_fh.write(' None\n')
print('-'*20, file=rep_fh)
print('', file=rep_fh)
# ----------------------------------------------------------------------
# Compare coordinates
# ----------------------------------------------------------------------
diffs = {}
dist_list = []
maxDist = 0
minDist = 0
for stn in coords1:
if stn in coords2:
dE = coords2[stn]['E'] - coords1[stn]['E']
dN = coords2[stn]['N'] - coords1[stn]['N']
dZ = coords2[stn]['Z'] - coords1[stn]['Z']
dOHGT = coords2[stn]['OHGT'] - coords1[stn]['OHGT']
dEHGT = coords2[stn]['EHGT'] - coords1[stn]['EHGT']
dSE = coords2[stn]['SE'] - coords1[stn]['SE']
dSN = coords2[stn]['SN'] - coords1[stn]['SN']
dSU = coords2[stn]['SU'] - coords1[stn]['SU']
dist = math.sqrt(dE**2 + dN**2)
brg = (180 / math.pi) * math.atan2(dE, dN)
brg = brg % 360
if dist < minDist:
minDist = dist
if dist > maxDist:
maxDist = dist
dist_list.append(dist)
diffs[stn] = {'dE': dE,
'dN': dN,
'DIST': dist,
'BRG': brg,
'dZ': dZ,
'dOHGT': dOHGT,
'dEHGT': dEHGT,
'dSE': dSE,
'dSN': dSN,
'dSU': dSU
}
# ----------------------------------------------------------------------
# Print coordinate differences
# ----------------------------------------------------------------------
common_stns = len(diffs)
print('Station Lat (dd) Lon (dd) dE dN Dist Brg dZ dOHGT '
'dEHGT dSD_E dSD_N dSD_U', file=rep_fh)
print('-'*145,file=rep_fh)
for stn in diffs:
print('{:20s}{:15.8f}{:15.8f}{:10.4f}{:10.4f}{:10.4f}{:8.1f}{:5d}{:10.4f}{:10.4f}{:10.4f}{:10.4f}{:10.4f}'
.format(stn,
coords1[stn]['LAT'],
coords1[stn]['LON'],
diffs[stn]['dE'],
diffs[stn]['dN'],
diffs[stn]['DIST'],
diffs[stn]['BRG'],
diffs[stn]['dZ'],
diffs[stn]['dOHGT'],
diffs[stn]['dEHGT'],
diffs[stn]['dSE'],
diffs[stn]['dSN'],
diffs[stn]['dSU'],),
file=rep_fh)
print('{:s},{:0.8f},{:0.8f},{:0.4f},{:0.4f},{:0.4f},{:0.1f},{:d},{:0.4f},{:0.4f},{:0.4f},{:0.4f},{:0.4f}'
.format(stn,
coords1[stn]['LAT'],
coords1[stn]['LON'],
diffs[stn]['dE'],
diffs[stn]['dN'],
diffs[stn]['DIST'],
diffs[stn]['BRG'],
diffs[stn]['dZ'],
diffs[stn]['dOHGT'],
diffs[stn]['dEHGT'],
diffs[stn]['dSE'],
diffs[stn]['dSN'],
diffs[stn]['dSU'],),
file=csv_fh)
rep_fh.close()
csv_fh.close()
# ----------------------------------------------------------------------
# write shapefile
# ----------------------------------------------------------------------
if common_stns > 0:
print(" writing point shapefile ...")
w = shapefile.Writer(shp_file, shapeType=1) # type 1 for points.
w.autoBalance = 1
w.field('Station', 'C', size=20)
w.field('Latitude', 'N', decimal=10)
w.field('Longitude', 'N', decimal=10)
w.field('dEast', 'N', decimal=4)
w.field('dNorth', 'N', decimal=4)
w.field('dist', 'N', decimal=4)
w.field('brg', 'N', decimal=4)
w.field('dZone', 'N', decimal=4)
w.field('dOHGT', 'N', decimal=4)
w.field('dEHGT', 'N', decimal=4)
w.field('dSD_E', 'N', decimal=4)
w.field('dSD_N', 'N', decimal=4)
w.field('dSD_U', 'N', decimal=4)
# write out point for each dictionary entry
for stn in diffs:
w.point(coords1[stn]['LON'], coords1[stn]['LAT'])
w.record(stn,
float(coords1[stn]['LAT']),
float(coords1[stn]['LON']),
float(diffs[stn]['dE']),
float(diffs[stn]['dN']),
float(diffs[stn]['DIST']),
float(diffs[stn]['BRG']),
float(diffs[stn]['dZ']),
float(diffs[stn]['dOHGT']),
float(diffs[stn]['dEHGT']),
float(diffs[stn]['dSE']),
float(diffs[stn]['dSN']),
float(diffs[stn]['dSU'])
)
w.close()
# ----------------------------------------------------------------------
# Print summary
# ----------------------------------------------------------------------
print()
print('-'*40)
print(' Compared {:d} common stations'.format(common_stns))
print(' Max hz change: {:10.4f}'.format(maxDist))
print(' Min hz change: {:10.4f}'.format(minDist))
print('')