source: trunk/src/civ.m @ 97

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

bug with missing CivBin? field re-corrected

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