source: trunk/src/civ.m @ 12

Last change on this file since 12 was 12, checked in by gostiaux, 14 years ago

The files from uvmat.2.2.beta that differed from the current version have been updated.
Now the /raid/soft/UVMAT/src should be operational

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