source: trunk/src/uvmat.m @ 309

Last change on this file since 309 was 309, checked in by sommeria, 9 years ago

many bugs corrected, cleaning of civ.m, resize of the GUI civ

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