Index: trunk/src/series/civ_input.m
===================================================================
--- trunk/src/series/civ_input.m	(revision 855)
+++ trunk/src/series/civ_input.m	(revision 856)
@@ -165,4 +165,5 @@
     set(handles.ref_j,'String',num2str(Param.IndexRange.first_j))
 end
+set(handles.ConfigSource,'String','\default')
 
 %%  set the menus of image pairs and default selection for civ_input   %%%%%%%%%%%%%%%%%%%
@@ -340,5 +341,5 @@
 set(handles.civ_input,'WindowStyle','modal')% Make the GUI modal
 drawnow
-uiwait(handles.civ_input);
+uiwait(handles.civ_input);% wait for OK action to end the function
 
 
@@ -368,13 +369,73 @@
 %------------------------------------------------------------------------
 function SetDefaultParam_Callback(hObject, eventdata, handles)
-    
-hseries=findobj(allchild(0),'Name','series');% look for the GUI series
-hhseries=guidata(hseries);%handles of elements in the GUI series
-SeriesData=get(hseries,'UserData');%read parameters on the GUI series
-if isfield (SeriesData,'ActionInput')
-    SeriesData=rmfield(SeriesData,'ActionInput');% remove recorded civ parameters
-end
-set(hseries,'UserData',SeriesData)
-series('ActionName_Callback',hObject,eventdata,hhseries); %
+
+Param.ConfigSource='\default';
+
+%% Civ1 parameters
+Param.Civ1.CorrBoxSize=[25 25];
+Param.Civ1.SearchBoxSize=[55 55];
+Param.Civ1.SearchBoxShift=[0 0];
+Param.Civ1.CorrSmooth=1;
+Param.Civ1.Dx=20;
+Param.Civ1.Dy=20;
+Param.Civ1.CheckGrid=0;
+Param.Civ1.CheckMask=0;
+Param.Civ1.CheckThreshold=0;
+Param.Civ1.TestCiv1=0;
+
+%% Fix1 parameters
+Param.Fix1.CheckFmin2=1;
+Param.Fix1.CheckF3=1;
+Param.Fix1.MinCorr=0.2000;
+
+%% Patch1 parameters
+Param.Patch1.FieldSmooth=10;
+Param.Patch1.MaxDiff=1.5000;
+Param.Patch1.SubDomainSize=1000;
+Param.Patch1.TestPatch1=0;
+
+%% Civ2 parameters
+Param.Civ2.CorrBoxSize=[21 21];
+Param.Civ2.SearchBoxSize=[31 31];
+Param.Civ2.CorrSmooth=1;
+Param.Civ2.Dx=10;
+Param.Civ2.Dy=10;
+Param.Civ2.CheckGrid=0;
+Param.Civ2.CheckMask=0;
+Param.Civ2.CheckThreshold=0;
+Param.Civ2.TestCiv2=0;
+
+%% Fix2 parameters
+Param.Fix2.CheckFmin2=1;
+Param.Fix2.CheckF4=1;
+Param.Fix2.CheckF3=1;
+Param.Fix2.MinCorr=0.2000;
+
+%% Patch2 parameters
+Param.Patch2.FieldSmooth=2;
+Param.Patch2.MaxDiff=1.5000;
+Param.Patch2.SubDomainSize=1000;
+Param.Patch2.TestPatch2=0;
+
+fill_GUI(Param,handles.civ_input)% fill the elements of the GUI series with the input parameters
+
+%update_CivOptions(handles,0)
+%set(handles.ConfigSource,'String','\default')
+
+% %Param=read_GUI(handles.civ_input)
+% hseries=findobj(allchild(0),'Name','series');% look for the GUI series
+% hhseries=guidata(hseries);%handles of elements in the GUI series
+% SeriesData=get(hseries,'UserData');%read parameters on the GUI series
+% if isfield (SeriesData,'ActionInput')
+%     SeriesData=rmfield(SeriesData,'ActionInput');% remove recorded civ parameters
+% end
+% set(hseries,'UserData',SeriesData)
+% %% exit the GUI and close it
+% %handles.output.ActionInput=rmfield(ActionInput;
+% guidata(hObject, handles);% Update handles structure
+% uiresume(handles.civ_input);
+% set(hhseries.ActionName,'BusyAction','cancel')
+% series('ActionName_Callback',hObject,eventdata,hhseries); %
+
 
 % -----------------------------------------------------------------------
@@ -771,4 +832,5 @@
 if isempty(errormsg)
     set(handles.ref_i,'BackgroundColor',[1 1 1])
+    set(handles.ref_j,'BackgroundColor',[1 1 1])
 else
     msgbox_uvmat('ERROR',errormsg)
@@ -827,5 +889,5 @@
 
 %% determine nom_type_nc, nomenclature type of the .nc files:
-[nom_type_nc]=nomtype2pair(nom_type_ima,mode);
+%[nom_type_nc]=nomtype2pair(nom_type_ima,mode);
 
 %% reads .nc subdirectoy and image numbers from the interface
@@ -920,5 +982,5 @@
 
 %% determine the default selection in the pair menu for Civ1
-ichoice=find(select,1);% index of selected pair
+ichoice=find(select,1);% index of first selected pair
 if (isempty(ichoice) || ichoice < 1); ichoice=1; end;
 initial=get(handles.ListPairCiv1,'Value');%initial choice of pair
@@ -1081,8 +1143,8 @@
 
 %------------------------------------------------------------------------
-% --- Executes on button press in CheckMask.
+% --- Executes on selection in menu CorrSmooth.
 function num_CorrSmooth_Callback(hObject, eventdata, handles)
 set(handles.configSource,'String','NEW')
-set(handles.configSource,'BackgroundColor',[1 0 1])
+set(handles.OK,'BackgroundColor',[1 0 1])
 %------------------------------------------------------------------------
 
@@ -1423,5 +1485,5 @@
 set(hObject,'BackgroundColor',[1 0 1])
 set(handles.configSource,'String','NEW')
-set(handles.configSource,'BackgroundColor',[1 0 1])
+set(handles.OK,'BackgroundColor',[1 0 1])
 %------------------------------------------------------------------------
 % --- Executes on button press in CheckMask: common to all panels (civ1, Civ2..)
@@ -1685,5 +1747,5 @@
 end
 set(handles.configSource,'String','NEW')
-set(handles.configSource,'BackgroundColor',[1 0 1])
+set(handles.OK,'BackgroundColor',[1 0 1])
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %%%%%%%%%%%%%%   TEST functions
@@ -1820,18 +1882,53 @@
 if get(handles.TestCiv2,'Value')
     set(handles.TestCiv2,'BackgroundColor',[1 1 0])% paint TestCiv1 button to yellow to confirm civ launch
-    % TO UPDATE TO UPDATE TO UPDATE TO UPDATE
-    [Data,Param.Civ2]=get_param_civ2(handles);
-    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
+      hseries=findobj(allchild(0),'Tag','series');
+     Param=read_GUI(hseries);
+     Param.Action.RUN=1;
+     Param.ActionInput=read_GUI(handles.civ_input);
+     Param=rmfield(Param,'OutputSubDir'); %remove output file option from civ_series
+     Param.ActionInput.Civ2.SmoothParam=0;% launch Civ2 with no data point (to get the image names for A and B)
+     [Data,errormsg]=civ_series(Param);% get the civ1+fix1 results
+     
+     %% create image data ImageData for display
+     ImageData.ListVarName={'ny','nx','A'};
+     ImageData.VarDimName= {'ny','nx',{'ny','nx'}};
+     ImageData.A=imread(Data.Civ2_ImageA); % read the first image
+     if ndims(ImageData.A)==3 %case of color image
+         ImageData.VarDimName= {'ny','nx',{'ny','nx','rgb'}};
+     end
+     ImageData.ny=[size(ImageData.A,1) 1];
+     ImageData.nx=[1 size(ImageData.A,2)];
+     ImageData.CoordUnit='pixel';% used to set equal scaling for x and y in image dispa=ly 
+
+     %% create the figure view_field for image visualization
+    hview_field=view_field(ImageData); %view the image in the GUI view_field 
     set(0,'CurrentFigure',hview_field)
+    RectCentre=squeeze(mean(Data.Civ1_SubRange,2));
+    for isub=1:size(Data.Civ1_SubRange,3);
+        pos_x=Data.Civ1_SubRange(1,1,isub);
+        pos_y=Data.Civ1_SubRange(2,1,isub);
+        width=Data.Civ1_SubRange(1,2,isub)-Data.Civ1_SubRange(1,1,isub);
+        height=Data.Civ1_SubRange(2,2,isub)-Data.Civ1_SubRange(2,1,isub);
+        rectangle('Position',[pos_x pos_y width height],'EdgeColor',[0 0 1])
+    end
     hhview_field=guihandles(hview_field);
     set(hview_field,'CurrentAxes',hhview_field.PlotAxes)
     ViewData=get(hview_field,'UserData');
     ViewData.CivHandle=handles.civ_input;% indicate the handle of the civ GUI in view_field
-    ViewData.PlotAxes.B=imread(Param.Civ1.ImageName_B);%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
+    ViewData.PlotAxes.B=imread(Data.Civ2_ImageB);%store the second image in the UserData of the GUI view_field
+    ViewData.PlotAxes.X=Data.Civ2_X';
+    ViewData.PlotAxes.Y=Data.Civ2_Y';
+    ViewData.PlotAxes.ShiftX=Data.Civ2_U';
+    ViewData.PlotAxes.ShiftY=Data.Civ2_V';
+    ViewData.PlotAxes.Civ1_SubRange=Data.Civ1_SubRange;
+    ViewData.PlotAxes.Civ1_NbCentres=Data.Civ1_NbCentres;
+    ViewData.PlotAxes.Civ1_Coord_tps=Data.Civ1_Coord_tps;
+    ViewData.PlotAxes.Civ1_U_tps=Data.Civ1_U_tps;
+    ViewData.PlotAxes.Civ1_V_tps=Data.Civ1_V_tps;
+    ViewData.PlotAxes.Civ1_Dt=Data.Civ1_Dt;
+    set(hview_field,'UserData',ViewData)% store the info in the UserData of image view_field
+    
+    %% look for a current figure for image correlation display
+    corrfig=findobj(allchild(0),'tag','corrfig');
     if isempty(corrfig)
         corrfig=figure;
@@ -1839,5 +1936,4 @@
         set(corrfig,'name','image correlation')
         set(corrfig,'DeleteFcn',{@closeview_field})%
-        % end
         set(handles.TestCiv1,'BackgroundColor',[1 0 0])
     else
@@ -1851,5 +1947,5 @@
             delete(hview_field)
         end
-    end
+    end   
 else
     hview_field=findobj(allchild(0),'Tag','view_field'); %view the image in the GUI view_field
@@ -2084,7 +2180,7 @@
 
 %------------------------------------------------------------------------
-% --- Executes on button press in InportParam.
-%------------------------------------------------------------------------
-function InportParam_Callback(hObject, eventdata, handles)
+% --- Executes on button press in ImportParam.
+%------------------------------------------------------------------------
+function ImportParam_Callback(hObject, eventdata, handles)
 hseries=findobj(allchild(0),'Tag','series');
 hhseries=guidata(hseries);
@@ -2116,7 +2212,7 @@
         if isfield(Param.ActionInput,'Program')&& strcmp(Param.ActionInput.Program,'civ_series')
             fill_GUI(Param.ActionInput,handles.civ_input)% fill the elements of the GUI series with the input parameters
+            set(handles.ConfigSource,'String',filexml)
             check_input=1;
-            update_CivOptions(handles,0)
-                
+            update_CivOptions(handles,0)             
         end
     end
@@ -2138,4 +2234,9 @@
 if isempty(find(strcmp(get(gco,'Tag'),ListExclude),1))% if the selected uicontrol is not in the Exclude list
     set(handles.ConfigSource,'String','NEW')% indicate that the configuration is new
-    set(handles.ConfigSource,'BackgroundColor',[1 0 1])%
-end
+    set(handles.OK,'BackgroundColor',[1 0 1])%
+    drawnow
+end
+
+
+% --- Executes on button press in TestPatch2.
+function TestPatch2_Callback(hObject, eventdata, handles)
Index: trunk/src/series/civ_series.m
===================================================================
--- trunk/src/series/civ_series.m	(revision 855)
+++ trunk/src/series/civ_series.m	(revision 856)
@@ -58,5 +58,5 @@
     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)
