source: trunk/src/civ.m @ 67

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

civ: RUN civ lounched out of the Matlab work space. RUN and BATCH now runned by a unique sub-function lounch.m.
FiLE PARAM.xml modified to provide different paths for Batch and Run !!!!!
RUN_FIX: minor error message modif
geometry_calib: calib point editing by the mouse
improvement of the interactions between the different GUIs. Close function...

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