-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmocalum_tutorial.py
More file actions
283 lines (220 loc) · 8.3 KB
/
Copy pathmocalum_tutorial.py
File metadata and controls
283 lines (220 loc) · 8.3 KB
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
import matplotlib.pyplot as plt
from matplotlib.pyplot import Polygon
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.patches as mpatches
import matplotlib.lines as mlines
import numpy as np
from numpy.linalg import inv as inv
def plot_sd_scan_setup(lidar_id, mc_obj):
"""
Plots 2D geometry of single lidar scan
Parameters
----------
lidar_id : str
Id of lidar
mc_obj : mocalum
Instance of mocalum class
"""
no_los = mc_obj.data.meas_cfg[lidar_id]['config']['no_los']
meas_pts= mc_obj._get_prob_cords(lidar_id)[:no_los]
lidar_pos = mc_obj.data.meas_cfg[lidar_id]['position']
fig, ax = plt.subplots(figsize=(7.5, 7.5))
plt.grid()
plt.scatter(meas_pts[:,0], meas_pts[:,1], c="blue",
label='measurements',zorder=10)
for i,pt in enumerate(meas_pts):
if i==0:
plt.plot([lidar_pos[0],pt[0]],[lidar_pos[1],pt[1]],
c='black',alpha=0.4, label='beam')
else:
plt.plot([lidar_pos[0],pt[0]],[lidar_pos[1],pt[1]],
c='black',alpha=0.4)
plt.scatter(lidar_pos[0],lidar_pos[1], c="green", label=lidar_id,zorder=150)
ax.set_aspect('equal')
plt.legend(loc="upper left")
plt.xlabel('Easting [m]')
plt.ylabel('Northing [m]')
plt.show()
def plot_md_scan_setup(lidar_ids, mc_obj):
"""
Plots 2D geometry of multi-lidar scan
Parameters
----------
lidar_ids: list
List of strings corresponding to lidar ids
mc_obj : mocalum
Instance of mocalum class
"""
no_los = mc_obj.data.meas_cfg[lidar_ids[0]]['config']['no_los']
meas_pts= mc_obj._get_prob_cords(lidar_ids[0])[:no_los]
lidar_pos = []
for id in lidar_ids:
lidar_pos += [mc_obj.data.meas_cfg[id]['position']]
fig, ax = plt.subplots(figsize=(7.5, 7.5))
plt.grid()
plt.scatter(meas_pts[:,0], meas_pts[:,1], c="blue",
label='measurements',zorder=10)
colors = ["green", "orange", "purple"]
for j,id in enumerate(lidar_ids):
plt.scatter(lidar_pos[j][0],
lidar_pos[j][1], c=colors[j], label=id,zorder=150)
for i,pt in enumerate(meas_pts):
if i==0 and j==0:
plt.plot([lidar_pos[j][0],pt[0]],[lidar_pos[j][1],pt[1]],
c='black',alpha=0.4, label='beam')
else:
plt.plot([lidar_pos[j][0],pt[0]],[lidar_pos[j][1],pt[1]],
c='black',alpha=0.4)
ax.set_aspect('equal')
plt.legend(loc="lower right")
plt.xlabel('Easting [m]')
plt.ylabel('Northing [m]')
plt.show()
def spher2cart(azimuth, elevation, radius):
"""Converts spherical coordinates to Cartesian coordinates
Parameters
----------
azimuth : numpy
Array containing azimuth angles
elevation : numpy
Array containing elevation angles
radius : numpy
Array containing radius angles
Returns
-------
x : numpy
Array containing x coordinate values.
y : numpy
Array containing y coordinate values.
z : numpy
Array containing z coordinate values.
Raises
------
TypeError
If dimensions of inputs are not the same
"""
azimuth = 90 - azimuth # converts to 'Cartesian' angle
azimuth = np.radians(azimuth) # converts to radians
elevation = np.radians(elevation)
try:
x = radius * np.cos(azimuth) * np.cos(elevation)
y = radius * np.sin(azimuth) * np.cos(elevation)
z = radius * np.sin(elevation)
return x, y, z
except:
raise TypeError('Dimensions of inputs are not the same!')
def bbox_pts_from_cfg(cfg):
"""
Creates 2D bounding box points from bounding box config dictionary
Parameters
----------
cfg : dict
Bounding box dictionary
Returns
-------
numpy
Numpy array of shape (2,2) corresponding to 4 corners of 2D bounding box
"""
bbox_pts = np.full((4,2), np.nan)
bbox_pts[0] = np.array([cfg['x']['min'],
cfg['y']['min']])
bbox_pts[1] = np.array([cfg['x']['min'],
cfg['y']['max']])
bbox_pts[2] = np.array([cfg['x']['max'],
cfg['y']['max']])
bbox_pts[3] = np.array([cfg['x']['max'],
cfg['y']['min']])
return bbox_pts
def plot_bbox(mc_obj):
"""
Plots 2D geometry of lidar scan and flow field box
Parameters
----------
mc_obj : mocalum
Instance of mocalum class
"""
flow_id = mc_obj.data.ffield.generator
lidar_ids = mc_obj.data.bbox_ffield[flow_id]['linked_lidars']
no_los = mc_obj.data.meas_cfg[lidar_ids[0]]['config']['no_los']
meas_pts= mc_obj._get_prob_cords(lidar_ids[0])[:no_los]
bbox_pts = bbox_pts_from_cfg(mc_obj.data.bbox_ffield[flow_id])
diag = np.abs(bbox_pts[0]-bbox_pts[2]).max()
R_tb = mc_obj.data.bbox_ffield[flow_id]['CRS']['rot_matrix']
min_bbox_pts = bbox_pts.dot(inv(R_tb))
bbox_c = min_bbox_pts.mean(axis = 0)
wind_dir = mc_obj.data.fmodel_cfg['wind_from_direction']
flowbox = Polygon(min_bbox_pts,alpha=0.4, color='grey', label="flow field bbox")
wind_dir_pt = spher2cart(wind_dir,0,diag/2)[:2]
lidar_pos = []
for id in lidar_ids:
lidar_pos += [mc_obj.data.meas_cfg[id]['position']]
fig, ax = plt.subplots(figsize=(7.5, 7.5))
plt.grid()
plt.arrow(bbox_c[0], bbox_c[1],-wind_dir_pt[0],-wind_dir_pt[1],
width=8,color="red", label='wind',zorder=50)
plt.scatter(meas_pts[:,0], meas_pts[:,1], c="blue",
label='measurements',zorder=10)
colors = ["green", "orange", "purple"]
for j,id in enumerate(lidar_ids):
plt.scatter(lidar_pos[j][0],
lidar_pos[j][1], c=colors[j], label=id,zorder=150)
for i,pt in enumerate(meas_pts):
if i==0 and j==0:
plt.plot([lidar_pos[j][0],pt[0]],[lidar_pos[j][1],pt[1]],
c='black',alpha=0.4, label='beam')
else:
plt.plot([lidar_pos[j][0],pt[0]],[lidar_pos[j][1],pt[1]],
c='black',alpha=0.4)
ax.add_patch(flowbox)
ax.set_aspect('equal')
plt.legend(loc="lower right")
plt.xlabel('Easting [m]')
plt.ylabel('Northing [m]')
plt.show()
def plot_ffield(mc_obj):
"""
Plots 2D geometry of lidar scan and flow field box
Parameters
----------
mc_obj : mocalum
Instance of mocalum class
"""
flow_id = mc_obj.data.ffield.generator
lidar_ids = mc_obj.data.bbox_ffield[flow_id]['linked_lidars']
no_los = mc_obj.data.meas_cfg[lidar_ids[0]]['config']['no_los']
meas_pts= mc_obj._get_prob_cords(lidar_ids[0])[:no_los]
bbox_pts = bbox_pts_from_cfg(mc_obj.data.bbox_ffield[flow_id])
diag = np.abs(bbox_pts[0]-bbox_pts[2]).max()
R_tb = mc_obj.data.bbox_ffield[flow_id]['CRS']['rot_matrix']
min_bbox_pts = bbox_pts.dot(inv(R_tb))
bbox_c = min_bbox_pts.mean(axis = 0)
wind_dir = mc_obj.data.fmodel_cfg['wind_from_direction']
flowbox = Polygon(min_bbox_pts,alpha=0.4, color='grey', label="flow field bbox")
wind_dir_pt = spher2cart(wind_dir,0,diag/2)[:2]
lidar_pos = []
for id in lidar_ids:
lidar_pos += [mc_obj.data.meas_cfg[id]['position']]
fig, ax = plt.subplots(figsize=(7.5, 7.5))
plt.grid()
mc_obj.data.ffield.u.isel(z=0, time=0).plot.pcolormesh('Easting', 'Northing',ax=ax,cmap='Greys')
plt.arrow(bbox_c[0], bbox_c[1],-wind_dir_pt[0],-wind_dir_pt[1],
width=8,color="red", label='wind',zorder=50)
plt.scatter(meas_pts[:,0], meas_pts[:,1], c="blue",
label='measurements',zorder=10)
colors = ["green", "orange", "purple"]
for j,id in enumerate(lidar_ids):
plt.scatter(lidar_pos[j][0],
lidar_pos[j][1], c=colors[j], label=id,zorder=150)
for i,pt in enumerate(meas_pts):
if i==0 and j==0:
plt.plot([lidar_pos[j][0],pt[0]],[lidar_pos[j][1],pt[1]],
c='black',alpha=0.4, label='beam')
else:
plt.plot([lidar_pos[j][0],pt[0]],[lidar_pos[j][1],pt[1]],
c='black',alpha=0.4)
ax.add_patch(flowbox)
ax.set_aspect('equal')
plt.legend(loc="lower right")
plt.xlabel('Easting [m]')
plt.ylabel('Northing [m]')
plt.show()