source: trunk/src/uvmat.m @ 580

Last change on this file since 580 was 580, checked in by sommeria, 11 years ago

bugs corrected for combining fields

File size: 215.9 KB
Line 
1%'uvmat': function associated with the GUI 'uvmat.fig' for images and data field visualization
2%------------------------------------------------------------------------
3% function huvmat=uvmat(input)
4%
5%OUTPUT
6% huvmat=current handles of the GUI uvmat.fig
7%%
8%
9%INPUT:
10% input: input file name (if character chain), or input image matrix to
11% visualize, or Matlab structure representing  netcdf fieldname (with fieldname
12% ListVarName....)
13%
14%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
15%  Copyright Joel Sommeria,  2008, LEGI / CNRS-UJF-INPG, joel.sommeria@legi.grenoble-inp.fr.
16%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
17%     This open is part of the toolbox UVMAT.
18%
19%     UVMAT is free software; you can redistribute it and/or modify
20%     it under the terms of the GNU General Public License as published by
21%     the Free Software Foundation; either version 2 of the License, or
22%     (at your option) any later version.
23%
24%     UVMAT is distributed in the hope that it will be useful,
25%     but WITHOUT ANY WARRANTY; without even the implied warranty of
26%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27%     GNU General Public License (open UVMAT/COPYING.txt) for more details.
28%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
29%
30% Information stored on the interface:(use 'Export/field in workspace' in
31% the menu bar of uvmat to retrieve it)
32%          .OpenParam: structure containing parameters defined when uvmat is opened
33%                       .PosColorbar: position (1x4 vector)of the colorbar (relative to the fig uvmat)
34%                       .PosSetObject: position of set_object
35%                       .PosGeometryCalib: size of set_object
36%                       .NbBuiltin: nbre of functions always displayed in transform_fct menu
37%          .Object: cell array of structures representing the current projection objects, as produced by 'set_object.m'={[]} by default
38%          .NewSeries: =0/1 flag telling whether a new field series has been opened
39%          .FileName_1: name of the current second field (used to detect a  constant field during file scanning)
40%          .FileType: current file type, as defined by the fct  get_file_type.m)
41%          .i1_series,.i2_series,.j1_series,.j1_series: series of i1,i2,j1,j2 indices detected in the input dir,set by  the fct find_file_series
42%          .MovieObject: current movie object
43%          .TimeUnit: unit for time
44%          .XmlData: cell array of 1 or 2 structures representing the xml files associated with the input fieldname (containing timing  and geometry calibration)
45%          .Field: cell array of 1 or 2 structures representing the current  input field(s)
46%          .PlotAxes: field structure representing the current field plotted  on the main axes  (used for mouse operations)
47%          .HistoAxes: idem for histogram axes
48
49% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   DATA FLOW  (for run0_Callback) %%%%%%%%%%%%%%%%%%%%:
50%
51%
52% 1) Input filenames are determined by MenuBrowse (first field), MenuBrowse_1
53% (second field), or by the stored file name .FileName_1, or as an input of uvmat.
54% 2) These functions call 'uvmat/display_file_name.m' which detects the file series, and fills the file index boxes
55% 3) Then 'uvmat/update_rootinfo.m' Updates information about a new field series (indices to scan, timing, calibration from an xml file)
56% 4) Then fieldname are opened and visualised by the main sub-function 'uvmat/refresh_field.m'
57% The function first reads the name of the input file(s) (one or two) from the edit boxes  of the GUI
58% It then reads the input file(s) with the function read_field.m and perform the following list of operations:
59%
60%    %%%%%%%%  structure of uvmat/refresh_field.m %%%%%%%%
61%
62%           Main input open       second input open_1       
63%                    |                   | 
64%             read_field.m            read_field.m
65%                    |                   |
66%                 Field{1}            Field{2}               
67%                    |                   |                                 
68%                    --->transform fct<---             transform (e.g. phys.m) and combine input fieldname 
69%                            |                                   
70%                        (calc_tps.m)               calculate tps coefficients (for filter projection or spatial derivatives).
71%                            |
72%                       UvData.Field-------------->histogram
73%               _____________|____________
74%              |                          |                   
75%        proj_field.m               proj_field.m       project the field on the projection objects (use calc_field.m)           
76%              |                          |
77%         UvData.PlotAxes          ViewData.PlotAxes (on view_field)
78%              |                          |
79%       plot_field.m (uvmat)       plot_field.m (view_field)      plot the projected fieldname
80%
81%
82%%%%%%%%%%%%%%    SCALARS: %%%%%%%%%%%%??%%%
83% scalars are displayed either as an image or countour plot, either as a color of
84% velocity vectors. The scalar values in the first case is represented by
85% UvData.Field.A, and by UvData.Field.C in the second case. The corresponding set of X
86% and Y coordinates are represented by UvData.Field.AX and UvData.Field.AY, and .X and
87% .Y for C (the same as velocity vectors). If A is a nxxny matrix (scalar
88% on a regtular grid), then .AX andf.AY contains only two elements, represneting the
89% coordinates of the four image corners. The scalar name is represented by
90% the strings .AName and/or .CName.
91% If the scalar exists in an input open (image or scalar stored under its
92% name in a netcdf open), it is directly read at the level of Field{1}or Field{2}.
93% Else only its name AName is recorded in Field{i}, and its field is then calculated
94%by the fuction calc_scal after the coordinate transform or after projection on an edit_object
95     
96% Properties attached to plotting figures (standard Matlab properties):
97%    'CurrentAxes'= gca or get(gcf,'CurrentAxes');
98%    'CurrentPoint'=get(gcf,'CurrentPoint'): figure coordinates of the point over which the mouse is positioned
99%    'CurrentCharacter'=get(gcf,'CurrentCharacter'): last character typed  over the figure where the mouse is positioned
100%    'WindowButtonMotionFcn': function permanently called by mouse motion over the figure
101%    'KeyPressFcn': function called by pressing a key on the key board
102%    'WindowButtonDownFcn':  function called by pressing the mouse over the  figure
103%    'WindowButtonUpFcn': function called by releasing  the mouse pressure over the  figure
104
105% Properties attached to plotting axes:
106%    'CurrentPoint'=get(gca,'CurrentPoint'); (standard Matlab) same as for the figure, but position in plot coordinates.
107%     AxeData:=get(gca,'UserData');
108%     AxeData.Drawing  = create: create a new object
109%                       = deform: modify an existing object by moving its defining create
110%                      = off: no current drawing action
111%                     = translate: translate an existing object
112%                    = calibration: move a calibration point
113%                    = CheckZoom: isolate a subregion for CheckZoom in=1 if an object is being currently drawn, 0 else (set to 0 by releasing mouse button)
114%            .CurrentOrigin: Origin of a curently drawn edit_object
115%            .CurrentLine: currently drawn menuline (A REVOIR)
116%            .CurrentObject: handle of the currently drawn edit_object
117%            .CurrentRectZoom: current rectangle used for CheckZoom
118
119% Properties attached to projection objects (create, menuline, menuplane...):
120%    'Tag'='proj_object': for all projection objects
121%    ObjectData.Type=...: style of projection object:
122%              .ProjMode
123%              .Coord: defines the position of the object
124%              .XMin,YMin....
125%              .XMax,YMax....
126%              .DX,DY,DZ
127%              .Phi, .Theta, .Psi : Euler angles
128%              .X,.Y,.U,.V.... : field data projected on the object
129%              .IndexObj: index in the list of UvData.Object
130           %during plotting
131%               .plotaxes: handles of the current axes used to plot the  result of field projection on the object
132%               .plothandle: vector of handle(s) of the object graphic represnetation in all the opened plotting axes
133% To each projection object #iobj, corresponds an axis
134% Object{iobj}.plotaxes and nbobj representation graphs  Object{iobj}.plothandles(:) (where nbobj is the
135% nbre of current objects opened in uvmat. Note that Object{iobj}.plothandles(iobj)=[] : an object is not represented in its own projection field;
136
137%------------------------------------------------------------------------
138%------------------------------------------------------------------------
139%  I - MAIN FUNCTION uvmat
140%------------------------------------------------------------------------
141%------------------------------------------------------------------------
142function varargout = uvmat(varargin)
143
144% Begin initialization code - DO NOT EDIT
145gui_Singleton = 1;
146gui_State = struct('gui_Name',          mfilename, ...
147                   'gui_Singleton',     gui_Singleton, ...
148                   'gui_OpeningFcn',    @uvmat_OpeningFcn, ...
149                   'gui_OutputFcn',     @uvmat_OutputFcn, ...
150                   'gui_LayoutFcn',     [], ...
151                   'gui_Callback',      []);
152if nargin && ischar(varargin{1})&& ~isempty(regexp(varargin{1},'_Callback','once'))
153    gui_State.gui_Callback = str2func(varargin{1});
154end
155
156if nargout
157    varargout{1:nargout} = gui_mainfcn(gui_State, varargin{:});
158else
159    gui_mainfcn(gui_State, varargin{:});
160end
161% End initialization code - DO NOT EDIT
162
163%------------------------------------------------------------------------
164% --- Executes just before the GUI uvmat is made visible.
165function uvmat_OpeningFcn(hObject, eventdata, handles, input )
166%------------------------------------------------------------------------
167
168%% Choose default command menuline output for uvmat (standard GUI)
169handles.output = hObject;
170
171%% Update handles structure (standard GUI)
172guidata(hObject, handles);
173
174%% add the path to uvmat (useful if uvmat has been opened in the working directory and a working directory change occured)
175path_uvmat=fileparts(which('uvmat'));
176
177%% set the position of colorbar and ancillary GUIs:
178set(hObject,'Units','Normalized')
179movegui(hObject,'center')
180%UvData.OpenParam.PosColorbar=[0.8450    0.0900    0.0190    0.3600];
181UvData.OpenParam.PosColorbar=[0.805 0.022 0.019 0.445];
182UvData.OpenParam.PosSetObject=[-0.05 -0.03 0.3 0.7]; %position for set_object
183UvData.OpenParam.PosGeometryCalib=[0.95 -0.03 0.28 1 ];%position for geometry_calib (TO IMPROVE)
184% UvData.OpenParam.CalSize=[0.28 1];
185% UvData.PlotAxes=[];%initiate the record of plotted field
186% UvData.axes2=[];
187% UvData.axes1=[];
188AxeData.LimEditBox=1; %initialise AxeData
189set(handles.PlotAxes,'UserData',AxeData)
190
191%% set functions for the mouse and keyboard
192set(hObject,'KeyPressFcn',{'keyboard_callback',handles})%set keyboard action function
193set(hObject,'WindowButtonMotionFcn',{'mouse_motion',handles})%set mouse action functio
194set(hObject,'WindowButtonDownFcn',{'mouse_down'})%set mouse click action function
195set(hObject,'WindowButtonUpFcn',{'mouse_up',handles})
196set(hObject,'DeleteFcn',{@closefcn})%
197
198%% initialisation
199% set(handles.ListObject,'Value',1)% default: empty projection objectproj_field
200% set(handles.ListObject,'String',{''})
201% set(handles.ListObject_1,'Value',1)% default: empty projection objectproj_field
202% set(handles.ListObject_1,'String',{''})
203set(handles.FieldName,'Value',1)
204set(handles.FieldName,'string',{''})
205UvData.Object={[]};
206
207%% TRANSFORM menu: builtin fcts
208transform_menu={'';'sub_field';'phys';'phys_polar'};
209UvData.OpenParam.NbBuiltin=numel(transform_menu); %number of functions
210transform_path=fullfile(path_uvmat,'transform_field');
211path_list=cell(UvData.OpenParam.NbBuiltin,1);
212path_list{1}='';
213for ilist=2:UvData.OpenParam.NbBuiltin
214path_list{ilist}=transform_path; % set transform_path to the path_list
215end
216
217%% load the list of previously browsed files in menus Open, Open_1 and transform_fct
218 dir_perso=prefdir; % path to the directory .matlab containing the personal data of the current user
219 profil_perso=fullfile(dir_perso,'uvmat_perso.mat');% personal data file uvmat_perso.mat' in .matlab
220 if exist(profil_perso,'file')
221     h=load (profil_perso);
222     if isfield(h,'MenuFile')% load the menu of previously opened files
223         for ifile=1:min(length(h.MenuFile),5)
224             set(handles.(['MenuFile_' num2str(ifile)]),'Label',h.MenuFile{ifile});
225             set(handles.(['MenuFile_' num2str(ifile) '_1']),'Label',h.MenuFile{ifile});
226         end
227     end
228     if isfield(h,'RootPath')
229         set(handles.RootPath,'UserData',h.RootPath); %store the previous campaign in the UserData of RootPath
230     end
231     if isfield(h,'transform_fct') && iscell(h.transform_fct) % load the menu of transform fct set by user
232         for ilist=1:length(h.transform_fct);
233             if exist(h.transform_fct{ilist},'file')
234                 [path,file]=fileparts(h.transform_fct{ilist});
235                 transform_menu=[transform_menu; {file}];
236                 path_list=[path_list; {path}];
237             end
238         end
239     end
240 end
241transform_menu=[transform_menu;{'more...'}];%append the option more.. to the menu
242set(handles.transform_fct,'String',transform_menu)% display the menu of transform fcts
243set(handles.transform_fct,'UserData',path_list)% store the corresponding list of path in UserData of uicontrol transform_fct
244set(handles.path_transform,'String','')
245set(handles.path_transform,'UserData',[])
246
247%% case of an input argument for uvmat
248testinputfield=0;
249inputfile=[];
250Field=[];
251if exist('input','var')
252    if ishandle(handles.UVMAT_title)
253        delete(handles.UVMAT_title)
254    end   
255    if isstruct(input)
256        if isfield(input,'InputFile')
257            inputfile=input.InputFile;
258        end
259        if isfield(input,'TimeIndex')
260            set(handles.i1,num2str(input.TimeIndex))
261        end
262        if isfield(input,'FieldsString')
263            UvData.FieldsString=input.FieldsString;
264        end
265    elseif ischar(input)% file name introduced as input
266           inputfile=input;
267    elseif isnumeric(input)%simple matrix introduced as input
268        sizinput=size(input);
269        if sizinput(1)<=1 || sizinput(2)<=1
270            msgbox_uvmat('ERROR','bad input for uvmat: file name, structure or numerical matrix accepted')
271            return
272        end
273        UvData.Field.ListVarName={'A','coord_y','coord_x'};
274        UvData.Field.VarDimName={{'coord_y','coord_x'},'cord_y','coord_x'};
275        UvData.Field.A=input;
276        UvData.Field.coord_x=[0.5 size(input,2)-0.5];
277        UvData.Field.coord_y=[size(input,1)-0.5 0.5];
278        testinputfield=1;
279    end
280else
281    %% check the path and date of modification of all functions in uvmat
282    path_to_uvmat=which ('uvmat');% check the path detected for source file uvmat
283    [infomsg,date_str,svn_info]=check_files;%check the path of the functions called by uvmat.m   
284    date_str=['last modification: ' date_str];
285    if ishandle(handles.UVMAT_title)
286        set(handles.UVMAT_title,'String',...
287            [{'Copyright  LEGI UMR 5519 /CNRS-UJF-Grenoble INP, 2010'};...
288            {'GNU General Public License'};...
289            {path_to_uvmat};...
290            {date_str};...
291            infomsg]);
292    end
293end
294set(handles.uvmat,'UserData',UvData)
295if ~isempty(inputfile)
296    %%%%% display the input field %%%%%%%
297    display_file_name(handles,inputfile)
298    %%%%%%%
299end
300
301set_vec_col_bar(handles) %update the display of color code for vectors
302
303%------------------------------------------------------------------------
304% --- Outputs from this function are returned to the command menuline.
305function varargout = uvmat_OutputFcn(hObject, eventdata, handles)
306varargout{1} = handles.output;% the only output argument is the handle to the GUI figure
307
308%------------------------------------------------------------------------
309% --- executed when closing uvmat: delete or desactivate the associated figures if exist
310function closefcn(gcbo,eventdata)
311%------------------------------------------------------------------------
312hh=findobj(allchild(0),'tag','view_field');
313if ~isempty(hh)
314    delete(hh)
315end
316hh=findobj(allchild(0),'tag','geometry_calib');
317if ~isempty(hh)
318    delete(hh)
319end
320hh=findobj(allchild(0),'tag','set_object');
321if ~isempty(hh)
322    hhh=findobj(hh,'tag','PLOT');
323    set(hhh,'enable','off')
324end
325
326%------------------------------------------------------------------------
327%------------------------------------------------------------------------
328%  II - FUNCTIONS FOR INTRODUCING THE INPUT FILES
329% automatically sets the global properties when the rootfile name is introduced
330% then activate the view-field action if selected
331% it is activated either by clicking on the RootPath window or by the
332% browser
333%------------------------------------------------------------------------
334%------------------------------------------------------------------------
335% --- Executes on the menu Open/Browse...
336% search the files, recognize their type according to their name and fill the rootfile input windows
337function MenuBrowse_Callback(hObject, eventdata, handles)
338[RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes(handles);
339oldfile=fullfile(RootPath,SubDir);
340if isempty(oldfile) %loads the previously stored file name and set it as default in the file_input box
341    oldfile=get(handles.RootPath,'UserData');
342end
343[FileName, PathName] = uigetfile({'*.*','All Files(*.*)'},'Pick a file',oldfile);
344if ~ischar(FileName),return,end %abandon if the browser is cancelled
345fileinput=[PathName FileName];%complete file name
346
347%% display the selected field and related information
348display_file_name( handles,fileinput)
349
350% -----------------------------------------------------------------------
351% --- Executes on the menu Open/Browse campaign...
352% search the file inside a campaign, using the GUI view_data
353function MenuBrowseCampaign_Callback(hObject, eventdata, handles)
354% -----------------------------------------------------------------------
355RootPath=get(handles.RootPath,'String');
356if isempty(RootPath)
357    RootPath=get(handles.RootPath,'UserData');%use Rootpath recored from the personal file at uvmat opening
358end
359CampaignPath=fileparts(fileparts(RootPath));
360DirFull = uigetdir(CampaignPath,'Select a Campaign dir, then press OK');
361if ~ischar(DirFull)|| ~exist(DirFull,'dir')
362    return
363end
364OutPut=browse_data(DirFull);% open the GUI browse_data to get select a campaign dir, experiment and device
365if ~isfield(OutPut,'Campaign')
366    return
367end
368DirName=fullfile(OutPut.Campaign,OutPut.Experiment{1},OutPut.Device{1});
369hdir=dir(DirName); %list files and dirs
370for ilist=1:numel(hdir)
371    if ~isequal(hdir(ilist).isdir,1)%look for files, not dir
372        FileName=hdir(ilist).name;
373        FileType=get_file_type(fullfile(DirName,FileName));
374        switch FileType
375            case {'image','multimage','civx','civdata','netcdf'}
376            break
377        end
378    end
379end
380
381%% display the selected field and related information
382display_file_name( handles,fullfile(DirName,FileName))
383
384% -----------------------------------------------------------------------
385% --- Open again the file whose name has been recorded in MenuFile_1
386function MenuFile_1_Callback(hObject, eventdata, handles)
387%------------------------------------------------------------------------
388fileinput=get(handles.MenuFile_1,'Label');
389display_file_name( handles,fileinput)
390
391% -----------------------------------------------------------------------
392% --- Open again the file whose name has been recorded in MenuFile_2
393function MenuFile_2_Callback(hObject, eventdata, handles)
394%------------------------------------------------------------------------
395fileinput=get(handles.MenuFile_2,'Label');
396display_file_name(handles,fileinput)
397
398% -----------------------------------------------------------------------
399% --- Open again the file whose name has been recorded in MenuFile_3
400function MenuFile_3_Callback(hObject, eventdata, handles)
401%------------------------------------------------------------------------
402fileinput=get(handles.MenuFile_3,'Label');
403display_file_name(handles,fileinput)
404
405% -----------------------------------------------------------------------
406% --- Open again the file whose name has been recorded in MenuFile_4
407function MenuFile_4_Callback(hObject, eventdata, handles)
408%------------------------------------------------------------------------
409fileinput=get(handles.MenuFile_4,'Label');
410display_file_name(handles,fileinput)
411
412% -----------------------------------------------------------------------
413% --- Open again the file whose name has been recorded in MenuFile_5
414function MenuFile_5_Callback(hObject, eventdata, handles)
415%------------------------------------------------------------------------
416fileinput=get(handles.MenuFile_5,'Label');
417display_file_name(handles,fileinput)
418
419%------------------------------------------------------------------------
420% --- Executes on the menu Open/Browse_1 for the second input field,
421%     search the files, recognize their type according to their name and fill the rootfile input windows
422function MenuBrowse_1_Callback(hObject, eventdata, handles)
423%------------------------------------------------------------------------
424RootPath=get(handles.RootPath,'String');
425[FileName, PathName] = uigetfile({'*.*','All Files(*.*)'},'Pick a file',RootPath);
426if ~ischar(FileName),return,end %abandon if the browser is cancelled
427fileinput_1=[PathName FileName];%complete file name
428
429% refresh the current displayed field
430set(handles.SubField,'Value',1)
431display_file_name(handles,fileinput_1,2)
432
433%update list of recent files in the menubar
434MenuFile_1=fileinput_1;
435MenuFile_2=get(handles.MenuFile_1,'Label');
436MenuFile_3=get(handles.MenuFile_2,'Label');
437MenuFile_4=get(handles.MenuFile_3,'Label');
438MenuFile_5=get(handles.MenuFile_4,'Label');
439set(handles.MenuFile_1,'Label',MenuFile_1)
440set(handles.MenuFile_2,'Label',MenuFile_2)
441set(handles.MenuFile_3,'Label',MenuFile_3)
442set(handles.MenuFile_4,'Label',MenuFile_4)
443set(handles.MenuFile_5,'Label',MenuFile_5)
444set(handles.MenuFile_1_1,'Label',MenuFile_1)
445set(handles.MenuFile_2_1,'Label',MenuFile_2)
446set(handles.MenuFile_3_1,'Label',MenuFile_3)
447set(handles.MenuFile_4_1,'Label',MenuFile_4)
448set(handles.MenuFile_5_1,'Label',MenuFile_5)
449% dir_perso=prefdir;
450% profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
451% if exist(profil_perso,'file')
452%     save (profil_perso,'MenuFile_1','MenuFile_2','MenuFile_3','MenuFile_4', 'MenuFile_5','-append'); %store the file names for future opening of uvmat
453% else
454%     txt=ver('MATLAB');
455%     Release=txt.Release;
456%     relnumb=str2double(Release(3:4));
457%     if relnumb >= 14
458%         save (profil_perso,'MenuFile_1','MenuFile_2','MenuFile_3','MenuFile_4', 'MenuFile_5','-V6'); %store the file names for future opening of uvmat
459%     else
460%         save (profil_perso,'MenuFile_1','MenuFile_2','MenuFile_3','MenuFile_4', 'MenuFile_5'); %store the file names for future opening of uvmat
461%     end
462% end
463
464% --------------------------------------------------------------------
465function MenuBrowseCampaign_1_Callback(hObject, eventdata, handles)
466% -----------------------------------------------------------------------
467CampaignPath=fileparts(fileparts(get(handles.RootPath,'String')));
468DirFull = uigetdir(CampaignPath,'Select a Campaign dir, then press OK');
469if ~ischar(DirFull)|| ~exist(DirFull,'dir')
470    return
471end
472OutPut=browse_data(DirFull);% open the GUI browse_data to get select a campaign dir, experiment and device
473if ~isfield(OutPut,'Campaign')
474    return
475end
476DirName=fullfile(OutPut.Campaign,OutPut.Experiment{1},OutPut.Device{1});
477hdir=dir(DirName); %list files and dirs
478for ilist=1:numel(hdir)
479    if ~isequal(hdir(ilist).isdir,1)%look for files, not dir
480        FileName=hdir(ilist).name;
481        FileType=get_file_type(fullfile(DirName,FileName));
482        switch FileType
483            case {'image','multimage','civx','civdata','netcdf'}
484            break
485        end
486    end
487end
488
489%% display the selected field and related information
490set(handles.SubField,'Value',1)
491display_file_name( handles,fullfile(DirName,FileName),2)
492
493% -----------------------------------------------------------------------
494% --- Open again as second field the file whose name has been recorded in MenuFile_1
495function MenuFile_1_1_Callback(hObject, eventdata, handles)
496% -----------------------------------------------------------------------
497fileinput_1=get(handles.MenuFile_1_1,'Label');
498set(handles.SubField,'Value',1)
499display_file_name(handles,fileinput_1,2)
500
501% -----------------------------------------------------------------------
502% --- Open again as second field the file whose name has been recorded in MenuFile_2
503function MenuFile_2_1_Callback(hObject, eventdata, handles)
504% -----------------------------------------------------------------------
505fileinput_1=get(handles.MenuFile_2_1,'Label');
506set(handles.SubField,'Value',1)
507display_file_name(handles,fileinput_1,2)
508
509% -----------------------------------------------------------------------
510% --- Open again as second field the file whose name has been recorded in MenuFile_3
511function MenuFile_3_1_Callback(hObject, eventdata, handles)
512% -----------------------------------------------------------------------
513fileinput_1=get(handles.MenuFile_3_1,'Label');
514set(handles.SubField,'Value',1)
515display_file_name(handles,fileinput_1,2)
516
517% -----------------------------------------------------------------------
518% --- Open again as second field the file whose name has been recorded in MenuFile_4
519function MenuFile_4_1_Callback(hObject, eventdata, handles)
520% -----------------------------------------------------------------------
521fileinput_1=get(handles.MenuFile_4_1,'Label');
522set(handles.SubField,'Value',1)
523display_file_name(handles,fileinput_1,2)
524
525% -----------------------------------------------------------------------
526% --- Open again as second field the file whose name has been recorded in MenuFile_5
527function MenuFile_5_1_Callback(hObject, eventdata, handles)
528% -----------------------------------------------------------------------
529fileinput_1=get(handles.MenuFile_5_1,'Label');
530set(handles.SubField,'Value',1)
531display_file_name(handles,fileinput_1,2)
532
533%------------------------------------------------------------------------
534% --- Called by action in RootPath edit box
535function RootPath_Callback(hObject,eventdata,handles)
536%------------------------------------------------------------------------
537% read the current input file name:
538[RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes(handles);
539if ~exist(fullfile(RootPath,SubDir),'dir')
540    msgbox_uvmat('ERROR',['directory ' fullfile(RootPath,SubDir) ' does not exist'])
541    return
542end
543% detect the file type, get the movie object if relevant, and look for the corresponding file series:
544[RootPath,SubDir,RootFile,i1_series,i2_series,j1_series,j2_series,tild,FileType,MovieObject]=find_file_series(fullfile(RootPath,SubDir),[RootFile FileIndices FileExt]);
545% initiate the input file series and refresh the current field view:
546update_rootinfo(handles,i1_series,i2_series,j1_series,j2_series,FileType,MovieObject,1);
547
548%-----------------------------------------------------------------------
549% --- Called by action in RootPath_1 edit box
550function RootPath_1_Callback(hObject,eventdata,handles)
551% -----------------------------------------------------------------------
552% update_rootinfo_1(hObject,eventdata,handles)
553[RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes_1(handles);
554if ~exist(fullfile(RootPath,SubDir),'dir')
555    msgbox_uvmat('ERROR',['directory ' fullfile(RootPath,SubDir) ' does not exist'])
556    return
557end
558% detect the file type, get the movie object if relevant, and look for the corresponding file series:
559[RootPath,SubDir,RootFile,i1_series,i2_series,j1_series,j2_series,tild,FileType,MovieObject]=find_file_series(fullfile(RootPath,SubDir),[RootFile FileIndices FileExt]);
560% initiate the input file series and refresh the current field view:
561update_rootinfo(handles,i1_series,i2_series,j1_series,j2_series,FileType,MovieObject,2);
562
563%------------------------------------------------------------------------
564% --- Called by action in RootFile edit box
565function SubDir_Callback(hObject, eventdata, handles)
566%------------------------------------------------------------------------
567%refresh the menu of input fieldname
568Fields_Callback(hObject, eventdata, handles);
569% refresh the current field view
570run0_Callback(hObject, eventdata, handles);
571
572%------------------------------------------------------------------------
573% --- Called by action in RootFile edit box
574function RootFile_Callback(hObject, eventdata, handles)
575%------------------------------------------------------------------------
576RootPath_Callback(hObject,eventdata,handles)
577
578%-----------------------------------------------------------------------
579% --- Called by action in RootFile_1 edit box
580function RootFile_1_Callback(hObject, eventdata, handles)
581% -----------------------------------------------------------------------
582RootPath_1_Callback(hObject,eventdata,handles)
583
584%------------------------------------------------------------------------
585% --- Called by action in FileIndex edit box
586function FileIndex_Callback(hObject, eventdata, handles)
587%------------------------------------------------------------------------
588[tild,tild,tild,i1,i2,j1,j2]=fileparts_uvmat(get(handles.FileIndex,'String'));
589set(handles.i1,'String',num2str(i1));
590set(handles.i2,'String',num2str(i2));
591set(handles.j1,'String',num2str(j1));
592set(handles.j2,'String',num2str(j2));
593
594% refresh the current field view
595run0_Callback(hObject, eventdata, handles)
596
597%------------------------------------------------------------------------
598% --- Called by action in FileIndex_1 edit box
599function FileIndex_1_Callback(hObject, eventdata, handles)
600%------------------------------------------------------------------------
601run0_Callback(hObject, eventdata, handles)
602
603%------------------------------------------------------------------------
604% --- Called by action in NomType edit box
605function NomType_Callback(hObject, eventdata, handles)
606%------------------------------------------------------------------------
607i1=str2num(get(handles.i1,'String'));
608i2=str2num(get(handles.i2,'String'));
609j1=str2num(get(handles.j1,'String'));
610j2=str2num(get(handles.j2,'String'));
611FileIndex=fullfile_uvmat('','','','',get(handles.NomType,'String'),i1,i2,j1,j2);
612set(handles.FileIndex,'String',FileIndex)
613% refresh the current settings and refresh the field view
614RootPath_Callback(hObject,eventdata,handles)
615
616%------------------------------------------------------------------------
617% --- Called by action in NomType edit box
618function NomType_1_Callback(hObject, eventdata, handles)
619%------------------------------------------------------------------------
620i1=str2num(get(handles.i1,'String'));
621i2=str2num(get(handles.i2,'String'));
622j1=str2num(get(handles.j1,'String'));
623j2=str2num(get(handles.j2,'String'));
624FileIndex=fullfile_uvmat('','','','',get(handles.NomType_1,'String'),i1,i2,j1,j2);
625set(handles.FileIndex_1,'String',FileIndex)
626% refresh the current settings and refresh the field view
627RootPath_1_Callback(hObject,eventdata,handles)
628
629%------------------------------------------------------------------------
630% --- Fills the edit boxes RootPath, RootFile,NomType...from an input file name 'fileinput'
631function display_file_name(handles,fileinput,index)
632%------------------------------------------------------------------------
633%% look for the input file existence
634if ~exist(fileinput,'file')
635    msgbox_uvmat('ERROR',['input file ' fileinput  ' does not exist'])
636    return
637end
638
639%% set the mouse pointer to 'watch'
640set(handles.uvmat,'Pointer','watch')
641set(handles.RootPath,'BackgroundColor',[1 1 0])
642drawnow
643
644%% define the relevant handles for the first field series (index=1) or the second file series (index=2)
645if ~exist('index','var')
646    index=1;
647end
648if index==1
649    handles_RootPath=handles.RootPath;
650    handles_SubDir=handles.SubDir;
651    handles_RootFile=handles.RootFile;
652    handles_FileIndex=handles.FileIndex;
653    handles_NomType=handles.NomType;
654    handles_FileExt=handles.FileExt;
655elseif index==2
656    handles_RootPath=handles.RootPath_1;
657    handles_SubDir=handles.SubDir_1;
658    handles_RootFile=handles.RootFile_1;
659    handles_FileIndex=handles.FileIndex_1;
660    handles_NomType=handles.NomType_1;
661    handles_FileExt=handles.FileExt_1;
662    set(handles.RootPath_1,'Visible','on')
663    set(handles.RootFile_1,'Visible','on')
664    set(handles.SubDir_1,'Visible','on');
665    set(handles.FileIndex_1,'Visible','on');
666    set(handles.FileExt_1,'Visible','on');
667    set(handles.NomType_1,'Visible','on');
668end
669
670%% detect root name, nomenclature and indices in the input file name:
671[FilePath,FileName,FileExt]=fileparts(fileinput);
672% detect the file type, get the movie object if relevant, and look for the corresponding file series:
673% the root name and indices may be corrected by including the first index i1 if a corresponding xml file exists
674[RootPath,SubDir,RootFile,i1_series,i2_series,j1_series,j2_series,NomType,FileType,MovieObject,i1,i2,j1,j2]=find_file_series(FilePath,[FileName FileExt]);
675
676%% open the file or fill the GUI uvmat according to the detected file type
677switch FileType
678    case ''
679        msgbox_uvmat('ERROR','invalid input file type')
680    case 'txt'
681        edit(fileinput)
682    case 'figure'                           %display matlab figure
683        hfig=open(fileinput);
684        set(hfig,'WindowButtonMotionFcn','mouse_motion')%set mouse action functio
685        set(hfig,'WindowButtonUpFcn','mouse_up')%set mouse click action function
686        set(hfig,'WindowButtonUpFcn','mouse_down')%set mouse click action function
687    case 'xml'                % edit xml files
688        t=xmltree(fileinput);
689        % the xml file marks a project or project link, open datatree_browser
690        if strcmp(get(t,1,'name'),'Project')&& exist(regexprep(fileinput,'.xml$',''),'dir')
691            datatree_browser(fileinput)
692        else % other xml file, open the xml editor
693            editxml(fileinput);
694        end
695    case 'xls'% Excel file opended by editxml
696        editxml(fileinput);
697    otherwise
698        set(handles_RootPath,'String',RootPath);
699        rootname=fullfile(RootPath,SubDir,RootFile);
700        set(handles_SubDir,'String',['/' SubDir]);
701        set(handles_RootFile,'String',['/' RootFile]); %display the separator
702        indices=fileinput(length(rootname)+1:end);
703        indices(end-length(FileExt)+1:end)=[]; %remove extension
704        set(handles_FileIndex,'String',indices);
705        set(handles_NomType,'String',NomType);
706        set(handles_FileExt,'String',FileExt);
707        if index==1
708            % fill file index counters if the first file series is opened
709            set(handles.i1,'String',num2str(i1));
710            set(handles.i2,'String',num2str(i2));
711            set(handles.j1,'String',num2stra(j1,NomType));
712            set(handles.j2,'String',num2stra(j2,NomType));
713        else %read the current field index to synchronise with the first series
714            i1_s=str2num(get(handles.i1,'String'));
715            i2_0=str2num(get(handles.i2,'String'));
716            if ~isempty(i2_0)
717                i2_s=i2_0;
718            else
719               i2_s=i2;
720            end
721            j1_0=stra2num(get(handles.j1,'String'));
722            if ~isempty(j1_0)
723                j1_s=j1_0;
724            else
725                j1_s=j1;
726            end
727            j2_0=stra2num(get(handles.j2,'String'));
728            if ~isempty(j2_0)
729                j2_s=j2_0;
730            else
731                j2_s=j2;
732            end
733        end
734       
735        % synchronise indices of the second  input file if it exists
736        if get(handles.SubField,'Value')==1% if the subfield button is activated, update the field numbers
737            Input=read_GUI(handles.InputFile);
738            if ~isfield(Input,'RootPath_1')||strcmp(Input.RootPath_1,'"')
739                Input.RootPath_1=Input.RootPath;
740            end
741            if ~isfield(Input,'SubDir_1')||strcmp(Input.SubDir_1,'"')
742                Input.SubDir_1=Input.SubDir;
743            end
744            if ~isfield(Input,'RootFile_1')||strcmp(Input.RootFile_1,'"')
745                Input.RootFile_1=Input.RootFile;
746            end
747            if ~isfield(Input,'FileExt_1')||strcmp(Input.FileExt_1,'"')
748                Input.FileExt_1=Input.FileExt;
749            end
750            if ~isfield(Input,'NomType_1')||strcmp(Input.NomType_1,'"')
751                Input.NomType_1=Input.NomType;
752            end
753            %updtate the indices of the second field series to correspond to the newly opened one
754            FileName_1=fullfile_uvmat(Input.RootPath_1,Input.SubDir_1,Input.RootFile_1,Input.FileExt_1,Input.NomType_1,i1_s,i2_s,j1_s,j2_s);
755            if exist(FileName_1,'file')
756                FileIndex_1=fullfile_uvmat('','','','',Input.NomType_1,i1_s,i2_s,j1_s,j2_s);
757            else
758                FileIndex_1=fullfile_uvmat('','','','',Input.NomType_1,i1,i2,j1,j2);
759                msgbox_uvmat('WARNING','unable to synchronise the indices of the two series')
760            end
761            set(handles.FileIndex_1,'String',FileIndex_1)
762        end
763       
764        %enable other menus
765        set(handles.MenuOpen_1,'Enable','on')
766        set(handles.MenuFile_1_1,'Enable','on')
767        set(handles.MenuFile_2_1,'Enable','on')
768        set(handles.MenuFile_3_1,'Enable','on')
769        set(handles.MenuFile_4_1,'Enable','on')
770        set(handles.MenuFile_5_1,'Enable','on')
771        set(handles.MenuExport,'Enable','on')
772        set(handles.MenuExportFigure,'Enable','on')
773        set(handles.MenuExportMovie,'Enable','on')
774        set(handles.MenuTools,'Enable','on')
775
776        % initiate input file series and refresh the current field view:     
777        update_rootinfo(handles,i1_series,i2_series,j1_series,j2_series,FileType,MovieObject,index);
778
779end
780
781%% update list of recent files in the menubar and save it for future opening
782MenuFile=[{get(handles.MenuFile_1,'Label')};{get(handles.MenuFile_2,'Label')};...
783    {get(handles.MenuFile_3,'Label')};{get(handles.MenuFile_4,'Label')};{get(handles.MenuFile_5,'Label')}];
784str_find=strcmp(fileinput,MenuFile);
785if isempty(find(str_find,1))
786    MenuFile=[{fileinput};MenuFile];%insert the current file if not already in the list
787end
788for ifile=1:min(length(MenuFile),5)
789    set(handles.(['MenuFile_' num2str(ifile)]),'Label',MenuFile{ifile});
790    set(handles.(['MenuFile_' num2str(ifile) '_1']),'Label',MenuFile{ifile});
791end
792dir_perso=prefdir;
793profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
794if exist(profil_perso,'file')
795    save (profil_perso,'MenuFile','RootPath','-append'); %store the file names for future opening of uvmat
796else
797    save (profil_perso,'MenuFile','RootPath','-V6'); %store the file names for future opening of uvmat
798end
799
800%% set back the mouse pointer to arrow
801set(handles.uvmat,'Pointer','arrow')
802
803
804%------------------------------------------------------------------------
805% --- Update information about a new field series (indices to scan, timing,
806%     calibration from an xml file, then refresh current plots
807
808function update_rootinfo(handles,i1_series,i2_series,j1_series,j2_series,FileType,VideoObject,index)
809%------------------------------------------------------------------------
810%% define the relevant handles depending on the index (1=first file series, 2= second file series)
811if ~exist('index','var')
812    index=1;
813end
814if index==1
815    handles_RootPath=handles.RootPath;
816    handles_Fields=handles.FieldName;
817elseif index==2
818    handles_RootPath=handles.RootPath_1;
819    handles_Fields=handles.FieldName_1;
820end
821
822set(handles_RootPath,'BackgroundColor',[1 1 0])
823drawnow
824set(handles.FieldName,'UserData',[])% reinialize data from uvmat opening
825UvData=get(handles.uvmat,'UserData');%huvmat=handles of the uvmat interface
826UvData.NewSeries=1; %flag for run0: begin a new series
827UvData.FileName_1='';% name of the current second field (used to detect a  constant field during file scanning)
828UvData.FileType{index}=FileType;
829UvData.i1_series{index}=i1_series;
830UvData.i2_series{index}=i2_series;
831UvData.j1_series{index}=j1_series;
832UvData.j2_series{index}=j2_series;
833set(handles.FixVelType,'Value',0); %desactivate fixed veltype
834if index==1
835    [RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes(handles);
836else
837    [RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes_1(handles);
838end
839FileName=[fullfile(RootPath,SubDir,RootFile) FileIndices FileExt];
840FileBase=fullfile(RootPath,RootFile);
841if ~exist(FileName,'file')
842   msgbox_uvmat('ERROR',['input file ' FileName ' not found']);
843    return
844end
845
846%% read timing and total frame number from the current file (movie files) !! may be overrid by xml file
847XmlData.Time=[];%default
848XmlData.GeometryCalib=[];%default
849TimeUnit='';%default
850Time=[];
851% testima=0; %test for image input
852imainfo=[];
853ColorType='falsecolor'; %default
854% hhh=[];
855UvData.MovieObject{index}=VideoObject;
856if ~isempty(VideoObject)
857    imainfo=get(VideoObject);
858%     testima=1;
859    TimeUnit='s';
860    if isempty(j1_series); %frame index along i
861        Time=(0:1/imainfo.FrameRate:(imainfo.NumberOfFrames)/imainfo.FrameRate)';
862    else
863        Time=ones(size(i1_series,1),1)*(0:1/imainfo.FrameRate:(imainfo.NumberOfFrames)/imainfo.FrameRate);
864    end
865    set(handles.Dt_txt,'String',['Dt=' num2str(1000/imainfo.FrameRate) 'ms']);%display the elementary time interval in millisec
866    ColorType='truecolor';
867elseif ~isempty(FileExt(2:end))&&(~isempty(imformats(FileExt(2:end))) || isequal(FileExt,'.vol'))%&& isequal(NomType,'*')% multi-frame image
868%     testima=1;
869    if ~isequal(SubDir,'')
870        imainfo=imfinfo([fullfile(RootPath,SubDir,RootFile) FileIndices FileExt]);
871    else
872        imainfo=imfinfo([FileBase FileIndices FileExt]);
873    end
874    ColorType=imainfo.ColorType;%='truecolor' for color images
875end
876if isfield(imainfo,'Width') && isfield(imainfo,'Height')
877    if length(imainfo)>1
878        set(handles.num_Npx,'String',num2str(imainfo(1).Width));%fills nbre of pixels x box
879        set(handles.num_Npy,'String',num2str(imainfo(1).Height));%fills nbre of pixels x box
880    else
881        set(handles.num_Npx,'String',num2str(imainfo.Width));%fills nbre of pixels x box
882        set(handles.num_Npy,'String',num2str(imainfo.Height));%fills nbre of pixels x box
883    end
884else
885    set(handles.num_Npx,'String','');%fills nbre of pixels x box
886    set(handles.num_Npy,'String','');%fills nbre of pixels x box
887end
888set(handles.CheckBW,'Value',strcmp(ColorType,'grayscale'))% select handles.CheckBW if grayscale image
889
890%% read parameters (time, geometric calibration..) from a documentation file (.xml advised)
891XmlFileName=find_imadoc(RootPath,SubDir,RootFile,FileExt);
892[tild,tild,DocExt]=fileparts(XmlFileName);
893warntext='';%default warning message
894NbSlice=1;%default
895set(handles.RootPath,'BackgroundColor',[1 1 1])
896if ~isempty(XmlFileName)
897    set(handles.view_xml,'Visible','on')
898    set(handles.view_xml,'BackgroundColor',[1 1 0])
899    set(handles.view_xml,'String','view .xml')
900    drawnow
901    [XmlDataRead,warntext]=imadoc2struct(XmlFileName);
902    if ~isempty(warntext)
903        msgbox_uvmat('WARNING',warntext)
904    end
905    if isempty(XmlDataRead)
906        set(handles.view_xml,'Visible','off')
907    else
908        set(handles.view_xml,'String',['view ' DocExt])   
909        XmlData=XmlDataRead;
910        if isfield(XmlData,'TimeUnit')
911            if isfield(XmlData,'TimeUnit')&& ~isempty(XmlData.TimeUnit)
912                TimeUnit=XmlData.TimeUnit;
913            end
914        end
915        set(handles.view_xml,'BackgroundColor',[1 1 1])
916        drawnow
917        if isfield(XmlData, 'GeometryCalib') && ~isempty(XmlData.GeometryCalib)
918            if isfield(XmlData.GeometryCalib,'VolumeScan') && isequal(XmlData.GeometryCalib.VolumeScan,'y')
919                set (handles.num_NbSlice,'String','volume')
920            end
921            hgeometry_calib=findobj('tag','geometry_calib');
922            if ~isempty(hgeometry_calib)
923                GUserData=get(hgeometry_calib,'UserData');
924                if ~(isfield(GUserData,'XmlInputFile') && strcmp(GUserData.XmlInputFile,XmlFileName))
925                    answer=msgbox_uvmat('INPUT_Y-N','replace the display of geometry_calib with the new input data?');
926                    if strcmp(answer,'Yes')
927                        geometry_calib(XmlFileName);%diplay the new calibration points and parameters in geometry_calib
928                    end
929                end
930            end
931        end
932    end
933end
934if ~(isfield(XmlData,'Time')&& ~isempty(XmlData.Time))
935    XmlData.Time=Time; %time set by video
936end
937
938%% store last index in handles.lat_i and .last_j
939nbfield=max(max(max(i2_series)));
940if isempty(nbfield)
941    nbfield=max(max(max(i1_series)));
942end
943nbfield_j=max(max(max(j2_series)));
944if isempty(nbfield_j)
945    nbfield_j=max(max(max(j1_series)));
946end
947if ~isempty(XmlData.Time)
948    %transform .Time to a column vector if it is a line vector the nomenclature uses a single index
949    if isequal(size(XmlData.Time,1),1)
950        %NomType=get(handles.NomType,'String');
951        XmlData.Time=(XmlData.Time)';
952    end
953end
954last_i_cell=get(handles.last_i,'String');
955if isempty(nbfield)
956    last_i_cell{index}='';
957else
958    last_i_cell{index}=num2str(nbfield);
959end
960set(handles.last_i,'String',last_i_cell)
961last_j_cell=get(handles.last_j,'String');
962if isempty(nbfield_j)
963     last_j_cell{index}='';
964else
965     last_j_cell{index}=num2str(nbfield_j);
966end
967set(handles.last_j,'String',last_j_cell);
968
969%% store geometric calibration in UvData
970if isfield(XmlData,'GeometryCalib')
971    GeometryCalib=XmlData.GeometryCalib;
972    if isempty(GeometryCalib)
973        set(handles.pxcm,'String','')
974        set(handles.pycm,'String','')
975        set(handles.transform_fct,'Value',1); %  no transform by default
976    else
977        if (isfield(GeometryCalib,'R')&& ~isequal(GeometryCalib.R(2,1),0) && ~isequal(GeometryCalib.R(1,2),0)) ||...
978            (isfield(GeometryCalib,'kappa1')&& ~isequal(GeometryCalib.kappa1,0))
979            set(handles.pxcm,'String','var')
980            set(handles.pycm,'String','var')
981        elseif isfield(GeometryCalib,'fx_fy')
982            pixcmx=GeometryCalib.fx_fy(1);%*GeometryCalib.R(1,1)*GeometryCalib.sx/(GeometryCalib.Tz*GeometryCalib.dpx);
983            pixcmy=GeometryCalib.fx_fy(2);%*GeometryCalib.R(2,2)/(GeometryCalib.Tz*GeometryCalib.dpy);
984            set(handles.pxcm,'String',num2str(pixcmx))
985            set(handles.pycm,'String',num2str(pixcmy))
986        end
987        if ~get(handles.CheckFixLimits,'Value')
988            set(handles.transform_fct,'Value',3); % phys transform by default if fixedLimits is off
989        end
990        if isfield(GeometryCalib,'SliceCoord')           
991           siz=size(GeometryCalib.SliceCoord);
992           if siz(1)>1
993               NbSlice=siz(1);
994               set(handles.slices,'Visible','on')
995               set(handles.slices,'Value',1)
996           end
997           if isfield(GeometryCalib,'VolumeScan') && isequal(GeometryCalib.VolumeScan,'y')
998               set(handles.num_NbSlice,'String','volume')
999           else
1000               set(handles.num_NbSlice,'String',num2str(NbSlice))
1001           end
1002           slices_Callback([],[], handles)
1003        end           
1004    end
1005end
1006
1007%% update the data attached to the uvmat interface
1008if ~isempty(TimeUnit)
1009    set(handles.time_txt,'String',['time (' TimeUnit ')'])
1010end
1011UvData.TimeUnit=TimeUnit;
1012UvData.XmlData{index}=XmlData;
1013UvData.NewSeries=1;
1014% UvData.MovieObject=MovieObject;
1015
1016%display warning message
1017if ~isequal(warntext,'')
1018    msgbox_uvmat('WARNING',warntext);
1019end
1020
1021%% set default options in menu 'FieldName'
1022switch FileType
1023    case {'civx','civdata'}
1024        [FieldList,ColorList]=calc_field;
1025        set(handles_Fields,'String',[{'image'};FieldList;{'get_field...'}]);%standard menu for civx data
1026        set(handles_Fields,'Value',2) % set menu to 'velocity
1027        if index==1
1028            set(handles.FieldName_1,'Value',1);
1029            set(handles.FieldName_1,'String',[{''};{'image'};FieldList;{'get_field...'}]);%standard menu for civx data reproduced for the second field
1030        end
1031        set(handles.ColorScalar,'Value',1)
1032        set(handles.ColorScalar,'String',ColorList)
1033        set(handles.Vectors,'Visible','on')
1034        set(handles.Coord_x,'Value',1);
1035        set(handles.Coord_x,'String',{'X'});
1036        set(handles.Coord_y,'Value',1);
1037        set(handles.Coord_y,'String',{'Y'});
1038    case 'netcdf'
1039        set(handles_Fields,'Value',1)
1040        set(handles_Fields,'String',{'get_field...'})
1041        hget_field=get_field(FileName);
1042        hhget_field=guidata(hget_field);
1043        get_field('RUN_Callback',hhget_field.RUN,[],hhget_field);
1044    otherwise
1045        set(handles_Fields,'Value',1) % set menu to 'image'
1046        set(handles_Fields,'String',{'image'})
1047        set(handles.Coord_x,'Value',1);
1048        set(handles.Coord_x,'String',{'AX'});
1049        set(handles.Coord_y,'Value',1);
1050        set(handles.Coord_y,'String',{'AY'});
1051end
1052set(handles.uvmat,'UserData',UvData)
1053
1054%% set index navigation options and refresh plots
1055scan_option='i';%default
1056state_j='off'; %default
1057if index==2
1058    if get(handles.scan_j,'Value')
1059        scan_option='j'; %keep the scan option for the second file series
1060    end
1061    if strcmp(get(handles.j1,'Visible'),'on')
1062        state_j='on';
1063    end
1064end
1065[ref_j,ref_i]=find(squeeze(i1_series(1,:,:)));
1066if ~isempty(j1_series)
1067        state_j='on';
1068        if index==1
1069            if isequal(ref_i,ref_i(1)*ones(size(ref_j)))% if ref_i is always equal to its first value
1070                scan_option='j'; %scan j indext               
1071            end
1072        end
1073end
1074if isequal(scan_option,'i')
1075    diff_ref_i=diff(ref_i,1);
1076    if isempty(diff_ref_i)
1077        diff_ref_i=1;
1078    end
1079    if isequal (diff_ref_i,diff_ref_i(1)*ones(size(diff_ref_i)))
1080        set(handles.num_IndexIncrement,'String',num2str(diff_ref_i(1)))
1081    end
1082     set(handles.scan_i,'Value',1)
1083     scan_i_Callback([],[], handles);
1084else
1085    diff_ref_j=diff(ref_j);
1086    if isempty(diff_ref_j)
1087        diff_ref_j=1;
1088    end
1089    if isequal (diff_ref_j,diff_ref_j(1)*ones(size(diff_ref_j)))
1090        set(handles.num_IndexIncrement,'String',num2str(diff_ref_j(1)))
1091    end
1092     set(handles.scan_j,'Value',1)
1093     scan_j_Callback([],[], handles);
1094end
1095set(handles.scan_j,'Visible',state_j)
1096set(handles.j1,'Visible',state_j)
1097set(handles.j2,'Visible',state_j)
1098set(handles.last_j,'Visible',state_j);
1099set(handles.frame_j,'Visible',state_j);
1100set(handles.j_text,'Visible',state_j);
1101if ~isempty(i2_series)||~isempty(j2_series)
1102    set(handles.CheckFixPair,'Visible','on')
1103elseif index==1
1104    set(handles.CheckFixPair,'Visible','off')
1105end
1106
1107%% apply the effect of the transform fct and view the field 
1108transform=get(handles.path_transform,'UserData');
1109if index==2 && (~isa(transform,'function_handle')||nargin(transform)<3)
1110    set(handles.transform_fct,'value',2); % set transform to sub_field if the current fct doe not accept two input fields
1111end
1112transform_fct_Callback([],[],handles)
1113mask_test=get(handles.CheckMask,'value');
1114if mask_test
1115    MaskData=get(handles.CheckMask,'UserData');
1116    if isfield(MaskData,'maskhandle') && ishandle(MaskData.maskhandle)
1117          delete(MaskData.maskhandle)    %delete old mask
1118    end
1119    CheckMask_Callback([],[],handles)
1120end
1121
1122%------------------------------------------------------------------------
1123% --- switch file index scanning options scan_i and scan_j in an exclusive way
1124function scan_i_Callback(hObject, eventdata, handles)
1125%------------------------------------------------------------------------
1126if get(handles.scan_i,'Value')==1
1127    set(handles.scan_i,'BackgroundColor',[1 1 0])
1128    set(handles.scan_j,'Value',0)
1129else
1130    set(handles.scan_i,'BackgroundColor',[0.831 0.816 0.784])
1131    set(handles.scan_j,'Value',1)
1132end
1133scan_j_Callback(hObject, eventdata, handles)
1134
1135%------------------------------------------------------------------------
1136% --- switch file index scanning options scan_i and scan_j in an exclusive way
1137function scan_j_Callback(hObject, eventdata, handles)
1138%------------------------------------------------------------------------
1139if get(handles.scan_j,'Value')==1
1140    set(handles.scan_j,'BackgroundColor',[1 1 0])
1141    set(handles.scan_i,'Value',0)
1142    set(handles.scan_i,'BackgroundColor',[0.831 0.816 0.784])
1143%     NomType=get(handles.NomType,'String');
1144%     switch NomType
1145%     case {'_1_1-2','#_ab','%3dab'},% pair with j index
1146%         set(handles.CheckFixPair,'Visible','on')% option fixed pair on/off made visible (choice of avaible pair with buttons + and - if ='off')
1147%     otherwise
1148%         set(handles.CheckFixPair,'Visible','off')
1149%     end
1150else
1151    set(handles.scan_j,'BackgroundColor',[0.831 0.816 0.784])
1152    set(handles.scan_i,'Value',1)
1153    set(handles.scan_i,'BackgroundColor',[1 1 0])
1154    set(handles.CheckFixPair,'Visible','off')
1155end
1156
1157%------------------------------------------------------------------------
1158function i1_Callback(hObject, eventdata, handles)
1159%------------------------------------------------------------------------
1160update_ij(handles,1)
1161
1162%------------------------------------------------------------------------
1163function i2_Callback(hObject, eventdata, handles)
1164%------------------------------------------------------------------------
1165update_ij(handles,2)
1166
1167%------------------------------------------------------------------------
1168function j1_Callback(hObject, eventdata, handles)
1169%------------------------------------------------------------------------
1170update_ij(handles,3)
1171
1172%------------------------------------------------------------------------
1173function j2_Callback(hObject, eventdata, handles)
1174%------------------------------------------------------------------------
1175update_ij(handles,4)
1176
1177%------------------------------------------------------------------------
1178%--- update the index display after action on edit boxes i1, i2, j1 or j2
1179function update_ij(handles,index_rank)
1180NomType=get(handles.NomType,'String');
1181indices=get(handles.FileIndex,'String');
1182[tild,tild,tild,i1,i2,j1,j2]=fileparts_uvmat(indices);% the indices for the second series taken from FileIndex
1183switch index_rank
1184    case 1
1185        indices=fullfile_uvmat('','','','',NomType,stra2num(get(handles.i1,'String')),i2,j1,j2);
1186        set(handles.i1,'BackgroundColor',[0.7 0.7 0.7])% mark the edit box in grey, then RUN0 will mark it in white for confirmation
1187    case 2
1188        indices=fullfile_uvmat('','','','',NomType,i1,stra2num(get(handles.i2,'String')),j1,j2);
1189        set(handles.i2,'BackgroundColor',[0.7 0.7 0.7])% mark the edit box in grey, then RUN0 will mark it in white for confirmation
1190    case 3
1191        indices=fullfile_uvmat('','','','',NomType,i1,i2,stra2num(get(handles.j1,'String')),j2);
1192        set(handles.j1,'BackgroundColor',[0.7 0.7 0.7])% mark the edit box in grey, then RUN0 will mark it in white for confirmation
1193    case 4
1194        indices=fullfile_uvmat('','','','',NomType,i1,i2,j1,stra2num(get(handles.j2,'String')));
1195        set(handles.j2,'BackgroundColor',[0.7 0.7 0.7])% mark the edit box in grey, then RUN0 will mark it in white for confirmation
1196end
1197set(handles.FileIndex,'String',indices)
1198set(handles.FileIndex,'BackgroundColor',[0.7 0.7 0.7])% mark the edit box in grey, then RUN0 will mark it in white for confirmation
1199% update the second index if relevant
1200if strcmp(get(handles.FileIndex_1,'Visible'),'on')
1201    NomType_1=get(handles.NomType_1,'String');
1202    indices_1=get(handles.FileIndex_1,'String');
1203    [tild,tild,tild,i1_1,i2_1,j1_1,j2_1]=fileparts_uvmat(indices_1);% the indices for the second series taken from FileIndex_1
1204    switch index_rank
1205        case 1
1206            indices_1=fullfile_uvmat('','','','',NomType_1,stra2num(get(handles.i1,'String')),i2_1,j1_1,j2_1);
1207        case 2
1208            indices_1=fullfile_uvmat('','','','',NomType_1,i1_1,stra2num(get(handles.i2,'String')),j1_1,j2_1);
1209        case 3
1210            indices_1=fullfile_uvmat('','','','',NomType_1,i1_1,i2_1,stra2num(get(handles.j1,'String')),j2_1);
1211        case 4
1212            indices_1=fullfile_uvmat('','','','',NomType_1,i1_1,i2_1,j1_1,stra2num(get(handles.j2,'String')));
1213    end
1214    set(handles.FileIndex_1,'String',indices_1)
1215    set(handles.FileIndex_1,'BackgroundColor',[0.7 0.7 0.7])% mark the edit box in grey, then RUN0 will mark it in white for confirmation
1216end
1217   
1218%------------------------------------------------------------------------
1219
1220%------------------------------------------------------------------------
1221function slices_Callback(hObject, eventdata, handles)
1222%------------------------------------------------------------------------
1223if get(handles.slices,'Value')==1
1224    set(handles.slices,'BackgroundColor',[1 1 0])
1225    set(handles.num_NbSlice,'Visible','on')
1226    set(handles.z_text,'Visible','on')
1227    set(handles.z_index,'Visible','on')
1228    num_NbSlice_Callback(hObject, eventdata, handles)
1229else
1230    set(handles.num_NbSlice,'Visible','off')
1231    set(handles.slices,'BackgroundColor',[0.7 0.7 0.7])
1232    set(handles.z_text,'Visible','off')
1233    set(handles.z_index,'Visible','off')
1234    set(handles.masklevel,'Value',1)
1235    set(handles.masklevel,'String',{'1'})
1236end
1237
1238%------------------------------------------------------------------------
1239function num_NbSlice_Callback(hObject, eventdata, handles)
1240%------------------------------------------------------------------------
1241nb_slice_str=get(handles.num_NbSlice,'String');
1242if isequal(nb_slice_str,'volume')
1243    num=stra2num(get(handles.j1,'String'));
1244    last_j=get(handles.last_j,'String');
1245    nbslice=str2double(last_j{1});
1246else
1247    num=str2double(get(handles.i1,'String'));
1248    nbslice=str2double(get(handles.num_NbSlice,'String'));
1249end
1250z=mod(num-1,nbslice)+1;
1251set(handles.z_index,'String',num2str(z))
1252for ilist=1:nbslice
1253    list_index{ilist,1}=num2str(ilist);
1254end   
1255set(handles.masklevel,'String',list_index)
1256set(handles.masklevel,'Value',z)
1257
1258%------------------------------------------------------------------------
1259% --- Executes on button press in view_xml.
1260function view_xml_Callback(hObject, eventdata, handles)
1261%------------------------------------------------------------------------
1262%[FileName,RootPath,FileBase,FileIndices,FileExt]=read_file_boxes(handles);
1263 [RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes(handles);
1264FileBase=fullfile(RootPath,RootFile);
1265option=get(handles.view_xml,'String');
1266if isequal(option,'view .xml')
1267    FileXml=fullfile(RootPath,[SubDir '.xml']);
1268    if ~exist(FileXml,'file')% case of civ files , removes the extension for subdir
1269        FileXml=fullfile(RootPath,[regexprep(SubDir,'\..+$','') '.xml']);
1270    end
1271    heditxml=editxml(FileXml);
1272end
1273
1274%------------------------------------------------------------------------
1275% --- Executes on button press in CheckMask.
1276function CheckMask_Callback(hObject, eventdata, handles)
1277%------------------------------------------------------------------------
1278%case of view mask selection
1279if isequal(get(handles.CheckMask,'Value'),1)
1280     [RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes(handles);
1281     FileBase=fullfile(RootPath,RootFile);
1282    num_i1=stra2num(get(handles.i1,'String'));
1283    num_j1=stra2num(get(handles.j1,'String'));
1284    currentdir=pwd; 
1285    cd(RootPath);
1286    maskfiles=dir('*_*mask_*.png');%look for a mask file
1287    cd(currentdir);%come back to the working directory
1288    mdetect=0;
1289    if ~isempty(maskfiles)
1290        for ilist=1:length(maskfiles)
1291            maskname=maskfiles(ilist).name;% take the first mask file in the list
1292            [tild,tild,tild,tild,tild,tild,tild,MaskExt,Mask_NomType{ilist}]=fileparts_uvmat(maskname);
1293             [tild,Name]=fileparts(maskname);
1294            Namedouble=double(Name);
1295            val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
1296            ind_mask=findstr('mask',Name);
1297            i=ind_mask-1;
1298            while val(i)==0 && i>0
1299                i=i-1;
1300            end
1301            nbmask_str=str2num(Name(i+1:ind_mask-1));
1302            if ~isempty(nbmask_str)
1303                nbslice(ilist)=nbmask_str; % number of different masks (slices)
1304            end
1305        end
1306        if isequal(min(nbslice),max(nbslice))
1307            nbslice=nbslice(1);
1308        else
1309            msgbox_uvmat('ERROR','several inconsistent mask sets coexist in the current image directory')
1310            return
1311        end
1312        if ~isempty(nbslice) && Name(i)=='_'
1313            Mask.Base=[FileBase Name(i:ind_mask+3)];
1314            Mask.NbSlice=nbslice;
1315            num_i1=mod(num_i1-1,nbslice)+1;
1316            Mask.NomType=regexprep(Mask_NomType{1},'0','');%remove '0' in nom type for masks
1317            [RootPath,RootFile]=fileparts(Mask.Base);
1318            maskname=fullfile_uvmat(RootPath,'',RootFile,'.png',Mask.NomType,num_i1,[],num_j1);
1319            %maskname=name_generator(Mask.Base,num_i1,num_j1,'.png',Mask.NomType);%
1320            mdetect=exist(maskname,'file');
1321            if mdetect
1322                set(handles.num_NbSlice,'String',Name(i+1:ind_mask-1));
1323                set(handles.num_NbSlice,'BackgroundColor',[1 1 0])
1324                set(handles.CheckMask,'UserData',Mask);
1325                set(handles.CheckMask,'BackgroundColor',[1 1 0])
1326                if nbslice > 1
1327                    set(handles.slices,'value',1)
1328                    slices_Callback(hObject, eventdata, handles)
1329                end
1330            end
1331        end
1332    end
1333    errormsg=[];%default
1334    if mdetect==0
1335        [FileName, PathName, filterindex] = uigetfile( ...
1336            {'*.png', ' (*.png)';
1337            '*.png',  '.png files '; ...
1338            '*.*', 'All Files (*.*)'}, ...
1339            'Pick a mask file *.png',FileBase);
1340        if ~ischar(FileName),return,end %abandon if the browser is cancelled
1341        maskname=fullfile(PathName,FileName);
1342        [RootDir,SubDir,RootFile,tild,tild,tild,tild,tild,Mask.NomType]=fileparts_uvmat(maskname);
1343        Mask.Base=fullfile(RootDir,SubDir,RootFile);
1344        Mask.NbSlice=1;
1345        set(handles.CheckMask,'UserData',Mask);
1346        set(handles.CheckMask,'BackgroundColor',[1 1 0])
1347    end
1348    if isempty(errormsg)
1349        errormsg=update_mask(handles,num_i1,num_j1);
1350    end
1351    if ~isempty(errormsg)
1352            set(handles.CheckMask,'Value',0)
1353            set(handles.CheckMask,'BackgroundColor',[0.7 0.7 0.7])
1354     end
1355else % desactivate mask display
1356    MaskData=get(handles.CheckMask,'UserData');
1357    if isfield(MaskData,'maskhandle') && ishandle(MaskData.maskhandle)
1358          delete(MaskData.maskhandle)   
1359    end
1360    set(handles.CheckMask,'UserData',[])   
1361    UvData=get(handles.uvmat,'UserData');
1362    if isfield(UvData,'MaskName')
1363        UvData=rmfield(UvData,'MaskName');
1364        set(handles.uvmat,'UserData',UvData)
1365    end
1366    set(handles.CheckMask,'BackgroundColor',[0.7 0.7 0.7])
1367end
1368
1369%------------------------------------------------------------------------
1370function errormsg=update_mask(handles,num_i1,num_j1)
1371%------------------------------------------------------------------------
1372errormsg=[];%default
1373MaskData=get(handles.CheckMask,'UserData');
1374if isfield(MaskData,'maskhandle')&& ishandle(MaskData.maskhandle)
1375    uistack(MaskData.maskhandle,'top');
1376end
1377num_i1_mask=mod(num_i1-1,MaskData.NbSlice)+1;
1378[RootPath,RootFile]=fileparts(MaskData.Base);
1379MaskName=fullfile_uvmat(RootPath,'',RootFile,'.png',MaskData.NomType,num_i1_mask,[],num_j1);
1380UvData=get(handles.uvmat,'UserData');
1381%update mask image if the mask is new
1382if ~ (isfield(UvData,'MaskName') && isequal(UvData.MaskName,MaskName))
1383    UvData.MaskName=MaskName; %update the recorded name on UvData
1384    set(handles.uvmat,'UserData',UvData);
1385    if ~exist(MaskName,'file')
1386        if isfield(MaskData,'maskhandle')&& ishandle(MaskData.maskhandle)
1387            delete(MaskData.maskhandle)   
1388        end
1389    else
1390        %read mask image
1391        [Mask,tild,errormsg] = read_field(MaskName,'image');
1392%         Mask.AName='image';
1393%         Mask.A=imread(MaskName);
1394        if ~isempty(errormsg)
1395            return
1396        end
1397        npxy=size(Mask.A);
1398        if length(npxy)>2
1399            errormsg=[MaskName ' is not a grey scale image'];
1400            return
1401        elseif ~isa(Mask.A,'uint8')
1402            errormsg=[MaskName ' is not a 8 bit grey level image'];
1403            return
1404        end
1405%         Mask.AX=[0.5 npxy(2)-0.5];
1406%         Mask.AY=[npxy(1)-0.5 0.5 ];
1407%         Mask.CoordUnit='pixel';
1408        if isequal(get(handles.slices,'Value'),1)
1409           NbSlice=str2num(get(handles.num_NbSlice,'String'));
1410           num_i1=str2num(get(handles.i1,'String'));
1411           Mask.ZIndex=mod(num_i1-1,NbSlice)+1;
1412        end
1413        %px to phys or other transform on field
1414         menu_transform=get(handles.transform_fct,'String');
1415        choice_value=get(handles.transform_fct,'Value');
1416        transform_name=menu_transform{choice_value};%name of the transform fct  given by the menu 'transform_fct'
1417        transform=get(handles.path_transform,'UserData');
1418        if  ~isequal(transform_name,'') && ~isequal(transform_name,'px')
1419            if isfield(UvData,'XmlData') && isfield(UvData.XmlData{1},'GeometryCalib')%use geometry calib recorded from the ImaDoc xml file as first priority
1420                Calib=UvData.XmlData{1}.GeometryCalib;
1421                Mask=transform(Mask,UvData.XmlData{1});
1422            end
1423        end
1424        flagmask=Mask.A < 200;
1425       
1426        %make brown color image
1427        imflag(:,:,1)=0.9*flagmask;
1428        imflag(:,:,2)=0.7*flagmask;
1429        imflag(:,:,3)=zeros(size(flagmask));
1430       
1431        %update mask image
1432        hmask=[]; %default
1433        if isfield(MaskData,'maskhandle')&& ishandle(MaskData.maskhandle)
1434            hmask=MaskData.maskhandle;
1435        end
1436        if ~isempty(hmask)
1437            set(hmask,'CData',imflag)   
1438            set(hmask,'AlphaData',flagmask*0.6)
1439            set(hmask,'XData',Mask.AX);
1440            set(hmask,'YData',Mask.AY);
1441%             uistack(hmask,'top')
1442        else
1443            axes(handles.PlotAxes)
1444            hold on   
1445            MaskData.maskhandle=image(Mask.AX,Mask.AY,imflag,'Tag','mask','HitTest','off','AlphaData',0.6*ones(size(flagmask)));
1446            set(handles.CheckMask,'UserData',MaskData)
1447        end
1448    end
1449end
1450
1451%------------------------------------------------------------------------
1452%------------------------------------------------------------------------
1453% III - MAIN REFRESH FUNCTIONS : 'FRAME PLOT'
1454%------------------------------------------------------------------------
1455
1456%------------------------------------------------------------------------
1457% --- Executes on button press in runplus: make one step forward and call
1458% --- run0. The step forward is along the fieldname series 1 or 2 depending on
1459% --- the scan_i and scan_j check box (exclusive each other)
1460function runplus_Callback(hObject, eventdata, handles)
1461%------------------------------------------------------------------------
1462set(handles.runplus,'BackgroundColor',[1 1 0])%paint the command button in yellow
1463drawnow
1464%TODO: introduce the option: increment ='*' to move to the next available view
1465increment=str2num(get(handles.num_IndexIncrement,'String')); %get the field increment d
1466% if isnan(increment)
1467%     set(handles.num_IndexIncrement,'String','1')%default value
1468%     increment=1;
1469% end
1470errormsg=runpm(hObject,eventdata,handles,increment);
1471if ~isempty(errormsg)
1472    msgbox_uvmat('ERROR',errormsg);
1473end
1474set(handles.runplus,'BackgroundColor',[1 0 0])%paint the command button back to red
1475
1476%------------------------------------------------------------------------
1477% --- Executes on button press in runmin: make one step backward and call
1478% --- run0. The step backward is along the fieldname series 1 or 2 depending on
1479% --- the scan_i and scan_j check box (exclusive each other)
1480function runmin_Callback(hObject, eventdata, handles)
1481%------------------------------------------------------------------------
1482set(handles.runmin,'BackgroundColor',[1 1 0])%paint the command button in yellow
1483drawnow
1484increment=-str2num(get(handles.num_IndexIncrement,'String')); %get the field increment d
1485% if isnan(increment)
1486%     set(handles.num_IndexIncrement,'String','1')%default value
1487%     increment=1;
1488% end
1489errormsg=runpm(hObject,eventdata,handles,increment);
1490if ~isempty(errormsg)
1491    msgbox_uvmat('ERROR',errormsg);
1492end
1493set(handles.runmin,'BackgroundColor',[1 0 0])%paint the command button back to red
1494
1495%------------------------------------------------------------------------
1496% -- Executes on button press in Movie: make a series of +> steps
1497function Movie_Callback(hObject, eventdata, handles)
1498%------------------------------------------------------------------------
1499set(handles.Movie,'BackgroundColor',[1 1 0])%paint the command button in yellow
1500drawnow
1501increment=str2num(get(handles.num_IndexIncrement,'String')); %get the field increment d
1502% if isnan(increment)
1503%     set(handles.num_IndexIncrement,'String','1')%default value
1504%     increment=1;
1505% end
1506set(handles.STOP,'Visible','on')
1507set(handles.speed,'Visible','on')
1508set(handles.speed_txt,'Visible','on')
1509set(handles.Movie,'BusyAction','queue')
1510UvData=get(handles.uvmat,'UserData');
1511
1512while get(handles.Movie,'Value')==1 && get(handles.speed,'Value')~=0 && isequal(get(handles.Movie,'BusyAction'),'queue') % enable STOP command
1513        errormsg=runpm(hObject,eventdata,handles,increment);
1514        if ~isempty(errormsg)
1515            set(handles.Movie,'BackgroundColor',[1 0 0])%paint the command buttonback to red
1516            return
1517        end
1518        pause(1.02-get(handles.speed,'Value'))% wait for next image
1519end
1520if isfield(UvData,'aviobj') && ~isempty( UvData.aviobj),
1521    UvData.aviobj=close(UvData.aviobj);
1522   set(handles.uvmat,'UserData',UvData);
1523end
1524set(handles.Movie,'BackgroundColor',[1 0 0])%paint the command buttonback to red
1525
1526%------------------------------------------------------------------------
1527% -- Executes on button press in Movie: make a series of <- steps
1528function MovieBackward_Callback(hObject, eventdata, handles)
1529%------------------------------------------------------------------------
1530set(handles.MovieBackward,'BackgroundColor',[1 1 0])%paint the command button in yellow
1531drawnow
1532increment=-str2num(get(handles.num_IndexIncrement,'String')); %get the field increment d
1533set(handles.STOP,'Visible','on')
1534set(handles.speed,'Visible','on')
1535set(handles.speed_txt,'Visible','on')
1536set(handles.MovieBackward,'BusyAction','queue')
1537UvData=get(handles.uvmat,'UserData');
1538
1539while get(handles.MovieBackward,'Value')==1 && get(handles.speed,'Value')~=0 && isequal(get(handles.MovieBackward,'BusyAction'),'queue') % enable STOP command
1540        errormsg=runpm(hObject,eventdata,handles,increment);
1541        if ~isempty(errormsg)
1542            set(handles.MovieBackward,'BackgroundColor',[1 0 0])%paint the command buttonback to red
1543            return
1544        end
1545        pause(1.02-get(handles.speed,'Value'))% wait for next image
1546end
1547if isfield(UvData,'aviobj') && ~isempty( UvData.aviobj),
1548    UvData.aviobj=close(UvData.aviobj);
1549   set(handles.uvmat,'UserData',UvData);
1550end
1551set(handles.MovieBackward,'BackgroundColor',[1 0 0])%paint the command buttonback to red
1552
1553%------------------------------------------------------------------------
1554function STOP_Callback(hObject, eventdata, handles)
1555%------------------------------------------------------------------------
1556set(handles.movie_pair,'BusyAction','Cancel')
1557set(handles.movie_pair,'value',0)
1558set(handles.Movie,'BusyAction','Cancel')
1559set(handles.MovieBackward,'BusyAction','Cancel')
1560set(handles.MenuExportMovie,'BusyAction','Cancel')
1561set(handles.movie_pair,'BackgroundColor',[1 0 0])%paint the command buttonback to red
1562set(handles.Movie,'BackgroundColor',[1 0 0])%paint the command buttonback to red
1563set(handles.MovieBackward,'BackgroundColor',[1 0 0])%paint the command buttonback to red
1564
1565%------------------------------------------------------------------------
1566% --- function activated by runplus and run minus
1567function errormsg=runpm(hObject,eventdata,handles,increment)
1568%------------------------------------------------------------------------
1569errormsg='';%default
1570%% check for movie pair status
1571movie_status=get(handles.movie_pair,'Value');
1572if isequal(movie_status,1)
1573    STOP_Callback(hObject, eventdata, handles)%interrupt movie pair if active
1574end
1575
1576%% read the current input file name(s) and field indices
1577InputFile=read_GUI(handles.InputFile);
1578InputFile.RootFile=regexprep(InputFile.RootFile,'^[\\/]|[\\/]$','');%suppress possible / or \ separator at the beginning or the end of the string
1579InputFile.SubDir=regexprep(InputFile.SubDir,'^[\\/]|[\\/]$','');%suppress possible / or \ separator at the beginning or the end of the string
1580FileExt=InputFile.FileExt;
1581NomType=get(handles.NomType,'String');
1582i1=str2num(get(handles.i1,'String'));%read the field indices (for movie, it is not given by the file name)
1583i2=[];%default
1584if strcmp(get(handles.i2,'Visible'),'on')
1585    i2=str2num(get(handles.i2,'String'));
1586end
1587j1=[];
1588if strcmp(get(handles.j1,'Visible'),'on')
1589    j1=stra2num(get(handles.j1,'String'));
1590end
1591j2=j1;
1592if strcmp(get(handles.j2,'Visible'),'on')
1593    j2=stra2num(get(handles.j2,'String'));
1594end
1595sub_value= get(handles.SubField,'Value');
1596if sub_value % a second input file has been entered
1597     [InputFile.RootPath_1,InputFile.SubDir_1,InputFile.RootFile_1,InputFile.FileIndex_1,InputFile.FileExt_1,InputFile.NomType_1]=read_file_boxes_1(handles);   
1598    [tild,tild,tild,i1_1,i2_1,j1_1,j2_1]=fileparts_uvmat(InputFile.FileIndex_1);% the indices for the second series taken from FileIndex_1
1599else
1600    filename_1=[];
1601end   
1602
1603%% increment (or decrement) the field indices and update the input filename(s)
1604if isempty(increment)
1605    set(handles.CheckFixPair,'Value',0)
1606end
1607CheckFixPair=get(handles.CheckFixPair,'Value')||(isempty(i2)&&isempty(j2));
1608
1609% the pair i1-i2 or j1-j2 is imposed (check box CheckFixPair selected)
1610        i1_1=i1;
1611        i2_1=i2;
1612        j1_1=j1;
1613        j2_1=j2;
1614if CheckFixPair
1615    if get(handles.scan_i,'Value')==1% case of scanning along index i
1616        i1=i1+increment;
1617        i2=i2+increment;
1618        if sub_value
1619            i1_1=i1_1+increment;
1620            i2_1=i2_1+increment;
1621        end
1622       
1623    else % case of scanning along index j (burst numbers)
1624        j1=j1+increment;
1625        j2=j2+increment;
1626        if sub_value
1627            j1_1=j1_1+increment;
1628            j2_1=j2_1+increment;
1629        end
1630    end
1631   
1632% the pair i1-i2 or j1-j2 is free (check box CheckFixPair not selected): the list of existing indices recorded in UvData is used
1633else
1634    UvData=get(handles.uvmat,'UserData');
1635    ref_i=i1;
1636    if ~isempty(i2)
1637        ref_i=floor((i1+i2)/2);% current reference index i
1638    end
1639    ref_j=1;
1640    if ~isempty(j1)
1641        ref_j=j1;
1642        if ~isempty(j2)
1643            ref_j=floor((j1+j2)/2);% current reference index j
1644        end
1645    end
1646    if ~isempty(increment)
1647        if get(handles.scan_i,'Value')==1% case of scanning along index i
1648            ref_i=ref_i+increment;% increment the current reference index i
1649        else % case of scanning along index j (burst numbers)
1650            ref_j=ref_j+increment;% increment the current reference index j if scan_j option is used
1651        end
1652    else % free increment
1653        runaction=get(gcbo,'tag');
1654        if strcmp(runaction,'runplus')||strcmp(runaction,'Movie')% if runplus or movie is activated
1655            step=1;
1656        else
1657            step=-1;
1658        end
1659        if get(handles.scan_i,'Value')==1% case of scanning along index i
1660            ref_i=ref_i+step;
1661            while ref_i>=0  && size(UvData.i1_series{1},3)>=ref_i+1 && UvData.i1_series{1}(1,ref_j+1,ref_i+1)==0
1662                ref_i=ref_i+step;
1663            end
1664        else % case of scanning along index j (burst numbers)
1665            ref_j=ref_j+step;
1666            while ref_j>=0  && size(UvData.i1_series{1},2)>=ref_j+1 && UvData.i1_series{1}(1,ref_j+1,ref_i+1)==0
1667                ref_j=ref_j+step;
1668            end
1669        end
1670    end
1671    if ref_i<0
1672        errormsg='minimum i index reached';
1673    elseif ref_j<0
1674        errormsg='minimum j index reached';
1675    elseif ref_i+1>size(UvData.i1_series{1},3)
1676        errormsg='maximum i index reached (reload the input file to update the index bound)';
1677    elseif ref_j+1>size(UvData.i1_series{1},2)
1678        errormsg='maximum j index reached (reload the input file to update the index bound)';
1679    end
1680    if ~isempty(errormsg),return,end
1681    siz=size(UvData.i1_series{1});
1682    ref_indices=ref_i*siz(1)*siz(2)+ref_j*siz(1)+1:ref_i*siz(1)*siz(2)+(ref_j+1)*siz(1);
1683    i1_subseries=UvData.i1_series{1}(ref_indices);
1684    ref_indices=ref_indices(i1_subseries>0);
1685    if isempty(ref_indices)% case of pairs (free index i)
1686        ref_indices=ref_i*siz(1)*siz(2)+1:(ref_i+1)*siz(1)*siz(2);
1687        i1_subseries=UvData.i1_series{1}(ref_indices);
1688        ref_indices=ref_indices(i1_subseries>0);
1689    end
1690    if isempty(ref_indices),errormsg='no next frame: set num_IndexIncrement =''*'' to reach the next existing file';return
1691    end
1692    i1=UvData.i1_series{1}(ref_indices(end));
1693    if ~isempty(UvData.i2_series{1})
1694        i2=UvData.i2_series{1}(ref_indices(end));
1695    end
1696    if ~isempty(UvData.j1_series{1})
1697        j1=UvData.j1_series{1}(ref_indices(end));
1698    end
1699    if ~isempty(UvData.j2_series{1})
1700        j2=UvData.j2_series{1}(ref_indices(end));
1701    end
1702   
1703    % case of a second file series
1704    if sub_value
1705        ref_i_1=i1_1;
1706        if ~isempty(i2_1)
1707            ref_i_1=floor((i1_1+i2_1)/2);% current reference index i
1708        end
1709        ref_j_1=1;
1710        if ~isempty(j1_1)
1711            ref_j_1=j1_1;
1712            if ~isempty(j2_1)
1713                ref_j_1=floor((j1_1+j2_1)/2);% current reference index j
1714            end
1715        end
1716        if ~isempty(increment)
1717            if get(handles.scan_i,'Value')==1% case of scanning along index i
1718                ref_i_1=ref_i_1+increment;% increment the current reference index i
1719            else % case of scanning along index j (burst numbers)
1720                ref_j_1=ref_j_1+increment;% increment the current reference index j if scan_j option is used
1721            end
1722        else % free increment, synchronise the ref indices with the first series
1723            ref_i_1=ref_i;
1724            ref_j_1=ref_j;
1725        end
1726        if numel(UvData.i1_series)==1
1727            UvData.i1_series{2}=UvData.i1_series{1};
1728            UvData.j1_series{2}=UvData.j1_series{1};
1729            UvData.i2_series{2}=UvData.i2_series{1};
1730            UvData.j2_series{2}=UvData.j2_series{1};
1731        end
1732        if ref_i_1<0
1733            errormsg='minimum i index reached';
1734        elseif ref_j_1<0
1735            errormsg='minimum j index reached';
1736        elseif ref_i_1+1>size(UvData.i1_series{2},3)
1737            errormsg='maximum i index reached for the second series (reload the input file to update the index bound)';
1738        elseif ref_j_1+1>size(UvData.i1_series{2},2)
1739            errormsg='maximum j index reached for the second series(reload the input file to update the index bound)';
1740        end
1741        if ~isempty(errormsg),return,end
1742        siz=size(UvData.i1_series{2});
1743        ref_indices=ref_i_1*siz(1)*siz(2)+ref_j_1*siz(1)+1:ref_i_1*siz(1)*siz(2)+(ref_j_1+1)*siz(1);
1744        i1_subseries=UvData.i1_series{2}(ref_indices);
1745        ref_indices=ref_indices(i1_subseries>0);
1746        if isempty(ref_indices)% case of pairs (free index i)
1747            ref_indices=ref_i_1*siz(1)*siz(2)+1:(ref_i_1+1)*siz(1)*siz(2);
1748            i1_subseries=UvData.i1_series{2}(ref_indices);
1749            ref_indices=ref_indices(i1_subseries>0);
1750        end
1751        i1_1=UvData.i1_series{2}(ref_indices(end));
1752        if ~isempty(UvData.i2_series{2})
1753            i2_1=UvData.i2_series{2}(ref_indices(end));
1754        end
1755        if ~isempty(UvData.j1_series{2})
1756            j1_1=UvData.j1_series{2}(ref_indices(end));
1757        end
1758        if ~isempty(UvData.j2_series{2})
1759            j2_1=UvData.j2_series{1}(ref_indices(end));
1760        end   
1761    else% the second series (if needed) is the same file as the first
1762        i1_1=i1;
1763        i2_1=i2;
1764        j1_1=j1;
1765        j2_1=j2;
1766    end
1767end
1768filename=fullfile_uvmat(InputFile.RootPath,InputFile.SubDir,InputFile.RootFile,FileExt,NomType,i1,i2,j1,j2);
1769if sub_value
1770    filename_1=fullfile_uvmat(InputFile.RootPath_1,InputFile.SubDir_1,InputFile.RootFile_1,InputFile.FileExt_1,InputFile.NomType_1,i1_1,i2_1,j1_1,j2_1);
1771end
1772
1773%% refresh plots
1774errormsg=refresh_field(handles,filename,filename_1,i1,i2,j1,j2,i1_1,i2_1,j1_1,j2_1);
1775
1776%% update the index counters if the index move is successfull
1777if isempty(errormsg)
1778    set(handles.i1,'String',num2stra(i1,NomType,1));
1779    if isequal(i2,i1)
1780        set(handles.i2,'String','');
1781    else
1782        set(handles.i2,'String',num2stra(i2,NomType,1));
1783    end
1784    set(handles.j1,'String',num2stra(j1,NomType,2));
1785    if isequal(j2,j1)
1786        set(handles.j2,'String','');
1787    else
1788        set(handles.j2,'String',num2stra(j2,NomType,2));
1789    end
1790    indices=fullfile_uvmat('','','','',NomType,i1,i2,j1,j2);
1791    set(handles.FileIndex,'String',indices);
1792    if ~isempty(filename_1)
1793        indices_1=fullfile_uvmat('','','','',InputFile.NomType_1,i1_1,i2_1,j1_1,j2_1);
1794        set(handles.FileIndex_1,'String',indices_1);
1795    end
1796    if isequal(movie_status,1)
1797        set(handles.movie_pair,'Value',1)
1798        movie_pair_Callback(hObject, eventdata, handles); %reactivate moviepair if it was activated
1799    end
1800end
1801
1802%------------------------------------------------------------------------
1803% --- Executes on button press in movie_pair: create an alternating movie with two view
1804function movie_pair_Callback(hObject, eventdata, handles)
1805%------------------------------------------------------------------------
1806%% stop movie action if the movie_pair button is off
1807if ~get(handles.movie_pair,'value')
1808    set(handles.movie_pair,'BusyAction','Cancel')%stop movie pair if button is 'off'
1809    set(handles.i2,'String','')
1810    set(handles.j2,'String','')
1811    return
1812else
1813    set(handles.movie_pair,'BusyAction','queue')
1814end
1815
1816%% initialisation
1817set(handles.movie_pair,'BackgroundColor',[1 1 0])%paint the command button in yellow
1818drawnow
1819list_fields=get(handles.FieldName,'String');% list menu fields
1820index_fields=get(handles.FieldName,'Value');% selected string index
1821FieldName=list_fields{index_fields}; % selected field
1822UvData=get(handles.uvmat,'UserData');
1823if isequal(FieldName,'image')
1824    index=1;
1825    [RootPath,SubDir,RootFile,FileIndices,Ext]=read_file_boxes(handles);
1826    NomType=get(handles.NomType,'String');
1827else
1828    list_fields=get(handles.FieldName_1,'String');% list menu fields
1829    index_fields=get(handles.FieldName_1,'Value');% selected string index
1830    FieldName=list_fields{index_fields}; % selected field
1831    if isequal(FieldName,'image')
1832        index=2;
1833        [RootPath,tild,RootFile,FileIndex_1,Ext,NomType]=read_file_boxes_1(handles);
1834    else
1835        msgbox_uvmat('ERROR','an image or movie must be first introduced as input')
1836        set(handles.movie_pair,'BackgroundColor',[1 0 0])%paint the command button in red
1837        set(handles.movie_pair,'Value',0)
1838        return
1839    end
1840end
1841num_i1=str2num(get(handles.i1,'String'));
1842num_j1=stra2num(get(handles.j1,'String'));
1843num_i2=str2num(get(handles.i2,'String'));
1844num_j2=stra2num(get(handles.j2,'String'));
1845if isempty(num_j2)
1846    if isempty(num_i2)   
1847        msgbox_uvmat('ERROR', 'a second image index i2 or j2 is needed to show the pair as a movie')
1848        set(handles.movie_pair,'BackgroundColor',[1 0 0])%paint the command button in red
1849         set(handles.movie_pair,'Value',0)
1850        return
1851    else
1852        num_j2=num_j1;%repeat the index i1 by default
1853    end
1854end
1855if isempty(num_i2)
1856    num_i2=num_i1;%repeat the index i1 by default
1857end
1858imaname_1=fullfile_uvmat(RootPath,SubDir,RootFile,Ext,NomType,num_i2,[],num_j2);
1859if strcmp(NomType,'*')
1860    num_frame=num_i2;
1861else
1862    num_frame=num_j2;
1863end
1864if ~exist(imaname_1,'file')
1865      msgbox_uvmat('ERROR',['second input open (-)  ' imaname_1 ' not found']);
1866      set(handles.movie_pair,'BackgroundColor',[1 0 0])%paint the command button in red
1867       set(handles.movie_pair,'Value',0)
1868      return
1869end
1870
1871%% get the first image
1872%Field.AName='image';
1873if index==1
1874    Field_a=UvData.Field;% movie on the second field
1875else
1876    Field_a=UvData.Field_1;% movie on the first field
1877end
1878
1879%% read the second image
1880MovieObject=[];
1881if numel(UvData.MovieObject)>=index
1882    MovieObject=UvData.MovieObject{index};
1883end
1884[Field_b,ParamOut,errormsg] = read_field(imaname_1,UvData.FileType{index},MovieObject,num_frame);
1885% Field_b.AX=Field_a.AX;
1886% Field_b.AY=Field_a.AY;
1887% % z index
1888% nbslice=str2double(get(handles.num_NbSlice,'String'));
1889% if ~isempty(nbslice)
1890%     Field_b.ZIndex=mod(num_i2-1,nbslice)+1;
1891% end
1892% Field_b.CoordUnit='pixel';
1893%
1894% %% determine the input file type
1895% if (test_1 && isfield(UvData,'MovieObject')&& numel(UvData.MovieObject>=2))||(~test_1 && ~isempty(UvData.MovieObject{1}))
1896%     FileType='movie';
1897% elseif isequal(lower(Ext),'.avi')
1898%     FileType='avi';
1899% elseif isequal(lower(Ext),'.vol')
1900%     FileType='vol';
1901% else
1902%    form=imformats(Ext(2:end));
1903%    if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
1904%        if isequal(NomType,'*');
1905%            FileType='multimage';
1906%        else
1907%            FileType='image';
1908%        end
1909%    end
1910% end
1911% switch FileType
1912%         case 'movie'
1913%             if test_1
1914%                 Field_b.A=read(UvData.MovieObject{2},num_i2);
1915%             else
1916%                 Field_b.A=read(UvData.MovieObject{1},num_i2);
1917%             end
1918%         case 'avi'
1919%             mov=aviread(imaname_1,num_i2);
1920%             Field_b.A=frame2im(mov(1));
1921%         case 'vol'
1922%             Field_b.A=imread(imaname_1);
1923%         case 'multimage'
1924%             Field_b.A=imread(imaname_1,num_i2);
1925%         case 'image'
1926%             Field_b.A=imread(imaname_1);
1927% end
1928% if get(handles.slices,'Value')
1929%     Field.ZIndex=str2double(get(handles.z_index,'String'));
1930% end
1931
1932%px to phys or other transform on field
1933transform=get(handles.path_transform,'UserData');
1934if  ~isempty(transform)
1935    if isfield(UvData,'XmlData') && numel(UvData.XmlData)>=index %use geometry calib recorded from the ImaDoc xml file as first priority
1936        if index==2
1937        Field_a=transform(Field_a,UvData.XmlData{index});%the first field has been stored without transform
1938        end
1939        Field_b=transform(Field_b,UvData.XmlData{index});
1940    end
1941end
1942
1943 % make movie until movie speed is set to 0 or STOP is activated
1944hima=findobj(handles.PlotAxes,'Tag','ima');% %handles.PlotAxes =main plotting window (A GENERALISER)
1945set(handles.STOP,'Visible','on')
1946set(handles.speed,'Visible','on')
1947set(handles.speed_txt,'Visible','on')
1948while get(handles.speed,'Value')~=0 && isequal(get(handles.movie_pair,'BusyAction'),'queue')%isequal(get(handles.run0,'BusyAction'),'queue'); % enable STOP command
1949    % read and plot the series of images in non erase mode
1950    set(hima,'CData',Field_b.A);
1951    pause(1.02-get(handles.speed,'Value'));% wait for next image
1952    set(hima,'CData',Field_a.A);
1953    pause(1.02-get(handles.speed,'Value'));% wait for next image
1954end
1955set(handles.movie_pair,'BackgroundColor',[1 0 0])%paint the command button in red
1956 set(handles.movie_pair,'Value',0)
1957
1958%------------------------------------------------------------------------
1959% --- Executes on button press in run0.
1960function run0_Callback(hObject, eventdata, handles)
1961%------------------------------------------------------------------------
1962set(handles.run0,'BackgroundColor',[1 1 0])%paint the command button in yellow
1963drawnow
1964[RootPath,SubDir,RootFile,FileIndex,FileExt]=read_file_boxes(handles);
1965filename=[fullfile(RootPath,SubDir,RootFile) FileIndex FileExt];
1966filename_1='';%default
1967FileIndex_1='';
1968if get(handles.SubField,'Value')
1969    [RootPath_1,SubDir_1,RootFile_1,FileIndex_1,FileExt_1]=read_file_boxes_1(handles);
1970    filename_1=[fullfile(RootPath_1,SubDir_1,RootFile_1) FileIndex_1 FileExt_1];
1971end
1972num_i1=stra2num(get(handles.i1,'String'));
1973num_i2=stra2num(get(handles.i2,'String'));
1974num_j1=stra2num(get(handles.j1,'String'));
1975num_j2=stra2num(get(handles.j2,'String'));
1976[tild,tild,tild,i1_1,i2_1,j1_1,j2_1]=fileparts_uvmat(FileIndex_1);
1977
1978errormsg=refresh_field(handles,filename,filename_1,num_i1,num_i2,num_j1,num_j2,i1_1,i2_1,j1_1,j2_1);
1979
1980if ~isempty(errormsg)
1981      msgbox_uvmat('ERROR',errormsg);
1982else
1983    set(handles.i1,'BackgroundColor',[1 1 1])
1984    set(handles.i2,'BackgroundColor',[1 1 1])
1985    set(handles.j1,'BackgroundColor',[1 1 1])
1986    set(handles.j2,'BackgroundColor',[1 1 1])
1987    set(handles.FileIndex,'BackgroundColor',[1 1 1])
1988    set(handles.FileIndex_1,'BackgroundColor',[1 1 1])
1989end   
1990set(handles.run0,'BackgroundColor',[1 0 0])
1991
1992
1993%------------------------------------------------------------------------
1994% --- read the input files and refresh all the plots, including projection.
1995% OUTPUT:
1996%  errormsg: error message char string  =[] by default
1997% INPUT:
1998% filename: first input file (=[] in the absence of input file)
1999% filename_1: second input file (=[] in the asbsenc of secodn input file)
2000% num_i1,num_i2,num_j1,num_j2; frame indices
2001% Field: structure describing an optional input field (then replace the input file)
2002
2003function errormsg=refresh_field(handles,FileName,FileName_1,num_i1,num_i2,num_j1,num_j2,i1_1,i2_1,j1_1,j2_1)
2004%------------------------------------------------------------------------
2005
2006%% initialisation
2007abstime=[];
2008abstime_1=[];
2009dt=[];
2010if ~exist('Field','var')
2011    Field={};
2012end
2013UvData=get(handles.uvmat,'UserData');
2014if ishandle(handles.UVMAT_title) %remove title panel on uvmat
2015    delete(handles.UVMAT_title)
2016end
2017
2018%% determine the main input file information for action
2019if ~exist(FileName,'file')
2020    errormsg=['input file ' FileName ' does not exist'];
2021    return
2022end
2023NomType=get(handles.NomType,'String');
2024NomType_1='';
2025if strcmp(get(handles.NomType_1,'Visible'),'on')
2026    NomType_1=get(handles.NomType_1,'String');
2027end
2028%update the z position index
2029nbslice_str=get(handles.num_NbSlice,'String');
2030if isequal(nbslice_str,'volume')%NOT USED
2031    z_index=num_j1;
2032    set(handles.z_index,'String',num2str(z_index))
2033else
2034    nbslice=str2num(nbslice_str);
2035    z_index=mod(num_i1-1,nbslice)+1;
2036    set(handles.z_index,'String',num2str(z_index))
2037end
2038% refresh menu for save_mask if relevant
2039masknumber=get(handles.masklevel,'String');
2040if length(masknumber)>=z_index
2041    set(handles.masklevel,'Value',z_index)
2042end
2043
2044%% test for need of tps
2045check_proj_tps=0;
2046if  (strcmp(UvData.FileType{1},'civdata')||strcmp(UvData.FileType{1},'civx'))
2047    for iobj=1:numel(UvData.Object)
2048        if isfield(UvData.Object{iobj},'ProjMode')&& strcmp(UvData.Object{iobj}.ProjMode,'filter')
2049            check_proj_tps=1;
2050            break
2051        end
2052    end
2053end
2054
2055%% read the first input field
2056ParamIn.ColorVar='';%default variable name for vector color
2057frame_index=1;%default
2058FieldName='';%default
2059VelType='';%default
2060switch UvData.FileType{1}
2061    case {'civx','civdata','netcdf'};
2062        list_fields=get(handles.FieldName,'String');% list menu fields
2063        FieldName= list_fields{get(handles.FieldName,'Value')}; % selected field
2064        % if get_field... is selected, the GUI get_field will be used to enter fields
2065        if ~strcmp(FieldName,'get_field...')
2066            if get(handles.FixVelType,'Value')
2067                VelTypeList=get(handles.VelType,'String');
2068                VelType=VelTypeList{get(handles.VelType,'Value')};
2069            end
2070        end
2071        % case of input vector field, get the scalar used for vector color
2072        if ~isempty(regexp(FieldName,'^vec('))
2073            list_code=get(handles.ColorCode,'String');% list menu fields
2074            index_code=get(handles.ColorCode,'Value');% selected string index
2075            if  ~strcmp(list_code{index_code},'black') &&  ~strcmp(list_code{index_code},'white')
2076                list_code=get(handles.ColorScalar,'String');% list menu fields
2077                index_code=get(handles.ColorScalar,'Value');% selected string index
2078                ParamIn.ColorVar= list_code{index_code}; % selected field
2079            end
2080        end
2081    case {'video','mmreader'}
2082        ParamIn=UvData.MovieObject{1};     
2083        if ~strcmp(NomType,'*')
2084            frame_index=num_j1;%frame index for movies or multimage
2085        else
2086            frame_index=num_i1;
2087        end
2088    case 'multimage'
2089        if ~strcmp(NomType,'*')
2090            frame_index=num_j1;%frame index for movies or multimage
2091        else
2092            frame_index=num_i1;
2093        end
2094    case 'vol' %TODO: update
2095        if isfield(UvData.XmlData,'Npy') && isfield(UvData.XmlData,'Npx')
2096            ParamIn.Npy=UvData.XmlData.Npy;
2097            ParamIn.Npx=UvData.XmlData.Npx;
2098        else           
2099            errormsg='Npx and Npy need to be defined in the xml file for volume images .vol';
2100            return
2101        end
2102end
2103if isstruct (ParamIn)
2104    ParamIn.FieldName=FieldName;
2105    ParamIn.VelType=VelType;
2106    XNameMenu=get(handles.Coord_x,'String');
2107    ParamIn.Coord_x=XNameMenu(get(handles.Coord_x,'Value'));
2108    YNameMenu=get(handles.Coord_y,'String');
2109    ParamIn.Coord_y=YNameMenu(get(handles.Coord_y,'Value'));
2110end
2111check_tps = 0;         
2112if strcmp(UvData.FileType{1},'civdata')&&~strcmp(ParamIn.FieldName,'velocity')&&~strcmp(ParamIn.FieldName,'get_field...')
2113       check_tps=1;%tps needed to get the requested field
2114end
2115[Field{1},ParamOut,errormsg] = read_field(FileName,UvData.FileType{1},ParamIn,frame_index);
2116if ~isempty(errormsg)
2117    errormsg=['uvmat / refresh_field / read_field( ' FileName ') / ' errormsg];
2118    return
2119end 
2120if isfield(ParamOut,'Npx')&& isfield(ParamOut,'Npy')
2121    set(handles.num_Npx,'String',num2str(ParamOut.Npx));% display image size on the interface
2122    set(handles.num_Npy,'String',num2str(ParamOut.Npy));
2123end
2124if isfield(ParamOut,'TimeIndex')% case of time obtained from get_field
2125    set(handles.i1,'String',num2str(ParamOut.TimeIndex))
2126end
2127if isfield(ParamOut,'TimeValue')
2128    Field{1}.Time=ParamOut.TimeValue;% case of time obtained from get_field
2129end
2130Field{1}.ZIndex=z_index; %used for multiplane 3D calibration
2131
2132
2133%% choose and read a second field FileName_1 if defined
2134VelType_1=[];%default
2135FieldName_1=[];
2136ParamIn_1=[];
2137ParamOut_1=[];
2138frame_index_1=1;
2139if ~isempty(FileName_1)
2140    if ~exist(FileName_1,'file')
2141        errormsg=['second file ' FileName_1 ' does not exist'];
2142        return
2143    end
2144   % Name=FileName_1;
2145    switch UvData.FileType{2}
2146        case {'civx','civdata','netcdf'};
2147            list_fields=get(handles.FieldName_1,'String');% list menu fields
2148            if ischar(list_fields),list_fields={list_fields};end
2149            FieldName_1= list_fields{get(handles.FieldName_1,'Value')}; % selected field
2150            if ~strcmp(FieldName,'get_field...')
2151                if get(handles.FixVelType,'Value')
2152                    VelTypeList=get(handles.VelType_1,'String');
2153                    VelType_1=VelTypeList{get(handles.VelType_1,'Value')};% read the velocity type.
2154                end
2155            end
2156            if strcmp(FieldName_1,'velocity')&& strcmp(get(handles.ColorCode,'Visible'),'on')
2157                list_code=get(handles.ColorCode,'String');% list menu fields
2158                index_code=get(handles.ColorCode,'Value');% selected string index
2159                if  ~strcmp(list_code{index_code},'black') &&  ~strcmp(list_code{index_code},'white')
2160                    list_code=get(handles.ColorScalar,'String');% list menu fields
2161                    index_code=get(handles.ColorScalar,'Value');% selected string index
2162                    ParamIn_1.ColorVar= list_code{index_code}; % selected field for vector color display                 
2163                end
2164            end
2165        case {'video','mmreader'}
2166            ParamIn_1=UvData.MovieObject{2};
2167                        if ~strcmp(NomType_1,'*')
2168                frame_index_1=j1_1;%frame index for movies or multimage
2169            else
2170                frame_index_1=i1_1;
2171            end 
2172         case 'multimage'
2173            if ~strcmp(NomType_1,'*')
2174                frame_index_1=j1_1;%frame index for movies or multimage
2175            else
2176                frame_index_1=i1_1;
2177            end   
2178        case 'vol' %TODO: update
2179            if isfield(UvData.XmlData,'Npy') && isfield(UvData.XmlData,'Npx')
2180                ParamIn_1.Npy=UvData.XmlData.Npy;
2181                ParamIn_1.Npx=UvData.XmlData.Npx;
2182            else
2183                errormsg='Npx and Npy need to be defined in the xml file for volume images .vol';
2184                return
2185            end
2186    end
2187    if isequal(get(handles.NomType_1,'Visible'),'on')
2188    NomType_1=get(handles.NomType_1,'String');
2189    else
2190        NomType_1=get(handles.NomType,'String');
2191    end
2192    test_keepdata_1=0;% test for keeping the previous stored data if the input files are unchanged
2193    if ~isequal(NomType_1,'*')&& isfield(UvData,'FileName_1')
2194           test_keepdata_1= strcmp(FileName_1,UvData.FileName_1) ;
2195    end
2196    if test_keepdata_1
2197        Field{2}=UvData.Field_1;% keep the stored field
2198        ParamOut_1=UvData.ParamOut_1;
2199    else
2200        if isempty(ParamIn_1) || isstruct(ParamIn_1)
2201        ParamIn_1.FieldName=FieldName_1;
2202        ParamIn_1.VelType=VelType_1;
2203        ParamIn_1.GUIName='get_field_1';
2204        end 
2205        [Field{2},ParamOut_1,errormsg] = read_field(FileName_1,UvData.FileType{2},ParamIn_1,frame_index_1);
2206        if ~isempty(errormsg)
2207            errormsg=['error in reading ' FieldName_1 ' in ' FileName_1 ': ' errormsg];
2208            return
2209        end
2210        if isstruct(ParamOut_1)&&~strcmp(ParamOut_1.FieldName,'get_field...')&& (strcmp(UvData.FileType{2},'civdata')||strcmp(UvData.FileType{2},'civx'))...
2211                &&~strcmp(ParamOut_1.FieldName,'velocity') && ~strcmp(ParamOut_1.FieldName,'get_field...')
2212            if ~check_proj_tps
2213             %   Field{2}=calc_field([{ParamOut_1.FieldName} {ParamOut_1.ColorVar}],Field{2});
2214            end
2215        end
2216    end
2217    Field{2}.ZIndex=z_index;%used for multi-plane 3D calibration
2218end
2219
2220%% update uvmat interface
2221if isfield(ParamOut,'Npx')
2222    set(handles.num_Npx,'String',num2str(ParamOut.Npx));% display image size on the interface
2223    set(handles.num_Npy,'String',num2str(ParamOut.Npy));
2224elseif isfield(ParamOut_1,'Npx')
2225    set(handles.num_Npx,'String',num2str(ParamOut_1.Npx));% display image size on the interface
2226    set(handles.num_Npy,'String',num2str(ParamOut_1.Npy));
2227end
2228
2229%% update the display menu for the first velocity type (first menuline)
2230test_veltype=0;
2231% if ~isequal(FileType,'netcdf')|| isequal(FieldName,'get_field...')
2232if (strcmp(UvData.FileType{1},'civx')||strcmp(UvData.FileType{1},'civdata'))&& ~strcmp(FieldName,'get_field...')
2233    test_veltype=1;
2234    set(handles.VelType,'Visible','on')
2235    set(handles.VelType_1,'Visible','on')
2236    set(handles.FixVelType,'Visible','on')
2237    menu=set_veltype_display(ParamOut.CivStage,UvData.FileType{1});
2238    index_menu=strcmp(ParamOut.VelType,menu);%look for VelType in  the menu
2239    index_val=find(index_menu,1);
2240    if isempty(index_val)
2241        index_val=1;
2242    end
2243    set(handles.VelType,'Value',index_val)
2244    if ~get(handles.SubField,'value')
2245        set(handles.VelType,'String',menu)
2246        set(handles.VelType_1,'Value',1)
2247        set(handles.VelType_1,'String',[{''};menu])
2248    end
2249else
2250    set(handles.VelType,'Visible','off')
2251end
2252% display the FieldName menu from the input file and pick the selected one:
2253% if isstruct(ParamOut)
2254%     field_index=strcmp(ParamOut.FieldName,ParamOut.FieldList);
2255%     set(handles.FieldName,'String',ParamOut.FieldList); %update the field menu
2256%     set(handles.FieldName,'Value',find(field_index,1))
2257% end
2258
2259%% update the display menu for the second velocity type (second menuline)
2260test_veltype_1=0;
2261if isempty(FileName_1)
2262    %     set(handles.FieldName_1,'Value',1); %update the field menu
2263    %     if isstruct(ParamOut)
2264    %     set(handles.FieldName_1,'String',[{''};ParamOut.FieldList]); %update the field menu
2265    %     end
2266elseif ~test_keepdata_1
2267    if (~strcmp(UvData.FileType{2},'netcdf')&&~strcmp(UvData.FileType{2},'civdata')&&~strcmp(UvData.FileType{2},'civx'))|| isequal(FieldName_1,'get_field...')
2268        set(handles.VelType_1,'Visible','off')
2269    else
2270        test_veltype_1=1;
2271        set(handles.VelType_1,'Visible','on')
2272        menu=set_veltype_display(ParamOut_1.CivStage,UvData.FileType{2});
2273        index_menu=strcmp(ParamOut_1.VelType,menu);
2274        set(handles.VelType_1,'Value',1+find(index_menu,1))
2275        set(handles.VelType_1,'String',[{''};menu])
2276    end
2277    % update the second field menu: the same quantity
2278    if isstruct(ParamOut_1)
2279%        set(handles.FieldName_1,'String',[{''};ParamOut_1.FieldList]); %update the field menu
2280        % display the FieldName menu from the input file and pick the selected one:
2281        FieldList=get(handles.FieldName_1,'String');
2282        field_index=strcmp(ParamOut_1.FieldName,FieldList);
2283        if ~isempty(field_index)
2284            set(handles.FieldName_1,'Value',find(field_index,1))
2285        end
2286    end
2287end
2288if test_veltype||test_veltype_1
2289    set(handles.FixVelType,'Visible','on')
2290else
2291    set(handles.FixVelType,'Visible','off')
2292end
2293
2294% field_index=strcmp(ParamOut_1.FieldName,ParamOut_1.FieldList);
2295% set(handles.FieldName,'String',ParamOut.FieldList); %update the field menu
2296% set(handles.FieldName,'Value',find(field_index,1))
2297   
2298%% introduce w as background image by default for a new series (only for nbdim=2)
2299if ~isfield(UvData,'NewSeries')
2300    UvData.NewSeries=1;
2301end
2302%put W as background image by default if NbDim=2:
2303if  UvData.NewSeries && isequal(get(handles.SubField,'Value'),0) && isfield(Field{1},'W') && ~isempty(Field{1}.W) && ~isequal(Field{1}.NbDim,3);
2304        set(handles.SubField,'Value',1);
2305        set(handles.RootPath_1,'String','"')
2306        set(handles.RootFile_1,'String','"')
2307        set(handles.SubDir_1,'String','"');
2308         indices=fullfile_uvmat('','','','',NomType,num_i1,num_i2,num_j1,num_j2);
2309        set(handles.FileIndex_1,'String',indices)
2310        set(handles.FileExt_1,'String','"');
2311        set(handles.FieldName_1,'Visible','on');
2312        set(handles.FieldName_1,'Visible','on');
2313        set(handles.RootPath_1,'Visible','on')
2314        set(handles.RootFile_1,'Visible','on')
2315        set(handles.SubDir_1,'Visible','on');
2316        set(handles.FileIndex_1,'Visible','on');
2317        set(handles.FileExt_1,'Visible','on');
2318        set(handles.FieldName_1,'Visible','on');
2319        Field{1}.AName='w';
2320end           
2321
2322%% display time
2323testimedoc=0;
2324TimeUnit='';
2325if isfield(Field{1},'Time')
2326    abstime=Field{1}.Time;%time read from the netcdf input file
2327end
2328if numel(Field)==2 && isfield(Field{2},'Time')
2329    abstime_1=Field{2}.Time;%time read from the netcdf input file
2330end
2331if isfield(Field{1},'Dt')
2332    dt=Field{1}.Dt;%dt read from the netcdf input file
2333    if isfield(Field{1},'TimeUnit')
2334       TimeUnit=Field{1}.TimeUnit;
2335    end
2336elseif numel(Field)==2 && isfield(Field{2},'Dt')%dt obtained from the second field if not defined in the first
2337    dt=Field{2}.Dt;%dt read from the netcdf input file
2338    if isfield(Field{2},'TimeUnit')
2339       TimeUnit=Field{2}.TimeUnit;
2340    end
2341end
2342% time from xml file overset previous result
2343if isfield(UvData,'XmlData') && isfield(UvData.XmlData{1},'Time')
2344    if isempty(num_i2)||isnan(num_i2)
2345        num_i2=num_i1;
2346    end
2347    if isempty(num_j1)||isnan(num_j1)
2348        num_j1=1;
2349    end
2350    if isempty(num_j2)||isnan(num_j2)
2351        num_j2=num_j1;
2352    end
2353    siz=size(UvData.XmlData{1}.Time);
2354    if ~isempty(num_i1)&& ~isempty(num_i2) && num_i1>0 &&siz(1)>=max(num_i1,num_i2) && siz(2)>=max(num_j1,num_j2)
2355        abstime=(UvData.XmlData{1}.Time(num_i1,num_j1)+UvData.XmlData{1}.Time(num_i2,num_j2))/2;%overset the time read from files
2356        dt=(UvData.XmlData{1}.Time(num_i2,num_j2)-UvData.XmlData{1}.Time(num_i1,num_j1));
2357        Field{1}.Dt=dt;
2358        if isfield(UvData.XmlData{1},'TimeUnit')
2359            TimeUnit=UvData.XmlData{1}.TimeUnit;
2360        end
2361    end
2362    if numel(UvData.XmlData)==2
2363        if isempty(i2_1)
2364            i2_1=num_i1;
2365        end
2366        if isempty(j1_1)
2367            j1_1=1;
2368        end
2369        if isempty(j2_1)
2370            j2_1=j1_1;
2371        end
2372        siz=size(UvData.XmlData{2}.Time);
2373        if ~isempty(i1_1) && siz(1)>=max(i1_1,i2_1) && siz(2)>=max(j1_1,j2_1)
2374            abstime_1=(UvData.XmlData{2}.Time(i1_1,j1_1)+UvData.XmlData{2}.Time(i2_1,j2_1))/2;%overset the time read from files
2375            Field{2}.Dt=(UvData.XmlData{2}.Time(i2_1,j2_1)-UvData.XmlData{2}.Time(i1_1,j1_1));
2376        end
2377    end
2378end
2379if ~isequal(numel(abstime),1)
2380    abstime=[];
2381end
2382if ~isequal(numel(abstime_1),1)
2383      abstime_1=[];
2384end 
2385set(handles.abs_time,'String',num2str(abstime,5))
2386set(handles.abs_time_1,'String',num2str(abstime_1,5))
2387if isempty(dt)||isequal(dt,0)
2388    set(handles.Dt_txt,'String','')
2389else
2390    if  isempty(TimeUnit)
2391        set(handles.Dt_txt,'String',['Dt=' num2str(1000*dt,3) '  10^(-3)'] )
2392    else
2393        set(handles.Dt_txt,'String',['Dt=' num2str(1000*dt,3) '  m' TimeUnit] )
2394    end
2395end
2396
2397
2398%% store the current open names, fieldname and vel types in uvmat interface
2399UvData.FileName_1=FileName_1;
2400UvData.ParamOut_1=ParamOut_1;
2401if numel(Field)==2
2402UvData.Field_1=Field{2}; %store the second field for possible use at next RUN
2403end
2404
2405%% apply coordinate transform or other user fct
2406transform=get(handles.path_transform,'UserData');
2407if isempty(transform)
2408    UvData.Field=Field{1};
2409else
2410    XmlData=[];%default
2411    XmlData_1=[];%default
2412    if isfield(UvData,'XmlData')%use geometry calib recorded from the ImaDoc xml file as first priority
2413        XmlData=UvData.XmlData{1};
2414        if numel(UvData.XmlData)==2
2415            XmlData_1=UvData.XmlData{2};
2416        end
2417    end
2418    transform=get(handles.path_transform,'UserData');
2419    switch nargin(transform)
2420        case 4
2421            if length(Field)==2
2422                UvData.Field=transform(Field{1},XmlData,Field{2},XmlData_1);
2423            else
2424                UvData.Field=transform(Field{1},XmlData);
2425            end
2426        case 3
2427            if length(Field)==2
2428                UvData.Field=transform(Field{1},XmlData,Field{2});
2429            else
2430                UvData.Field=transform(Field{1},XmlData);
2431            end
2432        case 2
2433            UvData.Field=transform(Field{1},XmlData);
2434        case 1
2435            UvData.Field=transform(Field{1});
2436    end
2437end
2438
2439%% calculate tps coefficients if needed
2440UvData.Field=calc_tps(UvData.Field,check_proj_tps);
2441
2442%% analyse input field
2443%test_x=0;
2444%test_z=0;% test for unstructured z coordinate
2445%[errormsg,ListDimName,DimValue,VarDimIndex]=check_field_structure(UvData.Field);% check the input field structure
2446% if ~isempty(errormsg)
2447%     errormsg=['error in uvmat/refresh_field/check_field_structure: ' errormsg];% display error
2448%     return
2449% end
2450[CellInfo,NbDimArray,errormsg]=find_field_cells(UvData.Field);% analyse  the input field structure
2451if ~isempty(errormsg)
2452    errormsg=['uvmat /refresh_field / find_field_cells / ' errormsg];% display error
2453    return
2454end
2455
2456NbDim=max(NbDimArray);% spatial dimension of the input field
2457imax=find(NbDimArray==NbDim);% indices of field cells to consider
2458if isfield(UvData.Field,'NbDim')
2459    NbDim=double(UvData.Field.NbDim);% deal with plane fields containing z coordinates
2460end
2461
2462%UvData.Field=get_bounds(UvData.Field);
2463
2464%% get bounds and mesh (needed  to propose default options for projection objects)
2465if NbDim>1
2466    CoordMax=zeros(numel(imax),NbDim);
2467    CoordMin=zeros(numel(imax),NbDim);
2468    Mesh=zeros(1,numel(imax));
2469    for ind=1:numel(imax)
2470        if strcmp(CellInfo{imax(ind)}.CoordType,'tps')
2471            CoordName=UvData.Field.ListVarName{CellInfo{imax(ind)}.CoordIndex};% X,Y coordinates in a single variable
2472            CoordMax(ind,NbDim)=max(max(UvData.Field.(CoordName)(1:end-3,1,:),[],1),[],3);% max of x component (2D case)
2473            CoordMax(ind,NbDim-1)=max(max(UvData.Field.(CoordName)(1:end-3,2,:),[],1),[],3);% max of y component (2D case)
2474            CoordMin(ind,NbDim)=min(min(UvData.Field.(CoordName)(1:end-3,1,:),[],1),[],3);
2475            CoordMin(ind,NbDim-1)=min(min(UvData.Field.(CoordName)(1:end-3,2,:),[],1),[],3);% min of y component (2D case)
2476        else
2477            XName=UvData.Field.ListVarName{CellInfo{imax(ind)}.CoordIndex(end)};
2478            YName=UvData.Field.ListVarName{CellInfo{imax(ind)}.CoordIndex(end-1)};
2479            CoordMax(ind,NbDim)=max(max(UvData.Field.(XName)));
2480            CoordMin(ind,NbDim)=min(min(UvData.Field.(XName)));
2481            CoordMax(ind,NbDim-1)=max(max(UvData.Field.(YName)));
2482            CoordMin(ind,NbDim-1)=min(min(UvData.Field.(YName)));
2483            %         test_x=1;%test for unstructured coordinates
2484            if NbDim==3
2485                ZName=UvData.Field.ListVarName{CellInfo{imax(ind)}.CoordIndex(1)};
2486                CoordMax(imax(ind),1)=max(max(UvData.Field.(ZName)));
2487                CoordMin(ind,1)=min(min(UvData.Field.(ZName)));
2488            end
2489        end
2490        switch CellInfo{imax(ind)}.CoordType
2491           
2492            case {'scattered','tps'} %unstructured coordinates
2493                NbPoints=CellInfo{imax(ind)}.CoordSize;% total nbre of points
2494                Mesh(ind)=(prod(CoordMax(ind,:)-CoordMin(ind,:))/NbPoints)^(1/NbDim); %(volume or area per point)^(1/NbDim)
2495            case 'grid'%structured coordinate
2496                NbPoints=CellInfo{imax(ind)}.CoordSize;% nbre of points in each direction
2497                Mesh(ind)=min((CoordMax(ind,:)-CoordMin(ind,:))./(NbPoints-1));               
2498        end
2499    end
2500    UvData.Field.CoordMesh=min(Mesh);
2501    UvData.Field.XMax=max(CoordMax(ind,end));
2502    UvData.Field.XMin=min(CoordMin(ind,end));
2503    UvData.Field.YMax=max(CoordMax(ind,end-1));
2504    UvData.Field.YMin=max(CoordMin(ind,end-1));
2505    if NbDim==3
2506        UvData.Field.ZMax=max(CoordMax(ind,1));
2507        UvData.Field.ZMin=max(CoordMin(ind,1));
2508    end
2509    % adjust the mesh to a value 1, 2 , 5 *10^n
2510    ord=10^(floor(log10(UvData.Field.CoordMesh)));%order of magnitude
2511    if UvData.Field.CoordMesh/ord>=5
2512        UvData.Field.CoordMesh=5*ord;
2513    elseif UvData.Field.CoordMesh/ord>=2
2514        UvData.Field.CoordMesh=2*ord;
2515    else
2516        UvData.Field.CoordMesh=ord;
2517    end
2518    % default projection plane
2519    if isempty(UvData.Object{1})
2520        UvData.Object{1}.Type='plane';%main plotting plane
2521        UvData.Object{1}.ProjMode='projection';%main plotting plane
2522        UvData.Object{1}.DisplayHandle.uvmat=[]; %plane not visible in uvmat
2523        UvData.Object{1}.DisplayHandle.view_field=[]; %plane not visible in uvmat
2524    end
2525    %% 3D case (menuvolume)
2526    if NbDim==3% && UvData.NewSeries
2527        test_set_object=1;
2528        hset_object=findobj(allchild(0),'tag','set_object');% look for the set_object GUI
2529        ZBounds(1)=UvData.Field.ZMin; %minimum for the Z slider
2530        ZBounds(2)=UvData.Field.ZMax;%maximum for the Z slider
2531        if ~isempty(hset_object) %if set_object is detected
2532            delete(hset_object);% delete the GUI set_object if it does not fit
2533        end
2534        if test_set_object% reinitiate the GUI set_object
2535            delete_object(1);% delete the current projection object in the list UvData.Object, delete its graphic representations and update the list displayed in handles.ListObject and 2
2536            UvData.Object{1}.NbDim=NbDim;%test for 3D objects
2537            UvData.Object{1}.RangeZ=UvData.Field.CoordMesh;%main plotting plane
2538            UvData.Object{1}.Coord(1,3)=(UvData.Field.ZMin+UvData.Field.ZMax)/2;%section at a middle plane chosen
2539            UvData.Object{1}.Angle=[0 0 0];
2540            UvData.Object{1}.HandlesDisplay=plot(0,0,'Tag','proj_object');% A REVOIR
2541            UvData.Object{1}.Name='1-PLANE';
2542            UvData.Object{1}.enable_plot=1;
2543            set_object(UvData.Object{1},handles,ZBounds);
2544            set(handles.ListObject,'Value',1);
2545            set(handles.ListObject,'String',{'1-PLANE'});
2546            set(handles.edit_object,'Value',1)% put the plane in edit mode to enable the z cursor
2547            edit_object_Callback([],[], handles)
2548        end
2549        %multilevel case (single menuplane in a 3D space)
2550    elseif isfield(UvData,'Z')
2551        if isfield(UvData,'CoordType')&& isequal(UvData.CoordType,'phys') && isfield(UvData,'XmlData')
2552            XmlData=UvData.XmlData{1};
2553            if isfield(XmlData,'PlanePos')
2554                UvData.Object{1}.Coord=XmlData.PlanePos(UvData.ZIndex,:);
2555            end
2556            if isfield(XmlData,'PlaneAngle')
2557                siz=size(XmlData.PlaneAngle);
2558                indangle=min(siz(1),UvData.ZIndex);%take first angle if a single angle is defined (translating scanning)
2559                UvData.Object{1}.PlaneAngle=XmlData.PlaneAngle(indangle,:);
2560            end
2561        elseif isfield(UvData,'ZIndex')
2562            UvData.Object{1}.ZObject=UvData.ZIndex;
2563        end
2564    end
2565end
2566
2567testnewseries=UvData.NewSeries;
2568UvData.NewSeries=0;% put to 0 the test for a new field series (set by RootPath_callback)
2569set(handles.uvmat,'UserData',UvData)
2570
2571%% reset the min and max of scalar if only the mask is displayed(TODO: check the need)
2572% if isfield(UvData,'Mask')&& ~isfield(UvData,'A')
2573%     set(handles.num_MinA,'String','0')
2574%     set(handles.num_MaxA,'String','255')
2575% end
2576
2577%% usual 1D (x,y) plots
2578if NbDim<=1
2579    set(handles.Objects,'Visible','off')
2580    set(handles.ListObject_1_title,'Visible','off')
2581    set(handles.ListObject_1,'Visible','off')
2582    [PlotType,PlotParamOut]=plot_field(UvData.Field,handles.PlotAxes,read_GUI(handles.uvmat));
2583    write_plot_param(handles,PlotParamOut) %update the auto plot parameters
2584   
2585%% 2D or 3D fieldname are generally projected
2586else
2587    set(handles.Objects,'Visible','on')
2588    set(handles.ListObject_1_title,'Visible','on')
2589    set(handles.ListObject_1,'Visible','on')
2590   
2591    %% Plot the projections on the selected  projection objects
2592    % main projection object (uvmat display)
2593    list_object=get(handles.ListObject_1,'String');
2594    if isequal(list_object,{''})||isequal(list_object,' ')%refresh list of objects if the menu is empty
2595        set(handles.ListObject,'Value',1)
2596        set(handles.ListObject,'String',{'plane'})
2597        UvData.Object{1}.Type='plane';%main plotting plane
2598        UvData.Object{1}.ProjMode='projection';%main plotting plane
2599        UvData.Object{1}.DisplayHandle.uvmat=[]; %plane not visible in uvmat
2600        UvData.Object{1}.DisplayHandle.view_field=[]; %plane not visible in uvmat
2601        set(handles.ListObject_1,'Value',1)
2602        set(handles.ListObject_1,'String',{'plane'})
2603    end
2604    IndexObj(1)=get(handles.ListObject_1,'Value');%selected projection object for main view
2605    if IndexObj(1)> numel(UvData.Object)
2606        IndexObj(1)=1;%select the first object if the selected one does not exist
2607        set(handles.ListObject_1,'Value',1)
2608    end
2609    IndexObj(2)=get(handles.ListObject,'Value');%selected projection object for main view
2610    if isequal(IndexObj(2),IndexObj(1))
2611        IndexObj(2)=[];
2612    end
2613    plot_handles{1}=handles;
2614    if isfield(UvData,'plotaxes')%case of movies
2615        haxes(1)=UvData.plotaxes;
2616    else
2617        haxes(1)=handles.PlotAxes;
2618    end
2619    PlotParam{1}=read_GUI(handles.uvmat);
2620    %default settings if vectors not visible
2621    if ~isfield(PlotParam{1},'Vectors')
2622        PlotParam{1}.Vectors.MaxVec=1;
2623        PlotParam{1}.Vectors.MinVec=0;
2624        PlotParam{1}.Vectors.CheckFixVecColor=1;
2625        PlotParam{1}.Vectors.ColCode1=0.33;
2626        PlotParam{1}.Vectors.ColCode2=0.66;
2627        PlotParam{1}.Vectors.ColorScalar={''};
2628        PlotParam{1}.Vectors.ColorCode= {'rgb'};
2629    end
2630    PosColorbar{1}=UvData.OpenParam.PosColorbar;%prescribe the colorbar position on the uvmat interface
2631   
2632    %% second projection object (view_field display)
2633    if length( IndexObj)>=2
2634        view_field_handle=findobj(allchild(0),'tag','view_field');%handles of the view_field GUI
2635        if ~isempty(view_field_handle)
2636            plot_handles{2}=guidata(view_field_handle);
2637            haxes(2)=plot_handles{2}.PlotAxes;
2638            PlotParam{2}=read_GUI(handles.uvmat);%read plotting parameters on the uvmat interface
2639            PosColorbar{2}='*'; %TODO: deal with colorbar position on view_field
2640        end
2641    end
2642   
2643    %% loop on the projection objects: one or two
2644   
2645    for imap=1:numel(IndexObj)
2646        iobj=IndexObj(imap);
2647        [ObjectData,errormsg]=proj_field(UvData.Field,UvData.Object{iobj});% project field on the object
2648        if ~isempty(errormsg)
2649            return
2650        end
2651        if testnewseries
2652            PlotParam{imap}.Scalar.CheckBW=[]; %B/W option depends on the input field (image or scalar)
2653            if isfield(ObjectData,'CoordUnit')
2654                PlotParam{imap}.Coordinates.CheckFixAspectRatio=1;% set x and y scaling equal if CoordUnit is defined (common unit for x and y)
2655                PlotParam{imap}.Coordinates.AspectRatio=1; %set aspect ratio to 1
2656            end
2657        end
2658        %use of mask (TODO: check)
2659        if isfield(ObjectData,'NbDim') && isequal(ObjectData.NbDim,2) && isfield(ObjectData,'Mask') && isfield(ObjectData,'A')
2660            flag_mask=double(ObjectData.Mask>200);%=0 for masked regions
2661            AX=ObjectData.AX;%x coordiantes for the scalar field
2662            AY=ObjectData.AY;%y coordinates for the scalar field
2663            MaskX=ObjectData.MaskX;%x coordiantes for the mask
2664            MaskY=ObjectData.MaskY;%y coordiantes for the mask
2665            if ~isequal(MaskX,AX)||~isequal(MaskY,AY)
2666                nxy=size(flag_mask);
2667                sizpx=(ObjectData.MaskX(end)-ObjectData.MaskX(1))/(nxy(2)-1);%size of a mask pixel
2668                sizpy=(ObjectData.MaskY(1)-ObjectData.MaskY(end))/(nxy(1)-1);
2669                x_mask=ObjectData.MaskX(1):sizpx:ObjectData.MaskX(end); % pixel x coordinates for image display
2670                y_mask=ObjectData.MaskY(1):-sizpy:ObjectData.MaskY(end);% pixel x coordinates for image display
2671                %project on the positions of the scalar
2672                npxy=size(ObjectData.A);
2673                dxy(1)=(ObjectData.AY(end)-ObjectData.AY(1))/(npxy(1)-1);%grid mesh in y
2674                dxy(2)=(ObjectData.AX(end)-ObjectData.AX(1))/(npxy(2)-1);%grid mesh in x
2675                xi=ObjectData.AX(1):dxy(2):ObjectData.AX(end);
2676                yi=ObjectData.AY(1):dxy(1):ObjectData.AY(end);
2677                [XI,YI]=meshgrid(xi,yi);% creates the matrix of regular coordinates
2678                flag_mask = interp2(x_mask,y_mask,flag_mask,XI,YI);
2679            end
2680            AClass=class(ObjectData.A);
2681            ObjectData.A=flag_mask.*double(ObjectData.A);
2682            ObjectData.A=feval(AClass,ObjectData.A);
2683        end
2684        if ~isempty(ObjectData)
2685            %PlotType='none'; %default
2686            if imap==2 && isempty(view_field_handle)
2687                view_field(ObjectData)
2688            else
2689                [PlotType,PlotParamOut]=plot_field(ObjectData,haxes(imap),PlotParam{imap},PosColorbar{imap});
2690                write_plot_param(plot_handles{imap},PlotParamOut) %update the auto plot parameters
2691                if isfield(Field,'CoordMesh')&&~isempty(Field.CoordMesh)
2692                    ObjectData.CoordMesh=Field.CoordMesh; % gives an estimated mesh size (useful for mouse action on the plot)
2693                end
2694            end
2695        end
2696    end
2697   
2698    %% update the mask
2699    if isequal(get(handles.CheckMask,'Value'),1)%if the mask option is on
2700        update_mask(handles,num_i1,num_i2);
2701    end
2702   
2703    %% prepare the menus of histograms and plot them (histogram of the whole volume in 3D case)
2704    menu_histo=(UvData.Field.ListVarName)';%list of field variables to be displayed for the menu of histogram display
2705    ind_skip=[];
2706    % nb_histo=1;
2707    Ustring='';
2708    Vstring='';
2709    % suppress coordinates from the histogram menu
2710    for ivar=1:numel(menu_histo)%l loop on field variables:
2711        if isfield(UvData.Field,'VarAttribute') && numel(UvData.Field.VarAttribute)>=ivar && isfield(UvData.Field.VarAttribute{ivar},'Role')
2712            Role=UvData.Field.VarAttribute{ivar}.Role;
2713            switch Role
2714                case {'coord_x','coord_y','coord_z','dimvar'}
2715                    ind_skip=[ind_skip ivar];
2716                case {'vector_x'}
2717                    Ustring=UvData.Field.ListVarName{ivar};
2718                    ind_skip=[ind_skip ivar];
2719                case {'vector_y'}
2720                    Vstring=UvData.Field.ListVarName{ivar};
2721                    ind_skip=[ind_skip ivar];
2722            end
2723        end
2724        DimCell=UvData.Field.VarDimName{ivar};
2725        DimName='';
2726        if ischar(DimCell)
2727            DimName=DimCell;
2728        elseif iscell(DimCell)&& numel(DimCell)==1
2729            DimName=DimCell{1};
2730        end
2731        if strcmp(DimName,menu_histo{ivar})
2732            ind_skip=[ind_skip ivar];
2733        end
2734    end
2735    menu_histo(ind_skip)=[];% remove skipped items
2736    if ~isempty(Ustring)
2737        menu_histo=[{[Ustring ',' Vstring]};menu_histo];% add U, V at the beginning if they exist
2738    end
2739   
2740    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2741    % display menus and plot histograms
2742    test_v=0;
2743    if ~isempty(menu_histo)
2744        set(handles.histo1_menu,'Value',1)
2745        set(handles.histo1_menu,'String',menu_histo)
2746        histo1_menu_Callback(handles.histo1_menu, [], handles)% plot first histogram
2747    end
2748end
2749
2750%------------------------------------------------------------------------
2751function histo1_menu_Callback(hObject, eventdata, handles)
2752%--------------------------------------------
2753histo_menu=get(handles.histo1_menu,'String');
2754histo_value=get(handles.histo1_menu,'Value');
2755FieldName=histo_menu{histo_value};
2756
2757UvData=get(handles.uvmat,'UserData');
2758Field=UvData.Field;
2759r=regexp(FieldName,'(?<var1>.*)(?<sep>,)(?<var2>.*)','names');
2760FieldName_2='';
2761if ~isempty(r)
2762    FieldName=r.var1;
2763    FieldName_2=r.var2;
2764end
2765if ~isfield(UvData.Field,FieldName)
2766    msgbox_uvmat('ERROR',['no field  ' FieldName ' for histogram'])
2767    return
2768end
2769FieldHisto=Field.(FieldName);
2770if isfield(Field,'FF') && ~isempty(Field.FF) && isequal(size(Field.FF),size(FieldHisto))
2771    indsel=find(Field.FF==0);%find values marked as false
2772    if ~isempty(indsel)
2773        FieldHisto=FieldHisto(indsel);
2774        if ~isempty(FieldName_2)
2775            FieldHisto(:,:,2)=Field.(FieldName_2)(indsel);
2776        end
2777    end
2778end
2779if isempty(Field)
2780    msgbox_uvmat('ERROR',['empty field ' FieldName])
2781else
2782    nxy=size(FieldHisto);
2783    Amin=double(min(min(min(FieldHisto))));%min of field value
2784    Amax=double(max(max(max(FieldHisto))));%max of field value
2785    if isequal(Amin,Amax)
2786        cla(handles.HistoAxes)
2787    else
2788        Histo.ListVarName={FieldName,'histo'};
2789        if isfield(Field,'NbDim') && isequal(Field.NbDim,3)
2790            Histo.VarDimName={FieldName,FieldName}; %dimensions for the histogram
2791        else
2792            if numel(nxy)==2
2793                Histo.VarDimName={FieldName,FieldName}; %dimensions for the histogram
2794            else
2795                Histo.VarDimName={FieldName,{FieldName,'component'}}; %dimensions for the histogram
2796            end
2797        end
2798        %unit
2799        units=[]; %default
2800        for ivar=1:numel(Field.ListVarName)
2801            if strcmp(Field.ListVarName{ivar},FieldName)
2802                if isfield(Field,'VarAttribute') && numel(Field.VarAttribute)>=ivar && isfield(Field.VarAttribute{ivar},'units')
2803                    units=Field.VarAttribute{ivar}.units;
2804                    break
2805                end
2806            end
2807        end
2808        if ~isempty(units)
2809            Histo.VarAttribute{1}.units=units;
2810        end
2811        eval(['Histo.' FieldName '=linspace(Amin,Amax,50);'])%absissa values for histo
2812        if isfield(Field,'NbDim') && isequal(Field.NbDim,3)
2813            C=reshape(double(FieldHisto),1,[]);% reshape in a vector
2814            Histo.histo(:,1)=hist(C, Histo.(FieldName));  %calculate histogram
2815        else
2816            for col=1:size(FieldHisto,3)
2817                B=FieldHisto(:,:,col);
2818                C=reshape(double(B),1,nxy(1)*nxy(2));% reshape in a vector
2819                Histo.histo(:,col)=hist(C, Histo.(FieldName));  %calculate histogram
2820            end
2821        end
2822        plot_field(Histo,handles.HistoAxes);
2823        hlegend=legend;
2824        if isempty(FieldName_2)
2825        set(hlegend,'String',FieldName)
2826        else
2827            set(hlegend,'String',{FieldName;FieldName_2})
2828        end
2829    end
2830end
2831
2832%-------------------------------------------------------------------
2833% --- translate coordinate to matrix index
2834%-------------------------------------------------------------------
2835function [indx,indy]=pos2ind(x0,rangx0,nxy)
2836indx=1+round((nxy(2)-1)*(x0-rangx0(1))/(rangx0(2)-rangx0(1)));% index x of pixel 
2837indy=1+round((nxy(1)-1)*(y12-rangy0(1))/(rangy0(2)-rangy0(1)));% index y of pixel
2838
2839
2840%-------------------------------------------------------------------
2841% --- Executes on button press in 'CheckZoom'.
2842%-------------------------------------------------------------------
2843function CheckZoom_Callback(hObject, eventdata, handles)
2844
2845if (get(handles.CheckZoom,'Value') == 1);
2846    set(handles.CheckZoom,'BackgroundColor',[1 1 0])
2847    set(handles.CheckFixLimits,'Value',1)% propose by default fixed limits for the plotting axes
2848    set(handles.CheckFixLimits,'BackgroundColor',[1 1 0])
2849else
2850    set(handles.CheckZoom,'BackgroundColor',[0.7 0.7 0.7])
2851end
2852
2853%-------------------------------------------------------------------
2854% --- Executes on button press in 'CheckFixLimits'.
2855%-------------------------------------------------------------------
2856function CheckFixLimits_Callback(hObject, eventdata, handles)
2857test=get(handles.CheckFixLimits,'Value');
2858if test
2859    set(handles.CheckFixLimits,'BackgroundColor',[1 1 0])
2860else
2861    set(handles.CheckFixLimits,'BackgroundColor',[0.7 0.7 0.7])
2862    update_plot(handles);
2863end
2864
2865%-------------------------------------------------------------------
2866% --- Executes on button press in CheckFixAspectRatio.
2867function CheckFixAspectRatio_Callback(hObject, eventdata, handles)
2868%-------------------------------------------------------------------
2869if get(handles.CheckFixAspectRatio,'Value')
2870    set(handles.CheckFixAspectRatio,'BackgroundColor',[1 1 0])
2871    update_plot(handles);
2872else
2873    set(handles.CheckFixAspectRatio,'BackgroundColor',[0.7 0.7 0.7])
2874    update_plot(handles);
2875end
2876
2877%-------------------------------------------------------------------
2878function num_AspectRatio_Callback(hObject, eventdata, handles)
2879%-------------------------------------------------------------------
2880set(handles.CheckFixAspectRatio,'Value',1)% select the fixed aspect ratio button
2881set(handles.CheckFixAspectRatio,'BackgroundColor',[1 1 0])% mark in yellow
2882update_plot(handles);
2883%-------------------------------------------------------------------
2884
2885%-------------------------------------------------------------------
2886
2887
2888
2889%-------------------------------------------------------------------
2890%----Executes on button press in 'record': records the current flags of manual correction.
2891%-------------------------------------------------------------------
2892function record_Callback(hObject, eventdata, handles)
2893% [filebase,num_i1,num_j1,num_i2,num_j2,Ext,NomType,SubDir]=read_input_file(handles);
2894[RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes(handles);
2895FileName=[fullfile(RootPath,SubDir,RootFile) FileIndices FileExt];
2896%FileName=read_file_boxes(handles);
2897[erread,message]=fileattrib(FileName);
2898if ~isempty(message) && ~isequal(message.UserWrite,1)
2899     msgbox_uvmat('ERROR',['no writting access to ' FileName])
2900     return
2901end
2902test_civ2=isequal(get(handles.civ2,'BackgroundColor'),[1 1 0]);
2903test_civ1=isequal(get(handles.VelType,'BackgroundColor'),[1 1 0]);
2904if ~test_civ2 && ~test_civ1
2905    msgbox_uvmat('ERROR','manual correction only possible for CIV1 or CIV2 velocity fields')
2906end
2907if test_civ2
2908    nbname='nb_vectors2';
2909   flagname='vec2_FixFlag';
2910   attrname='fix2';
2911end
2912if test_civ1
2913    nbname='nb_vectors';
2914   flagname='vec_FixFlag';
2915   attrname='fix';
2916end
2917%write fix flags in the netcdf file
2918UvData=get(handles.uvmat,'UserData');
2919hhh=which('netcdf.open');% look for built-in matlab netcdf library
2920if ~isequal(hhh,'')% case of new builtin Matlab netcdf library
2921    nc=netcdf.open(FileName,'NC_WRITE');
2922    netcdf.reDef(nc);
2923    netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),attrname,1);
2924    dimid = netcdf.inqDimID(nc,nbname);
2925    try
2926        varid = netcdf.inqVarID(nc,flagname);% look for already existing fixflag variable
2927    catch
2928        varid=netcdf.defVar(nc,flagname,'double',dimid);%create fixflag variable if it does not exist
2929    end
2930    netcdf.endDef(nc);
2931    netcdf.putVar(nc,varid,UvData.PlotAxes.FF);
2932    netcdf.close(nc); 
2933else %old netcdf library
2934    netcdf_toolbox(FileName,AxeData,attrname,nbname,flagname)
2935end
2936
2937%-------------------------------------------------------------------
2938%----Correct the netcdf file, using toolbox (old versions of Matlab).
2939%-------------------------------------------------------------------
2940function netcdf_toolbox(FileName,AxeData,attrname,nbname,flagname)
2941nc=netcdf(FileName,'write'); %open netcdf file
2942result=redef(nc);
2943eval(['nc.' attrname '=1;']);
2944theDim=nc(nbname) ;% get the number of velocity vectors
2945nb_vectors=size(theDim);
2946var_FixFlag=ncvar(flagname,nc);% var_FixFlag will be written as the netcdf variable vec_FixFlag
2947var_FixFlag(1:nb_vectors)=AxeData.FF;%
2948fin=close(nc);
2949
2950%-------------------------------------------------------------------
2951% --- Executes on button press in SubField
2952function SubField_Callback(hObject, eventdata, handles)
2953%-------------------------------------------------------------------
2954UvData=get(handles.uvmat,'UserData');
2955if get(handles.SubField,'Value')==0% if the subfield button is desactivated   
2956    set(handles.RootPath_1,'String','')
2957    set(handles.RootFile_1,'String','')
2958    set(handles.SubDir_1,'String','');
2959    set(handles.FileIndex_1,'String','');
2960    set(handles.FileExt_1,'String','');
2961    set(handles.RootPath_1,'Visible','off')
2962    set(handles.RootFile_1,'Visible','off')
2963    set(handles.SubDir_1,'Visible','off');
2964    set(handles.NomType_1,'Visible','off');
2965    set(handles.abs_time_1,'Visible','off')
2966    set(handles.FileIndex_1,'Visible','off');
2967    set(handles.FileExt_1,'Visible','off');
2968    set(handles.FieldName_1,'Value',1);%set to blank state
2969    set(handles.VelType_1,'Value',1);%set to blank state
2970    set(handles.num_Opacity,'String','')% desactivate opacity setting
2971    FieldList=get(handles.FieldName,'String');
2972    if numel(FieldList)>1   % if a choice of fields exists
2973        set(handles.FieldName_1,'Value',1)% set second field choice to blank
2974        set(handles.FieldName_1,'String',[{''};FieldList])% reproduce the menu FieldName plus a blank option
2975    else
2976        set(handles.FieldName_1,'String',{''})% set second field choice to blank
2977    end
2978    if ~strcmp(get(handles.VelType,'Visible'),'on')
2979        set(handles.VelType_1,'Visible','off')
2980    end
2981    if isfield(UvData,'XmlData_1')
2982        UvData=rmfield(UvData,'XmlData_1');
2983    end
2984    set(handles.uvmat,'UserData',UvData);
2985    transform_fct_list=get(handles.transform_fct,'String');
2986    transform_fct=transform_fct_list(get(handles.transform_fct,'Value'));
2987    if strcmp(transform_fct,'sub_field')
2988        set(handles.transform_fct,'Value',1)%suppress the sub_field transform
2989        transform_fct_Callback(hObject, eventdata, handles);
2990    else
2991        run0_Callback(hObject, eventdata, handles)
2992    end 
2993else
2994    MenuBrowse_1_Callback(hObject, eventdata, handles)
2995end
2996
2997%------------------------------------------------------------------------
2998% --- read the data displayed for the input rootfile windows (new): TODO use read_GUI
2999
3000function [RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes(handles)
3001%------------------------------------------------------------------------
3002InputFile=read_GUI(handles.InputFile);
3003RootPath=InputFile.RootPath;
3004SubDir=regexprep(InputFile.SubDir,'/|\','');
3005RootFile=regexprep(InputFile.RootFile,'/|\','');
3006FileIndices=InputFile.FileIndex;
3007FileExt=InputFile.FileExt;
3008
3009
3010%------------------------------------------------------------------------
3011% ---- read the data displayed for the second input rootfile windows
3012function [RootPath_1,SubDir_1,RootFile_1,FileIndex_1,FileExt_1,NomType_1]=read_file_boxes_1(handles)
3013%------------------------------------------------------------------------
3014RootPath_1=get(handles.RootPath_1,'String'); % read the data from the file1_input window
3015if isequal(get(handles.RootPath_1,'Visible'),'off') || isequal(RootPath_1,'"')
3016    RootPath_1=get(handles.RootPath,'String');
3017end;
3018SubDir_1=get(handles.SubDir_1,'String');
3019if isequal(get(handles.SubDir_1,'Visible'),'off')|| isequal(SubDir_1,'"')
3020    SubDir_1=get(handles.SubDir,'String');
3021end
3022SubDir_1=regexprep(SubDir_1,'\<[\\/]|[\\/]\>','');%suppress possible / or \ separator at the beginning or the end of the string
3023RootFile_1=get(handles.RootFile_1,'String');
3024if isequal(get(handles.RootFile_1,'Visible'),'off') || isequal(RootFile_1,'"')
3025    RootFile_1=get(handles.RootFile,'String');
3026end
3027RootFile_1=regexprep(RootFile_1,'\<[\\/]|[\\/]\>','');%suppress possible / or \ separator at the beginning or the end of the string
3028FileIndex_1=get(handles.FileIndex_1,'String');
3029if isequal(get(handles.FileIndex_1,'Visible'),'off')|| isequal(FileIndex_1,'"')
3030    FileIndex_1=get(handles.FileIndex,'String');
3031end
3032FileExt_1=get(handles.FileExt_1,'String');
3033if isequal(get(handles.FileExt_1,'Visible'),'off') || isequal(FileExt_1,'"')
3034    FileExt_1=get(handles.FileExt,'String');%read FileExt by default
3035end
3036NomType_1=get(handles.NomType_1,'String');
3037if isequal(get(handles.NomType_1,'Visible'),'off') || isequal(NomType_1,'"')
3038    NomType_1=get(handles.NomType,'String');%read FileExt by default
3039end
3040%------------------------------------------------------------------------
3041% --- Executes on menu selection FieldName
3042function FieldName_Callback(hObject, eventdata, handles)
3043%------------------------------------------------------------------------
3044list_fields=get(handles.FieldName,'String');% list menu fields
3045index_fields=get(handles.FieldName,'Value');% selected string index
3046field= list_fields{index_fields(1)}; % selected string
3047if isequal(field,'get_field...')
3048    set(handles.FixVelType,'visible','off')
3049    set(handles.VelType,'visible','off')
3050    set(handles.VelType_1,'visible','off')
3051    [RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes(handles);
3052    FileName=[fullfile(RootPath,SubDir,RootFile) FileIndices FileExt];
3053    hget_field=findobj(allchild(0),'name','get_field');
3054    if ~isempty(hget_field)
3055        delete(hget_field)
3056    end
3057    get_field(FileName);
3058    return %no further action
3059end
3060
3061UvData=get(handles.uvmat,'UserData');
3062
3063%read the rootfile input display
3064[RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes(handles);
3065FileName=[fullfile(RootPath,SubDir,RootFile) FileIndices FileExt];
3066[tild,tild,tild,i1,i2,j1,j2,tild,NomType]=fileparts_uvmat(['xxx' get(handles.FileIndex,'String') FileExt]);
3067if isequal(field,'image')
3068    if  isfield(UvData.Field,'Civ2_ImageA')%get the corresponding input image in the netcdf file
3069        imagename=UvData.Field.Civ2_ImageA;
3070    elseif isfield(UvData.Field,'Civ1_ImageA')%
3071        imagename=UvData.Field.Civ1_ImageA;
3072    else
3073        SubDirBase=regexprep(SubDir,'\..*','');%take the root part of SubDir, before the first dot '.'
3074        imagename=fullfile_uvmat(RootPath,SubDirBase,RootFile,'.png',NomType,i1,[],j1,[]);
3075    end
3076    if ~exist(imagename,'file')
3077        [FileName,PathName] = uigetfile( ...
3078            {'*.png;*.jpg;*.tif;*.avi;*.AVI;*.vol', ' (*.png, .tif, *.avi,*.vol)';
3079            '*.jpg',' jpeg image files'; ...
3080            '*.png','.png image files'; ...
3081            '*.tif','.tif image files'; ...
3082            '*.avi;*.AVI','.avi movie files'; ...
3083            '*.vol','.volume images (png)'; ...
3084            '*.*',  'All Files (*.*)'}, ...
3085            'Pick an image',imagename);
3086        if ~ischar(FileName),return,end %abandon if the browser is cancelled
3087        imagename=[PathName FileName];
3088    end
3089     % display the selected field and related information
3090    display_file_name(handles,imagename)%display the image
3091    return
3092else
3093    ext=get(handles.FileExt,'String');
3094    if ~isequal(ext,'.nc') %find the new NomType if the previous display was not already a netcdf file
3095        [FileName,PathName] = uigetfile( ...
3096            {'*.nc', ' (*.nc)';
3097            '*.nc',' netcdf files'; ...
3098            '*.*',  'All Files (*.*)'}, ...
3099            'Pick a netcdf file',FileBase);
3100        if ~ischar(FileName),return,end %abandon if the browser is cancelled
3101        FullFileName=[PathName FileName];
3102        % display the selected field and related information
3103        display_file_name( handles,FullFileName)
3104        return
3105    end
3106end
3107indices=fullfile_uvmat('','','','',NomType,i1,i2,j1,j2);
3108set(handles.FileIndex,'String',indices)
3109% set(handles.NomType,'String',NomType)
3110
3111%common to Fields_1_Callback
3112list_fields_1=get(handles.FieldName_1,'String');% list menu fields
3113field_1='';
3114if ~isempty(list_fields_1)
3115field_1= list_fields_1{get(handles.FieldName_1,'Value')}; % selected string
3116end
3117if isequal(field,'image')||isequal(field_1,'image')
3118    set(handles.TitleNpxy,'Visible','on')% visible npx,pxcm... buttons
3119    set(handles.num_Npx,'Visible','on')
3120    set(handles.num_Npy,'Visible','on')
3121else
3122    set(handles.TitleNpxy,'Visible','off')% visible npx,pxcm... buttons
3123    set(handles.num_Npx,'Visible','off')
3124    set(handles.num_Npy,'Visible','off')
3125end
3126if ~(isfield(UvData,'NewSeries')&&isequal(UvData.NewSeries,1))
3127    run0_Callback(hObject, eventdata, handles)
3128end
3129
3130%---------------------------------------------------
3131% --- Executes on menu selection FieldName
3132function FieldName_1_Callback(hObject, eventdata, handles)
3133%-------------------------------------------------
3134%% read input data
3135check_new=~get(handles.SubField,'Value'); %check_new=1 if a second field was not previously entered
3136UvData=get(handles.uvmat,'UserData');
3137if check_new && isfield(UvData,'XmlData')
3138    UvData.XmlData{2}=UvData.XmlData{1};
3139end
3140if isfield(UvData,'Field_1')
3141    UvData=rmfield(UvData,'Field_1');% remove the stored second field (a new one needs to be read)
3142end
3143UvData.FileName_1='';% desactivate the use of a constant second file
3144list_fields=get(handles.FieldName,'String');% list menu fields
3145field= list_fields{get(handles.FieldName,'Value')}; % selected string
3146list_fields=get(handles.FieldName_1,'String');% list menu fields
3147field_1= list_fields{get(handles.FieldName_1,'Value')}; % selected string for the second field
3148if isempty(field_1)%||(numel(UvData.FileType)>=2 && strcmp(UvData.FileType{2},'image'))
3149    set(handles.SubField,'Value',0)
3150    SubField_Callback(hObject, eventdata, handles)
3151    return
3152else
3153    set(handles.SubField,'Value',1)%state that a second field is now entered
3154end
3155
3156%% read the rootfile input display
3157[RootPath_1,SubDir_1,RootFile_1,FileIndex_1,FileExt_1]=read_file_boxes_1(handles);
3158FileName_1=[fullfile(RootPath_1,SubDir_1,RootFile_1) FileIndex_1 FileExt_1];
3159[tild,tild,tild,i1,i2,j1,j2]=fileparts_uvmat(get(handles.FileIndex,'String'));
3160switch field_1
3161    case 'get_field...'
3162        set_veltype_display(0) % no veltype display
3163        hget_field=findobj(allchild(0),'name','get_field_1');
3164        if ~isempty(hget_field)
3165            delete(hget_field)
3166        end
3167        hget_field=get_field(FileName_1);
3168        set(hget_field,'name','get_field_1')
3169        hhget_field=guidata(hget_field);
3170        set(hhget_field.list_fig,'Value',1)
3171        set(hhget_field.list_fig,'String',{'uvmat'})
3172        if check_new
3173            UvData.FileType{2}=UvData.FileType{1};
3174            set(handles.FileIndex_1,'String',get(handles.FileIndex,'String'))
3175              set(handles.uvmat,'UserData',UvData)
3176        end
3177    case 'image'
3178        % get the image name corresponding to the current netcdf name (no unique correspondance)
3179        if  isfield(UvData.Field,'Civ2_ImageA')%get the corresponding input image in the netcdf file
3180            imagename=UvData.Field.Civ2_ImageA;
3181        elseif isfield(UvData.Field,'Civ1_ImageA')%
3182            imagename=UvData.Field.Civ1_ImageA;
3183        else
3184            SubDirBase=regexprep(SubDir_1,'\..*','');%take the root part of SubDir, before the first dot '.'
3185            imagename=fullfile_uvmat(RootPath_1,SubDirBase,RootFile_1,'.png',get(handles.NomType,'String'),i1,[],j1);
3186        end
3187        if ~exist(imagename,'file') % browse for images if it is not found
3188            [FileName,PathName] = uigetfile( ...
3189                {'*.png;*.jpg;*.tif;*.avi;*.AVI;*.vol', ' (*.png, .tif, *.avi,*.vol)';
3190                '*.jpg',' jpeg image files'; ...
3191                '*.png','.png image files'; ...
3192                '*.tif','.tif image files'; ...
3193                '*.avi;*.AVI','.avi movie files'; ...
3194                '*.vol','.volume images (png)'; ...
3195                '*.*',  'All Files (*.*)'}, ...
3196                'Pick an image',imagename);
3197            if ~ischar(FileName),return,end %abandon if the browser is cancelled
3198            imagename=[PathName FileName];
3199        end
3200        if ~ischar(imagename)% quit if the browser has  been closed
3201            set(handles.SubField,'Value',0)
3202        else %valid browser input:  display the selected image
3203            set(handles.TitleNpxy,'Visible','on')% visible npx,pxcm... buttons
3204            set(handles.num_Npx,'Visible','on')
3205            set(handles.num_Npy,'Visible','on')
3206            display_file_name(handles,imagename,2)%display the imag
3207        end
3208    otherwise
3209        check_refresh=1;
3210        if check_new% if a second field was not previously entered, we just read another field in the first input file
3211            set(handles.FileIndex_1,'String',get(handles.FileIndex,'String'))
3212            set(handles.FileExt_1,'String',get(handles.FileExt,'String'))
3213           
3214            UvData.FileType{2}=UvData.FileType{1};
3215            UvData.XmlData{2}= UvData.XmlData{1};
3216            transform=get(handles.path_transform,'UserData');
3217             if (~isa(transform,'function_handle')||nargin(transform)<3)
3218                set(handles.uvmat,'UserData',UvData)
3219                set(handles.transform_fct,'value',2); % set transform fct to 'sub_field' if the current fct does not accept two input fields
3220                transform_fct_Callback(hObject, eventdata, handles)% activate transform_fct_Callback and refresh current plot
3221                 check_refresh=0;
3222             end             
3223        end
3224        if ~isequal(field,'image')
3225            set(handles.TitleNpxy,'Visible','off')% visible npx,pxcm... buttons
3226            set(handles.num_Npx,'Visible','off')
3227            set(handles.num_Npy,'Visible','off')
3228        end
3229        set(handles.uvmat,'UserData',UvData)
3230 
3231        if check_refresh && ~(isfield(UvData,'NewSeries')&&isequal(UvData.NewSeries,1))
3232            run0_Callback(hObject, eventdata, handles)
3233        end
3234end
3235
3236%------------------------------------------------------------------------
3237% --- set the visibility of relevant velocity type menus:
3238function menu=set_veltype_display(Civ,FileType)
3239%------------------------------------------------------------------------
3240if ~exist('FileType','var')
3241    FileType='civx';
3242end
3243switch FileType
3244    case 'civx'
3245        menu={'civ1';'interp1';'filter1';'civ2';'interp2';'filter2'};
3246        if isequal(Civ,0)
3247            imax=0;
3248        elseif isequal(Civ,1) || isequal(Civ,2)
3249            imax=1;
3250        elseif isequal(Civ,3)
3251            imax=3;
3252        elseif isequal(Civ,4) || isequal(Civ,5)
3253            imax=4;
3254        elseif isequal(Civ,6) %patch2
3255            imax=6;
3256        end
3257    case 'civdata'
3258        menu={'civ1';'filter1';'civ2';'filter2'};
3259        if isequal(Civ,0)
3260            imax=0;
3261        elseif isequal(Civ,1) || isequal(Civ,2)
3262            imax=1;
3263        elseif isequal(Civ,3)
3264            imax=2;
3265        elseif isequal(Civ,4) || isequal(Civ,5)
3266            imax=3;
3267        elseif isequal(Civ,6) %patch2
3268            imax=4;
3269        end
3270end
3271menu=menu(1:imax);
3272
3273%------------------------------------------------------------------------
3274% --- Executes on button press in FixVelType.
3275function FixVelType_Callback(hObject, eventdata, handles)
3276%------------------------------------------------------------------------
3277% refresh the current plot if the fixed  veltype is unselected
3278if ~get(handles.FixVelType,'Value')
3279    run0_Callback(hObject, eventdata, handles)
3280end
3281
3282%------------------------------------------------------------------------
3283% --- Executes on button press in VelType.
3284function VelType_Callback(hObject, eventdata, handles)
3285%------------------------------------------------------------------------
3286set(handles.FixVelType,'Value',1)
3287run0_Callback(hObject, eventdata, handles)
3288
3289%------------------------------------------------------------------------
3290% --- Executes on choice selection in VelType_1.
3291function VelType_1_Callback(hObject, eventdata, handles)
3292%------------------------------------------------------------------------
3293set(handles.FixVelType,'Value',1)% the velocity type is now imposed by the GUI (not automatic)
3294UvData=get(handles.uvmat,'UserData');
3295set(handles.run0,'BackgroundColor',[1 1 0])%paint run0 button in yellow to indicate its activation
3296drawnow   
3297InputFile=read_GUI(handles.InputFile);% read the input file parameters
3298[RootPath,SubDir,RootFile,FileIndex,FileExt]=read_file_boxes(handles);
3299[RootPath_1,SubDir_1,RootFile_1,FileIndex_1,FileExt_1]=read_file_boxes_1(handles);
3300FileName=[fullfile(RootPath,SubDir,RootFile) FileIndex FileExt];% name of the first input file
3301
3302check_refresh=0;
3303if isempty(InputFile.VelType_1)
3304        FileName_1='';% we plot the first input field without the second field
3305        set(handles.SubField,'Value',0)
3306        SubField_Callback(hObject, eventdata, handles)% activate SubField_Callback and refresh current plot, removing the second field
3307elseif get(handles.SubField,'Value')% if subfield is already 'on'
3308     FileName_1=[fullfile(RootPath_1,SubDir_1,RootFile_1) FileIndex_1 FileExt_1];% name of the second input file
3309     check_refresh=1;%will refresh the current plot
3310else% we introduce the same file (with a different field) for the second series
3311     FileName_1=FileName;% we compare two fields in the same file
3312     UvData.FileType{2}=UvData.FileType{1};
3313     UvData.XmlData{2}= UvData.XmlData{1};
3314     set(handles.SubField,'Value',1)
3315     transform=get(handles.path_transform,'UserData');
3316     if (~isa(transform,'function_handle')||nargin(transform)<3)
3317        set(handles.uvmat,'UserData',UvData)
3318        set(handles.transform_fct,'value',2); % set transform fct to 'sub_field' if the current fct does not accept two input fields
3319        transform_fct_Callback(hObject, eventdata, handles)% activate transform_fct_Callback and refresh current plot
3320     else
3321         check_refresh=1;
3322     end 
3323end
3324
3325% refresh the current plot if it has not been done previously
3326if check_refresh
3327    UvData.FileName_1='';% desactivate the use of a constant second file
3328    set(handles.uvmat,'UserData',UvData)
3329    num_i1=stra2num(get(handles.i1,'String'));
3330    num_i2=stra2num(get(handles.i2,'String'));
3331    num_j1=stra2num(get(handles.j1,'String'));
3332    num_j2=stra2num(get(handles.j2,'String'));
3333    [tild,tild,tild,i1_1,i2_1,j1_1,j2_1]=fileparts_uvmat(['xx' FileIndex_1]);
3334    errormsg=refresh_field(handles,FileName,FileName_1,num_i1,num_i2,num_j1,num_j2,i1_1,i2_1,j1_1,j2_1);
3335    if ~isempty(errormsg)
3336        msgbox_uvmat('ERROR',errormsg);
3337    else
3338        set(handles.i1,'BackgroundColor',[1 1 1])
3339        set(handles.i2,'BackgroundColor',[1 1 1])
3340        set(handles.j1,'BackgroundColor',[1 1 1])
3341        set(handles.j2,'BackgroundColor',[1 1 1])
3342        set(handles.FileIndex,'BackgroundColor',[1 1 1])
3343        set(handles.FileIndex_1,'BackgroundColor',[1 1 1])
3344    end
3345    set(handles.run0,'BackgroundColor',[1 0 0])
3346end
3347
3348
3349%-----------------------------------------------------------------------
3350% --- reset civ buttons
3351function reset_vel_type(handles_civ0,handle1)
3352%-----------------------------------------------------------------------
3353for ibutton=1:length(handles_civ0)
3354    set(handles_civ0(ibutton),'BackgroundColor',[0.831 0.816 0.784])
3355    set(handles_civ0(ibutton),'Value',0)
3356end
3357if exist('handle1','var')%handles of selected button
3358        set(handle1,'BackgroundColor',[1 1 0]) 
3359end
3360
3361%-----------------------------------------------------------------------
3362% --- Executes on button press in MENUVOLUME.
3363function VOLUME_Callback(hObject, eventdata, handles)
3364%-----------------------------------------------------------------------
3365%errordlg('command VOL not implemented yet')
3366if ishandle(handles.UVMAT_title)
3367    delete(handles.UVMAT_title)
3368end
3369UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
3370if isequal(get(handles.VOLUME,'Value'),1)
3371    set(handles.CheckZoom,'Value',0)
3372    set(handles.CheckZoom,'BackgroundColor',[0.7 0.7 0.7])
3373    set(handles.edit_vect,'Value',0)
3374    edit_vect_Callback(hObject, eventdata, handles)
3375    set(handles.edit_object,'Value',0)
3376    set(handles.edit_object,'BackgroundColor',[0.7 0.7 0.7])
3377%     set(handles.cal,'Value',0)
3378%     set(handles.cal,'BackgroundColor',[0 1 0])
3379    set(handles.edit_vect,'Value',0)
3380    edit_vect_Callback(hObject, eventdata, handles)
3381    %initiate set_object GUI
3382    data.Name='VOLUME';
3383    if isfield(UvData,'CoordType')
3384        data.CoordType=UvData.CoordType;
3385    end
3386    if isfield(UvData.Field,'CoordMesh')&~isempty(UvData.Field.CoordMesh)
3387        data.RangeX=[UvData.Field.XMin UvData.Field.XMax];
3388        data.RangeY=[UvData.Field.YMin UvData.Field.YMax];
3389        data.DX=UvData.Field.CoordMesh;
3390        data.DY=UvData.Field.CoordMesh;
3391    elseif isfield(UvData.Field,'AX')&isfield(UvData.Field,'AY')& isfield(UvData.Field,'A')%only image
3392        np=size(UvData.Field.A);
3393        meshx=(UvData.Field.AX(end)-UvData.Field.AX(1))/np(2);
3394        meshy=abs(UvData.Field.AY(end)-UvData.Field.AY(1))/np(1);
3395        data.RangeY=max(meshx,meshy);
3396        data.RangeX=max(meshx,meshy);
3397        data.DX=max(meshx,meshy);
3398    end
3399    data.ParentButton=handles.VOLUME;
3400    PlotHandles=get_plot_handles(handles);%get the handles of the interface elements setting the plotting parameters
3401    [hset_object,UvData.sethandles]=set_object(data,PlotHandles);% call the set_object interface with action on haxes,
3402                                                      % associate the set_object interface handle to the plotting axes
3403    set(hset_object,'Position',get(handles.uvmat,'Position')+UvData.OpenParam.PosSetObject)
3404    UvData.MouseAction='create_object';
3405else
3406    set(handles.VOLUME,'BackgroundColor',[0 1 0])
3407end
3408set(handles.uvmat,'UserData',UvData)
3409
3410%-------------------------------------------------------
3411function edit_vect_Callback(hObject, eventdata, handles)
3412%-------------------------------------------------------
3413%
3414if isequal(get(handles.edit_vect,'Value'),1)
3415    test_civ2=isequal(get(handles.civ2,'BackgroundColor'),[1 1 0]);
3416    test_civ1=isequal(get(handles.VelType,'BackgroundColor'),[1 1 0]);
3417    if ~test_civ2 && ~test_civ1
3418        msgbox_uvmat('ERROR','manual correction only possible for CIV1 or CIV2 velocity fields')
3419    end
3420    set(handles.record,'Visible','on')
3421    set(handles.edit_vect,'BackgroundColor',[1 1 0])
3422    set(handles.edit_object,'Value',0)
3423    set(handles.CheckZoom,'Value',0)
3424    set(handles.CheckZoom,'BackgroundColor',[0.7 0.7 0.7])
3425    set(handles.edit_object,'BackgroundColor',[0.7 0.7 0.7])
3426    set(gcf,'Pointer','arrow')
3427else
3428    set(handles.record,'Visible','off')
3429    set(handles.edit_vect,'BackgroundColor',[0.7 0.7 0.7])
3430end
3431
3432%----------------------------------------------
3433function save_mask_Callback(hObject, eventdata, handles)
3434%-----------------------------------------------------------------------
3435UvData=get(handles.uvmat,'UserData');
3436
3437flag=1;
3438npx=size(UvData.Field.A,2);
3439npy=size(UvData.Field.A,1);
3440xi=0.5:npx-0.5;
3441yi=0.5:npy-0.5;
3442[Xi,Yi]=meshgrid(xi,yi);
3443if isfield(UvData,'Object')
3444    for iobj=1:length(UvData.Object)
3445        ObjectData=UvData.Object{iobj};
3446        if isfield(ObjectData,'ProjMode') &&(isequal(ObjectData.ProjMode,'mask_inside')||isequal(ObjectData.ProjMode,'mask_outside'));
3447            flagobj=1;
3448            testphys=0; %coordinates in pixels by default
3449            if isfield(ObjectData,'CoordType') && isequal(ObjectData.CoordType,'phys')
3450                if isfield(UvData,'XmlData')&& isfield(UvData.XmlData{1},'GeometryCalib')
3451                    Calib=UvData.XmlData{1}.GeometryCalib;
3452                    testphys=1;
3453                end
3454            end
3455            if isfield(ObjectData,'Coord')& isfield(ObjectData,'Style')
3456                if isequal(ObjectData.Type,'polygon')
3457                    X=ObjectData.Coord(:,1);
3458                    Y=ObjectData.Coord(:,2);
3459                    if testphys
3460                        [X,Y]=px_XYZ(Calib,X,Y,0);% to generalise with 3D cases
3461                    end
3462                    flagobj=~inpolygon(Xi,Yi,X',Y');%=0 inside the polygon, 1 outside                 
3463                elseif isequal(ObjectData.Type,'ellipse')
3464                    if testphys
3465                        %[X,Y]=px_XYZ(Calib,X,Y,0);% TODO:create a polygon boundary and transform to phys
3466                    end
3467                    RangeX=max(ObjectData.RangeX);
3468                    RangeY=max(ObjectData.RangeY);
3469                    X2Max=RangeX*RangeX;
3470                    Y2Max=RangeY*RangeY;
3471                    distX=(Xi-ObjectData.Coord(1,1));
3472                    distY=(Yi-ObjectData.Coord(1,2));
3473                    flagobj=(distX.*distX/X2Max+distY.*distY/Y2Max)>1;
3474                elseif isequal(ObjectData.Type,'rectangle')
3475                    if testphys
3476                        %[X,Y]=px_XYZ(Calib,X,Y,0);% TODO:create a polygon boundary and transform to phys
3477                    end
3478                    distX=abs(Xi-ObjectData.Coord(1,1));
3479                    distY=abs(Yi-ObjectData.Coord(1,2));
3480                    flagobj=distX>max(ObjectData.RangeX) | distY>max(ObjectData.RangeY);
3481                end
3482                if isequal(ObjectData.ProjMode,'mask_outside')
3483                    flagobj=~flagobj;
3484                end
3485                flag=flag & flagobj;
3486            end
3487        end
3488    end
3489end
3490% flag=~flag;
3491%mask name
3492RootPath=get(handles.RootPath,'String');
3493RootFile=get(handles.RootFile,'String');
3494RootFile=regexprep(RootFile,'\<[\\/]|[\\/]\>','');%suppress possible / or \ separator at the beginning or the end of the string
3495filebase=fullfile(RootPath,RootFile);
3496list=get(handles.masklevel,'String');
3497masknumber=num2str(length(list));
3498maskindex=get(handles.masklevel,'Value');
3499mask_name=fullfile_uvmat(RootPath,SubDir,[RootFile '_' masknumber 'mask'],'.png','_1',maskindex);
3500%mask_name=name_generator([filebase '_' masknumber 'mask'],maskindex,1,'.png','_i');
3501imflag=uint8(255*(0.392+0.608*flag));% =100 for flag=0 (vectors not computed when 20<imflag<200)
3502imflag=flipdim(imflag,1);
3503% imflag=uint8(255*flag);% =0 for flag=0 (vectors=0 when 20<imflag<200)
3504msgbox_uvmat('CONFIRMATION',[mask_name ' saved'])
3505imwrite(imflag,mask_name,'BitDepth',8);
3506
3507%display the mask
3508figure;
3509vec=linspace(0,1,256);%define a linear greyscale colormap
3510map=[vec' vec' vec'];
3511colormap(map)
3512
3513image(imflag);
3514
3515%-------------------------------------------------------------------
3516%-------------------------------------------------------------------
3517%  - FUNCTIONS FOR SETTING PLOTTING PARAMETERS
3518
3519%------------------------------------------------------------------
3520%-------------------------------------------------------------
3521% --- Executes on selection change in transform_fct.
3522
3523function transform_fct_Callback(hObject, eventdata, handles)
3524%-------------------------------------------------------------
3525UvData=get(handles.uvmat,'UserData');
3526menu=get(handles.transform_fct,'String');refresh
3527ichoice=get(handles.transform_fct,'Value');%item number in the menu
3528transform_name=menu{ichoice};% choice of the transform fct
3529list_path=get(handles.transform_fct,'UserData');
3530
3531%% add a new item to the menu if the option 'more...' has been selected
3532prev_path=fullfile(get(handles.path_transform,'String'));
3533if ~exist(prev_path,'dir')
3534    prev_path=fullfile(fileparts(which('uvmat')),'transform_field');
3535end
3536if strcmp(transform_name,'more...');
3537    [FileName, PathName] = uigetfile( ...
3538        {'*.m', ' (*.m)';
3539        '*.m',  '.m files '; ...
3540        '*.*', 'All Files (*.*)'}, ...
3541        'Pick the transform function', prev_path);
3542    if ~ischar(FileName),return,end %abandon if the browser is cancelled
3543    path_transform_fct =fullfile(PathName,FileName);
3544    if isempty(regexp(FileName,'\.m$'))% detect file extension .m
3545        msgbox_uvmat('ERROR','a Matlab function .m must be introduced');
3546        return
3547    else
3548        transform_name=regexprep(FileName,'\.m','');
3549    end
3550    ichoice=find(strcmp(transform_name,menu),1);%look for the selected fct in the existing menu
3551    if isempty(ichoice)% if the item is not found, add it to the menu (before 'more...' and select it)
3552        menu=[menu(1:end-1);{transform_name};{'more...'}];
3553        ichoice=numel(menu)-1;   
3554    end
3555    list_path{ichoice}=PathName;%update the list fo fct paths
3556    set(handles.transform_fct,'String',menu)
3557    set(handles.transform_fct,'Value',ichoice)
3558   
3559    % save the new menu in the personal file 'uvmat_perso.mat'
3560    dir_perso=prefdir;%personal Matalb directory
3561    profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
3562    if exist(profil_perso,'file')
3563        nb_builtin=UvData.OpenParam.NbBuiltin;% number of 'builtin' (basic) transform fcts in uvmat
3564        if nb_builtin<numel(list_path)
3565        for ilist=nb_builtin+1:numel(list_path)
3566            transform_fct{ilist-nb_builtin}=[fullfile(list_path{ilist},menu{ilist}) '.m'];
3567        end
3568        save (profil_perso,'transform_fct','-append'); %store the root name for future opening of uvmat
3569        end
3570    end
3571end
3572
3573%% create the function handle of the selected fct
3574
3575if isempty(list_path{ichoice})% case of no selected fct
3576    transform_handle=[];
3577else
3578    if ~exist(list_path{ichoice},'dir')
3579        msgbox_uvmat('ERROR','invalid fct path: select the transform fct again with the option more...')
3580        return
3581    end
3582    current_dir=pwd;%current working dir
3583    cd(list_path{ichoice})
3584    transform_handle=str2func(transform_name);
3585    cd(current_dir)
3586end
3587set(handles.path_transform,'String',list_path{ichoice})
3588set(handles.path_transform,'UserData',transform_handle)
3589set(handles.transform_fct,'UserData',list_path)
3590
3591%% update the ToolTip string of the menu transform_fct with the first line of the selected fct file
3592if isempty(list_path{ichoice})% case of no selected fct
3593    set(handles.transform_fct,'ToolTipString','transform_fct:choose a transform function')
3594else
3595    try
3596        [fid,errormsg] =fopen([fullfile(list_path{ichoice},transform_name) '.m']);
3597        InputText=textscan(fid,'%s',1,'delimiter','\n');
3598        fclose(fid)
3599        set(handles.transform_fct,'ToolTipString',['transform_fct: ' InputText{1}{1}])% put the first line of the selected function as tooltip help
3600    end
3601end
3602
3603%% adapt the GUI to the input/output conditions of the selected transform fct
3604DataOut=[];
3605CoordUnit='';
3606CoordUnitPrev='';
3607if isfield(UvData,'Field')&&isfield(UvData.Field,'CoordUnit')
3608    CoordUnitPrev=UvData.Field.CoordUnit;
3609end
3610if ~isempty(list_path{ichoice})
3611    if nargin(transform_handle)>1 && isfield(UvData,'XmlData')&&~isempty(UvData.XmlData)
3612        XmlData=UvData.XmlData{1};
3613        DataOut=feval(transform_handle,'*',XmlData);
3614        if isfield(DataOut,'CoordUnit')
3615            CoordUnit=DataOut.CoordUnit;
3616        end
3617        if isfield(DataOut,'InputFieldType')
3618            UvData.InputFieldType=DataOut.InputFieldType;
3619        end
3620    else
3621        DataOut=feval(transform_handle,'*');
3622    end
3623end
3624
3625
3626%% delete drawn objects if the output CooordUnit is different from the previous one
3627if ~strcmp(CoordUnit,CoordUnitPrev)
3628    set(handles.CheckFixLimits,'Value',0)
3629set(handles.CheckFixLimits,'BackgroundColor',[0.7 0.7 0.7])
3630    hother=findobj('Tag','proj_object');%find all the proj objects
3631    for iobj=1:length(hother)
3632        delete_object(hother(iobj))
3633    end
3634    hother=findobj('Tag','DeformPoint');%find all the proj objects
3635    for iobj=1:length(hother)
3636        delete_object(hother(iobj))
3637    end
3638    hh=findobj('Tag','calib_points');
3639    if ~isempty(hh)
3640        delete(hh)
3641    end
3642    hhh=findobj('Tag','calib_marker');
3643    if ~isempty(hhh)
3644        delete(hhh)
3645    end
3646%     if isfield(UvData,'Object')
3647%         UvData.Object=UvData.Object(1);
3648%     end
3649    set(handles.ListObject,'Value',1)
3650    set(handles.ListObject,'String',{''})
3651    set(handles.ListObject_1,'Value',1)
3652    set(handles.ListObject_1,'String',{''})
3653    set(handles.ViewObject,'value',0)
3654    ViewObject_Callback(hObject, eventdata, handles)
3655    set(handles.ViewField,'value',0)
3656    ViewField_Callback(hObject, eventdata, handles)
3657    set(handles.edit_object,'Value',0)
3658    edit_object_Callback(hObject, eventdata, handles)
3659    UvData.Object={[]};
3660end
3661set(handles.uvmat,'UserData',UvData)
3662
3663%% refresh the current plot
3664if isempty(list_path{ichoice}) || nargin(transform_handle)<3
3665    set(handles.SubField,'Value',0)
3666    SubField_Callback(hObject, eventdata, handles)
3667else
3668    run0_Callback(hObject, eventdata, handles)
3669end
3670
3671%------------------------------------------------
3672%CALLBACKS FOR PLOTTING PARAMETERS
3673%-------------------------------------------------
3674%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3675% Plot coordinates
3676%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3677%------------------------------------------------------------------------
3678function num_MinX_Callback(hObject, eventdata, handles)
3679%------------------------------------------------------------------------
3680set(handles.CheckFixLimits,'Value',1) %suppress auto mode
3681set(handles.CheckFixLimits,'BackgroundColor',[1 1 0])
3682update_plot(handles);
3683
3684%------------------------------------------------------------------------
3685function num_MaxX_Callback(hObject, eventdata, handles)
3686%------------------------------------------------------------------------
3687set(handles.CheckFixLimits,'Value',1) %suppress auto mode
3688set(handles.CheckFixLimits,'BackgroundColor',[1 1 0])
3689update_plot(handles);
3690
3691%------------------------------------------------------------------------
3692function num_MinY_Callback(hObject, eventdata, handles)
3693%------------------------------------------
3694set(handles.CheckFixLimits,'Value',1) %suppress auto mode
3695set(handles.CheckFixLimits,'BackgroundColor',[1 1 0])
3696update_plot(handles);
3697
3698%------------------------------------------------------------------------
3699function num_MaxY_Callback(hObject, eventdata, handles)
3700%------------------------------------------------------------------------
3701set(handles.CheckFixLimits,'Value',1) %suppress auto mode
3702set(handles.CheckFixLimits,'BackgroundColor',[1 1 0])
3703update_plot(handles);
3704
3705%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3706% Scalar or image representation
3707%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3708%------------------------------------------------------------------------
3709function num_MinA_Callback(hObject, eventdata, handles)
3710%------------------------------------------
3711set(handles.CheckFixScalar,'Value',1) %suppress auto mode
3712set(handles.CheckFixScalar,'BackgroundColor',[1 1 0])
3713MinA=str2double(get(handles.num_MinA,'String'));
3714MaxA=str2double(get(handles.num_MaxA,'String'));
3715if MinA>MaxA% switch minA and maxA in case of error
3716    MinA_old=MinA;
3717    MinA=MaxA;
3718    MaxA=MinA_old;
3719    set(handles.num_MinA,'String',num2str(MinA,5));
3720    set(handles.num_MaxA,'String',num2str(MaxA,5));
3721end
3722update_plot(handles);
3723
3724%------------------------------------------------------------------------
3725function num_MaxA_Callback(hObject, eventdata, handles)
3726%------------------------------------------------------------------------
3727set(handles.CheckFixScalar,'Value',1) %suppress auto mode
3728set(handles.CheckFixScalar,'BackgroundColor',[1 1 0])
3729MinA=str2double(get(handles.num_MinA,'String'));
3730MaxA=str2double(get(handles.num_MaxA,'String'));
3731if MinA>MaxA% switch minA and maxA in case of error
3732        MinA_old=MinA;
3733    MinA=MaxA;
3734    MaxA=MinA_old;
3735    set(handles.num_MinA,'String',num2str(MinA,5));
3736    set(handles.num_MaxA,'String',num2str(MaxA,5));
3737end
3738update_plot(handles);
3739
3740%------------------------------------------------------------------------
3741function CheckFixScalar_Callback(hObject, eventdata, handles)
3742%------------------------------------------------------------------------
3743test=get(handles.CheckFixScalar,'Value');
3744if test
3745    set(handles.CheckFixScalar,'BackgroundColor',[1 1 0])
3746else
3747    set(handles.CheckFixScalar,'BackgroundColor',[0.7 0.7 0.7])
3748    update_plot(handles);
3749end
3750
3751%-------------------------------------------------------------------
3752function CheckBW_Callback(hObject, eventdata, handles)
3753%-------------------------------------------------------------------
3754update_plot(handles);
3755
3756%-------------------------------------------------------------------
3757function num_Opacity_Callback(hObject, eventdata, handles)
3758update_plot(handles);
3759%-------------------------------------------------------------------
3760
3761%-------------------------------------------------------------------
3762function ListContour_Callback(hObject, eventdata, handles)
3763%-------------------------------------------------------------------
3764val=get(handles.ListContour,'Value');
3765if val==2
3766    set(handles.interval_txt,'Visible','on')
3767    set(handles.num_IncrA,'Visible','on')
3768else
3769    set(handles.interval_txt,'Visible','off')
3770    set(handles.num_IncrA,'Visible','off')
3771end
3772update_plot(handles);
3773
3774%-------------------------------------------------------------------
3775function num_IncrA_Callback(hObject, eventdata, handles)
3776%-------------------------------------------------------------------
3777update_plot(handles);
3778
3779
3780%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3781% Vector representation
3782%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3783%-------------------------------------------------------------------
3784function CheckHideWarning_Callback(hObject, eventdata, handles)
3785%-------------------------------------------------------------------
3786update_plot(handles);
3787
3788%-------------------------------------------------------------------
3789function CheckHideFalse_Callback(hObject, eventdata, handles)
3790%-------------------------------------------------------------------
3791update_plot(handles);
3792
3793%-------------------------------------------------------------------
3794function num_VecScale_Callback(hObject, eventdata, handles)
3795%-------------------------------------------------------------------
3796set(handles.CheckFixVectors,'Value',1);
3797set(handles.CheckFixVectors,'BackgroundColor',[1 1 0])
3798update_plot(handles);
3799
3800%-------------------------------------------------------------------
3801function CheckFixVectors_Callback(hObject, eventdata, handles)
3802%-------------------------------------------------------------------
3803test=get(handles.CheckFixVectors,'Value');
3804if test
3805    set(handles.CheckFixVectors,'BackgroundColor',[1 1 0])
3806else
3807    update_plot(handles);
3808    %set(handles.num_VecScale,'String',num2str(ScalOut.num_VecScale,3))
3809    set(handles.CheckFixVectors,'BackgroundColor',[0.7 0.7 0.7])
3810end
3811
3812%------------------------------------------------------------------------
3813% --- Executes on selection change in CheckDecimate4 (nb_vec/4).
3814function CheckDecimate4_Callback(hObject, eventdata, handles)
3815%------------------------------------------------------------------------
3816update_plot(handles);
3817
3818%------------------------------------------------------------------------
3819% --- Executes on selection change in ColorCode menu
3820function ColorCode_Callback(hObject, eventdata, handles)
3821%------------------------------------------------------------------------
3822% edit the choice for color code
3823update_color_code_boxes(handles);
3824update_plot(handles);
3825
3826%------------------------------------------------------------------------
3827function update_color_code_boxes(handles)
3828%------------------------------------------------------------------------
3829list_code=get(handles.ColorCode,'String');% list menu fields
3830colcode= list_code{get(handles.ColorCode,'Value')}; % selected field
3831enable_slider='off';%default
3832enable_bounds='off';%default
3833enable_scalar='off';%default
3834switch colcode
3835    case {'rgb','bgr'}
3836        enable_slider='on';
3837        enable_bounds='on';
3838        enable_scalar='on';
3839    case '64 colors'
3840        enable_bounds='on';
3841        enable_scalar='on';
3842end
3843set(handles.Slider1,'Visible',enable_slider)
3844set(handles.Slider2,'Visible', enable_slider)
3845set(handles.num_ColCode1,'Visible',enable_slider)
3846set(handles.num_ColCode2,'Visible',enable_slider)
3847set(handles.TitleColCode1,'Visible',enable_slider)
3848set(handles.TitleColCode2,'Visible',enable_slider)
3849set(handles.CheckFixVecColor,'Visible',enable_bounds)
3850set(handles.num_MinVec,'Visible',enable_bounds)
3851set(handles.num_MaxVec,'Visible',enable_bounds)
3852set(handles.ColorScalar,'Visible',enable_scalar)
3853set_vec_col_bar(handles)
3854
3855%------------------------------------------------------------------
3856% --- Executes on selection change in ColorScalar: choice of the color code.
3857function ColorScalar_Callback(hObject, eventdata, handles)
3858%------------------------------------------------------------------
3859% edit the choice for color code
3860list_scalar=get(handles.ColorScalar,'String');% list menu fields
3861col_scalar= list_scalar{get(handles.ColorScalar,'Value')}; % selected field
3862if isequal(col_scalar,'ima_cor')
3863    set(handles.CheckFixVecColor,'Value',1)%fixed scale by default
3864    ColorCode='rgb';
3865    set(handles.num_MinVec,'String','0')
3866    set(handles.num_MaxVec,'String','1')
3867    set(handles.num_ColCode1,'String','0.333')
3868    set(handles.num_ColCode2,'String','0.666')
3869else
3870    set(handles.CheckFixVecColor,'Value',0)%auto scale between min,max by default
3871    ColorCode='64 colors';
3872end
3873ColorCodeList=get(handles.ColorCode,'String');
3874ichoice=find(strcmp(ColorCode,ColorCodeList),1);
3875set(handles.ColorCode,'Value',ichoice)% set color code in the menu
3876
3877update_color_code_boxes(handles);
3878%replot the current graph
3879run0_Callback(hObject, eventdata, handles)
3880
3881%----------------------------------------------------------------
3882% -- Executes on slider movement to set the color code
3883%
3884function Slider1_Callback(hObject, eventdata, handles)
3885%------------------------------------------------------------------
3886slider1=get(handles.Slider1,'Value');
3887min_val=str2num(get(handles.num_MinVec,'String'));
3888max_val=str2num(get(handles.num_MaxVec,'String'));
3889col=min_val+(max_val-min_val)*slider1;
3890set(handles.num_ColCode1,'String',num2str(col))
3891if(get(handles.Slider2,'Value') < col)%move also the second slider at the same value if needed
3892    set(handles.Slider2,'Value',col)
3893    set(handles.num_ColCode2,'String',num2str(col))
3894end
3895set_vec_col_bar(handles)
3896update_plot(handles);
3897
3898%----------------------------------------------------------------
3899% Executes on slider movement to set the color code
3900%----------------------------------------------------------------
3901function Slider2_Callback(hObject, eventdata, handles)
3902slider2=get(handles.Slider2,'Value');
3903min_val=str2num(get(handles.num_MinVec,'String'));
3904max_val=str2num(get(handles.num_MaxVec,'String'));
3905col=min_val+(max_val-min_val)*slider2;
3906set(handles.num_ColCode2,'String',num2str(col))
3907if(get(handles.Slider1,'Value') > col)%move also the first slider at the same value if needed
3908    set(handles.Slider1,'Value',col)
3909    set(handles.num_ColCode1,'String',num2str(col))
3910end
3911set_vec_col_bar(handles)
3912update_plot(handles);
3913
3914%----------------------------------------------------------------
3915% --- Execute on return carriage on the edit box corresponding to slider 1
3916%----------------------------------------------------------------
3917function num_ColCode1_Callback(hObject, eventdata, handles)
3918set_vec_col_bar(handles)
3919update_plot(handles);
3920
3921%----------------------------------------------------------------
3922% --- Execute on return carriage on the edit box corresponding to slider 2
3923%----------------------------------------------------------------
3924function num_ColCode2_Callback(hObject, eventdata, handles)
3925set_vec_col_bar(handles)
3926update_plot(handles);
3927%------------------------------------------------------------------------
3928%-------------------------------------------------------
3929% --- Executes on button press in CheckFixVecColor.
3930%-------------------------------------------------------
3931function VecColBar_Callback(hObject, eventdata, handles)
3932set_vec_col_bar(handles)
3933
3934%------------------------------------------------------------------------
3935% --- Executes on button press in CheckFixVecColor.
3936function CheckFixVecColor_Callback(hObject, eventdata, handles)
3937%------------------------------------------------------------------------
3938if ~get(handles.CheckFixVecColor,'Value')
3939    update_plot(handles);
3940end
3941
3942%------------------------------------------------------------------------
3943% --- Executes on selection change in num_MaxVec.
3944function num_MinVec_Callback(hObject, eventdata, handles)
3945%------------------------------------------------------------------------
3946max_vec_Callback(hObject, eventdata, handles)
3947
3948%------------------------------------------------------------------------
3949% --- Executes on selection change in num_MaxVec.
3950function num_MaxVec_Callback(hObject, eventdata, handles)
3951%------------------------------------------------------------------------
3952set(handles.CheckFixVecColor,'Value',1)
3953CheckFixVecColor_Callback(hObject, eventdata, handles)
3954min_val=str2num(get(handles.num_MinVec,'String'));
3955max_val=str2num(get(handles.num_MaxVec,'String'));
3956slider1=get(handles.Slider1,'Value');
3957slider2=get(handles.Slider2,'Value');
3958colcode1=min_val+(max_val-min_val)*slider1;
3959colcode2=min_val+(max_val-min_val)*slider2;
3960set(handles.num_ColCode1,'String',num2str(colcode1))
3961set(handles.num_ColCode2,'String',num2str(colcode2))
3962update_plot(handles);
3963
3964%------------------------------------------------------------------------
3965% --- update the display of color code for vectors (on vecColBar)
3966function set_vec_col_bar(handles)
3967%------------------------------------------------------------------------
3968%get the image of the color display button 'VecColBar' in pixels
3969set(handles.VecColBar,'Unit','pixel');
3970pos_vert=get(handles.VecColBar,'Position');
3971set(handles.VecColBar,'Unit','Normalized');
3972width=ceil(pos_vert(3));
3973height=ceil(pos_vert(4));
3974
3975%get slider indications
3976list=get(handles.ColorCode,'String');
3977ichoice=get(handles.ColorCode,'Value');
3978colcode.ColorCode=list{ichoice};
3979colcode.MinVec=str2num(get(handles.num_MinVec,'String'));
3980colcode.MaxVec=str2num(get(handles.num_MaxVec,'String'));
3981test3color=strcmp(colcode.ColorCode,'rgb') || strcmp(colcode.ColorCode,'bgr');
3982if test3color
3983    colcode.ColCode1=str2num(get(handles.num_ColCode1,'String'));
3984    colcode.ColCode2=str2num(get(handles.num_ColCode2,'String'));
3985end
3986vec_C=colcode.MinVec+(colcode.MaxVec-colcode.MinVec)*(0.5:width-0.5)/width;%sample of vec_C values from min to max
3987[colorlist,col_vec]=set_col_vec(colcode,vec_C);
3988oneheight=ones(1,height);
3989A1=colorlist(col_vec,1)*oneheight;
3990A2=colorlist(col_vec,2)*oneheight;
3991A3=colorlist(col_vec,3)*oneheight;
3992A(:,:,1)=A1';
3993A(:,:,2)=A2';
3994A(:,:,3)=A3';
3995set(handles.VecColBar,'Cdata',A)
3996
3997%-------------------------------------------------------------------
3998function update_plot(handles)
3999%-------------------------------------------------------------------
4000UvData=get(handles.uvmat,'UserData');
4001AxeData=UvData.PlotAxes;% retrieve the current plotted data
4002PlotParam=read_GUI(handles.uvmat);
4003[tild,PlotParamOut]= plot_field(AxeData,handles.PlotAxes,PlotParam);
4004write_plot_param(handles,PlotParamOut); %update the auto plot parameters
4005
4006%------------------------------------------------------------------------
4007%------------------------------------------------------------------------
4008%   SELECTION AND EDITION OF PROJECTION OBJECTS
4009%------------------------------------------------------------------------
4010%------------------------------------------------------------------------
4011
4012% --- Executes on selection change in ListObject_1.
4013function ListObject_1_Callback(hObject, eventdata, handles)
4014list_str=get(handles.ListObject,'String');
4015UvData=get(handles.uvmat,'UserData');
4016ObjectData=UvData.Object{get(handles.ListObject_1,'Value')};
4017
4018%% update the projection plot on uvmat
4019ProjData= proj_field(UvData.Field,ObjectData);%project the current input field on object ObjectData
4020plot_field(ProjData,handles.PlotAxes,read_GUI(handles.uvmat));% plot the projected field;
4021%replot all the objects within the new projected field
4022for IndexObj=1:numel(list_str)
4023        hobject=UvData.Object{IndexObj}.DisplayHandle.uvmat;
4024        if isempty(hobject) || ~ishandle(hobject)
4025            hobject=handles.PlotAxes;
4026        end
4027        if isequal(IndexObj,get(handles.ListObject,'Value'))
4028            objectcolor='m'; %paint in magenta the currently selected object in ListObject
4029        else
4030            objectcolor='b';
4031        end
4032        UvData.Object{IndexObj}.DisplayHandle.uvmat=plot_object(UvData.Object{IndexObj},ObjectData,hobject,objectcolor);%draw the object in uvmat     
4033end
4034set(handles.uvmat,'UserData',UvData)
4035
4036%% display the object parameters if the GUI set_object is already opened
4037if ~get(handles.ViewObject,'Value')
4038    ZBounds=0; % default
4039    if isfield(UvData.Field,'ZMin') && isfield(UvData.Field,'ZMax')
4040        ZBounds(1)=UvData.Field.ZMin; %minimum for the Z slider
4041        ZBounds(2)=UvData.Field.ZMax;%maximum for the Z slider
4042    end
4043    ObjectData.Name=list_str{get(handles.ListObject_1,'Value')};
4044    set_object(ObjectData,[],ZBounds);
4045    set(handles.ViewObject,'Value',1)% show that the selected object in ListObject_1 is currently visualised
4046end
4047
4048%  desactivate the edit object mode
4049set(handles.edit_object,'Value',0)
4050set(handles.edit_object,'BackgroundColor',[0.7,0.7,0.7])
4051
4052%------------------------------------------------------------------------
4053% --- Executes on selection change in ListObject.
4054function ListObject_Callback(hObject, eventdata, handles)
4055%------------------------------------------------------------------------
4056list_str=get(handles.ListObject,'String');
4057IndexObj=get(handles.ListObject,'Value');%present object selection
4058
4059%% The object  is displayed in set_object if this GUI is already opened
4060UvData=get(handles.uvmat,'UserData');
4061ObjectData=UvData.Object{IndexObj};
4062hset_object=findobj(allchild(0),'tag','set_object');
4063if ~isempty(hset_object)
4064    ZBounds=0; % default
4065    if isfield(UvData.Field,'ZMin') && isfield(UvData.Field,'ZMax')
4066        ZBounds(1)=UvData.Field.ZMin; %minimum for the Z slider
4067        ZBounds(2)=UvData.Field.ZMax;%maximum for the Z slider
4068    end
4069    ObjectData.Name=list_str{IndexObj};
4070    set_object(ObjectData,[],ZBounds);
4071    set(handles.ViewField,'Value',1)% show that the selected object in ListObject is currently visualised
4072end
4073
4074%%  desactivate the edit object mode
4075set(handles.edit_object,'Value',0)
4076set(handles.edit_object,'BackgroundColor',[0.7,0.7,0.7])
4077
4078%% update the  plot on view_field if view_field is already openened
4079hview_field=findobj(allchild(0),'tag','view_field');
4080if isempty(hview_field)
4081    hhview_field.PlotAxes=[];
4082else
4083    Data=get(hview_field,'UserData');
4084    hhview_field=guidata(hview_field);
4085    ProjData= proj_field(UvData.Field,ObjectData);%project the current interface field on ObjectData
4086    [PlotType,PlotParam]=plot_field(ProjData,hhview_field.PlotAxes,read_GUI(hview_field));%read plotting parameters on the uvmat interfachhview_fiel
4087    write_plot_param(hhview_field,PlotParam); %update the display of plotting parameters for the current object
4088    haxes=findobj(hview_field,'tag','axes3');
4089    pos=get(hview_field,'Position'); 
4090    if strcmp(get(haxes,'Visible'),'off')%sempty(PlotParam.Coordinates)% case of no plot display (pure text table)
4091        h_TableDisplay=findobj(hview_field,'tag','TableDisplay');
4092        pos_table=get(h_TableDisplay,'Position');
4093        set(hview_field,'Position',[pos(1)+pos(3)-pos_table(3) pos(2)+pos(4)-pos_table(4) pos_table(3) pos_table(4)])
4094        drawnow% needed to change position before the next command
4095        set(hview_field,'UserData',Data);% restore the previously stored GUI position after GUI resizing
4096    else
4097        set(hview_field,'Position',Data.GUISize)% return to the previously stored GUI position and size
4098    end
4099end
4100
4101%% update the color of the graphic object representation: the selected object in magenta, others in blue
4102update_object_color(handles.PlotAxes,hhview_field.PlotAxes,UvData.Object{IndexObj}.DisplayHandle.uvmat)
4103
4104%------------------------------------------------------------------------
4105%--- update the color representation of objects (indicating the selected ones)
4106function update_object_color(axes_uvmat,axes_view_field,DisplayHandle)
4107%------------------------------------------------------------------------
4108if isempty(axes_view_field)% case with no view_field plot
4109hother=[findobj(axes_uvmat,'Tag','proj_object');findobj(axes_uvmat,'Tag','DeformPoint')];%find all the proj object and deform point representations
4110else
4111hother=[findobj(axes_uvmat,'Tag','proj_object') ;findobj(axes_view_field,'Tag','proj_object');... %find all the proj object representations
4112findobj(axes_uvmat,'Tag','DeformPoint'); findobj(axes_view_field,'Tag','DeformPoint')];%find all the deform point representations
4113end
4114for iobj=1:length(hother)
4115    if isequal(get(hother(iobj),'Type'),'rectangle')||isequal(get(hother(iobj),'Type'),'patch')
4116        set(hother(iobj),'EdgeColor','b')
4117        if isequal(get(hother(iobj),'FaceColor'),'m')
4118            set(hother(iobj),'FaceColor','b')
4119        end
4120    elseif isequal(get(hother(iobj),'Type'),'image')
4121        Acolor=get(hother(iobj),'CData');
4122        Acolor(:,:,1)=zeros(size(Acolor,1),size(Acolor,2));
4123        set(hother(iobj),'CData',Acolor);
4124    else
4125        set(hother(iobj),'Color','b')
4126    end
4127    set(hother(iobj),'Selected','off')
4128end
4129if ishandle(DisplayHandle)
4130    linetype=get(DisplayHandle,'Type');
4131    if isequal(linetype,'line')
4132        set(DisplayHandle,'Color','m'); %set the selected object to magenta color
4133    elseif isequal(linetype,'rectangle')
4134        set(DisplayHandle,'EdgeColor','m'); %set the selected object to magenta color
4135    elseif isequal(linetype,'patch')
4136        set(DisplayHandle,'FaceColor','m'); %set the selected object to magenta color
4137    end
4138    SubObjectData=get(DisplayHandle,'UserData');
4139    if isfield(SubObjectData,'SubObject') & ishandle(SubObjectData.SubObject)
4140        for iobj=1:length(SubObjectData.SubObject)
4141            hsub=SubObjectData.SubObject(iobj);
4142            if isequal(get(hsub,'Type'),'rectangle')
4143                set(hsub,'EdgeColor','m'); %set the selected object to magenta color
4144            elseif isequal(get(hsub,'Type'),'image')
4145                Acolor=get(hsub,'CData');
4146                Acolor(:,:,1)=Acolor(:,:,3);
4147                set(hsub,'CData',Acolor);
4148            else
4149                set(hsub,'Color','m')
4150            end
4151        end
4152    end
4153    if isfield(SubObjectData,'DeformPoint') & ishandle(SubObjectData.DeformPoint)
4154        set(SubObjectData.DeformPoint,'Color','m')
4155    end
4156end
4157
4158%-------------------------------------------------------------------
4159% --- Executes on selection change in edit_object.
4160function edit_object_Callback(hObject, eventdata, handles)
4161%-------------------------------------------------------------------
4162hset_object=findobj(allchild(0),'Tag','set_object');
4163if get(handles.edit_object,'Value')
4164    set(handles.edit_object,'BackgroundColor',[1,1,0]) 
4165    %suppress the other options
4166    set(handles.CheckZoom,'Value',0)
4167    CheckZoom_Callback(hObject, eventdata, handles)
4168    hgeometry_calib=findobj(allchild(0),'tag','geometry_calib');
4169    if ishandle(hgeometry_calib)
4170        hhgeometry_calib=guidata(hgeometry_calib);
4171        set(hhgeometry_calib.edit_append,'Value',0)% desactivate mouse action in geometry_calib
4172        set(hhgeometry_calib.edit_append,'BackgroundColor',[0.7 0.7 0.7])
4173    end
4174    set(handles.ViewObject,'value',1)
4175    ViewObject_Callback(hObject, eventdata, handles)
4176else % desctivate object edit mode
4177    set(handles.edit_object,'BackgroundColor',[0.7,0.7,0.7]) 
4178    if ~isempty(hset_object)% open the
4179        hhset_object=guidata(hset_object);
4180        set(hhset_object.PLOT,'enable','off');
4181        set(get(hset_object,'children'),'enable','inactive')
4182    end
4183end
4184
4185
4186%------------------------------------------------------------------------
4187% --- Executes on button press in ViewObject.
4188function ViewObject_Callback(hObject, eventdata, handles)
4189%------------------------------------------------------------------------
4190check_view=get(handles.ViewObject,'Value');
4191
4192if check_view %activate set_object   
4193    IndexObj=get(handles.ListObject,'Value');
4194    list_object=get(handles.ListObject,'String');
4195    UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
4196    UvData.Object{IndexObj}.Name=list_object{IndexObj};
4197    if numel(UvData.Object)<IndexObj;% error in UvData
4198        msgbox_uvmat('ERROR','invalid object list')
4199        return
4200    end
4201    ZBounds=0; % default
4202    if isfield(UvData.Field,'ZMin') && isfield(UvData.Field,'ZMax')
4203        ZBounds(1)=UvData.Field.ZMin; %minimum for the Z slider
4204        ZBounds(2)=UvData.Field.ZMax;%maximum for the Z slider
4205    end
4206%     set(handles.ListObject_1,'Value',IndexObj);%restore ListObject selection after set_object deletion
4207    data=UvData.Object{IndexObj};
4208    if ~isfield(data,'Type')% default plane
4209        data.Type='plane';
4210    end
4211%     if isfield(UvData,'Field')
4212%         Field=UvData.Field;
4213%         if isfield(UvData.Field,'Mesh')&&~isempty(UvData.Field.Mesh)
4214%             data.RangeX=[UvData.Field.XMin UvData.Field.XMax];
4215%             if strcmp(data.Type,'line')||strcmp(data.Type,'polyline')
4216%                 data.RangeY=UvData.Field.Mesh;
4217%             else
4218%                 data.RangeY=[UvData.Field.YMin UvData.Field.YMax];
4219%             end
4220%             data.DX=UvData.Field.Mesh;
4221%             data.DY=UvData.Field.Mesh;
4222%         end
4223%         if isfield(Field,'NbDim')&& isequal(Field.NbDim,3)
4224%             data.Coord=[0 0 0]; %default
4225%         end
4226%         if isfield(Field,'CoordUnit')
4227%             data.CoordUnit=Field.CoordUnit;
4228%         end
4229%     end
4230    hset_object=set_object(data,[],ZBounds);
4231    hhset_object=guidata(hset_object);
4232    if get(handles.edit_object,'Value')% edit mode
4233        set(hhset_object.PLOT,'Enable','on')
4234        set(get(hset_object,'children'),'enable','on')
4235    else
4236        set(hhset_object.PLOT,'Enable','off')
4237        set(get(hset_object,'children'),'enable','inactive')% deactivate the GUI except SAVE
4238        set(hhset_object.SAVE,'Enable','on')
4239    end
4240else
4241    hset_object=findobj(allchild(0),'tag','set_object');
4242    if ~isempty(hset_object)
4243        delete(hset_object)% delete existing version of set_object
4244    end
4245end
4246 
4247
4248%------------------------------------------------------------------------
4249% --- Executes on button press in ViewField.
4250function ViewField_Callback(hObject, eventdata, handles)
4251%------------------------------------------------------------------------
4252check_view=get(handles.ViewField,'Value');
4253
4254if check_view
4255    IndexObj=get(handles.ListObject,'Value');
4256    UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
4257    if numel(UvData.Object)<IndexObj(end);% error in UvData
4258        msgbox_uvmat('ERROR','invalid object list')
4259        return
4260    end
4261    ZBounds=0; % default
4262    if isfield(UvData.Field,'ZMin') && isfield(UvData.Field,'ZMax')
4263        ZBounds(1)=UvData.Field.ZMin; %minimum for the Z slider
4264        ZBounds(2)=UvData.Field.ZMax;%maximum for the Z slider
4265    end
4266    set(handles.ListObject,'Value',IndexObj);%restore ListObject selection after set_object deletion
4267    if ~isfield(UvData.Object{IndexObj(1)},'Type')% default plane
4268        UvData.Object{IndexObj(1)}.Type='plane';
4269    end
4270    list_object=get(handles.ListObject,'String');
4271    UvData.Object{IndexObj(end)}.Name=list_object{IndexObj(end)};
4272   
4273    %% show the projection of the selected object on view_field
4274    ProjData= proj_field(UvData.Field,UvData.Object{IndexObj});%project the current field on ObjectData
4275    hview_field=findobj(allchild(0),'tag','view_field');
4276    if isempty(hview_field)
4277        hview_field=view_field;
4278    end
4279    hhview_field=guidata(hview_field);
4280    [PlotType,PlotParam]=plot_field(ProjData,hhview_field.PlotAxes,read_GUI(hview_field));%read plotting parameters on the GUI view_field);
4281    write_plot_param(hhview_field,PlotParam); %update the display of plotting parameters for the current object
4282    haxes=findobj(hview_field,'tag','axes3');
4283    pos=get(hview_field,'Position');
4284    if strcmp(get(haxes,'Visible'),'off')%sempty(PlotParam.Coordinates)% case of no plot display (pure text table)
4285        h_TableDisplay=findobj(hview_field,'tag','TableDisplay');
4286        pos_table=get(h_TableDisplay,'Position');
4287        set(hview_field,'Position',[pos(1)+pos(3)-pos_table(3) pos(2)+pos(4)-pos_table(4) pos_table(3) pos_table(4)])
4288    else
4289        Data=get(hview_field,'UserData');
4290        set(hview_field,'Position',Data.GUISize)% restore the size of view_field for plots
4291    end
4292else
4293    hview_field=findobj(allchild(0),'tag','view_field');
4294    if ~isempty(hview_field)
4295        delete(hview_field)% delete existing version of set_object
4296    end
4297end
4298
4299
4300%------------------------------------------------------------------------
4301% --- Executes on button press in delete_object.
4302function delete_object_Callback(hObject, eventdata, handles)
4303%------------------------------------------------------------------------
4304IndexObj=get(handles.ListObject,'Value');%projection object selected for view_field
4305IndexObj_1=get(handles.ListObject_1,'Value');%projection object selected for uvmat plot
4306if IndexObj>1 && ~isequal(IndexObj,IndexObj_1) % do not delete the object used for the uvmat plot
4307    delete_object(IndexObj)
4308end
4309
4310%------------------------------------------------------------------------
4311%------------------------------------------------------------------------
4312%  II - TOOLS FROM THE UPPER MENU BAR
4313%------------------------------------------------------------------------
4314%------------------------------------------------------------------------
4315
4316%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4317% Export  Menu Callbacks
4318%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4319%------------------------------------------------------------------------
4320% --- Executes on button press in Menu/Export/field in workspace.
4321function MenuExportField_Callback(hObject, eventdata, handles)
4322%------------------------------------------------------------------------
4323global Data_uvmat
4324Data_uvmat=get(handles.uvmat,'UserData');
4325evalin('base','global Data_uvmat')%make CurData global in the workspace
4326display('current field :')
4327evalin('base','Data_uvmat') %display CurData in the workspace
4328commandwindow; %brings the Matlab command window to the front
4329
4330%------------------------------------------------------------------------
4331% --- Executes on button press in Menu/Export/extract figure.
4332function MenuExportFigure_Callback(hObject, eventdata, handles)
4333%------------------------------------------------------------------------
4334% huvmat=get(handles.MenuExport,'parent');
4335hfig=figure;
4336copyobj(handles.PlotAxes,hfig);
4337map=colormap(handles.PlotAxes);
4338colormap(map);%transmit the current colormap to the zoom fig
4339colorbar
4340
4341% --------------------------------------------------------------------
4342function MenuExportAxis_Callback(hObject, eventdata, handles)
4343answer=msgbox_uvmat('CONFIRMATION','select a figure/axis on which the current uvmat plot will be exported')
4344if strcmp(answer,'Yes')
4345hchild=get(handles.PlotAxes,'children');
4346copyobj(hchild,gca);
4347end
4348
4349%------------------------------------------------------------------------
4350% --------------------------------------------------------------------
4351function MenuExportMovie_Callback(hObject, eventdata, handles)
4352% --------------------------------------------------------------------
4353set(handles.MenuExportMovie,'BusyAction','queue')% activate the button
4354huvmat=get(handles.run0,'parent');
4355UvData=get(huvmat,'UserData');
4356%[xx,xx,FileBase]=read_file_boxes(handles);
4357[RootPath,SubDir,RootFile,FileIndex,FileExt]=read_file_boxes(handles);
4358FileBase=fullfile(RootPath,RootFile);
4359 %read the current input file name
4360prompt = {'movie file name';'frames per second';'frame resolution (*[512x384] pixels)';'axis position relative to the frame';'total frame number (starting from the current uvmat display)'};
4361dlg_title = 'select properties of the output avi movie';
4362num_lines= 1;
4363def     = {[FileBase '_out.avi'];'10';'1';'[0.03 0.05 0.95 0.92]';'10'};
4364answer = inputdlg(prompt,dlg_title,num_lines,def,'on');
4365aviname=answer{1};
4366fps=str2double(answer{2});
4367% check for existing file with output name aviname
4368if exist(aviname,'file')
4369    backup=aviname;
4370    testexist=2;
4371    while testexist==2
4372        backup=[backup '~'];
4373        testexist=exist(backup,'file');     
4374    end
4375    [success,message]=copyfile(aviname,backup);%make backup of the existing file
4376    if isequal(success,1)
4377        delete(aviname)%delete existing file
4378    else
4379        msgbox_uvmat('ERROR',message)
4380        return
4381    end
4382end
4383%create avi open
4384aviobj=avifile(aviname,'Compression','None','fps',fps);
4385
4386%display first view for tests
4387newfig=figure;
4388newaxes=copyobj(handles.PlotAxes,newfig);%new plotting axes in the new figure
4389set(newaxes,'Tag','movieaxes')
4390nbpix=[512 384]*str2double(answer{3});
4391set(gcf,'Position',[1 1 nbpix])% resolution XVGA
4392set(newaxes,'Position',eval(answer{4}));
4393map=colormap(handles.PlotAxes);
4394colormap(map);%transmit the current colormap to the zoom fig
4395msgbox_uvmat('INPUT_Y-N',{['adjust figure ' num2str(newfig) ' with its matlab edit menu '] ;...
4396        ['then press OK to get the avi movie as a copy of figure ' num2str(newfig) ' display']});
4397UvData.plotaxes=newaxes;% the axis in the new figure becomes the current main plotting axes
4398set(huvmat,'UserData',UvData);
4399increment=str2num(get(handles.num_IndexIncrement,'String')); %get the field increment d
4400set(handles.STOP,'Visible','on')
4401set(handles.speed,'Visible','on')
4402set(handles.speed_txt,'Visible','on')
4403set(handles.Movie,'BusyAction','queue')
4404
4405%imin=str2double(get(handles.i1,'String'));
4406imax=str2double(answer{5});
4407% if isfield(UvData,'Time')
4408htitle=get(newaxes,'Title');
4409xlim=get(newaxes,'XLim');
4410ylim=get(newaxes,'YLim');
4411set(htitle,'Position',[xlim(2)+0.07*(xlim(2)-xlim(1)) ylim(2)-0.05*(ylim(2)-ylim(1)) 0])
4412time_str=get(handles.abs_time,'String');
4413set(htitle,'String',['t=' time_str])
4414set(handles.speed,'Value',1)
4415for i=1:imax
4416    if get(handles.speed,'Value')~=0 && isequal(get(handles.MenuExportMovie,'BusyAction'),'queue') % enable STOP command
4417            runpm(hObject,eventdata,handles,increment)% run plus
4418            drawnow
4419            time_str=get(handles.abs_time,'String');
4420            if ishandle(htitle)
4421             set(htitle,'String',['t=' time_str])
4422            end
4423            mov=getframe(newfig);
4424            aviobj=addframe(aviobj,mov);
4425    end
4426end
4427aviobj=close(aviobj);
4428UvData=rmfield(UvData,'plotaxes');
4429%UvData.Object{1}.plotaxes=handles.PlotAxes;
4430set(huvmat,'UserData',UvData);
4431msgbox_uvmat('CONFIRMATION',{['movie ' aviname ' created '];['with ' num2str(imax) ' frames']})
4432
4433
4434%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4435% Projection Objects Menu Callbacks
4436%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4437
4438% -----------------------------------------------------------------------
4439function Menupoints_Callback(hObject, eventdata, handles)
4440%------------------------------------------------------------------------
4441data.Type='points';
4442data.ProjMode='projection';%default
4443data.ProjModeMenu={};% do not restrict ProjMode menus
4444create_object(data,handles)
4445
4446% -----------------------------------------------------------------------
4447function Menuline_Callback(hObject, eventdata, handles)
4448%------------------------------------------------------------------------
4449data.Type='line';
4450data.ProjMode='projection';%default
4451data.ProjModeMenu={};% do not restrict ProjMode menus
4452create_object(data,handles)
4453
4454%------------------------------------------------------------------------
4455function Menupolyline_Callback(hObject, eventdata, handles)
4456%------------------------------------------------------------------------
4457data.Type='polyline';
4458data.ProjMode='projection';%default
4459data.ProjModeMenu={};% do not restrict ProjMode menus
4460create_object(data,handles)
4461
4462%------------------------------------------------------------------------
4463function Menupolygon_Callback(hObject, eventdata, handles)
4464%------------------------------------------------------------------------
4465data.Type='polygon';
4466data.ProjMode='inside';%default
4467data.ProjModeMenu={};% do not restrict ProjMode menus
4468create_object(data,handles)
4469
4470%------------------------------------------------------------------------
4471function Menurectangle_Callback(hObject, eventdata, handles)
4472%------------------------------------------------------------------------
4473data.Type='rectangle';
4474data.ProjMode='inside';%default
4475data.ProjModeMenu={};% do not restrict ProjMode menus
4476create_object(data,handles)
4477
4478%------------------------------------------------------------------------
4479function Menuellipse_Callback(hObject, eventdata, handles)
4480%------------------------------------------------------------------------
4481data.Type='ellipse';
4482data.ProjMode='inside';%default
4483data.ProjModeMenu={};% do not restrict ProjMode menus
4484create_object(data,handles)
4485
4486%------------------------------------------------------------------------
4487function MenuMaskObject_Callback(hObject, eventdata, handles)
4488%------------------------------------------------------------------------
4489data.Type='polygon';
4490data.TypeMenu={'polygon'};
4491data.ProjMode='mask_inside';%default
4492data.ProjModeMenu={'mask_inside';'mask_outside'};
4493create_object(data,handles)
4494
4495%------------------------------------------------------------------------
4496function Menuplane_Callback(hObject, eventdata, handles)
4497%------------------------------------------------------------------------
4498data.Type='plane';
4499data.ProjMode='projection';%default
4500data.ProjModeMenu={};% do not restrict ProjMode menus
4501create_object(data,handles)
4502
4503%------------------------------------------------------------------------
4504function Menuvolume_Callback(hObject, eventdata, handles)
4505%------------------------------------------------------------------------
4506data.Type='volume';
4507data.ProjMode='interp';%default
4508data.ProjModeMenu={};
4509% set(handles.create,'Visible','on')
4510% set(handles.create,'Value',1)
4511% VOLUME_Callback(hObject,eventdata,handles)data.ProjModeMenu={};
4512create_object(data,handles)
4513
4514%------------------------------------------------------------------------
4515% --- generic function used for the creation of a projection object
4516function create_object(data,handles)
4517%------------------------------------------------------------------------
4518% desactivate geometric calibration if opened
4519hgeometry_calib=findobj(allchild(0),'tag','geometry_calib');
4520if ishandle(hgeometry_calib)
4521    hhgeometry_calib=guidata(hgeometry_calib);
4522    set(hhgeometry_calib.edit_append,'Value',0)% desactivate mouse action in geometry_calib
4523    set(hhgeometry_calib.edit_append,'BackgroundColor',[0.7 0.7 0.7])
4524end
4525set(handles.edit_object,'Value',0); %suppress the object edit mode
4526set(handles.edit_object,'BackgroundColor',[0.7,0.7,0.7]) 
4527ListObject=get(handles.ListObject,'String');
4528if isempty(ListObject)
4529    ListObject={''};
4530end
4531if ~strcmp(ListObject{end},'')
4532    ListObject=[ListObject;{''}]; %append a blank to the list (if nort already done) to indicate the creation of a new object
4533    set(handles.ListObject,'String',ListObject)
4534end
4535IndexObj=length(ListObject);
4536set(handles.ListObject,'Value',IndexObj)
4537UvData=get(handles.uvmat,'UserData');
4538UvData.Object{IndexObj}=[]; %create a new empty object
4539UvData.Object{IndexObj}.DisplayHandle.uvmat=handles.PlotAxes; % axes for plot_object
4540UvData.Object{IndexObj}.DisplayHandle.view_field=[]; %no plot handle before plot_field operation
4541data.Name=data.Type;% default name=type
4542data.Coord=[0 0]; %default
4543if isfield(UvData,'Field')
4544    Field=UvData.Field;
4545    if isfield(UvData.Field,'CoordMesh')&&~isempty(UvData.Field.CoordMesh)
4546        data.RangeX=[UvData.Field.XMin UvData.Field.XMax];
4547        if strcmp(data.Type,'line')||strcmp(data.Type,'polyline')||strcmp(data.Type,'points')
4548            data.RangeY=UvData.Field.CoordMesh;
4549        else
4550            data.RangeY=[UvData.Field.YMin UvData.Field.YMax];
4551        end
4552        data.DX=UvData.Field.CoordMesh;
4553        data.DY=UvData.Field.CoordMesh;
4554    end
4555    if isfield(Field,'NbDim')&& isequal(Field.NbDim,3)
4556         data.Coord=[0 0 0]; %default
4557    end
4558    if isfield(Field,'CoordUnit')
4559        data.CoordUnit=Field.CoordUnit;
4560    end
4561end
4562if ishandle(handles.UVMAT_title)
4563    delete(handles.UVMAT_title)%delete the initial display of uvmat if no field has been entered
4564end
4565hset_object=set_object(data,handles);% call the set_object interface
4566hhset_object=guidata(hset_object);
4567hchild=get(hset_object,'children');
4568set(hchild,'enable','on')
4569set(handles.uvmat,'UserData',UvData)
4570set(handles.CheckZoom,'Value',0) %desactivate the zoom for object creation by the mouse
4571CheckZoom_Callback(handles.uvmat, [], handles)
4572set(handles.delete_object,'Visible','on')
4573
4574%------------------------------------------------------------------------
4575function MenuBrowseObject_Callback(hObject, eventdata, handles)
4576%------------------------------------------------------------------------
4577%get the object file
4578[FileName, PathName, filterindex] = uigetfile( ...
4579       {'*.xml;*.mat', ' (*.xml,*.mat)';
4580       '*.xml',  '.xml files '; ...
4581        '*.mat',  '.mat matlab files '}, ...
4582        'Pick an xml Object file',get(handles.RootPath,'String'));
4583fileinput=[PathName FileName];%complete file name
4584sizf=size(fileinput);
4585if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end
4586
4587%read the file
4588[data,heading]=xml2struct(fileinput);
4589if ~strcmp(heading,'ProjObject')
4590    msgbox_uvmat('WARNING','The xml file does not have the heading ProjObject for projection objects')
4591end
4592[tild,data.Name]=fileparts(FileName);% object name set as file name
4593ListObject=get(handles.ListObject,'String');
4594if ~strcmp(ListObject{end},'')
4595    ListObject=[ListObject;{''}]; %append a blank to the list (if not already done) to indicate the creation of a new object
4596    set(handles.ListObject,'String',ListObject)
4597end
4598IndexObj=length(ListObject);
4599
4600UvData=get(handles.uvmat,'UserData');
4601UvData.Object{IndexObj}=[]; %create a new empty object
4602UvData.Object{IndexObj}.DisplayHandle.uvmat=[]; %no plot handle before plot_field operation
4603UvData.Object{IndexObj}.DisplayHandle.view_field=[]; %no plot handle before plot_field operation
4604set(handles.uvmat,'UserData',UvData)
4605set(handles.ListObject,'Value',IndexObj)
4606hset_object=set_object(data);% call the set_object interface
4607set(get(hset_object,'children'),'enable','on')% enable edit action on elements on GUI set_object
4608set(handles.edit_object,'Value',0); %suppress the object edit mode
4609set(handles.edit_object,'BackgroundColor',[0.7,0.7,0.7]) 
4610set(handles.delete_object,'Visible','on')
4611
4612
4613%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4614% MenuEdit Callbacks
4615%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4616%------------------------------------------------------------------------
4617function MenuEditObject_Callback(hObject, eventdata, handles)
4618%------------------------------------------------------------------------
4619set(handles.edit_object,'Value',1)
4620edit_Callback(hObject, eventdata, handles)
4621
4622%------------------------------------------------------------------------
4623function MenuEditVectors_Callback(hObject, eventdata, handles)
4624%------------------------------------------------------------------------
4625set(handles.edit_vect,'Visible','on')
4626set(handles.edit_vect,'Value',1)
4627edit_vect_Callback(hObject, eventdata, handles)
4628
4629%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4630% MenuTools Callbacks
4631%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4632%------------------------------------------------------------------------
4633function MenuCalib_Callback(hObject, eventdata, handles)
4634%------------------------------------------------------------------------
4635
4636UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
4637
4638%suppress competing options
4639set(handles.CheckZoom,'Value',0)
4640set(handles.CheckZoom,'BackgroundColor',[0.7 0.7 0.7])
4641set(handles.ListObject,'Value',1)     
4642% initiate display of GUI geometry_calib
4643data=[]; %default
4644if isfield(UvData,'CoordType')
4645    data.CoordType=UvData.CoordType;
4646end
4647[RootPath,SubDir,RootFile,FileIndex,FileExt]=read_file_boxes(handles);
4648FileName=[fullfile(RootPath,SubDir,RootFile) FileIndex FileExt];
4649set(handles.view_xml,'Backgroundcolor',[1 1 0])%indicate the reading of the current xml file by geometry_calib
4650pos_uvmat=get(handles.uvmat,'Position');
4651pos_cal(1)=pos_uvmat(1)+UvData.OpenParam.PosGeometryCalib(1)*pos_uvmat(3);
4652pos_cal(2)=pos_uvmat(2)+UvData.OpenParam.PosGeometryCalib(2)*pos_uvmat(4);
4653pos_cal(3:4)=UvData.OpenParam.PosGeometryCalib(3:4).* pos_uvmat(3:4);
4654geometry_calib(FileName,pos_cal);% call the geometry_calib interface   
4655set(handles.view_xml,'Backgroundcolor',[1 1 1])%indicate the end of reading of the current xml file by geometry_calib
4656set(handles.MenuCalib,'checked','on')% indicate that MenuCalib is activated, test used by mouse action
4657
4658
4659%-----------------------------------------------------------------------
4660function MenuLIFCalib_Callback(hObject, eventdata, handles)
4661%------------------------------------------------------------------------
4662%% read UvData properties stored on the uvmat interface
4663UvData=get(handles.uvmat,'UserData');
4664if isfield(UvData,'XmlData')&& isfield(UvData.XmlData{1},'GeometryCalib')
4665    XmlData=UvData.XmlData{1};
4666else
4667    msgbox_uvmat('ERROR','geometric calibration needed: use Tools/geometric calibration in the menu bar');
4668    return
4669end
4670
4671%% read lines currently drawn
4672ListObj=UvData.Object;
4673select=zeros(1,numel(ListObj));
4674for iobj=1:numel(ListObj);
4675    if isfield(ListObj{iobj},'Type') && strcmp(ListObj{iobj}.Type,'line')
4676        select(iobj)=1;
4677    end
4678end
4679val=find(select);
4680if numel(val)<2
4681    msgbox_uvmat('ERROR','light rays must be defined by at least two lines created by Projection object/line in the menu bar');
4682    return
4683else
4684    set(handles.ListObject,'Value',val);% show the selected lines on the list
4685    ObjectData=UvData.Object(val);
4686    for iobj=1:length(ObjectData)
4687%         if isfield(ObjectData{iobj},'Coord')
4688            xA(iobj)=ObjectData{iobj}.Coord(1,1);
4689            yA(iobj)=ObjectData{iobj}.Coord(1,2);
4690            xB(iobj)=ObjectData{iobj}.Coord(2,1);
4691            yB(iobj)=ObjectData{iobj}.Coord(2,2);
4692%         end
4693    end
4694end
4695
4696%% find the origin as intersection of the two first lines (see http://www.ahristov.com/tutorial/geometry-games/intersection-lines.html )
4697x1=xA(1);x2=xB(1);
4698x3=xA(2);x4=xB(2);
4699y1=yA(1);y2=yB(1);
4700y3=yA(2);y4=yB(2);
4701D = (x1-x2)*(y3-y4) -(y1-y2)*(x3-x4);
4702if D==0
4703    msgbox_uvmat('ERROR','the two lines are parallel');
4704    return
4705end
4706x0=((x3-x4)*(x1*y2-y1*x2)-(x1-x2)*(x3*y4-y3*x4))/D;
4707y0=((y3-y4)*(x1*y2-y1*x2)-(y1-y2)*(x3*y4-y3*x4))/D;
4708XmlData.Illumination.Origin=[x0 y0];
4709XmlData.PolarCentre=[x0 y0];
4710
4711%% display the current image in polar coordinates with origin at the  illumination source
4712currentdir=pwd; 
4713uvmatpath=fileparts(which('uvmat'));
4714cd(fullfile(uvmatpath,'transform_field'));
4715phys_polar=str2func('phys_polar');
4716cd(currentdir)
4717DataOut=phys_polar(UvData.Field,XmlData);
4718view_field(DataOut);
4719
4720%% use the third line for reference luminosity
4721if numel(val)==3
4722    x_ref=linspace(ObjectData{3}.Coord(1,1),ObjectData{3}.Coord(2,1),10);
4723    y_ref=linspace(ObjectData{3}.Coord(1,2),ObjectData{3}.Coord(2,2),10);
4724    x_ref=x_ref-x0;
4725    y_ref=y_ref-y0;
4726    [theta_ref,r_ref] = cart2pol(x_ref,y_ref);%theta_ref  and r_ref are the polar coordinates of the points on the line
4727    theta_ref=theta_ref*180/pi;
4728    figure
4729    plot(theta_ref,r_ref)
4730    azimuth_ima=linspace(DataOut.AY(1),DataOut.AY(2),size(DataOut.A,1));%profile of x index on the transformed image
4731    dist_source = interp1(theta_ref,r_ref,azimuth_ima);
4732    dist_source_pixel=round(size(DataOut.A,2)*(dist_source-DataOut.AX(1))/(DataOut.AX(2)-DataOut.AX(1)));
4733    line_nan= isnan(dist_source_pixel);
4734    dist_source_pixel(line_nan)=1;
4735    width=20; %number of pixels used for reference
4736    DataOut.A=double(DataOut.A);
4737    Anorm=zeros(size(DataOut.A));
4738    Aval=mean(mean(DataOut.A));
4739    for iline=1:size(DataOut.A,1)
4740        lum(iline)=mean(DataOut.A(iline,dist_source_pixel(iline):dist_source_pixel(iline)+width));
4741        Anorm(iline,:)=uint16(Aval*DataOut.A(iline,:)/lum(iline));
4742    end
4743    lum(line_nan)=NaN;
4744    figure
4745    plot(1:size(DataOut.A,1),lum)
4746end
4747ImaName=regexprep([get(handles.RootFile,'String') get(handles.FileIndex,'String')],'//','');
4748NewImageName=fullfile(get(handles.RootPath,'String'),'polar',[ImaName get(handles.FileExt,'String')]);
4749imwrite(Anorm,NewImageName,'BitDepth',16)
4750
4751%% record the origin in the xml file
4752XmlFileName=find_imadoc(get(handles.RootPath,'String'),get(handles.SubDir,'String'),get(handles.RootFile,'String'),get(handles.FileExt,'String'));
4753answer=msgbox_uvmat('INPUT_Y-N','save the illumination origin in the current xml file?');
4754if strcmp(answer,'Yes')
4755    t=xmltree(XmlFileName); %read the file
4756    title=get(t,1,'name');
4757    if ~strcmp(title,'ImaDoc')
4758        msgbox_uvmat('ERROR','wrong xml file');
4759        return
4760    end
4761    % backup the output file if it already exist, and read it
4762    backupfile=XmlFileName;
4763    testexist=2;
4764    while testexist==2
4765        backupfile=[backupfile '~'];
4766        testexist=exist(backupfile,'file');
4767    end
4768    [success,message]=copyfile(XmlFileName,backupfile);%make backup
4769    if success~=1
4770        errormsg=['errror in xml file backup: ' message];
4771        return
4772    end
4773    uid_illumination=find(t,'ImaDoc/Illumination');
4774    if isempty(uid_illumination)  %if GeometryCalib does not already exists, create it
4775        [t,uid_illumination]=add(t,1,'element','Illumination');
4776    end
4777    uid_origin=find(t,'ImaDoc/Illumination/Origin');
4778    if ~isempty(uid_origin)  %if GeometryCalib does not already exists, create it
4779         t=delete(t,uid_origin);
4780    end
4781    % save the illumination origin
4782    t=struct2xml(XmlData.Illumination,t,uid_illumination);
4783    save(t,XmlFileName);
4784end
4785   
4786
4787
4788%------------------------------------------------------------------------
4789function MenuMask_Callback(hObject, eventdata, handles)
4790%------------------------------------------------------------------------
4791UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
4792ListObj=UvData.Object;
4793select=zeros(1,numel(ListObj));
4794for iobj=1:numel(ListObj);
4795    if strcmp(ListObj{iobj}.ProjMode,'mask_inside')||strcmp(ListObj{iobj}.ProjMode,'mask_outside')
4796        select(iobj)=1;
4797    end
4798end
4799val=find(select);
4800if isempty(val)
4801    msgbox_uvmat('ERROR','polygons must be first created by Projection object/mask polygon in the menu bar');
4802    return
4803else
4804    set(handles.ListObject,'Value',val);
4805    flag=1;
4806    npx=size(UvData.Field.A,2);
4807    npy=size(UvData.Field.A,1);
4808    xi=0.5:npx-0.5;
4809    yi=0.5:npy-0.5;
4810    [Xi,Yi]=meshgrid(xi,yi);
4811%     if isfield(UvData,'Object')
4812        for iobj=1:length(UvData.Object)
4813            ObjectData=UvData.Object{iobj};
4814            if isfield(ObjectData,'ProjMode') &&(isequal(ObjectData.ProjMode,'mask_inside')||isequal(ObjectData.ProjMode,'mask_outside'));
4815                flagobj=1;
4816                testphys=0; %coordinates in pixels by default
4817                if isfield(ObjectData,'CoordUnit') && ~isequal(ObjectData.CoordUnit,'pixel')
4818                    if isfield(UvData,'XmlData')&& isfield(UvData.XmlData{1},'GeometryCalib')
4819                        Calib=UvData.XmlData{1}.GeometryCalib;
4820                        testphys=1;
4821                    end
4822                end
4823                if isfield(ObjectData,'Coord')&& isfield(ObjectData,'Type')
4824                    if isequal(ObjectData.Type,'polygon')
4825                        X=ObjectData.Coord(:,1);
4826                        Y=ObjectData.Coord(:,2);
4827                        if testphys
4828                            pos=[X Y zeros(size(X))];
4829                            if isfield(Calib,'SliceCoord') && length(Calib.SliceCoord)>=3
4830                                if isfield(Calib,'SliceAngle')&&~isequal(Calib.SliceAngle,[0 0 0])
4831                                    om=norm(Calib.SliceAngle);%norm of rotation angle in radians
4832                                    OmAxis=Calib.SliceAngle/om; %unit vector marking the rotation axis
4833                                    cos_om=cos(pi*om/180);
4834                                    sin_om=sin(pi*om/180);
4835                                    pos=cos_om*pos+sin_om*cross(OmAxis,pos)+(1-cos_om)*(OmAxis*pos')*OmAxis;
4836                                end
4837                                pos(:,1)=pos(:,1)+Calib.SliceCoord(1);
4838                                pos(:,2)=pos(:,2)+Calib.SliceCoord(2);
4839                                pos(:,3)=pos(:,3)+Calib.SliceCoord(3);
4840                            end                           
4841                            [X,Y]=px_XYZ(Calib,pos(:,1),pos(:,2),pos(:,3));
4842                        end
4843                        flagobj=~inpolygon(Xi,Yi,X',Y');%=0 inside the polygon, 1 outside
4844                    elseif isequal(ObjectData.Type,'ellipse')
4845                        if testphys
4846                            %[X,Y]=px_XYZ(Calib,X,Y,0);% TODO:create a polygon boundary and transform to phys
4847                        end
4848                        RangeX=max(ObjectData.RangeX);
4849                        RangeY=max(ObjectData.RangeY);
4850                        X2Max=RangeX*RangeX;
4851                        Y2Max=RangeY*RangeY;
4852                        distX=(Xi-ObjectData.Coord(1,1));
4853                        distY=(Yi-ObjectData.Coord(1,2));
4854                        flagobj=(distX.*distX/X2Max+distY.*distY/Y2Max)>1;
4855                    elseif isequal(ObjectData.Type,'rectangle')
4856                        if testphys
4857                            %[X,Y]=px_XYZ(Calib,X,Y,0);% TODO:create a polygon boundary and transform to phys
4858                        end
4859                        distX=abs(Xi-ObjectData.Coord(1,1));
4860                        distY=abs(Yi-ObjectData.Coord(1,2));
4861                        flagobj=distX>max(ObjectData.RangeX) | distY>max(ObjectData.RangeY);
4862                    end
4863                    if isequal(ObjectData.ProjMode,'mask_outside')
4864                        flagobj=~flagobj;
4865                    end
4866                    flag=flag & flagobj;
4867                end
4868            end
4869        end
4870%     end
4871    %mask name
4872    RootPath=get(handles.RootPath,'String');
4873    SubDir=get(handles.SubDir,'String');
4874    RootFile=get(handles.RootFile,'String');
4875    if ~isempty(RootFile)&&(isequal(RootFile(1),'/')|| isequal(RootFile(1),'\'))
4876        RootFile(1)=[];
4877    end
4878    list=get(handles.masklevel,'String');
4879    masknumber=num2str(length(list));
4880    maskindex=get(handles.masklevel,'Value');
4881   % mask_name=fullfile_uvmat(RootPath,'',[RootFile '_' masknumber 'mask'],'.png','_1',maskindex);
4882    mask_name=fullfile_uvmat(RootPath,[SubDir '.mask'],'mask','.png','_1',maskindex);
4883    imflag=uint8(255*(0.392+0.608*flag));% =100 for flag=0 (vectors not computed when 20<imflag<200)
4884    imflag=flipdim(imflag,1);
4885
4886    %display the mask
4887    hfigmask=figure;
4888    set(hfigmask,'Name','mask image')
4889    vec=linspace(0,1,256);%define a linear greyscale colormap
4890    map=[vec' vec' vec'];
4891    colormap(map)
4892    image(imflag);
4893    answer=msgbox_uvmat('INPUT_TXT','mask file name:', mask_name);
4894    if ~strcmp(answer,'Cancel')
4895        mask_dir=fileparts(answer);
4896        if ~exist(mask_dir,'dir')
4897            [xx,msg1]=mkdir(mask_dir);
4898            if ~strcmp(msg1,'')
4899                errormsg=['cannot create ' mask_dir ': ' msg1];%error message for directory creation
4900                return
4901            end
4902        end
4903        imwrite(imflag,answer,'BitDepth',8);
4904    end
4905    set(handles.ListObject,'Value',1)
4906end
4907
4908%------------------------------------------------------------------------
4909%-- open the GUI set_grid.fig to create grid
4910function MenuGrid_Callback(hObject, eventdata, handles)
4911%------------------------------------------------------------------------
4912%suppress the other options if grid is chosen
4913set(handles.edit_vect,'Value',0)
4914edit_vect_Callback(hObject, eventdata, handles)
4915set(handles.edit_object,'BackgroundColor',[0.7 0.7 0.7])
4916set(handles.ListObject,'Value',1)     
4917
4918%prepare display of the set_grid GUI
4919[RootPath,SubDir,RootFile,FileIndex,FileExt]=read_file_boxes(handles);
4920FileName=[fullfile(RootPath,SubDir,RootFile) FileIndex FileExt];
4921UvData=get(handles.uvmat,'UserData');
4922% CoordList=get(handles.transform_fct,'String');
4923% val=get(handles.transform_fct,'Value');
4924set_grid(FileName,UvData.Field);% call the set_object interface
4925
4926
4927%------------------------------------------------------------------------
4928function MenuRuler_Callback(hObject, eventdata, handles)
4929%------------------------------------------------------------------------
4930set(handles.CheckZoom,'Value',0)
4931CheckZoom_Callback(handles.uvmat, [], handles)
4932set(handles.MenuRuler,'checked','on')
4933UvData=get(handles.uvmat,'UserData');
4934UvData.MouseAction='ruler';
4935set(handles.uvmat,'UserData',UvData);
4936
4937%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4938% MenuRun Callbacks
4939%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4940
4941%------------------------------------------------------------------------
4942% open the GUI 'series'
4943function MenuSeries_Callback(hObject, eventdata, handles)
4944%------------------------------------------------------------------------
4945series; %first display of the GUI to fill waiting time
4946[RootPath,SubDir,RootFile,FileIndex,FileExt]=read_file_boxes(handles);
4947Param.FileName=[fullfile(RootPath,SubDir,RootFile) FileIndex FileExt];%first input file name
4948if isequal(get(handles.SubField,'Value'),1)
4949    [RootPath_1,SubDir_1,RootFile_1,FileIndex_1,FileExt_1]=read_file_boxes_1(handles);
4950    FileName_1=[fullfile(RootPath_1,SubDir_1,RootFile_1) FileIndex_1 FileExt_1];
4951    if ~isequal(FileName_1,Param.FileName)
4952        Param.FileName_1=FileName_1;%second input file name if relevant
4953    end
4954end
4955Param.NomType=get(handles.NomType,'String');
4956Param.NomType_1=get(handles.NomType_1,'String');
4957Param.CheckFixPair=get(handles.CheckFixPair,'Value');
4958UvData=get(handles.uvmat,'UserData');
4959if isfield(UvData,'XmlData')&& isfield(UvData.XmlData{1},'Time')
4960    Param.Time=UvData.XmlData{1}.Time;
4961end
4962if isequal(get(handles.scan_i,'Value'),1)
4963    Param.incr_i=str2num(get(handles.num_IndexIncrement,'String'));
4964elseif isequal(get(handles.scan_j,'Value'),1)
4965    Param.incr_j=str2num(get(handles.num_IndexIncrement,'String'));
4966end
4967
4968%% transfer fields and coordinate names
4969Param.list_fields=get(handles.FieldName,'String');% list menu fields
4970FieldName=Param.list_fields{get(handles.FieldName,'Value')};
4971ind_image=find(strcmp('image',Param.list_fields));
4972if ~isempty(ind_image) && numel(Param.list_fields)>1
4973    Param.list_fields(ind_image)=[]; %suppress  'image' option
4974end
4975Param.index_fields=find(strcmp(FieldName,Param.list_fields));% selected string index
4976Param.list_fields_1=get(handles.FieldName_1,'String');% list menu fields
4977if ischar(Param.list_fields_1),Param.list_fields_1={Param.list_fields_1};end
4978FieldName_1=Param.list_fields_1{get(handles.FieldName_1,'Value')};
4979ind_image=find(strcmp('image',Param.list_fields_1));
4980if ~isempty(ind_image) && numel(Param.list_fields_1)>1
4981    Param.list_fields_1(ind_image)=[]; %suppress  'image' option
4982end
4983Param.index_fields_1=find(strcmp(FieldName_1,Param.list_fields_1));% selected string index
4984Param.transform_str=get(handles.transform_fct,'String');
4985Param.transform_val=get(handles.transform_fct,'Value');
4986Param.Coord_x_str=get(handles.Coord_x,'String');
4987Param.Coord_x_val=get(handles.Coord_x,'Value');
4988Param.Coord_y_str=get(handles.Coord_y,'String');
4989Param.Coord_y_val=get(handles.Coord_y,'Value');
4990series(Param); %run the series interface
4991
4992%------------------------------------------------------------------------
4993% -- open the GUI civ.fig for PIV
4994function MenuPIV_Callback(hObject, eventdata, handles)
4995%------------------------------------------------------------------------
4996 [RootPath,SubDir,RootFile,FileIndex,FileExt]=read_file_boxes(handles);
4997 FileName=[fullfile(RootPath,SubDir,RootFile) FileIndex FileExt];
4998civ(FileName);% interface de civ(not in the uvmat file)
4999
5000% --------------------------------------------------------------------
5001function MenuHelp_Callback(hObject, eventdata, handles)
5002% --------------------------------------------------------------------
5003path_to_uvmat=which ('uvmat');% check the path of uvmat
5004pathelp=fileparts(path_to_uvmat);
5005helpfile=fullfile(pathelp,'uvmat_doc','uvmat_doc.html');
5006if isempty(dir(helpfile)), msgbox_uvmat('ERROR','Please put the help file uvmat_doc.html in the sub-directory /uvmat_doc of the UVMAT package')
5007else
5008    addpath (fullfile(pathelp,'uvmat_doc'))
5009    web(helpfile);
5010end
5011
5012% --- Executes on selection change in Coord_y.
5013function Coord_y_Callback(hObject, eventdata, handles)
5014
5015% --- Executes on selection change in Coord_x.
5016function Coord_x_Callback(hObject, eventdata, handles)
5017
5018
5019% --- Executes on button press in CheckColorBar.
5020function CheckColorBar_Callback(hObject, eventdata, handles)
Note: See TracBrowser for help on using the repository browser.