source: trunk/src/civ.m @ 84

Last change on this file since 84 was 84, checked in by sommeria, 14 years ago

-update_imadoc: copy the timing information of the movie file avi in the new xml file when a geometry_calibration is performed
-set_grid: modified to produce grids in px coordiantes and to display the produced grid
-geometry_calib: display the point with max error
-civ: bug corrected for copying avi movies to png files
-uvmat: small bug fixes
-px_XYZ: introduce default values for Calib parameters

File size: 209.5 KB
Line 
1%'civ': function associated with the interface 'civ.fig' for PIV, spline interpolation and stereo PIV (patch)   
2%------------------------------------------------------------------------
3%  provides an interface for the software CIVx
4% function varargout = civ(varargin)
5% provides an interface for the software CIVx
6%
7%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
8%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
9%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
10%     This file is part of the toolbox UVMAT.
11%
12%     UVMAT is free software; you can redistribute it and/or modify
13%     it under the terms of the GNU General Public License as published by
14%     the Free Software Foundation; either version 2 of the License, or
15%     (at your option) any later version.
16%
17%     UVMAT is distributed in the hope that it will be useful,
18%     but WITHOUT ANY WARRANTY; without even the implied warranty of
19%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
21%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
22function varargout = civ(varargin)
23
24% Last Modified by GUIDE v2.5 27-Mar-2010 13:41:11
25% Begin initialization code - DO NOT EDIT
26gui_Singleton = 1;
27gui_State = struct('gui_Name',       mfilename, ...
28                   'gui_Singleton',  gui_Singleton, ...
29                   'gui_OpeningFcn', @civ_OpeningFcn, ...
30                   'gui_OutputFcn',  @civ_OutputFcn, ...
31                   'gui_LayoutFcn',  [] , ...
32                   'gui_Callback',   []);
33if nargin && ischar(varargin{1})
34    gui_State.gui_Callback = str2func(varargin{1});
35end
36
37if nargout
38    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
39else
40    gui_mainfcn(gui_State, varargin{:});
41end
42% End initialization code - DO NOT EDIT
43
44%------------------------------------------------------------------------
45% --- Executes just before civ is made visible.
46function civ_OpeningFcn(hObject, eventdata, handles, param)
47%------------------------------------------------------------------------
48% This function has no output args, see OutputFcn.
49% hObject    handle to figure
50% eventdata  reserved - to be defined in a future version of MATLAB
51% handles    structure with handles and user data (see GUIDATA)
52% varargin   command line arguments to civ (see VARARGIN)
53global patch_newBin %=1 if new patch processing available
54%filebase: root name
55%nom_type: nomencalture used ('png_old','_i_j'...)
56%list of field numbers to process
57%subdir: subdirectory of the opened netcdf file
58%ind_opening: operation number advised for beginning (1=civ1,2=fix1,3=patch1,4=civ2,5=fix2,6=patch2),
59%ind_a_opening ind_b_opening chosen pair from the opened netcdf file
60% Choose default command line output for civ
61handles.output = hObject;
62% Update handles structure
63guidata(hObject, handles);
64
65%default initial parameters
66filebase=''; % root file name ('filebase'.civ)
67ext=[];
68testall=0;
69browse=[];
70%default input parameters:
71num1=1; % set of field i numbers
72num2=2; % set of field i numbers
73num_a=1; % set of field j numbers (fields a)
74num_b=1; % second set of field j numbers (fields b)
75subdir='A'; % subdir for the netcdf result files
76ind_opening=1; % proposed operation number (1=civ1,2=fix1,3=patch1,4=civ2,5=fix2,6=patch2)
77%load the initial parameters if the interface is started from uvmat
78if exist('param','var')&&isstruct(param)% the interface is opened from uvmat
79    filebase=param.RootName;
80    nom_type_read=param.NomType;
81    num1=param.num1;
82    num2=param.num2;
83    num_a=param.num_a;
84    num_b=param.num_b;
85    subdir=param.SubDir;
86    ind_opening=param.IndOpening;
87    ext=param.ImaExt;
88end
89if ~isempty(ext) && (~isempty(imformats(ext(2:end)))||...
90                       strcmp(lower(ext),'.avi'));%if an image file has been opened by uvmat
91        set(handles.ImaExt,'String',ext)
92        if exist('nom_type_read','var')
93            browse.nom_type_ima=nom_type_read; % the image nomenclature is stored
94        end
95elseif isequal(ext,'.nc')
96    if exist('nom_type_read','var')
97        browse.nom_type_nc=nom_type_read;% the netcdf  nomenclature is stored
98    end
99end
100set(handles.RootName,'String',filebase);
101set(handles.ImaDoc,'UserData',testall);
102set(handles.ImaDoc,'String',ext)
103
104%read names of the .exe file to adjust the interface according to
105%available binaries
106path_uvmat=which('uvmat');% check the path detected for source file uvmat
107path_UVMAT=fileparts(path_uvmat); %path to UVMAT
108errormsg=[];%default error message
109xmlfile=fullfile(path_UVMAT,'PARAM.xml');
110if exist(xmlfile,'file')
111    try
112    t=xmltree(xmlfile);
113    sparam=convert(t);
114    catch
115         errormsg={[' Unable to read the file PARAM.xml defining the civx binaries:']; lasterr};   
116    end
117else
118    errormsg=[xmlfile ' not found: path to civx binaries undefined'];
119end
120
121if ~isempty(errormsg)
122       msgbox_uvmat('ERROR',errormsg);
123end
124% patch_newBin='';
125test_batch=0;%default: ,no batch mode available
126if isfield(sparam,'BatchParam') && isfield(sparam.BatchParam,'BatchMode')
127    test_batch=strcmp(sparam.BatchParam.BatchMode,'sge'); %sge is currently the only implemented batch mod
128end
129if test_batch==0
130    set(handles.BATCH,'Enable','off')% put the BATCH button in grey (unactivated)
131    set(handles.BATCH,'BackgroundColor',[0.831 0.816 0.784])% put the BATCH button in grey (unactivated)
132end
133set(handles.subdir_civ1,'String',subdir)%default subdir on which uvmat was working
134set(handles.subdir_civ2,'String',subdir)%default subdir on which uvmat was working
135
136%initiate advised operations
137if isequal(ind_opening,[])
138    ind_opening=1; % default
139end
140% set default operation options
141    enable_civ1(handles,'off')
142    enable_civ2(handles,'off')
143    enable_pair1(handles,'on')
144    enable_fix1(handles,'off')
145    desable_patch1(handles)
146    desable_fix2(handles)
147    desable_patch2(handles)
148    set(handles.CIV1,'Value',0)
149    set(handles.FIX1,'Value',0)
150    set(handles.PATCH1,'Value',0)
151    set(handles.CIV2,'Value',0)
152    set(handles.FIX2,'Value',0)
153    set(handles.PATCH2,'Value',0)
154    set(handles.frame_subdirciv2,'BackgroundColor',[0.831 0.816 0.784])
155if isequal(ind_opening,1)
156    set(handles.CIV1,'Value',1)
157    enable_civ1(handles,'on')
158elseif isequal(ind_opening,2)
159    set(handles.FIX1,'Value',1)
160    enable_fix1(handles,'on')
161elseif isequal(ind_opening,3)
162    set(handles.PATCH1,'Value',1)
163    enable_patch1(handles)
164elseif isequal(ind_opening,4)
165    set(handles.CIV2,'Value',1)
166    enable_civ2(handles,1)
167elseif isequal(ind_opening,5)
168    set(handles.FIX2,'Value',1)
169    enable_fix2(handles)
170     set(handles.frame_subdirciv2,'BackgroundColor',[1 1 0])
171     set(handles.list_pair_civ2,'Enable','On')
172     set(handles.list_pair_civ2,'Enable','On')
173     enable_pair1(handles,'off')
174elseif isequal(ind_opening,6)
175    set(handles.PATCH2,'Value',1)
176    enable_patch2(handles)
177    set(handles.frame_subdirciv2,'BackgroundColor',[1 1 0])
178    set(handles.list_pair_civ2,'Enable','On')
179    enable_pair1(handles,'off')
180end
181
182% set the range of fields (1:1 by default) and selected pair
183if isempty(num2)||isequal(num2,num1)
184    num_ref_i=num1;
185else
186    num_ref_i=floor((num1+num2)/2);
187    browse.incr_pair(1)=num2-num1;
188    browse.incr_pair(2)=0;
189end
190if isempty(num_b)||isequal(num_a,num_b)
191    if isempty(num_a)
192        num_ref_j=1;
193    else
194        num_ref_j=num_a;
195    end
196else
197    num_ref_j=floor((num_a+num_b)/2);
198    browse.incr_pair(2)=num_b-num_a;
199end
200set(handles.first_i,'String',num2str(num_ref_i));
201set(handles.last_i,'String',num2str(num_ref_i));
202set(handles.first_j,'String',num2str(num_ref_j));
203set(handles.last_j,'String',num2str(num_ref_j));
204set(handles.ref_i,'String',num2str(num_ref_i));
205set(handles.ref_j,'String',num2str(num_ref_j));
206set(handles.ref_i_civ2,'String',num2str(num_ref_i));
207set(handles.ref_j_civ2,'String',num2str(num_ref_j));
208set(handles.browse_root,'UserData',browse);
209if exist('param','var')%varargin the interface is opened from uvmat
210    RootName_Callback(hObject, eventdata, handles);
211end
212
213set(handles.waitbar_1,'Position',[0.946 0.877 0.03 0.001])
214set(handles.waitbar_patch1,'Position',[0.946 0.626 0.03 0.001])
215set(handles.waitbar_civ2,'Position',[0.946 0.406 0.03 0.001])
216set(handles.waitbar_patch2,'Position',[0.946 0.187 0.03 0.001])
217
218%------------------------------------------------------------------------
219% --- Outputs from this function are returned to the command line.
220function varargout = civ_OutputFcn(hObject, eventdata, handles)
221%------------------------------------------------------------------------
222% varargout  cell array for returning output args (see VARARGOUT);
223% hObject    handle to figure
224% eventdata  reserved - to be defined in a future version of MATLAB
225% handles    structure with handles and user data (see GUIDATA)
226% Get default command line output from handles structure
227varargout{1} = handles.output;
228
229%------------------------------------------------------------------------
230% --- Executes on button press in browse_root.
231function browse_root_Callback(hObject, eventdata, handles)
232%------------------------------------------------------------------------
233%get the input file properties
234filebase=get(handles.RootName,'String');
235oldfile=''; %default
236if isempty(filebase)|| isequal(filebase,'')%loads the previously stored file name and set it as default in the file_input box
237     dir_perso=prefdir;
238     profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
239     display(profil_perso)
240     if exist(profil_perso,'file')
241          h=load (profil_perso);
242         if isfield(h,'filebase')&& ischar(h.filebase)
243                 oldfile=h.filebase;
244         end
245         if isfield(h,'RootPath') && ischar(h.RootPath)
246                 oldfile=h.RootPath;
247         end
248     end
249 else
250     oldfile=filebase;
251 end
252testall=get(handles.ImaDoc,'UserData');
253ind_opening=1;%default
254browse.incr_pair=[0 0]; %default
255if testall
256    menu={'*.*', 'All Files (*.*)'; '*.xml; *.avi;*.AVI;*.nc','(*.xml,*.avi,*.nc)'; ...
257        '*.xml', '.xml files';'*.avi;*.AVI', '.avi files';'*.nc','.nc files'};
258else % menu selecting only .civ or .avi files
259menu={'*.xml;*.avi;*.AVI;*.nc','(*.xml,*.avi,*.nc)'; ...
260        '*.xml', '.xml files';'*.avi;*.AVI', '.avi files';'*.nc', '.nc files';...
261        '*.*', 'All Files (*.*)'};
262end
263[FileName, PathName, filtindex] = uigetfile( menu, 'Pick a file',oldfile);
264fileinput=[PathName FileName];%complete file name
265sizf=size(fileinput);
266if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
267[path,name,ext]=fileparts(fileinput);
268testeditxml=0;
269if isequal(ext,'.xml')
270    testeditxml=1;
271    t_browse=xmltree(fileinput);
272    head_element=get(t_browse,1);
273    if isfield(head_element,'name')&& isequal(head_element.name,'ImaDoc')
274        testeditxml=0;
275    end
276end
277if testeditxml==1 || isequal(ext,'.xls')
278   heditxml=editxml({fileinput});
279   set(heditxml,'Tag','browser')
280   waitfor(heditxml,'Tag','idle')
281   if ~ishandle(heditxml)
282       return
283   end
284   attr=findobj(get(heditxml,'children'),'Tag','CurrentAttributes');
285   set(handles.browse,'UserData',fileinput)% store for future opening with browser
286   fileinput=get(attr,'UserData');
287   if ~exist(fileinput,'file')
288       return
289   end
290end
291[RootPath,RootFile,str1,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fileinput);
292filebase=fullfile(RootPath,RootFile);
293num_i1=stra2num(str1);
294if isempty(num_i1),num_i1=1;end
295num_i2=stra2num(str2);
296if isempty(num_i2),num_i2=num_i1;end
297num_j1=stra2num(str_a);
298if isempty(num_j1),num_j1=1;end
299num_j2=stra2num(str_b);
300if isempty(num_j2),num_j2=num_j1;end
301if isequal(get(handles.compare,'Value'),1)
302    browse=[];%initialisation
303else
304    browse=get(handles.browse_root,'UserData');
305end
306if length(ext)>1 && (~isempty(imformats(ext([2:end])))||...
307                       isequal(ext,'.avi')||isequal(ext,'.AVI'));%if an image file has been opened by uvmat
308    set(handles.ImaExt,'String',ext)
309    browse.nom_type_ima=nom_type;
310    browse.field_count=num_i1;
311end
312set(handles.ImaDoc,'String',ext);
313
314%%%%% read the state of the selected netcdf file to advise default operation
315if isequal(ext,'.nc')
316    browse.nom_type_nc=nom_type;
317    ind_opening=2;% propose 'fix' as the default option   
318    Data=nc2struct(fileinput,[]);
319    if isfield(Data,'absolut_time_T0')%test for civx files       
320        if isfield(Data,'fix') && isequal(Data.fix,1)
321            ind_opening=3;
322        end
323        if isfield(Data,'patch') && isequal(Data.patch,1)
324            ind_opening=4;
325        end
326        if isfield(Data,'civ2') && isequal(Data.civ2,1)
327            ind_opening=5;
328        end
329        if isfield(Data,'fix2') && isequal(Data.fix2,1)
330            ind_opening=6;
331        end
332        testciv=1; %TO SUPPRESS WITH NEW VERSION OF CIVX
333    else
334        ind_opening=3; %GUI used only for patch
335        testciv=0;
336    end
337    set(handles.subdir_civ1,'String',subdir);%set the default subdir directories for installing the .nc results
338    set(handles.subdir_civ2,'String',subdir);
339    browse.testciv=testciv;
340    browse.ind_opening=ind_opening;
341end
342set(handles.RootName,'String',filebase);
343set(handles.ImaDoc,'String',ext);
344if ~isempty(num_i1)
345    ref_i=num_i1;
346    if ~isempty(num_i2)
347        ref_i=floor((ref_i+num_i2)/2);% reference image number corresponding to the file
348        browse.incr_pair(1)=num_i2-num_i1;
349        browse.incr_pair(2)=0;
350    end
351    set(handles.first_i,'String',num2str(ref_i));
352    set(handles.last_i,'String',num2str(ref_i));
353    set(handles.ref_i,'String',num2str(ref_i));
354    set(handles.ref_i_civ2,'String',num2str(ref_i))
355end
356if isempty(num_j1)
357    set(handles.ref_j,'String','1');
358    set(handles.ref_j_civ2,'String','1');
359else
360    ref_j=num_j1;
361    if ~isempty(num_j2)
362        ref_j=floor((num_j1+num_j2)/2);
363        browse.incr_pair(2)=num_j2-num_j1;
364    end
365    set(handles.first_j,'String',num2str(ref_j));
366    set(handles.last_j,'String',num2str(ref_j));
367    set(handles.ref_j,'String',num2str(ref_j));
368    set(handles.ref_j_civ2,'String',num2str(ref_j));
369end
370
371% set default operation options
372enable_civ1(handles,'off')
373enable_civ2(handles,'off')
374enable_pair1(handles,'on')
375enable_fix1(handles,'off')
376desable_patch1(handles)
377desable_fix2(handles)
378desable_patch2(handles)
379set(handles.CIV1,'Value',0)
380set(handles.FIX1,'Value',0)
381set(handles.PATCH1,'Value',0)
382set(handles.CIV2,'Value',0)
383set(handles.FIX2,'Value',0)
384set(handles.PATCH2,'Value',0)
385set(handles.frame_subdirciv2,'BackgroundColor',[0.831 0.816 0.784])
386if isequal(ind_opening,1)
387    set(handles.CIV1,'Value',1)
388    enable_civ1(handles,'on')
389elseif isequal(ind_opening,2)
390    set(handles.FIX1,'Value',1)
391    enable_fix1(handles,'on')
392elseif isequal(ind_opening,3)
393    set(handles.PATCH1,'Value',1)
394    enable_patch1(handles)
395elseif isequal(ind_opening,4)
396    set(handles.CIV2,'Value',1)
397    enable_civ2(handles,1)
398elseif isequal(ind_opening,5)
399    enable_pair1(handles,'off')
400    set(handles.FIX2,'Value',1)
401    enable_fix2(handles)
402     set(handles.frame_subdirciv2,'BackgroundColor',[1 1 0])
403     set(handles.list_pair_civ2,'Enable','On')
404     set(handles.list_pair_civ2,'Enable','On')
405elseif isequal(ind_opening,6)
406    enable_pair1(handles,'off')
407    set(handles.PATCH2,'Value',1)
408    enable_patch2(handles)
409    set(handles.frame_subdirciv2,'BackgroundColor',[1 1 0])
410    set(handles.list_pair_civ2,'Enable','On')
411end
412set(handles.browse_root,'UserData',browse);% store information from browser
413testall=isequal(menu(filtindex,1),{'*.*'});
414set(handles.ImaDoc,'UserData',testall);
415
416RootName_Callback(hObject, eventdata, handles);
417
418%------------------------------------------------------------------------
419function ImaDoc_Callback(hObject, eventdata, handles)
420%------------------------------------------------------------------------
421RootName_Callback(hObject, eventdata, handles)
422
423%------------------------------------------------------------------------
424% --- function activated when a new filebase (image series) is introduced
425function RootName_Callback(hObject, eventdata, handles)
426%------------------------------------------------------------------------
427set(handles.compare,'Visible','on')
428ext_ima=get(handles.ImaExt,'String');
429nom_type_ima=[];%default
430field_count=1;%default
431nom_type_nc=[];
432time=[];
433TimeUnit='frame'; %default
434CoordUnit='px';%default
435pxcmx_search=[];%default
436pxcmy_search=[];%default
437filebase=get(handles.RootName,'String');
438
439ext=get(handles.ImaDoc,'String');
440browse=get(handles.browse_root,'UserData');%default
441if ~isempty(browse)
442    if isfield(browse,'nom_type_ima')
443        nom_type_ima=browse.nom_type_ima;% get an image nomenclature type already determined by an input image name
444    end
445    if isfield(browse,'field_count')
446        field_count=browse.field_count;% get an image index type already determined by an input file
447    end
448end
449
450%default first_i and j and increments
451first_i=str2num(get(handles.first_i,'String'));%value possibly set by uvmat_Opening
452if isempty(first_i)|| first_i < 1
453    first_i=1; %default first_i
454end
455last_i=str2num(get(handles.last_i,'String'));
456if isempty(last_i)|| last_i < first_i
457    last_i=first_i;  %default last_i
458end
459first_j=str2num(get(handles.first_j,'String'));
460if isempty(first_j)|| first_j < 1
461    first_j=1; %default first_j
462end
463last_j=str2num(get(handles.last_j,'String'));
464if isempty(last_j)|| last_j < first_j
465    last_j=first_j; %default last_j
466end
467incr_i=str2num(get(handles.incr_i,'String'));
468if isempty(incr_i) || incr_i < 1;
469    set(handles.incr_i,'String','1') %default incr_i
470end
471incr_j=str2num(get(handles.incr_j,'String'));
472if isempty(incr_j) || incr_j < 1;
473    set(handles.incr_j,'String','1') %default incr_j
474end
475dt=[];%default
476testmode=0;%default
477nbfield=[]; %default
478nburst=[];%default
479pxcmx=1;
480pxcmy=1;
481
482 %look for an image documentation file
483if ~isequal(ext,'.xml') && ~isequal(ext,'.civ')&& ~isequal(ext,'.avi')&& ~isequal(ext,'.AVI')
484    if exist([filebase '.xml'],'file')
485         ext='.xml';
486    elseif exist([filebase '.civxml'],'file')
487         ext='.civxml';
488    elseif exist([filebase '.civ'],'file')
489         ext='.civ';
490    elseif exist([filebase '.avi'],'file')
491         ext='.avi';
492    elseif exist([filebase '.AVI'],'file')
493         ext='.AVI';
494    end
495    set(handles.ImaDoc,'String',ext)
496end
497
498%%%%%%%%   read image documentation file  %%%%%%%%%%%%%%%%%%%%%%%%%%%
499mode=''; %default
500set(handles.ImaDoc,'BackgroundColor',[1 1 0])
501drawnow
502if isequal(ext,'.civxml') || isequal(ext,'.xml')|| isequal(ext,'.civ')
503    set(handles.ref_i,'Visible','On')%use a reference index
504    set(handles.ref_j,'Visible','On')
505elseif isequal(ext,'.avi') || isequal(ext,'.AVI')
506    set(handles.ref_j,'Visible','Off')
507else
508    set(handles.ref_i,'Visible','Off')
509    set(handles.ref_j,'Visible','Off')
510end
511testima_xml=0;
512if isequal(ext,'.civxml')%TO ABANDON
513    [nbfield,nburst,time]=read_civxml([filebase '.civxml']);
514    mode='pair j1-j2';
515    if isempty(nom_type_ima)% dtermine types by default if not already selected by browser or uvmat
516            nom_type_ima='_i_j';
517    end
518elseif isequal(ext,'.xml')
519    [XmlData,warntext]=imadoc2struct([filebase '.xml']);
520    ext_ima_read=[];
521    nom_type_read=[];
522    if isfield(XmlData,'Heading')&&isfield(XmlData.Heading','ImageName')&&ischar(XmlData.Heading.ImageName)% get image nom type and extension from the xml file
523        [PP,FF,fc,str2,str_a,str_b,ext_ima_read,nom_type_read]=name2display(XmlData.Heading.ImageName);
524        fullname=fullfile(fileparts(filebase),XmlData.Heading.ImageName); %full name (including path) of the first image defined by the xmle file,
525        if ~exist(fullname,'file')
526            msgbox_uvmat('WARNING',['FirstImage ' fullname ' defined in the xml file does not exist'])
527        end
528    end
529    if isfield(XmlData,'Time')
530        time=XmlData.Time
531        nbfield=size(time,1);
532        nburst=size(time,2);
533        %transform .Time to a column vector if it is a line vector the nomenclature uses a single index
534        if isequal(nbfield,1) && ~isequal(nburst,1)% .Time is a line vector
535            if numel(nom_type_read)>=2 && (strcmp(nom_type_read,'_i')||strcmp(nom_type_read(1:2),'%0')||strcmp(nom_type_read(1:2),'_%'))
536                time=time';
537                nbfield=nburst;
538                nburst=1;
539            end
540        end
541    end
542    if isfield(XmlData,'TimeUnit')
543        TimeUnit=XmlData.TimeUnit;
544    end
545    if isfield(XmlData,'Npx')
546        npx=XmlData.Npx;
547        npy=XmlData.Npy;
548    end
549    pxcmx_search=1;
550    pxcmy_search=1;
551    if isfield(XmlData,'GeometryCalib')
552        tsai=XmlData.GeometryCalib;
553        if isfield(tsai,'f') & isfield(tsai,'Tz') & isfield(tsai,'dpx') & isfield(tsai,'dpy')& isfield(tsai,'R')
554             rot2D=tsai.R([1:2],[1,2]);
555             pxcmx_search=tsai.f * sqrt(det(rot2D))/(tsai.Tz*tsai.dpx);
556             pxcmy_search=tsai.f * sqrt(det(rot2D))/(tsai.Tz*tsai.dpy);           
557        end
558        if isfield(tsai,'CoordUnit')
559             CoordUnit=tsai.CoordUnit;
560        end
561    end               
562    if ~isempty(ext_ima_read) && ~isempty(nom_type_read)
563        if isempty(ext_ima)
564             ext_ima=ext_ima_read;% define image extension from the xml file if an image has not been opened previously
565        else   %keep the image extension           
566            if  ~strcmp(ext_ima_read,ext_ima)
567              msgbox_uvmat('WARNING',['FirtsImage extension ' ext_ima_read ' announced in the xml file inconsistent with the selected image'])
568            end
569        end
570        nom_type_ima=nom_type_read;
571    end
572elseif isequal(ext,'.civ')% case of .civ image documentation file
573        [error,time,TimeUnit,mode,npx,npy]=read_imatext([filebase '.civ']);
574        if error==2, msgbox_uvmat('WARNING',['no file ' filebase '.civ']);
575        elseif error==1, msgbox_uvmat('WARNING','inconsistent number of fields in the .civ file');
576        end
577%         ImaExt='.png';
578        nom_type_ima='png_old';       
579elseif isequal(lower(ext),'.avi')
580        nom_type_ima='*';
581        ext_ima=ext;
582        set(handles.mode,'String',{'series(Di)'})
583        dt=0.04;%default
584        if exist([filebase ext],'file')==2
585            info=aviinfo([filebase ext]);%read infos on the avi movie
586            dt=1/info.FramesPerSecond;%time interval between successive frames
587            nbfield=info.NumFrames;%number of frames
588        end
589        time=(dt*[0:nbfield-1])';%list of image times   
590        %set(handles.dt,'String',num2str(dt*1000));%store the time interval between successive images
591end
592if isempty(time)
593    set(handles.ImaDoc,'String',''); %xml file not used for timing
594end
595set(handles.ImaDoc,'BackgroundColor',[1 1 1])
596
597%get the imabe nomenclature type if not defiend by the input file nor by the xml file
598if isempty(nom_type_ima)
599    %look for double image series '_i_j'
600    dirima=dir([filebase '_' num2str(first_i) '_' num2str(first_j) '.*']);
601    if isempty(dirima)
602       % look for images series  with sub marker '_'
603       dirima=dir([filebase '_*' num2str(first_i) '.*']);
604       if isempty(dirima)
605          % look for other images series
606          dirima=dir([filebase '*' num2str(first_i) '.*']);
607          if ~isempty(dirima)
608              [pp,ff,fc,str2,str_a,str_b,ext_ima,nom_type_ima]=name2display(dirima(1).name);
609          end
610       else
611           [pp,ff,fc,str2,str_a,str_b,ext_ima,nom_type_ima]=name2display(dirima(1).name);
612       end
613    else
614       [pp,ff,fc,str2,str_a,str_b,ext_ima,nom_type_ima]=name2display(dirima(1).name);
615    end
616end   
617   
618% no image documentation file found: look for a series of existing images or .nc files
619if isempty(time) && ~isequal(ext,'.nc') && ~strcmp(nom_type_ima,'none') && ~strcmp(nom_type_ima,'') && ~strcmp(nom_type_ima,'*')
620    subdir=get(handles.subdir_civ1,'String');
621    incr_pair=[0 0];%default
622    if isfield(browse,'incr_pair')
623            incr_pair=browse.incr_pair;
624    end
625%     nbdetect=0;%test of detected images
626    field_i=field_count;
627    idetect=1;
628    while idetect==1 %look for the maximum file number in the series     
629        imagename=name_generator(filebase,field_i+1,1,ext_ima,nom_type_ima);
630        idetect=(exist(imagename,'file')==2);
631        if idetect
632            field_i=field_i+1;
633        end
634            %SEE CASE OF NETCDF FILES
635%             nbdetect=nbdetect+(exist(imagename,'file')==2);
636    end
637    nb_field=field_i;% last detected field number
638    field_i=field_count;%look for the minimum file number in the series
639    idetect=1;
640    while idetect==1
641        imagename=name_generator(filebase,field_i-1,1,ext_ima,nom_type_ima);
642        idetect=(exist(imagename,'file')==2);
643        if idetect
644           field_i=field_i-1;
645        end
646    end
647    first_i=max(field_i,1);
648        %determine the set of times and possible intervals for CIV
649 %   dt=(1/1000)*str2num(get(handles.dt,'String'));
650    time=[0:nb_field-1]';% time=file index -1  by default
651    set(handles.mode,'String',{'series(Di)'})
652end
653% if isequal(nom_type_ima,'none')% no file numbering used
654%   first_i=1;
655%   last_i=1;
656%    first_j=1;
657%   last_j=1;
658% end
659if exist('time','var')
660    if size(time,1)+size(time,2)>=3 % if there are at least two time values to define dt
661        nbfield=size(time,1);
662        nbfield2=size(time,2);
663        set(handles.RootName,'UserData',time); %store the set of times
664        set(handles.dt_unit,'String',['dt in m' TimeUnit]);
665        set(handles.dt_unit_civ2,'String',['dt in m' TimeUnit]);
666        set(handles.TimeUnit,'String',TimeUnit);
667        set(handles.nb_field,'String',num2str(nbfield));
668        set(handles.nb_field2,'String',num2str(nbfield2));
669    end
670end
671set(handles.CoordUnit,'String',CoordUnit)
672set(handles.calcul_search,'UserData',[pxcmx_search pxcmy_search]);
673% npxy=[npy npx];
674set(handles.ImaExt,'String',ext_ima)
675set(handles.first_i,'String',num2str(first_i));
676set(handles.last_i,'String',num2str(last_i));%
677set(handles.first_j,'String',num2str(first_j));
678set(handles.last_j,'String',num2str(last_j));%
679browse.nom_type_ima=nom_type_ima;
680set(handles.browse_root,'UserData',browse)% store the nomenclature type
681
682        %%%%%%%%%%%  set the menus of image pairs and default selection for civ   %%%%%%%%%%%%%%%%%%%
683if isequal(nom_type_ima,'_i')|| isequal(nom_type_nc,'_i1-i2')||~(exist('nbfield2','var')&&(nbfield2~=1))
684    set(handles.mode,'String',{'series(Di)'})
685    set(handles.mode,'Value',1)
686elseif isequal(nom_type_ima,'png_old')||isequal(nom_type_nc,'netc_old')
687    set(handles.mode,'String',{'pair j1-j2'})
688    set(handles.mode,'Value',1)
689elseif (nbfield==1)% simple series in j
690    set(handles.mode,'String',{'series(Dj)'})
691    set(handles.mode,'Value',1)
692else
693    set(handles.mode,'String',{'pair j1-j2';'series(Dj)';'series(Di)'})%multiple choice
694    if nbfield2 <= 5
695         set(handles.mode,'Value',1)% advice 'pair j1-j2' for small burst
696    end
697%     else
698%     if isequal(mode,'volume')
699%         set(handles.mode,'Value',3)
700%     elseif nbfield2 <= 5
701%         set(handles.mode,'Value',1)% advice 'pair j1-j2' for small bursts
702%     else
703%         set(handles.mode,'Value',2)% advice series Dj for long bursts, not volume
704%     end
705end
706
707%update the subdir
708pathdir=fileparts(filebase);%path to the current xml file
709listot=dir(pathdir);
710idir=0;
711listdir={''};%default
712for ilist=1:length(listot)
713    if listot(ilist).isdir
714        name=listot(ilist).name;
715        if ~isequal(name,'.') && ~isequal(name,'..')
716            idir=idir+1;
717            listdir{idir,1}=listot(ilist).name;
718        end
719    end
720end
721set(handles.list_subdir_civ1,'Value',1)
722set(handles.list_subdir_civ2,'Value',1)
723set(handles.list_subdir_civ1,'String',['browse...';listdir])
724set(handles.list_subdir_civ2,'String',['browse...';listdir])
725%check wether the current subdir exists:
726subdir_civ1=get(handles.subdir_civ1,'String');
727subdir_civ2=get(handles.subdir_civ2,'String');
728
729mode_Callback(hObject, eventdata, handles) 
730
731%%%%%% initialize waitbars and RUN button
732set(handles.waitbar_1,'Position',[0.946 0.876 0.03 0.001])
733set(handles.waitbar_patch1,'Position',[0.946 0.439 0.03 0.001])
734set(handles.waitbar_civ2,'Position',[0.946 0.219 0.03 0.001])
735set(handles.waitbar_patch2,'Position',[0.946 0.0 0.03 0.001])
736set(handles.RUN, 'Enable','On')
737set(handles.RUN,'BackgroundColor',[1 0 0])
738if isequal(get(handles.BATCH, 'Enable'),'On')
739    set(handles.BATCH,'BackgroundColor',[1 0 0])
740end
741   
742%%%%% store the root input filename for future opening
743dir_perso=prefdir;
744profil_perso=fullfile(prefdir,'uvmat_perso.mat');
745RootPath=fileparts(filebase);
746if exist(profil_perso,'file')
747    save (profil_perso,'RootPath','-append'); %store the root name for future opening of uvmat
748else
749    txt=ver;
750    Release=txt(1).Release;
751    relnumb=str2num(Release(3:4));
752    if relnumb >= 14
753        save (profil_perso,'RootPath','-V6'); %store the root name for future opening of uvmat
754    else
755        save (profil_perso,'RootPath'); %store the root name for future opening of uvmat
756    end
757end
758% save(profil_perso, 'filebase'); %store the root name for future opening of uvmat
759set(gcf,'Pointer','arrow')
760
761%------------------------------------------------------------------------
762% --- Executes on button press in mode.
763function mode_Callback(hObject, eventdata, handles)
764%------------------------------------------------------------------------
765browse=get(handles.browse_root,'UserData');
766compare_list=get(handles.compare,'String');
767val=get(handles.compare,'Value');
768compare=compare_list{val};
769if strcmp(compare,'displacement')
770    mode='displacement';
771else
772    mode_list=get(handles.mode,'String');
773    mode_value=get(handles.mode,'Value');
774    mode=mode_list{mode_value};
775end
776displ_num=[];%default
777ref_i=str2num(get(handles.ref_i,'String'));
778% last_i=str2num(get(handles.last_i,'String'));
779time=get(handles.RootName,'UserData'); %get the set of times
780siztime=size(time);
781nbfield=siztime(1);
782nbfield2=siztime(2);
783indchosen=1;  %%first pair selected by default
784if isequal(mode,'pair j1-j2')%| isequal(mode,'st_pair j1-j2')
785    dt=1;
786    displ='';
787    index=0;
788    numlist_a=[];
789    numlist_B=[];
790    %get all the time intervals in bursts
791    displ_dt=1;%default
792    nbfield2=min(nbfield2,10);%limitate the number of pairs to 10x10
793    for numod_a=1:nbfield2-1 %nbfield2 always >=2 for 'pair j1-j2' mode
794        for numod_b=(numod_a+1):nbfield2
795             index=index+1;
796             numlist_a(index)=numod_a;
797             numlist_b(index)=numod_b;
798             if ~isempty(time)
799                dt(numod_a,numod_b)=time(ref_i,numod_b)-time(ref_i,numod_a);%first time interval dt
800                displ_dt(index)=dt(numod_a,numod_b);
801             else
802                 displ_dt(index)=1;
803             end
804         end
805     end
806     [dtsort,indsort]=sort(displ_dt);
807     if ~isempty(numlist_a)
808     displ_num(1,:)=numlist_a(indsort);
809     displ_num(2,:)=numlist_b(indsort);
810     end
811     displ_num(3,:)=0;
812     displ_num(4,:)=0;
813     set(handles.jtext,'Visible','Off')
814    set(handles.first_j,'Visible','Off')
815    set(handles.last_j,'Visible','Off')
816    set(handles.incr_j,'Visible','Off')
817    set(handles.nb_field2,'Visible','Off')
818    set(handles.ref_j,'Visible','Off')
819elseif isequal(mode,'series(Dj)') %| isequal(mode,'st_series(Dj)')
820     for index=1:min(nbfield2-1,200)
821         displ_num(1,index)=-floor(index/2);
822         displ_num(2,index)=ceil(index/2);
823         displ_num(3,index)=0;
824         displ_num(4,index)=0;
825     end       
826     set(handles.jtext,'Visible','On')
827     set(handles.first_j,'Visible','On')
828     set(handles.last_j,'Visible','On')
829     set(handles.incr_j,'Visible','On')
830     set(handles.nb_field2,'Visible','On')
831     set(handles.ref_j,'Visible','On')
832     if nbfield > 1
833        set(handles.itext,'Visible','On')
834        set(handles.first_i,'Visible','On')
835        set(handles.last_i,'Visible','On')
836        set(handles.incr_i,'Visible','On')
837        set(handles.nb_field,'Visible','On')
838        set(handles.ref_i,'Visible','On')
839    else
840        set(handles.itext,'Visible','Off')
841        set(handles.first_i,'Visible','Off')
842        set(handles.last_i,'Visible','Off')
843        set(handles.incr_i,'Visible','Off')
844        set(handles.nb_field,'Visible','Off')
845        set(handles.ref_i,'Visible','Off')
846    end
847elseif isequal(mode,'series(Di)') %| isequal(mode,'st_series(Di)')
848     for index=1:200%min(nbfield-1,200)
849         displ_num(1,index)=0;
850         displ_num(2,index)=0;
851         displ_num(3,index)=-floor(index/2);
852         displ_num(4,index)=ceil(index/2);
853     end
854     set(handles.itext,'Visible','On')
855     set(handles.first_i,'Visible','On')
856     set(handles.last_i,'Visible','On')
857     set(handles.incr_i,'Visible','On')
858     set(handles.nb_field,'Visible','On')
859     set(handles.ref_i,'Visible','On')
860     if nbfield2 > 1
861        set(handles.jtext,'Visible','On')
862        set(handles.first_j,'Visible','On')
863        set(handles.last_j,'Visible','On')
864        set(handles.incr_j,'Visible','On')
865        set(handles.nb_field2,'Visible','On')
866        set(handles.ref_j,'Visible','On')
867     else
868        set(handles.jtext,'Visible','Off')
869        set(handles.first_j,'Visible','Off')
870        set(handles.last_j,'Visible','Off')
871        set(handles.incr_j,'Visible','Off')
872        set(handles.nb_field2,'Visible','Off')
873        set(handles.ref_j,'Visible','Off')
874    end
875elseif isequal(mode,'displacement')%the pairs have the same indices
876     displ_num(1,1)=0;
877     displ_num(2,1)=0;
878     displ_num(3,1)=0;
879     displ_num(4,1)=0;
880     if nbfield > 1
881        set(handles.itext,'Visible','On')
882        set(handles.first_i,'Visible','On')
883        set(handles.last_i,'Visible','On')
884        set(handles.incr_i,'Visible','On')
885        set(handles.nb_field,'Visible','On')
886        set(handles.ref_i,'Visible','On')
887    else
888        set(handles.itext,'Visible','Off')
889        set(handles.first_i,'Visible','Off')
890        set(handles.last_i,'Visible','Off')
891        set(handles.incr_i,'Visible','Off')
892        set(handles.nb_field,'Visible','Off')
893        set(handles.ref_i,'Visible','Off')
894    end
895    if nbfield2 > 1
896      set(handles.jtext,'Visible','On')
897     set(handles.first_j,'Visible','On')
898     set(handles.last_j,'Visible','On')
899     set(handles.incr_j,'Visible','On')
900     set(handles.nb_field2,'Visible','On')
901     set(handles.ref_j,'Visible','On')
902    else
903        set(handles.jtext,'Visible','Off')
904        set(handles.first_j,'Visible','Off')
905        set(handles.last_j,'Visible','Off')
906        set(handles.incr_j,'Visible','Off')
907        set(handles.nb_field2,'Visible','Off')
908        set(handles.ref_j,'Visible','Off')
909    end
910end 
911set(handles.list_pair_civ1,'UserData',displ_num);
912find_netcpair_civ1(hObject, eventdata, handles)
913find_netcpair_civ2(hObject, eventdata, handles)
914
915%------------------------------------------------------------------------
916% determine the menu for civ1 pairs depending on existing netcdf file at the middle of
917% the field series set by first_i, incr, last_i
918function find_netcpair_civ1(hObject, eventdata, handles)
919%------------------------------------------------------------------------
920set(gcf,'Pointer','watch')
921%nomenclature types
922filebase=get(handles.RootName,'String');
923[filepath,Nme,ext_dir]=fileparts(filebase);
924browse=get(handles.browse_root,'UserData');
925compare_list=get(handles.compare,'String');
926val=get(handles.compare,'Value');
927compare=compare_list{val};
928if strcmp(compare,'displacement')
929    mode='displacement';
930else
931    mode_list=get(handles.mode,'String');
932    mode_value=get(handles.mode,'Value');
933    mode=mode_list{mode_value};
934end
935
936% nomenclature type of the .nc files
937nom_type_ima=[];%default
938if isfield(browse,'nom_type_ima')
939    nom_type_ima=browse.nom_type_ima;
940end
941
942%determine nom_type_nc:
943nom_type_nc=[];%default
944if isfield(browse,'nom_type_nc')
945    nom_type_nc=browse.nom_type_nc;
946end
947if isempty(nom_type_nc)
948     [nom_type_nc]=nomtype2pair(nom_type_ima,isequal(mode,'series(Di)'),isequal(mode,'series(Dj)'));
949end
950browse.nom_type_nc=nom_type_nc;
951set(handles.browse_root,'UserData',browse)
952
953%reads .nc subdirectoy and image numbers from the interface
954subdir_civ1=get(handles.subdir_civ1,'String');%subdirectory subdir_civ1 for the netcdf data
955% first_i=str2num(get(handles.first_i,'String'));
956% last_i=str2num(get(handles.last_i,'String'));
957% incr=str2num(get(handles.incr_i,'String'));
958% num1=first_i:incr:last_i;
959% if isempty(num1)
960%     set(handles.list_pair_civ1,'String',{''});
961%     return
962% end
963ref_i=str2double(get(handles.ref_i,'String'));
964if isequal(mode,'pair j1-j2')%|isequal(mode,'st_pair j1-j2')
965    ref_j=0;
966else
967    ref_j=str2double(get(handles.ref_j,'String'));
968end
969time=get(handles.RootName,'UserData');%get the set of times
970if isempty(time)
971    time=[0 1];
972end
973%dt_unit=str2double(get(handles.dt,'String'));% used when there is no image documentation file
974dt_unit=1000;%default
975displ_num=get(handles.list_pair_civ1,'UserData');
976
977%eliminate the first pairs inconsistent with the position
978 if isempty(displ_num)
979     nbpair=0;
980 else
981    nbpair=length(displ_num(1,:));%nbre of displayed pairs
982    if  isequal(mode,'series(Di)')  %| isequal(mode,'st_series(Di)')
983        nbpair=min(2*ref_i-1,nbpair);%limit the number of pairs with positive first index
984    elseif  isequal(mode,'series(Dj)')% | isequal(mode,'st_series(Dj)')
985        nbpair=min(2*ref_j-1,nbpair);%limit the number of pairs with positive first index
986    end
987 end
988nbpair=min(200,nbpair);%limit the number of displayed pairs to 200
989
990%look for existing processed pairs involving the field at the middle of the series if civ1 will not
991% be performed, while the result is needed for next steps.
992displ_pair={''};
993select=ones(size(1:nbpair));%default =1 for numbers of displayed pairs
994testpair=0;
995if get(handles.CIV1,'Value')==0 %
996    if ~exist(fullfile(filepath,subdir_civ1,ext_dir),'dir')
997         msgbox_uvmat('ERROR',['no civ1 file available: subdirectory ' subdir_civ1 ' does not exist']);
998         set(handles.list_pair_civ1,'String',{});
999         return
1000    end
1001    for ipair=1:nbpair   
1002        filename=name_generator(filebase,ref_i+displ_num(3,ipair),ref_j+displ_num(1,ipair),'.nc',nom_type_nc,1,...
1003        ref_i+displ_num(4,ipair),ref_j+displ_num(2,ipair),subdir_civ1);
1004        select(ipair)=exist(filename,'file')==2;
1005    end
1006    if ~exist('select','var') || isequal(select,zeros(size(1:nbpair)))
1007        if isfield(browse,'incr_pair')
1008            num_i1=ref_i-floor(browse.incr_pair(1)/2);
1009            num_i2=ref_i+ceil(browse.incr_pair(1)/2);
1010            num_j1=ref_j-floor(browse.incr_pair(2)/2);
1011            num_j2=ref_j+ceil(browse.incr_pair(2)/2);
1012            filename=name_generator(filebase,num_i1,num_j1,'.nc',nom_type_nc,1,num_i2,num_j2,subdir_civ1);
1013            select(1)=exist(filename,'file')==2;
1014            testpair=1;
1015        else
1016            if  isequal(mode,'series(Dj)')% | isequal(mode,'st_series(Dj)')
1017                msgbox_uvmat('ERROR',['no civ1 file available for the selected reference index j=' num2str(ref_j) ' and subdirectory ' subdir_civ1]);
1018            else
1019                msgbox_uvmat('ERROR',['no civ1 file available for the selected reference index i=' num2str(ref_i) ' and subdirectory ' subdir_civ1]);
1020            end
1021             set(handles.list_pair_civ1,'String',{''});
1022             %COMPLETER CAS STEREO
1023            return
1024        end
1025    end
1026end
1027if isequal(mode,'series(Di)') %| isequal(mode,'st_series(Di)')
1028    if testpair
1029        displ_pair{1}=['Di= ' num2str(-floor(browse.incr_pair(1)/2)) '|' num2str(ceil(browse.incr_pair(1)/2))];
1030        %     elseif ~isequal(get(handles.root_txt,'String'),'dt(ms)=')
1031        %        for ipair=1:nbpair
1032        %           if select(ipair)
1033        %               if size(time,1)>=ref_i+displ_num(4,ipair) && size(time,2)>=ref_j+displ_num(2,ipair)
1034        %               dt=time(ref_i+displ_num(4,ipair),ref_j+displ_num(2,ipair))-time(ref_i+displ_num(3,ipair),ref_j+displ_num(1,ipair));%time interval dt
1035        %               displ_pair{ipair}=['Di= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ' :dt= ' num2str(dt*1000)];
1036        %               end
1037        %           else
1038        %              displ_pair{ipair}='...'; %pair not displayed in the menu
1039        %           end
1040        %        end
1041    else
1042        for ipair=1:nbpair
1043            if select(ipair)
1044                if size(time,1)>=ref_i+displ_num(4,ipair) && size(time,2)>=ref_j+displ_num(2,ipair)
1045                    dt=time(ref_i+displ_num(4,ipair),ref_j+displ_num(2,ipair))-time(ref_i+displ_num(3,ipair),ref_j+displ_num(1,ipair));%time interval dt
1046                    displ_pair{ipair}=['Di= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ' :dt= ' num2str(dt*1000)];
1047                else
1048                    displ_pair{ipair}='...'; %pair not displayed in the menu
1049                end
1050            end
1051        end
1052    end
1053elseif isequal(mode,'series(Dj)')%|isequal(mode,'st_series(Dj)')% series on the j index
1054    if testpair
1055         displ_pair{1}=['Dj= ' num2str(-floor(browse.incr_pair(1)/2)) '|' num2str(ceil(browse.incr_pair(1)/2))];
1056    else
1057       for ipair=1:nbpair
1058          if select(ipair)
1059              if size(time,1)>=ref_i+displ_num(4,ipair) && size(time,2)>=ref_j+displ_num(2,ipair)
1060              dt=time(ref_i+displ_num(4,ipair),ref_j+displ_num(2,ipair))-time(ref_i+displ_num(3,ipair),ref_j+displ_num(1,ipair));%time interval dt
1061              displ_pair{ipair}=['Dj= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ' :dt= ' num2str(dt*1000)];
1062              end
1063           elseif testpair
1064              displ_pair{1}=['Dj= ' num2str(-floor(browse.incr_pair(2)/2)) '|' num2str(ceil(browse.incr_pair(2)/2))];
1065          else
1066             displ_pair{ipair}='...'; %pair not displayed in the menu
1067          end
1068       end
1069   end
1070elseif isequal(mode,'pair j1-j2')%|isequal(mode,'st_pair j1-j2')%case of pairs
1071    for ipair=1:nbpair
1072        if select(ipair)
1073           dt=time(ref_i+displ_num(4,ipair),displ_num(2,ipair))-time(ref_i+displ_num(3,ipair),displ_num(1,ipair));%time interval dt
1074           displ_pair{ipair}=['j= ' num2stra(displ_num(1,ipair),nom_type_ima) '-' num2stra(displ_num(2,ipair),nom_type_ima) ...
1075                        ' :dt= ' num2str(dt*1000)];
1076       else
1077          displ_pair{ipair}='...'; %pair not displayed in the menu
1078        end   
1079   end
1080elseif isequal(mode,'displacement')
1081    displ_pair={'Di=Dj=0'}; 
1082end   
1083set(handles.list_pair_civ1,'String',displ_pair');
1084ichoice=min(find(select));
1085if (isempty(ichoice) || ichoice < 1); ichoice=1; end;
1086initial=get(handles.list_pair_civ1,'Value');%initial choice of pair
1087if initial>nbpair
1088     set(handles.list_pair_civ1,'Value',ichoice);% first valid pair proposed by default in the menu
1089end
1090if numel(select)>=initial && ~isequal(select(initial),1)
1091    set(handles.list_pair_civ1,'Value',ichoice);% first valid pair proposed by default in the menu
1092end
1093
1094%set(handles.list_pair_civ2,'String',displ_pair');
1095initial=get(handles.list_pair_civ2,'Value');
1096if initial>length(displ_pair')%|~isequal(select(initial),1)
1097    if ichoice <= length(displ_pair')
1098        set(handles.list_pair_civ2,'Value',ichoice);% same pair proposed by default for civ2
1099    else
1100        set(handles.list_pair_civ2,'Value',1);% same pair proposed by default for civ2
1101    end
1102end
1103set(handles.list_pair_civ2,'String',displ_pair');
1104set(gcf,'Pointer','arrow')
1105
1106%------------------------------------------------------------------------
1107% determine the menu for civ2 pairs depending on the existing netcdf file at the
1108%middle of the series set by first_i, incr, last_i
1109function find_netcpair_civ2(hObject, eventdata, handles)
1110%------------------------------------------------------------------------
1111set(gcf,'Pointer','watch')
1112%nomenclature types
1113filebase=get(handles.RootName,'String');
1114[filepath,Nme,ext_dir]=fileparts(filebase);
1115browse=get(handles.browse_root,'UserData');
1116compare_list=get(handles.compare,'String');
1117val=get(handles.compare,'Value');
1118compare=compare_list{val};
1119if strcmp(compare,'displacement')
1120    mode='displacement';
1121else
1122    mode_list=get(handles.mode,'String');
1123    mode_value=get(handles.mode,'Value');
1124    mode=mode_list{mode_value};
1125end
1126
1127% nomenclature type of the .nc files
1128nom_type_ima='ima_num';%default
1129if isfield(browse,'nom_type_ima')
1130    nom_type_ima=browse.nom_type_ima;
1131end
1132nom_type_nc='_i1-i2';%default
1133if isfield(browse,'nom_type_nc')
1134    nom_type_nc=browse.nom_type_nc;
1135end
1136if isequal(nom_type_ima,'png_old') | isequal(nom_type_ima,'netc_old')| isequal(nom_type_ima,'raw_SMD')| isequal(nom_type_nc,'netc_old')
1137    nom_type_nc='netc_old';%nom_type for the netcdf files
1138elseif isequal(nom_type_ima,'none')|isequal(nom_type_nc,'none')
1139    nom_type_nc='none';
1140elseif isequal(nom_type_ima,'avi')|isequal(nom_type_ima,'_i')|isequal(nom_type_ima,'ima_num')|isequal(nom_type_nc,'_i1-i2')
1141     nom_type_nc='_i1-i2';
1142else
1143    if  isequal(mode,'series(Di)')%|isequal(mode,'st_series(Di)')
1144        nom_type_nc='_i1-i2_j'; % PIV in volume
1145    else
1146        nom_type_nc='_i_j1-j2';
1147    end   
1148end
1149browse.nom_type_nc=nom_type_nc;
1150set(handles.browse_root,'UserData',browse)
1151
1152%reads .nc subdirectory and image numbers from the interface
1153subdir_civ1=get(handles.subdir_civ1,'String');%subdirectory subdir_civ1 for the netcdf data
1154subdir_civ2=get(handles.subdir_civ2,'String');%subdirectory subdir_civ2 for the netcdf data
1155% first_i=str2num(get(handles.first_i,'String'));
1156% last_i=str2num(get(handles.last_i,'String'));
1157% incr=str2num(get(handles.incr_i,'String'));
1158% num1=first_i:incr:last_i;
1159% if isempty(num1)
1160%     set(handles.list_pair_civ2,'Value',1);
1161%     set(handles.list_pair_civ2,'String',{''});
1162%     return
1163% end
1164ref_i=str2num(get(handles.ref_i_civ2,'String'));
1165if isequal(mode,'pair j1-j2')%|isequal(mode,'st_pair j1-j2')
1166    ref_j=0;
1167else
1168    ref_j=str2num(get(handles.ref_j_civ2,'String'));
1169end
1170time=get(handles.RootName,'UserData'); %get the set of times
1171if isempty(time)
1172    time=[0 1];%default
1173end
1174%dt_unit=str2num(get(handles.dt,'String'));% used when there is no image documentation file
1175%dt_unit=1000;
1176displ_num=get(handles.list_pair_civ1,'UserData');
1177
1178
1179%eliminate the first pairs inconsistent with the position
1180if isempty(displ_num)
1181    nbpair=0;
1182else
1183    nbpair=length(displ_num(1,:));%nbre of displayed pairs
1184    if  isequal(mode,'series(Di)')% | isequal(mode,'st_series(Di)')
1185        nbpair=min(2*ref_i-1,nbpair);%limit the number of pairs with positive first index
1186    elseif  isequal(mode,'series(Dj)')% | isequal(mode,'st_series(Dj)')
1187        nbpair=min(2*ref_j-1,nbpair);%limit the number of pairs with positive first index
1188    end
1189end
1190nbpair=min(200,nbpair);%limit the number of displayed pairs to 200
1191
1192%look for existing processed pairs involving the field at the middle of the series if civ1 will not
1193% be performed, while the result is needed for next steps.
1194displ_pair={''}; %default
1195select=ones(size(1:nbpair));%default =1 for numbers of displayed pairs
1196if get(handles.CIV2,'Value')==0 & get(handles.CIV1,'Value')==0 & get(handles.FIX1,'Value')==0 & get(handles.PATCH1,'Value')==0%&...
1197    if ~exist(fullfile(filepath,subdir_civ2,ext_dir),'dir')
1198         errordlg(['no civ2 file available: subdirectory ' subdir_civ2 ' does not exist'])
1199         set(handles.list_pair_civ2,'Value',1);
1200         set(handles.list_pair_civ2,'String',{''});
1201         return
1202    end
1203    for ipair=1:nbpair       
1204        filename=name_generator(filebase,ref_i+displ_num(3,ipair),ref_j+displ_num(1,ipair),'.nc',nom_type_nc,1,...
1205        ref_i+displ_num(4,ipair),ref_j+displ_num(2,ipair),subdir_civ1);
1206        select(ipair)=exist(filename,'file')==2;
1207    end
1208    if  isequal(select,zeros(size(1:nbpair)))
1209        if isfield(browse,'incr_pair')
1210            num_i1=ref_i-floor(browse.incr_pair(1)/2);
1211            num_i2=ref_i+floor((browse.incr_pair(1)+1)/2);
1212            num_j1=ref_j-floor(browse.incr_pair(2)/2);
1213            num_j2=ref_j+floor((browse.incr_pair(2)+1)/2);
1214            filename=name_generator(filebase,num_i1,num_j1,'.nc',nom_type_nc,1,num_i2,num_j2,subdir_civ2);
1215            select(1)=exist(filename,'file')==2;
1216        else
1217            if  isequal(mode,'series(Dj)')% | isequal(mode,'st_series(Dj)')
1218                errordlg(['no civ2 file available for the selected reference index j=' num2str(ref_j) ' and subdirectory ' subdir_civ2])
1219            else
1220                errordlg(['no civ2 file available for the selected reference index i=' num2str(ref_i) ' and subdirectory ' subdir_civ2])
1221            end
1222             set(handles.list_pair_civ2,'Value',1);
1223             set(handles.list_pair_civ2,'String',{''});
1224            return
1225        end
1226    end
1227end
1228if isequal(mode,'series(Di)') % | isequal(mode,'st_series(Di)')
1229       for ipair=1:nbpair
1230          if select(ipair)
1231              if size(time,1)>=ref_i+displ_num(4,ipair) && size(time,2)>=ref_j+displ_num(2,ipair)
1232                dt=time(ref_i+displ_num(4,ipair),ref_j+displ_num(2,ipair))-time(ref_i+displ_num(3,ipair),ref_j+displ_num(1,ipair));%time interval dt
1233                displ_pair{ipair}=['Di= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ' :dt= ' num2str(dt*1000)];
1234              end
1235          else
1236             displ_pair{ipair}='...'; %pair not displayed in the menu
1237          end
1238       end
1239elseif isequal(mode,'series(Dj)') %| isequal(mode,'st_series(Dj)') % series on the j index
1240       for ipair=1:nbpair
1241          if select(ipair)
1242              if size(time,1)>=ref_i+displ_num(4,ipair) && size(time,2)>=ref_j+displ_num(2,ipair)
1243              dt=time(ref_i+displ_num(4,ipair),ref_j+displ_num(2,ipair))-time(ref_i+displ_num(3,ipair),ref_j+displ_num(1,ipair));%time interval dt
1244              displ_pair{ipair}=['Dj= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ' :dt= ' num2str(dt*1000)];
1245              end
1246          else
1247             displ_pair{ipair}='...'; %pair not displayed in the menu
1248          end
1249       end
1250elseif isequal(mode,'pair j1-j2')% | isequal(mode,'st_pair j1-j2') %case of pairs
1251    for ipair=1:nbpair
1252        if select(ipair)
1253           dt=time(ref_i+displ_num(4,ipair),displ_num(2,ipair))-time(ref_i+displ_num(3,ipair),displ_num(1,ipair));%time interval dt
1254           displ_pair{ipair}=['j= ' num2stra(displ_num(1,ipair),nom_type_ima) '-' num2stra(displ_num(2,ipair),nom_type_ima) ...
1255                        ' :dt= ' num2str(dt*1000)];
1256       else
1257          displ_pair{ipair}='...'; %pair not displayed in the menu
1258       end   
1259   end
1260elseif isequal(mode,'displacement')
1261    displ_pair={'Di=Dj=0'}; 
1262end   
1263val=get(handles.list_pair_civ2,'Value');
1264ichoice=min(find(select));
1265if (isempty(ichoice) | ichoice < 1); ichoice=1; end;
1266if get(handles.CIV2,'Value')==0 & get(handles.CIV1,'Value')==0 & get(handles.FIX1,'Value')==0 & get(handles.PATCH1,'Value')==0
1267    val=ichoice;% first valid pair proposed by default in the menu
1268end
1269if val>length(displ_pair')
1270    set(handles.list_pair_civ2,'Value',1);% first valid pair proposed by default in the menu
1271else
1272    set(handles.list_pair_civ2,'Value',val);
1273end
1274set(handles.list_pair_civ2,'String',displ_pair');
1275set(gcf,'Pointer','arrow')
1276
1277%------------------------------------------------------------------------
1278%  determine the list of index pairs of processing file
1279function [num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2]=...
1280    find_pair_indices(handles,mode)
1281%------------------------------------------------------------------------
1282first_i=str2double(get(handles.first_i,'String'));%first index i
1283last_i=str2double(get(handles.last_i,'String'));%last index i
1284incr=str2double(get(handles.incr_i,'String'));% increment
1285num_i=[first_i:incr:last_i];% list of i indices (reference values for each pair)
1286if isequal(get(handles.first_j,'Visible'),'on')
1287    first_j=str2double(get(handles.first_j,'String'));%first index j
1288    last_j=str2double(get(handles.last_j,'String'));%last index j
1289    incr_j=str2double(get(handles.incr_j,'String'));% increment
1290else
1291    first_j=1;
1292    last_j=1;
1293    incr_j=1;
1294end
1295num_j=[first_j:incr_j:last_j];% list of j indices (reference values for each pair)
1296list_civ1=get(handles.list_pair_civ1,'String');
1297index_civ1=get(handles.list_pair_civ1,'Value');
1298str_civ1=list_civ1{index_civ1};%string defining the image pairs for civ1
1299if isempty(str_civ1)||isequal(str_civ1,'')
1300    msgbox_uvmat('ERROR','no image pair selected for civ1')
1301    return
1302end
1303list_civ2=get(handles.list_pair_civ2,'String');
1304index_civ2=get(handles.list_pair_civ2,'Value');
1305if index_civ2>length(list_civ2)
1306    list_civ2=list_civ1;
1307    index_civ2=index_civ1;
1308end
1309str_civ2=list_civ2{index_civ2};%string defining the image pairs for civ2
1310if isempty(first_i)||isempty(first_j), msgbox_uvmat('ERROR','first field number not defined'),...
1311    return,end;
1312if isequal(last_i,[])|| isequal(last_j,[]),msgbox_uvmat('ERROR','last field number not defined'),...
1313    return,end;
1314if isequal(incr,[])|| isequal(incr_j,[]),msgbox_uvmat('ERROR','increment in field number not defined'),...
1315    return,end;
1316if last_i < first_i || last_j < first_j , msgbox_uvmat('ERROR','last field number must be larger than the first one'),...
1317    return,end;
1318if isequal (mode,'series(Di)')
1319     %recognize the pair civ1 from the display
1320        indsel=find((double(str_civ1)<48)|(double(str_civ1)>57));% character indices of non numerical characters
1321    str_raw=str_civ1(indsel);
1322    indsepar=find(str_raw=='|'); %character index of the separator
1323    d1=str2double(str_civ1([indsel(indsepar-1)+1:indsel(indsepar)-1]));
1324    if indsepar==length(str_raw)
1325        d2=str2double(str_civ1([indsel(indsepar)+1:end]));
1326    else
1327        d2=str2double(str_civ1([indsel(indsepar)+1:indsel(indsepar+1)-1]));
1328    end   
1329    num1_civ1=num_i-d1;% set of first image numbers
1330    num2_civ1=num_i+d2;
1331    num_a_civ1=num_j;
1332    num_b_civ1=num_j;
1333   
1334    %recognize the pair civ2 from the display
1335    indsel=find((double(str_civ2)<48)|(double(str_civ2)>57));% character indices of non numerical characters
1336    str_raw=str_civ2(indsel);
1337    indsepar=find(str_raw=='|'); %character index of the separator
1338    d1=str2num(str_civ2([indsel(indsepar-1)+1:indsel(indsepar)-1]));
1339    if indsepar==length(str_raw)
1340        d2=str2num(str_civ2([indsel(indsepar)+1:end]));
1341    else
1342        d2=str2num(str_civ2([indsel(indsepar)+1:indsel(indsepar+1)-1]));
1343    end   
1344    if isempty(d1)
1345        num1_civ2=num_i;
1346    else
1347        num1_civ2=num_i-d1;% set of first image numbers
1348    end
1349    if isempty(d2)
1350        num2_civ2=num_i;
1351    else
1352        num2_civ2=num_i+d2;
1353    end
1354    num_a_civ2=num_j;
1355    num_b_civ2=num_j;
1356   
1357    % adjust the first and last field number
1358    lastfield=str2num(get(handles.nb_field,'String'));
1359    if isequal(lastfield,[])
1360        indsel=find((num1_civ1 >= 1)&(num1_civ2 >= 1));
1361    else
1362        indsel=find((num2_civ1 <= lastfield)&(num2_civ2 <= lastfield)&(num1_civ1 >= 1)&(num1_civ2 >= 1));
1363    end
1364    if length(indsel)>=1
1365        firstind=indsel(1);
1366        lastind=indsel(end);
1367        set(handles.first_i,'String',num2str(num_i(firstind)))%update the display of first and last fields
1368        set(handles.last_i,'String',num2str(num_i(lastind)))
1369        num_i=num_i(indsel);
1370        num1_civ1=num1_civ1(indsel);
1371        num1_civ2=num1_civ2(indsel);
1372        num2_civ1=num2_civ1(indsel);
1373        num2_civ2=num2_civ2(indsel);
1374    end
1375elseif isequal (mode,'series(Dj)')
1376    lastfield_j=str2num(get(handles.nb_field2,'String'));
1377    num1_civ1=num_i;% set of first image numbers
1378    num2_civ1=num_i;
1379    num_a_civ1=num_j-floor(index_civ1/2)*ones(size(num_j));
1380    num_b_civ1=num_j+ceil(index_civ1/2)*ones(size(num_j));
1381    num1_civ2=num_i;
1382    num2_civ2=num_i;
1383    num_a_civ2=num_j-floor(index_civ2/2)*ones(size(num_j));
1384    num_b_civ2=num_j+ceil(index_civ2/2)*ones(size(num_j));
1385    % adjust the first and last field number
1386    if isequal(lastfield_j,[])
1387        indsel=find((num_a_civ1 >= 1)&(num_a_civ2 >= 1));
1388    else
1389        indsel=find((num_b_civ1 <= lastfield_j)&(num_b_civ2 <= lastfield_j)&(num_a_civ1 >= 1)&(num_a_civ2 >= 1));
1390    end
1391    if length(indsel)>=1
1392        firstind=indsel(1);
1393        lastind=indsel(end);
1394        set(handles.first_j,'String',num2str(num_j(firstind)))%update the display of first and last fields
1395        set(handles.last_j,'String',num2str(num_j(lastind)))
1396        num_j=num_j(indsel);
1397        num_a_civ1=num_a_civ1(indsel);
1398        num_a_civ2=num_a_civ2(indsel);
1399        num_b_civ1=num_b_civ1(indsel);
1400        num_b_civ2=num_b_civ2(indsel);
1401    end
1402elseif isequal(mode,'pair j1-j2') %case of bursts (png_old or png_2D)
1403    num1_civ1=num_i;
1404    num1_civ2=num_i;
1405    displ_num=get(handles.list_pair_civ1,'UserData');
1406    num2_civ1=num_i;
1407    num_a_civ1=displ_num(1,index_civ1);
1408    num_b_civ1=displ_num(2,index_civ1);
1409    num2_civ2=num_i;
1410    num_a_civ2=displ_num(1,index_civ2);
1411    num_b_civ2=displ_num(2,index_civ2);
1412elseif isequal(mode,'displacement')
1413    num1_civ1=num_i;
1414    num2_civ1=num_i;
1415    num_a_civ1=num_j;
1416    num_b_civ1=num_j;
1417    num1_civ2=num_i;
1418    num2_civ2=num_i;
1419    num_a_civ2=num_j;
1420    num_b_civ2=num_j;
1421end
1422
1423
1424%------------------------------------------------------------------------
1425% --- Executes on selection change in list_pair_civ1.
1426function list_pair_civ1_Callback(hObject, eventdata, handles)
1427%------------------------------------------------------------------------
1428%reproduce by default the chosen pair in the civ2 menu
1429list_pair=get(handles.list_pair_civ1,'String');%get the menu of image pairs
1430index_pair=get(handles.list_pair_civ1,'Value');
1431displ_num=get(handles.list_pair_civ1,'UserData');
1432num_a=displ_num(1,index_pair);
1433num_b=displ_num(2,index_pair);
1434list_pair2=get(handles.list_pair_civ2,'String');%get the menu of image pairs
1435if index_pair<=length(list_pair2)
1436    set(handles.list_pair_civ2,'Value',index_pair);
1437end
1438
1439%update first_i and last_i according to the chosen image pairs
1440mode_list=get(handles.mode,'String');
1441mode_value=get(handles.mode,'Value');
1442mode=mode_list{mode_value};
1443if isequal(mode,'series(Di)')
1444    first_i=str2num(get(handles.first_i,'String'));
1445    last_i=str2num(get(handles.last_i,'String'));
1446    incr_i=str2num(get(handles.incr_i,'String'));
1447    num1=first_i:incr_i:last_i;
1448    lastfield=str2num(get(handles.nb_field,'String'));
1449    if ~isequal(lastfield,[])
1450        ind=find((num1-floor(index_pair/2)*ones(size(num1))>0)& ...
1451             (num1+ceil(index_pair/2)*ones(size(num1))<=lastfield));
1452        num1=num1(ind);
1453    end
1454    set(handles.first_i,'String',num2str(num1(1)));
1455    set(handles.last_i,'String',num2str(num1(end)));
1456elseif isequal(mode,'series(Dj)')
1457    first_j=str2num(get(handles.first_j,'String'));
1458    last_j=str2num(get(handles.last_j,'String'));
1459    incr_j=str2num(get(handles.incr_j,'String'));
1460    num_j=first_j:incr_j:last_j;
1461    lastfield2=str2num(get(handles.nb_field2,'String'));
1462    if ~isequal(lastfield2,[])
1463        ind=find((num_j-floor(index_pair/2)*ones(size(num_j))>0)& ...
1464             (num_j+ceil(index_pair/2)*ones(size(num_j))<=lastfield2));
1465        num1=num_j(ind);
1466    end
1467    set(handles.first_j,'String',num2str(num1(1)));
1468    set(handles.last_j,'String',num2str(num1(end)));
1469end
1470
1471%------------------------------------------------------------------------
1472% --- Executes on selection change in list_pair_civ2.
1473function list_pair_civ2_Callback(hObject, eventdata, handles)
1474%------------------------------------------------------------------------
1475index_pair=get(handles.list_pair_civ2,'Value');%get the selected position index in the menu
1476
1477%update first_i and last_i according to the chosen image pairs
1478mode_list=get(handles.mode,'String');
1479mode_value=get(handles.mode,'Value');
1480mode=mode_list{mode_value};
1481if isequal(mode,'series(Di)')
1482    first_i=str2num(get(handles.first_i,'String'));
1483    last_i=str2num(get(handles.last_i,'String'));
1484    incr_i=str2num(get(handles.incr_i,'String'));
1485    num1=first_i:incr_i:last_i;
1486    lastfield=str2num(get(handles.nb_field,'String'));
1487    if ~isequal(lastfield,[])
1488        ind=find((num1-floor(index_pair/2)*ones(size(num1))>0)& ...
1489             (num1+ceil(index_pair/2)*ones(size(num1))<=lastfield));
1490        num1=num1(ind);
1491    end
1492    set(handles.first_i,'String',num2str(num1(1)));
1493    set(handles.last_i,'String',num2str(num1(end)));
1494elseif isequal(mode,'series(Dj)')
1495    first_j=str2num(get(handles.first_j,'String'));
1496    last_j=str2num(get(handles.last_j,'String'));
1497    incr_j=str2num(get(handles.incr_j,'String'));
1498    num_j=first_j:incr_j:last_j;
1499    lastfield2=str2num(get(handles.nb_field2,'String'));
1500    if ~isequal(lastfield2,[])
1501        ind=find((num_j-floor(index_pair/2)*ones(size(num_j))>0)& ...
1502             (num_j+ceil(index_pair/2)*ones(size(num_j))<=lastfield2));
1503        num1=num_j(ind);
1504    end
1505    set(handles.first_j,'String',num2str(num1(1)));
1506    set(handles.last_j,'String',num2str(num1(end)));
1507end
1508
1509%------------------------------------------------------------------------
1510% --- Executes on button press in RUN.
1511
1512
1513
1514% compare=get(handles.compare,'Value');%test for usual PIV (compare=1) or displacement (=2) or stereo PIV (=3)
1515% %check the list of operations:
1516% operations={'CIV1','FIX1','PATCH1','CIV2','FIX2','PATCH2'};
1517% run_flag=1;
1518% box_test(1)=get(handles.CIV1,'Value');
1519% box_test(2)=get(handles.FIX1,'Value');
1520% box_test(3)=get(handles.PATCH1,'Value');
1521% box_test(4)=get(handles.CIV2,'Value');
1522% box_test(5)=get(handles.FIX2,'Value');
1523% box_test(6)=get(handles.PATCH2,'Value');
1524% index=find(box_test==1);
1525% if isempty(index)
1526%     msgbox_uvmat('ERROR','no selected operation')
1527%     return
1528% end
1529% index_first=min(index);
1530% index_last=max(index);
1531% box_used=box_test([index_first : index_last]);
1532% [box_missing,ind_missing]=min(box_used);
1533% if isequal(box_missing,0)
1534%     msgbox_uvmat('ERROR',['missing' cell2mat(operations(ind_missing))]);
1535%     return
1536% end
1537%
1538% %root name
1539% filebase=get(handles.RootName,'String');
1540% if isempty(filebase)||isequal(filebase,'')
1541%     msgbox_uvmat('ERROR','no input files')
1542%     return
1543% end
1544% %check mask if selecetd
1545% if isequal(get(handles.get_mask_civ1,'Value'),1)
1546%     maskname=get(handles.mask_civ1,'String');
1547%     if ~exist(maskname,'file')
1548%         get_mask_civ1_Callback(hObject, eventdata, handles);
1549%     end
1550% end
1551% if isequal(get(handles.get_mask_fix1,'Value'),1)
1552%     maskname=get(handles.mask_fix1,'String');
1553%     if ~exist(maskname,'file')
1554%      get_mask_fix1_Callback(hObject, eventdata, handles);
1555%     end
1556% end
1557% if isequal(get(handles.get_mask_civ2,'Value'),1)
1558%     maskname=get(handles.mask_civ2,'String');
1559%     if ~exist(maskname,'file')
1560%      get_mask_civ2_Callback(hObject, eventdata, handles);
1561%     end
1562% end
1563% if isequal(get(handles.get_mask_fix2,'Value'),1)
1564%     maskname=get(handles.mask_fix2,'String');
1565%     if ~exist(maskname,'file')
1566%      get_mask_fix2_Callback(hObject, eventdata, handles);
1567%     end
1568% end
1569%
1570% %read names of the .exe file
1571% if box_test(1)==1 || box_test(3)==1 || box_test(4)==1 || box_test(6)==1
1572%     path_uvmat=which('uvmat');% check the path detected for source file uvmat
1573%     path_UVMAT=fileparts(path_uvmat); %path to UVMAT
1574%     %fid = fopen(fullfile(path_UVMAT,'PARAM_LINUX.txt'),'r');%open the file with civ binary names
1575%     xmlfile=fullfile(path_UVMAT,'PARAM.xml');
1576%     if exist(xmlfile,'file')
1577%         t=xmltree(xmlfile);
1578%         sparam=convert(t);
1579%     end
1580%     if isfield(sparam,'Civ1Bin')
1581%         civ1Bin=sparam.Civ1Bin;
1582%         if ~exist(civ1Bin,'file')
1583%             civ1Bin=fullfile(path_UVMAT,civ1Bin);
1584%         end
1585%     end
1586%     if isfield(sparam,'Civ2Bin')
1587%         civ2Bin=sparam.Civ2Bin;
1588%         if ~exist(civ2_exe,'file')%the binary is defined in /bin, default setting
1589%             civ2_exe=fullfile(path_UVMAT,civ2_exe);
1590%         end
1591%     end
1592%     if isfield(sparam,'Patch_exe')
1593%         patch_exe=sparam.Patch_exe;
1594%         if ~exist(patch_exe,'file')%the binary is defined in /bin, default setting
1595%             patch_exe=fullfile(path_UVMAT,patch_exe);
1596%         end
1597%     end
1598%     if isfield(sparam,'Stinterp_exe')
1599%         stinterp_exe=sparam.Stinterp_exe;
1600%     end
1601%     if isfield(sparam,'SGE')
1602%         sge=str2double(sparam.SGE);
1603%     end
1604%     if ~isunix % for windows system, check whether the Matlab working dir is a UBC name
1605%         dircur=pwd;
1606%         if ~isequal(dircur([2 3]),':\')
1607%             msgbox_uvmat('ERROR','move to a Matlab current working directory with a Windows name, e.g. m:/...')
1608%             return;
1609%         end
1610%     end
1611% end
1612%
1613% %initialize the waitbars
1614% set(handles.waitbar_1,'Position',[0.946 0.877 0.03 0.001])
1615% set(handles.waitbar_patch1,'Position',[0.946 0.626 0.03 0.001])
1616% set(handles.waitbar_civ2,'Position',[0.946 0.406 0.03 0.001])
1617% set(handles.waitbar_patch2,'Position',[0.946 0.187 0.03 0.001])
1618% set(handles.RUN,'BackgroundColor',[0.831 0.816 0.784])
1619% set(handles.RUN, 'Enable','Off')
1620% drawnow
1621%
1622% % get the list of file names and check the files
1623% display('checking the files...')
1624% [filecell,num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2,nom_type_nc,file_ref_fix1,file_ref_fix2]=...
1625%        set_civ_filenames(handles,compare,box_test);
1626% nbfield=numel(num1_civ1);
1627% nbslice=size(num_a_civ1);
1628%
1629% if isempty(filecell)
1630%    set(handles.RUN, 'Enable','On')
1631%     set(handles.RUN,'BackgroundColor',[1 0 0])
1632%     set(handles.BATCH, 'Enable','On')
1633%     set(handles.BATCH,'BackgroundColor',[1 0 0])
1634%    return
1635% end
1636% display('files OK, processing...')   
1637% nbfield=size(num1_civ1,2);
1638% nbslice=size(num1_civ1,1);
1639%
1640% %RUN CIV1
1641% if box_test(1)==1
1642%       RUN_CIV1(handles,filecell.ima1.civ1,filecell.ima2.civ1,filecell.nc.civ1,num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,nom_type_nc)
1643%       if compare==3
1644%             RUN_CIV1(handles,filecell.imaA1.civ1,filecell.imaA2.civ1,filecell.ncA.civ1,num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,nom_type_nc)
1645%      end
1646% end
1647%
1648% %RUN FIX1
1649% if box_test(2)==1
1650%     filebase=get(handles.RootName,'String');
1651%     %names of the civ1 fields
1652%     field1.vel_type='civ1';
1653%     field1.nb='nb_vectors';
1654% %     field1.X='vec_X';
1655% %     field1.Y='vec_Y';
1656% %     field1.U='vec_U';
1657% %     field1.V='vec_V';
1658%     field1.fixflag='vec_FixFlag';
1659%     flagindex(1)=get(handles.vec_Fmin2, 'Value');
1660%     flagindex(2)=get(handles.vec_F3, 'Value');
1661%     flagindex(3)=get(handles.vec_F2, 'Value');
1662%     thresh_vecC=str2double(get(handles.thresh_vecC,'String'));%threshold on image correlation vec_C
1663%     thresh_vel=str2double(get(handles.thresh_vel,'String'));%threshold on velocity modulus
1664%     inf_sup=get(handles.inf_sup1,'Value');
1665%     menu=get(handles.field_ref1,'String');
1666%     index=get(handles.field_ref1,'Value');
1667%     if isempty(menu)
1668%         fieldchoice='';
1669%     else
1670%         fieldchoice=menu{index};
1671%     end   
1672%     h = waitbar(0,'removing velocity vectors, fix1');% display a wait bar
1673%     test_mask=get(handles.get_mask_fix1,'Value');   
1674%     if test_mask
1675%         maskdispl=get(handles.mask_fix1,'String');
1676%         if exist(maskdispl,'file')
1677%             test_mask=2;
1678%             maskname=maskdispl;
1679%         else
1680%             maskbase=[get(handles.RootName,'String') '_' maskdispl]; % mask root name
1681%         end
1682%     end
1683%     for ifile=1:nbfield
1684%         waitbar(ifile/nbfield); %update the waitbar
1685%         for j=1:nbslice
1686%             if test_mask==0
1687%                 maskname='noFile use default';
1688%                 maskflag=0;
1689%             elseif test_mask==1
1690%                 nbslice_mask=str2double(maskdispl(1:end-4)); %
1691%                 num1_mask=mod(num1_civ1(ifile)-1,nbslice_mask)+1;
1692%                 maskname=name_generator(maskbase,num1_mask,1,'.png','_i');% mask corresponding to the first image of the pair
1693%                 maskflag= exist(maskname,'file')==2;
1694%                 if ~maskflag;
1695%                     maskname='noFile use default';
1696%                 end             
1697%             end
1698%             if ~isempty(file_ref_fix1)
1699%                 file_ref=file_ref_fix1{ifile,j};
1700%             else
1701%                 file_ref='';
1702%             end
1703%             error=RUN_FIX(filecell.nc.civ1{ifile,j},field1,flagindex,1,thresh_vecC,maskflag,maskname,...
1704%                                     thresh_vel,inf_sup,file_ref,fieldchoice);
1705%
1706%             if compare==3 &&(isequal(mode,'pair j1-j2') || isequal(mode,'series(Dj)') || isequal(mode,'series(Di)'))
1707%                  if test_mask==0 
1708%                       maskname='noFile use default';
1709%                       maskflag=0;
1710%                  else
1711%                      maskbase=[get(handles.RootName2,'String') '_' maskdispl]; % mask root name
1712%                      nbslice_mask=str2num(maskdispl(1:end-4)); %
1713%                      num1_mask=mod(num1_civ1(ifile)-1,nbslice_mask)+1;               
1714%                      maskname=name_generator(maskbase,num1_mask,1,'.png','_i');
1715%                      maskflag= exist(maskname,'file')==2;
1716%                      if ~maskflag;
1717%                         maskname='noFile use default';
1718%                      end
1719%                  end
1720%                  error=RUN_FIX(filecell.ncA.civ1{ifile,j},field1,flagindex,1,thresh_vecC,maskflag,maskname,...
1721%                                     thresh_vel,inf_sup,file_ref,fieldchoice);
1722%                  if ~isempty(error)
1723%                     msgbox_uvmat('ERROR',error)
1724%                     return
1725%                  end             
1726%             end
1727%         end
1728%     end
1729%     close(h)
1730% end
1731%
1732% % RUN PATCH1
1733% if box_test(3)==1
1734%     rho_patch1=str2num(get(handles.rho_patch1,'String'));
1735%     if isempty(rho_patch1)
1736%         rho_patch1='1000';
1737%         set(handles.rho_patch1,'String','1')
1738%     else
1739%         rho_patch1=num2str(1000*rho_patch1);
1740%     end
1741%     nx_patch1=get(handles.nx_patch1,'String');
1742%     if isequal(str2num(nx_patch1),[])
1743%          nx_patch1='50' ;%default
1744%          set(handles.nx_patch1,'String','50');
1745%     end
1746%     ny_patch1=get(handles.ny_patch1,'String');
1747%     if isequal(str2num(ny_patch1),[])
1748%          ny_patch1='50' ;%default
1749%          set(handles.ny_patch1,'String','50');
1750%     end
1751%     subdomain_patch1=get(handles.subdomain_patch1,'String');
1752%     thresh_patch1=get(handles.thresh_patch1,'String');
1753%     test_interp=get(handles.test_interp,'Value');
1754%     icount=0;
1755%     for ifile=1:nbfield
1756%         for j=1:nbslice
1757%             icount=icount+1;
1758%             barlength=0.188*icount/(nbfield*nbslice);
1759%             set(handles.waitbar_patch1,'Position',[0.946 0.627-barlength 0.03 barlength])
1760%             drawnow
1761%             if  isequal(get(handles.test_stereo1,'Value'),0)
1762%                 cmd=RUN_PATCH(filecell.nc.civ1{ifile,j},nx_patch1,ny_patch1,rho_patch1,subdomain_patch1,thresh_patch1,test_interp);
1763%                 s=-1;
1764%                 if sge
1765%                     [s,w]=unix(['qrsh -q -fast.q ' cmd]);
1766%                 end
1767%                 if s~=0
1768%                     display(['!' cmd])
1769%                     eval(['!' cmd]);                 
1770%                 end
1771%
1772%                 %stereo case:
1773%             elseif  isequal(get(handles.test_stereo1,'Value'),1)
1774%                 if exist('stinterp_exe','var')%Prog Gauthier
1775%                     cmd=RUN_STINTERP(stinterp_exe,filecell.ncA.civ1{ifile,j},filecell.nc.civ1{ifile,j},filecell.st{ifile,j},...
1776%                           nx_patch1,ny_patch1,rho_patch1,subdomain_patch1,thresh_patch1,[filebase_A '.xml'],[filebase_B '.xml']);
1777%                     display(['!' cmd])
1778%                     eval(['!' cmd]);       
1779%                 else
1780%                     RUN_STLIN(filecell.ncA.civ1{ifile,j},filecell.nc.civ1{ifile,j},'civ1',filecell.st{ifile,j},...
1781%                         str2num(nx_patch1),str2num(ny_patch1),str2num(thresh_patch1),[filebase_A '.xml'],[filebase_B '.xml']);
1782%                 end
1783%             end
1784%             if compare==3 && isequal(get(handles.test_stereo1,'Value'),0)
1785%                 cmd=RUN_PATCH(filecell.ncA.civ1{ifile,j},nx_patch1,ny_patch1,rho_patch1,subdomain_patch1,thresh_patch1,test_interp);
1786%                 s=-1;
1787%                 if sge
1788%                     [s,w]=unix(['qrsh -q -fast.q ' cmd]);
1789%                 end
1790%                 if s~=0
1791%                    display(['!' cmd])
1792%                    eval(['!' cmd]);
1793%                 end
1794%             end
1795%         end
1796%     end
1797% end
1798%
1799% % CIV2
1800% if box_test(4)==1
1801%     RUN_CIV2(handles,filecell.ima1.civ2,filecell.ima2.civ2,filecell.nc.civ1,filecell.nc.civ2,num1_civ2,num2_civ2,...
1802%         num_a_civ2,num_b_civ2,nom_type_nc)
1803% end
1804%
1805% % FIX2
1806% if box_test(5)==1
1807%     %names of the civ2 fields
1808%     field2.vel_type='civ2';
1809%     field2.nb='nb_vectors2';
1810%     field2.X='vec2_X';
1811%     field2.Y='vec2_Y';
1812%     field2.U='vec2_U';
1813%     field2.V='vec2_V';
1814%     field2.fixflag='vec2_FixFlag';
1815%     flagindex(1)=get(handles.vec_Fmin2_2, 'Value');
1816%     flagindex(2)=get(handles.vec_F3_2, 'Value');
1817%     flagindex(3)=get(handles.vec_F4, 'Value');
1818%     thresh_vec2C=str2num(get(handles.thresh_vec2C,'String'));%threshold on image correlation vec_C
1819%     thresh_vel2=str2num(get(handles.thresh_vel2,'String'));%threshold on velocity modulus
1820%     inf_sup=get(handles.inf_sup2,'Value');
1821%     menu=get(handles.field_ref2,'String');
1822%     index=get(handles.field_ref2,'Value');
1823%     if isempty(menu)
1824%         fieldchoice='';
1825%     else
1826%         fieldchoice=menu{index};
1827%     end   
1828%     h = waitbar(0,['removing velocity vectors, fix2']);% display a wait bar
1829%     test_mask=get(handles.get_mask_fix2,'Value');
1830%     if test_mask
1831%         maskdispl=get(handles.mask_fix2,'String');
1832%         if exist(maskdispl,'file')
1833%             test_mask=2;
1834%             maskname=maskdispl;
1835%         else
1836%             maskbase=[get(handles.RootName,'String') '_' maskdispl]; % mask root name
1837%         end
1838%     end
1839%     for ifile=1:nbfield
1840%         waitbar(ifile/nbfield); %update the waitbar
1841%         for j=1:nbslice       
1842%            if test_mask==0
1843%                 maskname='noFile use default';
1844%                 maskflag=0;
1845%            elseif test_mask==1
1846%                 nbslice_mask=str2num(maskdispl(1:end-4)); %
1847%                 num1_mask=mod(num1_civ2(ifile)-1,nbslice_mask)+1;
1848%                 maskname=name_generator(maskbase,num1_mask,1,'.png','_i');
1849%                 maskflag= exist(maskname,'file')==2;
1850%                 if ~maskflag;
1851%                       maskname='noFile use default';
1852%                 end
1853%             end
1854%             if ~isempty(file_ref_fix2)
1855%                 file_ref=file_ref_fix2{ifile,j};
1856%             else
1857%                 file_ref='';
1858%             end
1859%             error=RUN_FIX(filecell.nc.civ2{ifile,j},field2,flagindex,2,thresh_vec2C,maskflag,maskname,...
1860%                                     thresh_vel2,inf_sup,file_ref,fieldchoice);
1861%             if ~isempty(error)
1862%                 msgbox_uvmat('ERROR',error)
1863%                 return
1864%             end 
1865%             if compare==3
1866%                  if test_mask==0 
1867%                       maskname='noFile use default';
1868%                       maskflag=0;
1869%                  else
1870%                       maskbase=[get(handles.RootName2,'String') '_' maskdispl]; % mask root name
1871%                       nbslice_mask=str2num(maskdispl(1:end-4)); %
1872%                       num1_mask=mod(num1_civ2(ifile)-1,nbslice_mask)+1;
1873%                       maskname=name_generator(maskbase,num1_mask,1,'.png','_i');
1874%                       maskflag= exist(maskname,'file')==2;
1875%                       if ~maskflag;
1876%                          maskname='noFile use default';
1877%                       end
1878%                  end
1879%                  error=RUN_FIX(filecell.ncA.civ2{ifile,j},field2,flagindex,1,thresh_vec2C,maskflag,maskname,...
1880%                                     thresh_vel2,inf_sup,file_ref,fieldchoice);
1881%                  if ~isempty(error)
1882%                     msgbox_uvmat('ERROR',error)
1883%                     return
1884%                  end 
1885%             end
1886%         end
1887%     end
1888%     close(h)
1889% end
1890%
1891% %PATCH 2
1892% if box_test(6)==1
1893%     rho_patch2=str2num(get(handles.rho_patch2,'String'));
1894%     if isempty(rho_patch2)
1895%         rho_patch2='1000';
1896%         set(handles.rho_patch2,'String','1')
1897%     else
1898%         rho_patch2=num2str(1000*rho_patch2);
1899%     end
1900%     nx_patch2=get(handles.nx_patch2,'String');
1901%     ny_patch2=get(handles.ny_patch2,'String');
1902%     thresh_patch2=get(handles.thresh_patch2,'String');
1903%     if isequal(str2num(nx_patch2),[])
1904%          nx_patch2='50' ;%default
1905%          set(handles.nx_patch2,'String','50');
1906%     end
1907%     if isequal(str2num(ny_patch2),[])
1908%          ny_patch2='50' ;%default
1909%          set(handles.ny_patch2,'String','50');
1910%     end
1911%     subdomain_patch2=get(handles.subdomain_patch2,'String');
1912%     icount=0;
1913%     for ifile=1:nbfield
1914%         for j=1:nbslice
1915%             icount=icount+1;
1916%             barlength=0.188*icount/(nbfield*nbslice);
1917%             set(handles.waitbar_patch2,'Position',[0.946 0.188-barlength 0.03 barlength])
1918%             drawnow
1919%             if compare~=3 | isequal(get(handles.test_stereo2,'Value'),0)
1920%                 cmd=RUN_PATCH(filecell.nc.civ2{ifile,j},nx_patch2,ny_patch2,rho_patch2,subdomain_patch2,1,0);
1921%                 s=-1;
1922%                 if sge
1923%                     [s,w]=unix(['qrsh -q -fast.q ' cmd]);
1924%                 end
1925%                 if s~=0
1926%                     display(['!' cmd])
1927%                     eval(['!' cmd]);
1928%                 end
1929%             elseif compare==3 &&  isequal(get(handles.test_stereo2,'Value'),1)
1930%                if exist('stinterp_exe','var')%Prog Gauthier 
1931%                    cmd=RUN_STINTERP(stinterp_exe,filecell.ncA.civ2{ifile,j},filecell.nc.civ2{ifile,j},filecell.st{ifile,j},...
1932%                    nx_patch2,ny_patch2,rho_patch2,subdomain_patch2,thresh_patch2,[filebase_A '.xml'],[filebase_B '.xml']);
1933%                    display(['!' cmd])
1934%                    eval(['!' cmd])
1935%                else
1936%                    RUN_STLIN(filecell.ncA.civ2{ifile,j},filecell.nc.civ2{ifile,j},'civ2',filecell.st{ifile,j},...
1937%                    str2num(nx_patch2),str2num(ny_patch2),str2num(thresh_patch2),[filebase_A '.xml'],[filebase_B '.xml'])
1938%                end
1939%             end   
1940%             if compare==3 && isequal(get(handles.test_stereo2,'Value'),0)
1941%                 cmd=RUN_PATCH(filecell.ncA.civ2{ifile,j},nx_patch2,ny_patch2,rho_patch2,subdomain_patch2,1,0);
1942%                 s=-1;
1943%                 if sge
1944%                     [s,w]=unix(['qrsh -q -fast.q ' cmd]);
1945%                 end
1946%                 if s~=0
1947%                     display(['!' cmd])
1948%                     eval(['!' cmd]);
1949%                 end
1950%             end
1951%         end
1952%     end
1953% end
1954%
1955% set(handles.RUN, 'Enable','On')
1956% set(handles.RUN,'BackgroundColor',[1 0 0])
1957%
1958% %save the current interface setting as figure namefig, append .0 to the name if it already exists
1959% if isfield(filecell,'st')
1960%     fileresu=filecell.st{1,1};
1961% elseif isfield(filecell,'nc')
1962%     if isfield(filecell.nc,'civ2')
1963%         fileresu=filecell.nc.civ2{1,1};
1964%     else
1965%         fileresu=filecell.nc.civ1{1,1};
1966%     end
1967% end
1968% [RootPath,RootFile,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fileresu);
1969% namedoc=fullfile(RootPath,subdir,RootFile);
1970% detect=1;
1971% while detect==1
1972%     namefigfull=[namedoc '.fig'];
1973%     hh=dir(namefigfull);
1974%     if ~isempty(hh)
1975%         detect=1;
1976%         namedoc=[namedoc '.0'];
1977%     else
1978%         detect=0;
1979%     end
1980% end
1981% saveas(gcbf,namefigfull);%save the interface with name namefigfull (A CHANGER EN FICHIER  .xml)
1982
1983function RUN_Callback(hObject, eventdata, handles)
1984%------------------------------------------------------------------------
1985% global civ1_exe civ2_exe patch_exe patch_new_exe sge
1986set(handles.RUN, 'Enable','Off')
1987set(handles.RUN,'BackgroundColor',[0.831 0.816 0.784])
1988batch=0;
1989launch_jobs(hObject, eventdata, handles,batch);
1990set(handles.RUN, 'Enable','On')
1991set(handles.RUN,'BackgroundColor',[1 0 0])
1992
1993%------------------------------------------------------------------------
1994% --- Executes on button press in BATCH: remote processing
1995function BATCH_Callback(hObject, eventdata, handles)
1996%------------------------------------------------------------------------
1997%global civ1_exe civ2_exe patch_exe patch_new_exe fix_exe todo_path sge Civ_exe % probabely to remove
1998set(handles.BATCH, 'Enable','Off')
1999set(handles.BATCH,'BackgroundColor',[0.831 0.816 0.784])
2000batch=1;
2001launch_jobs(hObject, eventdata, handles, batch)
2002set(handles.BATCH, 'Enable','On')
2003set(handles.BATCH,'BackgroundColor',[1 0 0])
2004
2005%------------------------------------------------------------------------
2006% --- Executes on button press in BATCH: remote processing
2007function launch_jobs(hObject, eventdata, handles, batch)
2008%-----------------------------------------------------------------------
2009% global civ2Bin patchBin patch_newBin fixBin CivBin % probabely to remove
2010
2011compare=get(handles.compare,'Value');%test for usual PIV (compare=1) or displacement (=2) or stereo PIV (=3)
2012
2013%check the list of operations:
2014operations={'CIV1','FIX1','PATCH1','CIV2','FIX2','PATCH2'};
2015run_flag=1;
2016box_test(1)=get(handles.CIV1,'Value');
2017box_test(2)=get(handles.FIX1,'Value');
2018box_test(3)=get(handles.PATCH1,'Value');
2019box_test(4)=get(handles.CIV2,'Value');
2020box_test(5)=get(handles.FIX2,'Value');
2021box_test(6)=get(handles.PATCH2,'Value');
2022index=find(box_test==1);
2023if isempty(index)
2024    msgbox_uvmat('ERROR','no selected operation')
2025    return
2026end
2027index_first=min(index);
2028index_last=max(index);
2029box_used=box_test([index_first : index_last]);
2030[box_missing,ind_missing]=min(box_used);
2031if isequal(box_missing,0)
2032    msgbox_uvmat('ERROR',['missing' cell2mat(operations(ind_missing))]);
2033    return
2034end
2035
2036%root name
2037filebase=get(handles.RootName,'String');
2038if isempty(filebase)||isequal(filebase,'')
2039    msgbox_uvmat('ERROR','no input files')
2040    return
2041end
2042   
2043%check mask if selecetd
2044if isequal(get(handles.get_mask_civ1,'Value'),1)
2045    maskname=get(handles.mask_civ1,'String');
2046    if ~exist(maskname,'file')
2047        get_mask_civ1_Callback(hObject, eventdata, handles);
2048    end
2049end
2050if isequal(get(handles.get_mask_fix1,'Value'),1)
2051    maskname=get(handles.mask_fix1,'String');
2052    if ~exist(maskname,'file')
2053        get_mask_fix1_Callback(hObject, eventdata, handles);
2054    end
2055end
2056if isequal(get(handles.get_mask_civ2,'Value'),1)
2057    maskname=get(handles.mask_civ2,'String');
2058    if ~exist(maskname,'file')
2059     get_mask_civ2_Callback(hObject, eventdata, handles);
2060    end
2061end
2062if isequal(get(handles.get_mask_fix2,'Value'),1)
2063    maskname=get(handles.mask_fix2,'String');
2064    if ~exist(maskname,'file')
2065     get_mask_fix2_Callback(hObject, eventdata, handles);
2066    end
2067end
2068
2069%read names of the .exe file
2070path_uvmat=which('uvmat');% check the path detected for source file uvmat
2071path_UVMAT=fileparts(path_uvmat); %path to UVMAT
2072xmlfile=fullfile(path_UVMAT,'PARAM.xml');
2073if exist(xmlfile,'file')
2074    t=xmltree(xmlfile);
2075    s=convert(t);
2076end
2077if batch
2078    if isfield(s,'BatchParam')
2079        sparam=s.BatchParam;
2080        if ~ismember(sparam.BatchMode,{'sge'})
2081            msgbox_uvmat('ERROR',['batch mode ' sparam.BatchMode ' not supported by UVMAT'])
2082        end
2083    else
2084        msgbox_uvmat('ERROR','no batch mode defined in PARAM.xml')
2085        return
2086    end
2087else
2088    if isfield(s,'RunParam')
2089        sparam=s.RunParam;
2090    else
2091        msgbox_uvmat('ERROR','no civ binaries defined in PARAM.xml')
2092        return
2093    end
2094end
2095if isfield(sparam,'CivBin')
2096    CivBin=sparam.CivBin;
2097end
2098if isfield(sparam,'Civ1Bin')
2099    civ1Bin=sparam.Civ1Bin;
2100end
2101if isfield(sparam,'Civ2Bin')
2102    civ2Bin=sparam.Civ2Bin;
2103end
2104test_interp=get(handles.test_interp,'Value');
2105if ~test_interp && isfield(sparam,'PatchBin')
2106    PatchBin=sparam.PatchBin;
2107end
2108if test_interp && isfield(sparam,'PatchNewBin')
2109    PatchBin=sparam.PatchNewBin;
2110end
2111if isfield(sparam,'FixBin')
2112    fixBin=sparam.FixBin;
2113end
2114if batch
2115    if isfield(sparam,'BatchMode')
2116        batch_mode=sparam.BatchMode;
2117    end
2118else
2119    MaxCivProcesses=50;
2120   if isfield(sparam,'MaxCivProcesses')
2121        MaxCivProcesses=str2double(sparam.MaxCivProcesses);
2122   end
2123end
2124
2125%initialize the waitbars
2126set(handles.waitbar_1,'Position',[0.946 0.876 0.03 0.001])
2127set(handles.waitbar_patch1,'Position',[0.946 0.439 0.03 0.001])
2128set(handles.waitbar_civ2,'Position',[0.946 0.219 0.03 0.001])
2129set(handles.waitbar_patch2,'Position',[0.946 0.0 0.03 0.001])
2130
2131drawnow
2132%get the filename root, nomenclature and numbers
2133
2134% for Windows system find the UBC path name if needed
2135if ~isunix & isequal(todo_path(1:2),'\\') & isequal(filebase(2:3),':\')
2136    cur_dir=pwd;
2137    if ~isequal(cur_dir(2:3),':\')
2138        cd(matlabroot); %move to the Matlab root directory if the current Matlab dir does not allow the dos command or is M:
2139    end
2140    [ss,ww]=dos(['net use ' filebase(1:2)]);
2141    if isequal(ss,0)
2142        rankpath=findstr(ww,'\\');
2143        if ~isempty(rankpath)
2144            wwrest=ww(rankpath:end);
2145            rankend=min(find(double(wwrest)==10))-1;
2146            filebase=[wwrest(1:rankend) filebase(3:end)];
2147            set(handles.RootName,'String',filebase);
2148        end
2149    else
2150         msgbox_uvmat('ERROR','for BATCH option, UBC file names, beginning by \\, are needed');
2151         set(handles.BATCH, 'Enable','On')
2152         set(handles.BATCH,'BackgroundColor',[1 0 0])
2153         return
2154    end
2155end
2156
2157% set the list of files and check them
2158display('checking the files...')
2159[filecell,num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2,nom_type_nc,file_ref_fix1,file_ref_fix2]=...
2160       set_civ_filenames(handles,compare,box_test);
2161
2162%choice of batch priority
2163ind_answer=2;
2164if batch
2165    [s,w]=unix('qstat -q civ.q|grep job_| wc -l'); %check the waiting list (command unix)
2166    if isequal(s,0)
2167        w(end)=[];
2168        str_displ={[w ' jobs in the waiting list'];'Select a priority:'};
2169        str={'urgent';'normal';'low'};
2170        [ind_answer,v] = listdlg('PromptString',str_displ,...
2171                'SelectionMode','single',...
2172                'ListString',str,'ListSize',[200 200],'Name','job priority','InitialValue',3);
2173        if isequal(v,0) % to handle Cancel button and figure close, 
2174           return % a better way should be create
2175        end
2176    else
2177        msgbox_uvmat('ERROR','batch system not available')
2178        return
2179    end
2180else
2181    if isunix
2182        [s,w]=unix('ps faux |grep civ|wc -l');
2183        w(end)=[];
2184        if str2num(w)+numel(filecell)> MaxCivProcesses
2185            msgbox_uvmat('ERROR',{['There are already ' w ' civ processes running locally'];'Use BATCH or submit RUN later'})
2186            return
2187        end
2188    end
2189end
2190 
2191display('files OK, processing...') 
2192nbfield=numel(num1_civ1);
2193nbslice=numel(num_a_civ1);
2194
2195%GET PARAMETERS:
2196  %get civ parameters
2197if box_test(1)==1
2198    par_civ1=read_param_civ1(handles,filecell.ima1.civ1{1,1});
2199end
2200
2201%get fix1 parameters
2202if box_test(2)==1
2203    flagindex1(1)=get(handles.vec_Fmin2, 'Value');
2204    flagindex1(2)=get(handles.vec_F3, 'Value');
2205    flagindex1(3)=get(handles.vec_F2, 'Value');
2206    thresh_vecC1=str2num(get(handles.thresh_vecC,'String'));%threshold on image correlation vec_C
2207    thresh_vel1=str2num(get(handles.thresh_vel,'String'));%threshold on velocity modulus
2208    test_mask=get(handles.get_mask_fix1,'Value');
2209    nbslice_mask=get(handles.mask_fix1,'UserData'); % get the number of slices (= number of masks)
2210    %%%%%%%%%%%%%COMPLETER LE PROGRAMME FIX
2211%     inf_sup=get(handles.inf_sup1,'Value');
2212%     fileref=get(handles.ref_fix1,'String');
2213%     refpath=get(handles.ref_fix1,'UserData');
2214%     fileref=fullfile(refpath,fileref);
2215    menu=get(handles.field_ref1,'String');
2216    index=get(handles.field_ref1,'Value');
2217    if isempty(menu)
2218        fieldchoice='';
2219    else
2220        fieldchoice=menu{index};
2221         msgbox_uvmat('WARNING','reference field is not used presently with batch, use RUN option')
2222    end   
2223end
2224 
2225 %get patch1 parameters
2226if box_test(3)==1
2227        rho_patch1=str2num(get(handles.rho_patch1,'String'));
2228        if isempty(rho_patch1)
2229            rho_patch1='1000';
2230            set(handles.rho_patch1,'String','1')
2231        else
2232            rho_patch1=num2str(1000*rho_patch1);
2233        end
2234        nx_patch1=get(handles.nx_patch1,'String');
2235        ny_patch1=get(handles.ny_patch1,'String');
2236        if isequal(str2num(nx_patch1),[])
2237            nx_patch1='50' ;%default
2238            set(handles.nx_patch1,'String','50');
2239        end
2240        if isequal(str2num(ny_patch1),[])
2241            ny_patch1='50' ;%default
2242            set(handles.ny_patch1,'String','50');
2243        end
2244        subdomain_patch1=get(handles.subdomain_patch1,'String');
2245        thresh_patch1=get(handles.thresh_patch1,'String');
2246 end
2247 
2248 %get civ2 parameters
2249if box_test(4)==1
2250    par_civ2=read_param_civ2(handles,cell2mat(filecell.ima1.civ2(1,1)));
2251end
2252
2253%get fix2 parameters
2254if box_test(5)==1
2255    flagindex2(1)=get(handles.vec_Fmin2_2, 'Value');
2256    flagindex2(2)=get(handles.vec_F3_2, 'Value');
2257    flagindex2(3)=get(handles.vec_F4, 'Value');
2258    thresh_vec2C=str2num(get(handles.thresh_vec2C,'String'));%threshold on image correlation vec_C
2259    thresh_vel2=str2num(get(handles.thresh_vel2,'String'));%threshold on velocity modulus
2260    test_mask=get(handles.get_mask_fix2,'Value');
2261    nbslice_mask=get(handles.mask_fix2,'UserData'); % get the number of slices (= number of masks)
2262        %%%%%%%%%%%%%COMPLETER LE PROGRAMME FIX AVEC REF FILE ET OPTION inf_sup=2
2263%     inf_sup=get(handles.inf_sup2,'Value');
2264%     ref=get(handles.ref_fix2,'UserData');
2265
2266    %%%%%%%%%%%%%%%%%%%
2267end
2268
2269
2270
2271%get patch2 parameters
2272if box_test(6)==1
2273    rho_patch2=str2num(get(handles.rho_patch2,'String'));
2274    if isempty(rho_patch2)
2275        rho_patch2='1000';
2276        set(handles.rho_patch2,'String','1')
2277    else
2278        rho_patch2=num2str(1000*rho_patch2);
2279    end
2280    nx_patch2=get(handles.nx_patch2,'String');
2281    ny_patch2=get(handles.ny_patch2,'String');
2282    if isequal(str2num(nx_patch2),[])
2283        nx_patch2='50' ;%default
2284        set(handles.nx_patch2,'String','50');
2285    end
2286    if isequal(str2num(ny_patch2),[])
2287        ny_patch2='50' ;%default
2288        set(handles.ny_patch2,'String','50');
2289    end
2290    subdomain_patch2=get(handles.subdomain_patch2,'String');
2291    thresh_patch2=get(handles.thresh_patch2,'String');
2292%              test_interp=get(handles.test_interp,'Value');
2293end
2294
2295%MAIN LOOP
2296% for ifile=1:nbfield
2297p1text=[];%initiate command text
2298time=get(handles.RootName,'UserData'); %get the set of times
2299civAll=get(handles.Experimental,'Value'); % Boolean for new civ excution method
2300for ifile=1:nbfield
2301    for j=1:nbslice
2302        i_cmd=0;
2303        cmd='';
2304        if isunix % check: necessaire aussi en RUN?
2305           %fid=fopen([filename '.cmx'],'w')
2306           cmd='#!/bin/bash';
2307           cmd=[cmd '\n' '#$ -cwd'];
2308           cmd=[cmd '\n' 'hostname && date'];
2309        end
2310        if civAll
2311            civAllxml=xmltree;% xml contents,  all parameters
2312            civAllCmd='';
2313            civAllxml=set(civAllxml,1,'name','CivDoc');
2314        end
2315        filename_cmx=filecell.nc.civ1{ifile,j};%output netcdf file
2316        filename_cmx(end-1:end)='cm';%name of cmx file
2317        filename_cmx=[filename_cmx 'x'];
2318       
2319   %CIV1
2320        if box_test(1)==1
2321            par_civ1.filename_ima_a=filecell.ima1.civ1{ifile,j};
2322            par_civ1.filename_ima_b=filecell.ima2.civ1{ifile,j};         
2323            namelog=[filename_cmx([1:end-3]) 'log'];
2324            par_civ1.Dt=num2str(time(num2_civ1(ifile),num_b_civ1(j))-time(num1_civ1(ifile),num_a_civ1(j)));
2325            par_civ1.T0=num2str((time(num2_civ1(ifile),num_b_civ1(j))+time(num1_civ1(ifile),num_a_civ1(j)))/2);
2326            par_civ1.term_a=num2stra(num_a_civ1(j),nom_type_nc);%UTILITE?
2327            par_civ1.term_b=num2stra(num_b_civ1(j),nom_type_nc);%
2328            test_mask=get(handles.get_mask_civ1,'Value');
2329            if test_mask==0 
2330                par_civ1.maskname='noFile use default';
2331                par_civ1.maskflag='n';
2332            else
2333                maskdispl=get(handles.mask_civ1,'String');
2334                if exist(maskdispl,'file')
2335                    par_civ1.maskname=maskdispl;
2336                    par_civ1.maskflag='y';   
2337                else
2338                    maskbase=[filebase '_' maskdispl]; %
2339                    nbslice_mask=str2num(maskdispl(1:end-4)); %
2340                    num1_mask=mod(num1_civ1(ifile)-1,nbslice_mask)+1;
2341                    par_civ1.maskname=name_generator(maskbase,num1_mask,1,'.png','_i');
2342                    if exist(par_civ1.maskname,'file')
2343                        par_civ1.maskflag='y';
2344                    else
2345                        par_civ1.maskname='noFile use default';
2346                        par_civ1.maskflag='n';
2347                    end
2348                end
2349            end
2350           
2351            test_grid=get(handles.browse_gridciv1,'Value');
2352            if test_grid
2353                par_civ1.gridflag='y';
2354                gridname=get(handles.grid_civ1,'String');
2355                if isequal(gridname(end-3:end),'grid')
2356                    nbslice_grid=str2num(gridname(1:end-4)); %
2357                    if ~isempty(nbslice_grid)
2358                        num1_grid=mod(num1_civ1(ifile)-1,nbslice_grid)+1;
2359                        par_civ1.gridname=[filebase '_' name_generator(gridname,num1_grid,1,'.grid','_i')];
2360                        if ~exist(par_civ1.gridname,'file')
2361                           msgbox_uvmat('ERROR','grid file absent for civ1')
2362                        end
2363                    elseif exist(gridname,'file')
2364                        par_civ1.gridname=gridname;
2365                    else
2366                        msgbox_uvmat('ERROR','grid file absent for civ1')
2367                    end
2368                end
2369            else
2370                  par_civ1.gridname='noFile use default';
2371                  par_civ1.gridflag='n';
2372            end
2373            %           
2374            i_cmd=i_cmd+1;
2375            if isequal(civAll,0)
2376                cmd=[cmd '\n' BATCH_CIV1(filename_cmx(1:end-4),namelog,par_civ1,handles,sparam)];
2377            else
2378                 civAllCmd=[civAllCmd ' civ1 '];
2379                 str=BATCH_CIV1_Unified(filename_cmx([1:end-4]),namelog,par_civ1);
2380                 fieldnames=fields(str);
2381                [civAllxml,uid_civ1]=add(civAllxml,1,'element','civ1');
2382                for ilist=1:length(fieldnames)
2383                  val=eval(['str.' fieldnames{ilist}]);
2384                  if ischar(val)
2385                    [civAllxml,uid_t]=add(civAllxml,uid_civ1,'element',fieldnames{ilist});
2386                    [civAllxml,uid_t2]=add(civAllxml,uid_t,'chardata',val);
2387                   end
2388                end   
2389            end
2390        end
2391       
2392   % FIX1
2393       if box_test(2)==1
2394           test_mask=get(handles.get_mask_fix1,'Value');
2395           if test_mask==0
2396                maskname='';
2397           else
2398                maskdispl=get(handles.mask_fix1,'String');
2399                nbslice_mask=str2num(maskdispl(1:end-4)); %
2400                num1_mask=mod(num1_civ1(ifile)-1,nbslice_mask)+1;
2401                maskbase=[filebase '_' maskdispl];
2402                maskname=name_generator(maskbase,num1_mask,1,'.png','_i');
2403           end
2404           if isequal(civAll,0)
2405           cmd_FIX=[fixBin ' -f ' filecell.nc.civ1{ifile,j} ' -fi1 ' num2str(flagindex1(1)) ...
2406                   ' -fi2 ' num2str(flagindex1(2)) ' -fi3 ' num2str(flagindex1(3)) ...
2407                   ' -threshC ' num2str(thresh_vecC1) ' -threshV ' num2str(thresh_vel1) ' -maskName ' maskname];
2408           cmd=[cmd '\n' cmd_FIX];
2409           else
2410                fix1.inputFileName=filecell.nc.civ1{ifile,j} ;
2411                fix1.fi1=num2str(flagindex1(1));
2412                fix1.fi2=num2str(flagindex1(2));
2413                fix1.fi3=num2str(flagindex1(3));
2414                fix1.threshC=num2str(thresh_vecC1);
2415                fix1.threshV=num2str(thresh_vel1);                 
2416                fieldnames=fields(fix1);
2417                [civAllxml,uid_fix1]=add(civAllxml,1,'element','fix1');
2418                for ilist=1:length(fieldnames)
2419                  val=eval(['fix1.' fieldnames{ilist}]);
2420                  if ischar(val)
2421                    [civAllxml,uid_t]=add(civAllxml,uid_fix1,'element',fieldnames{ilist});
2422                    [civAllxml,uid_t2]=add(civAllxml,uid_t,'chardata',val);
2423                   end
2424                end
2425             civAllCmd=[civAllCmd ' fix1 '];           
2426           end         
2427       end
2428   
2429    %PATCH1
2430    if box_test(3)==1
2431        if isequal(civAll,0)
2432            cmd_PATCH=RUN_PATCH(filecell.nc.civ1{ifile,j},nx_patch1,ny_patch1,rho_patch1,subdomain_patch1,thresh_patch1,test_interp,PatchBin);
2433            cmd=[cmd '\n' cmd_PATCH];
2434        else
2435            patch1.inputFileName=filecell.nc.civ1{ifile,j} ;
2436            patch1.nopt=subdomain_patch1;
2437            patch1.maxdiff=thresh_patch1;
2438            patch1.ro=rho_patch1;
2439            test_grid=get(handles.get_gridpatch1,'Value');
2440            if test_grid
2441                 patch1.gridflag='y';
2442                gridname=get(handles.grid_patch1,'String');
2443                if isequal(gridname(end-3:end),'grid')
2444                    nbslice_grid=str2num(gridname(1:end-4)); %
2445                    if ~isempty(nbslice_grid)
2446                        num1_grid=mod(num1_civ1(ifile)-1,nbslice_grid)+1;
2447                        patch1.gridPatch=[filebase '_' name_generator(gridname,num1_grid,1,'.grid','_i')];
2448                        if ~exist(patch1.gridPatch,'file')
2449                           msgbox_uvmat('ERROR','grid file absent for patch1')
2450                        end
2451                    elseif exist(gridname,'file')
2452                        patch1.gridPatch=gridname;
2453                    else
2454                        msgbox_uvmat('ERROR','grid file absent for patch1')
2455                    end
2456                end
2457            else
2458                   patch1.gridPatch='n';
2459                   patch1.gridflag='n';
2460                  patch1.m=nx_patch1;
2461                  patch1.n=ny_patch1;
2462            end
2463            patch1.convectFlow='n';
2464               fieldnames=fields(patch1);
2465                [civAllxml,uid_patch1]=add(civAllxml,1,'element','patch1');
2466                for ilist=1:length(fieldnames)
2467                  val=eval(['patch1.' fieldnames{ilist}]);
2468                  if ischar(val)
2469                    [civAllxml,uid_t]=add(civAllxml,uid_patch1,'element',fieldnames{ilist});
2470                    [civAllxml,uid_t2]=add(civAllxml,uid_t,'chardata',val);
2471                   end
2472                end             
2473            civAllCmd=[civAllCmd ' patch1 '];
2474        end
2475    end
2476   
2477    if box_test(4)==1 | box_test(5)==1 | box_test(6)==1
2478        filename_cmx=filecell.nc.civ2{ifile,j};%output netcdf file
2479        filename_cmx([end-1:end])=[ 'cm'];%name of cmx file
2480        filename_cmx=[filename_cmx 'x'];
2481    end
2482   
2483    if box_test(4)==1
2484            par_civ2.filename_ima_a=filecell.ima1.civ2{ifile,j};
2485            %par_civ2.filename_ima_a([end-3:end])=[];%remove .png extension
2486            par_civ2.filename_ima_b=filecell.ima2.civ2{ifile,j};
2487            %par_civ2.filename_ima_b([end-3:end])=[];%remove .png extension
2488            namelog=[filename_cmx([1:end-3]) 'log'];
2489            par_civ2.Dt=num2str(time(num2_civ2(ifile),num_b_civ2(j))-time(num1_civ2(ifile),num_a_civ2(j)));
2490            par_civ2.T0=num2str((time(num2_civ1(ifile),num_b_civ2(j))+time(num1_civ2(ifile),num_a_civ2(j)))/2);
2491            par_civ2.term_a=num2stra(num_a_civ2(j),nom_type_nc);
2492            par_civ2.term_b=num2stra(num_b_civ2(j),nom_type_nc);         
2493            par_civ2.filename_nc1=filecell.nc.civ1{ifile,j};
2494            par_civ2.filename_nc1([end-2:end])=[]; % remove '.nc'
2495            test_mask=get(handles.get_mask_civ2,'Value');
2496            if test_mask==0
2497                par_civ2.maskname='noFile use default';
2498                par_civ2.maskflag='n';
2499            else
2500                maskdispl=get(handles.mask_civ2,'String');
2501                if exist(maskdispl,'file')
2502                    par_civ2.maskname=maskdispl;
2503                    par_civ2.maskflag='y';
2504                else
2505                    maskbase=[filebase '_' maskdispl]; %
2506                    nbslice_mask=str2num(maskdispl(1:end-4)); %
2507                    num1_mask=mod(num1_civ2(ifile)-1,nbslice_mask)+1;
2508                    par_civ2.maskname=name_generator(maskbase,num1_mask,1,'.png','_i');
2509                    if exist(par_civ2.maskname,'file')
2510                        par_civ2.maskflag='y';
2511                    else
2512                        par_civ2.maskname='noFile use default';
2513                        par_civ2.maskflag='n';
2514                    end
2515                end
2516            end
2517            %TESTgrid
2518            test_grid=get(handles.browse_gridciv2,'Value');
2519            gridname=get(handles.grid_civ2,'String');
2520            gridflag='y';
2521            if numel(gridname)>=4 && isequal(gridname(end-3:end),'grid')
2522                nbslice_grid=str2num(gridname(1:end-4)); %
2523                if ~isempty(nbslice_grid)
2524                    par_civ2.gridflag='y';
2525                    num1_grid=mod(num1_civ2(ifile)-1,nbslice_grid)+1;
2526                    par_civ2.gridname=[filebase '_' name_generator(gridname,num1_grid,1,'.grid','_i')];
2527                    if exist(par_civ2.gridname,'file')
2528                        par_civ2.gridflag='y';
2529                    else
2530                        par_civ2.gridname='noFile use default';
2531                        par_civ2.gridflag='n';
2532                    end
2533                elseif exist(gridname,'file')
2534                    par_civ2.gridflag='y';
2535                else
2536                    par_civ2.gridname='noFile use default';
2537                    par_civ2.gridflag='n';
2538                end
2539            end
2540            %endTESTgrid
2541            i_cmd=i_cmd+1;
2542            cmd_CIV2=BATCH_CIV2(filename_cmx,namelog,par_civ2,sparam);
2543            if isequal(civAll,0)
2544                if(isunix)
2545                    cmd=[cmd '\n' 'cp -f ' filename_cmx '2 ' filename_cmx '\n' cmd_CIV2];
2546                else
2547                    cmd=[cmd '\n' 'copy /Y ' filename_cmx '2 ' filename_cmx '\n' cmd_CIV2];
2548                end
2549            else
2550                 civAllCmd=[civAllCmd ' civ2 '];
2551                 str=BATCH_CIV2_Unified(filename_cmx([1:end-4]),namelog,par_civ2);
2552                 fieldnames=fields(str);
2553                [civAllxml,uid_civ2]=add(civAllxml,1,'element','civ2');
2554                for ilist=1:length(fieldnames)
2555                    val=eval(['str.' fieldnames{ilist}]);
2556                    if ischar(val)
2557                       [civAllxml,uid_t]=add(civAllxml,uid_civ2,'element',fieldnames{ilist});
2558                       [civAllxml,uid_t2]=add(civAllxml,uid_t,'chardata',val);
2559                    end
2560                end   
2561            end
2562     end
2563     
2564     % FIX2
2565     if box_test(5)==1
2566           test_mask=get(handles.get_mask_fix2,'Value');   
2567            if test_mask==0 
2568                maskname=''; %no mask used
2569            else
2570                maskdispl=get(handles.mask_fix2,'String');
2571                maskbase=[filebase '_' maskdispl]; %
2572                nbslice_mask=str2num(maskdispl(1:end-4)); %
2573                num1_mask=mod(num1_civ2(ifile)-1,nbslice_mask)+1;
2574                maskname =name_generator(maskbase,num1_mask,1,'.png','_i');
2575            end                 
2576           if isequal(civAll,0)
2577                cmd_FIX=[fixBin ' -f ' filecell.nc.civ2{ifile,j} ' -fi1 ' num2str(flagindex2(1)) ...
2578                   ' -fi2 ' num2str(flagindex2(2)) ' -fi3 ' num2str(flagindex2(3)) ...
2579                   ' -threshC ' num2str(thresh_vec2C) ' -threshV ' num2str(thresh_vel2) ' -maskName ' maskname];
2580                cmd=[cmd '\n' cmd_FIX];
2581           else                     
2582                fix2.inputFileName=filecell.nc.civ2{ifile,j} ;
2583                fix2.fi1=num2str(flagindex2(1));
2584                fix2.fi2=num2str(flagindex2(2));
2585                fix2.fi3=num2str(flagindex2(3));
2586                fix2.threshC=num2str(thresh_vec2C);
2587                fix2.threshV=num2str(thresh_vel2);                 
2588                fieldnames=fields(fix2);
2589                [civAllxml,uid_fix2]=add(civAllxml,1,'element','fix2');
2590                for ilist=1:length(fieldnames)
2591                  val=eval(['fix2.' fieldnames{ilist}]);
2592                  if ischar(val)
2593                    [civAllxml,uid_t]=add(civAllxml,uid_fix2,'element',fieldnames{ilist});
2594                    [civAllxml,uid_t2]=add(civAllxml,uid_t,'chardata',val);
2595                   end
2596                end
2597                civAllCmd=[civAllCmd ' fix2 '];           
2598           end
2599       end
2600       
2601     %PATCH2
2602       if box_test(6)==1
2603            if isequal(civAll,0)
2604                cmd_PATCH=RUN_PATCH(filecell.nc.civ2{ifile,j},nx_patch2,ny_patch2,rho_patch2,subdomain_patch2,thresh_patch2,test_interp,PatchBin);
2605                cmd=[cmd '\n' cmd_PATCH];
2606            else
2607                patch2.inputFileName=filecell.nc.civ1{ifile,j} ;
2608                patch2.nopt=subdomain_patch1;
2609                patch2.maxdiff=thresh_patch1;
2610                patch2.ro=rho_patch1;
2611                test_grid=get(handles.get_gridpatch2,'Value');
2612                if test_grid
2613                    patch2.gridflag='y';
2614                    gridname=get(handles.grid_patch2,'String');
2615                    if isequal(gridname(end-3:end),'grid')
2616                        nbslice_grid=str2num(gridname(1:end-4)); %
2617                        if ~isempty(nbslice_grid)
2618                            num1_grid=mod(num1_civ2(ifile)-1,nbslice_grid)+1;
2619                            patch2.gridPatch=[filebase '_' name_generator(gridname,num1_grid,1,'.grid','_i')];
2620                            if ~exist(patch2.gridPatch,'file')
2621                               msgbox_uvmat('ERROR','grid file absent for patch2')
2622                            end
2623                        elseif exist(gridname,'file')
2624                            patch2.gridPatch=gridname;
2625                        else
2626                            msgbox_uvmat('ERROR','grid file absent for patch2')
2627                        end
2628                    end
2629                else
2630                      patch2.gridPatch='n';
2631                      patch2.gridflag='n';
2632                      patch2.m=nx_patch2;
2633                      patch2.n=ny_patch2;
2634                end
2635                patch2.convectFlow='n';
2636                fieldnames=fields(patch2);
2637                [civAllxml,uid_patch2]=add(civAllxml,1,'element','patch2');
2638                for ilist=1:length(fieldnames)
2639                   val=eval(['patch2.' fieldnames{ilist}]);
2640                   if ischar(val)
2641                      [civAllxml,uid_t]=add(civAllxml,uid_patch2,'element',fieldnames{ilist});
2642                      [civAllxml,uid_t2]=add(civAllxml,uid_t,'chardata',val);
2643                   end
2644                end             
2645                civAllCmd=[civAllCmd ' patch2 '];
2646            end     
2647        end
2648        if isequal(civAll,1)
2649            save(civAllxml,[filename_cmx([1:end-4]) '.xml']);
2650            %cmd=char({cmd;[CivBin ' -f ' [filename_cmx([1:end-4]) '.xml'] ' ' civAllCmd]});
2651            cmd=[cmd '\n' CivBin ' -f ' filename_cmx(1:end-4) '.xml '  civAllCmd];
2652        end
2653      % create the .bat file:
2654        if batch
2655                [Rootbat,Filebat,extbat]=fileparts(filename_cmx);
2656                filename_bat=fullfile(Rootbat,['job_' Filebat extbat]);
2657        else
2658            filename_bat=filename_cmx;
2659        end
2660        filename_bat(end-2:end)='bat';
2661        fid=fopen(filename_bat,'w');
2662        fprintf(fid,cmd);
2663        fclose(fid);
2664        %dlmwrite(filename_bat,cmd,'');%write commands in filename_bat
2665        if batch
2666            switch batch_mode
2667                case 'sge'
2668                    pvalue=num2str((1-ind_answer)*500);
2669                    namelog=[filename_bat '.patch.log'];
2670                    display(['!qsub -p ' pvalue ' -q civ.q -e ' filename_cmx(1:end-4) '.errors -o ' filename_cmx(1:end-4) '.log' ' ' filename_bat]);
2671                    eval(  ['!qsub -p ' pvalue ' -q civ.q -e ' filename_cmx(1:end-4) '.errors -o ' filename_cmx(1:end-4) '.log' ' ' filename_bat]);
2672            end
2673        else
2674%% to lauch the jobs locally :
2675            if(isunix)
2676              eval(['!. ' filename_bat ' &']);
2677              display(['!. ' filename_bat ' &'])
2678            else
2679              eval(['!' filename_bat ' &']);
2680              display(['!' filename_bat ' &'])
2681            end
2682%             if(isunix)
2683%               cmdtodo=['. ' filename_bat ];%removed for Mathieu tests %' && rm -f ' filename_bat] ;
2684%             else
2685%                cmdtodo=[filename_bat];%removed for Mathieu tests %' && del /F /Q ' filename_bat' ;
2686%             end
2687%             count= fprintf(p1,'%s\n', cmdtodo);
2688        end
2689     end
2690end
2691% if ~sge
2692%     fclose(p1);
2693%     fclose(p0);
2694%     delete(name_lock);
2695% end
2696
2697set(handles.BATCH, 'Enable','On')
2698set(handles.BATCH,'BackgroundColor',[1 0 0])
2699
2700%save interface state
2701if isfield(filecell,'nc')
2702    if isfield(filecell.nc,'civ2')
2703        fileresu=filecell.nc.civ2{1,1};
2704    else
2705        fileresu=filecell.nc.civ1{1,1};
2706    end
2707end
2708[RootPath,RootFile,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fileresu);
2709namedoc=fullfile(RootPath,subdir,RootFile);
2710detect=1;
2711while detect==1
2712    namefigfull=[namedoc '.fig'];
2713    hh=dir(namefigfull);
2714    if ~isempty(hh)
2715        detect=1;
2716        namedoc=[namedoc '.0'];
2717    else
2718        detect=0;
2719    end
2720end
2721saveas(gcbf,namefigfull);%save the interface with name namefigfull (A CHANGER EN FICHIER  .xml)
2722
2723%------------------------------------------------------------------------
2724%OUTPUT:
2725% filecell: structure of input and output files
2726%         .ima1.civ1{i,j}, .ima1.civ2{i,j}; set of first image names for  correlations, for civ1 and civ2
2727%         .ima2.civ1{i,j}, .ima2.civ2{i,j} ; set of second image names for correlations
2728%         .imaA1.civ1{i,j}, .ima1.civ2{i,j}; set of first image names for correlations, for civ1 and civ2, with camA in stereo case (then .ima1 corresponds to camB)
2729%         .imaA2.civ1{i,j}, .ima2.civ2{i,j} ; set of second image names for  correlations, with camA in stereo case (then .ima1 corresponds to camB)
2730%         .nc.civ1{i,j}, .nc.civ2{i,j}; set of nc files for PIV results
2731%         .ncA.civ1{i,j}, .ncA.civ2{i,j}; set of nc files for PIV results with camA (then .nc corresponds to camB)
2732%         .st{i,j};   set of nc files for the combined stereo fields
2733% num1_civ1(i),num2_civ1(i): lists of first and last i indices for civ1
2734% num_a_civ1(j),num_b_civ1(j): lists of first and last j indices for civ1
2735% num1_civ2(i),num2_civ2(i): lists of first and last i indices for civ2
2736% num_a_civ2(j),num_b_civ2(j): lists of first and last j indices for civ2
2737% nom_type_nc: nomenclature type for velocity files
2738% file_ref_fix1,file_ref_fix2: reference files possibly used by Fix1 and 2
2739function [filecell,num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2,nom_type_nc,file_ref_fix1,file_ref_fix2]=...
2740    set_civ_filenames(handles,compare,box_test)
2741%------------------------------------------------------------------------
2742%get the filename root, nomenclature and numbers
2743filebase=get(handles.RootName,'String');
2744browse=get(handles.browse_root,'UserData');
2745compare_list=get(handles.compare,'String');
2746val=get(handles.compare,'Value');
2747compare=compare_list{val};
2748if strcmp(compare,'displacement')
2749    mode='displacement';
2750else
2751    mode_list=get(handles.mode,'String');
2752    mode_value=get(handles.mode,'Value');
2753    mode=mode_list{mode_value};
2754end
2755time=get(handles.RootName,'UserData'); %get the set of times
2756ext_ima=get(handles.ImaExt,'String');
2757nom_type_nc=browse.nom_type_nc;
2758nom_type_ima2=browse.nom_type_ima;
2759if isequal(nom_type_ima2,[]),nom_type_ima2='ima_num';end; %default
2760if isequal(nom_type_nc,[]),nom_type_nc='_i1-i2';end; %default
2761[num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2]=...
2762    find_pair_indices(handles,mode);
2763%determine the new filebase for 'displacement' mode (comparison of two series)
2764filebase_B=filebase;% root name of the second field series for stereo
2765if strcmp(compare,'displacement') || strcmp(compare,'stereo PIV')
2766    test_disp=1;   
2767    nom_type_ima1=browse.nom_type_ima_1; %nomenclature type of the second file series
2768    [Path2,Name2]=fileparts(filebase_B);
2769    Path1=Path2;
2770    Name1=get(handles.RootName_1,'String');% root name of the first field series for stereo
2771    filebase_A=fullfile(Path1,Name1);
2772    if length(Name1)>6
2773        Name1=Name1(end-5:end);
2774    end
2775    if length(Name2)>6
2776        Name2=Name2(end-5:end);
2777    end
2778    filebase_AB=fullfile(Path2,[Name2 '-' Name1]);
2779else
2780    test_disp=0;
2781    filebase_A=filebase;
2782    nom_type_ima1=nom_type_ima2;
2783    filebase_AB=filebase;
2784end
2785if strcmp(compare,'displacement')
2786        filebase_ima1=filebase_A;
2787        filebase_ima2=filebase_B;
2788        filebase_nc=filebase_AB; %root name for the result of civ2 
2789else
2790        filebase_ima1=filebase_B;
2791        filebase_ima2=filebase_B;
2792        filebase_nc=filebase_B;
2793end   
2794
2795%determine reference files for fix:
2796file_ref_fix1={};%default
2797file_ref_fix2={};
2798nbfield=length(num1_civ1);
2799nbslice=length(num_a_civ1);
2800if box_test(2)==1% fix1 performed
2801    ref=get(handles.ref_fix1,'UserData');%read data on the ref file stored by get_ref_fix1_Callback
2802    if ~isempty(ref)
2803        first_i=str2num(get(handles.first_i,'String'));
2804        last_i=str2num(get(handles.last_i,'String'));
2805        incr_i=str2num(get(handles.incr_i,'String'));
2806        first_j=str2num(get(handles.first_j,'String'));
2807        last_j=str2num(get(handles.last_j,'String'));
2808        incr_j=str2num(get(handles.incr_j,'String'));
2809        num_i_ref=[first_i:incr_i:last_i];
2810        num_j_ref=[first_j:incr_j:last_j];
2811        if isequal(mode,'displacement')
2812            num_i1=num_i_ref;
2813            num_i2=num_i_ref;
2814            num_j1=num_j_ref;
2815            num_j2=num_j_ref;
2816        elseif isequal(mode,'pair j1-j2')% isequal(mode,'st_pair j1-j2')
2817            num_i1=num_i_ref;
2818            num_i2=num_i1;
2819            num_j1=ref.num_a*ones(size(num_i_ref));
2820            num_j2=ref.num_b*ones(size(num_i_ref));
2821        elseif isequal(mode,'series(Di)') % isequal(mode,'st_series(Di)')
2822            delta1=floor((ref.num2-ref.num1)/2);
2823            delta2=ceil((ref.num2-ref.num1)/2);
2824            num_i1=num_i_ref-delta1*ones(size(num_i_ref));
2825            num_i2=num_i_ref+delta2*ones(size(num_i_ref));
2826            if isempty(ref.num_a)
2827                ref.num_a=1;
2828            end
2829            num_j1=ref.num_a*ones(size(num_i1));
2830            num_j2=num_j1;
2831        elseif isequal(mode,'series(Dj)')%| isequal(mode,'st_series(Dj)')
2832            delta1=floor((ref.num_b-ref.num_a)/2);
2833            delta2=ceil((ref.num_b-ref.num_a)/2);
2834            num_i1=ref.num1*ones(size(num_i_ref));
2835            num_i2=num_i1;
2836            num_j1=num_j_ref-delta1*ones(size(num_j_ref));
2837            num_j2=num_j_ref+delta2*ones(size(num_j_ref));
2838        end
2839        for ifile=1:nbfield
2840            for j=1:nbslice
2841                file_ref=name_generator(ref.filebase,num_i1(ifile),num_j1(j),'.nc',ref.nom_type,1,num_i2(ifile),num_j2(j),ref.subdir);%
2842                file_ref_fix1(ifile,j)={file_ref};
2843                if ~exist(file_ref,'file')
2844                    msgbox_uvmat('ERROR',['reference file ' file_ref ' not found for fix1'])
2845                    filecell=[];
2846                    return
2847                end
2848            end
2849        end
2850    end
2851end
2852
2853%determine reference files for fix2:
2854if box_test(5)==1% fix2 performed
2855    ref=get(handles.ref_fix2,'UserData');
2856    if ~isempty(ref)
2857        first_i=str2num(get(handles.first_i,'String'));
2858        last_i=str2num(get(handles.last_i,'String'));
2859        incr_i=str2num(get(handles.incr_i,'String'));
2860        first_j=str2num(get(handles.first_j,'String'));
2861        last_j=str2num(get(handles.last_j,'String'));
2862        incr_j=str2num(get(handles.incr_j,'String'));
2863        num_i_ref=[first_i:incr_i:last_i];
2864        num_j_ref=[first_j:incr_j:last_j];
2865        if isequal(mode,'displacement')
2866            num_i1=num_i_ref;
2867            num_i2=num_i_ref;
2868            num_j1=num_j_ref;
2869            num_j2=num_j_ref;
2870        elseif isequal(mode,'pair j1-j2')
2871            num_i1=num_i_ref;
2872            num_i2=num_i1;
2873            num_j1=ref.num_a;
2874            num_j2=ref.num_b;
2875        elseif isequal(mode,'series(Di)')
2876            delta1=floor((ref.num2-ref.num1)/2);
2877            delta2=ceil((ref.num2-ref.num1)/2);
2878            num_i1=num_i_ref-delta1*ones(size(num_i_ref));
2879            num_i2=num_i_ref+delta2*ones(size(num_i_ref));
2880            num_j1=ref.num_a*ones(size(num_i1));
2881            num_j2=num_j1;
2882        elseif isequal(mode,'series(Dj)')
2883            delta1=floor((ref.num_b-ref.num_a)/2);
2884            delta2=ceil((ref.num_b-ref.num_a)/2);
2885            num_i1=ref.num1*ones(size(num_i_ref));
2886            num_i2=num_i1;
2887            num_j1=num_j_ref-delta1*ones(size(num_j_ref));
2888            num_j2=num_j_ref+delta2*ones(size(num_j_ref));
2889        end
2890        for ifile=1:nbfield
2891            for j=1:nbslice
2892                file_ref=name_generator(ref.filebase,num_i1(ifile),num_j1(j),'.nc',ref.nom_type,1,num_i2(ifile),num_j2(j),ref.subdir);%
2893                file_ref_fix2(ifile,j)={file_ref};
2894                if ~exist(file_ref,'file')
2895                    msgbox_uvmat('ERROR',['reference file ' file_ref ' not found for fix2'])
2896                    filecell=[];
2897                    return
2898                end
2899            end
2900        end
2901    end
2902end
2903
2904%check dir
2905subdir_civ1=get(handles.subdir_civ1,'String');%subdirectory subdir_civ1 for the netcdf output data
2906subdir_civ2=get(handles.subdir_civ2,'String');
2907if isequal(subdir_civ1,''),subdir_civ1='A'; end% put default subdir
2908if isequal(subdir_civ2,''),subdir_civ2=subdir_civ1; end% put default subdir
2909currentdir=pwd;%store the current working directory
2910[Path_ima,Name]=fileparts(filebase);%Path of the image files (.civ)
2911if ~exist(Path_ima,'dir')
2912    msgbox_uvmat('ERROR',['path to images ' Path_ima ' not found'])
2913    filecell=[];
2914    return
2915end
2916cd(Path_ima);%move to the directory of the images: needed to create the result dir by 'mkdir'
2917dircur=pwd; %current working directory
2918m2='';
2919[erread,message]=fileattrib(Path_ima);
2920if ~isempty(message) & ~isequal(message.UserWrite,1)
2921      msgbox_uvmat('ERROR',['No writting access to ' Path_ima])
2922      filecell=[];
2923      cd(currentdir);
2924      return
2925end
2926
2927%check the existence of the netcdf and image files involved
2928% %%%%%%%%%%%%  case CIV1 activated   %%%%%%%%%%%%%
2929if box_test(1)==1;
2930     detect=1;     
2931     while detect==1 %create a new subdir if the netcdf files already exist
2932          for ifile=1:nbfield;
2933              for j=1:nbslice
2934                  filename=name_generator(filebase_nc,num1_civ1(ifile),num_a_civ1(j),'.nc',nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);
2935                  detect=exist(filename,'file')==2;
2936                  if detect% if a netcdf file already exists
2937                     subdir_civ1=[subdir_civ1 '.0'];
2938                     subdir_civ2=subdir_civ1;
2939                     break
2940                  end
2941                  filecell.nc.civ1(ifile,j)={filename};
2942              end
2943              if detect% if a netcdf file already exists
2944                  break
2945              end
2946          end
2947           %create the new subdir_civ1
2948          if ~exist(fullfile(Path_ima,subdir_civ1),'dir')
2949              [m1,m2,m3]=mkdir(subdir_civ1);
2950                if ~isequal(m2,'')
2951                   msgbox_uvmat('ERROR', m2)
2952                   %msgbox(m2);%error message for directory creation
2953              end
2954          end
2955          if strcmp(compare,'stereo PIV')&&(strcmp(mode,'pair j1-j2')||strcmp(mode,'series(Dj)')||strcmp(mode,'series(Di)'))%check second nc series
2956                for ifile=1:nbfield
2957                     for j=1:nbslice
2958                         filename=name_generator(filebase_A,num1_civ1(ifile),num_a_civ1(j),'.nc',nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
2959                          detect=exist(filename,'file')==2;
2960                          if detect% if a netcdf file already exists
2961                              subdir_civ1=[subdir_civ1 '.0'];
2962                              subdir_civ2=subdir_civ1;
2963                              break
2964                          end
2965                          filecell.ncA.civ1(ifile,j)={filename};
2966                     end
2967                    if detect% if a netcdf file already exists
2968                         break
2969                    end
2970              end
2971           %create the new subdir_civ1
2972               if ~exist(fullfile(Path_ima,subdir_civ1),'dir')
2973                      [m1,m2,m3]=mkdir(subdir_civ1);
2974                       if ~isequal(m2,'')
2975                            msgbox_uvmat('ERROR', m2)
2976                      end
2977               end
2978          end
2979     end       
2980    % get image names
2981    for ifile=1:nbfield
2982         for j=1:nbslice
2983            filename=name_generator(filebase_ima1, num1_civ1(ifile),num_a_civ1(j),ext_ima,nom_type_ima1);
2984            idetect(j)=exist(filename,'file')==2;
2985            filecell.ima1.civ1(ifile,j)={filename}; %first image
2986            filename=name_generator(filebase_ima2, num2_civ1(ifile),num_b_civ1(j),ext_ima,nom_type_ima2);
2987            idetect_1(j)=exist(filename,'file')==2;
2988            filecell.ima2.civ1(ifile,j)={filename};%second image
2989         end
2990            [idetectmin,indexj]=min(idetect);
2991          if idetectmin==0,
2992                msgbox_uvmat('ERROR',[filecell.ima1.civ1{ifile,indexj} ' not found'])
2993                filecell=[];
2994                return
2995          end
2996            [idetectmin,indexj]=min(idetect_1);
2997          if idetectmin==0,
2998                msgbox_uvmat('ERROR',[filecell.ima2.civ1{ifile,indexj} ' not found'])
2999                filecell=[];
3000                cd(currentdir)
3001                return
3002          end
3003    end
3004    if strcmp(compare,'stereo PIV') && (strcmp(mode,'pair j1-j2') || strcmp(mode,'series(Dj)') || strcmp(mode,'series(Di)'))
3005         for ifile=1:nbfield
3006             for j=1:nbslice
3007                  filename=name_generator(filebase_A, num1_civ1(ifile),num_a_civ1(j),ext_ima,nom_type_ima1);
3008                  idetect(j)=exist(filename,'file')==2;
3009                  filecell.imaA1.civ1(ifile,j)={filename} ;%first image
3010                  filename=name_generator(filebase_A, num2_civ1(ifile),num_b_civ1(j),ext_ima,nom_type_ima2);
3011                  idetect_1(j)=exist(filename,'file')==2;
3012                  filecell.imaA2.civ1(ifile,j)={filename};%second image
3013             end
3014              [idetectmin,indexj]=min(idetect);
3015              if idetectmin==0,
3016                    msgbox_uvmat('ERROR',[filecell.imaA1.civ1{ifile,indexj} ' not found'])
3017                    filecell=[];
3018                    cd(currentdir)
3019                    return
3020              end
3021               [idetectmin,indexj]=min(idetect_1);
3022               if idetectmin==0,
3023                   msgbox_uvmat('ERROR',[filecell.imaA2.civ1{ifile,indexj} ' not found'])
3024                  filecell=[];
3025                  cd(currentdir)
3026                  return
3027               end
3028         end
3029    end
3030       
3031 %%%%%%%%%%%%%  fix1 or patch1 activated but no civ1   %%%%%%%%%%%%%
3032elseif (box_test(2)==1 || box_test(3)==1); 
3033    for ifile=1:nbfield
3034        for j=1:nbslice
3035            filename=name_generator(filebase_nc,num1_civ1(ifile),num_a_civ1(j),'.nc',...
3036                    nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
3037            detect=exist(filename,'file')==2;   
3038            filecell.nc.civ1(ifile,j)={filename};
3039        end
3040    end
3041    if strcmp(compare,'stereo PIV')
3042            for ifile=1:nbfield
3043                for j=1:nbslice
3044                    filename=name_generator(filebase_A,num1_civ1(ifile),num_a_civ1(j),'.nc',nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
3045                    filecell.ncA.civ1(ifile,j)={filename};
3046                    if ~exist(filename,'file')
3047                        msgbox_uvmat('ERROR',['input file ' filename ' not found'])
3048                        set(handles.RUN, 'Enable','On')
3049                        set(handles.RUN,'BackgroundColor',[1 0 0])
3050                        set(handles.BATCH, 'Enable','On')
3051                    set(handles.BATCH,'BackgroundColor',[1 0 0])
3052                    filecell=[];
3053                        cd(currentdir)
3054                        return
3055                    end
3056                end
3057            end
3058    end
3059end
3060
3061%%%%%%%%%%%%%  if civ2 performed with pairs different than civ1  %%%%%%%%%%%%%
3062testdiff=0;
3063if (box_test(4)==1)&&...
3064        ((get(handles.list_pair_civ1,'Value')~=get(handles.list_pair_civ2,'Value'))||~isequal(subdir_civ2,subdir_civ1))
3065    testdiff=1;
3066    detect=1;
3067    while detect==1 %create a new subdir if the netcdf files already exist
3068          for ifile=1:nbfield
3069              for j=1:nbslice
3070                  filename=name_generator(filebase_nc,num1_civ2(ifile),num_a_civ2(j),'.nc',nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
3071                  detect=exist(filename,'file')==2;
3072                  if detect% if a netcdf file already exists
3073                     subdir_civ2=[subdir_civ2 '.0'];
3074                     break
3075                  end
3076                  filecell.nc.civ2(ifile,j)={filename};
3077              end
3078              if detect% if a netcdf file already exists
3079                  break
3080              end
3081          end
3082           %create the new subdir_civ2
3083          if ~exist(fullfile(Path_ima,subdir_civ2),'dir')
3084              [m1,m2,m3]=mkdir(subdir_civ2);
3085              if ~isequal(m2,'')
3086                   msgbox_uvmat('ERROR', m2)
3087              end
3088          end
3089           if strcmp(compare,'stereo PIV')%check second nc series
3090                for ifile=1:nbfield
3091                     for j=1:nbslice
3092                         filename=name_generator(filebase_A,num1_civ2(ifile),num_a_civ2(j),'.nc',...
3093                          nom_type_nc,1,num2_civ2(ifile),num_b_civ1(j),subdir_civ2);%
3094                          detect=exist(filename,'file')==2;
3095                          if detect% if a netcdf file already exists
3096                              subdir_civ2=[subdir_civ2 '.0'];
3097                              break
3098                          end
3099                          filecell.ncA.civ2(ifile,j)={filename};
3100                     end
3101                    if detect% if a netcdf file already exists
3102                         break
3103                    end
3104              end
3105           %create the new subdir_civ1
3106               if ~exist(fullfile(Path_ima,subdir_civ2),'dir')
3107                      [m1,m2,m3]=mkdir(subdir_civ2);
3108                        if ~isequal(m2,'')
3109                            msgbox_uvmat('ERROR', m2)
3110                            %msgbox(m2);%error message for directory creation
3111                      end
3112               end
3113          end
3114    end 
3115end
3116cd(currentdir);%come back to the current working directory
3117   
3118
3119%%%%%%%%%%%%%  if civ2 results are obtained or used  %%%%%%%%%%%%%
3120if box_test(4)==1 || box_test(5)==1 || box_test(6)==1 %civ2
3121    %check source netcdf file of civ1 estimates
3122    if box_test(1)==0; %no civ1 performed
3123        for ifile=1:nbfield
3124            for j=1:nbslice
3125                filename=name_generator(filebase_nc,num1_civ1(ifile),num_a_civ1(j),'.nc',...
3126                    nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
3127                filecell.nc.civ1(ifile,j)={filename};% name of the civ1 file
3128                if ~exist(filename,'file')
3129                    msgbox_uvmat('ERROR',['input file ' filename ' not found'])
3130                  filecell=[];
3131                    return
3132                end
3133                if ~testdiff % civ2 or patch2 are written in the same file as civ1
3134                    if box_test(4)==0 ; %check the existence of civ2 if it is not calculated
3135                        Data=nc2struct(filename,'ListGlobalAttribute','civ2');
3136                        if isempty(Data.civ2)||isequal(Data.civ2,0)
3137                            msgbox_uvmat('ERROR',['no civ2 data in ' filename])
3138                            filecell=[];
3139                            return
3140                        end 
3141                    elseif box_test(3)==0; %check the existence of patch if it is not calculated
3142                        Data=nc2struct(filename,'ListGlobalAttribute','patch');
3143                        if isempty(Data.patch)||isequal(Data.patch,0)
3144                            msgbox_uvmat('ERROR',['no patch data in ' filename])
3145                            filecell=[];
3146                            return
3147                        end
3148                    end
3149                end
3150            end
3151        end
3152        if strcmp(compare,'stereo PIV')
3153            for ifile=1:nbfield
3154                for j=1:nbslice
3155                    filename=name_generator(filebase_A,num1_civ2(ifile),num_a_civ2(j),'.nc',...
3156                        nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
3157                    filecell.ncA.civ2(ifile,j)={filename};
3158                    if ~exist(filename,'file')
3159                        msgbox_uvmat('ERROR',['input file ' filename ' not found'])
3160                        set(handles.RUN, 'Enable','On')
3161                        set(handles.RUN,'BackgroundColor',[1 0 0])
3162                               set(handles.BATCH, 'Enable','On')
3163                    set(handles.BATCH,'BackgroundColor',[1 0 0])
3164                        cd(currentdir)
3165                        return
3166                    end
3167                end
3168            end
3169        end
3170    end
3171
3172    detect=1;
3173%     while detect==1%creates a new subdir if the netcdf files already contain civ2 data
3174        for ifile=1:nbfield
3175            for j=1:nbslice
3176                filename=name_generator(filebase_nc,num1_civ2(ifile),num_a_civ2(j),'.nc',...
3177                nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);
3178                detect=exist(filename,'file')==2;
3179                filecell.nc.civ2(ifile,j)={filename};
3180            end
3181        end
3182    %get first image names for civ2
3183    if box_test(1)==1 & isequal(num1_civ1,num1_civ2) & isequal(num_a_civ1,num_a_civ2)
3184        filecell.ima1.civ2=filecell.ima1.civ1;
3185    elseif box_test(4)==1
3186        for ifile=1:nbfield
3187            for j=1:nbslice
3188                filename=name_generator(filebase_ima1, num1_civ2(ifile),num_a_civ2(j),ext_ima,nom_type_ima1);
3189                idetect_2(j)=exist(filename,'file')==2;
3190                filecell.ima1.civ2(ifile,j)={filename};%first image
3191            end
3192                [idetectmin,indexj]=min(idetect_2);
3193            if idetectmin==0,
3194                    msgbox_uvmat('ERROR',['input image ' filecell.ima1.civ2{ifile,indexj} ' not found'])
3195                    filecell=[];
3196                    return
3197            end
3198        end
3199    end
3200   
3201    %get second image names for civ2
3202    if box_test(1)==1 & isequal(num2_civ1,num2_civ2) & isequal(num_b_civ1,num_b_civ2)
3203        filecell.ima2.civ2=filecell.ima2.civ1;
3204    elseif box_test(4)==1
3205        for ifile=1:nbfield
3206            for j=1:nbslice
3207                filename=name_generator(filebase_ima2, num2_civ2(ifile),num_b_civ2(j),ext_ima,nom_type_ima2);
3208                idetect_3(j)=exist(filename,'file')==2;
3209                filecell.ima2.civ2(ifile,j)={filename};%first image
3210            end
3211                [idetectmin,indexj]=min(idetect_3);
3212            if idetectmin==0,
3213                    msgbox_uvmat('ERROR',['input image ' filecell.ima2.civ2{ifile,indexj} ' not found'])
3214                    filecell=[];
3215                    return
3216            end
3217        end
3218    end
3219end
3220if (box_test(5)==1 || box_test(6)==1 ) && box_test(4)==0  % need to read an existing netcdf civ2 file
3221    if ~testdiff
3222        filecell.nc.civ2=filecell.nc.civ1;% file already checked
3223    else     % check the civ2 files
3224        for ifile=1:nbfield
3225            for j=1:nbslice
3226                filename=name_generator(filebase_nc,num1_civ2(ifile),num_a_civ2(j),'.nc',...
3227                        nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
3228                filecell.nc.civ2(ifile,j)={filename};
3229                if ~exist(filename,'file')
3230                    msgbox_uvmat('ERROR',['input file ' filename ' not found'])
3231                    filecell=[];
3232                    return
3233                else
3234                    Data=nc2struct(filename,'ListGlobalAttribute','civ2');
3235                    if isempty(Data.civ2)||isequal(Data.civ2,0)         
3236                        msgbox_uvmat('ERROR',['no civ2 data in ' filename])
3237                        filecell=[];
3238                        return
3239                    end
3240                end
3241            end
3242        end
3243    end
3244end
3245
3246%%%%%%%%%%%%%  if stereo fields are calculated by PATCH %%%%%%%%%%%%%
3247if strcmp(compare,'stereo PIV')
3248    if  box_test(3)==1 & isequal(get(handles.test_stereo1,'Value'),1)
3249        for ifile=1:nbfield
3250            for j=1:nbslice
3251                filename=name_generator(filebase_AB,num1_civ1(ifile),num_a_civ1(j),'.nc',...
3252                    nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
3253                filecell.st(ifile,j)={filename};
3254            end
3255        end
3256    end
3257     if  box_test(6)==1 & isequal(get(handles.test_stereo2,'Value'),1)
3258        for ifile=1:nbfield
3259            for j=1:nbslice
3260                filename=name_generator(filebase_AB,num1_civ2(ifile),num_a_civ2(j),'.nc',...
3261                    nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
3262                filecell.st(ifile,j)={filename};
3263            end
3264        end
3265     end
3266end
3267set(handles.subdir_civ1,'String',subdir_civ1);%update the edit box
3268set(handles.subdir_civ2,'String',subdir_civ2);%update the edit box
3269browse.nom_type_nc=nom_type_nc;
3270set(handles.browse_root,'UserData',browse); %update the nomenclature type for uvmat
3271
3272
3273%COPY IMAGES TO THE FORMAT .png IF NEEDED
3274if isequal(nom_type_ima1,'*')%case of movie files
3275    nom_type_imanew1='_i';
3276else
3277    nom_type_imanew1=nom_type_ima1;
3278end
3279if isequal(nom_type_ima2,'*')%case of movie files
3280    nom_type_imanew2='_i';
3281else
3282    nom_type_imanew2=nom_type_ima2;
3283end
3284if ~isequal(ext_ima,'.png')
3285    %%type of image file
3286    type_ima1='none';%default
3287    movieobject1=[];%default
3288    if isequal(lower(ext_ima),'.avi')
3289        hhh=which('mmreader');
3290        if ~isequal(hhh,'')&& mmreader.isPlatformSupported()% if the mmreader function is found (recent version of matlab)
3291            type_ima1='movie';
3292            movieobject1=mmreader([filebase_ima2 ext_ima]);
3293        else
3294            type_ima1='avi';
3295        end
3296    else
3297       form=imformats(ext_ima(2:end));
3298       if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
3299           if isequal(nom_type_ima1,'*');
3300               type_ima1='multimage';%image series in a single image file
3301           else
3302               type_ima1='image';
3303           end
3304       end
3305    end
3306    type_ima2='none';%default
3307    movieobject2=[];
3308    if isequal(lower(ext_ima),'.avi')
3309        hhh=which('mmreader');
3310        if ~isequal(hhh,'')&& mmreader.isPlatformSupported()% if the mmreader function is found (recent version of matlab)
3311            type_ima2='movie';
3312            movieobject2=mmreader([filebase_ima2 ext_ima]);
3313        else
3314            type_ima2='avi';
3315        end
3316    else
3317       form=imformats(ext_ima(2:end));
3318       if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
3319           if isequal(nom_type_ima1,'*');
3320               type_ima2='multimage';%image series in a single image file
3321           else
3322               type_ima2='image';
3323           end
3324       end
3325    end
3326    %npxy=get(handles.ImaExt,'UserData');
3327% %     if numel(npxy)<2
3328%     
3329%         filename=name_generator(filebase_ima1,num1_civ1(1),num_a_civ1(1),ImaExt,nom_type_ima1);
3330%         A=imread(filename);
3331%         npxy=size(A);
3332% %     end
3333%     npy=npxy(1);
3334%     npx=npxy(2);
3335    if box_test(1)==1 %if civ1 is performed
3336       h = waitbar(0,['copy images to the .png format for civ1']);% display a wait bar
3337       for ifile=1:nbfield
3338            waitbar(ifile/nbfield);
3339            for j=1:nbslice
3340                    filename=name_generator(filebase_ima1,num1_civ1(ifile),num_a_civ1(j),'.png',nom_type_imanew1);
3341                    if ~exist(filename,'file')
3342                        A=read_image(filecell.ima1.civ1{ifile,j},type_ima1,num1_civ1(ifile),movieobject1);
3343                        imwrite(A,filename,'BitDepth',16);
3344                    end
3345                    filecell.ima1.civ1(ifile,j)={filename};
3346                    filename=name_generator(filebase_ima2, num2_civ1(ifile),num_b_civ1(j),'.png',nom_type_imanew2);
3347                    if ~exist(filename,'file')
3348                        A=read_image(filecell.ima2.civ1{ifile,j},type_ima2,num2_civ1(ifile),movieobject2);
3349                        imwrite(A,filename,'BitDepth',16);
3350                    end
3351                    filecell.ima2.civ1(ifile,j)={filename};
3352            end
3353        end
3354        close(h)
3355    end
3356    if box_test(4)==1 %if civ2 is performed
3357        h = waitbar(0,['copy images to the .png format for civ2']);% display a wait bar
3358        for ifile=1:nbfield
3359            waitbar(ifile/nbfield);
3360            for j=1:nbslice
3361                    filename=name_generator(filebase_ima1,num1_civ2(ifile),num_a_civ2(j),'.png',nom_type_imanew1);
3362                    if ~exist(filename,'file')
3363                        A=read_image(cell2mat(filecell.ima1.civ2(ifile,j)),type_ima2,num1_civ2(ifile));
3364                        imwrite(A,filename,'BitDepth',16);
3365                    end
3366                    filecell.ima1.civ2(ifile,j)={filename};
3367                    filename=name_generator(filebase_ima2, num2_civ2(ifile),num_b_civ2(j),'.png',nom_type_imanew2);
3368                    if ~exist(filename,'file')
3369                        A=read_image(cell2mat(filecell.ima2.civ2(ifile,j)),type_ima2,num2_civ2(ifile));
3370                        imwrite(A,filename,'BitDepth',16);
3371                    end
3372                    filecell.ima2.civ2(ifile,j)={filename};
3373            end
3374        end
3375        close(h);
3376    end
3377end
3378
3379%------------------------------------------------------------------------
3380%CIV1  CIV1  CIV1 CIV1 NOT USED: replaced by RUN_BATCH
3381function RUN_CIV1(handles,filecell,filecell_1,filecell_nc1,num1,num2,num_a,num_b,nom_type_nc)
3382%------------------------------------------------------------------------
3383%pixels per cm and matrix of the image times, read from the .civ file by uvmat
3384global civ1Bin sge%name of the executable for civ1 calculation
3385
3386%get civ parameters
3387ibx_val=str2num(get(handles.ibx,'String'));
3388if isempty(ibx_val)
3389    ibx='21'; set(handles.ibx,'String','21')
3390else
3391    ibx=num2str(ibx_val);
3392end
3393iby_val=str2num(get(handles.iby,'String'));
3394if isempty(iby_val)
3395    iby='21'; set(handles.iby,'String','21')
3396else
3397    iby=num2str(iby_val);
3398end
3399isx=get(handles.isx,'String');
3400if isempty(str2num(isx)), isx='41'; set(handles.isx,'String','41'), end; %default
3401if str2num(isx)<ibx_val+8,isx=num2str(ibx_val+8); set(handles.isx,'String',num2str(ibx_val+8)); end
3402isy=get(handles.isy,'String');
3403if isempty(str2num(isy)), isy='41'; set(handles.isy,'String','41'), end;%default
3404if str2num(isy)<iby_val+8,isy=num2str(iby_val+8); set(handles.isy,'String',num2str(iby_val+8)); end
3405shiftx=get(handles.shiftx,'String');
3406if isempty(str2num(shiftx)), shiftx='0'; set(handles.shiftx,'String','0'), end;%default
3407shifty=get(handles.shifty,'String');
3408if isempty(str2num(shifty)), shifty='0'; set(handles.shifty,'String','0'), end;%default
3409rho=get(handles.rho,'String');
3410dx=get(handles.dx_civ1,'String');
3411if isequal(str2num(dx),[]), dx='20'; set(handles.dx_civ1,'String','20'); end%default
3412dy=get(handles.dy_civ1,'String');
3413if isequal(str2num(dy),[]), dy='20'; set(handles.dy_civ1,'String','20');end %default
3414if isequal(str2num(dy),[]), dy='20'; end%default
3415pxcmx='1'; %velocity fields are expressed in pixel displacement
3416pxcmy='1';
3417image_first=cell2mat(filecell(1,1));
3418if ~exist(image_first,'file')
3419    msgbox_uvmat('ERROR',['image ' image_first 'not found'])
3420    set(handles.RUN, 'Enable','On')
3421    set(handles.RUN,'BackgroundColor',[1 0 0])
3422    return
3423end
3424A=imread(cell2mat(filecell(1,1)));%read the first image to get the size
3425sizim=size(A);
3426npx=num2str(sizim(2));
3427npy=num2str(sizim(1));
3428time=get(handles.RootName,'UserData'); %get the set of times
3429gridname='';%default  ='noFile use default'
3430gridflag='n';%default
3431test_grid=get(handles.browse_gridciv1,'Value');
3432nbslice_grid=[];
3433if test_grid
3434    gridname=get(handles.grid_civ1,'String');
3435    if numel(gridname)>4 && isequal(gridname(end-3:end),'grid')
3436        nbslice_grid=str2num(gridname(1:end-4)); %
3437        if ~isempty(nbslice_grid)
3438            gridflag='y';
3439        end
3440    elseif exist(gridname,'file')
3441        gridflag='y';
3442    else
3443        msgbox_uvmat('ERROR',['input grid file ' gridname ' not found'])
3444        return
3445    end
3446end
3447if isequal(get(handles.ImaThreshold,'Value'),1)
3448    threshflag='y';
3449    min_ima=get(handles.MinIma,'String');
3450    max_ima=get(handles.MaxIma,'String');
3451else
3452    threshflag='n';
3453    min_ima='0';
3454    max_ima='4096';
3455end
3456 %main loop
3457filebase=get(handles.RootName,'String');
3458sizcell=size(filecell);
3459nbfield=sizcell(1);
3460nbslice=sizcell(2);
3461icount=0;
3462for ifile=1:nbfield
3463    for j=1:nbslice
3464        icount=icount+1;
3465        barlength=0.188*icount/(nbfield*nbslice);
3466        set(handles.waitbar_1,'Position',[0.946 0.877-barlength 0.03 barlength])
3467        drawnow
3468        filename_ima=cell2mat(filecell(ifile,j));
3469        filename_ima([end-3:end])=[];%remove .png extension
3470        filename_ima_1=cell2mat(filecell_1(ifile,j));
3471        filename_ima_1([end-3:end])=[];%remove .png extension
3472        filename_cmx=cell2mat(filecell_nc1(ifile,j));%output netcdf file
3473        filename_cmx([end-1:end])=[ 'cm'];%name of cmx file
3474        filename_cmx=[filename_cmx 'x'];
3475        namelog=[filename_cmx([1:end-3]) 'log'];       
3476        if size(time,1)>=num2(ifile) &  size(time,2)>=num_b(j)
3477            Dt=num2str(time(num2(ifile),num_b(j))-time(num1(ifile),num_a(j)));
3478            if isequal(Dt,'0')
3479                Dt='1' ;%case of 'displacement' mode
3480            end
3481            T0=num2str((time(num2(ifile),num_b(j))+time(num1(ifile),num_a(j)))/2);
3482        else
3483            Dt='1';
3484            T0='0';
3485        end
3486        term_a=num2stra(num_a(j),nom_type_nc);%UTILITE?
3487        term_b=num2stra(num_b(j),nom_type_nc);%
3488        if test_grid && ~isempty(nbslice_grid)       
3489             num1_grid=mod(num1(ifile)-1,nbslice_grid)+1;
3490            gridname=[filebase '_' name_generator(gridname,num1_grid,1,'.grid','_i')];
3491            if ~exist(gridname,'file')
3492                msgbox_uvmat('ERROR',['missing grid file ' gridname])
3493                return
3494            end
3495        end
3496        test_mask=get(handles.get_mask_civ1,'Value');
3497        if test_mask==0
3498            maskname='noFile use default';
3499            maskflag='n';
3500        else
3501            maskdispl=get(handles.mask_civ1,'String');%look for mask name in edit box
3502            maskbase=[filebase '_' maskdispl];%
3503            nbslice=str2num(maskdispl(1:end-4)); %
3504            num1_mask=mod(num1(ifile)-1,nbslice)+1;
3505            maskname=name_generator(maskbase,num1_mask,1,'.png','_i');
3506            if exist(maskname,'file')
3507                maskflag='y';
3508            else
3509                maskname='noFile use default';
3510                maskflag='n';
3511            end
3512        end
3513                textcmx={'##############   CMX file';...
3514                ['FirstImage ' filename_ima];...
3515                ['LastImage  ' filename_ima_1];...
3516                'XX' ;...
3517                ['Mask ' maskflag] ;...
3518                ['MaskName ' maskname];...
3519                ['ImageSize ' npx ' ' npy];...   %VERIFIER CAS GENERAL ?
3520                ['CorrelationBoxesSize ' ibx ' ' iby];...
3521                ['SearchBoxeSize ' isx ' ' isy];...
3522                ['RO ' rho];...
3523                ['GridSpacing ' dx ' ' dy];...
3524                'XX 1.0';...
3525                ['Dt_TO ' Dt ' ' T0];...
3526                ['PixCmXY ' pxcmx ' ' pxcmy];...
3527                'XX 1';...
3528                ['ShiftXY ' shiftx ' ' shifty];...
3529                ['Grid ' gridflag];...
3530                ['GridName ' gridname] ;...
3531                'XX 85';...
3532                'XX 1.0';...
3533                'XX 1.0';...
3534                'Hart 1';...
3535                'DecimalShift 0';...
3536                'Deformation 0';...
3537                'CorrelationMin 0';...
3538                'IntensityMin 0';...
3539                ['SeuilImage ' threshflag];...
3540                ['SeuilImageValues ' min_ima ' ' max_ima];...
3541                ['ImageToUse ' term_a ' ' term_b];... % VERIFIER ?
3542                'ImageUsedBefore null null'};
3543                textout=char(textcmx);
3544%               dlmwrite(filename_cmx,textout,'');
3545     fid=fopen([filename_cmx],'w');
3546fprintf(fid, ['##############   CMX file' '\n']);
3547 fprintf(fid,   ['FirstImage ' regexprep(filename_ima,'\\','\\\\') '\n' ]);
3548 fprintf(fid,   ['LastImage  ' regexprep(filename_ima_1,'\\','\\\\') '\n' ]);
3549  fprintf(fid,  ['XX' '\n' ]);
3550  fprintf(fid,  ['Mask '  maskflag '\n' ]);
3551  fprintf(fid,  ['MaskName '  maskname '\n' ]);
3552 fprintf(fid,   ['ImageSize '  npx ' '  npy '\n' ]);   %VERIFIER CAS GENERAL ?
3553 fprintf(fid,   ['CorrelationBoxesSize '  ibx ' '  iby '\n' ]);
3554 fprintf(fid,   ['SearchBoxeSize '  isx ' '  isy '\n' ]);
3555 fprintf(fid,   ['RO '  rho '\n' ]);
3556 fprintf(fid,   ['GridSpacing '  dx ' '  dy '\n' ]);
3557 fprintf(fid,   ['XX 1.0' '\n' ]);
3558 fprintf(fid,   ['Dt_TO '  Dt ' '  T0 '\n' ]);
3559  fprintf(fid,  ['PixCmXY '  pxcmx ' '  pxcmy '\n' ]);
3560  fprintf(fid,  ['XX 1' '\n' ]);
3561 fprintf(fid,   ['ShiftXY '  shiftx ' '   shifty '\n' ]);
3562  fprintf(fid,  ['Grid '  gridflag '\n' ]);
3563 fprintf(fid,   ['GridName '  gridname '\n' ]);
3564 fprintf(fid,   ['XX 85' '\n' ]);
3565 fprintf(fid,   ['XX 1.0' '\n' ]);
3566 fprintf(fid,   ['XX 1.0' '\n' ]);
3567 fprintf(fid,   ['Hart 1' '\n' ]);
3568 fprintf(fid,  [ 'DecimalShift 0' '\n' ]);
3569 fprintf(fid,   ['Deformation 0' '\n' ]);
3570  fprintf(fid,  ['CorrelationMin 0' '\n' ]);
3571 fprintf(fid,   ['IntensityMin 0' '\n' ]);
3572  fprintf(fid,  ['SeuilImage n' '\n' ]);
3573 fprintf(fid,   ['SeuilImageValues 0 4096' '\n' ]);
3574 fprintf(fid,   ['ImageToUse '  term_a ' '  term_b '\n' ]); % VERIFIER ?
3575 fprintf(fid,   ['ImageUsedBefore null null' '\n' ]);
3576fclose(fid);
3577       
3578                s=-1;
3579        display(['!' civ1Bin ' -f ' filename_cmx ' > ' namelog ])
3580        eval(['!' civ1Bin ' -f ' filename_cmx ' > ' namelog ]);
3581%               if sge%dispatch computation on the cluster using interactive queue
3582%           %  [s,w] = unix(['qrsh -q fast.q ' civ1Bin ' -f ' filename_cmx ' > ' namelog ' 2>&1' ]);
3583%        end     
3584%         if s~=0
3585%            %  ['!' civ1Bin ' -f ' filename_cmx ' > ' namelog]
3586%            % eval(['!' civ1Bin ' -f ' filename_cmx ' > ' namelog]);       
3587%         end
3588    end
3589end
3590
3591% %------------------------------------------------------------------------
3592% % RUN CIV2   CIV2    CIV2   CIV2
3593% function RUN_CIV2(handles,filecell_2,filecell_3,filecell_nc1,filecell_nc2,num1,num2,num_a,num_b,nom_type_nc)
3594% %------------------------------------------------------------------------
3595% %filecell_2: names of first image
3596% %filecell_3: names of second images
3597% global civ2Bin sge
3598%
3599% %names of the civ2 fields
3600% field.vel_type='civ2';
3601% field.nb='nb_vectors2';
3602% field.X='vec2_X';
3603% field.Y='vec2_Y';
3604% field.U='vec2_U';
3605% field.V='vec2_V';
3606
3607% %get civ parameters
3608% ibx=get(handles.ibx_civ2,'String');
3609% iby=get(handles.iby_civ2,'String');
3610% rho=get(handles.rho_civ2,'String');
3611% decimal=int2str(get(handles.decimal,'Value'));
3612% deformation=int2str(get(handles.deformation,'Value'));
3613% dx=get(handles.dx_civ2,'String');
3614% dy=get(handles.dy_civ2,'String');
3615% if isequal(str2num(dx),[])
3616%     dx='20';%default
3617% end
3618% if isequal(str2num(dy),[])
3619%     dy='20';%default
3620% end
3621%     pxcmx='1';%velocity fields are expressed in pixel displacement
3622%     pxcmy='1';
3623% A=imread(cell2mat(filecell_2(1,1)));%read the first image to get the size
3624% sizim=size(A);
3625% npx=num2str(sizim(2));
3626% npy=num2str(sizim(1));
3627% time=get(handles.RootName,'UserData'); %get the set of times
3628% filebase=get(handles.RootName,'String');
3629% %grid
3630% gridname='';%default  ='noFile use default'
3631% gridflag='n';%default
3632% test_grid=get(handles.browse_gridciv2,'Value');
3633% nbslice_grid=[];
3634% if test_grid
3635%     gridname=get(handles.grid_civ2,'String');
3636%     if numel(gridname)>4 && isequal(gridname(end-3:end),'grid')
3637%         nbslice_grid=str2num(gridname(1:end-4)); %
3638%         if ~isempty(nbslice_grid)
3639%             gridflag='y';
3640%         end
3641%     elseif exist(gridname,'file')
3642%         gridflag='y';
3643%     else
3644%         msgbox_uvmat('ERROR',['input grid file ' gridname ' not found'])
3645%         return
3646%     end
3647% end
3648% sizcell=size(filecell_2);
3649% nbfield=sizcell(1);
3650% nbslice=sizcell(2);
3651%   
3652% %main loop
3653% icount=0;
3654% for ifile=1:nbfield
3655%     for j=1:nbslice
3656%         icount=icount+1;
3657%         barlength=0.188*icount/(nbfield*nbslice);
3658%         set(handles.waitbar_civ2,'Position',[0.946 0.407-barlength 0.03 barlength])
3659%         drawnow
3660%         filename_ima_2=cell2mat(filecell_2(ifile,j));
3661%         filename_ima_2([end-3:end])=[];%remove .png extension
3662%         filename_ima_3=cell2mat(filecell_3(ifile,j));
3663%         filename_ima_3([end-3:end])=[];%remove .png extension
3664%         filename_cmx=cell2mat(filecell_nc2(ifile,j));%output netcdf file
3665%         filename_cmx([end-1:end])=[ 'cm'];%name of cmx file
3666%         filename_cmx=[filename_cmx 'x'];
3667%         namelog=[filename_cmx([1:end-3]) 'log'];
3668%         if size(time,1)>=num2(ifile) &  size(time,2)>=num_b(j)
3669%             Dt=num2str(time(num2(ifile),num_b(j))-time(num1(ifile),num_a(j)));
3670%             if isequal(Dt,'0')
3671%                 Dt='1' ;%case of 'displacement' mode
3672%             end
3673%             T0=num2str((time(num2(ifile),num_b(j))+time(num1(ifile),num_a(j)))/2);
3674%         else
3675%             Dt='1';
3676%             T0='0';
3677%         end
3678%         term_a=num2stra(num_a(j),nom_type_nc);
3679%         term_b=num2stra(num_b(j),nom_type_nc);
3680%         filename_nc1=cell2mat(filecell_nc1(ifile,j));
3681%         filename_nc1([end-2:end])=[]; % remove '.nc'     
3682%         if test_grid && ~isempty(nbslice_grid)       
3683%             num1_grid=mod(num1(ifile)-1,nbslice_grid)+1;
3684%             gridname=[filebase '_' name_generator(gridname,num1_grid,1,'.grid','_i')];
3685%             if ~exist(gridname,'file')
3686%                 msgbox_uvmat('ERROR',['missing grid file ' gridname])
3687%                 return
3688%             end
3689%         end     
3690%         test_mask=get(handles.get_mask_civ2,'Value');
3691%         if test_mask==0 
3692%             maskname='noFile use default';
3693%             maskflag='n';
3694%         else
3695%             maskdispl=get(handles.mask_civ2,'String');
3696%             maskbase=[filebase '_' maskdispl]; %
3697%             nbslice_mask=str2num(maskdispl(1:end-4)); %
3698%             num1_mask=mod(num1(ifile)-1,nbslice_mask)+1;
3699%             maskname =name_generator(maskbase,num1_mask,1,'.png','_i');
3700%             if ~exist(maskname,'file')
3701%                 maskflag='y';
3702%             else
3703%                 maskname='noFile use default';
3704%                 maskflag='n';
3705%             end
3706%         end
3707%     
3708%               textcmx={'##############   CMX file';...
3709%               ['FirstImage ' filename_ima_2];...
3710%               ['LastImage  ' filename_ima_3];...
3711%               'XX' ;...
3712%               ['Mask ' maskflag];...
3713%               ['MaskName ' maskname];...
3714%               ['ImageSize ' npx ' ' npy];...   
3715%               ['CorrelationBoxesSize ' ibx ' ' iby];...
3716%               ['SearchBoxeSize ' ibx ' ' iby];...
3717%               ['RO ' rho];...
3718%               ['GridSpacing ' dx ' ' dy];...
3719%               'XX 1.0';...
3720%               ['Dt_TO ' Dt ' ' T0];...
3721%               ['PixCmXY ' pxcmx ' ' pxcmy];...
3722%               'XX 1';...
3723%               ['ShiftXY 0 0'];...
3724%               ['Grid ' gridflag];...
3725%               ['GridName ' gridname];...
3726%               'XX 85';...
3727%               'XX 1.0';...
3728%               'XX 1.0';...
3729%               'Hart 1';...
3730%               ['DecimalShift ' decimal];...
3731%               ['Deformation ' deformation];...
3732%               'CorrelationMin 0';...
3733%               'IntensityMin 0';...
3734%               'SeuilImage n';...
3735%               'SeuilImageValues 0 4096';...
3736%               ['ImageToUse ' term_a ' ' term_b];... % VERIFIER ?
3737%               ['ImageUsedBefore ' filename_nc1]};
3738%         textout=char(textcmx);
3739%         dlmwrite(filename_cmx,textout,'');
3740%         s=-1; 
3741%         display(['!' civ2Bin ' -f ' filename_cmx ' > ' namelog ])
3742%         eval(['!' civ2Bin ' -f ' filename_cmx ' > ' namelog ]);   
3743%     
3744% %             if sge%dispatch computation on the cluster using interactive queue
3745% %             [s,w] = unix(['qrsh -q fast.q ' civ2Bin ' -f ' filename_cmx ' > ' namelog ' 2>&1']);
3746% %         end     
3747% %         if s~=0
3748% %             eval(['!' civ2Bin ' -f ' filename_cmx ' > ' namelog]);
3749% %             ['!' civ2Bin ' -f ' filename_cmx ' > ' namelog]
3750% %         end                     
3751%     end
3752% end
3753% % close(h)
3754
3755%------------------------------------------------------------------------
3756% --- PATCH
3757function cmd_PATCH=RUN_PATCH(filename_nc,nx_patch,ny_patch,rho_patch,subdomain_patch,thresh_value,test_interp,PatchBin)
3758%------------------------------------------------------------------------
3759%global patchBin patch_newBin
3760
3761
3762        namelog=[filename_nc([1:end-3]) '_patch.log'];
3763        if test_interp==0
3764            cmd_PATCH=[PatchBin ' -f ' filename_nc ' -m ' nx_patch  ' -n ' ny_patch ' -ro ' rho_patch ' -nopt ' subdomain_patch ...
3765            '  > ' namelog ' 2>&1']; % redirect standard output to the log file
3766         else %nouveau programme patch
3767             cmd_PATCH=[PatchBin ' -f ' filename_nc ' -m ' nx_patch  ' -n ' ny_patch ' -ro ' rho_patch ...
3768                ' -max ' thresh_value ' -nopt ' subdomain_patch  '  > ' namelog ' 2>&1']; % redirect standard output to the log file
3769        end
3770
3771%------------------------------------------------------------------------
3772% --- STEREO Interp
3773function cmd=RUN_STINTERP(stinterpBin,filename_A_nc,filename_B_nc,filename_nc,nx_patch,ny_patch,rho_patch,subdomain_patch,thresh_value,xmlA,xmlB)
3774%------------------------------------------------------------------------
3775namelog=[filename_nc([1:end-3]) '_stinterp.log'];
3776cmd=[stinterpBin ' -f1 ' filename_A_nc  ' -f2 ' filename_B_nc ' -f  ' filename_nc ...
3777    ' -m ' nx_patch  ' -n ' ny_patch ' -ro ' rho_patch ' -nopt ' subdomain_patch ' -c1 ' xmlA ' -c2 ' xmlB '  -xy  x -Nfy 1024 > ' namelog ' 2>&1']; % redirect standard output to the log file
3778
3779%------------------------------------------------------------------------
3780% --- Executes on button press in CIV1.
3781function CIV1_Callback(hObject, eventdata, handles)
3782%------------------------------------------------------------------------
3783val=get(handles.CIV1,'Value');
3784if isequal(val,1)
3785    enable_civ1(handles,'on')
3786    enable_pair1(handles,'on')
3787else
3788    enable_civ1(handles,'off')
3789end
3790find_netcpair_civ1(hObject, eventdata, handles);
3791
3792%------------------------------------------------------------------------
3793% --- Executes on button press in FIX1.
3794function FIX1_Callback(hObject, eventdata, handles)
3795%------------------------------------------------------------------------
3796enable_fix1(handles,get(handles.FIX1,'Value'))
3797
3798%------------------------------------------------------------------------
3799% --- Executes on button press in PATCH1.
3800function PATCH1_Callback(hObject, eventdata, handles)
3801%------------------------------------------------------------------------
3802if get(handles.PATCH1,'Value')==1
3803    enable_patch1(handles)
3804else
3805    desable_patch1(handles)
3806end
3807
3808%------------------------------------------------------------------------
3809% --- Executes on button press in CIV2.
3810function CIV2_Callback(hObject, eventdata, handles)
3811%------------------------------------------------------------------------
3812state=get(handles.CIV2,'Value');
3813enable_civ2(handles,state)
3814if state
3815    find_netcpair_civ2(hObject, eventdata, handles)
3816    enable_pair1(handles,'on')
3817end
3818
3819%------------------------------------------------------------------------
3820% --- Executes on button press in FIX2.
3821function FIX2_Callback(hObject, eventdata, handles)
3822%------------------------------------------------------------------------
3823if get(handles.FIX2,'Value')==1
3824    enable_fix2(handles)
3825    if get(handles.CIV2,'Value')==0
3826        find_netcpair_civ2(hObject, eventdata, handles) % select the available netcdf files
3827    end
3828else
3829    desable_fix2(handles)
3830end
3831
3832%------------------------------------------------------------------------
3833% --- Executes on button press in PATCH2.
3834function PATCH2_Callback(hObject, eventdata, handles)
3835%------------------------------------------------------------------------
3836if get(handles.PATCH2,'Value')==1
3837    enable_patch2(handles)
3838    if get(handles.CIV2,'Value')==0
3839        find_netcpair_civ2(hObject, eventdata, handles) % select the available netcdf files
3840    end
3841else
3842    desable_patch2(handles)
3843end
3844
3845%------------------------------------------------------------------------
3846function first_i_Callback(hObject, eventdata, handles)
3847%------------------------------------------------------------------------
3848% last_i_Callback(hObject, eventdata, handles)
3849first_i=str2num(get(handles.first_i,'String'));
3850% last_i=str2num(get(handles.last_i,'String'));
3851% ref_i=ceil((first_i+last_i)/2);
3852set(handles.ref_i,'String', num2str(first_i))% reference index for pair dt = first index
3853set(handles.ref_i_civ2,'String', num2str(first_i))% reference index for pair dt = first index
3854ref_i_Callback(hObject, eventdata, handles)%refresh dispaly of dt for pairs (in case of non constant dt)
3855
3856%------------------------------------------------------------------------
3857function first_j_Callback(hObject, eventdata, handles)
3858%------------------------------------------------------------------------
3859first_j=str2num(get(handles.first_j,'String'));
3860set(handles.ref_j,'String', num2str(first_j))% reference index for pair dt = first index
3861ref_j_Callback(hObject, eventdata, handles)%refresh dispaly of dt for pairs (in case of non constant dt)
3862
3863%------------------------------------------------------------------------
3864% --- Executes on button press in calcul_search: determine the search range isx,isy
3865function calcul_search_Callback(hObject, eventdata, handles)
3866%------------------------------------------------------------------------
3867%determine pair numbers
3868list_pair=get(handles.list_pair_civ1,'String');%get the menu of image pairs
3869index=get(handles.list_pair_civ1,'Value');
3870displ_num=get(handles.list_pair_civ1,'UserData');
3871time=get(handles.RootName,'UserData'); %get the set of times
3872pxcm_xy=get(handles.calcul_search,'UserData');
3873pxcmx=pxcm_xy(1);
3874pxcmy=pxcm_xy(2);
3875mode_list=get(handles.mode,'String');
3876mode_value=get(handles.mode,'Value');
3877mode=mode_list{mode_value};
3878if isequal (mode, 'series(Di)' )
3879    ref_i=str2num(get(handles.ref_i,'String'));
3880    num1=ref_i-floor(index/2);%  first image numbers
3881    num2=ref_i+ceil(index/2);
3882    num_a=1;
3883    num_b=1;
3884elseif isequal (mode, 'series(Dj)')
3885    num1=1;
3886    num2=1;
3887    ref_j=str2num(get(handles.ref_j,'String'));
3888    num_a=ref_j-floor(index/2);%  first image numbers
3889    num_b=ref_j+ceil(index/2);
3890elseif isequal(mode,'pair j1-j2') %case of bursts (png_old or png_2D)
3891    ref_i=str2num(get(handles.ref_i,'String'));
3892    num1=ref_i;
3893    num2=ref_i;
3894    num_a=displ_num(1,index);
3895    num_b=displ_num(2,index);
3896end
3897dt=time(num2,num_b)-time(num1,num_a);
3898ibx=str2num(get(handles.ibx,'String'));
3899iby=str2num(get(handles.iby,'String'));
3900umin=dt*pxcmx*str2num(get(handles.umin,'String'));
3901umax=dt*pxcmx*str2num(get(handles.umax,'String'));
3902vmin=dt*pxcmy*str2num(get(handles.vmin,'String'));
3903vmax=dt*pxcmy*str2num(get(handles.vmax,'String'));
3904shiftx=round((umin+umax)/2);
3905shifty=round((vmin+vmax)/2);
3906isx=(umax+2-shiftx)*2+ibx;
3907isx=2*ceil(isx/2)+1;
3908isy=(vmax+2-shifty)*2+iby;
3909isy=2*ceil(isy/2)+1;
3910set(handles.shiftx,'String',num2str(shiftx));
3911set(handles.shifty,'String',num2str(shifty));
3912set(handles.isx,'String',num2str(isx));
3913set(handles.isy,'String',num2str(isy));
3914
3915%------------------------------------------------------------------------
3916% --- Executes on carriage return on the subdir civ1 edit window
3917function subdir_civ1_Callback(hObject, eventdata, handles)
3918%------------------------------------------------------------------------
3919subdir=get(handles.subdir_civ1,'String');
3920set(handles.subdir_civ2,'String',subdir);
3921if get(handles.CIV1,'Value')==0
3922    find_netcpair_civ1(hObject, eventdata, handles); %update the list of available pairs from netcdf files in the new directory
3923end
3924
3925%------------------------------------------------------------------------
3926% --- Executes on carriage return on the subdir civ1 edit window
3927function subdir_civ2_Callback(hObject, eventdata, handles)
3928%------------------------------------------------------------------------
3929%update the list of available pairs from netcdf files in the new directory
3930if get(handles.CIV2,'Value')==0 & get(handles.CIV1,'Value')==0 & get(handles.FIX1,'Value')==0 & get(handles.PATCH1,'Value')==0
3931    find_netcpair_civ2(hObject, eventdata, handles);
3932end
3933
3934%------------------------------------------------------------------------
3935% --- Executes on button press in get_mask_civ1.
3936function get_mask_civ1_Callback(hObject, eventdata, handles)
3937%------------------------------------------------------------------------
3938maskval=get(handles.get_mask_civ1,'Value');
3939if isequal(maskval,0)
3940    set(handles.mask_civ1,'String','')
3941else
3942    mask_displ='no mask'; %default
3943    filebase=get(handles.RootName,'String');
3944    [ nbslice_mask, flag_mask]=get_mask(filebase,handles);
3945    if isequal(flag_mask,1)
3946          mask_displ=[num2str(nbslice_mask) 'mask'];
3947    elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3948            common_path=fileparts(filebase);
3949            filebase_a=fullfile(common_path,get(handles.RootName_1,'String'));
3950            [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3951            if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice_mask)
3952                mask_displ='no mask';
3953            end
3954    end
3955    if isequal(mask_displ,'no mask')
3956        [FileName, PathName, filterindex] = uigetfile( ...
3957               {'*.png', ' (*.png)';
3958                '*.png',  '.png files '; ...
3959                '*.*', 'All Files (*.*)'}, ...
3960                'Pick a mask file *.png',filebase);
3961        mask_displ=fullfile(PathName,FileName); 
3962        if ~exist(mask_displ,'file')
3963            mask_displ='no mask';
3964        end
3965    end
3966    if isequal(mask_displ,'no mask')
3967        set(handles.get_mask_civ1,'Value',0)
3968        set(handles.get_mask_fix1,'Value',0)
3969        set(handles.get_mask_civ2,'Value',0)
3970        set(handles.get_mask_fix2,'Value',0)
3971    else
3972        set(handles.get_mask_fix1,'Value',1)
3973        set(handles.get_mask_fix2,'Value',1)
3974    end
3975    set(handles.mask_civ1,'String',mask_displ)
3976    set(handles.mask_fix1,'String',mask_displ)
3977    set(handles.mask_civ2,'String',mask_displ)
3978    set(handles.mask_fix2,'String',mask_displ)
3979end
3980set(handles.mask_civ2,'Value',maskval)
3981
3982%------------------------------------------------------------------------
3983% --- Executes on button press in get_mask_fix1.
3984function get_mask_fix1_Callback(hObject, eventdata, handles)
3985%------------------------------------------------------------------------
3986maskval=get(handles.get_mask_fix1,'Value');
3987if isequal(maskval,0)
3988    set(handles.mask_fix1,'String','')
3989else
3990mask_displ='no mask'; %default
3991filebase=get(handles.RootName,'String');
3992[nbslice, flag_mask]=get_mask(filebase,handles);
3993if isequal(flag_mask,1)
3994      mask_displ=[num2str(nbslice) 'mask'];
3995elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3996        filebase_a=get(handles.RootName_1,'String');
3997        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3998        if isequal(flag_mask_a,0) | ~isequal(nbslice_a,nbslice)
3999            mask_displ='no mask';
4000        end
4001end
4002if isequal(mask_displ,'no mask')
4003    [FileName, PathName, filterindex] = uigetfile( ...
4004           {'*.png', ' (*.png)';
4005            '*.png',  '.png files '; ...
4006            '*.*', 'All Files (*.*)'}, ...
4007            'Pick a mask file *.png',filebase);
4008    mask_displ=fullfile(PathName,FileName); 
4009    if ~exist(mask_displ,'file')
4010        mask_displ='no mask';
4011    end
4012end
4013if isequal(mask_displ,'no mask')
4014    set(handles.get_mask_fix1,'Value',0)
4015    set(handles.get_mask_civ2,'Value',0)
4016    set(handles.get_mask_fix2,'Value',0)
4017else
4018    %set(handles.get_mask_civ2,'Value',1)
4019    set(handles.get_mask_fix2,'Value',1)
4020end
4021set(handles.mask_fix1,'String',mask_displ)
4022set(handles.mask_civ2,'String',mask_displ)
4023set(handles.mask_fix2,'String',mask_displ)
4024end
4025
4026%------------------------------------------------------------------------
4027% --- Executes on button press in get_mask_civ2.
4028function get_mask_civ2_Callback(hObject, eventdata, handles)
4029%------------------------------------------------------------------------
4030maskval=get(handles.get_mask_civ2,'Value');
4031if isequal(maskval,0)
4032    set(handles.mask_civ2,'String','')
4033else
4034mask_displ='no mask'; %default
4035filebase=get(handles.RootName,'String');
4036[nbslice, flag_mask]=get_mask(filebase,handles);
4037if isequal(flag_mask,1)
4038      mask_displ=[num2str(nbslice) 'mask'];
4039elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
4040        filebase_a=get(handles.RootName_1,'String');
4041        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
4042        if isequal(flag_mask_a,0) | ~isequal(nbslice_a,nbslice)
4043            mask_displ='no mask';
4044        end
4045end
4046if isequal(mask_displ,'no mask')
4047    [FileName, PathName, filterindex] = uigetfile( ...
4048           {'*.png', ' (*.png)';
4049            '*.png',  '.png files '; ...
4050            '*.*', 'All Files (*.*)'}, ...
4051            'Pick a mask file *.png',filebase);
4052    mask_displ=fullfile(PathName,FileName);
4053    if ~exist(mask_displ,'file')
4054        mask_displ='no mask';
4055    end
4056end
4057if isequal(mask_displ,'no mask')
4058    set(handles.get_mask_civ2,'Value',0)
4059    set(handles.get_mask_fix2,'Value',0)
4060else
4061    set(handles.get_mask_fix2,'Value',1)
4062end
4063set(handles.mask_civ2,'String',mask_displ)
4064set(handles.mask_fix2,'String',mask_displ)
4065end
4066
4067%------------------------------------------------------------------------
4068% --- Executes on button press in get_mask_fix2.
4069function get_mask_fix2_Callback(hObject, eventdata, handles)
4070%------------------------------------------------------------------------
4071maskval=get(handles.get_mask_fix2,'Value');
4072if isequal(maskval,0)
4073    set(handles.mask_fix2,'String','')
4074else
4075    mask_displ='no mask'; %default
4076    filebase=get(handles.RootName,'String');
4077    [nbslice, flag_mask]=get_mask(filebase,handles);
4078    if isequal(flag_mask,1)
4079          mask_displ=[num2str(nbslice) 'mask'];
4080    elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
4081            filebase_a=get(handles.RootName_1,'String');
4082            [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
4083            if isequal(flag_mask_a,0) | ~isequal(nbslice_a,nbslice)
4084                mask_displ='no mask';
4085            end
4086    end
4087    if isequal(mask_displ,'no mask')
4088        [FileName, PathName, filterindex] = uigetfile( ...
4089               {'*.png', ' (*.png)';
4090                '*.png',  '.png files '; ...
4091                '*.*', 'All Files (*.*)'}, ...
4092                'Pick a mask file *.png',filebase);
4093        mask_displ=fullfile(PathName,FileName); 
4094        if ~exist(mask_displ,'file')
4095            mask_displ='no mask';
4096        end
4097    end
4098    if isequal(mask_displ,'no mask')
4099        set(handles.get_mask_fix2,'Value',0)
4100    end
4101    set(handles.mask_fix2,'String',mask_displ)
4102end
4103
4104%------------------------------------------------------------------------
4105% --- function called to look for mask files
4106function [nbslice, flag_mask]=get_mask(filebase,handles)
4107%------------------------------------------------------------------------
4108%detect mask files, images with appropriate file base
4109%[filebase '_' xx 'mask'], xx=nbslice
4110%flag_mask=1 indicates detection
4111
4112flag_mask=0;%default
4113nbslice=1;
4114
4115% subdir=get(handles.subdir_civ1,'String');
4116[Path,Name]=fileparts(filebase);
4117if ~isdir(Path)
4118    msgbox_uvmat('ERROR','no path for input files')
4119    return
4120end
4121currentdir=pwd;
4122cd(Path);%move in the dir of the root name filebase
4123maskfiles=dir([Name '_*mask_*.png']);%look for mask files
4124cd(currentdir);%come back to the current working directory
4125if ~isempty(maskfiles)
4126%     msgbox_uvmat('ERROR','no mask available, to create it use Tools/Make mask in the upper menu bar of uvmat')
4127% else
4128    flag_mask=1;
4129    maskname=maskfiles(1).name;% take the first mask file in the list
4130    [Path2,Name,ext]=fileparts(maskname);
4131    Namedouble=double(Name);
4132    val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
4133    ind_mask=findstr('mask',Name);
4134    i=ind_mask-1;
4135    while val(i)==0 & i>0
4136       i=i-1;
4137    end
4138    nbslice=str2num(Name(i+1:ind_mask-1));
4139    if ~isequal(nbslice,[]) & Name(i)=='_'
4140          flag_mask=1;
4141    else
4142          msgbox_uvmat('ERROR',['bad mask file ' Name ext ' found in ' Path2])
4143          return
4144          nbslice=1;
4145    end
4146end   
4147
4148%------------------------------------------------------------------------
4149% --- function called to look for grid files
4150function [nbslice, flag_mask]=get_grid(filebase,handles)
4151%------------------------------------------------------------------------
4152flag_mask=0;%default
4153nbslice=1;
4154[Path,Name]=fileparts(filebase);
4155currentdir=pwd;
4156cd(Path);%move in the dir of the root name filebase
4157maskfiles=dir([Name '_*grid_*.grid']);%look for mask files
4158cd(currentdir);%come back to the current working directory
4159if ~isempty(maskfiles)
4160    flag_mask=1;
4161    maskname=maskfiles(1).name;% take the first mask file in the list
4162    [Path2,Name,ext]=fileparts(maskname);
4163    Namedouble=double(Name);
4164    val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
4165    ind_mask=findstr('grid',Name);
4166    i=ind_mask-1;
4167    while val(i)==0 & i>0
4168       i=i-1;
4169    end
4170    nbslice=str2num(Name(i+1:ind_mask-1));
4171    if ~isequal(nbslice,[]) & Name(i)=='_'
4172          flag_mask=1;
4173    else
4174          msgbox_uvmat('ERROR',['bad grid file ' Name ext ' found in ' Path2])
4175          return
4176          nbslice=1;
4177    end
4178end
4179
4180%------------------------------------------------------------------------
4181% --- transform numbers to letters
4182function str=num2stra(num,nom_type);
4183%------------------------------------------------------------------------
4184if isequal(nom_type,'png_old') | isequal(nom_type,'netc_old') |isequal(nom_type,'raw_SMD')
4185    str=char(96+num);
4186elseif isequal(nom_type,'_i')|isequal(nom_type,'_i1-i2')...
4187        |isequal(nom_type,'ima_num')| isequal(nom_type,'avi')| isequal(nom_type,'none')
4188    str='';
4189else
4190    str=num2str(num);
4191end
4192
4193%------------------------------------------------------------------------
4194function mask_civ1_Callback(hObject, eventdata, handles)
4195%------------------------------------------------------------------------
4196set(handles.mask_civ1,'UserData',[])
4197set(handles.mask_civ1,'String','')
4198
4199%------------------------------------------------------------------------
4200function mask_civ2_Callback(hObject, eventdata, handles)
4201%------------------------------------------------------------------------
4202set(handles.mask_civ2,'UserData',[])
4203set(handles.mask_civ2,'String','')
4204
4205%------------------------------------------------------------------------
4206function mask_fix1_Callback(hObject, eventdata, handles)
4207%------------------------------------------------------------------------
4208set(handles.mask_fix1,'UserData',[])
4209set(handles.mask_fix1,'String','')
4210
4211%------------------------------------------------------------------------
4212function mask_fix2_Callback(hObject, eventdata, handles)
4213%------------------------------------------------------------------------
4214set(handles.mask_fix2,'UserData',[])
4215set(handles.mask_fix2,'String','')
4216
4217%------------------------------------------------------------------------
4218% --- Executes on button press in list_subdir_civ1.
4219function list_subdir_civ1_Callback(hObject, eventdata, handles)
4220%------------------------------------------------------------------------
4221list_subdir_civ1=get(handles.list_subdir_civ1,'String');
4222val=get(handles.list_subdir_civ1,'Value');
4223if val>1
4224    subdir=list_subdir_civ1{val};
4225    set(handles.subdir_civ1,'String',subdir);
4226    set(handles.list_subdir_civ1,'Value',1);
4227end
4228
4229%------------------------------------------------------------------------
4230% --- Executes on button press in list_subdir_civ2.
4231function list_subdir_civ2_Callback(hObject, eventdata, handles)
4232%------------------------------------------------------------------------
4233list_subdir_civ2=get(handles.list_subdir_civ2,'String');
4234val=get(handles.list_subdir_civ2,'Value');
4235if val>1
4236    subdir=list_subdir_civ2{val};
4237    set(handles.subdir_civ2,'String',subdir);
4238    set(handles.list_subdir_civ2,'Value',1);
4239end
4240
4241%------------------------------------------------------------------------
4242% --- Executes on button press in browse_gridciv1.
4243function browse_gridciv1_Callback(hObject, eventdata, handles)
4244%------------------------------------------------------------------------
4245value=get(handles.browse_gridciv1,'Value');
4246testgrid=0;
4247if value
4248        filebase=get(handles.RootName,'String');
4249    [nbslice, flag_grid]=get_grid(filebase,handles);
4250    if isequal(flag_grid,1)
4251       filegrid=[num2str(nbslice) 'grid'];
4252       testgrid=1;
4253    else   
4254        [FileName, PathName, filterindex] = uigetfile( ...
4255               {'*.grid', ' (*.grid)';
4256                '*.grid',  '.grid files '; ...
4257                '*.*', 'All Files (*.*)'}, ...
4258                'Pick a file',filebase);
4259        filegrid=fullfile(PathName,FileName);
4260        if ~(isempty(FileName)||isempty(PathName)||isequal(FileName,0)||~exist(filegrid,'file'))
4261            testgrid=1;
4262        end
4263    end       
4264end
4265if testgrid
4266        set(handles.browse_gridciv2,'Value',1)
4267        set(handles.get_gridpatch1,'Value',1)
4268        set(handles.get_gridpatch2,'Value',1)
4269        set(handles.dx_civ1,'Visible','off');
4270        set(handles.dy_civ1,'Visible','off');
4271        set(handles.dx_civ2,'Visible','off');
4272        set(handles.dy_civ2,'Visible','off');
4273        set(handles.grid_civ1,'String',filegrid)     
4274        set(handles.grid_patch1,'String',filegrid)
4275        set(handles.grid_civ2,'String',filegrid)
4276        set(handles.grid_patch2,'String',filegrid)
4277    else
4278        set(handles.browse_gridciv1,'Value',0);
4279        set(handles.browse_gridciv2,'Value',0);
4280        set(handles.get_gridpatch1,'Value',0)
4281        set(handles.get_gridpatch2,'Value',0)
4282        set(handles.dx_civ1,'Visible','on');
4283        set(handles.dy_civ1,'Visible','on');
4284        set(handles.dx_civ2,'Visible','on');
4285        set(handles.dy_civ2,'Visible','on');
4286        set(handles.grid_civ1,'String','')     
4287        set(handles.grid_patch1,'String','')
4288        set(handles.grid_civ2,'String','')
4289        set(handles.grid_patch2,'String','')
4290end
4291
4292%------------------------------------------------------------------------
4293% --- Executes on button press in browse_gridciv1.
4294function browse_gridciv2_Callback(hObject, eventdata, handles)
4295%------------------------------------------------------------------------
4296value=get(handles.browse_gridciv2,'Value');
4297if value
4298        filebase=get(handles.RootName,'String');
4299    [nbslice, flag_grid]=get_grid(filebase,handles);
4300    if isequal(flag_grid,1)
4301        mask_displ=[num2str(nbslice) 'grid'];
4302        set(handles.grid_civ2,'String',mask_displ)
4303        set(handles.dx_civ2,'Visible','off');
4304        set(handles.dy_civ2,'Visible','off');
4305    else   
4306        [FileName, PathName, filterindex] = uigetfile( ...
4307               {'*.grid', ' (*.grid)';
4308                '*.grid',  '.grid files '; ...
4309                '*.*', 'All Files (*.*)'}, ...
4310                'Pick a file',filebase);
4311        filegrid=fullfile(PathName,FileName);
4312        if isempty(FileName)|isempty(PathName)|isequal(FileName,0)|~exist(filegrid,'file')
4313            set(handles.browse_gridciv2,'Value',0);
4314            set(handles.grid_civ2,'string','');
4315            set(handles.dx_civ2,'Visible','on');
4316            set(handles.dy_civ2,'Visible','on');
4317            set(handles.grid_civ2,'string','');
4318        else
4319            set(handles.grid_civ2,'string',filegrid);
4320            set(handles.dx_civ2,'Visible','off');
4321            set(handles.dy_civ2,'Visible','off');
4322            set(handles.grid_civ2,'string',filegrid);
4323        end
4324    end
4325else
4326    set(handles.grid_civ2,'string','');
4327        set(handles.dx_civ2,'Visible','on');
4328        set(handles.dy_civ2,'Visible','on');
4329        set(handles.grid_civ2,'string','');
4330end
4331
4332% % --- Executes on button press in browse_gridciv2.
4333% function browse_gridciv2_Callback(hObject, eventdata, handles)
4334%
4335% filebase=get(handles.RootName,'String');
4336% [FileName, PathName, filterindex] = uigetfile( ...
4337%        {'*.grid', ' (*.grid)';
4338%         '*.grid',  '.grid files '; ...
4339%         '*.*', 'All Files (*.*)'}, ...
4340%         'Pick a file',filebase);
4341% filegrid=fullfile(PathName,FileName);
4342% set(handles.grid_civ2,'string',filegrid);
4343% set(handles.dx_civ2,'String',' ');
4344% set(handles.dy_civ2,'String',' ');
4345% % set(handles.grid_patch2,'string',filegrid);
4346
4347% --- Executes on button press in get_gridpatch1.
4348function get_gridpatch1_Callback(hObject, eventdata, handles)
4349% hObject    handle to get_gridpatch1 (see GCBO)
4350% eventdata  reserved - to be defined in a future version of MATLAB
4351% handles    structure with handles and user data (see GUIDATA)
4352
4353filebase=get(handles.RootName,'String');
4354[FileName, PathName, filterindex] = uigetfile( ...
4355       {'*.grid', ' (*.grid)';
4356        '*.grid',  '.grid files '; ...
4357        '*.*', 'All Files (*.*)'}, ...
4358        'Pick a file',filebase);
4359filegrid=fullfile(PathName,FileName);
4360set(handles.grid_patch1,'string',filegrid);
4361% set(handles.grid_patch2,'string',filegrid
4362
4363%------------------------------------------------------------------------
4364% --- Executes on button press in get_gridpatch2.
4365function get_gridpatch2_Callback(hObject, eventdata, handles)
4366%------------------------------------------------------------------------
4367
4368%------------------------------------------------------------------------
4369function enable_civ1(handles,state)
4370%------------------------------------------------------------------------
4371if isequal(state,0)
4372    state='off';
4373end
4374if isequal(state,1)
4375    state='on';
4376end
4377if isequal(state,'on')
4378    set(handles.frame_civ1,'BackgroundColor',[1 1 0])
4379    set(handles.frame_para_civ1,'BackgroundColor',[1 1 0])
4380    set(handles.frame_grid_civ1,'BackgroundColor',[1 1 0])
4381else
4382    set(handles.frame_civ1,'BackgroundColor',[0.831 0.816 0.784])
4383    set(handles.frame_para_civ1,'BackgroundColor',[0.831 0.816 0.784])
4384    set(handles.frame_grid_civ1,'BackgroundColor',[0.831 0.816 0.784])
4385end
4386set(handles.ibx,'Visible',state)
4387set(handles.iby,'Visible',state)
4388set(handles.isx,'Visible',state)
4389set(handles.isy,'Visible',state)
4390set(handles.shiftx,'Visible',state)
4391set(handles.shifty,'Visible',state)
4392set(handles.rho,'Visible',state)
4393set(handles.dx_civ1,'Visible',state)
4394set(handles.dy_civ1,'Visible',state)
4395set(handles.calcul_search,'Visible',state)
4396set(handles.u_text,'Visible',state)
4397set(handles.v_text,'Visible',state)
4398set(handles.min,'Visible',state)
4399set(handles.max,'Visible',state)
4400set(handles.umin,'Visible',state)
4401set(handles.umax,'Visible',state)
4402set(handles.vmin,'Visible',state)
4403set(handles.vmax,'Visible',state)
4404set(handles.grid_civ1,'Visible',state)
4405set(handles.mask_civ1,'Visible',state)
4406set(handles.browse_gridciv1,'Visible',state)
4407set(handles.get_mask_civ1,'Visible',state)
4408set(handles.parameters,'Visible',state)
4409set(handles.grid,'Visible',state)
4410set(handles.dx_civ1,'Visible',state)
4411set(handles.dy_civ1,'Visible',state)
4412set(handles.ImaThreshold,'Visible',state)
4413if isequal(state,'off')
4414    set(handles.MinIma,'Visible','off')
4415    set(handles.MaxIma,'Visible','off')
4416    set(handles.ImaThreshold,'Value',0)
4417end
4418set(handles.dx_civ1_title,'Visible',state)
4419set(handles.dy_civ1_title,'Visible',state)
4420set(handles.ImaThreshold_title,'Visible',state)
4421set(handles.ib_title,'Visible',state)
4422set(handles.is_title,'Visible',state)
4423set(handles.shift_title,'Visible',state)
4424set(handles.rho_title,'Visible',state)
4425
4426%------------------------------------------------------------------------
4427function enable_fix1(handles,state)
4428%------------------------------------------------------------------------
4429if isequal(state,0)
4430    state='off';
4431end
4432if isequal(state,1)
4433    state='on';
4434end
4435if isequal(state,'on')
4436    set(handles.frame_fix1,'BackgroundColor',[1 1 0])
4437else
4438    set(handles.frame_fix1,'BackgroundColor',[0.7 0.7 0.7])
4439end
4440set(handles.REMOVE,'Visible',state)
4441set(handles.vec_Fmin2,'Visible',state)
4442set(handles.vec_F2,'Visible',state)
4443set(handles.vec_F3,'Visible',state)
4444set(handles.thresh_vecC,'Visible',state)
4445set(handles.thresh_vecC_title,'Visible',state)
4446set(handles.thresh_vel,'Visible',state)
4447set(handles.thresh_vel_text,'Visible',state)
4448set(handles.mask_fix1,'Visible',state)
4449set(handles.get_mask_fix1,'Visible',state)
4450set(handles.get_ref_fix1,'Visible',state)
4451set(handles.ref_fix1,'Visible',state)
4452set(handles.inf_sup1,'Visible',state)
4453set(handles.field_ref1,'Visible',state)
4454
4455%------------------------------------------------------------------------
4456function enable_patch1(handles)
4457%------------------------------------------------------------------------
4458global patch_newBin
4459set(handles.frame_patch1,'BackgroundColor',[1 1 0])
4460set(handles.rho_patch1,'Visible','on')
4461set(handles.rho_text1,'Visible','on')
4462set(handles.thresh_patch1,'Visible','on')
4463set(handles.thresh_text1,'Visible','on')
4464set(handles.subdomain_patch1,'Visible','on')
4465set(handles.subdomain_text1,'Visible','on')
4466set(handles.nx_patch1,'Visible','on')
4467set(handles.ny_patch1,'Visible','on')
4468set(handles.nx_patch1_title,'Visible','on')
4469set(handles.ny_patch1_title,'Visible','on')
4470if (~isequal(patch_newBin,[]) & ~isequal(patch_newBin,[]))
4471    set(handles.test_interp,'Visible','on');
4472end
4473set(handles.get_gridpatch1,'Visible','on')
4474set(handles.grid_patch1,'string','none');
4475set(handles.grid_patch1,'Visible','on')
4476
4477%------------------------------------------------------------------------
4478function desable_patch1(handles)
4479%------------------------------------------------------------------------
4480set(handles.frame_patch1,'BackgroundColor',[0.831 0.816 0.784])
4481set(handles.rho_patch1,'Visible','off')
4482set(handles.rho_text1,'Visible','off')
4483set(handles.thresh_patch1,'Visible','off')
4484set(handles.thresh_text1,'Visible','off')
4485set(handles.subdomain_patch1,'Visible','off')
4486set(handles.subdomain_text1,'Visible','off')
4487set(handles.nx_patch1,'Visible','off')
4488set(handles.ny_patch1,'Visible','off')
4489set(handles.nx_patch1_title,'Visible','off')
4490set(handles.ny_patch1_title,'Visible','off')
4491set(handles.test_interp,'Visible','off')
4492set(handles.get_gridpatch1,'Visible','off')
4493set(handles.grid_patch1,'Visible','off')
4494
4495%------------------------------------------------------------------------
4496function enable_civ2(handles,state)
4497%------------------------------------------------------------------------
4498if isequal(state,0)
4499    state='off';
4500end
4501if isequal(state,1)
4502    state='on';
4503end
4504if isequal(state,'on')
4505    set(handles.frame_civ2,'BackgroundColor',[1 1 0])
4506    set(handles.frame_para_civ2,'BackgroundColor',[1 1 0])
4507    set(handles.frame_grid_civ2,'BackgroundColor',[1 1 0])
4508    set(handles.frame_subdirciv2,'BackgroundColor',[1 1 0])
4509else
4510    set(handles.frame_civ2,'BackgroundColor',[0.831 0.816 0.784])
4511    set(handles.frame_para_civ2,'BackgroundColor',[0.831 0.816 0.784])
4512    set(handles.frame_grid_civ2,'BackgroundColor',[0.831 0.816 0.784])
4513    set(handles.frame_subdirciv2,'BackgroundColor',[0.831 0.816 0.784])
4514end
4515set(handles.ibx_civ2,'Visible',state)
4516set(handles.iby_civ2,'Visible',state)
4517set(handles.decimal,'Visible',state)
4518set(handles.deformation,'Visible',state)
4519set(handles.rho_civ2,'Visible',state)
4520set(handles.dx_civ2,'Visible',state)
4521set(handles.dy_civ2,'Visible',state)
4522set(handles.browse_gridciv2,'Visible',state)
4523set(handles.get_mask_civ2,'Visible',state)
4524set(handles.parameters,'Visible',state)
4525set(handles.grid,'Visible',state)
4526set(handles.parameters_text,'Visible',state)
4527set(handles.grid_text,'Visible',state)
4528set(handles.grid_civ2,'Visible',state)
4529set(handles.mask_civ2,'Visible',state)
4530set(handles.dx_civ2_title,'Visible',state)
4531set(handles.dy_civ2_title,'Visible',state)
4532set(handles.ibx_civ2_text,'Visible',state)
4533set(handles.rho_civ2_title,'Visible',state)
4534set(handles.ImaThreshold2,'Visible',state)
4535set(handles.ImaThreshold_title2,'Visible',state)
4536if isequal(state,'off')
4537    set(handles.MinIma2,'Visible','off')
4538    set(handles.MaxIma2,'Visible','off')
4539    set(handles.ImaThreshold2,'Value',0)
4540    if isequal(get(handles.FIX2,'Value'),0) & isequal(get(handles.PATCH2,'Value'),0)
4541        set(handles.list_pair_civ2,'Visible','off')
4542        set(handles.subdir_civ2,'Visible','off')
4543        set(handles.subdir_civ2_text,'Visible','off')
4544        set(handles.dt_unit_civ2,'Visible','off')
4545        set(handles.ref_i_civ2,'Visible','off')
4546        set(handles.i_ref_civ2_title,'Visible','off')
4547        set(handles.j_ref_civ2_title,'Visible','off')
4548        set(handles.ref_j_civ2,'Visible','off')
4549    end
4550else
4551    set(handles.list_pair_civ2,'Visible','on')
4552    set(handles.subdir_civ2,'Visible','on')
4553    set(handles.subdir_civ2_text,'Visible','on')
4554    set(handles.dt_unit_civ2,'Visible','on')
4555    set(handles.ref_i_civ2,'Visible','on')
4556    set(handles.i_ref_civ2_title,'Visible','on')
4557    set(handles.j_ref_civ2_title,'Visible','on')
4558    set(handles.ref_j_civ2,'Visible','on')
4559end
4560set(handles.rho_civ2_title,'Visible',state)
4561
4562%------------------------------------------------------------------------
4563function enable_fix2(handles)
4564%------------------------------------------------------------------------
4565set(handles.frame_fix2,'BackgroundColor',[1 1 0])
4566set(handles.REMOVE2,'Visible','on')
4567set(handles.vec_Fmin2_2,'Visible','on')
4568set(handles.vec_F4,'Visible','on')
4569set(handles.vec_F3_2,'Visible','on')
4570set(handles.thresh_vec2C,'Visible','on')
4571set(handles.thresh_vec2C_text,'Visible','on')
4572set(handles.thresh_vel2,'Visible','on')
4573set(handles.thresh_vel2_text,'Visible','on')
4574set(handles.mask_fix2,'Visible','on')
4575set(handles.get_mask_fix2,'Visible','on')
4576set(handles.list_pair_civ2,'Visible','on')
4577set(handles.subdir_civ2,'Visible','on')
4578set(handles.subdir_civ2_text,'Visible','on')
4579set(handles.get_ref_fix2,'Visible','on')
4580set(handles.ref_fix2,'Visible','on')
4581set(handles.inf_sup2,'Visible','on')
4582set(handles.field_ref2,'Visible','on')
4583
4584%------------------------------------------------------------------------
4585function desable_fix2(handles)
4586%------------------------------------------------------------------------
4587set(handles.frame_fix2,'BackgroundColor',[0.831 0.816 0.784])
4588set(handles.REMOVE2,'Visible','off')
4589set(handles.vec_Fmin2_2,'Visible','off')
4590set(handles.vec_F4,'Visible','off')
4591set(handles.vec_F3_2,'Visible','off')
4592set(handles.thresh_vec2C,'Visible','off')
4593set(handles.thresh_vec2C_text,'Visible','off')
4594set(handles.thresh_vel2,'Visible','off')
4595set(handles.thresh_vel2_text,'Visible','off')
4596set(handles.mask_fix2,'Visible','off')
4597set(handles.get_mask_fix2,'Visible','off')
4598set(handles.get_ref_fix2,'Visible','off')
4599set(handles.ref_fix2,'Visible','off')
4600set(handles.inf_sup2,'Visible','off')
4601set(handles.field_ref2,'Visible','off')
4602if isequal(get(handles.CIV2,'Value'),0) & isequal(get(handles.PATCH2,'Value'),0)
4603    set(handles.list_pair_civ2,'Visible','off')
4604    set(handles.subdir_civ2,'Visible','off')
4605    set(handles.subdir_civ2_text,'Visible','off')
4606end
4607
4608%------------------------------------------------------------------------
4609function enable_patch2(handles)
4610%------------------------------------------------------------------------
4611set(handles.frame_patch2,'BackgroundColor',[1 1 0])
4612%set(handles.rho_patch2,'Visible','on')
4613set(handles.rho_text2,'Visible','on')
4614set(handles.thresh_patch2,'Visible','on')
4615set(handles.thresh_text2,'Visible','on')
4616set(handles.subdomain_patch2,'Visible','on')
4617set(handles.subdomain_text2,'Visible','on')
4618set(handles.nx_patch2,'Visible','on')
4619set(handles.ny_patch2,'Visible','on')
4620set(handles.nx_patch2_title,'Visible','on')
4621set(handles.ny_patch2_title,'Visible','on')
4622set(handles.get_gridpatch2,'Visible','on')
4623set(handles.grid_patch2,'Visible','on')
4624set(handles.list_pair_civ2,'Visible','on')
4625set(handles.subdir_civ2,'Visible','on')
4626set(handles.subdir_civ2_text,'Visible','on')
4627
4628%------------------------------------------------------------------------
4629function desable_patch2(handles)
4630%------------------------------------------------------------------------
4631set(handles.frame_patch2,'BackgroundColor',[0.831 0.816 0.784])
4632set(handles.rho_patch2,'Visible','off')
4633set(handles.rho_text2,'Visible','off')
4634set(handles.thresh_patch2,'Visible','off')
4635set(handles.thresh_text2,'Visible','off')
4636set(handles.subdomain_patch2,'Visible','off')
4637set(handles.subdomain_text2,'Visible','off')
4638set(handles.nx_patch2,'Visible','off')
4639set(handles.ny_patch2,'Visible','off')
4640set(handles.nx_patch2_title,'Visible','off')
4641set(handles.ny_patch2_title,'Visible','off')
4642set(handles.get_gridpatch2,'Visible','off')
4643set(handles.grid_patch2,'Visible','off')
4644if isequal(get(handles.CIV2,'Value'),0) & isequal(get(handles.FIX2,'Value'),0)
4645    set(handles.list_pair_civ2,'Visible','off')
4646    set(handles.subdir_civ2,'Visible','off')
4647    set(handles.subdir_civ2_text,'Visible','off')
4648end
4649
4650%------------------------------------------------------------------------
4651function enable_pair1(handles,state)
4652%------------------------------------------------------------------------
4653set(handles.subdir_civ1,'Visible',state)
4654set(handles.list_subdir_civ1,'Visible',state)
4655set(handles.SUBDIR_CIV1_txt,'Visible',state)
4656set(handles.frame_subdirciv1,'Visible',state)
4657set(handles.list_pair_civ1,'Visible',state)
4658set(handles.PAIR_txt,'Visible',state)
4659%set(handles.dt_unit,'Visible',state)
4660set(handles.PAIR_frame,'Visible',state)
4661
4662%------------------------------------------------------------------------
4663% --- Read the parameters for civ1 on the interface
4664function par=read_param_civ1(handles,file_ima)
4665%------------------------------------------------------------------------
4666ibx_val=str2num(get(handles.ibx,'String'));
4667par.ibx=num2str(ibx_val);
4668iby_val=str2num(get(handles.iby,'String'));
4669par.iby=num2str(iby_val);
4670isx=get(handles.isx,'String');
4671if isempty(str2num(isx)), isx='41'; set(handles.isx,'String','41'), end; %default
4672if str2num(isx)<ibx_val+8,isx=num2str(ibx_val+8); set(handles.isx,'String',num2str(ibx_val+8)); end
4673isy=get(handles.isy,'String');
4674if isempty(str2num(isy)), isy='41'; set(handles.isy,'String','41'), end;%default
4675if str2num(isy)<iby_val+8,isy=num2str(iby_val+8); set(handles.isy,'String',num2str(iby_val+8)); end
4676par.isx=get(handles.isx,'String');
4677par.isy=get(handles.isy,'String');
4678par.shiftx=get(handles.shiftx,'String');
4679par.shifty=get(handles.shifty,'String');
4680if isempty(str2num(par.isx))
4681         par.isx='41';%default
4682         set(handles.isx,'String','41');
4683end
4684if isempty(str2num(par.isy))
4685         par.isy='41'; %default
4686         set(handles.isy,'String','41');
4687end
4688if isempty(str2num(par.shiftx))
4689         par.shiftx='0';%default
4690         set(handles.shiftx,'String','0');
4691end
4692if isempty(str2num(par.shifty))
4693         par.shifty='0'; %default
4694         set(handles.shifty,'String','0');
4695end
4696 par.rho=get(handles.rho,'String');
4697 par.dx=get(handles.dx_civ1,'String');
4698 par.dy=get(handles.dy_civ1,'String');
4699 if isequal(str2num(par.dx),[])
4700     if isempty(get(handles.grid_civ1,'String'));
4701         par.dx='0'; %just read by civ program, not used
4702     else
4703        par.dx='20';%default
4704        set(handles.dx_civ1,'String','20');
4705     end
4706 end
4707 if isequal(str2num(par.dy),[])
4708     if isempty(get(handles.grid_civ1,'String'));
4709         par.dy='0';%just read by civ program, not used
4710     else
4711        par.dy='20';%default
4712        set(handles.dy_civ1_title,'String','20');
4713     end
4714 end
4715    par.pxcmx='1'; %velocities are expressed in pixel dispalcement
4716     par.pxcmy='1';
4717%      end
4718 A=imread(file_ima);%read the first image to get the size
4719 sizim=size(A);
4720 par.npx=num2str(sizim(2));
4721 par.npy=num2str(sizim(1));
4722 time=get(handles.RootName,'UserData'); %get the set of times
4723 par.gridname=get(handles.grid_civ1,'String');
4724 par.gridflag='y';
4725 if isequal(par.gridname,'')| isempty(par.gridname)
4726     par.gridname='nogrid';
4727     par.gridflag='n';
4728 end
4729
4730%------------------------------------------------------------------------
4731function par=read_param_civ2(handles,file_ima)
4732%------------------------------------------------------------------------
4733par.ibx=get(handles.ibx_civ2,'String');
4734par.iby=get(handles.iby_civ2,'String');
4735par.rho=get(handles.rho_civ2,'String');
4736par.decimal=int2str(get(handles.decimal,'Value'));
4737par.deformation=int2str(get(handles.deformation,'Value'));
4738par.dx=get(handles.dx_civ2,'String');
4739par.dy=get(handles.dy_civ2,'String');
4740if isequal(str2num(par.dx),[])
4741     if isempty(get(handles.grid_civ2,'String'));
4742         par.dx='0'; %just read by civ program, not used
4743     else
4744        par.dx='20';%default
4745        set(handles.dx_civ2,'String','20');
4746     end
4747 end
4748 if isequal(str2num(par.dy),[])
4749     if isempty(get(handles.grid_civ2,'String'));
4750         par.dy='0';%just read by civ program, not used
4751     else
4752        par.dy='20';%default
4753        set(handles.dy_civ2,'String','20');
4754     end
4755 end
4756 par.pxcmx='1';
4757 par.pxcmy='1';
4758A=imread(file_ima);%read the first image to get the size
4759sizim=size(A);
4760par.npx=num2str(sizim(2));
4761par.npy=num2str(sizim(1));
4762time=get(handles.RootName,'UserData'); %get the set of times
4763par.gridname=get(handles.grid_civ2,'String');
4764par.gridflag='y';
4765if isequal(par.gridname,'')| isempty(par.gridname)
4766    par.gridname='nogrid';
4767    par.gridflag='n';
4768end
4769
4770%------------------------------------------------------------------------
4771% --- CIV1  CIV1  CIV1 CIV1
4772function cmd_CIV1=BATCH_CIV1(filename,namelog,par,handles,sparam)
4773%------------------------------------------------------------------------
4774%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4775
4776%changes : filename_cmx -> filename ( no extension )
4777
4778            if isequal(par.Dt,'0')
4779                par.Dt='1' ;%case of 'displacement' mode
4780            end         
4781
4782%     textcmx={'##############   CMX file';...
4783%     ['FirstImage ' par.filename_ima_a];...
4784%     ['LastImage  ' par.filename_ima_b];...
4785%     'XX' ;...
4786%     ['Mask ' par.maskflag] ;...
4787%     ['MaskName ' par.maskname];...
4788%     ['ImageSize ' par.npx ' ' par.npy];...   %VERIFIER CAS GENERAL ?
4789%     ['CorrelationBoxesSize ' par.ibx ' ' par.iby];...
4790%     ['SearchBoxeSize ' par.isx ' ' par.isy];...
4791%     ['RO ' par.rho];...
4792%     ['GridSpacing ' par.dx ' ' par.dy];...
4793%     'XX 1.0';...
4794%     ['Dt_TO ' par.Dt ' ' par.T0];...
4795%     ['PixCmXY ' par.pxcmx ' ' par.pxcmy];...
4796%     'XX 1';...
4797%     ['ShiftXY ' par.shiftx ' '  par.shifty];...
4798%     ['Grid ' par.gridflag];...
4799%     ['GridName ' par.gridname] ;...
4800%     'XX 85';...
4801%     'XX 1.0';...
4802%     'XX 1.0';...
4803%     'Hart 1';...
4804%     'DecimalShift 0';...
4805%     'Deformation 0';...
4806%     'CorrelationMin 0';...
4807%     'IntensityMin 0';...
4808%     'SeuilImage n';...
4809%     'SeuilImageValues 0 4096';...
4810%     ['ImageToUse ' par.term_a ' ' par.term_b];... % VERIFIER ?
4811%     'ImageUsedBefore null null'};
4812%
4813%             textout=char(textcmx);
4814
4815   fid=fopen([filename '.cmx'],'w');
4816fprintf(fid,['##############   CMX file' '\n' ]);
4817 fprintf(fid,   ['FirstImage ' par.filename_ima_a '\n' ]);
4818 fprintf(fid,   ['LastImage  ' par.filename_ima_b '\n' ]);
4819  fprintf(fid,  ['XX' '\n' ]);
4820  fprintf(fid,  ['Mask ' par.maskflag '\n' ]);
4821  fprintf(fid,  ['MaskName ' par.maskname '\n' ]);
4822 fprintf(fid,   ['ImageSize ' par.npx ' ' par.npy '\n' ]);   %VERIFIER CAS GENERAL ?
4823 fprintf(fid,   ['CorrelationBoxesSize ' par.ibx ' ' par.iby '\n' ]);
4824 fprintf(fid,   ['SearchBoxeSize ' par.isx ' ' par.isy '\n' ]);
4825 fprintf(fid,   ['RO ' par.rho '\n' ]);
4826 fprintf(fid,   ['GridSpacing ' par.dx ' ' par.dy '\n' ]);
4827 fprintf(fid,   ['XX 1.0' '\n' ]);
4828 fprintf(fid,   ['Dt_TO ' par.Dt ' ' par.T0 '\n' ]);
4829  fprintf(fid,  ['PixCmXY ' par.pxcmx ' ' par.pxcmy '\n' ]);
4830  fprintf(fid,  ['XX 1' '\n' ]);
4831 fprintf(fid,   ['ShiftXY ' par.shiftx ' '  par.shifty '\n' ]);
4832  fprintf(fid,  ['Grid ' par.gridflag '\n' ]);
4833 fprintf(fid,   ['GridName ' par.gridname '\n' ]);
4834 fprintf(fid,   ['XX 85' '\n' ]);
4835 fprintf(fid,   ['XX 1.0' '\n' ]);
4836 fprintf(fid,   ['XX 1.0' '\n' ]);
4837 fprintf(fid,   ['Hart 1' '\n' ]);
4838 fprintf(fid,  [ 'DecimalShift 0' '\n' ]);
4839 fprintf(fid,   ['Deformation 0' '\n' ]);
4840  fprintf(fid,  ['CorrelationMin 0' '\n' ]);
4841 fprintf(fid,   ['IntensityMin 0' '\n' ]);
4842  fprintf(fid,  ['SeuilImage n' '\n' ]);
4843 fprintf(fid,   ['SeuilImageValues 0 4096' '\n' ]);
4844 fprintf(fid,   ['ImageToUse ' par.term_a ' ' par.term_b '\n' ]); % VERIFIER ?
4845 fprintf(fid,   ['ImageUsedBefore null null' '\n' ]);
4846fclose(fid);
4847 
4848cmd_CIV1=[sparam.Civ1Bin ' -f ' filename '.cmx' ]; % redirect standard output to the log file
4849if(isunix)
4850    [Rootbat,Filebat,extbat]=fileparts(namelog);
4851    ncName=fullfile(Rootbat,[ Filebat '.nc']);
4852    cmd_CIV1=[cmd_CIV1 '\n' 'mv ' namelog  ' ' regexprep(namelog,'\.log','') '.civ1.log' '\n' 'chmod g+w ' ncName];
4853else
4854    cmd_CIV1=[cmd_CIV1 '\n' 'copy /Y ' namelog ' ' regexprep(namelog,'\.log','') '.civ1.log'];
4855end
4856
4857%------------------------------------------------------------------------
4858% --- CIV1  Unified
4859function xml_civ1_parameters=BATCH_CIV1_Unified(filename,namelog,par)
4860%------------------------------------------------------------------------
4861%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4862%global CivBin%name of the executable for civ1 calculation
4863
4864    civ1.image1=par.filename_ima_a;
4865    civ1.image2=par.filename_ima_b;
4866    civ1.imageSize_X=par.npx;
4867    civ1.imageSize_Y=par.npy;
4868    civ1.outputFileName=[filename '.nc'];
4869    civ1.correlationBoxesSize_X=par.ibx;
4870    civ1.correlationBoxesSize_Y=par.iby;
4871    civ1.searchBoxesSize_X=par.isx;
4872    civ1.searchBoxesSize_Y=par.isy;
4873    civ1.globalShift_X=par.shiftx;
4874    civ1.globalShift_Y=par.shifty;
4875    civ1.ro=par.rho;
4876    civ1.hart='y';
4877    if isequal(par.gridflag,'y')
4878        civ1.grid=par.gridname;
4879    else
4880        civ1.grid='n';
4881        civ1.gridSpacing_X=par.dx;
4882        civ1.gridSpacing_Y=par.dy;
4883    end
4884    if isequal(par.maskflag,'y')
4885        civ1.mask=par.maskname;
4886    end
4887    civ1.dt=par.Dt;
4888    civ1.unit='pixel';
4889    civ1.absolut_time_T0=par.T0;
4890    civ1.pixcmx=par.pxcmx;
4891    civ1.pixcmy=par.pxcmy;
4892    civ1.convectFlow='n';
4893 
4894    xml_civ1_parameters=civ1;
4895
4896%------------------------------------------------------------------------
4897% --- CIV2  Unified
4898function civ2=BATCH_CIV2_Unified(filename,namelog,par)
4899%------------------------------------------------------------------------
4900%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4901%global CivBin%name of the executable for civ1 calculation
4902
4903civ2.image1=par.filename_ima_a;
4904civ2.image2=par.filename_ima_b;
4905civ2.imageSize_X=par.npx;
4906civ2.imageSize_Y=par.npy;
4907civ2.inputFileName=[par.filename_nc1 '.nc'];
4908civ2.outputFileName=[filename '.nc'];
4909civ2.correlationBoxesSize_X=par.ibx;
4910civ2.correlationBoxesSize_Y=par.iby;
4911civ2.ro=par.rho;
4912%civ2.decimalShift=par.decimal;
4913%civ2.deformation=par.deformation;
4914if isequal(par.decimal,'1')
4915    civ2.decimalShift='y';
4916else
4917    civ2.decimalShift='n';
4918end
4919if isequal(par.deformation,'1')
4920    civ2.deformation='y';
4921else
4922    civ2.deformation='n';
4923end
4924if isequal(par.gridflag,'y')
4925    civ2.grid=par.gridname;
4926else
4927    civ2.grid='n';
4928    civ2.gridSpacing_X=par.dx;
4929    civ2.gridSpacing_Y=par.dy;
4930end
4931civ2.gridSpacing_X='10';
4932civ2.gridSpacing_Y='10';%NOTE: faut mettre gridSpacing pourque ca tourne, meme si c'est la grille qui est utilisee
4933if isequal(par.maskflag,'y')
4934    civ2.mask=par.maskname;
4935else
4936    civ2.mask='n';
4937end
4938civ2.dt=par.Dt;
4939civ2.unit='pixel';
4940civ2.absolut_time_T0=par.T0;
4941civ2.pixcmx=par.pxcmx;
4942civ2.pixcmy=par.pxcmy;
4943civ2.convectFlow='n';
4944civ2.pixcmx=par.pxcmx;
4945civ2.pixcmy=par.pxcmy;
4946civ2.convectFlow='n';
4947
4948%------------------------------------------------------------------------
4949% --- CIV2  CIV2  CIV2 CIV2
4950function cmd_CIV2=BATCH_CIV2(filename_cmx,namelog,par,sparam)
4951%------------------------------------------------------------------------
4952%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4953% global civ2Bin sge%name of the executable for civ1 calculation
4954   if isequal(par.Dt,'0')
4955                par.Dt='1' ;%case of 'displacement' mode
4956  end
4957textcmx=['##############   CMX file'  '\n'...
4958['FirstImage ' par.filename_ima_a]  '\n'...
4959['LastImage  ' par.filename_ima_b]  '\n'...
4960'XX'   '\n'...
4961['Mask ' par.maskflag]  '\n'...
4962['MaskName ' par.maskname]  '\n'...
4963['ImageSize ' par.npx ' ' par.npy]  '\n'...   
4964['CorrelationBoxesSize ' par.ibx ' ' par.iby]  '\n'...
4965['SearchBoxeSize ' par.ibx ' ' par.iby]  '\n'...
4966['RO ' par.rho]  '\n'...
4967['GridSpacing ' par.dx ' ' par.dy]  '\n'...
4968'XX 1.0'  '\n'...
4969['Dt_TO ' par.Dt ' ' par.T0]  '\n'...
4970['PixCmXY ' par.pxcmx ' ' par.pxcmy]  '\n'...
4971'XX 1'  '\n'...
4972['ShiftXY 0 0']  '\n'...
4973['Grid ' par.gridflag]  '\n'...
4974['GridName ' par.gridname]  '\n'...
4975'XX 85'  '\n'...
4976'XX 1.0'  '\n'...
4977'XX 1.0'  '\n'...
4978'Hart 1'  '\n'...
4979['DecimalShift ' par.decimal]  '\n'...
4980['Deformation ' par.deformation]  '\n'...
4981'CorrelationMin 0'  '\n'...
4982'IntensityMin 0'  '\n'...
4983'SeuilImage n'  '\n'...
4984'SeuilImageValues 0 4096'  '\n'...
4985['ImageToUse ' par.term_a ' ' par.term_b]  '\n'... % VERIFIER ?
4986['ImageUsedBefore ' par.filename_nc1]];
4987textout=char(textcmx);
4988fid=fopen([filename_cmx '2'],'w');
4989fprintf(fid,textout);
4990fclose(fid)
4991% if sge
4992    cmd_CIV2=[sparam.Civ2Bin ' -f ' filename_cmx ]; % redirect standard output to the log file
4993% else
4994%     cmd_CIV2=[civ2Bin ' -f ' filename_cmx ' > ' namelog ' 2>&1']; % redirect standard output to the log file
4995% end
4996
4997%------------------------------------------------------------------------
4998% --- Executes on button press in HELP.
4999function HELP_Callback(hObject, eventdata, handles)
5000%------------------------------------------------------------------------
5001path_to_uvmat=which ('uvmat');% check the path of uvmat
5002pathelp=fileparts(path_to_uvmat);
5003helpfile=fullfile(pathelp,'uvmat_doc','uvmat_doc.html')
5004if isempty(dir(helpfile)), msgbox_uvmat('ERROR','Please put the help file uvmat_doc.html in the sub-directory /uvmat_doc of the UVMAT package')
5005else
5006    addpath (fullfile(pathelp,'uvmat_doc'))
5007    web([helpfile '#civ'])   
5008end
5009
5010%------------------------------------------------------------------------
5011%--read images and convert them to the uint16 format used for PIV
5012function A=read_image(filename,type_ima,num,movieobject)
5013%------------------------------------------------------------------------
5014%num is the view number needed for an avi movie
5015switch type_ima
5016    case 'movie'
5017        A=read(movieobject,num);
5018    case 'avi'
5019       mov=aviread(filename,num);     
5020       A=frame2im(mov(1));
5021    case 'multimage'
5022        A=imread(filename,num);
5023    case 'image'   
5024        A=imread(filename);
5025end
5026siz=size(A);
5027if length(siz)==3;%color images
5028    A=sum(double(A),3);
5029    A=uint16(A);
5030end
5031
5032%------------------------------------------------------------------------
5033function ref_i_Callback(hObject, eventdata, handles)
5034%------------------------------------------------------------------------
5035mode_list=get(handles.mode,'String');
5036mode_value=get(handles.mode,'Value');
5037mode=mode_list{mode_value};
5038find_netcpair_civ1(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
5039if isequal(mode,'series(Di)') || ...% we do patch2 only
5040   (get(handles.CIV2,'Value')==0 && get(handles.CIV1,'Value')==0 && get(handles.FIX1,'Value')==0 && get(handles.PATCH1,'Value')==0)
5041    find_netcpair_civ2(hObject, eventdata, handles);
5042end
5043
5044%------------------------------------------------------------------------
5045function ref_j_Callback(hObject, eventdata, handles)
5046%------------------------------------------------------------------------
5047mode_list=get(handles.mode,'String');
5048mode_value=get(handles.mode,'Value');
5049mode=mode_list{mode_value};
5050if isequal(get(handles.CIV1,'Value'),0)|| isequal(mode,'series(Dj)')
5051    find_netcpair_civ1(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
5052end
5053if isequal(mode,'series(Dj)') || ...
5054   (get(handles.CIV2,'Value')==0 && get(handles.CIV1,'Value')==0 && get(handles.FIX1,'Value')==0 && get(handles.PATCH1,'Value')==0)
5055    find_netcpair_civ2(hObject, eventdata, handles);
5056end
5057
5058%------------------------------------------------------------------------
5059function ref_i_civ2_Callback(hObject, eventdata, handles)
5060%------------------------------------------------------------------------
5061mode_list=get(handles.mode,'String');
5062mode_value=get(handles.mode,'Value');
5063mode=mode_list{mode_value};
5064find_netcpair_civ2(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
5065
5066%------------------------------------------------------------------------
5067function ref_j_civ2_Callback(hObject, eventdata, handles)
5068%------------------------------------------------------------------------
5069mode_list=get(handles.mode,'String');
5070mode_value=get(handles.mode,'Value');
5071mode=mode_list{mode_value};
5072if  isequal(mode,'series(Dj)')
5073    find_netcpair_civ2(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
5074end
5075
5076%------------------------------------------------------------------------
5077% --- Executes on button press in compare.
5078function compare_Callback(hObject, eventdata, handles)
5079%------------------------------------------------------------------------
5080test=get(handles.compare,'Value');
5081if test==2 || test==3   
5082    filebase=get(handles.RootName,'String');
5083    browse=get(handles.browse_root,'Userdata');
5084    browse.nom_type_ima1=browse.nom_type_ima;
5085    set(handles.browse_root,'UserData',browse);
5086    set(handles.sub_txt,'Visible','on')
5087    set(handles.RootName_1,'Visible','On');%mkes the second file input window visible
5088    mode_store=get(handles.mode,'String');%get the present 'mode'
5089    set(handles.compare,'UserData',mode_store);%store the mode display 
5090    set(handles.mode,'Visible','off')
5091    if test==2
5092        set(handles.mode,'Visible','off')       
5093    else
5094        set(handles.mode,'Visible','on')
5095    end
5096   
5097    % open an image file with the browser
5098    ind_opening=1;%default
5099    browse.incr_pair=[0 0]; %default
5100    oldfile=get(handles.RootName,'String');
5101    menu={'*.xml;*.avi;*.AVI;*.nc','(*.xml,*.avi,*.nc)'; ...
5102            '*.xml', '.xml files';'*.avi;*.AVI', '.avi files';'*.nc', '.nc files';...
5103            '*.*', 'All Files (*.*)'};
5104    [FileName, PathName, filtindex] = uigetfile( menu, 'Pick a file',oldfile);
5105    fileinput=[PathName FileName];%complete file name
5106    sizf=size(fileinput);
5107    if (~ischar(fileinput)|~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
5108    [path,name,ext]=fileparts(fileinput);
5109    [path1]=fileparts(filebase);
5110    if ~strcmp(path1,path)
5111         msgbox_uvmat('ERROR','The two  input image series must be in the same directory')
5112        return
5113    end
5114    set(handles.RootName_1,'String',name);
5115    [RootPath,RootFile,field_count,str2,str_a,str_b,xx,nom_type,subdir]=name2display(name);
5116    browse=get(handles.browse_root,'UserData');
5117    browse.nom_type_ima_1=nom_type;
5118    set(handles.browse_root,'UserData',browse) 
5119   
5120    %check image extension
5121    if ~strcmp(ext,get(handles.ImaExt,'String'))
5122        msgbox_uvmat('ERROR','The two  input image series must have the same extenion name')
5123        return
5124    end
5125   
5126    %check image size
5127    A=imread(fileinput);
5128    npxy=get(handles.ImaExt,'UserData');
5129    if ~isequal(npxy(1),size(A,1))|| ~isequal(npxy(2),size(A,2))
5130        msgbox_uvmat('ERROR','The two input image series must have the same size')
5131        return
5132    end
5133else
5134    set(handles.mode,'Visible','on')
5135    set(handles.RootName_1,'Visible','Off');
5136    set(handles.sub_txt,'Visible','off')
5137    set(handles.RootName_1,'String',[]);
5138    mode_store=get(handles.compare,'UserData');
5139    set(handles.mode,'String',mode_store)
5140    set(handles.test_stereo1,'Value',0)
5141    set(handles.test_stereo2,'Value',0)
5142end
5143mode_Callback(hObject, eventdata, handles)
5144
5145%------------------------------------------------------------------------
5146% --- Executes on button press in get_ref_fix1.
5147function get_ref_fix1_Callback(hObject, eventdata, handles)
5148%------------------------------------------------------------------------
5149filebase=get(handles.RootName,'String');
5150[FileName, PathName, filterindex] = uigetfile( ...
5151       {'*.nc', ' (*.nc)';
5152        '*.nc',  'netcdf files '; ...
5153        '*.*', 'All Files (*.*)'}, ...
5154        'Pick a file',filebase);
5155   
5156fileinput=[PathName FileName];
5157sizf=size(fileinput);
5158if (~ischar(fileinput)|~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
5159[Path,File,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
5160ref.filebase=fullfile(Path,File);
5161ref.num_a=stra2num(str_a);
5162ref.num_b=stra2num(str_b);
5163ref.num1=str2num(field_count);
5164ref.num2=str2num(str2);
5165browse=[];%initialisation
5166if ~isequal(ref.ext,'.nc')
5167    msgbox_uvmat('ERROR','the reference file must be in netcdf format (*.nc)')
5168    return
5169end
5170set(handles.ref_fix1,'String',[fullfile(ref.subdir,File) '....nc']);
5171set(handles.ref_fix1,'UserData',ref)
5172menu_field{1}='civ1';
5173Data=nc2struct(fileinput,[]);
5174if isfield(Data,'patch') && isequal(Data.patch,1)
5175    menu_field{2}='filter1';
5176end
5177if isfield(Data,'civ2') && isequal(Data.civ2,1)
5178    menu_field{3}='civ2';
5179end
5180if isfield(Data,'patch2') && isequal(Data.patch2,1)
5181    menu_field{4}='filter2';
5182end
5183set(handles.field_ref1,'String',menu_field);
5184set(handles.field_ref1,'Value',length(menu_field));
5185set(handles.inf_sup1,'Value',2);
5186set(handles.thresh_vel,'String','1');%default threshold
5187set(handles.ref_fix1,'Enable','on')
5188
5189%------------------------------------------------------------------------
5190% --- Executes on button press in get_ref_fix2.
5191function get_ref_fix2_Callback(hObject, eventdata, handles)
5192%------------------------------------------------------------------------
5193if isequal(get(handles.get_ref_fix2,'Value'),1)
5194    filebase=get(handles.RootName,'String');
5195    [FileName, PathName, filterindex] = uigetfile( ...
5196           {'*.nc', ' (*.nc)';
5197            '*.nc',  'netcdf files '; ...
5198            '*.*', 'All Files (*.*)'}, ...
5199            'Pick a file',filebase);
5200    fileinput=[PathName FileName];
5201    sizf=size(fileinput);
5202    if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
5203    [Path,File,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
5204    ref.filebase=fullfile(Path,File);
5205    ref.num_a=stra2num(str_a);
5206    ref.num_b=stra2num(str_b);
5207    ref.num1=str2num(field_count);
5208    ref.num2=str2num(str2);
5209    browse=[];%initialisation
5210    if ~isequal(ref.ext,'.nc')
5211        msgbox_uvmat('ERROR','the reference file must be in netcdf format (*.nc)')
5212        return
5213    end
5214    set(handles.ref_fix2,'String',[fullfile(ref.subdir,File) '....nc']);
5215    set(handles.ref_fix2,'UserData',ref)   
5216    menu_field{1}='civ1';
5217    Data=nc2struct(fileinput,[]);
5218    if isfield(Data,'patch') & isequal(Data.patch,1)
5219        menu_field{2}='filter1';
5220    end
5221    if isfield(Data,'civ2') & isequal(Data.civ2,1)
5222        menu_field{3}='civ2';
5223    end
5224    if isfield(Data,'patch2') & isequal(Data.patch2,1)
5225        menu_field{4}='filter2';
5226    end
5227    set(handles.field_ref2,'String',menu_field);
5228    set(handles.field_ref2,'Value',length(menu_field));
5229    set(handles.inf_sup2,'Value',2);
5230    set(handles.thresh_vel2,'String','1');%default threshold
5231    set(handles.ref_fix2,'Enable','on')
5232    set(handles.ref_fix2,'Visible','on')
5233    set(handles.field_ref2,'Visible','on')
5234else
5235    set(handles.ref_fix2,'Visible','off')
5236    set(handles.field_ref2,'Visible','off')
5237end
5238
5239%------------------------------------------------------------------------
5240function ref_fix1_Callback(hObject, eventdata, handles)
5241%------------------------------------------------------------------------
5242set(handles.inf_sup1,'Value',1);
5243set(handles.field_ref1,'Value',1)
5244set(handles.field_ref1,'String',{' '})
5245set(handles.ref_fix1,'UserData',[]);
5246set(handles.ref_fix1,'String','');
5247set(handles.thresh_vel1,'String','0');
5248 
5249%------------------------------------------------------------------------
5250function ref_fix2_Callback(hObject, eventdata, handles)
5251%------------------------------------------------------------------------
5252set(handles.inf_sup2,'Value',1);
5253set(handles.field_ref2,'Value',1)
5254set(handles.field_ref2,'String',{' '})
5255set(handles.ref_fix2,'UserData',[]);
5256set(handles.ref_fix2,'String','');
5257set(handles.thresh_vel2,'String','0');
5258
5259%------------------------------------------------------------------------
5260% transform letters to numbers
5261function numres=stra2num(str)
5262%------------------------------------------------------------------------
5263numres=double(str)-96;
5264if double(str) >= 48 & double(str) <= 57 % = 1 for numbers
5265    numres=str2num(str);
5266end
5267
5268%------------------------------------------------------------------------
5269% --- Executes on button press in test_stereo1.
5270function test_stereo1_Callback(hObject, eventdata, handles)
5271%------------------------------------------------------------------------
5272if isequal(get(handles.test_stereo1,'Value'),0)
5273    set(handles.subdomain_patch1,'Visible','on')
5274    set(handles.rho_patch1,'Visible','on')
5275else
5276    set(handles.subdomain_patch1,'Visible','off')
5277    set(handles.rho_patch1,'Visible','off')
5278end
5279
5280%------------------------------------------------------------------------
5281% --- Executes on button press in test_stereo2.
5282function test_stereo2_Callback(hObject, eventdata, handles)
5283%------------------------------------------------------------------------
5284if isequal(get(handles.test_stereo2,'Value'),0)
5285    set(handles.subdomain_patch2,'Visible','on')
5286    set(handles.rho_patch2,'Visible','on')
5287else
5288    set(handles.subdomain_patch2,'Visible','off')
5289    set(handles.rho_patch2,'Visible','off')
5290end
5291
5292%------------------------------------------------------------------------
5293% --- Executes on button press in ImaThreshold.
5294function ImaThreshold_Callback(hObject, eventdata, handles)
5295%------------------------------------------------------------------------
5296if isequal(get(handles.ImaThreshold,'Value'),1)
5297    set(handles.MinIma,'Visible','on')
5298    set(handles.MaxIma,'Visible','on')
5299else
5300    set(handles.MinIma,'Visible','off')
5301    set(handles.MaxIma,'Visible','off')
5302end
5303
5304%------------------------------------------------------------------------
5305% --- Executes on button press in ImaThreshold2.
5306function ImaThreshold2_Callback(hObject, eventdata, handles)
5307%------------------------------------------------------------------------
5308if isequal(get(handles.ImaThreshold2,'Value'),1)
5309    set(handles.MinIma2,'Visible','on')
5310    set(handles.MaxIma2,'Visible','on')
5311else
5312    set(handles.MinIma2,'Visible','off')
5313    set(handles.MaxIma2,'Visible','off')
5314end
5315
5316
5317
5318
Note: See TracBrowser for help on using the repository browser.