source: trunk/src/civ.m @ 54

Last change on this file since 54 was 54, checked in by sommeria, 11 years ago

sub_background modified to accept various movie and image inputs
geometry_calib, editxml, civ modified to read a unique PARAM.xml parameter file (instead of PARAM_LINUX...)

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