source: trunk/src/civ.m @ 244

Last change on this file since 244 was 244, checked in by sommeria, 13 years ago

various bug repair, improved display of msgbox_uvmat

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