source: trunk/src/civ.m @ 46

Last change on this file since 46 was 45, checked in by gostiaux, 14 years ago

adaptation for movies

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