source: trunk/src/civ.m @ 269

Last change on this file since 269 was 269, checked in by gostiaux, 12 years ago

batch_file_list included in launch_jobs

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