Index: /trunk/src/check_files.m
===================================================================
--- /trunk/src/check_files.m	(revision 1088)
+++ /trunk/src/check_files.m	(revision 1089)
@@ -41,8 +41,4 @@
     'cell2tab';... %transform a Matlab cell in a character array suitable for display in a table
     'check_files';...
-    'civ';...   %function associated with the interface 'civ.fig' for PIV and spline interpolation (to be replaced by civ_series)
-    'civ.fig';...
-    'civ_3D';... % function associated with the interface 'civ_3D.fig' for PIV in volume (TODO: combine with civ.m)
-    'civ_3D.fig';...
     'compile';...% compile a Matlab function, create a binary in a subdirectory /bin
     'copyfields';...% copy fields between two matlab structures
Index: unk/src/civ.m
===================================================================
--- /trunk/src/civ.m	(revision 1088)
+++ 	(revision )
@@ -1,4610 +1,0 @@
-%'civ': function associated with the interface 'civ.fig' for PIV, spline interpolation and stereo PIV (patch). OBSOLETE: REPLACED BY civ_series
-%------------------------------------------------------------------------
-%  provides an interface for the software menucivx
-% function varargout = civ(varargin)
-% provides an interface for the software menucivx
-% OBSOLETE: REPLACED BY civ_series
-
-%=======================================================================
-% Copyright 2008-2020, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
-%   http://www.legi.grenoble-inp.fr
-%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
-%
-%     This file is part of the toolbox UVMAT.
-%
-%     UVMAT is free software; you can redistribute it and/or modify
-%     it under the terms of the GNU General Public License as published
-%     by the Free Software Foundation; either version 2 of the license,
-%     or (at your option) any later version.
-%
-%     UVMAT is distributed in the hope that it will be useful,
-%     but WITHOUT ANY WARRANTY; without even the implied warranty of
-%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-%     GNU General Public License (see LICENSE.txt) for more details.
-%=======================================================================
-
-function varargout = civ(varargin)
-%TODO: search range
-
-% Last Modified by GUIDE v2.5 09-Mar-2013 22:50:23
-% Begin initialization code - DO NOT EDIT
-gui_Singleton = 1;
-gui_State = struct('gui_Name',       mfilename, ...
-    'gui_Singleton',  gui_Singleton, ...
-    'gui_OpeningFcn', @civ_OpeningFcn, ...
-    'gui_OutputFcn',  @civ_OutputFcn, ...
-    'gui_LayoutFcn',  [] , ...
-    'gui_Callback',   []);
-if nargin && ischar(varargin{1}) && ~isempty(regexp(varargin{1},'_Callback$','once'))
-    gui_State.gui_Callback = str2func(varargin{1});
-end
-
-if nargout
-    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
-else
-    gui_mainfcn(gui_State, varargin{:});
-end
-% End initialization code - DO NOT EDIT
-
-%------------------------------------------------------------------------
-% --- Executes just before civ is made visible.
-function civ_OpeningFcn(hObject, eventdata, handles, fileinput)
-%------------------------------------------------------------------------
-% This function has no output args, see OutputFcn.
-
-%% General settings
-handles.output = hObject;
-guidata(hObject, handles); % Update handles structure
-set(hObject,'WindowButtonDownFcn',{'mouse_down'}) % allows mouse action with right button (zoom for uicontrol display)
-
-%% Adjust the GUI according to the binaries available in PARAM.xml
-path_civ=fileparts(which('civ')); %path to civ
-addpath (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)
-errormsg=[];%default error message
-xmlfile=fullfile(path_civ,'PARAM.xml');
-test_batch=0;%default: ,no batch mode available
-sparam=[];
-if ~exist(xmlfile,'file')
-    [success,message]=copyfile(fullfile(path_civ,'PARAM.xml.default'),xmlfile);
-end
-if exist(xmlfile,'file')
-    try
-        t=xmltree(xmlfile);
-        sparam=convert(t);
-    catch ME
-        errormsg={' Unable to read the file PARAM.xml defining the civx binaries:';ME.message};
-         msgbox_uvmat('WARNING',errormsg);
-    end
-else
-    [s,w]=system('oarstat');
-    if ~isequal(s,0)
-        [s,w]=system('qstat');
-    end
-    if isequal(s,0)
-        test_batch=1;
-    end           
-end
-if isfield(sparam,'BatchParam') && isfield(sparam.BatchParam,'BatchMode')
-    batch_mode=sparam.BatchParam.BatchMode;
-    test_command='';
-    switch batch_mode
-        case 'sge'
-            test_command='qstat';
-        case 'oar'
-            test_command='oarstat';
-    end
-    if ~isempty(test_command)
-        [s,w]=system(test_command);
-        if isequal(s,0)
-            test_batch=1;
-        end
-    end
-end
-RUNVal=get(handles.RunMode,'Value');
-if test_batch==0
-   if RUNVal>2
-       set(handles.RunMode,'Value',1)
-   end
-   set(handles.RunMode,'String',{'local';'background'})
-else
-    set(handles.RunMode,'String',{'local';'background';'cluster'})
-end
-
-%% load the list of previously browsed files in the upper bar menu Open/
-dir_perso=prefdir; % path to the directory .matlab for personal data
-profil_perso=fullfile(dir_perso,'uvmat_perso.mat');% personal data file uvmauvmat_perso.mat' in .matlab
-if exist(profil_perso,'file')
-    h=load (profil_perso);
-    if isfield(h,'MenuFile')
-        for ifile=1:min(length(h.MenuFile),5)
-            eval(['set(handles.MenuFile_' num2str(ifile) ',''Label'',h.MenuFile{ifile});'])
-        end
-    end
-end
-
-%% prepare the GUI with parameters from the input file if opened from uvmat
-if exist('fileinput','var')% && isfield(param,'RootName') && ~isempty(param.RootName)
-    set(handles.RootPath,'BackgroundColor',[1 1 0])%paint RootName edit box in yellow to indicate that the file input is proceeding
-    errormsg=display_file_name(handles,fileinput);
-    if ~isempty(errormsg)
-        msgbox_uvmat('ERROR',errormsg)
-    end
-    set(handles.RootPath,'BackgroundColor',[1 1 1])%paint RootName back to white to indicate that the file input is finished
-end
-Program_Callback([],[], handles)
-
-%------------------------------------------------------------------------
-% --- Outputs from this function are returned to the command line.
-function varargout = civ_OutputFcn(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-% Get default command line output from handles structure
-varargout{1} = handles.output;
-
-%------------------------------------------------------------------------
-% --- Function activated by the Open/Browse... option in the upper menu bar.
-function MenuBrowse_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-%% get the current input root file name to initiate the browser
-filebase=get(handles.RootPath,'String');
-oldfile=''; %default
-if isempty(filebase)|| isequal(filebase,'')%loads the previously stored root file name
-    dir_perso=prefdir;
-    profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
-    if exist(profil_perso,'file')
-        h=load (profil_perso);
-        if isfield(h,'filebase')&& ischar(h.filebase)
-            oldfile=h.filebase;
-        end
-        if isfield(h,'RootPath') && ischar(h.RootPath)
-            oldfile=h.RootPath;
-        end
-    end
-else
-    oldfile=filebase;
-end
-
-%% get the new input file with the browser
-menu={'*.xml;*.civ;*.png;*.jpg;*.tif;*.avi;*.AVI;*.nc;', ' (*.xml,*.civ,*.png,*.jpg ,.tif, *.avi,*.nc)';
-    '*.xml',  '.xml files '; ...
-    '*.civ',  '.civ files '; ...
-    '*.png','.png image files'; ...
-    '*.jpg',' jpeg image files'; ...
-    '*.tif','.tif image files'; ...
-    '*.avi;*.AVI','.avi movie files'; ...
-    '*.nc','.netcdf files'; ...
-    '*.*',  'All Files (*.*)'};
-[FileName, PathName, filtindex] = uigetfile( menu, 'Pick a file',oldfile);
-fileinput=[PathName FileName];%complete file name
-sizf=size(fileinput);
-if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
-
-%% case of the xml file opened as input (TODO: check and see whether it is useful)
-[path,name,ext]=fileparts(fileinput);
-testeditxml=0;
-% if isequal(ext,'.xml')
-%     testeditxml=1;
-%     t_browse=xmltree(fileinput);
-%     head_element=get(t_browse,1);
-%     if isfield(head_element,'name')&& isequal(head_element.name,'ImaDoc')
-%         testeditxml=0;
-%     end
-% end
-% if testeditxml==1 || isequal(ext,'.xls')
-%     heditxml=editxml({fileinput});
-%     set(heditxml,'Tag','browser')
-%     waitfor(heditxml,'Tag','idle')
-%     if ~ishandle(heditxml)
-%         return
-%     end
-%     attr=findobj(get(heditxml,'children'),'Tag','CurrentAttributes');
-%     set(handles.browse,'UserData',fileinput)% store for future opening with browser
-%     fileinput=get(attr,'UserData');
-%     if ~exist(fileinput,'file')
-%         return
-%     end
-% end
-[tild,tild,tild,i1,i2,j1,j2,FileExt,NomType]=fileparts_uvmat(fileinput);
-
-%% prepare the GUI with parameters from the input file 
-set(handles.RootPath,'BackgroundColor',[1 1 0])%paint RootName edit box in yellow to indicate that the file input is proceeding
-errormsg=display_file_name(handles,fileinput);
-if ~isempty(errormsg)
-    msgbox_uvmat('ERROR',erromsg)
-end
-set(handles.RootPath,'BackgroundColor',[1 1 1])%paint RootName back to white to indicate that the file input is finished
-
-%------------------------------------------------------------------------
-% --- Open again the file whose name has been recorded in MenuFile_1
-function MenuFile_1_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-set(handles.RootPath,'BackgroundColor',[1 1 0])%paint RootName edit box in yellow to indicate that the file input is proceeding
-fileinput=get(handles.MenuFile_1,'Label');
-errormsg=display_file_name(handles,fileinput);
-if ~isempty(errormsg)
-    msgbox_uvmat('ERROR',errormsg)
-end
-set(handles.RootPath,'BackgroundColor',[1 1 1])%paint RootName back to white to indicate that the file input is finished
-
-% -----------------------------------------------------------------------
-% --- Open again the file whose name has been recorded in MenuFile_2
-function MenuFile_2_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-set(handles.RootPath,'BackgroundColor',[1 1 0])%paint RootName edit box in yellow to indicate that the file input is proceeding
-fileinput=get(handles.MenuFile_2,'Label');
-errormsg=display_file_name(handles,fileinput);
-if ~isempty(errormsg)
-    msgbox_uvmat('ERROR',errormsg)
-end
-set(handles.RootPath,'BackgroundColor',[1 1 1])%paint RootName back to white to indicate that the file input is finished
-
-% -----------------------------------------------------------------------
-% --- Open again the file whose name has been recorded in MenuFile_3
-function MenuFile_3_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-set(handles.RootPath,'BackgroundColor',[1 1 0])%paint RootName edit box in yellow to indicate that the file input is proceeding
-fileinput=get(handles.MenuFile_3,'Label');
-errormsg=display_file_name(handles,fileinput);
-if ~isempty(errormsg)
-    msgbox_uvmat('ERROR',errormsg)
-end
-set(handles.RootPath,'BackgroundColor',[1 1 1])%paint RootName back to white to indicate that the file input is finished
-
-% -----------------------------------------------------------------------
-% --- Open again the file whose name has been recorded in MenuFile_4
-function MenuFile_4_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-set(handles.RootPath,'BackgroundColor',[1 1 0])%paint RootName edit box in yellow to indicate that the file input is proceeding
-fileinput=get(handles.MenuFile_4,'Label');
-errormsg=display_file_name(handles,fileinput);
-if ~isempty(errormsg)
-    msgbox_uvmat('ERROR',errormsg)
-end
-set(handles.RootPath,'BackgroundColor',[1 1 1])%paint RootName back to white to indicate that the file input is finished
-
-% -----------------------------------------------------------------------
-% --- Open again the file whose name has been recorded in MenuFile_5
-function MenuFile_5_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-set(handles.RootPath,'BackgroundColor',[1 1 0])%paint RootName edit box in yellow to indicate that the file input is proceeding
-fileinput=get(handles.MenuFile_5,'Label');
-errormsg=display_file_name(handles,fileinput);
-if ~isempty(errormsg)
-    msgbox_uvmat('ERROR',errormsg)
-end
-set(handles.RootPath,'BackgroundColor',[1 1 1])%paint RootName back to white to indicate that the file input is finished
-
-% -----------------------------------------------------------------------
-% -----------------------------------------------------------------------
-% --- Open the help html file 
-function MenuHelp_Callback(hObject, eventdata, handles)
-% -----------------------------------------------------------------------
-path_civ=fileparts(which ('civ'));
-helpfile=fullfile(path_civ,'uvmat_doc','uvmat_doc.html');
-if isempty(dir(helpfile))
-    msgbox_uvmat('ERROR','Please put the help file uvmat_doc.html in the sub-directory /uvmat_doc of the UVMAT package')
-else
-    addpath (fullfile(path_civ,'uvmat_doc'))
-    web([helpfile '#civ'])
-end
-
-%------------------------------------------------------------------------
-% --- Function activated when a new filebase (image series) is introduced
-function RootPath_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-set(handles.RootPath,'BackgroundColor',[1 1 0])%paint RootName edit box in yellow to indicate that the file input is proceeding
-RootPath=get(handles.RootPath,'String');
-SubDirImages=get(handles.SubDirImages,'String');
-RootFile=get(handles.RootFile,'String');
-ref_i=str2num(get(handles.ref_i,'String'));
-ref_j=str2num(get(handles.ref_j,'String'));
-NomType=get(handles.NomType,'String');
-ImaExt=get(handles.ImaExt,'String');
-fileinput=fullfile_uvmat(RootPath,SubDirImages,RootFile,ImaExt,NomType,ref_i,[],ref_j);
-errormsg=display_file_name(handles,fileinput);
-if ~isempty(errormsg)
-    msgbox_uvmat('ERROR',errormsg)
-end
-set(handles.RootPath,'BackgroundColor',[1 1 1])%paint RootName back to white to indicate that the file input is finished
-
-%------------------------------------------------------------------------
-% --- general function activated for an input file series
-function errormsg=display_file_name(handles,fileinput)
-%------------------------------------------------------------------------
-set(handles.ListCompareMode,'Visible','on')
-errormsg='';%default empty error message
-drawnow
-
-%% enable RUN, BATCH button and 'status' display
-set(handles.RUN, 'Enable','On')
-set(handles.RUN,'BackgroundColor',[1 0 0])%set RUN button to red color
-if isfield(handles,'status')
-    set(handles.status,'Value',0);       %suppress the 'status' display
-    status_Callback([], [], handles)
-end
-
-%% determine nomenclature types and extension of the input files
-[RootPath,SubDir,RootFile,i1,i2,j1,j2,ExtInput,NomTypeInput]=fileparts_uvmat(fileinput);
-NomTypeNc='';%default
-
-%% case of xml file as input, read the civ parameters
-ind_opening=0;%default
-if strcmp(ExtInput,'.xml')
-    %reinitialise menus
-        set(handles.ListPairMode,'Value',1)
-    set(handles.ListPairMode,'String',{''})
-    set(handles.ListPairCiv1,'Value',1)
-    set(handles.ListPairCiv1,'String',{''})
-        set(handles.ListPairCiv2,'Value',1)
-    set(handles.ListPairCiv2,'String',{''}) 
-    Param=xml2struct(fileinput);  %read parameters from the xml input file
-    fill_GUI(Param,handles.civ);%fill the GUI with the parameters retrieved from the xml file 
-    return
-end
-
-%% case of netcdf file as input, get the civ processing stage and look for a coresponding image
-imageinput=fileinput;%default
-if strcmp(ExtInput,'.nc')
-    NomTypeNc=NomTypeInput;
-    if isempty(regexp(NomTypeInput,'[ab|AB|-]', 'once'))
-        set(handles.ListCompareMode,'Value',2) %mode displacement advised if the nomencalture does not involve index pairs
-        set(handles.RootFile_1,'Visible','On');
-    else
-        set(handles.ListCompareMode,'Value',1)
-        set(handles.RootFile_1,'Visible','Off');
-    end
-    imageinput='';
-    Data=nc2struct(fileinput,'ListGlobalAttribute','Conventions','absolut_time_T0','CivStage','Civ2_ImageA','Civ1_ImageA','Civ2_ImageB','Civ1_ImageB','fix','patch','civ2','fix2');
-    if isfield(Data,'Txt')
-        errormsg=Data.Txt;
-        return
-    end
-    % settings for  new civ data,
-    if strcmp(Data.Conventions,'uvmat/civdata')% case of new civ data,
-        set(handles.Program,'Value',1) %select civ/Matlab by default
-        Program_Callback([],[], handles)
-        if ~isempty(Data.CivStage)%test for civ files
-            ind_opening=Data.CivStage;
-        end
-        if  ~isempty(Data.Civ2_ImageB)%get the corresponding input image in the netcdf file
-            imageinput=Data.Civ2_ImageB;
-            [tild,ImaName,ImaExt]=fileparts(Data.Civ2_ImageA);
-            set(handles.RootFile_1,'String',[ImaName ImaExt])
-        elseif ~isempty(Data.Civ1_ImageB)
-            imageinput=Data.Civ1_ImageB;
-            [tild,ImaName,ImaExt]=fileparts(Data.Civ1_ImageA);
-            set(handles.RootFile_1,'String',[ImaName ImaExt])
-        end
-        % settings for civx data,
-    elseif ~isempty(Data.absolut_time_T0')% case of  civx data,
-        set(handles.Program,'Value',3) %select Cix by default
-        Program_Callback([],[], handles)
-        if ~isempty(Data.fix2)
-            ind_opening=5;
-        elseif ~isempty(Data.civ2)
-            ind_opening=4;
-        elseif ~isempty(Data.patch)
-            ind_opening=3;
-        elseif ~isempty(Data.fix)
-            ind_opening=2;
-        end
-    else
-        errormsg='the input netcdf file is not civ data';
-        return
-    end
-    % look for the corresponding input images
-    check_letter=~isempty(regexp(NomTypeInput,'[ab|AB]$','once'));%detect pair label by letter
-    NomTypeIma=NomTypeInput;
-    if check_letter
-        NomTypeIma=NomTypeInput(1:end-1);
-    else
-        r=regexp(NomTypeIma,'.-(?<num2>\d+)$','names');
-        if ~isempty(r)
-            NomTypeIma=regexprep(NomTypeIma,['-' r.num2],'');
-        end
-        r=regexp(NomTypeIma,'.-(?<num2>\d+)','names');
-        if ~isempty(r)
-            NomTypeIma=regexprep(NomTypeIma,['-' r.num2],'');
-        end
-    end
-    if ~exist(imageinput,'file')
-    imageinput=fullfile_uvmat(RootPath,regexprep(SubDir,'.civ(_?)(\d*)$',''),RootFile,'.png',NomTypeIma,i1,[],j1);
-    end
-end
-
-%% no corresponding image found, select manually with the browser
-ImaExt=ExtInput;
-if ~isempty(NomTypeNc)
-    %no corresponding image found, select manually with the browser
-    if ~exist(imageinput,'file')
-        menu={'*.png;*.jpg;*.tif;*.avi;*.AVI', '(*.png,*.jpg ,*.tif, *.avi,*.AVI)';
-            '*.png','.png image files'; ...
-            '*.jpg',' jpeg image files'; ...
-            '*.tif','.tif image files'; ...
-            '*.avi;*.AVI','.avi movie files'; ...
-            '*.*',  'All Files (*.*)'};
-        [FileName, PathName] = uigetfile( menu, 'Pick an input image file',fileparts(fileparts(fileinput)));
-        imageinput=[PathName FileName];%complete file name
-        if ~exist(imageinput,'file')
-            return %abandon of the browser is cancelled
-        end
-    end    
-    %fileinput=imageinput;
-end
-
-%% scan the image file series 
-[FilePath,FileName,ImaExt]=fileparts(imageinput);
-% detect the file type, get the movie object if relevant, and look for the corresponding file series:
-% the root name and indices may be corrected by including the first index i1 if a corresponding xml file exists
-[RootPath,SubDirImages,RootFile,i1_series,tild,j1_series,tild,NomTypeIma,FileInfo,MovieObject]=find_file_series(FilePath,[FileName ImaExt]);
-FileType=FileInfo.FileType;
-switch FileType
-    case {'image','multimage','video','mmreader'}
-    otherwise
-        errormsg='invalid input file: enter an image, a movie or civ .nc file';
-        return
-end
-set(handles.RootPath,'String',RootPath)
-set(handles.SubDirImages,'String',SubDirImages)
-set(handles.RootFile,'String',RootFile)
-if strcmp(ExtInput,'.nc')
-    SubDirCiv=regexprep(SubDir,['^' SubDirImages],'');%suppress the root  SuddirImages;
-else
-    SubDirCiv= '.civ';
-end
-set(handles.SubdirCiv1,'String',SubDirCiv)
-set(handles.SubdirCiv2,'String',SubDirCiv)
-browse=get(handles.RootPath,'UserData');
-browse.incr_pair=[0 0];%default
-
-%% scan the images if a civ file has been opened
-MinIndex_i=min(i1_series(i1_series>0));
-MinIndex_j=min(j1_series(j1_series>0));
-MaxIndex_i=max(i1_series(i1_series>0));
-MaxIndex_j=max(j1_series(j1_series>0));
-
-%% look for an image documentation file
-XmlFileName=find_imadoc(RootPath,SubDir,RootFile,ImaExt);
-if isempty(XmlFileName)
-    if (strcmp(FileType,'video') || strcmp(FileType,'mmreader'))
-        ext_imadoc=ImaExt;% the timing from the video movie is used
-    else
-        ext_imadoc='';
-    end
-else
-    [tild,tild,ext_imadoc]=fileparts(XmlFileName);
-end
-set(handles.ImaDoc,'String',ext_imadoc)% display the extension name for the image documentation file used
-
-%%  read the time in the image documentation file  
-time=[];
-TimeUnit=''; %default
-CoordUnit='';%default
-pxcm_search=1;
-if ~isempty(XmlFileName)
-    set(handles.ImaDoc,'BackgroundColor',[1 1 0]) % set edit box to yellow cloro to indicate that the file reading is beginning
-    drawnow
-    [XmlData,warntext]=imadoc2struct(XmlFileName);
-    nom_type_read=[];
-    if isfield(XmlData,'Time') && ~isempty(XmlData.Time)
-        time=XmlData.Time;
-        %transform .Time to a column vector if it is a line vector thenomenclature uses a single index: correct possible bug in xml
-        if isequal(MaxIndex_i,1) && ~isequal(MaxIndex_j,1)% .Time is a line vector
-            if numel(nom_type_read)>=2 && isempty(regexp(nom_type_read(2:end),'\D','once'))
-                time=time';
-                MaxIndex_i=MaxIndex_j;
-                MaxIndex_j=1;
-            end
-        end
-    end
-    if isfield(XmlData,'Camera') && isfield(XmlData.Camera,'TimeUnit')
-        TimeUnit=XmlData.Camera.TimeUnit;
-    end
-    if isfield(XmlData,'GeometryCalib')
-        tsai=XmlData.GeometryCalib;
-        if isfield(tsai,'fx_fy')
-            pxcm_search=max(tsai.fx_fy(1),tsai.fx_fy(2));%pixels:cm estimated for the search range
-        end
-        if isfield(tsai,'CoordUnit')
-            CoordUnit=tsai.CoordUnit;
-        end
-    end
-end
-if isempty(time) && (strcmp(FileType,'video') || strcmp(FileType,'mmreader'))
-    set(handles.ListPairMode,'Value',1);
-    dt=1/get(MovieObject,'FrameRate');%time interval between successive frames
-    if strcmp(NomTypeIma,'*')
-        set(handles.ListPairMode,'String',{'series(Di)'})
-        MaxIndex_i=get(MovieObject,'NumberOfFrames');
-        %time=(dt*(0:MaxIndex_i-1))';%list of image times
-                    time=zeros(MaxIndex_i+1,2);
-            time(:,2)=dt*(0:MaxIndex_i)';
-    else
-        set(handles.ListPairMode,'String',[{'series(Dj)'};{'series(Di)'}])
-        MaxIndex_i=max(i1_series(i1_series>0));
-        MaxIndex_j=get(MovieObject,'NumberOfFrames');
-        %time=ones(MaxIndex_i,1)*(dt*(0:MaxIndex_j-1));%list of image times
-        time=[0;dt*ones(MaxIndex_j,1)]*(0:MaxIndex_j);
-        enable_j(handles,'on')
-    end
-    TimeUnit='s';
-    set(handles.ImaDoc,'BackgroundColor',[1 1 1])% set display box back to whiter
-end
-
-%% timing display
-%show the reference image edit box if relevant (not needed for movies or in the absence of time information
-if numel(time)>=2 % if there are at least two time values to define dt
-    if size(time,1)<MaxIndex_i+1;
-        msgbox_uvmat('WARNING','maximum i index restricted by the timing of the xml file');
-    elseif size(time,2)<MaxIndex_j+1
-        msgbox_uvmat('WARNING','maximum j index restricted by the timing of the xml file');
-    end
-    MaxIndex_i=min(size(time,1)-1,MaxIndex_i);%possibly adjust the max index according to time data
-    MaxIndex_j=min(size(time,2)-1,MaxIndex_j);
-%     time=[zeros(size(time,1),1) time]; %insert a vertical line of zeros (to deal with zero file indices)
-%     time=[zeros(1,size(time,2)); time]; %insert a horizontal line of zeros
-else
-    set(handles.ImaDoc,'String',''); %xml file not used for timing
-    time=(i1_series(:,1)+0:size(i1_series,3));% time=index i
-    time=time'*ones(1,size(i1_series,2)+1,1); %makes a time matrix with the same time for all j indices
-    TimeUnit='frame';
-end
-set(handles.ImaDoc,'UserData',time); %store the matrix of times
-set(handles.dt_unit,'String',['dt in m' TimeUnit]);%display dt in unit 10-3 of the time (e.g ms)
-set(handles.TimeUnit,'String',TimeUnit);
-set(handles.nb_field,'String',num2str(MaxIndex_i));
-set(handles.nb_field2,'String',num2str(MaxIndex_j));
-set(handles.CoordUnit,'String',CoordUnit)
-set(handles.SearchRange,'UserData', pxcm_search);
-set(handles.ImaExt,'String',ImaExt)
-set(handles.NomType,'String',NomTypeIma)
-
-%% set the reference indices from the input file indices
-num_ref_i=str2num(get(handles.ref_i,'String'));
-num_ref_j=str2num(get(handles.ref_j,'String'));
-% for movies don't modify except if the current ref is outside index bounds
-%if strcmp(ExtInput,'.nc')|| ~(strcmp(FileType,'mmreader')||strcmp(FileType,'VideoReader') && num_ref_i<=MaxIndex_i && num_ref_j<=MaxIndex_j)
-if ~isempty(i1)% if i1 has been selected by the input
-    num_ref_i=i1;%default ref index
-    if ~isempty(i2)
-        num_ref_i=floor((num_ref_i+i2)/2);
-    end
-    if ~isempty(j1)
-    num_ref_j=j1;
-    if ~isempty(j2)
-        num_ref_j=floor((num_ref_j+j2)/2);
-    end
-    end
-end
-if num_ref_i>MaxIndex_i||num_ref_i<MinIndex_i
-    num_ref_i=round((MinIndex_i+MaxIndex_i)/2);
-end
-if ~isempty(num_ref_j)&&~isempty(MaxIndex_j)&& ~isempty(MinIndex_j)
-    if (num_ref_j>MaxIndex_j||num_ref_j<MinIndex_j)
-        num_ref_j=round((MinIndex_j+MaxIndex_j)/2);
-    end
-end
-if isempty(num_ref_j)
-    num_ref_j=1;
-end
-
-%% update i and j index range if a nc file has been opened or pb withmin max image indices: 
-% then set first and last to the inputfile index by default
-first_i=str2num(get(handles.first_i,'String'));
-last_i=str2num(get(handles.last_i,'String'));
-if isempty(first_i) || isempty(last_i)||isempty(MinIndex_i)||isempty(MaxIndex_i)||ind_opening~=0 || isempty(first_i) || isempty(last_i)|| first_i<MinIndex_i || last_i>MaxIndex_i
-   first_i=num_ref_i;
-   last_i=num_ref_i;
-    set(handles.first_i,'String',num2str(first_i));
-    set(handles.last_i,'String',num2str(last_i));%
-end
-
-%j index range 
-first_j=str2num(get(handles.first_j,'String'));
-last_j=str2num(get(handles.last_j,'String'));
-if isempty(first_j) || isempty(last_j)||isempty(MinIndex_j)||isempty(MaxIndex_j)||ind_opening~=0 || first_j<MinIndex_j || last_j>MaxIndex_j
-       first_j=num_ref_j;
-   last_j=num_ref_j;
-    set(handles.first_j,'String',num2str(first_j));
-    set(handles.last_j,'String',num2str(last_j));%
-end
-if num_ref_i>last_i || num_ref_i<first_i 
-    num_ref_i=round((first_i+last_i)/2);
-end
-if num_ref_j>last_j || num_ref_j<first_j
-    num_ref_j=round((first_j+last_j)/2);
-end
-set(handles.ref_i,'String',num2str(num_ref_i))
-set(handles.ref_j,'String',num2str(num_ref_j))
-
-%% set the civ options depending on the input file content when a nc file has been opened
-ListOptions={'CheckCiv1', 'CheckFix1' 'CheckPatch1', 'CheckCiv2', 'CheckFix2', 'CheckPatch2'};
-checkbox=zeros(size(ListOptions));%default
-if ind_opening==0%case of image opening, start with Civ1
-    for index=1:numel(ListOptions)
-        checkbox(index)=get(handles.(ListOptions{index}),'Value');
-    end
-    index_max=find(checkbox, 1, 'last' );
-    if isempty(index_max),index_max=1;end        
-    for index=1:index_max
-        set(handles.(ListOptions{index}),'Value',1)% select all operations starting from CIV1
-    end
-else
-    for index = 1:min(ind_opening,5)
-        set(handles.(ListOptions{index}),'value',0)
-    end
-    set(handles.(ListOptions{min(ind_opening+1,6)}),'value',1)
-    for index = ind_opening+2:6
-        set(handles.(ListOptions{index}),'value',0)
-    end
-end
-%list_operation={'CheckCiv1','CheckFix1','CheckPatch1','CheckCiv2','CheckFix2','CheckPatch2'};
-
-%set(handles.(ListOptions{min(ind_opening+1,6)}),'value',1)
-update_CivOptions(handles,ind_opening)
-
-%%  set the menus of image pairs and default selection for civ   %%%%%%%%%%%%%%%%%%%
-%check_letter=~isempty(regexp(NomTypeIma,'[ab|AB]$'));%detect pair label by letter
-if  isequal(NomTypeNc,'_1-2')||isempty(MaxIndex_j)|| (MaxIndex_j==1)
-    set(handles.ListPairMode,'Value',1)
-    set(handles.ListPairMode,'String',{'series(Di)'})   
-elseif  MaxIndex_i==1 && MaxIndex_j>1% simple series in j
-    set(handles.ListPairMode,'String',{'pair j1-j2';'series(Dj)'})
-    if  MaxIndex_j <= 10
-        set(handles.ListPairMode,'Value',1)% advice 'pair j1-j2' except in MaxIndex_j is large
-    end
-elseif ~(strcmp(FileType,'video') || strcmp(FileType,'mmreader'))
-    set(handles.ListPairMode,'String',{'pair j1-j2';'series(Dj)';'series(Di)'})%multiple choice
-    if strcmp(NomTypeNc,'_1-2_1')
-        set(handles.ListPairMode,'Value',3)% advise 'series(Di)'
-    elseif  MaxIndex_j <= 10
-        set(handles.ListPairMode,'Value',1)% advice 'pair j1-j2' except in MaxIndex_j is large
-    end
-end
-
-%% scan files to update the subdirectory list display
-listot=dir(RootPath);%directory of RootPath
-idir=0;
-listdir={''};%default
-% get the list of existing civ subdirectories in the path of theinput root  file
-for ilist=1:length(listot)
-    if listot(ilist).isdir
-        name=listot(ilist).name;
-        if ~isequal(name,'.') && ~isequal(name,'..')
-            idir=idir+1;
-            listdir{idir,1}=listot(ilist).name;
-        end
-    end
-end
-
-%% store info
-set(handles.RootPath,'UserData',browse)% store the nomenclature type
-
-%% list the possible index pairs, depending on the option set in ListPairMode
-ListPairMode_Callback([], [], handles)
-
-%% store the root input filename for future opening
-profil_perso=fullfile(prefdir,'uvmat_perso.mat');
-if exist(profil_perso,'file')
-    save (profil_perso,'RootPath','-append'); %store the root name for future opening of uvmat
-else
-    txt=ver('MATLAB');
-    Release=txt.Release;
-    relnumb=str2double(Release(3:4));
-    if relnumb >= 14
-        save (profil_perso,'RootPath','-V6'); %store the root name for future opening of uvmat
-    else
-        save (profil_perso,'RootPath'); %store the root name for future opening of uvmat
-    end
-end
-set(handles.RootPath,'BackgroundColor',[1 1 1])
-
-%------------------------------------------------------------------------
-% --- Executes on carriage return on the subdir checkciv1 edit window
-function SubdirCiv1_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-SubDir=get(handles.SubdirCiv1,'String');
-menu_str=get(handles.ListSubdirCiv1,'String');% read the list of subdirectories for update
-ichoice=find(strcmp(SubDir,menu_str),1);
-if isempty(ichoice)
-    ilist=numel(menu_str); %select 'new...' in the menu
-else
-    ilist=ichoice;
-end
-set(handles.ListSubdirCiv1,'Value',ilist)% select the selected subdir in the menu
-if get(handles.CheckCiv1,'Value')% if Civ1 is performed
-    set(handles.SubdirCiv2,'String',SubDir);% set by default civ2 directory the same as civ1 
-%     set(handles.ListSubdirCiv2,'Value',ilist)
-else % if Civ1 data already exist
-    errormsg=find_netcpair_civ(handles,1); %update the list of available pairs from netcdf files in the new directory
-    if ~isempty(errormsg)
-    msgbox_uvmat('ERROR',errormsg)
-    end
-end
-
-%------------------------------------------------------------------------
-% --- Executes on carriage return on the SubDir checkciv1 edit window
-function SubdirCiv2_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-SubDir=get(handles.SubdirCiv1,'String');
-menu_str=get(handles.ListSubdirCiv2,'String');% read the list of subdirectories for update
-ichoice=find(strcmp(SubDir,menu_str),1);
-if isempty(ichoice)
-    ilist=numel(menu_str); %select 'new...' in the menu
-else
-    ilist=ichoice;
-end
-set(handles.ListSubdirCiv2,'Value',ilist)% select the selected subdir in the menu
-%update the list of available pairs from netcdf files in the new directory
-if ~get(handles.CheckCiv2,'Value') && ~get(handles.CheckCiv1,'Value') && ~get(handles.CheckFix1,'Value') && ~get(handles.CheckPatch1,'Value')
-    errormsg=find_netcpair_civ(handles,2);
-        if ~isempty(errormsg)
-    msgbox_uvmat('ERROR',errormsg)
-    end
-end
-
-%------------------------------------------------------------------------
-% --- Executes on button press in CheckCiv1.
-function CheckCiv1_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-update_CivOptions(handles,0)
-
-%------------------------------------------------------------------------
-% --- Executes on button press in CheckFix1.
-function CheckFix1_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-update_CivOptions(handles,0)
-
-%------------------------------------------------------------------------
-% --- Executes on button press in CheckPatch1.
-function CheckPatch1_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-update_CivOptions(handles,0)
-
-%------------------------------------------------------------------------
-% --- Executes on button press in CheckCiv2.
-function CheckCiv2_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-update_CivOptions(handles,0)
-
-%------------------------------------------------------------------------
-% --- Executes on button press in CheckFix2.
-function CheckFix2_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-update_CivOptions(handles,0)
-
-%------------------------------------------------------------------------
-% --- Executes on button press in CheckPatch2.
-function CheckPatch2_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-update_CivOptions(handles,0)
-
-%------------------------------------------------------------------------
-% --- activated by any checkbox controling the selection of Civ1,Fix1,Patch1,Civ2,Fix2,Patch2
-function update_CivOptions(handles,opening)
-%------------------------------------------------------------------------
-checkbox=zeros(1,6);
-checkbox(1)=get(handles.CheckCiv1,'Value');
-checkbox(2)=get(handles.CheckFix1,'Value');
-checkbox(3)=get(handles.CheckPatch1,'Value');
-checkbox(4)=get(handles.CheckCiv2,'Value');
-checkbox(5)=get(handles.CheckFix2,'Value');
-checkbox(6)=get(handles.CheckPatch2,'Value');
-ind_selected=find(checkbox,1);
-if ~isempty(ind_selected)
-    RootPath=get(handles.RootPath,'String');
-    if isempty(RootPath)
-        msgbox_uvmat('ERROR','Please open an image or PIV .nc file with the upper bar menu Open/Browse...')
-        return
-    end
-end
-set(handles.PairIndices,'Visible','on')
-set(handles.SubdirCiv1,'Visible','on')
-set(handles.TitleSubdirCiv1,'Visible','on')
-if opening==0
-    errormsg=find_netcpair_civ(handles,1); % select the available netcdf files
-    if ~isempty(errormsg)
-        msgbox_uvmat('ERROR',errormsg)
-    end
-end
-if max(checkbox(4:6))% case of civ2 pair choice needed
-    set(handles.TitlePairCiv2,'Visible','on')
-    set(handles.TitleSubdirCiv2,'Visible','on')
-    set(handles.SubdirCiv2,'Visible','on')
-    %set(handles.ListSubdirCiv2,'Visible','on')
-    set(handles.ListPairCiv2,'Visible','on')
-    if ~opening
-        errormsg=find_netcpair_civ(handles,2); % select the available netcdf files
-        if ~isempty(errormsg)
-            msgbox_uvmat('ERROR',errormsg)
-        end
-    end
-else
-    set(handles.TitleSubdirCiv2,'Visible','off')
-    set(handles.SubdirCiv2,'Visible','off')
-    set(handles.ListPairCiv2,'Visible','off')
-end
-options={'Civ1','Fix1','Patch1','Civ2','Fix2','Patch2'};
-for ilist=1:length(options)
-    if checkbox(ilist)
-        set(handles.(options{ilist}),'Visible','on')
-    else
-        set(handles.(options{ilist}),'Visible','off')
-    end
-end
-
-%------------------------------------------------------------------------
-% --- Executes on button press in RUN: processing on local computer
-function RUN_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-set(handles.RUN, 'Enable','Off')
-set(handles.RUN,'BackgroundColor',[0.831 0.816 0.784])
-set(handles.RUN,'UserData',now)% record the time of launch
-errormsg=launch_jobs(hObject, eventdata, handles);
-set(handles.RUN, 'Enable','On')
-set(handles.RUN,'BackgroundColor',[1 0 0])
-
-% display errors or start status callback to visualise results
-if ~isempty(errormsg)
-    display(errormsg)
-    msgbox_uvmat('ERROR',errormsg)
-elseif  isfield(handles,'status') %&& ~isequal(get(handles.ListPairMode,'Value'),3)
-    set(handles.status,'Value',1);%suppress status display
-    status_Callback(hObject, eventdata, handles)
-end
-
-%-------------------------------------------------------------------
-% --- Executes on button press in status.
-function status_Callback(hObject, eventdata, handles)
-%-------------------------------------------------------------------
-val=get(handles.status,'Value');
-if val==0
-    set(handles.status,'BackgroundColor',[0 1 0])
-    hfig=findobj(allchild(0),'name','civ_status');
-    if ~isempty(hfig)
-        delete(hfig)
-    end
-    return
-end
-set(handles.status,'BackgroundColor',[1 1 0])
-drawnow
-listtype={'civ1','fix1','patch1','civ2','fix2','patch2'};
-Param.CheckCiv1=get(handles.CheckCiv1,'Value');
-Param.CheckFix1=get(handles.CheckFix1,'Value');
-Param.CheckPatch1=get(handles.CheckPatch1,'Value');
-Param.CheckCiv2=get(handles.CheckCiv2,'Value');
-Param.CheckFix2=get(handles.CheckFix2,'Value');
-Param.CheckPatch2=get(handles.CheckPatch2,'Value');
-box_test=[Param.CheckCiv1 Param.CheckFix1 Param.CheckPatch1 Param.CheckCiv2 Param.CheckFix2 Param.CheckPatch2];
-
-option_civ=find(box_test,1,'last');%last selected option (non-zero index of box_test)
-filecell=get(handles.civ,'UserData');%retrieve the list of output files expected for PIV
-test_new=0;
-if ~isfield(filecell,'nc')
-    test_new=1;
-    [ref_i,ref_j,errormsg]=find_ref_indices(handles);
-    if ~isempty(errormsg)
-        msgbox_uvmat('ERROR',errormsg)
-        return
-    end
-    filecell=set_civ_filenames(handles,ref_i,ref_j,box_test);%determine the output file expected from the GUI status
-end
-if ~isequal(box_test(4:6),[0 0 0])
-    civ_files=filecell.nc.civ2;%case of civ2 operations
-else
-    civ_files=filecell.nc.civ1;
-end
-[root,filename,ext]=fileparts(civ_files{1});
-[rootroot,SubDir,extdir]=fileparts(root);
-hfig=findobj(allchild(0),'name','civ_status');
-if isempty(hfig)
-    hfig=figure('DeleteFcn',@stop_status);
-    set(hfig,'MenuBar','none')% suppress the menu bar
-    set(hfig,'NumberTitle','off')%suppress the fig number in the title
-    set(hfig,'name','civ_status')
-    set(hfig,'tag','civ_status')
-    set(hfig,'UserData',civ_files)
-    hlist= uicontrol('Style','listbox','Units','normalized', 'Position',[0.05 0.09 0.9 0.71], 'Callback', {@open_uvmat},'tag','list');
-    uicontrol('Style','edit','Units','normalized', 'Position', [0.05 0.87 0.9 0.1],'tag','msgbox','Max',2,'String','checking files...');
-    uicontrol('Style','frame','Units','normalized', 'Position', [0.05 0.81 0.9 0.05]);
-    %uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.7 0.01 0.2 0.07],'String','Close','FontWeight','bold','FontUnits','normalized','FontSize',0.9,'Callback',@close_GUI);
-    uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.7 0.01 0.2 0.07],'String','Close','FontWeight','bold','FontUnits','normalized','FontSize',0.9,'Callback',@stop_status);
-    hrefresh=uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.1 0.01 0.2 0.07],'String','Refresh','FontWeight','bold','FontUnits','normalized','FontSize',0.9,'Callback',@refresh_GUI);
-    BarPosition=[0.05 0.81 0.01 0.05];
-    uicontrol('Style','frame','Units','normalized', 'Position',BarPosition ,'BackgroundColor',[1 0 0],'tag','waitbar');
-    drawnow 
-end
-StatusData.time_ref=get(handles.RUN,'UserData');% get the time of launch
-StatusData.option_civ=option_civ;
-set(hrefresh,'UserData',StatusData)
-filepath=fileparts(civ_files{1});
-set(hlist,'UserData',fileparts(filepath))
-refresh_GUI(hrefresh,[])
-
-%------------------------------------------------------------------------   
-% launched by refreshing the status figure
-function refresh_GUI(hObject, eventdata)
-%------------------------------------------------------------------------
-Tabchar={};
-BarPosition=[0.05 0.81 0.01 0.05];
-hfig=get(hObject,'parent');
-StatusData=get(hObject,'UserData');
-civ_files=get(hfig,'UserData');
-[filepath,filename,ext]=fileparts(civ_files{1});
-[tild,SubDir,extdir]=fileparts(filepath);
-SubDir=[SubDir extdir];
-option_civ=StatusData.option_civ;
-nbfiles=numel(civ_files);
-testrecent=0;
-count=0;
-datnum=zeros(1,nbfiles);
-filefound=cell(1,nbfiles);
-for ifile=1:nbfiles
-    detect=exist(civ_files{ifile},'file'); % check the existence of the file
-    option=0;
-    if detect==0
-        option_str='not created';
-    else
-        datfile=dir(civ_files{ifile});
-        if isfield(datfile,'datenum')
-            datnum(ifile)=datfile.datenum;%only available in recent matlab versions
-            testrecent=1;
-        end
-        filefound(ifile)={datfile.name};
-        
-        % check the content  netcdf file
-        Data=nc2struct(civ_files{ifile},'ListGlobalAttribute','CivStage','patch2','fix2','civ2','patch','fix');
-        option_list={'civ1','fix1','patch1','civ2','fix2','patch2'};
-        if ~isempty(Data.CivStage)
-            option=Data.CivStage;%case of Matlab civ
-        else
-            if ~isempty(Data.patch2) && isequal(Data.patch2,1)
-                option=6;
-            elseif ~isempty(Data.fix2) && isequal(Data.fix2,1)
-                option=5;
-            elseif ~isempty(Data.civ2) && isequal(Data.civ2,1);
-                option=4;
-            elseif ~isempty(Data.patch) && isequal(Data.patch,1);
-                option=3;
-            elseif ~isempty(Data.fix) && isequal(Data.fix,1);
-                option=2;
-            else
-                option=1;
-            end
-        end
-        option_str=option_list{option};
-        if datnum(ifile)<StatusData.time_ref
-            option_str=[option_str '  --OLD--'];
-        end
-    end
-    if option >= option_civ
-        count=count+1;
-    end
-    [filepath,filename,ext]=fileparts(civ_files{ifile});
-    Tabchar{ifile,1}=[fullfile(SubDir,filename) ext  '...' option_str];
-end
-datnum=datnum(datnum~=0);%keep the non zero values corresponding to existing files
-if isempty(datnum)
-    if testrecent
-        message='no civ result created yet';
-    else
-        message='';
-    end
-else
-    datnum=datnum(datnum~=0);%keep the non zero values corresponding to existing files
-    [first,ind]=min(datnum);
-    [last,indlast]=max(datnum);
-    message={[num2str(count) ' file(s) done over ' num2str(nbfiles)] ;['oldest modification:  ' cell2mat(filefound(ind)) ' : ' datestr(first)];...
-        ['latest modification:  ' cell2mat(filefound(indlast)) ' : ' datestr(last)]};
-end
-hlist=findobj(hfig,'tag','list');
-hmsgbox=findobj(hfig,'tag','msgbox');
-hwaitbar=findobj(hfig,'tag','waitbar');
-set(hlist,'String',Tabchar)
-set(hmsgbox,'String', message)
-if count>0 %&& ~test_new
-    BarPosition(3)=0.9*count/nbfiles;
-    set(hwaitbar,'Position',BarPosition)
-end
-
-
-%------------------------------------------------------------------------   
-% launched by deleting the status figure
-function stop_status(hObject, eventdata)
-%------------------------------------------------------------------------
-hciv=findobj(allchild(0),'tag','civ');
-hhciv=guidata(hciv);
-set(hhciv.status,'value',0) %reset the status uicontrol in the GUI civ
-set(hhciv.status,'BackgroundColor',[0 1 0])
-
-% %------------------------------------------------------------------------   
-% % launched by pressing OK on the status figure
-% function close_GUI(hObject, eventdata)
-% %------------------------------------------------------------------------
-%     delete(gcbf)
-
-
-%------------------------------------------------------------------------
-% --- Main lauch command, called by RUN and BATCH
-function errormsg=launch_jobs(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-errormsg='';%default
-
-%% read the input parameters from the  GUI civ
-Param=read_GUI(handles.civ);
-
-%% check the selected list of operations:
-operations={'Civ1','Fix1','Patch1','Civ2','Fix2','Patch2'};
-box_test=[Param.CheckCiv1 Param.CheckFix1 Param.CheckPatch1 Param.CheckCiv2 Param.CheckFix2 Param.CheckPatch2];
-index_first=find(box_test==1,1);
-if isempty(index_first)
-    errormsg='no selected operation';
-    return
-end
-index_last=find(box_test==1,1,'last');
-box_used=box_test(index_first : index_last);
-[box_missing,ind_missing]=min(box_used);
-if isequal(box_missing,0); %there is a missing step in the sequence of operations
-    errormsg=['missing' cell2mat(operations(ind_missing))];
-    return
-end
-
-%% check mask if selecetd 
-%could be included in get_mask callback ?
-if isequal(get(handles.CheckMask,'Value'),1)
-    maskname=get(handles.Mask,'String');
-    if ~exist(maskname,'file')
-        get_mask_civ1_Callback(hObject, eventdata, handles);
-    end
-end
-if isequal(get(handles.CheckMask,'Value'),1)
-    maskname=get(handles.Mask,'String');
-    if ~exist(maskname,'file')
-        get_mask_fix1_Callback(hObject, eventdata, handles);
-    end
-end
-if isequal(get(handles.CheckMask,'Value'),1)
-    maskname=get(handles.Mask,'String');
-    if ~exist(maskname,'file')
-        get_mask_civ2_Callback(hObject, eventdata, handles);
-    end
-end
-if isequal(get(handles.CheckMask,'Value'),1)
-    maskname=get(handles.Mask,'String');
-    if ~exist(maskname,'file')
-        get_mask_fix2_Callback(hObject, eventdata, handles);
-    end
-end
-
-%% reinitialise status callback 
-if isfield(handles,'status')
-    set(handles.status,'Value',0);%suppress status display
-    status_Callback([], [], handles)
-end
-
-%% read the PARAM.xml file to get the binaries (and batch_mode if batch)
-path_civ=fileparts(which('civ')); %path to the source directory of uvmat
-xmlfile=fullfile(path_civ,'PARAM.xml');
-s=[];
-if exist(xmlfile,'file')% search parameter xml file in the whole matlab path
-    t=xmltree(xmlfile);
-    s=convert(t);
-end% default configuration
-if ~isfield(s,'RunParam')
-    Param.xml.Civ1Bin=fullfile('bin','civ1');
-    Param.xml.Civ2Bin=fullfile('bin','civ2');
-    Param.xml.FixBin=fullfile('bin','fix_flag');
-    Param.xml.PatchBin=fullfile('bin','patch_up');
-end
-if strcmp(Param.RunMode,'cluster') %computation dispatched on a cluster
-    if isfield(s,'BatchParam')
-        Param.xml=s.BatchParam;
-        if isfield(Param.xml,'BatchMode')
-            batch_mode=Param.xml.BatchMode;
-            if ~ismember(batch_mode,{'sge','oar'})
-                errormsg=['batch mode ' batch_mode ' not supported by UVMAT'];
-                return
-            end
-        end
-    else
-        %default configuration
-        Param.xml.Civ1Bin=fullfile('bin','civ1');
-        Param.xml.Civ2Bin=fullfile('bin','civ2');
-        Param.xml.FixBin=fullfile('bin','fix_flag');
-        Param.xml.PatchBin=fullfile('bin','patch_up');
-   %     Param.xml.CivmBin=fullfile('bin','civ_matlab');
-        Param.xml.BatchMode='oar';% TODO : allow choice for sge
-    end
-else % run
-    if isfield(s,'RunParam')
-        Param.xml=s.RunParam;
-    else %default configuration
-        Param.xml.Civ1Bin=fullfile('bin','civ1');
-        Param.xml.Civ2Bin=fullfile('bin','civ2');
-        Param.xml.FixBin=fullfile('bin','fix_flag');
-        Param.xml.PatchBin=fullfile('bin','patch_up');
-    end
-end
-%Param.xml.CivmBin=fullfile('bin','civ_matlab');
-
-%% check if the binaries exist : to move in civ_opening
-binary_list={};
-switch Param.Program
-    case 'CivX'
-        binary_list={'Civ1Bin','Civ2Bin','PatchBin','FixBin'};
-    case 'CivAll'% desactivated option
-        binary_list={'Civ'};
-%     case 'civ_matlab.sh'% compiled version of civ_matlab 
-%         binary_list={'CivmBin'};         
-end
-for bin_name=binary_list %loop on the list of binaries
-    if isfield(Param.xml,bin_name{1})% bin_name{1} =current name in the list
-        if ~isunix
-        Param.xml.(bin_name{1})=[regexprep(Param.xml.(bin_name{1}),'/','\') '.exe'];
-        end
-        if exist(Param.xml.(bin_name{1}),'file')
-            [path,name,ext]=fileparts(Param.xml.(bin_name{1}));
-            currentdir=pwd;
-            if isempty(path)
-                path=fileparts(which('civ.m'));
-            end
-            if exist(path,'dir')
-                cd(path);
-                binpath=pwd;%path of the binary
-                Param.xml.(bin_name{1})=fullfile(binpath,[name ext]);
-                cd(currentdir);
-            else
-                errormsg=['path ' path ' for binaries specified in PARAM.xml does not exist'];
-                return
-            end          
-        else  %look for the full path if the file name has been defined with a relative path in PARAM.xm
-            fullname=fullfile(path_civ,Param.xml.(bin_name{1}));
-            if exist(fullname,'file')
-                Param.xml.(bin_name{1})=fullname;
-            else
-                errormsg=['Binary ' Param.xml.(bin_name{1}) ' specified in PARAM.xml does not exist'];
-                return
-            end
-        end
-    end
-end
-if strcmp(Param.Program,'civ_matlab.sh')
-    if ~exist(fullfile(path_civ,'civ_matlab.sh'),'file')
-        errormsg=[{'no file civ_matlab.sh found'}; {'run compile_functions.m to create it by compiling civ_matlab.m'}];
-            return
-    end
-end
-
-%% set the list of files and check them
-display('checking the files...')
-[ref_i,ref_j,errormsg]=find_ref_indices(handles);
-if ~isempty(errormsg)
-    return
-end
-[filecell,i1_civ1,i2_civ1,j1_civ1,j2_civ1,i1_civ2,i2_civ2,j1_civ2,j2_civ2,nom_type_nc,tild,tild,compare,errormsg]=...
-    set_civ_filenames(handles,ref_i,ref_j,box_test);
-if ~isempty(errormsg)
-    return
-end
-set(handles.civ,'UserData',filecell);%store for futur use of status callback
-display('files OK, processing...')
-
-%% create subfolders for log, cmx, nml, xml, bat
-RootBat=fileparts(filecell.nc.civ1{1,1});
-switch(Param.Program)
-    case {'CivX','CivAll'}
-dir_list={'0_BAT','0_CMX','0_LOG'};
-    case {'civ_matlab','civ_matlab.sh'}
-        dir_list={'0_BAT','0_XML'};
-end
-for k=1:length(dir_list)
-    if ~exist(fullfile(RootBat,dir_list{k}),'dir')
-        mkdir(fullfile(RootBat,dir_list{k}));
-    end
-end
-
-%% get information on input images or movies
-nbfield=numel(i1_civ1);
-nbslice=numel(j1_civ1);
-% if strcmp(Param.Program,'civ_matlab')
-    if Param.CheckCiv1
-        [ImageInfoA_civ1,Param.Civ1.ImageA]=get_file_info(filecell.ima1.civ1{1});
-        Param.Civ1.FileTypeA=ImageInfoA_civ1.FileType;
-        [ImageInfoB_civ1,Param.Civ1.ImageB]=get_file_info(filecell.ima2.civ1{1});
-        Param.Civ1.FileTypeB=ImageInfoB_civ1.FileType;
-    end
-    if Param.CheckCiv2
-        [ImageInfoA_civ2,Param.Civ2.ImageA]=get_file_info(filecell.ima1.civ2{1});
-        Param.Civ2.FileTypeA=ImageInfoA_civ2.FileType;
-        [ImageInfoB_civ2,Param.Civ2.ImageB]=get_file_info(filecell.ima2.civ2{1});
-        Param.Civ2.FileTypeB=ImageInfoB_civ2.FileType;
-    end
-% end
-
-%% MAIN LOOP
-time=get(handles.ImaDoc,'UserData'); %get the set of times
-TimeUnit=get(handles.TimeUnit,'String');
-checkframe=strcmp(TimeUnit,'frame');
-batch_file_list=[];%should be renamed file_list, can be used for xml or bash files
-NomTypeIma=get(handles.NomType,'String');
-for ifile=1:nbfield
-    for j=1:nbslice
-            
-        % define output file name
-        if Param.CheckCiv2==1 || Param.CheckFix2==1 || Param.CheckPatch2==1
-            Param.OutputFile=filecell.nc.civ2{ifile,j};
-        else
-            Param.OutputFile=filecell.nc.civ1{ifile,j};
-        end
-        Param.OutputFile=regexprep(Param.OutputFile,'.nc','');
-
-        if Param.CheckCiv1
-            % read image-dependent parameters          
-            if ~checkframe% && size(time,1)>=i2_civ1(ifile) && size(time,2)>=j2_civ1(j)
-                Param.Civ1.Dt=(time(i2_civ1(ifile)+1,j2_civ1(j)+1)-time(i1_civ1(ifile)+1,j1_civ1(j)+1));
-            else
-                Param.Civ1.Dt=1;
-            end
-            Param.Civ1.Time=((time(i2_civ1(ifile)+1,j2_civ1(j)+1)+time(i1_civ1(ifile)+1,j1_civ1(j)+1))/2);
-            if strcmp(Param.Program,'CivX')
-                Param.Civ1.term_a=num2stra(j1_civ1(j),nom_type_nc);%UTILITE?
-                Param.Civ1.term_b=num2stra(j2_civ1(j),nom_type_nc);%
-            end
-            Param.Civ1.ImageA=filecell.ima1.civ1{ifile,j};
-            Param.Civ1.ImageB=filecell.ima2.civ1{ifile,j};
-            Param.Civ1.ImageBitDepth=ImageInfoA_civ1.BitDepth;
-            Param.Civ1.ImageWidth=ImageInfoA_civ1.Width;
-            Param.Civ1.ImageHeight=ImageInfoA_civ1.Height;
-            if strcmp(NomTypeIma,'*')
-                Param.Civ1.FrameIndexA=i1_civ1(ifile);
-                Param.Civ1.FrameIndexB=i2_civ1(ifile);
-            else% case of movies indexed with i, the frame index is then in j
-                Param.Civ1.FrameIndexA=j1_civ1(j);
-                Param.Civ1.FrameIndexB=j2_civ1(j);
-            end
-            % read mask )parameters
-            if Param.Civ1.CheckMask % the lines below should be changed with the new gui
-                if ~exist(Param.Civ1.Mask,'file')
-                    maskbase=[filecell.filebase '_' Param.Civ1.Mask]; %
-                    nbslice_mask=str2double(Param.Civ1.Mask(1:end-4)); %
-                    i1_mask=mod(i1_civ1(ifile)-1,nbslice_mask)+1;
-                    [RootPathMask,RootFileMask]=fileparts(maskbase);
-                    Param.Civ1.Mask=fullfile_uvmat(RootPathMask,[],RootFileMask,'.png','_1',i1_mask);
-                end
-            end
-            % read grid parameters
-            if Param.Civ1.CheckGrid
-                if numel(Param.Civ1.Grid)>=4 && isequal(Param.Civ1.Grid(end-3:end),'grid')
-                    nbslice_grid=str2double(Param.Civ1.Grid(1:end-4)); %
-                    if ~isnan(nbslice_grid)
-                        i1_grid=mod(i1_civ1(ifile)-1,nbslice_grid)+1;
-                        Param.Civ1.Grid=[filecell.filebase '_' fullfile_uvmat('','',Param.Civ1.Grid,'.grid','_1',i1_grid)];
-                        if ~exist(Param.Civ1.GridName,'file')
-                            errormsg='grid file absent for civ1';
-                            return
-                        end
-                    elseif ~exist(Param.Civ1.Grid,'file')
-                        errormsg='grid file absent for civ1';
-                        return
-                    end
-                end
-            end
-            
-        end
-        
-        if Param.CheckCiv2==1
-            Param.Civ2.ImageA=filecell.ima1.civ2{ifile,j};
-            Param.Civ2.ImageB=filecell.ima2.civ2{ifile,j};          
-            if ~checkframe %&& size(time,1)>=i2_civ2(ifile) && size(time,2)>=j2_civ2(j)
-                Param.Civ2.Dt=time(i2_civ2(ifile)+1,j2_civ2(j)+1)-time(i1_civ2(ifile)+1,j1_civ2(j)+1);
-            else
-                Param.Civ2.Dt=1;
-            end
-            Param.Civ2.Time=(time(i2_civ2(ifile)+1,j2_civ2(j)+1)+time(i1_civ2(ifile)+1,j1_civ2(j)+1))/2;
-            if strcmp(Param.Program,'CivX')
-                Param.Civ2.term_a=num2stra(j1_civ2(j),nom_type_nc);
-                Param.Civ2.term_b=num2stra(j2_civ2(j),nom_type_nc);
-            end
-            Param.Civ2.filename_nc1=filecell.nc.civ1{ifile,j};
-            Param.Civ2.filename_nc1(end-2:end)=[]; % remove '.nc'
-            
-            % mask
-            if Param.Civ2.CheckMask
-                if ~exist(Param.Civ2.Mask,'file')
-                    maskbase=[filecell.filebase '_' Param.Civ2.Mask]; %
-                    nbslice_mask=str2double(Param.Civ2.Mask(1:end-4)); %
-                    i1_mask=mod(i1_civ2(ifile)-1,nbslice_mask)+1;
-                    [RootPathMask,RootFileMask]=fileparts(maskbase);
-                    Param.Civ2.Mask=fullfile_uvmat(RootPathMask,[],RootFileMask,'.png','_1',i1_mask);
-                    %                     Param.Civ2.Mask=name_generator(maskbase,i1_mask,1,'.png','_i');
-                end
-            end
-            %grid
-            if Param.Civ2.CheckGrid
-                if numel(Param.Civ2.Grid)>=4 && isequal(Param.Civ2.Grid(end-3:end),'grid')
-                    nbslice_grid=str2double(Param.Civ2.Grid(1:end-4)); %
-                    if ~isnan(nbslice_grid)
-                        i1_grid=mod(i1_civ2(ifile)-1,nbslice_grid)+1;
-                        Param.Civ2.Grid=[filecell.filebase '_' fullfile_uvmat('','',gridname,'.grid','_1',i1_grid)];
-                        %                         Param.Civ2.Grid=[filecell.filebase '_' name_generator(gridname,i1_grid,1,'.grid','_i')];
-                    end
-                end
-            end
-
-            Param.Civ2.ImageBitDepth=ImageInfoA_civ2.BitDepth;
-            Param.Civ2.ImageWidth=ImageInfoA_civ2.Width;
-            Param.Civ2.ImageHeight=ImageInfoA_civ2.Height;
-            if strcmp(NomTypeIma,'*')
-                Param.Civ2.FrameIndexA=i1_civ2(ifile);
-                Param.Civ2.FrameIndexB=i2_civ2(ifile);
-            else% case of movies indexed with i, the frame index is then in j
-                Param.Civ2.FrameIndexA=j1_civ2(j);
-                Param.Civ2.FrameIndexB=j2_civ2(j);
-            end
-        end
-       
-        % write the command and eventually the cmx, xml or nml files
-        cmd=write_cmd(Param);
-        write_param(Param);
-              
-        % create the command file name .m, .bat or .sh depending on the run option
-        switch Param.Program
-            case {'civ_matlab'}
-                filename_bat=regexprep(Param.OutputFile,'(.+)([/\\])(.+$)','$1$20_BAT$2$3.m');           
-                [BatRoot,BatFile]=fileparts(filename_bat);
-                 BatFile=regexprep(BatFile,'-','__');%transform name to suppress'-' (not valid for .m files)
-                 filename_bat=[fullfile(BatRoot,BatFile) '.m'];
-            case {'CivX','CivAll','civ_matlab.sh'}
-                switch computer
-                    case {'PCWIN','PCWIN64'}
-                        filename_bat=regexprep(Param.OutputFile,'(.+)([/\\])(.+$)','$1$20_BAT$2$3.bat');
-                    case {'GLNX86','GLNXA64','MACI64'}
-                        filename_bat=regexprep(Param.OutputFile,'(.+)([/\\])(.+$)','$1$20_BAT$2$3.sh');
-                end
-        end
-        
-        % print the command in the bat file
-        [fid,message]=fopen(filename_bat,'w');
-        if isequal(fid,-1)
-            errormsg=['creation of .bat file: ' message];
-            return
-        end
-        fprintf(fid,cmd);
-        fclose(fid);
-        
-        % special case for civ_matlab on cluster
-        if strcmp(Param.Program,'civ_matlab') && strcmp(Param.RunMode,'cluster')
-            filename_bat2=regexprep(Param.OutputFile,'(.+)([/\\])(.+$)','$1$20_BAT$2$3.sh');
-            [fid,message]=fopen(filename_bat2,'w');
-            if isequal(fid,-1)
-                errormsg=['creation of .bat file: ' message];
-                return
-            end
-            fprintf(fid,['#!/bin/bash \n' ...
-                '/etc/sysprofile \n'...
-                'matlab -nodisplay -nosplash -nojvm -singleCompThread <<END_MATLAB \n'...
-                'addpath(''' path_civ ''');\n']);
-%             for p=1:length(batch_file_list)
-                fprintf(fid,['run ' filename_bat '\n']);
-%             end
-            fprintf(fid, 'exit\nEND_MATLAB\n');
-            fclose(fid);
-            filename_bat=filename_bat2;
-        end
-        
-        switch computer
-            case {'GLNX86','GLNXA64','MACI64'}
-                system(['chmod +x ' filename_bat]);
-        end
-        batch_file_list{length(batch_file_list)+1}=filename_bat; 
-    end
-end
-
-%% start calculation
-%computation on cluster
-%if batch ==3
-switch Param.RunMode,
-    case 'cluster'
-        switch batch_mode
-            case 'sge' %at the moment only psmn ENS Lyon uses it
-                for p=1:length(batch_file_list)
-                    %cmd=['!qsub -p ' pvalue ' -q civ.q -e ' flname '.errors -o ' flname '.log' ' ' batch_file_list{p}];
-                    cmd=['!qsub -q piv1,piv2,piv3 '...
-                        '-e ' regexprep(batch_file_list{p},'.bat','.errors') ' -o ' regexprep(batch_file_list{p},'.bat','.log ')...
-                        ' -v ' 'LD_LIBRARY_PATH=/home/sjoubaud/matlab_sylvain/civx/lib ' batch_file_list{p}];
-                    display(cmd);eval(cmd);
-                end
-            case 'oar_old' % to remove
-                for p=1:length(batch_file_list)
-                    oar_command=['!oarsub -n CIVX -q nicejob '...
-                        '-E ' regexprep(batch_file_list{p},'.bat','.errors') ' -O ' regexprep(batch_file_list{p},'.bat','.log ')...
-                        '-l "/core=1+{type = ''smalljob''}/licence=1,walltime=00:60:00"   ' batch_file_list{p}];
-                    display(oar_command);eval(oar_command);
-                end
-            case 'oar'
-                max_walltime=3600*12; % 12h max
-                oar_modes={'oar-parexec','oar-dispatch','mpilauncher'};
-                text={'Batch processing on servcalcul3 LEGI';...
-                    'Please choose one of the followint modes';...
-                    '* oar-parexec : default and best choice';...
-                    '* oar-dispatch : jobs in a container of several cores';...
-                    '* mpilauncher : one single parallel mpi job using several cores';...
-                    '**********************************'...
-                    };
-                [S,v]=listdlg('PromptString',text,'ListString',oar_modes,...
-                    'SelectionMode','single','ListSize',[400 100],'Name','LEGI job mode');
-                switch oar_modes{S}
-                    case 'oar-parexec' %oar-dispatch.pl
-                        if strcmp(Param.Program,'civ_matlab')
-                            ncores=1;
-                            msgbox_uvmat('WARNING','Number of cores =1: select the compiled version civ_matlab.sh for multi-core processing');
-                        else
-                        answer=inputdlg({'Number of cores (max 36)','extra oar options'},'oarsub parameter',1,{'12',''});
-                        ncores=str2double(answer{1});
-                        end
-
-                        extra_oar=answer{2};
-                        walltime_onejob=1800; % seconds
-                        filename_joblist=fullfile(RootBat,'job_list.txt');
-                        fid=fopen(filename_joblist,'w');
-                        for p=1:length(batch_file_list)
-                            fprintf(fid,[batch_file_list{p} '\n']);
-                        end
-                        fclose(fid);
-                        oar_command=['oarsub -n CIVX '...
-                            '-t idempotent --checkpoint ' num2str(walltime_onejob+60) ' '...
-                            '-l /core=' num2str(ncores) ','...
-                            'walltime=' datestr(min(1.05*walltime_onejob/86400*max(length(batch_file_list),ncores)/ncores,max_walltime/86400),13) ' '...
-                            '-E ' regexprep(filename_joblist,'\.txt\>','.stderr') ' '...
-                            '-O ' regexprep(filename_joblist,'\.txt\>','.stdout') ' '...
-                            extra_oar ' '...
-                            '"oar-parexec -s -f ' filename_joblist ' '...
-                            '-l ' filename_joblist '.log"\n'];
-                        filename_oarcommand=fullfile(RootBat,'oar_command');
-                        fid=fopen(filename_oarcommand,'w');
-                        fprintf(fid,oar_command);
-                        fclose(fid);
-                        fprintf(oar_command);% display in command line
-                        system(oar_command);
-%                         eval(['! . ' filename
-%                             _oarcommand])
-                    case 'oar-dispatch' %oar-dispatch.pl
-                        ncores=str2double(...
-                            inputdlg('Number of cores (max 36)','oarsub parameter',1,{'6'})...
-                            );
-                        walltime_onejob=1800; % seconds
-                        filename_joblist=fullfile(RootBat,'job_list.txt');
-                        fid=fopen(filename_joblist,'w');
-                        for p=1:length(batch_file_list)
-                            oar_command=['oarsub -n CIVX '...
-                                '-E ' regexprep(batch_file_list{p},'\.bat\>','.stderr') ' -O ' regexprep(batch_file_list{p},'\.bat\>','.stdout ')...
-                                '-l "/core=1,walltime=' datestr(walltime_onejob/86400,13) '"   ' batch_file_list{p}];
-                            fprintf(fid,[oar_command '\n']);
-                        end
-                        fclose(fid);
-                        oar_command=['oarsub -t container -n civx-container '...
-                            '-l /core=' num2str(ncores)...
-                            ',walltime=' datestr(1.05*walltime_onejob/86400*max(length(batch_file_list),ncores)/ncores,13) ' '...
-                            '-E ' regexprep(filename_joblist,'\.txt\>','.stderr') ' '...
-                            '-O ' regexprep(filename_joblist,'\.txt\>','.stdout') ' '...
-                            '"oar-dispatch -f ' filename_joblist '"'];
-                        filename_oarcommand=fullfile(RootBat,'oar_command');
-                        fid=fopen(filename_oarcommand,'w');
-                        fprintf(fid,[oar_command '\n']);
-                        fclose(fid);
-                        display(oar_command);
-                        eval(['! . ' filename_oarcommand])
-                    case 'mpilauncher'
-                        filename_joblist=fullfile(RootBat,'job_list.txt');
-                        fid=fopen(filename_joblist,'w');
-                        
-                        for p=1:length(batch_file_list)
-                            fprintf(fid,[batch_file_list{p} '\n']);
-                        end
-                        fclose(fid)
-                        text_oarscript=[...
-                            '#!/bin/bash \n'...
-                            '#OAR -n Mylauncher \n'...
-                            '#OAR -l node=4/core=5,walltime=0:15:00 \n'...
-                            '#OAR -E ' fullfile(RootBat,'stderrfile.log') ' \n'...
-                            '#OAR -O ' fullfile(RootBat,'stdoutfile.log') ' \n'...
-                            '# ========================================================= \n'...
-                            '# This simple program launch a multinode parallel OpenMPI mpilauncher \n'...
-                            '# application for coriolis PIV post-processing. \n'...
-                            '# OAR uses oarshmost wrapper to propagate the user environement. \n'...
-                            '# This wrapper assert that the user has the same environment on all the \n'...
-                            '# allocated nodes (basic behavior needed by most MPI applications).  \n'...
-                            '# \n'...
-                            '# REQUIREMENT: \n'...
-                            '# the oarshmost wrapper should be installed in $HOME/bin directory. \n'...
-                            '# If a different location is used, change the line following the comment "Bidouille" \n'...
-                            '# ========================================================= \n'...
-                            '#   USER should only modify these 2 lines  \n'...
-                            'WORKDIR=' pwd ' \n'...
-                            'COMMANDE="mpilauncher  -f ' filename_joblist '" \n'...
-                            '# ========================================================= \n'...
-                            '# DO NOT MODIFY the FOLOWING LINES. (or be carefull) \n'...
-                            'echo "job starting on: "`hostname` \n'...
-                            'MPINODES="-host `tr [\\\\\\n] [,] <$OAR_NODEFILE |sed -e "s/,$/ /"`" \n'...
-                            'NCPUS=`cat $OAR_NODEFILE |wc -l` \n'...
-                            '#========== Bidouille ============== \n'...
-                            'export OMPI_MCA_plm_rsh_agent=oar-envsh \n'...%                     'cd $WORKDIR \n'...
-                            'CMD="mpirun -np $NCPUS -wdir $WORKDIR $MPINODES $COMMANDE" \n'...
-                            'echo "I run: $CMD"  \n'...
-                            '$CMD \n'...
-                            'echo "job ending" \n'...
-                            ];
-                        %                 oarsub -S ./oar.sub
-                        filename_oarscript=fullfile(RootBat,'oar_command');
-                        fid=fopen(filename_oarscript,'w');
-                        fprintf(fid,[text_oarscript]);
-                        fclose(fid);
-                        eval(['!chmod +x  ' filename_oarscript]);
-                        eval(['!oarsub -S ' filename_oarscript]);
-                end
-        end
-    case {'background','local'}
-        switch Param.Program
-            case {'civ_matlab'}
-                switch Param.RunMode
-                    case 'background'
-                        switch computer
-                            case {'PCWIN','PCWIN64'}
-                                filename_superbat=fullfile(RootBat,'job_list.bat');
-                                fid=fopen(filename_superbat,'w');
-                                if fid==-1
-                                    msgbox_uvmat('ERROR',['cannot create the command file ' filename_superbat])
-                                    return
-                                end
-                                 fprintf(fid,['matlab -automation '...
-                                     '-r "addpath(''' regexprep(path_civ,'\\','\\\\') ''');']);
-                                for p=1:length(batch_file_list)
-                                    fprintf(fid,['run ' regexprep(batch_file_list{p},'\\','\\\\') ';']);
-                                end
-                                 fprintf(fid, 'exit"');
-                                fclose(fid);
-                                dos([filename_superbat ' &']);
-                            case {'GLNX86','GLNXA64','MACI64'} 
-                                filename_superbat=fullfile(RootBat,'job_list.sh');
-                                fid=fopen(filename_superbat,'w');
-                                if fid==-1
-                                    msgbox_uvmat('ERROR',['cannot create the command file ' filename_superbat])
-                                    return
-                                end
-                                fprintf(fid,['#!/bin/bash \n' ...
-                                    '/etc/sysprofile \n'...
-                                    'matlab -nodisplay -nosplash -nojvm -logfile  <<END_MATLAB \n'...
-                                    'addpath(''' path_civ ''');\n']);
-                                for p=1:length(batch_file_list)
-                                    fprintf(fid,['run ' batch_file_list{p} '\n']);
-                                end
-                                fprintf(fid, 'exit\nEND_MATLAB\n');
-                                fclose(fid);
-                                system(['chmod +x ' filename_superbat]);
-                                system([filename_superbat ' &']);
-                        end
-                    case 'local'
-                        for p=1:length(batch_file_list)
-                            fid=fopen(batch_file_list{p});
-                            eval(fscanf(fid,'%s'));
-                            fclose(fid);
-                        end
-                end
-            case {'CivX','CivAll','civ_matlab.sh'}
-                    switch computer
-                        case {'PCWIN','PCWIN64'}
-                            filename_superbat=fullfile(RootBat,'job_list.bat');
-                            fid=fopen(filename_superbat,'w');
-                            if fid==-1
-                                msgbox_uvmat('ERROR',['cannot create the command file ' filename_superbat])
-                                return
-                            end
-                            for p=1:length(batch_file_list)
-                                fprintf(fid,['@call "' regexprep(batch_file_list{p},'\\','\\\\') '"' '\n']);
-                            end
-                            fclose(fid);
-                            system(['chmod +x ' filename_superbat]);
-                        case {'GLNX86','GLNXA64','MACI64'}
-                            filename_superbat=fullfile(RootBat,'job_list.bat');
-                            fid=fopen(filename_superbat,'w');
-                            if fid==-1
-                                msgbox_uvmat('ERROR',['cannot create the command file ' filename_superbat])
-                                return
-                            end
-                            for p=1:length(batch_file_list)
-                                fprintf(fid,['sh ' batch_file_list{p} '\n']);
-                            end
-                            fclose(fid);
-                            system(['chmod +x ' filename_superbat]);
-                    end
-                switch Param.RunMode
-                    case 'background'
-                        system([filename_superbat ' &']);% execute main commmand see what it does in dos ?
-                    case 'local'
-                        system(filename_superbat);
-                end
-        end
-end
-
-
-%% save interface state
-if isfield(filecell,'nc')
-    if isfield(filecell.nc,'civ2')
-        fileresu=filecell.nc.civ2{1,1};
-    else
-        fileresu=filecell.nc.civ1{1,1};
-    end
-end
-[RootPath,SubDir,RootFile]=fileparts_uvmat(fileresu);
-namedoc=fullfile(RootPath,SubDir,RootFile);
-detect=1;
-while detect==1
-    namefigfull=[namedoc '.fig'];
-    hh=dir(namefigfull);
-    if ~isempty(hh)
-        detect=1;
-        namedoc=[namedoc '.0'];
-    else
-        detect=0;
-    end
-end
-Param=rmfield(Param,'status');
-Param=rmfield(Param,'xml');
-t=struct2xml(Param);
-t=set(t,1,'name','Civ');% set the head label
-save(t,[namedoc '.civ.xml']); %save GUI  parameters as xml file
-% saveas(gcbf,namefigfull);%save the interface with name namefigfull (A CHANGER EN FICHIER  .xml)
-
-%Save info in personal profile (initiate browser next time) TODO
-MenuFile={};
-dir_perso=prefdir;
-profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
-if exist(profil_perso,'file')
-    hh=load (profil_perso);
-      if isfield(hh,'MenuFile')
-          MenuFile=hh.MenuFile;
-      end
-      if isfield(filecell.nc,'civ2')
-          MenuFile=[filecell.nc.civ2{1,1}; MenuFile];
-      else
-           MenuFile=[filecell.nc.civ1{1,1}; MenuFile];
-      end
-      save (profil_perso,'MenuFile','-append'); %store the file names for future opening of uvmat
-else
-    MenuFile=filecell.ima1.civ1(1,1);
-    save (profil_perso,'MenuFile')
-end
-
-%------------------------------------------------------------------------
-% --- determine the list of reference indices of processing file
-function [ref_i,ref_j,errormsg]=find_ref_indices(handles)
-%------------------------------------------------------------------------
-errormsg=''; %default error message
-first_i=str2double(get(handles.first_i,'String'));%first index i
-last_i=str2double(get(handles.last_i,'String'));%last index i
-incr_i=str2double(get(handles.incr_i,'String'));% increment
-if isequal(get(handles.first_j,'Visible'),'on')
-    first_j=str2double(get(handles.first_j,'String'));%first index j
-    last_j=str2double(get(handles.last_j,'String'));%last index j
-    incr_j=str2double(get(handles.incr_j,'String'));% increment
-else
-    first_j=1;
-    last_j=1;
-    incr_j=1;
-end
-ref_i=first_i:incr_i:last_i;% list of i indices (reference values for each pair)
-ref_j=first_j:incr_j:last_j;% list of j indices (reference values for each pair)
-if isnan(first_i)||isnan(first_j)
-    errormsg='first field number not defined';
-elseif isnan(last_i)||isnan(last_j)
-    errormsg='last field number not defined';
-elseif isnan(incr_i)||isnan(incr_j)
-    errormsg='increment in field number not defined';
-elseif last_i < first_i || last_j < first_j 
-    errormsg='last field number must be larger than the first one';
-end
-
-%------------------------------------------------------------------------
-% --- determine the list of filenames and indices needed for launch_job
-%------------------------------------------------------------------------
-% OUTPUT:
-% filecell: structure of cell arrays {ref_i,ref_j} containing all the filenames involved in the civ process
-%    the indices ref_i and ref_j correspond to the list of reference indices
-%       .filebase=fullfile(RootPath,RootFile) used to construct mask names, grid names, CivDoc xml file
-%       .ima1.civ1,.ima1.civ2: first image for civ1 and civ2 respectively (possibly different)
-%       .ima2.civ1,.ima2.civ2: second image for civ1 and civ2 respectively (possibly different)
-%       .nc.civ1,.nc.civ2: netcdf files containing civ1 and civ2 data respectively (possibly different)
-% i1_civ1,i2_civ1,j1_civ1,j2_civ1,i1_civ2,i2_civ2,j1_civ2,j2_civ2: arrays of files indices, needed for timing records
-function [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,errormsg]=...
-    set_civ_filenames(handles,ref_i,ref_j,checkbox)
-%------------------------------------------------------------------------
-filecell=[];%default
-errormsg='';
-ListProgram=get(handles.Program,'String');
-CivMode=ListProgram{get(handles.Program,'Value')};%Program to use , CivX or Matlab
-
-%% get the root name and check dir
-RootPath=get(handles.RootPath,'String');
-SubDirImages=get(handles.SubDirImages,'String');
-RootFile=get(handles.RootFile,'String');
-filecell.filebase=fullfile(RootPath,SubDirImages,RootFile);
-if isempty(filecell.filebase)
-    errormsg='please open an image with the upper menu option Open/Browse...';
-    return
-end
-if ~exist(RootPath,'dir')
-    errormsg=['path to images ' RootPath ' not found'];
-    return
-end
-[tild,message]=fileattrib(RootPath);
-if ~isempty(message) && ~isequal(message.UserWrite,1)
-    errormsg=['No writing access to ' RootPath];
-    return
-end
-%check result directory
-subdir_civ1=regexprep(get(handles.SubdirCiv1,'String'),'^.','');%subdirectory subdir_civ1 for the netcdf output data
-subdir_civ2=regexprep(get(handles.SubdirCiv2,'String'),'^.','');
-if isequal(subdir_civ1,''),subdir_civ1='civ'; end% put default subdir
-% subdir_civ1=[ '.' subdir_civ1];
-% subdir_civ2=[ '.' subdir_civ2];
-if isequal(subdir_civ2,''),subdir_civ2=subdir_civ1; end% put default subdir
-subdir_civ1=[SubDirImages '.' subdir_civ1];
-subdir_civ2=[SubDirImages '.' subdir_civ2];
-
-%% choose root names depending on ListCompareMode =displacement, shift, PIV or stereo PIV
-ListCompareMode=get(handles.ListCompareMode,'String');
-compare=ListCompareMode{get(handles.ListCompareMode,'Value')};
-
-% set the nomenclature type of the nc files depending on the pair mode
-if strcmp(compare,'displacement')||strcmp(compare,'shift')
-    mode='displacement';
-else
-    mode_list=get(handles.ListPairMode,'String');
-    mode_value=get(handles.ListPairMode,'Value');
-    mode=mode_list{mode_value};
-end
-NomType_ima2=get(handles.NomType,'String');
-NomType_nc=nomtype2pair(NomType_ima2,mode);
-
-% set the rootfile and image indexing
-RootFile_ima2=get(handles.RootFile,'String');%root file for the second image series
-ext_ima=get(handles.ImaExt,'String'); % image extension (the same for all images)
-switch compare
-    case {'PIV','PIV volume'}
-       RootFile_ima1=RootFile_ima2;% root name of the two image series is the same
-       NomType_ima1=NomType_ima2;% the index of the first image follows the index of the second one
-       RootFile_nc=RootFile_ima2;
-    case 'displacement'
-       RootFile_ima1=get(handles.RootFile_1,'String');% root name of the first image series set by handles.RootFile_1
-       NomType_ima1='';% no indexing of the first image, a fixed reference for the whole series
-       RootFile_nc=RootFile_ima2;
-    case 'shift'
-       RootFile_ima1=get(handles.RootFile_1,'String');% root name of the first image series set by handles.RootFile_1
-       NomType_ima1=NomType_ima2;% the index of the first image follows the index of the second one
-       RootFile_nc=[RootFile_ima1 '-' RootFile_ima2];
-end
-
-%determine the list of file indices involved
-[i1_civ1,i2_civ1,j1_civ1,j2_civ1,i1_civ2,i2_civ2,j1_civ2,j2_civ2]=...
-    find_pair_indices(handles,ref_i,ref_j,mode);
-
-%determine the new filebase for 'displacement' ListPairMode (comparison of two series)
-%filebase_B=filebase;% root name of the second field series for stereo
-% filebase_A=filebase;%default
-% if strcmp(compare,'PIV') 
-%     filebase_AB=filebase;
-% else
-%     [Path2,Name2]=fileparts(filebase_B);
-%     Name1=RootFile_ima1;
-%     filebase_AB=fullfile(Path2,[Name2 '-' Name1]);   
-% end
-% [RootPath_AB,RootFile_AB]=fileparts(filebase_AB);
-% % [RootPath_ima1,RootFile_ima1]=fileparts(filebase_B);
-% [RootPath_ima2,RootFile_ima2]=fileparts(filebase_B);
-% [RootPath_nc,RootFile_nc]=fileparts(filebase_B);%default
-% if strcmp(compare,'displacement')
-% %     [RootPath_ima1,RootFile_ima1]=fileparts(filebase_B);
-% %     [RootPath_ima2,RootFile_ima2]=fileparts(filebase_B);
-%     [RootPath_nc,RootFile_nc]=fileparts(filebase_B);
-% elseif strcmp(compare,'shift')
-%     RootPath_nc=RootPath_AB;
-%     RootFile_nc=RootFile_AB;
-% end
-% else
-%     filebase_ima1=filebase_B;
-%     filebase_ima2=filebase_B;
-%     filebase_nc=filebase_B;
-% [RootPath_ima1,RootFile_ima1]=fileparts(filebase_ima1);
-% [RootPath_ima2,RootFile_ima2]=fileparts(filebase_ima2);
-% [RootPath_nc,RootFile_nc]=fileparts(filebase_nc);
-% [RootPath_A,RootFile_A]=fileparts(filebase_A);
-
-    
-%% determine reference files for fix:
-file_ref_fix1={};%default
-file_ref_fix2={};
-nbfield=length(i1_civ1);
-nbslice=length(j1_civ1);
-if checkbox(2)==1% fix1 performed
-    ref=get(handles.ref_fix1,'UserData');%read data on the ref file stored by get_ref_fix1_Callback
-    if ~isempty(ref)
-        first_i=str2double(get(handles.first_i,'String'));
-        last_i=str2double(get(handles.last_i,'String'));
-        incr_i=str2double(get(handles.incr_i,'String'));
-        first_j=str2double(get(handles.first_j,'String'));
-        last_j=str2double(get(handles.last_j,'String'));
-        incr_j=str2double(get(handles.incr_j,'String'));
-        num_i_ref=first_i:incr_i:last_i;
-        num_j_ref=first_j:incr_j:last_j;
-        if isequal(mode,'displacement')
-            num_i1=num_i_ref;
-            num_i2=num_i_ref;
-            num_j1=num_j_ref;
-            num_j2=num_j_ref;
-        elseif isequal(mode,'pair j1-j2')% isequal(mode,'st_pair j1-j2')
-            num_i1=num_i_ref;
-            num_i2=num_i1;
-            num_j1=ref.num_a*ones(size(num_i_ref));
-            num_j2=ref.num_b*ones(size(num_i_ref));
-        elseif isequal(mode,'series(Di)') % isequal(mode,'st_series(Di)')
-            delta1=floor((ref.num2-ref.num1)/2);
-            delta2=ceil((ref.num2-ref.num1)/2);
-            num_i1=num_i_ref-delta1*ones(size(num_i_ref));
-            num_i2=num_i_ref+delta2*ones(size(num_i_ref));
-            if isempty(ref.num_a)
-                ref.num_a=1;
-            end
-            num_j1=ref.num_a*ones(size(num_i1));
-            num_j2=num_j1;
-        elseif isequal(mode,'series(Dj)')%| isequal(mode,'st_series(Dj)')
-            delta1=floor((ref.num_b-ref.num_a)/2);
-            delta2=ceil((ref.num_b-ref.num_a)/2);
-            num_i1=ref.num1*ones(size(num_i_ref));
-            num_i2=num_i1;
-            num_j1=num_j_ref-delta1*ones(size(num_j_ref));
-            num_j2=num_j_ref+delta2*ones(size(num_j_ref));
-        end
-        for ifile=1:nbfield
-            for j=1:nbslice
-                [RootPathRef,RootFile]=fileparts(ref.filebase);
-                file_ref=fullfile_uvmat(RootPathRef,ref.subdir,RootFile,'.nc',ref.NomType,num_i1(ifile),num_i2(ifile),num_j1(j),num_j2(j));
-                file_ref_fix1(ifile,j)={file_ref};
-                if ~exist(file_ref,'file')
-                    errormsg=['reference file ' file_ref ' not found for fix1'];
-                    return
-                end
-            end
-        end
-    end
-end
-
-%% determine reference files for fix2:
-if checkbox(5)==1% fix2 performed
-    ref=get(handles.ref_fix2,'UserData');
-    if ~isempty(ref)
-        first_i=str2double(get(handles.first_i,'String'));
-        last_i=str2double(get(handles.last_i,'String'));
-        incr_i=str2double(get(handles.incr_i,'String'));
-        first_j=str2double(get(handles.first_j,'String'));
-        last_j=str2double(get(handles.last_j,'String'));
-        incr_j=str2double(get(handles.incr_j,'String'));
-        num_i_ref=first_i:incr_i:last_i;
-        num_j_ref=first_j:incr_j:last_j;
-        if isequal(mode,'displacement')
-            num_i1=num_i_ref;
-            num_i2=num_i_ref;
-            num_j1=num_j_ref;
-            num_j2=num_j_ref;
-        elseif isequal(mode,'pair j1-j2')
-            num_i1=num_i_ref;
-            num_i2=num_i1;
-            num_j1=ref.num_a;
-            num_j2=ref.num_b;
-        elseif isequal(mode,'series(Di)')
-            delta1=floor((ref.num2-ref.num1)/2);
-            delta2=ceil((ref.num2-ref.num1)/2);
-            num_i1=num_i_ref-delta1*ones(size(num_i_ref));
-            num_i2=num_i_ref+delta2*ones(size(num_i_ref));
-            num_j1=ref.num_a*ones(size(num_i1));
-            num_j2=num_j1;
-        elseif isequal(mode,'series(Dj)')
-            delta1=floor((ref.num_b-ref.num_a)/2);
-            delta2=ceil((ref.num_b-ref.num_a)/2);
-            num_i1=ref.num1*ones(size(num_i_ref));
-            num_i2=num_i1;
-            num_j1=num_j_ref-delta1*ones(size(num_j_ref));
-            num_j2=num_j_ref+delta2*ones(size(num_j_ref));
-        end
-        for ifile=1:nbfield
-            for j=1:nbslice
-                [RootPathRef,RootFile]=fileparts(ref.filebase);
-                file_ref=fullfile_uvmat(RootPathRef,ref.subdir,RootFile,'.nc',ref.NomType,num_i1(ifile),num_i2(ifile),num_j1(j),num_j2(j));
-                file_ref_fix2(ifile,j)={file_ref};
-                if ~exist(file_ref,'file')
-                    errormsg=['reference file ' file_ref ' not found for fix2'];
-                    return
-                end
-            end
-        end
-    end
-end
-
-%% check the existence of the netcdf and image files involved
-% %%%%%%%%%%%%  case CheckCiv1 activated   %%%%%%%%%%%%%
-if checkbox(1)==1;
-    detect=1;
-    vers=0;
-    subdir_civ1_new=subdir_civ1;
-    answer='No';
-    while detect==1 %create a new subdir if the netcdf files already exist
-        for ifile=1:nbfield
-            for j=1:nbslice
-                filename=fullfile_uvmat(RootPath,subdir_civ1_new,RootFile_nc,'.nc',NomType_nc,i1_civ1(ifile),i2_civ1(ifile),j1_civ1(j),j2_civ1(j));
-                detect=exist(filename,'file')==2;
-                if detect% if a netcdf file already exists
-                    if strcmp(answer,'No')
-                        answer=msgbox_uvmat('INPUT_Y-N',['overwrite existing civ files in ' subdir_civ1_new]);
-                    end
-                    if strcmp(answer,'Yes')
-                        detect=0;
-                        filecell.nc.civ1(ifile,j)={filename};
-                    else
-                        r=regexp(subdir_civ1_new,'(?<root>.*\D)(?<num1>\d+)$','names');%detect whether name ends by a number
-                        if isempty(r)
-                            r(1).root=[subdir_civ1_new '_'];
-                            r(1).num1='0';
-                        end
-                        subdir_civ1_new=[r(1).root num2str(str2num(r(1).num1)+1)];%increment the index by 1 or put 1
-                        subdir_civ2=subdir_civ1_new;
-                    end
-                    break
-                end
-                filecell.nc.civ1(ifile,j)={filename};
-            end
-            if detect% if a netcdf file already exists
-                break
-            end
-        end
-  
-        %create the new SubdirCiv1
-        if ~exist(fullfile(RootPath,subdir_civ1_new),'dir')     
-            [xx,msg1]=mkdir(fullfile(RootPath,subdir_civ1_new));
-            if ~strcmp(msg1,'')
-                errormsg=['cannot create ' subdir_civ1_new ': ' msg1];%error message for directory creation
-                return
-            elseif isunix          
-                [xx,msg2] = fileattrib(fullfile(RootPath,subdir_civ1_new),'+w','g'); %yield writing access (+w) to user group (g)
-                if ~strcmp(msg2,'')
-                    errormsg=['pb of permission for  ' fullfile(RootPath,subdir_civ1_new) ': ' msg2];%error message for directory creation
-                    return
-                end
-            end
-        end
-        if strcmp(compare,'stereo PIV')&&(strcmp(mode,'pair j1-j2')||strcmp(mode,'series(Dj)')||strcmp(mode,'series(Di)'))%check second nc series
-            for ifile=1:nbfield
-                for j=1:nbslice
-                     filename=fullfile_uvmat(RootPath,subdir_civ1_new,RootFile_A,'.nc',NomType_nc,i1_civ1(ifile),i2_civ1(ifile),j1_civ1(j),j2_civ1(j));
-                    detect=exist(filename,'file')==2;
-                    if detect% if a netcdf file already exists
-                       indstr=regexp(subdir_civ1_new,'\D');
-                       if indstr(end)<length(subdir_civ1_new) %subdir_civ1 ends by a number
-                           vers=str2double(subdir_civ1_new(indstr(end)+1:end))+1;
-                           subdir_civ1_new=[subdir_civ1_new(1:indstr(end)) num2str(vers)];
-                       else
-                           vers=vers+1;
-                           subdir_civ1_new=[subdir_civ1_new '_' num2str(vers)];
-                       end
-                       subdir_civ2=subdir_civ1;
-                       break
-                    end
-                    filecell.ncA.civ1(ifile,j)={filename};
-                end
-                if detect% if a netcdf file already exists
-                    break
-                end
-            end
-            %create the new SubdirCiv1
-            if ~exist(fullfile(RootPath,subdir_civ1_new),'dir')        
-                [xx,msg1]=mkdir(fullfile(RootPath,subdir_civ1_new));
-                if ~strcmp(msg1,'')
-                    errormsg=['cannot create ' subdir_civ1_new ': ' msg1];
-                    return
-                elseif isunix
-                    [xx,msg2] = fileattrib(fullfile(RootPath,subdir_civ1_new),'+w','g'); %yield writing access (+w) to user group (g)
-                    if ~strcmp(msg2,'')
-                        errormsg=['pb of permission for ' subdir_civ1_new ': ' msg2];%error message for directory creation
-                        return
-                    end
-                end
-            end
-        end
-    end
-    subdir_civ1=subdir_civ1_new;
-    % get image names
-    for ifile=1:nbfield
-        for j=1:nbslice
-             filename=fullfile_uvmat(RootPath,SubDirImages,RootFile_ima1,ext_ima,NomType_ima1,i1_civ1(ifile),[],j1_civ1(j));
-            idetect(j)=exist(filename,'file')==2;
-            filecell.ima1.civ1(ifile,j)={filename}; %first image
-            filename=fullfile_uvmat(RootPath,SubDirImages,RootFile_ima2,ext_ima,NomType_ima2,i2_civ1(ifile),[],j2_civ1(j));
-            idetect_1(j)=exist(filename,'file')==2;
-            filecell.ima2.civ1(ifile,j)={filename};%second image
-        end
-        [idetectmin,indexj]=min(idetect);
-        if idetectmin==0,
-            errormsg=[filecell.ima1.civ1{ifile,indexj} ' not found'];
-            return
-        end
-        [idetectmin,indexj]=min(idetect_1);
-        if idetectmin==0,
-            errormsg=[filecell.ima2.civ1{ifile,indexj} ' not found'];
-            return
-        end
-    end
-    if strcmp(compare,'stereo PIV') && (strcmp(mode,'pair j1-j2') || strcmp(mode,'series(Dj)') || strcmp(mode,'series(Di)'))
-        for ifile=1:nbfield
-            for j=1:nbslice
-                filename=fullfile_uvmat(RootPath,'',RootFile_A,ext_ima,NomType_ima1,i1_civ1(ifile),[],j1_civ1(j));
-                idetect(j)=exist(filename,'file')==2;
-                filecell.imaA1.civ1(ifile,j)={filename} ;%first image
-                filename=fullfile_uvmat(RootPath,'',RootFile_A,ext_ima,NomType_ima2,i2_civ1(ifile),[],j2_civ1(j));
-                idetect_1(j)=exist(filename,'file')==2;
-                filecell.imaA2.civ1(ifile,j)={filename};%second image
-            end
-            [idetectmin,indexj]=min(idetect);
-            if idetectmin==0,
-                errormsg=[filecell.imaA1.civ1{ifile,indexj} ' not found'];
-                return
-            end
-            [idetectmin,indexj]=min(idetect_1);
-            if idetectmin==0,
-                errormsg=[filecell.imaA2.civ1{ifile,indexj} ' not found'];
-                return
-            end
-        end
-    end
-    
-    %%%%%%%%%%%%%  checkfix1 or checkpatch1 activated but no checkciv1   %%%%%%%%%%%%%
-elseif (checkbox(2)==1 || checkbox(3)==1);
-    for ifile=1:nbfield
-        for j=1:nbslice
-            filename=fullfile_uvmat(RootPath,subdir_civ1,RootFile_nc,'.nc',NomType_nc,i1_civ1(ifile),i2_civ1(ifile),j1_civ1(j),j2_civ1(j));
-            detect=exist(filename,'file')==2;
-            if detect==0
-                errormsg=[filename ' not found'];
-                return
-            end
-            filecell.nc.civ1(ifile,j)={filename};
-        end
-    end
-    if strcmp(compare,'stereo PIV')
-        for ifile=1:nbfield
-            for j=1:nbslice
-                filename=fullfile_uvmat(RootPath,subdir_civ1,RootFile_A,'.nc',NomType_nc,i1_civ1(ifile),i2_civ1(ifile),j1_civ1(j),j2_civ1(j));
-                filecell.ncA.civ1(ifile,j)={filename};
-                if ~exist(filename,'file')
-                    errormsg=['input file ' filename ' not found'];
-                    return
-                end
-            end
-        end
-    end
-end
-
-%%%%%%%%%%%%%  if checkciv2 performed with pairs different than checkciv1  %%%%%%%%%%%%%
-testdiff=0;
-if (checkbox(4)==1)&&...
-        ((get(handles.ListPairCiv1,'Value')~=get(handles.ListPairCiv2,'Value'))||~strcmp(subdir_civ2,subdir_civ1))
-    testdiff=1;
-    detect=1;
-    vers=0;
-    subdir_civ2_new=subdir_civ2;
-    while detect==1 %create a new subdir if the netcdf files already exist
-        for ifile=1:nbfield
-            for j=1:nbslice
-                filename=fullfile_uvmat(RootPath,subdir_civ2_new,RootFile_nc,'.nc',NomType_nc,i1_civ2(ifile),i2_civ2(ifile),j1_civ2(j),j2_civ2(j));
-                detect=exist(filename,'file')==2;
-                if detect% if a netcdf file already exists
-                    indstr=regexp(subdir_civ2,'\D');
-                    if indstr(end)<length(subdir_civ2) %subdir_civ1 ends by a number
-                        vers=str2double(subdir_civ2(indstr(end)+1:end))+1;
-                        subdir_civ2_new=[subdir_civ2(1:indstr(end)) num2str(vers)];
-                    else
-                        vers=vers+1;
-                        subdir_civ2_new=[subdir_civ1 '_' num2str(vers)];
-                    end
-                    break
-                end
-                filecell.nc.civ2(ifile,j)={filename};
-            end
-            if detect% if a netcdf file already exists
-                break
-            end
-        end
-        %create the new subdir_civ2_new
-        if ~exist(fullfile(RootPath,subdir_civ2_new),'dir')
-            [xx,m2]=mkdir(fullfile(RootPath,subdir_civ2_new));
-            if isunix
-            [xx,msg2] = fileattrib(fullfile(RootPath,subdir_civ2_new),'+w','g'); %yield writing access (+w) to user group (g)
-            if ~isequal(m2,'')
-                errormsg=['cannot create ' fullfile(RootPath,subdir_civ2_new) ': ' m2];
-                return
-            end
-            end
-        end
-        if strcmp(compare,'stereo PIV')%check second nc series
-            for ifile=1:nbfield
-                for j=1:nbslice
-                    filename=fullfile_uvmat(RootPath,subdir_civ2_new,RootFile_A,'.nc',NomType_nc,i1_civ2(ifile),i2_civ2(ifile),j1_civ2(j),j2_civ2(j));
-                    detect=exist(filename,'file')==2;
-                    if detect% if a netcdf file already exists
-                        indstr=regexp(subdir_civ2,'\D');
-                        if indstr(end)<length(subdir_civ2) %subdir_civ1 ends by a number
-                           vers=str2double(subdir_civ2(indstr(end)+1:end))+1;
-                           subdir_civ2_new=[subdir_civ2(1:indstr(end)) num2str(vers)];
-                        else
-                           vers=vers+1;
-                           subdir_civ2_new=[subdir_civ1 '_' num2str(vers)];
-                        end
-                        break
-                    end
-                    filecell.ncA.civ2(ifile,j)={filename};
-                end
-                if detect% if a netcdf file already exists
-                    break
-                end
-            end
-            subdir_civ2=subdir_civ2_new;
-            %create the new SubdirCiv1
-            if ~exist(fullfile(RootPath,subdir_civ2_new),'dir')
-                [xx,m2]=mkdir(subdir_civ2_new);
-                if isunix
-                 [xx,msg2] = fileattrib(fullfile(RootPath,subdir_civ2_new),'+w','g'); %yield writing access (+w) to user group (g)
-                if ~isequal(m2,'')
-                    errormsg= ['cannot create ' fullfile(RootPath,subdir_civ2_new) ': ' m2];%error message for directory creation
-                    return
-                end
-                end
-            end
-        end
-    end
-    subdir_civ2=subdir_civ2_new;
-end
-
-%%%%%%%%%%%%%  if checkciv2 results are obtained or used  %%%%%%%%%%%%%
-if checkbox(4)==1 || checkbox(5)==1 || checkbox(6)==1 %civ2
-    %check source netcdf file of checkciv1 estimates
-    if checkbox(1)==0; %no civ1 performed
-        for ifile=1:nbfield
-            for j=1:nbslice
-                filename=fullfile_uvmat(RootPath,subdir_civ1,RootFile_nc,'.nc',NomType_nc,i1_civ1(ifile),i2_civ1(ifile),j1_civ1(j),j2_civ1(j));%
-                filecell.nc.civ1(ifile,j)={filename};% name of the civ1 file
-                if ~exist(filename,'file')
-                    errormsg=['input file ' filename ' not found'];
-                    return
-                end
-                if ~testdiff % civ2 or patch2 are written in the same file as civ1
-                    if checkbox(4)==0 ; %check the existence of civ2 if it is not calculated
-                        Data=nc2struct(filename,'ListGlobalAttribute','CivStage','civ2');
-                        if isfield(Data,'Txt')
-                            errormsg=Data.Txt; 
-                            return
-                        elseif ~isempty(Data.CivStage)% case of new civ files
-                            if Data.CivStage<4 %test for civ files
-                            errormsg=['no civ2 data in ' filename];
-                            return
-                            end
-                        elseif isempty(Data.civ2)||isequal(Data.civ2,0)
-                            errormsg=['no civ2 data in ' filename];
-                            return
-                        end
-                    elseif checkbox(3)==0; %check the existence of patch if it is not calculated
-                        Data=nc2struct(filename,'ListGlobalAttribute','CivStage','patch');
-                        if isfield(Data,'Txt')
-                            errormsg=Data.Txt;
-                            return
-                        elseif ~isempty(Data.CivStage)
-                            if Data.CivStage<3 %test for civ files
-                                errormsg=['no patch data in ' filename];
-                                return
-                            end
-                        elseif isempty(Data.patch)||isequal(Data.patch,0)
-                            errormsg=['no patch data in ' filename];
-                            return
-                        end
-                    end
-                end
-            end
-        end
-        if strcmp(compare,'stereo PIV')
-            for ifile=1:nbfield
-                for j=1:nbslice
-                    filename=fullfile_uvmat(RootPath,subdir_civ2,RootFile_A,'.nc',NomType_nc,i1_civ2(ifile),i2_civ2(ifile),j1_civ2(j),j2_civ2(j));
-                    filecell.ncA.civ2(ifile,j)={filename};
-                    if ~exist(filename,'file')
-                        errormsg=['input file ' filename ' not found'];
-                        return
-                    end
-                end
-            end
-        end
-    end
-    
-    detect=1;
-    %     while detect==1%creates a new subdir if the netcdf files already contain checkciv2 data
-    for ifile=1:nbfield
-        for j=1:nbslice
-            filename=fullfile_uvmat(RootPath,subdir_civ2,RootFile_nc,'.nc',NomType_nc,i1_civ2(ifile),i2_civ2(ifile),j1_civ2(j),j2_civ2(j));
-            detect=exist(filename,'file')==2;
-            filecell.nc.civ2(ifile,j)={filename};
-        end
-    end
-    %get first image names for checkciv2
-    if checkbox(1)==1 && isequal(i1_civ1,i1_civ2) && isequal(j1_civ1,j1_civ2)
-        filecell.ima1.civ2=filecell.ima1.civ1;
-    elseif checkbox(4)==1
-        for ifile=1:nbfield
-            for j=1:nbslice
-                filename=fullfile_uvmat(RootPath,SubDirImages,RootFile_ima1,ext_ima,NomType_ima1,i1_civ2(ifile),[],j1_civ2(j));
-                idetect_2(j)=exist(filename,'file')==2;
-                filecell.ima1.civ2(ifile,j)={filename};%first image
-            end
-            [idetectmin,indexj]=min(idetect_2);
-            if idetectmin==0,
-               errormsg=['input image ' filecell.ima1.civ2{ifile,indexj} ' not found'];
-                return
-            end
-        end
-    end
-    
-    %get second image names for checkciv2
-    if checkbox(1)==1 && isequal(i2_civ1,i2_civ2) && isequal(j2_civ1,j2_civ2)
-        filecell.ima2.civ2=filecell.ima2.civ1;
-    elseif checkbox(4)==1
-        for ifile=1:nbfield
-            for j=1:nbslice
-                filename=fullfile_uvmat(RootPath,SubDirImages,RootFile_ima2,ext_ima,NomType_ima2,i2_civ2(ifile),[],j2_civ2(j));
-                idetect_3(j)=exist(filename,'file')==2;
-                filecell.ima2.civ2(ifile,j)={filename};%first image
-            end
-            [idetectmin,indexj]=min(idetect_3);
-            if idetectmin==0,
-                errormsg=['input image ' filecell.ima2.civ2{ifile,indexj} ' not found'];
-                return
-            end
-        end
-    end
-end
-if (checkbox(5) || checkbox(6)) && ~checkbox(4)  % need to read an existing netcdf civ2 file
-    if ~testdiff
-        filecell.nc.civ2=filecell.nc.civ1;% file already checked
-    else     % check the civ2 files
-        for ifile=1:nbfield
-            for j=1:nbslice
-                 filename=fullfile_uvmat(RootPath,subdir_civ2,RootFile_nc,'.nc',NomType_nc,i1_civ2(ifile),i2_civ2(ifile),j1_civ2(j),j2_civ2(j));
-                filecell.nc.civ2(ifile,j)={filename};
-                if ~exist(filename,'file')
-                    errormsg=['input file ' filename ' not found'];
-                    return
-                else
-                    Data=nc2struct(filename,'ListGlobalAttribute','CivStage','civ2');
-                    if ~isempty(Data.CivStage) && Data.CivStage<4 %test for civ files
-                            errormsg=['no civ2 data in ' filename];
-                            return
-                    elseif isempty(Data.civ2)||isequal(Data.civ2,0)
-                        errormsg=['no civ2 data in ' filename];
-                        return
-                    end
-                end
-            end
-        end
-    end
-end
-
-%%%%%%%%%%%%%  if stereo fields are calculated by PATCH %%%%%%%%%%%%%
-if strcmp(compare,'stereo PIV')
-    if  checkbox(3) && isequal(get(handles.test_stereo1,'Value'),1)
-        for ifile=1:nbfield
-            for j=1:nbslice
-                 filename=fullfile_uvmat(RootPath,subdir_civ1,RootFile_AB,'.nc',NomType_nc,i1_civ1(ifile),i2_civ1(ifile),j1_civ1(j),j2_civ1(j));
-                filecell.st(ifile,j)={filename};
-            end
-        end
-    end
-    if  checkbox(6) && isequal(get(handles.CheckStereo,'Value'),1)
-        for ifile=1:nbfield
-            for j=1:nbslice
-                 filename=fullfile_uvmat(RootPath,subdir_civ2,RootFile_AB,'.nc',NomType_nc,i1_civ2(ifile),i2_civ2(ifile),j1_civ2(j),j2_civ2(j));
-                filecell.st(ifile,j)={filename};
-            end
-        end
-    end
-end
-set(handles.SubdirCiv1,'String',regexprep(subdir_civ1,['^' SubDirImages],''));%suppress the root  SuddirImages;);%update the edit box
-set(handles.SubdirCiv2,'String',regexprep(subdir_civ2,['^' SubDirImages],''));%update the edit box
-
-% For CivX COPY IMAGES TO THE FORMAT .png IF NEEDED 
-if strcmp(CivMode,'CivX')
-    NomType_imanew1=NomType_ima1;
-    NomType_imanew2=NomType_ima2;
-    if ~isequal(ext_ima,'.png')
-        if checkbox(1) %if civ1 is performed
-             [FileInfo,MovieObject]=get_file_info(filecell.ima1.civ1{1});
-             FileType=FileInfo.FileType;
-            check_j=0;
-            if strcmp(FileType,'mmreader')||strcmp(FileType,'VideoReader')||strcmp(FileType,'multimage')
-                if max(j1_civ1)>1
-                    check_j=1;
-                    NomType_imanew1='_1_1';
-                else
-                    NomType_imanew1='_1';
-                end
-            end
-            h = waitbar(0,'copy images to the .png format for civ1');% display a wait bar
-            for ifile=1:nbfield
-                waitbar(ifile/nbfield);
-                for j=1:nbslice
-                    filename=fullfile_uvmat(RootPath,SubDirImages,RootFile_ima1,'.png',NomType_imanew1,i1_civ1(ifile),[],j1_civ1(j));
-                    if ~exist(filename,'file')
-                        if check_j
-                        A=read_image(filecell.ima1.civ1{ifile,j},FileType,MovieObject,j1_civ1(j));
-                        else
-                            A=read_image(filecell.ima1.civ1{ifile,j},FileType,MovieObject,i1_civ1(ifile));
-                        end
-                        imwrite(uint16(sum(A,3)),filename,'BitDepth',16);
-                    end
-                    filecell.ima1.civ1(ifile,j)={filename};
-                    filename=fullfile_uvmat(RootPath,SubDirImages,RootFile_ima2,'.png',NomType_imanew1,i2_civ1(ifile),[],j2_civ1(j));
-                    if ~exist(filename,'file')
-                         if check_j
-                            A=read_image(filecell.ima1.civ1{ifile,j},FileType,MovieObject,j2_civ1(j));
-                        else
-                            A=read_image(filecell.ima1.civ1{ifile,j},FileType,MovieObject,i2_civ1(ifile));
-                         end
-                        imwrite(uint16(sum(A,3)),filename,'BitDepth',16);
-                    end
-                    filecell.ima2.civ1(ifile,j)={filename};
-                end
-            end
-            close(h)
-        end
-        if checkbox(4) %if civ2 is performed
-             [FileInfo,MovieObject]=get_file_info(filecell.ima1.civ2{1});
-             FileType=FileInfo.FileType;
-            check_j=0;
-            if strcmp(FileType,'mmreader')||strcmp(FileType,'VideoReader')||strcmp(FileType,'multimage')
-                if max(j1_civ2)>1
-                    check_j=1;
-                    NomType_imanew1='_1_1';
-                else
-                    NomType_imanew1='_1';
-                end
-            end
-            h = waitbar(0,'copy images to the .png format for civ2');% display a wait bar
-            for ifile=1:nbfield
-                waitbar(ifile/nbfield);
-                for j=1:nbslice
-                    filename=fullfile_uvmat(RootPath,SubDirImages,RootFile_ima1,'.png',NomType_imanew1,i1_civ2(ifile),[],j1_civ2(j));
-                    if ~exist(filename,'file')
-                        if check_j
-                        A=read_image(filecell.ima1.civ1{ifile,j},FileType,MovieObject,j1_civ2(j));
-                        else
-                            A=read_image(filecell.ima1.civ1{ifile,j},FileType,MovieObject,i1_civ2(ifile));
-                        end
-                        imwrite(uint16(sum(A,3)),filename,'BitDepth',16);
-                    end
-                    filecell.ima1.civ2(ifile,j)={filename};
-                    filename=fullfile_uvmat(RootPath,SubDirImages,RootFile_ima2,'.png',NomType_imanew2,i2_civ2(ifile),[],j2_civ2(j));
-                    if ~exist(filename,'file')
-                        if check_j
-                        A=read_image(filecell.ima1.civ1{ifile,j},FileType,MovieObject,j1_civ2(j));
-                        else
-                            A=read_image(filecell.ima1.civ1{ifile,j},FileType,MovieObject,i1_civ2(ifile));
-                        end
-                        imwrite(uint16(sum(A,3)),filename,'BitDepth',16);
-                    end
-                    filecell.ima2.civ2(ifile,j)={filename};
-                end
-            end
-            close(h);
-        end
-    end
-end
-
-%------------------------------------------------------------------------
-% --- determine the list of index pairs of processing file
-function [i1_civ1,i2_civ1,j1_civ1,j2_civ1,i1_civ2,i2_civ2,j1_civ2,j2_civ2]=...
-    find_pair_indices(handles,ref_i,ref_j,mode)
-%------------------------------------------------------------------------
-
-list_civ1=get(handles.ListPairCiv1,'String');
-index_civ1=get(handles.ListPairCiv1,'Value');
-str_civ1=list_civ1{index_civ1};%string defining the image pairs for civ1
-if isempty(str_civ1)||isequal(str_civ1,'')
-    msgbox_uvmat('ERROR','no image pair selected for civ1')
-    return
-end
-list_civ2=get(handles.ListPairCiv2,'String');
-index_civ2=get(handles.ListPairCiv2,'Value');
-if index_civ2>length(list_civ2)
-    list_civ2=list_civ1;
-    index_civ2=index_civ1;
-end
-str_civ2=list_civ2{index_civ2};%string defining the image pairs for civ2
-
-if isequal (mode,'series(Di)')
-    lastfield=str2double(get(handles.nb_field,'String'));
-    i1_civ1=ref_i-floor(index_civ1/2)*ones(size(ref_i));% set of first image numbers
-    i2_civ1=ref_i+ceil(index_civ1/2)*ones(size(ref_i));
-    j1_civ1=ref_j;
-    j2_civ1=ref_j;
-    i1_civ2=ref_i-floor(index_civ2/2)*ones(size(ref_i));
-    i2_civ2=ref_i+ceil(index_civ2/2)*ones(size(ref_i));
-    j1_civ2=ref_j;
-    j2_civ2=ref_j;   
-    
-    % adjust the first and last field number
-    lastfield=str2double(get(handles.nb_field,'String'));
-    if isnan(lastfield)
-        indsel=find((i1_civ1 >= 1)&(i1_civ2 >= 1));
-    else
-        indsel=find((i2_civ1 <= lastfield)&(i2_civ2 <= lastfield)&(i1_civ1 >= 1)&(i1_civ2 >= 1));
-    end
-    if length(indsel)>=1
-        firstind=indsel(1);
-        lastind=indsel(end);
-        set(handles.first_i,'String',num2str(ref_i(firstind)))%update the display of first and last fields
-        set(handles.last_i,'String',num2str(ref_i(lastind)))
-        ref_i=ref_i(indsel);
-        i1_civ1=i1_civ1(indsel);
-        i1_civ2=i1_civ2(indsel);
-        i2_civ1=i2_civ1(indsel);
-        i2_civ2=i2_civ2(indsel);
-    end
-elseif isequal (mode,'series(Dj)')
-    lastfield_j=str2double(get(handles.nb_field2,'String'));
-    i1_civ1=ref_i;% set of first image numbers
-    i2_civ1=ref_i;
-    j1_civ1=ref_j-floor(index_civ1/2)*ones(size(ref_j));
-    j2_civ1=ref_j+ceil(index_civ1/2)*ones(size(ref_j));
-    i1_civ2=ref_i;
-    i2_civ2=ref_i;
-    j1_civ2=ref_j-floor(index_civ2/2)*ones(size(ref_j));
-    j2_civ2=ref_j+ceil(index_civ2/2)*ones(size(ref_j));
-    % adjust the first and last field number
-    if isnan(lastfield_j)
-        indsel=find((j1_civ1 >= 1)&(j1_civ2 >= 1));
-    else
-        indsel=find((j2_civ1 <= lastfield_j)&(j2_civ2 <= lastfield_j)&(j1_civ1 >= 1)&(j1_civ2 >= 1));
-    end
-    if length(indsel)>=1
-        firstind=indsel(1);
-        lastind=indsel(end);
-        set(handles.first_j,'String',num2str(ref_j(firstind)))%update the display of first and last fields
-        set(handles.last_j,'String',num2str(ref_j(lastind)))
-        ref_j=ref_j(indsel);
-        j1_civ1=j1_civ1(indsel);
-        j2_civ1=j2_civ1(indsel);
-        j1_civ2=j1_civ2(indsel);
-        j2_civ2=j2_civ2(indsel);
-    end
-elseif isequal(mode,'pair j1-j2') %case of bursts (png_old or png_2D)
-    displ_num=get(handles.ListPairCiv1,'UserData');
-    i1_civ1=ref_i;
-    i2_civ1=ref_i;
-    j1_civ1=displ_num(1,index_civ1);
-    j2_civ1=displ_num(2,index_civ1);
-    i1_civ2=ref_i;
-    i2_civ2=ref_i;
-    j1_civ2=displ_num(1,index_civ2);
-    j2_civ2=displ_num(2,index_civ2);
-elseif isequal(mode,'displacement')
-    i1_civ1=ref_i;
-    i2_civ1=ref_i;
-    j1_civ1=ref_j;
-    j2_civ1=ref_j;
-    i1_civ2=ref_i;
-    i2_civ2=ref_i;
-    j1_civ2=ref_j;
-    j2_civ2=ref_j;
-end
-
-%------------------------------------------------------------------------
-% --- Executes on button press in ListCompareMode.
-function ListCompareMode_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-ListCompareMode=get(handles.ListCompareMode,'String');
-option=ListCompareMode{get(handles.ListCompareMode,'Value')};
-switch option
-    case 'PIV'
-        set(handles.RootFile_1,'Visible','Off');
-        set(handles.sub_txt,'Visible','off')
-        set(handles.RootFile_1,'String',[]);
-        mode_store=get(handles.ListCompareMode,'UserData');
-        set(handles.ListPairMode,'Visible','on')
-        set(handles.ListPairMode,'Value',1)
-        set(handles.ListPairMode,'String',mode_store)
-        set(handles.CheckStereo,'Value',0)      
-    case 'PIV volume'     
-        set(handles.RootFile_1,'Visible','Off');
-        set(handles.sub_txt,'Visible','off')
-        set(handles.RootFile_1,'String',[]);
-        mode_store=get(handles.ListCompareMode,'UserData');
-        set(handles.ListPairMode,'Visible','on')
-        set(handles.ListPairMode,'Value',1)
-        set(handles.ListPairMode,'String',{'series(Di)'})
-        set(handles.CheckStereo,'Value',0) 
-        set(handles.last_j,'String',get(handles.nb_field2,'String'))% select the whole volume scan by default
-        set(handles.incr_i,'String',num2str(2))% 
-    otherwise
-        filebase=get(handles.RootPath,'String');
-        set(handles.sub_txt,'Visible','on')
-        set(handles.RootFile_1,'Visible','On');%mkes the second file input window visible
-        mode_store=get(handles.ListPairMode,'String');%get the present 'mode'
-        set(handles.ListCompareMode,'UserData',mode_store);%store the mode display
-        set(handles.ListPairMode,'Visible','off')
-        
-        %% open an image file with the browser
-        ind_opening=1;%default
-        browse.incr_pair=[0 0]; %default
-        oldfile=get(handles.RootPath,'String');
-        menu={'*.png;*.jpg;*.tif;*.avi;*.AVI;', ' (*.png,*.jpg ,.tif, *.avi,*.AVI)';
-            '*.png','.png image files'; ...
-            '*.jpg',' jpeg image files'; ...
-            '*.tif','.tif image files'; ...
-            '*.avi;*.AVI','.avi movie files'; ...
-            '*.*',  'All Files (*.*)'};
-        if strcmp(option,'displacement')
-            comment='Pick the reference file for displacements';
-        else
-            comment='Pick a file of the second series';
-        end
-        [FileName, PathName] = uigetfile( menu, comment,oldfile);
-        fileinput=[PathName FileName];%complete file name
-        sizf=size(fileinput);
-        if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
-        [path,name,ext]=fileparts(fileinput);
-        [path1]=fileparts(filebase);
-        if isunix
-            [status,path]=system(['readlink ' path]);
-            [status,path1]=system(['readlink ' path1]);% look for the true path in case of symbolic paths
-        end
-        if ~strcmp(path1,path)
-            msgbox_uvmat('ERROR','The second image or series must be in the same directory as the first one')
-            return
-        end
-        if strcmp(option,'displacement')
-            [tild,RootFile_1]=fileparts(name);
-        else
-            [FilePath,FileName,Ext]=fileparts(fileinput);
-            % detect the file type, get the movie object if relevant, and look for the corresponding file series:
-            % the root name and indices may be corrected by including the first index i1 if a corresponding xml file exists
-            [RootPath,SubDir,RootFile_1,i1_series,i2_series,j1_series,j2_series,nom_type_1,FileInfo,Object,i1,i2,j1,j2]=find_file_series(FilePath,[FileName Ext]);
-            FileType=FileInfo.FileType;
-            %check image nom type
-            if ~strcmp(nom_type_1,get(handles.NomType,'String'))
-                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')
-                return
-            end
-        end
-        %check image  extension
-        if ~strcmp(ext,get(handles.ImaExt,'String'))
-            msgbox_uvmat('ERROR','The second image series must have the same extension name as the first one')
-            return
-        end
-        set(handles.RootFile_1,'String',RootFile_1);
-        
-end
-ListPairMode_Callback(hObject, eventdata, handles)
-
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% Callbacks in the uipanel Pair Indices
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%------------------------------------------------------------------------
-% --- Executes on button press in ListPairMode.
-function ListPairMode_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-compare_list=get(handles.ListCompareMode,'String');
-val=get(handles.ListCompareMode,'Value');
-compare=compare_list{val};
-if strcmp(compare,'displacement')||strcmp(compare,'shift')
-    mode='displacement';
-else
-    mode_list=get(handles.ListPairMode,'String');
-    if ischar(mode_list)
-        mode_list={mode_list};
-    end
-    mode_value=get(handles.ListPairMode,'Value');
-    mode=mode_list{mode_value};
-end
-displ_num=[];%default
-ref_i=str2double(get(handles.ref_i,'String'));
-% last_i=str2num(get(handles.last_i,'String'));
-time=get(handles.ImaDoc,'UserData'); %get the set of times
-TimeUnit=get(handles.TimeUnit,'String');
-checkframe=strcmp(TimeUnit,'frame');
-siztime=size(time);
-nbfield=siztime(1)-1;
-nbfield2=siztime(2)-1;
-indchosen=1;  %%first pair selected by default
-%displ_num used to define the indices of the civ pairs
-% in mode 'pair j1-j2', j1 and j2 are the file indices, else the indices
-% are relative to the reference indices ref_i and ref_j respectively.
-if isequal(mode,'pair j1-j2')%| isequal(mode,'st_pair j1-j2')
-    dt=1;
-    displ='';
-    index=0;
-    numlist_a=[];
-    numlist_B=[];
-    %get all the time intervals in bursts
-    displ_dt=1;%default
-    nbfield2=min(nbfield2,10);%limitate the number of pairs to 10x10
-    for numod_a=1:nbfield2-1 %nbfield2 always >=2 for 'pair j1-j2' mode
-        for numod_b=(numod_a+1):nbfield2
-            index=index+1;
-            numlist_a(index)=numod_a;
-            numlist_b(index)=numod_b;
-            if size(time,2)>1 && ~checkframe
-                dt(numod_a,numod_b)=time(ref_i+1,numod_b+1)-time(ref_i+1,numod_a+1);%first time interval dt
-                displ_dt(index)=dt(numod_a,numod_b);
-            else
-                displ_dt(index)=1;
-            end
-        end
-    end
-    [dtsort,indsort]=sort(displ_dt);
-    if ~isempty(numlist_a)
-        displ_num(1,:)=numlist_a(indsort);
-        displ_num(2,:)=numlist_b(indsort);
-    end
-    displ_num(3,:)=0;
-    displ_num(4,:)=0;
-    enable_j(handles, 'off')
-elseif isequal(mode,'series(Dj)') %| isequal(mode,'st_series(Dj)')
-    index=1:200;
-    displ_num(1,index)=-floor(index/2);
-    displ_num(2,index)=ceil(index/2);
-    displ_num(3:4,index)=zeros(2,200);
-    enable_j(handles, 'on')
-elseif isequal(mode,'series(Di)') %| isequal(mode,'st_series(Di)')
-    index=1:200;
-    displ_num(1:2,index)=zeros(2,200);
-    displ_num(3,index)=-floor(index/2);
-    displ_num(4,index)=ceil(index/2);
-    enable_i(handles, 'on')
-    if nbfield2 > 1
-        enable_j(handles, 'on')
-    else
-        enable_j(handles, 'off')
-    end
-elseif isequal(mode,'displacement')%the pairs have the same indices
-    displ_num(1,1)=0;
-    displ_num(2,1)=0;
-    displ_num(3,1)=0;
-    displ_num(4,1)=0;
-    if nbfield > 1 || nbfield==0
-        enable_i(handles, 'on')
-    else
-        enable_j(handles, 'off')
-    end
-    if nbfield2 > 1
-        enable_j(handles, 'on')
-    else
-        enable_j(handles, 'off')
-    end
-end
-set(handles.ListPairCiv1,'UserData',displ_num);
-errormsg=find_netcpair_civ( handles,1);
-    if ~isempty(errormsg)
-    msgbox_uvmat('ERROR',errormsg)
-    end
-% find_netcpair_civ2(handles)
-
-function enable_i(handles, state)
-set(handles.itext,'Visible',state)
-set(handles.first_i,'Visible',state)
-set(handles.last_i,'Visible',state)
-set(handles.incr_i,'Visible',state)
-set(handles.nb_field,'Visible',state)
-set(handles.ref_i,'Visible',state)
-
-function enable_j(handles, state)
-set(handles.jtext,'Visible',state)
-set(handles.first_j,'Visible',state)
-set(handles.last_j,'Visible',state)
-set(handles.incr_j,'Visible',state)
-set(handles.nb_field2,'Visible',state)
-set(handles.ref_j,'Visible',state)
-
-
-%------------------------------------------------------------------------
-% --- Executes on selection change in ListPairCiv1.
-function ListPairCiv1_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-%reproduce by default the chosen pair in the checkciv2 menu
-list_pair=get(handles.ListPairCiv1,'String');%get the menu of image pairs
-index_pair=get(handles.ListPairCiv1,'Value');
-displ_num=get(handles.ListPairCiv1,'UserData');
-list_pair2=get(handles.ListPairCiv2,'String');%get the menu of image pairs
-if index_pair<=length(list_pair2)
-    set(handles.ListPairCiv2,'Value',index_pair);
-end
-
-%update first_i and last_i according to the chosen image pairs
-mode_list=get(handles.ListPairMode,'String');
-mode_value=get(handles.ListPairMode,'Value');
-mode=mode_list{mode_value};
-if isequal(mode,'series(Di)')
-    first_i=str2double(get(handles.first_i,'String'));
-    last_i=str2double(get(handles.last_i,'String'));
-    incr_i=str2double(get(handles.incr_i,'String'));
-    num_i=first_i:incr_i:last_i;
-    lastfield=str2double(get(handles.nb_field,'String'));
-    if ~isnan(lastfield)
-        test_find=(num_i-floor(index_pair/2)*ones(size(num_i))>0)& ...
-            (num_i+ceil(index_pair/2)*ones(size(num_i))<=lastfield);
-        num_i=num_i(test_find);
-    end
-    set(handles.first_i,'String',num2str(num_i(1)));
-    set(handles.last_i,'String',num2str(num_i(end)));
-elseif isequal(mode,'series(Dj)')
-    first_j=str2double(get(handles.first_j,'String'));
-    last_j=str2double(get(handles.last_j,'String'));
-    incr_j=str2double(get(handles.incr_j,'String'));
-    num_j=first_j:incr_j:last_j;
-    lastfield2=str2double(get(handles.nb_field2,'String'));
-    if ~isnan(lastfield2)
-        test_find=(num_j-floor(index_pair/2)*ones(size(num_j))>0)& ...
-            (num_j+ceil(index_pair/2)*ones(size(num_j))<=lastfield2);
-        num_j=num_j(test_find);
-    end
-    set(handles.first_j,'String',num2str(num_j(1)));
-    set(handles.last_j,'String',num2str(num_j(end)));
-end
-
-%------------------------------------------------------------------------
-% --- Executes on selection change in ListPairCiv2.
-function ListPairCiv2_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-index_pair=get(handles.ListPairCiv2,'Value');%get the selected position index in the menu
-
-%update first_i and last_i according to the chosen image pairs
-mode_list=get(handles.ListPairMode,'String');
-mode_value=get(handles.ListPairMode,'Value');
-mode=mode_list{mode_value};
-if isequal(mode,'series(Di)')
-    first_i=str2double(get(handles.first_i,'String'));
-    last_i=str2double(get(handles.last_i,'String'));
-    incr_i=str2double(get(handles.incr_i,'String'));
-    num_i=first_i:incr_i:last_i;
-    lastfield=str2double(get(handles.nb_field,'String'));
-    if ~isnan(lastfield)
-        test_find=(num_i-floor(index_pair/2)*ones(size(num_i))>0)& ...
-            (num_i+ceil(index_pair/2)*ones(size(num_i))<=lastfield);
-        num_i=num_i(test_find);
-    end
-    set(handles.first_i,'String',num2str(num_i(1)));
-    set(handles.last_i,'String',num2str(num_i(end)));
-elseif isequal(mode,'series(Dj)')
-    first_j=str2double(get(handles.first_j,'String'));
-    last_j=str2double(get(handles.last_j,'String'));
-    incr_j=str2double(get(handles.incr_j,'String'));
-    num_j=first_j:incr_j:last_j;
-    lastfield2=str2double(get(handles.nb_field2,'String'));
-    if ~isnan(lastfield2)
-        test_find=(num_j-floor(index_pair/2)*ones(size(num_j))>0)& ...
-            (num_j+ceil(index_pair/2)*ones(size(num_j))<=lastfield2);
-        num_j=num_j(test_find);
-    end
-    set(handles.first_j,'String',num2str(num_j(1)));
-    set(handles.last_j,'String',num2str(num_j(end)));
-end
-
-%------------------------------------------------------------------------
-function ref_i_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-mode_list=get(handles.ListPairMode,'String');
-mode_value=get(handles.ListPairMode,'Value');
-mode=mode_list{mode_value};
-errormsg=find_netcpair_civ(handles,1);% update the menu of pairs depending on the available netcdf files
-if isequal(mode,'series(Di)') || ...% we do patch2 only
-        (get(handles.CheckCiv2,'Value')==0 && get(handles.CheckCiv1,'Value')==0 && get(handles.CheckFix1,'Value')==0 && get(handles.CheckPatch1,'Value')==0)
-    errormsg=find_netcpair_civ( handles,2);
-end
-    if ~isempty(errormsg)
-    msgbox_uvmat('ERROR',errormsg)
-    end
-
-%------------------------------------------------------------------------
-function ref_j_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-mode_list=get(handles.ListPairMode,'String');
-mode_value=get(handles.ListPairMode,'Value');
-mode=mode_list{mode_value};
-if isequal(get(handles.CheckCiv1,'Value'),0)|| isequal(mode,'series(Dj)')
-    errormsg=find_netcpair_civ(handles,1);% update the menu of pairs depending on the available netcdf files
-end
-if isequal(mode,'series(Dj)') || ...
-        (get(handles.CheckCiv2,'Value')==0 && get(handles.CheckCiv1,'Value')==0 && get(handles.CheckFix1,'Value')==0 && get(handles.CheckPatch1,'Value')==0)
-    errormsg=find_netcpair_civ(handles,2);
-end
-    if ~isempty(errormsg)
-    msgbox_uvmat('ERROR',errormsg)
-    end
-
-%------------------------------------------------------------------------
-% determine the menu for checkciv1 pairs depending on existing netcdf file at the middle of
-% the field series set by first_i, incr, last_i
-% index=1: look for pairs for civ1
-% index=2: look for pairs for civ2
-function errormsg=find_netcpair_civ(handles,index)
-%------------------------------------------------------------------------
-set(gcf,'Pointer','watch')% set the mouse pointer to 'watch' (clock)
-
-%% initialisation
-errormsg='';
-browse=get(handles.RootPath,'UserData');
-compare_list=get(handles.ListCompareMode,'String');
-val=get(handles.ListCompareMode,'Value');
-compare=compare_list{val};
-if strcmp(compare,'displacement')||strcmp(compare,'shift')
-    mode='displacement';
-else
-    mode_list=get(handles.ListPairMode,'String');
-    mode_value=get(handles.ListPairMode,'Value');
-    if isempty(mode_list)
-        return
-    end
-    mode=mode_list{mode_value};
-end
-nom_type_ima=get(handles.NomType,'String');
-
-%% determine nom_type_nc, nomenclature type of the .nc files:
-[nom_type_nc]=nomtype2pair(nom_type_ima,mode);
-
-%% reads .nc subdirectoy and image numbers from the interface
-SubDirImages=get(handles.SubDirImages,'String');
-subdir_civ1=[SubDirImages get(handles.SubdirCiv1,'String')];%subdirectory subdir_civ1 for the netcdf data
-subdir_civ2=[SubDirImages get(handles.SubdirCiv2,'String')];%subdirectory subdir_civ2 for the netcdf data
-ref_i=str2double(get(handles.ref_i,'String'));
-ref_j=[];
-if isequal(mode,'pair j1-j2')%|isequal(mode,'st_pair j1-j2')
-    ref_j=0;
-elseif strcmp(get(handles.ref_j,'Visible'),'on')
-    ref_j=str2double(get(handles.ref_j,'String'));
-end
-if isempty(ref_j)
-    ref_j=1;
-end
-time=get(handles.ImaDoc,'UserData');%get the set of times
-TimeUnit=get(handles.TimeUnit,'String');
-checkframe=strcmp(TimeUnit,'frame');
-displ_num=get(handles.ListPairCiv1,'UserData');
-
-%% eliminate the first pairs inconsistent with the position
-if isempty(displ_num)
-    nbpair=0;
-else
-    nbpair=length(displ_num(1,:));%nbre of displayed pairs
-    if  isequal(mode,'series(Di)')  %| isequal(mode,'st_series(Di)')
-        nbpair=min(2*ref_i-1,nbpair);%limit the number of pairs with positive first index
-    elseif  isequal(mode,'series(Dj)')% | isequal(mode,'st_series(Dj)')
-        nbpair=min(2*ref_j-1,nbpair);%limit the number of pairs with positive first index
-    end
-end
-nbpair=min(200,nbpair);%limit the number of displayed pairs to 200
-
-%% case with no Civ1 operation, netcdf files need to exist for reading
-displ_pair={''};
-select=ones(size(1:nbpair));%flag for displayed pairs =1 for display
-testpair=0;
-RootPath=get(handles.RootPath,'String');
-RootFile=get(handles.RootFile,'String');
-if index==1 % case civ1
-    if ~get(handles.CheckCiv1,'Value') %
-        if ~exist(fullfile(RootPath,subdir_civ1),'dir')
-            errormsg=['no civ1 file available: subdirectory ' subdir_civ1 ' does not exist'];
-            set(handles.ListPairCiv1,'String',{});
-            return
-        end
-        for ipair=1:nbpair
-            filename=fullfile_uvmat(RootPath,subdir_civ1,RootFile,'.nc',nom_type_nc,...
-                ref_i+displ_num(3,ipair),ref_i+displ_num(4,ipair),ref_j+displ_num(1,ipair),ref_j+displ_num(2,ipair));
-            select(ipair)=exist(filename,'file')==2;% put flag to 0 if the file does not exist
-        end
-        % case of no displayed pair
-        if isequal(select,zeros(size(1:nbpair)))
-            if isfield(browse,'incr_pair') && ~isequal(browse.incr_pair,[0 0])
-                num_i1=ref_i-floor(browse.incr_pair(1)/2);
-                num_i2=ref_i+ceil(browse.incr_pair(1)/2);
-                num_j1=ref_j-floor(browse.incr_pair(2)/2);
-                num_j2=ref_j+ceil(browse.incr_pair(2)/2);
-                filename=fullfile_uvmat(RootPath,subdir_civ1,RootFile,'.nc',nom_type_nc,num_i1,num_i2,num_j1,num_j2);
-                select(1)=exist(filename,'file')==2;
-                testpair=1;
-            else
-%                 if  isequal(mode,'series(Dj)')% | isequal(mode,'st_series(Dj)')
-%                     errormsg=['no civ1 file available for the selected reference index j=' num2str(ref_j) ' and subdirectory ' subdir_civ1];
-%                 else
-                    errormsg=['no civ1 file available for the selected reference indices (i,j)= ' num2str(ref_i) ', ' num2str(ref_j) ' and subdirectory ' subdir_civ1];
-%                 end
-                set(handles.ListPairCiv1,'String',{''});
-                %COMPLETER CAS STEREO
-                return
-            end
-        end
-    end
-else %case civ2 alone
-    if ~get(handles.CheckCiv2,'Value') && ~get(handles.CheckCiv1,'Value') && ~get(handles.CheckFix1,'Value') && ~get(handles.CheckPatch1,'Value')
-        if ~exist(fullfile(RootPath,subdir_civ2),'dir')
-            msgbox_uvmat('ERROR',['no civ2 file available: subdirectory ' subdir_civ2 ' does not exist'])
-            set(handles.ListPairCiv2,'Value',1);
-            set(handles.ListPairCiv2,'String',{''});
-            return
-        end
-        for ipair=1:nbpair
-            filename=fullfile_uvmat(RootPath,subdir_civ1,RootFile,'.nc',nom_type_nc,...
-                ref_i+displ_num(3,ipair),ref_i+displ_num(4,ipair),ref_j+displ_num(1,ipair),ref_j+displ_num(2,ipair));
-            select(ipair)=exist(filename,'file')==2;
-        end
-        if  isequal(select,zeros(size(1:nbpair)))
-            if isfield(browse,'incr_pair')
-                num_i1=ref_i-floor(browse.incr_pair(1)/2);
-                num_i2=ref_i+floor((browse.incr_pair(1)+1)/2);
-                num_j1=ref_j-floor(browse.incr_pair(2)/2);
-                num_j2=ref_j+floor((browse.incr_pair(2)+1)/2);
-                filename=fullfile_uvmat(RootPath,subdir_civ2,RootFile,'.nc',nom_type_nc,num_i1,num_i2,num_j1,num_j2);
-                select(1)=exist(filename,'file')==2;
-            else
-                if  isequal(mode,'series(Dj)')% | isequal(mode,'st_series(Dj)')
-                    errormsg=['no civ2 file available for the selected reference index j=' num2str(ref_j) ' and subdirectory ' subdir_civ2];
-                else
-                    errormsg=['no civ2 file available for the selected reference index i=' num2str(ref_i) ' and subdirectory ' subdir_civ2];
-                end
-                set(handles.ListPairCiv2,'Value',1);
-                set(handles.ListPairCiv2,'String',{''});
-                return
-            end
-        end
-    end
-end
-
-%% determine the menu display in .ListPairCiv1
-% the menu depends on the mode defined in ListPairMode_callback through the array displ_num:
-% displ_num(1,:)=indices j1
-% displ_num(2,:)=indices j2
-% displ_num(3,:)=indices i1
-% displ_num(4,:)=indices i2
-% in mode 'pair j1-j2', j1 and j2 are the file indices, else the indices
-% are relative to the reference indices ref_i and ref_j respectively.
-if isequal(mode,'series(Di)')
-    if testpair
-        displ_pair{1}=['Di= ' num2str(-floor(browse.incr_pair(1)/2)) '|' num2str(ceil(browse.incr_pair(1)/2))];
-    else
-        for ipair=1:nbpair
-            if select(ipair)
-                displ_pair{ipair}=['Di= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2))];
-                %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
-                 %   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
-               if ~checkframe && size(time,1)>=ref_i+1+ceil(ipair/2) && size(time,2)>=ref_j+1&& ref_i-floor(ipair/2)>=0 && ref_j>=0
-                 dt=time(ref_i+1+ceil(ipair/2),ref_j+1)-time(ref_i+1-floor(ipair/2),ref_j+1);%time interval dtref_j+1
-                else
-                    dt=1;
-                end
-                 displ_pair{ipair}=[displ_pair{ipair} ' :dt= ' num2str(dt*1000)];
-            else
-                displ_pair{ipair}='...'; %pair not displayed in the menu
-            end
-        end
-    end
-elseif isequal(mode,'series(Dj)')
-    if testpair
-        displ_pair{1}=['Dj= ' num2str(-floor(browse.incr_pair(1)/2)) '|' num2str(ceil(browse.incr_pair(1)/2))];
-    else
-        for ipair=1:nbpair
-            if select(ipair)
-                displ_pair{ipair}=['Dj= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2))];
-                if ~checkframe && size(time,1)>=ref_i+1+displ_num(4,ipair) && size(time,2)>=ref_j+1+displ_num(2,ipair)
-                    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
-                    displ_pair{ipair}=[displ_pair{ipair} ' :dt= ' num2str(dt*1000)];
-                end
-            else
-                displ_pair{ipair}='...'; %pair not displayed in the menu
-            end
-        end
-    end
-elseif isequal(mode,'pair j1-j2')%case of pairs
-    for ipair=1:nbpair
-        if select(ipair)
-            if ~checkframe && size(time,2)>1 
-            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
-            else % time set by default to i index
-                dt=1;
-            end
-            displ_pair{ipair}=['j= ' num2stra(displ_num(1,ipair),nom_type_ima) '-' num2stra(displ_num(2,ipair),nom_type_ima) ...
-                ' :dt= ' num2str(dt*1000)];
-        else
-            displ_pair{ipair}='...'; %pair not displayed in the menu
-        end
-    end
-elseif isequal(mode,'displacement')
-    displ_pair={'Di=Dj=0'};
-end
-if index==1
-set(handles.ListPairCiv1,'String',displ_pair');
-end
-
-%% determine the default selection in the pair menu
-ichoice=find(select,1);% index of selected pair
-if (isempty(ichoice) || ichoice < 1); ichoice=1; end;
-initial=get(handles.ListPairCiv1,'Value');%initial choice of pair
-if initial>nbpair || (numel(select)>=initial && ~isequal(select(initial),1))
-    set(handles.ListPairCiv1,'Value',ichoice);% first valid pair proposed by default in the menu
-end
-initial=get(handles.ListPairCiv2,'Value');
-if initial>length(displ_pair')%|~isequal(select(initial),1)
-    if ichoice <= length(displ_pair')
-        set(handles.ListPairCiv2,'Value',ichoice);% same pair proposed by default for civ2
-    else
-        set(handles.ListPairCiv2,'Value',1);% same pair proposed by default for civ2
-    end
-end
-set(handles.ListPairCiv2,'String',displ_pair');
-set(gcf,'Pointer','arrow')
-
-
-    
-% %------------------------------------------------------------------------   
-% % call 'view_field.fig' to display the  field selected in the list of 'status'
-% function open_view_field(hObject, eventdata)
-% %------------------------------------------------------------------------
-% list=get(hObject,'String');
-% index=get(hObject,'Value');
-% rootroot=get(hObject,'UserData');
-% filename=list{index};
-% ind_dot=strfind(filename,'...');
-% filename=filename(1:ind_dot-1);
-% filename=fullfile(rootroot,filename);
-% delete(get(hObject,'parent'))%delete the display figure to stop the check process
-% if exist(filename,'file')%visualise the vel field if it exists
-%     uvmat(filename)
-%     set(gcbo,'Value',1)
-% end
-
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% Callbacks in the uipanel Reference Indices
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%------------------------------------------------------------------------
-function first_i_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-first_i=str2double(get(handles.first_i,'String'));
-set(handles.ref_i,'String', num2str(first_i))% reference index for pair dt = first index
-ref_i_Callback(hObject, eventdata, handles)%refresh dispaly of dt for pairs (in case of non constant dt)
-
-%------------------------------------------------------------------------
-function first_j_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-first_j=str2num(get(handles.first_j,'String'));
-set(handles.ref_j,'String', num2str(first_j))% reference index for pair dt = first index
-ref_j_Callback(hObject, eventdata, handles)%refresh dispaly of dt for pairs (in case of non constant dt)
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% Callbacks in the uipanel Civ1
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%------------------------------------------------------------------------
-% --- Executes on button press in SearchRange: determine the search range num_SearchBoxSize_1,num_SearchBoxSize_2
-function SearchRange_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-%determine pair numbers
-if strcmp(get(handles.num_UMin,'Visible'),'off')
-    set(handles.u_title,'Visible','on')
-    set(handles.v_title,'Visible','on')
-    set(handles.num_UMin,'Visible','on')
-    set(handles.num_UMax,'Visible','on')
-    set(handles.num_VMin,'Visible','on')
-    set(handles.num_VMax,'Visible','on')
-    set(handles.CoordUnit,'Visible','on')
-    set(handles.TimeUnit,'Visible','on')
-    set(handles.slash_title,'Visible','on')
-    set(handles.min_title,'Visible','on')
-    set(handles.max_title,'Visible','on')
-    set(handles.unit_title,'Visible','on')
-else
-    get_search_range(hObject, eventdata, handles)
-end
-
-%------------------------------------------------------------------------
-% ---  determine the search range num_SearchBoxSize_1,num_SearchBoxSize_2 and shift
-function get_search_range(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-param_civ1=read_GUI(handles.Civ1);
-umin=param_civ1.UMin;
-umax=param_civ1.UMax;
-vmin=param_civ1.VMin;
-vmax=param_civ1.VMax;
-%switch min_title and max_title in case of error
-if umax<=umin
-    umin_old=umin;
-    umin=umax;
-    umax=umin_old;
-    set(handles.num_UMin,'String', num2str(umin))
-    set(handles.num_UMax,'String', num2str(umax))
-end
-if vmax<=vmin
-    vmin_old=vmin;
-    vmin=vmax;
-    vmax=vmin_old;
-    set(handles.num_VMin,'String', num2str(vmin))
-    set(handles.num_VMax,'String', num2str(vmax))
-end   
-if ~(isempty(umin)||isempty(umax)||isempty(vmin)||isempty(vmax))
-    list_pair=get(handles.ListPairCiv1,'String');%get the menu of image pairs
-    index=get(handles.ListPairCiv1,'Value');
-    pair_string=list_pair{index};
-    time=get(handles.ImaDoc,'UserData'); %get the set of times
-    pxcm=get(handles.SearchRange,'UserData');
-    mode_list=get(handles.ListPairMode,'String');
-    mode_value=get(handles.ListPairMode,'Value');
-    mode=mode_list{mode_value};      
-    if isequal (mode, 'series(Di)' )
-        ref_i=str2double(get(handles.ref_i,'String'));
-        num1=ref_i-floor(index/2);%  first image numbers
-        num2=ref_i+ceil(index/2);
-        num_a=1;
-        num_b=1;
-    elseif isequal (mode, 'series(Dj)')
-        num1=1;
-        num2=1;
-        ref_j=str2double(get(handles.ref_j,'String'));
-        num_a=ref_j-floor(index/2);%  first image numbers
-        num_b=ref_j+ceil(index/2);
-    elseif isequal(mode,'pair j1-j2') %case of bursts (png_old or png_2D)     
-        ref_i=str2double(get(handles.ref_i,'String'));
-        num1=ref_i;
-        num2=ref_i;
-                r=regexp(pair_string,'(?<mode>(Di=)|(Dj=)) -*(?<num1>\d+)\|(?<num2>\d+)','names');
-        if isempty(r)
-            r=regexp(pair_string,'(?<num1>\d+)(?<mode>-)(?<num2>\d+)','names');
-        end  
-        num_a=str2num(r.num1);
-        num_b=str2num(r.num2);
-    end
-    dt=time(num2+1,num_b+1)-time(num1+1,num_a+1);
-    ibx=str2double(get(handles.num_CorrBoxSize_1,'String'));
-    iby=str2double(get(handles.num_CorrBoxSize_2,'String'));
-    umin=dt*pxcm*umin;
-    umax=dt*pxcm*umax;
-    vmin=dt*pxcm*vmin;
-    vmax=dt*pxcm*vmax;
-    shiftx=round((umin+umax)/2);
-    shifty=round((vmin+vmax)/2);
-    isx=(umax+2-shiftx)*2+param_civ1.Bx;
-    isx=2*ceil(isx/2)+1;
-    isy=(vmax+2-shifty)*2+param_civ1.Bx;
-    isy=2*ceil(isy/2)+1;
-    set(handles.num_SearchBoxShift_1,'String',num2str(shiftx));
-    set(handles.num_SearchBoxShift_2,'String',num2str(shifty));
-    set(handles.num_SearchBoxSize_1,'String',num2str(isx));
-    set(handles.num_SearchBoxSize_2,'String',num2str(isy));
-end
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% Callbacks in the uipanel Fix1
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%------------------------------------------------------------------------
-% --- Executes on button press in CheckMask.
-function get_mask_fix1_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-maskval=get(handles.CheckMask,'Value');
-if isequal(maskval,0)
-    set(handles.Mask,'String','')
-else
-    mask_displ='no mask'; %default
-    filebase=get(handles.RootPath,'String');
-    [nbslice, flag_mask]=get_mask(filebase,handles);
-    if isequal(flag_mask,1)
-        mask_displ=[num2str(nbslice) 'mask'];
-    elseif get(handles.ListCompareMode,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
-        filebase_a=get(handles.RootFile_1,'String');
-        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
-        if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice)
-            mask_displ='no mask';
-        end
-    end
-    if isequal(mask_displ,'no mask')
-        [FileName, PathName, filterindex] = uigetfile( ...
-            {'*.png', ' (*.png)';
-            '*.png',  '.png files '; ...
-            '*.*', 'All Files (*.*)'}, ...
-            'Pick a mask file *.png',filebase);
-        mask_displ=fullfile(PathName,FileName);
-        if ~exist(mask_displ,'file')
-            mask_displ='no mask';
-        end
-    end
-    if isequal(mask_displ,'no mask')
-        set(handles.CheckMask,'Value',0)
-        set(handles.CheckMask,'Value',0)
-        set(handles.CheckMask,'Value',0)
-    else
-        %set(handles.CheckMask,'Value',1)
-        set(handles.CheckMask,'Value',1)
-    end
-    set(handles.Mask,'String',mask_displ)
-    set(handles.Mask,'String',mask_displ)
-    set(handles.Mask,'String',mask_displ)
-end
-
-%------------------------------------------------------------------------
-% --- Executes on button press in CheckMask: select box for mask option
-function get_mask_civ2_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-maskval=get(handles.CheckMask,'Value');
-if isequal(maskval,0)
-    set(handles.Mask,'String','')
-else
-    mask_displ='no mask'; %default
-    filebase=get(handles.RootPath,'String');
-    [nbslice, flag_mask]=get_mask(filebase,handles);
-    if isequal(flag_mask,1)
-        mask_displ=[num2str(nbslice) 'mask'];
-    elseif get(handles.ListCompareMode,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
-        filebase_a=get(handles.RootFile_1,'String');
-        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
-        if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice)
-            mask_displ='no mask';
-        end
-    end
-    if isequal(mask_displ,'no mask')
-        [FileName, PathName, filterindex] = uigetfile( ...
-            {'*.png', ' (*.png)';
-            '*.png',  '.png files '; ...
-            '*.*', 'All Files (*.*)'}, ...
-            'Pick a mask file *.png',filebase);
-        mask_displ=fullfile(PathName,FileName);
-        if ~exist(mask_displ,'file')
-            mask_displ='no mask';
-        end
-    end
-    if isequal(mask_displ,'no mask')
-        set(handles.CheckMask,'Value',0)
-        set(handles.CheckMask,'Value',0)
-    else
-        set(handles.CheckMask,'Value',1)
-    end
-    set(handles.Mask,'String',mask_displ)
-    set(handles.Mask,'String',mask_displ)
-end
-
-%------------------------------------------------------------------------
-% --- Executes on button press in CheckMask.
-function get_mask_fix2_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-maskval=get(handles.CheckMask,'Value');
-if isequal(maskval,0)
-    set(handles.Mask,'String','')
-else
-    mask_displ='no mask'; %default
-    filebase=get(handles.RootPath,'String');
-    [nbslice, flag_mask]=get_mask(filebase,handles);
-    if isequal(flag_mask,1)
-        mask_displ=[num2str(nbslice) 'mask'];
-    elseif get(handles.ListCompareMode,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
-        filebase_a=get(handles.RootFile_1,'String');
-        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
-        if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice)
-            mask_displ='no mask';
-        end
-    end
-    if isequal(mask_displ,'no mask')
-        [FileName, PathName, filterindex] = uigetfile( ...
-            {'*.png', ' (*.png)';
-            '*.png',  '.png files '; ...
-            '*.*', 'All Files (*.*)'}, ...
-            'Pick a mask file *.png',filebase);
-        mask_displ=fullfile(PathName,FileName);
-        if ~exist(mask_displ,'file')
-            mask_displ='no mask';
-        end
-    end
-    if isequal(mask_displ,'no mask')
-        set(handles.CheckMask,'Value',0)
-    end
-    set(handles.Mask,'String',mask_displ)
-end
-
-%------------------------------------------------------------------------
-% --- function called to look for mask files
-function [nbslice, flag_mask]=get_mask(filebase,handles)
-%------------------------------------------------------------------------
-%detect mask files, images with appropriate file base
-%[filebase '_' xx 'mask'], xx=nbslice
-%flag_mask=1 indicates detection
-
-flag_mask=0;%default
-nbslice=1;
-
-% subdir=get(handles.SubdirCiv1,'String');
-[Path,Name]=fileparts(filebase);
-if ~isdir(Path)
-    msgbox_uvmat('ERROR','no path for input files')
-    return
-end
-% currentdir=pwd;
-% cd(Path);%move in the dir of the root name filebase
-maskfiles=dir(fullfile(Path,[Name '_*mask_*.png']));%look for mask files
-% cd(currentdir);%come back to the current working directory
-if ~isempty(maskfiles)
-    %     msgbox_uvmat('ERROR','no mask available, to create it use Tools/Make mask in the upper menu bar of uvmat')
-    % else
-    flag_mask=1;
-    maskname=maskfiles(1).name;% take the first mask file in the list
-    [Path2,Name,ext]=fileparts(maskname);
-    Namedouble=double(Name);
-    val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
-    ind_mask=findstr('mask',Name);
-    i=ind_mask-1;
-    while val(i)==0 && i>0
-        i=i-1;
-    end
-    nbslice=str2double(Name(i+1:ind_mask-1));
-    if ~isnan(nbslice) && Name(i)=='_'
-        flag_mask=1;
-    else
-        msgbox_uvmat('ERROR',['bad mask file ' Name ext ' found in ' Path2])
-        return
-        nbslice=1;
-    end
-end
-
-%------------------------------------------------------------------------
-% --- function called to look for grid files
-function [nbslice, flag_grid]=get_grid(filebase,handles)
-%------------------------------------------------------------------------
-flag_grid=0;%default
-nbslice=1;
-[Path,Name]=fileparts(filebase);
-currentdir=pwd;
-cd(Path);%move in the dir of the root name filebase
-gridfiles=dir([Name '_*grid_*.grid']);%look for grid files
-cd(currentdir);%come back to the current working directory
-if ~isempty(gridfiles)
-    flag_grid=1;
-    gridname=gridfiles(1).name;% take the first grid file in the list
-    [Path2,Name,ext]=fileparts(gridname);
-    Namedouble=double(Name);
-    val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
-    ind_grid=findstr('grid',Name);
-    i=ind_grid-1;
-    while val(i)==0 && i>0
-        i=i-1;
-    end
-    nbslice=str2double(Name(i+1:ind_grid-1));
-    if ~isnan(nbslice) && Name(i)=='_'
-        flag_grid=1;
-    else
-        msgbox_uvmat('ERROR',['bad grid file ' Name ext ' found in ' Path2])
-        return
-        nbslice=1;
-    end
-end
-
-%------------------------------------------------------------------------
-% --- transform numbers to letters
-function str=num2stra(num,nom_type)
-%------------------------------------------------------------------------
-if isempty(nom_type)
-    str='';
-elseif strcmp(nom_type(end),'a')
-    str=char(96+num);
-elseif strcmp(nom_type(end),'A')
-    str=char(96+num);
-elseif isempty(nom_type(2:end))%a single index
-    str='';
-else
-    str=num2str(num);
-end
-
-% %------------------------------------------------------------------------
-% % --- Executes on button press in ListSubdirCiv1.
-% function ListSubdirCiv1_Callback(hObject, eventdata, handles)
-% %------------------------------------------------------------------------
-% list_subdir_civ1=get(handles.ListSubdirCiv1,'String');
-% val=get(handles.ListSubdirCiv1,'Value');
-% SubDir=list_subdir_civ1{val};
-% if strcmp(SubDir,'new...')
-%     if get(handles.CheckCiv1,'Value')
-%         SubDir='CIV'; %default subdirectory
-%     else
-%         msgbox_uvmat('ERROR','select CheckCiv1 to perform a new Civ operation')
-%         return
-%     end    
-% end
-% set(handles.SubdirCiv1,'String',SubDir);
-% errormsg=find_netcpair_civ(handles,1);
-% if ~isempty(errormsg)
-%     msgbox_uvmat('ERROR',errormsg)
-% end
-%     
-%------------------------------------------------------------------------
-% % --- Executes on button press in ListSubdirCiv2.
-% function ListSubdirCiv2_Callback(hObject, eventdata, handles)
-% %------------------------------------------------------------------------
-% list_subdir_civ2=get(handles.ListSubdirCiv2,'String');
-% val=get(handles.ListSubdirCiv2,'Value');
-% SubDir=list_subdir_civ2{val};
-% if strcmp(SubDir,'new...')
-%     if get(handles.CheckCiv2,'Value')
-%         SubDir='CIV'; %default subdirectory
-%     else
-%         msgbox_uvmat('ERROR','select CheckCiv2 to perform a new Civ operation')
-%         return
-%     end
-% end
-% set(handles.SubdirCiv2,'String',SubDir);
-
-%------------------------------------------------------------------------
-% --- Executes on button press in CheckGrid.
-function CheckGrid_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-value=get(hObject,'Value');
-hparent=get(hObject,'parent');%handles of the parent panel
-hchildren=get(hparent,'children');
-handle_txtbox=findobj(hchildren,'tag','Grid');% look for the grid name box in the same panel
-handle_dx=findobj(hchildren,'tag','num_Dx');
-handle_dy=findobj(hchildren,'tag','num_Dy');
-handle_title_dx=findobj(hchildren,'tag','title_Dx');
-handle_title_dy=findobj(hchildren,'tag','title_Dy');
-testgrid=0;
-filegrid='';
-if value
-    filebase=get(handles.RootPath,'String');
-    [nbslice, flag_grid]=get_grid(filebase,handles);% look for a grid with appropriate name 
-    if isequal(flag_grid,1)
-        filegrid=[num2str(nbslice) 'grid'];
-        testgrid=1;
-    else % browse for a grid 
-        filegrid=get(hObject,'UserData');%look for previous grid name stored as UserData
-        if exist(filegrid,'file')
-            filebase=filegrid;
-        end
-        [FileName, PathName] = uigetfile( ...
-            {'*.grid', ' (*.grid)';
-            '*.grid',  '.grid files '; ...
-            '*.*', 'All Files (*.*)'}, ...
-            'Pick a file',filebase);
-        filegrid=fullfile(PathName,FileName);
-        set(hObject,'UserData',filegrid);%store for future use
-        if ~(isempty(FileName)||isempty(PathName)||isequal(FileName,0)||~exist(filegrid,'file'))
-            testgrid=1;
-        end
-    end
-end
-if testgrid
-    set(handle_dx,'Visible','off');
-    set(handle_dy,'Visible','off');
-    set(handle_title_dy,'Visible','off');
-    set(handle_title_dx,'Visible','off');
-    set(handle_txtbox,'Visible','on')
-    set(handle_txtbox,'String',filegrid)
-else
-    set(hObject,'Value',0);
-    set(handle_dx,'Visible','on'); 
-    set(handle_dy,'Visible','on');
-    set(handle_title_dy,'Visible','on');
-    set(handle_title_dx,'Visible','on');
-    set(handle_txtbox,'Visible','off')
-end
-
-%% if hObject is on the checkciv1 frame, duplicate action for checkciv2 frame
-PanelName=get(hparent,'tag');
-if strcmp(PanelName,'Civ1')
-    hchildren=get(handles.Civ2,'children');
-    handle_checkbox=findobj(hchildren,'tag','CheckGrid');
-    handle_txtbox=findobj(hchildren,'tag','Grid');
-    handle_dx=findobj(hchildren,'tag','num_Dx');
-    handle_dy=findobj(hchildren,'tag','num_Dy');
-    handle_title_dx=findobj(hchildren,'tag','title_Dx');
-    handle_title_dy=findobj(hchildren,'tag','title_Dy');
-    set(handle_checkbox,'UserData',filegrid);%store for future use
-    if testgrid
-        set(handle_checkbox,'Value',1);
-        set(handle_dx,'Visible','off');
-        set(handle_dy,'Visible','off');
-        set(handle_title_dx,'Visible','off');
-        set(handle_title_dy,'Visible','off');
-        set(handle_txtbox,'Visible','on')
-        set(handle_txtbox,'String',filegrid)
-    end 
-end
-
-%------------------------------------------------------------------------
-% --- Executes on button press in CheckMask: common to all panels (civ1, Civ2..)
-function CheckMask_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-value=get(hObject,'Value');
-hparent=get(hObject,'parent');
-parent_tag=get(hparent,'Tag');
-hchildren=get(hparent,'children');
-handle_txtbox=findobj(hchildren,'tag','Mask');% look for the mask name box in the same panel
-testmask=0;
-if value
-    filebase=get(handles.RootPath,'String');
-    [nbslice, flag_mask]=get_mask(filebase,handles);% look for a mask with appropriate name 
-    if isequal(flag_mask,1)
-        filemask=[num2str(nbslice) 'mask'];
-        testmask=1;
-    else % browse for a mask 
-        filemask=get(hObject,'UserData');%look for previous mask name stored as UserData
-        if exist(filemask,'file')
-            filebase=filemask;
-        end
-        [FileName, PathName] = uigetfile( ...
-            {'*.png', ' (*.png)';
-            '*.png',  '.png files '; ...
-            '*.*', 'All Files (*.*)'}, ...
-            'Pick a mask file *.png',filebase);
-        filemask=fullfile(PathName,FileName);
-        set(hObject,'UserData',filemask);%store for future use
-        if ~(isempty(FileName)||isempty(PathName)||isequal(FileName,0)||~exist(filemask,'file'))
-            testmask=1;
-        end
-    end
-end
-if testmask
-    if strcmp(parent_tag,'Civ1')
-        set(handles.Mask,'Visible','on')
-        set(handles.Mask,'String',filemask)
-    set(handles.CheckMask,'Value',1)
-    end
-else
-    set(hObject,'Value',0);
-    set(handle_txtbox,'Visible','off')
-end
-
-%------------------------------------------------------------------------
-% --- Executes on button press in get_gridpatch1.
-function get_gridpatch1_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-filebase=get(handles.RootPath,'String');
-[FileName, PathName, filterindex] = uigetfile( ...
-    {'*.grid', ' (*.grid)';
-    '*.grid',  '.grid files '; ...
-    '*.*', 'All Files (*.*)'}, ...
-    'Pick a file',filebase);
-filegrid=fullfile(PathName,FileName);
-set(handles.grid_patch1,'string',filegrid);
-
-
-%------------------------------------------------------------------------
-% --- Executes on button press in get_gridpatch2.
-function get_gridpatch2_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-
-
-%------------------------------------------------------------------------
-% --- STEREO Interp
-function cmd=RUN_STINTERP(stinterpBin,filename_A_nc,filename_B_nc,filename_nc,nx_patch,ny_patch,rho_patch,subdomain_patch,thresh_value,xmlA,xmlB)
-%------------------------------------------------------------------------
-namelog=[filename_nc(1:end-3) '_stinterp.log'];
-cmd=[stinterpBin ' -f1 ' filename_A_nc  ' -f2 ' filename_B_nc ' -f  ' filename_nc ...
-    ' -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
-
-% %------------------------------------------------------------------------
-% %--read images and convert them to the uint16 format used for PIV
-% function A=read_image(filename,type_ima,num,movieobject)
-% %------------------------------------------------------------------------
-% %num is the view number needed for an avi movie
-% switch type_ima
-%     case 'movie'
-%         A=read(movieobject,num);
-%     case 'avi'
-%         mov=aviread(filename,num);
-%         A=frame2im(mov(1));
-%     case 'multimage'
-%         A=imread(filename,num);
-%     case 'image'
-%         A=imread(filename);
-% end
-% siz=size(A);
-% if length(siz)==3;%color images
-%     A=sum(double(A),3);
-%     A=uint16(A);
-% end
-
-
-%------------------------------------------------------------------------
-% --- Executes on button press in get_ref_fix1.
-function get_ref_fix1_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-filebase=get(handles.RootPath,'String');
-[FileName, PathName, filterindex] = uigetfile( ...
-    {'*.nc', ' (*.nc)';
-    '*.nc',  'netcdf files '; ...
-    '*.*', 'All Files (*.*)'}, ...
-    'Pick a file',filebase);
-
-fileinput=[PathName FileName];
-sizf=size(fileinput);
-if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
-%[Path,File,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
-[Path,ref.subdir,File,ref.num1,ref.num2,ref.num_a,ref.num_b,ref.ext,ref.nom_type]=fileparts_uvmat(fileinput);
-ref.filebase=fullfile(Path,File);
-% ref.num_a=stra2num(str_a);
-% ref.num_b=stra2num(str_b);
-% ref.num1=str2double(field_count);
-% ref.num2=str2double(str2);
-browse=[];%initialisation
-if ~isequal(ref.ext,'.nc')
-    msgbox_uvmat('ERROR','the reference file must be in netcdf format (*.nc)')
-    return
-end
-set(handles.ref_fix1,'String',[fullfile(ref.subdir,File) '....nc']);
-set(handles.ref_fix1,'UserData',ref)
-menu_field{1}='civ1';
-Data=nc2struct(fileinput,[]);
-if isfield(Data,'patch') && isequal(Data.patch,1)
-    menu_field{2}='filter1';
-end
-if isfield(Data,'civ2') && isequal(Data.civ2,1)
-    menu_field{3}='civ2';
-end
-if isfield(Data,'patch2') && isequal(Data.patch2,1)
-    menu_field{4}='filter2';
-end
-set(handles.field_ref1,'String',menu_field);
-set(handles.field_ref1,'Value',length(menu_field));
-set(handles.num_MinVel,'Value',2);
-set(handles.num_MinVel,'String','1');%default threshold
-set(handles.ref_fix1,'Enable','on')
-
-%------------------------------------------------------------------------
-% --- Executes on button press in get_ref_fix2.
-function get_ref_fix2_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-if isequal(get(handles.get_ref_fix2,'Value'),1)
-    filebase=get(handles.RootPath,'String');
-    [FileName, PathName, filterindex] = uigetfile( ...
-        {'*.nc', ' (*.nc)';
-        '*.nc',  'netcdf files '; ...
-        '*.*', 'All Files (*.*)'}, ...
-        'Pick a file',filebase);
-    fileinput=[PathName FileName];
-    sizf=size(fileinput);
-    if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
-    %[Path,File,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
-    [Path,ref.subdir,File,ref.num1,ref.num2,ref.num_a,ref.num_b,ref.ext,ref.nom_type]=fileparts_uvmat(fileinput);
-    ref.filebase=fullfile(Path,File);
-%     ref.num_a=stra2num(str_a);
-%     ref.num_b=stra2num(str_b);
-%     ref.num1=str2num(field_count);
-%     ref.num2=str2num(str2);
-    browse=[];%initialisation
-    if ~isequal(ref.ext,'.nc')
-        msgbox_uvmat('ERROR','the reference file must be in netcdf format (*.nc)')
-        return
-    end
-    set(handles.ref_fix2,'String',[fullfile(ref.subdir,File) '....nc']);
-    set(handles.ref_fix2,'UserData',ref)
-    menu_field{1}='civ1';
-    Data=nc2struct(fileinput,[]);
-    if isfield(Data,'patch') && isequal(Data.patch,1)
-        menu_field{2}='filter1';
-    end
-    if isfield(Data,'civ2') && isequal(Data.civ2,1)
-        menu_field{3}='civ2';
-    end
-    if isfield(Data,'patch2') && isequal(Data.patch2,1)
-        menu_field{4}='filter2';
-    end
-    set(handles.field_ref2,'String',menu_field);
-    set(handles.field_ref2,'Value',length(menu_field));
-    set(handles.num_MinVel,'Value',2);
-    set(handles.num_MinVel,'String','1');%default threshold
-    set(handles.ref_fix2,'Enable','on')
-    set(handles.ref_fix2,'Visible','on')
-    set(handles.field_ref2,'Visible','on')
-else
-    set(handles.ref_fix2,'Visible','off')
-    set(handles.field_ref2,'Visible','off')
-end
-
-%------------------------------------------------------------------------
-function ref_fix1_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-set(handles.num_MinVel,'Value',1);
-set(handles.field_ref1,'Value',1)
-set(handles.field_ref1,'String',{' '})
-set(handles.ref_fix1,'UserData',[]);
-set(handles.ref_fix1,'String','');
-set(handles.thresh_vel1,'String','0');
-
-%------------------------------------------------------------------------
-function ref_fix2_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-set(handles.num_MinVel,'Value',1);
-set(handles.field_ref2,'Value',1)
-set(handles.field_ref2,'String',{' '})
-set(handles.ref_fix2,'UserData',[]);
-set(handles.ref_fix2,'String','');
-set(handles.num_MinVel,'String','0');
-
-%------------------------------------------------------------------------
-% --- TO ABANDON Executes on button press in test_stereo1.
-function CheckStereo_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-hparent=get(hObject,'parent');
-parent_tag=get(hparent,'Tag');
-hchildren=get(hparent,'children');
-handle_txtbox=findobj(hchildren,'tag','txt_Mask');
-if isequal(get(hObject,'Value'),0)
-    set(handles.num_SubDomainSize,'Visible','on')
-    set(handles.num_FieldSmooth,'Visible','on')
-else
-    set(handles.num_SubDomainSize,'Visible','off')
-    set(handles.num_FieldSmooth,'Visible','off')
-end
-
-% %------------------------------------------------------------------------
-% % --- Executes on button press in CheckStereo.
-% function StereoCheck_Callback(hObject, eventdata, handles)
-% %------------------------------------------------------------------------
-% if isequal(get(handles.CheckStereo,'Value'),0)
-%     set(handles.num_subdomainsize,'Visible','on')
-%     set(handles.num_FieldSmooth,'Visible','on')
-% else
-%     set(handles.num_subdomainsize,'Visible','off')
-%     set(handles.num_FieldSmooth,'Visible','off')
-% end
-
-%------------------------------------------------------------------------
-% --- Executes on button press in TestCiv1: prepare the image correlation function
-% activated by mouse motion
-function TestCiv1_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-drawnow
-if get(handles.TestCiv1,'Value')
-    set(handles.TestCiv1,'BackgroundColor',[0.7 0.7 0.7])% paint TestCiv1 button to grey to confirm civ launch
-    ref_i=str2double(get(handles.ref_i,'String'));% read reference i index
-    if strcmp(get(handles.ref_j,'Visible'),'on')
-        ref_j=str2double(get(handles.ref_j,'String'));% read reference j index if relevant
-    else
-        ref_j=1;%default j index
-    end
-    [filecell,i1,i2]=set_civ_filenames(handles,ref_i,ref_j,[1 0 0 0 0 0]);% get the corresponding file name and indices
-    Data.ListVarName={'ny','nx','A'};
-    Data.VarDimName= {'ny','nx',{'ny','nx'}};
-
-    Data.A=imread(filecell.ima1.civ1{1}); % read the first image
-    if ndims(Data.A)==3 %case of color image
-        Data.VarDimName= {'ny','nx',{'ny','nx','rgb'}};
-    end
-    Data.ny=[size(Data.A,1) 1];
-    Data.nx=[1 size(Data.A,2)];
-    Data.CoordUnit='pixel';% used to set equal scaling for x and y in image dispaly
-    par_civ1=read_GUI(handles.Civ1);
-    FileInfo=get_file_info(filecell.ima1.civ1{1});
-    par_civ1.FileTypeA=FileInfo.FileType;
-    par_civ1.ImageWidth=size(Data.A,2);
-    par_civ1.ImageHeight=size(Data.A,1);
-    par_civ1.Mask='all';% will provide only the grid set for PIV, no image correlation
-    par_civ1.FrameIndexA=num2str(i1);
-    par_civ1.FrameIndexB=num2str(i2);
-    Param.Civ1=par_civ1;
-    Grid=civ_matlab(Param);% get the grid of x, y positions set for PIV 
-    hview_field=view_field(Data); %view the image in the GUI view_field
-    set(0,'CurrentFigure',hview_field)
-    hhview_field=guihandles(hview_field);
-    set(hview_field,'CurrentAxes',hhview_field.PlotAxes)
-    ViewData=get(hview_field,'UserData');
-    ViewData.CivHandle=handles.civ;% indicate the handle of the civ GUI in view_field
-    ViewData.PlotAxes.B=imread(filecell.ima2.civ1{1});%store the second image in the UserData of the GUI view_field
-    ViewData.PlotAxes.X=Grid.Civ1_X; %keep the set of points in memeory
-    ViewData.PlotAxes.Y=Grid.Civ1_Y;
-    set(hview_field,'UserData',ViewData)
-    corrfig=findobj(allchild(0),'tag','corrfig');% look for a current figure for image correlation display
-    if isempty(corrfig)
-        corrfig=figure;
-        set(corrfig,'tag','corrfig')
-        set(corrfig,'name','image correlation')
-        set(corrfig,'DeleteFcn',{@closeview_field})%
-    end
-    set(handles.TestCiv1,'BackgroundColor',[1 0 0])
-else
-    set(handles.TestCiv1,'BackgroundColor',[1 0 0])% paint button to red
-    corrfig=findobj(allchild(0),'tag','corrfig');% look for a current figure for image correlation display
-    if ~isempty(corrfig)
-        delete(corrfig)
-    end
-    hview_field=findobj(allchild(0),'tag','view_field');% look for view_field    
-    if ~isempty(hview_field)
-        delete(hview_field)
-    end
-end
-
-%------------------------------------------------------------------------ 
-%----function introduced for the correlation window figure, activated by deleting this window
-function closeview_field(gcbo,eventdata)
-%------------------------------------------------------------------------
-hview_field=findobj(allchild(0),'tag','view_field');% look for view_field
-if ~isempty(hview_field)
-    delete(hview_field)
-end
-
-%------------------------------------------------------------------------
-% --- Executes on button press in CheckThreshold.
-function CheckThreshold_Callback(hObject, eventdata, handles)
-%------------------------------------------------------------------------
-huipanel=get(hObject,'parent');
-obj(1)=findobj(huipanel,'Tag','num_MinIma');
-obj(2)=findobj(huipanel,'Tag','num_MaxIma');
-obj(3)=findobj(huipanel,'Tag','title_Threshold');
-if get(hObject,'Value')
-    set(obj,'Visible','on')
-else
-    set(obj,'Visible','off')
-end
-
-
-
-
-%'nomtype2pair': creates nomencalture for index pairs knowing the image nomenclature
-%---------------------------------------------------------------------
-function NomTypeNc=nomtype2pair(NomTypeIma,mode)
-%---------------------------------------------------------------------           
-% OUTPUT:
-% NomTypeNc
-%---------------------------------------------------------------------
-% INPUT:
-% 'NomTypeIma': string defining the kind of nomenclature used for images
-
-NomTypeNc=NomTypeIma;%default
-switch mode
-    case 'pair j1-j2'      
-    if ~isempty(regexp(NomTypeIma,'a$'))
-        NomTypeNc=[NomTypeIma 'b'];
-    elseif ~isempty(regexp(NomTypeIma,'A$'))
-        NomTypeNc=[NomTypeIma 'B'];
-    else
-        r=regexp(NomTypeIma,'(?<num1>\d+)_(?<num2>\d+)$','names');
-        if ~isempty(r)
-            NomTypeNc='_1_1-2';
-        end
-    end
-    case 'series(Dj)'  
-%         r=regexp(NomTypeIma,'(?<num1>\d+)_(?<num2>\d+)$','names');
-%         if ~isempty(r)
-            NomTypeNc='_1_1-2';
-%         end
-   case 'series(Di)'
-        r=regexp(NomTypeIma,'(?<num1>\d+)_(?<num2>\d+)$','names');
-        if ~isempty(r)
-            NomTypeNc='_1-2_1';
-        else
-            NomTypeNc='_1-2';
-        end
-end
-
-function NomType_Callback(hObject, eventdata, handles)
-set(handles.RootPath,'BackgroundColor',[1 1 0])%paint RootName edit box in yellow to indicate that the file input is proceeding
-RootPath=get(handles.RootPath,'String');
-RootFile=get(handles.RootFile,'String');
-SubDirImages=get(handles.SubDirImages,'String');
-ref_i=str2num(get(handles.ref_i,'String'));
-ref_j=str2num(get(handles.ref_j,'String'));
-NomType=get(handles.NomType,'String');
-ImaExt=get(handles.ImaExt,'String');
-fileinput=fullfile_uvmat(RootPath,SubDirImages,RootFile,ImaExt,NomType,ref_i,[],ref_j);
-errormsg=display_file_name(handles,fileinput);
-if ~isempty(errormsg)
-    msgbox_uvmat('ERROR',errormsg)
-end
-set(handles.RootPath,'BackgroundColor',[1 1 1])%paint RootName back to white to indicate that the file input is finished
-
-% --- Executes on selection change in Program.
-function Program_Callback(hObject, eventdata, handles)
-ListProgram=get(handles.Program,'String');
-Program=ListProgram{get(handles.Program,'value')};
-switch Program
-    case 'CivX'
-        set(handles.num_MaxDiff,'Visible','off')
-        set(handles.num_MaxVel,'Visible','off')
-        set(handles.title_MaxVel,'Visible','off')
-        set(handles.num_Nx,'Visible','on')
-        set(handles.num_Ny,'Visible','on')
-        set(handles.title_Nx,'Visible','on')
-        set(handles.title_Ny,'Visible','on')
-        set(handles.title_MaxDiff,'Visible','off')
-        set(handles.num_CorrSmooth,'Style','edit')
-        set(handles.num_CorrSmooth,'String','1')
-        set(handles.BATCH,'Enable','on')
-        set(handles.CheckThreshold,'Visible','off')
-        set(handles.CheckDeformation,'Value',1)
-        set(handles.CheckDecimal,'Value',1)
-    case {'civ_matlab','civ_matlab.sh'}
-        set(handles.num_MaxDiff,'Visible','on')
-        set(handles.num_MaxVel,'Visible','on')
-        set(handles.title_MaxVel,'Visible','on')
-        set(handles.title_MaxDiff,'Visible','on')
-        set(handles.num_Nx,'Visible','off')
-        set(handles.num_Ny,'Visible','off')
-        set(handles.title_Nx,'Visible','off')
-        set(handles.title_Ny,'Visible','off')
-        set(handles.num_CorrSmooth,'Style','popupmenu')
-        set(handles.num_CorrSmooth,'Value',1)
-        set(handles.num_CorrSmooth,'String',{'1';'2'})
-        set(handles.CheckThreshold,'Visible','on')
-        set(handles.CheckDeformation,'Value',0)% desactivate (work in progress)
-        set(handles.CheckDecimal,'Value',0)% desactivate (work in progress)
-end
-
-% --- Executes on button press in TestPatch1.
-function TestPatch1_Callback(hObject, eventdata, handles)
-set(handles.TestPatch1,'BackgroundColor',[1 1 0])
-drawnow
-if get(handles.TestPatch1,'Value')
-    ref_i=str2double(get(handles.ref_i,'String'));
-    if strcmp(get(handles.ref_j,'Visible'),'on')
-        ref_j=str2double(get(handles.ref_j,'String'));
-    else
-        ref_j=1;%default
-    end
-    filecell=set_civ_filenames(handles,ref_i,ref_j,[0 0 1 0 0 0]);    
-    Data.ListVarName={'ny','nx','A'};
-    Data.VarDimName= {'ny','nx',{'ny','nx'}};   
-    param_patch1=read_GUI(handles.Patch1);
-    param_patch1.CivFile=filecell.nc.civ1{1};
-    Param.Patch1=param_patch1;
-    for irho=1:7
-        [Data,errormsg]=civ_matlab(Param);% get the grid of x, y positions set for PIV
-        if ~isempty(errormsg)
-            msgbox_uvmat('ERROR',errormsg)
-            return
-        end
-        SmoothingParam(irho)=Param.Patch1.FieldSmooth;
-        Data.Civ1_U_Diff=Data.Civ1_U_Diff(Data.Civ1_FF==0);
-        Data.Civ1_V_Diff=Data.Civ1_V_Diff(Data.Civ1_FF==0);
-        DiffVel(irho)=sqrt(mean(Data.Civ1_U_Diff.*Data.Civ1_U_Diff+Data.Civ1_V_Diff.*Data.Civ1_V_Diff))
-        NbSites(irho,:)=Data.Civ1_NbSites*numel(Data.Civ1_NbSites)/numel(Data.Civ1_U_Diff);
-        Param.Patch1.SmoothingParam=2*Param.Patch1.FieldSmooth;
-    end
-    figure
-    plot(SmoothingParam,DiffVel,'b',SmoothingParam,NbSites,'r')
-    set(handles.TestPatch1,'BackgroundColor',[1 0 0])
-else
-    corrfig=findobj(allchild(0),'tag','corrfig');% look for a current figure for image correlation display
-    if ~isempty(corrfig)
-        delete(corrfig)
-    end
-    hview_field=findobj(allchild(0),'tag','view_field');% look for view_field
-    if ~isempty(hview_field)
-        delete(hview_field)
-    end
-end
-
-
-% --- Executes on button press in TestCiv2.
-function TestCiv2_Callback(hObject, eventdata, handles)
-
-function RootFile_Callback(hObject, eventdata, handles)
-
-function SubDirImages_Callback(hObject, eventdata, handles)
-
-
-
-function errormsg=write_param(Param)
-%------------------------------------------------------------------------
-%pixels per cm and matrix of the image times, read from the .civ file by uvmat
-%changes : filename_cmx -> filename ( no extension )
-errormsg='';
-switch Param.Program
-    case 'CivX'
-        if Param.CheckCiv1
-            filename=regexprep(Param.OutputFile,'(.+)([/\\])(.+$)','$1$20_CMX$2$3.civ1.cmx');
-            if isequal(Param.Civ1.Dt,0)
-                Param.Civ1.Dt=1 ;%case of 'displacement' mode
-            end
-            Param.Civ1.ImageA=regexprep(Param.Civ1.ImageA,'.png','');
-            Param.Civ1.ImageB=regexprep(Param.Civ1.ImageB,'.png','');
-            [fid,errormsg]=fopen(filename,'w');
-            if isequal(fid,-1)
-                errormsg=['cmd file ' filename ' cannot be created: ' errormsg];
-                return
-            end
-            fprintf(fid,['##############   CMX file' '\n' ]);
-            fprintf(fid,   ['FirstImage ' regexprep(Param.Civ1.ImageA,'\\','\\\\') '\n' ]);% for windows compatibility
-            fprintf(fid,   ['LastImage  ' regexprep(Param.Civ1.ImageB,'\\','\\\\') '\n' ]);% for windows compatibility
-            fprintf(fid,  ['XX' '\n' ]);
-            if isfield(Param.Civ1,'Mask')
-                fprintf(fid,  ['Mask ' 'y' '\n' ]);
-                fprintf(fid,  ['MaskName ' regexprep(Param.Civ1.Mask,'\\','\\\\') '\n' ]);
-            else
-                fprintf(fid,  ['Mask ' 'n' '\n' ]);
-                fprintf(fid,  ['MaskName ' 'noFile use default' '\n' ]);
-            end
-            fprintf(fid,   ['ImageSize ' num2str(Param.Civ1.ImageWidth) ' ' num2str(Param.Civ1.ImageHeight) '\n' ]);   %VERIFIER CAS GENERAL ?
-            fprintf(fid,   ['CorrelationBoxesSize ' num2str(Param.Civ1.CorrBoxSize(1)) ' ' num2str(Param.Civ1.CorrBoxSize(2)) '\n' ]);
-            fprintf(fid,   ['SearchBoxeSize ' num2str(Param.Civ1.SearchBoxSize(1)) ' ' num2str(Param.Civ1.SearchBoxSize(2)) '\n' ]);
-            fprintf(fid,   ['RO ' num2str(Param.Civ1.CorrSmooth) '\n' ]);
-            if isfield(Param.Civ1,'Grid')
-                fprintf(fid,   ['GridSpacing ' '25' ' ' '25' '\n' ]);
-            else
-                fprintf(fid,   ['GridSpacing ' num2str(Param.Civ1.Dx) ' ' num2str(Param.Civ1.Dy) '\n' ]);
-            end
-            fprintf(fid,   ['XX 1.0' '\n' ]);
-            fprintf(fid,   ['Dt_TO ' num2str(Param.Civ1.Dt) ' ' num2str(Param.Civ1.Time) '\n' ]);
-            fprintf(fid,  ['PixCmXY ' '1' ' ' '1' '\n' ]);
-            fprintf(fid,  ['XX 1' '\n' ]);
-            fprintf(fid,   ['ShiftXY ' num2str(Param.Civ1.SearchBoxShift(1)) ' '  num2str(Param.Civ1.SearchBoxShift(2)) '\n' ]);
-            if isfield(Param.Civ1,'Grid')
-                fprintf(fid,  ['Grid ' 'y' '\n' ]);
-                fprintf(fid,   ['GridName ' regexprep(Param.Civ1.Grid,'\\','\\\\') '\n' ]);
-            else
-                fprintf(fid,  ['Grid ' 'n' '\n' ]);
-                fprintf(fid,   ['GridName ' 'noFile use default' '\n' ]);
-            end
-            fprintf(fid,   ['XX 85' '\n' ]);
-            fprintf(fid,   ['XX 1.0' '\n' ]);
-            fprintf(fid,   ['XX 1.0' '\n' ]);
-            fprintf(fid,   ['Hart 1' '\n' ]);
-            fprintf(fid,  [ 'DecimalShift 0' '\n' ]);
-            fprintf(fid,   ['Deformation 0' '\n' ]);
-            fprintf(fid,  ['CorrelationMin 0' '\n' ]);
-            fprintf(fid,   ['IntensityMin 0' '\n' ]);
-            if ~isfield(Param.Civ1,'MinIma')% Image threshold not activated
-                fprintf(fid,  ['SeuilImage n' '\n' ]);
-                fprintf(fid,   ['SeuilImageValues 0 4096' '\n' ]);%not used in principle
-            else% Image threshold  activated
-                if isempty(Param.Civ1.MaxIma)||isnan(Param.Civ1.MaxIma)
-                    Param.Civ1.MaxIma=2^Param.Civ1.ImageBitDepth;%take the max image value as upper bound by default
-                end
-                fprintf(fid,  ['SeuilImage y' '\n' ]);
-                fprintf(fid,   ['SeuilImageValues ' num2str(Param.Civ1.MinIma) ' ' num2str(Param.Civ1.MaxIma) '\n' ]);
-            end
-            fprintf(fid,   ['ImageToUse ' Param.Civ1.term_a ' ' Param.Civ1.term_b '\n' ]); % VERIFIER ?
-            fprintf(fid,   ['ImageUsedBefore null null' '\n' ]);
-            fclose(fid);
-        end
-        
-        if Param.CheckCiv2
-            filename=regexprep(Param.OutputFile,'(.+)([/\\])(.+$)','$1$20_CMX$2$3.civ2.cmx');
-
-            if isequal(Param.Civ2.Dt,'0')
-                Param.Civ2.Dt='1' ;%case of 'displacement' mode
-            end
-            Param.Civ2.ImageA=regexprep(Param.Civ2.ImageA,'.png','');
-            Param.Civ2.ImageB=regexprep(Param.Civ2.ImageB,'.png','');% bug : .png appears two times ?
-            [fid,errormsg]=fopen(filename,'w');
-            if isequal(fid,-1)
-                return
-            end
-            fprintf(fid,['##############   CMX file' '\n' ]);
-            fprintf(fid,   ['FirstImage ' regexprep(Param.Civ2.ImageA,'\\','\\\\') '\n' ]);% for windows compatibility
-            fprintf(fid,   ['LastImage  ' regexprep(Param.Civ2.ImageB,'\\','\\\\') '\n' ]);% for windows compatibility
-            fprintf(fid,  ['XX' '\n' ]);
-            if isfield(Param.Civ2,'Mask')
-                fprintf(fid,  ['Mask ' 'y' '\n' ]);
-                fprintf(fid,  ['MaskName ' regexprep(Param.Civ2.Mask,'\\','\\\\') '\n' ]);
-            else
-                fprintf(fid,  ['Mask ' 'n' '\n' ]);
-                fprintf(fid,  ['MaskName ' 'noFile use default' '\n' ]);
-            end
-            % fprintf(fid, ['Mask ' Param.Civ2.MaskFlag '\n' ]);
-            % fprintf(fid, ['MaskName ' regexprep(Param.Civ2.MaskName,'\\','\\\\') '\n' ]);% for windows compatibility
-            fprintf(fid,   ['ImageSize ' num2str(Param.Civ2.ImageWidth) ' ' num2str(Param.Civ2.ImageHeight) '\n' ]);
-            % fprintf(fid, ['ImageSize ' num2str(Param.Civ2.npx) ' ' num2str(Param.Civ2.npy) '\n' ]);   %VERIFIER CAS GENERAL ?
-            fprintf(fid, ['CorrelationBoxesSize ' num2str(Param.Civ2.CorrBoxSize(1)) ' ' num2str(Param.Civ2.CorrBoxSize(2)) '\n' ]);
-            fprintf(fid, ['SearchBoxeSize ' num2str(Param.Civ2.CorrBoxSize(1)) ' ' num2str(Param.Civ2.CorrBoxSize(2)) '\n']);
-            fprintf(fid, ['RO ' num2str(Param.Civ2.CorrSmooth) '\n']);
-            if isfield(Param.Civ2,'Grid')
-                fprintf(fid,   ['GridSpacing ' '25' ' ' '25' '\n' ]);
-            else
-                fprintf(fid,   ['GridSpacing ' num2str(Param.Civ2.Dx) ' ' num2str(Param.Civ2.Dy) '\n' ]);
-            end
-            % fprintf(fid, ['GridSpacing ' num2str(Param.Civ2.Dx) ' ' num2str(Param.Civ2.Dy) '\n']);
-            fprintf(fid, ['XX 1.0' '\n' ]);
-            fprintf(fid, ['Dt_TO ' num2str(Param.Civ2.Dt) ' ' num2str(Param.Civ2.Time) '\n' ]);
-            fprintf(fid, ['PixCmXY ' '1' ' ' '1' '\n' ]);
-            fprintf(fid, ['XX 1' '\n' ]);
-            fprintf(fid, 'ShiftXY 0 0\n');
-            if isfield(Param.Civ2,'Grid')
-                fprintf(fid,  ['Grid ' 'y' '\n' ]);
-                fprintf(fid,   ['GridName ' regexprep(Param.Civ2.Grid,'\\','\\\\') '\n' ]);
-            else
-                fprintf(fid,  ['Grid ' 'n' '\n' ]);
-                fprintf(fid,   ['GridName ' 'noFile use default' '\n' ]);
-            end
-            % fprintf(fid, ['Grid ' Param.Civ2.GridFlag '\n' ]);
-            % fprintf(fid, ['GridName ' regexprep(Param.Civ2.GridName,'\\','\\\\') '\n']);
-            fprintf(fid, ['XX 85' '\n' ]);
-            fprintf(fid, ['XX 1.0' '\n' ]);
-            fprintf(fid, ['XX 1.0' '\n' ]);
-            fprintf(fid, ['Hart 1' '\n' ]);
-            fprintf(fid, ['DecimalShift ' num2str(Param.Civ2.CheckDecimal) '\n']);
-            fprintf(fid, ['Deformation ' num2str(Param.Civ2.CheckDeformation) '\n']);
-            fprintf(fid,  ['CorrelationMin 0' '\n' ]);
-            fprintf(fid,   ['IntensityMin 0' '\n' ]);
-            
-            if ~isfield(Param.Civ2,'MinIma')% Image threshold not activated
-                fprintf(fid,  ['SeuilImage n' '\n' ]);
-                fprintf(fid,   ['SeuilImageValues 0 4096' '\n' ]);%not used in principle
-            else% Image threshold  activated
-                if isempty(Param.Civ2.MaxIma)||isnan(Param.Civ2.MaxIma)
-                    Param.Civ2.MaxIma=2^Param.Civ2.ImageBitDepth;%take the max image value as upper bound by default
-                end
-                fprintf(fid,  ['SeuilImage y' '\n' ]);
-                fprintf(fid,   ['SeuilImageValues ' num2str(Param.Civ2.MinIma) ' ' num2str(Param.Civ2.MaxIma) '\n' ]);
-            end
-            fprintf(fid,   ['ImageToUse ' Param.Civ2.term_a ' ' Param.Civ2.term_b '\n' ]); % VERIFIER ?
-            fprintf(fid, ['ImageUsedBefore ' regexprep(Param.Civ2.filename_nc1,'\\','\\\\') '\n']);
-            fclose(fid);
-        end
-    case {'civ_matlab','civ_matlab.sh'}
-        filename=regexprep(Param.OutputFile,'(.+)([/\\])(.+$)','$1$20_XML$2$3.xml');
-        if isunix
-        fileattrib(fileparts(filename),'+w +x','o g');% set writting access
-        end
-        save(struct2xml(Param),filename);
-end
-
-
-function cmd=write_cmd(Param)
-
-% initiate system command
-cmd=[];
-
-switch Param.Program
-    case 'CivX'
-        if isunix % check: necessaire aussi en RUN?
-            cmd=[cmd '#!/bin/bash \n'...
-                '#$ -cwd \n'...
-                'hostname && date \n'...
-                'umask 002 \n'];%allow writting access to created files for user group
-        end
-    case 'CivAll'
-        if isunix % check: necessaire aussi en RUN?
-            cmd=[cmd '#!/bin/bash \n'...
-                '#$ -cwd \n'...
-                'hostname && date \n'...
-                'umask 002 \n'];%allow writting access to created files for user group
-        end
-end
-
-filename=regexprep(Param.OutputFile,'.nc','');
-
-if Param.CheckCiv1
-    switch Param.Program
-        case 'CivX'
-            if(isunix) %unix (or Mac) system
-                cmd=[cmd 'cp -f ' regexprep(filename,'(.+)/(.+$)','$1/0_CMX/$2.civ1.cmx ') regexprep(filename,'(.+)/(.+$)','$1/$2.cmx \n')...% the cmx file gives the name to the nc file
-                    Param.xml.Civ1Bin ' -f ' regexprep(filename,'(.+)/(.+$)','$1/$2.cmx >') regexprep(filename,'(.+)/(.+$)','$1/0_LOG/$2.civ1.log \n')... % redirect standard output to the log file, the result file is named [filename '.nc'] by CIVx
-                    'rm ' regexprep(filename,'(.+)/(.+$)','$1/$2.cmx \n')];
-            else %Windows system
-                filename=regexprep(filename,'\\','\\\\');
-                cmd=['copy /Y ' regexprep(filename,'(.+)\\\\(.+$)','"$1\\\\0_CMX\\\\$2.civ1.cmx" ') regexprep(filename,'(.+)\\\\(.+$)','"$1\\\\$2.cmx" \n')...
-                    '"' regexprep(Param.xml.Civ1Bin,'\\','\\\\') '" -f ' regexprep(filename,'(.+)\\\\(.+$)','"$1\\\\$2.cmx" > ')...
-                    regexprep(filename,'(.+)\\\\(.+$)','"$1\\\\0_LOG\\\\$2.civ1.log" \n')... % redirect standard output to the log file
-                    'del ' regexprep(filename,'(.+)\\\\(.+$)','"$1\\\\$2.cmx" \n')];
-            end
-        case 'CivAll'
-            CivAllCmd=[CivAllCmd ' civ1 '];
-            str=CIV1_CMD_Unified(filecell.nc.civ1{ifile,j},'',Param.Civ1);
-            fieldnames=fields(str);
-            [CivAllxml,uid_civ1]=add(CivAllxml,1,'element','civ1');
-            for ilist=1:length(fieldnames)
-                val=eval(['str.' fieldnames{ilist}]);
-                if ischar(val)
-                    [CivAllxml,uid_t]=add(CivAllxml,uid_civ1,'element',fieldnames{ilist});
-                    [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
-                end
-            end
-    end
-end
-
-if Param.CheckFix1
-    switch Param.Program
-        case 'CivX'
-            cmd=[cmd...
-                cmd_fix(Param,'Fix1') '\n'];
-        case 'CivAll'%to abandon
-            fix1.inputFileName=filecell.nc.civ1{ifile,j} ;
-            fix1.fi1=num2str(param.fix1.flagindex1(1));
-            fix1.fi2=num2str(param.fix1.flagindex1(2));
-            fix1.fi3=num2str(param.fix1.flagindex1(3));
-            fix1.threshC=num2str(param.fix1.thresh_vecC1);
-            fix1.threshV=num2str(param.fix1.thresh_vel1);
-            fieldnames=fields(fix1);
-            [CivAllxml,uid_fix1]=add(CivAllxml,1,'element','fix1');
-            for ilist=1:length(fieldnames)
-                val=eval(['fix1.' fieldnames{ilist}]);
-                if ischar(val)
-                    [CivAllxml,uid_t]=add(CivAllxml,uid_fix1,'element',fieldnames{ilist});
-                    [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
-                end
-            end
-            CivAllCmd=[CivAllCmd ' fix1 '];
-    end
-end
-
-
-%CheckPatch1
-if Param.CheckPatch1
-    switch Param.Program
-        case 'CivX'
-            cmd=[cmd...
-                cmd_patch(Param,'Patch1') '\n'];
-        case 'CivAll'
-            patch1.inputFileName=filecell.nc.civ1{ifile,j} ;
-            patch1.nopt=subdomain_patch1;
-            patch1.maxdiff=thresh_patch1;
-            patch1.ro=rho_patch1;
-            test_grid=get(handles.get_gridpatch1,'Value');
-            if test_grid
-                patch1.gridflag='y';
-                gridname=get(handles.grid_patch1,'String');
-                if isequal(gridname(end-3:end),'grid')
-                    nbslice_grid=str2double(gridname(1:end-4)); %
-                    if ~isnan(nbslice_grid)
-                        i1_grid=mod(i1_civ1(ifile)-1,nbslice_grid)+1;
-                        patch1.gridPatch=[filecell.filebase '_' fullfile_uvmat('','',gridname,'.grid','_1',i1_grid)];
-                        %                                 patch1.gridPatch=[filecell.filebase '_' name_generator(gridname,i1_grid,1,'.grid','_i')];
-                        if ~exist(patch1.gridPatch,'file')
-                            errormsg='grid file absent for patch1';
-                            return
-                        end
-                    elseif exist(gridname,'file')
-                        patch1.gridPatch=gridname;
-                    else
-                        errormsg='grid file absent for patch1';
-                        return
-                    end
-                end
-            else
-                patch1.gridPatch='none';
-                patch1.gridflag='n';
-                patch1.m=nx_patch1;
-                patch1.n=ny_patch1;
-            end
-            patch1.convectFlow='n';
-            fieldnames=fields(patch1);
-            [CivAllxml,uid_patch1]=add(CivAllxml,1,'element','patch1');
-            for ilist=1:length(fieldnames)
-                val=eval(['patch1.' fieldnames{ilist}]);
-                if ischar(val)
-                    [CivAllxml,uid_t]=add(CivAllxml,uid_patch1,'element',fieldnames{ilist});
-                    [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
-                end
-            end
-            CivAllCmd=[CivAllCmd ' patch1 '];
-    end
-end
-
-if Param.CheckCiv2
-    switch Param.Program
-        case 'CivX'
-            if(isunix)
-                cmd=[cmd 'cp -f '  regexprep(filename,'(.+)/(.+$)','$1/0_CMX/$2.civ2.cmx ') regexprep(filename,'(.+)/(.+$)','$1/$2.cmx \n')...
-                    Param.xml.Civ2Bin ' -f ' regexprep(filename,'(.+)/(.+$)','$1/$2.cmx >') regexprep(filename,'(.+)/(.+$)','$1/0_LOG/$2.civ2.log \n')...% redirect standard output to the log file, the result file is named [filename '.nc'] by CIVx
-                    'rm ' regexprep(filename,'(.+)/(.+$)','$1/$2.cmx \n')];%rename .cmx as .checkciv2.cmx, the result file is named [filename '.nc'] by CIVx
-            else
-                filename=regexprep(Param.OutputFile,'.nc','');
-                filename=regexprep(filename,'\\','\\\\');
-                cmd=[cmd 'copy /Y ' regexprep(filename,'(.+)\\\\(.+$)','"$1\\\\0_CMX\\\\$2.civ2.cmx" ') regexprep(filename,'(.+)\\\\(.+$)','"$1\\\\$2.cmx" \n')...
-                    '"' regexprep(Param.xml.Civ2Bin,'\\','\\\\') '" -f ' regexprep(filename,'(.+)\\\\(.+$)','"$1\\\\$2.cmx" > ')...
-                     regexprep(filename,'(.+)\\\\(.+$)','"$1\\\\0_LOG\\\\$2.civ2.log" \n')... % redirect standard output to the log file
-                    'del ' regexprep(filename,'(.+)\\\\(.+$)','"$1\\\\$2.cmx" \n')];                       
-            end
-                 
-        case 'CivAll'
-            CivAllCmd=[CivAllCmd ' civ2 '];
-            str=CIV2_CMD_Unified(filecell.nc.civ2{ifile,j},'',Param.Civ2);
-            fieldnames=fields(str);
-            [CivAllxml,uid_civ2]=add(CivAllxml,1,'element','civ2');
-            for ilist=1:length(fieldnames)
-                val=eval(['str.' fieldnames{ilist}]);
-                if ischar(val)
-                    [CivAllxml,uid_t]=add(CivAllxml,uid_civ2,'element',fieldnames{ilist});
-                    [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
-                end
-            end
-    end
-end
-
-% CheckFix2
-if Param.CheckFix2==1
-    switch Param.Program
-        case 'CivX'
-            cmd=[cmd...
-                cmd_fix(Param,'Fix2') '\n'];
-        case 'CivAll'
-            fix2.inputFileName=filecell.nc.civ2{ifile,j} ;
-            fix2.fi1=num2str(flagindex2(1));
-            fix2.fi2=num2str(flagindex2(2));
-            fix2.fi3=num2str(flagindex2(3));
-            fix2.threshC=num2str(thresh_vec2C);
-            fix2.threshV=num2str(thresh_vel2);
-            fieldnames=fields(fix2);
-            [CivAllxml,uid_fix2]=add(CivAllxml,1,'element','fix2');
-            for ilist=1:length(fieldnames)
-                val=eval(['fix2.' fieldnames{ilist}]);
-                if ischar(val)
-                    [CivAllxml,uid_t]=add(CivAllxml,uid_fix2,'element',fieldnames{ilist});
-                    [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
-                end
-            end
-            CivAllCmd=[CivAllCmd ' fix2 '];
-    end
-end
-
-%CheckPatch2
-if Param.CheckPatch2==1
-    
-    switch Param.Program
-        
-        case 'CivX'
-            cmd=[cmd...
-                cmd_patch(Param,'Patch2') '\n'];
-        case 'CivAll'
-            patch2.inputFileName=filecell.nc.civ1{ifile,j} ;
-            patch2.nopt=subdomain_patch2;
-            patch2.maxdiff=thresh_patch2;
-            patch2.ro=rho_patch2;
-            test_grid=get(handles.get_gridpatch2,'Value');
-            if test_grid
-                patch2.gridflag='y';
-                gridname=get(handles.grid_patch2,'String');
-                if isequal(gridname(end-3:end),'grid')
-                    nbslice_grid=str2double(gridname(1:end-4)); %
-                    if ~isnan(nbslice_grid)
-                        i1_grid=mod(i1_civ2(ifile)-1,nbslice_grid)+1;
-                        patch2.gridPatch=[filecell.filebase '_' fullfile_uvmat('','',gridname,'.grid','_1',i1_grid)];
-                        %                                 patch2.gridPatch=[filecell.filebase '_' name_generator(gridname,i1_grid,1,'.grid','_i')];
-                        if ~exist(patch2.gridPatch,'file')
-                            errormsg='grid file absent for patch2';
-                            return
-                        end
-                    elseif exist(gridname,'file')
-                        patch2.gridPatch=gridname;
-                    else
-                        errormsg='grid file absent for patch2';
-                        return
-                    end
-                end
-            else
-                patch2.gridPatch='none';
-                patch2.gridflag='n';
-                patch2.m=nx_patch2;
-                patch2.n=ny_patch2;
-            end
-            patch2.convectFlow='n';
-            fieldnames=fields(patch2);
-            [CivAllxml,uid_patch2]=add(CivAllxml,1,'element','patch2');
-            for ilist=1:length(fieldnames)
-                val=eval(['patch2.' fieldnames{ilist}]);
-                if ischar(val)
-                    [CivAllxml,uid_t]=add(CivAllxml,uid_patch2,'element',fieldnames{ilist});
-                    [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
-                end
-            end
-            CivAllCmd=[CivAllCmd ' patch2 '];
-    end
-end
-
-switch Param.Program
-    case 'CivAll'
-        save(CivAllxml,[Param.OutputFile '.xml']);
-        cmd=[cmd sparam.CivBin ' -f ' Param.OutputFile '.xml '  CivAllCmd ' >' Param.OutputFile '.log' '\n'];
-    case 'civ_matlab'
-                    switch computer
-                        case {'PCWIN','PCWIN64'}                     
-                            filename=regexprep(filename,'\\','\\\\');% add '\' so that '\' are left as characters
-                                    cmd=['civ_matlab(''' regexprep(filename,'(.+)([/\\])(.+$)','$1$20_XML\\$2$3.xml') ''','''...
-            filename '.nc'');'];
-                        case {'GLNX86','GLNXA64','MACI64'}
-                                    cmd=['civ_matlab(''' regexprep(filename,'(.+)([/\\])(.+$)','$1$20_XML$2$3.xml') ''','''...
-            filename '.nc'');'];
-                    end
-
-        
-    case 'civ_matlab.sh'
-        CivmBin=fullfile(fileparts(which('civ')),'civ_matlab.sh'); %path to the source directory of uvmat
-        switch computer
-            case {'PCWIN','PCWIN64'}
-                filename=regexprep(filename,'\\','\\\\');% add '\' so that '\' are left as characters
-                % TODO launch command in DOS
-            case {'GLNX86','GLNXA64','MACI64'}
-                cmd=['#!/bin/bash \n '...
-                    '#$ -cwd \n '...
-                    'hostname && date \n '...
-                    'umask 002 \n'...
-                    CivmBin ' ' Param.xml.RunTime ' ' regexprep(filename,'(.+)([/\\])(.+$)','$1$20_XML$2$3.xml') ' ' Param.OutputFile '.nc'];%allow writting access to created files for user group
-        end
-end    
-    
-
-function cmd=cmd_fix(Param,fixname)
-%%
-switch fixname
-    case 'Fix1'
-        fi2_value=num2str(Param.(fixname).CheckF2);
-        filename=regexprep(Param.OutputFile,'.nc','');
-    case 'Fix2'
-        fi2_value=num2str(Param.(fixname).CheckF4);%need to understand why...
-        filename=regexprep(Param.OutputFile,'.nc','');        
-end
-
-% filename=regexprep(Param.(fixname).OutFileName,'.nc','');
-MaskName_string='';%default
-MaxVel_string='';%default
-if ~isempty(Param.(fixname).MinVel)
-    MaxVel_string=[' -threshV ' num2str(Param.(fixname).MinVel)];
-end
-if isunix
-    cmd=[Param.xml.FixBin ' -f ' filename '.nc -fi1 ' num2str(Param.(fixname).CheckFmin2) ...
-        ' -fi2 ' fi2_value ' -fi3 ' num2str(Param.(fixname).CheckF3) ...
-        ' -threshC ' num2str(Param.(fixname).MinCorr) MaxVel_string MaskName_string...
-        ' >' regexprep(filename,'(.+)/(.+$)','$1/0_LOG/$2.')  lower(fixname) '.log 2>&1'];
-else
-    cmd=['"' Param.xml.FixBin '" -f "' filename '.nc" -fi1 ' num2str(Param.(fixname).CheckFmin2)...
-        ' -fi2 ' fi2_value ' -fi3 ' num2str(Param.(fixname).CheckF3) ...
-        ' -threshC ' num2str(Param.(fixname).MinCorr) MaxVel_string MaskName_string...
-        ' > "' regexprep(filename,'(\w+)\\(\w+$)','$1\\0_LOG\\$2.') lower(fixname) '.log"'];
-    cmd=regexprep(cmd,'\\','\\\\');
-end
-
-
-function cmd=cmd_patch(Param,patchname)
-%% ------------------------------------------------------------------------
-switch patchname
-    case 'Patch1'
-        filename=regexprep(Param.OutputFile,'.nc','');
-    case 'Patch2'
-        filename=regexprep(Param.OutputFile,'.nc','');        
-end
-% filename=regexprep(Param.(patchname).OutFileName,'.nc','');
-if isunix
-    cmd=[Param.xml.PatchBin...
-        ' -f ' filename '.nc -m ' num2str(Param.(patchname).Nx)...
-        ' -n ' num2str(Param.(patchname).Ny) ' -ro ' num2str(Param.(patchname).FieldSmooth)...
-        ' -nopt ' num2str(Param.(patchname).SubDomainSize) ...
-        '  > ' regexprep(filename,'(.+)/(.+$)','$1/0_LOG/$2.')  lower(patchname) '.log 2>&1']; % redirect standard output to the log file
-else
-    cmd=['"' Param.xml.PatchBin...
-        '" -f "' filename '.nc" -m ' num2str(Param.(patchname).Nx)...
-        ' -n ' num2str(Param.(patchname).Ny) ' -ro ' num2str(Param.(patchname).FieldSmooth)...
-        ' -nopt ' num2str(Param.(patchname).SubDomainSize)...
-        '  > "' filename '.' lower(patchname) '.log" 2>&1']; % redirect standard output to the log file
-    cmd=regexprep(cmd,'\\','\\\\');
-end
-
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% USELESS FUNCTIONS BELOW HERE,  TO CLEAN
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
-%------------------------------------------------------------------------
-% --- CheckCiv1  Unified: TO ABADON
-function xml_civ1_parameters=CIV1_CMD_Unified(filename,namelog,par)
-%------------------------------------------------------------------------
-%pixels per cm and matrix of the image times, read from the .civ file by uvmat
-%global CivBin%name of the executable for checkciv1 calculation
-
-civ1.image1=par.ImageA;
-civ1.image2=par.ImageB;
-civ1.imageSize_X=par.npx;
-civ1.imageSize_Y=par.npy;
-civ1.outputFileName=[filename '.nc'];
-civ1.correlationBoxesSize_X=par.ibx;
-civ1.correlationBoxesSize_Y=par.iby;
-civ1.searchBoxesSize_X=par.isx;
-civ1.searchBoxesSize_Y=par.isy;
-civ1.globalShift_X=par.shiftx;
-civ1.globalShift_Y=par.shifty;
-civ1.ro=par.rho;
-civ1.hart='y';
-if isequal(par.gridflag,'y')
-    civ1.grid=par.gridname;
-else
-    civ1.grid='n';
-    civ1.gridSpacing_X=par.dx;
-    civ1.gridSpacing_Y=par.dy;
-end
-if isequal(par.maskflag,'y')
-    civ1.mask=par.maskname;
-end
-civ1.dt=par.Dt;
-civ1.unit='pixel';
-civ1.absolut_time_T0=par.Time;
-civ1.pixcmx='1';
-civ1.pixcmy='1';
-civ1.convectFlow='n';
-
-xml_civ1_parameters=civ1;
-
-%------------------------------------------------------------------------
-% --- CheckCiv2  Unified: TO ABADON
-function civ2=CIV2_CMD_Unified(filename,namelog,par)
-%------------------------------------------------------------------------
-%pixels per cm and matrix of the image times, read from the .civ file by uvmat
-%global CivBin%name of the executable for checkciv1 calculation
-
-filename=regexprep(filename,'.nc','');
-
-civ2.image1=par.ImageA;
-civ2.image2=par.ImageB;
-civ2.imageSize_X=par.npx;
-civ2.imageSize_Y=par.npy;
-civ2.inputFileName=[par.filename_nc1 '.nc'];
-civ2.outputFileName=[filename '.nc'];
-civ2.correlationBoxesSize_X=par.ibx;
-civ2.correlationBoxesSize_Y=par.iby;
-civ2.ro=par.rho;
-%checkciv2.decimalShift=par.CheckDecimal;
-%checkciv2.CheckDeformation=par.CheckDeformation;
-if isequal(par.decimal,'1')
-    civ2.decimalShift='y';
-else
-    civ2.decimalShift='n';
-end
-if isequal(par.deformation,'1')
-    civ2.deformation='y';
-else
-    civ2.deformation='n';
-end
-if isequal(par.gridflag,'y')
-    civ2.grid=par.gridname;
-else
-    civ2.grid='n';
-    civ2.gridSpacing_X=par.dx;
-    civ2.gridSpacing_Y=par.dy;
-end
-civ2.gridSpacing_X='10';
-civ2.gridSpacing_Y='10';%NOTE: faut mettre gridSpacing pourque ca tourne, meme si c'est la grille qui est utilisee
-if isequal(par.maskflag,'y')
-    civ2.mask=par.maskname;
-else
-    civ2.mask='n';
-end
-civ2.dt=par.Dt;
-civ2.unit='pixel';
-civ2.absolut_time_T0=par.Time;
-civ2.pixcmx='1';
-civ2.pixcmy='1';
-civ2.convectFlow='n';
-
-% 'open_uvmat': open with uvmat the  field selected in the list of 'civ/status' 
-%------------------------------------------------------------------------
-%function open_uvmat(hObject, eventdata)
-%
-% INPUT: 
-% hObject: handle of uicontrol object containing the list 
-% eventdata: not used
-function open_uvmat(hObject, eventdata)
-%------------------------------------------------------------------------
-list=get(hObject,'String');
-index=get(hObject,'Value');
-rootroot=get(hObject,'UserData');
-filename=list{index};
-ind_dot=strfind(filename,'...');
-if ~isempty(ind_dot)
-    filename=filename(1:ind_dot-1);
-end
-filename=fullfile(rootroot,filename);
-if exist(filename,'file')%visualise the vel field if it exists
-    uvmat(filename)
-    set(gcbo,'Value',1)
-end
-
-% 
-% % --- Executes on selection change in RunMode.
-% function RunMode_Callback(hObject, eventdata, handles)
-% 
-% 
-% function nb_field2_Callback(hObject, eventdata, handles)
-% 
-% 
-% function last_j_Callback(hObject, eventdata, handles)
-% 
-% 
-% function last_i_Callback(hObject, eventdata, handles)
Index: /trunk/src/get_file_info.m
===================================================================
--- /trunk/src/get_file_info.m	(revision 1088)
+++ /trunk/src/get_file_info.m	(revision 1089)
@@ -7,4 +7,5 @@
 %      .FileType: type of file, needed as input of read_field.m
 %               ='figure': Matlab figure
+%               ='mat': Matlab data file
 %               ='xml': xml file
 %               ='xls': Excel file
@@ -191,5 +192,5 @@
 end
 
-if ismember (FileInfo.FileType,{'image','image_DaVis','multimage','mmreader','cine_phantom','video','netcdf','civdata'})
+if ismember (FileInfo.FileType,{'mat','image','image_DaVis','multimage','mmreader','cine_phantom','video','netcdf','civdata'})
         FileInfo.FileIndexing='on'; % allow to detect file index for scanning series
 else
Index: /trunk/src/series/civ_3D.m
===================================================================
--- /trunk/src/series/civ_3D.m	(revision 1089)
+++ /trunk/src/series/civ_3D.m	(revision 1089)
@@ -0,0 +1,1412 @@
+%'civ_3D': PIV function activated by the general GUI series
+% --- call the sub-functions:
+%   civ: PIV function itself
+%   fix: removes false vectors after detection by various criteria
+%   filter_tps: make interpolation-smoothing 
+%------------------------------------------------------------------------
+% function [Data,errormsg,result_conv]= civ_series(Param)
+%
+%OUTPUT
+% Data=structure containing the PIV results and information on the processing parameters
+% errormsg=error message char string, decd ..fault=''
+% resul_conv: image inter-correlation function for the last grid point (used for tests)
+%
+%INPUT:
+% Param: Matlab structure of input  parameters
+%     Param contains info of the GUI series using the fct read_GUI.
+%     Param.Action.RUN = 0 (to set the status of the GUI series) or =1 to RUN the computation 
+%     Param.InputTable: sets the input file(s)
+%           if absent, the fct looks for input data in Param.ActionInput     (test mode)
+%     Param.OutputSubDir: sets the folder name of output file(s,
+%           if absent no file is produced, result in the output structure Data (test mode)
+%     Param.ActionInput: substructure with the parameters provided by the GUI civ_input
+%                      .Civ1: parameters for civ1
+%                      .Fix1: parameters for fix1
+%                      .Patch1:
+%                      .Civ2: for civ2
+%                      .Fix2:
+%                      .Patch2:
+
+%=======================================================================
+% Copyright 2008-2020, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
+%   http://www.legi.grenoble-inp.fr
+%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
+%
+%     This file is part of the toolbox UVMAT.
+%
+%     UVMAT is free software; you can redistribute it and/or modify
+%     it under the terms of the GNU General Public License as published
+%     by the Free Software Foundation; either version 2 of the license,
+%     or (at your option) any later version.
+%
+%     UVMAT is distributed in the hope that it will be useful,
+%     but WITHOUT ANY WARRANTY; without even the implied warranty of
+%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+%     GNU General Public License (see LICENSE.txt) for more details.
+%=======================================================================
+
+function [Data,errormsg,result_conv]= civ_3D(Param)
+errormsg='';
+
+%% set the input elements needed on the GUI series when the action is selected in the menu ActionName or InputTable refreshed
+if isstruct(Param) && isequal(Param.Action.RUN,0)% function activated from the GUI series but not RUN
+    path_series=fileparts(which('series'));
+    addpath(fullfile(path_series,'series'))
+    Data=civ_input_3D(Param);% introduce the civ parameters using the GUI civ_input
+    %Data=civ_input_App(Param);% introduce the civ parameters using the GUI
+    %civ_input %%%%  TO MOVE
+    if isempty(Data)
+        Data=Param;% if  civ_input has been cancelled, keep previous parameters
+    end
+    Data.Program=mfilename;%gives the name of the current function
+    Data.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default)
+    Data.WholeIndexRange='off';% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default)
+    Data.NbSlice='off'; %nbre of slices ('off' by default)
+    Data.VelType='off';% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
+    Data.FieldName='on';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
+    Data.FieldTransform = 'off';%can use a transform function
+    Data.ProjObject='off';%can use projection object(option 'off'/'on',
+    Data.Mask='off';%can use mask option   (option 'off'/'on', 'off' by default)
+    Data.OutputDirExt='.civ';%set the output dir extension
+    Data.OutputSubDirMode='last'; %select the last subDir in the input table as root of the output subdir name (option 'all'/'first'/'last', 'all' by default)
+    Data.OutputFileMode='NbInput_i';% one output file expected per value of i index (used for waitbar)
+    Data.CheckOverwriteVisible='on'; % manage the overwrite of existing files (default=1)
+    if isfield(Data.ActionInput,'PairIndices') && strcmp(Data.ActionInput.PairIndices.ListPairMode,'pair j1-j2')
+        if isfield(Data.ActionInput.PairIndices,'ListPairCiv2')
+            str_civ=Data.ActionInput.PairIndices.ListPairCiv2;
+        else
+            str_civ=Data.ActionInput.PairIndices.ListPairCiv1;
+        end
+        r=regexp(str_civ,'^j= (?<num1>[a-z])-(?<num2>[a-z])','names');
+        if isempty(r)
+            r=regexp(str_civ,'^j= (?<num1>[A-Z])-(?<num2>[A-Z])','names');
+            if isempty(r)
+                r=regexp(str_civ,'^j= (?<num1>\d+)-(?<num2>\d+)','names');
+            end
+        end
+        if ~isempty(r)
+            Data.j_index_1=stra2num(r.num1);
+            Data.j_index_2=stra2num(r.num2);
+        end
+    end
+    % estimated CPUTime
+    CPUtime_unit=0.01;%estimated time for a multiplication (in microsecond)
+    if isfield(Param.SeriesData,'FileInfo')&&isfield(Param.SeriesData.FileInfo{1},'Height')&&isfield(Param.SeriesData.FileInfo{1},'Width')
+        pixnbre=Param.SeriesData.FileInfo{1}.Height*Param.SeriesData.FileInfo{1}.Width; % total number of pxels for input images  
+        CPUtime=0;
+        if isfield(Data.ActionInput,'Civ1')
+            %BoxSize=Data.ActionInput.Civ1.CorrBoxSize(1)*Data.ActionInput.Civ1.CorrBoxSize(2);
+            tic
+            testboxa=rand(Data.ActionInput.Civ1.CorrBoxSize(1),Data.ActionInput.Civ1.CorrBoxSize(2));
+            testboxb=rand(Data.ActionInput.Civ1.SearchBoxSize(1),Data.ActionInput.Civ1.SearchBoxSize(2));
+            anss=conv2(testboxa,testboxb);
+            CPUtime_unit=toc;
+            nb_box=pixnbre/(Data.ActionInput.Civ1.Dx*Data.ActionInput.Civ1.Dy);    
+            %nbpos=Data.ActionInput.Civ1.SearchBoxSize-Data.ActionInput.Civ1.CorrBoxSize;
+            CPUtime=2*CPUtime_unit*nb_box%*BoxSize*nbpos(1)*nbpos(2);% adjustement factor 2 used
+        end
+        if isfield(Data.ActionInput,'Patch1')
+            CPUtime=2*CPUtime;
+        end
+        if isfield(Data.ActionInput,'Civ2')
+            tic
+            testboxa=rand(Data.ActionInput.Civ2.CorrBoxSize(1),Data.ActionInput.Civ2.CorrBoxSize(2));
+            testboxb=rand(Data.ActionInput.Civ2.SearchBoxSize(1),Data.ActionInput.Civ2.SearchBoxSize(2));
+            anss=conv2(testboxa,testboxb);
+            CPUtime_unit=toc;
+            nb_box=pixnbre/(Data.ActionInput.Civ2.Dx*Data.ActionInput.Civ2.Dy);
+            %BoxSize=Data.ActionInput.Civ2.CorrBoxSize(1)*Data.ActionInput.Civ2.CorrBoxSize(2);
+            %nbpos=Data.ActionInput.Civ2.SearchBoxSize-Data.ActionInput.Civ2.CorrBoxSize;
+            CPUtime=CPUtime+2*CPUtime_unit*nb_box;%*BoxSize*nbpos(1)*nbpos(2);
+        end
+        if isfield(Data.ActionInput,'Patch2')
+            CPUtime=(4/3)*CPUtime;
+        end
+        Data.CPUTime=ceil(CPUtime/6); % estimated CPU time per field pair in minute
+        Data.CPUTime=Data.CPUTime/10; % displqy CPU time with 1 digit beyond dot
+    end
+    return
+end
+
+%% read input parameters from an xml file if input is a file name (batch mode)
+checkrun=1;
+if ischar(Param)
+    Param=xml2struct(Param);% read Param as input file (batch case)
+    checkrun=0;
+end
+
+%% test input
+if ~isfield(Param,'ActionInput')
+    disp_uvmat('ERROR','no parameter set for PIV',checkrun)
+    return
+end
+iview_A=0;%default values
+NbField=1;
+RUNHandle=[];
+CheckInputFile=isfield(Param,'InputTable');%= 1 in test use for TestCiv (no nc file involved)
+CheckOutputFile=isfield(Param,'OutputSubDir');%= 1 in test use for TestPatch (no nc file produced)
+
+%% input files and indexing (skipped in Test mode)
+if CheckInputFile
+    hseries=findobj(allchild(0),'Tag','series');
+    RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series
+    WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series
+    MaxIndex_i=Param.IndexRange.MaxIndex_i;
+    MinIndex_i=Param.IndexRange.MinIndex_i;
+    MaxIndex_j=ones(size(MaxIndex_i));MinIndex_j=ones(size(MinIndex_i));
+    if isfield(Param.IndexRange,'MaxIndex_j')&& isfield(Param.IndexRange,'MinIndex_j')
+        MaxIndex_j=Param.IndexRange.MaxIndex_j;
+        MinIndex_j=Param.IndexRange.MinIndex_j;
+    end
+    if isfield(Param,'InputTable')
+        [tild,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
+        iview_A=0;% series index (iview) for the first image series
+        iview_B=0;% series index (iview) for the second image series (only non zero for option 'shift' comparing two image series )
+        if Param.ActionInput.CheckCiv1
+            iview_A=1;% usual PIV, the image series is on the first line of the table
+        elseif Param.ActionInput.CheckCiv2 % civ2 is performed without Civ1, a netcdf file series is needed in the first table line
+            iview_A=2;% the second line is used for the input images of Civ2
+        end
+%         if strcmp(Param.ActionInput.ListCompareMode,'shift')
+%             iview_B=iview_A+1; % the second image series is on the next line of the input table
+%         end
+        if iview_A~=0
+            RootPath_A=Param.InputTable{iview_A,1};
+            RootFile_A=Param.InputTable{iview_A,3};
+            SubDir_A=Param.InputTable{iview_A,2};
+            NomType_A=Param.InputTable{iview_A,4};
+            FileExt_A=Param.InputTable{iview_A,5};
+            if iview_B==0
+                iview_B=iview_A;% the second image series is the same as the first
+            end
+            RootPath_B=Param.InputTable{iview_B,1};
+            RootFile_B=Param.InputTable{iview_B,3};
+            SubDir_B=Param.InputTable{iview_B,2};
+            NomType_B=Param.InputTable{iview_B,4};
+            FileExt_B=Param.InputTable{iview_B,5};
+        end
+        
+        PairCiv2='';
+        switch Param.ActionInput.ListCompareMode
+            case 'PIV'
+                PairCiv1=Param.ActionInput.PairIndices.ListPairCiv1;
+                if isfield(Param.ActionInput.PairIndices,'ListPairCiv2')
+                    PairCiv2=Param.ActionInput.PairIndices.ListPairCiv2;%string which determines the civ2 pair
+                end
+                if iview_A==1% if Civ1 is performed
+                    [i1_series_Civ1,i2_series_Civ1,j1_series_Civ1,j2_series_Civ1,check_bounds,NomTypeNc]=...
+                        find_pair_indices(PairCiv1,i1_series{1},j1_series{1},MinIndex_i,MaxIndex_i,MinIndex_j,MaxIndex_j);
+                    if ~isempty(PairCiv2)
+                        [i1_series_Civ2,i2_series_Civ2,j1_series_Civ2,j2_series_Civ2,check_bounds_Civ2]=...
+                            find_pair_indices(PairCiv2,i1_series{1},j1_series{1},MinIndex_i(1),MaxIndex_i(1),MinIndex_j(1),MaxIndex_j(1));
+                        check_bounds=check_bounds | check_bounds_Civ2;
+                    end
+                else% we start from an existing Civ1 file
+                    i1_series_Civ1=i1_series{1};
+                    i2_series_Civ1=i2_series{1};
+                    j1_series_Civ1=j1_series{1};
+                    j2_series_Civ1=j2_series{1};
+                    NomTypeNc=Param.InputTable{1,4};
+                    if ~isempty(PairCiv2)
+                        [i1_series_Civ2,i2_series_Civ2,j1_series_Civ2,j2_series_Civ2,check_bounds,NomTypeNc]=...
+                            find_pair_indices(PairCiv2,i1_series{2},j1_series{2},MinIndex_i(2),MaxIndex_i(2),MinIndex_j(2),MaxIndex_j(2));
+                    end
+                end
+            case 'displacement'
+                if isfield(Param.ActionInput,'OriginIndex')
+                i1_series_Civ1=Param.ActionInput.OriginIndex*ones(size(i1_series{1}));
+                else
+                    i1_series_Civ1=ones(size(i1_series{1}));
+                end
+                i1_series_Civ2=i1_series_Civ1;
+                i2_series_Civ1=i1_series{1};
+                i2_series_Civ2=i1_series{1};
+                j1_series_Civ1=[];% no j index variation for the ref image
+                j1_series_Civ2=[];
+                if isempty(j1_series{1})
+                    j2_series_Civ1=ones(size(i1_series_Civ1));
+                else
+                    j2_series_Civ1=j1_series{1};% if j index exist  
+                end
+                j2_series_Civ2=j2_series_Civ1;
+                NomTypeNc='_1';
+            case 'PIV volume'
+                % TODO, TODO
+        end
+        %determine frame indices for input with movie or other multiframe input file
+        if isempty(j1_series_Civ1)% simple movie with index i
+            FrameIndex_A_Civ1=i1_series_Civ1;
+            FrameIndex_B_Civ1=i2_series_Civ1;
+            j1_series_Civ1=ones(size(i1_series_Civ1));
+            if strcmp(Param.ActionInput.ListCompareMode,'PIV')
+            j2_series_Civ1=ones(size(i1_series_Civ1));
+            end
+        else % movie for each burst or volume (index j)
+            FrameIndex_A_Civ1=j1_series_Civ1;
+            FrameIndex_B_Civ1=j2_series_Civ1;
+        end
+        if isempty(PairCiv2)
+            FrameIndex_A_Civ2=FrameIndex_A_Civ1;
+            FrameIndex_B_Civ2=FrameIndex_B_Civ1;
+        else
+            if isempty(j1_series_Civ2)
+                FrameIndex_A_Civ2=i1_series_Civ2;
+                FrameIndex_B_Civ2=i2_series_Civ2;
+                j1_series_Civ2=ones(size(i1_series_Civ2));
+                if strcmp(Param.ActionInput.ListCompareMode,'PIV')
+                j2_series_Civ2=ones(size(i1_series_Civ2));
+                end
+            else
+                FrameIndex_A_Civ2=j1_series_Civ2;
+                FrameIndex_B_Civ2=j2_series_Civ2;
+            end
+        end
+        if isempty(i1_series_Civ1)||(~isempty(PairCiv2) && isempty(i1_series_Civ2))
+            disp_uvmat('ERROR','no image pair for civ in the input file index range',checkrun)
+            return
+        end
+    end
+    
+    %% check the first image pair
+        if Param.ActionInput.CheckCiv1% Civ1 is performed
+            NbField=numel(i1_series_Civ1);
+        elseif Param.ActionInput.CheckCiv2 % Civ2 is performed without Civ1
+            NbField=numel(i1_series_Civ2);
+        else
+            NbField=numel(i1_series_Civ1);% no image used (only fix or patch) TO CHECK
+        end
+
+    %% Output directory
+    OutputDir='';
+    if CheckOutputFile
+        OutputDir=[Param.OutputSubDir Param.OutputDirExt];
+    end
+end
+
+%% prepare output Data
+ListGlobalAttribute={'Conventions','Program','CivStage'};
+Data.Conventions='uvmat/civdata';% states the conventions used for the description of field variables and attributes
+Data.Program='civ_series';
+Data.CivStage=0;%default
+check_civx=0;%default
+
+%% get timing from the ImaDoc file or input video
+if iview_A~=0
+    XmlFileName=find_imadoc(RootPath_A,SubDir_A,RootFile_A,FileExt_A);
+    Time=[];
+    if ~isempty(XmlFileName)
+        XmlData=imadoc2struct(XmlFileName);
+        if isfield(XmlData,'Time')
+            Time=XmlData.Time;
+        end
+        if isfield(XmlData,'Camera')
+            if isfield(XmlData.Camera,'NbSlice')&& ~isempty(XmlData.Camera.NbSlice)
+                NbSlice_calib{iview}=XmlData.Camera.NbSlice;% Nbre of slices for Zindex in phys transform
+                if ~isequal(NbSlice_calib{iview},NbSlice_calib{1})
+                    msgbox_uvmat('WARNING','inconsistent number of Z indices for the two field series');
+                end
+            end
+            if isfield(XmlData.Camera,'TimeUnit')&& ~isempty(XmlData.Camera.TimeUnit)
+                TimeUnit=XmlData.Camera.TimeUnit;
+            end
+        end
+    end
+end
+
+%%%%% MAIN LOOP %%%%%%
+maskoldname='';% initiate the mask name
+FileType_A='';
+FileType_B='';
+CheckOverwrite=1;%default
+if isfield(Param,'CheckOverwrite')
+    CheckOverwrite=Param.CheckOverwrite;
+end
+for ifield=1:NbField
+    tic
+    if ~isempty(RUNHandle)% update the waitbar in interactive mode with GUI series  (checkrun=1)
+        update_waitbar(WaitbarHandle,ifield/NbField)
+        if  checkrun && ~strcmp(get(RUNHandle,'BusyAction'),'queue')
+            disp('program stopped by user')
+            break
+        end
+    end
+    if CheckInputFile
+        if iview_A==0 % no nc file has been entered
+            ncfile=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},Param.InputTable{1,5},...
+                NomTypeNc,i1_series_Civ1(ifield),i2_series_Civ1(ifield),j1_series_Civ1(ifield),j2_series_Civ1(ifield));
+        else% an existing nc file has been entered
+            if iview_A==1% if Civ1 is performed
+                Civ1Dir=OutputDir;
+            else
+                Civ1Dir=Param.InputTable{1,2};
+            end
+            if strcmp(Param.ActionInput.ListCompareMode,'PIV')
+                ncfile=fullfile_uvmat(RootPath_A,Civ1Dir,RootFile_A,'.nc',NomTypeNc,i1_series_Civ1(ifield),i2_series_Civ1(ifield),...
+                    j1_series_Civ1(ifield),j2_series_Civ1(ifield));
+            else
+                ncfile=fullfile_uvmat(RootPath_A,Civ1Dir,RootFile_A,'.nc',NomTypeNc,i2_series_Civ1(ifield),[],...
+                    j1_series_Civ1(ifield),j2_series_Civ1(ifield));
+            end
+        end
+        ncfile_out=ncfile;% by default
+        if isfield (Param.ActionInput,'Civ2')
+            i1_civ2=i1_series_Civ2(ifield);
+            i2_civ2=i1_civ2;
+            if ~isempty(i2_series_Civ2)
+                i2_civ2=i2_series_Civ2(ifield);
+            end
+            j1_civ2=1;
+            if ~isempty(j1_series_Civ2)
+                j1_civ2=j1_series_Civ2(ifield);
+            end
+            j2_civ2=i1_civ2;
+            if ~isempty(j2_series_Civ2)
+                j2_civ2=j2_series_Civ2(ifield);
+            end
+            if strcmp(Param.ActionInput.ListCompareMode,'PIV')
+                ncfile_out=fullfile_uvmat(RootPath_A,OutputDir,RootFile_A,'.nc',NomTypeNc,i1_civ2,i2_civ2,j1_civ2,j2_civ2);
+            else % displacement
+                ncfile_out=fullfile_uvmat(RootPath_A,OutputDir,RootFile_A,'.nc',NomTypeNc,i2_civ2,[],j2_civ2);
+            end
+        end
+        if ~CheckOverwrite && exist(ncfile_out,'file')
+            disp(['existing output file ' ncfile_out ' already exists, skip to next field'])
+            continue% skip iteration if the mode overwrite is desactivated and the result file already exists
+        end
+    end
+    %% Civ1
+    % if Civ1 computation is requested
+    if isfield (Param.ActionInput,'Civ1')
+        if CheckInputFile
+            disp('civ1 started')
+        end
+        par_civ1=Param.ActionInput.Civ1;% parameters for civ1
+        if CheckInputFile % read input images (except in mode Test where it is introduced directly in Param.ActionInput.Civ1.ImageNameA and B)
+            try
+                if strcmp(Param.ActionInput.ListCompareMode,'displacement')
+                    ImageName_A=Param.ActionInput.RefFile;
+                else
+                ImageName_A=fullfile_uvmat(RootPath_A,SubDir_A,RootFile_A,FileExt_A,NomType_A,i1_series_Civ1(ifield),[],j1_series_Civ1(ifield));
+                end
+                if strcmp(FileExt_A,'.nc')% case of input images in format netcdf
+                    FieldName_A=Param.InputFields.FieldName;
+                    [DataIn,tild,tild,errormsg]=nc2struct(ImageName_A,{FieldName_A});
+                    par_civ1.ImageA=DataIn.(FieldName_A);
+                else % usual image formats for image A
+                    if isempty(FileType_A)% open the image object if not already done in case of movie input
+                        [FileInfo_A,VideoObject_A]=get_file_info(ImageName_A);
+                        FileType_A=FileInfo_A.FileType;
+                        if isempty(Time) && ~isempty(find(strcmp(FileType_A,{'mmreader','video','cine_phantom'})))% case of video input
+                            Time=zeros(FileInfo_A.NumberOfFrames+1,2);
+                            Time(:,2)=(0:1/FileInfo_A.FrameRate:(FileInfo_A.NumberOfFrames)/FileInfo_A.FrameRate)';
+                        end
+                        if ~isempty(FileType_A) && isempty(Time)% Time = index i +0.001 index j by default
+                            MaxIndex_i=max(i2_series_Civ1);
+                            MaxIndex_j=max(j2_series_Civ1);
+                            Time=(1:MaxIndex_i)'*ones(1,MaxIndex_j);
+                            Time=Time+0.001*ones(MaxIndex_i,1)*(1:MaxIndex_j);
+                            Time=[zeros(1,MaxIndex_j);Time];% insert a first line of zeros
+                            Time=[zeros(MaxIndex_i+1,1) Time];% insert a first column of zeros
+                        end
+                    end
+                    if ~exist(ImageName_A,'file')
+                        disp([ImageName_A ' missing'])
+                        continue
+                    end
+                    [par_civ1.ImageA,VideoObject_A] = read_image(ImageName_A,FileType_A,VideoObject_A,FrameIndex_A_Civ1(ifield));
+                end
+                ImageName_B=fullfile_uvmat(RootPath_B,SubDir_B,RootFile_B,FileExt_B,NomType_B,i2_series_Civ1(ifield),[],j2_series_Civ1(ifield));
+                if strcmp(FileExt_B,'.nc') % case of input images in format netcdf
+                    FieldName_B=Param.InputFields.FieldName;
+                    [DataIn,tild,tild,errormsg]=nc2struct(ImageName_B,{FieldName_B});
+                    par_civ1.ImageB=DataIn.(FieldName_B);
+                else % usual image formats for image B
+                    if isempty(FileType_B)
+                        [FileInfo_B,VideoObject_B]=get_file_info(ImageName_B);
+                        FileType_B=FileInfo_B.FileType;
+                    end
+                    if ~exist(ImageName_B,'file')
+                        disp([ImageName_B ' missing'])
+                        continue
+                    end
+                    [par_civ1.ImageB,VideoObject_B] = read_image(ImageName_B,FileType_B,VideoObject_B,FrameIndex_B_Civ1(ifield));
+                end
+            catch ME % display errors in reading input images
+                if ~isempty(ME.message)
+                    disp_uvmat('ERROR', ['error reading input image: ' ME.message],checkrun)
+                    continue
+                end
+            end
+            par_civ1.ImageWidth=size(par_civ1.ImageA,2);
+            par_civ1.ImageHeight=size(par_civ1.ImageA,1);
+            list_param=(fieldnames(Param.ActionInput.Civ1))';
+            list_param(strcmp('TestCiv1',list_param))=[];% remove the parameter TestCiv1 from the list
+            Civ1_param=regexprep(list_param,'^.+','Civ1_$0');% insert 'Civ1_' before  each string in list_param
+            Civ1_param=[{'Civ1_ImageA','Civ1_ImageB','Civ1_Time','Civ1_Dt'} Civ1_param]; %insert the names of the two input images
+            %indicate the values of all the global attributes in the output data
+            Data.Civ1_ImageA=ImageName_A;
+            Data.Civ1_ImageB=ImageName_B;
+            i1=i1_series_Civ1(ifield);
+            i2=i1;
+            if ~isempty(i2_series_Civ1)
+                i2=i2_series_Civ1(ifield);
+            end
+            j1=1;
+            if ~isempty(j1_series_Civ1)
+                j1=j1_series_Civ1(ifield);
+            end
+            j2=j1;
+            if ~isempty(j2_series_Civ1)
+                j2=j2_series_Civ1(ifield);
+            end
+            if strcmp(Param.ActionInput.ListCompareMode,'displacement')
+                Data.Civ1_Time=Time(i2+1,j2+1);% the Time is the Time of the secodn image
+                Data.Civ1_Dt=1;% Time interval is 1, to yield displacement instead of velocity=displacement/Dt at reading
+            else
+                Data.Civ1_Time=(Time(i2+1,j2+1)+Time(i1+1,j1+1))/2;% the Time is the Time at the middle of the image pair
+                Data.Civ1_Dt=Time(i2+1,j2+1)-Time(i1+1,j1+1);
+            end
+            for ilist=1:length(list_param)
+                Data.(Civ1_param{4+ilist})=Param.ActionInput.Civ1.(list_param{ilist});
+            end
+            Data.ListGlobalAttribute=[ListGlobalAttribute Civ1_param];
+            Data.CivStage=1;
+        end
+        % set the list of variables
+        Data.ListVarName={'Civ1_X','Civ1_Y','Civ1_U','Civ1_V','Civ1_F','Civ1_C'};%  cell array containing the names of the fields to record
+        Data.VarDimName={'nb_vec_1','nb_vec_1','nb_vec_1','nb_vec_1','nb_vec_1','nb_vec_1'};
+        Data.VarAttribute{1}.Role='coord_x';
+        Data.VarAttribute{2}.Role='coord_y';
+        Data.VarAttribute{3}.Role='vector_x';
+        Data.VarAttribute{4}.Role='vector_y';
+        Data.VarAttribute{5}.Role='warnflag';
+        % case of mask
+        if par_civ1.CheckMask&&~isempty(par_civ1.Mask)
+            if isfield(par_civ1,'NbSlice')
+                [RootPath_mask,SubDir_mask,RootFile_mask,i1_mask,i2_mask,j1_mask,j2_mask,Ext_mask]=fileparts_uvmat(Param.ActionInput.Civ1.Mask);
+                i1_mask=mod(i1-1,par_civ1.NbSlice)+1;
+                maskname=fullfile_uvmat(RootPath_mask,SubDir_mask,RootFile_mask,Ext_mask,'_1',i1_mask);
+            else
+                maskname=Param.ActionInput.Civ1.Mask;
+            end
+            if strcmp(maskoldname,maskname)% mask exist, not already read in civ1
+                par_civ1.Mask=mask; %use mask already opened
+            else
+                if exist(maskname,'file')
+                    try
+                        par_civ1.Mask=imread(maskname);%update the mask, an store it for future use
+                    catch ME
+                        if ~isempty(ME.message)
+                            errormsg=['error reading input image: ' ME.message];
+                            disp_uvmat('ERROR',errormsg,checkrun)
+                            return
+                        end
+                    end
+                else
+                    par_civ1.Mask=[];
+                end
+                mask=par_civ1.Mask;
+                maskoldname=maskname;
+            end
+        end
+        if strcmp(Param.ActionInput.ListCompareMode, 'PIV volume')
+            Data.ListVarName=[Data.ListVarName 'Civ1_Z'];
+            Data.Civ1_X=[];Data.Civ1_Y=[];Data.Civ1_Z=[];
+            Data.Civ1_U=[];Data.Civ1_V=[];Data.Civ1_C=[];Data.Civ1_F=[];
+            for ivol=1:NbSlice
+                % caluclate velocity data (y and v in indices, reverse to y component)
+                [xtable, ytable, utable, vtable, ctable, F, result_conv, errormsg] = civ (par_civ1);
+                if ~isempty(errormsg)
+                    disp_uvmat('ERROR',errormsg,checkrun)
+                    return
+                end
+                Data.Civ1_X=[Data.Civ1_X reshape(xtable,[],1)];
+                Data.Civ1_Y=[Data.Civ1_Y reshape(Param.Civ1.ImageHeight-ytable+1,[],1)];
+                Data.Civ1_Z=[Data.Civ1_Z ivol*ones(numel(xtable),1)];% z=image index in image coordinates
+                Data.Civ1_U=[Data.Civ1_U reshape(utable,[],1)];
+                Data.Civ1_V=[Data.Civ1_V reshape(-vtable,[],1)];
+                Data.Civ1_C=[Data.Civ1_C reshape(ctable,[],1)];
+                Data.Civ1_F=[Data.Civ1_C reshape(F,[],1)];
+            end
+        else %usual PIV
+            % caluclate velocity data (y and v in indices, reverse to y component)
+            [xtable, ytable, utable, vtable, ctable, F, result_conv, errormsg] = civ (par_civ1);
+            if ~isempty(errormsg)
+                disp_uvmat('ERROR',errormsg,checkrun)
+                return
+            end
+            Data.Civ1_X=reshape(xtable,[],1);
+            Data.Civ1_Y=reshape(par_civ1.ImageHeight-ytable+1,[],1);
+            Data.Civ1_U=reshape(utable,[],1);
+            Data.Civ1_V=reshape(-vtable,[],1);
+            Data.Civ1_C=reshape(ctable,[],1);
+            Data.Civ1_F=reshape(F,[],1);
+        end
+    else% we use existing Civ1 data
+        if exist('ncfile','var')
+            CivFile=ncfile;
+            [Data,tild,tild,errormsg]=nc2struct(CivFile,'ListGlobalAttribute','absolut_time_T0'); %look for the constant 'absolut_time_T0' to detect old civx data format
+            if ~isempty(errormsg)
+                disp_uvmat('ERROR',errormsg,checkrun)
+                return
+            end
+            [Data,tild,tild,errormsg]=nc2struct(CivFile);%read civ1 and fix1 data in the existing netcdf file
+        elseif isfield(Param,'Civ1_X')
+            Data.ListGlobalAttribute={};
+            Data.ListVarName={};
+            Data.VarDimName={};
+            Data.Civ1_X=Param.Civ1_X;
+            Data.Civ1_Y=Param.Civ1_Y;
+            Data.Civ1_U=Param.Civ1_U;
+            Data.Civ1_V=Param.Civ1_V;
+            Data.Civ1_FF=Param.Civ1_FF;
+        end
+    end
+    
+    %% Fix1
+    if isfield (Param.ActionInput,'Fix1')
+        disp('fix1 started')
+        if ~isfield (Param.ActionInput,'Civ1')% if we use existing Civ1, remove previous data beyond Civ1
+            Fix1_attr=find(strcmp('Fix1',Data.ListGlobalAttribute));
+            Data.ListGlobalAttribute(Fix1_attr)=[];
+            for ilist=1:numel(Fix1_attr)
+                Data=rmfield(Data,Data.ListGlobalAttribute{Fix1_attr(ilist)});
+            end
+        end
+        list_param=fieldnames(Param.ActionInput.Fix1)';
+        Fix1_param=regexprep(list_param,'^.+','Fix1_$0');% insert 'Fix1_' before  each string in ListFixParam
+        %indicate the values of all the global attributes in the output data
+        for ilist=1:length(list_param)
+            Data.(Fix1_param{ilist})=Param.ActionInput.Fix1.(list_param{ilist});
+        end
+        Data.ListGlobalAttribute=[Data.ListGlobalAttribute Fix1_param];
+        Data.ListVarName=[Data.ListVarName {'Civ1_FF'}];
+        Data.VarDimName=[Data.VarDimName {'nb_vec_1'}];
+        nbvar=length(Data.ListVarName);
+        Data.VarAttribute{nbvar}.Role='errorflag';
+        Data.Civ1_FF=int8(fix(Param.ActionInput.Fix1,Data.Civ1_F,Data.Civ1_C,Data.Civ1_U,Data.Civ1_V));
+        Data.CivStage=2;
+    end
+    %% Patch1
+    if isfield (Param.ActionInput,'Patch1')
+        disp('patch1 started')
+        if check_civx
+            errormsg='Civ Matlab input needed for patch';
+            disp_uvmat('ERROR',errormsg,checkrun)
+            return
+        end
+        
+        % record the processing parameters of Patch1 as global attributes in the result nc file
+        list_param=fieldnames(Param.ActionInput.Patch1)';
+        list_param(strcmp('TestPatch1',list_param))=[];% remove 'TestPatch1' from the list of parameters
+        Patch1_param=regexprep(list_param,'^.+','Patch1_$0');% insert 'Patch1_' before  each parameter name
+        for ilist=1:length(list_param)
+            Data.(Patch1_param{ilist})=Param.ActionInput.Patch1.(list_param{ilist});
+        end
+        Data.CivStage=3;% record the new state of processing
+        Data.ListGlobalAttribute=[Data.ListGlobalAttribute Patch1_param];
+        
+        % list the variables to record
+        nbvar=length(Data.ListVarName);
+        Data.ListVarName=[Data.ListVarName {'Civ1_U_smooth','Civ1_V_smooth','Civ1_SubRange','Civ1_NbCentres','Civ1_Coord_tps','Civ1_U_tps','Civ1_V_tps'}];
+        Data.VarDimName=[Data.VarDimName {'nb_vec_1','nb_vec_1',{'nb_coord','nb_bounds','nb_subdomain_1'},'nb_subdomain_1',...
+            {'nb_tps_1','nb_coord','nb_subdomain_1'},{'nb_tps_1','nb_subdomain_1'},{'nb_tps_1','nb_subdomain_1'}}];
+        Data.VarAttribute{nbvar+1}.Role='vector_x';
+        Data.VarAttribute{nbvar+2}.Role='vector_y';
+        Data.VarAttribute{nbvar+5}.Role='coord_tps';
+        Data.VarAttribute{nbvar+6}.Role='vector_x';
+        Data.VarAttribute{nbvar+7}.Role='vector_y';
+        Data.Civ1_U_smooth=Data.Civ1_U; % zeros(size(Data.Civ1_X));
+        Data.Civ1_V_smooth=Data.Civ1_V; %zeros(size(Data.Civ1_X));
+        if isfield(Data,'Civ1_FF')
+            ind_good=find(Data.Civ1_FF==0);
+        else
+            ind_good=1:numel(Data.Civ1_X);
+        end
+        
+        % perform Patch calculation using the UVMAT fct 'filter_tps'
+        [Data.Civ1_SubRange,Data.Civ1_NbCentres,Data.Civ1_Coord_tps,Data.Civ1_U_tps,Data.Civ1_V_tps,tild,Ures, Vres,tild,FFres]=...
+            filter_tps([Data.Civ1_X(ind_good) Data.Civ1_Y(ind_good)],Data.Civ1_U(ind_good),Data.Civ1_V(ind_good),[],Data.Patch1_SubDomainSize,Data.Patch1_FieldSmooth,Data.Patch1_MaxDiff);
+        Data.Civ1_U_smooth(ind_good)=Ures;% take the interpolated (smoothed) velocity values for good vectors, keep civ1 data for the other
+        Data.Civ1_V_smooth(ind_good)=Vres;
+        Data.Civ1_FF(ind_good)=int8(FFres);
+        disp('patch1 performed')
+    end
+    
+    %% Civ2
+    if isfield (Param.ActionInput,'Civ2')
+        disp('civ2 started')
+        par_civ2=Param.ActionInput.Civ2;
+        if CheckInputFile % read input images (except in mode Test where it is introduced directly in Param.ActionInput.Civ1.ImageNameA and B)
+            par_civ2.ImageA=[];
+            par_civ2.ImageB=[];
+            if strcmp(Param.ActionInput.ListCompareMode,'displacement')
+                    ImageName_A_Civ2=Param.ActionInput.RefFile;
+                else
+            ImageName_A_Civ2=fullfile_uvmat(RootPath_A,SubDir_A,RootFile_A,FileExt_A,NomType_A,i1_civ2,[],j1_civ2);
+            end
+            if strcmp(ImageName_A_Civ2,ImageName_A) && isequal(FrameIndex_A_Civ1(ifield),FrameIndex_A_Civ2(ifield))
+                par_civ2.ImageA=par_civ1.ImageA;
+            else
+                [par_civ2.ImageA,VideoObject_A] = read_image(ImageName_A_Civ2,FileType_A,VideoObject_A,FrameIndex_A_Civ2(ifield));
+            end
+            ImageName_B_Civ2=fullfile_uvmat(RootPath_B,SubDir_B,RootFile_B,FileExt_B,NomType_B,i2_civ2,[],j2_civ2);
+            if strcmp(ImageName_B_Civ2,ImageName_B) && isequal(FrameIndex_B_Civ1(ifield),FrameIndex_B_Civ2)
+                par_civ2.ImageB=par_civ1.ImageB;
+            else
+                [par_civ2.ImageB,VideoObject_B] = read_image(ImageName_B_Civ2,FileType_B,VideoObject_B,FrameIndex_B_Civ2(ifield));
+            end
+            par_civ2.ImageWidth=FileInfo_A.Width;
+            par_civ2.ImageHeight=FileInfo_A.Height;
+            if isfield(par_civ2,'Grid')% grid points set as input file
+                if ischar(par_civ2.Grid)%read the grid file if the input is a file name
+                    par_civ2.Grid=dlmread(par_civ2.Grid);
+                    par_civ2.Grid(1,:)=[];%the first line must be removed (heading in the grid file)
+                end
+            else% automatic grid
+                minix=floor(par_civ2.Dx/2)-0.5;
+                maxix=minix+par_civ2.Dx*floor((par_civ2.ImageWidth-1)/par_civ2.Dx);
+                miniy=floor(par_civ2.Dy/2)-0.5;
+                maxiy=minix+par_civ2.Dy*floor((par_civ2.ImageHeight-1)/par_civ2.Dy);
+                [GridX,GridY]=meshgrid(minix:par_civ2.Dx:maxix,miniy:par_civ2.Dy:maxiy);
+                par_civ2.Grid(:,1)=reshape(GridX,[],1);
+                par_civ2.Grid(:,2)=reshape(GridY,[],1);
+            end
+        end
+        
+        % get the guess from patch1 or patch2 (case 'CheckCiv3')
+        if CheckInputFile % read input images (except in mode Test where it is introduced directly in Param.ActionInput.Civ1.ImageNameA and B)
+            if isfield (par_civ2,'CheckCiv3') && par_civ2.CheckCiv3 %get the guess from  patch2
+                SubRange= Data.Civ2_SubRange;
+                NbCentres=Data.Civ2_NbCentres;
+                Coord_tps=Data.Civ2_Coord_tps;
+                U_tps=Data.Civ2_U_tps;
+                V_tps=Data.Civ2_V_tps;
+                CivStage=Data.CivStage;%store the current CivStage
+                Civ1_Dt=Data.Civ2_Dt;
+                Data=[];%reinitialise the result structure Data
+                Data.ListGlobalAttribute={'Conventions','Program','CivStage'};
+                Data.Conventions='uvmat/civdata';% states the conventions used for the description of field variables and attributes
+                Data.Program='civ_series';
+                Data.CivStage=CivStage+1;%update the current civStage after reinitialisation of Data
+                Data.ListVarName={};
+                Data.VarDimName={};
+            else % get the guess from patch1
+                SubRange= Data.Civ1_SubRange;
+                NbCentres=Data.Civ1_NbCentres;
+                Coord_tps=Data.Civ1_Coord_tps;
+                U_tps=Data.Civ1_U_tps;
+                V_tps=Data.Civ1_V_tps;
+                Civ1_Dt=Data.Civ1_Dt;
+                Data.CivStage=4;
+            end
+        else
+            SubRange= par_civ2.Civ1_SubRange;
+            NbCentres=par_civ2.Civ1_NbCentres;
+            Coord_tps=par_civ2.Civ1_Coord_tps;
+            U_tps=par_civ2.Civ1_U_tps;
+            V_tps=par_civ2.Civ1_V_tps;
+            Civ1_Dt=par_civ2.Civ1_Dt;
+            Civ2_Dt=par_civ2.Civ1_Dt;
+            Data.ListVarName={};
+            Data.VarDimName={};
+        end
+        Shiftx=zeros(size(par_civ2.Grid,1),1);% shift expected from civ1 data
+        Shifty=zeros(size(par_civ2.Grid,1),1);
+        nbval=zeros(size(par_civ2.Grid,1),1);% nbre of interpolated values at each grid point (from the different patch subdomains)
+        if par_civ2.CheckDeformation
+            DUDX=zeros(size(par_civ2.Grid,1),1);
+            DUDY=zeros(size(par_civ2.Grid,1),1);
+            DVDX=zeros(size(par_civ2.Grid,1),1);
+            DVDY=zeros(size(par_civ2.Grid,1),1);
+        end
+        NbSubDomain=size(SubRange,3);
+        for isub=1:NbSubDomain% for each sub-domain of Patch1
+            nbvec_sub=NbCentres(isub);% nbre of Civ vectors in the subdomain
+            ind_sel=find(par_civ2.Grid(:,1)>=SubRange(1,1,isub) & par_civ2.Grid(:,1)<=SubRange(1,2,isub) &...
+                par_civ2.Grid(:,2)>=SubRange(2,1,isub) & par_civ2.Grid(:,2)<=SubRange(2,2,isub));% grid points in the subdomain
+            if ~isempty(ind_sel)
+                epoints = par_civ2.Grid(ind_sel,:);% coordinates of interpolation sites (measurement grids)
+                ctrs=Coord_tps(1:nbvec_sub,:,isub) ;%(=initial points) ctrs
+                nbval(ind_sel)=nbval(ind_sel)+1;% records the number of values for each interpolation point (in case of subdomain overlap)
+                EM = tps_eval(epoints,ctrs);% thin plate spline (tps) coefficient
+                Shiftx(ind_sel)=Shiftx(ind_sel)+EM*U_tps(1:nbvec_sub+3,isub);%velocity shift estimated by tps from civ1
+                Shifty(ind_sel)=Shifty(ind_sel)+EM*V_tps(1:nbvec_sub+3,isub);
+                if par_civ2.CheckDeformation
+                    [EMDX,EMDY] = tps_eval_dxy(epoints,ctrs);%2D matrix of distances between extrapolation points epoints and spline centres (=site points) ctrs
+                    DUDX(ind_sel)=DUDX(ind_sel)+EMDX*U_tps(1:nbvec_sub+3,isub);
+                    DUDY(ind_sel)=DUDY(ind_sel)+EMDY*U_tps(1:nbvec_sub+3,isub);
+                    DVDX(ind_sel)=DVDX(ind_sel)+EMDX*V_tps(1:nbvec_sub+3,isub);
+                    DVDY(ind_sel)=DVDY(ind_sel)+EMDY*V_tps(1:nbvec_sub+3,isub);
+                end
+            end
+        end
+        if par_civ2.CheckMask&&~isempty(par_civ2.Mask)        
+            if isfield(par_civ2,'NbSlice')
+                [RootPath_mask,SubDir_mask,RootFile_mask,i1_mask,i2_mask,j1_mask,j2_mask,Ext_mask]=fileparts_uvmat(Param.ActionInput.Civ2.Mask);
+                i1_mask=mod(i1-1,par_civ2.NbSlice)+1;
+                maskname=fullfile_uvmat(RootPath_mask,SubDir_mask,RootFile_mask,Ext_mask,'_1',i1_mask);
+            else
+                maskname=Param.ActionInput.Civ2.Mask;
+            end
+            if strcmp(maskoldname,maskname)% mask exist, not already read in civ1
+                par_civ2.Mask=mask; %use mask already opened
+            else
+                if exist(maskname,'file')
+                try
+                    par_civ2.Mask=imread(maskname);%update the mask, an store it for future use
+                catch ME
+                    if ~isempty(ME.message)
+                        errormsg=['error reading input image: ' ME.message];
+                        disp_uvmat('ERROR',errormsg,checkrun)
+                        return
+                    end
+                end
+                else
+                    par_civ2.Mask=[];
+                end
+                mask=par_civ2.Mask;
+                maskoldname=maskname;
+            end
+        end
+
+        if CheckInputFile % else Dt given by par_civ2
+            if strcmp(Param.ActionInput.ListCompareMode,'displacement')
+                Civ1_Dt=1;
+                Civ2_Dt=1;
+            else
+                Civ2_Dt=Time(i2_civ2+1,j2_civ2+1)-Time(i1_civ2+1,j1_civ2+1);
+            end
+        end
+        par_civ2.SearchBoxShift=(Civ2_Dt/Civ1_Dt)*[Shiftx(nbval>=1)./nbval(nbval>=1) Shifty(nbval>=1)./nbval(nbval>=1)];
+        % shift the grid points by half the expected shift to provide the correlation box position in image A
+        par_civ2.Grid=[par_civ2.Grid(nbval>=1,1)-par_civ2.SearchBoxShift(:,1)/2 par_civ2.Grid(nbval>=1,2)-par_civ2.SearchBoxShift(:,2)/2];
+        if par_civ2.CheckDeformation
+            par_civ2.DUDX=DUDX(nbval>=1)./nbval(nbval>=1);
+            par_civ2.DUDY=DUDY(nbval>=1)./nbval(nbval>=1);
+            par_civ2.DVDX=DVDX(nbval>=1)./nbval(nbval>=1);
+            par_civ2.DVDY=DVDY(nbval>=1)./nbval(nbval>=1);
+        end
+        
+        % calculate velocity data (y and v in image indices, reverse to y component)
+        [xtable, ytable, utable, vtable, ctable, F,result_conv,errormsg] = civ (par_civ2);
+        
+        list_param=(fieldnames(Param.ActionInput.Civ2))';
+        list_param(strcmp('TestCiv2',list_param))=[];% remove the parameter TestCiv2 from the list
+        Civ2_param=regexprep(list_param,'^.+','Civ2_$0');% insert 'Civ2_' before  each string in list_param
+        Civ2_param=[{'Civ2_ImageA','Civ2_ImageB','Civ2_Time','Civ2_Dt'} Civ2_param]; %insert the names of the two input images
+        %indicate the values of all the global attributes in the output data
+        if exist('ImageName_A','var')
+            Data.Civ2_ImageA=ImageName_A;
+            Data.Civ2_ImageB=ImageName_B;
+            if strcmp(Param.ActionInput.ListCompareMode,'displacement')
+                Data.Civ2_Time=Time(i2_civ2+1,j2_civ2+1);% the Time is the Time of the secodn image
+                Data.Civ2_Dt=1;% Time interval is 1, to yield displacement instead of velocity=displacement/Dt at reading
+            else
+                Data.Civ2_Time=(Time(i2_civ2+1,j2_civ2+1)+Time(i1_civ2+1,j1_civ2+1))/2;
+                Data.Civ2_Dt=Civ2_Dt;
+            end
+        end
+        for ilist=1:length(list_param)
+            Data.(Civ2_param{4+ilist})=Param.ActionInput.Civ2.(list_param{ilist});
+        end
+        Data.ListGlobalAttribute=[Data.ListGlobalAttribute Civ2_param];
+        
+        nbvar=numel(Data.ListVarName);
+        % define the Civ2 variable (if Civ2 data are not replaced from previous calculation)
+        if isempty(find(strcmp('Civ2_X',Data.ListVarName),1))
+            Data.ListVarName=[Data.ListVarName {'Civ2_X','Civ2_Y','Civ2_U','Civ2_V','Civ2_F','Civ2_C'}];%  cell array containing the names of the fields to record
+            Data.VarDimName=[Data.VarDimName {'nb_vec_2','nb_vec_2','nb_vec_2','nb_vec_2','nb_vec_2','nb_vec_2'}];
+            Data.VarAttribute{nbvar+1}.Role='coord_x';
+            Data.VarAttribute{nbvar+2}.Role='coord_y';
+            Data.VarAttribute{nbvar+3}.Role='vector_x';
+            Data.VarAttribute{nbvar+4}.Role='vector_y';
+            Data.VarAttribute{nbvar+5}.Role='warnflag';
+        end
+        Data.Civ2_X=reshape(xtable,[],1);
+        Data.Civ2_Y=reshape(size(par_civ2.ImageA,1)-ytable+1,[],1);
+        Data.Civ2_U=reshape(utable,[],1);
+        Data.Civ2_V=reshape(-vtable,[],1);
+        Data.Civ2_C=reshape(ctable,[],1);
+        Data.Civ2_F=reshape(F,[],1);
+        disp('civ2 performed')
+    elseif ~isfield(Data,'ListVarName') % we start there, using existing Civ2 data
+        if exist('ncfile','var')
+            CivFile=ncfile;
+            [Data,tild,tild,errormsg]=nc2struct(CivFile);%read civ1 and fix1 data in the existing netcdf file
+            if ~isempty(errormsg)
+                disp_uvmat('ERROR',errormsg,checkrun)
+                return
+            end
+        elseif isfield(Param,'Civ2_X')% use Civ2 data as input in Param (test mode)
+            Data.ListGlobalAttribute={};
+            Data.ListVarName={};
+            Data.VarDimName={};
+            Data.Civ2_X=Param.Civ2_X;
+            Data.Civ2_Y=Param.Civ2_Y;
+            Data.Civ2_U=Param.Civ2_U;
+            Data.Civ2_V=Param.Civ2_V;
+            Data.Civ2_FF=Param.Civ2_FF;
+        end
+    end
+    
+    %% Fix2
+    if isfield (Param.ActionInput,'Fix2')
+        disp('fix2 started')
+        list_param=fieldnames(Param.ActionInput.Fix2)';
+        Fix2_param=regexprep(list_param,'^.+','Fix2_$0');% insert 'Fix1_' before  each string in ListFixParam
+        %indicate the values of all the global attributes in the output data
+        for ilist=1:length(list_param)
+            Data.(Fix2_param{ilist})=Param.ActionInput.Fix2.(list_param{ilist});
+        end
+        Data.ListGlobalAttribute=[Data.ListGlobalAttribute Fix2_param];
+        %
+        %         ListFixParam=fieldnames(Param.ActionInput.Fix2);
+        %         for ilist=1:length(ListFixParam)
+        %             ParamName=ListFixParam{ilist};
+        %             ListName=['Fix2_' ParamName];
+        %             eval(['Data.ListGlobalAttribute=[Data.ListGlobalAttribute ''' ParamName '''];'])
+        %             eval(['Data.' ListName '=Param.ActionInput.Fix2.' ParamName ';'])
+        %         end
+        if check_civx
+            if ~isfield(Data,'fix2')
+                Data.ListGlobalAttribute=[Data.ListGlobalAttribute 'fix2'];
+                Data.fix2=1;
+                Data.ListVarName=[Data.ListVarName {'vec2_FixFlag'}];
+                Data.VarDimName=[Data.VarDimName {'nb_vectors2'}];
+            end
+            Data.vec_FixFlag=fix(Param.Fix2,Data.vec2_F,Data.vec2_C,Data.vec2_U,Data.vec2_V,Data.vec2_X,Data.vec2_Y);
+        else
+            Data.ListVarName=[Data.ListVarName {'Civ2_FF'}];
+            Data.VarDimName=[Data.VarDimName {'nb_vec_2'}];
+            nbvar=length(Data.ListVarName);
+            Data.VarAttribute{nbvar}.Role='errorflag';
+            Data.Civ2_FF=double(fix(Param.ActionInput.Fix2,Data.Civ2_F,Data.Civ2_C,Data.Civ2_U,Data.Civ2_V));
+            Data.CivStage=Data.CivStage+1;
+        end
+    end
+    
+    %% Patch2
+    if isfield (Param.ActionInput,'Patch2')
+        disp('patch2 started')
+        list_param=fieldnames(Param.ActionInput.Patch2)';
+        list_param(strcmp('TestPatch2',list_param))=[];% remove the parameter TestCiv1 from the list
+        Patch2_param=regexprep(list_param,'^.+','Patch2_$0');% insert 'Fix1_' before  each string in ListFixParam
+        %indicate the values of all the global attributes in the output data
+        for ilist=1:length(list_param)
+            Data.(Patch2_param{ilist})=Param.ActionInput.Patch2.(list_param{ilist});
+        end
+        Data.ListGlobalAttribute=[Data.ListGlobalAttribute Patch2_param];
+        
+        nbvar=length(Data.ListVarName);
+        Data.ListVarName=[Data.ListVarName {'Civ2_U_smooth','Civ2_V_smooth','Civ2_SubRange','Civ2_NbCentres','Civ2_Coord_tps','Civ2_U_tps','Civ2_V_tps'}];
+        Data.VarDimName=[Data.VarDimName {'nb_vec_2','nb_vec_2',{'nb_coord','nb_bounds','nb_subdomain_2'},{'nb_subdomain_2'},...
+            {'nb_tps_2','nb_coord','nb_subdomain_2'},{'nb_tps_2','nb_subdomain_2'},{'nb_tps_2','nb_subdomain_2'}}];
+        
+        Data.VarAttribute{nbvar+1}.Role='vector_x';
+        Data.VarAttribute{nbvar+2}.Role='vector_y';
+        Data.VarAttribute{nbvar+5}.Role='coord_tps';
+        Data.VarAttribute{nbvar+6}.Role='vector_x';
+        Data.VarAttribute{nbvar+7}.Role='vector_y';
+        Data.Civ2_U_smooth=Data.Civ2_U;
+        Data.Civ2_V_smooth=Data.Civ2_V;
+        if isfield(Data,'Civ2_FF')
+            ind_good=find(Data.Civ2_FF==0);
+        else
+            ind_good=1:numel(Data.Civ2_X);
+        end
+        [Data.Civ2_SubRange,Data.Civ2_NbCentres,Data.Civ2_Coord_tps,Data.Civ2_U_tps,Data.Civ2_V_tps,tild,Ures,Vres,tild,FFres]=...
+            filter_tps([Data.Civ2_X(ind_good) Data.Civ2_Y(ind_good)],Data.Civ2_U(ind_good),Data.Civ2_V(ind_good),[],Data.Patch2_SubDomainSize,Data.Patch2_FieldSmooth,Data.Patch2_MaxDiff);
+        Data.Civ2_U_smooth(ind_good)=Ures;
+        Data.Civ2_V_smooth(ind_good)=Vres;
+        Data.Civ2_FF(ind_good)=FFres;
+        Data.CivStage=Data.CivStage+1;
+        disp('patch2 performed')
+    end
+    
+    %% write result in a netcdf file if requested
+    if CheckOutputFile
+        errormsg=struct2nc(ncfile_out,Data);
+        if isempty(errormsg)
+            disp([ncfile_out ' written'])
+            %[success,msg] = fileattrib(ncfile_out ,'+w','g');% done in struct2nc
+        else
+            disp(errormsg)
+        end
+        disp(['ellapsed time ' num2str(toc/60,2) ' minutes'])
+    end
+end
+
+
+% 'civ': function piv.m adapted from PIVlab http://pivlab.blogspot.com/
+%--------------------------------------------------------------------------
+% function [xtable ytable utable vtable typevector] = civ (image1,image2,ibx,iby step, subpixfinder, mask, roi)
+%
+% OUTPUT:
+% xtable: set of x coordinates
+% ytable: set of y coordiantes
+% utable: set of u displacements (along x)
+% vtable: set of v displacements (along y)
+% ctable: max image correlation for each vector
+% typevector: set of flags, =1 for good, =0 for NaN vectors
+%
+%INPUT:
+% par_civ: structure of input parameters, with fields:
+%  .ImageA: first image for correlation (matrix)
+%  .ImageB: second image for correlation(matrix)
+%  .CorrBoxSize: 1,2 vector giving the size of the correlation box in x and y
+%  .SearchBoxSize:  1,2 vector giving the size of the search box in x and y
+%  .SearchBoxShift: 1,2 vector or 2 column matrix (for civ2) giving the shift of the search box in x and y
+%  .CorrSmooth: =1 or 2 determines the choice of the sub-pixel determination of the correlation max
+%  .ImageWidth: nb of pixels of the image in x
+%  .Dx, Dy: mesh for the PIV calculation
+%  .Grid: grid giving the PIV calculation points (alternative to .Dx .Dy): centres of the correlation boxes in Image A
+%  .Mask: name of a mask file or mask image matrix itself
+%  .MinIma: thresholds for image luminosity
+%  .MaxIma
+%  .CheckDeformation=1 for subpixel interpolation and image deformation (linear transform)
+%  .DUDX: matrix of deformation obtained from patch at each grid point
+%  .DUDY
+%  .DVDX:
+%  .DVDY
+
+function [xtable,ytable,utable,vtable,ctable,F,result_conv,errormsg] = civ (par_civ)
+
+%% prepare measurement grid
+if isfield(par_civ,'Grid')% grid points set as input, central positions of the sub-images in image A
+    if ischar(par_civ.Grid)%read the grid file if the input is a file name (grid in x, y image coordinates)
+        par_civ.Grid=dlmread(par_civ.Grid);
+        par_civ.Grid(1,:)=[];%the first line must be removed (heading in the grid file)
+    end
+    % else par_civ.Grid is already an array, no action here
+else% automatic grid in x, y image coordinates
+    minix=floor(par_civ.Dx/2)-0.5;
+    maxix=minix+par_civ.Dx*floor((par_civ.ImageWidth-1)/par_civ.Dx);
+    miniy=floor(par_civ.Dy/2)-0.5;% first automatic grid point at half the mesh Dy
+    maxiy=minix+par_civ.Dy*floor((par_civ.ImageHeight-1)/par_civ.Dy);
+    [GridX,GridY]=meshgrid(minix:par_civ.Dx:maxix,miniy:par_civ.Dy:maxiy);
+    par_civ.Grid(:,1)=reshape(GridX,[],1);
+    par_civ.Grid(:,2)=reshape(GridY,[],1);% increases with array index
+end
+nbvec=size(par_civ.Grid,1);
+
+%% prepare correlation and search boxes
+ibx2=floor(par_civ.CorrBoxSize(1)/2);
+iby2=floor(par_civ.CorrBoxSize(2)/2);
+isx2=floor(par_civ.SearchBoxSize(1)/2);
+isy2=floor(par_civ.SearchBoxSize(2)/2);
+shiftx=round(par_civ.SearchBoxShift(:,1));%use the input shift estimate, rounded to the next integer value
+shifty=-round(par_civ.SearchBoxShift(:,2));% sign minus because image j index increases when y decreases
+if numel(shiftx)==1% case of a unique shift for the whole field( civ1)
+    shiftx=shiftx*ones(nbvec,1);
+    shifty=shifty*ones(nbvec,1);
+end
+
+%% Array initialisation and default output  if par_civ.CorrSmooth=0 (just the grid calculated, no civ computation)
+xtable=round(par_civ.Grid(:,1)+0.5)-0.5;
+ytable=round(par_civ.ImageHeight-par_civ.Grid(:,2)+0.5)-0.5;% y index corresponding to the position in image coordiantes
+utable=shiftx;%zeros(nbvec,1);
+vtable=shifty;%zeros(nbvec,1);
+ctable=zeros(nbvec,1);
+F=zeros(nbvec,1);
+result_conv=[];
+errormsg='';
+
+%% prepare mask
+if isfield(par_civ,'Mask') && ~isempty(par_civ.Mask)
+    if strcmp(par_civ.Mask,'all')
+        return    % get the grid only, no civ calculation
+    elseif ischar(par_civ.Mask)
+        par_civ.Mask=imread(par_civ.Mask);% read the mask if not allready done
+    end
+end
+check_MinIma=isfield(par_civ,'MinIma');% test for image luminosity threshold
+check_MaxIma=isfield(par_civ,'MaxIma') && ~isempty(par_civ.MaxIma);
+
+par_civ.ImageA=sum(double(par_civ.ImageA),3);%sum over rgb component for color images
+par_civ.ImageB=sum(double(par_civ.ImageB),3);
+[npy_ima npx_ima]=size(par_civ.ImageA);
+if ~isequal(size(par_civ.ImageB),[npy_ima npx_ima])
+    errormsg='image pair with unequal size';
+    return
+end
+
+%% Apply mask
+% Convention for mask IDEAS TO IMPLEMENT ?
+% mask >200 : velocity calculated
+%  200 >=mask>150;velocity not calculated, interpolation allowed (bad spots)
+% 150>=mask >100: velocity not calculated, nor interpolated
+%  100>=mask> 20: velocity not calculated, impermeable (no flux through mask boundaries)
+%  20>=mask: velocity=0
+checkmask=0;
+MinA=min(min(par_civ.ImageA));
+%MinB=min(min(par_civ.ImageB));
+%check_undefined=false(size(par_civ.ImageA));
+if isfield(par_civ,'Mask') && ~isempty(par_civ.Mask)
+    checkmask=1;
+    if ~isequal(size(par_civ.Mask),[npy_ima npx_ima])
+        errormsg='mask must be an image with the same size as the images';
+        return
+    end
+    check_undefined=(par_civ.Mask<200 & par_civ.Mask>=20 );
+    %     par_civ.ImageA(check_undefined)=0;% put image A to zero (i.e. the min image value) in the undefined  area
+    %     par_civ.ImageB(check_undefined)=0;% put image B to zero (i.e. the min image value) in the undefined  area
+end
+
+%% compute image correlations: MAINLOOP on velocity vectors
+corrmax=0;
+sum_square=1;% default
+mesh=1;% default
+CheckDeformation=isfield(par_civ,'CheckDeformation')&& par_civ.CheckDeformation==1;
+if CheckDeformation
+    mesh=0.25;%mesh in pixels for subpixel image interpolation (x 4 in each direction)
+    par_civ.CorrSmooth=2;% use SUBPIX2DGAUSS (take into account more points near the max)
+end
+
+if par_civ.CorrSmooth~=0 % par_civ.CorrSmooth=0 implies no civ computation (just input image and grid points given)
+    for ivec=1:nbvec
+        iref=round(par_civ.Grid(ivec,1)+0.5);% xindex on the image A for the middle of the correlation box
+        jref=round(par_civ.ImageHeight-par_civ.Grid(ivec,2)+0.5);%  j index  for the middle of the correlation box in the image A
+        F(ivec)=0;
+        subrange1_x=iref-ibx2:iref+ibx2;% x indices defining the first subimage
+        subrange1_y=jref-iby2:jref+iby2;% y indices defining the first subimage
+        subrange2_x=iref+shiftx(ivec)-isx2:iref+shiftx(ivec)+isx2;%x indices defining the second subimage
+        subrange2_y=jref+shifty(ivec)-isy2:jref+shifty(ivec)+isy2;%y indices defining the second subimage
+        image1_crop=MinA*ones(numel(subrange1_y),numel(subrange1_x));% default value=min of image A
+        image2_crop=MinA*ones(numel(subrange2_y),numel(subrange2_x));% default value=min of image A
+        check1_x=subrange1_x>=1 & subrange1_x<=par_civ.ImageWidth;% check which points in the subimage 1 are contained in the initial image 1
+        check1_y=subrange1_y>=1 & subrange1_y<=par_civ.ImageHeight;
+        check2_x=subrange2_x>=1 & subrange2_x<=par_civ.ImageWidth;% check which points in the subimage 2 are contained in the initial image 2
+        check2_y=subrange2_y>=1 & subrange2_y<=par_civ.ImageHeight;
+        image1_crop(check1_y,check1_x)=par_civ.ImageA(subrange1_y(check1_y),subrange1_x(check1_x));%extract a subimage (correlation box) from image A
+        image2_crop(check2_y,check2_x)=par_civ.ImageB(subrange2_y(check2_y),subrange2_x(check2_x));%extract a larger subimage (search box) from image B
+        if checkmask
+            mask1_crop=ones(numel(subrange1_y),numel(subrange1_x));% default value=1 for mask
+            mask2_crop=ones(numel(subrange2_y),numel(subrange2_x));% default value=1 for mask
+            mask1_crop(check1_y,check1_x)=check_undefined(subrange1_y(check1_y),subrange1_x(check1_x));%extract a mask subimage (correlation box) from image A
+            mask2_crop(check2_y,check2_x)=check_undefined(subrange2_y(check2_y),subrange2_x(check2_x));%extract a mask subimage (search box) from image B
+            sizemask=sum(sum(mask1_crop))/(numel(subrange1_y)*numel(subrange1_x));%size of the masked part relative to the correlation sub-image
+            if sizemask > 1/2% eliminate point if more than half of the correlation box is masked
+                F(ivec)=3; %
+                utable(ivec)=0;
+                vtable(ivec)=0;
+            else
+                image1_crop=image1_crop.*~mask1_crop;% put to zero the masked pixels (mask1_crop='true'=1)
+                image2_crop=image2_crop.*~mask2_crop;
+                image1_mean=mean(mean(image1_crop))/(1-sizemask);
+                image2_mean=mean(mean(image2_crop))/(1-sizemask);
+            end
+        else
+            image1_mean=mean(mean(image1_crop));
+            image2_mean=mean(mean(image2_crop));
+        end
+        %threshold on image minimum
+        if F(ivec)~=3
+            if check_MinIma && (image1_mean < par_civ.MinIma || image2_mean < par_civ.MinIma)
+                F(ivec)=3;
+                %threshold on image maximum
+            elseif check_MaxIma && (image1_mean > par_civ.MaxIma || image2_mean > par_civ.MaxIma)
+                F(ivec)=3;
+            end
+            if F(ivec)==3
+                utable(ivec)=0;
+                vtable(ivec)=0;
+            else
+                %mask
+                if checkmask
+                    image1_crop=(image1_crop-image1_mean).*~mask1_crop;%substract the mean, put to zero the masked parts
+                    image2_crop=(image2_crop-image2_mean).*~mask2_crop;
+                else
+                    image1_crop=(image1_crop-image1_mean);
+                    image2_crop=(image2_crop-image2_mean);
+                end
+                %deformation
+                if CheckDeformation
+                    xi=(1:mesh:size(image1_crop,2));
+                    yi=(1:mesh:size(image1_crop,1))';
+                    [XI,YI]=meshgrid(xi-ceil(size(image1_crop,2)/2),yi-ceil(size(image1_crop,1)/2));
+                    XIant=XI-par_civ.DUDX(ivec)*XI+par_civ.DUDY(ivec)*YI+ceil(size(image1_crop,2)/2);
+                    YIant=YI+par_civ.DVDX(ivec)*XI-par_civ.DVDY(ivec)*YI+ceil(size(image1_crop,1)/2);
+                    image1_crop=interp2(image1_crop,XIant,YIant);
+                    image1_crop(isnan(image1_crop))=0;
+                    xi=(1:mesh:size(image2_crop,2));
+                    yi=(1:mesh:size(image2_crop,1))';
+                    image2_crop=interp2(image2_crop,xi,yi,'*spline');
+                    image2_crop(isnan(image2_crop))=0;
+                end
+                sum_square=sum(sum(image1_crop.*image1_crop));
+                %reference: Oliver Pust, PIV: Direct Cross-Correlation
+                result_conv= conv2(image2_crop,flipdim(flipdim(image1_crop,2),1),'valid');
+                corrmax= max(max(result_conv));
+                result_conv=(result_conv/corrmax)*255; %normalize, peak=always 255
+                %Find the correlation max, at 255
+                [y,x] = find(result_conv==255,1);
+                subimage2_crop=image2_crop(y:y+2*iby2/mesh,x:x+2*ibx2/mesh);%subimage of image 2 corresponding to the optimum displacement of first image
+                sum_square=sum_square*sum(sum(subimage2_crop.*subimage2_crop));% product of variances of image 1 and 2
+                sum_square=sqrt(sum_square);% srt of the variance product to normalise correlation
+                if ~isempty(y) && ~isempty(x)
+                    try
+                        if par_civ.CorrSmooth==1
+                            [vector,F(ivec)] = SUBPIXGAUSS (result_conv,x,y);
+                        elseif par_civ.CorrSmooth==2
+                            [vector,F(ivec)] = SUBPIX2DGAUSS (result_conv,x,y);
+                        else
+                            [vector,F(ivec)] = quadr_fit(result_conv,x,y);
+                        end
+                        utable(ivec)=vector(1)*mesh+shiftx(ivec);
+                        vtable(ivec)=vector(2)*mesh+shifty(ivec);
+                        xtable(ivec)=iref+utable(ivec)/2-0.5;% convec flow (velocity taken at the point middle from imgae 1 and 2)
+                        ytable(ivec)=jref+vtable(ivec)/2-0.5;% and position of pixel 1=0.5 (convention for image coordinates=0 at the edge)
+                        iref=round(xtable(ivec)+0.5);% nearest image index for the middle of the vector
+                        jref=round(ytable(ivec)+0.5);
+                        % eliminate vectors located in the mask
+                        if  checkmask && (iref<1 || jref<1 ||iref>npx_ima || jref>npy_ima ||( par_civ.Mask(jref,iref)<200 && par_civ.Mask(jref,iref)>=100))
+                            utable(ivec)=0;
+                            vtable(ivec)=0;
+                            F(ivec)=3;
+                        end
+                        ctable(ivec)=corrmax/sum_square;% correlation value
+                    catch ME
+                        F(ivec)=3;
+                        disp(ME.message)
+                    end
+                else
+                    F(ivec)=3;
+                end
+            end
+        end
+    end
+end
+result_conv=result_conv*corrmax/(255*sum_square);% keep the last correlation matrix for output
+
+%------------------------------------------------------------------------
+% --- Find the maximum of the correlation function after interpolation
+% OUPUT:
+% vector = optimum displacement vector with subpixel correction
+% F =flag: =0 OK
+%           =-2 , warning: max too close to the edge of the search box (1 pixel margin)
+% INPUT:
+% x,y: position of the maximum correlation at integer values
+
+function [vector,F] = SUBPIXGAUSS (result_conv,x,y)
+%------------------------------------------------------------------------
+% vector=[0 0]; %default
+F=0;
+[npy,npx]=size(result_conv);
+result_conv(result_conv<1)=1; %set to 1 correlation values smaller than 1  (=0 by discretisation, to avoid divergence in the log)
+%the following 8 lines are copyright (c) 1998, Uri Shavit, Roi Gurka, Alex Liberzon, Technion ??? Israel Institute of Technology
+%http://urapiv.wordpress.com
+peaky = y;
+if y < npy && y > 1
+    f0 = log(result_conv(y,x));
+    f1 = log(result_conv(y-1,x));
+    f2 = log(result_conv(y+1,x));
+    peaky = peaky+ (f1-f2)/(2*f1-4*f0+2*f2);
+else
+    F=-2; % warning flag for vector truncated by the limited search box
+end
+peakx=x;
+if x < npx-1 && x > 1
+    f0 = log(result_conv(y,x));
+    f1 = log(result_conv(y,x-1));
+    f2 = log(result_conv(y,x+1));
+    peakx = peakx+ (f1-f2)/(2*f1-4*f0+2*f2);
+else
+    F=-2; % warning flag for vector truncated by the limited search box
+end
+vector=[peakx-floor(npx/2)-1 peaky-floor(npy/2)-1];
+
+%------------------------------------------------------------------------
+% --- Find the maximum of the correlation function after interpolation
+function [vector,F] = SUBPIX2DGAUSS (result_conv,x,y)
+%------------------------------------------------------------------------
+% vector=[0 0]; %default
+F=-2;
+peaky=y;
+peakx=x;
+result_conv(result_conv<1)=1; %set to 1 correlation values smaller than 1 (to avoid divergence in the log)
+[npy,npx]=size(result_conv);
+if (x < npx) && (y < npy) && (x > 1) && (y > 1)
+    F=0;
+    for i=-1:1
+        for j=-1:1
+            %following 15 lines based on
+            %H. Nobach ??? M. Honkanen (2005)
+            %Two-dimensional Gaussian regression for sub-pixel displacement
+            %estimation in particle image velocimetry or particle position
+            %estimation in particle tracking velocimetry
+            %Experiments in Fluids (2005) 38: 511???515
+            c10(j+2,i+2)=i*log(result_conv(y+j, x+i));
+            c01(j+2,i+2)=j*log(result_conv(y+j, x+i));
+            c11(j+2,i+2)=i*j*log(result_conv(y+j, x+i));
+            c20(j+2,i+2)=(3*i^2-2)*log(result_conv(y+j, x+i));
+            c02(j+2,i+2)=(3*j^2-2)*log(result_conv(y+j, x+i));
+        end
+    end
+    c10=(1/6)*sum(sum(c10));
+    c01=(1/6)*sum(sum(c01));
+    c11=(1/4)*sum(sum(c11));
+    c20=(1/6)*sum(sum(c20));
+    c02=(1/6)*sum(sum(c02));
+    deltax=(c11*c01-2*c10*c02)/(4*c20*c02-c11^2);
+    deltay=(c11*c10-2*c01*c20)/(4*c20*c02-c11^2);
+    if abs(deltax)<1
+        peakx=x+deltax;
+    end
+    if abs(deltay)<1
+        peaky=y+deltay;
+    end
+end
+vector=[peakx-floor(npx/2)-1 peaky-floor(npy/2)-1];
+
+%------------------------------------------------------------------------
+% --- Find the maximum of the correlation function after quadratic interpolation
+function [vector,F] = quadr_fit(result_conv,x,y)
+[npy,npx]=size(result_conv);
+if x<4 || y<4 || npx-x<4 ||npy-y <4
+    F=-2;
+    vector=[x y];
+else
+    F=0;
+    x_ind=x-4:x+4;
+    y_ind=y-4:y+4;
+    x_vec=0.25*(x_ind-x);
+    y_vec=0.25*(y_ind-y);
+    [X,Y]=meshgrid(x_vec,y_vec);
+    coord=[reshape(X,[],1) reshape(Y,[],1)];
+    result_conv=reshape(result_conv(y_ind,x_ind),[],1);
+    
+    
+    % n=numel(X);
+    % x=[X Y];
+    % X=X-0.5;
+    % Y=Y+0.5;
+    % y = (X.*X+2*Y.*Y+X.*Y+6) + 0.1*rand(n,1);
+    p = polyfitn(coord,result_conv,2);
+    A(1,1)=2*p.Coefficients(1);
+    A(1,2)=p.Coefficients(2);
+    A(2,1)=p.Coefficients(2);
+    A(2,2)=2*p.Coefficients(4);
+    vector=[x y]'-A\[p.Coefficients(3) p.Coefficients(5)]';
+    vector=vector'-[floor(npx/2) floor(npy/2)]-1 ;
+    % zg = polyvaln(p,coord);
+    % figure
+    % surf(x_vec,y_vec,reshape(zg,9,9))
+    % hold on
+    % plot3(X,Y,reshape(result_conv,9,9),'o')
+    % hold off
+end
+
+%'RUN_FIX': function for fixing velocity fields:
+%-----------------------------------------------
+% RUN_FIX(filename,field,flagindex,thresh_vecC,thresh_vel,iter,flag_mask,maskname,fileref,fieldref)
+%
+%filename: name of the netcdf file (used as input and output)
+%field: structure specifying the names of the fields to fix (depending on civ1 or civ2)
+%.vel_type='civ1' or 'civ2';
+%.nb=name of the dimension common to the field to fix ('nb_vectors' for civ1);
+%.fixflag=name of fix flag variable ('vec_FixFlag' for civ1)
+%flagindex: flag specifying which values of vec_f are removed:
+% if flagindex(1)=1: vec_f=-2 vectors are removed
+% if flagindex(2)=1: vec_f=3 vectors are removed
+% if flagindex(3)=1: vec_f=2 vectors are removed (if iter=1) or vec_f=4 vectors are removed (if iter=2)
+%iter=1 for civ1 fields and iter=2 for civ2 fields
+%thresh_vecC: threshold in the image correlation vec_C
+%flag_mask: =1 mask used to remove vectors (0 else)
+%maskname: name of the mask image file for fix
+%thresh_vel: threshold on velocity, or on the difference with the reference file fileref if exists
+%inf_sup=1: remove values smaller than threshold thresh_vel, =2, larger than threshold
+%fileref: .nc file name for a reference velocity (='': refrence 0 used)
+%fieldref: 'civ1','filter1'...feld used in fileref
+
+function FF=fix(Param,F,C,U,V,X,Y)
+FF=zeros(size(F));%default
+
+%criterium on warn flags
+FlagName={'CheckFmin2','CheckF2','CheckF3','CheckF4'};
+FlagVal=[-2 2 3 4];
+for iflag=1:numel(FlagName)
+    if isfield(Param,FlagName{iflag}) && Param.(FlagName{iflag})
+        FF=(FF==1| F==FlagVal(iflag));
+    end
+end
+%criterium on correlation values
+if isfield (Param,'MinCorr')
+    FF=FF==1 | C<Param.MinCorr;
+end
+if (isfield(Param,'MinVel')&&~isempty(Param.MinVel))||(isfield (Param,'MaxVel')&&~isempty(Param.MaxVel))
+    Umod= U.*U+V.*V;
+    if isfield (Param,'MinVel')&&~isempty(Param.MinVel)
+        FF=FF==1 | Umod<(Param.MinVel*Param.MinVel);
+    end
+    if isfield (Param,'MaxVel')&&~isempty(Param.MaxVel)
+        FF=FF==1 | Umod>(Param.MaxVel*Param.MaxVel);
+    end
+end
+
+
+%------------------------------------------------------------------------
+% --- determine the list of index pairs of processing file
+function [i1_series,i2_series,j1_series,j2_series,check_bounds,NomTypeNc]=...
+    find_pair_indices(str_civ,i_series,j_series,MinIndex_i,MaxIndex_i,MinIndex_j,MaxIndex_j)
+%------------------------------------------------------------------------
+i1_series=i_series;% set of first image indexes
+i2_series=i_series;
+j1_series=j_series;%ones(size(i_series));% set of first image numbers
+j2_series=j_series;%ones(size(i_series));
+r=regexp(str_civ,'^\D(?<ind>[i|j])=( -| )(?<num1>\d+)\|(?<num2>\d+)','names');
+if ~isempty(r)
+    mode=['D' r.ind];
+    ind1=str2num(r.num1);
+    ind2=str2num(r.num2);
+else
+    mode='j1-j2';
+    r=regexp(str_civ,'^j= (?<num1>[a-z])-(?<num2>[a-z])','names');
+    if ~isempty(r)
+        NomTypeNc='_1ab';
+    else
+        r=regexp(str_civ,'^j= (?<num1>[A-Z])-(?<num2>[A-Z])','names');
+        if ~isempty(r)
+            NomTypeNc='_1AB';
+        else
+            r=regexp(str_civ,'^j= (?<num1>\d+)-(?<num2>\d+)','names');
+            if ~isempty(r)
+                NomTypeNc='_1_1-2';
+            end
+        end
+    end
+    if isempty(r)
+        display('wrong pair mode input option')
+    else
+        ind1=stra2num(r.num1);
+        ind2=stra2num(r.num2);
+    end
+end
+switch mode
+    case 'Di'
+        i1_series=i_series-ind1;% set of first image numbers
+        i2_series=i_series+ind2;
+        check_bounds=i1_series<MinIndex_i | i2_series>MaxIndex_i;
+        if isempty(j_series)
+            NomTypeNc='_1-2';
+        else
+            j1_series=j_series;
+            j2_series=j_series;
+            NomTypeNc='_1-2_1';
+        end
+    case 'Dj'
+        j1_series=j_series-ind1;
+        j2_series=j_series+ind2;
+        check_bounds=j1_series<MinIndex_j | j2_series>MaxIndex_j;
+        NomTypeNc='_1_1-2';
+    otherwise %bursts
+        i1_series=i_series(1,:);% do not sweep the j index
+        i2_series=i_series(1,:);
+        j1_series=ind1*ones(1,size(i_series,2));% j index is fixed by pair choice
+        j2_series=ind2*ones(1,size(i_series,2));
+        check_bounds=zeros(size(i1_series));% no limitations due to min-max indices
+end
+
+
+
+
Index: /trunk/src/series/civ_series.m
===================================================================
--- /trunk/src/series/civ_series.m	(revision 1088)
+++ /trunk/src/series/civ_series.m	(revision 1089)
@@ -54,4 +54,5 @@
     addpath(fullfile(path_series,'series'))
     Data=civ_input(Param);% introduce the civ parameters using the GUI civ_input
+    %Data=civ_input_App(Param);% introduce the civ parameters using the GUI civ_input
     if isempty(Data)
         Data=Param;% if  civ_input has been cancelled, keep previous parameters
Index: /trunk/src/series/ima2vol.m
===================================================================
--- /trunk/src/series/ima2vol.m	(revision 1088)
+++ /trunk/src/series/ima2vol.m	(revision 1089)
@@ -137,5 +137,5 @@
         end
         VolData.A(jfile,:,:)=Data.A;%concacene along y
-        VolData.Coord_z(jfile)=Data.PlaneCoord(3);
+        VolData.Coord_z(jfile)=XmlData{1}.GeometryCalib.SliceCoord(jfile,3);
     end
 %         npx=size(Data.A,2);
