This repository has been archived by the owner on Oct 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptics.m
724 lines (606 loc) · 21.9 KB
/
optics.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
classdef optics < handle
% OPTICS defines the class for viewing snow optics image files
% DEFINE THE PUBLIC PROPERTIES
properties
FTP; % FTP object
fig; % Handle of the Program Control Window
position; % Position of the Program Control
exp; % Folder for the experiment(s) (top level)
folder; % Sub folder(s) of the experiment (second level)
type; % Folder indicating the type of image (third level)
angle; % Angle folder (ZxxVxxY, fourth level)
thepath; % Current folder path for ftp download
opticsPath = ''; % Path of savde workspace
opticsFile = ''; % Filename of saved workspace (*.sws)
% List of user preferences
pref = {'keeplocal','appendWorkspace','target','useftp'};
% Define the user preferences
keeplocal = true; % Toggle for saving/removing local files
appendWorkspace = true; % Toggle for clearing exiting files
target = 'database'; % The location of the image database
useftp = true ; % Toggle for using ftp site
end
% DEFINE THE PRIVATE PROPERTIES
properties (SetAccess = private)
version = 0.39
verdate = '09/16/2010';
handles = imObject.empty; % Initilize the imObject handles
end
% DEFINE THE PRIVATE, UNSAVED PROPERTIES
properties (SetAccess = private, Transient = true)
host = 'caesar.ce.montana.edu';
username = 'anonymous';
password = 'snowoptics';
thedir = '/pub/snow/optics';
extensions = {'.jpg','.bip','.bil'}; % File extensions to allow
end
% DEFINE THE METHODS
methods
% OPTICS: operates on creation of optics object
function obj = optics(varargin)
% Add the bin path and open the optics GUI
addpath('bin');
% Open the workspace
if nargin == 1;
obj.loadWS(varargin{:});
% Opens the GUI
else
obj.fig = open('controlGUI.fig');
% Update the position
if ispref('imobject','LastOpticsGUIPosition');
p = getpref('imobject','LastOpticsGUIPosition');
set(obj.fig,'units','normalized','position',p);
end
% Initilize the control
initControlGUI(obj);
end
% Check for updates
obj.update;
end
% UPDATE: check for updates
function obj = update(obj)
% Locate the latest git tag
loc = 'http://github.com/aeslaughter/optics/downloads';
pattern = '/aeslaughter/optics/zipball/';
str = urlread(loc);
i = strfind(str,pattern);
j = strfind(str(i(1):end),'">zip');
% Determine the latest available version
v1 = i(1)+length(pattern)+1;
v2 = i(1)+j(1)-2;
ver = str2double(str(v1:v2));
% Compare the versions
if ver <= obj.version;
msgbox('You have the lastest version.',...
'Update...','Help');
return;
end
% Prompt the user
mes = ['A newer version of Snow Optics Toolbox is available, ',...
'would you like to download this version?'];
q = questdlg(mes,'New version...',...
'Download','Continue','Downolad');
% Return if user selects continue
if ~strcmpi(q,'Download'); return; end;
% Unzip the latest and greatest
web('http://github.com/aeslaughter/optics/downloads','-browser');
end
% CONNECTFTP: connects the user to the FTP server
function obj = connectFTP(obj)
try
obj.FTP = ftp(obj.host,obj.username,obj.password);
cd(obj.FTP,obj.thedir);
catch
msg = ['Failed to connect to the FTP server!',...
' The local directory is being used.'];
warndlg('Failed to connect to the FTP server!');
obj.useftp = false;
obj.FTP = NaN;
end
end
% SETPREF: changes the current optics preferencs
function obj = opticsPref(obj,pref)
% Gather handle for the options GUI
H = findobj('Tag','OpticsOptions');
% Throw an error if called without the options GUI open
if isempty(H) || ~ishandle(H);
error('The options window must be opened!');
else
h = guihandles(H);
end
% Gather the new preference
if isnumeric(obj.(pref));
obj.(pref) = get(h.(pref),'Value');
elseif ischar(obj.(pref));
obj.(pref) = get(h.(pref),'String');
end
end
% SETDEFAULTPREF: sets the current preferences as the default
function obj = setdefaultPref(obj)
% Loop through each preference and store
p = obj.pref;
for i = 1:length(p);
setpref('OpticsOptions',p{i},obj.(p{i}));
end
end
% GETDEFAULTPREF: recalls and implements the default preferences
function obj = getdefaultPref(obj)
% Loop through each preference and load the default
p = obj.pref;
for i = 1:length(p);
if ispref('OpticsOptions',p{i});
obj.(p{i}) = getpref('OpticsOptions',p{i});
end
end
% Applies the default preferences, if the options windows is open
H = findobj('Tag','OpticsOptions');
if ~isempty(H) && ishandle(H);
h = guihandles(H); % GUI handles of the options window
for i = 1:length(p); % Loops through each preference
if isnumeric(obj.(p{i}));
set(h.(p{i}),'Value',obj.(p{i}));
elseif ischar(obj.(p{i}));
set(h.(p{i}),'String',obj.(p{i}));
end
end
end
end
% GETFILES: gathers the image files based on the folders selected
function obj = getFiles(obj)
% Gather the gui handles and existing images files
h = guihandles(obj.fig);
if obj.useftp;
obj.connectFTP;
data = dir(obj.FTP,[obj.thepath,'/*.*']);
close(obj.FTP);
else
data = dir([obj.target,'/',obj.thepath,'/*.*']);
end
% Cycle through each file and build a list of images only
list = {}; k = 1;
for i = 1:length(data);
[~,~,e] = fileparts(data(i).name);
if any(strcmpi(e,obj.extensions))
list{k} = data(i).name;
k = k + 1;
end
end
% Update the GUI object containing the list of images
if isempty(list);
set(h.images,'enable','off','String',{},'Value',0);
else
set(h.images,'enable','on','String',list,'Value',1);
end
end
% OPENIMAGES: operates upon selection of the open button
function obj = openImages(obj)
% Gather guihandles and the selected files
h = guihandles(obj.fig);
str = get(h.images,'String');
files = str(get(h.images,'Value'));
% Open the images
if obj.useftp;
openRemote(obj,files);
else
openLocal(obj,files);
end
% Update the imObject handle list
idx = isvalid(obj.handles);
obj.handles = obj.handles(idx);
end
% SAVEWORKSPACE: operates to save the current workspace
function saveWS(obj,thepath,thefile)
% Determine the file to save
filename = fullfile(thepath,thefile);
spec = {'*.sws','MATLAB snow optics workspace (*.sws)'};
filename = gatherfile('put','LastUsedWorkSpaceDir',spec,filename);
if isempty(filename); return; end
[pth,fn,ext] = fileparts(filename);
% Remove invalid (delete) imObjects
idx = isvalid(obj.handles);
obj.handles = obj.handles(idx);
% Remove . directory if it exits
dotdir = [pth,filesep,'.',fn];
clearDotDir(dotdir);
% Save the children .figs
for i = 1:length(obj.handles);
obj.handles(i).imObjectName = fn;
obj.handles(i).imObjectPath = pth;
obj.handles(i).saveChildren;
end
% Update the control window position
set(obj.fig,'Units','normalized');
obj.position = get(obj.fig,'position');
% Save the optics object
obj.opticsPath = pth;
obj.opticsFile = [fn,ext];
save(filename,'-mat','obj');
end
% LOADWS: operates when loading a workspace
function obj = loadWS(obj,varargin)
% Load the *.sws file
if nargin == 2 && exist(varargin{1},'file');
filename = varargin{1};
else
spec = {'*.sws','MATLAB snow optics workspace (*.sws)'};
filename = gatherfile('get','LastUsedWorkSpaceDir',spec);
if isempty(filename); return; end
end
% Remove existing imObject, if desired
oldObj = obj;
if ~obj.appendWorkspace
idx = isvalid(obj.handles);
delete(obj.handles(idx));
end
% Load the workspace file
tmp = load(filename,'-mat'); obj = tmp.obj;
% Update the handles structure
obj.handles = [oldObj.handles,obj.handles];
idx = isvalid(obj.handles);
obj.handles = obj.handles(idx);
% updates the optics window position
set(obj.fig,'Units','normalized','Position',obj.position);
end
% CLOSEOPTICS: operates when the GUI is being closed
function closeOptics(obj)
% Close the ftp connection
if ~isnumeric(obj.FTP); close(obj.FTP); end
% Delete the imObjects
idx = isvalid(obj.handles);
obj.handles = obj.handles(idx);
for i = 1:length(obj.handles);
delete(obj.handles(i));
end
% Update the last used position
p = get(obj.fig,'Position');
set(obj.fig,'units','normalized');
setpref('imobject','LastOpticsGUIPosition',p);
% Delete the program control window
delete(obj.fig);
% Delete the options window
H = findobj('Tag','OpticsOptions'); delete(H);
% Delete database images, if desired
if ~obj.keeplocal
rmdir(obj.target,'s');
end
end
end
% DEFINE THE STATIC METHODS FOR optics CLASS
methods (Static)
% LOADOBJ: Operates when the object is loaded via load function
function obj = loadobj(obj)
% Add required paths
addpath('imPlugin','bin');
obj.fig = findobj('Name','Optics Program Control');
if isempty(obj.fig);
obj.fig = open('controlGUI.fig');
set(obj.fig,'Units','Normalized','Position',obj.position);
end
initControlGUI(obj); % Initilizes the image (as created)
end
end % ends static methods
end
%--------------------------------------------------------------------------
function initControlGUI(obj)
% INITCONTROLGUI initilizes the control GUI
% Gather the handles to the GUI and store optics object handle
h = guihandles(obj.fig);
guidata(obj.fig,obj);
% Set the window name and callbacks for closing the GUI
set(obj.fig,'Name','Optics Program Control');
set(obj.fig,'CloseRequestFcn',@(src,event)closeOptics(obj));
set(h.options,'Callback',@callback_pref);
set(h.exit,'Callback',@(src,event)closeOptics(obj));
% Define callbacks for folder selection
set(h.exp,'Callback',@callback_exp,'Value',1);
set(h.folder,'Callback',@callback_folder,'Value',1);
set(h.type,'Callback',@callback_type,'Value',1);
set([h.angles,h.zenith,h.viewer,h.azimuth],'Callback',@callback_angle,...
'Value',1);
% Define callback for opening the images
set(h.openimages,'Callback',@(src,event)openImages(obj));
% Define callbacks for the menu items
set(h.WSsave,'callback',@(src,event)saveWS(obj,obj.opticsPath,...
obj.opticsFile));
set(h.WSsaveas,'callback',@(src,event)saveWS(obj,'',''));
set(h.WSopen,'callback',@(src,event)loadWS(obj));
set(h.gethelp,'callback','gethelp');
set(h.about,'callback',{'about',obj});
% Get the default preferences
obj.getdefaultPref;
% Set the version propertery
setpref('OpticsObject','version',{obj.version,obj.verdate});
% Intilize the GUI by calling the experiment folder callback
callback_exp(h.exp,[],'init');
end
%--------------------------------------------------------------------------
function callback_exp(hObject,~,varargin)
% CALLBACK_EXP operates when the user selects an experiment
% Gather the optics object and GUI handles
obj = guidata(hObject);
h = guihandles(hObject);
% Gather the folder structure from the FTP site
if obj.useftp
obj.connectFTP;
if isnumeric(obj.FTP);
data = getLocalFolderData(obj.target);
else
data = struct2cell(dir(obj.FTP));
close(obj.FTP);
end
else
data = getLocalFolderData(obj.target);
end
str = data(1,:);
set(hObject,'String',str);
% Setup the folder structure, varagin{1} = 'init' uses the saved value
val = initFolder(hObject,obj.exp,varargin{:});
obj.exp = str{val};
% Move to the next folder level
callback_folder(h.folder,[],varargin{:});
end
%--------------------------------------------------------------------------
function callback_folder(hObject,~,varargin)
% CALLBACK_FOLDER operates when the user selects a folder
% Gather the optics object and GUI handles
obj = guidata(hObject);
h = guihandles(hObject);
% Gather the folder structure from the FTP site
if obj.useftp
obj.connectFTP;
data = struct2cell(dir(obj.FTP,obj.exp));
loc = 3;
close(obj.FTP);
else
data = getLocalFolderData([obj.target,filesep,obj.exp]);
loc = 4;
end
% Only considers folder
idx = cell2mat(data(loc,:));
str = data(1,idx);
set(hObject,'String',str);
% Setup the folder structure, varagin{1} = 'init' uses the saved value
val = initFolder(hObject,obj.folder,varargin{:});
obj.folder = str{val};
% Move to the next folder level
callback_type(h.type,[],varargin{:});
end
%--------------------------------------------------------------------------
function callback_type(hObject,~,varargin)
% CALLBACK_TYPE operates when the user selects a type
% Gather the optics object
obj = guidata(hObject);
% Gather the folder structure
thepath = buildpath(obj.exp,obj.folder);
if obj.useftp
obj.connectFTP;
data = struct2cell(dir(obj.FTP,thepath));
loc = 3;
close(obj.FTP);
else
data = getLocalFolderData([obj.target,filesep,thepath]);
loc = 4;
end
% Only consider folders
idx = cell2mat(data(loc,:));
str = data(1,idx);
set(hObject,'String',str);
% Setup the folder structure, varagin{1} = 'init' uses the saved value
val = initFolder(hObject,obj.type,varargin{:});
obj.type = str{val};
% Move to the next folder level
callback_angle(hObject,[],varargin{:});
end
%--------------------------------------------------------------------------
function callback_angle(hObject,~,varargin)
% CALLBACK_ANGLE operates when angle panel is changed or toggled
% Gather the optics object and GUI handles
obj = guidata(hObject);
h = guihandles(hObject);
hangles = [h.zenith,h.viewer,h.azimuth];
% Gather files from current directory if toggle is 'off'
value = get(h.angles,'Value');
if ~value; % case when 'off' (ignores angle directory)
set(hangles,'enable','off');
obj.thepath = buildpath(obj.exp,obj.folder,obj.type);
obj.angle = '';
obj.getFiles;
else % case when 'on' (uses angle directory)
set(hangles,'enable','on');
obj.angle = getAngleFolder(h,obj,varargin{:});
obj.thepath = buildpath(obj.exp,obj.folder,obj.type,obj.angle);
obj.getFiles;
end
end
%--------------------------------------------------------------------------
function callback_pref(hObject,~)
% CALLBACK_PREF opens the optics object preferences
% Gather GUI and object information and open the pref window
obj = guidata(hObject);
H = findobj('Tag','OpticsOptions'); delete(H);
H = open('opticsPrefGUI.fig'); drawnow;
h = guihandles(H);
% Cylce through each pref and update the value and callback
for i = 1:length(obj.pref);
p = obj.pref{i};
if isnumeric(obj.(p)) || islogical(obj.(p));
type = 'Value'; obj.(p) = double(obj.(p));
elseif ischar(p);
type = 'String';
end
set(h.(p),'Callback',@(src,evnt)opticsPref(obj,p),...
type,obj.(p));
end
% Set the load/save callbacks
set(h.changetarget,'callback',{@callback_changetarget,obj});
set(h.savedef,'callback',@(src,evnt)setdefaultPref(obj));
set(h.loaddef,'callback',@(src,evnt)getdefaultPref(obj));
set(h.close,'Callback','close(gcbf)');
end
%--------------------------------------------------------------------------
function callback_changetarget(hObject,~,obj)
% CALLBACK_CHANGETARGET changes the targed directory for local image files
% Determine/set the last used directory
if ispref('OpticsOptions','LastUsedOpticsDatabaseDir');
pth = getpref('OpticsOptions','LastUsedOpticsDatabaseDir');
else
pth = cd;
end
% Gather the directory
pth = uigetdir(pth,'Select taget directory');
if isnumeric(pth); return; end
% Update the object, GUI, and stored directory
obj.target = pth;
h = guihandles(hObject);
set(h.target,'String',pth);
setpref('OpticsOptions','LastUsedOpticsDatabaseDir',pth);
end
%--------------------------------------------------------------------------
function angle = getAngleFolder(h,obj,varargin)
% GETANGLEFOLDER gathers the angle, viewer, zintth folder
% 1 - INTILIZATION
% Gather the current path based on selected folders
thepath = buildpath(obj.exp,obj.folder,obj.type);
% Gather exiting angles
z = ''; v = ''; a = '';
if ~isempty(obj.angle);
z = obj.angle(2:3);
v = obj.angle(5:6);
a = obj.angle(end);
end
% 2 - DEFINE THE ZENITH ANGLES
% Gather folders for available zeinth angles
if obj.useftp;
zdir = dir(obj.FTP,[thepath,'/Z*']);
else
zdir = dir([obj.target,filesep,thepath,'/Z*']);
end
% Return if the zeinth angle folder does not exist
if isempty(zdir);
disp('No angle directories exist!');
set(h.angles,'Value',0);
callback_exp(h.exp,[],'init');
angle = '';
return;
end
% Collect the available zeith angles
if obj.useftp
data = struct2cell(dir(obj.FTP,[thepath,'/Z*']));
else
data = getLocalFolderData([obj.target,'/',thepath,'/Z*']);
end
str = char(data(1,:));
Z = unique(cellstr(str(:,2:3)));
set(h.zenith,'String',Z);
val = initFolder(h.zenith,z,varargin{:});
z = Z{val};
% 3 - DEFINE THE VIEWER ANGLES
% Define the available viewer angles
if obj.useftp
data = struct2cell(dir(obj.FTP,[thepath,'/Z',z,'*']));
else
data = getLocalFolderData([obj.target,'/',thepath,'/Z',z,'*']);
end
% Collect/dispaly the available viewer angles
str = char(data(1,:));
V = unique(cellstr(str(:,5:6)));
set(h.viewer,'String',V);
val = initFolder(h.viewer,v,varargin{:});
if length(V) < val; set(h.viewer,'Value',1); end
v = V{get(h.viewer,'Value')};
% 4 - DEFINE ThE AVAILALBE AZIMUTHS
% Define the available azimuths
if obj.useftp
data = struct2cell(dir(obj.FTP,[thepath,'/Z',z,'V',v,'*']));
else
data = getLocalFolderData([obj.target,'/',thepath,'/Z',z,'V',v,'*']);
end
% Gather dispaly the azimuths
str = char(data(1,:));
A = unique(cellstr(str(:,7)));
set(h.azimuth,'String',A);
val = initFolder(h.azimuth,a,varargin{:});
if length(A) < val; set(h.azimuth,'Value',1); end
a = A{get(h.azimuth,'Value')};
% 5 - OUTPUT THE CURRENT ANGLE FOLDER
angle = ['Z',z,'V',v,a];
end
%--------------------------------------------------------------------------
function thepath = buildpath(varargin)
% BUILDPATH: construct the path for accessing images in the ftp server
thepath = varargin{1};
for i = 2:length(varargin);
if ~isempty(varargin{i});
thepath = [thepath,'/',varargin{i}];
end
end
end
%--------------------------------------------------------------------------
function val = initFolder(hObject,str,varargin)
% INITFOLDER initilize the folder sturture of the optics control GUI
strarray = get(hObject,'String');
if ~isempty(varargin) && strcmpi(varargin{1},'init') && ~isempty(str);
val = strmatch(str,strarray);
if isempty(val); val = get(hObject,'Value'); end
else
val = get(hObject,'Value');
end
set(hObject,'Value',val) ;
end
%--------------------------------------------------------------------------
function openRemote(obj,files)
% OPENREMOTE downloads and opens files from the remote FTP site
% Establish FTP
obj.connectFTP;
% Change the directory to the desired folder
cd(obj.FTP,obj.thepath);
localpath = regexprep(obj.thepath,'/',filesep);
localpath = [obj.target,filesep,localpath];
% Create the local directory if it does not exist
if ~exist(localpath,'dir');
mkdir(localpath);
end
% Cylce through the images and download to local directory
d = waitdlg('Opening/downloading images, please wait...');
for i = 1:length(files);
filename = [localpath,filesep,files{i}];
[~,~,ext] = fileparts(filename);
if ~exist(filename,'file');
mget(obj.FTP,files{i},localpath);
if strcmpi('.bil',ext) || strcmpi('.bip',ext);
mget(obj.FTP,[files{i},'.hdr'],localpath);
end
end
obj.handles(end+1) = imObject(filename);
end
if ishandle(d); delete(d); end
% Close the FTP connection
close(obj.FTP);
% % Return the ftp directory to the base
% cd(obj.FTP,obj.thedir);
end
%--------------------------------------------------------------------------
function openLocal(obj,files)
% OPENLOCAL opens files from the local database
% Change the directory to the desired folder
localpath = [obj.target,filesep,obj.thepath];
% Cylce through the images and download to local directory
for i = 1:length(files);
filename = [localpath,filesep,files{i}];
obj.handles(end+1) = imObject(filename);
end
end
%--------------------------------------------------------------------------
function data = getLocalFolderData(thepath)
% GETLOCALFOLDERDATA returns the cell array for local folders
% Gather the local folders
tmp = struct2cell(dir(thepath));
k = 1;
for i = 1:size(tmp,2);
if ~strcmpi(tmp{1,i}(1),'.');
idx(k) = i;
k = k + 1;
end
end
data = tmp(:,idx);
end