source: trunk/src/civ.m @ 81

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

several bug repairs:
option movie backward introduced for uvmat

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