source: trunk/src/civ.m @ 270

Last change on this file since 270 was 270, checked in by sommeria, 12 years ago

minor bug repairs

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