source: trunk/src/civ.m @ 219

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

civ: put job_list.bat in executable mode (it was removed during tests with mac)
needed to do civ in run mode

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