source: trunk/src/civ.m @ 238

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

various bug corrections

File size: 197.0 KB
Line 
1%'civ': function associated with the interface 'civ.fig' for PIV, spline interpolation and stereo PIV (patch)
2%------------------------------------------------------------------------
3%  provides an interface for the software CIVx
4% function varargout = civ(varargin)
5% provides an interface for the software CIVx
6%
7%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
8%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
9%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
10%     This file is part of the toolbox UVMAT.
11%
12%     UVMAT is free software; you can redistribute it and/or modify
13%     it under the terms of the GNU General Public License as published by
14%     the Free Software Foundation; either version 2 of the License, or
15%     (at your option) any later version.
16%
17%     UVMAT is distributed in the hope that it will be useful,
18%     but WITHOUT ANY WARRANTY; without even the implied warranty of
19%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
21%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
22function varargout = civ(varargin)
23
24% Last Modified by GUIDE v2.5 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            if box_test(1)==1
2125                Param.Civ1=par_civ1;
2126            end
2127            if box_test(2)==1
2128                fix1.WarnFlags=[];
2129                if get(handles.vec_Fmin2,'Value')
2130                    fix1.WarnFlags=[fix1.WarnFlags -2];
2131                end
2132                if get(handles.vec_F3,'Value')
2133                    fix1.WarnFlags=[fix1.WarnFlags 3];
2134                end
2135                fix1.LowerBoundCorr=thresh_vecC1;
2136                if get(handles.inf_sup1,'Value')
2137                    fix1.UppperBoundVel=thresh_vel1;
2138                else
2139                     fix1.LowerBoundVel=thresh_vel1;
2140                end
2141                Param.Fix1=fix1;
2142            end
2143            if box_test(3)==1
2144                Param.Patch1.Rho=rho_patch1;
2145                Param.Patch1.Threshold=thresh_patch1;
2146                Param.Patch1.SubDomain=subdomain_patch1;
2147            end
2148            [Data,erromsg]=civ_uvmat(Param,filecell.nc.civ1{ifile,j});
2149            if isempty(errormsg)
2150                display([filecell.nc.civ1{ifile,j} ' written'])
2151            else
2152                msgbox_uvmat('ERROR',errormsg)
2153            end
2154        end                 
2155    end
2156end
2157
2158if ~batch && ~CivUvmat
2159    [Rootbat,Filebat,extbat]=fileparts(filename_bat);
2160    filename_superbat=fullfile(Rootbat,'job_list.bat');
2161    fid=fopen(filename_superbat,'w');
2162    fprintf(fid,super_cmd');
2163    fclose(fid);
2164     if(isunix)
2165        system(['chmod +x ' filename_superbat])
2166     end
2167     system([filename_superbat ' &'])% execute main commmand
2168end
2169
2170%% save interface state
2171if isfield(filecell,'nc')
2172    if isfield(filecell.nc,'civ2')
2173        fileresu=filecell.nc.civ2{1,1};
2174    else
2175        fileresu=filecell.nc.civ1{1,1};
2176    end
2177end
2178[RootPath,RootFile,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fileresu);
2179namedoc=fullfile(RootPath,subdir,RootFile);
2180detect=1;
2181while detect==1
2182    namefigfull=[namedoc '.fig'];
2183    hh=dir(namefigfull);
2184    if ~isempty(hh)
2185        detect=1;
2186        namedoc=[namedoc '.0'];
2187    else
2188        detect=0;
2189    end
2190end
2191saveas(gcbf,namefigfull);%save the interface with name namefigfull (A CHANGER EN FICHIER  .xml)
2192
2193%------------------------------------------------------------------------
2194% --- determine the list of reference indices of processing file
2195function [ref_i,ref_j,errormsg]=find_ref_indices(handles)
2196%------------------------------------------------------------------------
2197errormsg=''; %default error message
2198first_i=str2double(get(handles.first_i,'String'));%first index i
2199last_i=str2double(get(handles.last_i,'String'));%last index i
2200incr_i=str2double(get(handles.incr_i,'String'));% increment
2201if isequal(get(handles.first_j,'Visible'),'on')
2202    first_j=str2double(get(handles.first_j,'String'));%first index j
2203    last_j=str2double(get(handles.last_j,'String'));%last index j
2204    incr_j=str2double(get(handles.incr_j,'String'));% increment
2205else
2206    first_j=1;
2207    last_j=1;
2208    incr_j=1;
2209end
2210ref_i=first_i:incr_i:last_i;% list of i indices (reference values for each pair)
2211ref_j=first_j:incr_j:last_j;% list of j indices (reference values for each pair)
2212if isnan(first_i)||isnan(first_j)
2213    errormsg='first field number not defined';
2214elseif isnan(last_i)||isnan(last_j)
2215    errormsg='last field number not defined';
2216elseif isnan(incr_i)||isnan(incr_j)
2217    errormsg='increment in field number not defined';
2218elseif last_i < first_i || last_j < first_j
2219    errormsg='last field number must be larger than the first one';
2220end
2221
2222%------------------------------------------------------------------------
2223% --- determine the list of filenames and indices needed for launch_job
2224function [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]=...
2225    set_civ_filenames(handles,ref_i,ref_j,box_test)
2226%------------------------------------------------------------------------
2227filecell=[];%default
2228
2229%% get the root names nomenclature and numbers
2230filebase=get(handles.RootName,'String');
2231
2232if isempty(filebase)||isequal(filebase,'')
2233    msgbox_uvmat('ERROR','no input files')
2234    return
2235end
2236
2237%filebase=regexprep(filebase,'\.fsnet','fsnet');% temporary fix for cluster Coriolis
2238filecell.filebase=filebase;
2239
2240browse=get(handles.browse_root,'UserData');
2241compare_list=get(handles.compare,'String');
2242val=get(handles.compare,'Value');
2243compare=compare_list{val};
2244if strcmp(compare,'displacement')
2245    mode='displacement';
2246else
2247    mode_list=get(handles.mode,'String');
2248    mode_value=get(handles.mode,'Value');
2249    mode=mode_list{mode_value};
2250end
2251%time=get(handles.RootName,'UserData'); %get the set of times
2252ext_ima=get(handles.ImaExt,'String');
2253nom_type_nc=browse.nom_type_nc;
2254if isfield(browse,'nom_type_ima')
2255    nom_type_ima2=browse.nom_type_ima;
2256end
2257if isempty(nom_type_ima2),nom_type_ima2='1';end; %default
2258if isempty(nom_type_nc),nom_type_nc='_i1-i2';end; %default
2259[num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2]=...
2260    find_pair_indices(handles,ref_i,ref_j,mode);
2261%determine the new filebase for 'displacement' mode (comparison of two series)
2262filebase_B=filebase;% root name of the second field series for stereo
2263if strcmp(compare,'displacement') || strcmp(compare,'stereo PIV')
2264%     test_disp=1;
2265    nom_type_ima1=browse.nom_type_ima_1; %nomenclature type of the second file series
2266    [Path2,Name2]=fileparts(filebase_B);
2267    Path1=Path2;
2268    Name1=get(handles.RootName_1,'String');% root name of the first field series for stereo
2269    filebase_A=fullfile(Path1,Name1);
2270    if length(Name1)>6
2271        Name1=Name1(end-5:end);
2272    end
2273    if length(Name2)>6
2274        Name2=Name2(end-5:end);
2275    end
2276    filebase_AB=fullfile(Path2,[Name2 '-' Name1]);
2277else
2278%     test_disp=0;
2279    filebase_A=filebase;
2280    nom_type_ima1=nom_type_ima2;
2281    filebase_AB=filebase;
2282end
2283if strcmp(compare,'displacement')
2284    filebase_ima1=filebase_A;
2285    filebase_ima2=filebase_B;
2286    filebase_nc=filebase_AB; %root name for the result of civ2
2287else
2288    filebase_ima1=filebase_B;
2289    filebase_ima2=filebase_B;
2290    filebase_nc=filebase_B;
2291end
2292
2293%determine reference files for fix:
2294file_ref_fix1={};%default
2295file_ref_fix2={};
2296nbfield=length(num1_civ1);
2297nbslice=length(num_a_civ1);
2298if box_test(2)==1% fix1 performed
2299    ref=get(handles.ref_fix1,'UserData');%read data on the ref file stored by get_ref_fix1_Callback
2300    if ~isempty(ref)
2301        first_i=str2double(get(handles.first_i,'String'));
2302        last_i=str2double(get(handles.last_i,'String'));
2303        incr_i=str2double(get(handles.incr_i,'String'));
2304        first_j=str2double(get(handles.first_j,'String'));
2305        last_j=str2double(get(handles.last_j,'String'));
2306        incr_j=str2double(get(handles.incr_j,'String'));
2307        num_i_ref=first_i:incr_i:last_i;
2308        num_j_ref=first_j:incr_j:last_j;
2309        if isequal(mode,'displacement')
2310            num_i1=num_i_ref;
2311            num_i2=num_i_ref;
2312            num_j1=num_j_ref;
2313            num_j2=num_j_ref;
2314        elseif isequal(mode,'pair j1-j2')% isequal(mode,'st_pair j1-j2')
2315            num_i1=num_i_ref;
2316            num_i2=num_i1;
2317            num_j1=ref.num_a*ones(size(num_i_ref));
2318            num_j2=ref.num_b*ones(size(num_i_ref));
2319        elseif isequal(mode,'series(Di)') % isequal(mode,'st_series(Di)')
2320            delta1=floor((ref.num2-ref.num1)/2);
2321            delta2=ceil((ref.num2-ref.num1)/2);
2322            num_i1=num_i_ref-delta1*ones(size(num_i_ref));
2323            num_i2=num_i_ref+delta2*ones(size(num_i_ref));
2324            if isempty(ref.num_a)
2325                ref.num_a=1;
2326            end
2327            num_j1=ref.num_a*ones(size(num_i1));
2328            num_j2=num_j1;
2329        elseif isequal(mode,'series(Dj)')%| isequal(mode,'st_series(Dj)')
2330            delta1=floor((ref.num_b-ref.num_a)/2);
2331            delta2=ceil((ref.num_b-ref.num_a)/2);
2332            num_i1=ref.num1*ones(size(num_i_ref));
2333            num_i2=num_i1;
2334            num_j1=num_j_ref-delta1*ones(size(num_j_ref));
2335            num_j2=num_j_ref+delta2*ones(size(num_j_ref));
2336        end
2337        for ifile=1:nbfield
2338            for j=1:nbslice
2339                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);%
2340                file_ref_fix1(ifile,j)={file_ref};
2341                if ~exist(file_ref,'file')
2342                    msgbox_uvmat('ERROR',['reference file ' file_ref ' not found for fix1'])
2343                    filecell=[];
2344                    return
2345                end
2346            end
2347        end
2348    end
2349end
2350
2351%determine reference files for fix2:
2352if box_test(5)==1% fix2 performed
2353    ref=get(handles.ref_fix2,'UserData');
2354    if ~isempty(ref)
2355        first_i=str2double(get(handles.first_i,'String'));
2356        last_i=str2double(get(handles.last_i,'String'));
2357        incr_i=str2double(get(handles.incr_i,'String'));
2358        first_j=str2double(get(handles.first_j,'String'));
2359        last_j=str2double(get(handles.last_j,'String'));
2360        incr_j=str2double(get(handles.incr_j,'String'));
2361        num_i_ref=first_i:incr_i:last_i;
2362        num_j_ref=first_j:incr_j:last_j;
2363        if isequal(mode,'displacement')
2364            num_i1=num_i_ref;
2365            num_i2=num_i_ref;
2366            num_j1=num_j_ref;
2367            num_j2=num_j_ref;
2368        elseif isequal(mode,'pair j1-j2')
2369            num_i1=num_i_ref;
2370            num_i2=num_i1;
2371            num_j1=ref.num_a;
2372            num_j2=ref.num_b;
2373        elseif isequal(mode,'series(Di)')
2374            delta1=floor((ref.num2-ref.num1)/2);
2375            delta2=ceil((ref.num2-ref.num1)/2);
2376            num_i1=num_i_ref-delta1*ones(size(num_i_ref));
2377            num_i2=num_i_ref+delta2*ones(size(num_i_ref));
2378            num_j1=ref.num_a*ones(size(num_i1));
2379            num_j2=num_j1;
2380        elseif isequal(mode,'series(Dj)')
2381            delta1=floor((ref.num_b-ref.num_a)/2);
2382            delta2=ceil((ref.num_b-ref.num_a)/2);
2383            num_i1=ref.num1*ones(size(num_i_ref));
2384            num_i2=num_i1;
2385            num_j1=num_j_ref-delta1*ones(size(num_j_ref));
2386            num_j2=num_j_ref+delta2*ones(size(num_j_ref));
2387        end
2388        for ifile=1:nbfield
2389            for j=1:nbslice
2390                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);%
2391                file_ref_fix2(ifile,j)={file_ref};
2392                if ~exist(file_ref,'file')
2393                    msgbox_uvmat('ERROR',['reference file ' file_ref ' not found for fix2'])
2394                    filecell={};
2395                    return
2396                end
2397            end
2398        end
2399    end
2400end
2401
2402%check dir
2403subdir_civ1=get(handles.subdir_civ1,'String');%subdirectory subdir_civ1 for the netcdf output data
2404subdir_civ2=get(handles.subdir_civ2,'String');
2405if isequal(subdir_civ1,''),subdir_civ1='CIV'; end% put default subdir
2406if isequal(subdir_civ2,''),subdir_civ2=subdir_civ1; end% put default subdir
2407currentdir=pwd;%store the current working directory
2408[Path_ima,Name]=fileparts(filebase);%Path of the image files (.civ)
2409if ~exist(Path_ima,'dir')
2410    msgbox_uvmat('ERROR',['path to images ' Path_ima ' not found'])
2411    filecell={};
2412    return
2413end
2414[xx,message]=fileattrib(Path_ima);
2415if ~isempty(message) && ~isequal(message.UserWrite,1)
2416    msgbox_uvmat('ERROR',['No writting access to ' Path_ima])
2417    filecell={};
2418    cd(currentdir);
2419    return
2420end
2421
2422%check the existence of the netcdf and image files involved
2423% %%%%%%%%%%%%  case CIV1 activated   %%%%%%%%%%%%%
2424if box_test(1)==1;
2425    detect=1;
2426    vers=0;
2427    subdir_civ1_new=subdir_civ1;
2428    while detect==1 %create a new subdir if the netcdf files already exist
2429        for ifile=1:nbfield
2430            for j=1:nbslice
2431                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);
2432                detect=exist(filename,'file')==2;
2433                if detect% if a netcdf file already exists
2434                    indstr=regexp(subdir_civ1_new,'\D');
2435                    if indstr(end)<length(subdir_civ1_new) %subdir_civ1 ends by a number
2436                        vers=str2double(subdir_civ1_new(indstr(end)+1:end))+1;
2437                        subdir_civ1_new=[subdir_civ1_new(1:indstr(end)) num2str(vers)];
2438                    else
2439                        vers=vers+1;
2440                        subdir_civ1_new=[subdir_civ1_new(1:indstr(end)) '_' num2str(vers)];       
2441                    end
2442                    subdir_civ2=subdir_civ1_new;
2443                    break
2444                end
2445                filecell.nc.civ1(ifile,j)={filename};
2446            end
2447            if detect% if a netcdf file already exists
2448                break
2449            end
2450        end
2451 
2452        %create the new subdir_civ1
2453        if ~exist(fullfile(Path_ima,subdir_civ1_new),'dir')
2454            cd(Path_ima);         
2455            [xx,msg1]=mkdir(subdir_civ1_new);
2456
2457            if ~strcmp(msg1,'')
2458                msgbox_uvmat('ERROR',['cannot create ' subdir_civ1_new ': ' msg1])%error message for directory creation
2459                filecell={};
2460                return
2461            elseif isunix         
2462                [xx,msg2] = fileattrib(subdir_civ1_new,'+w','g'); %yield writing access (+w) to user group (g)
2463                if ~strcmp(msg2,'')
2464                    msgbox_uvmat('ERROR',['pb of permission for  ' subdir_civ1_new ': ' msg2])%error message for directory creation
2465                    filecell={};
2466                    return
2467                end
2468            end
2469            cd(currentdir);
2470        end
2471        if strcmp(compare,'stereo PIV')&&(strcmp(mode,'pair j1-j2')||strcmp(mode,'series(Dj)')||strcmp(mode,'series(Di)'))%check second nc series
2472            for ifile=1:nbfield
2473                for j=1:nbslice
2474                    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);%
2475                    detect=exist(filename,'file')==2;
2476                    if detect% if a netcdf file already exists
2477                       indstr=regexp(subdir_civ1_new,'\D');
2478                       if indstr(end)<length(subdir_civ1_new) %subdir_civ1 ends by a number
2479                           vers=str2double(subdir_civ1_new(indstr(end)+1:end))+1;
2480                           subdir_civ1_new=[subdir_civ1_new(1:indstr(end)) num2str(vers)];
2481                       else
2482                           vers=vers+1;
2483                           subdir_civ1_new=[subdir_civ1_new '_' num2str(vers)];
2484                       end
2485                       subdir_civ2=subdir_civ1;
2486                       break
2487                    end
2488                    filecell.ncA.civ1(ifile,j)={filename};
2489                end
2490                if detect% if a netcdf file already exists
2491                    break
2492                end
2493            end
2494            %create the new subdir_civ1
2495            if exist(fullfile(Path_ima,subdir_civ1_new),'dir')
2496                   cd(Path_ima);         
2497                [xx,msg1]=mkdir(subdir_civ1_new);
2498                            cd(currentdir);
2499                if ~strcmpl(msg1,'')
2500                    msgbox_uvmat('ERROR',['cannot create ' subdir_civ1_new ': ' msg1])
2501                    cd(currentdir)
2502                    filecell={};
2503                    return
2504                else
2505                    [xx,msg2] = fileattrib(subdir_civ1_new,'+w','g'); %yield writing access (+w) to user group (g)
2506                    if ~strcmp(msg2,'')
2507                        msgbox_uvmat('ERROR',['pb of permission for ' subdir_civ1_new ': ' msg2])%error message for directory creation
2508                        cd(currentdir)
2509                        filecell={};
2510                        return
2511                    end
2512                end
2513            end
2514        end
2515    end
2516    subdir_civ1=subdir_civ1_new;
2517    % get image names
2518    for ifile=1:nbfield
2519        for j=1:nbslice
2520            filename=name_generator(filebase_ima1, num1_civ1(ifile),num_a_civ1(j),ext_ima,nom_type_ima1);
2521            idetect(j)=exist(filename,'file')==2;
2522            filecell.ima1.civ1(ifile,j)={filename}; %first image
2523            filename=name_generator(filebase_ima2, num2_civ1(ifile),num_b_civ1(j),ext_ima,nom_type_ima2);
2524            idetect_1(j)=exist(filename,'file')==2;
2525            filecell.ima2.civ1(ifile,j)={filename};%second image
2526        end
2527        [idetectmin,indexj]=min(idetect);
2528        if idetectmin==0,
2529            msgbox_uvmat('ERROR',[filecell.ima1.civ1{ifile,indexj} ' not found'])
2530            filecell={};
2531            cd(currentdir)
2532            return
2533        end
2534        [idetectmin,indexj]=min(idetect_1);
2535        if idetectmin==0,
2536            msgbox_uvmat('ERROR',[filecell.ima2.civ1{ifile,indexj} ' not found'])
2537            filecell={};
2538            cd(currentdir)
2539            return
2540        end
2541    end
2542    if strcmp(compare,'stereo PIV') && (strcmp(mode,'pair j1-j2') || strcmp(mode,'series(Dj)') || strcmp(mode,'series(Di)'))
2543        for ifile=1:nbfield
2544            for j=1:nbslice
2545                filename=name_generator(filebase_A, num1_civ1(ifile),num_a_civ1(j),ext_ima,nom_type_ima1);
2546                idetect(j)=exist(filename,'file')==2;
2547                filecell.imaA1.civ1(ifile,j)={filename} ;%first image
2548                filename=name_generator(filebase_A, num2_civ1(ifile),num_b_civ1(j),ext_ima,nom_type_ima2);
2549                idetect_1(j)=exist(filename,'file')==2;
2550                filecell.imaA2.civ1(ifile,j)={filename};%second image
2551            end
2552            [idetectmin,indexj]=min(idetect);
2553            if idetectmin==0,
2554                msgbox_uvmat('ERROR',[filecell.imaA1.civ1{ifile,indexj} ' not found'])
2555                filecell={};
2556                cd(currentdir)
2557                return
2558            end
2559            [idetectmin,indexj]=min(idetect_1);
2560            if idetectmin==0,
2561                msgbox_uvmat('ERROR',[filecell.imaA2.civ1{ifile,indexj} ' not found'])
2562                filecell={};
2563                cd(currentdir)
2564                return
2565            end
2566        end
2567    end
2568   
2569    %%%%%%%%%%%%%  fix1 or patch1 activated but no civ1   %%%%%%%%%%%%%
2570elseif (box_test(2)==1 || box_test(3)==1);
2571    for ifile=1:nbfield
2572        for j=1:nbslice
2573            filename=name_generator(filebase_nc,num1_civ1(ifile),num_a_civ1(j),'.nc',...
2574                nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
2575            detect=exist(filename,'file')==2;
2576            if detect==0
2577                msgbox_uvmat('ERROR',[filename ' not found'])
2578                filecell={};
2579                cd(currentdir)
2580                return
2581            end
2582            filecell.nc.civ1(ifile,j)={filename};
2583        end
2584    end
2585    if strcmp(compare,'stereo PIV')
2586        for ifile=1:nbfield
2587            for j=1:nbslice
2588                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);%
2589                filecell.ncA.civ1(ifile,j)={filename};
2590                if ~exist(filename,'file')
2591                    msgbox_uvmat('ERROR',['input file ' filename ' not found'])
2592                    set(handles.RUN, 'Enable','On')
2593                    set(handles.RUN,'BackgroundColor',[1 0 0])
2594                    filecell={};
2595                    cd(currentdir)
2596                    return
2597                end
2598            end
2599        end
2600    end
2601end
2602
2603%%%%%%%%%%%%%  if civ2 performed with pairs different than civ1  %%%%%%%%%%%%%
2604testdiff=0;
2605if (box_test(4)==1)&&...
2606        ((get(handles.list_pair_civ1,'Value')~=get(handles.list_pair_civ2,'Value'))||~strcmp(subdir_civ2,subdir_civ1))
2607    testdiff=1;
2608    detect=1;
2609    vers=0;
2610    subdir_civ2_new=subdir_civ2;
2611    while detect==1 %create a new subdir if the netcdf files already exist
2612        for ifile=1:nbfield
2613            for j=1:nbslice
2614                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);%
2615                detect=exist(filename,'file')==2;
2616                if detect% if a netcdf file already exists
2617                    indstr=regexp(subdir_civ2,'\D');
2618                    if indstr(end)<length(subdir_civ2) %subdir_civ1 ends by a number
2619                        vers=str2double(subdir_civ2(indstr(end)+1:end))+1;
2620                        subdir_civ2_new=[subdir_civ2(1:indstr(end)) num2str(vers)];
2621                    else
2622                        vers=vers+1;
2623                        subdir_civ2_new=[subdir_civ1 '_' num2str(vers)];
2624                    end
2625                    break
2626                end
2627                filecell.nc.civ2(ifile,j)={filename};
2628            end
2629            if detect% if a netcdf file already exists
2630                break
2631            end
2632        end
2633        %create the new subdir_civ2_new
2634        if ~exist(fullfile(Path_ima,subdir_civ2_new),'dir')
2635            [xx,m2]=mkdir(fullfile(Path_ima,subdir_civ2_new));
2636            [xx,msg2] = fileattrib(subdir_civ2_new,'+w','g'); %yield writing access (+w) to user group (g)
2637            if ~isequal(m2,'')
2638                msgbox_uvmat('ERROR',['cannot create ' subdir_civ2_new ': ' m2])
2639                filecell={};
2640                cd(currentdir)
2641                return
2642            end
2643        end
2644        if strcmp(compare,'stereo PIV')%check second nc series
2645            for ifile=1:nbfield
2646                for j=1:nbslice
2647                    filename=name_generator(filebase_A,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2648                        nom_type_nc,1,num2_civ2(ifile),num_b_civ1(j),subdir_civ2_new);%
2649                    detect=exist(filename,'file')==2;
2650                    if detect% if a netcdf file already exists
2651                        indstr=regexp(subdir_civ2,'\D');
2652                        if indstr(end)<length(subdir_civ2) %subdir_civ1 ends by a number
2653                           vers=str2double(subdir_civ2(indstr(end)+1:end))+1;
2654                           subdir_civ2_new=[subdir_civ2(1:indstr(end)) num2str(vers)];
2655                        else
2656                           vers=vers+1;
2657                           subdir_civ2_new=[subdir_civ1 '_' num2str(vers)];
2658                        end
2659                        break
2660                    end
2661                    filecell.ncA.civ2(ifile,j)={filename};
2662                end
2663                if detect% if a netcdf file already exists
2664                    break
2665                end
2666            end
2667            subdir_civ2=subdir_civ2_new;
2668            %create the new subdir_civ1
2669            if ~exist(fullfile(Path_ima,subdir_civ2_new),'dir')
2670                [xx,m2]=mkdir(subdir_civ2_new);
2671                 [xx,msg2] = fileattrib(subdir_civ2_new,'+w','g'); %yield writing access (+w) to user group (g)
2672                if ~isequal(m2,'')
2673                    msgbox_uvmat('ERROR', ['cannot create ' subdir_civ2_new ': ' m2])%error message for directory creation
2674                    cd(currentdir)
2675                    filecell={};
2676                    return
2677                end
2678            end
2679        end
2680    end
2681    subdir_civ2=subdir_civ2_new;
2682end
2683cd(currentdir);%come back to the current working directory
2684
2685%%%%%%%%%%%%%  if civ2 results are obtained or used  %%%%%%%%%%%%%
2686if box_test(4)==1 || box_test(5)==1 || box_test(6)==1 %civ2
2687    %check source netcdf file of civ1 estimates
2688    if box_test(1)==0; %no civ1 performed
2689        for ifile=1:nbfield
2690            for j=1:nbslice
2691                filename=name_generator(filebase_nc,num1_civ1(ifile),num_a_civ1(j),'.nc',...
2692                    nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
2693                filecell.nc.civ1(ifile,j)={filename};% name of the civ1 file
2694                if ~exist(filename,'file')
2695                    msgbox_uvmat('ERROR',['input file ' filename ' not found'])
2696                    filecell={};
2697                    return
2698                end
2699                if ~testdiff % civ2 or patch2 are written in the same file as civ1
2700                    if box_test(4)==0 ; %check the existence of civ2 if it is not calculated
2701                        Data=nc2struct(filename,'ListGlobalAttribute','civ2');
2702                        if isempty(Data.civ2)||isequal(Data.civ2,0)
2703                            msgbox_uvmat('ERROR',['no civ2 data in ' filename])
2704                            filecell=[];
2705                            return
2706                        end
2707                    elseif box_test(3)==0; %check the existence of patch if it is not calculated
2708                        Data=nc2struct(filename,'ListGlobalAttribute','patch');
2709                        if isempty(Data.patch)||isequal(Data.patch,0)
2710                            msgbox_uvmat('ERROR',['no patch data in ' filename])
2711                            filecell=[];
2712                            return
2713                        end
2714                    end
2715                end
2716            end
2717        end
2718        if strcmp(compare,'stereo PIV')
2719            for ifile=1:nbfield
2720                for j=1:nbslice
2721                    filename=name_generator(filebase_A,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2722                        nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
2723                    filecell.ncA.civ2(ifile,j)={filename};
2724                    if ~exist(filename,'file')
2725                        msgbox_uvmat('ERROR',['input file ' filename ' not found'])
2726                        set(handles.RUN, 'Enable','On')
2727                        set(handles.RUN,'BackgroundColor',[1 0 0])
2728                        return
2729                    end
2730                end
2731            end
2732        end
2733    end
2734   
2735    detect=1;
2736    %     while detect==1%creates a new subdir if the netcdf files already contain civ2 data
2737    for ifile=1:nbfield
2738        for j=1:nbslice
2739            filename=name_generator(filebase_nc,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2740                nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);
2741            detect=exist(filename,'file')==2;
2742            filecell.nc.civ2(ifile,j)={filename};
2743        end
2744    end
2745    %get first image names for civ2
2746    if box_test(1)==1 & isequal(num1_civ1,num1_civ2) & isequal(num_a_civ1,num_a_civ2)
2747        filecell.ima1.civ2=filecell.ima1.civ1;
2748    elseif box_test(4)==1
2749        for ifile=1:nbfield
2750            for j=1:nbslice
2751                filename=name_generator(filebase_ima1, num1_civ2(ifile),num_a_civ2(j),ext_ima,nom_type_ima1);
2752                idetect_2(j)=exist(filename,'file')==2;
2753                filecell.ima1.civ2(ifile,j)={filename};%first image
2754            end
2755            [idetectmin,indexj]=min(idetect_2);
2756            if idetectmin==0,
2757                msgbox_uvmat('ERROR',['input image ' filecell.ima1.civ2{ifile,indexj} ' not found'])
2758                filecell=[];
2759                return
2760            end
2761        end
2762    end
2763   
2764    %get second image names for civ2
2765    if box_test(1)==1 & isequal(num2_civ1,num2_civ2) & isequal(num_b_civ1,num_b_civ2)
2766        filecell.ima2.civ2=filecell.ima2.civ1;
2767    elseif box_test(4)==1
2768        for ifile=1:nbfield
2769            for j=1:nbslice
2770                filename=name_generator(filebase_ima2, num2_civ2(ifile),num_b_civ2(j),ext_ima,nom_type_ima2);
2771                idetect_3(j)=exist(filename,'file')==2;
2772                filecell.ima2.civ2(ifile,j)={filename};%first image
2773            end
2774            [idetectmin,indexj]=min(idetect_3);
2775            if idetectmin==0,
2776                msgbox_uvmat('ERROR',['input image ' filecell.ima2.civ2{ifile,indexj} ' not found'])
2777                filecell=[];
2778                return
2779            end
2780        end
2781    end
2782end
2783if (box_test(5)==1 || box_test(6)==1 ) && box_test(4)==0  % need to read an existing netcdf civ2 file
2784    if ~testdiff
2785        filecell.nc.civ2=filecell.nc.civ1;% file already checked
2786    else     % check the civ2 files
2787        for ifile=1:nbfield
2788            for j=1:nbslice
2789                filename=name_generator(filebase_nc,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2790                    nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
2791                filecell.nc.civ2(ifile,j)={filename};
2792                if ~exist(filename,'file')
2793                    msgbox_uvmat('ERROR',['input file ' filename ' not found'])
2794                    filecell=[];
2795                    return
2796                else
2797                    Data=nc2struct(filename,'ListGlobalAttribute','civ2');
2798                    if isempty(Data.civ2)||isequal(Data.civ2,0)
2799                        msgbox_uvmat('ERROR',['no civ2 data in ' filename])
2800                        filecell=[];
2801                        return
2802                    end
2803                end
2804            end
2805        end
2806    end
2807end
2808
2809%%%%%%%%%%%%%  if stereo fields are calculated by PATCH %%%%%%%%%%%%%
2810if strcmp(compare,'stereo PIV')
2811    if  box_test(3)==1 && isequal(get(handles.test_stereo1,'Value'),1)
2812        for ifile=1:nbfield
2813            for j=1:nbslice
2814                filename=name_generator(filebase_AB,num1_civ1(ifile),num_a_civ1(j),'.nc',...
2815                    nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
2816                filecell.st(ifile,j)={filename};
2817            end
2818        end
2819    end
2820    if  box_test(6)==1 && isequal(get(handles.test_stereo2,'Value'),1)
2821        for ifile=1:nbfield
2822            for j=1:nbslice
2823                filename=name_generator(filebase_AB,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2824                    nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
2825                filecell.st(ifile,j)={filename};
2826            end
2827        end
2828    end
2829end
2830set(handles.subdir_civ1,'String',subdir_civ1);%update the edit box
2831set(handles.subdir_civ2,'String',subdir_civ2);%update the edit box
2832browse.nom_type_nc=nom_type_nc;
2833set(handles.browse_root,'UserData',browse); %update the nomenclature type for uvmat
2834
2835
2836%COPY IMAGES TO THE FORMAT .png IF NEEDED
2837if isequal(nom_type_ima1,'*')%case of movie files
2838    nom_type_imanew1='_i';
2839else
2840    nom_type_imanew1=nom_type_ima1;
2841end
2842if isequal(nom_type_ima2,'*')%case of movie files
2843    nom_type_imanew2='_i';
2844else
2845    nom_type_imanew2=nom_type_ima2;
2846end
2847if ~isequal(ext_ima,'.png')
2848    %%type of image file
2849    type_ima1='none';%default
2850    movieobject1=[];%default
2851    if strcmpi(ext_ima,'.avi')
2852        hhh=which('mmreader');
2853        if ~isequal(hhh,'')&& mmreader.isPlatformSupported()% if the mmreader function is found (recent version of matlab)
2854            type_ima1='movie';
2855            movieobject1=mmreader([filebase_ima2 ext_ima]);
2856        else
2857            type_ima1='avi';
2858        end
2859    elseif ischar(ext_ima) && ~isempty(ext_ima(2:end))
2860        form=imformats(ext_ima(2:end));
2861        if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
2862            if isequal(nom_type_ima1,'*');
2863                type_ima1='multimage';%image series in a single image file
2864            else
2865                type_ima1='image';
2866            end
2867        end
2868    end
2869    type_ima2='none';%default
2870    movieobject2=[];
2871    if strcmpi(ext_ima,'.avi')
2872        hhh=which('mmreader');
2873        if ~isequal(hhh,'')&& mmreader.isPlatformSupported()% if the mmreader function is found (recent version of matlab)
2874            type_ima2='movie';
2875            movieobject2=mmreader([filebase_ima2 ext_ima]);
2876        else
2877            type_ima2='avi';
2878        end
2879    elseif ischar(ext_ima) && ~isempty(ext_ima(2:end))
2880        form=imformats(ext_ima(2:end));
2881        if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
2882            if isequal(nom_type_ima1,'*');
2883                type_ima2='multimage';%image series in a single image file
2884            else
2885                type_ima2='image';
2886            end
2887        end
2888    end
2889    %npxy=get(handles.ImaExt,'UserData');
2890    % %     if numel(npxy)<2
2891    %
2892    %         filename=name_generator(filebase_ima1,num1_civ1(1),num_a_civ1(1),ImaExt,nom_type_ima1);
2893    %         A=imread(filename);
2894    %         npxy=size(A);
2895    % %     end
2896    %     npy=npxy(1);
2897    %     npx=npxy(2);
2898    if box_test(1)==1 %if civ1 is performed
2899        h = waitbar(0,'copy images to the .png format for civ1');% display a wait bar
2900        for ifile=1:nbfield
2901            waitbar(ifile/nbfield);
2902            for j=1:nbslice
2903                filename=name_generator(filebase_ima1,num1_civ1(ifile),num_a_civ1(j),'.png',nom_type_imanew1);
2904                if ~exist(filename,'file')
2905                    A=read_image(filecell.ima1.civ1{ifile,j},type_ima1,num1_civ1(ifile),movieobject1);
2906                    imwrite(A,filename,'BitDepth',16);
2907                end
2908                filecell.ima1.civ1(ifile,j)={filename};
2909                filename=name_generator(filebase_ima2, num2_civ1(ifile),num_b_civ1(j),'.png',nom_type_imanew2);
2910                if ~exist(filename,'file')
2911                    A=read_image(filecell.ima2.civ1{ifile,j},type_ima2,num2_civ1(ifile),movieobject2);
2912                    imwrite(A,filename,'BitDepth',16);
2913                end
2914                filecell.ima2.civ1(ifile,j)={filename};
2915            end
2916        end
2917        close(h)
2918    end
2919    if box_test(4)==1 %if civ2 is performed
2920        h = waitbar(0,'copy images to the .png format for civ2');% display a wait bar
2921        for ifile=1:nbfield
2922            waitbar(ifile/nbfield);
2923            for j=1:nbslice
2924                filename=name_generator(filebase_ima1,num1_civ2(ifile),num_a_civ2(j),'.png',nom_type_imanew1);
2925                if ~exist(filename,'file')
2926                    A=read_image(cell2mat(filecell.ima1.civ2(ifile,j)),type_ima2,num1_civ2(ifile));
2927                    imwrite(A,filename,'BitDepth',16);
2928                end
2929                filecell.ima1.civ2(ifile,j)={filename};
2930                filename=name_generator(filebase_ima2, num2_civ2(ifile),num_b_civ2(j),'.png',nom_type_imanew2);
2931                if ~exist(filename,'file')
2932                    A=read_image(cell2mat(filecell.ima2.civ2(ifile,j)),type_ima2,num2_civ2(ifile));
2933                    imwrite(A,filename,'BitDepth',16);
2934                end
2935                filecell.ima2.civ2(ifile,j)={filename};
2936            end
2937        end
2938        close(h);
2939    end
2940end
2941
2942%------------------------------------------------------------------------
2943% --- determine the list of index pairs of processing file
2944function [num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2]=...
2945    find_pair_indices(handles,ref_i,ref_j,mode)
2946%------------------------------------------------------------------------
2947
2948list_civ1=get(handles.list_pair_civ1,'String');
2949index_civ1=get(handles.list_pair_civ1,'Value');
2950str_civ1=list_civ1{index_civ1};%string defining the image pairs for civ1
2951if isempty(str_civ1)||isequal(str_civ1,'')
2952    msgbox_uvmat('ERROR','no image pair selected for civ1')
2953    return
2954end
2955list_civ2=get(handles.list_pair_civ2,'String');
2956index_civ2=get(handles.list_pair_civ2,'Value');
2957if index_civ2>length(list_civ2)
2958    list_civ2=list_civ1;
2959    index_civ2=index_civ1;
2960end
2961str_civ2=list_civ2{index_civ2};%string defining the image pairs for civ2
2962
2963if isequal (mode,'series(Di)')
2964    lastfield=str2double(get(handles.nb_field,'String'));
2965    num1_civ1=ref_i-floor(index_civ1/2)*ones(size(ref_i));% set of first image numbers
2966    num2_civ1=ref_i+ceil(index_civ1/2)*ones(size(ref_i));
2967    num_a_civ1=ref_j;
2968    num_b_civ1=ref_j;
2969    num1_civ2=ref_i-floor(index_civ2/2)*ones(size(ref_i));
2970    num2_civ2=ref_i+ceil(index_civ2/2)*ones(size(ref_i));
2971    num_a_civ2=ref_j;
2972    num_b_civ2=ref_j;   
2973   
2974    % adjust the first and last field number
2975    lastfield=str2double(get(handles.nb_field,'String'));
2976    if isnan(lastfield)
2977        indsel=find((num1_civ1 >= 1)&(num1_civ2 >= 1));
2978    else
2979        indsel=find((num2_civ1 <= lastfield)&(num2_civ2 <= lastfield)&(num1_civ1 >= 1)&(num1_civ2 >= 1));
2980    end
2981    if length(indsel)>=1
2982        firstind=indsel(1);
2983        lastind=indsel(end);
2984        set(handles.first_i,'String',num2str(ref_i(firstind)))%update the display of first and last fields
2985        set(handles.last_i,'String',num2str(ref_i(lastind)))
2986        ref_i=ref_i(indsel);
2987        num1_civ1=num1_civ1(indsel);
2988        num1_civ2=num1_civ2(indsel);
2989        num2_civ1=num2_civ1(indsel);
2990        num2_civ2=num2_civ2(indsel);
2991    end
2992elseif isequal (mode,'series(Dj)')
2993    lastfield_j=str2double(get(handles.nb_field2,'String'));
2994    num1_civ1=ref_i;% set of first image numbers
2995    num2_civ1=ref_i;
2996    num_a_civ1=ref_j-floor(index_civ1/2)*ones(size(ref_j));
2997    num_b_civ1=ref_j+ceil(index_civ1/2)*ones(size(ref_j));
2998    num1_civ2=ref_i;
2999    num2_civ2=ref_i;
3000    num_a_civ2=ref_j-floor(index_civ2/2)*ones(size(ref_j));
3001    num_b_civ2=ref_j+ceil(index_civ2/2)*ones(size(ref_j));
3002    % adjust the first and last field number
3003    if isnan(lastfield_j)
3004        indsel=find((num_a_civ1 >= 1)&(num_a_civ2 >= 1));
3005    else
3006        indsel=find((num_b_civ1 <= lastfield_j)&(num_b_civ2 <= lastfield_j)&(num_a_civ1 >= 1)&(num_a_civ2 >= 1));
3007    end
3008    if length(indsel)>=1
3009        firstind=indsel(1);
3010        lastind=indsel(end);
3011        set(handles.first_j,'String',num2str(ref_j(firstind)))%update the display of first and last fields
3012        set(handles.last_j,'String',num2str(ref_j(lastind)))
3013        ref_j=ref_j(indsel);
3014        num_a_civ1=num_a_civ1(indsel);
3015        num_b_civ1=num_b_civ1(indsel);
3016        num_a_civ2=num_a_civ2(indsel);
3017        num_b_civ2=num_b_civ2(indsel);
3018    end
3019elseif isequal(mode,'pair j1-j2') %case of bursts (png_old or png_2D)
3020    displ_num=get(handles.list_pair_civ1,'UserData');
3021    num1_civ1=ref_i;
3022    num2_civ1=ref_i;
3023    num_a_civ1=displ_num(1,index_civ1);
3024    num_b_civ1=displ_num(2,index_civ1);
3025    num1_civ2=ref_i;
3026    num2_civ2=ref_i;
3027    num_a_civ2=displ_num(1,index_civ2);
3028    num_b_civ2=displ_num(2,index_civ2);
3029elseif isequal(mode,'displacement')
3030    num1_civ1=ref_i;
3031    num2_civ1=ref_i;
3032    num_a_civ1=ref_j;
3033    num_b_civ1=ref_j;
3034    num1_civ2=ref_i;
3035    num2_civ2=ref_i;
3036    num_a_civ2=ref_j;
3037    num_b_civ2=ref_j;
3038end
3039
3040
3041%------------------------------------------------------------------------
3042% --- PATCH
3043function cmd_PATCH=PATCH_CMD(filename_nc,nx_patch,ny_patch,rho_patch,subdomain_patch,thresh_value,test_interp,PatchBin)
3044%------------------------------------------------------------------------
3045namelog=[filename_nc(1:end-3) '_patch.log'];
3046if test_interp==0
3047    if isunix
3048    cmd_PATCH=[PatchBin ' -f ' filename_nc ' -m ' nx_patch  ' -n ' ny_patch ' -ro ' rho_patch ' -nopt ' subdomain_patch ...
3049        '  > ' namelog ' 2>&1']; % redirect standard output to the log file
3050    else
3051      cmd_PATCH=['"' PatchBin '" -f "' filename_nc '" -m ' nx_patch  ' -n ' ny_patch ' -ro ' rho_patch ' -nopt ' subdomain_patch ...
3052        '  > "' namelog '" 2>&1']; % redirect standard output to the log file
3053    end
3054else %nouveau programme patch
3055    cmd_PATCH=[PatchBin ' -f ' filename_nc ' -m ' nx_patch  ' -n ' ny_patch ' -ro ' rho_patch ...
3056        ' -max ' thresh_value ' -nopt ' subdomain_patch  '  > ' namelog ' 2>&1']; % redirect standard output to the log file
3057end
3058cmd_PATCH=regexprep(cmd_PATCH,'\\','\\\\');
3059%------------------------------------------------------------------------
3060% --- STEREO Interp
3061function cmd=RUN_STINTERP(stinterpBin,filename_A_nc,filename_B_nc,filename_nc,nx_patch,ny_patch,rho_patch,subdomain_patch,thresh_value,xmlA,xmlB)
3062%------------------------------------------------------------------------
3063namelog=[filename_nc(1:end-3) '_stinterp.log'];
3064cmd=[stinterpBin ' -f1 ' filename_A_nc  ' -f2 ' filename_B_nc ' -f  ' filename_nc ...
3065    ' -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
3066
3067%------------------------------------------------------------------------
3068% --- Executes on button press in CIV1.
3069function CIV1_Callback(hObject, eventdata, handles)
3070%------------------------------------------------------------------------
3071val=get(handles.CIV1,'Value');
3072if isequal(val,1)
3073    enable_civ1(handles,'on')
3074    enable_pair1(handles,'on')
3075else
3076    enable_civ1(handles,'off')
3077end
3078find_netcpair_civ1(hObject, eventdata, handles);
3079
3080%------------------------------------------------------------------------
3081% --- Executes on button press in FIX1.
3082function FIX1_Callback(hObject, eventdata, handles)
3083%------------------------------------------------------------------------
3084enable_fix1(handles,get(handles.FIX1,'Value'))
3085
3086%------------------------------------------------------------------------
3087% --- Executes on button press in PATCH1.
3088function PATCH1_Callback(hObject, eventdata, handles)
3089%------------------------------------------------------------------------
3090if get(handles.PATCH1,'Value')==1
3091    enable_patch1(handles)
3092else
3093    desable_patch1(handles)
3094end
3095
3096%------------------------------------------------------------------------
3097% --- Executes on button press in CIV2.
3098function CIV2_Callback(hObject, eventdata, handles)
3099%------------------------------------------------------------------------
3100state=get(handles.CIV2,'Value');
3101enable_civ2(handles,state)
3102if state
3103    find_netcpair_civ2(hObject, eventdata, handles)
3104    enable_pair1(handles,'on')
3105end
3106
3107%------------------------------------------------------------------------
3108% --- Executes on button press in FIX2.
3109function FIX2_Callback(hObject, eventdata, handles)
3110%------------------------------------------------------------------------
3111if get(handles.FIX2,'Value')==1
3112    enable_fix2(handles)
3113    if get(handles.CIV2,'Value')==0
3114        find_netcpair_civ2(hObject, eventdata, handles) % select the available netcdf files
3115    end
3116else
3117    desable_fix2(handles)
3118end
3119
3120%------------------------------------------------------------------------
3121% --- Executes on button press in PATCH2.
3122function PATCH2_Callback(hObject, eventdata, handles)
3123%------------------------------------------------------------------------
3124if get(handles.PATCH2,'Value')==1
3125    enable_patch2(handles)
3126    if get(handles.CIV2,'Value')==0
3127        find_netcpair_civ2(hObject, eventdata, handles) % select the available netcdf files
3128    end
3129else
3130    desable_patch2(handles)
3131end
3132
3133%------------------------------------------------------------------------
3134function first_i_Callback(hObject, eventdata, handles)
3135%------------------------------------------------------------------------
3136% last_i_Callback(hObject, eventdata, handles)
3137first_i=str2double(get(handles.first_i,'String'));
3138% last_i=str2num(get(handles.last_i,'String'));
3139% ref_i=ceil((first_i+last_i)/2);
3140set(handles.ref_i,'String', num2str(first_i))% reference index for pair dt = first index
3141set(handles.ref_i_civ2,'String', num2str(first_i))% reference index for pair dt = first index
3142ref_i_Callback(hObject, eventdata, handles)%refresh dispaly of dt for pairs (in case of non constant dt)
3143
3144%------------------------------------------------------------------------
3145function first_j_Callback(hObject, eventdata, handles)
3146%------------------------------------------------------------------------
3147first_j=str2num(get(handles.first_j,'String'));
3148set(handles.ref_j,'String', num2str(first_j))% reference index for pair dt = first index
3149ref_j_Callback(hObject, eventdata, handles)%refresh dispaly of dt for pairs (in case of non constant dt)
3150
3151%------------------------------------------------------------------------
3152% --- Executes on button press in calcul_search: determine the search range isx,isy
3153function calcul_search_Callback(hObject, eventdata, handles)
3154%------------------------------------------------------------------------
3155%determine pair numbers
3156list_pair=get(handles.list_pair_civ1,'String');%get the menu of image pairs
3157index=get(handles.list_pair_civ1,'Value');
3158displ_num=get(handles.list_pair_civ1,'UserData');
3159time=get(handles.RootName,'UserData'); %get the set of times
3160pxcm_xy=get(handles.calcul_search,'UserData');
3161pxcmx=pxcm_xy(1);
3162pxcmy=pxcm_xy(2);
3163mode_list=get(handles.mode,'String');
3164mode_value=get(handles.mode,'Value');
3165mode=mode_list{mode_value};
3166if isequal (mode, 'series(Di)' )
3167    ref_i=str2double(get(handles.ref_i,'String'));
3168    num1=ref_i-floor(index/2);%  first image numbers
3169    num2=ref_i+ceil(index/2);
3170    num_a=1;
3171    num_b=1;
3172elseif isequal (mode, 'series(Dj)')
3173    num1=1;
3174    num2=1;
3175    ref_j=str2double(get(handles.ref_j,'String'));
3176    num_a=ref_j-floor(index/2);%  first image numbers
3177    num_b=ref_j+ceil(index/2);
3178elseif isequal(mode,'pair j1-j2') %case of bursts (png_old or png_2D)
3179    ref_i=str2double(get(handles.ref_i,'String'));
3180    num1=ref_i;
3181    num2=ref_i;
3182    num_a=displ_num(1,index);
3183    num_b=displ_num(2,index);
3184end
3185dt=time(num2,num_b)-time(num1,num_a);
3186ibx=str2double(get(handles.ibx,'String'));
3187iby=str2double(get(handles.iby,'String'));
3188umin=dt*pxcmx*str2double(get(handles.umin,'String'));
3189umax=dt*pxcmx*str2double(get(handles.umax,'String'));
3190vmin=dt*pxcmy*str2double(get(handles.vmin,'String'));
3191vmax=dt*pxcmy*str2double(get(handles.vmax,'String'));
3192shiftx=round((umin+umax)/2);
3193shifty=round((vmin+vmax)/2);
3194isx=(umax+2-shiftx)*2+ibx;
3195isx=2*ceil(isx/2)+1;
3196isy=(vmax+2-shifty)*2+iby;
3197isy=2*ceil(isy/2)+1;
3198set(handles.shiftx,'String',num2str(shiftx));
3199set(handles.shifty,'String',num2str(shifty));
3200set(handles.isx,'String',num2str(isx));
3201set(handles.isy,'String',num2str(isy));
3202
3203%------------------------------------------------------------------------
3204% --- Executes on carriage return on the subdir civ1 edit window
3205function subdir_civ1_Callback(hObject, eventdata, handles)
3206%------------------------------------------------------------------------
3207subdir=get(handles.subdir_civ1,'String');
3208set(handles.subdir_civ2,'String',subdir);
3209if get(handles.CIV1,'Value')==0
3210    find_netcpair_civ1(hObject, eventdata, handles); %update the list of available pairs from netcdf files in the new directory
3211end
3212
3213%------------------------------------------------------------------------
3214% --- Executes on carriage return on the subdir civ1 edit window
3215function subdir_civ2_Callback(hObject, eventdata, handles)
3216%------------------------------------------------------------------------
3217%update the list of available pairs from netcdf files in the new directory
3218if get(handles.CIV2,'Value')==0 & get(handles.CIV1,'Value')==0 & get(handles.FIX1,'Value')==0 & get(handles.PATCH1,'Value')==0
3219    find_netcpair_civ2(hObject, eventdata, handles);
3220end
3221
3222%------------------------------------------------------------------------
3223% --- Executes on button press in get_mask_civ1: select box for mask option
3224function get_mask_civ1_Callback(hObject, eventdata, handles)
3225%------------------------------------------------------------------------
3226maskval=get(handles.get_mask_civ1,'Value');
3227if isequal(maskval,0)
3228    set(handles.mask_civ1,'String','')
3229else
3230    mask_displ='no mask'; %default
3231    filebase=get(handles.RootName,'String');
3232    [ nbslice_mask, flag_mask]=get_mask(filebase,handles);
3233    if isequal(flag_mask,1)
3234        mask_displ=[num2str(nbslice_mask) 'mask'];
3235    elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3236        common_path=fileparts(filebase);
3237        filebase_a=fullfile(common_path,get(handles.RootName_1,'String'));
3238        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3239        if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice_mask)
3240            mask_displ='no mask';
3241        end
3242    end
3243    if isequal(mask_displ,'no mask')
3244        [FileName, PathName, filterindex] = uigetfile( ...
3245            {'*.png', ' (*.png)';
3246            '*.png',  '.png files '; ...
3247            '*.*', 'All Files (*.*)'}, ...
3248            'Pick a mask file *.png',filebase);
3249        mask_displ=fullfile(PathName,FileName);
3250        if ~exist(mask_displ,'file')
3251            mask_displ='no mask';
3252        end
3253    end
3254    if isequal(mask_displ,'no mask')
3255        set(handles.get_mask_civ1,'Value',0)
3256        set(handles.get_mask_fix1,'Value',0)
3257        set(handles.get_mask_civ2,'Value',0)
3258        set(handles.get_mask_fix2,'Value',0)
3259    else
3260        set(handles.get_mask_fix1,'Value',1)
3261        set(handles.get_mask_fix2,'Value',1)
3262    end
3263    set(handles.mask_civ1,'String',mask_displ)
3264    set(handles.mask_fix1,'String',mask_displ)
3265    set(handles.mask_civ2,'String',mask_displ)
3266    set(handles.mask_fix2,'String',mask_displ)
3267end
3268set(handles.get_mask_civ2,'Value',maskval)%update the civ2 mask with the same option as civ1
3269
3270%------------------------------------------------------------------------
3271% --- Executes on button press in get_mask_fix1.
3272function get_mask_fix1_Callback(hObject, eventdata, handles)
3273%------------------------------------------------------------------------
3274maskval=get(handles.get_mask_fix1,'Value');
3275if isequal(maskval,0)
3276    set(handles.mask_fix1,'String','')
3277else
3278    mask_displ='no mask'; %default
3279    filebase=get(handles.RootName,'String');
3280    [nbslice, flag_mask]=get_mask(filebase,handles);
3281    if isequal(flag_mask,1)
3282        mask_displ=[num2str(nbslice) 'mask'];
3283    elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3284        filebase_a=get(handles.RootName_1,'String');
3285        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3286        if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice)
3287            mask_displ='no mask';
3288        end
3289    end
3290    if isequal(mask_displ,'no mask')
3291        [FileName, PathName, filterindex] = uigetfile( ...
3292            {'*.png', ' (*.png)';
3293            '*.png',  '.png files '; ...
3294            '*.*', 'All Files (*.*)'}, ...
3295            'Pick a mask file *.png',filebase);
3296        mask_displ=fullfile(PathName,FileName);
3297        if ~exist(mask_displ,'file')
3298            mask_displ='no mask';
3299        end
3300    end
3301    if isequal(mask_displ,'no mask')
3302        set(handles.get_mask_fix1,'Value',0)
3303        set(handles.get_mask_civ2,'Value',0)
3304        set(handles.get_mask_fix2,'Value',0)
3305    else
3306        %set(handles.get_mask_civ2,'Value',1)
3307        set(handles.get_mask_fix2,'Value',1)
3308    end
3309    set(handles.mask_fix1,'String',mask_displ)
3310    set(handles.mask_civ2,'String',mask_displ)
3311    set(handles.mask_fix2,'String',mask_displ)
3312end
3313
3314%------------------------------------------------------------------------
3315% --- Executes on button press in get_mask_civ2: select box for mask option
3316function get_mask_civ2_Callback(hObject, eventdata, handles)
3317%------------------------------------------------------------------------
3318maskval=get(handles.get_mask_civ2,'Value');
3319if isequal(maskval,0)
3320    set(handles.mask_civ2,'String','')
3321else
3322    mask_displ='no mask'; %default
3323    filebase=get(handles.RootName,'String');
3324    [nbslice, flag_mask]=get_mask(filebase,handles);
3325    if isequal(flag_mask,1)
3326        mask_displ=[num2str(nbslice) 'mask'];
3327    elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3328        filebase_a=get(handles.RootName_1,'String');
3329        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3330        if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice)
3331            mask_displ='no mask';
3332        end
3333    end
3334    if isequal(mask_displ,'no mask')
3335        [FileName, PathName, filterindex] = uigetfile( ...
3336            {'*.png', ' (*.png)';
3337            '*.png',  '.png files '; ...
3338            '*.*', 'All Files (*.*)'}, ...
3339            'Pick a mask file *.png',filebase);
3340        mask_displ=fullfile(PathName,FileName);
3341        if ~exist(mask_displ,'file')
3342            mask_displ='no mask';
3343        end
3344    end
3345    if isequal(mask_displ,'no mask')
3346        set(handles.get_mask_civ2,'Value',0)
3347        set(handles.get_mask_fix2,'Value',0)
3348    else
3349        set(handles.get_mask_fix2,'Value',1)
3350    end
3351    set(handles.mask_civ2,'String',mask_displ)
3352    set(handles.mask_fix2,'String',mask_displ)
3353end
3354
3355%------------------------------------------------------------------------
3356% --- Executes on button press in get_mask_fix2.
3357function get_mask_fix2_Callback(hObject, eventdata, handles)
3358%------------------------------------------------------------------------
3359maskval=get(handles.get_mask_fix2,'Value');
3360if isequal(maskval,0)
3361    set(handles.mask_fix2,'String','')
3362else
3363    mask_displ='no mask'; %default
3364    filebase=get(handles.RootName,'String');
3365    [nbslice, flag_mask]=get_mask(filebase,handles);
3366    if isequal(flag_mask,1)
3367        mask_displ=[num2str(nbslice) 'mask'];
3368    elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3369        filebase_a=get(handles.RootName_1,'String');
3370        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3371        if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice)
3372            mask_displ='no mask';
3373        end
3374    end
3375    if isequal(mask_displ,'no mask')
3376        [FileName, PathName, filterindex] = uigetfile( ...
3377            {'*.png', ' (*.png)';
3378            '*.png',  '.png files '; ...
3379            '*.*', 'All Files (*.*)'}, ...
3380            'Pick a mask file *.png',filebase);
3381        mask_displ=fullfile(PathName,FileName);
3382        if ~exist(mask_displ,'file')
3383            mask_displ='no mask';
3384        end
3385    end
3386    if isequal(mask_displ,'no mask')
3387        set(handles.get_mask_fix2,'Value',0)
3388    end
3389    set(handles.mask_fix2,'String',mask_displ)
3390end
3391
3392%------------------------------------------------------------------------
3393% --- function called to look for mask files
3394function [nbslice, flag_mask]=get_mask(filebase,handles)
3395%------------------------------------------------------------------------
3396%detect mask files, images with appropriate file base
3397%[filebase '_' xx 'mask'], xx=nbslice
3398%flag_mask=1 indicates detection
3399
3400flag_mask=0;%default
3401nbslice=1;
3402
3403% subdir=get(handles.subdir_civ1,'String');
3404[Path,Name]=fileparts(filebase);
3405if ~isdir(Path)
3406    msgbox_uvmat('ERROR','no path for input files')
3407    return
3408end
3409currentdir=pwd;
3410cd(Path);%move in the dir of the root name filebase
3411maskfiles=dir([Name '_*mask_*.png']);%look for mask files
3412cd(currentdir);%come back to the current working directory
3413if ~isempty(maskfiles)
3414    %     msgbox_uvmat('ERROR','no mask available, to create it use Tools/Make mask in the upper menu bar of uvmat')
3415    % else
3416    flag_mask=1;
3417    maskname=maskfiles(1).name;% take the first mask file in the list
3418    [Path2,Name,ext]=fileparts(maskname);
3419    Namedouble=double(Name);
3420    val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
3421    ind_mask=findstr('mask',Name);
3422    i=ind_mask-1;
3423    while val(i)==0 && i>0
3424        i=i-1;
3425    end
3426    nbslice=str2double(Name(i+1:ind_mask-1));
3427    if ~isnan(nbslice) && Name(i)=='_'
3428        flag_mask=1;
3429    else
3430        msgbox_uvmat('ERROR',['bad mask file ' Name ext ' found in ' Path2])
3431        return
3432        nbslice=1;
3433    end
3434end
3435
3436%------------------------------------------------------------------------
3437% --- function called to look for grid files
3438function [nbslice, flag_mask]=get_grid(filebase,handles)
3439%------------------------------------------------------------------------
3440flag_mask=0;%default
3441nbslice=1;
3442[Path,Name]=fileparts(filebase);
3443currentdir=pwd;
3444cd(Path);%move in the dir of the root name filebase
3445maskfiles=dir([Name '_*grid_*.grid']);%look for mask files
3446cd(currentdir);%come back to the current working directory
3447if ~isempty(maskfiles)
3448    flag_mask=1;
3449    maskname=maskfiles(1).name;% take the first mask file in the list
3450    [Path2,Name,ext]=fileparts(maskname);
3451    Namedouble=double(Name);
3452    val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
3453    ind_mask=findstr('grid',Name);
3454    i=ind_mask-1;
3455    while val(i)==0 && i>0
3456        i=i-1;
3457    end
3458    nbslice=str2double(Name(i+1:ind_mask-1));
3459    if ~isnan(nbslice) && Name(i)=='_'
3460        flag_mask=1;
3461    else
3462        msgbox_uvmat('ERROR',['bad grid file ' Name ext ' found in ' Path2])
3463        return
3464        nbslice=1;
3465    end
3466end
3467
3468%------------------------------------------------------------------------
3469% --- transform numbers to letters
3470function str=num2stra(num,nom_type)
3471%------------------------------------------------------------------------
3472if isempty(nom_type)
3473    str='';
3474elseif strcmp(nom_type(end),'a')
3475    str=char(96+num);
3476elseif strcmp(nom_type(end),'A')
3477    str=char(96+num);
3478elseif isempty(nom_type(2:end))%a single index
3479    str='';
3480else
3481    str=num2str(num);
3482end
3483
3484% %------------------------------------------------------------------------
3485% function mask_civ1_Callback(hObject, eventdata, handles)
3486% %------------------------------------------------------------------------
3487% set(handles.mask_civ1,'UserData',[])
3488% set(handles.mask_civ1,'String','')
3489%
3490% %------------------------------------------------------------------------
3491% function mask_civ2_Callback(hObject, eventdata, handles)
3492% %------------------------------------------------------------------------
3493% set(handles.mask_civ2,'UserData',[])
3494% set(handles.mask_civ2,'String','')
3495%
3496% %------------------------------------------------------------------------
3497% function mask_fix1_Callback(hObject, eventdata, handles)
3498% %------------------------------------------------------------------------
3499% set(handles.mask_fix1,'UserData',[])
3500% set(handles.mask_fix1,'String','')
3501%
3502% %------------------------------------------------------------------------
3503% function mask_fix2_Callback(hObject, eventdata, handles)
3504% %------------------------------------------------------------------------
3505% set(handles.mask_fix2,'UserData',[])
3506% set(handles.mask_fix2,'String','')
3507
3508%------------------------------------------------------------------------
3509% --- Executes on button press in list_subdir_civ1.
3510function list_subdir_civ1_Callback(hObject, eventdata, handles)
3511%------------------------------------------------------------------------
3512list_subdir_civ1=get(handles.list_subdir_civ1,'String');
3513val=get(handles.list_subdir_civ1,'Value');
3514if val>1
3515    subdir=list_subdir_civ1{val};
3516    set(handles.subdir_civ1,'String',subdir);
3517    set(handles.list_subdir_civ1,'Value',1);
3518end
3519
3520%------------------------------------------------------------------------
3521% --- Executes on button press in list_subdir_civ2.
3522function list_subdir_civ2_Callback(hObject, eventdata, handles)
3523%------------------------------------------------------------------------
3524list_subdir_civ2=get(handles.list_subdir_civ2,'String');
3525val=get(handles.list_subdir_civ2,'Value');
3526if val>1
3527    subdir=list_subdir_civ2{val};
3528    set(handles.subdir_civ2,'String',subdir);
3529    set(handles.list_subdir_civ2,'Value',1);
3530end
3531
3532%------------------------------------------------------------------------
3533% --- Executes on button press in browse_gridciv1.
3534function browse_gridciv1_Callback(hObject, eventdata, handles)
3535%------------------------------------------------------------------------
3536value=get(handles.browse_gridciv1,'Value');
3537testgrid=0;
3538if value
3539    filebase=get(handles.RootName,'String');
3540    [nbslice, flag_grid]=get_grid(filebase,handles);
3541    if isequal(flag_grid,1)
3542        filegrid=[num2str(nbslice) 'grid'];
3543        testgrid=1;
3544    else
3545        [FileName, PathName, filterindex] = uigetfile( ...
3546            {'*.grid', ' (*.grid)';
3547            '*.grid',  '.grid files '; ...
3548            '*.*', 'All Files (*.*)'}, ...
3549            'Pick a file',filebase);
3550        filegrid=fullfile(PathName,FileName);
3551        if ~(isempty(FileName)||isempty(PathName)||isequal(FileName,0)||~exist(filegrid,'file'))
3552            testgrid=1;
3553        end
3554    end
3555end
3556if testgrid
3557    set(handles.browse_gridciv2,'Value',1)
3558    set(handles.get_gridpatch1,'Value',1)
3559    set(handles.get_gridpatch2,'Value',1)
3560    set(handles.dx_civ1,'Visible','off');
3561    set(handles.dy_civ1,'Visible','off');
3562    set(handles.dx_civ2,'Visible','off');
3563    set(handles.dy_civ2,'Visible','off');
3564    set(handles.grid_civ1,'String',filegrid)
3565    set(handles.grid_patch1,'String',filegrid)
3566    set(handles.grid_civ2,'String',filegrid)
3567    set(handles.grid_patch2,'String',filegrid)
3568else
3569    set(handles.browse_gridciv1,'Value',0);
3570    set(handles.browse_gridciv2,'Value',0);
3571    set(handles.get_gridpatch1,'Value',0)
3572    set(handles.get_gridpatch2,'Value',0)
3573    set(handles.dx_civ1,'Visible','on');
3574    set(handles.dy_civ1,'Visible','on');
3575    set(handles.dx_civ2,'Visible','on');
3576    set(handles.dy_civ2,'Visible','on');
3577    set(handles.grid_civ1,'String','')
3578    set(handles.grid_patch1,'String','')
3579    set(handles.grid_civ2,'String','')
3580    set(handles.grid_patch2,'String','')
3581end
3582
3583%------------------------------------------------------------------------
3584% --- Executes on button press in browse_gridciv1.
3585function browse_gridciv2_Callback(hObject, eventdata, handles)
3586%------------------------------------------------------------------------
3587value=get(handles.browse_gridciv2,'Value');
3588if value
3589    filebase=get(handles.RootName,'String');
3590    [nbslice, flag_grid]=get_grid(filebase,handles);
3591    if isequal(flag_grid,1)
3592        mask_displ=[num2str(nbslice) 'grid'];
3593        set(handles.grid_civ2,'String',mask_displ)
3594        set(handles.dx_civ2,'Visible','off');
3595        set(handles.dy_civ2,'Visible','off');
3596    else
3597        [FileName, PathName, filterindex] = uigetfile( ...
3598            {'*.grid', ' (*.grid)';
3599            '*.grid',  '.grid files '; ...
3600            '*.*', 'All Files (*.*)'}, ...
3601            'Pick a file',filebase);
3602        filegrid=fullfile(PathName,FileName);
3603        if isempty(FileName)||isempty(PathName)||isequal(FileName,0)||~exist(filegrid,'file')
3604            set(handles.browse_gridciv2,'Value',0);
3605            set(handles.grid_civ2,'string','');
3606            set(handles.dx_civ2,'Visible','on');
3607            set(handles.dy_civ2,'Visible','on');
3608            set(handles.grid_civ2,'string','');
3609        else
3610            set(handles.grid_civ2,'string',filegrid);
3611            set(handles.dx_civ2,'Visible','off');
3612            set(handles.dy_civ2,'Visible','off');
3613            set(handles.grid_civ2,'string',filegrid);
3614        end
3615    end
3616else
3617    set(handles.grid_civ2,'string','');
3618    set(handles.dx_civ2,'Visible','on');
3619    set(handles.dy_civ2,'Visible','on');
3620    set(handles.grid_civ2,'string','');
3621end
3622
3623% % --- Executes on button press in browse_gridciv2.
3624% function browse_gridciv2_Callback(hObject, eventdata, handles)
3625%
3626% filebase=get(handles.RootName,'String');
3627% [FileName, PathName, filterindex] = uigetfile( ...
3628%        {'*.grid', ' (*.grid)';
3629%         '*.grid',  '.grid files '; ...
3630%         '*.*', 'All Files (*.*)'}, ...
3631%         'Pick a file',filebase);
3632% filegrid=fullfile(PathName,FileName);
3633% set(handles.grid_civ2,'string',filegrid);
3634% set(handles.dx_civ2,'String',' ');
3635% set(handles.dy_civ2,'String',' ');
3636% % set(handles.grid_patch2,'string',filegrid);
3637
3638% --- Executes on button press in get_gridpatch1.
3639function get_gridpatch1_Callback(hObject, eventdata, handles)
3640% hObject    handle to get_gridpatch1 (see GCBO)
3641% eventdata  reserved - to be defined in a future version of MATLAB
3642% handles    structure with handles and user data (see GUIDATA)
3643
3644filebase=get(handles.RootName,'String');
3645[FileName, PathName, filterindex] = uigetfile( ...
3646    {'*.grid', ' (*.grid)';
3647    '*.grid',  '.grid files '; ...
3648    '*.*', 'All Files (*.*)'}, ...
3649    'Pick a file',filebase);
3650filegrid=fullfile(PathName,FileName);
3651set(handles.grid_patch1,'string',filegrid);
3652% set(handles.grid_patch2,'string',filegrid
3653
3654%------------------------------------------------------------------------
3655% --- Executes on button press in get_gridpatch2.
3656function get_gridpatch2_Callback(hObject, eventdata, handles)
3657%------------------------------------------------------------------------
3658
3659%------------------------------------------------------------------------
3660function enable_civ1(handles,state)
3661%------------------------------------------------------------------------
3662if isequal(state,'on')
3663    set(handles.frame_civ1,'BackgroundColor',[1 1 0])
3664    set(handles.frame_para_civ1,'BackgroundColor',[1 1 0])
3665    set(handles.frame_grid_civ1,'BackgroundColor',[1 1 0])
3666else
3667    set(handles.frame_civ1,'BackgroundColor',[0.831 0.816 0.784])
3668    set(handles.frame_para_civ1,'BackgroundColor',[0.831 0.816 0.784])
3669    set(handles.frame_grid_civ1,'BackgroundColor',[0.831 0.816 0.784])
3670end
3671set(handles.ibx,'Visible',state)
3672set(handles.iby,'Visible',state)
3673set(handles.isx,'Visible',state)
3674set(handles.isy,'Visible',state)
3675set(handles.shiftx,'Visible',state)
3676set(handles.shifty,'Visible',state)
3677set(handles.rho,'Visible',state)
3678set(handles.dx_civ1,'Visible',state)
3679set(handles.dy_civ1,'Visible',state)
3680set(handles.calcul_search,'Visible',state)
3681set(handles.u_text,'Visible',state)
3682set(handles.v_text,'Visible',state)
3683set(handles.min,'Visible',state)
3684set(handles.max,'Visible',state)
3685set(handles.umin,'Visible',state)
3686set(handles.umax,'Visible',state)
3687set(handles.vmin,'Visible',state)
3688set(handles.vmax,'Visible',state)
3689set(handles.grid_civ1,'Visible',state)
3690set(handles.mask_civ1,'Visible',state)
3691set(handles.browse_gridciv1,'Visible',state)
3692set(handles.get_mask_civ1,'Visible',state)
3693set(handles.parameters,'Visible',state)
3694set(handles.grid,'Visible',state)
3695set(handles.dx_civ1,'Visible',state)
3696set(handles.dy_civ1,'Visible',state)
3697set(handles.ImaThreshold,'Visible',state)
3698if isequal(state,'off')
3699    set(handles.MinIma,'Visible','off')
3700    set(handles.MaxIma,'Visible','off')
3701    set(handles.ImaThreshold,'Value',0)
3702end
3703set(handles.dx_civ1_title,'Visible',state)
3704set(handles.dy_civ1_title,'Visible',state)
3705set(handles.ImaThreshold_title,'Visible',state)
3706set(handles.ib_title,'Visible',state)
3707set(handles.is_title,'Visible',state)
3708set(handles.shift_title,'Visible',state)
3709set(handles.rho_title,'Visible',state)
3710set(handles.TestCiv1,'Visible',state)
3711%set(handles.CivAll,'Visible',state)
3712
3713%------------------------------------------------------------------------
3714function enable_fix1(handles,state)
3715%------------------------------------------------------------------------
3716if isequal(state,0)
3717    state='off';
3718end
3719if isequal(state,1)
3720    state='on';
3721end
3722if isequal(state,'on')
3723    set(handles.frame_fix1,'BackgroundColor',[1 1 0])
3724else
3725    set(handles.frame_fix1,'BackgroundColor',[0.7 0.7 0.7])
3726end
3727set(handles.REMOVE,'Visible',state)
3728set(handles.vec_Fmin2,'Visible',state)
3729set(handles.vec_F2,'Visible',state)
3730set(handles.vec_F3,'Visible',state)
3731set(handles.thresh_vecC,'Visible',state)
3732set(handles.thresh_vecC_title,'Visible',state)
3733set(handles.thresh_vel,'Visible',state)
3734set(handles.thresh_vel_text,'Visible',state)
3735set(handles.mask_fix1,'Visible',state)
3736set(handles.get_mask_fix1,'Visible',state)
3737set(handles.get_ref_fix1,'Visible',state)
3738set(handles.ref_fix1,'Visible',state)
3739set(handles.inf_sup1,'Visible',state)
3740set(handles.field_ref1,'Visible',state)
3741
3742%------------------------------------------------------------------------
3743function enable_patch1(handles)
3744%------------------------------------------------------------------------
3745set(handles.frame_patch1,'BackgroundColor',[1 1 0])
3746set(handles.rho_patch1,'Visible','on')
3747set(handles.rho_text1,'Visible','on')
3748if get(handles.CivAll,'Value')==2
3749    set(handles.thresh_patch1,'Visible','on')
3750    set(handles.thresh_text1,'Visible','on')
3751end
3752set(handles.subdomain_patch1,'Visible','on')
3753set(handles.subdomain_text1,'Visible','on')
3754set(handles.nx_patch1,'Visible','on')
3755set(handles.ny_patch1,'Visible','on')
3756set(handles.nx_patch1_title,'Visible','on')
3757set(handles.ny_patch1_title,'Visible','on')
3758% if ~isempty(patch_newBin)
3759set(handles.test_interp,'Visible','off');
3760% end
3761set(handles.get_gridpatch1,'Visible','on')
3762set(handles.grid_patch1,'string','none');
3763set(handles.grid_patch1,'Visible','on')
3764
3765%------------------------------------------------------------------------
3766function desable_patch1(handles)
3767%------------------------------------------------------------------------
3768set(handles.frame_patch1,'BackgroundColor',[0.831 0.816 0.784])
3769set(handles.rho_patch1,'Visible','off')
3770set(handles.rho_text1,'Visible','off')
3771set(handles.thresh_patch1,'Visible','off')
3772set(handles.thresh_text1,'Visible','off')
3773set(handles.subdomain_patch1,'Visible','off')
3774set(handles.subdomain_text1,'Visible','off')
3775set(handles.nx_patch1,'Visible','off')
3776set(handles.ny_patch1,'Visible','off')
3777set(handles.nx_patch1_title,'Visible','off')
3778set(handles.ny_patch1_title,'Visible','off')
3779%set(handles.test_interp,'Visible','off')
3780set(handles.get_gridpatch1,'Visible','off')
3781set(handles.grid_patch1,'Visible','off')
3782
3783%------------------------------------------------------------------------
3784function enable_civ2(handles,state)
3785%------------------------------------------------------------------------
3786if isequal(state,0)
3787    state='off';
3788end
3789if isequal(state,1)
3790    state='on';
3791end
3792if isequal(state,'on')
3793    set(handles.frame_civ2,'BackgroundColor',[1 1 0])
3794    set(handles.frame_para_civ2,'BackgroundColor',[1 1 0])
3795    set(handles.frame_grid_civ2,'BackgroundColor',[1 1 0])
3796    set(handles.frame_subdirciv2,'BackgroundColor',[1 1 0])
3797else
3798    set(handles.frame_civ2,'BackgroundColor',[0.831 0.816 0.784])
3799    set(handles.frame_para_civ2,'BackgroundColor',[0.831 0.816 0.784])
3800    set(handles.frame_grid_civ2,'BackgroundColor',[0.831 0.816 0.784])
3801    set(handles.frame_subdirciv2,'BackgroundColor',[0.831 0.816 0.784])
3802end
3803set(handles.ibx_civ2,'Visible',state)
3804set(handles.iby_civ2,'Visible',state)
3805set(handles.decimal,'Visible',state)
3806set(handles.deformation,'Visible',state)
3807set(handles.rho_civ2,'Visible',state)
3808set(handles.dx_civ2,'Visible',state)
3809set(handles.dy_civ2,'Visible',state)
3810set(handles.browse_gridciv2,'Visible',state)
3811set(handles.get_mask_civ2,'Visible',state)
3812set(handles.parameters,'Visible',state)
3813set(handles.grid,'Visible',state)
3814set(handles.parameters_text,'Visible',state)
3815set(handles.grid_text,'Visible',state)
3816set(handles.grid_civ2,'Visible',state)
3817set(handles.mask_civ2,'Visible',state)
3818set(handles.dx_civ2_title,'Visible',state)
3819set(handles.dy_civ2_title,'Visible',state)
3820set(handles.ibx_civ2_text,'Visible',state)
3821set(handles.rho_civ2_title,'Visible',state)
3822set(handles.ImaThreshold2,'Visible',state)
3823set(handles.ImaThreshold_title2,'Visible',state)
3824if isequal(state,'off')
3825    set(handles.MinIma2,'Visible','off')
3826    set(handles.MaxIma2,'Visible','off')
3827    set(handles.ImaThreshold2,'Value',0)
3828    if isequal(get(handles.FIX2,'Value'),0) & isequal(get(handles.PATCH2,'Value'),0)
3829        set(handles.list_pair_civ2,'Visible','off')
3830        set(handles.subdir_civ2,'Visible','off')
3831        set(handles.subdir_civ2_text,'Visible','off')
3832        set(handles.dt_unit_civ2,'Visible','off')
3833        set(handles.ref_i_civ2,'Visible','off')
3834        set(handles.i_ref_civ2_title,'Visible','off')
3835        set(handles.j_ref_civ2_title,'Visible','off')
3836        set(handles.ref_j_civ2,'Visible','off')
3837    end
3838else
3839    set(handles.list_pair_civ2,'Visible','on')
3840    set(handles.subdir_civ2,'Visible','on')
3841    set(handles.subdir_civ2_text,'Visible','on')
3842    set(handles.dt_unit_civ2,'Visible','on')
3843    set(handles.ref_i_civ2,'Visible','on')
3844    set(handles.i_ref_civ2_title,'Visible','on')
3845    set(handles.j_ref_civ2_title,'Visible','on')
3846    set(handles.ref_j_civ2,'Visible','on')
3847end
3848set(handles.rho_civ2_title,'Visible',state)
3849
3850%------------------------------------------------------------------------
3851function enable_fix2(handles)
3852%------------------------------------------------------------------------
3853set(handles.frame_fix2,'BackgroundColor',[1 1 0])
3854set(handles.REMOVE2,'Visible','on')
3855set(handles.vec_Fmin2_2,'Visible','on')
3856set(handles.vec_F4,'Visible','on')
3857set(handles.vec_F3_2,'Visible','on')
3858set(handles.thresh_vec2C,'Visible','on')
3859set(handles.thresh_vec2C_text,'Visible','on')
3860set(handles.thresh_vel2,'Visible','on')
3861set(handles.thresh_vel2_text,'Visible','on')
3862set(handles.mask_fix2,'Visible','on')
3863set(handles.get_mask_fix2,'Visible','on')
3864set(handles.list_pair_civ2,'Visible','on')
3865set(handles.subdir_civ2,'Visible','on')
3866set(handles.subdir_civ2_text,'Visible','on')
3867set(handles.get_ref_fix2,'Visible','on')
3868set(handles.ref_fix2,'Visible','on')
3869set(handles.inf_sup2,'Visible','on')
3870set(handles.field_ref2,'Visible','on')
3871
3872%------------------------------------------------------------------------
3873function desable_fix2(handles)
3874%------------------------------------------------------------------------
3875set(handles.frame_fix2,'BackgroundColor',[0.831 0.816 0.784])
3876set(handles.REMOVE2,'Visible','off')
3877set(handles.vec_Fmin2_2,'Visible','off')
3878set(handles.vec_F4,'Visible','off')
3879set(handles.vec_F3_2,'Visible','off')
3880set(handles.thresh_vec2C,'Visible','off')
3881set(handles.thresh_vec2C_text,'Visible','off')
3882set(handles.thresh_vel2,'Visible','off')
3883set(handles.thresh_vel2_text,'Visible','off')
3884set(handles.mask_fix2,'Visible','off')
3885set(handles.get_mask_fix2,'Visible','off')
3886set(handles.get_ref_fix2,'Visible','off')
3887set(handles.ref_fix2,'Visible','off')
3888set(handles.inf_sup2,'Visible','off')
3889set(handles.field_ref2,'Visible','off')
3890if isequal(get(handles.CIV2,'Value'),0) & isequal(get(handles.PATCH2,'Value'),0)
3891    set(handles.list_pair_civ2,'Visible','off')
3892    set(handles.subdir_civ2,'Visible','off')
3893    set(handles.subdir_civ2_text,'Visible','off')
3894end
3895
3896%------------------------------------------------------------------------
3897function enable_patch2(handles)
3898%------------------------------------------------------------------------
3899set(handles.frame_patch2,'BackgroundColor',[1 1 0])
3900set(handles.rho_patch2,'Visible','on')
3901set(handles.rho_text2,'Visible','on')
3902set(handles.thresh_patch2,'Visible','on')
3903set(handles.thresh_text2,'Visible','on')
3904set(handles.subdomain_patch2,'Visible','on')
3905set(handles.subdomain_text2,'Visible','on')
3906set(handles.nx_patch2,'Visible','on')
3907set(handles.ny_patch2,'Visible','on')
3908set(handles.nx_patch2_title,'Visible','on')
3909set(handles.ny_patch2_title,'Visible','on')
3910set(handles.get_gridpatch2,'Visible','on')
3911set(handles.grid_patch2,'Visible','on')
3912set(handles.list_pair_civ2,'Visible','on')
3913set(handles.subdir_civ2,'Visible','on')
3914set(handles.subdir_civ2_text,'Visible','on')
3915
3916%------------------------------------------------------------------------
3917function desable_patch2(handles)
3918%------------------------------------------------------------------------
3919set(handles.frame_patch2,'BackgroundColor',[0.831 0.816 0.784])
3920set(handles.rho_patch2,'Visible','off')
3921set(handles.rho_text2,'Visible','off')
3922set(handles.thresh_patch2,'Visible','off')
3923set(handles.thresh_text2,'Visible','off')
3924set(handles.subdomain_patch2,'Visible','off')
3925set(handles.subdomain_text2,'Visible','off')
3926set(handles.nx_patch2,'Visible','off')
3927set(handles.ny_patch2,'Visible','off')
3928set(handles.nx_patch2_title,'Visible','off')
3929set(handles.ny_patch2_title,'Visible','off')
3930set(handles.get_gridpatch2,'Visible','off')
3931set(handles.grid_patch2,'Visible','off')
3932if isequal(get(handles.CIV2,'Value'),0) & isequal(get(handles.FIX2,'Value'),0)
3933    set(handles.list_pair_civ2,'Visible','off')
3934    set(handles.subdir_civ2,'Visible','off')
3935    set(handles.subdir_civ2_text,'Visible','off')
3936end
3937
3938%------------------------------------------------------------------------
3939function enable_pair1(handles,state)
3940%------------------------------------------------------------------------
3941set(handles.subdir_civ1,'Visible',state)
3942set(handles.list_subdir_civ1,'Visible',state)
3943set(handles.SUBDIR_CIV1_txt,'Visible',state)
3944set(handles.frame_subdirciv1,'Visible',state)
3945set(handles.list_pair_civ1,'Visible',state)
3946set(handles.PAIR_txt,'Visible',state)
3947%set(handles.dt_unit,'Visible',state)
3948set(handles.PAIR_frame,'Visible',state)
3949
3950%------------------------------------------------------------------------
3951% --- Read the parameters for civ1 on the interface
3952function par=read_param_civ1(handles,file_ima)
3953%------------------------------------------------------------------------
3954ibx_val=str2double(get(handles.ibx,'String'));
3955par.ibx=num2str(ibx_val);
3956iby_val=str2double(get(handles.iby,'String'));
3957par.iby=num2str(iby_val);
3958isx=get(handles.isx,'String');
3959if isnan(str2double(isx)), isx='41'; set(handles.isx,'String','41'), end; %default
3960if str2double(isx)<ibx_val+8,isx=num2str(ibx_val+8); set(handles.isx,'String',num2str(ibx_val+8)); end
3961isy=get(handles.isy,'String');
3962if isnan(str2double(isy)), isy='41'; set(handles.isy,'String','41'), end;%default
3963if str2double(isy)<iby_val+8,isy=num2str(iby_val+8); set(handles.isy,'String',num2str(iby_val+8)); end
3964par.isx=get(handles.isx,'String');
3965par.isy=get(handles.isy,'String');
3966par.shiftx=get(handles.shiftx,'String');
3967par.shifty=get(handles.shifty,'String');
3968if isnan(str2double(par.isx))
3969    par.isx='41';%default
3970    set(handles.isx,'String','41');
3971end
3972if isnan(str2double(par.isy))
3973    par.isy='41'; %default
3974    set(handles.isy,'String','41');
3975end
3976if isnan(str2double(par.shiftx))
3977    par.shiftx='0';%default
3978    set(handles.shiftx,'String','0');
3979end
3980if isnan(str2double(par.shifty))
3981    par.shifty='0'; %default
3982    set(handles.shifty,'String','0');
3983end
3984par.rho=get(handles.rho,'String');
3985if isequal(get(handles.rho,'Style'),'popupmenu')
3986    index=get(handles.rho,'Value');
3987    par.rho=par.rho{index};
3988end
3989par.dx=get(handles.dx_civ1,'String');
3990par.dy=get(handles.dy_civ1,'String');
3991if isnan(str2double(par.dx))
3992    if isempty(get(handles.grid_civ1,'String'));
3993        par.dx='0'; %just read by civ program, not used
3994    else
3995        par.dx='20';%default
3996        set(handles.dx_civ1,'String','20');
3997    end
3998end
3999if isnan(str2double(par.dy))
4000    if isempty(get(handles.grid_civ1,'String'));
4001        par.dy='0';%just read by civ program, not used
4002    else
4003        par.dy='20';%default
4004        set(handles.dy_civ1_title,'String','20');
4005    end
4006end
4007par.pxcmx='1'; %velocities are expressed in pixel dispalcement
4008par.pxcmy='1';
4009if exist('file_ima','var')
4010A=imread(file_ima);%read the first image to get the size
4011sizim=size(A);
4012par.npx=num2str(sizim(2));
4013par.npy=num2str(sizim(1));
4014end
4015%time=get(handles.RootName,'UserData'); %get the set of times
4016par.gridname=get(handles.grid_civ1,'String');
4017par.gridflag='y';
4018if strcmp(par.gridname,'')|| isempty(par.gridname)
4019    par.gridname='nogrid';
4020    par.gridflag='n';
4021end
4022
4023%------------------------------------------------------------------------
4024function par=read_param_civ2(handles,file_ima)
4025%------------------------------------------------------------------------
4026par.ibx=get(handles.ibx_civ2,'String');
4027par.iby=get(handles.iby_civ2,'String');
4028par.rho=get(handles.rho_civ2,'String');
4029par.decimal=int2str(get(handles.decimal,'Value'));
4030par.deformation=int2str(get(handles.deformation,'Value'));
4031par.dx=get(handles.dx_civ2,'String');
4032par.dy=get(handles.dy_civ2,'String');
4033if isnan(str2double(par.dx))
4034    if isempty(get(handles.grid_civ2,'String'));
4035        par.dx='0'; %just read by civ program, not used
4036    else
4037        par.dx='20';%default
4038        set(handles.dx_civ2,'String','20');
4039    end
4040end
4041if isnan(str2double(par.dy))
4042    if isempty(get(handles.grid_civ2,'String'));
4043        par.dy='0';%just read by civ program, not used
4044    else
4045        par.dy='20';%default
4046        set(handles.dy_civ2,'String','20');
4047    end
4048end
4049par.pxcmx='1';
4050par.pxcmy='1';
4051A=imread(file_ima);%read the first image to get the size
4052sizim=size(A);
4053par.npx=num2str(sizim(2));
4054par.npy=num2str(sizim(1));
4055%time=get(handles.RootName,'UserData'); %get the set of times
4056par.gridname=get(handles.grid_civ2,'String');
4057par.gridflag='y';
4058if strcmp(par.gridname,'')|| isempty(par.gridname)
4059    par.gridname='nogrid';
4060    par.gridflag='n';
4061end
4062
4063%------------------------------------------------------------------------
4064% --- CIV1  CIV1  CIV1 CIV1
4065function cmd_CIV1=CIV1_CMD(filename,namelog,par,handles,sparam)
4066%------------------------------------------------------------------------
4067%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4068
4069%changes : filename_cmx -> filename ( no extension )
4070% input namelog not used
4071if isequal(par.Dt,'0')
4072    par.Dt='1' ;%case of 'displacement' mode
4073end
4074par.filename_ima_a=regexprep(par.filename_ima_a,'.png','');
4075par.filename_ima_b=regexprep(par.filename_ima_b,'.png','');
4076fid=fopen([filename '.civ1.cmx'],'w');
4077fprintf(fid,['##############   CMX file' '\n' ]);
4078fprintf(fid,   ['FirstImage ' regexprep(par.filename_ima_a,'\\','\\\\') '\n' ]);% for windows compatibility
4079fprintf(fid,   ['LastImage  ' regexprep(par.filename_ima_b,'\\','\\\\') '\n' ]);% for windows compatibility
4080fprintf(fid,  ['XX' '\n' ]);
4081fprintf(fid,  ['Mask ' par.maskflag '\n' ]);
4082fprintf(fid,  ['MaskName ' regexprep(par.maskname,'\\','\\\\') '\n' ]);
4083fprintf(fid,   ['ImageSize ' par.npx ' ' par.npy '\n' ]);   %VERIFIER CAS GENERAL ?
4084fprintf(fid,   ['CorrelationBoxesSize ' par.ibx ' ' par.iby '\n' ]);
4085fprintf(fid,   ['SearchBoxeSize ' par.isx ' ' par.isy '\n' ]);
4086fprintf(fid,   ['RO ' par.rho '\n' ]);
4087fprintf(fid,   ['GridSpacing ' par.dx ' ' par.dy '\n' ]);
4088fprintf(fid,   ['XX 1.0' '\n' ]);
4089fprintf(fid,   ['Dt_TO ' par.Dt ' ' par.T0 '\n' ]);
4090fprintf(fid,  ['PixCmXY ' par.pxcmx ' ' par.pxcmy '\n' ]);
4091fprintf(fid,  ['XX 1' '\n' ]);
4092fprintf(fid,   ['ShiftXY ' par.shiftx ' '  par.shifty '\n' ]);
4093fprintf(fid,  ['Grid ' par.gridflag '\n' ]);
4094fprintf(fid,   ['GridName ' regexprep(par.gridname,'\\','\\\\') '\n' ]);
4095fprintf(fid,   ['XX 85' '\n' ]);
4096fprintf(fid,   ['XX 1.0' '\n' ]);
4097fprintf(fid,   ['XX 1.0' '\n' ]);
4098fprintf(fid,   ['Hart 1' '\n' ]);
4099fprintf(fid,  [ 'DecimalShift 0' '\n' ]);
4100fprintf(fid,   ['Deformation 0' '\n' ]);
4101fprintf(fid,  ['CorrelationMin 0' '\n' ]);
4102fprintf(fid,   ['IntensityMin 0' '\n' ]);
4103fprintf(fid,  ['SeuilImage n' '\n' ]);
4104fprintf(fid,   ['SeuilImageValues 0 4096' '\n' ]);
4105fprintf(fid,   ['ImageToUse ' par.term_a ' ' par.term_b '\n' ]); % VERIFIER ?
4106fprintf(fid,   ['ImageUsedBefore null null' '\n' ]);
4107fclose(fid);
4108
4109% cmd_CIV1=[sparam.Civ1Bin ' -f ' filename '.cmx >' filename '.log' ]; % redirect standard output to the log file
4110% cmd_CIV1=regexprep(cmd_CIV1,'\\','\\\\');
4111% namelog=regexprep(namelog,'\\','\\\\');
4112if(isunix)
4113    cmd_CIV1=['cp -f ' filename '.civ1.cmx ' filename '.cmx\n'];
4114    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
4115    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
4116   % cmd_CIV1=[cmd_CIV1 '\n' 'mv ' filename '.cmx' ' ' filename '.civ1.cmx' '\n'];%rename .cmx as .civ1.cmx
4117else %Windows system
4118                    flname=regexprep(flname,'\\','\\\\');
4119%                     cmd=[cmd 'copy /Y "' flname '.civ1.cmx" "' flname '.cmx"\n'];
4120    filename=regexprep(filename,'\\','\\\\');
4121    cmd_CIV1=['copy /Y "' filename '.civ1.cmx" "' filename '.cmx"\n'];% copy the .civ1.cmx parameter file to .cmx
4122    cmd_CIV1=['"' sparam.Civ1Bin '" -f "' filename '.cmx" >"' filename '.log"' ]; % redirect standard output to the log file
4123    cmd_CIV1=regexprep(cmd_CIV1,'\\','\\\\');
4124    namelog=regexprep(namelog,'\\','\\\\');
4125    cmd_CIV1=[cmd_CIV1 '\n' 'copy /Y "' filename '.log' '" "' filename '.civ1.log"']; %preserve the log file as .civ1.log
4126  %  cmd_CIV1=[cmd_CIV1 '\n' 'copy /Y "' filename '.cmx' '" "' filename '.civ1.cmx"'];
4127end
4128
4129%------------------------------------------------------------------------
4130% --- CIV1  Unified
4131function xml_civ1_parameters=CIV1_CMD_Unified(filename,namelog,par)
4132%------------------------------------------------------------------------
4133%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4134%global CivBin%name of the executable for civ1 calculation
4135
4136civ1.image1=par.filename_ima_a;
4137civ1.image2=par.filename_ima_b;
4138civ1.imageSize_X=par.npx;
4139civ1.imageSize_Y=par.npy;
4140civ1.outputFileName=[filename '.nc'];
4141civ1.correlationBoxesSize_X=par.ibx;
4142civ1.correlationBoxesSize_Y=par.iby;
4143civ1.searchBoxesSize_X=par.isx;
4144civ1.searchBoxesSize_Y=par.isy;
4145civ1.globalShift_X=par.shiftx;
4146civ1.globalShift_Y=par.shifty;
4147civ1.ro=par.rho;
4148civ1.hart='y';
4149if isequal(par.gridflag,'y')
4150    civ1.grid=par.gridname;
4151else
4152    civ1.grid='n';
4153    civ1.gridSpacing_X=par.dx;
4154    civ1.gridSpacing_Y=par.dy;
4155end
4156if isequal(par.maskflag,'y')
4157    civ1.mask=par.maskname;
4158end
4159civ1.dt=par.Dt;
4160civ1.unit='pixel';
4161civ1.absolut_time_T0=par.T0;
4162civ1.pixcmx=par.pxcmx;
4163civ1.pixcmy=par.pxcmy;
4164civ1.convectFlow='n';
4165
4166xml_civ1_parameters=civ1;
4167
4168%------------------------------------------------------------------------
4169% --- CIV2  Unified
4170function civ2=CIV2_CMD_Unified(filename,namelog,par)
4171%------------------------------------------------------------------------
4172%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4173%global CivBin%name of the executable for civ1 calculation
4174
4175civ2.image1=par.filename_ima_a;
4176civ2.image2=par.filename_ima_b;
4177civ2.imageSize_X=par.npx;
4178civ2.imageSize_Y=par.npy;
4179civ2.inputFileName=[par.filename_nc1 '.nc'];
4180civ2.outputFileName=[filename '.nc'];
4181civ2.correlationBoxesSize_X=par.ibx;
4182civ2.correlationBoxesSize_Y=par.iby;
4183civ2.ro=par.rho;
4184%civ2.decimalShift=par.decimal;
4185%civ2.deformation=par.deformation;
4186if isequal(par.decimal,'1')
4187    civ2.decimalShift='y';
4188else
4189    civ2.decimalShift='n';
4190end
4191if isequal(par.deformation,'1')
4192    civ2.deformation='y';
4193else
4194    civ2.deformation='n';
4195end
4196if isequal(par.gridflag,'y')
4197    civ2.grid=par.gridname;
4198else
4199    civ2.grid='n';
4200    civ2.gridSpacing_X=par.dx;
4201    civ2.gridSpacing_Y=par.dy;
4202end
4203civ2.gridSpacing_X='10';
4204civ2.gridSpacing_Y='10';%NOTE: faut mettre gridSpacing pourque ca tourne, meme si c'est la grille qui est utilisee
4205if isequal(par.maskflag,'y')
4206    civ2.mask=par.maskname;
4207else
4208    civ2.mask='n';
4209end
4210civ2.dt=par.Dt;
4211civ2.unit='pixel';
4212civ2.absolut_time_T0=par.T0;
4213civ2.pixcmx=par.pxcmx;
4214civ2.pixcmy=par.pxcmy;
4215civ2.convectFlow='n';
4216civ2.pixcmx=par.pxcmx;
4217civ2.pixcmy=par.pxcmy;
4218civ2.convectFlow='n';
4219
4220%------------------------------------------------------------------------
4221% --- CIV2  CIV2  CIV2 CIV2
4222function cmd_CIV2=CIV2_CMD(filename,namelog,par,sparam)
4223%------------------------------------------------------------------------
4224%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4225% global civ2Bin sge%name of the executable for civ1 calculation
4226if isequal(par.Dt,'0')
4227    par.Dt='1' ;%case of 'displacement' mode
4228end
4229par.filename_ima_a=regexprep(par.filename_ima_a,'.png','');
4230par.filename_ima_b=regexprep(par.filename_ima_b,'.png','');% bug : .png appears two times ?
4231[fid,errormsg]=fopen([filename '.civ2.cmx'],'w');
4232if isequal(fid,-1)
4233    msgbox_uvmat('ERROR',errormsg)
4234    cmd_CIV2='';
4235    return
4236end
4237fprintf(fid,['##############   CMX file' '\n' ]);
4238fprintf(fid,   ['FirstImage ' regexprep(par.filename_ima_a,'\\','\\\\') '\n' ]);% for windows compatibility
4239fprintf(fid,   ['LastImage  ' regexprep(par.filename_ima_b,'\\','\\\\') '\n' ]);% for windows compatibility
4240fprintf(fid,  ['XX' '\n' ]);
4241fprintf(fid, ['Mask ' par.maskflag '\n' ]);
4242fprintf(fid, ['MaskName ' regexprep(par.maskname,'\\','\\\\') '\n' ]);% for windows compatibility
4243fprintf(fid, ['ImageSize ' par.npx ' ' par.npy '\n' ]);   %VERIFIER CAS GENERAL ?
4244fprintf(fid, ['CorrelationBoxesSize ' par.ibx ' ' par.iby '\n' ]);
4245fprintf(fid, ['SearchBoxeSize ' par.ibx ' ' par.iby '\n']);
4246fprintf(fid, ['RO ' par.rho '\n']);
4247fprintf(fid, ['GridSpacing ' par.dx ' ' par.dy '\n']);
4248fprintf(fid, ['XX 1.0' '\n' ]);
4249fprintf(fid, ['Dt_TO ' par.Dt ' ' par.T0 '\n' ]);
4250fprintf(fid, ['PixCmXY ' par.pxcmx ' ' par.pxcmy '\n' ]);
4251fprintf(fid, ['XX 1' '\n' ]);
4252fprintf(fid, 'ShiftXY 0 0\n');
4253fprintf(fid, ['Grid ' par.gridflag '\n' ]);
4254fprintf(fid, ['GridName ' regexprep(par.gridname,'\\','\\\\') '\n']);
4255fprintf(fid, ['XX 85' '\n' ]);
4256fprintf(fid, ['XX 1.0' '\n' ]);
4257fprintf(fid, ['XX 1.0' '\n' ]);
4258fprintf(fid, ['Hart 1' '\n' ]);
4259fprintf(fid, ['DecimalShift ' par.decimal '\n']);
4260fprintf(fid, ['Deformation ' par.deformation '\n']);
4261fprintf(fid,  ['CorrelationMin 0' '\n' ]);
4262fprintf(fid,   ['IntensityMin 0' '\n' ]);
4263fprintf(fid,  ['SeuilImage n' '\n' ]);
4264fprintf(fid,   ['SeuilImageValues 0 4096' '\n' ]);
4265fprintf(fid,   ['ImageToUse ' par.term_a ' ' par.term_b '\n' ]); % VERIFIER ?
4266fprintf(fid, ['ImageUsedBefore ' regexprep(par.filename_nc1,'\\','\\\\') '\n']);
4267fclose(fid);
4268
4269if(isunix)
4270    cmd_CIV2=['cp -f ' filename '.civ2.cmx ' filename '.cmx\n'];
4271    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
4272    cmd_CIV2=[cmd_CIV2 '\n' 'mv ' filename '.log' ' ' filename '.civ2.log' '\n' 'chmod g+w ' filename '.nc'];%preserve the log file as .civ2.log
4273%    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
4274
4275else
4276    filename=regexprep(filename,'\\','\\\\');
4277    cmd_CIV2=['copy /Y "' filename '.civ2.cmx" "' filename '.cmx"\n'];
4278    cmd_CIV2=[cmd_CIV2 '"' sparam.Civ2Bin '" -f "' filename  '.cmx" >"' filename '.log"' ]; % redirect standard output to the log file
4279    cmd_CIV2=regexprep(cmd_CIV2,'\\','\\\\');
4280    cmd_CIV2=[cmd_CIV2 '\n' 'copy /Y "' filename '.log' '" "' filename '.civ2.log"'];
4281 %    cmd_CIV2=[cmd_CIV2 '\n' 'copy /Y "' filename '.cmx' '" "' filename '.civ2.cmx"'];
4282end
4283
4284% %------------------------------------------------------------------------
4285% % --- civ using pivlab
4286% function Data=civ_uvmat(par_civ1)
4287% %------------------------------------------------------------------------
4288% image1=imread(par_civ1.filename_ima_a);
4289% image2=imread(par_civ1.filename_ima_b);
4290% stepx=str2num(par_civ1.dx);
4291% stepy=str2num(par_civ1.dy);
4292% ibx2=ceil(str2num(par_civ1.ibx)/2);
4293% iby2=ceil(str2num(par_civ1.iby)/2);
4294% isx2=ceil(str2num(par_civ1.isx)/2);
4295% isy2=ceil(str2num(par_civ1.isy)/2);
4296% shiftx=str2num(par_civ1.shiftx);
4297% shifty=str2num(par_civ1.shifty);
4298% miniy=max(1+isy2-shifty,1+iby2);
4299% minix=max(1+isx2-shiftx,1+ibx2);
4300% maxiy=min(size(image1,1)-isy2-shifty,size(image1,1)-iby2);
4301% maxix=min(size(image1,2)-isx2-shiftx,size(image1,2)-ibx2);
4302% [GridX,GridY]=meshgrid(minix:stepx:maxix,miniy:stepy:maxiy);
4303% PointCoord(:,1)=reshape(GridX,[],1);
4304% PointCoord(:,2)=reshape(GridY,[],1);
4305% % caluclate velocity data (y and v in indices, reverse to y component)
4306% [xtable ytable utable vtable ctable F] = pivlab (image1,image2,ibx2,iby2,isx2,isy2,shiftx,shifty,PointCoord, 1, []);
4307% Data.ListGlobalAttribute=[{'Conventions','Program','CivStage'} {'Time','Dt'}];
4308% Data.Conventions='uvmat/civdata';
4309% Data.Program='civ_uvmat';
4310% Data.CivStage=1;
4311% % list_param=fieldnames(Param.Civ1);
4312% % for ilist=1:length(list_param)
4313% %     eval(['Data.Civ1_' list_param{ilist} '=Param.Civ1.' list_param{ilist} ';'])
4314% % end
4315% Data.Time=str2double(par_civ1.T0);
4316% Data.Dt=str2double(par_civ1.Dt);
4317% Data.ListVarName={'Civ1_X','Civ1_Y','Civ1_U','Civ1_V','Civ1_C','Civ1_F'};%  cell array containing the names of the fields to record
4318% Data.VarDimName={'nbvec','nbvec','nbvec','nbvec','nbvec','nbvec'};
4319% Data.VarAttribute{1}.Role='coord_x';
4320% Data.VarAttribute{2}.Role='coord_y';
4321% Data.VarAttribute{3}.Role='vector_x';
4322% Data.VarAttribute{4}.Role='vector_y';
4323% Data.VarAttribute{5}.Role='warnflag';
4324% Data.Civ1_X=reshape(xtable,[],1);
4325% Data.Civ1_Y=reshape(size(image1,1)-ytable+1,[],1);
4326% Data.Civ1_U=reshape(utable,[],1);
4327% Data.Civ1_V=reshape(-vtable,[],1);
4328% Data.Civ1_C=reshape(ctable,[],1);
4329% Data.Civ1_F=reshape(F,[],1);
4330
4331%------------------------------------------------------------------------
4332% --- Executes on button press in HELP.
4333function HELP_Callback(hObject, eventdata, handles)
4334%------------------------------------------------------------------------
4335path_to_uvmat=which ('uvmat');% check the path of uvmat
4336pathelp=fileparts(path_to_uvmat);
4337helpfile=fullfile(pathelp,'uvmat_doc','uvmat_doc.html');
4338if isempty(dir(helpfile)), msgbox_uvmat('ERROR','Please put the help file uvmat_doc.html in the sub-directory /uvmat_doc of the UVMAT package')
4339else
4340    addpath (fullfile(pathelp,'uvmat_doc'))
4341    web([helpfile '#civ'])
4342end
4343
4344%------------------------------------------------------------------------
4345%--read images and convert them to the uint16 format used for PIV
4346function A=read_image(filename,type_ima,num,movieobject)
4347%------------------------------------------------------------------------
4348%num is the view number needed for an avi movie
4349switch type_ima
4350    case 'movie'
4351        A=read(movieobject,num);
4352    case 'avi'
4353        mov=aviread(filename,num);
4354        A=frame2im(mov(1));
4355    case 'multimage'
4356        A=imread(filename,num);
4357    case 'image'
4358        A=imread(filename);
4359end
4360siz=size(A);
4361if length(siz)==3;%color images
4362    A=sum(double(A),3);
4363    A=uint16(A);
4364end
4365
4366%------------------------------------------------------------------------
4367function ref_i_Callback(hObject, eventdata, handles)
4368%------------------------------------------------------------------------
4369mode_list=get(handles.mode,'String');
4370mode_value=get(handles.mode,'Value');
4371mode=mode_list{mode_value};
4372find_netcpair_civ1(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4373if isequal(mode,'series(Di)') || ...% we do patch2 only
4374        (get(handles.CIV2,'Value')==0 && get(handles.CIV1,'Value')==0 && get(handles.FIX1,'Value')==0 && get(handles.PATCH1,'Value')==0)
4375    find_netcpair_civ2(hObject, eventdata, handles);
4376end
4377
4378%------------------------------------------------------------------------
4379function ref_j_Callback(hObject, eventdata, handles)
4380%------------------------------------------------------------------------
4381mode_list=get(handles.mode,'String');
4382mode_value=get(handles.mode,'Value');
4383mode=mode_list{mode_value};
4384if isequal(get(handles.CIV1,'Value'),0)|| isequal(mode,'series(Dj)')
4385    find_netcpair_civ1(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4386end
4387if isequal(mode,'series(Dj)') || ...
4388        (get(handles.CIV2,'Value')==0 && get(handles.CIV1,'Value')==0 && get(handles.FIX1,'Value')==0 && get(handles.PATCH1,'Value')==0)
4389    find_netcpair_civ2(hObject, eventdata, handles);
4390end
4391
4392%------------------------------------------------------------------------
4393function ref_i_civ2_Callback(hObject, eventdata, handles)
4394%------------------------------------------------------------------------
4395mode_list=get(handles.mode,'String');
4396mode_value=get(handles.mode,'Value');
4397mode=mode_list{mode_value};
4398find_netcpair_civ2(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4399
4400%------------------------------------------------------------------------
4401function ref_j_civ2_Callback(hObject, eventdata, handles)
4402%------------------------------------------------------------------------
4403mode_list=get(handles.mode,'String');
4404mode_value=get(handles.mode,'Value');
4405mode=mode_list{mode_value};
4406if  isequal(mode,'series(Dj)')
4407    find_netcpair_civ2(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4408end
4409
4410%------------------------------------------------------------------------
4411% --- Executes on button press in compare.
4412function compare_Callback(hObject, eventdata, handles)
4413%------------------------------------------------------------------------
4414test=get(handles.compare,'Value');
4415if test==2 || test==3
4416    filebase=get(handles.RootName,'String');
4417    browse=get(handles.browse_root,'Userdata');
4418    browse.nom_type_ima1=browse.nom_type_ima;
4419    set(handles.browse_root,'UserData',browse);
4420    set(handles.sub_txt,'Visible','on')
4421    set(handles.RootName_1,'Visible','On');%mkes the second file input window visible
4422    mode_store=get(handles.mode,'String');%get the present 'mode'
4423    set(handles.compare,'UserData',mode_store);%store the mode display
4424    set(handles.mode,'Visible','off')
4425    if test==2
4426        set(handles.mode,'Visible','off')
4427    else
4428        set(handles.mode,'Visible','on')
4429    end
4430   
4431    %% open an image file with the browser
4432    ind_opening=1;%default
4433    browse.incr_pair=[0 0]; %default
4434    oldfile=get(handles.RootName,'String');
4435     menu={'*.xml;*.civ;*.png;*.jpg;*.tif;*.avi;*.AVI;*.nc;', ' (*.xml,*.civ,*.png,*.jpg ,.tif, *.avi,*.nc)';
4436       '*.xml',  '.xml files '; ...
4437        '*.civ',  '.civ files '; ...
4438        '*.png','.png image files'; ...
4439        '*.jpg',' jpeg image files'; ...
4440        '*.tif','.tif image files'; ...
4441        '*.avi;*.AVI','.avi movie files'; ...
4442        '*.nc','.netcdf files'; ...
4443        '*.*',  'All Files (*.*)'};
4444    [FileName, PathName, filtindex] = uigetfile( menu, 'Pick a file of the second series',oldfile);
4445    fileinput=[PathName FileName];%complete file name
4446    sizf=size(fileinput);
4447    if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
4448    [path,name,ext]=fileparts(fileinput);
4449    [path1]=fileparts(filebase);
4450%     if isunix
4451%         ['readlink ' path]
4452%         [status,path]=system(['readlink ' path])
4453%         [status,path1]=system(['readlink ' path1])% look for the true path in case of symbolic paths
4454%     end
4455    if ~strcmp(path1,path)
4456        msgbox_uvmat('ERROR','The two  input image series must be in the same directory')
4457        return
4458    end
4459    set(handles.RootName_1,'String',name);
4460    [RootPath,RootFile,field_count,str2,str_a,str_b,xx,nom_type,subdir]=name2display(name);
4461    browse=get(handles.browse_root,'UserData');
4462    browse.nom_type_ima_1=nom_type;
4463    set(handles.browse_root,'UserData',browse)
4464   
4465    %check image extension
4466    if ~strcmp(ext,get(handles.ImaExt,'String'))
4467        msgbox_uvmat('ERROR','The two  input image series must have the same extension name')
4468        return
4469    end
4470   
4471    %% check coincidence of image sizes
4472    ref_i=get(handles.ref_i,'string');
4473    ref_j=get(handles.ref_j,'string');
4474    [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]);
4475    A=imread(filecell.ima1.civ1{1});
4476    A_1=imread(fileinput);
4477    npxy=size(A);
4478    npxy_1=size(A_1);
4479    if ~isequal(size(A),size(A_1))
4480        msgbox_uvmat('ERROR','The two input image series do not have the same size')
4481        return
4482    end
4483else
4484    set(handles.mode,'Visible','on')
4485    set(handles.RootName_1,'Visible','Off');
4486    set(handles.sub_txt,'Visible','off')
4487    set(handles.RootName_1,'String',[]);
4488    mode_store=get(handles.compare,'UserData');
4489    set(handles.mode,'Value',1)
4490    set(handles.mode,'String',mode_store)
4491    set(handles.test_stereo1,'Value',0)
4492    set(handles.test_stereo2,'Value',0)
4493end
4494mode_Callback(hObject, eventdata, handles)
4495
4496%------------------------------------------------------------------------
4497% --- Executes on button press in get_ref_fix1.
4498function get_ref_fix1_Callback(hObject, eventdata, handles)
4499%------------------------------------------------------------------------
4500filebase=get(handles.RootName,'String');
4501[FileName, PathName, filterindex] = uigetfile( ...
4502    {'*.nc', ' (*.nc)';
4503    '*.nc',  'netcdf files '; ...
4504    '*.*', 'All Files (*.*)'}, ...
4505    'Pick a file',filebase);
4506
4507fileinput=[PathName FileName];
4508sizf=size(fileinput);
4509if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
4510[Path,File,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
4511ref.filebase=fullfile(Path,File);
4512ref.num_a=stra2num(str_a);
4513ref.num_b=stra2num(str_b);
4514ref.num1=str2double(field_count);
4515ref.num2=str2double(str2);
4516browse=[];%initialisation
4517if ~isequal(ref.ext,'.nc')
4518    msgbox_uvmat('ERROR','the reference file must be in netcdf format (*.nc)')
4519    return
4520end
4521set(handles.ref_fix1,'String',[fullfile(ref.subdir,File) '....nc']);
4522set(handles.ref_fix1,'UserData',ref)
4523menu_field{1}='civ1';
4524Data=nc2struct(fileinput,[]);
4525if isfield(Data,'patch') && isequal(Data.patch,1)
4526    menu_field{2}='filter1';
4527end
4528if isfield(Data,'civ2') && isequal(Data.civ2,1)
4529    menu_field{3}='civ2';
4530end
4531if isfield(Data,'patch2') && isequal(Data.patch2,1)
4532    menu_field{4}='filter2';
4533end
4534set(handles.field_ref1,'String',menu_field);
4535set(handles.field_ref1,'Value',length(menu_field));
4536set(handles.inf_sup1,'Value',2);
4537set(handles.thresh_vel,'String','1');%default threshold
4538set(handles.ref_fix1,'Enable','on')
4539
4540%------------------------------------------------------------------------
4541% --- Executes on button press in get_ref_fix2.
4542function get_ref_fix2_Callback(hObject, eventdata, handles)
4543%------------------------------------------------------------------------
4544if isequal(get(handles.get_ref_fix2,'Value'),1)
4545    filebase=get(handles.RootName,'String');
4546    [FileName, PathName, filterindex] = uigetfile( ...
4547        {'*.nc', ' (*.nc)';
4548        '*.nc',  'netcdf files '; ...
4549        '*.*', 'All Files (*.*)'}, ...
4550        'Pick a file',filebase);
4551    fileinput=[PathName FileName];
4552    sizf=size(fileinput);
4553    if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
4554    [Path,File,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
4555    ref.filebase=fullfile(Path,File);
4556    ref.num_a=stra2num(str_a);
4557    ref.num_b=stra2num(str_b);
4558    ref.num1=str2num(field_count);
4559    ref.num2=str2num(str2);
4560    browse=[];%initialisation
4561    if ~isequal(ref.ext,'.nc')
4562        msgbox_uvmat('ERROR','the reference file must be in netcdf format (*.nc)')
4563        return
4564    end
4565    set(handles.ref_fix2,'String',[fullfile(ref.subdir,File) '....nc']);
4566    set(handles.ref_fix2,'UserData',ref)
4567    menu_field{1}='civ1';
4568    Data=nc2struct(fileinput,[]);
4569    if isfield(Data,'patch') & isequal(Data.patch,1)
4570        menu_field{2}='filter1';
4571    end
4572    if isfield(Data,'civ2') & isequal(Data.civ2,1)
4573        menu_field{3}='civ2';
4574    end
4575    if isfield(Data,'patch2') & isequal(Data.patch2,1)
4576        menu_field{4}='filter2';
4577    end
4578    set(handles.field_ref2,'String',menu_field);
4579    set(handles.field_ref2,'Value',length(menu_field));
4580    set(handles.inf_sup2,'Value',2);
4581    set(handles.thresh_vel2,'String','1');%default threshold
4582    set(handles.ref_fix2,'Enable','on')
4583    set(handles.ref_fix2,'Visible','on')
4584    set(handles.field_ref2,'Visible','on')
4585else
4586    set(handles.ref_fix2,'Visible','off')
4587    set(handles.field_ref2,'Visible','off')
4588end
4589
4590%------------------------------------------------------------------------
4591function ref_fix1_Callback(hObject, eventdata, handles)
4592%------------------------------------------------------------------------
4593set(handles.inf_sup1,'Value',1);
4594set(handles.field_ref1,'Value',1)
4595set(handles.field_ref1,'String',{' '})
4596set(handles.ref_fix1,'UserData',[]);
4597set(handles.ref_fix1,'String','');
4598set(handles.thresh_vel1,'String','0');
4599
4600%------------------------------------------------------------------------
4601function ref_fix2_Callback(hObject, eventdata, handles)
4602%------------------------------------------------------------------------
4603set(handles.inf_sup2,'Value',1);
4604set(handles.field_ref2,'Value',1)
4605set(handles.field_ref2,'String',{' '})
4606set(handles.ref_fix2,'UserData',[]);
4607set(handles.ref_fix2,'String','');
4608set(handles.thresh_vel2,'String','0');
4609
4610%------------------------------------------------------------------------
4611% --- Executes on button press in test_stereo1.
4612function test_stereo1_Callback(hObject, eventdata, handles)
4613%------------------------------------------------------------------------
4614if isequal(get(handles.test_stereo1,'Value'),0)
4615    set(handles.subdomain_patch1,'Visible','on')
4616    set(handles.rho_patch1,'Visible','on')
4617else
4618    set(handles.subdomain_patch1,'Visible','off')
4619    set(handles.rho_patch1,'Visible','off')
4620end
4621
4622%------------------------------------------------------------------------
4623% --- Executes on button press in test_stereo2.
4624function test_stereo2_Callback(hObject, eventdata, handles)
4625%------------------------------------------------------------------------
4626if isequal(get(handles.test_stereo2,'Value'),0)
4627    set(handles.subdomain_patch2,'Visible','on')
4628    set(handles.rho_patch2,'Visible','on')
4629else
4630    set(handles.subdomain_patch2,'Visible','off')
4631    set(handles.rho_patch2,'Visible','off')
4632end
4633
4634%------------------------------------------------------------------------
4635% --- Executes on button press in ImaThreshold.
4636function ImaThreshold_Callback(hObject, eventdata, handles)
4637%------------------------------------------------------------------------
4638if isequal(get(handles.ImaThreshold,'Value'),1)
4639    set(handles.MinIma,'Visible','on')
4640    set(handles.MaxIma,'Visible','on')
4641else
4642    set(handles.MinIma,'Visible','off')
4643    set(handles.MaxIma,'Visible','off')
4644end
4645
4646%------------------------------------------------------------------------
4647% --- Executes on button press in ImaThreshold2.
4648function ImaThreshold2_Callback(hObject, eventdata, handles)
4649%------------------------------------------------------------------------
4650if isequal(get(handles.ImaThreshold2,'Value'),1)
4651    set(handles.MinIma2,'Visible','on')
4652    set(handles.MaxIma2,'Visible','on')
4653else
4654    set(handles.MinIma2,'Visible','off')
4655    set(handles.MaxIma2,'Visible','off')
4656end
4657
4658%------------------------------------------------------------------------
4659% --- Executes on button press in TestCiv1: display image correlation function
4660function TestCiv1_Callback(hObject, eventdata, handles)
4661%------------------------------------------------------------------------
4662set(handles.TestCiv1,'BackgroundColor',[1 1 0])
4663drawnow
4664test_civ1=get(handles.TestCiv1,'Value');
4665if test_civ1
4666    ref_i=str2double(get(handles.ref_i,'String'));
4667    if strcmp(get(handles.ref_j,'Visible'),'on')
4668        ref_j=str2double(get(handles.ref_j,'String'));
4669    else
4670        ref_j=1;%default
4671    end
4672    [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]=...
4673        set_civ_filenames(handles,ref_i,ref_j,[1 0 0 0 0 0]);
4674    Data.ListVarName={'ny','nx','A'};
4675    Data.VarDimName={'ny','nx',{'ny','nx'}};
4676    Data.A=imread(filecell.ima1.civ1{1});
4677    Data.ny=[size(Data.A,1) 1];
4678    Data.nx=[1 size(Data.A,2)];
4679    par_civ1=read_param_civ1(handles,filecell.ima1.civ1{1});
4680    par_civ1.filename_ima_a=filecell.ima1.civ1{1};
4681    par_civ1.filename_ima_b=filecell.ima2.civ1{1};
4682    par_civ1.T0=0;
4683    par_civ1.Dt=1;
4684    Data=civ_uvmat(par_civ1);
4685%     stepx=str2num(par_civ1.dx);
4686%     stepy=str2num(par_civ1.dy);
4687%     ibx2=ceil(str2num(par_civ1.ibx)/2);
4688%     iby2=ceil(str2num(par_civ1.iby)/2);
4689%     isx2=ceil(str2num(par_civ1.isx)/2);
4690%     isy2=ceil(str2num(par_civ1.isy)/2);
4691%     shiftx=str2num(par_civ1.shiftx);
4692%     shifty=str2num(par_civ1.shifty);
4693% %             ibx=2*ibx2-1;%ibx and iby odd, reduced by 1 if even
4694% %             iby=2*iby2-1;
4695%     miniy=max(1+isy2-shifty,1+iby2)
4696%     minix=max(1+isx2-shiftx,1+ibx2)
4697%     maxiy=min(size(Data.A,1)-isy2-shifty,size(Data.A,1)-iby2)
4698%     maxix=min(size(Data.A,2)-isx2-shiftx,size(Data.A,2)-ibx2)
4699% %             maxix=stepx*(floor(size(image1_roi,2)/stepx))-(2*ibx2-2)+ibx2;
4700%     [GridX,GridY]=meshgrid(minix:stepx:maxix,miniy:stepy:maxiy);
4701%     PointCoord(:,1)=reshape(GridX,[],1);
4702%     PointCoord(:,2)=reshape(GridY,[],1);
4703    Data.ListVarName=[Data.ListVarName {'ny','nx','A'}];
4704    Data.VarDimName=[Data.VarDimName {'ny','nx',{'ny','nx'}}];
4705    Data.A=imread(filecell.ima1.civ1{1});
4706    Data.ny=[size(Data.A,1) 1];
4707    Data.nx=[1 size(Data.A,2)];
4708    hview_field=view_field(Data);
4709    set(0,'CurrentFigure',hview_field)
4710    hhview_field=guihandles(hview_field);
4711    set(hview_field,'CurrentAxes',hhview_field.axes3)
4712    %hpoints=line(PointCoord(:,1),PointCoord(:,2),'Color','y','LineStyle','.','Marker','*','LineWidth',4,'Tag','grid_points');
4713    ViewData=get(hview_field,'UserData');
4714    ViewData.CivHandle=handles.civ;% indicate the handle of the civ GUI in view_field
4715    ViewData.axes3.B=imread(filecell.ima2.civ1{1});%store the second image in the UserData of the GUI view_field
4716    ViewData.axes3.X=Data.Civ1_X; %keep the set of points in memeory
4717    ViewData.axes3.Y=Data.Civ1_Y;
4718    set(hview_field,'UserData',ViewData)
4719    corrfig=findobj(allchild(0),'tag','corrfig');% look for a current figure for image correlation display
4720    if isempty(corrfig)
4721        corrfig=figure;
4722        set(corrfig,'tag','corrfig')
4723        set(corrfig,'name','image correlation')
4724        set(corrfig,'DeleteFcn',{@closeview_field})%
4725    end
4726    set(handles.TestCiv1,'BackgroundColor',[1 0 0])
4727else
4728    corrfig=findobj(allchild(0),'tag','corrfig');% look for a current figure for image correlation display
4729    if ~isempty(corrfig)
4730        delete(corrfig)
4731    end
4732    hview_field=findobj(allchild(0),'tag','view_field');% look for view_field   
4733    if ~isempty(hview_field)
4734        delete(hview_field)
4735    end
4736end
4737
4738function closeview_field(gcbo,eventdata)
4739hview_field=findobj(allchild(0),'tag','view_field');% look for view_field   
4740    if ~isempty(hview_field)
4741        delete(hview_field)
4742    end
4743%-------------------------------------------------------------------
4744% --- Executes on button press in status.
4745function status_Callback(hObject, eventdata, handles)
4746%-------------------------------------------------------------------
4747val=get(handles.status,'Value');
4748if val==0
4749    hfig=findobj(allchild(0),'name','civ_status');
4750    if ~isempty(hfig)
4751        delete(hfig)
4752    end
4753    return
4754end
4755listtype={'civ1','fix1','patch1','civ2','fix2','patch2'};
4756box_test(1)=get(handles.CIV1,'Value');
4757box_test(2)=get(handles.FIX1,'Value');
4758box_test(3)=get(handles.PATCH1,'Value');
4759box_test(4)=get(handles.CIV2,'Value');
4760box_test(5)=get(handles.FIX2,'Value');
4761box_test(6)=get(handles.PATCH2,'Value');
4762option_civ=find(box_test,1,'last');%last selected option (non-zero index of box_test)
4763filecell=get(handles.civ,'UserData');
4764if ~isfield(filecell,'nc')
4765    [ref_i,ref_j,errormsg]=find_ref_indices(handles);
4766    if ~isempty(errormsg)
4767        msgbox_uvmat('ERROR',errormsg)
4768        return
4769    end
4770    filecell=set_civ_filenames(handles,ref_i,ref_j,box_test);%determine the list of output files expected from the GUI status
4771end
4772
4773if ~isequal(box_test(4:6),[0 0 0])
4774    civ_files=filecell.nc.civ2;
4775else
4776    civ_files=filecell.nc.civ1;
4777end
4778[root,filename,ext]=fileparts(civ_files{1});
4779[rootroot,subdir,extdir]=fileparts(root);
4780hfig=findobj(allchild(0),'name','civ_status');
4781if isempty(hfig)
4782    hfig=figure;
4783    set(hfig,'name','civ_status')
4784    hlist=uicontrol('Style','listbox','Units','normalized', 'Position',[0.05 0.09 0.9 0.71], 'Callback', @open_view_field,'tag','list');
4785    uicontrol('Style','listbox','Units','normalized', 'Position', [0.05 0.87 0.9 0.1],'tag','msgbox');
4786    uicontrol('Style','frame','Units','normalized', 'Position', [0.05 0.81 0.9 0.05]);
4787    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);
4788    BarPosition=[0.05 0.81 0.01 0.05];
4789    hwaitbar=uicontrol('Style','frame','Units','normalized', 'Position',BarPosition ,'BackgroundColor',[1 0 0],'tag','waitbar');
4790end
4791% datnum=[];
4792Tabchar={};
4793nbfiles=numel(civ_files);
4794count=0;
4795testrecent=0;
4796while count<nbfiles
4797    count=0;
4798    datnum=zeros(1,nbfiles);
4799    for ifile=1:nbfiles
4800        detect=exist(civ_files{ifile},'file'); % check the existence of the file
4801        option=0;
4802        if detect==0
4803            option_str='not created';
4804        else
4805            datfile=dir(civ_files{ifile});
4806            if isfield(datfile,'datenum')
4807                datnum(ifile)=datfile.datenum;%only available in recent matlab versions
4808                testrecent=1;
4809            end
4810            filefound(ifile)={datfile.name};
4811            lastfield='';
4812            % check the content  netcdf file
4813            Data=nc2struct(civ_files{ifile},'ListGlobalAttribute','patch2','fix2','civ2','patch','fix');
4814            if ~isempty(Data.patch2) && isequal(Data.patch2,1)
4815                option=6;
4816                option_str='patch2';
4817            elseif ~isempty(Data.fix2) && isequal(Data.fix2,1)
4818                option=5;
4819                option_str='fix2';
4820            elseif ~isempty(Data.civ2) && isequal(Data.civ2,1);
4821                option=4;
4822                option_str='civ2';
4823            elseif ~isempty(Data.patch) && isequal(Data.patch,1);
4824                option=3;
4825                option_str='patch1';
4826            elseif ~isempty(Data.fix) && isequal(Data.fix,1);
4827                option=2;
4828                option_str='fix1';
4829            else
4830                option=1;
4831                option_str='civ1';
4832            end
4833        end
4834        if option >= option_civ
4835            count=count+1;
4836        end
4837        [rr,filename,ext]=fileparts(civ_files{ifile});
4838        Tabchar{ifile,1}=[fullfile([subdir extdir],filename) ext  '...' option_str];
4839    end
4840    datnum=datnum(datnum~=0);%keep the non zero values corresponding to existing files
4841    if isempty(datnum)
4842        if testrecent
4843            message='no civ result created yet';
4844        else
4845            message='';
4846        end
4847    else
4848        datnum=datnum(datnum~=0);%keep the non zero values corresponding to existing files
4849        [first,ind]=min(datnum);
4850        [last,indlast]=max(datnum);
4851        message={[num2str(count) ' file(s) done over ' num2str(nbfiles)] ;['oldest modification:  ' cell2mat(filefound(ind)) ' : ' datestr(first)];...
4852            ['latest modification:  ' cell2mat(filefound(indlast)) ' : ' datestr(last)]};
4853    end
4854    hfig=findobj(allchild(0),'name','civ_status');
4855    if isempty(hfig)
4856        set(handles.status,'Value',0)%stop program if the figure has been suppressed
4857        return
4858    else
4859        hlist=findobj(hfig,'tag','list');
4860        hmsgbox=findobj(hfig,'tag','msgbox');
4861        hwaitbar=findobj(hfig,'tag','waitbar');
4862        set(hlist,'String',Tabchar)
4863        set(hmsgbox,'String', message)
4864        if count>0
4865            BarPosition(3)=0.9*count/nbfiles;
4866            set(hwaitbar,'Position',BarPosition)
4867        end
4868    end
4869    set(hlist,'UserData',rootroot)
4870    pause(5)% wait 5 seconds for next check
4871end
4872
4873   
4874%-------------------------------------------------------------------   
4875% call 'view_field.fig' to display the selected field
4876function open_view_field(hObject, eventdata)
4877%-------------------------------------------------------------------
4878     list=get(gcbo,'String');
4879     index=get(gcbo,'Value');
4880     rootroot=get(gcbo,'UserData');
4881     filename=list{index};
4882     ind_dot=findstr(filename,'...');
4883     filename=filename(1:ind_dot-1);
4884      filename=fullfile(rootroot,filename);
4885      if exist(filename,'file')%visualise the vel field if it exists
4886        %[Field,VelTypeOut]=read_civxdata(filename);
4887        %view_field(Field)
4888        uvmat(filename)
4889        set(gcbo,'Value',1)
4890      end
4891
4892%-------------------------------------------------------------------   
4893% call 'view_field.fig' to display the selected field
4894function close_GUI(hObject, eventdata)
4895%-------------------------------------------------------------------
4896     delete(gcbf)
4897
4898
4899% --- Executes on button press in CivAll.
4900function CivAll_Callback(hObject, eventdata, handles)
4901Listprog=get(handles.CivAll,'String');
4902index=get(handles.CivAll,'Value');
4903prog=Listprog{index};
4904switch prog
4905    case 'CivX'
4906        set(handles.thresh_patch1,'Visible','off')
4907        set(handles.thresh_text1,'Visible','off')
4908        set(handles.rho,'Style','edit')
4909        set(handles.rho,'String','1')
4910    case 'CivAll'
4911        if get(handles.PATCH1,'Value')
4912            set(handles.thresh_patch1,'Visible','on')
4913            set(handles.thresh_text1,'Visible','on')
4914        end
4915        set(handles.rho,'Style','edit')
4916        set(handles.rho,'String','1')
4917    case 'CivUvmat'
4918        if get(handles.PATCH1,'Value')
4919            set(handles.thresh_patch1,'Visible','on')
4920            set(handles.thresh_text1,'Visible','on')
4921        end
4922        set(handles.rho,'Style','popupmenu')
4923        set(handles.rho,'Value',1)
4924        set(handles.rho,'String',{'1';'2'})
4925end
4926
Note: See TracBrowser for help on using the repository browser.