source: trunk/src/civ.m @ 87

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

improvement of vioew_field
suppress spurious 'todo' path in civ

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