source: trunk/src/civ.m @ 180

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

rationalisation of uvmat, introduction of the new function read_field, links with get_field, several bug repairs

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