source: trunk/src/uvmat.m @ 295

Last change on this file since 295 was 295, checked in by sommeria, 13 years ago

plotting functions debugged and checked after the introduction of uipanels

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