source: trunk/src/uvmat.m @ 258

Last change on this file since 258 was 258, checked in by sommeria, 10 years ago

bug repaired for 1D plot (using get_field)

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