source: trunk/src/civ.m @ 90

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