source: trunk/src/civ.m @ 94

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

civ.m corrected to work for windows system

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