-    if isfield(Data,'ActionInput') && strcmp(Data.ActionInput.PairIndices.ListPairMode,'pair j1-j2')
+    if isfield(Data,'ActionInput') && isfield(Data.ActionInput,'PairIndices')&& strcmp(Data.ActionInput.PairIndices.ListPairMode,'pair j1-j2')
         Data.Desable_j_index='on';% hide the j index in series (set by the pair choice in civ_input)
     end
@@ -174,5 +174,5 @@
                 j1_series_Civ2=j1_series_Civ1;
                 j2_series_Civ2=j2_series_Civ1;
-                NomTypeNc=NomType;
+                NomTypeNc=Param.InputTable{1,4};
             case 'PIV volume'
                 % TODO, TODO
@@ -329,4 +329,5 @@
 
 %%%%% MAIN LOOP %%%%%%
+maskname='';% initiate the mask name
 for ifield=1:NbField
     if ~isempty(RUNHandle)% update the waitbar in interactive mode with GUI series  (checkrun=1)
@@ -360,5 +361,5 @@
     if isfield (Param.ActionInput,'Civ1')
         par_civ1=Param.ActionInput.Civ1;
-        if CheckInputFile % read input images (except in mode Test where it is introduced directly in Param.ActionInput.Civ1)
+        if CheckInputFile % read input images (except in mode Test where it is introduced directly in Param.ActionInput.Civ1.ImageNameA and B)
             try
                 ImageName_A=fullfile_uvmat(RootPath_A,SubDir_A,RootFile_A,FileExt_A,NomType_A,i1_series_Civ1(ifield),[],j1_series_Civ1(ifield));
