source: trunk/src/civ.m @ 275

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

further cleaning after new GUI civ

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