source: trunk/src/civ.m @ 236

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

correct Matlab PIV, remove call to image tool box. Improve menu of uvmat VelType? (replacement of buttons)

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