@@ -422,5 +423,13 @@
         Data.VarAttribute{4}.Role='vector_y';
         Data.VarAttribute{5}.Role='warnflag';
-        
+        if par_civ1.CheckMask&&~isempty(par_civ1.Mask)
+            if strcmp(maskname,par_civ1.Mask)% mask exist, not already read in civ1
+                par_civ1.Mask=mask; %use mask already opened
+            else
+                par_civ1.Mask=imread(par_civ1.Mask);%update the mask, an store it for future use
+                mask=par_civ1.Mask;
+                maskname=par_civ1.Mask;
+            end
+        end
         if strcmp(Param.ActionInput.ListCompareMode, 'PIV volume')
             Data.ListVarName=[Data.ListVarName 'Civ1_Z'];
@@ -429,5 +438,5 @@
             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);
+                [xtable, ytable, utable, vtable, ctable, F, result_conv, errormsg] = civ (par_civ1);
                 if ~isempty(errormsg)
                      disp_uvmat('ERROR',errormsg,checkrun)
@@ -444,5 +453,5 @@
         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);
+            [xtable, ytable, utable, vtable, ctable, F, result_conv, errormsg] = civ (par_civ1);
             if ~isempty(errormsg)
                 disp_uvmat('ERROR',errormsg,checkrun)
