source: trunk/src/civ.m @ 252

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

various bugs corrected.

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