source: trunk/src/civ.m @ 58

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

small bug repaired in civ (spurious call to first_i)
image pair movie improved in uvmat

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