@@ -473,5 +482,5 @@
                 [Data,tild,tild,errormsg]=nc2struct(CivFile);%read civ1 and fix1 data in the existing netcdf file
             end
-        else
+        elseif isfield(Param,'Civ1_X')
             Data.ListGlobalAttribute={};
             Data.ListVarName={};
@@ -572,54 +581,55 @@
     if isfield (Param.ActionInput,'Civ2')
         par_civ2=Param.ActionInput.Civ2;
-        par_civ2.ImageA=[];
-        par_civ2.ImageB=[];
-        i1=i1_series_Civ2(ifield);
-        i2=i1;
-        if ~isempty(i2_series_Civ2)
-            i2=i2_series_Civ2(ifield);
-        end
-        j1=1;
-        if ~isempty(j1_series_Civ2)
-            j1=j1_series_Civ2(ifield);
-        end
-        j2=j1;
-        if ~isempty(j2_series_Civ2)
-            j2=j2_series_Civ2(ifield);
-        end
-        ImageName_A_Civ2=fullfile_uvmat(RootPath_A,SubDir_A,RootFile_A,FileExt_A,NomType_A,i1,[],j1);
-
-        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,[],j2);
-        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     
-        
-        ncfile=fullfile_uvmat(RootPath_A,OutputDir,RootFile_A,'.nc',NomTypeNc,i1,i2,j1,j2);
-        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
-        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);
+        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=[];
+            i1=i1_series_Civ2(ifield);
+            i2=i1;
+            if ~isempty(i2_series_Civ2)
+                i2=i2_series_Civ2(ifield);
+            end
+            j1=1;
+            if ~isempty(j1_series_Civ2)
+                j1=j1_series_Civ2(ifield);
+            end
+            j2=j1;
+            if ~isempty(j2_series_Civ2)
+                j2=j2_series_Civ2(ifield);
+            end
+            ImageName_A_Civ2=fullfile_uvmat(RootPath_A,SubDir_A,RootFile_A,FileExt_A,NomType_A,i1,[],j1);
+            
+            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,[],j2);
+            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
+            if strcmp(Param.ActionInput.ListCompareMode,'PIV')
+                ncfile=fullfile_uvmat(RootPath_A,OutputDir,RootFile_A,'.nc',NomTypeNc,i1,i2,j1,j2);
+            else % displacement
+                ncfile=fullfile_uvmat(RootPath_A,OutputDir,RootFile_A,'.nc',NomTypeNc,i2,[],j2);
+            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
         if par_civ2.CheckDeformation
             DUDX=zeros(size(par_civ2.Grid,1),1);
