source: trunk/src/civ.m @ 227

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

add function sub_field_series to apply the sub_field operation to a series of fileds (for instance subtracting a background to an image series)

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