Repository navigation
Expand file tree
/
Copy pathAbstractMagneticMap.m
More file actions
484 lines (407 loc) · 22.1 KB
/
Copy pathAbstractMagneticMap.m
File metadata and controls
484 lines (407 loc) · 22.1 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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
classdef (Abstract) AbstractMagneticMap < handle
%ABSTRACTMAGNETICMAP Superclass for magnetic maps
% Required add-ons (use MATLAB's Add-On Explorer to install):
% - Mapping Toolbox
% - getContourLineCoordinates (from MathWorks File Exchange)
% Optional add-ons (use MATLAB's Add-On Explorer to install):
% - Parallel Computing Toolbox (install for potential speed gains)
properties
magmodel MagneticModel
agent Agent
ax
colors
trajectory
start
goal
position
level_curves
level_curves_type
end
methods (Abstract)
InitializeAxes(obj, varargin)
line = AddLine(obj, lat, lon, linespec, varargin)
[new_lat, new_lon] = CleanLatLon(obj, lat, lon)
end
methods
function obj = AbstractMagneticMap(magmodel, agent, varargin)
%ABSTRACTMAGNETICMAP Construct an instance of this class
obj.magmodel = magmodel;
obj.agent = agent;
obj.colors.agent.start = 'b';
obj.colors.agent.goal = 'g';
obj.colors.agent.position = 'm';
obj.colors.agent.trajectory = 'm';
% obj.colors.contours.I_INCL = "#02B187"; % green from Taylor (2018)
% obj.colors.contours.I_INCL_0 = obj.colors.contours.I_INCL;
% obj.colors.contours.F_TOTAL = "#8212B4"; % purple from Taylor (2018)
obj.colors.contours.I_INCL = 'w'; % white
obj.colors.contours.I_INCL_0 = "#FF8080"; % light red, magnetic equator
obj.colors.contours.F_TOTAL = "#444444"; % dark gray
% obj.colors.contours.I_INCL = "#EB3725"; % red from Boström et al. (2012)
% obj.colors.contours.I_INCL_0 = obj.colors.contours.I_INCL;
% obj.colors.contours.F_TOTAL = '#0085C9'; % blue from Boström et al. (2012)
obj.colors.parallelgradients = "#FF8000"; % orange
obj.colors.nullclines.x = obj.colors.contours.F_TOTAL;
obj.colors.nullclines.y = obj.colors.contours.I_INCL;
% obj.colors.nullclines.x = "#FF0000"; % red
% obj.colors.nullclines.y = "#FF8000"; % orange
obj.InitializeAxes(varargin{:});
obj.SetLevelCurves("contours");
% obj.SetLevelCurves("parallelgradients");
% obj.SetLevelCurves("nullclines");
obj.AddAgentPlots;
addlistener(obj.agent, "NavigationChanged", @obj.DrawNullclinePlots);
addlistener(obj.agent, "GoalChanged", @obj.DrawNullclinePlots);
% addlistener(obj.agent, "VelocitiesChanged", @obj.DrawNullclinePlots);
end
function SetLevelCurves(obj, level_curves_type)
%SETLEVELCURVES ...
obj.level_curves_type = level_curves_type;
switch obj.level_curves_type
case "none"
delete(obj.level_curves); obj.level_curves = [];
case "contours"
obj.DrawContourPlots();
case "parallelgradients"
obj.DrawParallelGradientsPlot();
case "nullclines"
obj.DrawNullclinePlots();
end
end
function DrawContourPlots(obj)
%DRAWCONTOURPLOTS Add magnetic property contours to map
if obj.level_curves_type == "contours"
delete(obj.level_curves); obj.level_curves = [];
interpm_maxdiff = 1; % degrees (or nan to skip contour interpolation)
% interpm_maxdiff = nan; % degrees (or nan to skip contour interpolation)
% add contours
for param = ["I_INCL", "F_TOTAL"]
contour_table = obj.magmodel.contour_tables.(param);
nContours = max(contour_table.Group);
% plot each contour level one at a time
for i = 1:nContours
gidx = contour_table.Group == i;
level = unique(contour_table.Level(gidx));
% set line width
linewidth = 0.5; % default
switch param
case "I_INCL"
if mod(level, 30) == 0
linewidth = 3; % multiples of 30 degrees
else
if mod(level, 10) == 0
linewidth = 2; % multiples of 10 degrees
end
end
case "F_TOTAL"
if mod(level, 10) == 0
linewidth = 2; % multiples of 10 microtesla
end
end
% set color
color = obj.colors.contours.(param);
if param == "I_INCL" && level == 0
color = obj.colors.contours.I_INCL_0;
end
% set zorder
zorder = 2;
switch param
case "I_INCL"
zorder = 2.1;
case "F_TOTAL"
zorder = 2.2;
end
% get contour line coordinates
contour_lat = contour_table.Y(gidx);
contour_lon = contour_table.X(gidx);
if ~isnan(interpm_maxdiff)
% interpolate points on the contour line if necessary
[contour_lat, contour_lon] = interpm(contour_lat, contour_lon, interpm_maxdiff, 'gc');
end
% prepare data tooltips and other metadata
% note: lines can be accessed later via tags, e.g., findobj(obj.ax, "Tag", "I_INCL = 0")
tag = [char(string(param)), ' = ', char(string(level))];
datatipvalues = level*ones(size(contour_lat));
datatiplabel = param.replace('_', '\_'); % default
datatipformat = 'auto'; % default
switch param
case "I_INCL"
datatiplabel = "Inclination";
datatipformat = 'degrees';
case "F_TOTAL"
datatiplabel = "Intensity";
datatipformat = '%g μT';
end
% plot contour line
line = obj.AddLine( ...
contour_lat, contour_lon, ...
'-', LineWidth=linewidth, Color=color, ...
Tag=tag, ZOrder=zorder ...
);
if isprop(line, "DataTipTemplate")
% add tooltips if the axes support them
line.DataTipTemplate.DataTipRows(end+1) = dataTipTextRow(datatiplabel, datatipvalues, datatipformat);
end
obj.level_curves = [obj.level_curves; line];
end
end
% update graphics layering
obj.SortZStack();
end
end
function DrawParallelGradientsPlot(obj, ~, ~)
%DRAWPARALLELGRADIENTSPLOT Add contour lines where the inclination and intensity gradients are parallel to map
if obj.level_curves_type == "parallelgradients"
delete(obj.level_curves); obj.level_curves = [];
interpm_maxdiff = 1; % degrees (or nan to skip contour interpolation)
% interpm_maxdiff = nan; % degrees (or nan to skip contour interpolation)
lat = obj.magmodel.sample_latitudes;
lon = obj.magmodel.sample_longitudes;
orthogonality = obj.magmodel.sample_orthogonality;
% locate parallel gradients contours (orthogonality contours with level -180, 0, or 180 degrees)
contours = contourc(lon, lat, orthogonality, [-180, 0, 180]);
contour_table = getContourLineCoordinates(contours);
% collect contour coordinates
parallel_x = [];
parallel_y = [];
nContours = max(contour_table.Group);
for i = 1:nContours
gidx = contour_table.Group == i;
% get contour coordinates
contour_lat = contour_table.Y(gidx);
contour_lon = contour_table.X(gidx);
if ~isnan(interpm_maxdiff)
% interpolate points on the contour line if necessary
[contour_lat, contour_lon] = interpm(contour_lat, contour_lon, interpm_maxdiff, 'gc');
end
% store multiple contour lines with same level in a
% single pair of arrays to reduce the number of lines
% drawn
% - nan is appended to each contour line to prevent
% connections being drawn at contour discontinuities
parallel_x = [parallel_x; contour_lon; nan];
parallel_y = [parallel_y; contour_lat; nan];
end
% plot parallel gradients contour
tag = "Orthogonality = 0 deg";
datatiplabel = "PARALLEL GRADIENTS";
line = obj.AddLine( ...
parallel_y, parallel_x, ...
'-', LineWidth=2, Color=obj.colors.parallelgradients, ...
Tag=tag, ZOrder=2 ...
);
try
line.LineStyle = ':';
catch
% will fail for GeographicGlobe which does not support
% dashed or dotted lines
end
if isprop(line, "DataTipTemplate")
% add tooltips if the axes support them
line.DataTipTemplate.DataTipRows = [dataTipTextRow(datatiplabel, ''); line.DataTipTemplate.DataTipRows];
end
obj.level_curves = [obj.level_curves; line];
% update graphics layering
obj.SortZStack();
end
end
function DrawNullclinePlots(obj, ~, ~)
%DRAWNULLCLINEPLOTS Add velocity nullclines to map
if obj.level_curves_type == "nullclines"
delete(obj.level_curves); obj.level_curves = [];
obj.agent.ComputeVelocities();
interpm_maxdiff = 1; % degrees (or nan to skip contour interpolation)
% interpm_maxdiff = nan; % degrees (or nan to skip contour interpolation)
lat = obj.magmodel.sample_latitudes;
lon = obj.magmodel.sample_longitudes;
dlon = squeeze(obj.agent.sample_velocities(1, :, :));
dlat = squeeze(obj.agent.sample_velocities(2, :, :));
% locate velocity nullclines (velocity contours with level 0)
dlat_contours = contourc(lon, lat, dlat, [0, 0]); % contourc requires an array of at least two levels, but repeats are allowed
dlon_contours = contourc(lon, lat, dlon, [0, 0]); % contourc requires an array of at least two levels, but repeats are allowed
dlat_table = getContourLineCoordinates(dlat_contours);
dlon_table = getContourLineCoordinates(dlon_contours);
% collect nullcline coordinates
dlat_x = [];
dlat_y = [];
dlon_x = [];
dlon_y = [];
for param = ["dlat", "dlon"]
switch param
case "dlat"
contour_table = dlat_table;
case "dlon"
contour_table = dlon_table;
end
nContours = max(contour_table.Group);
for i = 1:nContours
gidx = contour_table.Group == i;
% get nullcline coordinates
contour_lat = contour_table.Y(gidx);
contour_lon = contour_table.X(gidx);
if ~isnan(interpm_maxdiff)
% interpolate points on the contour line if necessary
[contour_lat, contour_lon] = interpm(contour_lat, contour_lon, interpm_maxdiff, 'gc');
end
% store coordinates for equilibria detection
% - nan is appended to each contour line to prevent
% spurious intersection detection at nullcline
% discontinuities
switch param
case "dlat"
dlat_x = [dlat_x; contour_lon; nan];
dlat_y = [dlat_y; contour_lat; nan];
case "dlon"
dlon_x = [dlon_x; contour_lon; nan];
dlon_y = [dlon_y; contour_lat; nan];
end
end
end
% plot x-nullcline (E/W-nullcline, dlon=0)
tag = "Nullcline dlon = 0";
datatiplabel = "NULLCLINE (E/W speed = 0)";
line = obj.AddLine( ...
dlon_y, dlon_x, ...
'-', LineWidth=2, Color=obj.colors.nullclines.x, ...
Tag=tag, ZOrder=2.2 ...
);
if isprop(line, "DataTipTemplate")
% add tooltips if the axes support them
line.DataTipTemplate.DataTipRows = [dataTipTextRow(datatiplabel, ''); line.DataTipTemplate.DataTipRows];
end
obj.level_curves = [obj.level_curves; line];
% plot y-nullcline (N/S nullcline, dlat=0)
tag = "Nullcline dlat = 0";
datatiplabel = "NULLCLINE (N/S speed = 0)";
line = obj.AddLine( ...
dlat_y, dlat_x, ...
'-', LineWidth=2, Color=obj.colors.nullclines.y, ...
Tag=tag, ZOrder=2.1 ...
);
if isprop(line, "DataTipTemplate")
% add tooltips if the axes support them
line.DataTipTemplate.DataTipRows = [dataTipTextRow(datatiplabel, ''); line.DataTipTemplate.DataTipRows];
end
obj.level_curves = [obj.level_curves; line];
% locate equilibria at the intersections of nullclines
[intersections_lon, intersections_lat] = polyxpoly(dlat_x, dlat_y, dlon_x, dlon_y);
% disp("eq pt lat eq pt lon");
% disp([intersections_lat, intersections_lon]);
% plot nullclines intersections
try
line = obj.AddLine( ...
intersections_lat, intersections_lon, ...
'ro', MarkerSize=3, LineWidth=2, ...
Tag="Nullclines Intersections", ZOrder=2.3 ...
);
if isprop(line, "DataTipTemplate")
% add tooltips if the axes support them
line.DataTipTemplate.DataTipRows = [dataTipTextRow('EQUILIBRIUM POINT', ''); line.DataTipTemplate.DataTipRows];
end
obj.level_curves = [obj.level_curves; line];
catch
% will fail if there are no intersections
end
% update graphics layering
obj.SortZStack();
end
end
function AddAgentPlots(obj)
%ADDAGENTPLOTS Add agent trajectory and markers to map
% plot trajectory
% coords are cleaned up here since otherwise the 2D plot would
% draw the trajectory off screen when longitude is outside
% [-180, 180] (3D plot does not need this correction)
[new_lat, new_lon] = obj.CleanLatLon(obj.agent.trajectory_lat, obj.agent.trajectory_lon);
obj.trajectory = obj.AddLine(new_lat, new_lon, '-', Tag="Agent Trajectory", Color=obj.colors.agent.trajectory, MarkerSize=2, LineWidth=2, ZOrder=10);
% plot markers for agent start, goal, and current position
obj.start = obj.AddLine(obj.agent.start_lat, obj.agent.start_lon, 'o', Tag="Agent Start", Color=obj.colors.agent.start, MarkerSize=8, LineWidth=2, ZOrder=11);
obj.goal = obj.AddLine(obj.agent.goal_lat, obj.agent.goal_lon, 'o', Tag="Agent Goal", Color=obj.colors.agent.goal, MarkerSize=8, LineWidth=2, ZOrder=12);
obj.position = obj.AddLine(obj.agent.trajectory_lat(end), wrapTo180(obj.agent.trajectory_lon(end)), 'o', Tag="Agent Position", Color=obj.colors.agent.position, MarkerSize=8, LineWidth=2, ZOrder=13);
% add tooltips if the axes support them
if isprop(obj.start, "DataTipTemplate")
obj.start.DataTipTemplate.DataTipRows = [dataTipTextRow('START', ''); obj.start.DataTipTemplate.DataTipRows];
obj.start.DataTipTemplate.DataTipRows(end+1) = dataTipTextRow('Inclination', @(~) obj.agent.start_I_INCL, 'degrees');
obj.start.DataTipTemplate.DataTipRows(end+1) = dataTipTextRow('Intensity', @(~) obj.agent.start_F_TOTAL, '%g μT');
obj.goal.DataTipTemplate.DataTipRows = [dataTipTextRow('GOAL', ''); obj.goal.DataTipTemplate.DataTipRows];
obj.goal.DataTipTemplate.DataTipRows(end+1) = dataTipTextRow('Inclination', @(~) obj.agent.goal_I_INCL, 'degrees');
obj.goal.DataTipTemplate.DataTipRows(end+1) = dataTipTextRow('Intensity', @(~) obj.agent.goal_F_TOTAL, '%g μT');
obj.position.DataTipTemplate.DataTipRows = [dataTipTextRow('AGENT', ''); obj.position.DataTipTemplate.DataTipRows];
obj.position.DataTipTemplate.DataTipRows(end+1) = dataTipTextRow('Inclination', @(~) obj.agent.current_I_INCL, 'degrees');
obj.position.DataTipTemplate.DataTipRows(end+1) = dataTipTextRow('Intensity', @(~) obj.agent.current_F_TOTAL, '%g μT');
end
% update plots when agent changes
addlistener(obj.agent, 'StartChanged', @obj.UpdateAgentStart);
addlistener(obj.agent, 'GoalChanged', @obj.UpdateAgentGoal);
addlistener(obj.agent, 'TrajectoryChanged', @obj.UpdateAgentTrajectory);
% update graphics layering
obj.SortZStack();
end
function ToggleAgentTrajectoryMarkers(obj)
%TOGGLEAGENTTRAJECTORYMARKERS Toggle the display of markers for every trajectory step
if obj.trajectory.Marker == "none"
obj.trajectory.Marker = 'o';
else
obj.trajectory.Marker = 'none';
end
drawnow; % force figure to update immediately
end
function UpdateAgentStart(obj, ~, ~)
%UPDATEAGENTSTART Update marker for agent start
obj.start.LatitudeData = obj.agent.start_lat;
obj.start.LongitudeData = obj.agent.start_lon;
drawnow; % force figure to update immediately
end
function UpdateAgentGoal(obj, ~, ~)
%UPDATEAGENTGOAL Update marker for agent goal
obj.goal.LatitudeData = obj.agent.goal_lat;
obj.goal.LongitudeData = obj.agent.goal_lon;
drawnow; % force figure to update immediately
end
function UpdateAgentTrajectory(obj, ~, ~)
%UPDATEAGENTTRAJECTORY Update plots/markers of agent trajectory and current position
% coords are cleaned up here since otherwise the 2D plot would
% draw the trajectory off screen when longitude is outside
% [-180, 180] (3D plot does not need this correction)
[new_lat, new_lon] = obj.CleanLatLon(obj.agent.trajectory_lat, obj.agent.trajectory_lon);
obj.trajectory.LatitudeData = new_lat;
obj.trajectory.LongitudeData = new_lon;
obj.position.LatitudeData = new_lat(end);
obj.position.LongitudeData = new_lon(end);
drawnow; % force figure to update immediately
end
function SortZStack(obj)
%SORTZSTACK Sort graphics elements according to ZOrder (greater values on top)
children = obj.ax.Children;
zorders = nan(length(children), 1);
for i = 1:length(children)
if isfield(children(i).UserData, "ZOrder")
zorders(i) = children(i).UserData.ZOrder;
else
zorders(i) = 1; % default
end
end
[~, sortidx] = sort(zorders);
sortidx = sortidx(end:-1:1); % reverse to get greatest to smallest ZOrder
obj.ax.Children = children(sortidx);
drawnow; % force figure to update immediately
end
function [value, newvarargs] = PopArg(~, varargs, name, default)
%POPARG Extract one name-value pair from a function varargin
% convert varargs from a cell array to a struct
s = struct(varargs{:});
if isfield(s, name)
% extract value of named property if it exists
value = s.(name);
% remove the name-value pair and convert the struct back to a cell array
s = rmfield(s, name);
newvarargs = [fieldnames(s), struct2cell(s)]';
newvarargs = newvarargs(:)';
else
% return default value and original cell array if named property does not exist
value = default;
newvarargs = varargs;
end
end
end
end