@@ -629,29 +639,43 @@
         end
         
-         % get the guess from patch1 or patch2 (case 'CheckCiv3')
-        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;
-           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;
-           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;
-        end
-        NbSubDomain=size(SubRange,3);        
+        % 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;
+                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;
+                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;
+            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;
+                         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)
+        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
@@ -660,6 +684,6 @@
             epoints = par_civ2.Grid(ind_sel,:);% coordinates of interpolation sites
             ctrs=Coord_tps(1:nbvec_sub,:,isub) ;%(=initial points) ctrs
-            nbval(ind_sel)=nbval(ind_sel)+1;% records the number of values for eacn interpolation point (in case of subdomain overlap)
-            EM = tps_eval(epoints,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);
             Shiftx(ind_sel)=Shiftx(ind_sel)+EM*U_tps(1:nbvec_sub+3,isub);
             Shifty(ind_sel)=Shifty(ind_sel)+EM*V_tps(1:nbvec_sub+3,isub);
@@ -672,7 +696,12 @@
             end
         end
-        mask='';
-        if par_civ2.CheckMask&&~isempty(par_civ2.Mask)&& ~strcmp(maskname,par_civ2.Mask)% mask exist, not already read in civ1
-            mask=imread(par_civ2.Mask);
+        if par_civ2.CheckMask&&~isempty(par_civ2.Mask)
+            if strcmp(maskname,par_civ2.Mask)% mask exist, not already read in civ1
+                par_civ2.Mask=mask; %use mask already opened
+            else
+                par_civ2.Mask=imread(par_civ2.Mask);%update the mask, and store it for future use
+                mask=par_civ2.Mask;
+                maskname=par_civ2.Mask;
+            end
         end
         ibx2=ceil(par_civ2.CorrBoxSize(1)/2);
