-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevaluator.m
1738 lines (1475 loc) · 68.1 KB
/
evaluator.m
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function varargout = evaluator(varargin)
% EVALUATOR MATLAB code for evaluator.fig
% EVALUATOR, by itself, creates a new EVALUATOR or raises the existing
% singleton*.
%
% H = EVALUATOR returns the handle to a new EVALUATOR or the handle to
% the existing singleton*.
%
% EVALUATOR('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in EVALUATOR.M with the given input arguments.
%
% EVALUATOR('Property','Value',...) creates a new EVALUATOR or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before evaluator_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to evaluator_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Generally this code contains functions that are directly linked to
% the functionality of UI elements, such as Callbacks.
% Where possible logic and program functionality is deferred to
% functions in external source files.
% Copyright 2016 Elliot Sefton-Nash
% Initialization
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @evaluator_OpeningFcn, ...
'gui_OutputFcn', @evaluator_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
end
% Executes just before evaluator is made visible.
function evaluator_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to evaluator (see VARARGIN)
% Choose default command line output for evaluator
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes evaluator wait for user response (see UIRESUME)
% uiwait(handles.figure1);
end
% --- Outputs from this function are returned to the command line.
function varargout = evaluator_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
end
% NOTE: The arguments 'hObject, eventdata, handles' MUST be passed to a
% callback function in order to address using the @ notation from another
% function in this GUI.
% Calls layerPBAdd, which is in a separate .m file. Adds layers to the GUIs
% layer list.
function layerPBAdd_Callback(hObject, eventdata, handles)
setStatus('Loading layer...');
rasterLayers = layerPBAdd();
% If not an empty array add these layers to the guidata.
if ~isempty(rasterLayers)
% NOTE: gcbf if the handle of the figure that contains the object
% whose callback is executing. We use this to our advantage because
% we can store effectively global variables in the guidata of the
% main figure.
%
% Data are accessed using: data = guidata(gcbf);
% And set using: guidata(gcbf, dat)
%
% The structure 'handles' contains handles to UI elements, but also
% further structures containing user data:
% handles.layers - Cell array of rasterLayer objects that are
% loaded in the list of layers.
data = guidata(gcbf);
% If the layers field doesn't exist, make and empty cell array.
if ~isfield(data,'layers')
data.layers = {};
data.nlayers = 0;
end
if data.nlayers == 0
% If these are new layers, enable the layer controls.
data = setLayerControls(data, 'on');
end
% Add each new raster layer.
for i = 1:numel(rasterLayers)
data.nlayers = data.nlayers + 1;
data.layers{data.nlayers} = rasterLayers{i};
% If the layer added is now the only layer, then we select it.
if data.nlayers == 1
data.layerListBox.Value = data.nlayers;
end
% Add this layer to the layerListBox
data.layerListBox.String = ...
vertcat(data.layerListBox.String,{data.layers{data.nlayers}.fname});
% Update setbounds listbox on the Evaluate tab
data.evaluateSetBoundsPopup.String = data.layerListBox.String;
data.evaluateSetBoundsPopup.Value = data.layerListBox.Value;
% If this is the last layer in the list to be added, update layer controls
% passing only required structures and selected layer from 'data'.
[data.layerMinEdit, data.layerMaxEdit, data.layerMinSlider,...
data.layerMaxSlider, data.layerCBInvert, data.layers{data.nlayers}] = ...
setlayerMinMaxEditSliders(...
data.layerMinEdit,data.layerMaxEdit,...
data.layerMinSlider, data.layerMaxSlider, data.layerCBInvert,...
data.layers{data.nlayers});
end
% Put guidata back
guidata(gcbf, data);
end
setStatus('Ready.');
end
% Remove a layer from the list of layers
function layerPBRemove_Callback(hObject, eventdata, handles)
setStatus('Removing layer...');
data = guidata(gcbf);
if data.nlayers > 0
% The array position of the layer should be the same as the list
% position.
data.nlayers = data.nlayers - 1;
% Index of layer to remove.
i = data.layerListBox.Value;
% Index of selected layer should be same as array index in
% layers.
%data.layerListBox.String = ...
% data.layerListBox.String([1:data.layerListBox.Value-1,data.layerListBox.Value+1:end]);
% Make empty elements in cell arrays of listbox and for rasterlayer
% objects, to delete layer.
data.layerListBox.String{i} = [];
data.layers{i} = [];
% Remove empty elements from these arrays.
data.layers = removeEmptyCells(data.layers);
data.layerListBox.String = removeEmptyCells(data.layerListBox.String);
% Update setbounds listbox
data.evaluateSetBoundsPopup.String = data.layerListBox.String;
data.evaluateSetBoundsPopup.Value = data.layerListBox.Value;
% If there was only one layer, then now there are none. disable layer controls.
if data.nlayers == 0
data.layerListBox.String = {};
data.layerListBox.Value = 0;
% Remove the min,max values from the threshold controls
data.layerMinEdit.String = '';
data.layerMaxEdit.String = '';
% Reset the set bounds listbox - produces warning if Value <= 0
% || isempty(String)
data.evaluateSetBoundsPopup.String = ' '; % Requires non-empty string.
data.evaluateSetBoundsPopup.Value = 1;
data = setLayerControls(data, 'off');
else
% If still layers left, set selected layer back to first layer.
data.layerListBox.Value = 1;
data.evaluateSetBoundsPopup.Value = data.layerListBox.Value;
end
end
% Put guidata back
guidata(gcbf, data);
setStatus('Ready.');
end
% Remove empty cells from a cell array.
function A = removeEmptyCells(A)
A(cellfun(@(A) isempty(A),A))=[];
end
% When min slider is adjusted, update layer edit box and layer minthresh
function layerMinSlider_Callback(hObject, eventdata, handles)
data = guidata(gcbf);
% Slider can only be adjusted within min and max ranges of current
% layer, so new values are intrinsically range- and format-validated.
% Get new value of slider.
v = data.layerMinSlider.Value;
% Index of selected layer.
i = data.layerListBox.Value;
% Only set values if value has changed. Avoids running when mouse click
% without slider drag.
if v ~= data.layers{i}.threshmin
% If value is above threshmax for this layer then set it to threshmax.
if v > data.layers{i}.threshmax
v = data.layers{i}.threshmax;
% Set slider back to maximum allowed position.
data.layerMinSlider.Value = v;
end
% Set edit box and layer threshmin.
data.layers{i}.threshmin = v;
data.layerMinEdit.String = num2str(v);
end
% Put guidata back
guidata(gcbf, data);
end
% When max slider is adjusted, update layer edit box and layer maxthresh
function layerMaxSlider_Callback(hObject, eventdata, handles)
data = guidata(gcbf);
% Slider can only be adjusted within min and max ranges of current
% layer, so new values are intrinsically range- and format-validated.
% Get new value of slider.
v = data.layerMaxSlider.Value;
% Index of selected layer.
i = data.layerListBox.Value;
% Only set values if value has changed. Avoids running when mouse click
% without slider drag.
if v ~= data.layers{i}.threshmax
% If value is above threshmax for this layer then set it to threshmax.
if v < data.layers{i}.threshmin
v = data.layers{i}.threshmin;
% Set slider back to minimum allowed position.
data.layerMaxSlider.Value = v;
end
% Set edit box and layer threshmin.
data.layers{i}.threshmax = v;
data.layerMaxEdit.String = num2str(v);
end
% Put guidata back
guidata(gcbf, data);
end
% Validate and set new minimum when value is entered.
function layerMinEdit_Callback(hObject, eventdata, handles)
% Get guidata
data = guidata(gcbf);
% Validate entered strings, returns v as double. Empty if invalid.
v = validNumeric(data.layerMinEdit.String);
% If invalid number, set string to empty.
if isempty(v)
data.layerMinEdit.String = '';
else
% If valid, test if value is between allowed limits.
if v < data.layers{data.layerListBox.Value}.zmin || v > data.layers{data.layerListBox.Value}.zmax
% If outside bounds, set minimum to minimum value in raster.
v = data.layers{data.layerListBox.Value}.zmin;
data.layerMinEdit.String = num2str(v);
end
% Set slider position.
data.layerMinSlider.Value = v;
% Update value for layer.
data.layers{data.layerListBox.Value}.threshmin = v;
end
% Put guidata back
guidata(gcbf, data);
end
% Validate and set new maximum when value is entered.
function layerMaxEdit_Callback(hObject, eventdata, handles)
% Get guidata
data = guidata(gcbf);
% Validate entered strings, returns v as double. Empty if invalid.
v = validNumeric(data.layerMaxEdit.String);
% If invalid number, set string to empty.
if isempty(v)
data.layerMaxEdit.String = '';
else
% If valid, test if value is between allowed limits.
if v < data.layers{data.layerListBox.Value}.zmin || v > data.layers{data.layerListBox.Value}.zmax
% If outside bounds, set maximum to maximum value in raster.
v = data.layers{data.layerListBox.Value}.zmax;
data.layerMaxEdit.String = num2str(v);
end
% Set slider position.
data.layerMaxSlider.Value = v;
% Update value for layer.
data.layers{data.layerListBox.Value}.threshmax = v;
end
% Put guidata back
guidata(gcbf, data);
end
% Plot the currently selected image in the axes, accounting for the
% thresholds set.
function layerPBPreview_Callback(hObject, eventdata, handles)
setStatus('Plotting layer...');
data = guidata(gcbf);
% Clear the plot axes.
cla(data.plot.hAx);
% Index of selected layer with threshold applied.
i = data.layerListBox.Value;
data.layers{i} = data.layers{i}.calcMask();
imagesc(data.layers{i}.lonvec,data.layers{i}.latvec,...
data.layers{i}.im,'AlphaData',data.layers{i}.mask,...
'Parent',data.plot.hAx);
data.plot.hAx.YDir = 'normal';
data.plot.hAx.DataAspectRatio = [1 1 1];
xlabel(data.plot.hAx, 'Longitude');
ylabel(data.plot.hAx, 'Latitude');
% Set title to name of plotted layer
title(data.plot.hAx, data.layers{i}.fname, 'Interpreter','none');
% Nothing to change in the raster layer, so no need to put back the
% guidata.
setStatus('Ready.');
end
% List box callback. When an item in the list box is selected the values in
% the sliders are updated.
function layerLB_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns contents
% contents{get(hObject,'Value')} returns selected item from listbox1
% Get the guidata.
data = guidata(gcbf);
% Only if there are any layers loaded
if data.nlayers > 0
% Update layer controls for selected layer, passing only required
% structures and selected layer from 'data'.
[data.layerMinEdit, data.layerMaxEdit, data.layerMinSlider,...
data.layerMaxSlider, data.layerCBInvert, data.layers{data.layerListBox.Value}] = ...
setlayerMinMaxEditSliders(...
data.layerMinEdit,data.layerMaxEdit,...
data.layerMinSlider, data.layerMaxSlider, data.layerCBInvert,...
data.layers{data.layerListBox.Value});
end
guidata(gcbf, data);
end
% Function to set relevant properties of edit boxes and threshold sliders
% based on the values set for a particular layer. This is called when
% either a new layer is added or a layer in the list box is selected.
function [layerMinEdit,layerMaxEdit, layerMinSlider,...
layerMaxSlider, layerCBInvert, thisRasterLayer] = setlayerMinMaxEditSliders(...
layerMinEdit,layerMaxEdit, layerMinSlider,...
layerMaxSlider, layerCBInvert, thisRasterLayer)
layerMinEdit.String = num2str(thisRasterLayer.threshmin);
layerMaxEdit.String = num2str(thisRasterLayer.threshmax);
% Set the limits of both sliders to the limits of the raster.
layerMaxSlider.Value = thisRasterLayer.threshmax;
layerMaxSlider.Max = thisRasterLayer.zmax;
layerMaxSlider.Min = thisRasterLayer.zmin;
layerMinSlider.Value = thisRasterLayer.threshmin;
layerMinSlider.Max = thisRasterLayer.zmax;
layerMinSlider.Min = thisRasterLayer.zmin;
% Invert box
layerCBInvert.Value = thisRasterLayer.invert;
end
% Set whether layer has inverted mask or not.
function layerCBInvert_Callback(hObject, eventdata, handles)
data = guidata(gcbf);
% Set selected layer invert state.
data.layers{data.layerListBox.Value}.invert = data.layerCBInvert.Value;
guidata(gcbf, data);
end
% Edit the minor axis dimension
function ellipseYAEdit_Callback(hObject, eventdata, handles)
data = guidata(gcbf);
% If not valid number value gets set empty. Set empty string.
data.ellipseYAEdit.Value = validNumericAndPositive(data.ellipseYAEdit.String);
if isempty(data.ellipseYAEdit.Value)
data.ellipseYAEdit.String = '';
end
guidata(gcbf, data);
end
% Edit the major axis dimension
function ellipseXAEdit_Callback(hObject, eventdata, handles)
data = guidata(gcbf);
% If not valid number value gets set empty. Set empty string.
data.ellipseXAEdit.Value = validNumericAndPositive(data.ellipseXAEdit.String);
if isempty(data.ellipseXAEdit.Value)
data.ellipseXAEdit.String = '';
end
guidata(gcbf, data);
end
% Edit the azimuth in the ellipse tab
function ellipseAzEdit_Callback(hObject, eventdata, handles)
data = guidata(gcbf);
[data.ellipseAzEdit.String, data.ellipseAzEdit.Value] = ...
validAz(data.ellipseAzEdit.String, data.ellipseAzEditUnitsListBox.Value);
guidata(gcbf, data);
end
% Edit the longitude position of the ellipse
function ellipseXEdit_Callback(hObject, eventdata, handles)
data = guidata(gcbf);
[data.ellipseXEdit.String, data.ellipseXEdit.Value] = validLon(data.ellipseXEdit.String);
% Ellipse is special case, position has to be somewhere.
defaultLon = 180;
if isempty(data.ellipseXEdit.String)
data.ellipseXEdit.String = num2str(defaultLon);
data.ellipseXEdit.String = defaultLon;
end
guidata(gcbf, data);
end
% Edit the latitude position of the ellipse
function ellipseYEdit_Callback(hObject, eventdata, handles)
data = guidata(gcbf);
[data.ellipseYEdit.String, data.ellipseYEdit.Value] = validLat(data.ellipseYEdit.String);
% Defaults to 0 rather than empty when outside +/-90.
guidata(gcbf, data);
end
%% No need to convert units when list box is changed.
% Could be an annoyance to user if not desired.
% % List box units edit, convert units if needed.
% function ellipseYAEditUnitsListBox_Callback(hObject, eventdata, handles)
% data = guidata(gcbf);
% % Get the value of the edit box and convert the units if necessary.
% end
%
% % List box units edit, convert units if needed.
% function ellipseXAEditUnitsListBox_Callback(hObject, eventdata, handles)
% data = guidata(gcbf);
% end
%
% % List box units edit, convert units if needed.
% function ellipseAzEditUnitsListBox_Callback(hObject, eventdata, handles)
% data = guidata(gcbf);
% end
% Callback for when ellipse Preview button is pressed.
function ellipsePreviewPB_Callback(hObject, eventdata, handles)
data = guidata(gcbf);
plotted = false;
% If layer footprint checkbox is checked & if there are any layers
% loaded, then plot the footprints of all the raster layers.
if data.ellipseRasterFootprintCB.Value == 1 && data.nlayers > 0
% Clear the plot axes.
cla(data.plot.hAx);
setStatus('Plotting layer footprints...');
plotLayerFootprints(data.layers, data.plot.hAx);
setStatus('Ready.');
plotted = true;
end
% If there are no polygons, then there must be an ellipse
if isempty(data.poly)
% Plot ellipse last == on top.
% If there are values in the edit boxes. Already validated.
if ~isempty(data.ellipseXAEdit.Value) && ...
~isempty(data.ellipseYAEdit.Value)
% Can still plot without having entered a value for azimuth, assume
% user wants no rotation.
az = 0;
if ~isempty(data.ellipseAzEdit.Value)
% Turn ellipse azimuth into radians for plotting.
[~, ~, mult] = getAngUnitList();
az = data.ellipseAzEdit.Value * mult(data.ellipseAzEditUnitsListBox.Value);
end
% Turn ellipse dimensions into metres for plotting.
[~, ~, mult] = getLengthUnitList;
xa = data.ellipseXAEdit.Value * mult(data.ellipseXAEditUnitsListBox.Value);
ya = data.ellipseYAEdit.Value * mult(data.ellipseYAEditUnitsListBox.Value);
% 1 degree angular resolution of the ellipse.
angRes = pi/180;
% Set ellipse centre, in metres.
if isempty(data.ellipseXEdit.Value)
data.ellipseXEdit.Value = 0;
else
% Convert longitude to x-coord.
clon = data.ellipseXEdit.Value;
end
if isempty(data.ellipseYEdit.Value)
data.ellipseYEdit.Value = 0;
else
% Convert to latitude to y-coord.
clat = data.ellipseYEdit.Value;
end
% Values in edit boxes are in latlon, convert to equal-area map coords for
% drawing ellipse.
[xc, yc] = latlon2eqa( clat, clon, data.re , data.proj.lat1, data.proj.lonO );
% Make an ellipse object and put it into data.
data.ellipse{1} = ellipseObj(xa, ya, az, angRes, xc, yc);
% Make the ellipses lat,lon coordinates
fe = 0; fn = 0;
data.ellipse{1} = data.ellipse{1}.getLatLonFromEqaXY(fe, fn, data.re, data.proj.lat1, data.proj.lonO);
% If we're plotting the ellipse only, i.e. if layers havn't been drawn here,
% then clear the axes, because a previous plot might persist. Can
% tell by setting of titleStr.
if ~plotted
cla(data.plot.hAx);
end
% Convention is that hold is normally turned off and re-turned on every time
% overlay plotting is required. hold may have been off either because no rasters
% were drawn, or because it was set back to off in the function plotRasterLayers.
hold(data.plot.hAx, 'on');
plotPoly(data.ellipse{1}.lon, data.ellipse{1}.lat, data.plot.hAx);
hold(data.plot.hAx, 'off');
% Wrap the axes to its children
axis(data.plot.hAx, 'tight');
% Put data back.
guidata(gcbf, data);
end
else
% Clear the axes, because a previous plot might persist.
if ~plotted
cla(data.plot.hAx);
end
% There are polygons, gotta plot them all.
hold(data.plot.hAx, 'on');
for i = 1:numel(data.poly)
plotPoly(data.poly{i}.x, data.poly{i}.y, data.plot.hAx);
end
hold(data.plot.hAx, 'off');
% Put data back.
guidata(gcbf, data);
end
setStatus('Ready.');
end
% Callback for evaluate minimum x coord. edit box
function evaluateXMinEdit_Callback(hObject, eventdata, handles)
data = guidata(gcbf);
[data.evaluateXMinEdit.String, data.evaluateXMinEdit.Value] = validLon(data.evaluateXMinEdit.String);
guidata(gcbf, data);
end
% Callback for evaluate x coord. interval edit box
function evaluateXStepEdit_Callback(hObject, eventdata, handles)
data = guidata(gcbf);
[data.evaluateXStepEdit.String, data.evaluateXStepEdit.Value] = validLon(data.evaluateXStepEdit.String);
guidata(gcbf, data);
end
% Callback for evaluate maximum x coord. edit box
function evaluateXMaxEdit_Callback(hObject, eventdata, handles)
data = guidata(gcbf);
[data.evaluateXMaxEdit.String, data.evaluateXMaxEdit.Value] = validLon(data.evaluateXMaxEdit.String);
guidata(gcbf, data);
end
% Callback for evaluate minimum y coord. edit box
function evaluateYMinEdit_Callback(hObject, eventdata, handles)
data = guidata(gcbf);
[data.evaluateYMinEdit.String, data.evaluateYMinEdit.Value] = validLat(data.evaluateYMinEdit.String);
guidata(gcbf, data);
end
% Callback for evaluate y coord. interval edit box
function evaluateYStepEdit_Callback(hObject, eventdata, handles)
data = guidata(gcbf);
[data.evaluateYStepEdit.String, data.evaluateYStepEdit.Value] = validLat(data.evaluateYStepEdit.String);
guidata(gcbf, data);
end
% Callback for evaluate maximum y coord. edit box
function evaluateYMaxEdit_Callback(hObject, eventdata, handles)
data = guidata(gcbf);
[data.evaluateYMaxEdit.String, data.evaluateYMaxEdit.Value] = validLat(data.evaluateYMaxEdit.String);
guidata(gcbf, data);
end
% Edit the azimuth in the ellipse tab, min
function evaluateAzMinEdit_Callback(hObject, eventdata, handles)
data = guidata(gcbf);
[data.evaluateAzMinEdit.String, data.evaluateAzMinEdit.Value] = ...
validAz(data.evaluateAzMinEdit.String, data.evaluateAzEditUnitsListBox.Value);
guidata(gcbf, data);
end
% Edit the azimuth in the ellipse tab, step
function evaluateAzStepEdit_Callback(hObject, eventdata, handles)
data = guidata(gcbf);
[data.evaluateAzStepEdit.String, data.evaluateAzStepEdit.Value] = ...
validAz(data.evaluateAzStepEdit.String, data.evaluateAzEditUnitsListBox.Value);
guidata(gcbf, data);
end
% Edit the azimuth in the ellipse tab, max
function evaluateAzMaxEdit_Callback(hObject, eventdata, handles)
data = guidata(gcbf);
[data.evaluateAzMaxEdit.String, data.evaluateAzMaxEdit.Value] = ...
validAz(data.evaluateAzMaxEdit.String, data.evaluateAzEditUnitsListBox.Value);
guidata(gcbf, data);
end
% Callback for when evaluate set bounds is called. This is only pressed
% when layers are present, so no need to check that.
function layerEvaluateSetBoundsPB_Callback(hObject, eventdata, handles)
data = guidata(gcbf);
% Array of layers should be same as order in ellipseLayerListBox
i = data.evaluateSetBoundsPopup.Value;
% Set values
data.evaluateXMinEdit.Value = data.layers{i}.lonlims(1);
data.evaluateXMaxEdit.Value = data.layers{i}.lonlims(2);
data.evaluateYMinEdit.Value = data.layers{i}.latlims(1);
data.evaluateYMaxEdit.Value = data.layers{i}.latlims(2);
% Set strings;
data.evaluateXMinEdit.String = num2str(data.layers{i}.lonlims(1));
data.evaluateXMaxEdit.String = num2str(data.layers{i}.lonlims(2));
data.evaluateYMinEdit.String = num2str(data.layers{i}.latlims(1));
data.evaluateYMaxEdit.String = num2str(data.layers{i}.latlims(2));
guidata(gcbf, data);
end
% Returns 1 if we are in azimuth evaluation mode, 0 if not.
function azMode = getAzMode(val)
modes = getEvaluateModes();
azMode = false;
if strcmpi(modes{val},'azimuth')
azMode = true;
end
end
% Callback for when evaluate Preview button is pressed. Purpose of this
% callback is to visualise the spatial extent of the ellipses that will be
% evaluated, with options for a bounding box and layer footprints.
function layerEvaluatePreviewPB_Callback(hObject, eventdata, handles)
data = guidata(gcbf);
% Check if a valid ellipse exists, if not there is no point in
% continuing.
if isfield(data, 'ellipse')
if isempty(data.ellipse) || ~iscell(data.ellipse)
setStatus('No valid ellipse defined.')
return
end
else
setStatus('No ellipse defined.')
return
end
azMode = getAzMode(data.mode);
% We always plot the cumulative ellipse perimeter and the ellipse
% centres. If there are large numbers of ellipses the perimeter could just be a
% bounding rectangle to speed up the calculation.
setStatus('Calculating...');
if azMode
% Check for valid azimuth vector definitions.
if ~isempty(data.evaluateAzMinEdit.Value) &&...
~isempty(data.evaluateAzMaxEdit.Value) &&...
~isempty(data.evaluateAzStepEdit.Value)
% Try to make a vector of azimuths, in radians.
try
[~, ~, mult] = getAngUnitList();
% List box value corresponds to position in arrays returned by
% getAngUnitList. Convert to radians.
azVec = validAzVec(data.evaluateAzMinEdit.Value,...
data.evaluateAzStepEdit.Value,...
data.evaluateAzMaxEdit.Value,...
mult(data.evaluateAzEditUnitsListBox.Value));
% Get the ellipse extents at the azimuth range.
[xEllPoly, yEllPoly] = getEllipseExtentAz(...
data.ellipse{1}, azVec, data.re, data.proj.lat1, data.proj.lonO);
% Plot the enclosing area.
plotExtentPatch(data.plot.hAx,xEllPoly,yEllPoly);
catch
% Vector is invalid
setStatus('Azimuth definition is invalid.');
return
end
else
setStatus('No valid azimuth vector entered.');
return
end
else % Grid mode
% Check that all the required edit boxes have values.
if ~isempty(data.evaluateXMinEdit.Value) && ~isempty(data.evaluateXMaxEdit.Value) &&...
~isempty(data.evaluateYMinEdit.Value) && ~isempty(data.evaluateYMaxEdit.Value) &&...
~isempty(data.evaluateYStepEdit.Value) && ~isempty(data.evaluateXStepEdit.Value)
% Try to make a grid object, if vectors are returned empty then
% grid is invalid.
data.grid{1} = grdObj(getVec([data.evaluateXMinEdit.Value data.evaluateXMaxEdit.Value],...
data.evaluateXStepEdit.Value),...
getVec([data.evaluateYMinEdit.Value data.evaluateYMaxEdit.Value],...
data.evaluateYStepEdit.Value) );
if isempty(data.grid{1})
setStatus('Grid definition is invalid.');
return
end
% A valid grid exists if we are here, so find the outer
% boundary of ellipses at the corners of the grid
% (in lat,lon). Ellipses are drawn in equal area
% projection, grid is defined in lat lon, but must
% convert it to working projection to figure out extent
data.grid{1} = data.grid{1}.getEqaXYFromLatLon(...
data.re,data.proj.lat1,data.proj.lonO);
[xEllPoly, yEllPoly] = getEllipseExtentOnGrid(...
data.ellipse{1}, data.grid{1},...
data.re, data.proj.lat1, data.proj.lonO);
else
setStatus('No valid grid defined.')
return
end
end
% If we made it here, grid or azVec is valid.
cla(data.plot.hAx);
setStatus('Plotting...');
% PLOT RASTER FOOTPRINTS
% If the check box is enabled and checked, plot raster layer footprints.
if strcmpi(data.evaluatePlotRasterFootprintCB.Enable, 'on') && ...
data.evaluatePlotRasterFootprintCB.Value == 1
hold(data.plot.hAx, 'on');
plotLayerFootprints(data.layers, data.plot.hAx);
hold(data.plot.hAx, 'off');
end
% Plot the ellipse bounding polygon.
plotExtentPatch(data.plot.hAx,xEllPoly,yEllPoly);
% Plot the ellipse centres.
if azMode
xp = data.ellipse{1}.lonc;
yp = data.ellipse{1}.latc;
else
% Plot grid as centre points of each ellipse placement, i.e.
% each pixel.
[xp,yp] = meshgrid(data.grid{1}.lonc, data.grid{1}.latc);
end
hold(data.plot.hAx, 'on');
plot(xp(:), yp(:), 'k+', 'Parent',data.plot.hAx);
hold(data.plot.hAx, 'off');
title(data.plot.hAx, 'Ellipse extents');
guidata(gcbf, data);
setStatus('Ready.');
end
% Function to enable/disable all the UI elements that require layers to be
% loaded for them to function.
function data = setLayerControls(data, state)
% Layer controls on layer tab.
data.layerListBox.Enable = state;
data.layerPBRemove.Enable = state;
data.layerMinSlider.Enable = state;
data.layerMinEdit.Enable = state;
data.layerMaxSlider.Enable = state;
data.layerMaxEdit.Enable = state;
data.layerPBPreview.Enable = state;
data.layerCBInvert.Enable = state;
% Draw raster layer footprint checkbox on Ellipse tab.
data.ellipseRasterFootprintCB.Enable = state;
% 'Set bounds' PB, Popup and raster CB and on the Evaluate tab.
data.evaluateSetBoundsPB.Enable = state;
data.evaluateSetBoundsPopup.Enable = state;
data.evaluatePlotRasterFootprintCB.Enable = state;
end
% Status
function setStatus(msg)
data = guidata(gcbf);
data.status.String = ['STATUS: ', msg];
end
% Evaluate pushbutton callback. DO THE EVALUATE.
function layerEvaluateEvaluatePB_Callback(hObject, eventdata, handles)
data = guidata(gcbf);
% Check for the 3 required items:
% At least one layer
goFlag = true;
if data.nlayers < 1
setStatus('Can''t evaluate - no layers loaded.')
return
end
% A valid ellipse?
errStr = 'Can''t evaluate - no valid ellipse entered.';
if ~isfield(data,'ellipse')
goFlag = false;
else
if isempty(data.ellipse)
goFlag=false;
end
end
if ~goFlag
setStatus(errStr);
return
end
% A valid grid, or azvec
errStr = 'Can''t evaluate - no valid parameters entered.';
azMode = getAzMode(data.mode);
if azMode
[~, ~, mult] = getAngUnitList();
% List box value corresponds to position in arrays returned by
% getAngUnitList. Convert to radians.
data.azVec = validAzVec(data.evaluateAzMinEdit.Value,...
data.evaluateAzStepEdit.Value,...
data.evaluateAzMaxEdit.Value,...
mult(data.evaluateAzEditUnitsListBox.Value));
if ~data.azVec % Returned empty if invalid.
setStatus(errStr);
return
end
else
% Lat-lon grid mode
if ~isfield(data,'grid')
goFlag = false;
else
if isempty(data.grid)
goFlag = false;
end
end
if ~goFlag
setStatus(errStr);
return
end
end
% If we made it this far, everything appears valid.
% Pass all the details to the evaluating routine
setStatus('Evaluating...');
% data.ellipse, data.grid and data.layers are cell arrays of thier
% respective objects. We pass the grid and ellipse objects to
% evaluateXYCore or evaluateAzCore as single objects not embedded in a
% cell array. However we DO pass a cell array of rasterLayer objects.
wb = true; % We want a waitbar.
if azMode
data.result = evaluateAzCore(data.ellipse{1}, data.azVec, data.layers, wb);
else
% Make sure the grid has map coordinates. It may not have been
% previewed.
data.grid{1} = data.grid{1}.getEqaXYFromLatLon(data.re,data.proj.lat1,data.proj.lonO);
% Run assessment.
data.result = evaluateXYCore(data.ellipse{1}, data.grid{1}, data.layers, wb);
end
setStatus('Ready.');
if ~isempty(data.result)
% There is a result. Make the results panel visible.
data = setResultsControlsState(data, 'on');
end
guidata(gcbf, data);
end
% Function to set the state of the results panel UI elements.
function data = setResultsControlsState(data, state)
% Set control states.
data.evaluateResultsPreviewPB.Enable = state;
data.evaluateResultsOutputPB.Enable = state;
data.evaluateResultsOutputEdit.Enable = state;
data.evaluateResultsFormatListBox.Enable = state;
data.evaluateResultsSavePB.Enable = state;
end
% Function to select the output directory from the built-in UI.
function evaluateResultsOutputPB_Callback(hObject, eventdata, handles)
% Open the directory file selection dialogue.
dirName = uigetdir(matlabroot, 'Select results output directory');
% A single path is returned, and 0 if nothing is selected.
if ~isnumeric(dirName)
data = guidata(gcbf);
% Set edit box to returned path.
data.evaluateResultsOutputEdit.String = dirName;
guidata(gcbf, data);
end
end
% Function to write the results
function evaluateResultsSavePB_Callback(hObject, eventdata, handles)
data = guidata(gcbf);
% If the results directory is not valid then do not write and set the
% save path to null.
if ~exist(data.evaluateResultsOutputEdit.String,'dir')
data.evaluateResultsOutputEdit.String = '';
% Warn here that the data directory is not valid.
warn('Data output directory is invalid.');
else
setStatus('Saving results.');
% Save the results in desired format, ext is the identifier (not
% desc, which is the string of the list box).
outfpath = [data.evaluateResultsOutputEdit.String,'/ee_result_',getTimeStrNow()];
data.result.write(data.mode, data.evaluateResultsFormatListBox.Value, outfpath);
setStatus('Ready.');
end
guidata(gcbf, data);
end
% Function to preview the results.
function evaluateResultsPreviewPB_Callback(hObject, eventdata, data)
data = guidata(gcbf);
% Check whether the result object is a raster, ellipse over x-y
% position, or ellipse drawn at different azimuths, which would give
% just a vector of results.
if isempty(data.result.grid)
% Plot ellTrueFrac, the cumulative result for all layers,
% as a function of ellipse azimuth.
cla(data.plot.hAx);
[data.hAx, data.hTitle, data.hXLab, data.hYLab, data.hLeg] = ...
plotEllFracVsAz(data.plot.hAx, data.layers, data.result);
else
% TODO