source: trunk/src/uvmat.m @ 278

Last change on this file since 278 was 278, checked in by gostiaux, 12 years ago

revision instead of version

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