@@ -680,5 +709,12 @@
         par_civ2.SearchBoxSize(1)=2*ibx2+9;% search ara +-4 pixels around the guess
         par_civ2.SearchBoxSize(2)=2*iby2+9;
-        Civ2_Dt=time(i2+1,j2+1)-time(i1+1,j1+1);
+        if strcmp(Param.ActionInput.ListCompareMode,'displacement')
+            Civ1_Dt=1;
+            Civ2_Dt=1;
+        elseif exist('time','var')
+            Civ2_Dt=time(i2+1,j2+1)-time(i1+1,j1+1);
+        else 
+            Civ2_Dt=1;%TO CHECK, TEST
+        end
         par_civ2.SearchBoxShift=(Civ2_Dt/Civ1_Dt)*[Shiftx(nbval>=1)./nbval(nbval>=1) Shifty(nbval>=1)./nbval(nbval>=1)];
         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];% grid taken at the extrapolated origin of the displacement vectors
@@ -690,15 +726,18 @@
         end
         % calculate velocity data (y and v in indices, reverse to y component)
-        [xtable ytable utable vtable ctable F] = civ (par_civ2);
+        [xtable, ytable, utable, vtable, ctable, F,result_conv,errormsg] = civ (par_civ2);
+        
         list_param=(fieldnames(Param.ActionInput.Civ2))';
         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 
+        %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;
         Data.Civ2_Time=(time(i2+1,j2+1)+time(i1+1,j1+1))/2;
         Data.Civ2_Dt=Civ2_Dt;
-%         Data.Civ2_Time=1;
-%         Data.Civ2_Dt=1;
+        end
+        %         Data.Civ2_Time=1;
+        %         Data.Civ2_Dt=1;
         for ilist=1:length(list_param)
             Data.(Civ2_param{4+ilist})=Param.ActionInput.Civ2.(list_param{ilist});
@@ -725,5 +764,5 @@
     %% Fix2
     if isfield (Param.ActionInput,'Fix2')
-                list_param=fieldnames(Param.ActionInput.Fix2)';
+        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 
@@ -824,31 +863,31 @@
 %INPUT:
 % par_civ: structure of input parameters, with fields:
-%  .CorrBoxSize
-%  .SearchBoxSize
-%  .SearchBoxShift
-%  .ImageHeight
-%  .ImageWidth
-%  .Dx, Dy
-%  .Grid
-%  .Mask
-%  .MinIma
+%  .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)
+%  .Mask: name of a mask file or mask image matrix itself
+%  .MinIma: thresholds for image luminosity
 %  .MaxIma
