source: trunk/src/civ.m @ 178

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

condition on status in civ, change of viw_field by uvmat, bug correction in uvmat in case of input file

File size: 188.6 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 05-Jan-2011 16:19:30
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,1)
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')%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,1)
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=find(box_test==1);
1601if isempty(index)
1602    msgbox_uvmat('ERROR','no selected operation')
1603    return
1604end
1605index_first=min(index);
1606index_last=max(index);
1607box_used=box_test(index_first : index_last);
1608[box_missing,ind_missing]=min(box_used);
1609if isequal(box_missing,0)
1610    msgbox_uvmat('ERROR',['missing' cell2mat(operations(ind_missing))]);
1611    return
1612end
1613
1614%% check mask if selecetd
1615if isequal(get(handles.get_mask_civ1,'Value'),1)
1616    maskname=get(handles.mask_civ1,'String');
1617    if ~exist(maskname,'file')
1618        get_mask_civ1_Callback(hObject, eventdata, handles);
1619    end
1620end
1621if isequal(get(handles.get_mask_fix1,'Value'),1)
1622    maskname=get(handles.mask_fix1,'String');
1623    if ~exist(maskname,'file')
1624        get_mask_fix1_Callback(hObject, eventdata, handles);
1625    end
1626end
1627if isequal(get(handles.get_mask_civ2,'Value'),1)
1628    maskname=get(handles.mask_civ2,'String');
1629    if ~exist(maskname,'file')
1630        get_mask_civ2_Callback(hObject, eventdata, handles);
1631    end
1632end
1633if isequal(get(handles.get_mask_fix2,'Value'),1)
1634    maskname=get(handles.mask_fix2,'String');
1635    if ~exist(maskname,'file')
1636        get_mask_fix2_Callback(hObject, eventdata, handles);
1637    end
1638end
1639
1640%% reinitialise status callback
1641if isfield(handles,'status')
1642set(handles.status,'Value',0);%suppress status display
1643status_Callback(hObject, eventdata, handles)
1644end
1645
1646%% set the list of files and check them
1647display('checking the files...')
1648%compare=get(handles.compare,'Value');%test for usual PIV (compare=1) or displacement (=2) or stereo PIV (=3)
1649[filecell,num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2,nom_type_nc]=...
1650    set_civ_filenames(handles,box_test);
1651set(handles.civ,'UserData',filecell);%store for futur use of status callback
1652if isempty(filecell)% (error message displayed in fct set_civ_filenames)
1653    return
1654end
1655nbfield=numel(num1_civ1);
1656nbslice=numel(num_a_civ1);
1657
1658%% choice of batch priority
1659ind_answer=2;
1660if batch
1661    [s,w]=unix('qstat -q civ.q|grep job_| wc -l'); %check the waiting list (command unix)
1662    if isequal(s,0)
1663        w(end)=[];
1664        str_displ={[w ' jobs in the waiting list'];'Select a priority:'};
1665        str={'urgent';'normal';'low'};
1666        [ind_answer,v] = listdlg('PromptString',str_displ,...
1667            'SelectionMode','single',...
1668            'ListString',str,'ListSize',[200 200],'Name','job priority','InitialValue',3);
1669        if isequal(v,0) % to handle Cancel button and figure close,
1670            return % a better way should be create
1671        end
1672    else
1673        msgbox_uvmat('ERROR','batch system not available')
1674        return
1675    end
1676else
1677    if isunix
1678        [xx,w]=unix('ps faux |grep civ|wc -l');
1679        w(end)=[];
1680        if str2double(w)+numel(num1_civ1)> 50
1681            msgbox_uvmat('ERROR',{['There are already ' w ' civ processes running locally'];'Use BATCH or submit RUN later'})
1682            return
1683        end
1684    end
1685end
1686
1687
1688%% read names of the .exe files for PIV and patch
1689path_UVMAT=fileparts(which('uvmat')); %path to the source directory of uvmat
1690xmlfile='PARAM.xml';
1691if exist(xmlfile,'file')% search parameter xml file in the whole matlab path
1692    t=xmltree(xmlfile);
1693    s=convert(t);
1694end
1695test_interp=0;
1696if batch
1697    if isfield(s,'BatchParam')
1698        sparam=s.BatchParam;
1699        if ~ismember(sparam.BatchMode,{'sge'})
1700            msgbox_uvmat('ERROR',['batch mode ' sparam.BatchMode ' not supported by UVMAT'])
1701        end
1702    else
1703        msgbox_uvmat('ERROR','no batch mode defined in PARAM.xml')
1704        return
1705    end
1706else
1707    if isfield(s,'RunParam')
1708        sparam=s.RunParam;
1709    else
1710        msgbox_uvmat('ERROR','no civ binaries defined in PARAM.xml')
1711        return
1712    end
1713    if isfield(sparam,'CivBin')
1714        if ~exist(sparam.CivBin,'file')
1715            sparam.CivBin=fullfile(path_UVMAT,sparam.CivBin);
1716        end
1717    end
1718    if isfield(sparam,'Civ1Bin')
1719        if ~exist(sparam.Civ1Bin,'file')
1720            sparam.Civ1Bin=fullfile(path_UVMAT,sparam.Civ1Bin);
1721        end
1722    end
1723    if isfield(sparam,'Civ2Bin')
1724        if ~exist(sparam.Civ2Bin,'file')
1725            sparam.Civ2Bin=fullfile(path_UVMAT,sparam.Civ2Bin);
1726        end
1727    end
1728    %test_interp=get(handles.test_interp,'Value');
1729
1730    if  isfield(sparam,'PatchBin')
1731        if ~exist(sparam.PatchBin,'file')
1732            sparam.PatchBin=fullfile(path_UVMAT,sparam.PatchBin);
1733        end
1734    end
1735    % if test_interp && isfield(sparam,'PatchNewBin')
1736    %     if ~exist(sparam.PatchNewBin,'file')
1737    %          sparam.PatchNewBin=fullfile(path_UVMAT,sparam.PatchNewBin);
1738    %     end
1739    % end
1740    if isfield(sparam,'FixBin')
1741        if ~exist(sparam.FixBin,'file')
1742            sparam.FixBin=fullfile(path_UVMAT,sparam.FixBin);
1743        end
1744    end
1745end
1746if batch
1747    if isfield(sparam,'BatchMode')
1748        batch_mode=sparam.BatchMode;
1749    end
1750else
1751%     MaxCivProcesses=50;
1752%     if isfield(sparam,'MaxCivProcesses')
1753%         MaxCivProcesses=str2double(sparam.MaxCivProcesses);
1754%     end
1755end
1756
1757
1758%% get civ1 parameters:
1759display('files OK, processing...')
1760%get civ parameters
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.Experimental,'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            %fid=fopen([filename '.cmx'],'w')
1868            cmd='#!/bin/bash \n';
1869            cmd=[cmd '#$ -cwd \n'];
1870            cmd=[cmd 'hostname && date \n'];
1871            cmd=[cmd 'umask 002 \n'];%allow writting access for user group for created files
1872        end
1873        if civAll
1874            civAllxml=xmltree;% xml contents,  all parameters
1875            civAllCmd='';
1876            civAllxml=set(civAllxml,1,'name','CivDoc');
1877        end
1878        %filename_cur=filecell.nc.civ1{ifile,j};%output netcdf file
1879        [Rootbat,Filebat]=fileparts(filecell.nc.civ1{ifile,j});%output netcdf file (without extention)
1880        flname=fullfile(Rootbat,Filebat);
1881        %filename_cmx(end-1:end+6)='civ1.cmx';%name of cmx file
1882        if batch
1883%             [Rootbat,Filebat,extbat]=fileparts(filename_cmx);
1884            filename_bat=fullfile(Rootbat,['job_' Filebat]);
1885         else
1886            filename_bat=flname;
1887        end
1888        filename_bat=[filename_bat '.bat'];
1889       
1890        %CIV1
1891        if box_test(1)==1
1892            par_civ1.filename_ima_a=filecell.ima1.civ1{ifile,j};
1893            par_civ1.filename_ima_b=filecell.ima2.civ1{ifile,j};
1894            %namelog=[fullfile(Rootbat,Filebat) '.civ1.log'];
1895            par_civ1.Dt=num2str(time(num2_civ1(ifile),num_b_civ1(j))-time(num1_civ1(ifile),num_a_civ1(j)));
1896            par_civ1.T0=num2str((time(num2_civ1(ifile),num_b_civ1(j))+time(num1_civ1(ifile),num_a_civ1(j)))/2);
1897            par_civ1.term_a=num2stra(num_a_civ1(j),nom_type_nc);%UTILITE?
1898            par_civ1.term_b=num2stra(num_b_civ1(j),nom_type_nc);%
1899            test_mask=get(handles.get_mask_civ1,'Value');
1900            if test_mask==0
1901                par_civ1.maskname='noFile use default';
1902                par_civ1.maskflag='n';
1903            else
1904                maskdispl=get(handles.mask_civ1,'String');
1905                if exist(maskdispl,'file')
1906                    par_civ1.maskname=maskdispl;
1907                    par_civ1.maskflag='y';
1908                else
1909                    maskbase=[filecell.filebase '_' maskdispl]; %
1910                    nbslice_mask=str2double(maskdispl(1:end-4)); %
1911                    num1_mask=mod(num1_civ1(ifile)-1,nbslice_mask)+1;
1912                    par_civ1.maskname=name_generator(maskbase,num1_mask,1,'.png','_i');
1913                    if exist(par_civ1.maskname,'file')
1914                        par_civ1.maskflag='y';
1915                    else
1916                        par_civ1.maskname='noFile use default';
1917                        par_civ1.maskflag='n';
1918                    end
1919                end
1920            end
1921           
1922            test_grid=get(handles.browse_gridciv1,'Value');
1923            if test_grid
1924                par_civ1.gridflag='y';
1925                gridname=get(handles.grid_civ1,'String');
1926                if isequal(gridname(end-3:end),'grid')
1927                    nbslice_grid=str2double(gridname(1:end-4)); %
1928                    if ~isnan(nbslice_grid)
1929                        num1_grid=mod(num1_civ1(ifile)-1,nbslice_grid)+1;
1930                        par_civ1.gridname=[filecell.filebase '_' name_generator(gridname,num1_grid,1,'.grid','_i')];
1931                        if ~exist(par_civ1.gridname,'file')
1932                            msgbox_uvmat('ERROR','grid file absent for civ1')
1933                        end
1934                    elseif exist(gridname,'file')
1935                        par_civ1.gridname=gridname;
1936                    else
1937                        msgbox_uvmat('ERROR','grid file absent for civ1')
1938                    end
1939                end
1940            else
1941                par_civ1.gridname='noFile use default';
1942                par_civ1.gridflag='n';
1943            end
1944            %
1945            i_cmd=i_cmd+1;
1946            if isequal(civAll,0)
1947                cmd=[cmd CIV1_CMD(fullfile(Rootbat,Filebat),'',par_civ1,handles,sparam) '\n'];
1948            else
1949                civAllCmd=[civAllCmd ' civ1 '];
1950                str=CIV1_CMD_Unified(fullfile(Rootbat,Filebat),'',par_civ1);
1951                fieldnames=fields(str);
1952                [civAllxml,uid_civ1]=add(civAllxml,1,'element','civ1');
1953                for ilist=1:length(fieldnames)
1954                    val=eval(['str.' fieldnames{ilist}]);
1955                    if ischar(val)
1956                        [civAllxml,uid_t]=add(civAllxml,uid_civ1,'element',fieldnames{ilist});
1957                        [civAllxml,uid_t2]=add(civAllxml,uid_t,'chardata',val);
1958                    end
1959                end
1960            end
1961        end
1962       
1963        % FIX1
1964        if box_test(2)==1
1965            test_mask=get(handles.get_mask_fix1,'Value');
1966            if test_mask==0
1967                maskname='';
1968            else
1969                maskdispl=get(handles.mask_fix1,'String');
1970                nbslice_mask=str2double(maskdispl(1:end-4)); %
1971                num1_mask=mod(num1_civ1(ifile)-1,nbslice_mask)+1;
1972                maskbase=[filecell.filebase '_' maskdispl];
1973                maskname=name_generator(maskbase,num1_mask,1,'.png','_i');
1974            end
1975            if isequal(civAll,0)
1976                if isunix %unix system
1977                cmd_FIX=[sparam.FixBin ' -f ' filecell.nc.civ1{ifile,j} ' -fi1 ' num2str(flagindex1(1)) ...
1978                    ' -fi2 ' num2str(flagindex1(2)) ' -fi3 ' num2str(flagindex1(3)) ...
1979                    ' -threshC ' num2str(thresh_vecC1) ' -threshV ' num2str(thresh_vel1) ' -maskName ' maskname];
1980                else %windows system
1981                    cmd_FIX=['"' sparam.FixBin '" -f "' filecell.nc.civ1{ifile,j} '" -fi1 ' num2str(flagindex1(1)) ...
1982                    ' -fi2 ' num2str(flagindex1(2)) ' -fi3 ' num2str(flagindex1(3)) ...
1983                    ' -threshC ' num2str(thresh_vecC1) ' -threshV ' num2str(thresh_vel1) ' -maskName "' maskname '"'];
1984                    cmd_FIX=regexprep(cmd_FIX,'\\','\\\\');
1985                end
1986                cmd=[cmd cmd_FIX '\n'];
1987            else
1988                fix1.inputFileName=filecell.nc.civ1{ifile,j} ;
1989                fix1.fi1=num2str(flagindex1(1));
1990                fix1.fi2=num2str(flagindex1(2));
1991                fix1.fi3=num2str(flagindex1(3));
1992                fix1.threshC=num2str(thresh_vecC1);
1993                fix1.threshV=num2str(thresh_vel1);
1994                fieldnames=fields(fix1);
1995                [civAllxml,uid_fix1]=add(civAllxml,1,'element','fix1');
1996                for ilist=1:length(fieldnames)
1997                    val=eval(['fix1.' fieldnames{ilist}]);
1998                    if ischar(val)
1999                        [civAllxml,uid_t]=add(civAllxml,uid_fix1,'element',fieldnames{ilist});
2000                        [civAllxml,uid_t2]=add(civAllxml,uid_t,'chardata',val);
2001                    end
2002                end
2003                civAllCmd=[civAllCmd ' fix1 '];
2004            end
2005        end
2006       
2007        %PATCH1
2008        if box_test(3)==1
2009            if isequal(civAll,0)
2010                cmd_PATCH=PATCH_CMD(filecell.nc.civ1{ifile,j},nx_patch1,ny_patch1,rho_patch1,subdomain_patch1,thresh_patch1,test_interp,sparam.PatchBin);
2011                cmd=[cmd cmd_PATCH '\n'];
2012            else
2013                patch1.inputFileName=filecell.nc.civ1{ifile,j} ;
2014                patch1.nopt=subdomain_patch1;
2015                patch1.maxdiff=thresh_patch1;
2016                patch1.ro=rho_patch1;
2017                test_grid=get(handles.get_gridpatch1,'Value');
2018                if test_grid
2019                    patch1.gridflag='y';
2020                    gridname=get(handles.grid_patch1,'String');
2021                    if isequal(gridname(end-3:end),'grid')
2022                        nbslice_grid=str2double(gridname(1:end-4)); %
2023                        if ~isnan(nbslice_grid)
2024                            num1_grid=mod(num1_civ1(ifile)-1,nbslice_grid)+1;
2025                            patch1.gridPatch=[filecell.filebase '_' name_generator(gridname,num1_grid,1,'.grid','_i')];
2026                            if ~exist(patch1.gridPatch,'file')
2027                                msgbox_uvmat('ERROR','grid file absent for patch1')
2028                            end
2029                        elseif exist(gridname,'file')
2030                            patch1.gridPatch=gridname;
2031                        else
2032                            msgbox_uvmat('ERROR','grid file absent for patch1')
2033                        end
2034                    end
2035                else
2036                    patch1.gridPatch='none';
2037                    patch1.gridflag='n';
2038                    patch1.m=nx_patch1;
2039                    patch1.n=ny_patch1;
2040                end
2041                patch1.convectFlow='n';
2042                fieldnames=fields(patch1);
2043                [civAllxml,uid_patch1]=add(civAllxml,1,'element','patch1');
2044                for ilist=1:length(fieldnames)
2045                    val=eval(['patch1.' fieldnames{ilist}]);
2046                    if ischar(val)
2047                        [civAllxml,uid_t]=add(civAllxml,uid_patch1,'element',fieldnames{ilist});
2048                        [civAllxml,uid_t2]=add(civAllxml,uid_t,'chardata',val);
2049                    end
2050                end
2051                civAllCmd=[civAllCmd ' patch1 '];
2052            end
2053        end
2054       
2055        if box_test(4)==1 || box_test(5)==1 || box_test(6)==1
2056            filename_cmx=filecell.nc.civ2{ifile,j};%output netcdf file
2057            filename_cmx([end-1:end+1])=[ 'cmx'];%name of cmx file
2058%             filename_cmx=[filename_cmx 'x'];
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_a([end-3:end])=[];%remove .png extension
2064            par_civ2.filename_ima_b=filecell.ima2.civ2{ifile,j};
2065            %par_civ2.filename_ima_b([end-3:end])=[];%remove .png extension
2066            [Rootbat,Filebat]=fileparts(filecell.nc.civ2{ifile,j});%output netcdf file (without extention)
2067            %namelog=[fullfile(Rootbat,Filebat) '.civ2.log'];
2068            par_civ2.Dt=num2str(time(num2_civ2(ifile),num_b_civ2(j))-time(num1_civ2(ifile),num_a_civ2(j)));
2069            par_civ2.T0=num2str((time(num2_civ1(ifile),num_b_civ2(j))+time(num1_civ2(ifile),num_a_civ2(j)))/2);
2070            par_civ2.term_a=num2stra(num_a_civ2(j),nom_type_nc);
2071            par_civ2.term_b=num2stra(num_b_civ2(j),nom_type_nc);
2072            par_civ2.filename_nc1=filecell.nc.civ1{ifile,j};
2073            par_civ2.filename_nc1([end-2:end])=[]; % remove '.nc'
2074            test_mask=get(handles.get_mask_civ2,'Value');
2075            if test_mask==0
2076                par_civ2.maskname='noFile use default';
2077                par_civ2.maskflag='n';
2078            else
2079                maskdispl=get(handles.mask_civ2,'String');
2080                if exist(maskdispl,'file')
2081                    par_civ2.maskname=maskdispl;
2082                    par_civ2.maskflag='y';
2083                else
2084                    maskbase=[filecell.filebase '_' maskdispl]; %
2085                    nbslice_mask=str2double(maskdispl(1:end-4)); %
2086                    num1_mask=mod(num1_civ2(ifile)-1,nbslice_mask)+1;
2087                    par_civ2.maskname=name_generator(maskbase,num1_mask,1,'.png','_i');
2088                    if exist(par_civ2.maskname,'file')
2089                        par_civ2.maskflag='y';
2090                    else
2091                        par_civ2.maskname='noFile use default';
2092                        par_civ2.maskflag='n';
2093                    end
2094                end
2095            end
2096            %TESTgrid
2097            %test_grid=get(handles.browse_gridciv2,'Value');
2098            gridname=get(handles.grid_civ2,'String');
2099            %gridflag='y';
2100            if numel(gridname)>=4 && isequal(gridname(end-3:end),'grid')
2101                nbslice_grid=str2double(gridname(1:end-4)); %
2102                if ~isnan(nbslice_grid)
2103                    par_civ2.gridflag='y';
2104                    num1_grid=mod(num1_civ2(ifile)-1,nbslice_grid)+1;
2105                    par_civ2.gridname=[filecell.filebase '_' name_generator(gridname,num1_grid,1,'.grid','_i')];
2106                    if exist(par_civ2.gridname,'file')
2107                        par_civ2.gridflag='y';
2108                    else
2109                        par_civ2.gridname='noFile use default';
2110                        par_civ2.gridflag='n';
2111                    end
2112                elseif exist(gridname,'file')
2113                    par_civ2.gridflag='y';
2114                else
2115                    par_civ2.gridname='noFile use default';
2116                    par_civ2.gridflag='n';
2117                end
2118            end
2119            %endTESTgrid
2120            i_cmd=i_cmd+1;
2121            flname=fullfile(Rootbat,Filebat);
2122            cmd_CIV2=CIV2_CMD(flname,[],par_civ2,sparam);%creates the cmx file [fullfile(Rootbat,Filebat) '.civ2.cmx]
2123            if isequal(civAll,0)
2124                if(isunix)
2125                     cmd=[cmd 'cp -f ' flname '.civ2.cmx ' flname '.cmx\n' cmd_CIV2 '\n'];
2126                else
2127                    flname=regexprep(flname,'\\','\\\\');
2128                    cmd=[cmd 'copy /Y "' flname '.civ2.cmx" "' flname '.cmx"\n' cmd_CIV2 '\n'];
2129                end
2130            else
2131                civAllCmd=[civAllCmd ' civ2 '];
2132                str=CIV2_CMD_Unified(filename_cmx([1:end-4]),namelog,par_civ2);
2133                fieldnames=fields(str);
2134                [civAllxml,uid_civ2]=add(civAllxml,1,'element','civ2');
2135                for ilist=1:length(fieldnames)
2136                    val=eval(['str.' fieldnames{ilist}]);
2137                    if ischar(val)
2138                        [civAllxml,uid_t]=add(civAllxml,uid_civ2,'element',fieldnames{ilist});
2139                        [civAllxml,uid_t2]=add(civAllxml,uid_t,'chardata',val);
2140                    end
2141                end
2142            end
2143        end
2144       
2145        % FIX2
2146        if box_test(5)==1
2147            test_mask=get(handles.get_mask_fix2,'Value');
2148            if test_mask==0
2149                maskname=''; %no mask used
2150            else
2151                maskdispl=get(handles.mask_fix2,'String');
2152                maskbase=[filecell.filebase '_' maskdispl]; %
2153                nbslice_mask=str2double(maskdispl(1:end-4)); %
2154                num1_mask=mod(num1_civ2(ifile)-1,nbslice_mask)+1;
2155                maskname =name_generator(maskbase,num1_mask,1,'.png','_i');
2156            end
2157            if isequal(civAll,0)
2158                if isunix
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                else
2163                    cmd_FIX=['"' sparam.FixBin '" -f "' filecell.nc.civ2{ifile,j} '" -fi1 ' num2str(flagindex2(1)) ...
2164                    ' -fi2 ' num2str(flagindex2(2)) ' -fi3 ' num2str(flagindex2(3)) ...
2165                    ' -threshC ' num2str(thresh_vec2C) ' -threshV ' num2str(thresh_vel2) ' -maskName "' maskname '"'];
2166                    cmd_FIX=regexprep(cmd_FIX,'\\','\\\\');
2167                end
2168                cmd=[cmd cmd_FIX '\n'];
2169            else
2170                fix2.inputFileName=filecell.nc.civ2{ifile,j} ;
2171                fix2.fi1=num2str(flagindex2(1));
2172                fix2.fi2=num2str(flagindex2(2));
2173                fix2.fi3=num2str(flagindex2(3));
2174                fix2.threshC=num2str(thresh_vec2C);
2175                fix2.threshV=num2str(thresh_vel2);
2176                fieldnames=fields(fix2);
2177                [civAllxml,uid_fix2]=add(civAllxml,1,'element','fix2');
2178                for ilist=1:length(fieldnames)
2179                    val=eval(['fix2.' fieldnames{ilist}]);
2180                    if ischar(val)
2181                        [civAllxml,uid_t]=add(civAllxml,uid_fix2,'element',fieldnames{ilist});
2182                        [civAllxml,uid_t2]=add(civAllxml,uid_t,'chardata',val);
2183                    end
2184                end
2185                civAllCmd=[civAllCmd ' fix2 '];
2186            end
2187        end
2188       
2189        %PATCH2
2190        if box_test(6)==1
2191            if isequal(civAll,0)
2192                cmd_PATCH=PATCH_CMD(filecell.nc.civ2{ifile,j},nx_patch2,ny_patch2,rho_patch2,subdomain_patch2,thresh_patch2,test_interp,sparam.PatchBin);
2193                cmd=[cmd cmd_PATCH '\n'];
2194            else
2195                patch2.inputFileName=filecell.nc.civ1{ifile,j} ;
2196                patch2.nopt=subdomain_patch1;
2197                patch2.maxdiff=thresh_patch1;
2198                patch2.ro=rho_patch1;
2199                test_grid=get(handles.get_gridpatch2,'Value');
2200                if test_grid
2201                    patch2.gridflag='y';
2202                    gridname=get(handles.grid_patch2,'String');
2203                    if isequal(gridname(end-3:end),'grid')
2204                        nbslice_grid=str2double(gridname(1:end-4)); %
2205                        if ~isnan(nbslice_grid)
2206                            num1_grid=mod(num1_civ2(ifile)-1,nbslice_grid)+1;
2207                            patch2.gridPatch=[filecell.filebase '_' name_generator(gridname,num1_grid,1,'.grid','_i')];
2208                            if ~exist(patch2.gridPatch,'file')
2209                                msgbox_uvmat('ERROR','grid file absent for patch2')
2210                            end
2211                        elseif exist(gridname,'file')
2212                            patch2.gridPatch=gridname;
2213                        else
2214                            msgbox_uvmat('ERROR','grid file absent for patch2')
2215                        end
2216                    end
2217                else
2218                    patch2.gridPatch='none';
2219                    patch2.gridflag='n';
2220                    patch2.m=nx_patch2;
2221                    patch2.n=ny_patch2;
2222                end
2223                patch2.convectFlow='n';
2224                fieldnames=fields(patch2);
2225                [civAllxml,uid_patch2]=add(civAllxml,1,'element','patch2');
2226                for ilist=1:length(fieldnames)
2227                    val=eval(['patch2.' fieldnames{ilist}]);
2228                    if ischar(val)
2229                        [civAllxml,uid_t]=add(civAllxml,uid_patch2,'element',fieldnames{ilist});
2230                        [civAllxml,uid_t2]=add(civAllxml,uid_t,'chardata',val);
2231                    end
2232                end
2233                civAllCmd=[civAllCmd ' patch2 '];
2234            end
2235        end
2236        if isequal(civAll,1)
2237            save(civAllxml,[filename_cmx([1:end-4]) '.xml']);
2238            %cmd=char({cmd;[CivBin ' -f ' [filename_cmx([1:end-4]) '.xml'] ' ' civAllCmd]});
2239            cmd=[cmd CivBin ' -f ' filename_cmx(1:end-4) '.xml '  civAllCmd  '\n'];
2240        end
2241        % create the .bat file:
2242        [fid,message]=fopen(filename_bat,'w');
2243        if isequal(fid,-1)
2244            msgbox_uvmat('ERROR', ['creation of .bat file: ' message])
2245            return
2246        end
2247        fprintf(fid,cmd);
2248        fclose(fid);
2249        %dlmwrite(filename_bat,cmd,'');%write commands in filename_bat
2250        if batch
2251            switch batch_mode
2252                case 'sge'
2253                    pvalue=num2str((1-ind_answer)*500);
2254                    %namelog=[filename_bat '.patch.log'];
2255                    display(['!qsub -p ' pvalue ' -q civ.q -e ' flname '.errors -o ' flname '.log' ' ' filename_bat]);
2256                    eval(  ['!qsub -p ' pvalue ' -q civ.q -e ' flname '.errors -o ' flname '.log' ' ' filename_bat]);
2257            end
2258        else
2259            %% to lauch the jobs locally :
2260            if(isunix)
2261                cmd_str=['. ' filename_bat];
2262                % cmd_str=['!at -qb now -f ' filename_bat ' &']; %ou at -qb now -f bad idea...
2263            else %case of Windows
2264                cmd_str=['@call "' regexprep(filename_bat,'\\','\\\\') '"'];
2265            end
2266            super_cmd=[super_cmd cmd_str '\n'];         
2267            disp(cmd_str);
2268        end
2269    end
2270end
2271
2272if ~batch
2273    [Rootbat,Filebat,extbat]=fileparts(filename_bat);
2274    filename_superbat=fullfile(Rootbat,['job_list.bat']);
2275    fid=fopen(filename_superbat,'w');
2276    fprintf(fid,super_cmd');
2277    fclose(fid);
2278    if(isunix)
2279        eval(['!. ' filename_superbat ' &']);
2280    else
2281        eval(['!' filename_superbat ' &']);
2282    end
2283end
2284
2285%% save interface state
2286if isfield(filecell,'nc')
2287    if isfield(filecell.nc,'civ2')
2288        fileresu=filecell.nc.civ2{1,1};
2289    else
2290        fileresu=filecell.nc.civ1{1,1};
2291    end
2292end
2293[RootPath,RootFile,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fileresu);
2294namedoc=fullfile(RootPath,subdir,RootFile);
2295detect=1;
2296while detect==1
2297    namefigfull=[namedoc '.fig'];
2298    hh=dir(namefigfull);
2299    if ~isempty(hh)
2300        detect=1;
2301        namedoc=[namedoc '.0'];
2302    else
2303        detect=0;
2304    end
2305end
2306saveas(gcbf,namefigfull);%save the interface with name namefigfull (A CHANGER EN FICHIER  .xml)
2307
2308
2309
2310
2311function [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]=...
2312    set_civ_filenames(handles,box_test)
2313%------------------------------------------------------------------------
2314filecell=[];%default
2315
2316%% get the root names nomenclature and numbers
2317filebase=get(handles.RootName,'String');
2318
2319if isempty(filebase)||isequal(filebase,'')
2320    msgbox_uvmat('ERROR','no input files')
2321    return
2322end
2323
2324%filebase=regexprep(filebase,'\.fsnet','fsnet');% temporary fix for cluster Coriolis
2325filecell.filebase=filebase;
2326
2327browse=get(handles.browse_root,'UserData');
2328compare_list=get(handles.compare,'String');
2329val=get(handles.compare,'Value');
2330compare=compare_list{val};
2331if strcmp(compare,'displacement')
2332    mode='displacement';
2333else
2334    mode_list=get(handles.mode,'String');
2335    mode_value=get(handles.mode,'Value');
2336    mode=mode_list{mode_value};
2337end
2338%time=get(handles.RootName,'UserData'); %get the set of times
2339ext_ima=get(handles.ImaExt,'String');
2340nom_type_nc=browse.nom_type_nc;
2341if isfield(browse,'nom_type_ima')
2342    nom_type_ima2=browse.nom_type_ima;
2343end
2344if isempty(nom_type_ima2),nom_type_ima2='1';end; %default
2345if isempty(nom_type_nc),nom_type_nc='_i1-i2';end; %default
2346[num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2]=...
2347    find_pair_indices(handles,mode);
2348%determine the new filebase for 'displacement' mode (comparison of two series)
2349filebase_B=filebase;% root name of the second field series for stereo
2350if strcmp(compare,'displacement') || strcmp(compare,'stereo PIV')
2351%     test_disp=1;
2352    nom_type_ima1=browse.nom_type_ima_1; %nomenclature type of the second file series
2353    [Path2,Name2]=fileparts(filebase_B);
2354    Path1=Path2;
2355    Name1=get(handles.RootName_1,'String');% root name of the first field series for stereo
2356    filebase_A=fullfile(Path1,Name1);
2357    if length(Name1)>6
2358        Name1=Name1(end-5:end);
2359    end
2360    if length(Name2)>6
2361        Name2=Name2(end-5:end);
2362    end
2363    filebase_AB=fullfile(Path2,[Name2 '-' Name1]);
2364else
2365%     test_disp=0;
2366    filebase_A=filebase;
2367    nom_type_ima1=nom_type_ima2;
2368    filebase_AB=filebase;
2369end
2370if strcmp(compare,'displacement')
2371    filebase_ima1=filebase_A;
2372    filebase_ima2=filebase_B;
2373    filebase_nc=filebase_AB; %root name for the result of civ2
2374else
2375    filebase_ima1=filebase_B;
2376    filebase_ima2=filebase_B;
2377    filebase_nc=filebase_B;
2378end
2379
2380%determine reference files for fix:
2381file_ref_fix1={};%default
2382file_ref_fix2={};
2383nbfield=length(num1_civ1);
2384nbslice=length(num_a_civ1);
2385if box_test(2)==1% fix1 performed
2386    ref=get(handles.ref_fix1,'UserData');%read data on the ref file stored by get_ref_fix1_Callback
2387    if ~isempty(ref)
2388        first_i=str2double(get(handles.first_i,'String'));
2389        last_i=str2double(get(handles.last_i,'String'));
2390        incr_i=str2double(get(handles.incr_i,'String'));
2391        first_j=str2double(get(handles.first_j,'String'));
2392        last_j=str2double(get(handles.last_j,'String'));
2393        incr_j=str2double(get(handles.incr_j,'String'));
2394        num_i_ref=first_i:incr_i:last_i;
2395        num_j_ref=first_j:incr_j:last_j;
2396        if isequal(mode,'displacement')
2397            num_i1=num_i_ref;
2398            num_i2=num_i_ref;
2399            num_j1=num_j_ref;
2400            num_j2=num_j_ref;
2401        elseif isequal(mode,'pair j1-j2')% isequal(mode,'st_pair j1-j2')
2402            num_i1=num_i_ref;
2403            num_i2=num_i1;
2404            num_j1=ref.num_a*ones(size(num_i_ref));
2405            num_j2=ref.num_b*ones(size(num_i_ref));
2406        elseif isequal(mode,'series(Di)') % isequal(mode,'st_series(Di)')
2407            delta1=floor((ref.num2-ref.num1)/2);
2408            delta2=ceil((ref.num2-ref.num1)/2);
2409            num_i1=num_i_ref-delta1*ones(size(num_i_ref));
2410            num_i2=num_i_ref+delta2*ones(size(num_i_ref));
2411            if isempty(ref.num_a)
2412                ref.num_a=1;
2413            end
2414            num_j1=ref.num_a*ones(size(num_i1));
2415            num_j2=num_j1;
2416        elseif isequal(mode,'series(Dj)')%| isequal(mode,'st_series(Dj)')
2417            delta1=floor((ref.num_b-ref.num_a)/2);
2418            delta2=ceil((ref.num_b-ref.num_a)/2);
2419            num_i1=ref.num1*ones(size(num_i_ref));
2420            num_i2=num_i1;
2421            num_j1=num_j_ref-delta1*ones(size(num_j_ref));
2422            num_j2=num_j_ref+delta2*ones(size(num_j_ref));
2423        end
2424        for ifile=1:nbfield
2425            for j=1:nbslice
2426                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);%
2427                file_ref_fix1(ifile,j)={file_ref};
2428                if ~exist(file_ref,'file')
2429                    msgbox_uvmat('ERROR',['reference file ' file_ref ' not found for fix1'])
2430                    filecell=[];
2431                    return
2432                end
2433            end
2434        end
2435    end
2436end
2437
2438%determine reference files for fix2:
2439if box_test(5)==1% fix2 performed
2440    ref=get(handles.ref_fix2,'UserData');
2441    if ~isempty(ref)
2442        first_i=str2double(get(handles.first_i,'String'));
2443        last_i=str2double(get(handles.last_i,'String'));
2444        incr_i=str2double(get(handles.incr_i,'String'));
2445        first_j=str2double(get(handles.first_j,'String'));
2446        last_j=str2double(get(handles.last_j,'String'));
2447        incr_j=str2double(get(handles.incr_j,'String'));
2448        num_i_ref=[first_i:incr_i:last_i];
2449        num_j_ref=[first_j:incr_j:last_j];
2450        if isequal(mode,'displacement')
2451            num_i1=num_i_ref;
2452            num_i2=num_i_ref;
2453            num_j1=num_j_ref;
2454            num_j2=num_j_ref;
2455        elseif isequal(mode,'pair j1-j2')
2456            num_i1=num_i_ref;
2457            num_i2=num_i1;
2458            num_j1=ref.num_a;
2459            num_j2=ref.num_b;
2460        elseif isequal(mode,'series(Di)')
2461            delta1=floor((ref.num2-ref.num1)/2);
2462            delta2=ceil((ref.num2-ref.num1)/2);
2463            num_i1=num_i_ref-delta1*ones(size(num_i_ref));
2464            num_i2=num_i_ref+delta2*ones(size(num_i_ref));
2465            num_j1=ref.num_a*ones(size(num_i1));
2466            num_j2=num_j1;
2467        elseif isequal(mode,'series(Dj)')
2468            delta1=floor((ref.num_b-ref.num_a)/2);
2469            delta2=ceil((ref.num_b-ref.num_a)/2);
2470            num_i1=ref.num1*ones(size(num_i_ref));
2471            num_i2=num_i1;
2472            num_j1=num_j_ref-delta1*ones(size(num_j_ref));
2473            num_j2=num_j_ref+delta2*ones(size(num_j_ref));
2474        end
2475        for ifile=1:nbfield
2476            for j=1:nbslice
2477                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);%
2478                file_ref_fix2(ifile,j)={file_ref};
2479                if ~exist(file_ref,'file')
2480                    msgbox_uvmat('ERROR',['reference file ' file_ref ' not found for fix2'])
2481                    filecell={};
2482                    return
2483                end
2484            end
2485        end
2486    end
2487end
2488
2489%check dir
2490subdir_civ1=get(handles.subdir_civ1,'String');%subdirectory subdir_civ1 for the netcdf output data
2491subdir_civ2=get(handles.subdir_civ2,'String');
2492if isequal(subdir_civ1,''),subdir_civ1='CIV'; end% put default subdir
2493if isequal(subdir_civ2,''),subdir_civ2=subdir_civ1; end% put default subdir
2494currentdir=pwd;%store the current working directory
2495[Path_ima,Name]=fileparts(filebase);%Path of the image files (.civ)
2496if ~exist(Path_ima,'dir')
2497    msgbox_uvmat('ERROR',['path to images ' Path_ima ' not found'])
2498    filecell={};
2499    return
2500end
2501[xx,message]=fileattrib(Path_ima);
2502if ~isempty(message) && ~isequal(message.UserWrite,1)
2503    msgbox_uvmat('ERROR',['No writting access to ' Path_ima])
2504    filecell={};
2505    cd(currentdir);
2506    return
2507end
2508
2509%check the existence of the netcdf and image files involved
2510% %%%%%%%%%%%%  case CIV1 activated   %%%%%%%%%%%%%
2511if box_test(1)==1;
2512    detect=1;
2513    vers=0;
2514    subdir_civ1_new=subdir_civ1;
2515    while detect==1 %create a new subdir if the netcdf files already exist
2516        for ifile=1:nbfield
2517            for j=1:nbslice
2518                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);
2519                detect=exist(filename,'file')==2;
2520                if detect% if a netcdf file already exists
2521                    indstr=regexp(subdir_civ1_new,'\D');
2522                    if indstr(end)<length(subdir_civ1_new) %subdir_civ1 ends by a number
2523                        vers=str2double(subdir_civ1_new(indstr(end)+1:end))+1;
2524                        subdir_civ1_new=[subdir_civ1_new(1:indstr(end)) num2str(vers)];
2525                    else
2526                        vers=vers+1;
2527                        subdir_civ1_new=[subdir_civ1_new(1:indstr(end)) '_' num2str(vers)];       
2528                    end
2529                    subdir_civ2=subdir_civ1_new;
2530                    break
2531                end
2532                filecell.nc.civ1(ifile,j)={filename};
2533            end
2534            if detect% if a netcdf file already exists
2535                break
2536            end
2537        end
2538 
2539        %create the new subdir_civ1
2540        if ~exist(fullfile(Path_ima,subdir_civ1_new),'dir')
2541            cd(Path_ima);         
2542            [xx,msg1]=mkdir(subdir_civ1_new);
2543
2544            if ~strcmp(msg1,'')
2545                msgbox_uvmat('ERROR',['cannot create ' subdir_civ1_new ': ' msg1])%error message for directory creation
2546                filecell={};
2547                return
2548            elseif isunix         
2549                [xx,msg2] = fileattrib(subdir_civ1_new,'+w','g'); %yield writing access (+w) to user group (g)
2550                if ~strcmp(msg2,'')
2551                    msgbox_uvmat('ERROR',['pb of permission for  ' subdir_civ1_new ': ' msg2])%error message for directory creation
2552                    filecell={};
2553                    return
2554                end
2555            end
2556            cd(currentdir);
2557        end
2558        if strcmp(compare,'stereo PIV')&&(strcmp(mode,'pair j1-j2')||strcmp(mode,'series(Dj)')||strcmp(mode,'series(Di)'))%check second nc series
2559            for ifile=1:nbfield
2560                for j=1:nbslice
2561                    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);%
2562                    detect=exist(filename,'file')==2;
2563                    if detect% if a netcdf file already exists
2564                       indstr=regexp(subdir_civ1_new,'\D');
2565                       if indstr(end)<length(subdir_civ1_new) %subdir_civ1 ends by a number
2566                           vers=str2double(subdir_civ1_new(indstr(end)+1:end))+1;
2567                           subdir_civ1_new=[subdir_civ1_new(1:indstr(end)) num2str(vers)];
2568                       else
2569                           vers=vers+1;
2570                           subdir_civ1_new=[subdir_civ1_new '_' num2str(vers)];
2571                       end
2572                       subdir_civ2=subdir_civ1;
2573                       break
2574                    end
2575                    filecell.ncA.civ1(ifile,j)={filename};
2576                end
2577                if detect% if a netcdf file already exists
2578                    break
2579                end
2580            end
2581            %create the new subdir_civ1
2582            if exist(fullfile(Path_ima,subdir_civ1_new),'dir')
2583                   cd(Path_ima);         
2584                [xx,msg1]=mkdir(subdir_civ1_new);
2585                            cd(currentdir);
2586                if ~strcmpl(msg1,'')
2587                    msgbox_uvmat('ERROR',['cannot create ' subdir_civ1_new ': ' msg1])
2588                    cd(currentdir)
2589                    filecell={};
2590                    return
2591                else
2592                    [xx,msg2] = fileattrib(subdir_civ1_new,'+w','g'); %yield writing access (+w) to user group (g)
2593                    if ~strcmp(msg2,'')
2594                        msgbox_uvmat('ERROR',['pb of permission for ' subdir_civ1_new ': ' msg2])%error message for directory creation
2595                        cd(currentdir)
2596                        filecell={};
2597                        return
2598                    end
2599                end
2600            end
2601        end
2602    end
2603    subdir_civ1=subdir_civ1_new;
2604    % get image names
2605    for ifile=1:nbfield
2606        for j=1:nbslice
2607            filename=name_generator(filebase_ima1, num1_civ1(ifile),num_a_civ1(j),ext_ima,nom_type_ima1);
2608            idetect(j)=exist(filename,'file')==2;
2609            filecell.ima1.civ1(ifile,j)={filename}; %first image
2610            filename=name_generator(filebase_ima2, num2_civ1(ifile),num_b_civ1(j),ext_ima,nom_type_ima2);
2611            idetect_1(j)=exist(filename,'file')==2;
2612            filecell.ima2.civ1(ifile,j)={filename};%second image
2613        end
2614        [idetectmin,indexj]=min(idetect);
2615        if idetectmin==0,
2616            msgbox_uvmat('ERROR',[filecell.ima1.civ1{ifile,indexj} ' not found'])
2617            filecell={};
2618            cd(currentdir)
2619            return
2620        end
2621        [idetectmin,indexj]=min(idetect_1);
2622        if idetectmin==0,
2623            msgbox_uvmat('ERROR',[filecell.ima2.civ1{ifile,indexj} ' not found'])
2624            filecell={};
2625            cd(currentdir)
2626            return
2627        end
2628    end
2629    if strcmp(compare,'stereo PIV') && (strcmp(mode,'pair j1-j2') || strcmp(mode,'series(Dj)') || strcmp(mode,'series(Di)'))
2630        for ifile=1:nbfield
2631            for j=1:nbslice
2632                filename=name_generator(filebase_A, num1_civ1(ifile),num_a_civ1(j),ext_ima,nom_type_ima1);
2633                idetect(j)=exist(filename,'file')==2;
2634                filecell.imaA1.civ1(ifile,j)={filename} ;%first image
2635                filename=name_generator(filebase_A, num2_civ1(ifile),num_b_civ1(j),ext_ima,nom_type_ima2);
2636                idetect_1(j)=exist(filename,'file')==2;
2637                filecell.imaA2.civ1(ifile,j)={filename};%second image
2638            end
2639            [idetectmin,indexj]=min(idetect);
2640            if idetectmin==0,
2641                msgbox_uvmat('ERROR',[filecell.imaA1.civ1{ifile,indexj} ' not found'])
2642                filecell={};
2643                cd(currentdir)
2644                return
2645            end
2646            [idetectmin,indexj]=min(idetect_1);
2647            if idetectmin==0,
2648                msgbox_uvmat('ERROR',[filecell.imaA2.civ1{ifile,indexj} ' not found'])
2649                filecell={};
2650                cd(currentdir)
2651                return
2652            end
2653        end
2654    end
2655   
2656    %%%%%%%%%%%%%  fix1 or patch1 activated but no civ1   %%%%%%%%%%%%%
2657elseif (box_test(2)==1 || box_test(3)==1);
2658    for ifile=1:nbfield
2659        for j=1:nbslice
2660            filename=name_generator(filebase_nc,num1_civ1(ifile),num_a_civ1(j),'.nc',...
2661                nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
2662            detect=exist(filename,'file')==2;
2663            if detect==0
2664                msgbox_uvmat('ERROR',[filename ' not found'])
2665                filecell={};
2666                cd(currentdir)
2667                return
2668            end
2669            filecell.nc.civ1(ifile,j)={filename};
2670        end
2671    end
2672    if strcmp(compare,'stereo PIV')
2673        for ifile=1:nbfield
2674            for j=1:nbslice
2675                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);%
2676                filecell.ncA.civ1(ifile,j)={filename};
2677                if ~exist(filename,'file')
2678                    msgbox_uvmat('ERROR',['input file ' filename ' not found'])
2679                    set(handles.RUN, 'Enable','On')
2680                    set(handles.RUN,'BackgroundColor',[1 0 0])
2681                    filecell={};
2682                    cd(currentdir)
2683                    return
2684                end
2685            end
2686        end
2687    end
2688end
2689
2690%%%%%%%%%%%%%  if civ2 performed with pairs different than civ1  %%%%%%%%%%%%%
2691testdiff=0;
2692if (box_test(4)==1)&&...
2693        ((get(handles.list_pair_civ1,'Value')~=get(handles.list_pair_civ2,'Value'))||~strcmp(subdir_civ2,subdir_civ1))
2694    testdiff=1;
2695    detect=1;
2696    vers=0;
2697    subdir_civ2_new=subdir_civ2;
2698    while detect==1 %create a new subdir if the netcdf files already exist
2699        for ifile=1:nbfield
2700            for j=1:nbslice
2701                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);%
2702                detect=exist(filename,'file')==2;
2703                if detect% if a netcdf file already exists
2704                    indstr=regexp(subdir_civ2,'\D');
2705                    if indstr(end)<length(subdir_civ2) %subdir_civ1 ends by a number
2706                        vers=str2double(subdir_civ2(indstr(end)+1:end))+1;
2707                        subdir_civ2_new=[subdir_civ2(1:indstr(end)) num2str(vers)];
2708                    else
2709                        vers=vers+1;
2710                        subdir_civ2_new=[subdir_civ1 '_' num2str(vers)];
2711                    end
2712                    break
2713                end
2714                filecell.nc.civ2(ifile,j)={filename};
2715            end
2716            if detect% if a netcdf file already exists
2717                break
2718            end
2719        end
2720        %create the new subdir_civ2_new
2721        if ~exist(fullfile(Path_ima,subdir_civ2_new),'dir')
2722            [xx,m2]=mkdir(fullfile(Path_ima,subdir_civ2_new));
2723            [xx,msg2] = fileattrib(subdir_civ2_new,'+w','g'); %yield writing access (+w) to user group (g)
2724            if ~isequal(m2,'')
2725                msgbox_uvmat('ERROR',['cannot create ' subdir_civ2_new ': ' m2])
2726                filecell={};
2727                cd(currentdir)
2728                return
2729            end
2730        end
2731        if strcmp(compare,'stereo PIV')%check second nc series
2732            for ifile=1:nbfield
2733                for j=1:nbslice
2734                    filename=name_generator(filebase_A,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2735                        nom_type_nc,1,num2_civ2(ifile),num_b_civ1(j),subdir_civ2_new);%
2736                    detect=exist(filename,'file')==2;
2737                    if detect% if a netcdf file already exists
2738                        indstr=regexp(subdir_civ2,'\D');
2739                        if indstr(end)<length(subdir_civ2) %subdir_civ1 ends by a number
2740                           vers=str2double(subdir_civ2(indstr(end)+1:end))+1;
2741                           subdir_civ2_new=[subdir_civ2(1:indstr(end)) num2str(vers)];
2742                        else
2743                           vers=vers+1;
2744                           subdir_civ2_new=[subdir_civ1 '_' num2str(vers)];
2745                        end
2746                        break
2747                    end
2748                    filecell.ncA.civ2(ifile,j)={filename};
2749                end
2750                if detect% if a netcdf file already exists
2751                    break
2752                end
2753            end
2754            subdir_civ2=subdir_civ2_new;
2755            %create the new subdir_civ1
2756            if ~exist(fullfile(Path_ima,subdir_civ2_new),'dir')
2757                [xx,m2]=mkdir(subdir_civ2_new);
2758                 [xx,msg2] = fileattrib(subdir_civ2_new,'+w','g'); %yield writing access (+w) to user group (g)
2759                if ~isequal(m2,'')
2760                    msgbox_uvmat('ERROR', ['cannot create ' subdir_civ2_new ': ' m2])%error message for directory creation
2761                    cd(currentdir)
2762                    filecell={};
2763                    return
2764                end
2765            end
2766        end
2767    end
2768    subdir_civ2=subdir_civ2_new;
2769end
2770cd(currentdir);%come back to the current working directory
2771
2772%%%%%%%%%%%%%  if civ2 results are obtained or used  %%%%%%%%%%%%%
2773if box_test(4)==1 || box_test(5)==1 || box_test(6)==1 %civ2
2774    %check source netcdf file of civ1 estimates
2775    if box_test(1)==0; %no civ1 performed
2776        for ifile=1:nbfield
2777            for j=1:nbslice
2778                filename=name_generator(filebase_nc,num1_civ1(ifile),num_a_civ1(j),'.nc',...
2779                    nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
2780                filecell.nc.civ1(ifile,j)={filename};% name of the civ1 file
2781                if ~exist(filename,'file')
2782                    msgbox_uvmat('ERROR',['input file ' filename ' not found'])
2783                    filecell={};
2784                    return
2785                end
2786                if ~testdiff % civ2 or patch2 are written in the same file as civ1
2787                    if box_test(4)==0 ; %check the existence of civ2 if it is not calculated
2788                        Data=nc2struct(filename,'ListGlobalAttribute','civ2');
2789                        if isempty(Data.civ2)||isequal(Data.civ2,0)
2790                            msgbox_uvmat('ERROR',['no civ2 data in ' filename])
2791                            filecell=[];
2792                            return
2793                        end
2794                    elseif box_test(3)==0; %check the existence of patch if it is not calculated
2795                        Data=nc2struct(filename,'ListGlobalAttribute','patch');
2796                        if isempty(Data.patch)||isequal(Data.patch,0)
2797                            msgbox_uvmat('ERROR',['no patch data in ' filename])
2798                            filecell=[];
2799                            return
2800                        end
2801                    end
2802                end
2803            end
2804        end
2805        if strcmp(compare,'stereo PIV')
2806            for ifile=1:nbfield
2807                for j=1:nbslice
2808                    filename=name_generator(filebase_A,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2809                        nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
2810                    filecell.ncA.civ2(ifile,j)={filename};
2811                    if ~exist(filename,'file')
2812                        msgbox_uvmat('ERROR',['input file ' filename ' not found'])
2813                        set(handles.RUN, 'Enable','On')
2814                        set(handles.RUN,'BackgroundColor',[1 0 0])
2815                        return
2816                    end
2817                end
2818            end
2819        end
2820    end
2821   
2822    detect=1;
2823    %     while detect==1%creates a new subdir if the netcdf files already contain civ2 data
2824    for ifile=1:nbfield
2825        for j=1:nbslice
2826            filename=name_generator(filebase_nc,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2827                nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);
2828            detect=exist(filename,'file')==2;
2829            filecell.nc.civ2(ifile,j)={filename};
2830        end
2831    end
2832    %get first image names for civ2
2833    if box_test(1)==1 & isequal(num1_civ1,num1_civ2) & isequal(num_a_civ1,num_a_civ2)
2834        filecell.ima1.civ2=filecell.ima1.civ1;
2835    elseif box_test(4)==1
2836        for ifile=1:nbfield
2837            for j=1:nbslice
2838                filename=name_generator(filebase_ima1, num1_civ2(ifile),num_a_civ2(j),ext_ima,nom_type_ima1);
2839                idetect_2(j)=exist(filename,'file')==2;
2840                filecell.ima1.civ2(ifile,j)={filename};%first image
2841            end
2842            [idetectmin,indexj]=min(idetect_2);
2843            if idetectmin==0,
2844                msgbox_uvmat('ERROR',['input image ' filecell.ima1.civ2{ifile,indexj} ' not found'])
2845                filecell=[];
2846                return
2847            end
2848        end
2849    end
2850   
2851    %get second image names for civ2
2852    if box_test(1)==1 & isequal(num2_civ1,num2_civ2) & isequal(num_b_civ1,num_b_civ2)
2853        filecell.ima2.civ2=filecell.ima2.civ1;
2854    elseif box_test(4)==1
2855        for ifile=1:nbfield
2856            for j=1:nbslice
2857                filename=name_generator(filebase_ima2, num2_civ2(ifile),num_b_civ2(j),ext_ima,nom_type_ima2);
2858                idetect_3(j)=exist(filename,'file')==2;
2859                filecell.ima2.civ2(ifile,j)={filename};%first image
2860            end
2861            [idetectmin,indexj]=min(idetect_3);
2862            if idetectmin==0,
2863                msgbox_uvmat('ERROR',['input image ' filecell.ima2.civ2{ifile,indexj} ' not found'])
2864                filecell=[];
2865                return
2866            end
2867        end
2868    end
2869end
2870if (box_test(5)==1 || box_test(6)==1 ) && box_test(4)==0  % need to read an existing netcdf civ2 file
2871    if ~testdiff
2872        filecell.nc.civ2=filecell.nc.civ1;% file already checked
2873    else     % check the civ2 files
2874        for ifile=1:nbfield
2875            for j=1:nbslice
2876                filename=name_generator(filebase_nc,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2877                    nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
2878                filecell.nc.civ2(ifile,j)={filename};
2879                if ~exist(filename,'file')
2880                    msgbox_uvmat('ERROR',['input file ' filename ' not found'])
2881                    filecell=[];
2882                    return
2883                else
2884                    Data=nc2struct(filename,'ListGlobalAttribute','civ2');
2885                    if isempty(Data.civ2)||isequal(Data.civ2,0)
2886                        msgbox_uvmat('ERROR',['no civ2 data in ' filename])
2887                        filecell=[];
2888                        return
2889                    end
2890                end
2891            end
2892        end
2893    end
2894end
2895
2896%%%%%%%%%%%%%  if stereo fields are calculated by PATCH %%%%%%%%%%%%%
2897if strcmp(compare,'stereo PIV')
2898    if  box_test(3)==1 && isequal(get(handles.test_stereo1,'Value'),1)
2899        for ifile=1:nbfield
2900            for j=1:nbslice
2901                filename=name_generator(filebase_AB,num1_civ1(ifile),num_a_civ1(j),'.nc',...
2902                    nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
2903                filecell.st(ifile,j)={filename};
2904            end
2905        end
2906    end
2907    if  box_test(6)==1 && isequal(get(handles.test_stereo2,'Value'),1)
2908        for ifile=1:nbfield
2909            for j=1:nbslice
2910                filename=name_generator(filebase_AB,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2911                    nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
2912                filecell.st(ifile,j)={filename};
2913            end
2914        end
2915    end
2916end
2917set(handles.subdir_civ1,'String',subdir_civ1);%update the edit box
2918set(handles.subdir_civ2,'String',subdir_civ2);%update the edit box
2919browse.nom_type_nc=nom_type_nc;
2920set(handles.browse_root,'UserData',browse); %update the nomenclature type for uvmat
2921
2922
2923%COPY IMAGES TO THE FORMAT .png IF NEEDED
2924if isequal(nom_type_ima1,'*')%case of movie files
2925    nom_type_imanew1='_i';
2926else
2927    nom_type_imanew1=nom_type_ima1;
2928end
2929if isequal(nom_type_ima2,'*')%case of movie files
2930    nom_type_imanew2='_i';
2931else
2932    nom_type_imanew2=nom_type_ima2;
2933end
2934if ~isequal(ext_ima,'.png')
2935    %%type of image file
2936    type_ima1='none';%default
2937    movieobject1=[];%default
2938    if strcmpi(ext_ima,'.avi')
2939        hhh=which('mmreader');
2940        if ~isequal(hhh,'')&& mmreader.isPlatformSupported()% if the mmreader function is found (recent version of matlab)
2941            type_ima1='movie';
2942            movieobject1=mmreader([filebase_ima2 ext_ima]);
2943        else
2944            type_ima1='avi';
2945        end
2946    elseif ischar(ext_ima) && ~isempty(ext_ima(2:end))
2947        form=imformats(ext_ima(2:end));
2948        if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
2949            if isequal(nom_type_ima1,'*');
2950                type_ima1='multimage';%image series in a single image file
2951            else
2952                type_ima1='image';
2953            end
2954        end
2955    end
2956    type_ima2='none';%default
2957    movieobject2=[];
2958    if strcmpi(ext_ima,'.avi')
2959        hhh=which('mmreader');
2960        if ~isequal(hhh,'')&& mmreader.isPlatformSupported()% if the mmreader function is found (recent version of matlab)
2961            type_ima2='movie';
2962            movieobject2=mmreader([filebase_ima2 ext_ima]);
2963        else
2964            type_ima2='avi';
2965        end
2966    elseif ischar(ext_ima) && ~isempty(ext_ima(2:end))
2967        form=imformats(ext_ima(2:end));
2968        if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
2969            if isequal(nom_type_ima1,'*');
2970                type_ima2='multimage';%image series in a single image file
2971            else
2972                type_ima2='image';
2973            end
2974        end
2975    end
2976    %npxy=get(handles.ImaExt,'UserData');
2977    % %     if numel(npxy)<2
2978    %
2979    %         filename=name_generator(filebase_ima1,num1_civ1(1),num_a_civ1(1),ImaExt,nom_type_ima1);
2980    %         A=imread(filename);
2981    %         npxy=size(A);
2982    % %     end
2983    %     npy=npxy(1);
2984    %     npx=npxy(2);
2985    if box_test(1)==1 %if civ1 is performed
2986        h = waitbar(0,['copy images to the .png format for civ1']);% display a wait bar
2987        for ifile=1:nbfield
2988            waitbar(ifile/nbfield);
2989            for j=1:nbslice
2990                filename=name_generator(filebase_ima1,num1_civ1(ifile),num_a_civ1(j),'.png',nom_type_imanew1);
2991                if ~exist(filename,'file')
2992                    A=read_image(filecell.ima1.civ1{ifile,j},type_ima1,num1_civ1(ifile),movieobject1);
2993                    imwrite(A,filename,'BitDepth',16);
2994                end
2995                filecell.ima1.civ1(ifile,j)={filename};
2996                filename=name_generator(filebase_ima2, num2_civ1(ifile),num_b_civ1(j),'.png',nom_type_imanew2);
2997                if ~exist(filename,'file')
2998                    A=read_image(filecell.ima2.civ1{ifile,j},type_ima2,num2_civ1(ifile),movieobject2);
2999                    imwrite(A,filename,'BitDepth',16);
3000                end
3001                filecell.ima2.civ1(ifile,j)={filename};
3002            end
3003        end
3004        close(h)
3005    end
3006    if box_test(4)==1 %if civ2 is performed
3007        h = waitbar(0,['copy images to the .png format for civ2']);% display a wait bar
3008        for ifile=1:nbfield
3009            waitbar(ifile/nbfield);
3010            for j=1:nbslice
3011                filename=name_generator(filebase_ima1,num1_civ2(ifile),num_a_civ2(j),'.png',nom_type_imanew1);
3012                if ~exist(filename,'file')
3013                    A=read_image(cell2mat(filecell.ima1.civ2(ifile,j)),type_ima2,num1_civ2(ifile));
3014                    imwrite(A,filename,'BitDepth',16);
3015                end
3016                filecell.ima1.civ2(ifile,j)={filename};
3017                filename=name_generator(filebase_ima2, num2_civ2(ifile),num_b_civ2(j),'.png',nom_type_imanew2);
3018                if ~exist(filename,'file')
3019                    A=read_image(cell2mat(filecell.ima2.civ2(ifile,j)),type_ima2,num2_civ2(ifile));
3020                    imwrite(A,filename,'BitDepth',16);
3021                end
3022                filecell.ima2.civ2(ifile,j)={filename};
3023            end
3024        end
3025        close(h);
3026    end
3027end
3028
3029%------------------------------------------------------------------------
3030% --- PATCH
3031function cmd_PATCH=PATCH_CMD(filename_nc,nx_patch,ny_patch,rho_patch,subdomain_patch,thresh_value,test_interp,PatchBin)
3032%------------------------------------------------------------------------
3033namelog=[filename_nc([1:end-3]) '_patch.log'];
3034if test_interp==0
3035    if isunix
3036    cmd_PATCH=[PatchBin ' -f ' filename_nc ' -m ' nx_patch  ' -n ' ny_patch ' -ro ' rho_patch ' -nopt ' subdomain_patch ...
3037        '  > ' namelog ' 2>&1']; % redirect standard output to the log file
3038    else
3039      cmd_PATCH=['"' PatchBin '" -f "' filename_nc '" -m ' nx_patch  ' -n ' ny_patch ' -ro ' rho_patch ' -nopt ' subdomain_patch ...
3040        '  > "' namelog '" 2>&1']; % redirect standard output to the log file
3041    end
3042else %nouveau programme patch
3043    cmd_PATCH=[PatchBin ' -f ' filename_nc ' -m ' nx_patch  ' -n ' ny_patch ' -ro ' rho_patch ...
3044        ' -max ' thresh_value ' -nopt ' subdomain_patch  '  > ' namelog ' 2>&1']; % redirect standard output to the log file
3045end
3046cmd_PATCH=regexprep(cmd_PATCH,'\\','\\\\');
3047%------------------------------------------------------------------------
3048% --- STEREO Interp
3049function cmd=RUN_STINTERP(stinterpBin,filename_A_nc,filename_B_nc,filename_nc,nx_patch,ny_patch,rho_patch,subdomain_patch,thresh_value,xmlA,xmlB)
3050%------------------------------------------------------------------------
3051namelog=[filename_nc([1:end-3]) '_stinterp.log'];
3052cmd=[stinterpBin ' -f1 ' filename_A_nc  ' -f2 ' filename_B_nc ' -f  ' filename_nc ...
3053    ' -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
3054
3055%------------------------------------------------------------------------
3056% --- Executes on button press in CIV1.
3057function CIV1_Callback(hObject, eventdata, handles)
3058%------------------------------------------------------------------------
3059val=get(handles.CIV1,'Value');
3060if isequal(val,1)
3061    enable_civ1(handles,'on')
3062    enable_pair1(handles,'on')
3063else
3064    enable_civ1(handles,'off')
3065end
3066find_netcpair_civ1(hObject, eventdata, handles);
3067
3068%------------------------------------------------------------------------
3069% --- Executes on button press in FIX1.
3070function FIX1_Callback(hObject, eventdata, handles)
3071%------------------------------------------------------------------------
3072enable_fix1(handles,get(handles.FIX1,'Value'))
3073
3074%------------------------------------------------------------------------
3075% --- Executes on button press in PATCH1.
3076function PATCH1_Callback(hObject, eventdata, handles)
3077%------------------------------------------------------------------------
3078if get(handles.PATCH1,'Value')==1
3079    enable_patch1(handles)
3080else
3081    desable_patch1(handles)
3082end
3083
3084%------------------------------------------------------------------------
3085% --- Executes on button press in CIV2.
3086function CIV2_Callback(hObject, eventdata, handles)
3087%------------------------------------------------------------------------
3088state=get(handles.CIV2,'Value');
3089enable_civ2(handles,state)
3090if state
3091    find_netcpair_civ2(hObject, eventdata, handles)
3092    enable_pair1(handles,'on')
3093end
3094
3095%------------------------------------------------------------------------
3096% --- Executes on button press in FIX2.
3097function FIX2_Callback(hObject, eventdata, handles)
3098%------------------------------------------------------------------------
3099if get(handles.FIX2,'Value')==1
3100    enable_fix2(handles)
3101    if get(handles.CIV2,'Value')==0
3102        find_netcpair_civ2(hObject, eventdata, handles) % select the available netcdf files
3103    end
3104else
3105    desable_fix2(handles)
3106end
3107
3108%------------------------------------------------------------------------
3109% --- Executes on button press in PATCH2.
3110function PATCH2_Callback(hObject, eventdata, handles)
3111%------------------------------------------------------------------------
3112if get(handles.PATCH2,'Value')==1
3113    enable_patch2(handles)
3114    if get(handles.CIV2,'Value')==0
3115        find_netcpair_civ2(hObject, eventdata, handles) % select the available netcdf files
3116    end
3117else
3118    desable_patch2(handles)
3119end
3120
3121%------------------------------------------------------------------------
3122function first_i_Callback(hObject, eventdata, handles)
3123%------------------------------------------------------------------------
3124% last_i_Callback(hObject, eventdata, handles)
3125first_i=str2double(get(handles.first_i,'String'));
3126% last_i=str2num(get(handles.last_i,'String'));
3127% ref_i=ceil((first_i+last_i)/2);
3128set(handles.ref_i,'String', num2str(first_i))% reference index for pair dt = first index
3129set(handles.ref_i_civ2,'String', num2str(first_i))% reference index for pair dt = first index
3130ref_i_Callback(hObject, eventdata, handles)%refresh dispaly of dt for pairs (in case of non constant dt)
3131
3132%------------------------------------------------------------------------
3133function first_j_Callback(hObject, eventdata, handles)
3134%------------------------------------------------------------------------
3135first_j=str2num(get(handles.first_j,'String'));
3136set(handles.ref_j,'String', num2str(first_j))% reference index for pair dt = first index
3137ref_j_Callback(hObject, eventdata, handles)%refresh dispaly of dt for pairs (in case of non constant dt)
3138
3139%------------------------------------------------------------------------
3140% --- Executes on button press in calcul_search: determine the search range isx,isy
3141function calcul_search_Callback(hObject, eventdata, handles)
3142%------------------------------------------------------------------------
3143%determine pair numbers
3144list_pair=get(handles.list_pair_civ1,'String');%get the menu of image pairs
3145index=get(handles.list_pair_civ1,'Value');
3146displ_num=get(handles.list_pair_civ1,'UserData');
3147time=get(handles.RootName,'UserData'); %get the set of times
3148pxcm_xy=get(handles.calcul_search,'UserData');
3149pxcmx=pxcm_xy(1);
3150pxcmy=pxcm_xy(2);
3151mode_list=get(handles.mode,'String');
3152mode_value=get(handles.mode,'Value');
3153mode=mode_list{mode_value};
3154if isequal (mode, 'series(Di)' )
3155    ref_i=str2double(get(handles.ref_i,'String'));
3156    num1=ref_i-floor(index/2);%  first image numbers
3157    num2=ref_i+ceil(index/2);
3158    num_a=1;
3159    num_b=1;
3160elseif isequal (mode, 'series(Dj)')
3161    num1=1;
3162    num2=1;
3163    ref_j=str2double(get(handles.ref_j,'String'));
3164    num_a=ref_j-floor(index/2);%  first image numbers
3165    num_b=ref_j+ceil(index/2);
3166elseif isequal(mode,'pair j1-j2') %case of bursts (png_old or png_2D)
3167    ref_i=str2double(get(handles.ref_i,'String'));
3168    num1=ref_i;
3169    num2=ref_i;
3170    num_a=displ_num(1,index);
3171    num_b=displ_num(2,index);
3172end
3173dt=time(num2,num_b)-time(num1,num_a);
3174ibx=str2double(get(handles.ibx,'String'));
3175iby=str2double(get(handles.iby,'String'));
3176umin=dt*pxcmx*str2double(get(handles.umin,'String'));
3177umax=dt*pxcmx*str2double(get(handles.umax,'String'));
3178vmin=dt*pxcmy*str2double(get(handles.vmin,'String'));
3179vmax=dt*pxcmy*str2double(get(handles.vmax,'String'));
3180shiftx=round((umin+umax)/2);
3181shifty=round((vmin+vmax)/2);
3182isx=(umax+2-shiftx)*2+ibx;
3183isx=2*ceil(isx/2)+1;
3184isy=(vmax+2-shifty)*2+iby;
3185isy=2*ceil(isy/2)+1;
3186set(handles.shiftx,'String',num2str(shiftx));
3187set(handles.shifty,'String',num2str(shifty));
3188set(handles.isx,'String',num2str(isx));
3189set(handles.isy,'String',num2str(isy));
3190
3191%------------------------------------------------------------------------
3192% --- Executes on carriage return on the subdir civ1 edit window
3193function subdir_civ1_Callback(hObject, eventdata, handles)
3194%------------------------------------------------------------------------
3195subdir=get(handles.subdir_civ1,'String');
3196set(handles.subdir_civ2,'String',subdir);
3197if get(handles.CIV1,'Value')==0
3198    find_netcpair_civ1(hObject, eventdata, handles); %update the list of available pairs from netcdf files in the new directory
3199end
3200
3201%------------------------------------------------------------------------
3202% --- Executes on carriage return on the subdir civ1 edit window
3203function subdir_civ2_Callback(hObject, eventdata, handles)
3204%------------------------------------------------------------------------
3205%update the list of available pairs from netcdf files in the new directory
3206if get(handles.CIV2,'Value')==0 & get(handles.CIV1,'Value')==0 & get(handles.FIX1,'Value')==0 & get(handles.PATCH1,'Value')==0
3207    find_netcpair_civ2(hObject, eventdata, handles);
3208end
3209
3210%------------------------------------------------------------------------
3211% --- Executes on button press in get_mask_civ1: select box for mask option
3212function get_mask_civ1_Callback(hObject, eventdata, handles)
3213%------------------------------------------------------------------------
3214maskval=get(handles.get_mask_civ1,'Value');
3215if isequal(maskval,0)
3216    set(handles.mask_civ1,'String','')
3217else
3218    mask_displ='no mask'; %default
3219    filebase=get(handles.RootName,'String');
3220    [ nbslice_mask, flag_mask]=get_mask(filebase,handles);
3221    if isequal(flag_mask,1)
3222        mask_displ=[num2str(nbslice_mask) 'mask'];
3223    elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3224        common_path=fileparts(filebase);
3225        filebase_a=fullfile(common_path,get(handles.RootName_1,'String'));
3226        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3227        if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice_mask)
3228            mask_displ='no mask';
3229        end
3230    end
3231    if isequal(mask_displ,'no mask')
3232        [FileName, PathName, filterindex] = uigetfile( ...
3233            {'*.png', ' (*.png)';
3234            '*.png',  '.png files '; ...
3235            '*.*', 'All Files (*.*)'}, ...
3236            'Pick a mask file *.png',filebase);
3237        mask_displ=fullfile(PathName,FileName);
3238        if ~exist(mask_displ,'file')
3239            mask_displ='no mask';
3240        end
3241    end
3242    if isequal(mask_displ,'no mask')
3243        set(handles.get_mask_civ1,'Value',0)
3244        set(handles.get_mask_fix1,'Value',0)
3245        set(handles.get_mask_civ2,'Value',0)
3246        set(handles.get_mask_fix2,'Value',0)
3247    else
3248        set(handles.get_mask_fix1,'Value',1)
3249        set(handles.get_mask_fix2,'Value',1)
3250    end
3251    set(handles.mask_civ1,'String',mask_displ)
3252    set(handles.mask_fix1,'String',mask_displ)
3253    set(handles.mask_civ2,'String',mask_displ)
3254    set(handles.mask_fix2,'String',mask_displ)
3255end
3256set(handles.get_mask_civ2,'Value',maskval)%update the civ2 mask with the same option as civ1
3257
3258%------------------------------------------------------------------------
3259% --- Executes on button press in get_mask_fix1.
3260function get_mask_fix1_Callback(hObject, eventdata, handles)
3261%------------------------------------------------------------------------
3262maskval=get(handles.get_mask_fix1,'Value');
3263if isequal(maskval,0)
3264    set(handles.mask_fix1,'String','')
3265else
3266    mask_displ='no mask'; %default
3267    filebase=get(handles.RootName,'String');
3268    [nbslice, flag_mask]=get_mask(filebase,handles);
3269    if isequal(flag_mask,1)
3270        mask_displ=[num2str(nbslice) 'mask'];
3271    elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3272        filebase_a=get(handles.RootName_1,'String');
3273        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3274        if isequal(flag_mask_a,0) | ~isequal(nbslice_a,nbslice)
3275            mask_displ='no mask';
3276        end
3277    end
3278    if isequal(mask_displ,'no mask')
3279        [FileName, PathName, filterindex] = uigetfile( ...
3280            {'*.png', ' (*.png)';
3281            '*.png',  '.png files '; ...
3282            '*.*', 'All Files (*.*)'}, ...
3283            'Pick a mask file *.png',filebase);
3284        mask_displ=fullfile(PathName,FileName);
3285        if ~exist(mask_displ,'file')
3286            mask_displ='no mask';
3287        end
3288    end
3289    if isequal(mask_displ,'no mask')
3290        set(handles.get_mask_fix1,'Value',0)
3291        set(handles.get_mask_civ2,'Value',0)
3292        set(handles.get_mask_fix2,'Value',0)
3293    else
3294        %set(handles.get_mask_civ2,'Value',1)
3295        set(handles.get_mask_fix2,'Value',1)
3296    end
3297    set(handles.mask_fix1,'String',mask_displ)
3298    set(handles.mask_civ2,'String',mask_displ)
3299    set(handles.mask_fix2,'String',mask_displ)
3300end
3301
3302%------------------------------------------------------------------------
3303% --- Executes on button press in get_mask_civ2: select box for mask option
3304function get_mask_civ2_Callback(hObject, eventdata, handles)
3305%------------------------------------------------------------------------
3306maskval=get(handles.get_mask_civ2,'Value');
3307if isequal(maskval,0)
3308    set(handles.mask_civ2,'String','')
3309else
3310    mask_displ='no mask'; %default
3311    filebase=get(handles.RootName,'String');
3312    [nbslice, flag_mask]=get_mask(filebase,handles);
3313    if isequal(flag_mask,1)
3314        mask_displ=[num2str(nbslice) 'mask'];
3315    elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3316        filebase_a=get(handles.RootName_1,'String');
3317        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3318        if isequal(flag_mask_a,0) | ~isequal(nbslice_a,nbslice)
3319            mask_displ='no mask';
3320        end
3321    end
3322    if isequal(mask_displ,'no mask')
3323        [FileName, PathName, filterindex] = uigetfile( ...
3324            {'*.png', ' (*.png)';
3325            '*.png',  '.png files '; ...
3326            '*.*', 'All Files (*.*)'}, ...
3327            'Pick a mask file *.png',filebase);
3328        mask_displ=fullfile(PathName,FileName);
3329        if ~exist(mask_displ,'file')
3330            mask_displ='no mask';
3331        end
3332    end
3333    if isequal(mask_displ,'no mask')
3334        set(handles.get_mask_civ2,'Value',0)
3335        set(handles.get_mask_fix2,'Value',0)
3336    else
3337        set(handles.get_mask_fix2,'Value',1)
3338    end
3339    set(handles.mask_civ2,'String',mask_displ)
3340    set(handles.mask_fix2,'String',mask_displ)
3341end
3342
3343%------------------------------------------------------------------------
3344% --- Executes on button press in get_mask_fix2.
3345function get_mask_fix2_Callback(hObject, eventdata, handles)
3346%------------------------------------------------------------------------
3347maskval=get(handles.get_mask_fix2,'Value');
3348if isequal(maskval,0)
3349    set(handles.mask_fix2,'String','')
3350else
3351    mask_displ='no mask'; %default
3352    filebase=get(handles.RootName,'String');
3353    [nbslice, flag_mask]=get_mask(filebase,handles);
3354    if isequal(flag_mask,1)
3355        mask_displ=[num2str(nbslice) 'mask'];
3356    elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3357        filebase_a=get(handles.RootName_1,'String');
3358        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3359        if isequal(flag_mask_a,0) | ~isequal(nbslice_a,nbslice)
3360            mask_displ='no mask';
3361        end
3362    end
3363    if isequal(mask_displ,'no mask')
3364        [FileName, PathName, filterindex] = uigetfile( ...
3365            {'*.png', ' (*.png)';
3366            '*.png',  '.png files '; ...
3367            '*.*', 'All Files (*.*)'}, ...
3368            'Pick a mask file *.png',filebase);
3369        mask_displ=fullfile(PathName,FileName);
3370        if ~exist(mask_displ,'file')
3371            mask_displ='no mask';
3372        end
3373    end
3374    if isequal(mask_displ,'no mask')
3375        set(handles.get_mask_fix2,'Value',0)
3376    end
3377    set(handles.mask_fix2,'String',mask_displ)
3378end
3379
3380%------------------------------------------------------------------------
3381% --- function called to look for mask files
3382function [nbslice, flag_mask]=get_mask(filebase,handles)
3383%------------------------------------------------------------------------
3384%detect mask files, images with appropriate file base
3385%[filebase '_' xx 'mask'], xx=nbslice
3386%flag_mask=1 indicates detection
3387
3388flag_mask=0;%default
3389nbslice=1;
3390
3391% subdir=get(handles.subdir_civ1,'String');
3392[Path,Name]=fileparts(filebase);
3393if ~isdir(Path)
3394    msgbox_uvmat('ERROR','no path for input files')
3395    return
3396end
3397currentdir=pwd;
3398cd(Path);%move in the dir of the root name filebase
3399maskfiles=dir([Name '_*mask_*.png']);%look for mask files
3400cd(currentdir);%come back to the current working directory
3401if ~isempty(maskfiles)
3402    %     msgbox_uvmat('ERROR','no mask available, to create it use Tools/Make mask in the upper menu bar of uvmat')
3403    % else
3404    flag_mask=1;
3405    maskname=maskfiles(1).name;% take the first mask file in the list
3406    [Path2,Name,ext]=fileparts(maskname);
3407    Namedouble=double(Name);
3408    val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
3409    ind_mask=findstr('mask',Name);
3410    i=ind_mask-1;
3411    while val(i)==0 && i>0
3412        i=i-1;
3413    end
3414    nbslice=str2double(Name(i+1:ind_mask-1));
3415    if ~isnan(nbslice) && Name(i)=='_'
3416        flag_mask=1;
3417    else
3418        msgbox_uvmat('ERROR',['bad mask file ' Name ext ' found in ' Path2])
3419        return
3420        nbslice=1;
3421    end
3422end
3423
3424%------------------------------------------------------------------------
3425% --- function called to look for grid files
3426function [nbslice, flag_mask]=get_grid(filebase,handles)
3427%------------------------------------------------------------------------
3428flag_mask=0;%default
3429nbslice=1;
3430[Path,Name]=fileparts(filebase);
3431currentdir=pwd;
3432cd(Path);%move in the dir of the root name filebase
3433maskfiles=dir([Name '_*grid_*.grid']);%look for mask files
3434cd(currentdir);%come back to the current working directory
3435if ~isempty(maskfiles)
3436    flag_mask=1;
3437    maskname=maskfiles(1).name;% take the first mask file in the list
3438    [Path2,Name,ext]=fileparts(maskname);
3439    Namedouble=double(Name);
3440    val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
3441    ind_mask=findstr('grid',Name);
3442    i=ind_mask-1;
3443    while val(i)==0 && i>0
3444        i=i-1;
3445    end
3446    nbslice=str2double(Name(i+1:ind_mask-1));
3447    if ~isnan(nbslice) && Name(i)=='_'
3448        flag_mask=1;
3449    else
3450        msgbox_uvmat('ERROR',['bad grid file ' Name ext ' found in ' Path2])
3451        return
3452        nbslice=1;
3453    end
3454end
3455
3456%------------------------------------------------------------------------
3457% --- transform numbers to letters
3458function str=num2stra(num,nom_type)
3459%------------------------------------------------------------------------
3460if isempty(nom_type)
3461    str='';
3462elseif strcmp(nom_type(end),'a')
3463    str=char(96+num);
3464elseif strcmp(nom_type(end),'A')
3465    str=char(96+num);
3466elseif isempty(nom_type(2:end))%a single index
3467    str='';
3468else
3469    str=num2str(num);
3470end
3471
3472% %------------------------------------------------------------------------
3473% function mask_civ1_Callback(hObject, eventdata, handles)
3474% %------------------------------------------------------------------------
3475% set(handles.mask_civ1,'UserData',[])
3476% set(handles.mask_civ1,'String','')
3477%
3478% %------------------------------------------------------------------------
3479% function mask_civ2_Callback(hObject, eventdata, handles)
3480% %------------------------------------------------------------------------
3481% set(handles.mask_civ2,'UserData',[])
3482% set(handles.mask_civ2,'String','')
3483%
3484% %------------------------------------------------------------------------
3485% function mask_fix1_Callback(hObject, eventdata, handles)
3486% %------------------------------------------------------------------------
3487% set(handles.mask_fix1,'UserData',[])
3488% set(handles.mask_fix1,'String','')
3489%
3490% %------------------------------------------------------------------------
3491% function mask_fix2_Callback(hObject, eventdata, handles)
3492% %------------------------------------------------------------------------
3493% set(handles.mask_fix2,'UserData',[])
3494% set(handles.mask_fix2,'String','')
3495
3496%------------------------------------------------------------------------
3497% --- Executes on button press in list_subdir_civ1.
3498function list_subdir_civ1_Callback(hObject, eventdata, handles)
3499%------------------------------------------------------------------------
3500list_subdir_civ1=get(handles.list_subdir_civ1,'String');
3501val=get(handles.list_subdir_civ1,'Value');
3502if val>1
3503    subdir=list_subdir_civ1{val};
3504    set(handles.subdir_civ1,'String',subdir);
3505    set(handles.list_subdir_civ1,'Value',1);
3506end
3507
3508%------------------------------------------------------------------------
3509% --- Executes on button press in list_subdir_civ2.
3510function list_subdir_civ2_Callback(hObject, eventdata, handles)
3511%------------------------------------------------------------------------
3512list_subdir_civ2=get(handles.list_subdir_civ2,'String');
3513val=get(handles.list_subdir_civ2,'Value');
3514if val>1
3515    subdir=list_subdir_civ2{val};
3516    set(handles.subdir_civ2,'String',subdir);
3517    set(handles.list_subdir_civ2,'Value',1);
3518end
3519
3520%------------------------------------------------------------------------
3521% --- Executes on button press in browse_gridciv1.
3522function browse_gridciv1_Callback(hObject, eventdata, handles)
3523%------------------------------------------------------------------------
3524value=get(handles.browse_gridciv1,'Value');
3525testgrid=0;
3526if value
3527    filebase=get(handles.RootName,'String');
3528    [nbslice, flag_grid]=get_grid(filebase,handles);
3529    if isequal(flag_grid,1)
3530        filegrid=[num2str(nbslice) 'grid'];
3531        testgrid=1;
3532    else
3533        [FileName, PathName, filterindex] = uigetfile( ...
3534            {'*.grid', ' (*.grid)';
3535            '*.grid',  '.grid files '; ...
3536            '*.*', 'All Files (*.*)'}, ...
3537            'Pick a file',filebase);
3538        filegrid=fullfile(PathName,FileName);
3539        if ~(isempty(FileName)||isempty(PathName)||isequal(FileName,0)||~exist(filegrid,'file'))
3540            testgrid=1;
3541        end
3542    end
3543end
3544if testgrid
3545    set(handles.browse_gridciv2,'Value',1)
3546    set(handles.get_gridpatch1,'Value',1)
3547    set(handles.get_gridpatch2,'Value',1)
3548    set(handles.dx_civ1,'Visible','off');
3549    set(handles.dy_civ1,'Visible','off');
3550    set(handles.dx_civ2,'Visible','off');
3551    set(handles.dy_civ2,'Visible','off');
3552    set(handles.grid_civ1,'String',filegrid)
3553    set(handles.grid_patch1,'String',filegrid)
3554    set(handles.grid_civ2,'String',filegrid)
3555    set(handles.grid_patch2,'String',filegrid)
3556else
3557    set(handles.browse_gridciv1,'Value',0);
3558    set(handles.browse_gridciv2,'Value',0);
3559    set(handles.get_gridpatch1,'Value',0)
3560    set(handles.get_gridpatch2,'Value',0)
3561    set(handles.dx_civ1,'Visible','on');
3562    set(handles.dy_civ1,'Visible','on');
3563    set(handles.dx_civ2,'Visible','on');
3564    set(handles.dy_civ2,'Visible','on');
3565    set(handles.grid_civ1,'String','')
3566    set(handles.grid_patch1,'String','')
3567    set(handles.grid_civ2,'String','')
3568    set(handles.grid_patch2,'String','')
3569end
3570
3571%------------------------------------------------------------------------
3572% --- Executes on button press in browse_gridciv1.
3573function browse_gridciv2_Callback(hObject, eventdata, handles)
3574%------------------------------------------------------------------------
3575value=get(handles.browse_gridciv2,'Value');
3576if value
3577    filebase=get(handles.RootName,'String');
3578    [nbslice, flag_grid]=get_grid(filebase,handles);
3579    if isequal(flag_grid,1)
3580        mask_displ=[num2str(nbslice) 'grid'];
3581        set(handles.grid_civ2,'String',mask_displ)
3582        set(handles.dx_civ2,'Visible','off');
3583        set(handles.dy_civ2,'Visible','off');
3584    else
3585        [FileName, PathName, filterindex] = uigetfile( ...
3586            {'*.grid', ' (*.grid)';
3587            '*.grid',  '.grid files '; ...
3588            '*.*', 'All Files (*.*)'}, ...
3589            'Pick a file',filebase);
3590        filegrid=fullfile(PathName,FileName);
3591        if isempty(FileName)|isempty(PathName)|isequal(FileName,0)|~exist(filegrid,'file')
3592            set(handles.browse_gridciv2,'Value',0);
3593            set(handles.grid_civ2,'string','');
3594            set(handles.dx_civ2,'Visible','on');
3595            set(handles.dy_civ2,'Visible','on');
3596            set(handles.grid_civ2,'string','');
3597        else
3598            set(handles.grid_civ2,'string',filegrid);
3599            set(handles.dx_civ2,'Visible','off');
3600            set(handles.dy_civ2,'Visible','off');
3601            set(handles.grid_civ2,'string',filegrid);
3602        end
3603    end
3604else
3605    set(handles.grid_civ2,'string','');
3606    set(handles.dx_civ2,'Visible','on');
3607    set(handles.dy_civ2,'Visible','on');
3608    set(handles.grid_civ2,'string','');
3609end
3610
3611% % --- Executes on button press in browse_gridciv2.
3612% function browse_gridciv2_Callback(hObject, eventdata, handles)
3613%
3614% filebase=get(handles.RootName,'String');
3615% [FileName, PathName, filterindex] = uigetfile( ...
3616%        {'*.grid', ' (*.grid)';
3617%         '*.grid',  '.grid files '; ...
3618%         '*.*', 'All Files (*.*)'}, ...
3619%         'Pick a file',filebase);
3620% filegrid=fullfile(PathName,FileName);
3621% set(handles.grid_civ2,'string',filegrid);
3622% set(handles.dx_civ2,'String',' ');
3623% set(handles.dy_civ2,'String',' ');
3624% % set(handles.grid_patch2,'string',filegrid);
3625
3626% --- Executes on button press in get_gridpatch1.
3627function get_gridpatch1_Callback(hObject, eventdata, handles)
3628% hObject    handle to get_gridpatch1 (see GCBO)
3629% eventdata  reserved - to be defined in a future version of MATLAB
3630% handles    structure with handles and user data (see GUIDATA)
3631
3632filebase=get(handles.RootName,'String');
3633[FileName, PathName, filterindex] = uigetfile( ...
3634    {'*.grid', ' (*.grid)';
3635    '*.grid',  '.grid files '; ...
3636    '*.*', 'All Files (*.*)'}, ...
3637    'Pick a file',filebase);
3638filegrid=fullfile(PathName,FileName);
3639set(handles.grid_patch1,'string',filegrid);
3640% set(handles.grid_patch2,'string',filegrid
3641
3642%------------------------------------------------------------------------
3643% --- Executes on button press in get_gridpatch2.
3644function get_gridpatch2_Callback(hObject, eventdata, handles)
3645%------------------------------------------------------------------------
3646
3647%------------------------------------------------------------------------
3648function enable_civ1(handles,state)
3649%------------------------------------------------------------------------
3650if isequal(state,0)
3651    state='off';
3652end
3653if isequal(state,1)
3654    state='on';
3655end
3656if isequal(state,'on')
3657    set(handles.frame_civ1,'BackgroundColor',[1 1 0])
3658    set(handles.frame_para_civ1,'BackgroundColor',[1 1 0])
3659    set(handles.frame_grid_civ1,'BackgroundColor',[1 1 0])
3660else
3661    set(handles.frame_civ1,'BackgroundColor',[0.831 0.816 0.784])
3662    set(handles.frame_para_civ1,'BackgroundColor',[0.831 0.816 0.784])
3663    set(handles.frame_grid_civ1,'BackgroundColor',[0.831 0.816 0.784])
3664end
3665set(handles.ibx,'Visible',state)
3666set(handles.iby,'Visible',state)
3667set(handles.isx,'Visible',state)
3668set(handles.isy,'Visible',state)
3669set(handles.shiftx,'Visible',state)
3670set(handles.shifty,'Visible',state)
3671set(handles.rho,'Visible',state)
3672set(handles.dx_civ1,'Visible',state)
3673set(handles.dy_civ1,'Visible',state)
3674set(handles.calcul_search,'Visible',state)
3675set(handles.u_text,'Visible',state)
3676set(handles.v_text,'Visible',state)
3677set(handles.min,'Visible',state)
3678set(handles.max,'Visible',state)
3679set(handles.umin,'Visible',state)
3680set(handles.umax,'Visible',state)
3681set(handles.vmin,'Visible',state)
3682set(handles.vmax,'Visible',state)
3683set(handles.grid_civ1,'Visible',state)
3684set(handles.mask_civ1,'Visible',state)
3685set(handles.browse_gridciv1,'Visible',state)
3686set(handles.get_mask_civ1,'Visible',state)
3687set(handles.parameters,'Visible',state)
3688set(handles.grid,'Visible',state)
3689set(handles.dx_civ1,'Visible',state)
3690set(handles.dy_civ1,'Visible',state)
3691set(handles.ImaThreshold,'Visible',state)
3692if isequal(state,'off')
3693    set(handles.MinIma,'Visible','off')
3694    set(handles.MaxIma,'Visible','off')
3695    set(handles.ImaThreshold,'Value',0)
3696end
3697set(handles.dx_civ1_title,'Visible',state)
3698set(handles.dy_civ1_title,'Visible',state)
3699set(handles.ImaThreshold_title,'Visible',state)
3700set(handles.ib_title,'Visible',state)
3701set(handles.is_title,'Visible',state)
3702set(handles.shift_title,'Visible',state)
3703set(handles.rho_title,'Visible',state)
3704
3705%------------------------------------------------------------------------
3706function enable_fix1(handles,state)
3707%------------------------------------------------------------------------
3708if isequal(state,0)
3709    state='off';
3710end
3711if isequal(state,1)
3712    state='on';
3713end
3714if isequal(state,'on')
3715    set(handles.frame_fix1,'BackgroundColor',[1 1 0])
3716else
3717    set(handles.frame_fix1,'BackgroundColor',[0.7 0.7 0.7])
3718end
3719set(handles.REMOVE,'Visible',state)
3720set(handles.vec_Fmin2,'Visible',state)
3721set(handles.vec_F2,'Visible',state)
3722set(handles.vec_F3,'Visible',state)
3723set(handles.thresh_vecC,'Visible',state)
3724set(handles.thresh_vecC_title,'Visible',state)
3725set(handles.thresh_vel,'Visible',state)
3726set(handles.thresh_vel_text,'Visible',state)
3727set(handles.mask_fix1,'Visible',state)
3728set(handles.get_mask_fix1,'Visible',state)
3729set(handles.get_ref_fix1,'Visible',state)
3730set(handles.ref_fix1,'Visible',state)
3731set(handles.inf_sup1,'Visible',state)
3732set(handles.field_ref1,'Visible',state)
3733
3734%------------------------------------------------------------------------
3735function enable_patch1(handles)
3736%------------------------------------------------------------------------
3737global patch_newBin
3738set(handles.frame_patch1,'BackgroundColor',[1 1 0])
3739set(handles.rho_patch1,'Visible','on')
3740set(handles.rho_text1,'Visible','on')
3741set(handles.thresh_patch1,'Visible','on')
3742set(handles.thresh_text1,'Visible','on')
3743set(handles.subdomain_patch1,'Visible','on')
3744set(handles.subdomain_text1,'Visible','on')
3745set(handles.nx_patch1,'Visible','on')
3746set(handles.ny_patch1,'Visible','on')
3747set(handles.nx_patch1_title,'Visible','on')
3748set(handles.ny_patch1_title,'Visible','on')
3749% if ~isempty(patch_newBin)
3750%     set(handles.test_interp,'Visible','on');
3751% end
3752set(handles.get_gridpatch1,'Visible','on')
3753set(handles.grid_patch1,'string','none');
3754set(handles.grid_patch1,'Visible','on')
3755
3756%------------------------------------------------------------------------
3757function desable_patch1(handles)
3758%------------------------------------------------------------------------
3759set(handles.frame_patch1,'BackgroundColor',[0.831 0.816 0.784])
3760set(handles.rho_patch1,'Visible','off')
3761set(handles.rho_text1,'Visible','off')
3762set(handles.thresh_patch1,'Visible','off')
3763set(handles.thresh_text1,'Visible','off')
3764set(handles.subdomain_patch1,'Visible','off')
3765set(handles.subdomain_text1,'Visible','off')
3766set(handles.nx_patch1,'Visible','off')
3767set(handles.ny_patch1,'Visible','off')
3768set(handles.nx_patch1_title,'Visible','off')
3769set(handles.ny_patch1_title,'Visible','off')
3770%set(handles.test_interp,'Visible','off')
3771set(handles.get_gridpatch1,'Visible','off')
3772set(handles.grid_patch1,'Visible','off')
3773
3774%------------------------------------------------------------------------
3775function enable_civ2(handles,state)
3776%------------------------------------------------------------------------
3777if isequal(state,0)
3778    state='off';
3779end
3780if isequal(state,1)
3781    state='on';
3782end
3783if isequal(state,'on')
3784    set(handles.frame_civ2,'BackgroundColor',[1 1 0])
3785    set(handles.frame_para_civ2,'BackgroundColor',[1 1 0])
3786    set(handles.frame_grid_civ2,'BackgroundColor',[1 1 0])
3787    set(handles.frame_subdirciv2,'BackgroundColor',[1 1 0])
3788else
3789    set(handles.frame_civ2,'BackgroundColor',[0.831 0.816 0.784])
3790    set(handles.frame_para_civ2,'BackgroundColor',[0.831 0.816 0.784])
3791    set(handles.frame_grid_civ2,'BackgroundColor',[0.831 0.816 0.784])
3792    set(handles.frame_subdirciv2,'BackgroundColor',[0.831 0.816 0.784])
3793end
3794set(handles.ibx_civ2,'Visible',state)
3795set(handles.iby_civ2,'Visible',state)
3796set(handles.decimal,'Visible',state)
3797set(handles.deformation,'Visible',state)
3798set(handles.rho_civ2,'Visible',state)
3799set(handles.dx_civ2,'Visible',state)
3800set(handles.dy_civ2,'Visible',state)
3801set(handles.browse_gridciv2,'Visible',state)
3802set(handles.get_mask_civ2,'Visible',state)
3803set(handles.parameters,'Visible',state)
3804set(handles.grid,'Visible',state)
3805set(handles.parameters_text,'Visible',state)
3806set(handles.grid_text,'Visible',state)
3807set(handles.grid_civ2,'Visible',state)
3808set(handles.mask_civ2,'Visible',state)
3809set(handles.dx_civ2_title,'Visible',state)
3810set(handles.dy_civ2_title,'Visible',state)
3811set(handles.ibx_civ2_text,'Visible',state)
3812set(handles.rho_civ2_title,'Visible',state)
3813set(handles.ImaThreshold2,'Visible',state)
3814set(handles.ImaThreshold_title2,'Visible',state)
3815if isequal(state,'off')
3816    set(handles.MinIma2,'Visible','off')
3817    set(handles.MaxIma2,'Visible','off')
3818    set(handles.ImaThreshold2,'Value',0)
3819    if isequal(get(handles.FIX2,'Value'),0) & isequal(get(handles.PATCH2,'Value'),0)
3820        set(handles.list_pair_civ2,'Visible','off')
3821        set(handles.subdir_civ2,'Visible','off')
3822        set(handles.subdir_civ2_text,'Visible','off')
3823        set(handles.dt_unit_civ2,'Visible','off')
3824        set(handles.ref_i_civ2,'Visible','off')
3825        set(handles.i_ref_civ2_title,'Visible','off')
3826        set(handles.j_ref_civ2_title,'Visible','off')
3827        set(handles.ref_j_civ2,'Visible','off')
3828    end
3829else
3830    set(handles.list_pair_civ2,'Visible','on')
3831    set(handles.subdir_civ2,'Visible','on')
3832    set(handles.subdir_civ2_text,'Visible','on')
3833    set(handles.dt_unit_civ2,'Visible','on')
3834    set(handles.ref_i_civ2,'Visible','on')
3835    set(handles.i_ref_civ2_title,'Visible','on')
3836    set(handles.j_ref_civ2_title,'Visible','on')
3837    set(handles.ref_j_civ2,'Visible','on')
3838end
3839set(handles.rho_civ2_title,'Visible',state)
3840
3841%------------------------------------------------------------------------
3842function enable_fix2(handles)
3843%------------------------------------------------------------------------
3844set(handles.frame_fix2,'BackgroundColor',[1 1 0])
3845set(handles.REMOVE2,'Visible','on')
3846set(handles.vec_Fmin2_2,'Visible','on')
3847set(handles.vec_F4,'Visible','on')
3848set(handles.vec_F3_2,'Visible','on')
3849set(handles.thresh_vec2C,'Visible','on')
3850set(handles.thresh_vec2C_text,'Visible','on')
3851set(handles.thresh_vel2,'Visible','on')
3852set(handles.thresh_vel2_text,'Visible','on')
3853set(handles.mask_fix2,'Visible','on')
3854set(handles.get_mask_fix2,'Visible','on')
3855set(handles.list_pair_civ2,'Visible','on')
3856set(handles.subdir_civ2,'Visible','on')
3857set(handles.subdir_civ2_text,'Visible','on')
3858set(handles.get_ref_fix2,'Visible','on')
3859set(handles.ref_fix2,'Visible','on')
3860set(handles.inf_sup2,'Visible','on')
3861set(handles.field_ref2,'Visible','on')
3862
3863%------------------------------------------------------------------------
3864function desable_fix2(handles)
3865%------------------------------------------------------------------------
3866set(handles.frame_fix2,'BackgroundColor',[0.831 0.816 0.784])
3867set(handles.REMOVE2,'Visible','off')
3868set(handles.vec_Fmin2_2,'Visible','off')
3869set(handles.vec_F4,'Visible','off')
3870set(handles.vec_F3_2,'Visible','off')
3871set(handles.thresh_vec2C,'Visible','off')
3872set(handles.thresh_vec2C_text,'Visible','off')
3873set(handles.thresh_vel2,'Visible','off')
3874set(handles.thresh_vel2_text,'Visible','off')
3875set(handles.mask_fix2,'Visible','off')
3876set(handles.get_mask_fix2,'Visible','off')
3877set(handles.get_ref_fix2,'Visible','off')
3878set(handles.ref_fix2,'Visible','off')
3879set(handles.inf_sup2,'Visible','off')
3880set(handles.field_ref2,'Visible','off')
3881if isequal(get(handles.CIV2,'Value'),0) & isequal(get(handles.PATCH2,'Value'),0)
3882    set(handles.list_pair_civ2,'Visible','off')
3883    set(handles.subdir_civ2,'Visible','off')
3884    set(handles.subdir_civ2_text,'Visible','off')
3885end
3886
3887%------------------------------------------------------------------------
3888function enable_patch2(handles)
3889%------------------------------------------------------------------------
3890set(handles.frame_patch2,'BackgroundColor',[1 1 0])
3891set(handles.rho_patch2,'Visible','on')
3892set(handles.rho_text2,'Visible','on')
3893set(handles.thresh_patch2,'Visible','on')
3894set(handles.thresh_text2,'Visible','on')
3895set(handles.subdomain_patch2,'Visible','on')
3896set(handles.subdomain_text2,'Visible','on')
3897set(handles.nx_patch2,'Visible','on')
3898set(handles.ny_patch2,'Visible','on')
3899set(handles.nx_patch2_title,'Visible','on')
3900set(handles.ny_patch2_title,'Visible','on')
3901set(handles.get_gridpatch2,'Visible','on')
3902set(handles.grid_patch2,'Visible','on')
3903set(handles.list_pair_civ2,'Visible','on')
3904set(handles.subdir_civ2,'Visible','on')
3905set(handles.subdir_civ2_text,'Visible','on')
3906
3907%------------------------------------------------------------------------
3908function desable_patch2(handles)
3909%------------------------------------------------------------------------
3910set(handles.frame_patch2,'BackgroundColor',[0.831 0.816 0.784])
3911set(handles.rho_patch2,'Visible','off')
3912set(handles.rho_text2,'Visible','off')
3913set(handles.thresh_patch2,'Visible','off')
3914set(handles.thresh_text2,'Visible','off')
3915set(handles.subdomain_patch2,'Visible','off')
3916set(handles.subdomain_text2,'Visible','off')
3917set(handles.nx_patch2,'Visible','off')
3918set(handles.ny_patch2,'Visible','off')
3919set(handles.nx_patch2_title,'Visible','off')
3920set(handles.ny_patch2_title,'Visible','off')
3921set(handles.get_gridpatch2,'Visible','off')
3922set(handles.grid_patch2,'Visible','off')
3923if isequal(get(handles.CIV2,'Value'),0) & isequal(get(handles.FIX2,'Value'),0)
3924    set(handles.list_pair_civ2,'Visible','off')
3925    set(handles.subdir_civ2,'Visible','off')
3926    set(handles.subdir_civ2_text,'Visible','off')
3927end
3928
3929%------------------------------------------------------------------------
3930function enable_pair1(handles,state)
3931%------------------------------------------------------------------------
3932set(handles.subdir_civ1,'Visible',state)
3933set(handles.list_subdir_civ1,'Visible',state)
3934set(handles.SUBDIR_CIV1_txt,'Visible',state)
3935set(handles.frame_subdirciv1,'Visible',state)
3936set(handles.list_pair_civ1,'Visible',state)
3937set(handles.PAIR_txt,'Visible',state)
3938%set(handles.dt_unit,'Visible',state)
3939set(handles.PAIR_frame,'Visible',state)
3940
3941%------------------------------------------------------------------------
3942% --- Read the parameters for civ1 on the interface
3943function par=read_param_civ1(handles,file_ima)
3944%------------------------------------------------------------------------
3945ibx_val=str2double(get(handles.ibx,'String'));
3946par.ibx=num2str(ibx_val);
3947iby_val=str2double(get(handles.iby,'String'));
3948par.iby=num2str(iby_val);
3949isx=get(handles.isx,'String');
3950if isnan(str2double(isx)), isx='41'; set(handles.isx,'String','41'), end; %default
3951if str2double(isx)<ibx_val+8,isx=num2str(ibx_val+8); set(handles.isx,'String',num2str(ibx_val+8)); end
3952isy=get(handles.isy,'String');
3953if isnan(str2double(isy)), isy='41'; set(handles.isy,'String','41'), end;%default
3954if str2double(isy)<iby_val+8,isy=num2str(iby_val+8); set(handles.isy,'String',num2str(iby_val+8)); end
3955par.isx=get(handles.isx,'String');
3956par.isy=get(handles.isy,'String');
3957par.shiftx=get(handles.shiftx,'String');
3958par.shifty=get(handles.shifty,'String');
3959if isnan(str2double(par.isx))
3960    par.isx='41';%default
3961    set(handles.isx,'String','41');
3962end
3963if isnan(str2double(par.isy))
3964    par.isy='41'; %default
3965    set(handles.isy,'String','41');
3966end
3967if isnan(str2double(par.shiftx))
3968    par.shiftx='0';%default
3969    set(handles.shiftx,'String','0');
3970end
3971if isnan(str2double(par.shifty))
3972    par.shifty='0'; %default
3973    set(handles.shifty,'String','0');
3974end
3975par.rho=get(handles.rho,'String');
3976par.dx=get(handles.dx_civ1,'String');
3977par.dy=get(handles.dy_civ1,'String');
3978if isnan(str2double(par.dx))
3979    if isempty(get(handles.grid_civ1,'String'));
3980        par.dx='0'; %just read by civ program, not used
3981    else
3982        par.dx='20';%default
3983        set(handles.dx_civ1,'String','20');
3984    end
3985end
3986if isnan(str2double(par.dy))
3987    if isempty(get(handles.grid_civ1,'String'));
3988        par.dy='0';%just read by civ program, not used
3989    else
3990        par.dy='20';%default
3991        set(handles.dy_civ1_title,'String','20');
3992    end
3993end
3994par.pxcmx='1'; %velocities are expressed in pixel dispalcement
3995par.pxcmy='1';
3996if exist('file_ima','var')
3997A=imread(file_ima);%read the first image to get the size
3998sizim=size(A);
3999par.npx=num2str(sizim(2));
4000par.npy=num2str(sizim(1));
4001end
4002%time=get(handles.RootName,'UserData'); %get the set of times
4003par.gridname=get(handles.grid_civ1,'String');
4004par.gridflag='y';
4005if strcmp(par.gridname,'')|| isempty(par.gridname)
4006    par.gridname='nogrid';
4007    par.gridflag='n';
4008end
4009
4010%------------------------------------------------------------------------
4011function par=read_param_civ2(handles,file_ima)
4012%------------------------------------------------------------------------
4013par.ibx=get(handles.ibx_civ2,'String');
4014par.iby=get(handles.iby_civ2,'String');
4015par.rho=get(handles.rho_civ2,'String');
4016par.decimal=int2str(get(handles.decimal,'Value'));
4017par.deformation=int2str(get(handles.deformation,'Value'));
4018par.dx=get(handles.dx_civ2,'String');
4019par.dy=get(handles.dy_civ2,'String');
4020if isnan(str2double(par.dx))
4021    if isempty(get(handles.grid_civ2,'String'));
4022        par.dx='0'; %just read by civ program, not used
4023    else
4024        par.dx='20';%default
4025        set(handles.dx_civ2,'String','20');
4026    end
4027end
4028if isnan(str2double(par.dy))
4029    if isempty(get(handles.grid_civ2,'String'));
4030        par.dy='0';%just read by civ program, not used
4031    else
4032        par.dy='20';%default
4033        set(handles.dy_civ2,'String','20');
4034    end
4035end
4036par.pxcmx='1';
4037par.pxcmy='1';
4038A=imread(file_ima);%read the first image to get the size
4039sizim=size(A);
4040par.npx=num2str(sizim(2));
4041par.npy=num2str(sizim(1));
4042%time=get(handles.RootName,'UserData'); %get the set of times
4043par.gridname=get(handles.grid_civ2,'String');
4044par.gridflag='y';
4045if strcmp(par.gridname,'')|| isempty(par.gridname)
4046    par.gridname='nogrid';
4047    par.gridflag='n';
4048end
4049
4050%------------------------------------------------------------------------
4051% --- CIV1  CIV1  CIV1 CIV1
4052function cmd_CIV1=CIV1_CMD(filename,namelog,par,handles,sparam)
4053%------------------------------------------------------------------------
4054%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4055
4056%changes : filename_cmx -> filename ( no extension )
4057% input namelog not used
4058if isequal(par.Dt,'0')
4059    par.Dt='1' ;%case of 'displacement' mode
4060end
4061par.filename_ima_a=regexprep(par.filename_ima_a,'.png','');
4062par.filename_ima_b=regexprep(par.filename_ima_b,'.png','');
4063fid=fopen([filename '.cmx'],'w');
4064fprintf(fid,['##############   CMX file' '\n' ]);
4065fprintf(fid,   ['FirstImage ' regexprep(par.filename_ima_a,'\\','\\\\') '\n' ]);% for windows compatibility
4066fprintf(fid,   ['LastImage  ' regexprep(par.filename_ima_b,'\\','\\\\') '\n' ]);% for windows compatibility
4067fprintf(fid,  ['XX' '\n' ]);
4068fprintf(fid,  ['Mask ' par.maskflag '\n' ]);
4069fprintf(fid,  ['MaskName ' regexprep(par.maskname,'\\','\\\\') '\n' ]);
4070fprintf(fid,   ['ImageSize ' par.npx ' ' par.npy '\n' ]);   %VERIFIER CAS GENERAL ?
4071fprintf(fid,   ['CorrelationBoxesSize ' par.ibx ' ' par.iby '\n' ]);
4072fprintf(fid,   ['SearchBoxeSize ' par.isx ' ' par.isy '\n' ]);
4073fprintf(fid,   ['RO ' par.rho '\n' ]);
4074fprintf(fid,   ['GridSpacing ' par.dx ' ' par.dy '\n' ]);
4075fprintf(fid,   ['XX 1.0' '\n' ]);
4076fprintf(fid,   ['Dt_TO ' par.Dt ' ' par.T0 '\n' ]);
4077fprintf(fid,  ['PixCmXY ' par.pxcmx ' ' par.pxcmy '\n' ]);
4078fprintf(fid,  ['XX 1' '\n' ]);
4079fprintf(fid,   ['ShiftXY ' par.shiftx ' '  par.shifty '\n' ]);
4080fprintf(fid,  ['Grid ' par.gridflag '\n' ]);
4081fprintf(fid,   ['GridName ' regexprep(par.gridname,'\\','\\\\') '\n' ]);
4082fprintf(fid,   ['XX 85' '\n' ]);
4083fprintf(fid,   ['XX 1.0' '\n' ]);
4084fprintf(fid,   ['XX 1.0' '\n' ]);
4085fprintf(fid,   ['Hart 1' '\n' ]);
4086fprintf(fid,  [ 'DecimalShift 0' '\n' ]);
4087fprintf(fid,   ['Deformation 0' '\n' ]);
4088fprintf(fid,  ['CorrelationMin 0' '\n' ]);
4089fprintf(fid,   ['IntensityMin 0' '\n' ]);
4090fprintf(fid,  ['SeuilImage n' '\n' ]);
4091fprintf(fid,   ['SeuilImageValues 0 4096' '\n' ]);
4092fprintf(fid,   ['ImageToUse ' par.term_a ' ' par.term_b '\n' ]); % VERIFIER ?
4093fprintf(fid,   ['ImageUsedBefore null null' '\n' ]);
4094fclose(fid);
4095
4096% cmd_CIV1=[sparam.Civ1Bin ' -f ' filename '.cmx >' filename '.log' ]; % redirect standard output to the log file
4097% cmd_CIV1=regexprep(cmd_CIV1,'\\','\\\\');
4098% namelog=regexprep(namelog,'\\','\\\\');
4099if(isunix)
4100%     filename
4101%     namelog
4102%     
4103%     [Rootbat,Filebat,extbat]=fileparts(namelog);
4104%     ncName=fullfile(Rootbat,[ Filebat '.nc']);
4105    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
4106    cmd_CIV1=[cmd_CIV1 '\n' 'mv ' filename '.log' ' ' filename '.civ1.log' '\n' 'chmod g+w ' filename '.nc'];%rename .log as .civ1.log and set the netcdf result file for group user writting
4107    cmd_CIV1=[cmd_CIV1 '\n' 'mv ' filename '.cmx' ' ' filename '.civ1.cmx' '\n'];%rename .cmx as .civ1.cmx
4108else %Windows system
4109    filename=regexprep(filename,'\\','\\\\');
4110    cmd_CIV1=['"' sparam.Civ1Bin '" -f "' filename '.cmx" >"' filename '.log"' ]; % redirect standard output to the log file
4111    cmd_CIV1=regexprep(cmd_CIV1,'\\','\\\\');
4112    namelog=regexprep(namelog,'\\','\\\\');
4113    cmd_CIV1=[cmd_CIV1 '\n' 'copy /Y "' filename '.log' '" "' filename '.civ1.log"'];
4114    cmd_CIV1=[cmd_CIV1 '\n' 'copy /Y "' filename '.cmx' '" "' filename '.civ1.cmx"'];
4115end
4116
4117%------------------------------------------------------------------------
4118% --- CIV1  Unified
4119function xml_civ1_parameters=CIV1_CMD_Unified(filename,namelog,par)
4120%------------------------------------------------------------------------
4121%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4122%global CivBin%name of the executable for civ1 calculation
4123
4124civ1.image1=par.filename_ima_a;
4125civ1.image2=par.filename_ima_b;
4126civ1.imageSize_X=par.npx;
4127civ1.imageSize_Y=par.npy;
4128civ1.outputFileName=[filename '.nc'];
4129civ1.correlationBoxesSize_X=par.ibx;
4130civ1.correlationBoxesSize_Y=par.iby;
4131civ1.searchBoxesSize_X=par.isx;
4132civ1.searchBoxesSize_Y=par.isy;
4133civ1.globalShift_X=par.shiftx;
4134civ1.globalShift_Y=par.shifty;
4135civ1.ro=par.rho;
4136civ1.hart='y';
4137if isequal(par.gridflag,'y')
4138    civ1.grid=par.gridname;
4139else
4140    civ1.grid='n';
4141    civ1.gridSpacing_X=par.dx;
4142    civ1.gridSpacing_Y=par.dy;
4143end
4144if isequal(par.maskflag,'y')
4145    civ1.mask=par.maskname;
4146end
4147civ1.dt=par.Dt;
4148civ1.unit='pixel';
4149civ1.absolut_time_T0=par.T0;
4150civ1.pixcmx=par.pxcmx;
4151civ1.pixcmy=par.pxcmy;
4152civ1.convectFlow='n';
4153
4154xml_civ1_parameters=civ1;
4155
4156%------------------------------------------------------------------------
4157% --- CIV2  Unified
4158function civ2=CIV2_CMD_Unified(filename,namelog,par)
4159%------------------------------------------------------------------------
4160%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4161%global CivBin%name of the executable for civ1 calculation
4162
4163civ2.image1=par.filename_ima_a;
4164civ2.image2=par.filename_ima_b;
4165civ2.imageSize_X=par.npx;
4166civ2.imageSize_Y=par.npy;
4167civ2.inputFileName=[par.filename_nc1 '.nc'];
4168civ2.outputFileName=[filename '.nc'];
4169civ2.correlationBoxesSize_X=par.ibx;
4170civ2.correlationBoxesSize_Y=par.iby;
4171civ2.ro=par.rho;
4172%civ2.decimalShift=par.decimal;
4173%civ2.deformation=par.deformation;
4174if isequal(par.decimal,'1')
4175    civ2.decimalShift='y';
4176else
4177    civ2.decimalShift='n';
4178end
4179if isequal(par.deformation,'1')
4180    civ2.deformation='y';
4181else
4182    civ2.deformation='n';
4183end
4184if isequal(par.gridflag,'y')
4185    civ2.grid=par.gridname;
4186else
4187    civ2.grid='n';
4188    civ2.gridSpacing_X=par.dx;
4189    civ2.gridSpacing_Y=par.dy;
4190end
4191civ2.gridSpacing_X='10';
4192civ2.gridSpacing_Y='10';%NOTE: faut mettre gridSpacing pourque ca tourne, meme si c'est la grille qui est utilisee
4193if isequal(par.maskflag,'y')
4194    civ2.mask=par.maskname;
4195else
4196    civ2.mask='n';
4197end
4198civ2.dt=par.Dt;
4199civ2.unit='pixel';
4200civ2.absolut_time_T0=par.T0;
4201civ2.pixcmx=par.pxcmx;
4202civ2.pixcmy=par.pxcmy;
4203civ2.convectFlow='n';
4204civ2.pixcmx=par.pxcmx;
4205civ2.pixcmy=par.pxcmy;
4206civ2.convectFlow='n';
4207
4208%------------------------------------------------------------------------
4209% --- CIV2  CIV2  CIV2 CIV2
4210function cmd_CIV2=CIV2_CMD(filename,namelog,par,sparam)
4211%------------------------------------------------------------------------
4212%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4213% global civ2Bin sge%name of the executable for civ1 calculation
4214if isequal(par.Dt,'0')
4215    par.Dt='1' ;%case of 'displacement' mode
4216end
4217par.filename_ima_a=regexprep(par.filename_ima_a,'.png','');
4218par.filename_ima_b=regexprep(par.filename_ima_b,'.png','');% bug : .png appears two times ?
4219[fid,errormsg]=fopen([filename '.civ2.cmx'],'w');
4220if isequal(fid,-1)
4221    msgbox_uvmat('ERROR',errormsg)
4222    cmd_CIV2='';
4223    return
4224end
4225fprintf(fid,['##############   CMX file' '\n' ]);
4226fprintf(fid,   ['FirstImage ' regexprep(par.filename_ima_a,'\\','\\\\') '\n' ]);% for windows compatibility
4227fprintf(fid,   ['LastImage  ' regexprep(par.filename_ima_b,'\\','\\\\') '\n' ]);% for windows compatibility
4228fprintf(fid,  ['XX' '\n' ]);
4229fprintf(fid, ['Mask ' par.maskflag '\n' ]);
4230fprintf(fid, ['MaskName ' regexprep(par.maskname,'\\','\\\\') '\n' ]);% for windows compatibility
4231fprintf(fid, ['ImageSize ' par.npx ' ' par.npy '\n' ]);   %VERIFIER CAS GENERAL ?
4232fprintf(fid, ['CorrelationBoxesSize ' par.ibx ' ' par.iby '\n' ]);
4233fprintf(fid, ['SearchBoxeSize ' par.ibx ' ' par.iby '\n']);
4234fprintf(fid, ['RO ' par.rho '\n']);
4235fprintf(fid, ['GridSpacing ' par.dx ' ' par.dy '\n']);
4236fprintf(fid, ['XX 1.0' '\n' ]);
4237fprintf(fid, ['Dt_TO ' par.Dt ' ' par.T0 '\n' ]);
4238fprintf(fid, ['PixCmXY ' par.pxcmx ' ' par.pxcmy '\n' ]);
4239fprintf(fid, ['XX 1' '\n' ]);
4240fprintf(fid, 'ShiftXY 0 0\n');
4241fprintf(fid, ['Grid ' par.gridflag '\n' ]);
4242fprintf(fid, ['GridName ' regexprep(par.gridname,'\\','\\\\') '\n']);
4243fprintf(fid, ['XX 85' '\n' ]);
4244fprintf(fid, ['XX 1.0' '\n' ]);
4245fprintf(fid, ['XX 1.0' '\n' ]);
4246fprintf(fid, ['Hart 1' '\n' ]);
4247fprintf(fid, ['DecimalShift ' par.decimal '\n']);
4248fprintf(fid, ['Deformation ' par.deformation '\n']);
4249fprintf(fid,  ['CorrelationMin 0' '\n' ]);
4250fprintf(fid,   ['IntensityMin 0' '\n' ]);
4251fprintf(fid,  ['SeuilImage n' '\n' ]);
4252fprintf(fid,   ['SeuilImageValues 0 4096' '\n' ]);
4253fprintf(fid,   ['ImageToUse ' par.term_a ' ' par.term_b '\n' ]); % VERIFIER ?
4254fprintf(fid, ['ImageUsedBefore ' regexprep(par.filename_nc1,'\\','\\\\') '\n']);
4255fclose(fid);
4256
4257if(isunix)
4258%         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
4259%     cmd_CIV1=[cmd_CIV1 '\n' 'mv ' filename '.log' ' ' filename '.civ1.log' '\n' 'chmod g+w ' filename '.nc'];
4260%     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
4261   
4262    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
4263    cmd_CIV2=[cmd_CIV2 '\n' 'mv ' filename '.log' ' ' filename '.civ2.log' '\n' 'chmod g+w ' filename '.nc'];
4264    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
4265%     [Rootbat,Filebat,extbat]=fileparts(namelog);
4266%     ncName=fullfile(Rootbat,[ Filebat '.nc']);
4267%    cmd_CIV2=[cmd_CIV2 '\n' 'mv ' namelog  ' ' regexprep(namelog,'\.log','') '.civ2.log' '\n' 'chmod g+w ' ncName];
4268else
4269    filename=regexprep(filename,'\\','\\\\');
4270    cmd_CIV2=['"' sparam.Civ2Bin '" -f "' filename  '.cmx" >"' filename '.log"' ]; % redirect standard output to the log file
4271    cmd_CIV2=regexprep(cmd_CIV2,'\\','\\\\');
4272    cmd_CIV2=[cmd_CIV2 '\n' 'copy /Y "' filename '.log' '" "' filename '.civ2.log"'];
4273     cmd_CIV2=[cmd_CIV2 '\n' 'copy /Y "' filename '.cmx' '" "' filename '.civ2.cmx"'];
4274end
4275
4276
4277
4278%------------------------------------------------------------------------
4279% --- Executes on button press in HELP.
4280function HELP_Callback(hObject, eventdata, handles)
4281%------------------------------------------------------------------------
4282path_to_uvmat=which ('uvmat');% check the path of uvmat
4283pathelp=fileparts(path_to_uvmat);
4284helpfile=fullfile(pathelp,'uvmat_doc','uvmat_doc.html');
4285if isempty(dir(helpfile)), msgbox_uvmat('ERROR','Please put the help file uvmat_doc.html in the sub-directory /uvmat_doc of the UVMAT package')
4286else
4287    addpath (fullfile(pathelp,'uvmat_doc'))
4288    web([helpfile '#civ'])
4289end
4290
4291%------------------------------------------------------------------------
4292%--read images and convert them to the uint16 format used for PIV
4293function A=read_image(filename,type_ima,num,movieobject)
4294%------------------------------------------------------------------------
4295%num is the view number needed for an avi movie
4296switch type_ima
4297    case 'movie'
4298        A=read(movieobject,num);
4299    case 'avi'
4300        mov=aviread(filename,num);
4301        A=frame2im(mov(1));
4302    case 'multimage'
4303        A=imread(filename,num);
4304    case 'image'
4305        A=imread(filename);
4306end
4307siz=size(A);
4308if length(siz)==3;%color images
4309    A=sum(double(A),3);
4310    A=uint16(A);
4311end
4312
4313%------------------------------------------------------------------------
4314function ref_i_Callback(hObject, eventdata, handles)
4315%------------------------------------------------------------------------
4316mode_list=get(handles.mode,'String');
4317mode_value=get(handles.mode,'Value');
4318mode=mode_list{mode_value};
4319find_netcpair_civ1(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4320if isequal(mode,'series(Di)') || ...% we do patch2 only
4321        (get(handles.CIV2,'Value')==0 && get(handles.CIV1,'Value')==0 && get(handles.FIX1,'Value')==0 && get(handles.PATCH1,'Value')==0)
4322    find_netcpair_civ2(hObject, eventdata, handles);
4323end
4324
4325%------------------------------------------------------------------------
4326function ref_j_Callback(hObject, eventdata, handles)
4327%------------------------------------------------------------------------
4328mode_list=get(handles.mode,'String');
4329mode_value=get(handles.mode,'Value');
4330mode=mode_list{mode_value};
4331if isequal(get(handles.CIV1,'Value'),0)|| isequal(mode,'series(Dj)')
4332    find_netcpair_civ1(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4333end
4334if isequal(mode,'series(Dj)') || ...
4335        (get(handles.CIV2,'Value')==0 && get(handles.CIV1,'Value')==0 && get(handles.FIX1,'Value')==0 && get(handles.PATCH1,'Value')==0)
4336    find_netcpair_civ2(hObject, eventdata, handles);
4337end
4338
4339%------------------------------------------------------------------------
4340function ref_i_civ2_Callback(hObject, eventdata, handles)
4341%------------------------------------------------------------------------
4342mode_list=get(handles.mode,'String');
4343mode_value=get(handles.mode,'Value');
4344mode=mode_list{mode_value};
4345find_netcpair_civ2(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4346
4347%------------------------------------------------------------------------
4348function ref_j_civ2_Callback(hObject, eventdata, handles)
4349%------------------------------------------------------------------------
4350mode_list=get(handles.mode,'String');
4351mode_value=get(handles.mode,'Value');
4352mode=mode_list{mode_value};
4353if  isequal(mode,'series(Dj)')
4354    find_netcpair_civ2(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4355end
4356
4357%------------------------------------------------------------------------
4358% --- Executes on button press in compare.
4359function compare_Callback(hObject, eventdata, handles)
4360%------------------------------------------------------------------------
4361test=get(handles.compare,'Value');
4362if test==2 || test==3
4363    filebase=get(handles.RootName,'String');
4364    browse=get(handles.browse_root,'Userdata');
4365    browse.nom_type_ima1=browse.nom_type_ima;
4366    set(handles.browse_root,'UserData',browse);
4367    set(handles.sub_txt,'Visible','on')
4368    set(handles.RootName_1,'Visible','On');%mkes the second file input window visible
4369    mode_store=get(handles.mode,'String');%get the present 'mode'
4370    set(handles.compare,'UserData',mode_store);%store the mode display
4371    set(handles.mode,'Visible','off')
4372    if test==2
4373        set(handles.mode,'Visible','off')
4374    else
4375        set(handles.mode,'Visible','on')
4376    end
4377   
4378    % open an image file with the browser
4379    ind_opening=1;%default
4380    browse.incr_pair=[0 0]; %default
4381    oldfile=get(handles.RootName,'String');
4382    menu={'*.xml;*.avi;*.AVI;*.nc','(*.xml,*.avi,*.nc)'; ...
4383        '*.xml', '.xml files';'*.avi;*.AVI', '.avi files';'*.nc', '.nc files';...
4384        '*.*', 'All Files (*.*)'};
4385    [FileName, PathName, filtindex] = uigetfile( menu, 'Pick a file',oldfile);
4386    fileinput=[PathName FileName];%complete file name
4387    sizf=size(fileinput);
4388    if (~ischar(fileinput)|~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
4389    [path,name,ext]=fileparts(fileinput);
4390    [path1]=fileparts(filebase);
4391    if ~strcmp(path1,path)
4392        msgbox_uvmat('ERROR','The two  input image series must be in the same directory')
4393        return
4394    end
4395    set(handles.RootName_1,'String',name);
4396    [RootPath,RootFile,field_count,str2,str_a,str_b,xx,nom_type,subdir]=name2display(name);
4397    browse=get(handles.browse_root,'UserData');
4398    browse.nom_type_ima_1=nom_type;
4399    set(handles.browse_root,'UserData',browse)
4400   
4401    %check image extension
4402    if ~strcmp(ext,get(handles.ImaExt,'String'))
4403        msgbox_uvmat('ERROR','The two  input image series must have the same extenion name')
4404        return
4405    end
4406   
4407    %check image size
4408    A=imread(fileinput);
4409    npxy=get(handles.ImaExt,'UserData');
4410    if ~isequal(npxy(1),size(A,1))|| ~isequal(npxy(2),size(A,2))
4411        msgbox_uvmat('ERROR','The two input image series must have the same size')
4412        return
4413    end
4414else
4415    set(handles.mode,'Visible','on')
4416    set(handles.RootName_1,'Visible','Off');
4417    set(handles.sub_txt,'Visible','off')
4418    set(handles.RootName_1,'String',[]);
4419    mode_store=get(handles.compare,'UserData');
4420    set(handles.mode,'Value',1)
4421    set(handles.mode,'String',mode_store)
4422    set(handles.test_stereo1,'Value',0)
4423    set(handles.test_stereo2,'Value',0)
4424end
4425mode_Callback(hObject, eventdata, handles)
4426
4427%------------------------------------------------------------------------
4428% --- Executes on button press in get_ref_fix1.
4429function get_ref_fix1_Callback(hObject, eventdata, handles)
4430%------------------------------------------------------------------------
4431filebase=get(handles.RootName,'String');
4432[FileName, PathName, filterindex] = uigetfile( ...
4433    {'*.nc', ' (*.nc)';
4434    '*.nc',  'netcdf files '; ...
4435    '*.*', 'All Files (*.*)'}, ...
4436    'Pick a file',filebase);
4437
4438fileinput=[PathName FileName];
4439sizf=size(fileinput);
4440if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
4441[Path,File,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
4442ref.filebase=fullfile(Path,File);
4443ref.num_a=stra2num(str_a);
4444ref.num_b=stra2num(str_b);
4445ref.num1=str2double(field_count);
4446ref.num2=str2double(str2);
4447browse=[];%initialisation
4448if ~isequal(ref.ext,'.nc')
4449    msgbox_uvmat('ERROR','the reference file must be in netcdf format (*.nc)')
4450    return
4451end
4452set(handles.ref_fix1,'String',[fullfile(ref.subdir,File) '....nc']);
4453set(handles.ref_fix1,'UserData',ref)
4454menu_field{1}='civ1';
4455Data=nc2struct(fileinput,[]);
4456if isfield(Data,'patch') && isequal(Data.patch,1)
4457    menu_field{2}='filter1';
4458end
4459if isfield(Data,'civ2') && isequal(Data.civ2,1)
4460    menu_field{3}='civ2';
4461end
4462if isfield(Data,'patch2') && isequal(Data.patch2,1)
4463    menu_field{4}='filter2';
4464end
4465set(handles.field_ref1,'String',menu_field);
4466set(handles.field_ref1,'Value',length(menu_field));
4467set(handles.inf_sup1,'Value',2);
4468set(handles.thresh_vel,'String','1');%default threshold
4469set(handles.ref_fix1,'Enable','on')
4470
4471%------------------------------------------------------------------------
4472% --- Executes on button press in get_ref_fix2.
4473function get_ref_fix2_Callback(hObject, eventdata, handles)
4474%------------------------------------------------------------------------
4475if isequal(get(handles.get_ref_fix2,'Value'),1)
4476    filebase=get(handles.RootName,'String');
4477    [FileName, PathName, filterindex] = uigetfile( ...
4478        {'*.nc', ' (*.nc)';
4479        '*.nc',  'netcdf files '; ...
4480        '*.*', 'All Files (*.*)'}, ...
4481        'Pick a file',filebase);
4482    fileinput=[PathName FileName];
4483    sizf=size(fileinput);
4484    if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
4485    [Path,File,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
4486    ref.filebase=fullfile(Path,File);
4487    ref.num_a=stra2num(str_a);
4488    ref.num_b=stra2num(str_b);
4489    ref.num1=str2num(field_count);
4490    ref.num2=str2num(str2);
4491    browse=[];%initialisation
4492    if ~isequal(ref.ext,'.nc')
4493        msgbox_uvmat('ERROR','the reference file must be in netcdf format (*.nc)')
4494        return
4495    end
4496    set(handles.ref_fix2,'String',[fullfile(ref.subdir,File) '....nc']);
4497    set(handles.ref_fix2,'UserData',ref)
4498    menu_field{1}='civ1';
4499    Data=nc2struct(fileinput,[]);
4500    if isfield(Data,'patch') & isequal(Data.patch,1)
4501        menu_field{2}='filter1';
4502    end
4503    if isfield(Data,'civ2') & isequal(Data.civ2,1)
4504        menu_field{3}='civ2';
4505    end
4506    if isfield(Data,'patch2') & isequal(Data.patch2,1)
4507        menu_field{4}='filter2';
4508    end
4509    set(handles.field_ref2,'String',menu_field);
4510    set(handles.field_ref2,'Value',length(menu_field));
4511    set(handles.inf_sup2,'Value',2);
4512    set(handles.thresh_vel2,'String','1');%default threshold
4513    set(handles.ref_fix2,'Enable','on')
4514    set(handles.ref_fix2,'Visible','on')
4515    set(handles.field_ref2,'Visible','on')
4516else
4517    set(handles.ref_fix2,'Visible','off')
4518    set(handles.field_ref2,'Visible','off')
4519end
4520
4521%------------------------------------------------------------------------
4522function ref_fix1_Callback(hObject, eventdata, handles)
4523%------------------------------------------------------------------------
4524set(handles.inf_sup1,'Value',1);
4525set(handles.field_ref1,'Value',1)
4526set(handles.field_ref1,'String',{' '})
4527set(handles.ref_fix1,'UserData',[]);
4528set(handles.ref_fix1,'String','');
4529set(handles.thresh_vel1,'String','0');
4530
4531%------------------------------------------------------------------------
4532function ref_fix2_Callback(hObject, eventdata, handles)
4533%------------------------------------------------------------------------
4534set(handles.inf_sup2,'Value',1);
4535set(handles.field_ref2,'Value',1)
4536set(handles.field_ref2,'String',{' '})
4537set(handles.ref_fix2,'UserData',[]);
4538set(handles.ref_fix2,'String','');
4539set(handles.thresh_vel2,'String','0');
4540
4541%------------------------------------------------------------------------
4542% --- Executes on button press in test_stereo1.
4543function test_stereo1_Callback(hObject, eventdata, handles)
4544%------------------------------------------------------------------------
4545if isequal(get(handles.test_stereo1,'Value'),0)
4546    set(handles.subdomain_patch1,'Visible','on')
4547    set(handles.rho_patch1,'Visible','on')
4548else
4549    set(handles.subdomain_patch1,'Visible','off')
4550    set(handles.rho_patch1,'Visible','off')
4551end
4552
4553%------------------------------------------------------------------------
4554% --- Executes on button press in test_stereo2.
4555function test_stereo2_Callback(hObject, eventdata, handles)
4556%------------------------------------------------------------------------
4557if isequal(get(handles.test_stereo2,'Value'),0)
4558    set(handles.subdomain_patch2,'Visible','on')
4559    set(handles.rho_patch2,'Visible','on')
4560else
4561    set(handles.subdomain_patch2,'Visible','off')
4562    set(handles.rho_patch2,'Visible','off')
4563end
4564
4565%------------------------------------------------------------------------
4566% --- Executes on button press in ImaThreshold.
4567function ImaThreshold_Callback(hObject, eventdata, handles)
4568%------------------------------------------------------------------------
4569if isequal(get(handles.ImaThreshold,'Value'),1)
4570    set(handles.MinIma,'Visible','on')
4571    set(handles.MaxIma,'Visible','on')
4572else
4573    set(handles.MinIma,'Visible','off')
4574    set(handles.MaxIma,'Visible','off')
4575end
4576
4577%------------------------------------------------------------------------
4578% --- Executes on button press in ImaThreshold2.
4579function ImaThreshold2_Callback(hObject, eventdata, handles)
4580%------------------------------------------------------------------------
4581if isequal(get(handles.ImaThreshold2,'Value'),1)
4582    set(handles.MinIma2,'Visible','on')
4583    set(handles.MaxIma2,'Visible','on')
4584else
4585    set(handles.MinIma2,'Visible','off')
4586    set(handles.MaxIma2,'Visible','off')
4587end
4588
4589
4590% --- Executes on button press in TestCiv1.
4591function TestCiv1_Callback(hObject, eventdata, handles)
4592test_civ1=get(handles.TestCiv1,'Value');
4593if test_civ1
4594[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]=...
4595    set_civ_filenames(handles,[1 0 0 0 0 0]);
4596Data.ListVarName={'ny','nx','A'};
4597Data.VarDimName={'ny','nx',{'ny','nx'}};
4598Data.A=imread(filecell.ima1.civ1{1});
4599Data.ny=[size(Data.A,1) 1];
4600Data.nx=[1 size(Data.A,2)];
4601hh=view_field(Data);
4602ViewData=get(hh,'UserData');
4603ViewData.axes3.B=imread(filecell.ima2.civ1{1});
4604set(hh,'UserData',ViewData)
4605end
4606
4607%-------------------------------------------------------------------
4608% --- Executes on button press in status.
4609function status_Callback(hObject, eventdata, handles)
4610%-------------------------------------------------------------------
4611val=get(handles.status,'Value');
4612if val==0
4613    hfig=findobj(allchild(0),'name','civ_status');
4614    if ~isempty(hfig)
4615        delete(hfig)
4616    end
4617    return
4618end
4619listtype={'civ1','fix1','patch1','civ2','fix2','patch2'};
4620box_test(1)=get(handles.CIV1,'Value');
4621box_test(2)=get(handles.FIX1,'Value');
4622box_test(3)=get(handles.PATCH1,'Value');
4623box_test(4)=get(handles.CIV2,'Value');
4624box_test(5)=get(handles.FIX2,'Value');
4625box_test(6)=get(handles.PATCH2,'Value');
4626option=listtype{max(box_test(find(box_test)))};
4627filecell=get(handles.civ,'UserData');
4628if ~isfield(filecell,'nc')
4629    filecell=set_civ_filenames(handles,box_test);%determine the list of output files expected from the GUI status
4630end
4631
4632if ~isequal(box_test(4:6),[0 0 0])
4633    civ_files=filecell.nc.civ2;
4634else
4635    civ_files=filecell.nc.civ1;
4636end
4637[root,filename,ext]=fileparts(civ_files{1});
4638[rootroot,subdir,extdir]=fileparts(root);
4639hfig=findobj(allchild(0),'name','civ_status');
4640if isempty(hfig)
4641    hfig=figure;
4642    set(hfig,'name','civ_status')
4643    hlist=uicontrol('Style','listbox','Units','normalized', 'Position',[0.05 0.05 0.9 0.75], 'Callback', @open_view_field,'tag','list');
4644    uicontrol('Style','listbox','Units','normalized', 'Position', [0.05 0.87 0.9 0.1],'tag','msgbox');
4645    uicontrol('Style','frame','Units','normalized', 'Position', [0.05 0.81 0.9 0.05]);
4646    BarPosition=[0.05 0.81 0.01 0.05];
4647    hwaitbar=uicontrol('Style','frame','Units','normalized', 'Position',BarPosition ,'BackgroundColor',[1 0 0],'tag','waitbar');
4648end
4649datnum=[];
4650Tabchar={};
4651nbfiles=numel(civ_files);
4652count=0;
4653while count<nbfiles
4654    count=0;
4655    for ifile=1:nbfiles
4656        detect=exist(civ_files{ifile},'file'); % check the existence of the file
4657        if detect==0
4658            lastfield='not created';
4659        else
4660            datfile=dir(civ_files{ifile});
4661            datnum(ifile)=datenum(datfile.date);
4662            filefound(ifile)={datfile.name};
4663            lastfield='';
4664            % check the content  netcdf file
4665            Data=nc2struct(civ_files{ifile},'ListGlobalAttribute','patch2','fix2','civ2','patch','fix');
4666            if ~isempty(Data.patch2) && isequal(Data.patch2,1)
4667                lastfield='patch2';
4668            elseif ~isempty(Data.fix2) && isequal(Data.fix2,1)
4669                lastfield='fix2';
4670            elseif ~isempty(Data.civ2) && isequal(Data.civ2,1);
4671                lastfield='civ2';
4672            elseif ~isempty(Data.patch) && isequal(Data.patch,1);
4673                lastfield='patch1';
4674            elseif ~isempty(Data.fix) && isequal(Data.fix,1);
4675                lastfield='fix1';
4676            else
4677                lastfield='civ1';
4678            end
4679        end
4680        if strcmp(lastfield,option)
4681            count=count+1;
4682        end
4683        [rr,filename,ext]=fileparts(civ_files{ifile});
4684        Tabchar{ifile,1}=[fullfile([subdir extdir],filename) ext  '...' lastfield];
4685    end
4686    if isempty(datnum)
4687         message='no civ result created yet';
4688    else
4689        datnum=datnum(find(datnum));%keep the non zero values corresponding to existing files
4690        [first,ind]=min(datnum);
4691        [last,indlast]=max(datnum);
4692        message={[num2str(count) ' file(s) done over ' num2str(nbfiles)] ;['oldest modification:  ' cell2mat(filefound(ind)) ' : ' datestr(first)];...
4693            ['latest modification:  ' cell2mat(filefound(indlast)) ' : ' datestr(last)]};
4694    end
4695    hfig=findobj(allchild(0),'name','civ_status');
4696    if isempty(hfig)
4697        set(handles.status,'Value',0)%stop program if the figure has been suppressed
4698        return
4699    else
4700        hlist=findobj(hfig,'tag','list');
4701        hmsgbox=findobj(hfig,'tag','msgbox');
4702        hwaitbar=findobj(hfig,'tag','waitbar');
4703        set(hlist,'String',Tabchar)
4704        set(hmsgbox,'String', message)
4705        if count>0
4706            BarPosition(3)=0.9*count/nbfiles;
4707            set(hwaitbar,'Position',BarPosition)
4708        end
4709    end
4710    set(hlist,'UserData',rootroot)
4711    pause(5)% wait 5 seconds for next check
4712end
4713
4714   
4715%-------------------------------------------------------------------   
4716% call 'view_field.fig' to display the selected field
4717function open_view_field(hObject, eventdata)
4718%-------------------------------------------------------------------
4719     list=get(gcbo,'String');
4720     index=get(gcbo,'Value');
4721     rootroot=get(gcbo,'UserData');
4722     filename=list{index};
4723     ind_dot=findstr(filename,'...');
4724     filename=filename(1:ind_dot-1);
4725      filename=fullfile(rootroot,filename)
4726      if exist(filename,'file')%visualise the vel field if it exists
4727        %[Field,VelTypeOut]=read_civxdata(filename);
4728        %view_field(Field)
4729        uvmat(filename)
4730        set(gcbo,'Value',1)
4731      end
4732
4733
Note: See TracBrowser for help on using the repository browser.