source: trunk/src/civ.m @ 225

Last change on this file since 225 was 225, checked in by sommeria, 10 years ago

civ : bug in binary check repaired. Introduction of the option PIVlab for PIV (still under test)

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