-
Notifications
You must be signed in to change notification settings - Fork 2
/
incl.py
230 lines (177 loc) · 6.25 KB
/
incl.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
import json
import numpy as np
from scipy import optimize
import pdal
def get_pnts(filename):
pdal_pipe = [
filename,
{
"type":"filters.sort",
"dimension":"GpsTime"
}
]
p = pdal.Pipeline(json.dumps(pdal_pipe))
p.validate()
p.execute()
arrays = p.arrays
array = arrays[0]
return array
def get_incl(filename):
# Read in text file of RXP inclination data (time, roll, pitch)
incl = np.loadtxt(filename, delimiter=",", skiprows=1)
# Remove garbage
incl = incl[incl[:,0] > 0]
incl = incl[~np.isnan(incl[:,0])]
# Remove duplicates
garbage, idx = np.unique(incl[:,0], return_index=True)
incl = incl[idx]
t = incl[:,0]
roll = incl[:,1]
pitch = incl[:,2]
return t, roll, pitch
def filter_incl(incl, kernel_length=101):
# Blackman window kernel. Default kernel length based on prior work
# published in Journal of Glaciology.
kernel = np.blackman(kernel_length)
kernel = kernel / np.sum(kernel)
# Pad roll and pitch signals
pad_width = np.int(kernel_length/2)
start_pad = incl[0:pad_width]
start_pad = np.flip(start_pad)
start_pad = 2 * np.mean(start_pad[-10:]) - start_pad
end_pad = incl[-pad_width:]
end_pad = np.flip(end_pad)
end_pad = 2 * np.mean(end_pad[0:10]) - end_pad
padded_incl = np.hstack((start_pad, incl, end_pad))
# Filter
filtered_incl = np.convolve(padded_incl, kernel, mode='valid')
return filtered_incl
def warp_cloud(pt, x, y, z, it, roll, pitch):
# Inclination time midpoints for masking
it_mid = (it[1:] + it[:-1]) / 2
# Rotate points according to closes inclination time
xyz_rot = np.zeros((len(x), 3))
for i in range(0, len(it)):
# Mask for points closest to current inclination time
if i == 0:
mask = pt <= it_mid[i]
elif i == (len(it) - 1):
mask = pt > it_mid[i-1]
else:
mask = np.logical_and(pt > it_mid[i-1], pt <= it_mid[i])
# Apply inclination rotation
R_roll = np.array([
[1, 0, 0],
[0, np.cos(np.deg2rad(roll[i])), -np.sin(np.deg2rad(roll[i]))],
[0, np.sin(np.deg2rad(roll[i])), np.cos(np.deg2rad(roll[i]))]
])
R_pitch = np.array([
[np.cos(np.deg2rad(pitch[i])), 0, np.sin(np.deg2rad(pitch[i]))],
[0, 1, 0],
[-np.sin(np.deg2rad(pitch[i])), 0, np.cos(np.deg2rad(pitch[i]))],
])
xyz_rot[mask,0:3] = (R_pitch @ R_roll @ np.vstack((x[mask], y[mask], z[mask]))).T
x_rot = xyz_rot[:,0]
y_rot = xyz_rot[:,1]
z_rot = xyz_rot[:,2]
return x_rot, y_rot, z_rot
def save_utm(filename, array):
pdal_pipe = [
{
"type":"filters.reprojection",
"in_srs":"EPSG:7789",
"out_srs":"EPSG:32624"
},
{
"type":"writers.las",
"filename":filename
}
]
p = pdal.Pipeline(json=json.dumps(pdal_pipe), arrays=[array,])
p.validate()
p.execute()
def get_phi(it, pt, x, y):
# Near field points cause odd phi solutions
mask = np.sqrt(x**2 + y**2) > 100
pt = pt[mask]
x = x[mask]
y = y[mask]
# Indices of closest points in time to the inclination reading times
idx = np.searchsorted(pt, it)
# Handle any out of bound indices on the high side
idx[idx >= len(pt)] -= 1
# Angle in xy plane
phi = np.arctan2(y[idx], x[idx])
return np.asarray(phi)
def model(phi, a, c, d):
return a * np.sin(phi + c) + d
def fit_model(phi, incl):
# These initial values don't seem to matter much
a0 = 0
c0 = 0
d0 = 0
params, params_cov = optimize.curve_fit(model, phi, incl, p0=[a0,c0,d0])
return params
def remove_reg_trend_incl(phi, roll, pitch, roll_params, pitch_params):
# Modeled registration roll and pitch at phi values
roll_modeled = model(phi, roll_params[0], roll_params[1], roll_params[2])
pitch_modeled = model(phi, pitch_params[0], pitch_params[1], pitch_params[2])
# Remove modeled trend
roll -= roll_modeled
pitch -= pitch_modeled
return roll, pitch
def sop_pop_cloud(x, y, z, mat_file):
mat = np.loadtxt(mat_file, delimiter=" ")
xyz1 = np.vstack((x, y, z, np.ones(len(x))))
xyz_rot = (mat @ xyz1).T
x_rot = xyz_rot[:,0]
y_rot = xyz_rot[:,1]
z_rot = xyz_rot[:,2]
return x_rot, y_rot, z_rot
def save_incl(t, roll, pitch, data_dir, root, ext):
outfilename = data_dir + "/" + root + ext
np.savetxt(
outfilename,
np.column_stack((t, roll, pitch)),
"%0.4f",
delimiter=',',
header="Time,Roll,Pitch"
)
def tr_warp_adj(array, it, roll, pitch,
msa_roll_params, msa_pitch_params,
sop_file, pop_file, data_dir, basename):
# Compute scan phi (horizontal) angle
phi = get_phi(it, array["GpsTime"], array["X"], array["Y"])
# Remove MSA registration scan inclination cyclical trends
tr_roll, tr_pitch = remove_reg_trend_incl(phi, roll, pitch,
msa_roll_params, msa_pitch_params)
# Noise filter
filtered_tr_roll = filter_incl(tr_roll)
filtered_tr_pitch = filter_incl(tr_pitch)
# Apply inclination
xw, yw, zw = warp_cloud(
array["GpsTime"], array["X"], array["Y"], array["Z"],
it, filtered_tr_roll, filtered_tr_pitch
)
# Save warped points
x, y, z = sop_pop_cloud(xw, yw, zw, sop_file)
x, y, z = sop_pop_cloud(x, y, z, pop_file)
array["X"] = x
array["Y"] = y
array["Z"] = z
outfilename = data_dir + "/" + basename + "-msatrendrem-warped-utm.laz"
save_utm(outfilename, array)
# Save detrended and filtered detrended inclination
ext = "-incl-msatrendrem.txt"
save_incl(it, tr_roll, tr_pitch, data_dir, basename, ext)
ext = "-incl-msatrendrem-filtered.txt"
save_incl(it, filtered_tr_roll, filtered_tr_pitch, data_dir, basename, ext)
def no_adj(array, sop_file, pop_file, data_dir, basename):
# Save points in UTM
x, y, z = sop_pop_cloud(array["X"], array["Y"], array["Z"], sop_file)
x, y, z = sop_pop_cloud(x, y, z, pop_file)
array["X"] = x
array["Y"] = y
array["Z"] = z
outfilename = data_dir + "/" + basename + "-utm.laz"
save_utm(outfilename, array)