-%  .image1:first image (matrix)
-% image2: second image (matrix)
-% ibx2,iby2: half size of the correlation box along x and y, in px (size=(2*iby2+1,2*ibx2+1)
-% isx2,isy2: half size of the search box along x and y, in px (size=(2*isy2+1,2*isx2+1)
-% shiftx, shifty: shift of the search box (in pixel index, yshift reversed)
-% step: mesh of the measurement points (in px)
-% subpixfinder=1 or 2 controls the curve fitting of the image correlation
-% mask: =[] for no mask
-% roi: 4 element vector defining a region of interest: x position, y position, width, height, (in image indices), for the whole image, roi=[];
+%  .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 
-    if ischar(par_civ.Grid)%read the drid file if the input is a file name
+    if ischar(par_civ.Grid)%read the grid file if the input is a file name
         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
 else% automatic grid
     minix=floor(par_civ.Dx/2)-0.5;
@@ -889,5 +928,5 @@
         return    % get the grid only, no civ calculation
     elseif ischar(par_civ.Mask)
-        par_civ.Mask=imread(par_civ.Mask);
+        par_civ.Mask=imread(par_civ.Mask);% read the mask if not allready done
     end
 end
@@ -948,97 +987,82 @@
 sum_square=1;% default
 mesh=1;% default
-CheckDecimal=isfield(par_civ,'CheckDecimal')&& par_civ.CheckDecimal==1;
-if CheckDecimal
-    mesh=0.2;%mesh in pixels for subpixel image interpolation
-    CheckDeformation=isfield(par_civ,'CheckDeformation')&& par_civ.CheckDeformation==1;
-end
-% vector=[0 0];%default
-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);% yindex on the image B for the middle of the correlation box
-    %if ~(checkmask && par_civ.Mask(jref,iref)<=20) %velocity not set to zero by the black mask
-    %         if jref-iby2<1 || jref+iby2>par_civ.ImageHeight|| iref-ibx2<1 || iref+ibx2>par_civ.ImageWidth||...
-    %               jref+shifty(ivec)-isy2<1||jref+shifty(ivec)+isy2>par_civ.ImageHeight|| iref+shiftx(ivec)-isx2<1 || iref+shiftx(ivec)+isx2>par_civ.ImageWidth  % we are outside the image
-    %             F(ivec)=3;
-    %         else
-    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
-    image1_mean=mean(mean(image1_crop));
-    image2_mean=mean(mean(image2_crop));
-    %threshold on image minimum
-    if check_MinIma && (image1_mean < par_civ.MinIma || image2_mean < par_civ.MinIma)
-        F(ivec)=3;
-    end
-    %threshold on image maximum
-    if check_MaxIma && (image1_mean > par_civ.MaxIma || image2_mean > par_civ.MaxIma)
-        F(ivec)=3;
-    end
-    %         end
-    if F(ivec)~=3
-        image1_crop=image1_crop-image1_mean;%substract the mean
-        image2_crop=image2_crop-image2_mean;
-        if CheckDecimal
-            xi=(1:mesh:size(image1_crop,2));
-            yi=(1:mesh:size(image1_crop,1))';
+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)
+end
+if par_civ.CorrSmooth~=0 % par_civ.CorrSmooth=0 implies no civ computation (just input image and grid points viven)
+    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);% yindex on the image B for the middle of the correlation box
+        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
+        image1_mean=mean(mean(image1_crop));
+        image2_mean=mean(mean(image2_crop));
+        %threshold on image minimum
+        if check_MinIma && (image1_mean < par_civ.MinIma || image2_mean < par_civ.MinIma)
+            F(ivec)=3;
+        end
+        %threshold on image maximum
+        if check_MaxIma && (image1_mean > par_civ.MaxIma || image2_mean > par_civ.MaxIma)
+            F(ivec)=3;
+        end
+        
+        if F(ivec)~=3
+            image1_crop=image1_crop-image1_mean;%substract the mean
+            image2_crop=image2_crop-image2_mean;
             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);
-            else
-                image1_crop=interp2(image1_crop,xi,yi);
-            end
-            xi=(1:mesh:size(image2_crop,2));
-            yi=(1:mesh:size(image2_crop,1))';
-            image2_crop=interp2(image2_crop,xi,yi);
-        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);
-        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);
-                end
-%                 if ~isfield(par_civ,'CheckDeformation')
-                utable(ivec)=vector(1)*mesh+shiftx(ivec); 
-                vtable(ivec)=vector(2)*mesh+shifty(ivec);
-%                 else
-%                                 utable(ivec)=shiftx(ivec);% TEST TEST !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-%                 vtable(ivec)=shifty(ivec);
-%                 end
-                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));% image index for the middle of the vector
-                jref=round(ytable(ivec));
-                if checkmask && par_civ.Mask(jref,iref)<200 && par_civ.Mask(jref,iref)>=100
-                    utable(ivec)=0;
-                    vtable(ivec)=0;
+                xi=(1:mesh:size(image2_crop,2));
+                yi=(1:mesh:size(image2_crop,1))';
+                image2_crop=interp2(image2_crop,xi,yi);
+            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);
+            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);
+                    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));% image index for the middle of the vector
+                    jref=round(ytable(ivec));
+                    if checkmask && 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;
                 end
-                ctable(ivec)=corrmax/sum_square;% correlation value
-            catch ME
+            else
                 F(ivec)=3;
             end
-        else
-            F(ivec)=3;
         end
     end
