source: trunk/src/civ.m @ 371

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

various bug fixes with file indexing. mode displacement from a fixed image introduced.
PIV stereo suppressed (todo: introduce in the GUI series).
find_file_series cleaned, with a change in the function arguments

File size: 185.2 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 menucivx
4% function varargout = civ(varargin)
5% provides an interface for the software menucivx
6%
7%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
8%  Copyright Joel Sommeria, 2011, LEGI / CNRS-UJF-INPG, sommeria@legi.grenoble-inp.fr
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 14-Jan-2012 00:46:29
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, fileinput)
48%------------------------------------------------------------------------
49% This function has no output args, see OutputFcn.
50
51%% General settings
52handles.output = hObject;
53guidata(hObject, handles); % Update handles structure
54set(hObject,'WindowButtonDownFcn',{'mouse_down'}) % allows mouse action with right button (zoom for uicontrol display)
55
56%% Adjust the GUI according to the binaries available in PARAM.xml
57path_civ=fileparts(which('civ')); %path to civ
58addpath (path_civ) ; %add the path to civ, (useful in case of change of working directory after civ has been s opened in the working directory)
59errormsg=[];%default error message
60xmlfile='PARAM.xml';
61if exist(xmlfile,'file')
62    try
63        t=xmltree(xmlfile);
64        sparam=convert(t);
65    catch
66        errormsg={' Unable to read the file PARAM.xml defining the civx binaries:'; lasterr};
67    end
68else
69    errormsg=[xmlfile ' not found: path to civx binaries undefined'];
70end
71if ~isempty(errormsg)
72    msgbox_uvmat('WARNING',errormsg);
73end
74test_batch=0;%default: ,no batch mode available
75if isfield(sparam,'BatchParam') && isfield(sparam.BatchParam,'BatchMode')
76    test_batch=strcmp(sparam.BatchParam.BatchMode,'sge'); %sge is currently the only implemented batch mod
77end
78if test_batch==0
79    set(handles.BATCH,'Enable','off')% put the BATCH button in grey (unactivated)
80    set(handles.BATCH,'BackgroundColor',[0.831 0.816 0.784])% put the BATCH button in grey (unactivated)
81end
82if isfield(sparam.RunParam,'CivBin')
83    if ~exist(sparam.RunParam.CivBin,'file')
84        sparam.RunParam.CivBin=fullfile(path_civ,sparam.RunParam.CivBin);
85    end
86else
87    sparam.RunParam.CivBin='';
88end
89
90%% load the list of previously browsed files in the upper bar menu Open/
91dir_perso=prefdir; % path to the directory .matlab for personal data
92profil_perso=fullfile(dir_perso,'uvmat_perso.mat');% personal data file uvmauvmat_perso.mat' in .matlab
93if exist(profil_perso,'file')
94    h=load (profil_perso);
95    if isfield(h,'MenuFile')
96        for ifile=1:min(length(h.MenuFile),5)
97            eval(['set(handles.MenuFile_' num2str(ifile) ',''Label'',h.MenuFile{ifile});'])
98        end
99    end
100end
101
102%% prepare the GUI with parameters from the input file if opened from uvmat
103if exist('fileinput','var')% && isfield(param,'RootName') && ~isempty(param.RootName)
104    set(handles.RootPath,'BackgroundColor',[1 1 0])%paint RootName edit box in yellow to indicate that the file input is proceeding
105    errormsg=display_file_name(handles,fileinput);
106    if ~isempty(errormsg)
107        msgbox_uvmat('ERROR',errormsg)
108    end
109    set(handles.RootPath,'BackgroundColor',[1 1 1])%paint RootName back to white to indicate that the file input is finished
110end
111
112%------------------------------------------------------------------------
113% --- Outputs from this function are returned to the command line.
114function varargout = civ_OutputFcn(hObject, eventdata, handles)
115%------------------------------------------------------------------------
116% Get default command line output from handles structure
117varargout{1} = handles.output;
118
119%------------------------------------------------------------------------
120% --- Function activated by the Open/Browse... option in the upper menu bar.
121function MenuBrowse_Callback(hObject, eventdata, handles)
122%------------------------------------------------------------------------
123%% get the current input root file name to initiate the browser
124filebase=get(handles.RootPath,'String');
125oldfile=''; %default
126if isempty(filebase)|| isequal(filebase,'')%loads the previously stored root file name
127    dir_perso=prefdir;
128    profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
129    if exist(profil_perso,'file')
130        h=load (profil_perso);
131        if isfield(h,'filebase')&& ischar(h.filebase)
132            oldfile=h.filebase;
133        end
134        if isfield(h,'RootPath') && ischar(h.RootPath)
135            oldfile=h.RootPath;
136        end
137    end
138else
139    oldfile=filebase;
140end
141
142%% get the new input file with the browser
143menu={'*.xml;*.civ;*.png;*.jpg;*.tif;*.avi;*.AVI;*.nc;', ' (*.xml,*.civ,*.png,*.jpg ,.tif, *.avi,*.nc)';
144    '*.xml',  '.xml files '; ...
145    '*.civ',  '.civ files '; ...
146    '*.png','.png image files'; ...
147    '*.jpg',' jpeg image files'; ...
148    '*.tif','.tif image files'; ...
149    '*.avi;*.AVI','.avi movie files'; ...
150    '*.nc','.netcdf files'; ...
151    '*.*',  'All Files (*.*)'};
152[FileName, PathName, filtindex] = uigetfile( menu, 'Pick a file',oldfile);
153fileinput=[PathName FileName];%complete file name
154sizf=size(fileinput);
155if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
156
157%% case of the xml file opened as input (TODO: check and see whether it is useful)
158[path,name,ext]=fileparts(fileinput);
159testeditxml=0;
160% if isequal(ext,'.xml')
161%     testeditxml=1;
162%     t_browse=xmltree(fileinput);
163%     head_element=get(t_browse,1);
164%     if isfield(head_element,'name')&& isequal(head_element.name,'ImaDoc')
165%         testeditxml=0;
166%     end
167% end
168% if testeditxml==1 || isequal(ext,'.xls')
169%     heditxml=editxml({fileinput});
170%     set(heditxml,'Tag','browser')
171%     waitfor(heditxml,'Tag','idle')
172%     if ~ishandle(heditxml)
173%         return
174%     end
175%     attr=findobj(get(heditxml,'children'),'Tag','CurrentAttributes');
176%     set(handles.browse,'UserData',fileinput)% store for future opening with browser
177%     fileinput=get(attr,'UserData');
178%     if ~exist(fileinput,'file')
179%         return
180%     end
181% end
182[tild,tild,tild,i1,i2,j1,j2,FileExt,NomType]=fileparts_uvmat(fileinput);
183
184%% prepare the GUI with parameters from the input file
185set(handles.RootPath,'BackgroundColor',[1 1 0])%paint RootName edit box in yellow to indicate that the file input is proceeding
186errormsg=display_file_name(handles,fileinput);
187if ~isempty(errormsg)
188    msgbox_uvmat('ERROR',erromsg)
189end
190set(handles.RootPath,'BackgroundColor',[1 1 1])%paint RootName back to white to indicate that the file input is finished
191
192%------------------------------------------------------------------------
193% --- Open again the file whose name has been recorded in MenuFile_1
194function MenuFile_1_Callback(hObject, eventdata, handles)
195%------------------------------------------------------------------------
196set(handles.RootPath,'BackgroundColor',[1 1 0])%paint RootName edit box in yellow to indicate that the file input is proceeding
197fileinput=get(handles.MenuFile_1,'Label');
198errormsg=display_file_name(handles,fileinput);
199if ~isempty(errormsg)
200    msgbox_uvmat('ERROR',errormsg)
201end
202set(handles.RootPath,'BackgroundColor',[1 1 1])%paint RootName back to white to indicate that the file input is finished
203
204% -----------------------------------------------------------------------
205% --- Open again the file whose name has been recorded in MenuFile_2
206function MenuFile_2_Callback(hObject, eventdata, handles)
207%------------------------------------------------------------------------
208set(handles.RootPath,'BackgroundColor',[1 1 0])%paint RootName edit box in yellow to indicate that the file input is proceeding
209fileinput=get(handles.MenuFile_2,'Label');
210errormsg=display_file_name(handles,fileinput);
211if ~isempty(errormsg)
212    msgbox_uvmat('ERROR',errormsg)
213end
214set(handles.RootPath,'BackgroundColor',[1 1 1])%paint RootName back to white to indicate that the file input is finished
215
216% -----------------------------------------------------------------------
217% --- Open again the file whose name has been recorded in MenuFile_3
218function MenuFile_3_Callback(hObject, eventdata, handles)
219%------------------------------------------------------------------------
220set(handles.RootPath,'BackgroundColor',[1 1 0])%paint RootName edit box in yellow to indicate that the file input is proceeding
221fileinput=get(handles.MenuFile_3,'Label');
222errormsg=display_file_name(handles,fileinput);
223if ~isempty(errormsg)
224    msgbox_uvmat('ERROR',errormsg)
225end
226set(handles.RootPath,'BackgroundColor',[1 1 1])%paint RootName back to white to indicate that the file input is finished
227
228% -----------------------------------------------------------------------
229% --- Open again the file whose name has been recorded in MenuFile_4
230function MenuFile_4_Callback(hObject, eventdata, handles)
231%------------------------------------------------------------------------
232set(handles.RootPath,'BackgroundColor',[1 1 0])%paint RootName edit box in yellow to indicate that the file input is proceeding
233fileinput=get(handles.MenuFile_4,'Label');
234errormsg=display_file_name(handles,fileinput);
235if ~isempty(errormsg)
236    msgbox_uvmat('ERROR',errormsg)
237end
238set(handles.RootPath,'BackgroundColor',[1 1 1])%paint RootName back to white to indicate that the file input is finished
239
240% -----------------------------------------------------------------------
241% --- Open again the file whose name has been recorded in MenuFile_5
242function MenuFile_5_Callback(hObject, eventdata, handles)
243%------------------------------------------------------------------------
244set(handles.RootPath,'BackgroundColor',[1 1 0])%paint RootName edit box in yellow to indicate that the file input is proceeding
245fileinput=get(handles.MenuFile_5,'Label');
246errormsg=display_file_name(handles,fileinput);
247if ~isempty(errormsg)
248    msgbox_uvmat('ERROR',errormsg)
249end
250set(handles.RootPath,'BackgroundColor',[1 1 1])%paint RootName back to white to indicate that the file input is finished
251
252% -----------------------------------------------------------------------
253% -----------------------------------------------------------------------
254% --- Open the help html file
255function MenuHelp_Callback(hObject, eventdata, handles)
256% -----------------------------------------------------------------------
257path_civ=fileparts(which ('civ'));
258helpfile=fullfile(path_civ,'uvmat_doc','uvmat_doc.html');
259if isempty(dir(helpfile))
260    msgbox_uvmat('ERROR','Please put the help file uvmat_doc.html in the sub-directory /uvmat_doc of the UVMAT package')
261else
262    addpath (fullfile(path_civ,'uvmat_doc'))
263    web([helpfile '#civ'])
264end
265
266%------------------------------------------------------------------------
267% --- Function activated when a new filebase (image series) is introduced
268function RootPath_Callback(hObject, eventdata, handles)
269%------------------------------------------------------------------------
270set(handles.RootPath,'BackgroundColor',[1 1 0])%paint RootName edit box in yellow to indicate that the file input is proceeding
271RootPath=get(handles.RootPath,'String');
272RootFile=get(handles.RootFile,'String');
273ref_i=str2num(get(handles.ref_i,'String'));
274ref_j=str2num(get(handles.ref_j,'String'));
275NomType=get(handles.NomType,'String');
276ImaExt=get(handles.ImaExt,'String');
277fileinput=fullfile_uvmat(RootPath,'',RootFile,ImaExt,NomType,ref_i,[],ref_j);
278errormsg=display_file_name(handles,fileinput);
279if ~isempty(errormsg)
280    msgbox_uvmat('ERROR',errormsg)
281end
282set(handles.RootPath,'BackgroundColor',[1 1 1])%paint RootName back to white to indicate that the file input is finished
283%------------------------------------------------------------------------
284% --- general function activated for an input file series
285function errormsg=display_file_name(handles,fileinput)
286%------------------------------------------------------------------------
287set(handles.ListCompareMode,'Visible','on')
288errormsg='';%default empty error message
289drawnow
290
291%% enable RUN, BATCH button and 'status' display
292set(handles.RUN, 'Enable','On')
293set(handles.RUN,'BackgroundColor',[1 0 0])%set RUN button to red color
294set(handles.BATCH,'Enable','On')
295set(handles.BATCH,'BackgroundColor',[1 0 0])%set BATCH button to red color
296if isfield(handles,'status')
297    set(handles.status,'Value',0);       %suppress the 'status' display
298    status_Callback([], [], handles)
299end
300
301%% determine nomenclature types and extension of the input files
302[RootPath,SubDir,RootFile,i1,i2,j1,j2,ExtInput,NomTypeInput]=fileparts_uvmat(fileinput);
303NomTypeNc='';%default
304
305%% case of netcdf file as input, look for a coresponding image
306ind_opening=0;%default
307if strcmp(ExtInput,'.xml')
308    Param=xml2struct(fileinput);
309    fill_GUI(Param,handles);%fill the GUI with the parameters retrieved from the xml file
310    return
311end
312if strcmp(ExtInput,'.nc')
313    NomTypeNc=NomTypeInput;
314    if isempty(regexp(NomTypeInput,'[ab|AB|-]'))
315        set(handles.ListCompareMode,'Value',2) %mode displacement advised if the nomencalture does not involve index pairs
316        [RootPath,SubDir]=fileparts(RootPath);
317    set(handles.RootFile_1,'Visible','On');
318    else
319         set(handles.ListCompareMode,'Value',1)
320         set(handles.RootFile_1,'Visible','Off');
321    end
322    Data=nc2struct(fileinput,'ListGlobalAttribute','Conventions','absolut_time_T0','CivStage','Civ2_ImageA','Civ1_ImageA','Civ2_ImageB','Civ1_ImageB','fix','patch','civ2','fix2');
323    if isfield(Data,'Txt')
324        errormsg=Data.Txt;
325        return
326    end
327    if strcmp(Data.Conventions,'uvmat/civdata')% case of new civ data,
328        set(handles.ListProgram,'Value',2) %select civ/Matlab by default
329        ListProgram_Callback([],[], handles)
330        if ~isempty(Data.CivStage)%test for civ files
331            ind_opening=Data.CivStage;
332        end
333        if  ~isempty(Data.Civ2_ImageB)%get the corresponding input image in the netcdf file
334            imageinput=Data.Civ2_ImageB;
335            [tild,ImaName,ImaExt]=fileparts(Data.Civ2_ImageA);
336            set(handles.RootFile_1,'String',[ImaName ImaExt])
337        elseif ~isempty(Data.Civ1_ImageB)
338            imageinput=Data.Civ1_ImageB;
339            [tild,ImaName,ImaExt]=fileparts(Data.Civ1_ImageA);
340            set(handles.RootFile_1,'String',[ImaName ImaExt])
341        end
342       
343    elseif ~isempty(Data.absolut_time_T0')% case of  civx data,
344        set(handles.ListProgram,'Value',1) %select Cix by default
345        ListProgram_Callback([],[], handles)
346        if ~isempty(Data.fix2)
347            ind_opening=5;
348        elseif ~isempty(Data.civ2)
349            ind_opening=4;
350        elseif ~isempty(Data.patch)
351            ind_opening=3;
352        elseif ~isempty(Data.fix)
353            ind_opening=2;
354        end
355        % look for the corresponding input images
356        check_letter=~isempty(regexp(NomTypeInput,'[ab|AB]$'));%detect pair label by letter
357        NomTypeIma=NomTypeInput;
358        if check_letter
359            NomTypeIma=NomTypeInput(1:end-1);
360        else
361            r=regexp(NomTypeIma,'.-(?<num2>\d+)$','names');
362            if ~isempty(r)
363                NomTypeIma=regexprep(NomTypeIma,['-' r.num2],'');
364            end
365            r=regexp(NomTypeIma,'.-(?<num2>\d+)','names');
366            if ~isempty(r)
367                NomTypeIma=regexprep(NomTypeIma,['-' r.num2],'');
368            end
369        end
370        imageinput=fullfile_uvmat(RootPath,'',RootFile,'.png',NomTypeIma,i1,[],j1);
371    else
372        errormsg='the input netcdf file is not civ data';
373        return
374    end
375end
376
377%no corresponding image found, select manually with the browser
378ImaExt=ExtInput;
379if ~isempty(NomTypeNc)
380    %no corresponding image found, select manually with the browser
381    if ~exist(imageinput,'file')
382        menu={'*.png;*.jpg;*.tif;*.avi;*.AVI', '(*.png,*.jpg ,*.tif, *.avi,*.AVI)';
383            '*.png','.png image files'; ...
384            '*.jpg',' jpeg image files'; ...
385            '*.tif','.tif image files'; ...
386            '*.avi;*.AVI','.avi movie files'; ...
387            '*.*',  'All Files (*.*)'};
388        [FileName, PathName] = uigetfile( menu, 'Pick an input image file',fileparts(fileparts(fileinput)));
389        imageinput=[PathName FileName];%complete file name
390     
391    end   
392    fileinput=imageinput;
393end
394
395%% scan the image file series
396[RootPath,FileName,ImaExt]=fileparts(fileinput);
397[RootFile,i1_series,tild,j1_series,tild,NomTypeIma,FileType,Object]=find_file_series(RootPath,[FileName ImaExt]);
398if strcmp(NomTypeInput,'*')% movies will be opened at the first frame
399    i1=1;
400    i2=[];
401    j1=[];
402    j2=[];
403end
404switch FileType
405    case {'image','multimage','video'}
406    otherwise
407        errormsg='invalid input file: enter an image, a movie or civ .nc file';
408        return
409end
410% RootName=fullfile(RootPath,RootFile);
411set(handles.RootPath,'String',RootPath)
412set(handles.RootFile,'String',RootFile)
413browse=get(handles.RootPath,'UserData');
414% browse.nom_type_nc=NomTypeNc;
415browse.incr_pair=[0 0];%default
416
417%% fill reference indices from the input file indices
418num_ref_i=i1;%efaulmt ref index
419if ~isempty(i2)
420    num_ref_i=floor((num_ref_i+i2)/2);
421end
422num_ref_j=j1;
423if ~isempty(j2)
424    num_ref_j=floor((num_ref_j+j2)/2);
425end
426
427%% scan the images if a civ file has been opened
428% if ~isempty(NomTypeNc)
429% [tild,i1_series,tild,j1_series,tild,NomTypeIma,ImageType,Object]=find_file_series(RootPath,RootFile);
430% end
431MinIndex_i=min(i1_series(i1_series>0));
432MinIndex_j=min(j1_series(j1_series>0));
433MaxIndex_i=max(i1_series(i1_series>0));
434MaxIndex_j=max(j1_series(j1_series>0));
435
436%% look for an image documentation file
437ext_imadoc='';%default
438RootName=fullfile(RootPath,RootFile);
439if exist([RootName '.xml'],'file')
440    ext_imadoc='.xml';
441elseif exist([RootName '.civxml'],'file')
442    ext_imadoc='.civxml';
443elseif exist([RootName '.civ'],'file')
444    ext_imadoc='.civ';
445elseif exist([RootName '.avi'],'file')
446    ext_imadoc='.avi';
447elseif exist([RootName '.AVI'],'file')
448    ext_imadoc='.AVI';
449end
450set(handles.ImaDoc,'String',ext_imadoc)% display the extension name for the image documentation file used
451
452%%  read the time in the image documentation file 
453mode=''; %default
454time=[];
455TimeUnit=''; %default
456CoordUnit='';%default
457pxcmx_search=[];%default
458pxcmy_search=[];%default
459if ~isempty(ext_imadoc)
460    set(handles.ImaDoc,'BackgroundColor',[1 1 0]) % set edit box to yellow cloro to indicate that the file reading is beginning
461    drawnow
462    switch ext_imadoc
463        case '.civxml'%OBSOLETE
464            [tild,tild,time]=read_civxml([RootName '.civxml']);
465            mode='pair j1-j2';
466            if isempty(nom_type_ima)% dtermine types by default if not already selected by browser or uvmat
467                nom_type_ima='_i_j';
468            end
469        case '.xml'
470            [XmlData,warntext]=imadoc2struct([RootName '.xml']);
471            ext_ima_read=[];
472            nom_type_read=[];
473            if isfield(XmlData,'Heading')&&isfield(XmlData.Heading','ImageName')&&ischar(XmlData.Heading.ImageName)% get image nom type and extension from the xml file
474                %[PP,FF,fc,str2,str_a,str_b,ext_ima_read,nom_type_read]=name2display(XmlData.Heading.ImageName);
475                [tild,tild,tild,tild,tild,tild,tild,tild,nom_type_read]=fileparts_uvmat(XmlData.Heading.ImageName);
476                fullname=fullfile(fileparts(RootName),XmlData.Heading.ImageName); %full name (including path) of the first image defined by the xmle file,
477                if ~exist(fullname,'file')
478                    msgbox_uvmat('WARNING',['FirstImage ' fullname ' defined in the xml file does not exist'])
479                end
480            end
481            if isfield(XmlData,'Time')
482                time=XmlData.Time;
483                %transform .Time to a column vector if it is a line vector thenomenclature uses a single index: correct possible bug in xml
484                if isequal(MaxIndex_i,1) && ~isequal(MaxIndex_j,1)% .Time is a line vector
485                    if numel(nom_type_read)>=2 && isempty(regexp(nom_type_read(2:end),'\D','once'))
486                        time=time';
487                        MaxIndex_i=MaxIndex_j;
488                        MaxIndex_j=1;
489                    end
490                end
491            end
492            if isfield(XmlData,'TimeUnit')
493                TimeUnit=XmlData.TimeUnit;
494            end
495            pxcmx_search=1;
496            pxcmy_search=1;
497            if isfield(XmlData,'GeometryCalib')
498                tsai=XmlData.GeometryCalib;
499                if isfield(tsai,'f') && isfield(tsai,'Tz') && isfield(tsai,'dpx') && isfield(tsai,'dpy')&& isfield(tsai,'R')
500                    rot2D=tsai.R(1:2,[1,2]);
501                    pxcmx_search=tsai.f * sqrt(det(rot2D))/(tsai.Tz*tsai.dpx);
502                    pxcmy_search=tsai.f * sqrt(det(rot2D))/(tsai.Tz*tsai.dpy);
503                end
504                if isfield(tsai,'CoordUnit')
505                    CoordUnit=tsai.CoordUnit;
506                end
507            end
508        case '.civ'% OBSOLETE: case of .civ image documentation file
509            [error,time,TimeUnit,mode,npx,npy]=read_imatext([RootName '.civ']);
510            if error==2, msgbox_uvmat('WARNING',['no file ' RootName '.civ']);
511            elseif error==1, msgbox_uvmat('WARNING','inconsistent number of fields in the .civ file');
512            end
513            nom_type_ima='001a';
514        case {'.avi','.AVI'}
515            nom_type_ima='*';
516            ImaExt=ext_imadoc;
517            set(handles.ListPairMode,'Value',1);
518            set(handles.ListPairMode,'String',{'series(Di)'})
519            dt=0.04;%default
520            if exist([RootName ext_imadoc],'file')==2
521                info=aviinfo([RootName ext_imadoc]);%read infos on the avi movie
522                dt=1/info.FramesPerSecond;%time interval between successive frames
523                MaxIndex_i=info.NumFrames;%number of frames
524            end
525            time=(dt*(0:MaxIndex_i-1))';%list of image times
526            TimeUnit='s';
527    end
528    set(handles.ImaDoc,'BackgroundColor',[1 1 1])% set display box back to whiter
529end
530
531%% timing display
532%show the reference image edit box if relevant (not needed for movies or in the absence of time information
533if numel(time)>=2 % if there are at least two time values to define dt
534    MaxIndex_i=min(size(time,1),MaxIndex_i);
535    MaxIndex_j=min(size(time,2),MaxIndex_j);
536    time=[zeros(size(time,1),1) time]; %insert a vertical line of zeros (to deal with zero file indices)
537    time=[zeros(1,size(time,2)); time]; %insert a horizontal line of zeros
538    set(handles.ImaDoc,'UserData',time); %store the matrix of times
539    set(handles.dt_unit,'String',['dt in m' TimeUnit]);
540    set(handles.TimeUnit,'String',TimeUnit);
541else
542    set(handles.ImaDoc,'String',''); %xml file not used for timing
543    time=(i1_series(:,1)+0:size(i1_series,1)-1);% time=index i
544    time=time'*ones(1,size(i1_series,2),1); %makes a time matrix with the same time for all j indices
545    TimeUnit='frame';
546end
547set(handles.ImaDoc,'UserData',time); %store the matrix of times
548set(handles.dt_unit,'String',['dt in m' TimeUnit]);%display dt in unit 10-3 of the time (e.g ms)
549set(handles.TimeUnit,'String',TimeUnit);
550set(handles.nb_field,'String',num2str(MaxIndex_i));
551set(handles.nb_field2,'String',num2str(MaxIndex_j));
552set(handles.CoordUnit,'String',CoordUnit)
553set(handles.SearchRange,'UserData',[pxcmx_search pxcmy_search]);
554set(handles.ImaExt,'String',ImaExt)
555set(handles.NomType,'String',NomTypeIma)
556set(handles.ref_i,'String',num2str(num_ref_i))
557set(handles.ref_j,'String',num2str(num_ref_j))
558
559%% update i and j index range if a nc file has been opened or pb withmin max image indices:
560% then set first and last to the inputfile index by default
561first_i=str2num(get(handles.first_i,'String'));
562last_i=str2num(get(handles.last_i,'String'));
563if isempty(first_i) || isempty(last_i)||isempty(MinIndex_i)||isempty(MaxIndex_i)
564    set(handles.first_i,'String',num2str(num_ref_i));
565    set(handles.last_i,'String',num2str(num_ref_i));%
566end
567if ind_opening~=0 || isempty(first_i) || isempty(last_i)|| first_i<MinIndex_i || last_i>MaxIndex_i
568set(handles.first_i,'String',num2str(num_ref_i));
569set(handles.last_i,'String',num2str(num_ref_i));%
570end
571
572%j index range
573first_j=str2num(get(handles.first_j,'String'));
574last_j=str2num(get(handles.last_i,'String'));
575if isempty(first_j) || isempty(last_j)||isempty(MinIndex_j)||isempty(MaxIndex_j)
576    set(handles.first_j,'String',num2str(num_ref_j));
577    set(handles.last_j,'String',num2str(num_ref_j));%
578elseif ind_opening~=0 || first_j<MinIndex_j || last_j>MaxIndex_j
579    set(handles.first_j,'String',num2str(num_ref_j));
580set(handles.last_j,'String',num2str(num_ref_j));%
581end
582
583%% set the civ options depending on the input file content when a nc file has been opened
584ListOptions={'CheckCiv1', 'CheckFix1' 'CheckPatch1', 'CheckCiv2', 'CheckFix2', 'CheckPatch2'};
585if ind_opening~=0
586    for index = 1:ind_opening
587        set(handles.(ListOptions{index}),'value',0)
588    end
589end
590for index = ind_opening+2:6
591    set(handles.(ListOptions{index}),'value',0)
592end
593set(handles.(ListOptions{min(ind_opening+1,6)}),'value',1)
594update_CivOptions(handles,1)
595
596%%  set the menus of image pairs and default selection for civ   %%%%%%%%%%%%%%%%%%%
597%check_letter=~isempty(regexp(NomTypeIma,'[ab|AB]$'));%detect pair label by letter
598if  isequal(NomTypeNc,'_1-2')||isempty(MaxIndex_j)|| (MaxIndex_j==1)
599    set(handles.ListPairMode,'Value',1)
600    set(handles.ListPairMode,'String',{'series(Di)'})   
601elseif  MaxIndex_i==1 && MaxIndex_j>1% simple series in j
602    set(handles.ListPairMode,'String',{'pair j1-j2';'series(Dj)'})
603    if  MaxIndex_j <= 10
604        set(handles.ListPairMode,'Value',1)% advice 'pair j1-j2' except in MaxIndex_j is large
605    end
606else
607    set(handles.ListPairMode,'String',{'pair j1-j2';'series(Dj)';'series(Di)'})%multiple choice
608    if strcmp(NomTypeNc,'_1-2_1')
609        set(handles.ListPairMode,'Value',3)% advise 'series(Di)'
610    elseif  MaxIndex_j <= 10
611        set(handles.ListPairMode,'Value',1)% advice 'pair j1-j2' except in MaxIndex_j is large
612    end
613end
614
615%% scan files to update the subdirectory list display
616listot=dir(RootPath);%directory of RootPath
617idir=0;
618listdir={''};%default
619% get the list of existing civ subdirectories in the path of theinput root  file
620for ilist=1:length(listot)
621    if listot(ilist).isdir
622        name=listot(ilist).name;
623        if ~isequal(name,'.') && ~isequal(name,'..')
624            idir=idir+1;
625            listdir{idir,1}=listot(ilist).name;
626        end
627    end
628end
629
630%% update the selection for civ1 and civ2
631if ~isempty(SubDir)% subdir for civ1 and civ2 initiated by the input
632    SubdirCiv1=SubDir;
633    SubdirCiv2=SubDir;
634    set(handles.SubdirCiv1,'String',SubDir)
635    set(handles.SubdirCiv2,'String',SubDir)
636else% currently selected subdir preserved
637    SubdirCiv1=get(handles.SubdirCiv1,'String');
638    SubdirCiv2=get(handles.SubdirCiv2,'String');
639    if isempty(SubdirCiv1)% default subdir name='CIV'
640        set(handles.SubdirCiv1,'String','CIV');
641        SubdirCiv1='CIV';
642    end
643    if isempty(SubdirCiv2)% default subdir name='CIV'
644        set(handles.SubdirCiv2,'String','CIV');
645        SubdirCiv2='CIV';
646    end
647end
648
649%% update the subdirectory menus
650ValueCiv1=find(strcmp(SubdirCiv1,listdir));%search the index of subdir in the cell listdir
651if isempty(ValueCiv1)% if the input subdir is not found
652    ValueCiv1=numel(listdir)+1;%new subdirectory requested for civ1
653end
654ValueCiv2=find(strcmp(SubdirCiv2,listdir));%search the index of subdir in the cell listdir
655if isempty(ValueCiv2)% if the input subdir is not found
656    ValueCiv2=numel(listdir)+1;%new subdirectory requested for civ2
657end
658set(handles.ListSubdirCiv1,'String',[listdir;'new...'])
659set(handles.ListSubdirCiv2,'String',[listdir;'new...'])
660set(handles.ListSubdirCiv1,'Value',ValueCiv1)
661set(handles.ListSubdirCiv2,'Value',ValueCiv2)
662if isempty(listdir)
663    set(handles.SubdirCiv1,'String','CIV')
664    set(handles.SubdirCiv2,'String','CIV')
665end
666
667%% store info
668set(handles.RootPath,'UserData',browse)% store the nomenclature type
669
670%% list the possible index pairs, depending on the option set in ListPairMode
671ListPairMode_Callback([], [], handles)
672
673%% store the root input filename for future opening
674profil_perso=fullfile(prefdir,'uvmat_perso.mat');
675if exist(profil_perso,'file')
676    save (profil_perso,'RootPath','-append'); %store the root name for future opening of uvmat
677else
678    txt=ver('MATLAB');
679    Release=txt.Release;
680    relnumb=str2double(Release(3:4));
681    if relnumb >= 14
682        save (profil_perso,'RootPath','-V6'); %store the root name for future opening of uvmat
683    else
684        save (profil_perso,'RootPath'); %store the root name for future opening of uvmat
685    end
686end
687set(handles.RootPath,'BackgroundColor',[1 1 1])
688
689%------------------------------------------------------------------------
690% --- Executes on carriage return on the subdir checkciv1 edit window
691function SubdirCiv1_Callback(hObject, eventdata, handles)
692%------------------------------------------------------------------------
693SubDir=get(handles.SubdirCiv1,'String');
694menu_str=get(handles.ListSubdirCiv1,'String');% read the list of subdirectories for update
695ichoice=find(strcmp(SubDir,menu_str),1);
696if isempty(ichoice)
697    ilist=numel(menu_str); %select 'new...' in the menu
698else
699    ilist=ichoice;
700end
701set(handles.ListSubdirCiv1,'Value',ilist)% select the selected subdir in the menu
702if get(handles.CheckCiv1,'Value')% if Civ1 is performed
703    set(handles.SubdirCiv2,'String',SubDir);% set by default civ2 directory the same as civ1
704    set(handles.ListSubdirCiv2,'Value',ilist)
705else % if Civ1 data already exist
706    errormsg=find_netcpair_civ(handles,1); %update the list of available pairs from netcdf files in the new directory
707    if ~isempty(errormsg)
708    msgbox_uvmat('ERROR',errormsg)
709    end
710end
711
712%------------------------------------------------------------------------
713% --- Executes on carriage return on the SubDir checkciv1 edit window
714function SubdirCiv2_Callback(hObject, eventdata, handles)
715%------------------------------------------------------------------------
716SubDir=get(handles.SubdirCiv1,'String');
717menu_str=get(handles.ListSubdirCiv2,'String');% read the list of subdirectories for update
718ichoice=find(strcmp(SubDir,menu_str),1);
719if isempty(ichoice)
720    ilist=numel(menu_str); %select 'new...' in the menu
721else
722    ilist=ichoice;
723end
724set(handles.ListSubdirCiv2,'Value',ilist)% select the selected subdir in the menu
725%update the list of available pairs from netcdf files in the new directory
726if ~get(handles.CheckCiv2,'Value') && ~get(handles.CheckCiv1,'Value') && ~get(handles.CheckFix1,'Value') && ~get(handles.CheckPatch1,'Value')
727    errormsg=find_netcpair_civ(handles,2);
728        if ~isempty(errormsg)
729    msgbox_uvmat('ERROR',errormsg)
730    end
731end
732
733%------------------------------------------------------------------------
734% --- Executes on button press in CheckCiv1.
735function CheckCiv1_Callback(hObject, eventdata, handles)
736%------------------------------------------------------------------------
737update_CivOptions(handles,0)
738
739%------------------------------------------------------------------------
740% --- Executes on button press in CheckFix1.
741function CheckFix1_Callback(hObject, eventdata, handles)
742%------------------------------------------------------------------------
743update_CivOptions(handles,0)
744
745%------------------------------------------------------------------------
746% --- Executes on button press in CheckPatch1.
747function CheckPatch1_Callback(hObject, eventdata, handles)
748%------------------------------------------------------------------------
749update_CivOptions(handles,0)
750
751%------------------------------------------------------------------------
752% --- Executes on button press in CheckCiv2.
753function CheckCiv2_Callback(hObject, eventdata, handles)
754%------------------------------------------------------------------------
755update_CivOptions(handles,0)
756
757%------------------------------------------------------------------------
758% --- Executes on button press in CheckFix2.
759function CheckFix2_Callback(hObject, eventdata, handles)
760%------------------------------------------------------------------------
761update_CivOptions(handles,0)
762
763%------------------------------------------------------------------------
764% --- Executes on button press in CheckPatch2.
765function CheckPatch2_Callback(hObject, eventdata, handles)
766%------------------------------------------------------------------------
767update_CivOptions(handles,0)
768
769%------------------------------------------------------------------------
770% --- activated by any checkbox controling the selection of Civ1,Fix1,Patch1,Civ2,Fix2,Patch2
771function update_CivOptions(handles,opening)
772%------------------------------------------------------------------------
773checkbox=zeros(1,6);
774checkbox(1)=get(handles.CheckCiv1,'Value');
775checkbox(2)=get(handles.CheckFix1,'Value');
776checkbox(3)=get(handles.CheckPatch1,'Value');
777checkbox(4)=get(handles.CheckCiv2,'Value');
778checkbox(5)=get(handles.CheckFix2,'Value');
779checkbox(6)=get(handles.CheckPatch2,'Value');
780ind_selected=find(checkbox,1);
781if ~isempty(ind_selected)
782    RootPath=get(handles.RootPath,'String');
783    if isempty(RootPath)
784        msgbox_uvmat('ERROR','Please open an image or PIV .nc file with the upper bar menu Open/Browse...')
785        return
786    end
787end
788set(handles.PairIndices,'Visible','on')
789set(handles.SubdirCiv1,'Visible','on')
790set(handles.ListSubdirCiv1,'Visible','on')
791set(handles.TitleSubdirCiv1,'Visible','on')
792if ~opening
793    errormsg=find_netcpair_civ(handles,1); % select the available netcdf files
794    if ~isempty(errormsg)
795        msgbox_uvmat('ERROR',errormsg)
796    end
797end
798if max(checkbox(4:6))% case of civ2 pair choice needed
799    set(handles.TitlePairCiv2,'Visible','on')
800    set(handles.TitleSubdirCiv2,'Visible','on')
801    set(handles.SubdirCiv2,'Visible','on')
802    set(handles.ListSubdirCiv2,'Visible','on')
803    set(handles.ListPairCiv2,'Visible','on')
804    if ~opening
805        errormsg=find_netcpair_civ(handles,2); % select the available netcdf files
806        if ~isempty(errormsg)
807            msgbox_uvmat('ERROR',errormsg)
808        end
809    end
810else
811    set(handles.TitleSubdirCiv2,'Visible','off')
812    set(handles.SubdirCiv2,'Visible','off')
813    set(handles.ListSubdirCiv2,'Visible','off')
814    set(handles.ListPairCiv2,'Visible','off')
815end
816options={'Civ1','Fix1','Patch1','Civ2','Fix2','Patch2'};
817for ilist=1:length(options)
818    if checkbox(ilist)
819        set(handles.(options{ilist}),'Visible','on')
820    else
821        set(handles.(options{ilist}),'Visible','off')
822    end
823end
824
825%------------------------------------------------------------------------
826% --- Executes on button press in RUN: processing on local computer
827function RUN_Callback(hObject, eventdata, handles)
828%------------------------------------------------------------------------
829set(handles.RUN, 'Enable','Off')
830set(handles.RUN,'BackgroundColor',[0.831 0.816 0.784])
831batch=0;
832errormsg=launch_jobs(hObject, eventdata, handles,batch);
833set(handles.RUN, 'Enable','On')
834set(handles.RUN,'BackgroundColor',[1 0 0])
835
836% display errors or start status callback to visualise results
837if ~isempty(errormsg)
838    display(errormsg)
839    msgbox_uvmat('ERROR',errormsg)
840elseif  isfield(handles,'status') %&& ~isequal(get(handles.ListPairMode,'Value'),3)
841    set(handles.status,'Value',1);%suppress status display
842    status_Callback(hObject, eventdata, handles)
843end
844
845%------------------------------------------------------------------------
846% --- Executes on button press in BATCH: remote processing
847function BATCH_Callback(hObject, eventdata, handles)
848% -----------------------------------------------------------------------
849set(handles.BATCH, 'Enable','Off')
850set(handles.BATCH,'BackgroundColor',[0.831 0.816 0.784])
851batch=1;
852errormsg=launch_jobs(hObject, eventdata, handles, batch);
853set(handles.BATCH, 'Enable','On')
854set(handles.BATCH,'BackgroundColor',[1 0 0])
855
856% display errors or start status callback to visualise results
857if ~isempty(errormsg)
858    display(errormsg)
859    msgbox_uvmat('ERROR',errormsg)
860elseif isfield(handles,'status')
861    set(handles.status,'Value',1);%suppress status display
862    status_Callback(hObject, eventdata, handles)
863end
864
865%-------------------------------------------------------------------
866% --- Executes on button press in status.
867function status_Callback(hObject, eventdata, handles)
868%-------------------------------------------------------------------
869val=get(handles.status,'Value');
870if val==0
871    set(handles.status,'BackgroundColor',[0 1 0])
872    hfig=findobj(allchild(0),'name','civ_status');
873    if ~isempty(hfig)
874        delete(hfig)
875    end
876    return
877end
878set(handles.status,'BackgroundColor',[1 1 0])
879drawnow
880listtype={'civ1','fix1','patch1','civ2','fix2','patch2'};
881Param.CheckCiv1=get(handles.CheckCiv1,'Value');
882Param.CheckFix1=get(handles.CheckFix1,'Value');
883Param.CheckPatch1=get(handles.CheckPatch1,'Value');
884Param.CheckCiv2=get(handles.CheckCiv2,'Value');
885Param.CheckFix2=get(handles.CheckFix2,'Value');
886Param.CheckPatch2=get(handles.CheckPatch2,'Value');
887box_test=[Param.CheckCiv1 Param.CheckFix1 Param.CheckPatch1 Param.CheckCiv2 Param.CheckFix2 Param.CheckPatch2];
888
889option_civ=find(box_test,1,'last');%last selected option (non-zero index of box_test)
890filecell=get(handles.civ,'UserData');%retrieve the list of output files expected for PIV
891test_new=0;
892if ~isfield(filecell,'nc')
893    test_new=1;
894    [ref_i,ref_j,errormsg]=find_ref_indices(handles);
895    if ~isempty(errormsg)
896        msgbox_uvmat('ERROR',errormsg)
897        return
898    end
899    filecell=set_civ_filenames(handles,ref_i,ref_j,box_test);%determine the output file expected from the GUI status
900end
901if ~isequal(box_test(4:6),[0 0 0])
902    civ_files=filecell.nc.civ2;%case of civ2 operations
903else
904    civ_files=filecell.nc.civ1;
905end
906[root,filename,ext]=fileparts(civ_files{1});
907[rootroot,SubDir,extdir]=fileparts(root);
908hfig=findobj(allchild(0),'name','civ_status');
909if isempty(hfig)
910    hfig=figure('DeleteFcn',@stop_status);
911    set(hfig,'name','civ_status')
912     uicontrol('Style','listbox','Units','normalized', 'Position',[0.05 0.09 0.9 0.71], 'Callback', {'open_uvmat'},'tag','list');
913    uicontrol('Style','edit','Units','normalized', 'Position', [0.05 0.87 0.9 0.1],'tag','msgbox','Max',2,'String','checking files...');
914    uicontrol('Style','frame','Units','normalized', 'Position', [0.05 0.81 0.9 0.05]);
915    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);
916    BarPosition=[0.05 0.81 0.01 0.05];
917    hwaitbar=uicontrol('Style','frame','Units','normalized', 'Position',BarPosition ,'BackgroundColor',[1 0 0],'tag','waitbar');
918    drawnow
919end
920% datnum=[];
921Tabchar={};
922nbfiles=numel(civ_files);
923count=0;
924testrecent=0;
925while count<nbfiles
926    count=0;
927    datnum=zeros(1,nbfiles);
928    for ifile=1:nbfiles
929        detect=exist(civ_files{ifile},'file'); % check the existence of the file
930        option=0;
931        if detect==0
932            option_str='not created';
933        else
934            datfile=dir(civ_files{ifile});
935            if isfield(datfile,'datenum')
936                datnum(ifile)=datfile.datenum;%only available in recent matlab versions
937                testrecent=1;
938            end
939            filefound(ifile)={datfile.name};
940            lastfield='';
941            % check the content  netcdf file
942            Data=nc2struct(civ_files{ifile},'ListGlobalAttribute','CivStage','patch2','fix2','civ2','patch','fix');
943            option_list={'civ1','fix1','patch1','civ2','fix2','patch2'};
944            if ~isempty(Data.CivStage)
945                option=Data.CivStage;%case of Matlab civ
946            else
947                if ~isempty(Data.patch2) && isequal(Data.patch2,1)
948                    option=6;
949                elseif ~isempty(Data.fix2) && isequal(Data.fix2,1)
950                    option=5;
951                elseif ~isempty(Data.civ2) && isequal(Data.civ2,1);
952                    option=4;
953                elseif ~isempty(Data.patch) && isequal(Data.patch,1);
954                    option=3;
955                elseif ~isempty(Data.fix) && isequal(Data.fix,1);
956                    option=2;
957                else
958                    option=1;
959                end
960            end
961            option_str=option_list{option};
962        end
963        if option >= option_civ
964            count=count+1;
965        end
966        [rr,filename,ext]=fileparts(civ_files{ifile});
967        Tabchar{ifile,1}=[fullfile([SubDir extdir],filename) ext  '...' option_str];
968    end
969    datnum=datnum(datnum~=0);%keep the non zero values corresponding to existing files
970    if isempty(datnum)
971        if testrecent
972            message='no civ result created yet';
973        else
974            message='';
975        end
976    else
977        datnum=datnum(datnum~=0);%keep the non zero values corresponding to existing files
978        [first,ind]=min(datnum);
979        [last,indlast]=max(datnum);
980        if test_new
981            message='existing file status, no processing launched yet';
982        else
983        message={[num2str(count) ' file(s) done over ' num2str(nbfiles)] ;['oldest modification:  ' cell2mat(filefound(ind)) ' : ' datestr(first)];...
984            ['latest modification:  ' cell2mat(filefound(indlast)) ' : ' datestr(last)]};
985        end
986    end
987    hfig=findobj(allchild(0),'name','civ_status');
988    if isempty(hfig)% the status list has been deleted
989        return
990    else
991        hlist=findobj(hfig,'tag','list');
992        hmsgbox=findobj(hfig,'tag','msgbox');
993        hwaitbar=findobj(hfig,'tag','waitbar');
994        set(hlist,'String',Tabchar)
995        set(hmsgbox,'String', message)
996        if count>0 && ~test_new
997            BarPosition(3)=0.9*count/nbfiles;
998            set(hwaitbar,'Position',BarPosition)
999        end
1000    end
1001    set(hlist,'UserData',rootroot)
1002    if count<10||(nbfiles-count)<10
1003    pause(.5)% wait 0.5 seconds for next check
1004    else
1005        pause(10)% wait 10 seconds for next check
1006    end
1007end
1008
1009%------------------------------------------------------------------------
1010% --- Main lauch command, called by RUN and BATCH
1011function errormsg=launch_jobs(hObject, eventdata, handles, batch)
1012%-----------------------------------------------------------------------
1013errormsg='';%default
1014
1015%% read the input parameters from the  GUI civ
1016Param=read_GUI(handles.civ);
1017
1018%% check the selected list of operations:
1019operations={'Civ1','Fix1','Patch1','Civ2','Fix2','Patch2'};
1020box_test=[Param.CheckCiv1 Param.CheckFix1 Param.CheckPatch1 Param.CheckCiv2 Param.CheckFix2 Param.CheckPatch2];
1021index_first=find(box_test==1,1);
1022if isempty(index_first)
1023    errormsg='no selected operation';
1024    return
1025end
1026index_last=find(box_test==1,1,'last');
1027box_used=box_test(index_first : index_last);
1028[box_missing,ind_missing]=min(box_used);
1029if isequal(box_missing,0); %there is a missing step in the sequence of operations
1030    errormsg=['missing' cell2mat(operations(ind_missing))];
1031    return
1032end
1033
1034%% check mask if selecetd
1035%could be included in get_mask callback ?
1036if isequal(get(handles.CheckMask,'Value'),1)
1037    maskname=get(handles.txt_Mask,'String');
1038    if ~exist(maskname,'file')
1039        get_mask_civ1_Callback(hObject, eventdata, handles);
1040    end
1041end
1042if isequal(get(handles.CheckMask,'Value'),1)
1043    maskname=get(handles.txt_Mask,'String');
1044    if ~exist(maskname,'file')
1045        get_mask_fix1_Callback(hObject, eventdata, handles);
1046    end
1047end
1048if isequal(get(handles.CheckMask,'Value'),1)
1049    maskname=get(handles.txt_Mask,'String');
1050    if ~exist(maskname,'file')
1051        get_mask_civ2_Callback(hObject, eventdata, handles);
1052    end
1053end
1054if isequal(get(handles.CheckMask,'Value'),1)
1055    maskname=get(handles.txt_Mask,'String');
1056    if ~exist(maskname,'file')
1057        get_mask_fix2_Callback(hObject, eventdata, handles);
1058    end
1059end
1060
1061%% reinitialise status callback
1062if isfield(handles,'status')
1063    set(handles.status,'Value',0);%suppress status display
1064    status_Callback(hObject, eventdata, handles)
1065end
1066
1067%% read the PARAM.xml file to get the binaries (and batch_mode if batch)
1068path_civ=fileparts(which('civ')); %path to the source directory of uvmat
1069xmlfile='PARAM.xml';
1070if exist(xmlfile,'file')% search parameter xml file in the whole matlab path
1071    t=xmltree(xmlfile);
1072    s=convert(t);
1073else
1074    errormsg=['no file ' xmlfile];
1075    return
1076end
1077test_interp=0; %eviter les variables test_ (LG)
1078if batch
1079    if isfield(s,'BatchParam')
1080        Param.xml=s.BatchParam;
1081        if isfield(Param.xml,'BatchMode')
1082            batch_mode=Param.xml.BatchMode;
1083            if ~ismember(batch_mode,{'sge','oar'})
1084                errormsg=['batch mode ' batch_mode ' not supported by UVMAT'];
1085                return
1086            end
1087        end
1088    else
1089        errormsg='no batch civ binaries defined in PARAM.xml';
1090        return
1091    end
1092else % run
1093    if isfield(s,'RunParam')
1094        Param.xml=s.RunParam;
1095    else
1096        msgbox_uvmat('ERROR','no run civ binaries defined in PARAM.xml')
1097        return
1098    end
1099end
1100
1101%% check batch mode supported
1102if batch
1103    switch batch_mode
1104        case 'sge'
1105            test_command='qstat';
1106        case 'oar'
1107            test_command='oarstat';
1108    end   
1109    [s,w]=system(test_command);
1110    if ~isequal(s,0)
1111        msgbox_uvmat('ERROR',[batch_mode ' batch system not available'])
1112        return
1113    end
1114end
1115
1116%% check if the binaries exist
1117ListProgram=get(handles.ListProgram,'String');
1118CivMode=ListProgram{get(handles.ListProgram,'Value')};
1119binary_list={};
1120switch CivMode
1121    case 'CivX'
1122        binary_list={'Civ1Bin','Civ2Bin','PatchBin','FixBin'};
1123    case 'CivAll'% desactivated option
1124        binary_list={'Civ'};
1125    case 'Matlab'
1126        if batch
1127            % verifier MenuMatlab installe sur le cluster
1128            % difficile a faire a priori
1129        end         
1130end
1131for bin_name=binary_list %loop on the list of binaries
1132    if isfield(Param.xml,bin_name{1})% bin_name{1} =current name in the list
1133        if ~exist(Param.xml.(bin_name{1}),'file')%look for the full path if the file name has been defined with a relative path in PARAM.xml
1134            fullname=fullfile(path_civ,Param.xml.(bin_name{1}));
1135            if exist(fullname,'file')
1136                Param.xml.(bin_name{1})=fullname;
1137            else
1138                msgbox_uvmat('ERROR',['Binary ' Param.xml.(bin_name{1}) ' defined in PARAM.xml does not exist'])
1139                return
1140            end
1141        else
1142            [path,name,ext]=fileparts(Param.xml.(bin_name{1}));
1143            currentdir=pwd;
1144            cd(path);
1145            binpath=pwd;%path of the binary
1146            Param.xml.(bin_name{1})=fullfile(binpath,[name ext]);
1147            cd(currentdir);
1148        end
1149       
1150    end
1151end
1152display('files OK, processing...')
1153
1154%% set the list of files and check them
1155display('checking the files...')
1156[ref_i,ref_j,errormsg]=find_ref_indices(handles);
1157if ~isempty(errormsg)
1158    return
1159end
1160[filecell,i1_civ1,i2_civ1,j1_civ1,j2_civ1,i1_civ2,i2_civ2,j1_civ2,j2_civ2,nom_type_nc,xx,yy,compare]=...
1161    set_civ_filenames(handles,ref_i,ref_j,box_test);
1162
1163Rootbat=fileparts(filecell.nc.civ1{1,1});%output netcdf file (without extention)
1164set(handles.civ,'UserData',filecell);%store for futur use of status callback
1165if isempty(filecell)% (error message displayed in fct set_civ_filenames)
1166    return
1167end
1168nbfield=numel(i1_civ1);
1169nbslice=numel(j1_civ1);
1170
1171%% MAIN LOOP
1172time=get(handles.ImaDoc,'UserData'); %get the set of times
1173TimeUnit=get(handles.TimeUnit,'String');
1174checkframe=strcmp(TimeUnit,'frame');
1175batch_file_list=[];%should be renamed file_list, can be used for xml or bash files
1176 
1177for ifile=1:nbfield
1178    for j=1:nbslice
1179        % initiate system command
1180        switch CivMode
1181            case 'CivX'
1182                if isunix % check: necessaire aussi en RUN?
1183                    cmd=['#!/bin/bash \n '...
1184                        '#$ -cwd \n '...
1185                        'hostname && date \n '...
1186                        'umask 002 \n'];%allow writting access to created files for user group
1187                else
1188                    cmd=[];
1189                end
1190            case 'CivAll'
1191                CivAllxml=xmltree;% xml contents,  all parameters
1192                CivAllCmd='';
1193                CivAllxml=set(CivAllxml,1,'name','CivDoc');
1194        end
1195           
1196        % define output file name
1197        if Param.CheckCiv2==1 || Param.CheckFix2==1 || Param.CheckPatch2==1
1198            OutputFile=filecell.nc.civ2{ifile,j};
1199        else
1200            OutputFile=filecell.nc.civ1{ifile,j};
1201        end
1202        OutputFile=regexprep(OutputFile,'.nc','');
1203       
1204        if Param.CheckCiv1
1205            % read image-dependent parameters
1206            Param.Civ1.ImageA=filecell.ima1.civ1{ifile,j};
1207            Param.Civ1.ImageB=filecell.ima2.civ1{ifile,j};
1208            if ~checkframe% && size(time,1)>=i2_civ1(ifile) && size(time,2)>=j2_civ1(j)
1209                Param.Civ1.Dt=(time(i2_civ1(ifile)+1,j2_civ1(j)+1)-time(i1_civ1(ifile)+1,j1_civ1(j)+1));
1210            else
1211                Param.Civ1.Dt=1;
1212            end
1213            Param.Civ1.Time=((time(i2_civ1(ifile)+1,j2_civ1(j)+1)+time(i1_civ1(ifile)+1,j1_civ1(j)+1))/2);
1214            Param.Civ1.term_a=num2stra(j1_civ1(j),nom_type_nc);%UTILITE?
1215            Param.Civ1.term_b=num2stra(j2_civ1(j),nom_type_nc);%   
1216            ImageInfo=imfinfo(filecell.ima1.civ1{1,1});%read the first image to get the size
1217            Param.Civ1.ImageWidth=ImageInfo.Width;
1218            Param.Civ1.ImageHeight=ImageInfo.Height;
1219            Param.Civ1.ImageBitDepth=ImageInfo.BitDepth;
1220            % read mask parameters
1221            if Param.Civ1.CheckMask % the lines below should be changed with the new gui
1222                if ~exist(Param.Civ1.Mask,'file')
1223                    maskbase=[filecell.filebase '_' Param.Civ1.Mask]; %
1224                    nbslice_mask=str2double(Param.Civ1.Mask(1:end-4)); %
1225                    i1_mask=mod(i1_civ1(ifile)-1,nbslice_mask)+1;
1226                    [RootPathMask,RootFileMask]=fileparts(maskbase);
1227                    Param.Civ1.Mask=fullfile_uvmat(RootPathMask,[],RootFileMask,'.png','_1',i1_mask);
1228                   % Param.Civ1.Mask=name_generator(maskbase,i1_mask,1,'.png','_i');
1229                end
1230            end
1231            % read grid parameters
1232            if Param.Civ1.CheckGrid
1233                if numel(Param.Civ1.Grid)>=4 && isequal(Param.Civ1.Grid(end-3:end),'grid')
1234                    nbslice_grid=str2double(Param.Civ1.Grid(1:end-4)); %
1235                    if ~isnan(nbslice_grid)
1236                        i1_grid=mod(i1_civ1(ifile)-1,nbslice_grid)+1;
1237                        Param.Civ1.Grid=[filecell.filebase '_' fullfile_uvmat('','',Param.Civ1.Grid,'.grid','_1',i1_grid)];
1238%                         Param.Civ1.Grid=[filecell.filebase '_' name_generator(Param.Civ1.Grid,i1_grid,1,'.grid','_i')];
1239                        if ~exist(Param.Civ1.GridName,'file')
1240                            msgbox_uvmat('ERROR','grid file absent for civ1')
1241                        end
1242                    elseif ~exist(Param.Civ1.Grid,'file')
1243                        msgbox_uvmat('ERROR','grid file absent for civ1')
1244                    end
1245                end
1246            end
1247           
1248            % send command
1249            switch CivMode
1250                case 'CivX'
1251                    cmd=[cmd...
1252                        cmd_civ1(filecell.nc.civ1{ifile,j},Param) '\n'];
1253                    if ~isempty(errormsg)
1254                        msgbox_uvmat('ERROR',errormsg)
1255                        return
1256                    end
1257                case 'CivAll'
1258                    CivAllCmd=[CivAllCmd ' civ1 '];
1259                    str=CIV1_CMD_Unified(filecell.nc.civ1{ifile,j},'',Param.Civ1);
1260                    fieldnames=fields(str);
1261                    [CivAllxml,uid_civ1]=add(CivAllxml,1,'element','civ1');
1262                    for ilist=1:length(fieldnames)
1263                        val=eval(['str.' fieldnames{ilist}]);
1264                        if ischar(val)
1265                            [CivAllxml,uid_t]=add(CivAllxml,uid_civ1,'element',fieldnames{ilist});
1266                            [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
1267                        end
1268                    end
1269            end
1270        end
1271       
1272        if Param.CheckFix1
1273            switch CivMode
1274                case 'CivX'
1275                    cmd=[cmd...
1276                        cmd_fix(filecell.nc.civ1{ifile,j},Param,'Fix1') '\n'];
1277                case 'CivAll'%to abandon
1278                    fix1.inputFileName=filecell.nc.civ1{ifile,j} ;
1279                    fix1.fi1=num2str(param.fix1.flagindex1(1));
1280                    fix1.fi2=num2str(param.fix1.flagindex1(2));
1281                    fix1.fi3=num2str(param.fix1.flagindex1(3));
1282                    fix1.threshC=num2str(param.fix1.thresh_vecC1);
1283                    fix1.threshV=num2str(param.fix1.thresh_vel1);
1284                    fieldnames=fields(fix1);
1285                    [CivAllxml,uid_fix1]=add(CivAllxml,1,'element','fix1');
1286                    for ilist=1:length(fieldnames)
1287                        val=eval(['fix1.' fieldnames{ilist}]);
1288                        if ischar(val)
1289                            [CivAllxml,uid_t]=add(CivAllxml,uid_fix1,'element',fieldnames{ilist});
1290                            [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
1291                        end
1292                    end
1293                    CivAllCmd=[CivAllCmd ' fix1 '];
1294            end
1295        end
1296       
1297        %CheckPatch1
1298        if Param.CheckPatch1==1
1299            switch CivMode
1300                case 'CivX'
1301                    cmd=[cmd...
1302                        cmd_patch(filecell.nc.civ1{ifile,j},Param,'Patch1') '\n'];
1303                case 'CivAll'
1304                    patch1.inputFileName=filecell.nc.civ1{ifile,j} ;
1305                    patch1.nopt=subdomain_patch1;
1306                    patch1.maxdiff=thresh_patch1;
1307                    patch1.ro=rho_patch1;
1308                    test_grid=get(handles.get_gridpatch1,'Value');
1309                    if test_grid
1310                        patch1.gridflag='y';
1311                        gridname=get(handles.grid_patch1,'String');
1312                        if isequal(gridname(end-3:end),'grid')
1313                            nbslice_grid=str2double(gridname(1:end-4)); %
1314                            if ~isnan(nbslice_grid)
1315                                i1_grid=mod(i1_civ1(ifile)-1,nbslice_grid)+1;
1316                                patch1.gridPatch=[filecell.filebase '_' fullfile_uvmat('','',gridname,'.grid','_1',i1_grid)];
1317%                                 patch1.gridPatch=[filecell.filebase '_' name_generator(gridname,i1_grid,1,'.grid','_i')];
1318                                if ~exist(patch1.gridPatch,'file')
1319                                    msgbox_uvmat('ERROR','grid file absent for patch1')
1320                                end
1321                            elseif exist(gridname,'file')
1322                                patch1.gridPatch=gridname;
1323                            else
1324                                msgbox_uvmat('ERROR','grid file absent for patch1')
1325                            end
1326                        end
1327                    else
1328                        patch1.gridPatch='none';
1329                        patch1.gridflag='n';
1330                        patch1.m=nx_patch1;
1331                        patch1.n=ny_patch1;
1332                    end
1333                    patch1.convectFlow='n';
1334                    fieldnames=fields(patch1);
1335                    [CivAllxml,uid_patch1]=add(CivAllxml,1,'element','patch1');
1336                    for ilist=1:length(fieldnames)
1337                        val=eval(['patch1.' fieldnames{ilist}]);
1338                        if ischar(val)
1339                            [CivAllxml,uid_t]=add(CivAllxml,uid_patch1,'element',fieldnames{ilist});
1340                            [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
1341                        end
1342                    end
1343                    CivAllCmd=[CivAllCmd ' patch1 '];
1344            end
1345        end
1346        if Param.CheckCiv2==1
1347            Param.Civ2.ImageA=filecell.ima1.civ2{ifile,j};
1348            Param.Civ2.ImageB=filecell.ima2.civ2{ifile,j};
1349            if ~checkframe %&& size(time,1)>=i2_civ2(ifile) && size(time,2)>=j2_civ2(j)
1350                Param.Civ2.Dt=num2str(time(i2_civ2(ifile)+1,j2_civ2(j)+1)-time(i1_civ2(ifile)+1,j1_civ2(j)+1));
1351            else
1352                Param.Civ2.Dt=1;
1353            end
1354            Param.Civ2.Time=num2str((time(i2_civ2(ifile)+1,j2_civ2(j)+1)+time(i1_civ2(ifile)+1,j1_civ2(j)+1))/2);
1355            Param.Civ2.term_a=num2stra(j1_civ2(j),nom_type_nc);
1356            Param.Civ2.term_b=num2stra(j2_civ2(j),nom_type_nc);
1357            Param.Civ2.filename_nc1=filecell.nc.civ1{ifile,j};
1358            Param.Civ2.filename_nc1(end-2:end)=[]; % remove '.nc'
1359           
1360            % mask
1361            if Param.Civ2.CheckMask
1362                if ~exist(Param.Civ2.Mask,'file')
1363                    maskbase=[filecell.filebase '_' Param.Civ2.Mask]; %
1364                    nbslice_mask=str2double(Param.Civ2.Mask(1:end-4)); %
1365                    i1_mask=mod(i1_civ2(ifile)-1,nbslice_mask)+1;
1366                    [RootPathMask,RootFileMask]=fileparts(maskbase);
1367                    Param.Civ2.Mask=fullfile_uvmat(RootPathMask,[],RootFileMask,'.png','_1',i1_mask);
1368%                     Param.Civ2.Mask=name_generator(maskbase,i1_mask,1,'.png','_i');
1369                end
1370            end
1371            %grid
1372            if Param.Civ2.CheckGrid
1373                if numel(Param.Civ2.Grid)>=4 && isequal(Param.Civ2.Grid(end-3:end),'grid')
1374                    nbslice_grid=str2double(Param.Civ2.Grid(1:end-4)); %
1375                    if ~isnan(nbslice_grid)
1376                        i1_grid=mod(i1_civ2(ifile)-1,nbslice_grid)+1;
1377                        Param.Civ2.Grid=[filecell.filebase '_' fullfile_uvmat('','',gridname,'.grid','_1',i1_grid)];
1378%                         Param.Civ2.Grid=[filecell.filebase '_' name_generator(gridname,i1_grid,1,'.grid','_i')];
1379                    end
1380                end
1381            end
1382            ImageInfo=imfinfo(filecell.ima1.civ2{1,1});%read the first image to get the size
1383            Param.Civ2.ImageWidth=ImageInfo.Width;
1384            Param.Civ2.ImageHeight=ImageInfo.Height;
1385            Param.Civ2.ImageBitDepth=ImageInfo.BitDepth;
1386            % TODO: case of movie   
1387            switch CivMode
1388                case 'CivX'
1389                    cmd=[cmd...
1390                        cmd_civ2(filecell.nc.civ2{ifile,j},Param) '\n'];
1391                case 'CivAll'
1392                    CivAllCmd=[CivAllCmd ' civ2 '];
1393                    str=CIV2_CMD_Unified(filecell.nc.civ2{ifile,j},'',Param.Civ2);
1394                    fieldnames=fields(str);
1395                    [CivAllxml,uid_civ2]=add(CivAllxml,1,'element','civ2');
1396                    for ilist=1:length(fieldnames)
1397                        val=eval(['str.' fieldnames{ilist}]);
1398                        if ischar(val)
1399                            [CivAllxml,uid_t]=add(CivAllxml,uid_civ2,'element',fieldnames{ilist});
1400                            [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
1401                        end
1402                    end
1403            end
1404        end
1405       
1406        % CheckFix2
1407        if Param.CheckFix2==1
1408            switch CivMode
1409                case 'CivX'
1410                    cmd=[cmd...
1411                        cmd_fix(filecell.nc.civ2{ifile,j},Param,'Fix2') '\n'];
1412                case 'CivAll'
1413                    fix2.inputFileName=filecell.nc.civ2{ifile,j} ;
1414                    fix2.fi1=num2str(flagindex2(1));
1415                    fix2.fi2=num2str(flagindex2(2));
1416                    fix2.fi3=num2str(flagindex2(3));
1417                    fix2.threshC=num2str(thresh_vec2C);
1418                    fix2.threshV=num2str(thresh_vel2);
1419                    fieldnames=fields(fix2);
1420                    [CivAllxml,uid_fix2]=add(CivAllxml,1,'element','fix2');
1421                    for ilist=1:length(fieldnames)
1422                        val=eval(['fix2.' fieldnames{ilist}]);
1423                        if ischar(val)
1424                            [CivAllxml,uid_t]=add(CivAllxml,uid_fix2,'element',fieldnames{ilist});
1425                            [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
1426                        end
1427                    end
1428                    CivAllCmd=[CivAllCmd ' fix2 '];
1429            end
1430        end
1431       
1432        %CheckPatch2
1433        if Param.CheckPatch2==1
1434           
1435            switch CivMode
1436               
1437                case 'CivX'
1438                    cmd=[cmd...
1439                        cmd_patch(filecell.nc.civ1{ifile,j},Param,'Patch2') '\n'];
1440                   
1441                case 'CivAll'
1442                    patch2.inputFileName=filecell.nc.civ1{ifile,j} ;
1443                    patch2.nopt=subdomain_patch2;
1444                    patch2.maxdiff=thresh_patch2;
1445                    patch2.ro=rho_patch2;
1446                    test_grid=get(handles.get_gridpatch2,'Value');
1447                    if test_grid
1448                        patch2.gridflag='y';
1449                        gridname=get(handles.grid_patch2,'String');
1450                        if isequal(gridname(end-3:end),'grid')
1451                            nbslice_grid=str2double(gridname(1:end-4)); %
1452                            if ~isnan(nbslice_grid)
1453                                i1_grid=mod(i1_civ2(ifile)-1,nbslice_grid)+1;
1454                                patch2.gridPatch=[filecell.filebase '_' fullfile_uvmat('','',gridname,'.grid','_1',i1_grid)];
1455%                                 patch2.gridPatch=[filecell.filebase '_' name_generator(gridname,i1_grid,1,'.grid','_i')];
1456                                if ~exist(patch2.gridPatch,'file')
1457                                    msgbox_uvmat('ERROR','grid file absent for patch2')
1458                                end
1459                            elseif exist(gridname,'file')
1460                                patch2.gridPatch=gridname;
1461                            else
1462                                msgbox_uvmat('ERROR','grid file absent for patch2')
1463                            end
1464                        end
1465                    else
1466                        patch2.gridPatch='none';
1467                        patch2.gridflag='n';
1468                        patch2.m=nx_patch2;
1469                        patch2.n=ny_patch2;
1470                    end
1471                    patch2.convectFlow='n';
1472                    fieldnames=fields(patch2);
1473                    [CivAllxml,uid_patch2]=add(CivAllxml,1,'element','patch2');
1474                    for ilist=1:length(fieldnames)
1475                        val=eval(['patch2.' fieldnames{ilist}]);
1476                        if ischar(val)
1477                            [CivAllxml,uid_t]=add(CivAllxml,uid_patch2,'element',fieldnames{ilist});
1478                            [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
1479                        end
1480                    end
1481                    CivAllCmd=[CivAllCmd ' patch2 '];
1482            end
1483        end
1484       
1485        switch CivMode
1486           
1487            case {'CivX','CivAll'}
1488                if isequal(CivMode,'CivAll')
1489                    save(CivAllxml,[OutputFile '.xml']);
1490                    cmd=[cmd sparam.CivBin ' -f ' OutputFile '.xml '  CivAllCmd ' >' OutputFile '.log' '\n'];
1491                end             
1492                % create the .bat file used in run or batch
1493                filename_bat=[OutputFile '.bat'];
1494                [fid,message]=fopen(filename_bat,'w');
1495                if isequal(fid,-1)
1496                    msgbox_uvmat('ERROR', ['creation of .bat file: ' message])
1497                    return
1498                end
1499                fprintf(fid,cmd);
1500                fclose(fid);           
1501                if isunix
1502                    system(['chmod +x ' filename_bat]);
1503                end             
1504                batch_file_list{length(batch_file_list)+1}=filename_bat;
1505               
1506            case 'Matlab'
1507                drawnow
1508                if ~strcmp(compare,'stereo PIV')
1509                    filename_xml=[OutputFile '.civ.xml'];
1510                    t=struct2xml(Param);           
1511                    save(t,filename_xml)
1512                    if batch   
1513                        path_civ=fileparts(which('civ'));
1514                        filename_bat=[OutputFile '.bat'];
1515                        [fid,message]=fopen(filename_bat,'w');
1516                        if isequal(fid,-1)
1517                            msgbox_uvmat('ERROR', ['creation of .bat file: ' message])
1518                            return
1519                        end
1520                        text_matlabscript=[...
1521                        '#!/bin/bash \n'...
1522                        '. /etc/sysprofile \n'...
1523                        'matlab -nodisplay -nosplash -nojvm <<END_MATLAB \n'...
1524                        'cd(''' path_civ '''); \n'...
1525                        'civ_matlab(''' filename_xml ''',''' OutputFile '.nc''); \n'...
1526                        'exit \n'...
1527                        'END_MATLAB \n'];
1528                        fprintf(fid,text_matlabscript);
1529                        fclose(fid);
1530                        if isunix
1531                            system(['chmod +x ' filename_bat]);
1532                        end
1533                        batch_file_list{length(batch_file_list)+1}=filename_bat;
1534                    else
1535                        [Data,erromsg]=civ_matlab(Param,filecell.nc.civ1{ifile,j});
1536                        if isempty(errormsg)
1537                            display([filecell.nc.civ1{ifile,j} ' written'])
1538                        else
1539                            msgbox_uvmat('ERROR',errormsg)
1540                        end
1541                    end
1542                end
1543        end
1544    end
1545end
1546
1547if batch 
1548    switch batch_mode   
1549       
1550        case 'sge'
1551            for p=1:length(batch_file_list)
1552                cmd=['!qsub -p ' pvalue ' -q civ.q -e ' flname '.errors -o ' flname '.log' ' ' batch_file_list{p}];
1553                display(cmd);eval(cmd);
1554            end           
1555        case 'oar_old'
1556                for p=1:length(batch_file_list)
1557                    oar_command=['!oarsub -n CIVX -q nicejob '...
1558                   '-E ' regexprep(batch_file_list{p},'.bat','.errors') ' -O ' regexprep(batch_file_list{p},'.bat','.log ')...
1559                    '-l "/core=1+{type = ''smalljob''}/licence=1,walltime=00:60:00"   ' batch_file_list{p}];
1560                display(oar_command);eval(oar_command);
1561                end               
1562        case 'oar'
1563           
1564            oar_modes={'oar-dispatch','mpilauncher','oar-parexec'};
1565            text={'Batch processing on servcalcul3 LEGI';...
1566                'Please choose one of the followint modes';...
1567                '* oar-dispatch : jobs in a container of several cores';...
1568                '* mpilauncher : one single parallel mpi job using several cores';...
1569                '* oar-parexec (under development) ';...
1570                '**********************************'...
1571                };
1572            [S,v]=listdlg('PromptString',text,'ListString',oar_modes,...
1573                'SelectionMode','single','ListSize',[400 100],'Name','LEGI job mode');
1574            switch oar_modes{S}
1575                case 'oar-parexec' %oar-dispatch.pl
1576                    ncores=str2double(...
1577                        inputdlg('Number of cores (max 36)','oarsub parameter',1,{'12'})...
1578                        );
1579                    walltime_onejob=600;%seconds
1580                    filename_joblist=fullfile(Rootbat,'job_list.txt');
1581                    fid=fopen(filename_joblist,'w');
1582                    for p=1:length(batch_file_list)
1583                        fprintf(fid,[batch_file_list{p} '\n']);
1584                    end
1585                    fclose(fid)
1586                    oar_command=['oarsub -n test '...
1587                         '-l /core=' num2str(ncores) ','...
1588                        'walltime=' datestr(1.05*walltime_onejob/86400*max(length(batch_file_list),ncores)/ncores,13) ' '...
1589                        '-E ' regexprep(filename_joblist,'\.txt\>','.errors') ' '...
1590                        '-O ' regexprep(filename_joblist,'\.txt\>','.log') ' '...
1591                        '"oar-parexec -f ' filename_joblist ' -l ' filename_joblist '.log"'];
1592                    filename_oarcommand=fullfile(Rootbat,'oar_command');
1593                    fid=fopen(filename_oarcommand,'w');
1594                    fprintf(fid,[oar_command '\n']);
1595                    fclose(fid);
1596                    display(oar_command);
1597                    eval(['! . ' filename_oarcommand])
1598                case 'oar-dispatch' %oar-dispatch.pl
1599                    ncores=str2double(...
1600                        inputdlg('Number of cores (max 36)','oarsub parameter',1,{'6'})...
1601                        );
1602                    walltime_onejob=600;%seconds
1603                    filename_joblist=fullfile(Rootbat,'job_list.txt');
1604                    fid=fopen(filename_joblist,'w');
1605                    for p=1:length(batch_file_list)
1606                        oar_command=['oarsub -n CIVX '...
1607                            '-E ' regexprep(batch_file_list{p},'\.bat\>','.errors') ' -O ' regexprep(batch_file_list{p},'\.bat\>','.log ')...
1608                            '-l "/core=1,walltime=' datestr(walltime_onejob/86400,13) '"   ' batch_file_list{p}];
1609                        fprintf(fid,[oar_command '\n']);
1610                    end
1611                    fclose(fid);
1612                    oar_command=['oarsub -t container -n civx-container '...
1613                        '-l /core=' num2str(ncores)...
1614                        ',walltime=' datestr(1.05*walltime_onejob/86400*max(length(batch_file_list),ncores)/ncores,13) ' '...
1615                        '-E ' regexprep(filename_joblist,'\.txt\>','.errors') ' '...
1616                        '-O ' regexprep(filename_joblist,'\.txt\>','.log') ' '...
1617                        '"oar-dispatch -f ' filename_joblist '"'];
1618                    filename_oarcommand=fullfile(Rootbat,'oar_command');
1619                    fid=fopen(filename_oarcommand,'w');
1620                    fprintf(fid,[oar_command '\n']);
1621                    fclose(fid);
1622                    display(oar_command);
1623                    eval(['! . ' filename_oarcommand])
1624                case 'mpilauncher'
1625                    filename_joblist=fullfile(Rootbat,'job_list.txt');
1626                    fid=fopen(filename_joblist,'w');
1627                   
1628                    for p=1:length(batch_file_list)
1629                        fprintf(fid,[batch_file_list{p} '\n']);
1630                    end
1631                    fclose(fid)
1632                    text_oarscript=[...
1633                        '#!/bin/bash \n'...
1634                        '#OAR -n Mylauncher \n'...
1635                        '#OAR -l node=4/core=5,walltime=0:15:00 \n'...
1636                        '#OAR -E ' fullfile(Rootbat,'stderrfile.log') ' \n'...
1637                        '#OAR -O ' fullfile(Rootbat,'stdoutfile.log') ' \n'...
1638                        '# ========================================================= \n'...
1639                        '# This simple program launch a multinode parallel OpenMPI mpilauncher \n'...
1640                        '# application for coriolis PIV post-processing. \n'...
1641                        '# OAR uses oarshmost wrapper to propagate the user environement. \n'...
1642                        '# This wrapper assert that the user has the same environment on all the \n'...
1643                        '# allocated nodes (basic behavior needed by most MPI applications).  \n'...
1644                        '# \n'...
1645                        '# REQUIREMENT: \n'...
1646                        '# the oarshmost wrapper should be installed in $HOME/bin directory. \n'...
1647                        '# If a different location is used, change the line following the comment "Bidouille" \n'...
1648                        '# ========================================================= \n'...
1649                        '#   USER should only modify these 2 lines  \n'...
1650                        'WORKDIR=' pwd ' \n'...
1651                        'COMMANDE="mpilauncher  -f ' filename_joblist '" \n'...
1652                        '# ========================================================= \n'...
1653                        '# DO NOT MODIFY the FOLOWING LINES. (or be carefull) \n'...
1654                        'echo "job starting on: "`hostname` \n'...
1655                        'MPINODES="-host `tr [\\\\\\n] [,] <$OAR_NODEFILE |sed -e "s/,$/ /"`" \n'...
1656                        'NCPUS=`cat $OAR_NODEFILE |wc -l` \n'...
1657                        '#========== Bidouille ============== \n'...
1658                        'export OMPI_MCA_plm_rsh_agent=oar-envsh \n'...%                     'cd $WORKDIR \n'...
1659                        'CMD="mpirun -np $NCPUS -wdir $WORKDIR $MPINODES $COMMANDE" \n'...
1660                        'echo "I run: $CMD"  \n'...
1661                        '$CMD \n'...
1662                        'echo "job ending" \n'...
1663                        ];
1664                    %                 oarsub -S ./oar.sub
1665                    filename_oarscript=fullfile(Rootbat,'oar_command');
1666                    fid=fopen(filename_oarscript,'w');
1667                    fprintf(fid,[text_oarscript]);
1668                    fclose(fid);
1669                    eval(['!chmod +x  ' filename_oarscript]);
1670                    eval(['!oarsub -S ' filename_oarscript]);
1671            end
1672    end
1673else
1674    if ~isequal(CivMode,'Matlab')
1675        filename_superbat=fullfile(Rootbat,'job_list.bat');
1676        fid=fopen(filename_superbat,'w');
1677        if fid==-1
1678            msgbox_uvmat('ERROR',['cannot create the command file ' filename_superbat])
1679            return
1680        end
1681        for p=1:length(batch_file_list)
1682            if isunix
1683                fprintf(fid,['sh ' batch_file_list{p} '\n']);
1684            else
1685                fprintf(fid,['@call "' regexprep(batch_file_list{p},'\\','\\\\') '"' '\n']);
1686            end
1687        end
1688        fclose(fid);
1689        if(isunix)
1690            system(['chmod +x ' filename_superbat]);
1691        end
1692        system([filename_superbat ' &']);% execute main commmand
1693    end
1694end
1695
1696
1697%% save interface state
1698if isfield(filecell,'nc')
1699    if isfield(filecell.nc,'civ2')
1700        fileresu=filecell.nc.civ2{1,1};
1701    else
1702        fileresu=filecell.nc.civ1{1,1};
1703    end
1704end
1705[RootPath,SubDir,RootFile]=fileparts_uvmat(fileresu);
1706namedoc=fullfile(RootPath,SubDir,RootFile);
1707detect=1;
1708while detect==1
1709    namefigfull=[namedoc '.fig'];
1710    hh=dir(namefigfull);
1711    if ~isempty(hh)
1712        detect=1;
1713        namedoc=[namedoc '.0'];
1714    else
1715        detect=0;
1716    end
1717end
1718Param=rmfield(Param,'status');
1719Param=rmfield(Param,'xml');
1720t=struct2xml(Param);
1721t=set(t,1,'Name','CivDoc');% set the head label
1722save(t,[namedoc '.CivDoc.xml']); %save GUI  parameters as xml file
1723saveas(gcbf,namefigfull);%save the interface with name namefigfull (A CHANGER EN FICHIER  .xml)
1724
1725%Save info in personal profile (initiate browser next time) TODO
1726MenuFile={};
1727dir_perso=prefdir;
1728profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
1729if exist(profil_perso,'file')
1730    hh=load (profil_perso);
1731      if isfield(hh,'MenuFile')
1732          MenuFile=hh.MenuFile;
1733      end
1734      if isfield(filecell.nc,'civ2')
1735          MenuFile=[filecell.nc.civ2{1,1}; MenuFile];
1736      else
1737           MenuFile=[filecell.nc.civ1{1,1}; MenuFile];
1738      end
1739      save (profil_perso,'MenuFile','-append'); %store the file names for future opening of uvmat
1740else
1741    MenuFile=filecell.ima1.civ1(1,1);
1742    save (profil_perso,'MenuFile')
1743end
1744
1745%------------------------------------------------------------------------
1746% --- determine the list of reference indices of processing file
1747function [ref_i,ref_j,errormsg]=find_ref_indices(handles)
1748%------------------------------------------------------------------------
1749errormsg=''; %default error message
1750first_i=str2double(get(handles.first_i,'String'));%first index i
1751last_i=str2double(get(handles.last_i,'String'));%last index i
1752incr_i=str2double(get(handles.incr_i,'String'));% increment
1753if isequal(get(handles.first_j,'Visible'),'on')
1754    first_j=str2double(get(handles.first_j,'String'));%first index j
1755    last_j=str2double(get(handles.last_j,'String'));%last index j
1756    incr_j=str2double(get(handles.incr_j,'String'));% increment
1757else
1758    first_j=1;
1759    last_j=1;
1760    incr_j=1;
1761end
1762ref_i=first_i:incr_i:last_i;% list of i indices (reference values for each pair)
1763ref_j=first_j:incr_j:last_j;% list of j indices (reference values for each pair)
1764if isnan(first_i)||isnan(first_j)
1765    errormsg='first field number not defined';
1766elseif isnan(last_i)||isnan(last_j)
1767    errormsg='last field number not defined';
1768elseif isnan(incr_i)||isnan(incr_j)
1769    errormsg='increment in field number not defined';
1770elseif last_i < first_i || last_j < first_j
1771    errormsg='last field number must be larger than the first one';
1772end
1773
1774%------------------------------------------------------------------------
1775% --- determine the list of filenames and indices needed for launch_job
1776%------------------------------------------------------------------------
1777% OUTPUT:
1778% filecell: structure of cell arrays {ref_i,ref_j} containing all the filenames involved in the civ process
1779%    the indices ref_i and ref_j correspond to the list of reference indices
1780%       .filebase=fullfile(RootPath,RootFile) used to construct mask names, grid names, CivDoc xml file
1781%       .ima1.civ1,.ima1.civ2: first image for civ1 and civ2 respectively (possibly different)
1782%       .ima2.civ1,.ima2.civ2: second image for civ1 and civ2 respectively (possibly different)
1783%       .nc.civ1,.nc.civ2: netcdf files containing civ1 and civ2 data respectively (possibly different)
1784% i1_civ1,i2_civ1,j1_civ1,j2_civ1,i1_civ2,i2_civ2,j1_civ2,j2_civ2: arrays of files indices, needed for timing records
1785function [filecell,i1_civ1,i2_civ1,j1_civ1,j2_civ1,i1_civ2,i2_civ2,j1_civ2,j2_civ2,NomType_nc,file_ref_fix1,file_ref_fix2,compare]=...
1786    set_civ_filenames(handles,ref_i,ref_j,checkbox)
1787%------------------------------------------------------------------------
1788filecell=[];%default
1789
1790%% get the root name and check dir
1791RootPath=get(handles.RootPath,'String');
1792RootFile=get(handles.RootFile,'String');
1793filecell.filebase=fullfile(RootPath,RootFile);
1794if isempty(filecell.filebase)
1795    msgbox_uvmat('ERROR','please open an image with the upper menu option Open/Browse...')
1796    return
1797end
1798if ~exist(RootPath,'dir')
1799    msgbox_uvmat('ERROR',['path to images ' RootPath ' not found'])
1800    return
1801end
1802[xx,message]=fileattrib(RootPath);
1803if ~isempty(message) && ~isequal(message.UserWrite,1)
1804    msgbox_uvmat('ERROR',['No writting access to ' RootPath])
1805    return
1806end
1807%check result directory
1808subdir_civ1=get(handles.SubdirCiv1,'String');%subdirectory subdir_civ1 for the netcdf output data
1809subdir_civ2=get(handles.SubdirCiv2,'String');
1810if isequal(subdir_civ1,''),subdir_civ1='CIV'; end% put default subdir
1811if isequal(subdir_civ2,''),subdir_civ2=subdir_civ1; end% put default subdir
1812
1813%% choose root names depending on ListCompareMode =displacement, shift, PIV or stereo PIV
1814ListCompareMode=get(handles.ListCompareMode,'String');
1815compare=ListCompareMode{get(handles.ListCompareMode,'Value')};
1816
1817% set the nomenclature type of the nc files depending on the pair mode
1818if strcmp(compare,'displacement')||strcmp(compare,'shift')
1819    mode='displacement';
1820else
1821    mode_list=get(handles.ListPairMode,'String');
1822    mode_value=get(handles.ListPairMode,'Value');
1823    mode=mode_list{mode_value};
1824end
1825NomType_ima2=get(handles.NomType,'String');
1826NomType_nc=nomtype2pair(NomType_ima2,mode);
1827
1828% set the rootfile and image indexing
1829RootFile_ima2=get(handles.RootFile,'String');%root file for the second image series
1830ext_ima=get(handles.ImaExt,'String'); % image extension (the same for all images)
1831switch compare
1832    case 'PIV'
1833       RootFile_ima1=RootFile_ima2;% root name of the two image series is the same
1834       NomType_ima1=NomType_ima2;% the index of the first image follows the index of the second one
1835       RootFile_nc=RootFile_ima2;
1836    case 'displacement'
1837       RootFile_ima1=get(handles.RootFile_1,'String');% root name of the first image series set by handles.RootFile_1
1838       NomType_ima1='';% no indexing of the first image, a fixed reference for the whole series
1839       RootFile_nc=RootFile_ima2;
1840    case 'shift'
1841       RootFile_ima1=get(handles.RootFile_1,'String');% root name of the first image series set by handles.RootFile_1
1842       NomType_ima1=NomType_ima2;% the index of the first image follows the index of the second one
1843       RootFile_nc=[RootFile_ima1 '-' RootFile_ima2];
1844end
1845
1846%determine the list of file indices involved
1847[i1_civ1,i2_civ1,j1_civ1,j2_civ1,i1_civ2,i2_civ2,j1_civ2,j2_civ2]=...
1848    find_pair_indices(handles,ref_i,ref_j,mode);
1849
1850%determine the new filebase for 'displacement' ListPairMode (comparison of two series)
1851%filebase_B=filebase;% root name of the second field series for stereo
1852% filebase_A=filebase;%default
1853% if strcmp(compare,'PIV')
1854%     filebase_AB=filebase;
1855% else
1856%     [Path2,Name2]=fileparts(filebase_B);
1857%     Name1=RootFile_ima1;
1858%     filebase_AB=fullfile(Path2,[Name2 '-' Name1]);   
1859% end
1860% [RootPath_AB,RootFile_AB]=fileparts(filebase_AB);
1861% % [RootPath_ima1,RootFile_ima1]=fileparts(filebase_B);
1862% [RootPath_ima2,RootFile_ima2]=fileparts(filebase_B);
1863% [RootPath_nc,RootFile_nc]=fileparts(filebase_B);%default
1864% if strcmp(compare,'displacement')
1865% %     [RootPath_ima1,RootFile_ima1]=fileparts(filebase_B);
1866% %     [RootPath_ima2,RootFile_ima2]=fileparts(filebase_B);
1867%     [RootPath_nc,RootFile_nc]=fileparts(filebase_B);
1868% elseif strcmp(compare,'shift')
1869%     RootPath_nc=RootPath_AB;
1870%     RootFile_nc=RootFile_AB;
1871% end
1872% else
1873%     filebase_ima1=filebase_B;
1874%     filebase_ima2=filebase_B;
1875%     filebase_nc=filebase_B;
1876% [RootPath_ima1,RootFile_ima1]=fileparts(filebase_ima1);
1877% [RootPath_ima2,RootFile_ima2]=fileparts(filebase_ima2);
1878% [RootPath_nc,RootFile_nc]=fileparts(filebase_nc);
1879% [RootPath_A,RootFile_A]=fileparts(filebase_A);
1880
1881   
1882%% determine reference files for fix:
1883file_ref_fix1={};%default
1884file_ref_fix2={};
1885nbfield=length(i1_civ1);
1886nbslice=length(j1_civ1);
1887if checkbox(2)==1% fix1 performed
1888    ref=get(handles.ref_fix1,'UserData');%read data on the ref file stored by get_ref_fix1_Callback
1889    if ~isempty(ref)
1890        first_i=str2double(get(handles.first_i,'String'));
1891        last_i=str2double(get(handles.last_i,'String'));
1892        incr_i=str2double(get(handles.incr_i,'String'));
1893        first_j=str2double(get(handles.first_j,'String'));
1894        last_j=str2double(get(handles.last_j,'String'));
1895        incr_j=str2double(get(handles.incr_j,'String'));
1896        num_i_ref=first_i:incr_i:last_i;
1897        num_j_ref=first_j:incr_j:last_j;
1898        if isequal(mode,'displacement')
1899            num_i1=num_i_ref;
1900            num_i2=num_i_ref;
1901            num_j1=num_j_ref;
1902            num_j2=num_j_ref;
1903        elseif isequal(mode,'pair j1-j2')% isequal(mode,'st_pair j1-j2')
1904            num_i1=num_i_ref;
1905            num_i2=num_i1;
1906            num_j1=ref.num_a*ones(size(num_i_ref));
1907            num_j2=ref.num_b*ones(size(num_i_ref));
1908        elseif isequal(mode,'series(Di)') % isequal(mode,'st_series(Di)')
1909            delta1=floor((ref.num2-ref.num1)/2);
1910            delta2=ceil((ref.num2-ref.num1)/2);
1911            num_i1=num_i_ref-delta1*ones(size(num_i_ref));
1912            num_i2=num_i_ref+delta2*ones(size(num_i_ref));
1913            if isempty(ref.num_a)
1914                ref.num_a=1;
1915            end
1916            num_j1=ref.num_a*ones(size(num_i1));
1917            num_j2=num_j1;
1918        elseif isequal(mode,'series(Dj)')%| isequal(mode,'st_series(Dj)')
1919            delta1=floor((ref.num_b-ref.num_a)/2);
1920            delta2=ceil((ref.num_b-ref.num_a)/2);
1921            num_i1=ref.num1*ones(size(num_i_ref));
1922            num_i2=num_i1;
1923            num_j1=num_j_ref-delta1*ones(size(num_j_ref));
1924            num_j2=num_j_ref+delta2*ones(size(num_j_ref));
1925        end
1926        for ifile=1:nbfield
1927            for j=1:nbslice
1928                [RootPathRef,RootFile]=fileparts(ref.filebase);
1929                file_ref=fullfile_uvmat(RootPathRef,ref.subdir,RootFile,'.nc',ref.NomType,num_i1(ifile),num_i2(ifile),num_j1(j),num_j2(j));
1930                file_ref_fix1(ifile,j)={file_ref};
1931                if ~exist(file_ref,'file')
1932                    msgbox_uvmat('ERROR',['reference file ' file_ref ' not found for fix1'])
1933                    filecell=[];
1934                    return
1935                end
1936            end
1937        end
1938    end
1939end
1940
1941%% determine reference files for fix2:
1942if checkbox(5)==1% fix2 performed
1943    ref=get(handles.ref_fix2,'UserData');
1944    if ~isempty(ref)
1945        first_i=str2double(get(handles.first_i,'String'));
1946        last_i=str2double(get(handles.last_i,'String'));
1947        incr_i=str2double(get(handles.incr_i,'String'));
1948        first_j=str2double(get(handles.first_j,'String'));
1949        last_j=str2double(get(handles.last_j,'String'));
1950        incr_j=str2double(get(handles.incr_j,'String'));
1951        num_i_ref=first_i:incr_i:last_i;
1952        num_j_ref=first_j:incr_j:last_j;
1953        if isequal(mode,'displacement')
1954            num_i1=num_i_ref;
1955            num_i2=num_i_ref;
1956            num_j1=num_j_ref;
1957            num_j2=num_j_ref;
1958        elseif isequal(mode,'pair j1-j2')
1959            num_i1=num_i_ref;
1960            num_i2=num_i1;
1961            num_j1=ref.num_a;
1962            num_j2=ref.num_b;
1963        elseif isequal(mode,'series(Di)')
1964            delta1=floor((ref.num2-ref.num1)/2);
1965            delta2=ceil((ref.num2-ref.num1)/2);
1966            num_i1=num_i_ref-delta1*ones(size(num_i_ref));
1967            num_i2=num_i_ref+delta2*ones(size(num_i_ref));
1968            num_j1=ref.num_a*ones(size(num_i1));
1969            num_j2=num_j1;
1970        elseif isequal(mode,'series(Dj)')
1971            delta1=floor((ref.num_b-ref.num_a)/2);
1972            delta2=ceil((ref.num_b-ref.num_a)/2);
1973            num_i1=ref.num1*ones(size(num_i_ref));
1974            num_i2=num_i1;
1975            num_j1=num_j_ref-delta1*ones(size(num_j_ref));
1976            num_j2=num_j_ref+delta2*ones(size(num_j_ref));
1977        end
1978        for ifile=1:nbfield
1979            for j=1:nbslice
1980                [RootPathRef,RootFile]=fileparts(ref.filebase);
1981                file_ref=fullfile_uvmat(RootPathRef,ref.subdir,RootFile,'.nc',ref.NomType,num_i1(ifile),num_i2(ifile),num_j1(j),num_j2(j));
1982                file_ref_fix2(ifile,j)={file_ref};
1983                if ~exist(file_ref,'file')
1984                    msgbox_uvmat('ERROR',['reference file ' file_ref ' not found for fix2'])
1985                    filecell={};
1986                    return
1987                end
1988            end
1989        end
1990    end
1991end
1992
1993%% check the existence of the netcdf and image files involved
1994% %%%%%%%%%%%%  case CheckCiv1 activated   %%%%%%%%%%%%%
1995if checkbox(1)==1;
1996    detect=1;
1997    vers=0;
1998    subdir_civ1_new=subdir_civ1;
1999    while detect==1 %create a new subdir if the netcdf files already exist
2000        for ifile=1:nbfield
2001            for j=1:nbslice
2002                filename=fullfile_uvmat(RootPath,subdir_civ1_new,RootFile_nc,'.nc',NomType_nc,i1_civ1(ifile),i2_civ1(ifile),j1_civ1(j),j2_civ1(j));
2003                detect=exist(filename,'file')==2;
2004                if detect% if a netcdf file already exists
2005                    indstr=regexp(subdir_civ1_new,'\D');
2006                    if indstr(end)<length(subdir_civ1_new) %subdir_civ1 ends by a number
2007                        vers=str2double(subdir_civ1_new(indstr(end)+1:end))+1;
2008                        subdir_civ1_new=[subdir_civ1_new(1:indstr(end)) num2str(vers)];
2009                    else
2010                        vers=vers+1;
2011                        subdir_civ1_new=[subdir_civ1_new(1:indstr(end)) '_' num2str(vers)];       
2012                    end
2013                    subdir_civ2=subdir_civ1_new;
2014                    break
2015                end
2016                filecell.nc.civ1(ifile,j)={filename};
2017            end
2018            if detect% if a netcdf file already exists
2019                break
2020            end
2021        end
2022 
2023        %create the new SubdirCiv1
2024        if ~exist(fullfile(RootPath,subdir_civ1_new),'dir')     
2025            [xx,msg1]=mkdir(fullfile(RootPath,subdir_civ1_new));
2026
2027            if ~strcmp(msg1,'')
2028                msgbox_uvmat('ERROR',['cannot create ' subdir_civ1_new ': ' msg1])%error message for directory creation
2029                filecell={};
2030                return
2031            elseif isunix         
2032                [xx,msg2] = fileattrib(fullfile(RootPath,subdir_civ1_new),'+w','g'); %yield writing access (+w) to user group (g)
2033                if ~strcmp(msg2,'')
2034                    msgbox_uvmat('ERROR',['pb of permission for  ' fullfile(RootPath,subdir_civ1_new) ': ' msg2])%error message for directory creation
2035                    filecell={};
2036                    return
2037                end
2038            end
2039        end
2040        if strcmp(compare,'stereo PIV')&&(strcmp(mode,'pair j1-j2')||strcmp(mode,'series(Dj)')||strcmp(mode,'series(Di)'))%check second nc series
2041            for ifile=1:nbfield
2042                for j=1:nbslice
2043                     filename=fullfile_uvmat(RootPath,subdir_civ1_new,RootFile_A,'.nc',NomType_nc,i1_civ1(ifile),i2_civ1(ifile),j1_civ1(j),j2_civ1(j));
2044                   % filename=name_generator(filebase_A,i1_civ1(ifile),j1_civ1(j),'.nc',NomType_nc,1,i2_civ1(ifile),j2_civ1(j),subdir_civ1_new);%
2045                    detect=exist(filename,'file')==2;
2046                    if detect% if a netcdf file already exists
2047                       indstr=regexp(subdir_civ1_new,'\D');
2048                       if indstr(end)<length(subdir_civ1_new) %subdir_civ1 ends by a number
2049                           vers=str2double(subdir_civ1_new(indstr(end)+1:end))+1;
2050                           subdir_civ1_new=[subdir_civ1_new(1:indstr(end)) num2str(vers)];
2051                       else
2052                           vers=vers+1;
2053                           subdir_civ1_new=[subdir_civ1_new '_' num2str(vers)];
2054                       end
2055                       subdir_civ2=subdir_civ1;
2056                       break
2057                    end
2058                    filecell.ncA.civ1(ifile,j)={filename};
2059                end
2060                if detect% if a netcdf file already exists
2061                    break
2062                end
2063            end
2064            %create the new SubdirCiv1
2065            if ~exist(fullfile(RootPath,subdir_civ1_new),'dir')       
2066                [xx,msg1]=mkdir(fullfile(RootPath,subdir_civ1_new));
2067                if ~strcmp(msg1,'')
2068                    msgbox_uvmat('ERROR',['cannot create ' subdir_civ1_new ': ' msg1])
2069                    filecell={};
2070                    return
2071                else
2072                    [xx,msg2] = fileattrib(fullfile(RootPath,subdir_civ1_new),'+w','g'); %yield writing access (+w) to user group (g)
2073                    if ~strcmp(msg2,'')
2074                        msgbox_uvmat('ERROR',['pb of permission for ' subdir_civ1_new ': ' msg2])%error message for directory creation
2075                        filecell={};
2076                        return
2077                    end
2078                end
2079            end
2080        end
2081    end
2082    subdir_civ1=subdir_civ1_new;
2083    % get image names
2084    for ifile=1:nbfield
2085        for j=1:nbslice
2086             filename=fullfile_uvmat(RootPath,'',RootFile_ima1,ext_ima,NomType_ima1,i1_civ1(ifile),[],j1_civ1(j));
2087            idetect(j)=exist(filename,'file')==2;
2088            filecell.ima1.civ1(ifile,j)={filename}; %first image
2089            filename=fullfile_uvmat(RootPath,'',RootFile_ima2,ext_ima,NomType_ima2,i2_civ1(ifile),[],j2_civ1(j));
2090            idetect_1(j)=exist(filename,'file')==2;
2091            filecell.ima2.civ1(ifile,j)={filename};%second image
2092        end
2093        [idetectmin,indexj]=min(idetect);
2094        if idetectmin==0,
2095            msgbox_uvmat('ERROR',[filecell.ima1.civ1{ifile,indexj} ' not found'])
2096            filecell={};
2097            return
2098        end
2099        [idetectmin,indexj]=min(idetect_1);
2100        if idetectmin==0,
2101            msgbox_uvmat('ERROR',[filecell.ima2.civ1{ifile,indexj} ' not found'])
2102            filecell={};
2103            return
2104        end
2105    end
2106    if strcmp(compare,'stereo PIV') && (strcmp(mode,'pair j1-j2') || strcmp(mode,'series(Dj)') || strcmp(mode,'series(Di)'))
2107        for ifile=1:nbfield
2108            for j=1:nbslice
2109                filename=fullfile_uvmat(RootPath,'',RootFile_A,ext_ima,NomType_ima1,i1_civ1(ifile),[],j1_civ1(j));
2110                idetect(j)=exist(filename,'file')==2;
2111                filecell.imaA1.civ1(ifile,j)={filename} ;%first image
2112                filename=fullfile_uvmat(RootPath,'',RootFile_A,ext_ima,NomType_ima2,i2_civ1(ifile),[],j2_civ1(j));
2113                idetect_1(j)=exist(filename,'file')==2;
2114                filecell.imaA2.civ1(ifile,j)={filename};%second image
2115            end
2116            [idetectmin,indexj]=min(idetect);
2117            if idetectmin==0,
2118                msgbox_uvmat('ERROR',[filecell.imaA1.civ1{ifile,indexj} ' not found'])
2119                filecell={};
2120               % cd(currentdir)
2121                return
2122            end
2123            [idetectmin,indexj]=min(idetect_1);
2124            if idetectmin==0,
2125                msgbox_uvmat('ERROR',[filecell.imaA2.civ1{ifile,indexj} ' not found'])
2126                filecell={};
2127               % cd(currentdir)
2128                return
2129            end
2130        end
2131    end
2132   
2133    %%%%%%%%%%%%%  checkfix1 or checkpatch1 activated but no checkciv1   %%%%%%%%%%%%%
2134elseif (checkbox(2)==1 || checkbox(3)==1);
2135    for ifile=1:nbfield
2136        for j=1:nbslice
2137            filename=fullfile_uvmat(RootPath,subdir_civ1,RootFile_nc,'.nc',NomType_nc,i1_civ1(ifile),i2_civ1(ifile),j1_civ1(j),j2_civ1(j));
2138            detect=exist(filename,'file')==2;
2139            if detect==0
2140                msgbox_uvmat('ERROR',[filename ' not found'])
2141                filecell={};
2142               % cd(currentdir)
2143                return
2144            end
2145            filecell.nc.civ1(ifile,j)={filename};
2146        end
2147    end
2148    if strcmp(compare,'stereo PIV')
2149        for ifile=1:nbfield
2150            for j=1:nbslice
2151                filename=fullfile_uvmat(RootPath,subdir_civ1,RootFile_A,'.nc',NomType_nc,i1_civ1(ifile),i2_civ1(ifile),j1_civ1(j),j2_civ1(j));
2152                filecell.ncA.civ1(ifile,j)={filename};
2153                if ~exist(filename,'file')
2154                    msgbox_uvmat('ERROR',['input file ' filename ' not found'])
2155                    set(handles.RUN, 'Enable','On')
2156                    set(handles.RUN,'BackgroundColor',[1 0 0])
2157                    filecell={};
2158                    %cd(currentdir)
2159                    return
2160                end
2161            end
2162        end
2163    end
2164end
2165
2166%%%%%%%%%%%%%  if checkciv2 performed with pairs different than checkciv1  %%%%%%%%%%%%%
2167testdiff=0;
2168if (checkbox(4)==1)&&...
2169        ((get(handles.ListPairCiv1,'Value')~=get(handles.ListPairCiv2,'Value'))||~strcmp(subdir_civ2,subdir_civ1))
2170    testdiff=1;
2171    detect=1;
2172    vers=0;
2173    subdir_civ2_new=subdir_civ2;
2174    while detect==1 %create a new subdir if the netcdf files already exist
2175        for ifile=1:nbfield
2176            for j=1:nbslice
2177                filename=fullfile_uvmat(RootPath,subdir_civ2_new,RootFile_nc,'.nc',NomType_nc,i1_civ2(ifile),i2_civ2(ifile),j1_civ2(j),j2_civ2(j));
2178                detect=exist(filename,'file')==2;
2179                if detect% if a netcdf file already exists
2180                    indstr=regexp(subdir_civ2,'\D');
2181                    if indstr(end)<length(subdir_civ2) %subdir_civ1 ends by a number
2182                        vers=str2double(subdir_civ2(indstr(end)+1:end))+1;
2183                        subdir_civ2_new=[subdir_civ2(1:indstr(end)) num2str(vers)];
2184                    else
2185                        vers=vers+1;
2186                        subdir_civ2_new=[subdir_civ1 '_' num2str(vers)];
2187                    end
2188                    break
2189                end
2190                filecell.nc.civ2(ifile,j)={filename};
2191            end
2192            if detect% if a netcdf file already exists
2193                break
2194            end
2195        end
2196        %create the new subdir_civ2_new
2197        if ~exist(fullfile(RootPath,subdir_civ2_new),'dir')
2198            [xx,m2]=mkdir(fullfile(RootPath,subdir_civ2_new));
2199            [xx,msg2] = fileattrib(fullfile(RootPath,subdir_civ2_new),'+w','g'); %yield writing access (+w) to user group (g)
2200            if ~isequal(m2,'')
2201                msgbox_uvmat('ERROR',['cannot create ' fullfile(RootPath,subdir_civ2_new) ': ' m2])
2202                filecell={};
2203                return
2204            end
2205        end
2206        if strcmp(compare,'stereo PIV')%check second nc series
2207            for ifile=1:nbfield
2208                for j=1:nbslice
2209                    filename=fullfile_uvmat(RootPath,subdir_civ2_new,RootFile_A,'.nc',NomType_nc,i1_civ2(ifile),i2_civ2(ifile),j1_civ2(j),j2_civ2(j));
2210                    detect=exist(filename,'file')==2;
2211                    if detect% if a netcdf file already exists
2212                        indstr=regexp(subdir_civ2,'\D');
2213                        if indstr(end)<length(subdir_civ2) %subdir_civ1 ends by a number
2214                           vers=str2double(subdir_civ2(indstr(end)+1:end))+1;
2215                           subdir_civ2_new=[subdir_civ2(1:indstr(end)) num2str(vers)];
2216                        else
2217                           vers=vers+1;
2218                           subdir_civ2_new=[subdir_civ1 '_' num2str(vers)];
2219                        end
2220                        break
2221                    end
2222                    filecell.ncA.civ2(ifile,j)={filename};
2223                end
2224                if detect% if a netcdf file already exists
2225                    break
2226                end
2227            end
2228            subdir_civ2=subdir_civ2_new;
2229            %create the new SubdirCiv1
2230            if ~exist(fullfile(RootPath,subdir_civ2_new),'dir')
2231                [xx,m2]=mkdir(subdir_civ2_new);
2232                 [xx,msg2] = fileattrib(fullfile(RootPath,subdir_civ2_new),'+w','g'); %yield writing access (+w) to user group (g)
2233                if ~isequal(m2,'')
2234                    msgbox_uvmat('ERROR', ['cannot create ' fullfile(RootPath,subdir_civ2_new) ': ' m2])%error message for directory creation
2235                  %  cd(currentdir)
2236                    filecell={};
2237                    return
2238                end
2239            end
2240        end
2241    end
2242    subdir_civ2=subdir_civ2_new;
2243end
2244%cd(currentdir);%come back to the current working directory
2245
2246%%%%%%%%%%%%%  if checkciv2 results are obtained or used  %%%%%%%%%%%%%
2247if checkbox(4)==1 || checkbox(5)==1 || checkbox(6)==1 %civ2
2248    %check source netcdf file of checkciv1 estimates
2249    if checkbox(1)==0; %no civ1 performed
2250        for ifile=1:nbfield
2251            for j=1:nbslice
2252                filename=fullfile_uvmat(RootPath,subdir_civ1,RootFile_nc,'.nc',NomType_nc,i1_civ1(ifile),i2_civ1(ifile),j1_civ1(j),j2_civ1(j));%
2253                filecell.nc.civ1(ifile,j)={filename};% name of the civ1 file
2254                if ~exist(filename,'file')
2255                    msgbox_uvmat('ERROR',['input file ' filename ' not found'])
2256                    filecell={};
2257                    return
2258                end
2259                if ~testdiff % civ2 or patch2 are written in the same file as civ1
2260                    if checkbox(4)==0 ; %check the existence of civ2 if it is not calculated
2261                        Data=nc2struct(filename,'ListGlobalAttribute','CivStage','civ2');
2262                        if isfield(Data,'Txt')
2263                            msgbox_uvmat('ERROR',Data.Txt);
2264                            return
2265                        elseif ~isempty(Data.CivStage)% case of new civ files
2266                            if Data.CivStage<4 %test for civ files
2267                            msgbox_uvmat('ERROR',['no civ2 data in ' filename])
2268                            filecell=[];
2269                            return
2270                            end
2271                        elseif isempty(Data.civ2)||isequal(Data.civ2,0)
2272                            msgbox_uvmat('ERROR',['no civ2 data in ' filename])
2273                            filecell=[];
2274                            return
2275                        end
2276                    elseif checkbox(3)==0; %check the existence of patch if it is not calculated
2277                        Data=nc2struct(filename,'ListGlobalAttribute','CivStage','patch');
2278                        if ~isempty(Data.CivStage)
2279                            if Data.CivStage<3 %test for civ files
2280                                msgbox_uvmat('ERROR',['no patch data in ' filename])
2281                                filecell=[];
2282                                return
2283                            end
2284                        elseif isempty(Data.patch)||isequal(Data.patch,0)
2285                            msgbox_uvmat('ERROR',['no patch data in ' filename])
2286                            filecell=[];
2287                            return
2288                        end
2289                    end
2290                end
2291            end
2292        end
2293        if strcmp(compare,'stereo PIV')
2294            for ifile=1:nbfield
2295                for j=1:nbslice
2296                    filename=fullfile_uvmat(RootPath,subdir_civ2,RootFile_A,'.nc',NomType_nc,i1_civ2(ifile),i2_civ2(ifile),j1_civ2(j),j2_civ2(j));
2297                    filecell.ncA.civ2(ifile,j)={filename};
2298                    if ~exist(filename,'file')
2299                        msgbox_uvmat('ERROR',['input file ' filename ' not found'])
2300                        set(handles.RUN, 'Enable','On')
2301                        set(handles.RUN,'BackgroundColor',[1 0 0])
2302                        return
2303                    end
2304                end
2305            end
2306        end
2307    end
2308   
2309    detect=1;
2310    %     while detect==1%creates a new subdir if the netcdf files already contain checkciv2 data
2311    for ifile=1:nbfield
2312        for j=1:nbslice
2313            filename=fullfile_uvmat(RootPath,subdir_civ2,RootFile_nc,'.nc',NomType_nc,i1_civ2(ifile),i2_civ2(ifile),j1_civ2(j),j2_civ2(j));
2314            detect=exist(filename,'file')==2;
2315            filecell.nc.civ2(ifile,j)={filename};
2316        end
2317    end
2318    %get first image names for checkciv2
2319    if checkbox(1)==1 && isequal(i1_civ1,i1_civ2) && isequal(j1_civ1,j1_civ2)
2320        filecell.ima1.civ2=filecell.ima1.civ1;
2321    elseif checkbox(4)==1
2322        for ifile=1:nbfield
2323            for j=1:nbslice
2324                filename=fullfile_uvmat(RootPath,[],RootFile_ima1,ext_ima,NomType_ima1,i1_civ2(ifile),[],j1_civ2(j));
2325                idetect_2(j)=exist(filename,'file')==2;
2326                filecell.ima1.civ2(ifile,j)={filename};%first image
2327            end
2328            [idetectmin,indexj]=min(idetect_2);
2329            if idetectmin==0,
2330                msgbox_uvmat('ERROR',['input image ' filecell.ima1.civ2{ifile,indexj} ' not found'])
2331                filecell=[];
2332                return
2333            end
2334        end
2335    end
2336   
2337    %get second image names for checkciv2
2338    if checkbox(1)==1 && isequal(i2_civ1,i2_civ2) && isequal(j2_civ1,j2_civ2)
2339        filecell.ima2.civ2=filecell.ima2.civ1;
2340    elseif checkbox(4)==1
2341        for ifile=1:nbfield
2342            for j=1:nbslice
2343                filename=fullfile_uvmat(RootPath,[],RootFile_ima2,ext_ima,NomType_ima2,i2_civ2(ifile),[],j2_civ2(j));
2344                idetect_3(j)=exist(filename,'file')==2;
2345                filecell.ima2.civ2(ifile,j)={filename};%first image
2346            end
2347            [idetectmin,indexj]=min(idetect_3);
2348            if idetectmin==0,
2349                msgbox_uvmat('ERROR',['input image ' filecell.ima2.civ2{ifile,indexj} ' not found'])
2350                filecell=[];
2351                return
2352            end
2353        end
2354    end
2355end
2356if (checkbox(5) || checkbox(6)) && ~checkbox(4)  % need to read an existing netcdf civ2 file
2357    if ~testdiff
2358        filecell.nc.civ2=filecell.nc.civ1;% file already checked
2359    else     % check the civ2 files
2360        for ifile=1:nbfield
2361            for j=1:nbslice
2362                 filename=fullfile_uvmat(RootPath,subdir_civ2,RootFile_nc,'.nc',NomType_nc,i1_civ2(ifile),i2_civ2(ifile),j1_civ2(j),j2_civ2(j));
2363                filecell.nc.civ2(ifile,j)={filename};
2364                if ~exist(filename,'file')
2365                    msgbox_uvmat('ERROR',['input file ' filename ' not found'])
2366                    filecell=[];
2367                    return
2368                else
2369                    Data=nc2struct(filename,'ListGlobalAttribute','CivStage','civ2');
2370                    if ~isempty(Data.CivStage) && Data.CivStage<4 %test for civ files
2371                            msgbox_uvmat('ERROR',['no civ2 data in ' filename])
2372                            filecell=[];
2373                            return
2374                    elseif isempty(Data.civ2)||isequal(Data.civ2,0)
2375                        msgbox_uvmat('ERROR',['no civ2 data in ' filename])
2376                        filecell=[];
2377                        return
2378                    end
2379                end
2380            end
2381        end
2382    end
2383end
2384
2385%%%%%%%%%%%%%  if stereo fields are calculated by PATCH %%%%%%%%%%%%%
2386if strcmp(compare,'stereo PIV')
2387    if  checkbox(3) && isequal(get(handles.test_stereo1,'Value'),1)
2388        for ifile=1:nbfield
2389            for j=1:nbslice
2390                 filename=fullfile_uvmat(RootPath,subdir_civ1,RootFile_AB,'.nc',NomType_nc,i1_civ1(ifile),i2_civ1(ifile),j1_civ1(j),j2_civ1(j));
2391                filecell.st(ifile,j)={filename};
2392            end
2393        end
2394    end
2395    if  checkbox(6) && isequal(get(handles.CheckStereo,'Value'),1)
2396        for ifile=1:nbfield
2397            for j=1:nbslice
2398                 filename=fullfile_uvmat(RootPath,subdir_civ2,RootFile_AB,'.nc',NomType_nc,i1_civ2(ifile),i2_civ2(ifile),j1_civ2(j),j2_civ2(j));
2399                filecell.st(ifile,j)={filename};
2400            end
2401        end
2402    end
2403end
2404set(handles.SubdirCiv1,'String',subdir_civ1);%update the edit box
2405set(handles.SubdirCiv2,'String',subdir_civ2);%update the edit box
2406
2407%COPY IMAGES TO THE FORMAT .png IF NEEDED
2408if isequal(NomType_ima1,'*')%case of movie files
2409    NomType_imanew1='_i';
2410else
2411    NomType_imanew1=NomType_ima1;
2412end
2413if isequal(NomType_ima2,'*')%case of movie files
2414    NomType_imanew2='_i';
2415else
2416    NomType_imanew2=NomType_ima2;
2417end
2418if ~isequal(ext_ima,'.png')
2419    %%type of image file
2420    type_ima1='none';%default
2421    movieobject1=[];%default
2422    if strcmpi(ext_ima,'.avi')
2423        hhh=which('mmreader');
2424        if ~isequal(hhh,'')&& mmreader.isPlatformSupported()% if the mmreader function is found (recent version of matlab)
2425            type_ima1='movie';
2426            movieobject1=mmreader([filebase_ima2 ext_ima]);
2427        else
2428            type_ima1='avi';
2429        end
2430    elseif ischar(ext_ima) && ~isempty(ext_ima(2:end))
2431        form=imformats(ext_ima(2:end));
2432        if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
2433            if isequal(NomType_ima1,'*');
2434                type_ima1='multimage';%image series in a single image file
2435            else
2436                type_ima1='image';
2437            end
2438        end
2439    end
2440    type_ima2='none';%default
2441    movieobject2=[];
2442    if strcmpi(ext_ima,'.avi')
2443        hhh=which('mmreader');
2444        if ~isequal(hhh,'')&& mmreader.isPlatformSupported()% if the mmreader function is found (recent version of matlab)
2445            type_ima2='movie';
2446            movieobject2=mmreader([filebase_ima2 ext_ima]);
2447        else
2448            type_ima2='avi';
2449        end
2450    elseif ischar(ext_ima) && ~isempty(ext_ima(2:end))
2451        form=imformats(ext_ima(2:end));
2452        if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
2453            if isequal(NomType_ima1,'*');
2454                type_ima2='multimage';%image series in a single image file
2455            else
2456                type_ima2='image';
2457            end
2458        end
2459    end
2460    if checkbox(1) %if civ1 is performed
2461        h = waitbar(0,'copy images to the .png format for civ1');% display a wait bar
2462        for ifile=1:nbfield
2463            waitbar(ifile/nbfield);
2464            for j=1:nbslice
2465                filename=fullfile_uvmat(RootPath,[],RootFile_ima1,'.png',NomType_imanew1,i1_civ1(ifile),[],j1_civ1(j));
2466                if ~exist(filename,'file')
2467                    A=read_image(filecell.ima1.civ1{ifile,j},type_ima1,i1_civ1(ifile),movieobject1);
2468                    imwrite(A,filename,'BitDepth',16);
2469                end
2470                filecell.ima1.civ1(ifile,j)={filename};
2471                filename=fullfile_uvmat(RootPath,[],RootFile_ima2,'.png',NomType_imanew2,i2_civ1(ifile),[],j2_civ1(j));
2472                if ~exist(filename,'file')
2473                    A=read_image(filecell.ima2.civ1{ifile,j},type_ima2,i2_civ1(ifile),movieobject2);
2474                    imwrite(A,filename,'BitDepth',16);
2475                end
2476                filecell.ima2.civ1(ifile,j)={filename};
2477            end
2478        end
2479        close(h)
2480    end
2481    if checkbox(4) %if civ2 is performed
2482        h = waitbar(0,'copy images to the .png format for civ2');% display a wait bar
2483        for ifile=1:nbfield
2484            waitbar(ifile/nbfield);
2485            for j=1:nbslice
2486                filename=fullfile_uvmat(RootPath,[],RootFile_ima1,'.png',NomType_imanew1,i1_civ2(ifile),[],j1_civ2(j));
2487                if ~exist(filename,'file')
2488                    A=read_image(cell2mat(filecell.ima1.civ2(ifile,j)),type_ima2,i1_civ2(ifile));
2489                    imwrite(A,filename,'BitDepth',16);
2490                end
2491                filecell.ima1.civ2(ifile,j)={filename};
2492                filename=fullfile_uvmat(RootPath,[],RootFile_ima2,'.png',NomType_imanew2,i2_civ2(ifile),[],j2_civ2(j));
2493                if ~exist(filename,'file')
2494                    A=read_image(cell2mat(filecell.ima2.civ2(ifile,j)),type_ima2,i2_civ2(ifile));
2495                    imwrite(A,filename,'BitDepth',16);
2496                end
2497                filecell.ima2.civ2(ifile,j)={filename};
2498            end
2499        end
2500        close(h);
2501    end
2502end
2503
2504%------------------------------------------------------------------------
2505% --- determine the list of index pairs of processing file
2506function [num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2]=...
2507    find_pair_indices(handles,ref_i,ref_j,mode)
2508%------------------------------------------------------------------------
2509
2510list_civ1=get(handles.ListPairCiv1,'String');
2511index_civ1=get(handles.ListPairCiv1,'Value');
2512str_civ1=list_civ1{index_civ1};%string defining the image pairs for civ1
2513if isempty(str_civ1)||isequal(str_civ1,'')
2514    msgbox_uvmat('ERROR','no image pair selected for civ1')
2515    return
2516end
2517list_civ2=get(handles.ListPairCiv2,'String');
2518index_civ2=get(handles.ListPairCiv2,'Value');
2519if index_civ2>length(list_civ2)
2520    list_civ2=list_civ1;
2521    index_civ2=index_civ1;
2522end
2523str_civ2=list_civ2{index_civ2};%string defining the image pairs for civ2
2524
2525if isequal (mode,'series(Di)')
2526    lastfield=str2double(get(handles.nb_field,'String'));
2527    num1_civ1=ref_i-floor(index_civ1/2)*ones(size(ref_i));% set of first image numbers
2528    num2_civ1=ref_i+ceil(index_civ1/2)*ones(size(ref_i));
2529    num_a_civ1=ref_j;
2530    num_b_civ1=ref_j;
2531    num1_civ2=ref_i-floor(index_civ2/2)*ones(size(ref_i));
2532    num2_civ2=ref_i+ceil(index_civ2/2)*ones(size(ref_i));
2533    num_a_civ2=ref_j;
2534    num_b_civ2=ref_j;   
2535   
2536    % adjust the first and last field number
2537    lastfield=str2double(get(handles.nb_field,'String'));
2538    if isnan(lastfield)
2539        indsel=find((num1_civ1 >= 1)&(num1_civ2 >= 1));
2540    else
2541        indsel=find((num2_civ1 <= lastfield)&(num2_civ2 <= lastfield)&(num1_civ1 >= 1)&(num1_civ2 >= 1));
2542    end
2543    if length(indsel)>=1
2544        firstind=indsel(1);
2545        lastind=indsel(end);
2546        set(handles.first_i,'String',num2str(ref_i(firstind)))%update the display of first and last fields
2547        set(handles.last_i,'String',num2str(ref_i(lastind)))
2548        ref_i=ref_i(indsel);
2549        num1_civ1=num1_civ1(indsel);
2550        num1_civ2=num1_civ2(indsel);
2551        num2_civ1=num2_civ1(indsel);
2552        num2_civ2=num2_civ2(indsel);
2553    end
2554elseif isequal (mode,'series(Dj)')
2555    lastfield_j=str2double(get(handles.nb_field2,'String'));
2556    num1_civ1=ref_i;% set of first image numbers
2557    num2_civ1=ref_i;
2558    num_a_civ1=ref_j-floor(index_civ1/2)*ones(size(ref_j));
2559    num_b_civ1=ref_j+ceil(index_civ1/2)*ones(size(ref_j));
2560    num1_civ2=ref_i;
2561    num2_civ2=ref_i;
2562    num_a_civ2=ref_j-floor(index_civ2/2)*ones(size(ref_j));
2563    num_b_civ2=ref_j+ceil(index_civ2/2)*ones(size(ref_j));
2564    % adjust the first and last field number
2565    if isnan(lastfield_j)
2566        indsel=find((num_a_civ1 >= 1)&(num_a_civ2 >= 1));
2567    else
2568        indsel=find((num_b_civ1 <= lastfield_j)&(num_b_civ2 <= lastfield_j)&(num_a_civ1 >= 1)&(num_a_civ2 >= 1));
2569    end
2570    if length(indsel)>=1
2571        firstind=indsel(1);
2572        lastind=indsel(end);
2573        set(handles.first_j,'String',num2str(ref_j(firstind)))%update the display of first and last fields
2574        set(handles.last_j,'String',num2str(ref_j(lastind)))
2575        ref_j=ref_j(indsel);
2576        num_a_civ1=num_a_civ1(indsel);
2577        num_b_civ1=num_b_civ1(indsel);
2578        num_a_civ2=num_a_civ2(indsel);
2579        num_b_civ2=num_b_civ2(indsel);
2580    end
2581elseif isequal(mode,'pair j1-j2') %case of bursts (png_old or png_2D)
2582    displ_num=get(handles.ListPairCiv1,'UserData');
2583    num1_civ1=ref_i;
2584    num2_civ1=ref_i;
2585    num_a_civ1=displ_num(1,index_civ1);
2586    num_b_civ1=displ_num(2,index_civ1);
2587    num1_civ2=ref_i;
2588    num2_civ2=ref_i;
2589    num_a_civ2=displ_num(1,index_civ2);
2590    num_b_civ2=displ_num(2,index_civ2);
2591elseif isequal(mode,'displacement')
2592    num1_civ1=ref_i;
2593    num2_civ1=ref_i;
2594    num_a_civ1=ref_j;
2595    num_b_civ1=ref_j;
2596    num1_civ2=ref_i;
2597    num2_civ2=ref_i;
2598    num_a_civ2=ref_j;
2599    num_b_civ2=ref_j;
2600end
2601
2602%------------------------------------------------------------------------
2603% --- Executes on button press in ListCompareMode.
2604function ListCompareMode_Callback(hObject, eventdata, handles)
2605%------------------------------------------------------------------------
2606ListCompareMode=get(handles.ListCompareMode,'String');
2607option=ListCompareMode{get(handles.ListCompareMode,'Value')};
2608if ~strcmp(option,'PIV') % case 'displacement' or 'stereo PIV'
2609    filebase=get(handles.RootPath,'String');
2610    set(handles.sub_txt,'Visible','on')
2611    set(handles.RootFile_1,'Visible','On');%mkes the second file input window visible
2612    mode_store=get(handles.ListPairMode,'String');%get the present 'mode'
2613    set(handles.ListCompareMode,'UserData',mode_store);%store the mode display
2614    set(handles.ListPairMode,'Visible','off')
2615   
2616    %% open an image file with the browser
2617    ind_opening=1;%default
2618    browse.incr_pair=[0 0]; %default
2619    oldfile=get(handles.RootPath,'String');
2620    menu={'*.png;*.jpg;*.tif;*.avi;*.AVI;', ' (*.png,*.jpg ,.tif, *.avi,*.AVI)';
2621        '*.png','.png image files'; ...
2622        '*.jpg',' jpeg image files'; ...
2623        '*.tif','.tif image files'; ...
2624        '*.avi;*.AVI','.avi movie files'; ...
2625        '*.*',  'All Files (*.*)'};
2626    if strcmp(option,'displacement')
2627        comment='Pick the reference file for displacements';
2628    else
2629        comment='Pick a file of the second series';
2630    end
2631    [FileName, PathName] = uigetfile( menu, comment,oldfile);
2632    fileinput=[PathName FileName];%complete file name
2633    sizf=size(fileinput);
2634    if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
2635    [path,name,ext]=fileparts(fileinput);
2636    [path1]=fileparts(filebase);
2637    if isunix
2638        [status,path]=system(['readlink ' path]);
2639        [status,path1]=system(['readlink ' path1]);% look for the true path in case of symbolic paths
2640    end
2641    if ~strcmp(path1,path)
2642        msgbox_uvmat('ERROR','The second image or series must be in the same directory as the first one')
2643        return
2644    end
2645    if strcmp(option,'displacement')
2646        [tild,RootFile_1]=fileparts(name);
2647    else
2648        [tild,tild,RootFile_1,tild,tild,tild,tild,tild,nom_type_1]=fileparts_uvmat(fileinput);
2649        [RootFile_1,i1_series,tild,j1_series,tild,nom_type_1,FileType,Object]=find_file_series(PathName,FileName);
2650        %check image nom type
2651        if ~strcmp(nom_type_1,get(handles.NomType,'String'))
2652        msgbox_uvmat('ERROR','The second image series must have the same indexing type as the first one, or use the option displacement for a fixed image')
2653        return
2654        end
2655    end   
2656    %check image  extension
2657    if ~strcmp(ext,get(handles.ImaExt,'String'))
2658        msgbox_uvmat('ERROR','The second image series must have the same extension name as the first one')
2659        return
2660    end
2661    set(handles.RootFile_1,'String',RootFile_1);
2662else
2663    set(handles.ListPairMode,'Visible','on')
2664    set(handles.RootFile_1,'Visible','Off');
2665    set(handles.sub_txt,'Visible','off')
2666    set(handles.RootFile_1,'String',[]);
2667    mode_store=get(handles.ListCompareMode,'UserData');
2668    set(handles.ListPairMode,'Value',1)
2669    set(handles.ListPairMode,'String',mode_store)
2670    set(handles.CheckStereo,'Value',0)
2671    set(handles.ListPairMode,'Value',1) % mode 'civX' selected by default
2672end
2673% if strcmp(option,'stereo PIV') && get(handles.CheckPatch1,'Value')
2674%     set(handles.CheckStereo,'Visible','on')
2675% else
2676%     set(handles.CheckStereo,'Visible','off')
2677% end
2678% if strcmp(option,'stereo PIV') && get(handles.CheckPatch2,'Value')
2679%     set(handles.CheckStereo,'Visible','on')
2680% else
2681%     set(handles.CheckStereo,'Visible','off')
2682% end
2683ListPairMode_Callback(hObject, eventdata, handles)
2684
2685
2686%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2687% Callbacks in the uipanel Pair Indices
2688%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2689%------------------------------------------------------------------------
2690% --- Executes on button press in ListPairMode.
2691function ListPairMode_Callback(hObject, eventdata, handles)
2692%------------------------------------------------------------------------
2693compare_list=get(handles.ListCompareMode,'String');
2694val=get(handles.ListCompareMode,'Value');
2695compare=compare_list{val};
2696if strcmp(compare,'displacement')||strcmp(compare,'shift')
2697    mode='displacement';
2698else
2699    mode_list=get(handles.ListPairMode,'String');
2700    if ischar(mode_list)
2701        mode_list={mode_list};
2702    end
2703    mode_value=get(handles.ListPairMode,'Value');
2704    mode=mode_list{mode_value};
2705end
2706displ_num=[];%default
2707ref_i=str2double(get(handles.ref_i,'String'));
2708% last_i=str2num(get(handles.last_i,'String'));
2709time=get(handles.ImaDoc,'UserData'); %get the set of times
2710TimeUnit=get(handles.TimeUnit,'String');
2711checkframe=strcmp(TimeUnit,'frame');
2712siztime=size(time);
2713nbfield=siztime(1)-1;
2714nbfield2=siztime(2)-1;
2715indchosen=1;  %%first pair selected by default
2716%displ_num used to define the indices of the civ pairs
2717% in mode 'pair j1-j2', j1 and j2 are the file indices, else the indices
2718% are relative to the reference indices ref_i and ref_j respectively.
2719if isequal(mode,'pair j1-j2')%| isequal(mode,'st_pair j1-j2')
2720    dt=1;
2721    displ='';
2722    index=0;
2723    numlist_a=[];
2724    numlist_B=[];
2725    %get all the time intervals in bursts
2726    displ_dt=1;%default
2727    nbfield2=min(nbfield2,10);%limitate the number of pairs to 10x10
2728    for numod_a=1:nbfield2-1 %nbfield2 always >=2 for 'pair j1-j2' mode
2729        for numod_b=(numod_a+1):nbfield2
2730            index=index+1;
2731            numlist_a(index)=numod_a;
2732            numlist_b(index)=numod_b;
2733            if size(time,2)>1 && ~checkframe
2734                dt(numod_a,numod_b)=time(ref_i+1,numod_b+1)-time(ref_i+1,numod_a+1);%first time interval dt
2735                displ_dt(index)=dt(numod_a,numod_b);
2736            else
2737                displ_dt(index)=1;
2738            end
2739        end
2740    end
2741    [dtsort,indsort]=sort(displ_dt);
2742    if ~isempty(numlist_a)
2743        displ_num(1,:)=numlist_a(indsort);
2744        displ_num(2,:)=numlist_b(indsort);
2745    end
2746    displ_num(3,:)=0;
2747    displ_num(4,:)=0;
2748    enable_j(handles, 'off')
2749elseif isequal(mode,'series(Dj)') %| isequal(mode,'st_series(Dj)')
2750    index=1:200;
2751    displ_num(1,index)=-floor(index/2);
2752    displ_num(2,index)=ceil(index/2);
2753    displ_num(3:4,index)=zeros(2,200);
2754    enable_j(handles, 'on')
2755elseif isequal(mode,'series(Di)') %| isequal(mode,'st_series(Di)')
2756    index=1:200;
2757    displ_num(1:2,index)=zeros(2,200);
2758    displ_num(3,index)=-floor(index/2);
2759    displ_num(4,index)=ceil(index/2);
2760    enable_i(handles, 'on')
2761    if nbfield2 > 1
2762        enable_j(handles, 'on')
2763    else
2764        enable_j(handles, 'off')
2765    end
2766elseif isequal(mode,'displacement')%the pairs have the same indices
2767    displ_num(1,1)=0;
2768    displ_num(2,1)=0;
2769    displ_num(3,1)=0;
2770    displ_num(4,1)=0;
2771    if nbfield > 1 || nbfield==0
2772        enable_i(handles, 'on')
2773    else
2774        enable_j(handles, 'off')
2775    end
2776    if nbfield2 > 1
2777        enable_j(handles, 'on')
2778    else
2779        enable_j(handles, 'off')
2780    end
2781end
2782set(handles.ListPairCiv1,'UserData',displ_num);
2783errormsg=find_netcpair_civ( handles,1);
2784    if ~isempty(errormsg)
2785    msgbox_uvmat('ERROR',errormsg)
2786    end
2787% find_netcpair_civ2(handles)
2788
2789function enable_i(handles, state)
2790set(handles.itext,'Visible',state)
2791set(handles.first_i,'Visible',state)
2792set(handles.last_i,'Visible',state)
2793set(handles.incr_i,'Visible',state)
2794set(handles.nb_field,'Visible',state)
2795set(handles.ref_i,'Visible',state)
2796
2797function enable_j(handles, state)
2798set(handles.jtext,'Visible',state)
2799set(handles.first_j,'Visible',state)
2800set(handles.last_j,'Visible',state)
2801set(handles.incr_j,'Visible',state)
2802set(handles.nb_field2,'Visible',state)
2803set(handles.ref_j,'Visible',state)
2804
2805
2806%------------------------------------------------------------------------
2807% --- Executes on selection change in ListPairCiv1.
2808function ListPairCiv1_Callback(hObject, eventdata, handles)
2809%------------------------------------------------------------------------
2810%reproduce by default the chosen pair in the checkciv2 menu
2811list_pair=get(handles.ListPairCiv1,'String');%get the menu of image pairs
2812index_pair=get(handles.ListPairCiv1,'Value');
2813displ_num=get(handles.ListPairCiv1,'UserData');
2814% num_a=displ_num(1,index_pair);
2815% num_b=displ_num(2,index_pair);
2816list_pair2=get(handles.ListPairCiv2,'String');%get the menu of image pairs
2817if index_pair<=length(list_pair2)
2818    set(handles.ListPairCiv2,'Value',index_pair);
2819end
2820
2821%update first_i and last_i according to the chosen image pairs
2822mode_list=get(handles.ListPairMode,'String');
2823mode_value=get(handles.ListPairMode,'Value');
2824mode=mode_list{mode_value};
2825if isequal(mode,'series(Di)')
2826    first_i=str2double(get(handles.first_i,'String'));
2827    last_i=str2double(get(handles.last_i,'String'));
2828    incr_i=str2double(get(handles.incr_i,'String'));
2829    num1=first_i:incr_i:last_i;
2830    lastfield=str2double(get(handles.nb_field,'String'));
2831    if ~isnan(lastfield)
2832        test_find=(num1-floor(index_pair/2)*ones(size(num1))>0)& ...
2833            (num1+ceil(index_pair/2)*ones(size(num1))<=lastfield);
2834        num1=num1(test_find);
2835    end
2836    set(handles.first_i,'String',num2str(num1(1)));
2837    set(handles.last_i,'String',num2str(num1(end)));
2838elseif isequal(mode,'series(Dj)')
2839    first_j=str2double(get(handles.first_j,'String'));
2840    last_j=str2double(get(handles.last_j,'String'));
2841    incr_j=str2double(get(handles.incr_j,'String'));
2842    num_j=first_j:incr_j:last_j;
2843    lastfield2=str2double(get(handles.nb_field2,'String'));
2844    if ~isnan(lastfield2)
2845        test_find=(num_j-floor(index_pair/2)*ones(size(num_j))>0)& ...
2846            (num_j+ceil(index_pair/2)*ones(size(num_j))<=lastfield2);
2847        num1=num_j(test_find);
2848    end
2849    set(handles.first_j,'String',num2str(num1(1)));
2850    set(handles.last_j,'String',num2str(num1(end)));
2851end
2852
2853%------------------------------------------------------------------------
2854% --- Executes on selection change in ListPairCiv2.
2855function ListPairCiv2_Callback(hObject, eventdata, handles)
2856%------------------------------------------------------------------------
2857index_pair=get(handles.ListPairCiv2,'Value');%get the selected position index in the menu
2858
2859%update first_i and last_i according to the chosen image pairs
2860mode_list=get(handles.ListPairMode,'String');
2861mode_value=get(handles.ListPairMode,'Value');
2862mode=mode_list{mode_value};
2863if isequal(mode,'series(Di)')
2864    first_i=str2double(get(handles.first_i,'String'));
2865    last_i=str2double(get(handles.last_i,'String'));
2866    incr_i=str2double(get(handles.incr_i,'String'));
2867    num1=first_i:incr_i:last_i;
2868    lastfield=str2double(get(handles.nb_field,'String'));
2869    if ~isnan(lastfield)
2870        test_find=(num1-floor(index_pair/2)*ones(size(num1))>0)& ...
2871            (num1+ceil(index_pair/2)*ones(size(num1))<=lastfield);
2872        num1=num1(test_find);
2873    end
2874    set(handles.first_i,'String',num2str(num1(1)));
2875    set(handles.last_i,'String',num2str(num1(end)));
2876elseif isequal(mode,'series(Dj)')
2877    first_j=str2double(get(handles.first_j,'String'));
2878    last_j=str2double(get(handles.last_j,'String'));
2879    incr_j=str2double(get(handles.incr_j,'String'));
2880    num_j=first_j:incr_j:last_j;
2881    lastfield2=str2double(get(handles.nb_field2,'String'));
2882    if ~isnan(lastfield2)
2883        test_find=(num_j-floor(index_pair/2)*ones(size(num_j))>0)& ...
2884            (num_j+ceil(index_pair/2)*ones(size(num_j))<=lastfield2);
2885        num1=num_j(test_find);
2886    end
2887    set(handles.first_j,'String',num2str(num1(1)));
2888    set(handles.last_j,'String',num2str(num1(end)));
2889end
2890
2891%------------------------------------------------------------------------
2892function ref_i_Callback(hObject, eventdata, handles)
2893%------------------------------------------------------------------------
2894mode_list=get(handles.ListPairMode,'String');
2895mode_value=get(handles.ListPairMode,'Value');
2896mode=mode_list{mode_value};
2897errormsg=find_netcpair_civ(handles,1);% update the menu of pairs depending on the available netcdf files
2898if isequal(mode,'series(Di)') || ...% we do patch2 only
2899        (get(handles.CheckCiv2,'Value')==0 && get(handles.CheckCiv1,'Value')==0 && get(handles.CheckFix1,'Value')==0 && get(handles.CheckPatch1,'Value')==0)
2900    errormsg=find_netcpair_civ( handles,2);
2901end
2902    if ~isempty(errormsg)
2903    msgbox_uvmat('ERROR',errormsg)
2904    end
2905
2906%------------------------------------------------------------------------
2907function ref_j_Callback(hObject, eventdata, handles)
2908%------------------------------------------------------------------------
2909mode_list=get(handles.ListPairMode,'String');
2910mode_value=get(handles.ListPairMode,'Value');
2911mode=mode_list{mode_value};
2912if isequal(get(handles.CheckCiv1,'Value'),0)|| isequal(mode,'series(Dj)')
2913    errormsg=find_netcpair_civ(handles,1);% update the menu of pairs depending on the available netcdf files
2914end
2915if isequal(mode,'series(Dj)') || ...
2916        (get(handles.CheckCiv2,'Value')==0 && get(handles.CheckCiv1,'Value')==0 && get(handles.CheckFix1,'Value')==0 && get(handles.CheckPatch1,'Value')==0)
2917    errormsg=find_netcpair_civ(handles,2);
2918end
2919    if ~isempty(errormsg)
2920    msgbox_uvmat('ERROR',errormsg)
2921    end
2922
2923%------------------------------------------------------------------------
2924% determine the menu for checkciv1 pairs depending on existing netcdf file at the middle of
2925% the field series set by first_i, incr, last_i
2926function errormsg=find_netcpair_civ(handles,index)
2927%------------------------------------------------------------------------
2928set(gcf,'Pointer','watch')% set the mouse pointer to 'watch' (clock)
2929
2930%% initialisation
2931errormsg='';
2932% filebase=get(handles.RootPath,'String');
2933% [filepath,Nme,ext_dir]=fileparts(filebase);
2934browse=get(handles.RootPath,'UserData');
2935compare_list=get(handles.ListCompareMode,'String');
2936val=get(handles.ListCompareMode,'Value');
2937compare=compare_list{val};
2938if strcmp(compare,'displacement')||strcmp(compare,'shift')
2939    mode='displacement';
2940else
2941    mode_list=get(handles.ListPairMode,'String');
2942    mode_value=get(handles.ListPairMode,'Value');
2943    if isempty(mode_list)
2944        return
2945    end
2946    mode=mode_list{mode_value};
2947end
2948nom_type_ima=get(handles.NomType,'String');
2949
2950%% determine nom_type_nc, nomenclature type of the .nc files:
2951% nom_type_nc='';%default
2952% if isfield(browse,'nom_type_nc')
2953%     nom_type_nc=browse.nom_type_nc;
2954% end
2955% if isempty(nom_type_nc)
2956    [nom_type_nc]=nomtype2pair(nom_type_ima,mode);
2957% end
2958% browse.nom_type_nc=nom_type_nc;
2959% set(handles.RootPath,'UserData',browse)
2960
2961%% reads .nc subdirectoy and image numbers from the interface
2962subdir_civ1=get(handles.SubdirCiv1,'String');%subdirectory subdir_civ1 for the netcdf data
2963subdir_civ2=get(handles.SubdirCiv2,'String');%subdirectory subdir_civ2 for the netcdf data
2964ref_i=str2double(get(handles.ref_i,'String'));
2965if isequal(mode,'pair j1-j2')%|isequal(mode,'st_pair j1-j2')
2966    ref_j=0;
2967else
2968    ref_j=str2double(get(handles.ref_j,'String'));
2969end
2970time=get(handles.ImaDoc,'UserData');%get the set of times
2971TimeUnit=get(handles.TimeUnit,'String');
2972checkframe=strcmp(TimeUnit,'frame');
2973displ_num=get(handles.ListPairCiv1,'UserData');
2974
2975%% eliminate the first pairs inconsistent with the position
2976if isempty(displ_num)
2977    nbpair=0;
2978else
2979    nbpair=length(displ_num(1,:));%nbre of displayed pairs
2980    if  isequal(mode,'series(Di)')  %| isequal(mode,'st_series(Di)')
2981        nbpair=min(2*ref_i-1,nbpair);%limit the number of pairs with positive first index
2982    elseif  isequal(mode,'series(Dj)')% | isequal(mode,'st_series(Dj)')
2983        nbpair=min(2*ref_j-1,nbpair);%limit the number of pairs with positive first index
2984    end
2985end
2986nbpair=min(200,nbpair);%limit the number of displayed pairs to 200
2987
2988%% case with no Civ1 operation, netcdf files need to exist for reading
2989% be performed, while the result is needed for next steps.
2990displ_pair={''};
2991select=ones(size(1:nbpair));%flag for displayed pairs =1 for display
2992testpair=0;
2993RootPath=get(handles.RootPath,'String');
2994RootFile=get(handles.RootFile,'String');
2995if index==1 % case civ1
2996    if ~get(handles.CheckCiv1,'Value') %
2997        if ~exist(fullfile(RootPath,subdir_civ1),'dir')
2998            errormsg=['no civ1 file available: subdirectory ' subdir_civ1 ' does not exist'];
2999            set(handles.ListPairCiv1,'String',{});
3000            return
3001        end
3002        for ipair=1:nbpair
3003            filename=fullfile_uvmat(RootPath,subdir_civ1,RootFile,'.nc',nom_type_nc,...
3004                ref_i+displ_num(3,ipair),ref_i+displ_num(4,ipair),ref_j+displ_num(1,ipair),ref_j+displ_num(2,ipair));
3005            select(ipair)=exist(filename,'file')==2;% put flag to 0 if the file does not exist
3006        end
3007        % case of no displayed pair
3008        if isequal(select,zeros(size(1:nbpair)))
3009            if isfield(browse,'incr_pair') && ~isequal(browse.incr_pair,[0 0])
3010                num_i1=ref_i-floor(browse.incr_pair(1)/2);
3011                num_i2=ref_i+ceil(browse.incr_pair(1)/2);
3012                num_j1=ref_j-floor(browse.incr_pair(2)/2);
3013                num_j2=ref_j+ceil(browse.incr_pair(2)/2);
3014                filename=fullfile_uvmat(RootPath,subdir_civ1,RootFile,'.nc',nom_type_nc,num_i1,num_i2,num_j1,num_j2);
3015                select(1)=exist(filename,'file')==2;
3016                testpair=1;
3017            else
3018                if  isequal(mode,'series(Dj)')% | isequal(mode,'st_series(Dj)')
3019                    errormsg=['no civ1 file available for the selected reference index j=' num2str(ref_j) ' and subdirectory ' subdir_civ1];
3020                else
3021                    errormsg=['no civ1 file available for the selected reference index i=' num2str(ref_i) ' and subdirectory ' subdir_civ1];
3022                end
3023                set(handles.ListPairCiv1,'String',{''});
3024                %COMPLETER CAS STEREO
3025                return
3026            end
3027        end
3028    end
3029else %case civ2 alone
3030    if ~get(handles.CheckCiv2,'Value') && ~get(handles.CheckCiv1,'Value') && ~get(handles.CheckFix1,'Value') && ~get(handles.CheckPatch1,'Value')
3031        if ~exist(fullfile(filepath,subdir_civ2,ext_dir),'dir')
3032            errordlg(['no civ2 file available: subdirectory ' subdir_civ2 ' does not exist'])
3033            set(handles.ListPairCiv2,'Value',1);
3034            set(handles.ListPairCiv2,'String',{''});
3035            return
3036        end
3037        for ipair=1:nbpair
3038            filename=fullfile_uvmat(RootPath,subdir_civ1,RootFile,'.nc',nom_type_nc,...
3039                ref_i+displ_num(3,ipair),ref_i+displ_num(4,ipair),ref_j+displ_num(1,ipair),ref_j+displ_num(2,ipair));
3040            select(ipair)=exist(filename,'file')==2;
3041        end
3042        if  isequal(select,zeros(size(1:nbpair)))
3043            if isfield(browse,'incr_pair')
3044                num_i1=ref_i-floor(browse.incr_pair(1)/2);
3045                num_i2=ref_i+floor((browse.incr_pair(1)+1)/2);
3046                num_j1=ref_j-floor(browse.incr_pair(2)/2);
3047                num_j2=ref_j+floor((browse.incr_pair(2)+1)/2);
3048                filename=fullfile_uvmat(RootPath,subdir_civ2,RootFile,'.nc',nom_type_nc,num_i1,num_i2,num_j1,num_j2);
3049                select(1)=exist(filename,'file')==2;
3050            else
3051                if  isequal(mode,'series(Dj)')% | isequal(mode,'st_series(Dj)')
3052                    errormsg=['no civ2 file available for the selected reference index j=' num2str(ref_j) ' and subdirectory ' subdir_civ2];
3053                else
3054                    errormsg=['no civ2 file available for the selected reference index i=' num2str(ref_i) ' and subdirectory ' subdir_civ2];
3055                end
3056                set(handles.ListPairCiv2,'Value',1);
3057                set(handles.ListPairCiv2,'String',{''});
3058                return
3059            end
3060        end
3061    end
3062end
3063
3064%% determine the menu display in .ListPairCiv1
3065% the menu depends on the mode defined in ListPairMode_callback through the array displ_num:
3066% displ_num(1,:)=indices j1
3067% displ_num(2,:)=indices j2
3068% displ_num(3,:)=indices i1
3069% displ_num(4,:)=indices i2
3070% in mode 'pair j1-j2', j1 and j2 are the file indices, else the indices
3071% are relative to the reference indices ref_i and ref_j respectively.
3072if isequal(mode,'series(Di)')
3073    if testpair
3074        displ_pair{1}=['Di= ' num2str(-floor(browse.incr_pair(1)/2)) '|' num2str(ceil(browse.incr_pair(1)/2))];
3075    else
3076        for ipair=1:nbpair
3077            if select(ipair)
3078                displ_pair{ipair}=['Di= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2))];
3079                if ~checkframe && size(time,1)>=ref_i+1+displ_num(4,ipair) && size(time,2)>=ref_j+1+displ_num(2,ipair)&&displ_num(2,ipair)>=1 &&displ_num(1,ipair)>=1
3080                    dt=time(ref_i+1+displ_num(4,ipair),ref_j+1+displ_num(2,ipair))-time(ref_i+1+displ_num(3,ipair),ref_j+1+displ_num(1,ipair));%time interval dt
3081                else
3082                    dt=1;
3083                end
3084                 displ_pair{ipair}=[displ_pair{ipair} ' :dt= ' num2str(dt*1000)];
3085            else
3086                displ_pair{ipair}='...'; %pair not displayed in the menu
3087            end
3088        end
3089    end
3090elseif isequal(mode,'series(Dj)')
3091    if testpair
3092        displ_pair{1}=['Dj= ' num2str(-floor(browse.incr_pair(1)/2)) '|' num2str(ceil(browse.incr_pair(1)/2))];
3093    else
3094        for ipair=1:nbpair
3095            if select(ipair)
3096                displ_pair{ipair}=['Dj= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2))];
3097                if ~checkframe && size(time,1)>=ref_i+1+displ_num(4,ipair) && size(time,2)>=ref_j+1+displ_num(2,ipair)
3098                    dt=time(ref_i+1+displ_num(4,ipair),ref_j+1+displ_num(2,ipair))-time(ref_i+1+displ_num(3,ipair),ref_j+1+displ_num(1,ipair));%time interval dt
3099                    displ_pair{ipair}=[displ_pair{ipair} ' :dt= ' num2str(dt*1000)];
3100                end
3101            else
3102                displ_pair{ipair}='...'; %pair not displayed in the menu
3103            end
3104        end
3105    end
3106elseif isequal(mode,'pair j1-j2')%case of pairs
3107    for ipair=1:nbpair
3108        if select(ipair)
3109            if ~checkframe && size(time,2)>1
3110            dt=time(ref_i+1+displ_num(4,ipair),displ_num(2,ipair)+1)-time(ref_i+1+displ_num(3,ipair),displ_num(1,ipair)+1);%time interval dt
3111            else % time set by default to i index
3112                dt=1;
3113            end
3114            displ_pair{ipair}=['j= ' num2stra(displ_num(1,ipair),nom_type_ima) '-' num2stra(displ_num(2,ipair),nom_type_ima) ...
3115                ' :dt= ' num2str(dt*1000)];
3116        else
3117            displ_pair{ipair}='...'; %pair not displayed in the menu
3118        end
3119    end
3120elseif isequal(mode,'displacement')
3121    displ_pair={'Di=Dj=0'};
3122end
3123if index==1
3124set(handles.ListPairCiv1,'String',displ_pair');
3125end
3126
3127%% determine the default selection in the pair menu
3128ichoice=find(select,1);% index of selected pair
3129if (isempty(ichoice) || ichoice < 1); ichoice=1; end;
3130initial=get(handles.ListPairCiv1,'Value');%initial choice of pair
3131if initial>nbpair || (numel(select)>=initial && ~isequal(select(initial),1))
3132    set(handles.ListPairCiv1,'Value',ichoice);% first valid pair proposed by default in the menu
3133end
3134initial=get(handles.ListPairCiv2,'Value');
3135if initial>length(displ_pair')%|~isequal(select(initial),1)
3136    if ichoice <= length(displ_pair')
3137        set(handles.ListPairCiv2,'Value',ichoice);% same pair proposed by default for civ2
3138    else
3139        set(handles.ListPairCiv2,'Value',1);% same pair proposed by default for civ2
3140    end
3141end
3142set(handles.ListPairCiv2,'String',displ_pair');
3143set(gcf,'Pointer','arrow')
3144
3145%-------------------------------------------------------------------
3146% ---
3147function closeview_field(gcbo,eventdata)
3148hview_field=findobj(allchild(0),'tag','view_field');% look for view_field   
3149    if ~isempty(hview_field)
3150        delete(hview_field)
3151    end
3152   
3153   
3154%------------------------------------------------------------------------   
3155% call 'view_field.fig' to display the  field selected in the list of 'status'
3156function open_view_field(hObject, eventdata)
3157%------------------------------------------------------------------------
3158list=get(hObject,'String');
3159index=get(hObject,'Value');
3160rootroot=get(hObject,'UserData');
3161filename=list{index};
3162ind_dot=strfind(filename,'...');
3163filename=filename(1:ind_dot-1);
3164filename=fullfile(rootroot,filename);
3165delete(get(hObject,'parent'))%delete the display figure to stop the check process
3166if exist(filename,'file')%visualise the vel field if it exists
3167    uvmat(filename)
3168    set(gcbo,'Value',1)
3169end
3170
3171%------------------------------------------------------------------------   
3172% launched by pressing OK on the status figure
3173function close_GUI(hObject, eventdata)
3174%------------------------------------------------------------------------
3175    delete(gcbf)
3176   
3177%------------------------------------------------------------------------   
3178% launched by deleting the status figure
3179function stop_status(hObject, eventdata)
3180%------------------------------------------------------------------------
3181hciv=findobj(allchild(0),'tag','civ');
3182hhciv=guidata(hciv);
3183set(hhciv.status,'value',0) %reset the status uicontrol in the GUI civ
3184set(hhciv.status,'BackgroundColor',[0 1 0])
3185
3186
3187%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3188% Callbacks in the uipanel Reference Indices
3189%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3190%------------------------------------------------------------------------
3191function first_i_Callback(hObject, eventdata, handles)
3192%------------------------------------------------------------------------
3193first_i=str2double(get(handles.first_i,'String'));
3194set(handles.ref_i,'String', num2str(first_i))% reference index for pair dt = first index
3195ref_i_Callback(hObject, eventdata, handles)%refresh dispaly of dt for pairs (in case of non constant dt)
3196
3197%------------------------------------------------------------------------
3198function first_j_Callback(hObject, eventdata, handles)
3199%------------------------------------------------------------------------
3200first_j=str2num(get(handles.first_j,'String'));
3201set(handles.ref_j,'String', num2str(first_j))% reference index for pair dt = first index
3202ref_j_Callback(hObject, eventdata, handles)%refresh dispaly of dt for pairs (in case of non constant dt)
3203
3204%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3205% Callbacks in the uipanel Civ1
3206%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3207%------------------------------------------------------------------------
3208% --- Executes on button press in SearchRange: determine the search range num_Searchx,num_Searchy
3209function SearchRange_Callback(hObject, eventdata, handles)
3210%------------------------------------------------------------------------
3211%determine pair numbers
3212if strcmp(get(handles.umin,'Visible'),'off')
3213    set(handles.u_title,'Visible','on')
3214    set(handles.v_title,'Visible','on')
3215    set(handles.umin,'Visible','on')
3216    set(handles.umax,'Visible','on')
3217    set(handles.vmin,'Visible','on')
3218    set(handles.vmax,'Visible','on')
3219    set(handles.CoordUnit,'Visible','on')
3220    set(handles.TimeUnit,'Visible','on')
3221    set(handles.slash_title,'Visible','on')
3222    set(handles.min_title,'Visible','on')
3223    set(handles.max_title,'Visible','on')
3224    set(handles.unit_title,'Visible','on')
3225else
3226    get_search_range(hObject, eventdata, handles)
3227end
3228
3229%------------------------------------------------------------------------
3230% ---  determine the search range num_Searchx,num_Searchy and shift
3231function get_search_range(hObject, eventdata, handles)
3232umin=str2double(get(handles.umin,'String'));
3233umax=str2double(get(handles.umax,'String'));
3234vmin=str2double(get(handles.umin,'String'));
3235vmax=str2double(get(handles.vmax,'String'));
3236%switch min_title and max_title in case of error
3237if umax<=umin
3238    umin_old=umin;
3239    umin=umax;
3240    umax=umin_old;
3241    set(handles.umin,'String', num2str(umin))
3242    set(handles.umax,'String', num2str(umax))
3243end
3244if vmax<=vmin
3245    vmin_old=vmin;
3246    vmin=vmax;
3247    vmax=vmin_old;
3248    set(handles.vmin,'String', num2str(vmin))
3249    set(handles.vmax,'String', num2str(vmax))
3250end   
3251if ~(isnan(umin)||isnan(umax)||isnan(vmin)||isnan(vmax))
3252    list_pair=get(handles.ListPairCiv1,'String');%get the menu of image pairs
3253    index=get(handles.ListPairCiv1,'Value');
3254    displ_num=get(handles.ListPairCiv1,'UserData');
3255    time=get(handles.ImaDoc,'UserData'); %get the set of times
3256    pxcm_xy=get(handles.SearchRange,'UserData');
3257    pxcmx=pxcm_xy(1);
3258    pxcmy=pxcm_xy(2);
3259    mode_list=get(handles.ListPairMode,'String');
3260    mode_value=get(handles.ListPairMode,'Value');
3261    mode=mode_list{mode_value};
3262    if isequal (mode, 'series(Di)' )
3263        ref_i=str2double(get(handles.ref_i,'String'));
3264        num1=ref_i-floor(index/2);%  first image numbers
3265        num2=ref_i+ceil(index/2);
3266        num_a=1;
3267        num_b=1;
3268    elseif isequal (mode, 'series(Dj)')
3269        num1=1;
3270        num2=1;
3271        ref_j=str2double(get(handles.ref_j,'String'));
3272        num_a=ref_j-floor(index/2);%  first image numbers
3273        num_b=ref_j+ceil(index/2);
3274    elseif isequal(mode,'pair j1-j2') %case of bursts (png_old or png_2D)
3275        ref_i=str2double(get(handles.ref_i,'String'));
3276        num1=ref_i;
3277        num2=ref_i;
3278        num_a=displ_num(1,index);
3279        num_b=displ_num(2,index);
3280    end
3281    dt=time(num2,num_b)-time(num1,num_a);
3282    ibx=str2double(get(handles.num_Bx,'String'));
3283    iby=str2double(get(handles.num_By,'String'));
3284    umin=dt*pxcmx*umin;
3285    umax=dt*pxcmx*umax;
3286    vmin=dt*pxcmy*vmin;
3287    vmax=dt*pxcmy*vmax;
3288    shiftx=round((umin+umax)/2);
3289    shifty=round((vmin+vmax)/2);
3290    isx=(umax+2-shiftx)*2+ibx;
3291    isx=2*ceil(isx/2)+1;
3292    isy=(vmax+2-shifty)*2+iby;
3293    isy=2*ceil(isy/2)+1;
3294    set(handles.num_Shiftx,'String',num2str(shiftx));
3295    set(handles.num_Shifty,'String',num2str(shifty));
3296    set(handles.num_Searchx,'String',num2str(isx));
3297    set(handles.num_Searchy,'String',num2str(isy));
3298end
3299
3300%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3301% Callbacks in the uipanel Fix1
3302%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3303%------------------------------------------------------------------------
3304% --- Executes on button press in CheckMask.
3305function get_mask_fix1_Callback(hObject, eventdata, handles)
3306%------------------------------------------------------------------------
3307maskval=get(handles.CheckMask,'Value');
3308if isequal(maskval,0)
3309    set(handles.txt_Mask,'String','')
3310else
3311    mask_displ='no mask'; %default
3312    filebase=get(handles.RootPath,'String');
3313    [nbslice, flag_mask]=get_mask(filebase,handles);
3314    if isequal(flag_mask,1)
3315        mask_displ=[num2str(nbslice) 'mask'];
3316    elseif get(handles.ListCompareMode,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3317        filebase_a=get(handles.RootFile_1,'String');
3318        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3319        if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice)
3320            mask_displ='no mask';
3321        end
3322    end
3323    if isequal(mask_displ,'no mask')
3324        [FileName, PathName, filterindex] = uigetfile( ...
3325            {'*.png', ' (*.png)';
3326            '*.png',  '.png files '; ...
3327            '*.*', 'All Files (*.*)'}, ...
3328            'Pick a mask file *.png',filebase);
3329        mask_displ=fullfile(PathName,FileName);
3330        if ~exist(mask_displ,'file')
3331            mask_displ='no mask';
3332        end
3333    end
3334    if isequal(mask_displ,'no mask')
3335        set(handles.CheckMask,'Value',0)
3336        set(handles.CheckMask,'Value',0)
3337        set(handles.CheckMask,'Value',0)
3338    else
3339        %set(handles.CheckMask,'Value',1)
3340        set(handles.CheckMask,'Value',1)
3341    end
3342    set(handles.txt_Mask,'String',mask_displ)
3343    set(handles.txt_Mask,'String',mask_displ)
3344    set(handles.txt_Mask,'String',mask_displ)
3345end
3346
3347%------------------------------------------------------------------------
3348% --- Executes on button press in CheckMask: select box for mask option
3349function get_mask_civ2_Callback(hObject, eventdata, handles)
3350%------------------------------------------------------------------------
3351maskval=get(handles.CheckMask,'Value');
3352if isequal(maskval,0)
3353    set(handles.txt_Mask,'String','')
3354else
3355    mask_displ='no mask'; %default
3356    filebase=get(handles.RootPath,'String');
3357    [nbslice, flag_mask]=get_mask(filebase,handles);
3358    if isequal(flag_mask,1)
3359        mask_displ=[num2str(nbslice) 'mask'];
3360    elseif get(handles.ListCompareMode,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3361        filebase_a=get(handles.RootFile_1,'String');
3362        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3363        if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice)
3364            mask_displ='no mask';
3365        end
3366    end
3367    if isequal(mask_displ,'no mask')
3368        [FileName, PathName, filterindex] = uigetfile( ...
3369            {'*.png', ' (*.png)';
3370            '*.png',  '.png files '; ...
3371            '*.*', 'All Files (*.*)'}, ...
3372            'Pick a mask file *.png',filebase);
3373        mask_displ=fullfile(PathName,FileName);
3374        if ~exist(mask_displ,'file')
3375            mask_displ='no mask';
3376        end
3377    end
3378    if isequal(mask_displ,'no mask')
3379        set(handles.CheckMask,'Value',0)
3380        set(handles.CheckMask,'Value',0)
3381    else
3382        set(handles.CheckMask,'Value',1)
3383    end
3384    set(handles.txt_Mask,'String',mask_displ)
3385    set(handles.txt_Mask,'String',mask_displ)
3386end
3387
3388%------------------------------------------------------------------------
3389% --- Executes on button press in CheckMask.
3390function get_mask_fix2_Callback(hObject, eventdata, handles)
3391%------------------------------------------------------------------------
3392maskval=get(handles.CheckMask,'Value');
3393if isequal(maskval,0)
3394    set(handles.txt_Mask,'String','')
3395else
3396    mask_displ='no mask'; %default
3397    filebase=get(handles.RootPath,'String');
3398    [nbslice, flag_mask]=get_mask(filebase,handles);
3399    if isequal(flag_mask,1)
3400        mask_displ=[num2str(nbslice) 'mask'];
3401    elseif get(handles.ListCompareMode,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3402        filebase_a=get(handles.RootFile_1,'String');
3403        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3404        if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice)
3405            mask_displ='no mask';
3406        end
3407    end
3408    if isequal(mask_displ,'no mask')
3409        [FileName, PathName, filterindex] = uigetfile( ...
3410            {'*.png', ' (*.png)';
3411            '*.png',  '.png files '; ...
3412            '*.*', 'All Files (*.*)'}, ...
3413            'Pick a mask file *.png',filebase);
3414        mask_displ=fullfile(PathName,FileName);
3415        if ~exist(mask_displ,'file')
3416            mask_displ='no mask';
3417        end
3418    end
3419    if isequal(mask_displ,'no mask')
3420        set(handles.CheckMask,'Value',0)
3421    end
3422    set(handles.txt_Mask,'String',mask_displ)
3423end
3424
3425%------------------------------------------------------------------------
3426% --- function called to look for mask files
3427function [nbslice, flag_mask]=get_mask(filebase,handles)
3428%------------------------------------------------------------------------
3429%detect mask files, images with appropriate file base
3430%[filebase '_' xx 'mask'], xx=nbslice
3431%flag_mask=1 indicates detection
3432
3433flag_mask=0;%default
3434nbslice=1;
3435
3436% subdir=get(handles.SubdirCiv1,'String');
3437[Path,Name]=fileparts(filebase);
3438if ~isdir(Path)
3439    msgbox_uvmat('ERROR','no path for input files')
3440    return
3441end
3442% currentdir=pwd;
3443% cd(Path);%move in the dir of the root name filebase
3444maskfiles=dir(fullfile(Path,[Name '_*mask_*.png']));%look for mask files
3445% cd(currentdir);%come back to the current working directory
3446if ~isempty(maskfiles)
3447    %     msgbox_uvmat('ERROR','no mask available, to create it use Tools/Make mask in the upper menu bar of uvmat')
3448    % else
3449    flag_mask=1;
3450    maskname=maskfiles(1).name;% take the first mask file in the list
3451    [Path2,Name,ext]=fileparts(maskname);
3452    Namedouble=double(Name);
3453    val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
3454    ind_mask=findstr('mask',Name);
3455    i=ind_mask-1;
3456    while val(i)==0 && i>0
3457        i=i-1;
3458    end
3459    nbslice=str2double(Name(i+1:ind_mask-1));
3460    if ~isnan(nbslice) && Name(i)=='_'
3461        flag_mask=1;
3462    else
3463        msgbox_uvmat('ERROR',['bad mask file ' Name ext ' found in ' Path2])
3464        return
3465        nbslice=1;
3466    end
3467end
3468
3469%------------------------------------------------------------------------
3470% --- function called to look for grid files
3471function [nbslice, flag_mask]=get_grid(filebase,handles)
3472%------------------------------------------------------------------------
3473flag_mask=0;%default
3474nbslice=1;
3475[Path,Name]=fileparts(filebase);
3476currentdir=pwd;
3477cd(Path);%move in the dir of the root name filebase
3478maskfiles=dir([Name '_*grid_*.grid']);%look for mask files
3479cd(currentdir);%come back to the current working directory
3480if ~isempty(maskfiles)
3481    flag_mask=1;
3482    maskname=maskfiles(1).name;% take the first mask file in the list
3483    [Path2,Name,ext]=fileparts(maskname);
3484    Namedouble=double(Name);
3485    val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
3486    ind_mask=findstr('grid',Name);
3487    i=ind_mask-1;
3488    while val(i)==0 && i>0
3489        i=i-1;
3490    end
3491    nbslice=str2double(Name(i+1:ind_mask-1));
3492    if ~isnan(nbslice) && Name(i)=='_'
3493        flag_mask=1;
3494    else
3495        msgbox_uvmat('ERROR',['bad grid file ' Name ext ' found in ' Path2])
3496        return
3497        nbslice=1;
3498    end
3499end
3500
3501%------------------------------------------------------------------------
3502% --- transform numbers to letters
3503function str=num2stra(num,nom_type)
3504%------------------------------------------------------------------------
3505if isempty(nom_type)
3506    str='';
3507elseif strcmp(nom_type(end),'a')
3508    str=char(96+num);
3509elseif strcmp(nom_type(end),'A')
3510    str=char(96+num);
3511elseif isempty(nom_type(2:end))%a single index
3512    str='';
3513else
3514    str=num2str(num);
3515end
3516
3517%------------------------------------------------------------------------
3518% --- Executes on button press in ListSubdirCiv1.
3519function ListSubdirCiv1_Callback(hObject, eventdata, handles)
3520%------------------------------------------------------------------------
3521list_subdir_civ1=get(handles.ListSubdirCiv1,'String');
3522val=get(handles.ListSubdirCiv1,'Value');
3523SubDir=list_subdir_civ1{val};
3524if strcmp(SubDir,'new...')
3525    if get(handles.CheckCiv1,'Value')
3526        SubDir='CIV'; %default subdirectory
3527    else
3528        msgbox_uvmat('ERROR','select CheckCiv1 to perform a new Civ operation')
3529        return
3530    end   
3531end
3532set(handles.SubdirCiv1,'String',SubDir);
3533errormsg=find_netcpair_civ(handles,1);
3534if ~isempty(errormsg)
3535    msgbox_uvmat('ERROR',errormsg)
3536end
3537   
3538%------------------------------------------------------------------------
3539% --- Executes on button press in ListSubdirCiv2.
3540function ListSubdirCiv2_Callback(hObject, eventdata, handles)
3541%------------------------------------------------------------------------
3542list_subdir_civ2=get(handles.ListSubdirCiv2,'String');
3543val=get(handles.ListSubdirCiv2,'Value');
3544SubDir=list_subdir_civ2{val};
3545if strcmp(SubDir,'new...')
3546    if get(handles.CheckCiv2,'Value')
3547        SubDir='CIV'; %default subdirectory
3548    else
3549        msgbox_uvmat('ERROR','select CheckCiv2 to perform a new Civ operation')
3550        return
3551    end
3552end
3553set(handles.SubdirCiv2,'String',SubDir);
3554
3555%------------------------------------------------------------------------
3556% --- Executes on button press in CheckGrid.
3557function CheckGrid_Callback(hObject, eventdata, handles)
3558%------------------------------------------------------------------------
3559value=get(hObject,'Value');
3560hparent=get(hObject,'parent');
3561hchildren=get(hparent,'children');
3562handle_txtbox=findobj(hchildren,'tag','txt_Grid');
3563handle_dx=findobj(hchildren,'tag','num_Dx');
3564handle_dy=findobj(hchildren,'tag','num_Dy');
3565handle_title_dx=findobj(hchildren,'tag','title_Dx');
3566handle_title_dy=findobj(hchildren,'tag','title_Dy');
3567testgrid=0;
3568filegrid='';
3569if value
3570    filebase=get(handles.RootPath,'String');
3571    [nbslice, flag_grid]=get_grid(filebase,handles);% look for a grid with appropriate name
3572    if isequal(flag_grid,1)
3573        filegrid=[num2str(nbslice) 'grid'];
3574        testgrid=1;
3575    else % browse for a grid
3576        filegrid=get(hObject,'UserData');%look for previous grid name stored as UserData
3577        if exist(filegrid,'file')
3578            filebase=filegrid;
3579        end
3580        [FileName, PathName, filterindex] = uigetfile( ...
3581            {'*.grid', ' (*.grid)';
3582            '*.grid',  '.grid files '; ...
3583            '*.*', 'All Files (*.*)'}, ...
3584            'Pick a file',filebase);
3585        filegrid=fullfile(PathName,FileName);
3586        set(hObject,'UserData',filegrid);%store for future use
3587        if ~(isempty(FileName)||isempty(PathName)||isequal(FileName,0)||~exist(filegrid,'file'))
3588            testgrid=1;
3589        end
3590    end
3591end
3592if testgrid
3593    set(handle_dx,'Visible','off');
3594    set(handle_dy,'Visible','off');
3595    set(handle_title_dy,'Visible','off');
3596    set(handle_title_dx,'Visible','off');
3597    set(handle_txtbox,'Visible','on')
3598    set(handle_txtbox,'String',filegrid)
3599else
3600    set(hObject,'Value',0);
3601    set(handle_dx,'Visible','on');
3602    set(handle_dy,'Visible','on');
3603    set(handle_title_dy,'Visible','on');
3604    set(handle_title_dx,'Visible','on');
3605    set(handle_txtbox,'Visible','off')
3606end
3607
3608%% if hObject is on the checkciv1 frame, duplicate action for checkciv2 frame
3609PanelName=get(hparent,'tag');
3610if strcmp(PanelName,'Civ1')
3611    hchildren=get(handles.Civ2,'children');
3612    handle_checkbox=findobj(hchildren,'tag','CheckGrid');
3613    handle_txtbox=findobj(hchildren,'tag','txt_Grid');
3614    handle_dx=findobj(hchildren,'tag','num_Dx');
3615    handle_dy=findobj(hchildren,'tag','num_Dy');
3616    handle_title_dx=findobj(hchildren,'tag','title_Dx');
3617    handle_title_dy=findobj(hchildren,'tag','title_Dy');
3618    set(handle_checkbox,'UserData',filegrid);%store for future use
3619    if testgrid
3620        set(handle_checkbox,'Value',1);
3621        set(handle_dx,'Visible','off');
3622        set(handle_dy,'Visible','off');
3623        set(handle_title_dx,'Visible','off');
3624        set(handle_title_dy,'Visible','off');
3625        set(handle_txtbox,'Visible','on')
3626        set(handle_txtbox,'String',filegrid)
3627%     else
3628%         set(handle_checkbox,'Value',0);
3629%         set(handles.CheckGrid,'Value',0);
3630%         set(handle_dx,'Visible','on');
3631%         set(handle_dy,'Visible','on');
3632%          set(handle_title_dx,'Visible','on');
3633%         set(handle_title_dy,'Visible','on');
3634%         set(handle_txtbox,'Visible','off')
3635    end
3636end
3637%------------------------------------------------------------------------
3638% --- Executes on button press in CheckMask.
3639function CheckMask_Callback(hObject, eventdata, handles)
3640%------------------------------------------------------------------------
3641value=get(hObject,'Value');
3642hparent=get(hObject,'parent');
3643parent_tag=get(hparent,'Tag');
3644hchildren=get(hparent,'children');
3645handle_txtbox=findobj(hchildren,'tag','txt_Mask');
3646% handle_dx=findobj(hchildren,'tag','num_Dx');
3647% handle_dy=findobj(hchildren,'tag','num_Dy');
3648testmask=0;
3649if value
3650    filebase=get(handles.RootPath,'String');
3651    [nbslice, flag_mask]=get_mask(filebase,handles);% look for a mask with appropriate name
3652    if isequal(flag_mask,1)
3653        filemask=[num2str(nbslice) 'mask'];
3654        testmask=1;
3655    else % browse for a mask
3656        filemask=get(hObject,'UserData');%look for previous mask name stored as UserData
3657        if exist(filemask,'file')
3658            filebase=filemask;
3659        end
3660        [FileName, PathName] = uigetfile( ...
3661            {'*.png', ' (*.png)';
3662            '*.png',  '.png files '; ...
3663            '*.*', 'All Files (*.*)'}, ...
3664            'Pick a mask file *.png',filebase);
3665        filemask=fullfile(PathName,FileName);
3666        set(hObject,'UserData',filemask);%store for future use
3667        if ~(isempty(FileName)||isempty(PathName)||isequal(FileName,0)||~exist(filemask,'file'))
3668            testmask=1;
3669        end
3670    end
3671end
3672if testmask
3673%     stage=4;%default
3674    if strcmp(parent_tag,'Civ1')
3675            set(handles.txt_Mask,'Visible','on')
3676        set(handles.txt_Mask,'String',filemask)
3677    set(handles.CheckMask,'Value',1)
3678    end
3679%     switch parent_tag
3680% %         case 'Fix1'
3681% %             stage=2;
3682%         case 'Civ2'
3683%              stage=3;
3684% %         case 'Fix2'
3685% %             stage=4;
3686%     end
3687%     set(handles.txt_Mask(stage:end),'Visible','on')
3688%     set(handles.txt_Mask(stage:end),'String',filemask)
3689%     set(handles.CheckMask(stage:end),'Value',1)
3690else
3691    set(hObject,'Value',0);
3692    set(handle_txtbox,'Visible','off')
3693end
3694
3695
3696% --- Executes on button press in get_gridpatch1.
3697function get_gridpatch1_Callback(hObject, eventdata, handles)
3698filebase=get(handles.RootPath,'String');
3699[FileName, PathName, filterindex] = uigetfile( ...
3700    {'*.grid', ' (*.grid)';
3701    '*.grid',  '.grid files '; ...
3702    '*.*', 'All Files (*.*)'}, ...
3703    'Pick a file',filebase);
3704filegrid=fullfile(PathName,FileName);
3705set(handles.grid_patch1,'string',filegrid);
3706
3707
3708%------------------------------------------------------------------------
3709% --- Executes on button press in get_gridpatch2.
3710function get_gridpatch2_Callback(hObject, eventdata, handles)
3711%------------------------------------------------------------------------
3712
3713
3714%------------------------------------------------------------------------
3715% --- STEREO Interp
3716function cmd=RUN_STINTERP(stinterpBin,filename_A_nc,filename_B_nc,filename_nc,nx_patch,ny_patch,rho_patch,subdomain_patch,thresh_value,xmlA,xmlB)
3717%------------------------------------------------------------------------
3718namelog=[filename_nc(1:end-3) '_stinterp.log'];
3719cmd=[stinterpBin ' -f1 ' filename_A_nc  ' -f2 ' filename_B_nc ' -f  ' filename_nc ...
3720    ' -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
3721
3722%------------------------------------------------------------------------
3723%--read images and convert them to the uint16 format used for PIV
3724function A=read_image(filename,type_ima,num,movieobject)
3725%------------------------------------------------------------------------
3726%num is the view number needed for an avi movie
3727switch type_ima
3728    case 'movie'
3729        A=read(movieobject,num);
3730    case 'avi'
3731        mov=aviread(filename,num);
3732        A=frame2im(mov(1));
3733    case 'multimage'
3734        A=imread(filename,num);
3735    case 'image'
3736        A=imread(filename);
3737end
3738siz=size(A);
3739if length(siz)==3;%color images
3740    A=sum(double(A),3);
3741    A=uint16(A);
3742end
3743
3744
3745%------------------------------------------------------------------------
3746% --- Executes on button press in get_ref_fix1.
3747function get_ref_fix1_Callback(hObject, eventdata, handles)
3748%------------------------------------------------------------------------
3749filebase=get(handles.RootPath,'String');
3750[FileName, PathName, filterindex] = uigetfile( ...
3751    {'*.nc', ' (*.nc)';
3752    '*.nc',  'netcdf files '; ...
3753    '*.*', 'All Files (*.*)'}, ...
3754    'Pick a file',filebase);
3755
3756fileinput=[PathName FileName];
3757sizf=size(fileinput);
3758if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
3759%[Path,File,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
3760[Path,ref.subdir,File,ref.num1,ref.num2,ref.num_a,ref.num_b,ref.ext,ref.nom_type]=fileparts_uvmat(fileinput);
3761ref.filebase=fullfile(Path,File);
3762% ref.num_a=stra2num(str_a);
3763% ref.num_b=stra2num(str_b);
3764% ref.num1=str2double(field_count);
3765% ref.num2=str2double(str2);
3766browse=[];%initialisation
3767if ~isequal(ref.ext,'.nc')
3768    msgbox_uvmat('ERROR','the reference file must be in netcdf format (*.nc)')
3769    return
3770end
3771set(handles.ref_fix1,'String',[fullfile(ref.subdir,File) '....nc']);
3772set(handles.ref_fix1,'UserData',ref)
3773menu_field{1}='civ1';
3774Data=nc2struct(fileinput,[]);
3775if isfield(Data,'patch') && isequal(Data.patch,1)
3776    menu_field{2}='filter1';
3777end
3778if isfield(Data,'civ2') && isequal(Data.civ2,1)
3779    menu_field{3}='civ2';
3780end
3781if isfield(Data,'patch2') && isequal(Data.patch2,1)
3782    menu_field{4}='filter2';
3783end
3784set(handles.field_ref1,'String',menu_field);
3785set(handles.field_ref1,'Value',length(menu_field));
3786set(handles.num_MinVel,'Value',2);
3787set(handles.num_MinVel,'String','1');%default threshold
3788set(handles.ref_fix1,'Enable','on')
3789
3790%------------------------------------------------------------------------
3791% --- Executes on button press in get_ref_fix2.
3792function get_ref_fix2_Callback(hObject, eventdata, handles)
3793%------------------------------------------------------------------------
3794if isequal(get(handles.get_ref_fix2,'Value'),1)
3795    filebase=get(handles.RootPath,'String');
3796    [FileName, PathName, filterindex] = uigetfile( ...
3797        {'*.nc', ' (*.nc)';
3798        '*.nc',  'netcdf files '; ...
3799        '*.*', 'All Files (*.*)'}, ...
3800        'Pick a file',filebase);
3801    fileinput=[PathName FileName];
3802    sizf=size(fileinput);
3803    if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
3804    %[Path,File,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
3805    [Path,ref.subdir,File,ref.num1,ref.num2,ref.num_a,ref.num_b,ref.ext,ref.nom_type]=fileparts_uvmat(fileinput);
3806    ref.filebase=fullfile(Path,File);
3807%     ref.num_a=stra2num(str_a);
3808%     ref.num_b=stra2num(str_b);
3809%     ref.num1=str2num(field_count);
3810%     ref.num2=str2num(str2);
3811    browse=[];%initialisation
3812    if ~isequal(ref.ext,'.nc')
3813        msgbox_uvmat('ERROR','the reference file must be in netcdf format (*.nc)')
3814        return
3815    end
3816    set(handles.ref_fix2,'String',[fullfile(ref.subdir,File) '....nc']);
3817    set(handles.ref_fix2,'UserData',ref)
3818    menu_field{1}='civ1';
3819    Data=nc2struct(fileinput,[]);
3820    if isfield(Data,'patch') && isequal(Data.patch,1)
3821        menu_field{2}='filter1';
3822    end
3823    if isfield(Data,'civ2') && isequal(Data.civ2,1)
3824        menu_field{3}='civ2';
3825    end
3826    if isfield(Data,'patch2') && isequal(Data.patch2,1)
3827        menu_field{4}='filter2';
3828    end
3829    set(handles.field_ref2,'String',menu_field);
3830    set(handles.field_ref2,'Value',length(menu_field));
3831    set(handles.num_MinVel,'Value',2);
3832    set(handles.num_MinVel,'String','1');%default threshold
3833    set(handles.ref_fix2,'Enable','on')
3834    set(handles.ref_fix2,'Visible','on')
3835    set(handles.field_ref2,'Visible','on')
3836else
3837    set(handles.ref_fix2,'Visible','off')
3838    set(handles.field_ref2,'Visible','off')
3839end
3840
3841%------------------------------------------------------------------------
3842function ref_fix1_Callback(hObject, eventdata, handles)
3843%------------------------------------------------------------------------
3844set(handles.num_MinVel,'Value',1);
3845set(handles.field_ref1,'Value',1)
3846set(handles.field_ref1,'String',{' '})
3847set(handles.ref_fix1,'UserData',[]);
3848set(handles.ref_fix1,'String','');
3849set(handles.thresh_vel1,'String','0');
3850
3851%------------------------------------------------------------------------
3852function ref_fix2_Callback(hObject, eventdata, handles)
3853%------------------------------------------------------------------------
3854set(handles.num_MinVel,'Value',1);
3855set(handles.field_ref2,'Value',1)
3856set(handles.field_ref2,'String',{' '})
3857set(handles.ref_fix2,'UserData',[]);
3858set(handles.ref_fix2,'String','');
3859set(handles.num_MinVel,'String','0');
3860
3861%------------------------------------------------------------------------
3862% --- TO ABANDON Executes on button press in test_stereo1.
3863function CheckStereo_Callback(hObject, eventdata, handles)
3864%------------------------------------------------------------------------
3865hparent=get(hObject,'parent');
3866parent_tag=get(hparent,'Tag');
3867hchildren=get(hparent,'children');
3868handle_txtbox=findobj(hchildren,'tag','txt_Mask');
3869if isequal(get(hObject,'Value'),0)
3870    set(handles.num_SubdomainSize,'Visible','on')
3871    set(handles.num_SmoothingParam,'Visible','on')
3872else
3873    set(handles.num_SubdomainSize,'Visible','off')
3874    set(handles.num_SmoothingParam,'Visible','off')
3875end
3876
3877% %------------------------------------------------------------------------
3878% % --- Executes on button press in CheckStereo.
3879% function StereoCheck_Callback(hObject, eventdata, handles)
3880% %------------------------------------------------------------------------
3881% if isequal(get(handles.CheckStereo,'Value'),0)
3882%     set(handles.num_SubdomainSize,'Visible','on')
3883%     set(handles.num_SmoothingParam,'Visible','on')
3884% else
3885%     set(handles.num_SubdomainSize,'Visible','off')
3886%     set(handles.num_SmoothingParam,'Visible','off')
3887% end
3888
3889%------------------------------------------------------------------------
3890% --- Executes on button press in TestCiv1: display image correlation function
3891function TestCiv1_Callback(hObject, eventdata, handles)
3892%------------------------------------------------------------------------
3893set(handles.TestCiv1,'BackgroundColor',[1 1 0])
3894drawnow
3895if get(handles.TestCiv1,'Value')
3896    ref_i=str2double(get(handles.ref_i,'String'));
3897    if strcmp(get(handles.ref_j,'Visible'),'on')
3898        ref_j=str2double(get(handles.ref_j,'String'));
3899    else
3900        ref_j=1;%default
3901    end
3902    [filecell,i1,i21,j1,j2,i1_civ2,i2_civ2,j1_civ2,j2_civ2,nom_type_nc,file_ref_fix1,file_ref_fix2]=...
3903        set_civ_filenames(handles,ref_i,ref_j,[1 0 0 0 0 0]);
3904    Data.ListVarName={'ny','nx','A'};
3905    Data.VarDimName= {'ny','nx',{'ny','nx'}};
3906    Data.A=imread(filecell.ima1.civ1{1});
3907    Data.ny=[size(Data.A,1) 1];
3908    Data.nx=[1 size(Data.A,2)];
3909    par_civ1=read_GUI(handles.Civ1);
3910    par_civ1.ImageWidth=size(Data.A,1);
3911    par_civ1.ImageHeight=size(Data.A,2);
3912    par_civ1.Mask='all';% will provide only the grid set for PIV, no image correlation
3913    Param.Civ1=par_civ1;
3914    Grid=civ_matlab(Param);% get the grid of x, y positions set for PIV
3915    hview_field=view_field(Data);
3916    set(0,'CurrentFigure',hview_field)
3917    hhview_field=guihandles(hview_field);
3918    set(hview_field,'CurrentAxes',hhview_field.axes3)
3919    ViewData=get(hview_field,'UserData');
3920    ViewData.CivHandle=handles.civ;% indicate the handle of the civ GUI in view_field
3921    ViewData.axes3.B=imread(filecell.ima2.civ1{1});%store the second image in the UserData of the GUI view_field
3922    ViewData.axes3.X=Grid.Civ1_X; %keep the set of points in memeory
3923    ViewData.axes3.Y=Grid.Civ1_Y;
3924    set(hview_field,'UserData',ViewData)
3925    corrfig=findobj(allchild(0),'tag','corrfig');% look for a current figure for image correlation display
3926    if isempty(corrfig)
3927        corrfig=figure;
3928        set(corrfig,'tag','corrfig')
3929        set(corrfig,'name','image correlation')
3930        set(corrfig,'DeleteFcn',{@closeview_field})%
3931    end
3932    set(handles.TestCiv1,'BackgroundColor',[1 0 0])
3933else
3934    corrfig=findobj(allchild(0),'tag','corrfig');% look for a current figure for image correlation display
3935    if ~isempty(corrfig)
3936        delete(corrfig)
3937    end
3938    hview_field=findobj(allchild(0),'tag','view_field');% look for view_field   
3939    if ~isempty(hview_field)
3940        delete(hview_field)
3941    end
3942end
3943
3944
3945%------------------------------------------------------------------------
3946% --- Executes on button press in CheckThreshold.
3947function CheckThreshold_Callback(hObject, eventdata, handles)
3948%------------------------------------------------------------------------
3949huipanel=get(hObject,'parent');
3950obj(1)=findobj(huipanel,'Tag','num_MinIma');
3951obj(2)=findobj(huipanel,'Tag','num_MaxIma');
3952obj(3)=findobj(huipanel,'Tag','title_Threshold');
3953if get(hObject,'Value')
3954    set(obj,'Visible','on')
3955else
3956    set(obj,'Visible','off')
3957end
3958
3959%------------------------------------------------------------------------
3960function cmd=cmd_civ1(filename,Param)
3961%------------------------------------------------------------------------
3962%pixels per cm and matrix of the image times, read from the .civ file by uvmat
3963%changes : filename_cmx -> filename ( no extension )
3964cmd='';
3965errormsg=''; %default
3966filename=regexprep(filename,'.nc',''); %file name for the result
3967if isequal(Param.Civ1.Dt,0)
3968    Param.Civ1.Dt=1 ;%case of 'displacement' mode
3969end
3970Param.Civ1.ImageA=regexprep(Param.Civ1.ImageA,'.png','');
3971Param.Civ1.ImageB=regexprep(Param.Civ1.ImageB,'.png','');
3972fid=fopen([filename '.civ1.cmx'],'w');
3973if isequal(fid,-1)
3974    display(['cmd file ' filename ' cannot be created'])
3975    return
3976end
3977fprintf(fid,['##############   CMX file' '\n' ]);
3978fprintf(fid,   ['FirstImage ' regexprep(Param.Civ1.ImageA,'\\','\\\\') '\n' ]);% for windows compatibility
3979fprintf(fid,   ['LastImage  ' regexprep(Param.Civ1.ImageB,'\\','\\\\') '\n' ]);% for windows compatibility
3980fprintf(fid,  ['XX' '\n' ]);
3981if isfield(Param.Civ1,'Mask')
3982    fprintf(fid,  ['Mask ' 'y' '\n' ]);
3983    fprintf(fid,  ['MaskName ' regexprep(Param.Civ1.Mask,'\\','\\\\') '\n' ]);
3984else
3985    fprintf(fid,  ['Mask ' 'n' '\n' ]);
3986    fprintf(fid,  ['MaskName ' 'noFile use default' '\n' ]);
3987end
3988fprintf(fid,   ['ImageSize ' num2str(Param.Civ1.ImageWidth) ' ' num2str(Param.Civ1.ImageHeight) '\n' ]);   %VERIFIER CAS GENERAL ?
3989fprintf(fid,   ['CorrelationBoxesSize ' num2str(Param.Civ1.Bx) ' ' num2str(Param.Civ1.By) '\n' ]);
3990fprintf(fid,   ['SearchBoxeSize ' num2str(Param.Civ1.Searchx) ' ' num2str(Param.Civ1.Searchy) '\n' ]);
3991fprintf(fid,   ['RO ' num2str(Param.Civ1.Rho) '\n' ]);
3992if isfield(Param.Civ1,'Grid')
3993    fprintf(fid,   ['GridSpacing ' '25' ' ' '25' '\n' ]);
3994else
3995    fprintf(fid,   ['GridSpacing ' num2str(Param.Civ1.Dx) ' ' num2str(Param.Civ1.Dy) '\n' ]);
3996end
3997fprintf(fid,   ['XX 1.0' '\n' ]);
3998fprintf(fid,   ['Dt_TO ' num2str(Param.Civ1.Dt) ' ' num2str(Param.Civ1.Time) '\n' ]);
3999fprintf(fid,  ['PixCmXY ' '1' ' ' '1' '\n' ]);
4000fprintf(fid,  ['XX 1' '\n' ]);
4001fprintf(fid,   ['ShiftXY ' num2str(Param.Civ1.Shiftx) ' '  num2str(Param.Civ1.Shifty) '\n' ]);
4002if isfield(Param.Civ1,'Grid')
4003    fprintf(fid,  ['Grid ' 'y' '\n' ]);
4004    fprintf(fid,   ['GridName ' regexprep(Param.Civ1.Grid,'\\','\\\\') '\n' ]);
4005else
4006    fprintf(fid,  ['Grid ' 'n' '\n' ]);
4007    fprintf(fid,   ['GridName ' 'noFile use default' '\n' ]);
4008end
4009fprintf(fid,   ['XX 85' '\n' ]);
4010fprintf(fid,   ['XX 1.0' '\n' ]);
4011fprintf(fid,   ['XX 1.0' '\n' ]);
4012fprintf(fid,   ['Hart 1' '\n' ]);
4013fprintf(fid,  [ 'DecimalShift 0' '\n' ]);
4014fprintf(fid,   ['Deformation 0' '\n' ]);
4015fprintf(fid,  ['CorrelationMin 0' '\n' ]);
4016fprintf(fid,   ['IntensityMin 0' '\n' ]);
4017if ~isfield(Param.Civ1,'MinIma')% Image threshold not activated
4018    fprintf(fid,  ['SeuilImage n' '\n' ]);
4019    fprintf(fid,   ['SeuilImageValues 0 4096' '\n' ]);%not used in principle
4020else% Image threshold  activated
4021    if isempty(Param.Civ1.MaxIma)||isnan(Param.Civ1.MaxIma)
4022        Param.Civ1.MaxIma=2^Param.Civ1.ImageBitDepth;%take the max image value as upper bound by default
4023    end
4024    fprintf(fid,  ['SeuilImage y' '\n' ]);
4025    fprintf(fid,   ['SeuilImageValues ' num2str(Param.Civ1.MinIma) ' ' num2str(Param.Civ1.MaxIma) '\n' ]);
4026end
4027fprintf(fid,   ['ImageToUse ' Param.Civ1.term_a ' ' Param.Civ1.term_b '\n' ]); % VERIFIER ?
4028fprintf(fid,   ['ImageUsedBefore null null' '\n' ]);
4029fclose(fid);
4030
4031if(isunix) %unix (or Mac) system
4032    cmd=['cp -f ' filename '.civ1.cmx ' filename '.cmx \n '];% the cmx file gives the name to the nc file
4033    cmd=[cmd Param.xml.Civ1Bin ' -f ' filename '.cmx >' filename '.civ1.log \n ' ]; % redirect standard output to the log file, the result file is named [filename '.nc'] by CIVx
4034    cmd=[cmd 'rm ' filename '.cmx'];
4035else %Windows system
4036    filename=regexprep(filename,'\\','\\\\');
4037    cmd=['copy /Y "' filename '.civ1.cmx" "' filename '.cmx" \n '];
4038    cmd=[cmd '"' regexprep(Param.xml.Civ1Bin,'\\','\\\\')...
4039        '" -f "' filename '.cmx" >"' filename '.civ1.log" \n ' ]; % redirect standard output to the log file
4040    cmd=[cmd 'del "' filename '.cmx"'];
4041end
4042
4043
4044function cmd=cmd_fix(filename,Param,fixname)
4045%%
4046switch fixname
4047    case 'Fix1'
4048        fi2_value=num2str(Param.(fixname).CheckF2);
4049    case 'Fix2'
4050        fi2_value=num2str(Param.(fixname).CheckF4);%need to understand why...
4051end
4052filename=regexprep(filename,'.nc','');
4053MaskName_string='';%default
4054% if Param.(fixname).CheckMask
4055%     MaskName_string=[' -maskName "' Param.(fixname).Mask '"'];
4056% end
4057MaxVel_string='';%default
4058if ~isempty(Param.(fixname).MaxVel)
4059    MaxVel_string=[' -threshV ' num2str(Param.(fixname).MaxVel)];
4060end
4061if isunix
4062    cmd=[Param.xml.FixBin ' -f ' filename '.nc -fi1 ' num2str(Param.(fixname).CheckFmin2) ...
4063        ' -fi2 ' fi2_value ' -fi3 ' num2str(Param.(fixname).CheckF3) ...
4064        ' -threshC ' num2str(Param.(fixname).MinCorr) MaxVel_string MaskName_string...
4065        ' >' filename '.' lower(fixname) '.log 2>&1'];
4066else
4067    cmd=['"' Param.xml.FixBin '" -f "' filename '.nc" -fi1 ' num2str(Param.(fixname).CheckFmin2)...
4068        ' -fi2 ' fi2_value ' -fi3 ' num2str(Param.(fixname).CheckF3) ...
4069        ' -threshC ' num2str(Param.(fixname).MinCorr) MaxVel_string MaskName_string...
4070        ' > "' filename '.' lower(fixname) '.log"'];
4071    cmd=regexprep(cmd,'\\','\\\\');
4072end
4073
4074
4075function cmd=cmd_patch(filename,Param,patchname)
4076%% ------------------------------------------------------------------------
4077filename=regexprep(filename,'.nc','');
4078if isunix
4079    cmd=[Param.xml.PatchBin...
4080        ' -f ' filename '.nc -m ' num2str(Param.(patchname).Nx)...
4081        ' -n ' num2str(Param.(patchname).Ny) ' -ro ' num2str(Param.(patchname).SmoothingParam)...
4082        ' -nopt ' num2str(Param.(patchname).SubdomainSize) ...
4083        '  > ' filename '.' lower(patchname) '.log 2>&1']; % redirect standard output to the log file
4084else
4085    cmd=['"' Param.xml.PatchBin...
4086        '" -f "' filename '.nc" -m ' num2str(Param.(patchname).Nx)...
4087        ' -n ' num2str(Param.(patchname).Ny) ' -ro ' num2str(Param.(patchname).SmoothingParam)...
4088        ' -nopt ' num2str(Param.(patchname).SubdomainSize)...
4089        '  > "' filename '.' lower(patchname) '.log" 2>&1']; % redirect standard output to the log file
4090    cmd=regexprep(cmd,'\\','\\\\');
4091end
4092
4093%------------------------------------------------------------------------
4094% --- CheckCiv2  CheckCiv2  CheckCiv2 CheckCiv2
4095function cmd=cmd_civ2(filename,Param)
4096%------------------------------------------------------------------------
4097%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4098% global civ2Bin sge%name of the executable for checkciv1 calculation
4099filename=regexprep(filename,'.nc','');
4100if isequal(Param.Civ2.Dt,'0')
4101    Param.Civ2.Dt='1' ;%case of 'displacement' mode
4102end
4103Param.Civ2.ImageA=regexprep(Param.Civ2.ImageA,'.png','');
4104Param.Civ2.ImageB=regexprep(Param.Civ2.ImageB,'.png','');% bug : .png appears two times ?
4105[fid,errormsg]=fopen([filename '.civ2.cmx'],'w');
4106if isequal(fid,-1)
4107    msgbox_uvmat('ERROR',errormsg)
4108    cmd='';
4109    return
4110end
4111fprintf(fid,['##############   CMX file' '\n' ]);
4112fprintf(fid,   ['FirstImage ' regexprep(Param.Civ2.ImageA,'\\','\\\\') '\n' ]);% for windows compatibility
4113fprintf(fid,   ['LastImage  ' regexprep(Param.Civ2.ImageB,'\\','\\\\') '\n' ]);% for windows compatibility
4114fprintf(fid,  ['XX' '\n' ]);
4115if isfield(Param.Civ2,'Mask')
4116    fprintf(fid,  ['Mask ' 'y' '\n' ]);
4117    fprintf(fid,  ['MaskName ' regexprep(Param.Civ2.Mask,'\\','\\\\') '\n' ]);
4118else
4119    fprintf(fid,  ['Mask ' 'n' '\n' ]);
4120    fprintf(fid,  ['MaskName ' 'noFile use default' '\n' ]);
4121end
4122% fprintf(fid, ['Mask ' Param.Civ2.MaskFlag '\n' ]);
4123% fprintf(fid, ['MaskName ' regexprep(Param.Civ2.MaskName,'\\','\\\\') '\n' ]);% for windows compatibility
4124fprintf(fid,   ['ImageSize ' num2str(Param.Civ2.ImageWidth) ' ' num2str(Param.Civ2.ImageHeight) '\n' ]); 
4125% fprintf(fid, ['ImageSize ' num2str(Param.Civ2.npx) ' ' num2str(Param.Civ2.npy) '\n' ]);   %VERIFIER CAS GENERAL ?
4126fprintf(fid, ['CorrelationBoxesSize ' num2str(Param.Civ2.Bx) ' ' num2str(Param.Civ2.By) '\n' ]);
4127fprintf(fid, ['SearchBoxeSize ' num2str(Param.Civ2.Bx) ' ' num2str(Param.Civ2.By) '\n']);
4128fprintf(fid, ['RO ' num2str(Param.Civ2.Rho) '\n']);
4129if isfield(Param.Civ2,'Grid')
4130    fprintf(fid,   ['GridSpacing ' '25' ' ' '25' '\n' ]);
4131else
4132    fprintf(fid,   ['GridSpacing ' num2str(Param.Civ2.Dx) ' ' num2str(Param.Civ2.Dy) '\n' ]);
4133end
4134% fprintf(fid, ['GridSpacing ' num2str(Param.Civ2.Dx) ' ' num2str(Param.Civ2.Dy) '\n']);
4135fprintf(fid, ['XX 1.0' '\n' ]);
4136fprintf(fid, ['Dt_TO ' num2str(Param.Civ2.Dt) ' ' num2str(Param.Civ2.Time) '\n' ]);
4137fprintf(fid, ['PixCmXY ' '1' ' ' '1' '\n' ]);
4138fprintf(fid, ['XX 1' '\n' ]);
4139fprintf(fid, 'ShiftXY 0 0\n');
4140if isfield(Param.Civ2,'Grid')
4141    fprintf(fid,  ['Grid ' 'y' '\n' ]);
4142    fprintf(fid,   ['GridName ' regexprep(Param.Civ2.Grid,'\\','\\\\') '\n' ]);
4143else
4144    fprintf(fid,  ['Grid ' 'n' '\n' ]);
4145    fprintf(fid,   ['GridName ' 'noFile use default' '\n' ]);
4146end
4147% fprintf(fid, ['Grid ' Param.Civ2.GridFlag '\n' ]);
4148% fprintf(fid, ['GridName ' regexprep(Param.Civ2.GridName,'\\','\\\\') '\n']);
4149fprintf(fid, ['XX 85' '\n' ]);
4150fprintf(fid, ['XX 1.0' '\n' ]);
4151fprintf(fid, ['XX 1.0' '\n' ]);
4152fprintf(fid, ['Hart 1' '\n' ]);
4153fprintf(fid, ['DecimalShift ' num2str(Param.Civ2.CheckDecimal) '\n']);
4154fprintf(fid, ['Deformation ' num2str(Param.Civ2.CheckDeformation) '\n']);
4155fprintf(fid,  ['CorrelationMin 0' '\n' ]);
4156fprintf(fid,   ['IntensityMin 0' '\n' ]);
4157
4158if ~isfield(Param.Civ2,'MinIma')% Image threshold not activated
4159    fprintf(fid,  ['SeuilImage n' '\n' ]);
4160    fprintf(fid,   ['SeuilImageValues 0 4096' '\n' ]);%not used in principle
4161else% Image threshold  activated
4162    if isempty(Param.Civ2.MaxIma)||isnan(Param.Civ2.MaxIma)
4163        Param.Civ2.MaxIma=2^Param.Civ2.ImageBitDepth;%take the max image value as upper bound by default
4164    end
4165    fprintf(fid,  ['SeuilImage y' '\n' ]);
4166    fprintf(fid,   ['SeuilImageValues ' num2str(Param.Civ2.MinIma) ' ' num2str(Param.Civ2.MaxIma) '\n' ]);
4167end
4168fprintf(fid,   ['ImageToUse ' Param.Civ2.term_a ' ' Param.Civ2.term_b '\n' ]); % VERIFIER ?
4169fprintf(fid, ['ImageUsedBefore ' regexprep(Param.Civ2.filename_nc1,'\\','\\\\') '\n']);
4170fclose(fid);
4171
4172if(isunix)
4173    cmd=['cp -f ' filename '.civ2.cmx ' filename '.cmx\n'...
4174        Param.xml.Civ2Bin ' -f ' filename  '.cmx >' filename '.civ2.log \n '... % redirect standard output to the log file, the result file is named [filename '.nc'] by CIVx
4175        'rm ' filename '.cmx \n'];%rename .cmx as .checkciv2.cmx, the result file is named [filename '.nc'] by CIVx
4176else
4177    filename=regexprep(filename,'\\','\\\\');
4178    cmd=['copy /Y "' filename '.civ2.cmx" "' filename '.cmx" \n'...
4179        '"' regexprep(Param.xml.Civ2Bin,'\\','\\\\') '" -f "' filename  '.cmx" >"' filename '.civ2.log" \n'...
4180        'del "' filename '.cmx" \n'];
4181end
4182
4183%------------------------------------------------------------------------
4184% --- CheckCiv1  Unified: TO ABADON
4185function xml_civ1_parameters=CIV1_CMD_Unified(filename,namelog,par)
4186%------------------------------------------------------------------------
4187%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4188%global CivBin%name of the executable for checkciv1 calculation
4189
4190civ1.image1=par.ImageA;
4191civ1.image2=par.ImageB;
4192civ1.imageSize_X=par.npx;
4193civ1.imageSize_Y=par.npy;
4194civ1.outputFileName=[filename '.nc'];
4195civ1.correlationBoxesSize_X=par.ibx;
4196civ1.correlationBoxesSize_Y=par.iby;
4197civ1.searchBoxesSize_X=par.isx;
4198civ1.searchBoxesSize_Y=par.isy;
4199civ1.globalShift_X=par.shiftx;
4200civ1.globalShift_Y=par.shifty;
4201civ1.ro=par.rho;
4202civ1.hart='y';
4203if isequal(par.gridflag,'y')
4204    civ1.grid=par.gridname;
4205else
4206    civ1.grid='n';
4207    civ1.gridSpacing_X=par.dx;
4208    civ1.gridSpacing_Y=par.dy;
4209end
4210if isequal(par.maskflag,'y')
4211    civ1.mask=par.maskname;
4212end
4213civ1.dt=par.Dt;
4214civ1.unit='pixel';
4215civ1.absolut_time_T0=par.Time;
4216civ1.pixcmx='1';
4217civ1.pixcmy='1';
4218civ1.convectFlow='n';
4219
4220xml_civ1_parameters=civ1;
4221
4222%------------------------------------------------------------------------
4223% --- CheckCiv2  Unified: TO ABADON
4224function civ2=CIV2_CMD_Unified(filename,namelog,par)
4225%------------------------------------------------------------------------
4226%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4227%global CivBin%name of the executable for checkciv1 calculation
4228
4229filename=regexprep(filename,'.nc','');
4230
4231civ2.image1=par.ImageA;
4232civ2.image2=par.ImageB;
4233civ2.imageSize_X=par.npx;
4234civ2.imageSize_Y=par.npy;
4235civ2.inputFileName=[par.filename_nc1 '.nc'];
4236civ2.outputFileName=[filename '.nc'];
4237civ2.correlationBoxesSize_X=par.ibx;
4238civ2.correlationBoxesSize_Y=par.iby;
4239civ2.ro=par.rho;
4240%checkciv2.decimalShift=par.CheckDecimal;
4241%checkciv2.CheckDeformation=par.CheckDeformation;
4242if isequal(par.decimal,'1')
4243    civ2.decimalShift='y';
4244else
4245    civ2.decimalShift='n';
4246end
4247if isequal(par.deformation,'1')
4248    civ2.deformation='y';
4249else
4250    civ2.deformation='n';
4251end
4252if isequal(par.gridflag,'y')
4253    civ2.grid=par.gridname;
4254else
4255    civ2.grid='n';
4256    civ2.gridSpacing_X=par.dx;
4257    civ2.gridSpacing_Y=par.dy;
4258end
4259civ2.gridSpacing_X='10';
4260civ2.gridSpacing_Y='10';%NOTE: faut mettre gridSpacing pourque ca tourne, meme si c'est la grille qui est utilisee
4261if isequal(par.maskflag,'y')
4262    civ2.mask=par.maskname;
4263else
4264    civ2.mask='n';
4265end
4266civ2.dt=par.Dt;
4267civ2.unit='pixel';
4268civ2.absolut_time_T0=par.Time;
4269civ2.pixcmx='1';
4270civ2.pixcmy='1';
4271civ2.convectFlow='n';
4272
4273
4274% --- Executes on button press in TestPatch1.
4275function TestPatch1_Callback(hObject, eventdata, handles)
4276set(handles.TestPatch1,'BackgroundColor',[1 1 0])
4277drawnow
4278if get(handles.TestPatch1,'Value')
4279    ref_i=str2double(get(handles.ref_i,'String'));
4280    if strcmp(get(handles.ref_j,'Visible'),'on')
4281        ref_j=str2double(get(handles.ref_j,'String'));
4282    else
4283        ref_j=1;%default
4284    end
4285    [filecell,i1,i21,j1,j2,i1_civ2,i2_civ2,j1_civ2,j2_civ2,nom_type_nc,file_ref_fix1,file_ref_fix2]=...
4286        set_civ_filenames(handles,ref_i,ref_j,[0 0 1 0 0 0]);
4287   
4288    Data.ListVarName={'ny','nx','A'};
4289    Data.VarDimName= {'ny','nx',{'ny','nx'}};
4290   
4291    param_patch1=read_GUI(handles.Patch1);
4292    param_patch1.CivFile=filecell.nc.civ1{1};
4293    Param.Patch1=param_patch1;
4294    for irho=1:7
4295        [Data,errormsg]=civ_matlab(Param);% get the grid of x, y positions set for PIV
4296        if ~isempty(errormsg)
4297            msgbox_uvmat('ERROR',errormsg)
4298            return
4299        end
4300        SmoothingParam(irho)=Param.Patch1.SmoothingParam;
4301        Data.Civ1_U_Diff=Data.Civ1_U_Diff(Data.Civ1_FF==0);
4302        Data.Civ1_V_Diff=Data.Civ1_V_Diff(Data.Civ1_FF==0);
4303        DiffVel(irho)=sqrt(mean(Data.Civ1_U_Diff.*Data.Civ1_U_Diff+Data.Civ1_V_Diff.*Data.Civ1_V_Diff))
4304        NbSites(irho,:)=Data.Civ1_NbSites*numel(Data.Civ1_NbSites)/numel(Data.Civ1_U_Diff);
4305        Param.Patch1.SmoothingParam=2*Param.Patch1.SmoothingParam;
4306    end
4307    figure
4308    plot(SmoothingParam,DiffVel,'b',SmoothingParam,NbSites,'r')
4309    set(handles.TestPatch1,'BackgroundColor',[1 0 0])
4310else
4311    corrfig=findobj(allchild(0),'tag','corrfig');% look for a current figure for image correlation display
4312    if ~isempty(corrfig)
4313        delete(corrfig)
4314    end
4315    hview_field=findobj(allchild(0),'tag','view_field');% look for view_field
4316    if ~isempty(hview_field)
4317        delete(hview_field)
4318    end
4319end
4320%'nomtype2pair': creates nomencalture for index pairs knowing the image nomenclature
4321%---------------------------------------------------------------------
4322% [nom_type_pair]=nomtype2pair(nom_type,Dti,Dtj);
4323%---------------------------------------------------------------------           
4324
4325% OUTPUT:
4326%nom_type_nc
4327
4328%---------------------------------------------------------------------
4329% INPUT:
4330% 'nom_type': string defining the kind of nomenclature used:
4331     %nom_type='': constant name [filebase ext] (default output if 'nom_type' is undefined)
4332     %nom_type='*': the same  file [filebase ext] contains successive fields (ex avi movies)
4333     %nom_type='_i': series of files with a single index i preceded by '_'(e.g. 'aa_45.png').
4334     %nom_type='#' series of indexed images wich is not series_i [filebase index ext], e.g. 'aa045.jpg' or 'aa45.tif'
4335     %nom_type='_i_j' matrix of files with two indices i and j separated by '_'(e.g. 'aa_45_2.png')
4336     %nom_type='_i1-i2' from pairs from a single index (e.g. 'aa_45-47.nc')
4337     %nom_type='_i_j1-j2'pairs of j indices (e.g. 'aa_45_2-3.nc')
4338     %nom_type='_i1-i2_j' pairs of i indices (e.g. 'aa_45-46_2.nc')
4339     %nom_type='#a','#A', with a numerical index and an index letter(e.g.'aa045b.png'), OBSOLETE (replaced by 'series_i_j')
4340     %nom_type='%03d' or '%04d', series of indexed images with numbers completed with zeros to 3 or 4 digits, e.g.'aa045.tif'
4341     %nom_type='_%03d', '_%04d', or '_%05d', series of indexed images with _ and numbers completed with zeros to 3, 4 or 5 digits, e.g.'aa_045.tif'
4342     %nom_type='raw_SMD', same as '#a' but with no extension ext='', OBSOLETE
4343     %nom_type='#_ab' from pairs of '#a' images (e.g. 'aa045bc.nc'), ext='.nc', OBSOLETE (replaced by 'netc_2D')
4344     %nom_type='%3dab' from pairs of '%3da' images (e.g. 'aa045bc.nc'), ext='.nc', OBSOLETE (replaced by 'netc_2D')
4345% Dti: ~=0 if i index pairs are used
4346% Dtj: ~=0 if i index pairs are used
4347
4348function NomTypeNc=nomtype2pair(NomTypeIma,mode)
4349
4350%determine nom_type_nc:
4351NomTypeNc=NomTypeIma;%default
4352switch mode
4353    case 'pair j1-j2'     
4354    if ~isempty(regexp(NomTypeIma,'a$'))
4355        NomTypeNc=[NomTypeIma 'b'];
4356    elseif ~isempty(regexp(NomTypeIma,'A$'))
4357        NomTypeNc=[NomTypeIma 'B'];
4358    else
4359        r=regexp(NomTypeIma,'(?<num1>\d+)_(?<num2>\d+)$','names');
4360        if ~isempty(r)
4361            NomTypeNc='_1_1-2';
4362        end
4363    end
4364    case 'series(Dj)' 
4365        r=regexp(NomTypeIma,'(?<num1>\d+)_(?<num2>\d+)$','names');
4366        if ~isempty(r)
4367            NomTypeNc='_1_1-2';
4368        end
4369   case 'series(Di)'
4370        r=regexp(NomTypeIma,'(?<num1>\d+)_(?<num2>\d+)$','names');
4371        if ~isempty(r)
4372            NomTypeNc='_1-2_1';
4373        else
4374            NomTypeNc='_1-2';
4375        end
4376end
4377
4378function NomType_Callback(hObject, eventdata, handles)
4379
4380
4381% --- Executes on selection change in ListProgram.
4382function ListProgram_Callback(hObject, eventdata, handles)
4383ListProgram=get(handles.ListProgram,'String');
4384Program=ListProgram{get(handles.ListProgram,'value')};
4385switch Program
4386    case 'CivX'
4387        set(handles.num_MaxDiff,'Visible','off')
4388        set(handles.num_Nx,'Visible','on')
4389        set(handles.num_Ny,'Visible','on')
4390        set(handles.title_Nx,'Visible','on')
4391        set(handles.title_Ny,'Visible','on')
4392        set(handles.title_MaxDiff,'Visible','off')
4393        set(handles.num_Rho,'Style','edit')
4394        set(handles.num_Rho,'String','1')
4395        set(handles.BATCH,'Enable','on')
4396    case 'Matlab'
4397        set(handles.num_MaxDiff,'Visible','on')
4398        set(handles.title_MaxDiff,'Visible','on')
4399        set(handles.num_Nx,'Visible','off')
4400        set(handles.num_Ny,'Visible','off')
4401        set(handles.title_Nx,'Visible','off')
4402        set(handles.title_Ny,'Visible','off')
4403        set(handles.num_Rho,'Style','popupmenu')
4404        set(handles.num_Rho,'Value',1)
4405        set(handles.num_Rho,'String',{'1';'2'})
4406end
4407
4408
4409% --- Executes on button press in TestCiv2.
4410function TestCiv2_Callback(hObject, eventdata, handles)
4411
4412
4413
4414function RootFile_Callback(hObject, eventdata, handles)
4415
4416
4417
Note: See TracBrowser for help on using the repository browser.