source: trunk/src/civ.m @ 230

Last change on this file since 230 was 230, checked in by sommeria, 14 years ago

gestion of .civ1;cmx , .civ2.cmx clarified

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