source: trunk/src/civ.m @ 89

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

many bug corrections and cleaning. Activation of the BW option in uvmat. Improvement of the interaction of get_field with uvmat.

File size: 210.0 KB
Line 
1%'civ': function associated with the interface 'civ.fig' for PIV, spline interpolation and stereo PIV (patch)   
2%------------------------------------------------------------------------
3%  provides an interface for the software CIVx
4% function varargout = civ(varargin)
5% provides an interface for the software CIVx
6%
7%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
8%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
9%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
10%     This file is part of the toolbox UVMAT.
11%
12%     UVMAT is free software; you can redistribute it and/or modify
13%     it under the terms of the GNU General Public License as published by
14%     the Free Software Foundation; either version 2 of the License, or
15%     (at your option) any later version.
16%
17%     UVMAT is distributed in the hope that it will be useful,
18%     but WITHOUT ANY WARRANTY; without even the implied warranty of
19%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
21%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
22function varargout = civ(varargin)
23
24% Last Modified by GUIDE v2.5 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';
2332           cmd=[cmd '\n' '#$ -cwd'];
2333           cmd=[cmd '\n' 'hostname && date'];
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 '\n' BATCH_CIV1(filename_cmx(1:end-4),namelog,par_civ1,handles,sparam)];
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 '\n' cmd_FIX];
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 '\n' cmd_PATCH];
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 '\n' 'cp -f ' filename_cmx '2 ' filename_cmx '\n' cmd_CIV2];
2571                else
2572                    cmd=[cmd '\n' 'copy /Y ' filename_cmx '2 ' filename_cmx '\n' cmd_CIV2];
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 '\n' cmd_FIX];
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 '\n' cmd_PATCH];
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 '\n' CivBin ' -f ' filename_cmx(1:end-4) '.xml '  civAllCmd];
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         
2688        fprintf(fid,cmd);
2689        fclose(fid);
2690        %dlmwrite(filename_bat,cmd,'');%write commands in filename_bat
2691        if batch
2692            switch batch_mode
2693                case 'sge'
2694                    pvalue=num2str((1-ind_answer)*500);
2695                    namelog=[filename_bat '.patch.log'];
2696                    display(['!qsub -p ' pvalue ' -q civ.q -e ' filename_cmx(1:end-4) '.errors -o ' filename_cmx(1:end-4) '.log' ' ' filename_bat]);
2697                    eval(  ['!qsub -p ' pvalue ' -q civ.q -e ' filename_cmx(1:end-4) '.errors -o ' filename_cmx(1:end-4) '.log' ' ' filename_bat]);
2698            end
2699        else
2700%% to lauch the jobs locally :
2701            if(isunix)
2702              eval(['!. ' filename_bat ' &']);
2703              display(['!. ' filename_bat ' &'])
2704            else
2705              eval(['!' filename_bat ' &']);
2706              display(['!' filename_bat ' &'])
2707            end
2708        end
2709     end
2710end
2711% set(handles.BATCH, 'Enable','On')
2712% set(handles.BATCH,'BackgroundColor',[1 0 0])
2713
2714%save interface state
2715if isfield(filecell,'nc')
2716    if isfield(filecell.nc,'civ2')
2717        fileresu=filecell.nc.civ2{1,1};
2718    else
2719        fileresu=filecell.nc.civ1{1,1};
2720    end
2721end
2722[RootPath,RootFile,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fileresu);
2723namedoc=fullfile(RootPath,subdir,RootFile);
2724detect=1;
2725while detect==1
2726    namefigfull=[namedoc '.fig'];
2727    hh=dir(namefigfull);
2728    if ~isempty(hh)
2729        detect=1;
2730        namedoc=[namedoc '.0'];
2731    else
2732        detect=0;
2733    end
2734end
2735saveas(gcbf,namefigfull);%save the interface with name namefigfull (A CHANGER EN FICHIER  .xml)
2736
2737%------------------------------------------------------------------------
2738%OUTPUT:
2739% filecell: structure of input and output files
2740%         .ima1.civ1{i,j}, .ima1.civ2{i,j}; set of first image names for  correlations, for civ1 and civ2
2741%         .ima2.civ1{i,j}, .ima2.civ2{i,j} ; set of second image names for correlations
2742%         .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)
2743%         .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)
2744%         .nc.civ1{i,j}, .nc.civ2{i,j}; set of nc files for PIV results
2745%         .ncA.civ1{i,j}, .ncA.civ2{i,j}; set of nc files for PIV results with camA (then .nc corresponds to camB)
2746%         .st{i,j};   set of nc files for the combined stereo fields
2747% num1_civ1(i),num2_civ1(i): lists of first and last i indices for civ1
2748% num_a_civ1(j),num_b_civ1(j): lists of first and last j indices for civ1
2749% num1_civ2(i),num2_civ2(i): lists of first and last i indices for civ2
2750% num_a_civ2(j),num_b_civ2(j): lists of first and last j indices for civ2
2751% nom_type_nc: nomenclature type for velocity files
2752% file_ref_fix1,file_ref_fix2: reference files possibly used by Fix1 and 2
2753function [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]=...
2754    set_civ_filenames(handles,compare,box_test)
2755%------------------------------------------------------------------------
2756filecell=[];%default
2757%get the filename root, nomenclature and numbers
2758filebase=get(handles.RootName,'String');
2759browse=get(handles.browse_root,'UserData');
2760compare_list=get(handles.compare,'String');
2761val=get(handles.compare,'Value');
2762compare=compare_list{val};
2763if strcmp(compare,'displacement')
2764    mode='displacement';
2765else
2766    mode_list=get(handles.mode,'String');
2767    mode_value=get(handles.mode,'Value');
2768    mode=mode_list{mode_value};
2769end
2770time=get(handles.RootName,'UserData'); %get the set of times
2771ext_ima=get(handles.ImaExt,'String');
2772nom_type_nc=browse.nom_type_nc;
2773nom_type_ima2=browse.nom_type_ima;
2774if isequal(nom_type_ima2,[]),nom_type_ima2='ima_num';end; %default
2775if isequal(nom_type_nc,[]),nom_type_nc='_i1-i2';end; %default
2776[num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2]=...
2777    find_pair_indices(handles,mode);
2778%determine the new filebase for 'displacement' mode (comparison of two series)
2779filebase_B=filebase;% root name of the second field series for stereo
2780if strcmp(compare,'displacement') || strcmp(compare,'stereo PIV')
2781    test_disp=1;   
2782    nom_type_ima1=browse.nom_type_ima_1; %nomenclature type of the second file series
2783    [Path2,Name2]=fileparts(filebase_B);
2784    Path1=Path2;
2785    Name1=get(handles.RootName_1,'String');% root name of the first field series for stereo
2786    filebase_A=fullfile(Path1,Name1);
2787    if length(Name1)>6
2788        Name1=Name1(end-5:end);
2789    end
2790    if length(Name2)>6
2791        Name2=Name2(end-5:end);
2792    end
2793    filebase_AB=fullfile(Path2,[Name2 '-' Name1]);
2794else
2795    test_disp=0;
2796    filebase_A=filebase;
2797    nom_type_ima1=nom_type_ima2;
2798    filebase_AB=filebase;
2799end
2800if strcmp(compare,'displacement')
2801        filebase_ima1=filebase_A;
2802        filebase_ima2=filebase_B;
2803        filebase_nc=filebase_AB; %root name for the result of civ2 
2804else
2805        filebase_ima1=filebase_B;
2806        filebase_ima2=filebase_B;
2807        filebase_nc=filebase_B;
2808end   
2809
2810%determine reference files for fix:
2811file_ref_fix1={};%default
2812file_ref_fix2={};
2813nbfield=length(num1_civ1);
2814nbslice=length(num_a_civ1);
2815if box_test(2)==1% fix1 performed
2816    ref=get(handles.ref_fix1,'UserData');%read data on the ref file stored by get_ref_fix1_Callback
2817    if ~isempty(ref)
2818        first_i=str2num(get(handles.first_i,'String'));
2819        last_i=str2num(get(handles.last_i,'String'));
2820        incr_i=str2num(get(handles.incr_i,'String'));
2821        first_j=str2num(get(handles.first_j,'String'));
2822        last_j=str2num(get(handles.last_j,'String'));
2823        incr_j=str2num(get(handles.incr_j,'String'));
2824        num_i_ref=[first_i:incr_i:last_i];
2825        num_j_ref=[first_j:incr_j:last_j];
2826        if isequal(mode,'displacement')
2827            num_i1=num_i_ref;
2828            num_i2=num_i_ref;
2829            num_j1=num_j_ref;
2830            num_j2=num_j_ref;
2831        elseif isequal(mode,'pair j1-j2')% isequal(mode,'st_pair j1-j2')
2832            num_i1=num_i_ref;
2833            num_i2=num_i1;
2834            num_j1=ref.num_a*ones(size(num_i_ref));
2835            num_j2=ref.num_b*ones(size(num_i_ref));
2836        elseif isequal(mode,'series(Di)') % isequal(mode,'st_series(Di)')
2837            delta1=floor((ref.num2-ref.num1)/2);
2838            delta2=ceil((ref.num2-ref.num1)/2);
2839            num_i1=num_i_ref-delta1*ones(size(num_i_ref));
2840            num_i2=num_i_ref+delta2*ones(size(num_i_ref));
2841            if isempty(ref.num_a)
2842                ref.num_a=1;
2843            end
2844            num_j1=ref.num_a*ones(size(num_i1));
2845            num_j2=num_j1;
2846        elseif isequal(mode,'series(Dj)')%| isequal(mode,'st_series(Dj)')
2847            delta1=floor((ref.num_b-ref.num_a)/2);
2848            delta2=ceil((ref.num_b-ref.num_a)/2);
2849            num_i1=ref.num1*ones(size(num_i_ref));
2850            num_i2=num_i1;
2851            num_j1=num_j_ref-delta1*ones(size(num_j_ref));
2852            num_j2=num_j_ref+delta2*ones(size(num_j_ref));
2853        end
2854        for ifile=1:nbfield
2855            for j=1:nbslice
2856                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);%
2857                file_ref_fix1(ifile,j)={file_ref};
2858                if ~exist(file_ref,'file')
2859                    msgbox_uvmat('ERROR',['reference file ' file_ref ' not found for fix1'])
2860                    filecell=[];
2861                    return
2862                end
2863            end
2864        end
2865    end
2866end
2867
2868%determine reference files for fix2:
2869if box_test(5)==1% fix2 performed
2870    ref=get(handles.ref_fix2,'UserData');
2871    if ~isempty(ref)
2872        first_i=str2num(get(handles.first_i,'String'));
2873        last_i=str2num(get(handles.last_i,'String'));
2874        incr_i=str2num(get(handles.incr_i,'String'));
2875        first_j=str2num(get(handles.first_j,'String'));
2876        last_j=str2num(get(handles.last_j,'String'));
2877        incr_j=str2num(get(handles.incr_j,'String'));
2878        num_i_ref=[first_i:incr_i:last_i];
2879        num_j_ref=[first_j:incr_j:last_j];
2880        if isequal(mode,'displacement')
2881            num_i1=num_i_ref;
2882            num_i2=num_i_ref;
2883            num_j1=num_j_ref;
2884            num_j2=num_j_ref;
2885        elseif isequal(mode,'pair j1-j2')
2886            num_i1=num_i_ref;
2887            num_i2=num_i1;
2888            num_j1=ref.num_a;
2889            num_j2=ref.num_b;
2890        elseif isequal(mode,'series(Di)')
2891            delta1=floor((ref.num2-ref.num1)/2);
2892            delta2=ceil((ref.num2-ref.num1)/2);
2893            num_i1=num_i_ref-delta1*ones(size(num_i_ref));
2894            num_i2=num_i_ref+delta2*ones(size(num_i_ref));
2895            num_j1=ref.num_a*ones(size(num_i1));
2896            num_j2=num_j1;
2897        elseif isequal(mode,'series(Dj)')
2898            delta1=floor((ref.num_b-ref.num_a)/2);
2899            delta2=ceil((ref.num_b-ref.num_a)/2);
2900            num_i1=ref.num1*ones(size(num_i_ref));
2901            num_i2=num_i1;
2902            num_j1=num_j_ref-delta1*ones(size(num_j_ref));
2903            num_j2=num_j_ref+delta2*ones(size(num_j_ref));
2904        end
2905        for ifile=1:nbfield
2906            for j=1:nbslice
2907                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);%
2908                file_ref_fix2(ifile,j)={file_ref};
2909                if ~exist(file_ref,'file')
2910                    msgbox_uvmat('ERROR',['reference file ' file_ref ' not found for fix2'])
2911                    filecell=[];
2912                    return
2913                end
2914            end
2915        end
2916    end
2917end
2918
2919%check dir
2920subdir_civ1=get(handles.subdir_civ1,'String');%subdirectory subdir_civ1 for the netcdf output data
2921subdir_civ2=get(handles.subdir_civ2,'String');
2922if isequal(subdir_civ1,''),subdir_civ1='A'; end% put default subdir
2923if isequal(subdir_civ2,''),subdir_civ2=subdir_civ1; end% put default subdir
2924currentdir=pwd;%store the current working directory
2925[Path_ima,Name]=fileparts(filebase);%Path of the image files (.civ)
2926if ~exist(Path_ima,'dir')
2927    msgbox_uvmat('ERROR',['path to images ' Path_ima ' not found'])
2928    filecell=[];
2929    return
2930end
2931cd(Path_ima);%move to the directory of the images: needed to create the result dir by 'mkdir'
2932dircur=pwd; %current working directory
2933m2='';
2934[erread,message]=fileattrib(Path_ima);
2935if ~isempty(message) & ~isequal(message.UserWrite,1)
2936      msgbox_uvmat('ERROR',['No writting access to ' Path_ima])
2937      filecell=[];
2938      cd(currentdir);
2939      return
2940end
2941
2942%check the existence of the netcdf and image files involved
2943% %%%%%%%%%%%%  case CIV1 activated   %%%%%%%%%%%%%
2944if box_test(1)==1;
2945     detect=1;     
2946     while detect==1 %create a new subdir if the netcdf files already exist
2947          for ifile=1:nbfield;
2948              for j=1:nbslice
2949                  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);
2950                  detect=exist(filename,'file')==2;
2951                  if detect% if a netcdf file already exists
2952                     subdir_civ1=[subdir_civ1 '.0'];
2953                     subdir_civ2=subdir_civ1;
2954                     break
2955                  end
2956                  filecell.nc.civ1(ifile,j)={filename};
2957              end
2958              if detect% if a netcdf file already exists
2959                  break
2960              end
2961          end
2962           %create the new subdir_civ1
2963          if ~exist(fullfile(Path_ima,subdir_civ1),'dir')
2964              [m1,m2,m3]=mkdir(subdir_civ1);
2965                if ~isequal(m2,'')
2966                   msgbox_uvmat('ERROR', m2)
2967                   %msgbox(m2);%error message for directory creation
2968              end
2969          end
2970          if strcmp(compare,'stereo PIV')&&(strcmp(mode,'pair j1-j2')||strcmp(mode,'series(Dj)')||strcmp(mode,'series(Di)'))%check second nc series
2971                for ifile=1:nbfield
2972                     for j=1:nbslice
2973                         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);%
2974                          detect=exist(filename,'file')==2;
2975                          if detect% if a netcdf file already exists
2976                              subdir_civ1=[subdir_civ1 '.0'];
2977                              subdir_civ2=subdir_civ1;
2978                              break
2979                          end
2980                          filecell.ncA.civ1(ifile,j)={filename};
2981                     end
2982                    if detect% if a netcdf file already exists
2983                         break
2984                    end
2985              end
2986           %create the new subdir_civ1
2987               if ~exist(fullfile(Path_ima,subdir_civ1),'dir')
2988                      [m1,m2,m3]=mkdir(subdir_civ1);
2989                       if ~isequal(m2,'')
2990                            msgbox_uvmat('ERROR', m2)
2991                      end
2992               end
2993          end
2994     end       
2995    % get image names
2996    for ifile=1:nbfield
2997         for j=1:nbslice
2998            filename=name_generator(filebase_ima1, num1_civ1(ifile),num_a_civ1(j),ext_ima,nom_type_ima1);
2999            idetect(j)=exist(filename,'file')==2;
3000            filecell.ima1.civ1(ifile,j)={filename}; %first image
3001            filename=name_generator(filebase_ima2, num2_civ1(ifile),num_b_civ1(j),ext_ima,nom_type_ima2);
3002            idetect_1(j)=exist(filename,'file')==2;
3003            filecell.ima2.civ1(ifile,j)={filename};%second image
3004         end
3005            [idetectmin,indexj]=min(idetect);
3006          if idetectmin==0,
3007                msgbox_uvmat('ERROR',[filecell.ima1.civ1{ifile,indexj} ' not found'])
3008                filecell=[];
3009                return
3010          end
3011            [idetectmin,indexj]=min(idetect_1);
3012          if idetectmin==0,
3013                msgbox_uvmat('ERROR',[filecell.ima2.civ1{ifile,indexj} ' not found'])
3014                filecell=[];
3015                cd(currentdir)
3016                return
3017          end
3018    end
3019    if strcmp(compare,'stereo PIV') && (strcmp(mode,'pair j1-j2') || strcmp(mode,'series(Dj)') || strcmp(mode,'series(Di)'))
3020         for ifile=1:nbfield
3021             for j=1:nbslice
3022                  filename=name_generator(filebase_A, num1_civ1(ifile),num_a_civ1(j),ext_ima,nom_type_ima1);
3023                  idetect(j)=exist(filename,'file')==2;
3024                  filecell.imaA1.civ1(ifile,j)={filename} ;%first image
3025                  filename=name_generator(filebase_A, num2_civ1(ifile),num_b_civ1(j),ext_ima,nom_type_ima2);
3026                  idetect_1(j)=exist(filename,'file')==2;
3027                  filecell.imaA2.civ1(ifile,j)={filename};%second image
3028             end
3029              [idetectmin,indexj]=min(idetect);
3030              if idetectmin==0,
3031                    msgbox_uvmat('ERROR',[filecell.imaA1.civ1{ifile,indexj} ' not found'])
3032                    filecell=[];
3033                    cd(currentdir)
3034                    return
3035              end
3036               [idetectmin,indexj]=min(idetect_1);
3037               if idetectmin==0,
3038                   msgbox_uvmat('ERROR',[filecell.imaA2.civ1{ifile,indexj} ' not found'])
3039                  filecell=[];
3040                  cd(currentdir)
3041                  return
3042               end
3043         end
3044    end
3045       
3046 %%%%%%%%%%%%%  fix1 or patch1 activated but no civ1   %%%%%%%%%%%%%
3047elseif (box_test(2)==1 || box_test(3)==1); 
3048    for ifile=1:nbfield
3049        for j=1:nbslice
3050            filename=name_generator(filebase_nc,num1_civ1(ifile),num_a_civ1(j),'.nc',...
3051                    nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
3052            detect=exist(filename,'file')==2;   
3053            filecell.nc.civ1(ifile,j)={filename};
3054        end
3055    end
3056    if strcmp(compare,'stereo PIV')
3057            for ifile=1:nbfield
3058                for j=1:nbslice
3059                    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);%
3060                    filecell.ncA.civ1(ifile,j)={filename};
3061                    if ~exist(filename,'file')
3062                        msgbox_uvmat('ERROR',['input file ' filename ' not found'])
3063                        set(handles.RUN, 'Enable','On')
3064                        set(handles.RUN,'BackgroundColor',[1 0 0])
3065%                         set(handles.BATCH, 'Enable','On')
3066%                         set(handles.BATCH,'BackgroundColor',[1 0 0])
3067                        filecell=[];
3068                        cd(currentdir)
3069                        return
3070                    end
3071                end
3072            end
3073    end
3074end
3075
3076%%%%%%%%%%%%%  if civ2 performed with pairs different than civ1  %%%%%%%%%%%%%
3077testdiff=0;
3078if (box_test(4)==1)&&...
3079        ((get(handles.list_pair_civ1,'Value')~=get(handles.list_pair_civ2,'Value'))||~isequal(subdir_civ2,subdir_civ1))
3080    testdiff=1;
3081    detect=1;
3082    while detect==1 %create a new subdir if the netcdf files already exist
3083          for ifile=1:nbfield
3084              for j=1:nbslice
3085                  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);%
3086                  detect=exist(filename,'file')==2;
3087                  if detect% if a netcdf file already exists
3088                     subdir_civ2=[subdir_civ2 '.0'];
3089                     break
3090                  end
3091                  filecell.nc.civ2(ifile,j)={filename};
3092              end
3093              if detect% if a netcdf file already exists
3094                  break
3095              end
3096          end
3097           %create the new subdir_civ2
3098          if ~exist(fullfile(Path_ima,subdir_civ2),'dir')
3099              [m1,m2,m3]=mkdir(subdir_civ2);
3100              if ~isequal(m2,'')
3101                   msgbox_uvmat('ERROR', m2)
3102              end
3103          end
3104           if strcmp(compare,'stereo PIV')%check second nc series
3105                for ifile=1:nbfield
3106                     for j=1:nbslice
3107                         filename=name_generator(filebase_A,num1_civ2(ifile),num_a_civ2(j),'.nc',...
3108                          nom_type_nc,1,num2_civ2(ifile),num_b_civ1(j),subdir_civ2);%
3109                          detect=exist(filename,'file')==2;
3110                          if detect% if a netcdf file already exists
3111                              subdir_civ2=[subdir_civ2 '.0'];
3112                              break
3113                          end
3114                          filecell.ncA.civ2(ifile,j)={filename};
3115                     end
3116                    if detect% if a netcdf file already exists
3117                         break
3118                    end
3119              end
3120           %create the new subdir_civ1
3121               if ~exist(fullfile(Path_ima,subdir_civ2),'dir')
3122                      [m1,m2,m3]=mkdir(subdir_civ2);
3123                        if ~isequal(m2,'')
3124                            msgbox_uvmat('ERROR', m2)
3125                            %msgbox(m2);%error message for directory creation
3126                      end
3127               end
3128          end
3129    end 
3130end
3131cd(currentdir);%come back to the current working directory
3132   
3133
3134%%%%%%%%%%%%%  if civ2 results are obtained or used  %%%%%%%%%%%%%
3135if box_test(4)==1 || box_test(5)==1 || box_test(6)==1 %civ2
3136    %check source netcdf file of civ1 estimates
3137    if box_test(1)==0; %no civ1 performed
3138        for ifile=1:nbfield
3139            for j=1:nbslice
3140                filename=name_generator(filebase_nc,num1_civ1(ifile),num_a_civ1(j),'.nc',...
3141                    nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
3142                filecell.nc.civ1(ifile,j)={filename};% name of the civ1 file
3143                if ~exist(filename,'file')
3144                    msgbox_uvmat('ERROR',['input file ' filename ' not found'])
3145                  filecell=[];
3146                    return
3147                end
3148                if ~testdiff % civ2 or patch2 are written in the same file as civ1
3149                    if box_test(4)==0 ; %check the existence of civ2 if it is not calculated
3150                        Data=nc2struct(filename,'ListGlobalAttribute','civ2');
3151                        if isempty(Data.civ2)||isequal(Data.civ2,0)
3152                            msgbox_uvmat('ERROR',['no civ2 data in ' filename])
3153                            filecell=[];
3154                            return
3155                        end 
3156                    elseif box_test(3)==0; %check the existence of patch if it is not calculated
3157                        Data=nc2struct(filename,'ListGlobalAttribute','patch');
3158                        if isempty(Data.patch)||isequal(Data.patch,0)
3159                            msgbox_uvmat('ERROR',['no patch data in ' filename])
3160                            filecell=[];
3161                            return
3162                        end
3163                    end
3164                end
3165            end
3166        end
3167        if strcmp(compare,'stereo PIV')
3168            for ifile=1:nbfield
3169                for j=1:nbslice
3170                    filename=name_generator(filebase_A,num1_civ2(ifile),num_a_civ2(j),'.nc',...
3171                        nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
3172                    filecell.ncA.civ2(ifile,j)={filename};
3173                    if ~exist(filename,'file')
3174                        msgbox_uvmat('ERROR',['input file ' filename ' not found'])
3175                        set(handles.RUN, 'Enable','On')
3176                        set(handles.RUN,'BackgroundColor',[1 0 0])
3177%                                set(handles.BATCH, 'Enable','On')
3178%                     set(handles.BATCH,'BackgroundColor',[1 0 0])
3179                        cd(currentdir)
3180                        return
3181                    end
3182                end
3183            end
3184        end
3185    end
3186
3187    detect=1;
3188%     while detect==1%creates a new subdir if the netcdf files already contain civ2 data
3189        for ifile=1:nbfield
3190            for j=1:nbslice
3191                filename=name_generator(filebase_nc,num1_civ2(ifile),num_a_civ2(j),'.nc',...
3192                nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);
3193                detect=exist(filename,'file')==2;
3194                filecell.nc.civ2(ifile,j)={filename};
3195            end
3196        end
3197    %get first image names for civ2
3198    if box_test(1)==1 & isequal(num1_civ1,num1_civ2) & isequal(num_a_civ1,num_a_civ2)
3199        filecell.ima1.civ2=filecell.ima1.civ1;
3200    elseif box_test(4)==1
3201        for ifile=1:nbfield
3202            for j=1:nbslice
3203                filename=name_generator(filebase_ima1, num1_civ2(ifile),num_a_civ2(j),ext_ima,nom_type_ima1);
3204                idetect_2(j)=exist(filename,'file')==2;
3205                filecell.ima1.civ2(ifile,j)={filename};%first image
3206            end
3207                [idetectmin,indexj]=min(idetect_2);
3208            if idetectmin==0,
3209                    msgbox_uvmat('ERROR',['input image ' filecell.ima1.civ2{ifile,indexj} ' not found'])
3210                    filecell=[];
3211                    return
3212            end
3213        end
3214    end
3215   
3216    %get second image names for civ2
3217    if box_test(1)==1 & isequal(num2_civ1,num2_civ2) & isequal(num_b_civ1,num_b_civ2)
3218        filecell.ima2.civ2=filecell.ima2.civ1;
3219    elseif box_test(4)==1
3220        for ifile=1:nbfield
3221            for j=1:nbslice
3222                filename=name_generator(filebase_ima2, num2_civ2(ifile),num_b_civ2(j),ext_ima,nom_type_ima2);
3223                idetect_3(j)=exist(filename,'file')==2;
3224                filecell.ima2.civ2(ifile,j)={filename};%first image
3225            end
3226                [idetectmin,indexj]=min(idetect_3);
3227            if idetectmin==0,
3228                    msgbox_uvmat('ERROR',['input image ' filecell.ima2.civ2{ifile,indexj} ' not found'])
3229                    filecell=[];
3230                    return
3231            end
3232        end
3233    end
3234end
3235if (box_test(5)==1 || box_test(6)==1 ) && box_test(4)==0  % need to read an existing netcdf civ2 file
3236    if ~testdiff
3237        filecell.nc.civ2=filecell.nc.civ1;% file already checked
3238    else     % check the civ2 files
3239        for ifile=1:nbfield
3240            for j=1:nbslice
3241                filename=name_generator(filebase_nc,num1_civ2(ifile),num_a_civ2(j),'.nc',...
3242                        nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
3243                filecell.nc.civ2(ifile,j)={filename};
3244                if ~exist(filename,'file')
3245                    msgbox_uvmat('ERROR',['input file ' filename ' not found'])
3246                    filecell=[];
3247                    return
3248                else
3249                    Data=nc2struct(filename,'ListGlobalAttribute','civ2');
3250                    if isempty(Data.civ2)||isequal(Data.civ2,0)         
3251                        msgbox_uvmat('ERROR',['no civ2 data in ' filename])
3252                        filecell=[];
3253                        return
3254                    end
3255                end
3256            end
3257        end
3258    end
3259end
3260
3261%%%%%%%%%%%%%  if stereo fields are calculated by PATCH %%%%%%%%%%%%%
3262if strcmp(compare,'stereo PIV')
3263    if  box_test(3)==1 & isequal(get(handles.test_stereo1,'Value'),1)
3264        for ifile=1:nbfield
3265            for j=1:nbslice
3266                filename=name_generator(filebase_AB,num1_civ1(ifile),num_a_civ1(j),'.nc',...
3267                    nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
3268                filecell.st(ifile,j)={filename};
3269            end
3270        end
3271    end
3272     if  box_test(6)==1 & isequal(get(handles.test_stereo2,'Value'),1)
3273        for ifile=1:nbfield
3274            for j=1:nbslice
3275                filename=name_generator(filebase_AB,num1_civ2(ifile),num_a_civ2(j),'.nc',...
3276                    nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
3277                filecell.st(ifile,j)={filename};
3278            end
3279        end
3280     end
3281end
3282set(handles.subdir_civ1,'String',subdir_civ1);%update the edit box
3283set(handles.subdir_civ2,'String',subdir_civ2);%update the edit box
3284browse.nom_type_nc=nom_type_nc;
3285set(handles.browse_root,'UserData',browse); %update the nomenclature type for uvmat
3286
3287
3288%COPY IMAGES TO THE FORMAT .png IF NEEDED
3289if isequal(nom_type_ima1,'*')%case of movie files
3290    nom_type_imanew1='_i';
3291else
3292    nom_type_imanew1=nom_type_ima1;
3293end
3294if isequal(nom_type_ima2,'*')%case of movie files
3295    nom_type_imanew2='_i';
3296else
3297    nom_type_imanew2=nom_type_ima2;
3298end
3299if ~isequal(ext_ima,'.png')
3300    %%type of image file
3301    type_ima1='none';%default
3302    movieobject1=[];%default
3303    if isequal(lower(ext_ima),'.avi')
3304        hhh=which('mmreader');
3305        if ~isequal(hhh,'')&& mmreader.isPlatformSupported()% if the mmreader function is found (recent version of matlab)
3306            type_ima1='movie';
3307            movieobject1=mmreader([filebase_ima2 ext_ima]);
3308        else
3309            type_ima1='avi';
3310        end
3311    else
3312       form=imformats(ext_ima(2:end));
3313       if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
3314           if isequal(nom_type_ima1,'*');
3315               type_ima1='multimage';%image series in a single image file
3316           else
3317               type_ima1='image';
3318           end
3319       end
3320    end
3321    type_ima2='none';%default
3322    movieobject2=[];
3323    if isequal(lower(ext_ima),'.avi')
3324        hhh=which('mmreader');
3325        if ~isequal(hhh,'')&& mmreader.isPlatformSupported()% if the mmreader function is found (recent version of matlab)
3326            type_ima2='movie';
3327            movieobject2=mmreader([filebase_ima2 ext_ima]);
3328        else
3329            type_ima2='avi';
3330        end
3331    else
3332       form=imformats(ext_ima(2:end));
3333       if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
3334           if isequal(nom_type_ima1,'*');
3335               type_ima2='multimage';%image series in a single image file
3336           else
3337               type_ima2='image';
3338           end
3339       end
3340    end
3341    %npxy=get(handles.ImaExt,'UserData');
3342% %     if numel(npxy)<2
3343%     
3344%         filename=name_generator(filebase_ima1,num1_civ1(1),num_a_civ1(1),ImaExt,nom_type_ima1);
3345%         A=imread(filename);
3346%         npxy=size(A);
3347% %     end
3348%     npy=npxy(1);
3349%     npx=npxy(2);
3350    if box_test(1)==1 %if civ1 is performed
3351       h = waitbar(0,['copy images to the .png format for civ1']);% display a wait bar
3352       for ifile=1:nbfield
3353            waitbar(ifile/nbfield);
3354            for j=1:nbslice
3355                    filename=name_generator(filebase_ima1,num1_civ1(ifile),num_a_civ1(j),'.png',nom_type_imanew1);
3356                    if ~exist(filename,'file')
3357                        A=read_image(filecell.ima1.civ1{ifile,j},type_ima1,num1_civ1(ifile),movieobject1);
3358                        imwrite(A,filename,'BitDepth',16);
3359                    end
3360                    filecell.ima1.civ1(ifile,j)={filename};
3361                    filename=name_generator(filebase_ima2, num2_civ1(ifile),num_b_civ1(j),'.png',nom_type_imanew2);
3362                    if ~exist(filename,'file')
3363                        A=read_image(filecell.ima2.civ1{ifile,j},type_ima2,num2_civ1(ifile),movieobject2);
3364                        imwrite(A,filename,'BitDepth',16);
3365                    end
3366                    filecell.ima2.civ1(ifile,j)={filename};
3367            end
3368        end
3369        close(h)
3370    end
3371    if box_test(4)==1 %if civ2 is performed
3372        h = waitbar(0,['copy images to the .png format for civ2']);% display a wait bar
3373        for ifile=1:nbfield
3374            waitbar(ifile/nbfield);
3375            for j=1:nbslice
3376                    filename=name_generator(filebase_ima1,num1_civ2(ifile),num_a_civ2(j),'.png',nom_type_imanew1);
3377                    if ~exist(filename,'file')
3378                        A=read_image(cell2mat(filecell.ima1.civ2(ifile,j)),type_ima2,num1_civ2(ifile));
3379                        imwrite(A,filename,'BitDepth',16);
3380                    end
3381                    filecell.ima1.civ2(ifile,j)={filename};
3382                    filename=name_generator(filebase_ima2, num2_civ2(ifile),num_b_civ2(j),'.png',nom_type_imanew2);
3383                    if ~exist(filename,'file')
3384                        A=read_image(cell2mat(filecell.ima2.civ2(ifile,j)),type_ima2,num2_civ2(ifile));
3385                        imwrite(A,filename,'BitDepth',16);
3386                    end
3387                    filecell.ima2.civ2(ifile,j)={filename};
3388            end
3389        end
3390        close(h);
3391    end
3392end
3393
3394%------------------------------------------------------------------------
3395%CIV1  CIV1  CIV1 CIV1 NOT USED: replaced by RUN_BATCH
3396function RUN_CIV1(handles,filecell,filecell_1,filecell_nc1,num1,num2,num_a,num_b,nom_type_nc)
3397%------------------------------------------------------------------------
3398%pixels per cm and matrix of the image times, read from the .civ file by uvmat
3399global civ1Bin sge%name of the executable for civ1 calculation
3400
3401%get civ parameters
3402ibx_val=str2num(get(handles.ibx,'String'));
3403if isempty(ibx_val)
3404    ibx='21'; set(handles.ibx,'String','21')
3405else
3406    ibx=num2str(ibx_val);
3407end
3408iby_val=str2num(get(handles.iby,'String'));
3409if isempty(iby_val)
3410    iby='21'; set(handles.iby,'String','21')
3411else
3412    iby=num2str(iby_val);
3413end
3414isx=get(handles.isx,'String');
3415if isempty(str2num(isx)), isx='41'; set(handles.isx,'String','41'), end; %default
3416if str2num(isx)<ibx_val+8,isx=num2str(ibx_val+8); set(handles.isx,'String',num2str(ibx_val+8)); end
3417isy=get(handles.isy,'String');
3418if isempty(str2num(isy)), isy='41'; set(handles.isy,'String','41'), end;%default
3419if str2num(isy)<iby_val+8,isy=num2str(iby_val+8); set(handles.isy,'String',num2str(iby_val+8)); end
3420shiftx=get(handles.shiftx,'String');
3421if isempty(str2num(shiftx)), shiftx='0'; set(handles.shiftx,'String','0'), end;%default
3422shifty=get(handles.shifty,'String');
3423if isempty(str2num(shifty)), shifty='0'; set(handles.shifty,'String','0'), end;%default
3424rho=get(handles.rho,'String');
3425dx=get(handles.dx_civ1,'String');
3426if isequal(str2num(dx),[]), dx='20'; set(handles.dx_civ1,'String','20'); end%default
3427dy=get(handles.dy_civ1,'String');
3428if isequal(str2num(dy),[]), dy='20'; set(handles.dy_civ1,'String','20');end %default
3429if isequal(str2num(dy),[]), dy='20'; end%default
3430pxcmx='1'; %velocity fields are expressed in pixel displacement
3431pxcmy='1';
3432image_first=cell2mat(filecell(1,1));
3433if ~exist(image_first,'file')
3434    msgbox_uvmat('ERROR',['image ' image_first 'not found'])
3435    set(handles.RUN, 'Enable','On')
3436    set(handles.RUN,'BackgroundColor',[1 0 0])
3437    return
3438end
3439A=imread(cell2mat(filecell(1,1)));%read the first image to get the size
3440sizim=size(A);
3441npx=num2str(sizim(2));
3442npy=num2str(sizim(1));
3443time=get(handles.RootName,'UserData'); %get the set of times
3444gridname='';%default  ='noFile use default'
3445gridflag='n';%default
3446test_grid=get(handles.browse_gridciv1,'Value');
3447nbslice_grid=[];
3448if test_grid
3449    gridname=get(handles.grid_civ1,'String');
3450    if numel(gridname)>4 && isequal(gridname(end-3:end),'grid')
3451        nbslice_grid=str2num(gridname(1:end-4)); %
3452        if ~isempty(nbslice_grid)
3453            gridflag='y';
3454        end
3455    elseif exist(gridname,'file')
3456        gridflag='y';
3457    else
3458        msgbox_uvmat('ERROR',['input grid file ' gridname ' not found'])
3459        return
3460    end
3461end
3462if isequal(get(handles.ImaThreshold,'Value'),1)
3463    threshflag='y';
3464    min_ima=get(handles.MinIma,'String');
3465    max_ima=get(handles.MaxIma,'String');
3466else
3467    threshflag='n';
3468    min_ima='0';
3469    max_ima='4096';
3470end
3471 %main loop
3472filebase=get(handles.RootName,'String');
3473sizcell=size(filecell);
3474nbfield=sizcell(1);
3475nbslice=sizcell(2);
3476icount=0;
3477for ifile=1:nbfield
3478    for j=1:nbslice
3479        icount=icount+1;
3480        barlength=0.188*icount/(nbfield*nbslice);
3481        set(handles.waitbar_1,'Position',[0.946 0.877-barlength 0.03 barlength])
3482        drawnow
3483        filename_ima=cell2mat(filecell(ifile,j));
3484        filename_ima([end-3:end])=[];%remove .png extension
3485        filename_ima_1=cell2mat(filecell_1(ifile,j));
3486        filename_ima_1([end-3:end])=[];%remove .png extension
3487        filename_cmx=cell2mat(filecell_nc1(ifile,j));%output netcdf file
3488        filename_cmx([end-1:end])=[ 'cm'];%name of cmx file
3489        filename_cmx=[filename_cmx 'x'];
3490        namelog=[filename_cmx([1:end-3]) 'log'];       
3491        if size(time,1)>=num2(ifile) &  size(time,2)>=num_b(j)
3492            Dt=num2str(time(num2(ifile),num_b(j))-time(num1(ifile),num_a(j)));
3493            if isequal(Dt,'0')
3494                Dt='1' ;%case of 'displacement' mode
3495            end
3496            T0=num2str((time(num2(ifile),num_b(j))+time(num1(ifile),num_a(j)))/2);
3497        else
3498            Dt='1';
3499            T0='0';
3500        end
3501        term_a=num2stra(num_a(j),nom_type_nc);%UTILITE?
3502        term_b=num2stra(num_b(j),nom_type_nc);%
3503        if test_grid && ~isempty(nbslice_grid)       
3504             num1_grid=mod(num1(ifile)-1,nbslice_grid)+1;
3505            gridname=[filebase '_' name_generator(gridname,num1_grid,1,'.grid','_i')];
3506            if ~exist(gridname,'file')
3507                msgbox_uvmat('ERROR',['missing grid file ' gridname])
3508                return
3509            end
3510        end
3511        test_mask=get(handles.get_mask_civ1,'Value');
3512        if test_mask==0
3513            maskname='noFile use default';
3514            maskflag='n';
3515        else
3516            maskdispl=get(handles.mask_civ1,'String');%look for mask name in edit box
3517            maskbase=[filebase '_' maskdispl];%
3518            nbslice=str2num(maskdispl(1:end-4)); %
3519            num1_mask=mod(num1(ifile)-1,nbslice)+1;
3520            maskname=name_generator(maskbase,num1_mask,1,'.png','_i');
3521            if exist(maskname,'file')
3522                maskflag='y';
3523            else
3524                maskname='noFile use default';
3525                maskflag='n';
3526            end
3527        end
3528                textcmx={'##############   CMX file';...
3529                ['FirstImage ' filename_ima];...
3530                ['LastImage  ' filename_ima_1];...
3531                'XX' ;...
3532                ['Mask ' maskflag] ;...
3533                ['MaskName ' maskname];...
3534                ['ImageSize ' npx ' ' npy];...   %VERIFIER CAS GENERAL ?
3535                ['CorrelationBoxesSize ' ibx ' ' iby];...
3536                ['SearchBoxeSize ' isx ' ' isy];...
3537                ['RO ' rho];...
3538                ['GridSpacing ' dx ' ' dy];...
3539                'XX 1.0';...
3540                ['Dt_TO ' Dt ' ' T0];...
3541                ['PixCmXY ' pxcmx ' ' pxcmy];...
3542                'XX 1';...
3543                ['ShiftXY ' shiftx ' ' shifty];...
3544                ['Grid ' gridflag];...
3545                ['GridName ' gridname] ;...
3546                'XX 85';...
3547                'XX 1.0';...
3548                'XX 1.0';...
3549                'Hart 1';...
3550                'DecimalShift 0';...
3551                'Deformation 0';...
3552                'CorrelationMin 0';...
3553                'IntensityMin 0';...
3554                ['SeuilImage ' threshflag];...
3555                ['SeuilImageValues ' min_ima ' ' max_ima];...
3556                ['ImageToUse ' term_a ' ' term_b];... % VERIFIER ?
3557                'ImageUsedBefore null null'};
3558                textout=char(textcmx);
3559%               dlmwrite(filename_cmx,textout,'');
3560     fid=fopen([filename_cmx],'w');
3561fprintf(fid, ['##############   CMX file' '\n']);
3562 fprintf(fid,   ['FirstImage ' regexprep(filename_ima,'\\','\\\\') '\n' ]);
3563 fprintf(fid,   ['LastImage  ' regexprep(filename_ima_1,'\\','\\\\') '\n' ]);
3564  fprintf(fid,  ['XX' '\n' ]);
3565  fprintf(fid,  ['Mask '  maskflag '\n' ]);
3566  fprintf(fid,  ['MaskName '  maskname '\n' ]);
3567 fprintf(fid,   ['ImageSize '  npx ' '  npy '\n' ]);   %VERIFIER CAS GENERAL ?
3568 fprintf(fid,   ['CorrelationBoxesSize '  ibx ' '  iby '\n' ]);
3569 fprintf(fid,   ['SearchBoxeSize '  isx ' '  isy '\n' ]);
3570 fprintf(fid,   ['RO '  rho '\n' ]);
3571 fprintf(fid,   ['GridSpacing '  dx ' '  dy '\n' ]);
3572 fprintf(fid,   ['XX 1.0' '\n' ]);
3573 fprintf(fid,   ['Dt_TO '  Dt ' '  T0 '\n' ]);
3574  fprintf(fid,  ['PixCmXY '  pxcmx ' '  pxcmy '\n' ]);
3575  fprintf(fid,  ['XX 1' '\n' ]);
3576 fprintf(fid,   ['ShiftXY '  shiftx ' '   shifty '\n' ]);
3577  fprintf(fid,  ['Grid '  gridflag '\n' ]);
3578 fprintf(fid,   ['GridName '  gridname '\n' ]);
3579 fprintf(fid,   ['XX 85' '\n' ]);
3580 fprintf(fid,   ['XX 1.0' '\n' ]);
3581 fprintf(fid,   ['XX 1.0' '\n' ]);
3582 fprintf(fid,   ['Hart 1' '\n' ]);
3583 fprintf(fid,  [ 'DecimalShift 0' '\n' ]);
3584 fprintf(fid,   ['Deformation 0' '\n' ]);
3585  fprintf(fid,  ['CorrelationMin 0' '\n' ]);
3586 fprintf(fid,   ['IntensityMin 0' '\n' ]);
3587  fprintf(fid,  ['SeuilImage n' '\n' ]);
3588 fprintf(fid,   ['SeuilImageValues 0 4096' '\n' ]);
3589 fprintf(fid,   ['ImageToUse '  term_a ' '  term_b '\n' ]); % VERIFIER ?
3590 fprintf(fid,   ['ImageUsedBefore null null' '\n' ]);
3591fclose(fid);
3592       
3593                s=-1;
3594        display(['!' civ1Bin ' -f ' filename_cmx ' > ' namelog ])
3595        eval(['!' civ1Bin ' -f ' filename_cmx ' > ' namelog ]);
3596%               if sge%dispatch computation on the cluster using interactive queue
3597%           %  [s,w] = unix(['qrsh -q fast.q ' civ1Bin ' -f ' filename_cmx ' > ' namelog ' 2>&1' ]);
3598%        end     
3599%         if s~=0
3600%            %  ['!' civ1Bin ' -f ' filename_cmx ' > ' namelog]
3601%            % eval(['!' civ1Bin ' -f ' filename_cmx ' > ' namelog]);       
3602%         end
3603    end
3604end
3605
3606% %------------------------------------------------------------------------
3607% % RUN CIV2   CIV2    CIV2   CIV2
3608% function RUN_CIV2(handles,filecell_2,filecell_3,filecell_nc1,filecell_nc2,num1,num2,num_a,num_b,nom_type_nc)
3609% %------------------------------------------------------------------------
3610% %filecell_2: names of first image
3611% %filecell_3: names of second images
3612% global civ2Bin sge
3613%
3614% %names of the civ2 fields
3615% field.vel_type='civ2';
3616% field.nb='nb_vectors2';
3617% field.X='vec2_X';
3618% field.Y='vec2_Y';
3619% field.U='vec2_U';
3620% field.V='vec2_V';
3621
3622% %get civ parameters
3623% ibx=get(handles.ibx_civ2,'String');
3624% iby=get(handles.iby_civ2,'String');
3625% rho=get(handles.rho_civ2,'String');
3626% decimal=int2str(get(handles.decimal,'Value'));
3627% deformation=int2str(get(handles.deformation,'Value'));
3628% dx=get(handles.dx_civ2,'String');
3629% dy=get(handles.dy_civ2,'String');
3630% if isequal(str2num(dx),[])
3631%     dx='20';%default
3632% end
3633% if isequal(str2num(dy),[])
3634%     dy='20';%default
3635% end
3636%     pxcmx='1';%velocity fields are expressed in pixel displacement
3637%     pxcmy='1';
3638% A=imread(cell2mat(filecell_2(1,1)));%read the first image to get the size
3639% sizim=size(A);
3640% npx=num2str(sizim(2));
3641% npy=num2str(sizim(1));
3642% time=get(handles.RootName,'UserData'); %get the set of times
3643% filebase=get(handles.RootName,'String');
3644% %grid
3645% gridname='';%default  ='noFile use default'
3646% gridflag='n';%default
3647% test_grid=get(handles.browse_gridciv2,'Value');
3648% nbslice_grid=[];
3649% if test_grid
3650%     gridname=get(handles.grid_civ2,'String');
3651%     if numel(gridname)>4 && isequal(gridname(end-3:end),'grid')
3652%         nbslice_grid=str2num(gridname(1:end-4)); %
3653%         if ~isempty(nbslice_grid)
3654%             gridflag='y';
3655%         end
3656%     elseif exist(gridname,'file')
3657%         gridflag='y';
3658%     else
3659%         msgbox_uvmat('ERROR',['input grid file ' gridname ' not found'])
3660%         return
3661%     end
3662% end
3663% sizcell=size(filecell_2);
3664% nbfield=sizcell(1);
3665% nbslice=sizcell(2);
3666%   
3667% %main loop
3668% icount=0;
3669% for ifile=1:nbfield
3670%     for j=1:nbslice
3671%         icount=icount+1;
3672%         barlength=0.188*icount/(nbfield*nbslice);
3673%         set(handles.waitbar_civ2,'Position',[0.946 0.407-barlength 0.03 barlength])
3674%         drawnow
3675%         filename_ima_2=cell2mat(filecell_2(ifile,j));
3676%         filename_ima_2([end-3:end])=[];%remove .png extension
3677%         filename_ima_3=cell2mat(filecell_3(ifile,j));
3678%         filename_ima_3([end-3:end])=[];%remove .png extension
3679%         filename_cmx=cell2mat(filecell_nc2(ifile,j));%output netcdf file
3680%         filename_cmx([end-1:end])=[ 'cm'];%name of cmx file
3681%         filename_cmx=[filename_cmx 'x'];
3682%         namelog=[filename_cmx([1:end-3]) 'log'];
3683%         if size(time,1)>=num2(ifile) &  size(time,2)>=num_b(j)
3684%             Dt=num2str(time(num2(ifile),num_b(j))-time(num1(ifile),num_a(j)));
3685%             if isequal(Dt,'0')
3686%                 Dt='1' ;%case of 'displacement' mode
3687%             end
3688%             T0=num2str((time(num2(ifile),num_b(j))+time(num1(ifile),num_a(j)))/2);
3689%         else
3690%             Dt='1';
3691%             T0='0';
3692%         end
3693%         term_a=num2stra(num_a(j),nom_type_nc);
3694%         term_b=num2stra(num_b(j),nom_type_nc);
3695%         filename_nc1=cell2mat(filecell_nc1(ifile,j));
3696%         filename_nc1([end-2:end])=[]; % remove '.nc'     
3697%         if test_grid && ~isempty(nbslice_grid)       
3698%             num1_grid=mod(num1(ifile)-1,nbslice_grid)+1;
3699%             gridname=[filebase '_' name_generator(gridname,num1_grid,1,'.grid','_i')];
3700%             if ~exist(gridname,'file')
3701%                 msgbox_uvmat('ERROR',['missing grid file ' gridname])
3702%                 return
3703%             end
3704%         end     
3705%         test_mask=get(handles.get_mask_civ2,'Value');
3706%         if test_mask==0 
3707%             maskname='noFile use default';
3708%             maskflag='n';
3709%         else
3710%             maskdispl=get(handles.mask_civ2,'String');
3711%             maskbase=[filebase '_' maskdispl]; %
3712%             nbslice_mask=str2num(maskdispl(1:end-4)); %
3713%             num1_mask=mod(num1(ifile)-1,nbslice_mask)+1;
3714%             maskname =name_generator(maskbase,num1_mask,1,'.png','_i');
3715%             if ~exist(maskname,'file')
3716%                 maskflag='y';
3717%             else
3718%                 maskname='noFile use default';
3719%                 maskflag='n';
3720%             end
3721%         end
3722%     
3723%               textcmx={'##############   CMX file';...
3724%               ['FirstImage ' filename_ima_2];...
3725%               ['LastImage  ' filename_ima_3];...
3726%               'XX' ;...
3727%               ['Mask ' maskflag];...
3728%               ['MaskName ' maskname];...
3729%               ['ImageSize ' npx ' ' npy];...   
3730%               ['CorrelationBoxesSize ' ibx ' ' iby];...
3731%               ['SearchBoxeSize ' ibx ' ' iby];...
3732%               ['RO ' rho];...
3733%               ['GridSpacing ' dx ' ' dy];...
3734%               'XX 1.0';...
3735%               ['Dt_TO ' Dt ' ' T0];...
3736%               ['PixCmXY ' pxcmx ' ' pxcmy];...
3737%               'XX 1';...
3738%               ['ShiftXY 0 0'];...
3739%               ['Grid ' gridflag];...
3740%               ['GridName ' gridname];...
3741%               'XX 85';...
3742%               'XX 1.0';...
3743%               'XX 1.0';...
3744%               'Hart 1';...
3745%               ['DecimalShift ' decimal];...
3746%               ['Deformation ' deformation];...
3747%               'CorrelationMin 0';...
3748%               'IntensityMin 0';...
3749%               'SeuilImage n';...
3750%               'SeuilImageValues 0 4096';...
3751%               ['ImageToUse ' term_a ' ' term_b];... % VERIFIER ?
3752%               ['ImageUsedBefore ' filename_nc1]};
3753%         textout=char(textcmx);
3754%         dlmwrite(filename_cmx,textout,'');
3755%         s=-1; 
3756%         display(['!' civ2Bin ' -f ' filename_cmx ' > ' namelog ])
3757%         eval(['!' civ2Bin ' -f ' filename_cmx ' > ' namelog ]);   
3758%     
3759% %             if sge%dispatch computation on the cluster using interactive queue
3760% %             [s,w] = unix(['qrsh -q fast.q ' civ2Bin ' -f ' filename_cmx ' > ' namelog ' 2>&1']);
3761% %         end     
3762% %         if s~=0
3763% %             eval(['!' civ2Bin ' -f ' filename_cmx ' > ' namelog]);
3764% %             ['!' civ2Bin ' -f ' filename_cmx ' > ' namelog]
3765% %         end                     
3766%     end
3767% end
3768% % close(h)
3769
3770%------------------------------------------------------------------------
3771% --- PATCH
3772function cmd_PATCH=RUN_PATCH(filename_nc,nx_patch,ny_patch,rho_patch,subdomain_patch,thresh_value,test_interp,PatchBin)
3773%------------------------------------------------------------------------
3774%global patchBin patch_newBin
3775
3776
3777        namelog=[filename_nc([1:end-3]) '_patch.log'];
3778        if test_interp==0
3779            cmd_PATCH=[PatchBin ' -f ' filename_nc ' -m ' nx_patch  ' -n ' ny_patch ' -ro ' rho_patch ' -nopt ' subdomain_patch ...
3780            '  > ' namelog ' 2>&1']; % redirect standard output to the log file
3781         else %nouveau programme patch
3782             cmd_PATCH=[PatchBin ' -f ' filename_nc ' -m ' nx_patch  ' -n ' ny_patch ' -ro ' rho_patch ...
3783                ' -max ' thresh_value ' -nopt ' subdomain_patch  '  > ' namelog ' 2>&1']; % redirect standard output to the log file
3784        end
3785
3786%------------------------------------------------------------------------
3787% --- STEREO Interp
3788function cmd=RUN_STINTERP(stinterpBin,filename_A_nc,filename_B_nc,filename_nc,nx_patch,ny_patch,rho_patch,subdomain_patch,thresh_value,xmlA,xmlB)
3789%------------------------------------------------------------------------
3790namelog=[filename_nc([1:end-3]) '_stinterp.log'];
3791cmd=[stinterpBin ' -f1 ' filename_A_nc  ' -f2 ' filename_B_nc ' -f  ' filename_nc ...
3792    ' -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
3793
3794%------------------------------------------------------------------------
3795% --- Executes on button press in CIV1.
3796function CIV1_Callback(hObject, eventdata, handles)
3797%------------------------------------------------------------------------
3798val=get(handles.CIV1,'Value');
3799if isequal(val,1)
3800    enable_civ1(handles,'on')
3801    enable_pair1(handles,'on')
3802else
3803    enable_civ1(handles,'off')
3804end
3805find_netcpair_civ1(hObject, eventdata, handles);
3806
3807%------------------------------------------------------------------------
3808% --- Executes on button press in FIX1.
3809function FIX1_Callback(hObject, eventdata, handles)
3810%------------------------------------------------------------------------
3811enable_fix1(handles,get(handles.FIX1,'Value'))
3812
3813%------------------------------------------------------------------------
3814% --- Executes on button press in PATCH1.
3815function PATCH1_Callback(hObject, eventdata, handles)
3816%------------------------------------------------------------------------
3817if get(handles.PATCH1,'Value')==1
3818    enable_patch1(handles)
3819else
3820    desable_patch1(handles)
3821end
3822
3823%------------------------------------------------------------------------
3824% --- Executes on button press in CIV2.
3825function CIV2_Callback(hObject, eventdata, handles)
3826%------------------------------------------------------------------------
3827state=get(handles.CIV2,'Value');
3828enable_civ2(handles,state)
3829if state
3830    find_netcpair_civ2(hObject, eventdata, handles)
3831    enable_pair1(handles,'on')
3832end
3833
3834%------------------------------------------------------------------------
3835% --- Executes on button press in FIX2.
3836function FIX2_Callback(hObject, eventdata, handles)
3837%------------------------------------------------------------------------
3838if get(handles.FIX2,'Value')==1
3839    enable_fix2(handles)
3840    if get(handles.CIV2,'Value')==0
3841        find_netcpair_civ2(hObject, eventdata, handles) % select the available netcdf files
3842    end
3843else
3844    desable_fix2(handles)
3845end
3846
3847%------------------------------------------------------------------------
3848% --- Executes on button press in PATCH2.
3849function PATCH2_Callback(hObject, eventdata, handles)
3850%------------------------------------------------------------------------
3851if get(handles.PATCH2,'Value')==1
3852    enable_patch2(handles)
3853    if get(handles.CIV2,'Value')==0
3854        find_netcpair_civ2(hObject, eventdata, handles) % select the available netcdf files
3855    end
3856else
3857    desable_patch2(handles)
3858end
3859
3860%------------------------------------------------------------------------
3861function first_i_Callback(hObject, eventdata, handles)
3862%------------------------------------------------------------------------
3863% last_i_Callback(hObject, eventdata, handles)
3864first_i=str2num(get(handles.first_i,'String'));
3865% last_i=str2num(get(handles.last_i,'String'));
3866% ref_i=ceil((first_i+last_i)/2);
3867set(handles.ref_i,'String', num2str(first_i))% reference index for pair dt = first index
3868set(handles.ref_i_civ2,'String', num2str(first_i))% reference index for pair dt = first index
3869ref_i_Callback(hObject, eventdata, handles)%refresh dispaly of dt for pairs (in case of non constant dt)
3870
3871%------------------------------------------------------------------------
3872function first_j_Callback(hObject, eventdata, handles)
3873%------------------------------------------------------------------------
3874first_j=str2num(get(handles.first_j,'String'));
3875set(handles.ref_j,'String', num2str(first_j))% reference index for pair dt = first index
3876ref_j_Callback(hObject, eventdata, handles)%refresh dispaly of dt for pairs (in case of non constant dt)
3877
3878%------------------------------------------------------------------------
3879% --- Executes on button press in calcul_search: determine the search range isx,isy
3880function calcul_search_Callback(hObject, eventdata, handles)
3881%------------------------------------------------------------------------
3882%determine pair numbers
3883list_pair=get(handles.list_pair_civ1,'String');%get the menu of image pairs
3884index=get(handles.list_pair_civ1,'Value');
3885displ_num=get(handles.list_pair_civ1,'UserData');
3886time=get(handles.RootName,'UserData'); %get the set of times
3887pxcm_xy=get(handles.calcul_search,'UserData');
3888pxcmx=pxcm_xy(1);
3889pxcmy=pxcm_xy(2);
3890mode_list=get(handles.mode,'String');
3891mode_value=get(handles.mode,'Value');
3892mode=mode_list{mode_value};
3893if isequal (mode, 'series(Di)' )
3894    ref_i=str2num(get(handles.ref_i,'String'));
3895    num1=ref_i-floor(index/2);%  first image numbers
3896    num2=ref_i+ceil(index/2);
3897    num_a=1;
3898    num_b=1;
3899elseif isequal (mode, 'series(Dj)')
3900    num1=1;
3901    num2=1;
3902    ref_j=str2num(get(handles.ref_j,'String'));
3903    num_a=ref_j-floor(index/2);%  first image numbers
3904    num_b=ref_j+ceil(index/2);
3905elseif isequal(mode,'pair j1-j2') %case of bursts (png_old or png_2D)
3906    ref_i=str2num(get(handles.ref_i,'String'));
3907    num1=ref_i;
3908    num2=ref_i;
3909    num_a=displ_num(1,index);
3910    num_b=displ_num(2,index);
3911end
3912dt=time(num2,num_b)-time(num1,num_a);
3913ibx=str2num(get(handles.ibx,'String'));
3914iby=str2num(get(handles.iby,'String'));
3915umin=dt*pxcmx*str2num(get(handles.umin,'String'));
3916umax=dt*pxcmx*str2num(get(handles.umax,'String'));
3917vmin=dt*pxcmy*str2num(get(handles.vmin,'String'));
3918vmax=dt*pxcmy*str2num(get(handles.vmax,'String'));
3919shiftx=round((umin+umax)/2);
3920shifty=round((vmin+vmax)/2);
3921isx=(umax+2-shiftx)*2+ibx;
3922isx=2*ceil(isx/2)+1;
3923isy=(vmax+2-shifty)*2+iby;
3924isy=2*ceil(isy/2)+1;
3925set(handles.shiftx,'String',num2str(shiftx));
3926set(handles.shifty,'String',num2str(shifty));
3927set(handles.isx,'String',num2str(isx));
3928set(handles.isy,'String',num2str(isy));
3929
3930%------------------------------------------------------------------------
3931% --- Executes on carriage return on the subdir civ1 edit window
3932function subdir_civ1_Callback(hObject, eventdata, handles)
3933%------------------------------------------------------------------------
3934subdir=get(handles.subdir_civ1,'String');
3935set(handles.subdir_civ2,'String',subdir);
3936if get(handles.CIV1,'Value')==0
3937    find_netcpair_civ1(hObject, eventdata, handles); %update the list of available pairs from netcdf files in the new directory
3938end
3939
3940%------------------------------------------------------------------------
3941% --- Executes on carriage return on the subdir civ1 edit window
3942function subdir_civ2_Callback(hObject, eventdata, handles)
3943%------------------------------------------------------------------------
3944%update the list of available pairs from netcdf files in the new directory
3945if get(handles.CIV2,'Value')==0 & get(handles.CIV1,'Value')==0 & get(handles.FIX1,'Value')==0 & get(handles.PATCH1,'Value')==0
3946    find_netcpair_civ2(hObject, eventdata, handles);
3947end
3948
3949%------------------------------------------------------------------------
3950% --- Executes on button press in get_mask_civ1.
3951function get_mask_civ1_Callback(hObject, eventdata, handles)
3952%------------------------------------------------------------------------
3953maskval=get(handles.get_mask_civ1,'Value');
3954if isequal(maskval,0)
3955    set(handles.mask_civ1,'String','')
3956else
3957    mask_displ='no mask'; %default
3958    filebase=get(handles.RootName,'String');
3959    [ nbslice_mask, flag_mask]=get_mask(filebase,handles);
3960    if isequal(flag_mask,1)
3961          mask_displ=[num2str(nbslice_mask) 'mask'];
3962    elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3963            common_path=fileparts(filebase);
3964            filebase_a=fullfile(common_path,get(handles.RootName_1,'String'));
3965            [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3966            if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice_mask)
3967                mask_displ='no mask';
3968            end
3969    end
3970    if isequal(mask_displ,'no mask')
3971        [FileName, PathName, filterindex] = uigetfile( ...
3972               {'*.png', ' (*.png)';
3973                '*.png',  '.png files '; ...
3974                '*.*', 'All Files (*.*)'}, ...
3975                'Pick a mask file *.png',filebase);
3976        mask_displ=fullfile(PathName,FileName); 
3977        if ~exist(mask_displ,'file')
3978            mask_displ='no mask';
3979        end
3980    end
3981    if isequal(mask_displ,'no mask')
3982        set(handles.get_mask_civ1,'Value',0)
3983        set(handles.get_mask_fix1,'Value',0)
3984        set(handles.get_mask_civ2,'Value',0)
3985        set(handles.get_mask_fix2,'Value',0)
3986    else
3987        set(handles.get_mask_fix1,'Value',1)
3988        set(handles.get_mask_fix2,'Value',1)
3989    end
3990    set(handles.mask_civ1,'String',mask_displ)
3991    set(handles.mask_fix1,'String',mask_displ)
3992    set(handles.mask_civ2,'String',mask_displ)
3993    set(handles.mask_fix2,'String',mask_displ)
3994end
3995set(handles.mask_civ2,'Value',maskval)
3996
3997%------------------------------------------------------------------------
3998% --- Executes on button press in get_mask_fix1.
3999function get_mask_fix1_Callback(hObject, eventdata, handles)
4000%------------------------------------------------------------------------
4001maskval=get(handles.get_mask_fix1,'Value');
4002if isequal(maskval,0)
4003    set(handles.mask_fix1,'String','')
4004else
4005mask_displ='no mask'; %default
4006filebase=get(handles.RootName,'String');
4007[nbslice, flag_mask]=get_mask(filebase,handles);
4008if isequal(flag_mask,1)
4009      mask_displ=[num2str(nbslice) 'mask'];
4010elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
4011        filebase_a=get(handles.RootName_1,'String');
4012        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
4013        if isequal(flag_mask_a,0) | ~isequal(nbslice_a,nbslice)
4014            mask_displ='no mask';
4015        end
4016end
4017if isequal(mask_displ,'no mask')
4018    [FileName, PathName, filterindex] = uigetfile( ...
4019           {'*.png', ' (*.png)';
4020            '*.png',  '.png files '; ...
4021            '*.*', 'All Files (*.*)'}, ...
4022            'Pick a mask file *.png',filebase);
4023    mask_displ=fullfile(PathName,FileName); 
4024    if ~exist(mask_displ,'file')
4025        mask_displ='no mask';
4026    end
4027end
4028if isequal(mask_displ,'no mask')
4029    set(handles.get_mask_fix1,'Value',0)
4030    set(handles.get_mask_civ2,'Value',0)
4031    set(handles.get_mask_fix2,'Value',0)
4032else
4033    %set(handles.get_mask_civ2,'Value',1)
4034    set(handles.get_mask_fix2,'Value',1)
4035end
4036set(handles.mask_fix1,'String',mask_displ)
4037set(handles.mask_civ2,'String',mask_displ)
4038set(handles.mask_fix2,'String',mask_displ)
4039end
4040
4041%------------------------------------------------------------------------
4042% --- Executes on button press in get_mask_civ2.
4043function get_mask_civ2_Callback(hObject, eventdata, handles)
4044%------------------------------------------------------------------------
4045maskval=get(handles.get_mask_civ2,'Value');
4046if isequal(maskval,0)
4047    set(handles.mask_civ2,'String','')
4048else
4049mask_displ='no mask'; %default
4050filebase=get(handles.RootName,'String');
4051[nbslice, flag_mask]=get_mask(filebase,handles);
4052if isequal(flag_mask,1)
4053      mask_displ=[num2str(nbslice) 'mask'];
4054elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
4055        filebase_a=get(handles.RootName_1,'String');
4056        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
4057        if isequal(flag_mask_a,0) | ~isequal(nbslice_a,nbslice)
4058            mask_displ='no mask';
4059        end
4060end
4061if isequal(mask_displ,'no mask')
4062    [FileName, PathName, filterindex] = uigetfile( ...
4063           {'*.png', ' (*.png)';
4064            '*.png',  '.png files '; ...
4065            '*.*', 'All Files (*.*)'}, ...
4066            'Pick a mask file *.png',filebase);
4067    mask_displ=fullfile(PathName,FileName);
4068    if ~exist(mask_displ,'file')
4069        mask_displ='no mask';
4070    end
4071end
4072if isequal(mask_displ,'no mask')
4073    set(handles.get_mask_civ2,'Value',0)
4074    set(handles.get_mask_fix2,'Value',0)
4075else
4076    set(handles.get_mask_fix2,'Value',1)
4077end
4078set(handles.mask_civ2,'String',mask_displ)
4079set(handles.mask_fix2,'String',mask_displ)
4080end
4081
4082%------------------------------------------------------------------------
4083% --- Executes on button press in get_mask_fix2.
4084function get_mask_fix2_Callback(hObject, eventdata, handles)
4085%------------------------------------------------------------------------
4086maskval=get(handles.get_mask_fix2,'Value');
4087if isequal(maskval,0)
4088    set(handles.mask_fix2,'String','')
4089else
4090    mask_displ='no mask'; %default
4091    filebase=get(handles.RootName,'String');
4092    [nbslice, flag_mask]=get_mask(filebase,handles);
4093    if isequal(flag_mask,1)
4094          mask_displ=[num2str(nbslice) 'mask'];
4095    elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
4096            filebase_a=get(handles.RootName_1,'String');
4097            [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
4098            if isequal(flag_mask_a,0) | ~isequal(nbslice_a,nbslice)
4099                mask_displ='no mask';
4100            end
4101    end
4102    if isequal(mask_displ,'no mask')
4103        [FileName, PathName, filterindex] = uigetfile( ...
4104               {'*.png', ' (*.png)';
4105                '*.png',  '.png files '; ...
4106                '*.*', 'All Files (*.*)'}, ...
4107                'Pick a mask file *.png',filebase);
4108        mask_displ=fullfile(PathName,FileName); 
4109        if ~exist(mask_displ,'file')
4110            mask_displ='no mask';
4111        end
4112    end
4113    if isequal(mask_displ,'no mask')
4114        set(handles.get_mask_fix2,'Value',0)
4115    end
4116    set(handles.mask_fix2,'String',mask_displ)
4117end
4118
4119%------------------------------------------------------------------------
4120% --- function called to look for mask files
4121function [nbslice, flag_mask]=get_mask(filebase,handles)
4122%------------------------------------------------------------------------
4123%detect mask files, images with appropriate file base
4124%[filebase '_' xx 'mask'], xx=nbslice
4125%flag_mask=1 indicates detection
4126
4127flag_mask=0;%default
4128nbslice=1;
4129
4130% subdir=get(handles.subdir_civ1,'String');
4131[Path,Name]=fileparts(filebase);
4132if ~isdir(Path)
4133    msgbox_uvmat('ERROR','no path for input files')
4134    return
4135end
4136currentdir=pwd;
4137cd(Path);%move in the dir of the root name filebase
4138maskfiles=dir([Name '_*mask_*.png']);%look for mask files
4139cd(currentdir);%come back to the current working directory
4140if ~isempty(maskfiles)
4141%     msgbox_uvmat('ERROR','no mask available, to create it use Tools/Make mask in the upper menu bar of uvmat')
4142% else
4143    flag_mask=1;
4144    maskname=maskfiles(1).name;% take the first mask file in the list
4145    [Path2,Name,ext]=fileparts(maskname);
4146    Namedouble=double(Name);
4147    val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
4148    ind_mask=findstr('mask',Name);
4149    i=ind_mask-1;
4150    while val(i)==0 & i>0
4151       i=i-1;
4152    end
4153    nbslice=str2num(Name(i+1:ind_mask-1));
4154    if ~isequal(nbslice,[]) & Name(i)=='_'
4155          flag_mask=1;
4156    else
4157          msgbox_uvmat('ERROR',['bad mask file ' Name ext ' found in ' Path2])
4158          return
4159          nbslice=1;
4160    end
4161end   
4162
4163%------------------------------------------------------------------------
4164% --- function called to look for grid files
4165function [nbslice, flag_mask]=get_grid(filebase,handles)
4166%------------------------------------------------------------------------
4167flag_mask=0;%default
4168nbslice=1;
4169[Path,Name]=fileparts(filebase);
4170currentdir=pwd;
4171cd(Path);%move in the dir of the root name filebase
4172maskfiles=dir([Name '_*grid_*.grid']);%look for mask files
4173cd(currentdir);%come back to the current working directory
4174if ~isempty(maskfiles)
4175    flag_mask=1;
4176    maskname=maskfiles(1).name;% take the first mask file in the list
4177    [Path2,Name,ext]=fileparts(maskname);
4178    Namedouble=double(Name);
4179    val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
4180    ind_mask=findstr('grid',Name);
4181    i=ind_mask-1;
4182    while val(i)==0 & i>0
4183       i=i-1;
4184    end
4185    nbslice=str2num(Name(i+1:ind_mask-1));
4186    if ~isequal(nbslice,[]) & Name(i)=='_'
4187          flag_mask=1;
4188    else
4189          msgbox_uvmat('ERROR',['bad grid file ' Name ext ' found in ' Path2])
4190          return
4191          nbslice=1;
4192    end
4193end
4194
4195%------------------------------------------------------------------------
4196% --- transform numbers to letters
4197function str=num2stra(num,nom_type);
4198%------------------------------------------------------------------------
4199if isequal(nom_type,'png_old') | isequal(nom_type,'netc_old') |isequal(nom_type,'raw_SMD')
4200    str=char(96+num);
4201elseif isequal(nom_type,'_i')|isequal(nom_type,'_i1-i2')...
4202        |isequal(nom_type,'ima_num')| isequal(nom_type,'avi')| isequal(nom_type,'none')
4203    str='';
4204else
4205    str=num2str(num);
4206end
4207
4208%------------------------------------------------------------------------
4209function mask_civ1_Callback(hObject, eventdata, handles)
4210%------------------------------------------------------------------------
4211set(handles.mask_civ1,'UserData',[])
4212set(handles.mask_civ1,'String','')
4213
4214%------------------------------------------------------------------------
4215function mask_civ2_Callback(hObject, eventdata, handles)
4216%------------------------------------------------------------------------
4217set(handles.mask_civ2,'UserData',[])
4218set(handles.mask_civ2,'String','')
4219
4220%------------------------------------------------------------------------
4221function mask_fix1_Callback(hObject, eventdata, handles)
4222%------------------------------------------------------------------------
4223set(handles.mask_fix1,'UserData',[])
4224set(handles.mask_fix1,'String','')
4225
4226%------------------------------------------------------------------------
4227function mask_fix2_Callback(hObject, eventdata, handles)
4228%------------------------------------------------------------------------
4229set(handles.mask_fix2,'UserData',[])
4230set(handles.mask_fix2,'String','')
4231
4232%------------------------------------------------------------------------
4233% --- Executes on button press in list_subdir_civ1.
4234function list_subdir_civ1_Callback(hObject, eventdata, handles)
4235%------------------------------------------------------------------------
4236list_subdir_civ1=get(handles.list_subdir_civ1,'String');
4237val=get(handles.list_subdir_civ1,'Value');
4238if val>1
4239    subdir=list_subdir_civ1{val};
4240    set(handles.subdir_civ1,'String',subdir);
4241    set(handles.list_subdir_civ1,'Value',1);
4242end
4243
4244%------------------------------------------------------------------------
4245% --- Executes on button press in list_subdir_civ2.
4246function list_subdir_civ2_Callback(hObject, eventdata, handles)
4247%------------------------------------------------------------------------
4248list_subdir_civ2=get(handles.list_subdir_civ2,'String');
4249val=get(handles.list_subdir_civ2,'Value');
4250if val>1
4251    subdir=list_subdir_civ2{val};
4252    set(handles.subdir_civ2,'String',subdir);
4253    set(handles.list_subdir_civ2,'Value',1);
4254end
4255
4256%------------------------------------------------------------------------
4257% --- Executes on button press in browse_gridciv1.
4258function browse_gridciv1_Callback(hObject, eventdata, handles)
4259%------------------------------------------------------------------------
4260value=get(handles.browse_gridciv1,'Value');
4261testgrid=0;
4262if value
4263        filebase=get(handles.RootName,'String');
4264    [nbslice, flag_grid]=get_grid(filebase,handles);
4265    if isequal(flag_grid,1)
4266       filegrid=[num2str(nbslice) 'grid'];
4267       testgrid=1;
4268    else   
4269        [FileName, PathName, filterindex] = uigetfile( ...
4270               {'*.grid', ' (*.grid)';
4271                '*.grid',  '.grid files '; ...
4272                '*.*', 'All Files (*.*)'}, ...
4273                'Pick a file',filebase);
4274        filegrid=fullfile(PathName,FileName);
4275        if ~(isempty(FileName)||isempty(PathName)||isequal(FileName,0)||~exist(filegrid,'file'))
4276            testgrid=1;
4277        end
4278    end       
4279end
4280if testgrid
4281        set(handles.browse_gridciv2,'Value',1)
4282        set(handles.get_gridpatch1,'Value',1)
4283        set(handles.get_gridpatch2,'Value',1)
4284        set(handles.dx_civ1,'Visible','off');
4285        set(handles.dy_civ1,'Visible','off');
4286        set(handles.dx_civ2,'Visible','off');
4287        set(handles.dy_civ2,'Visible','off');
4288        set(handles.grid_civ1,'String',filegrid)     
4289        set(handles.grid_patch1,'String',filegrid)
4290        set(handles.grid_civ2,'String',filegrid)
4291        set(handles.grid_patch2,'String',filegrid)
4292    else
4293        set(handles.browse_gridciv1,'Value',0);
4294        set(handles.browse_gridciv2,'Value',0);
4295        set(handles.get_gridpatch1,'Value',0)
4296        set(handles.get_gridpatch2,'Value',0)
4297        set(handles.dx_civ1,'Visible','on');
4298        set(handles.dy_civ1,'Visible','on');
4299        set(handles.dx_civ2,'Visible','on');
4300        set(handles.dy_civ2,'Visible','on');
4301        set(handles.grid_civ1,'String','')     
4302        set(handles.grid_patch1,'String','')
4303        set(handles.grid_civ2,'String','')
4304        set(handles.grid_patch2,'String','')
4305end
4306
4307%------------------------------------------------------------------------
4308% --- Executes on button press in browse_gridciv1.
4309function browse_gridciv2_Callback(hObject, eventdata, handles)
4310%------------------------------------------------------------------------
4311value=get(handles.browse_gridciv2,'Value');
4312if value
4313        filebase=get(handles.RootName,'String');
4314    [nbslice, flag_grid]=get_grid(filebase,handles);
4315    if isequal(flag_grid,1)
4316        mask_displ=[num2str(nbslice) 'grid'];
4317        set(handles.grid_civ2,'String',mask_displ)
4318        set(handles.dx_civ2,'Visible','off');
4319        set(handles.dy_civ2,'Visible','off');
4320    else   
4321        [FileName, PathName, filterindex] = uigetfile( ...
4322               {'*.grid', ' (*.grid)';
4323                '*.grid',  '.grid files '; ...
4324                '*.*', 'All Files (*.*)'}, ...
4325                'Pick a file',filebase);
4326        filegrid=fullfile(PathName,FileName);
4327        if isempty(FileName)|isempty(PathName)|isequal(FileName,0)|~exist(filegrid,'file')
4328            set(handles.browse_gridciv2,'Value',0);
4329            set(handles.grid_civ2,'string','');
4330            set(handles.dx_civ2,'Visible','on');
4331            set(handles.dy_civ2,'Visible','on');
4332            set(handles.grid_civ2,'string','');
4333        else
4334            set(handles.grid_civ2,'string',filegrid);
4335            set(handles.dx_civ2,'Visible','off');
4336            set(handles.dy_civ2,'Visible','off');
4337            set(handles.grid_civ2,'string',filegrid);
4338        end
4339    end
4340else
4341    set(handles.grid_civ2,'string','');
4342        set(handles.dx_civ2,'Visible','on');
4343        set(handles.dy_civ2,'Visible','on');
4344        set(handles.grid_civ2,'string','');
4345end
4346
4347% % --- Executes on button press in browse_gridciv2.
4348% function browse_gridciv2_Callback(hObject, eventdata, handles)
4349%
4350% filebase=get(handles.RootName,'String');
4351% [FileName, PathName, filterindex] = uigetfile( ...
4352%        {'*.grid', ' (*.grid)';
4353%         '*.grid',  '.grid files '; ...
4354%         '*.*', 'All Files (*.*)'}, ...
4355%         'Pick a file',filebase);
4356% filegrid=fullfile(PathName,FileName);
4357% set(handles.grid_civ2,'string',filegrid);
4358% set(handles.dx_civ2,'String',' ');
4359% set(handles.dy_civ2,'String',' ');
4360% % set(handles.grid_patch2,'string',filegrid);
4361
4362% --- Executes on button press in get_gridpatch1.
4363function get_gridpatch1_Callback(hObject, eventdata, handles)
4364% hObject    handle to get_gridpatch1 (see GCBO)
4365% eventdata  reserved - to be defined in a future version of MATLAB
4366% handles    structure with handles and user data (see GUIDATA)
4367
4368filebase=get(handles.RootName,'String');
4369[FileName, PathName, filterindex] = uigetfile( ...
4370       {'*.grid', ' (*.grid)';
4371        '*.grid',  '.grid files '; ...
4372        '*.*', 'All Files (*.*)'}, ...
4373        'Pick a file',filebase);
4374filegrid=fullfile(PathName,FileName);
4375set(handles.grid_patch1,'string',filegrid);
4376% set(handles.grid_patch2,'string',filegrid
4377
4378%------------------------------------------------------------------------
4379% --- Executes on button press in get_gridpatch2.
4380function get_gridpatch2_Callback(hObject, eventdata, handles)
4381%------------------------------------------------------------------------
4382
4383%------------------------------------------------------------------------
4384function enable_civ1(handles,state)
4385%------------------------------------------------------------------------
4386if isequal(state,0)
4387    state='off';
4388end
4389if isequal(state,1)
4390    state='on';
4391end
4392if isequal(state,'on')
4393    set(handles.frame_civ1,'BackgroundColor',[1 1 0])
4394    set(handles.frame_para_civ1,'BackgroundColor',[1 1 0])
4395    set(handles.frame_grid_civ1,'BackgroundColor',[1 1 0])
4396else
4397    set(handles.frame_civ1,'BackgroundColor',[0.831 0.816 0.784])
4398    set(handles.frame_para_civ1,'BackgroundColor',[0.831 0.816 0.784])
4399    set(handles.frame_grid_civ1,'BackgroundColor',[0.831 0.816 0.784])
4400end
4401set(handles.ibx,'Visible',state)
4402set(handles.iby,'Visible',state)
4403set(handles.isx,'Visible',state)
4404set(handles.isy,'Visible',state)
4405set(handles.shiftx,'Visible',state)
4406set(handles.shifty,'Visible',state)
4407set(handles.rho,'Visible',state)
4408set(handles.dx_civ1,'Visible',state)
4409set(handles.dy_civ1,'Visible',state)
4410set(handles.calcul_search,'Visible',state)
4411set(handles.u_text,'Visible',state)
4412set(handles.v_text,'Visible',state)
4413set(handles.min,'Visible',state)
4414set(handles.max,'Visible',state)
4415set(handles.umin,'Visible',state)
4416set(handles.umax,'Visible',state)
4417set(handles.vmin,'Visible',state)
4418set(handles.vmax,'Visible',state)
4419set(handles.grid_civ1,'Visible',state)
4420set(handles.mask_civ1,'Visible',state)
4421set(handles.browse_gridciv1,'Visible',state)
4422set(handles.get_mask_civ1,'Visible',state)
4423set(handles.parameters,'Visible',state)
4424set(handles.grid,'Visible',state)
4425set(handles.dx_civ1,'Visible',state)
4426set(handles.dy_civ1,'Visible',state)
4427set(handles.ImaThreshold,'Visible',state)
4428if isequal(state,'off')
4429    set(handles.MinIma,'Visible','off')
4430    set(handles.MaxIma,'Visible','off')
4431    set(handles.ImaThreshold,'Value',0)
4432end
4433set(handles.dx_civ1_title,'Visible',state)
4434set(handles.dy_civ1_title,'Visible',state)
4435set(handles.ImaThreshold_title,'Visible',state)
4436set(handles.ib_title,'Visible',state)
4437set(handles.is_title,'Visible',state)
4438set(handles.shift_title,'Visible',state)
4439set(handles.rho_title,'Visible',state)
4440
4441%------------------------------------------------------------------------
4442function enable_fix1(handles,state)
4443%------------------------------------------------------------------------
4444if isequal(state,0)
4445    state='off';
4446end
4447if isequal(state,1)
4448    state='on';
4449end
4450if isequal(state,'on')
4451    set(handles.frame_fix1,'BackgroundColor',[1 1 0])
4452else
4453    set(handles.frame_fix1,'BackgroundColor',[0.7 0.7 0.7])
4454end
4455set(handles.REMOVE,'Visible',state)
4456set(handles.vec_Fmin2,'Visible',state)
4457set(handles.vec_F2,'Visible',state)
4458set(handles.vec_F3,'Visible',state)
4459set(handles.thresh_vecC,'Visible',state)
4460set(handles.thresh_vecC_title,'Visible',state)
4461set(handles.thresh_vel,'Visible',state)
4462set(handles.thresh_vel_text,'Visible',state)
4463set(handles.mask_fix1,'Visible',state)
4464set(handles.get_mask_fix1,'Visible',state)
4465set(handles.get_ref_fix1,'Visible',state)
4466set(handles.ref_fix1,'Visible',state)
4467set(handles.inf_sup1,'Visible',state)
4468set(handles.field_ref1,'Visible',state)
4469
4470%------------------------------------------------------------------------
4471function enable_patch1(handles)
4472%------------------------------------------------------------------------
4473global patch_newBin
4474set(handles.frame_patch1,'BackgroundColor',[1 1 0])
4475set(handles.rho_patch1,'Visible','on')
4476set(handles.rho_text1,'Visible','on')
4477set(handles.thresh_patch1,'Visible','on')
4478set(handles.thresh_text1,'Visible','on')
4479set(handles.subdomain_patch1,'Visible','on')
4480set(handles.subdomain_text1,'Visible','on')
4481set(handles.nx_patch1,'Visible','on')
4482set(handles.ny_patch1,'Visible','on')
4483set(handles.nx_patch1_title,'Visible','on')
4484set(handles.ny_patch1_title,'Visible','on')
4485if (~isequal(patch_newBin,[]) & ~isequal(patch_newBin,[]))
4486    set(handles.test_interp,'Visible','on');
4487end
4488set(handles.get_gridpatch1,'Visible','on')
4489set(handles.grid_patch1,'string','none');
4490set(handles.grid_patch1,'Visible','on')
4491
4492%------------------------------------------------------------------------
4493function desable_patch1(handles)
4494%------------------------------------------------------------------------
4495set(handles.frame_patch1,'BackgroundColor',[0.831 0.816 0.784])
4496set(handles.rho_patch1,'Visible','off')
4497set(handles.rho_text1,'Visible','off')
4498set(handles.thresh_patch1,'Visible','off')
4499set(handles.thresh_text1,'Visible','off')
4500set(handles.subdomain_patch1,'Visible','off')
4501set(handles.subdomain_text1,'Visible','off')
4502set(handles.nx_patch1,'Visible','off')
4503set(handles.ny_patch1,'Visible','off')
4504set(handles.nx_patch1_title,'Visible','off')
4505set(handles.ny_patch1_title,'Visible','off')
4506set(handles.test_interp,'Visible','off')
4507set(handles.get_gridpatch1,'Visible','off')
4508set(handles.grid_patch1,'Visible','off')
4509
4510%------------------------------------------------------------------------
4511function enable_civ2(handles,state)
4512%------------------------------------------------------------------------
4513if isequal(state,0)
4514    state='off';
4515end
4516if isequal(state,1)
4517    state='on';
4518end
4519if isequal(state,'on')
4520    set(handles.frame_civ2,'BackgroundColor',[1 1 0])
4521    set(handles.frame_para_civ2,'BackgroundColor',[1 1 0])
4522    set(handles.frame_grid_civ2,'BackgroundColor',[1 1 0])
4523    set(handles.frame_subdirciv2,'BackgroundColor',[1 1 0])
4524else
4525    set(handles.frame_civ2,'BackgroundColor',[0.831 0.816 0.784])
4526    set(handles.frame_para_civ2,'BackgroundColor',[0.831 0.816 0.784])
4527    set(handles.frame_grid_civ2,'BackgroundColor',[0.831 0.816 0.784])
4528    set(handles.frame_subdirciv2,'BackgroundColor',[0.831 0.816 0.784])
4529end
4530set(handles.ibx_civ2,'Visible',state)
4531set(handles.iby_civ2,'Visible',state)
4532set(handles.decimal,'Visible',state)
4533set(handles.deformation,'Visible',state)
4534set(handles.rho_civ2,'Visible',state)
4535set(handles.dx_civ2,'Visible',state)
4536set(handles.dy_civ2,'Visible',state)
4537set(handles.browse_gridciv2,'Visible',state)
4538set(handles.get_mask_civ2,'Visible',state)
4539set(handles.parameters,'Visible',state)
4540set(handles.grid,'Visible',state)
4541set(handles.parameters_text,'Visible',state)
4542set(handles.grid_text,'Visible',state)
4543set(handles.grid_civ2,'Visible',state)
4544set(handles.mask_civ2,'Visible',state)
4545set(handles.dx_civ2_title,'Visible',state)
4546set(handles.dy_civ2_title,'Visible',state)
4547set(handles.ibx_civ2_text,'Visible',state)
4548set(handles.rho_civ2_title,'Visible',state)
4549set(handles.ImaThreshold2,'Visible',state)
4550set(handles.ImaThreshold_title2,'Visible',state)
4551if isequal(state,'off')
4552    set(handles.MinIma2,'Visible','off')
4553    set(handles.MaxIma2,'Visible','off')
4554    set(handles.ImaThreshold2,'Value',0)
4555    if isequal(get(handles.FIX2,'Value'),0) & isequal(get(handles.PATCH2,'Value'),0)
4556        set(handles.list_pair_civ2,'Visible','off')
4557        set(handles.subdir_civ2,'Visible','off')
4558        set(handles.subdir_civ2_text,'Visible','off')
4559        set(handles.dt_unit_civ2,'Visible','off')
4560        set(handles.ref_i_civ2,'Visible','off')
4561        set(handles.i_ref_civ2_title,'Visible','off')
4562        set(handles.j_ref_civ2_title,'Visible','off')
4563        set(handles.ref_j_civ2,'Visible','off')
4564    end
4565else
4566    set(handles.list_pair_civ2,'Visible','on')
4567    set(handles.subdir_civ2,'Visible','on')
4568    set(handles.subdir_civ2_text,'Visible','on')
4569    set(handles.dt_unit_civ2,'Visible','on')
4570    set(handles.ref_i_civ2,'Visible','on')
4571    set(handles.i_ref_civ2_title,'Visible','on')
4572    set(handles.j_ref_civ2_title,'Visible','on')
4573    set(handles.ref_j_civ2,'Visible','on')
4574end
4575set(handles.rho_civ2_title,'Visible',state)
4576
4577%------------------------------------------------------------------------
4578function enable_fix2(handles)
4579%------------------------------------------------------------------------
4580set(handles.frame_fix2,'BackgroundColor',[1 1 0])
4581set(handles.REMOVE2,'Visible','on')
4582set(handles.vec_Fmin2_2,'Visible','on')
4583set(handles.vec_F4,'Visible','on')
4584set(handles.vec_F3_2,'Visible','on')
4585set(handles.thresh_vec2C,'Visible','on')
4586set(handles.thresh_vec2C_text,'Visible','on')
4587set(handles.thresh_vel2,'Visible','on')
4588set(handles.thresh_vel2_text,'Visible','on')
4589set(handles.mask_fix2,'Visible','on')
4590set(handles.get_mask_fix2,'Visible','on')
4591set(handles.list_pair_civ2,'Visible','on')
4592set(handles.subdir_civ2,'Visible','on')
4593set(handles.subdir_civ2_text,'Visible','on')
4594set(handles.get_ref_fix2,'Visible','on')
4595set(handles.ref_fix2,'Visible','on')
4596set(handles.inf_sup2,'Visible','on')
4597set(handles.field_ref2,'Visible','on')
4598
4599%------------------------------------------------------------------------
4600function desable_fix2(handles)
4601%------------------------------------------------------------------------
4602set(handles.frame_fix2,'BackgroundColor',[0.831 0.816 0.784])
4603set(handles.REMOVE2,'Visible','off')
4604set(handles.vec_Fmin2_2,'Visible','off')
4605set(handles.vec_F4,'Visible','off')
4606set(handles.vec_F3_2,'Visible','off')
4607set(handles.thresh_vec2C,'Visible','off')
4608set(handles.thresh_vec2C_text,'Visible','off')
4609set(handles.thresh_vel2,'Visible','off')
4610set(handles.thresh_vel2_text,'Visible','off')
4611set(handles.mask_fix2,'Visible','off')
4612set(handles.get_mask_fix2,'Visible','off')
4613set(handles.get_ref_fix2,'Visible','off')
4614set(handles.ref_fix2,'Visible','off')
4615set(handles.inf_sup2,'Visible','off')
4616set(handles.field_ref2,'Visible','off')
4617if isequal(get(handles.CIV2,'Value'),0) & isequal(get(handles.PATCH2,'Value'),0)
4618    set(handles.list_pair_civ2,'Visible','off')
4619    set(handles.subdir_civ2,'Visible','off')
4620    set(handles.subdir_civ2_text,'Visible','off')
4621end
4622
4623%------------------------------------------------------------------------
4624function enable_patch2(handles)
4625%------------------------------------------------------------------------
4626set(handles.frame_patch2,'BackgroundColor',[1 1 0])
4627%set(handles.rho_patch2,'Visible','on')
4628set(handles.rho_text2,'Visible','on')
4629set(handles.thresh_patch2,'Visible','on')
4630set(handles.thresh_text2,'Visible','on')
4631set(handles.subdomain_patch2,'Visible','on')
4632set(handles.subdomain_text2,'Visible','on')
4633set(handles.nx_patch2,'Visible','on')
4634set(handles.ny_patch2,'Visible','on')
4635set(handles.nx_patch2_title,'Visible','on')
4636set(handles.ny_patch2_title,'Visible','on')
4637set(handles.get_gridpatch2,'Visible','on')
4638set(handles.grid_patch2,'Visible','on')
4639set(handles.list_pair_civ2,'Visible','on')
4640set(handles.subdir_civ2,'Visible','on')
4641set(handles.subdir_civ2_text,'Visible','on')
4642
4643%------------------------------------------------------------------------
4644function desable_patch2(handles)
4645%------------------------------------------------------------------------
4646set(handles.frame_patch2,'BackgroundColor',[0.831 0.816 0.784])
4647set(handles.rho_patch2,'Visible','off')
4648set(handles.rho_text2,'Visible','off')
4649set(handles.thresh_patch2,'Visible','off')
4650set(handles.thresh_text2,'Visible','off')
4651set(handles.subdomain_patch2,'Visible','off')
4652set(handles.subdomain_text2,'Visible','off')
4653set(handles.nx_patch2,'Visible','off')
4654set(handles.ny_patch2,'Visible','off')
4655set(handles.nx_patch2_title,'Visible','off')
4656set(handles.ny_patch2_title,'Visible','off')
4657set(handles.get_gridpatch2,'Visible','off')
4658set(handles.grid_patch2,'Visible','off')
4659if isequal(get(handles.CIV2,'Value'),0) & isequal(get(handles.FIX2,'Value'),0)
4660    set(handles.list_pair_civ2,'Visible','off')
4661    set(handles.subdir_civ2,'Visible','off')
4662    set(handles.subdir_civ2_text,'Visible','off')
4663end
4664
4665%------------------------------------------------------------------------
4666function enable_pair1(handles,state)
4667%------------------------------------------------------------------------
4668set(handles.subdir_civ1,'Visible',state)
4669set(handles.list_subdir_civ1,'Visible',state)
4670set(handles.SUBDIR_CIV1_txt,'Visible',state)
4671set(handles.frame_subdirciv1,'Visible',state)
4672set(handles.list_pair_civ1,'Visible',state)
4673set(handles.PAIR_txt,'Visible',state)
4674%set(handles.dt_unit,'Visible',state)
4675set(handles.PAIR_frame,'Visible',state)
4676
4677%------------------------------------------------------------------------
4678% --- Read the parameters for civ1 on the interface
4679function par=read_param_civ1(handles,file_ima)
4680%------------------------------------------------------------------------
4681ibx_val=str2num(get(handles.ibx,'String'));
4682par.ibx=num2str(ibx_val);
4683iby_val=str2num(get(handles.iby,'String'));
4684par.iby=num2str(iby_val);
4685isx=get(handles.isx,'String');
4686if isempty(str2num(isx)), isx='41'; set(handles.isx,'String','41'), end; %default
4687if str2num(isx)<ibx_val+8,isx=num2str(ibx_val+8); set(handles.isx,'String',num2str(ibx_val+8)); end
4688isy=get(handles.isy,'String');
4689if isempty(str2num(isy)), isy='41'; set(handles.isy,'String','41'), end;%default
4690if str2num(isy)<iby_val+8,isy=num2str(iby_val+8); set(handles.isy,'String',num2str(iby_val+8)); end
4691par.isx=get(handles.isx,'String');
4692par.isy=get(handles.isy,'String');
4693par.shiftx=get(handles.shiftx,'String');
4694par.shifty=get(handles.shifty,'String');
4695if isempty(str2num(par.isx))
4696         par.isx='41';%default
4697         set(handles.isx,'String','41');
4698end
4699if isempty(str2num(par.isy))
4700         par.isy='41'; %default
4701         set(handles.isy,'String','41');
4702end
4703if isempty(str2num(par.shiftx))
4704         par.shiftx='0';%default
4705         set(handles.shiftx,'String','0');
4706end
4707if isempty(str2num(par.shifty))
4708         par.shifty='0'; %default
4709         set(handles.shifty,'String','0');
4710end
4711 par.rho=get(handles.rho,'String');
4712 par.dx=get(handles.dx_civ1,'String');
4713 par.dy=get(handles.dy_civ1,'String');
4714 if isequal(str2num(par.dx),[])
4715     if isempty(get(handles.grid_civ1,'String'));
4716         par.dx='0'; %just read by civ program, not used
4717     else
4718        par.dx='20';%default
4719        set(handles.dx_civ1,'String','20');
4720     end
4721 end
4722 if isequal(str2num(par.dy),[])
4723     if isempty(get(handles.grid_civ1,'String'));
4724         par.dy='0';%just read by civ program, not used
4725     else
4726        par.dy='20';%default
4727        set(handles.dy_civ1_title,'String','20');
4728     end
4729 end
4730    par.pxcmx='1'; %velocities are expressed in pixel dispalcement
4731     par.pxcmy='1';
4732%      end
4733 A=imread(file_ima);%read the first image to get the size
4734 sizim=size(A);
4735 par.npx=num2str(sizim(2));
4736 par.npy=num2str(sizim(1));
4737 time=get(handles.RootName,'UserData'); %get the set of times
4738 par.gridname=get(handles.grid_civ1,'String');
4739 par.gridflag='y';
4740 if isequal(par.gridname,'')| isempty(par.gridname)
4741     par.gridname='nogrid';
4742     par.gridflag='n';
4743 end
4744
4745%------------------------------------------------------------------------
4746function par=read_param_civ2(handles,file_ima)
4747%------------------------------------------------------------------------
4748par.ibx=get(handles.ibx_civ2,'String');
4749par.iby=get(handles.iby_civ2,'String');
4750par.rho=get(handles.rho_civ2,'String');
4751par.decimal=int2str(get(handles.decimal,'Value'));
4752par.deformation=int2str(get(handles.deformation,'Value'));
4753par.dx=get(handles.dx_civ2,'String');
4754par.dy=get(handles.dy_civ2,'String');
4755if isequal(str2num(par.dx),[])
4756     if isempty(get(handles.grid_civ2,'String'));
4757         par.dx='0'; %just read by civ program, not used
4758     else
4759        par.dx='20';%default
4760        set(handles.dx_civ2,'String','20');
4761     end
4762 end
4763 if isequal(str2num(par.dy),[])
4764     if isempty(get(handles.grid_civ2,'String'));
4765         par.dy='0';%just read by civ program, not used
4766     else
4767        par.dy='20';%default
4768        set(handles.dy_civ2,'String','20');
4769     end
4770 end
4771 par.pxcmx='1';
4772 par.pxcmy='1';
4773A=imread(file_ima);%read the first image to get the size
4774sizim=size(A);
4775par.npx=num2str(sizim(2));
4776par.npy=num2str(sizim(1));
4777time=get(handles.RootName,'UserData'); %get the set of times
4778par.gridname=get(handles.grid_civ2,'String');
4779par.gridflag='y';
4780if isequal(par.gridname,'')| isempty(par.gridname)
4781    par.gridname='nogrid';
4782    par.gridflag='n';
4783end
4784
4785%------------------------------------------------------------------------
4786% --- CIV1  CIV1  CIV1 CIV1
4787function cmd_CIV1=BATCH_CIV1(filename,namelog,par,handles,sparam)
4788%------------------------------------------------------------------------
4789%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4790
4791%changes : filename_cmx -> filename ( no extension )
4792
4793            if isequal(par.Dt,'0')
4794                par.Dt='1' ;%case of 'displacement' mode
4795            end         
4796
4797%     textcmx={'##############   CMX file';...
4798%     ['FirstImage ' par.filename_ima_a];...
4799%     ['LastImage  ' par.filename_ima_b];...
4800%     'XX' ;...
4801%     ['Mask ' par.maskflag] ;...
4802%     ['MaskName ' par.maskname];...
4803%     ['ImageSize ' par.npx ' ' par.npy];...   %VERIFIER CAS GENERAL ?
4804%     ['CorrelationBoxesSize ' par.ibx ' ' par.iby];...
4805%     ['SearchBoxeSize ' par.isx ' ' par.isy];...
4806%     ['RO ' par.rho];...
4807%     ['GridSpacing ' par.dx ' ' par.dy];...
4808%     'XX 1.0';...
4809%     ['Dt_TO ' par.Dt ' ' par.T0];...
4810%     ['PixCmXY ' par.pxcmx ' ' par.pxcmy];...
4811%     'XX 1';...
4812%     ['ShiftXY ' par.shiftx ' '  par.shifty];...
4813%     ['Grid ' par.gridflag];...
4814%     ['GridName ' par.gridname] ;...
4815%     'XX 85';...
4816%     'XX 1.0';...
4817%     'XX 1.0';...
4818%     'Hart 1';...
4819%     'DecimalShift 0';...
4820%     'Deformation 0';...
4821%     'CorrelationMin 0';...
4822%     'IntensityMin 0';...
4823%     'SeuilImage n';...
4824%     'SeuilImageValues 0 4096';...
4825%     ['ImageToUse ' par.term_a ' ' par.term_b];... % VERIFIER ?
4826%     'ImageUsedBefore null null'};
4827%
4828%             textout=char(textcmx);
4829
4830   fid=fopen([filename '.cmx'],'w');
4831fprintf(fid,['##############   CMX file' '\n' ]);
4832 fprintf(fid,   ['FirstImage ' par.filename_ima_a '\n' ]);
4833 fprintf(fid,   ['LastImage  ' 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
4864if(isunix)
4865    [Rootbat,Filebat,extbat]=fileparts(namelog);
4866    ncName=fullfile(Rootbat,[ Filebat '.nc']);
4867    cmd_CIV1=[cmd_CIV1 '\n' 'mv ' namelog  ' ' regexprep(namelog,'\.log','') '.civ1.log' '\n' 'chmod g+w ' ncName];
4868else
4869    cmd_CIV1=[cmd_CIV1 '\n' 'copy /Y ' namelog ' ' regexprep(namelog,'\.log','') '.civ1.log'];
4870end
4871
4872%------------------------------------------------------------------------
4873% --- CIV1  Unified
4874function xml_civ1_parameters=BATCH_CIV1_Unified(filename,namelog,par)
4875%------------------------------------------------------------------------
4876%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4877%global CivBin%name of the executable for civ1 calculation
4878
4879    civ1.image1=par.filename_ima_a;
4880    civ1.image2=par.filename_ima_b;
4881    civ1.imageSize_X=par.npx;
4882    civ1.imageSize_Y=par.npy;
4883    civ1.outputFileName=[filename '.nc'];
4884    civ1.correlationBoxesSize_X=par.ibx;
4885    civ1.correlationBoxesSize_Y=par.iby;
4886    civ1.searchBoxesSize_X=par.isx;
4887    civ1.searchBoxesSize_Y=par.isy;
4888    civ1.globalShift_X=par.shiftx;
4889    civ1.globalShift_Y=par.shifty;
4890    civ1.ro=par.rho;
4891    civ1.hart='y';
4892    if isequal(par.gridflag,'y')
4893        civ1.grid=par.gridname;
4894    else
4895        civ1.grid='n';
4896        civ1.gridSpacing_X=par.dx;
4897        civ1.gridSpacing_Y=par.dy;
4898    end
4899    if isequal(par.maskflag,'y')
4900        civ1.mask=par.maskname;
4901    end
4902    civ1.dt=par.Dt;
4903    civ1.unit='pixel';
4904    civ1.absolut_time_T0=par.T0;
4905    civ1.pixcmx=par.pxcmx;
4906    civ1.pixcmy=par.pxcmy;
4907    civ1.convectFlow='n';
4908 
4909    xml_civ1_parameters=civ1;
4910
4911%------------------------------------------------------------------------
4912% --- CIV2  Unified
4913function civ2=BATCH_CIV2_Unified(filename,namelog,par)
4914%------------------------------------------------------------------------
4915%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4916%global CivBin%name of the executable for civ1 calculation
4917
4918civ2.image1=par.filename_ima_a;
4919civ2.image2=par.filename_ima_b;
4920civ2.imageSize_X=par.npx;
4921civ2.imageSize_Y=par.npy;
4922civ2.inputFileName=[par.filename_nc1 '.nc'];
4923civ2.outputFileName=[filename '.nc'];
4924civ2.correlationBoxesSize_X=par.ibx;
4925civ2.correlationBoxesSize_Y=par.iby;
4926civ2.ro=par.rho;
4927%civ2.decimalShift=par.decimal;
4928%civ2.deformation=par.deformation;
4929if isequal(par.decimal,'1')
4930    civ2.decimalShift='y';
4931else
4932    civ2.decimalShift='n';
4933end
4934if isequal(par.deformation,'1')
4935    civ2.deformation='y';
4936else
4937    civ2.deformation='n';
4938end
4939if isequal(par.gridflag,'y')
4940    civ2.grid=par.gridname;
4941else
4942    civ2.grid='n';
4943    civ2.gridSpacing_X=par.dx;
4944    civ2.gridSpacing_Y=par.dy;
4945end
4946civ2.gridSpacing_X='10';
4947civ2.gridSpacing_Y='10';%NOTE: faut mettre gridSpacing pourque ca tourne, meme si c'est la grille qui est utilisee
4948if isequal(par.maskflag,'y')
4949    civ2.mask=par.maskname;
4950else
4951    civ2.mask='n';
4952end
4953civ2.dt=par.Dt;
4954civ2.unit='pixel';
4955civ2.absolut_time_T0=par.T0;
4956civ2.pixcmx=par.pxcmx;
4957civ2.pixcmy=par.pxcmy;
4958civ2.convectFlow='n';
4959civ2.pixcmx=par.pxcmx;
4960civ2.pixcmy=par.pxcmy;
4961civ2.convectFlow='n';
4962
4963%------------------------------------------------------------------------
4964% --- CIV2  CIV2  CIV2 CIV2
4965function cmd_CIV2=BATCH_CIV2(filename_cmx,namelog,par,sparam)
4966%------------------------------------------------------------------------
4967%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4968% global civ2Bin sge%name of the executable for civ1 calculation
4969   if isequal(par.Dt,'0')
4970                par.Dt='1' ;%case of 'displacement' mode
4971  end
4972textcmx=['##############   CMX file'  '\n'...
4973['FirstImage ' par.filename_ima_a]  '\n'...
4974['LastImage  ' par.filename_ima_b]  '\n'...
4975'XX'   '\n'...
4976['Mask ' par.maskflag]  '\n'...
4977['MaskName ' par.maskname]  '\n'...
4978['ImageSize ' par.npx ' ' par.npy]  '\n'...   
4979['CorrelationBoxesSize ' par.ibx ' ' par.iby]  '\n'...
4980['SearchBoxeSize ' par.ibx ' ' par.iby]  '\n'...
4981['RO ' par.rho]  '\n'...
4982['GridSpacing ' par.dx ' ' par.dy]  '\n'...
4983'XX 1.0'  '\n'...
4984['Dt_TO ' par.Dt ' ' par.T0]  '\n'...
4985['PixCmXY ' par.pxcmx ' ' par.pxcmy]  '\n'...
4986'XX 1'  '\n'...
4987['ShiftXY 0 0']  '\n'...
4988['Grid ' par.gridflag]  '\n'...
4989['GridName ' par.gridname]  '\n'...
4990'XX 85'  '\n'...
4991'XX 1.0'  '\n'...
4992'XX 1.0'  '\n'...
4993'Hart 1'  '\n'...
4994['DecimalShift ' par.decimal]  '\n'...
4995['Deformation ' par.deformation]  '\n'...
4996'CorrelationMin 0'  '\n'...
4997'IntensityMin 0'  '\n'...
4998'SeuilImage n'  '\n'...
4999'SeuilImageValues 0 4096'  '\n'...
5000['ImageToUse ' par.term_a ' ' par.term_b]  '\n'... % VERIFIER ?
5001['ImageUsedBefore ' par.filename_nc1]];
5002textout=char(textcmx);
5003fid=fopen([filename_cmx '2'],'w');
5004fprintf(fid,textout);
5005fclose(fid)
5006% if sge
5007    cmd_CIV2=[sparam.Civ2Bin ' -f ' filename_cmx ]; % redirect standard output to the log file
5008% else
5009%     cmd_CIV2=[civ2Bin ' -f ' filename_cmx ' > ' namelog ' 2>&1']; % redirect standard output to the log file
5010% end
5011
5012%------------------------------------------------------------------------
5013% --- Executes on button press in HELP.
5014function HELP_Callback(hObject, eventdata, handles)
5015%------------------------------------------------------------------------
5016path_to_uvmat=which ('uvmat');% check the path of uvmat
5017pathelp=fileparts(path_to_uvmat);
5018helpfile=fullfile(pathelp,'uvmat_doc','uvmat_doc.html');
5019if isempty(dir(helpfile)), msgbox_uvmat('ERROR','Please put the help file uvmat_doc.html in the sub-directory /uvmat_doc of the UVMAT package')
5020else
5021    addpath (fullfile(pathelp,'uvmat_doc'))
5022    web([helpfile '#civ'])   
5023end
5024
5025%------------------------------------------------------------------------
5026%--read images and convert them to the uint16 format used for PIV
5027function A=read_image(filename,type_ima,num,movieobject)
5028%------------------------------------------------------------------------
5029%num is the view number needed for an avi movie
5030switch type_ima
5031    case 'movie'
5032        A=read(movieobject,num);
5033    case 'avi'
5034       mov=aviread(filename,num);     
5035       A=frame2im(mov(1));
5036    case 'multimage'
5037        A=imread(filename,num);
5038    case 'image'   
5039        A=imread(filename);
5040end
5041siz=size(A);
5042if length(siz)==3;%color images
5043    A=sum(double(A),3);
5044    A=uint16(A);
5045end
5046
5047%------------------------------------------------------------------------
5048function ref_i_Callback(hObject, eventdata, handles)
5049%------------------------------------------------------------------------
5050mode_list=get(handles.mode,'String');
5051mode_value=get(handles.mode,'Value');
5052mode=mode_list{mode_value};
5053find_netcpair_civ1(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
5054if isequal(mode,'series(Di)') || ...% we do patch2 only
5055   (get(handles.CIV2,'Value')==0 && get(handles.CIV1,'Value')==0 && get(handles.FIX1,'Value')==0 && get(handles.PATCH1,'Value')==0)
5056    find_netcpair_civ2(hObject, eventdata, handles);
5057end
5058
5059%------------------------------------------------------------------------
5060function ref_j_Callback(hObject, eventdata, handles)
5061%------------------------------------------------------------------------
5062mode_list=get(handles.mode,'String');
5063mode_value=get(handles.mode,'Value');
5064mode=mode_list{mode_value};
5065if isequal(get(handles.CIV1,'Value'),0)|| isequal(mode,'series(Dj)')
5066    find_netcpair_civ1(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
5067end
5068if isequal(mode,'series(Dj)') || ...
5069   (get(handles.CIV2,'Value')==0 && get(handles.CIV1,'Value')==0 && get(handles.FIX1,'Value')==0 && get(handles.PATCH1,'Value')==0)
5070    find_netcpair_civ2(hObject, eventdata, handles);
5071end
5072
5073%------------------------------------------------------------------------
5074function ref_i_civ2_Callback(hObject, eventdata, handles)
5075%------------------------------------------------------------------------
5076mode_list=get(handles.mode,'String');
5077mode_value=get(handles.mode,'Value');
5078mode=mode_list{mode_value};
5079find_netcpair_civ2(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
5080
5081%------------------------------------------------------------------------
5082function ref_j_civ2_Callback(hObject, eventdata, handles)
5083%------------------------------------------------------------------------
5084mode_list=get(handles.mode,'String');
5085mode_value=get(handles.mode,'Value');
5086mode=mode_list{mode_value};
5087if  isequal(mode,'series(Dj)')
5088    find_netcpair_civ2(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
5089end
5090
5091%------------------------------------------------------------------------
5092% --- Executes on button press in compare.
5093function compare_Callback(hObject, eventdata, handles)
5094%------------------------------------------------------------------------
5095test=get(handles.compare,'Value');
5096if test==2 || test==3   
5097    filebase=get(handles.RootName,'String');
5098    browse=get(handles.browse_root,'Userdata');
5099    browse.nom_type_ima1=browse.nom_type_ima;
5100    set(handles.browse_root,'UserData',browse);
5101    set(handles.sub_txt,'Visible','on')
5102    set(handles.RootName_1,'Visible','On');%mkes the second file input window visible
5103    mode_store=get(handles.mode,'String');%get the present 'mode'
5104    set(handles.compare,'UserData',mode_store);%store the mode display 
5105    set(handles.mode,'Visible','off')
5106    if test==2
5107        set(handles.mode,'Visible','off')       
5108    else
5109        set(handles.mode,'Visible','on')
5110    end
5111   
5112    % open an image file with the browser
5113    ind_opening=1;%default
5114    browse.incr_pair=[0 0]; %default
5115    oldfile=get(handles.RootName,'String');
5116    menu={'*.xml;*.avi;*.AVI;*.nc','(*.xml,*.avi,*.nc)'; ...
5117            '*.xml', '.xml files';'*.avi;*.AVI', '.avi files';'*.nc', '.nc files';...
5118            '*.*', 'All Files (*.*)'};
5119    [FileName, PathName, filtindex] = uigetfile( menu, 'Pick a file',oldfile);
5120    fileinput=[PathName FileName];%complete file name
5121    sizf=size(fileinput);
5122    if (~ischar(fileinput)|~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
5123    [path,name,ext]=fileparts(fileinput);
5124    [path1]=fileparts(filebase);
5125    if ~strcmp(path1,path)
5126         msgbox_uvmat('ERROR','The two  input image series must be in the same directory')
5127        return
5128    end
5129    set(handles.RootName_1,'String',name);
5130    [RootPath,RootFile,field_count,str2,str_a,str_b,xx,nom_type,subdir]=name2display(name);
5131    browse=get(handles.browse_root,'UserData');
5132    browse.nom_type_ima_1=nom_type;
5133    set(handles.browse_root,'UserData',browse) 
5134   
5135    %check image extension
5136    if ~strcmp(ext,get(handles.ImaExt,'String'))
5137        msgbox_uvmat('ERROR','The two  input image series must have the same extenion name')
5138        return
5139    end
5140   
5141    %check image size
5142    A=imread(fileinput);
5143    npxy=get(handles.ImaExt,'UserData');
5144    if ~isequal(npxy(1),size(A,1))|| ~isequal(npxy(2),size(A,2))
5145        msgbox_uvmat('ERROR','The two input image series must have the same size')
5146        return
5147    end
5148else
5149    set(handles.mode,'Visible','on')
5150    set(handles.RootName_1,'Visible','Off');
5151    set(handles.sub_txt,'Visible','off')
5152    set(handles.RootName_1,'String',[]);
5153    mode_store=get(handles.compare,'UserData');
5154    set(handles.mode,'String',mode_store)
5155    set(handles.test_stereo1,'Value',0)
5156    set(handles.test_stereo2,'Value',0)
5157end
5158mode_Callback(hObject, eventdata, handles)
5159
5160%------------------------------------------------------------------------
5161% --- Executes on button press in get_ref_fix1.
5162function get_ref_fix1_Callback(hObject, eventdata, handles)
5163%------------------------------------------------------------------------
5164filebase=get(handles.RootName,'String');
5165[FileName, PathName, filterindex] = uigetfile( ...
5166       {'*.nc', ' (*.nc)';
5167        '*.nc',  'netcdf files '; ...
5168        '*.*', 'All Files (*.*)'}, ...
5169        'Pick a file',filebase);
5170   
5171fileinput=[PathName FileName];
5172sizf=size(fileinput);
5173if (~ischar(fileinput)|~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
5174[Path,File,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
5175ref.filebase=fullfile(Path,File);
5176ref.num_a=stra2num(str_a);
5177ref.num_b=stra2num(str_b);
5178ref.num1=str2double(field_count);
5179ref.num2=str2double(str2);
5180browse=[];%initialisation
5181if ~isequal(ref.ext,'.nc')
5182    msgbox_uvmat('ERROR','the reference file must be in netcdf format (*.nc)')
5183    return
5184end
5185set(handles.ref_fix1,'String',[fullfile(ref.subdir,File) '....nc']);
5186set(handles.ref_fix1,'UserData',ref)
5187menu_field{1}='civ1';
5188Data=nc2struct(fileinput,[]);
5189if isfield(Data,'patch') && isequal(Data.patch,1)
5190    menu_field{2}='filter1';
5191end
5192if isfield(Data,'civ2') && isequal(Data.civ2,1)
5193    menu_field{3}='civ2';
5194end
5195if isfield(Data,'patch2') && isequal(Data.patch2,1)
5196    menu_field{4}='filter2';
5197end
5198set(handles.field_ref1,'String',menu_field);
5199set(handles.field_ref1,'Value',length(menu_field));
5200set(handles.inf_sup1,'Value',2);
5201set(handles.thresh_vel,'String','1');%default threshold
5202set(handles.ref_fix1,'Enable','on')
5203
5204%------------------------------------------------------------------------
5205% --- Executes on button press in get_ref_fix2.
5206function get_ref_fix2_Callback(hObject, eventdata, handles)
5207%------------------------------------------------------------------------
5208if isequal(get(handles.get_ref_fix2,'Value'),1)
5209    filebase=get(handles.RootName,'String');
5210    [FileName, PathName, filterindex] = uigetfile( ...
5211           {'*.nc', ' (*.nc)';
5212            '*.nc',  'netcdf files '; ...
5213            '*.*', 'All Files (*.*)'}, ...
5214            'Pick a file',filebase);
5215    fileinput=[PathName FileName];
5216    sizf=size(fileinput);
5217    if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
5218    [Path,File,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
5219    ref.filebase=fullfile(Path,File);
5220    ref.num_a=stra2num(str_a);
5221    ref.num_b=stra2num(str_b);
5222    ref.num1=str2num(field_count);
5223    ref.num2=str2num(str2);
5224    browse=[];%initialisation
5225    if ~isequal(ref.ext,'.nc')
5226        msgbox_uvmat('ERROR','the reference file must be in netcdf format (*.nc)')
5227        return
5228    end
5229    set(handles.ref_fix2,'String',[fullfile(ref.subdir,File) '....nc']);
5230    set(handles.ref_fix2,'UserData',ref)   
5231    menu_field{1}='civ1';
5232    Data=nc2struct(fileinput,[]);
5233    if isfield(Data,'patch') & isequal(Data.patch,1)
5234        menu_field{2}='filter1';
5235    end
5236    if isfield(Data,'civ2') & isequal(Data.civ2,1)
5237        menu_field{3}='civ2';
5238    end
5239    if isfield(Data,'patch2') & isequal(Data.patch2,1)
5240        menu_field{4}='filter2';
5241    end
5242    set(handles.field_ref2,'String',menu_field);
5243    set(handles.field_ref2,'Value',length(menu_field));
5244    set(handles.inf_sup2,'Value',2);
5245    set(handles.thresh_vel2,'String','1');%default threshold
5246    set(handles.ref_fix2,'Enable','on')
5247    set(handles.ref_fix2,'Visible','on')
5248    set(handles.field_ref2,'Visible','on')
5249else
5250    set(handles.ref_fix2,'Visible','off')
5251    set(handles.field_ref2,'Visible','off')
5252end
5253
5254%------------------------------------------------------------------------
5255function ref_fix1_Callback(hObject, eventdata, handles)
5256%------------------------------------------------------------------------
5257set(handles.inf_sup1,'Value',1);
5258set(handles.field_ref1,'Value',1)
5259set(handles.field_ref1,'String',{' '})
5260set(handles.ref_fix1,'UserData',[]);
5261set(handles.ref_fix1,'String','');
5262set(handles.thresh_vel1,'String','0');
5263 
5264%------------------------------------------------------------------------
5265function ref_fix2_Callback(hObject, eventdata, handles)
5266%------------------------------------------------------------------------
5267set(handles.inf_sup2,'Value',1);
5268set(handles.field_ref2,'Value',1)
5269set(handles.field_ref2,'String',{' '})
5270set(handles.ref_fix2,'UserData',[]);
5271set(handles.ref_fix2,'String','');
5272set(handles.thresh_vel2,'String','0');
5273
5274% %------------------------------------------------------------------------
5275% % transform letters to numbers
5276% function numres=stra2num(str)
5277% %------------------------------------------------------------------------
5278% numres=double(str)-96;
5279% if double(str) >= 48 & double(str) <= 57 % = 1 for numbers
5280%     numres=str2num(str);
5281% end
5282
5283%------------------------------------------------------------------------
5284% --- Executes on button press in test_stereo1.
5285function test_stereo1_Callback(hObject, eventdata, handles)
5286%------------------------------------------------------------------------
5287if isequal(get(handles.test_stereo1,'Value'),0)
5288    set(handles.subdomain_patch1,'Visible','on')
5289    set(handles.rho_patch1,'Visible','on')
5290else
5291    set(handles.subdomain_patch1,'Visible','off')
5292    set(handles.rho_patch1,'Visible','off')
5293end
5294
5295%------------------------------------------------------------------------
5296% --- Executes on button press in test_stereo2.
5297function test_stereo2_Callback(hObject, eventdata, handles)
5298%------------------------------------------------------------------------
5299if isequal(get(handles.test_stereo2,'Value'),0)
5300    set(handles.subdomain_patch2,'Visible','on')
5301    set(handles.rho_patch2,'Visible','on')
5302else
5303    set(handles.subdomain_patch2,'Visible','off')
5304    set(handles.rho_patch2,'Visible','off')
5305end
5306
5307%------------------------------------------------------------------------
5308% --- Executes on button press in ImaThreshold.
5309function ImaThreshold_Callback(hObject, eventdata, handles)
5310%------------------------------------------------------------------------
5311if isequal(get(handles.ImaThreshold,'Value'),1)
5312    set(handles.MinIma,'Visible','on')
5313    set(handles.MaxIma,'Visible','on')
5314else
5315    set(handles.MinIma,'Visible','off')
5316    set(handles.MaxIma,'Visible','off')
5317end
5318
5319%------------------------------------------------------------------------
5320% --- Executes on button press in ImaThreshold2.
5321function ImaThreshold2_Callback(hObject, eventdata, handles)
5322%------------------------------------------------------------------------
5323if isequal(get(handles.ImaThreshold2,'Value'),1)
5324    set(handles.MinIma2,'Visible','on')
5325    set(handles.MaxIma2,'Visible','on')
5326else
5327    set(handles.MinIma2,'Visible','off')
5328    set(handles.MaxIma2,'Visible','off')
5329end
5330
5331
5332
5333
Note: See TracBrowser for help on using the repository browser.