source: trunk/src/civ.m @ 82

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

mouse_up: improve zoom : keep the selected point at the same place on the graph
plot_field: memorize the max coordiantes on the graph (AxeData?) for zoom out
set_object: input possibilities extende to restrict menu (for mask objects)
uvmat: improvement of mask creation
civ: select mask option for civ2 when select(ed for civ1

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