source: trunk/src/civ.m @ 224

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

civ : case of relative path for binaries treated. Test_civ improved: real time calculation of image correlation

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