Index: /trunk/src/check_files.m
===================================================================
--- /trunk/src/check_files.m	(revision 314)
+++ /trunk/src/check_files.m	(revision 315)
@@ -41,5 +41,5 @@
     'civ_3D';... function associated with the interface 'civ_3D.fig' for PIV in volume (in progress)
     'civ_3D.fig';...
-    'civ_uvmat';...% civ programs, Matlab version (called by civ.m, option Civuvmat)
+    'civ_matlab';...% civ programs, Matlab version (called by civ.m, option Civprogram/Matlab in the upper menu bar)
     'close_fig';...% function  activated when a figure is closed
     'copyfields';...% copy fields between two matlab structures
@@ -71,5 +71,4 @@
     'peaklock';...%
     'phys_XYZ';...% transform coordiantes from pixels to phys
-    'pivlab';...% PIV program (Civ1), called by civ_uvmat
     'px_XYZ';...% transform coordiantes from phys to pixels
     'plot_field';...%displays a vector field and/or scalar or images
Index: /trunk/src/civ.m
===================================================================
--- /trunk/src/civ.m	(revision 314)
+++ /trunk/src/civ.m	(revision 315)
@@ -23,5 +23,5 @@
 %TODO: search range
 
-% Last Modified by GUIDE v2.5 30-Nov-2011 21:52:52
+% Last Modified by GUIDE v2.5 03-Dec-2011 16:08:25
 % Begin initialization code - DO NOT EDIT
 gui_Singleton = 1;
@@ -533,4 +533,8 @@
     ind_opening=2;% propose 'fix' as the default option
     Data=nc2struct(fileinput,'ListGlobalAttribute','CivStage','absolut_time_T0','fix','patch','civ2','fix2');
+    if isfield(Data,'Txt')
+        msgbox_uvmat('ERROR',Data.Txt)
+        return
+    end
     if ~isempty(Data.CivStage)%test for civ files
         ind_opening=Data.CivStage;
@@ -959,5 +963,5 @@
 %could be included in get_mask callback ?
 if isequal(get(handles.CheckMask,'Value'),1)
-    maskname=get(handles.txt_MaskName,'String');
+    maskname=get(handles.txt_Mask,'String');
     if ~exist(maskname,'file')
         get_mask_civ1_Callback(hObject, eventdata, handles);
@@ -965,5 +969,5 @@
 end
 if isequal(get(handles.CheckMask,'Value'),1)
-    maskname=get(handles.txt_MaskName,'String');
+    maskname=get(handles.txt_Mask,'String');
     if ~exist(maskname,'file')
         get_mask_fix1_Callback(hObject, eventdata, handles);
@@ -977,5 +981,5 @@
 end
 if isequal(get(handles.CheckMask,'Value'),1)
-    maskname=get(handles.txt_MaskName,'String');
+    maskname=get(handles.txt_Mask,'String');
     if ~exist(maskname,'file')
         get_mask_fix2_Callback(hObject, eventdata, handles);
@@ -1097,5 +1101,4 @@
 %% MAIN LOOP
 time=get(handles.ImaDoc,'UserData'); %get the set of times
-super_cmd=[];
 batch_file_list=[];
  
@@ -1129,61 +1132,42 @@
         if Param.CheckCiv1
             % read image-dependent parameters
-            Param.Civ1.filename_ima_a=filecell.ima1.civ1{ifile,j};
-            Param.Civ1.filename_ima_b=filecell.ima2.civ1{ifile,j};
+            Param.Civ1.ImageA=filecell.ima1.civ1{ifile,j};
+            Param.Civ1.ImageB=filecell.ima2.civ1{ifile,j};
             if size(time,1)>=i2_civ1(ifile) && size(time,2)>=j2_civ1(j)
                 Param.Civ1.Dt=(time(i2_civ1(ifile),j2_civ1(j))-time(i1_civ1(ifile),j1_civ1(j)));
-                Param.Civ1.T0=((time(i2_civ1(ifile),j2_civ1(j))+time(i1_civ1(ifile),j1_civ1(j)))/2);
+                Param.Civ1.Time=((time(i2_civ1(ifile),j2_civ1(j))+time(i1_civ1(ifile),j1_civ1(j)))/2);
             else
                 Param.Civ1.Dt=1;
-                Param.Civ1.T0=0;
+                Param.Civ1.Time=0;
             end
             Param.Civ1.term_a=num2stra(j1_civ1(j),nom_type_nc);%UTILITE?
-            Param.Civ1.term_b=num2stra(j2_civ1(j),nom_type_nc);%
-            Param.Civ1.pxcmx=1; %velocities are expressed in pixel dispalcement
-            Param.Civ1.pxcmy=1;      
-            Param.Civ1.ImageInfo=imfinfo(filecell.ima1.civ1{1,1});%read the first image to get the size
-            
-            %TODO : civ should not need npx and npy
-            
+            Param.Civ1.term_b=num2stra(j2_civ1(j),nom_type_nc);%    
+            ImageInfo=imfinfo(filecell.ima1.civ1{1,1});%read the first image to get the size
+            Param.Civ1.ImageWidth=ImageInfo.Width;
+            Param.Civ1.ImageHeight=ImageInfo.Height;
+            Param.Civ1.ImageBitDepth=ImageInfo.BitDepth;
             % read mask parameters
             if Param.Civ1.CheckMask % the lines below should be changed with the new gui
-                %matbe they when check_mask the Maskname is read
-                if exist(Param.Civ1.MaskName,'file')
-                    Param.Civ1.MaskFlag='y';
-                else
-                    maskbase=[filecell.filebase '_' Param.Civ1.MaskName]; %
-                    nbslice_mask=str2double(maskdispl(1:end-4)); %
+                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;
-                    Param.Civ1.MaskName=name_generator(maskbase,i1_mask,1,'.png','_i');
-                    if exist(Param.Civ1.MaskName,'file')
-                        Param.Civ1.maskflag='y';
-                    else
-                        Param.Civ1.MaskName='noFile use default';
-                        Param.Civ1.MaskFlag='n';
-                    end
+                    Param.Civ1.Mask=name_generator(maskbase,i1_mask,1,'.png','_i');
                 end
-            else
-                Param.Civ1.MaskName='noFile use default';
-                Param.Civ1.MaskFlag='n';
-            end
-            
+            end
             % read grid parameters
             if Param.Civ1.CheckGrid
-%                 Param.Civ1.GridFlag='y';
-                if isequal(Param.Civ1.GridName(end-3:end),'grid')
-                    nbslice_grid=str2double(Param.Civ1.GridName(1:end-4)); %
+                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.GridName=[filecell.filebase '_' name_generator(Param.Civ1.GridName,i1_grid,1,'.grid','_i')];
+                        Param.Civ1.Grid=[filecell.filebase '_' name_generator(Param.Civ1.Grid,i1_grid,1,'.grid','_i')];
                         if ~exist(Param.Civ1.GridName,'file')
                             msgbox_uvmat('ERROR','grid file absent for civ1')
                         end
-                    elseif ~exist(Param.Civ1.GridName,'file')
+                    elseif ~exist(Param.Civ1.Grid,'file')
                         msgbox_uvmat('ERROR','grid file absent for civ1')
                     end
                 end
-%            else
-%                 Param.Civ1.GridName='noFile use default';
-%                 Param.Civ1.GridFlag='n';
             end
             
@@ -1282,12 +1266,12 @@
         end
         if Param.CheckCiv2==1
-            Param.Civ2.filename_ima_a=filecell.ima1.civ2{ifile,j};
-            Param.Civ2.filename_ima_b=filecell.ima2.civ2{ifile,j};
+            Param.Civ2.ImageA=filecell.ima1.civ2{ifile,j};
+            Param.Civ2.ImageB=filecell.ima2.civ2{ifile,j};
             if size(time,1)>=i2_civ2(ifile) && size(time,2)>=j2_civ2(j)
                 Param.Civ2.Dt=num2str(time(i2_civ2(ifile),j2_civ2(j))-time(i1_civ2(ifile),j1_civ2(j)));
-                Param.Civ2.T0=num2str((time(i2_civ2(ifile),j2_civ2(j))+time(i1_civ2(ifile),j1_civ2(j)))/2);
+                Param.Civ2.Time=num2str((time(i2_civ2(ifile),j2_civ2(j))+time(i1_civ2(ifile),j1_civ2(j)))/2);
             else
                 Param.Civ2.Dt=1;
-                Param.Civ2.T0=0;
+                Param.Civ2.Time=0;
             end
             Param.Civ2.term_a=num2stra(j1_civ2(j),nom_type_nc);
@@ -1295,56 +1279,29 @@
             Param.Civ2.filename_nc1=filecell.nc.civ1{ifile,j};
             Param.Civ2.filename_nc1(end-2:end)=[]; % remove '.nc'
-            Param.Civ2.pxcmx=1; %velocities are expressed in pixel dispalcement
-            Param.Civ2.pxcmy=1;
+            
+            % mask 
             if Param.Civ2.CheckMask
-                maskdispl=get(handles.txt_Mask,'String');
-                if exist(maskdispl,'file')
-                    Param.Civ2.MaskName=maskdispl;
-                    Param.Civ2.MaskFlag='y';
-                else
-                    maskbase=[filecell.filebase '_' maskdispl]; %
-                    nbslice_mask=str2double(maskdispl(1:end-4)); %
+                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;
-                    Param.Civ2.MaskName=name_generator(maskbase,i1_mask,1,'.png','_i');
-                    if exist(Param.Civ2.MaskName,'file')
-                        Param.Civ2.MaskFlag='y';
-                    else
-                        Param.Civ2.MaskName='noFile use default';
-                        Param.Civ2.MaskFlag='n';
+                    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 '_' name_generator(gridname,i1_grid,1,'.grid','_i')];
                     end
                 end
-            else
-                Param.Civ2.MaskName='noFile use default';
-                Param.Civ2.MaskFlag='n';
-            end
-            gridname=get(handles.txt_GridName,'String');
-            if numel(gridname)>=4 && isequal(gridname(end-3:end),'grid')
-                nbslice_grid=str2double(gridname(1:end-4)); %
-                if ~isnan(nbslice_grid)
-                    Param.Civ2.GridFlag='y';
-                    i1_grid=mod(i1_civ2(ifile)-1,nbslice_grid)+1;
-                    Param.Civ2.GridName=[filecell.filebase '_' name_generator(gridname,i1_grid,1,'.grid','_i')];
-%                     if exist(Param.Civ2.GridName,'file')
-%                         Param.Civ2.GridFlag='y';
-%                     else
-%                         Param.Civ2.GridName='noFile use default';
-%                         Param.Civ2.GridFlag='n';
-%                     end
-                elseif exist(gridname,'file')
-%                     Param.Civ2.GridFlag='y';
-%                 else
-%                     Param.Civ2.GridName='noFile use default';
-%                     Param.Civ2.GridFlag='n';
-                end
-%             else
-%                 Param.Civ2.GridName='noFile use default';
-%                 Param.Civ2.GridFlag='n';
-            end
-%             A=imread(filecell.ima1.civ2{1,1});%read the first image to get the size
-%             sizim=size(A);
-            Param.Civ2.ImageInfo=imfinfo(filecell.ima1.civ2{1,1});%read the first image to get the size
-            % TODO: case of movie
-%             Param.Civ2.npx=(sizim(2));
-%             Param.Civ2.npy=(sizim(1));      
+            end
+            ImageInfo=imfinfo(filecell.ima1.civ2{1,1});%read the first image to get the size
+            Param.Civ2.ImageWidth=ImageInfo.Width;
+            Param.Civ2.ImageHeight=ImageInfo.Height;
+            Param.Civ2.ImageBitDepth=ImageInfo.BitDepth;
+            % TODO: case of movie   
             switch CivMode
                 case 'CivX'
@@ -3235,5 +3192,44 @@
 set(gcf,'Pointer','arrow')
 
-
+%-------------------------------------------------------------------
+% --- 
+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
+    
+    
+%------------------------------------------------------------------------   
+% 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
+
+%------------------------------------------------------------------------   
+% launched by pressing OK on the status figure
+function close_GUI(hObject, eventdata)
+%------------------------------------------------------------------------
+    delete(gcbf)
+    
+%------------------------------------------------------------------------   
+% 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])
 
 
@@ -3351,5 +3347,7 @@
 end
 
-
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% Callbacks in the uipanel Fix1
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %------------------------------------------------------------------------
 % --- Executes on button press in CheckMask.
@@ -3358,5 +3356,5 @@
 maskval=get(handles.CheckMask,'Value');
 if isequal(maskval,0)
-    set(handles.txt_MaskName,'String','')
+    set(handles.txt_Mask,'String','')
 else
     mask_displ='no mask'; %default
@@ -3391,7 +3389,7 @@
         set(handles.CheckMask,'Value',1)
     end
-    set(handles.txt_MaskName,'String',mask_displ)
     set(handles.txt_Mask,'String',mask_displ)
-    set(handles.txt_MaskName,'String',mask_displ)
+    set(handles.txt_Mask,'String',mask_displ)
+    set(handles.txt_Mask,'String',mask_displ)
 end
 
@@ -3434,5 +3432,5 @@
     end
     set(handles.txt_Mask,'String',mask_displ)
-    set(handles.txt_MaskName,'String',mask_displ)
+    set(handles.txt_Mask,'String',mask_displ)
 end
 
@@ -3443,5 +3441,5 @@
 maskval=get(handles.CheckMask,'Value');
 if isequal(maskval,0)
-    set(handles.txt_MaskName,'String','')
+    set(handles.txt_Mask,'String','')
 else
     mask_displ='no mask'; %default
@@ -3471,5 +3469,5 @@
         set(handles.CheckMask,'Value',0)
     end
-    set(handles.txt_MaskName,'String',mask_displ)
+    set(handles.txt_Mask,'String',mask_displ)
 end
 
@@ -3608,7 +3606,9 @@
 hparent=get(hObject,'parent');
 hchildren=get(hparent,'children');
-handle_txtbox=findobj(hchildren,'tag','txt_GridName');
+handle_txtbox=findobj(hchildren,'tag','txt_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');
 testgrid=0;
 filegrid='';
@@ -3639,21 +3639,27 @@
     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_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,'panel_Civ1')
+PanelName=get(hparent,'tag')
+if strcmp(PanelName,'Civ1')
     hchildren=get(handles.Civ2,'children');
-    handle_checkbox=findobj(hchildren,'tag','check_Grid');
-    handle_txtbox=findobj(hchildren,'tag','txt_GridName');
+    handle_checkbox=findobj(hchildren,'tag','CheckGrid');
+    handle_txtbox=findobj(hchildren,'tag','txt_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
@@ -3661,12 +3667,16 @@
         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)
-    else
-        set(handle_checkbox,'Value',0);
-        set(handles.CheckGrid,'Value',0);
-        set(handle_dx,'Visible','on');
-        set(handle_dy,'Visible','on');
-        set(handle_txtbox,'Visible','off')
+%     else
+%         set(handle_checkbox,'Value',0);
+%         set(handles.CheckGrid,'Value',0);
+%         set(handle_dx,'Visible','on');
+%         set(handle_dy,'Visible','on');
+%          set(handle_title_dx,'Visible','on');
+%         set(handle_title_dy,'Visible','on');
+%         set(handle_txtbox,'Visible','off')
     end 
 end
@@ -3676,4 +3686,10 @@
 %------------------------------------------------------------------------
 value=get(hObject,'Value');
+hparent=get(hObject,'parent');
+parent_tag=get(hparent,'Tag');
+hchildren=get(hparent,'children');
+handle_txtbox=findobj(hchildren,'tag','txt_Mask');
+% handle_dx=findobj(hchildren,'tag','num_Dx');
+% handle_dy=findobj(hchildren,'tag','num_Dy');
 testmask=0;
 if value
@@ -3684,9 +3700,9 @@
         testmask=1;
     else % browse for a mask 
-        filemask=get(handles.CheckMask,'UserData');%look for previous mask name stored as UserData
+        filemask=get(hObject,'UserData');%look for previous mask name stored as UserData
         if exist(filemask,'file')
             filebase=filemask;
         end
-        [FileName, PathName, filterindex] = uigetfile( ...
+        [FileName, PathName] = uigetfile( ...
             {'*.png', ' (*.png)';
             '*.png',  '.png files '; ...
@@ -3694,5 +3710,5 @@
             'Pick a mask file *.png',filebase);
         filemask=fullfile(PathName,FileName);
-        set(handles.CheckMask,'UserData',filemask);%store for future use
+        set(hObject,'UserData',filemask);%store for future use
         if ~(isempty(FileName)||isempty(PathName)||isequal(FileName,0)||~exist(filemask,'file'))
             testmask=1;
@@ -3701,18 +3717,19 @@
 end
 if testmask
-    set(handles.num_Dx,'Visible','off');
-    set(handles.num_Dy,'Visible','off');
-    set(handles.num_Dx,'Visible','off');
-    set(handles.num_Dy,'Visible','off');
-    set(handles.txt_MaskName,'Visible','on')
-    set(handles.txt_MaskName,'String',filemask)
-else
-    set(handles.CheckMask,'Value',0);
-    set(handles.CheckGrid,'Value',0);
-    set(handles.num_Dx,'Visible','on');
-    set(handles.num_Dy,'Visible','on');
-    set(handles.num_Dx,'Visible','on');
-    set(handles.num_Dy,'Visible','on');
-    set(handles.txt_MaskName,'Visible','off')
+    stage=1;%default
+    switch parent_tag
+%         case 'Fix1'
+%             stage=2;
+        case 'Civ2'
+             stage=3;
+%         case 'Fix2'
+%             stage=4;
+    end
+    set(handles.txt_Mask(stage:end),'Visible','on')
+    set(handles.txt_Mask(stage:end),'String',filemask)
+    set(handles.CheckMask(stage:end),'Value',1)
+else
+    set(hObject,'Value',0);
+    set(handle_txtbox,'Visible','off')
 end
 
@@ -3922,23 +3939,17 @@
         ref_j=1;%default
     end
-    [filecell,num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2,nom_type_nc,file_ref_fix1,file_ref_fix2]=...
+    [filecell,i1,i21,j1,j2,i1_civ2,i2_civ2,j1_civ2,j2_civ2,nom_type_nc,file_ref_fix1,file_ref_fix2]=...
         set_civ_filenames(handles,ref_i,ref_j,[1 0 0 0 0 0]);
     Data.ListVarName={'ny','nx','A'};
-    Data.VarDimName={'ny','nx',{'ny','nx'}};
+    Data.VarDimName= {'ny','nx',{'ny','nx'}};
     Data.A=imread(filecell.ima1.civ1{1});
     Data.ny=[size(Data.A,1) 1];
     Data.nx=[1 size(Data.A,2)];
     par_civ1=read_GUI(handles.Civ1);
-    par_civ1.filename_ima_a=filecell.ima1.civ1{1};
-    par_civ1.filename_ima_b=filecell.ima2.civ1{1};
-    par_civ1.T0=0;
-    par_civ1.Dt=1;
+    par_civ1.ImageWidth=size(Data.A,1);
+    par_civ1.ImageHeight=size(Data.A,2);
+    par_civ1.Mask='all';% will provide only the grid set for PIV, no image correlation
     Param.Civ1=par_civ1;
-    Data=civ_uvmat(Param);
-    Data.ListVarName=[Data.ListVarName {'ny','nx','A'}];
-    Data.VarDimName=[Data.VarDimName {'ny','nx',{'ny','nx'}}];
-    Data.A=imread(filecell.ima1.civ1{1});
-    Data.ny=[size(Data.A,1) 1];
-    Data.nx=[1 size(Data.A,2)];
+    Grid=civ_matlab(Param);% get the grid of x, y positions set for PIV 
     hview_field=view_field(Data);
     set(0,'CurrentFigure',hview_field)
@@ -3948,6 +3959,6 @@
     ViewData.CivHandle=handles.civ;% indicate the handle of the civ GUI in view_field
     ViewData.axes3.B=imread(filecell.ima2.civ1{1});%store the second image in the UserData of the GUI view_field
-    ViewData.axes3.X=Data.Civ1_X; %keep the set of points in memeory
-    ViewData.axes3.Y=Data.Civ1_Y;
+    ViewData.axes3.X=Grid.Civ1_X; %keep the set of points in memeory
+    ViewData.axes3.Y=Grid.Civ1_Y;
     set(hview_field,'UserData',ViewData)
     corrfig=findobj(allchild(0),'tag','corrfig');% look for a current figure for image correlation display
@@ -3970,44 +3981,19 @@
 end
 
-%-------------------------------------------------------------------
-% --- 
-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
-    
-    
-%------------------------------------------------------------------------   
-% 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=findstr(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
-
-%------------------------------------------------------------------------   
-% launched by pressing OK on the status figure
-function close_GUI(hObject, eventdata)
-%------------------------------------------------------------------------
-    delete(gcbf)
-    
-%------------------------------------------------------------------------   
-% 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])
+
+%------------------------------------------------------------------------
+% --- 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
+
 
 %------------------------------------------------------------------------
@@ -4039,7 +4025,4 @@
 % end
 
-
-
-
 %------------------------------------------------------------------------
 function cmd=cmd_civ1(filename,Param)
@@ -4048,22 +4031,26 @@
 %changes : filename_cmx -> filename ( no extension )
 filename=regexprep(filename,'.nc',''); %file name for the result 
-Param.Civ1
 if isequal(Param.Civ1.Dt,'0')
     Param.Civ1.Dt='1' ;%case of 'displacement' mode
 end
-Param.Civ1.filename_ima_a=regexprep(Param.Civ1.filename_ima_a,'.png','');
-Param.Civ1.filename_ima_b=regexprep(Param.Civ1.filename_ima_b,'.png','');
+Param.Civ1.ImageA=regexprep(Param.Civ1.ImageA,'.png','');
+Param.Civ1.ImageB=regexprep(Param.Civ1.ImageB,'.png','');
 fid=fopen([filename '.civ1.cmx'],'w');
 fprintf(fid,['##############   CMX file' '\n' ]);
-fprintf(fid,   ['FirstImage ' regexprep(Param.Civ1.filename_ima_a,'\\','\\\\') '\n' ]);% for windows compatibility
-fprintf(fid,   ['LastImage  ' regexprep(Param.Civ1.filename_ima_b,'\\','\\\\') '\n' ]);% for windows compatibility
+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' ]);
-fprintf(fid,  ['Mask ' Param.Civ1.MaskFlag '\n' ]);
-fprintf(fid,  ['MaskName ' regexprep(Param.Civ1.MaskName,'\\','\\\\') '\n' ]);
-fprintf(fid,   ['ImageSize ' num2str(Param.Civ1.ImageInfo.Width) ' ' num2str(Param.Civ1.ImageInfo.Height) '\n' ]);   %VERIFIER CAS GENERAL ?
+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.Bx) ' ' num2str(Param.Civ1.By) '\n' ]);
 fprintf(fid,   ['SearchBoxeSize ' num2str(Param.Civ1.Searchx) ' ' num2str(Param.Civ1.Searchy) '\n' ]);
 fprintf(fid,   ['RO ' num2str(Param.Civ1.Rho) '\n' ]);
-if isfield(Param.Civ1,'GridName')
+if isfield(Param.Civ1,'Grid')
     fprintf(fid,   ['GridSpacing ' '25' ' ' '25' '\n' ]);
 else
@@ -4071,11 +4058,11 @@
 end
 fprintf(fid,   ['XX 1.0' '\n' ]);
-fprintf(fid,   ['Dt_TO ' num2str(Param.Civ1.Dt) ' ' num2str(Param.Civ1.T0) '\n' ]);
-fprintf(fid,  ['PixCmXY ' num2str(Param.Civ1.pxcmx) ' ' num2str(Param.Civ1.pxcmy) '\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.Shiftx) ' '  num2str(Param.Civ1.Shifty) '\n' ]);
-if isfield(Param.Civ1,'GridName')
+if isfield(Param.Civ1,'Grid')
     fprintf(fid,  ['Grid ' 'y' '\n' ]);
-    fprintf(fid,   ['GridName ' regexprep(Param.Civ1.GridName,'\\','\\\\') '\n' ]);
+    fprintf(fid,   ['GridName ' regexprep(Param.Civ1.Grid,'\\','\\\\') '\n' ]);
 else
     fprintf(fid,  ['Grid ' 'n' '\n' ]);
@@ -4095,5 +4082,5 @@
 else% Image threshold  activated
     if isempty(Param.Civ1.MaxIma)||isnan(Param.Civ1.MaxIma)
-        Param.Civ1.MaxIma=2^Param.Civ1.ImageInfo.BitDepth;%take the max image value as upper bound by default
+        Param.Civ1.MaxIma=2^Param.Civ1.ImageBitDepth;%take the max image value as upper bound by default
     end
     fprintf(fid,  ['SeuilImage y' '\n' ]);
@@ -4176,6 +4163,6 @@
     Param.Civ2.Dt='1' ;%case of 'displacement' mode
 end
-Param.Civ2.filename_ima_a=regexprep(Param.Civ2.filename_ima_a,'.png','');
-Param.Civ2.filename_ima_b=regexprep(Param.Civ2.filename_ima_b,'.png','');% bug : .png appears two times ?
+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 '.civ2.cmx'],'w');
 if isequal(fid,-1)
@@ -4185,15 +4172,22 @@
 end
 fprintf(fid,['##############   CMX file' '\n' ]);
-fprintf(fid,   ['FirstImage ' regexprep(Param.Civ2.filename_ima_a,'\\','\\\\') '\n' ]);% for windows compatibility
-fprintf(fid,   ['LastImage  ' regexprep(Param.Civ2.filename_ima_b,'\\','\\\\') '\n' ]);% for windows compatibility
+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' ]);
-fprintf(fid, ['Mask ' Param.Civ2.MaskFlag '\n' ]);
-fprintf(fid, ['MaskName ' regexprep(Param.Civ2.MaskName,'\\','\\\\') '\n' ]);% for windows compatibility
-fprintf(fid,   ['ImageSize ' num2str(Param.Civ2.ImageInfo.Width) ' ' num2str(Param.Civ2.ImageInfo.Height) '\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.Bx) ' ' num2str(Param.Civ2.By) '\n' ]);
 fprintf(fid, ['SearchBoxeSize ' num2str(Param.Civ2.Bx) ' ' num2str(Param.Civ2.By) '\n']);
 fprintf(fid, ['RO ' num2str(Param.Civ2.Rho) '\n']);
-if isfield(Param.Civ2,'GridName')
+if isfield(Param.Civ2,'Grid')
     fprintf(fid,   ['GridSpacing ' '25' ' ' '25' '\n' ]);
 else
@@ -4202,11 +4196,11 @@
 % 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.T0) '\n' ]);
-fprintf(fid, ['PixCmXY ' num2str(Param.Civ2.pxcmx) ' ' num2str(Param.Civ2.pxcmy) '\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,'GridName')
+if isfield(Param.Civ2,'Grid')
     fprintf(fid,  ['Grid ' 'y' '\n' ]);
-    fprintf(fid,   ['GridName ' regexprep(Param.Civ2.GridName,'\\','\\\\') '\n' ]);
+    fprintf(fid,   ['GridName ' regexprep(Param.Civ2.Grid,'\\','\\\\') '\n' ]);
 else
     fprintf(fid,  ['Grid ' 'n' '\n' ]);
@@ -4229,5 +4223,5 @@
 else% Image threshold  activated
     if isempty(Param.Civ2.MaxIma)||isnan(Param.Civ2.MaxIma)
-        Param.Civ2.MaxIma=2^Param.Civ2.ImageInfo.BitDepth;%take the max image value as upper bound by default
+        Param.Civ2.MaxIma=2^Param.Civ2.ImageBitDepth;%take the max image value as upper bound by default
     end
     fprintf(fid,  ['SeuilImage y' '\n' ]);
@@ -4250,18 +4244,4 @@
 
 %------------------------------------------------------------------------
-% --- 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
-
-%------------------------------------------------------------------------
 % --- CheckCiv1  Unified: TO ABADON
 function xml_civ1_parameters=CIV1_CMD_Unified(filename,namelog,par)
@@ -4270,6 +4250,6 @@
 %global CivBin%name of the executable for checkciv1 calculation
 
-civ1.image1=par.filename_ima_a;
-civ1.image2=par.filename_ima_b;
+civ1.image1=par.ImageA;
+civ1.image2=par.ImageB;
 civ1.imageSize_X=par.npx;
 civ1.imageSize_Y=par.npy;
@@ -4295,7 +4275,7 @@
 civ1.dt=par.Dt;
 civ1.unit='pixel';
-civ1.absolut_time_T0=par.T0;
-civ1.pixcmx=par.pxcmx;
-civ1.pixcmy=par.pxcmy;
+civ1.absolut_time_T0=par.Time;
+civ1.pixcmx='1';
+civ1.pixcmy='1';
 civ1.convectFlow='n';
 
@@ -4311,6 +4291,6 @@
 filename=regexprep(filename,'.nc','');
 
-civ2.image1=par.filename_ima_a;
-civ2.image2=par.filename_ima_b;
+civ2.image1=par.ImageA;
+civ2.image2=par.ImageB;
 civ2.imageSize_X=par.npx;
 civ2.imageSize_Y=par.npy;
@@ -4348,9 +4328,6 @@
 civ2.dt=par.Dt;
 civ2.unit='pixel';
-civ2.absolut_time_T0=par.T0;
-civ2.pixcmx=par.pxcmx;
-civ2.pixcmy=par.pxcmy;
+civ2.absolut_time_T0=par.Time;
+civ2.pixcmx='1';
+civ2.pixcmy='1';
 civ2.convectFlow='n';
-civ2.pixcmx=par.pxcmx;
-civ2.pixcmy=par.pxcmy;
-civ2.convectFlow='n';
Index: /trunk/src/civ_matlab.m
===================================================================
--- /trunk/src/civ_matlab.m	(revision 315)
+++ /trunk/src/civ_matlab.m	(revision 315)
@@ -0,0 +1,817 @@
+%'civ_matlab': Matlab version of the PIV programs CivX
+% --- call the sub-functions:
+%   civ: PIV function itself
+%   fix: removes false vectors after detection by various criteria
+%   patch: make interpolation-smoothing 
+%------------------------------------------------------------------------
+% function [Data,errormsg,result_conv]= civ_uvmat(Param,ncfile)
+%
+%OUTPUT
+% Data=structure containing the PIV results and information on the processing parameters
+% errormsg=error message char string, default=''
+% resul_conv: image inter-correlation function for the last grid point (used for tests)
+%
+%INPUT:
+% Param: input images and processing parameters
+% ncfile: name of a netcdf file to be created for the result (extension .nc)
+%
+%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+%  Copyright  2011, LEGI / CNRS-UJF-INPG, joel.sommeria@legi.grenoble-inp.fr.
+%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+%     This 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 (open UVMAT/COPYING.txt) for more details.
+%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+
+function [Data,errormsg,result_conv]= civ_matlab(Param,ncfile)
+errormsg='';
+Data.ListGlobalAttribute={'Conventions','Program','CivStage'};
+Data.Conventions='uvmat/civdata';% states the conventions used for the description of field variables and attributes
+Data.Program='civ_matlab';
+Data.CivStage=0;%default
+ListVarCiv1={'Civ1_X','Civ1_Y','Civ1_U','Civ1_V','Civ1_C','Civ1_F'}; %variables to read
+ListVarFix1={'Civ1_X','Civ1_Y','Civ1_U','Civ1_V','Civ1_C','Civ1_F','Civ1_FF'};
+mask='';
+maskname='';%default
+check_civx=0;%default
+check_civ1=0;%default
+check_patch1=0;%default
+
+%% Civ1
+if isfield (Param,'Civ1')
+    check_civ1=1;% test for further use of civ1 results
+    % caluclate velocity data (y and v in indices, reverse to y component)
+    [xtable ytable utable vtable ctable F result_conv errormsg] = civ (Param.Civ1);
+    if ~isempty(errormsg)
+        return
+    end
+    list_param=(fieldnames(Param.Civ1))';
+    Civ1_param=list_param;%default
+    for ilist=1:length(list_param)
+        Civ1_param{ilist}=['Civ1_' list_param{ilist}];
+        %         eval(['Data.Civ1_' list_param{ilist} '=Param.Civ1.' list_param{ilist} ';'])
+        Data.(['Civ1_' list_param{ilist}])=Param.Civ1.(list_param{ilist});
+    end
+    Data.ListGlobalAttribute=[Data.ListGlobalAttribute Civ1_param];% {'Civ1_Time','Civ1_Dt'}];
+%     if exist('ncfile','var')% TEST for use interactively with mouse_motion (no file created)
+        Data.ListVarName={'Civ1_X','Civ1_Y','Civ1_U','Civ1_V','Civ1_C','Civ1_F'};%  cell array containing the names of the fields to record
+        Data.VarDimName={'nbvec1','nbvec1','nbvec1','nbvec1','nbvec1','nbvec1'};
+        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';
+    Data.Civ1_X=reshape(xtable,[],1);
+    Data.Civ1_Y=reshape(Param.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);
+    Data.CivStage=1;
+
+else
+    Data=nc2struct(ncfile,'ListGlobalAttribute','absolut_time_T0'); %look for the constant 'absolut_time_T0' to detect old civx data format 
+    if ~isempty(Data.absolut_time_T0')%read civx file
+        check_civx=1;% test for old civx data format
+        [Data,vardetect,ichoice]=nc2struct(ncfile);%read the variables in the netcdf file
+    else
+        if isfield(Param,'Fix1')
+            Data=nc2struct(ncfile,ListVarCiv1);%read civ1 data in the existing netcdf file
+        else
+            Data=nc2struct(ncfile,ListVarFix1);%read civ1 and fix1 data in the existing netcdf file
+        end
+    end
+    if isfield(Data,'Txt')
+        msgbox_uvmat('ERROR',Data.Txt)
+        return
+    end
+end
+
+%% Fix1
+if isfield (Param,'Fix1')
+    ListFixParam=fieldnames(Param.Fix1);
+    for ilist=1:length(ListFixParam)
+        ParamName=ListFixParam{ilist};
+        ListName=['Fix1_' ParamName];
+        eval(['Data.ListGlobalAttribute=[Data.ListGlobalAttribute ''' ParamName '''];'])
+        eval(['Data.' ListName '=Param.Fix1.' ParamName ';'])
+    end
+%     Data.ListGlobalAttribute=[Data.ListGlobalAttribute {'Fix1_WarnFlags','Fix1_TreshCorr','Fix1_TreshVel','Fix1_UpperBoundTest'}];
+%     Data.Fix1_WarnFlags=Param.Fix1.WarnFlags;
+%     Data.Fix1_ThreshCorr=Param.Fix1.ThreshCorr;
+%     Data.Fix1_ThreshVel=Param.Fix1.ThreshVel;
+%     Data.Fix1_UpperBoundTest=Param.Fix1.UpperBoundTest;
+
+    if check_civx
+        if ~isfield(Data,'fix')
+            Data.ListGlobalAttribute=[Data.ListGlobalAttribute 'fix'];
+            Data.fix=1;
+            Data.ListVarName=[Data.ListVarName {'vec_FixFlag'}];
+            Data.VarDimName=[Data.VarDimName {'nb_vectors'}];
+        end
+        Data.vec_FixFlag=fix(Param.Fix1,Data.vec_F,Data.vec_C,Data.vec_U,Data.vec_V,Data.vec_X,Data.vec_Y);
+    else
+        Data.ListVarName=[Data.ListVarName {'Civ1_FF'}];
+        Data.VarDimName=[Data.VarDimName {'nbvec1'}];
+        nbvar=length(Data.ListVarName);
+        Data.VarAttribute{nbvar}.Role='errorflag';    
+        Data.Civ1_FF=fix(Param.Fix1,Data.Civ1_F,Data.Civ1_C,Data.Civ1_U,Data.Civ1_V);
+        Data.CivStage=2;    
+    end
+end   
+%% Patch1
+if isfield (Param,'Patch1')
+    check_patch1=1;
+    Data.ListGlobalAttribute=[Data.ListGlobalAttribute {'Patch1_Rho','Patch1_Threshold','Patch1_SubDomain'}];
+    Data.Patch1_Rho=str2double(Param.Patch1.Rho);
+    Data.Patch1_Threshold=str2double(Param.Patch1.Threshold);
+    Data.Patch1_SubDomain=str2double(Param.Patch1.SubDomain);
+    Data.ListVarName=[Data.ListVarName {'Civ1_U_Diff','Civ1_V_Diff','Civ1_X_SubRange','Civ1_Y_SubRange','Civ1_NbSites','Civ1_X_tps','Civ1_Y_tps','Civ1_U_tps','Civ1_V_tps'}];
+    Data.VarDimName=[Data.VarDimName {'NbVec1','NbVec1',{'NbSubDomain1','Two'},{'NbSubDomain1','Two'},'NbSubDomain1',...
+             {'NbVec1Sub','NbSubDomain1'},{'NbVec1Sub','NbSubDomain1'},{'Nbtps1','NbSubDomain1'},{'Nbtps1','NbSubDomain1'}}];
+    nbvar=length(Data.ListVarName);
+    Data.VarAttribute{nbvar-1}.Role='vector_x';
+    Data.VarAttribute{nbvar}.Role='vector_y';
+    Data.Civ1_U_Diff=zeros(size(Data.Civ1_X));
+    Data.Civ1_V_Diff=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
+    [Data.Civ1_X_SubRange,Data.Civ1_Y_SubRange,Data.Civ1_NbSites,FFres,Ures, Vres,Data.Civ1_X_tps,Data.Civ1_Y_tps,Data.Civ1_U_tps,Data.Civ1_V_tps]=...
+                            patch(Data.Civ1_X(ind_good)',Data.Civ1_Y(ind_good)',Data.Civ1_U(ind_good)',Data.Civ1_V(ind_good)',Data.Patch1_Rho,Data.Patch1_Threshold,Data.Patch1_SubDomain); 
+      Data.Civ1_U_Diff(ind_good)=Data.Civ1_U(ind_good)-Ures;
+      Data.Civ1_V_Diff(ind_good)=Data.Civ1_V(ind_good)-Vres;
+      Data.Civ1_FF(ind_good)=FFres;
+      Data.CivStage=3;                             
+end   
+
+%% Civ2
+if isfield (Param,'Civ2')
+    par_civ2=Param.Civ2;
+    if ~check_civ1 || ~strcmp(par_civ1.filename_ima_a,par_civ2.filename_ima_a)
+            par_civ2.ImageA=imread(par_civ2.filename_ima_a);%read first image if not already done for civ1 
+    end
+    if ~check_civ1|| ~strcmp(par_civ1.filename_ima_b,par_civ2.filename_ima_b)
+            par_civ2.ImageB=imread(par_civ2.filename_ima_b);%read second image if not already done for civ1 
+    end
+%     stepx=str2double(par_civ2.dx);
+%     stepy=str2double(par_civ2.dy);
+    ibx2=ceil(str2double(par_civ2.ibx)/2);
+    iby2=ceil(str2double(par_civ2.iby)/2);
+    isx2=ibx2+2;
+    isy2=iby2+2;
+    %get the previous guess for displacement
+    if ~check_patch1
+        [Guess,VelType]=read_civdata(ObjectName,InputField,ParamIn.VelType);%TO DEVELOP
+    else
+        Data.Civ1_U_Diff=zeros(size(Data.Civ1_X));
+        Data.Civ1_V_Diff=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
+            [Data.Civ1_X_SubRange,Data.Civ1_Y_SubRange,Data.Civ1_NbSites,FFres,Ures, Vres,Data.Civ1_X_tps,Data.Civ1_Y_tps,Data.Civ1_U_tps,Data.Civ1_V_tps]=...
+                                patch(Data.Civ1_X(ind_good)',Data.Civ1_Y(ind_good)',Data.Civ1_U(ind_good)',Data.Civ1_V(ind_good)',Data.Patch1_Rho,Data.Patch1_Threshold,Data.Patch1_SubDomain);
+        end 
+%     shiftx=str2num(par_civ1.shiftx);
+%     shifty=str2num(par_civ1.shifty);
+% TO GET shift from par_civ2.filename_nc1
+    % shiftx=velocity interpolated at position 
+    miniy=max(1+isy2+shifty,1+iby2);
+    minix=max(1+isx2-shiftx,1+ibx2);
+    maxiy=min(size(par_civ2.ImageA,1)-isy2+shifty,size(par_civ2.ImageA,1)-iby2);
+    maxix=min(size(par_civ2.ImageA,2)-isx2-shiftx,size(par_civ2.ImageA,2)-ibx2);
+    [GridX,GridY]=meshgrid(minix:par_civ2.Dx:maxix,miniy:par_civ2.Dy:maxiy);
+    PointCoord(:,1)=reshape(GridX,[],1);
+    PointCoord(:,2)=reshape(GridY,[],1);
+    if ~isempty(par_civ2.maskname)&& ~strcmp(maskname,par_civ2.maskname)% mask exist, not already read in civ1
+        mask=imread(par_civ2.maskname);
+    end
+    % caluclate velocity data (y and v in indices, reverse to y component)
+    [xtable ytable utable vtable ctable F] = civ (par_civ2.ImageA,par_civ1.ImageB,ibx2,iby2,isx2,isy2,shiftx,-shifty,PointCoord,str2num(par_civ1.rho),mask);
+    list_param=(fieldnames(par_civ1))';
+    list_remove={'pxcmx','pxcmy','npx','npy','gridflag','maskflag','term_a','term_b','T0'};
+    index=zeros(size(list_param));
+    for ilist=1:length(list_remove)
+        index=strcmp(list_remove{ilist},list_param);
+        if ~isempty(find(index,1))
+            list_param(index)=[];
+        end
+    end
+    for ilist=1:length(list_param)
+        Civ1_param{ilist}=['Civ1_' list_param{ilist}];
+        eval(['Data.Civ1_' list_param{ilist} '=Param.Civ1.' list_param{ilist} ';'])
+    end
+    if isfield(Data,'Civ1_gridname') && strcmp(Data.Civ1_gridname(1:6),'noFile')
+        Data.Civ1_gridname='';
+    end
+    if isfield(Data,'Civ1_maskname') && strcmp(Data.Civ1_maskname(1:6),'noFile')
+        Data.Civ1_maskname='';
+    end
+    Data.ListGlobalAttribute=[Data.ListGlobalAttribute Civ1_param {'Civ1_Time','Civ1_Dt'}];
+    Data.Civ1_Time=str2double(par_civ1.T0);
+    Data.Civ1_Dt=str2double(par_civ1.Dt);
+    Data.ListVarName={'Civ1_X','Civ1_Y','Civ1_U','Civ1_V','Civ1_C','Civ1_F'};%  cell array containing the names of the fields to record
+    Data.VarDimName={'nbvec1','nbvec1','nbvec1','nbvec1','nbvec1','nbvec1'};
+    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';
+    Data.Civ1_X=reshape(xtable,[],1);
+    Data.Civ1_Y=reshape(size(par_civ2.ImageA,1)-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);
+    Data.CivStage=Data.CivStage+1;
+end
+
+%% Fix2
+if isfield (Param,'Fix2')
+    ListFixParam=fieldnames(Param.Fix2);
+    for ilist=1:length(ListFixParam)
+        ParamName=ListFixParam{ilist};
+        ListName=['Fix1_' ParamName];
+        eval(['Data.ListGlobalAttribute=[Data.ListGlobalAttribute ''' ParamName '''];'])
+        eval(['Data.' ListName '=Param.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 {'nbvec2'}];
+        nbvar=length(Data.ListVarName);
+        Data.VarAttribute{nbvar}.Role='errorflag';    
+        Data.Civ2_FF=fix(Param.Fix2,Data.Civ2_F,Data.Civ2_C,Data.Civ2_U,Data.Civ2_V);
+        Data.CivStage=5;    
+    end
+    
+end   
+
+%% Patch2
+if isfield (Param,'Patch2')
+    Data.ListGlobalAttribute=[Data.ListGlobalAttribute {'Patch2_Rho','Patch2_Threshold','Patch2_SubDomain'}];
+    Data.Patch2_Rho=str2double(Param.Patch2.Rho);
+    Data.Patch2_Threshold=str2double(Param.Patch2.Threshold);
+    Data.Patch2_SubDomain=str2double(Param.Patch2.SubDomain);
+    Data.ListVarName=[Data.ListVarName {'Civ2_U_Diff','Civ2_V_Diff','Civ2_X_SubRange','Civ2_Y_SubRange','Civ2_NbSites','Civ2_X_tps','Civ2_Y_tps','Civ2_U_tps','Civ2_V_tps'}];
+    Data.VarDimName=[Data.VarDimName {'NbVec2','NbVec2',{'NbSubDomain2','Two'},{'NbSubDomain2','Two'},'NbSubDomain2',...
+             {'NbVec2Sub','NbSubDomain2'},{'NbVec2Sub','NbSubDomain2'},{'Nbtps2','NbSubDomain2'},{'Nbtps2','NbSubDomain2'}}];
+    nbvar=length(Data.ListVarName);
+    Data.VarAttribute{nbvar-1}.Role='vector_x';
+    Data.VarAttribute{nbvar}.Role='vector_y';
+    Data.Civ2_U_Diff=zeros(size(Data.Civ2_X));
+    Data.Civ2_V_Diff=zeros(size(Data.Civ2_X));
+    if isfield(Data,'Civ2_FF')
+        ind_good=find(Data.Civ2_FF==0);
+    else
+        ind_good=1:numel(Data.Civ2_X);
+    end
+    [Data.Civ2_X_SubRange,Data.Civ2_Y_SubRange,Data.Civ2_NbSites,FFres,Ures, Vres,Data.Civ2_X_tps,Data.Civ2_Y_tps,Data.Civ2_U_tps,Data.Civ2_V_tps]=...
+                            patch(Data.Civ2_X(ind_good)',Data.Civ2_Y(ind_good)',Data.Civ2_U(ind_good)',Data.Civ2_V(ind_good)',Data.Patch2_Rho,Data.Patch2_Threshold,Data.Patch2_SubDomain); 
+      Data.Civ2_U_Diff(ind_good)=Data.Civ2_U(ind_good)-Ures;
+      Data.Civ2_V_Diff(ind_good)=Data.Civ2_V(ind_good)-Vres;
+      Data.Civ2_FF(ind_good)=FFres;
+      Data.CivStage=6;                             
+end   
+
+%% write result in a netcdf file if requested
+if exist('ncfile','var')
+    errormsg=struct2nc(ncfile,Data);
+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:
+% 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=[];
+function [xtable ytable utable vtable ctable F result_conv errormsg] = civ (par_civ)
+%this funtion performs the DCC PIV analysis. Recent window-deformation
+%methods perform better and will maybe be implemented in the future.
+
+%% prepare grid
+ibx2=ceil(par_civ.Bx/2);
+iby2=ceil(par_civ.By/2);
+isx2=ceil(par_civ.Searchx/2);
+isy2=ceil(par_civ.Searchy/2);
+shiftx=par_civ.Shiftx;
+shifty=-par_civ.Shifty;% sign minus because image j index increases when y decreases
+if isfield(par_civ,'Grid')
+    if ischar(par_civ.Grid)
+        par_civ.Grid;
+        par_civ.Grid=dlmread(par_civ.Grid);
+        par_civ.Grid(1,:)=[];%the first line must be removed (heading in the grid file)
+    end
+else% automatic measurement grid
+    ibx2=ceil(par_civ.Bx/2);
+    iby2=ceil(par_civ.By/2);
+    isx2=ceil(par_civ.Searchx/2);
+    isy2=ceil(par_civ.Searchy/2);
+    shiftx=par_civ.Shiftx;
+    shifty=-par_civ.Shifty;
+    miniy=max(1+isy2+shifty,1+iby2);
+    minix=max(1+isx2-shiftx,1+ibx2);
+    maxiy=min(par_civ.ImageHeight-isy2+shifty,par_civ.ImageHeight-iby2);
+    maxix=min(par_civ.ImageWidth-isx2-shiftx,par_civ.ImageWidth-ibx2);
+    [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);
+end
+
+%% Default output
+nbvec=size(par_civ.Grid,1);
+xtable=par_civ.Grid(:,1);
+ytable=par_civ.Grid(:,2);
+utable=zeros(nbvec,1);
+vtable=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);
+    end
+end
+check_MinIma=isfield(par_civ,'MinIma');% test for image luminosity threshold
+check_MaxIma=isfield(par_civ,'MaxIma') && ~isempty(par_civ.MaxIma);
+
+%% prepare images
+if ischar(par_civ.ImageA)
+    par_civ.ImageA=imread(par_civ.ImageA);
+end
+if ischar(par_civ.ImageB)
+    par_civ.ImageB=imread(par_civ.ImageB);
+end
+[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
+par_civ.ImageA=double(par_civ.ImageA);
+par_civ.ImageB=double(par_civ.ImageB);
+
+
+%% Apply mask
+    % Convention for mask
+    % 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) TO IMPLEMENT
+    %  20>=mask: velocity=0
+checkmask=0;
+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_noflux=(par_civ.Mask<100) ;%TODO: to implement
+    check_undefined=(par_civ.Mask<200 & par_civ.Mask>=100 );
+    par_civ.ImageA(check_undefined)=min(min(par_civ.ImageA));% put image A to zero (i.e. the min image value) in the undefined  area
+    par_civ.ImageB(check_undefined)=min(min(par_civ.ImageB));% 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
+% vector=[0 0];%default
+for ivec=1:nbvec
+    iref=par_civ.Grid(ivec,1);% xindex on the image A for the middle of the correlation box
+    jref=par_civ.Grid(ivec,2);% yindex on the image B for the middle of the correlation box
+%     xtable(ivec)=iref;
+%     ytable(ivec)=jref;%default
+    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
+            F(ivec)=3;
+        else
+            image1_crop=par_civ.ImageA(jref-iby2:jref+iby2,iref-ibx2:iref+ibx2);%extract a subimage (correlation box) from image A
+            image2_crop=par_civ.ImageB(jref+shifty-isy2:jref+shifty+isy2,iref+shiftx-isx2:iref+shiftx+isx2);%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
+        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;
+            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.Rho==1
+                        [vector,F(ivec)] = SUBPIXGAUSS (result_conv,x,y);
+                    elseif par_civ.Rho==2
+                        [vector,F(ivec)] = SUBPIX2DGAUSS (result_conv,x,y);
+                    end
+                    utable(ivec)=vector(1)+shiftx;
+                    vtable(ivec)=vector(2)+shifty;
+                    xtable(ivec)=iref+utable(ivec)/2;% convec flow (velocity taken at the point middle from imgae1 and 2)
+                    ytable(ivec)=jref+vtable(ivec)/2;
+                    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
+                    %                     vector=[0 0]; %if something goes wrong with cross correlation.....
+                    F(ivec)=3;
+                end
+            else
+                F(ivec)=3;
+            end
+        end
+    end
+    
+    %Create the vector matrix x, y, u, v
+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
+function [vector,F] = SUBPIXGAUSS (result_conv,x,y)
+%------------------------------------------------------------------------
+vector=[0 0]; %default
+F=0;
+[npy,npx]=size(result_conv);
+
+% if (x <= (size(result_conv,1)-1)) && (y <= (size(result_conv,1)-1)) && (x >= 1) && (y >= 1)
+    %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-1 && y >= 1
+        f0 = log(result_conv(y,x));
+        f1 = real(log(result_conv(y-1,x)));
+        f2 = real(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 = real(log(result_conv(y,x-1)));
+        f2 = real(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;
+[npy,npx]=size(result_conv);
+if (x <= npx-1) && (y <= npy-1) && (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: 511515
+            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];
+
+%'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
+Param
+
+%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));
+% if isfield (Param,'WarnFlags')
+%     for iflag=1:numel(Param.WarnFlags)
+%         FF=(FF==1| F==Param.WarnFlags(iflag));
+%     end
+% end
+    end
+end
+%criterium on correlation values
+if isfield (Param,'MinCorr')
+    FF=FF==1 | C<Param.MinCorr;
+end
+if isfield (Param,'MinVel')||(isfield (Param,'MaxVel')&&~isempty(Param.MaxVel))
+    Umod= U.*U+V.*V;
+    if isfield (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
+return
+
+% 
+% if isfield (Param,'LowerBoundVel')&& ~isequal(Param.LowerBoundVel,0)
+%     thresh=Param.LowerBoundVel*Param.LowerBoundVel;
+%     FF=FF==1 | (U.*U+V.*V)<thresh;
+% end
+% if isfield (Param,'UpperBoundVel')&& ~isequal(Param.UpperBoundVel,0)
+%     thresh=Param.UpperBoundVel*Param.UpperBoundVel;
+%     FF=FF==1 | (U.*U+V.*V)>thresh;
+% end
+% if isfield(Param,'MaskName')
+%    M=imread(Param.MaskName);
+%    nxy=size(M);
+%    M=reshape(M,1,[]);
+%    rangx0=[0.5 nxy(2)-0.5];
+%    rangy0=[0.5 nxy(1)-0.5];
+%    vec_x1=X-U/2;%beginning points
+%    vec_x2=X+U/2;%end points of vectors
+%    vec_y1=Y-V/2;%beginning points
+%    vec_y2=Y+V/2;%end points of vectors
+%    indx=1+round((nxy(2)-1)*(vec_x1-rangx0(1))/(rangx0(2)-rangx0(1)));% image index x at abcissa vec_x1
+%    indy=1+round((nxy(1)-1)*(vec_y1-rangy0(1))/(rangy0(2)-rangy0(1)));% image index y at ordinate vec_y1   
+%    check_in=~(indx < 1 |indy < 1 | indx > nxy(2) |indy > nxy(1)); %=0 out of the mask image, 1 inside
+%    indx=indx.*check_in+(1-check_in); %replace indx by 1 out of the mask range
+%    indy=indy.*check_in+(1-check_in); %replace indy by 1 out of the mask range
+%    ICOMB=((indx-1)*nxy(1)+(nxy(1)+1-indy));%determine the indices in the image reshaped in a Matlab vector
+%    Mvalues=M(ICOMB);
+%    flag7b=((20 < Mvalues) & (Mvalues < 200))| ~check_in';
+%    indx=1+round((nxy(2)-1)*(vec_x2-rangx0(1))/(rangx0(2)-rangx0(1)));% image index x at abcissa vec_x2
+%    indy=1+round((nxy(1)-1)*(vec_y2-rangy0(1))/(rangy0(2)-rangy0(1)));% image index y at ordinate vec_y2
+%    check_in=~(indx < 1 |indy < 1 | indx > nxy(2) |indy > nxy(1)); %=0 out of the mask image, 1 inside
+%    indx=indx.*check_in+(1-check_in); %replace indx by 1 out of the mask range
+%    indy=indy.*check_in+(1-check_in); %replace indy by 1 out of the mask range
+%    ICOMB=((indx-1)*nxy(1)+(nxy(1)+1-indy));%determine the indices in the image reshaped in a Matlab vector
+%    Mvalues=M(ICOMB);
+%    flag7e=((Mvalues > 20) & (Mvalues < 200))| ~check_in';
+%    FF=FF==1 |(flag7b|flag7e)';
+% end
+% %    flag7=0;
+% % end   
+% 
+
+FF=double(FF);
+% 
+% % criterium on velocity values
+% delta_u=Field.U;%default without ref file
+% delta_v=Field.V;
+% if exist('fileref','var') && ~isempty(fileref)
+%     if ~exist(fileref,'file')
+%         error='reference file not found in RUN_FIX.m';
+%         display(error);
+%         return
+%     end
+%     FieldRef=read_civxdata(fileref,[],fieldref);   
+%     if isfield(FieldRef,'FF')
+%         index_true=find(FieldRef.FF==0);
+%         FieldRef.X=FieldRef.X(index_true);
+%         FieldRef.Y=FieldRef.Y(index_true);
+%         FieldRef.U=FieldRef.U(index_true);
+%         FieldRef.V=FieldRef.V(index_true);
+%     end
+%     if ~isfield(FieldRef,'X') || ~isfield(FieldRef,'Y') || ~isfield(FieldRef,'U') || ~isfield(FieldRef,'V')
+%         error='reference file is not a velocity field in RUN_FIX.m '; %bad input file
+%         return
+%     end
+%     if length(FieldRef.X)<=1
+%         errordlg('reference field with one vector or less in RUN_FIX.m')
+%         return
+%     end
+%     vec_U_ref=griddata_uvmat(FieldRef.X,FieldRef.Y,FieldRef.U,Field.X,Field.Y);  %interpolate vectors in the ref field
+%     vec_V_ref=griddata_uvmat(FieldRef.X,FieldRef.Y,FieldRef.V,Field.X,Field.Y);  %interpolate vectors in the ref field to the positions  of the main field     
+%     delta_u=Field.U-vec_U_ref;%take the difference with the interpolated ref field
+%     delta_v=Field.V-vec_V_ref;
+% end
+% thresh_vel_x=thresh_vel; 
+% thresh_vel_y=thresh_vel; 
+% if isequal(inf_sup,1)
+%     flag5=abs(delta_u)<thresh_vel_x & abs(delta_v)<thresh_vel_y &(flag1~=1)&(flag2~=1)&(flag3~=1)&(flag4~=1);
+% elseif isequal(inf_sup,2)
+%     flag5=(abs(delta_u)>thresh_vel_x | abs(delta_v)>thresh_vel_y) &(flag1~=1)&(flag2~=1)&(flag3~=1)&(flag4~=1);
+% end
+% 
+%             % flag7 introduce a grey mask, matrix M
+
+% flagmagenta=flag1|flag2|flag3|flag4|flag5|flag7;
+% fixflag_unit=Field.FF-10*floor(Field.FF/10); %unity term of fix_flag
+
+
+
+%------------------------------------------------------------------------
+% patch function
+function [SubRangx,SubRangy,nbpoints,FF,U_smooth,V_smooth,X_tps,Y_tps,U_tps,V_tps] =patch(X,Y,U,V,Rho,Threshold,SubDomain)
+%subdomain decomposition
+warning off
+U=reshape(U,[],1);
+V=reshape(V,[],1);
+X=reshape(X,[],1);
+Y=reshape(Y,[],1);
+nbvec=numel(X);
+NbSubDomain=ceil(nbvec/SubDomain);
+MinX=min(X);
+MinY=min(Y);
+MaxX=max(X);
+MaxY=max(Y);
+RangX=MaxX-MinX;
+RangY=MaxY-MinY;
+AspectRatio=RangY/RangX;
+NbSubDomainX=max(floor(sqrt(NbSubDomain/AspectRatio)),1);
+NbSubDomainY=max(floor(sqrt(NbSubDomain*AspectRatio)),1);
+NbSubDomain=NbSubDomainX*NbSubDomainY;
+SizX=RangX/NbSubDomainX;%width of subdomains
+SizY=RangY/NbSubDomainY;%height of subdomains
+CentreX=linspace(MinX+SizX/2,MaxX-SizX/2,NbSubDomainX);
+CentreY=linspace(MinY+SizY/2,MaxY-SizY/2,NbSubDomainY);
+[CentreX,CentreY]=meshgrid(CentreX,CentreY);
+CentreY=reshape(CentreY,1,[]);
+CentreX=reshape(CentreX,1,[]);
+rho=SizX*SizY*Rho/1000000;%optimum rho increase as the area of the subdomain (division by 10^6 to reach good values with the default GUI input)
+U_tps_sub=zeros(length(X),NbSubDomain);%default spline
+V_tps_sub=zeros(length(X),NbSubDomain);%default spline
+U_smooth=zeros(length(X),1);
+V_smooth=zeros(length(X),1);
+
+nb_select=zeros(length(X),1);
+FF=zeros(length(X),1);
+check_empty=zeros(1,NbSubDomain);
+for isub=1:NbSubDomain
+    SubRangx(isub,:)=[CentreX(isub)-SizX/2 CentreX(isub)+SizX/2];
+    SubRangy(isub,:)=[CentreY(isub)-SizY/2 CentreY(isub)+SizY/2];
+    ind_sel_previous=[];
+    ind_sel=0;
+    while numel(ind_sel)>numel(ind_sel_previous) %increase the subdomain during four iterations at most
+        ind_sel_previous=ind_sel;
+        ind_sel=find(X>SubRangx(isub,1) & X<SubRangx(isub,2) & Y>SubRangy(isub,1) & Y<SubRangy(isub,2));
+        % if no vector in the subdomain, skip the subdomain
+        if isempty(ind_sel)
+            check_empty(isub)=1;    
+            U_tps(1,isub)=0;%define U_tps and V_tps by default
+            V_tps(1,isub)=0;
+            break
+            % if too few selected vectors, increase the subrange for next iteration
+        elseif numel(ind_sel)<SubDomain/4 && ~isequal( ind_sel,ind_sel_previous);
+            SubRangx(isub,1)=SubRangx(isub,1)-SizX/4;
+            SubRangx(isub,2)=SubRangx(isub,2)+SizX/4;
+            SubRangy(isub,1)=SubRangy(isub,1)-SizY/4;
+            SubRangy(isub,2)=SubRangy(isub,2)+SizY/4;
+        else
+            [U_smooth_sub,U_tps_sub]=tps_coeff(X(ind_sel),Y(ind_sel),U(ind_sel),rho);
+            [V_smooth_sub,V_tps_sub]=tps_coeff(X(ind_sel),Y(ind_sel),V(ind_sel),rho);
+            UDiff=U_smooth_sub-U(ind_sel);
+            VDiff=V_smooth_sub-V(ind_sel);
+            NormDiff=UDiff.*UDiff+VDiff.*VDiff;
+            FF(ind_sel)=20*(NormDiff>Threshold);%put FF value to 20 to identify the criterium of elimmination
+            ind_ind_sel=find(FF(ind_sel)==0); % select the indices of ind_sel corresponding to the remaining vectors
+            % no value exceeds threshold, the result is recorded
+            if isequal(numel(ind_ind_sel),numel(ind_sel))
+                U_smooth(ind_sel)=U_smooth(ind_sel)+U_smooth_sub;
+                V_smooth(ind_sel)=V_smooth(ind_sel)+V_smooth_sub;
+                nbpoints(isub)=numel(ind_sel);
+                X_tps(1:nbpoints(isub),isub)=X(ind_sel);
+                Y_tps(1:nbpoints(isub),isub)=Y(ind_sel);
+                U_tps(1:nbpoints(isub)+3,isub)=U_tps_sub;
+                V_tps(1:nbpoints(isub)+3,isub)=V_tps_sub;         
+                nb_select(ind_sel)=nb_select(ind_sel)+1;
+                 display('good')
+                break
+                % too few selected vectors, increase the subrange for next iteration
+            elseif numel(ind_ind_sel)<SubDomain/4 && ~isequal( ind_sel,ind_sel_previous);
+                SubRangx(isub,1)=SubRangx(isub,1)-SizX/4;
+                SubRangx(isub,2)=SubRangx(isub,2)+SizX/4;
+                SubRangy(isub,1)=SubRangy(isub,1)-SizY/4;
+                SubRangy(isub,2)=SubRangy(isub,2)+SizY/4;
+%                 display('fewsmooth')
+                % interpolation-smoothing is done again with the selected vectors
+            else
+                [U_smooth_sub,U_tps_sub]=tps_coeff(X(ind_sel(ind_ind_sel)),Y(ind_sel(ind_ind_sel)),U(ind_sel(ind_ind_sel)),rho);
+                [V_smooth_sub,V_tps_sub]=tps_coeff(X(ind_sel(ind_ind_sel)),Y(ind_sel(ind_ind_sel)),V(ind_sel(ind_ind_sel)),rho);
+                U_smooth(ind_sel(ind_ind_sel))=U_smooth(ind_sel(ind_ind_sel))+U_smooth_sub;
+                V_smooth(ind_sel(ind_ind_sel))=V_smooth(ind_sel(ind_ind_sel))+V_smooth_sub;
+                nbpoints(isub)=numel(ind_ind_sel);
+                X_tps(1:nbpoints(isub),isub)=X(ind_sel(ind_ind_sel));
+                Y_tps(1:nbpoints(isub),isub)=Y(ind_sel(ind_ind_sel));
+                U_tps(1:nbpoints(isub)+3,isub)=U_tps_sub;
+                V_tps(1:nbpoints(isub)+3,isub)=V_tps_sub;
+                nb_select(ind_sel(ind_ind_sel))=nb_select(ind_sel(ind_ind_sel))+1;
+                display('good2')
+                break
+            end
+        end
+    end
+end
+ind_empty=find(check_empty);
+%remove empty subdomains
+if ~isempty(ind_empty)
+    SubRangx(ind_empty,:)=[];
+    SubRangy(ind_empty,:)=[];
+    X_tps(:,ind_empty)=[];
+    Y_tps(:,ind_empty)=[];
+    U_tps(:,ind_empty)=[];
+    V_tps(:,ind_empty)=[];
+end
+nb_select(nb_select==0)=1;%ones(size(find(nb_select==0)));
+U_smooth=U_smooth./nb_select;
+V_smooth=V_smooth./nb_select;
+
+
+
+
+
Index: unk/src/civ_uvmat.m
===================================================================
--- /trunk/src/civ_uvmat.m	(revision 314)
+++ 	(revision )
@@ -1,857 +1,0 @@
-%'civ_matlab': Matlab version of the PIV programs CivX
-% --- call the sub-functions:
-%   civ: PIV function itself
-%   fix: removes false vectors after detection by various criteria
-%   patch: make interpolation-smoothing 
-%------------------------------------------------------------------------
-% function [Data,errormsg,result_conv]= civ_uvmat(Param,ncfile)
-%
-%OUTPUT
-% Data=structure containing the PIV results and information on the processing parameters
-% errormsg=error message char string, default=''
-% resul_conv: image inter-correlation function for the last grid point (used for tests)
-%
-%INPUT:
-% Param: input images and processing parameters
-% ncfile: name of a netcdf file to be created for the result (extension .nc)
-%
-%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
-%  Copyright  2011, LEGI / CNRS-UJF-INPG, joel.sommeria@legi.grenoble-inp.fr.
-%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
-%     This 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 (open UVMAT/COPYING.txt) for more details.
-%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
-
-function [Data,errormsg,result_conv]= civ_uvmat(Param,ncfile)
-errormsg='';
-Data.ListGlobalAttribute={'Conventions','Program','CivStage'};
-Data.Conventions='uvmat/civdata';% states the conventions used for the description of field variables and attributes
-Data.Program='civ_uvmat';
-Data.CivStage=0;%default
-ListVarCiv1={'Civ1_X','Civ1_Y','Civ1_U','Civ1_V','Civ1_C','Civ1_F'}; %variables to read
-ListVarFix1={'Civ1_X','Civ1_Y','Civ1_U','Civ1_V','Civ1_C','Civ1_F','Civ1_FF'};
-mask='';
-maskname='';%default
-test_civx=0;%default
-test_civ1=0;%default
-test_patch1=0;%default
-
-%% Civ1
-if isfield (Param,'Civ1')
-    test_civ1=1;% test for further use of civ1 results
-    par_civ1=Param.Civ1;
-    image1=imread(par_civ1.filename_ima_a);
-    image2=imread(par_civ1.filename_ima_b);
-    ibx2=ceil(par_civ1.Bx/2);
-    iby2=ceil(par_civ1.By/2);
-    isx2=ceil(par_civ1.Searchx/2);
-    isy2=ceil(par_civ1.Searchy/2);
-    shiftx=par_civ1.Shiftx;
-    shifty=par_civ1.Shifty;
-    miniy=max(1+isy2+shifty,1+iby2);
-    minix=max(1+isx2-shiftx,1+ibx2);
-    maxiy=min(size(image1,1)-isy2+shifty,size(image1,1)-iby2);
-    maxix=min(size(image1,2)-isx2-shiftx,size(image1,2)-ibx2);
-    if ~isfield(par_civ1,'PointCoord')    
-        [GridX,GridY]=meshgrid(minix:par_civ1.Dx:maxix,miniy:par_civ1.Dy:maxiy);
-        par_civ1.PointCoord(:,1)=reshape(GridX,[],1);
-        par_civ1.PointCoord(:,2)=reshape(GridY,[],1);
-    end
-    if par_civ1.CheckMask && isfield(par_civ1,'MaskName') && ~isempty(par_civ1.MaskName) 
-        maskname=par_civ1.MaskName;
-        mask=imread(maskname);
-    end
-    % caluclate velocity data (y and v in indices, reverse to y component)
-    [xtable ytable utable vtable ctable F result_conv errormsg] = civ (image1,image2,ibx2,iby2,isx2,isy2,shiftx,-shifty,par_civ1.PointCoord,par_civ1.Rho, mask);
-    list_param=(fieldnames(par_civ1))';
-    list_remove={'pxcmx','pxcmy','npx','npy','gridflag','maskflag','term_a','term_b','T0'};
-    index_remove=zeros(size(list_param));
-    for name=list_remove %loop on the list of names
-        index_remove=index_remove +strcmp(name{1},list_param);%index of the current name = name{1} 
-    end
-    list_param(find(index_remove,1))=[];
-    Civ1_param=list_param;%initialisation
-    for ilist=1:length(list_param)
-        Civ1_param{ilist}=['Civ1_' list_param{ilist}];
-        Data.(['Civ1_' list_param{ilist}])=par_civ1.(list_param{ilist});
-    end
-    if isfield(Data,'Civ1_gridname') && strcmp(Data.Civ1_gridname(1:6),'noFile')
-        Data.Civ1_gridname='';
-    end
-    if isfield(Data,'Civ1_maskname') && strcmp(Data.Civ1_maskname(1:6),'noFile')
-        Data.Civ1_maskname='';
-    end
-    Data.ListGlobalAttribute=[Data.ListGlobalAttribute Civ1_param {'Civ1_Time','Civ1_Dt'}];
-    Data.Civ1_Time=str2double(par_civ1.T0);
-    Data.Civ1_Dt=str2double(par_civ1.Dt);
-    Data.ListVarName={'Civ1_X','Civ1_Y','Civ1_U','Civ1_V','Civ1_C','Civ1_F'};%  cell array containing the names of the fields to record
-    Data.VarDimName={'nbvec1','nbvec1','nbvec1','nbvec1','nbvec1','nbvec1'};
-    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';
-    Data.Civ1_X=reshape(xtable,[],1);
-    Data.Civ1_Y=reshape(size(image1,1)-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);
-    Data.CivStage=1;
-else
-    Data=nc2struct(ncfile,'ListGlobalAttribute','absolut_time_T0'); %look for the constant 'absolut_time_T0' to detect old civx data format 
-    if ~isempty(Data.absolut_time_T0')%read civx file
-        test_civx=1;% test for old civx data format
-        [Data,vardetect,ichoice]=nc2struct(ncfile);%read the variables in the netcdf file
-    else
-        if isfield(Param,'Fix1')
-            Data=nc2struct(ncfile,ListVarCiv1);%read civ1 data in the existing netcdf file
-        else
-            Data=nc2struct(ncfile,ListVarFix1);%read civ1 and fix1 data in the existing netcdf file
-        end
-    end
-    if isfield(Data,'Txt')
-        msgbox_uvmat('ERROR',Data.Txt)
-        return
-    end
-end
-
-%% Fix1
-%                 if Param.CheckCiv1==1
-%                     Param.Civ1=Param.Civ1;
-%                 end
-%                 if Param.CheckFix1==1
-%                     Param.Fix1=Param.Fix1;
-%                     fix1.WarnFlags=[];
-%                     if get(handles.CheckFmin2,'Value')
-%                         fix1.WarnFlags=[fix1.WarnFlags -2];
-%                     end
-%                     if get(handles.CheckF3,'Value')
-%                         fix1.WarnFlags=[fix1.WarnFlags 3];
-%                     end
-%                     fix1.LowerBoundCorr=thresh_vecC1;
-%                     if get(handles.num_MinVel,'Value')
-%                         fix1.UppperBoundVel=thresh_vel1;
-%                     else
-%                         fix1.LowerBoundVel=thresh_vel1;
-%                     end
-%                     if get(handles.CheckMask,'Value')
-%                         fix1.MaskName=maskname;
-%                     end
-%                     Param.Fix1=fix1;
-%                 end
-%                 if Param.CheckPatch1==1
-%                     if strcmp(compare,'stereo PIV')
-%                         filebase_A=filecell.filebase;
-%                         [pp,ff]=fileparts(filebase_A);
-%                         filebase_B=fullfile(pp,get(handles.RootName_1,'String'));
-%                         %TO CHECK: filecell.nc.civ1{ifile,j},filecell.ncA.civ1{ifile,j} have been switched according to Matias Duran
-%                         RUN_STLIN(filecell.nc.civ1{ifile,j},filecell.ncA.civ1{ifile,j},'civ1',filecell.st{ifile,j},...
-%                             str2num(nx_patch1),str2num(ny_patch1),str2num(thresh_patch1),[filebase_A '.xml'],[filebase_B '.xml'])
-%                     else
-%                         Param.Patch1.Rho=rho_patch1;
-%                         Param.Patch1.Threshold=thresh_patch1;
-%                         Param.Patch1.SubDomain=subdomain_patch1;
-%                     end
-%                 end
-%                 if Param.CheckCiv2==1
-%                     Param.Civ2=Param.Civ2;
-%                 end
-%                 if Param.CheckFix2==1
-%                     fix2.WarnFlags=[];
-%                     if get(handles.CheckFmin2,'Value')
-%                         fix2.WarnFlags=[fix2.WarnFlags -2];
-%                     end
-%                     if get(handles.CheckF4,'Value')
-%                         fix2.WarnFlags=[fix2.WarnFlags 4];
-%                     end
-%                     if get(handles.CheckF3,'Value')
-%                         fix2.WarnFlags=[fix2.WarnFlags 3];
-%                     end
-%                     fix2.LowerBoundCorr=thresh_vec2C;
-%                     if get(handles.num_MinVel,'Value')
-%                         fix2.UppperBoundVel=thresh_vel2;
-%                     else
-%                         fix2.LowerBoundVel=thresh_vel2;
-%                     end
-%                     if get(handles.CheckMask,'Value')
-%                         fix2.MaskName=maskname;
-%                     end
-%                     Param.Fix2=fix2;
-%                 end
-%                 if Param.CheckPatch2==1
-%                     if strcmp(compare,'stereo PIV')
-%                         filebase_A=filecell.filebase;
-%                         [pp,ff]=fileparts(filebase_A);
-%                         filebase_B=fullfile(pp,get(handles.RootName_1,'String'));
-%                         RUN_STLIN(filecell.ncA.civ2{ifile,j},filecell.nc.civ2{ifile,j},'civ2',filecell.st{ifile,j},...
-%                             str2num(nx_patch2),str2num(ny_patch2),str2num(thresh_patch2),[filebase_A '.xml'],[filebase_B '.xml'])
-%                     else
-%                         Param.Patch2.Rho=rho_patch2;
-%                         Param.Patch2.Threshold=thresh_patch2;
-%                         Param.Patch2.SubDomain=subdomain_patch2;
-%                     end
-%                 end
-
-if isfield (Param,'Fix1')
-    ListFixParam=fieldnames(Param.Fix1);
-    for ilist=1:length(ListFixParam)
-        ParamName=ListFixParam{ilist};
-        ListName=['Fix1_' ParamName];
-        eval(['Data.ListGlobalAttribute=[Data.ListGlobalAttribute ''' ParamName '''];'])
-        eval(['Data.' ListName '=Param.Fix1.' ParamName ';'])
-    end
-%     Data.ListGlobalAttribute=[Data.ListGlobalAttribute {'Fix1_WarnFlags','Fix1_TreshCorr','Fix1_TreshVel','Fix1_UpperBoundTest'}];
-%     Data.Fix1_WarnFlags=Param.Fix1.WarnFlags;
-%     Data.Fix1_ThreshCorr=Param.Fix1.ThreshCorr;
-%     Data.Fix1_ThreshVel=Param.Fix1.ThreshVel;
-%     Data.Fix1_UpperBoundTest=Param.Fix1.UpperBoundTest;
-
-    if test_civx
-        if ~isfield(Data,'fix')
-            Data.ListGlobalAttribute=[Data.ListGlobalAttribute 'fix'];
-            Data.fix=1;
-            Data.ListVarName=[Data.ListVarName {'vec_FixFlag'}];
-            Data.VarDimName=[Data.VarDimName {'nb_vectors'}];
-        end
-        Data.vec_FixFlag=fix(Param.Fix1,Data.vec_F,Data.vec_C,Data.vec_U,Data.vec_V,Data.vec_X,Data.vec_Y);
-    else
-        Data.ListVarName=[Data.ListVarName {'Civ1_FF'}];
-        Data.VarDimName=[Data.VarDimName {'nbvec1'}];
-        nbvar=length(Data.ListVarName);
-        Data.VarAttribute{nbvar}.Role='errorflag';    
-        Data.Civ1_FF=fix(Param.Fix1,Data.Civ1_F,Data.Civ1_C,Data.Civ1_U,Data.Civ1_V);
-        Data.CivStage=2;    
-    end
-end   
-%% Patch1
-if isfield (Param,'Patch1')
-    test_patch1=1;
-    Data.ListGlobalAttribute=[Data.ListGlobalAttribute {'Patch1_Rho','Patch1_Threshold','Patch1_SubDomain'}];
-    Data.Patch1_Rho=str2double(Param.Patch1.Rho);
-    Data.Patch1_Threshold=str2double(Param.Patch1.Threshold);
-    Data.Patch1_SubDomain=str2double(Param.Patch1.SubDomain);
-    Data.ListVarName=[Data.ListVarName {'Civ1_U_Diff','Civ1_V_Diff','Civ1_X_SubRange','Civ1_Y_SubRange','Civ1_NbSites','Civ1_X_tps','Civ1_Y_tps','Civ1_U_tps','Civ1_V_tps'}];
-    Data.VarDimName=[Data.VarDimName {'NbVec1','NbVec1',{'NbSubDomain1','Two'},{'NbSubDomain1','Two'},'NbSubDomain1',...
-             {'NbVec1Sub','NbSubDomain1'},{'NbVec1Sub','NbSubDomain1'},{'Nbtps1','NbSubDomain1'},{'Nbtps1','NbSubDomain1'}}];
-    nbvar=length(Data.ListVarName);
-    Data.VarAttribute{nbvar-1}.Role='vector_x';
-    Data.VarAttribute{nbvar}.Role='vector_y';
-    Data.Civ1_U_Diff=zeros(size(Data.Civ1_X));
-    Data.Civ1_V_Diff=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
-    [Data.Civ1_X_SubRange,Data.Civ1_Y_SubRange,Data.Civ1_NbSites,FFres,Ures, Vres,Data.Civ1_X_tps,Data.Civ1_Y_tps,Data.Civ1_U_tps,Data.Civ1_V_tps]=...
-                            patch(Data.Civ1_X(ind_good)',Data.Civ1_Y(ind_good)',Data.Civ1_U(ind_good)',Data.Civ1_V(ind_good)',Data.Patch1_Rho,Data.Patch1_Threshold,Data.Patch1_SubDomain); 
-      Data.Civ1_U_Diff(ind_good)=Data.Civ1_U(ind_good)-Ures;
-      Data.Civ1_V_Diff(ind_good)=Data.Civ1_V(ind_good)-Vres;
-      Data.Civ1_FF(ind_good)=FFres;
-      Data.CivStage=3;                             
-end   
-
-%% Civ2
-if isfield (Param,'Civ2')
-    par_civ2=Param.Civ2;
-    if ~test_civ1 || ~strcmp(par_civ1.filename_ima_a,par_civ2.filename_ima_a)
-            image1=imread(par_civ2.filename_ima_a);%read first image if not already done for civ1 
-    end
-    if ~test_civ1|| ~strcmp(par_civ1.filename_ima_b,par_civ2.filename_ima_b)
-            image2=imread(par_civ2.filename_ima_b);%read second image if not already done for civ1 
-    end
-%     stepx=str2double(par_civ2.dx);
-%     stepy=str2double(par_civ2.dy);
-    ibx2=ceil(str2double(par_civ2.ibx)/2);
-    iby2=ceil(str2double(par_civ2.iby)/2);
-    isx2=ibx2+2;
-    isy2=iby2+2;
-    %get the previous guess for displacement
-    if ~test_patch1
-        [Guess,VelType]=read_civdata(ObjectName,InputField,ParamIn.VelType);%TO DEVELOP
-    else
-        Data.Civ1_U_Diff=zeros(size(Data.Civ1_X));
-        Data.Civ1_V_Diff=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
-            [Data.Civ1_X_SubRange,Data.Civ1_Y_SubRange,Data.Civ1_NbSites,FFres,Ures, Vres,Data.Civ1_X_tps,Data.Civ1_Y_tps,Data.Civ1_U_tps,Data.Civ1_V_tps]=...
-                                patch(Data.Civ1_X(ind_good)',Data.Civ1_Y(ind_good)',Data.Civ1_U(ind_good)',Data.Civ1_V(ind_good)',Data.Patch1_Rho,Data.Patch1_Threshold,Data.Patch1_SubDomain);
-        end 
-%     shiftx=str2num(par_civ1.shiftx);
-%     shifty=str2num(par_civ1.shifty);
-% TO GET shift from par_civ2.filename_nc1
-    % shiftx=velocity interpolated at position 
-    miniy=max(1+isy2+shifty,1+iby2);
-    minix=max(1+isx2-shiftx,1+ibx2);
-    maxiy=min(size(image1,1)-isy2+shifty,size(image1,1)-iby2);
-    maxix=min(size(image1,2)-isx2-shiftx,size(image1,2)-ibx2);
-    [GridX,GridY]=meshgrid(minix:par_civ2.Dx:maxix,miniy:par_civ2.Dy:maxiy);
-    PointCoord(:,1)=reshape(GridX,[],1);
-    PointCoord(:,2)=reshape(GridY,[],1);
-    if ~isempty(par_civ2.maskname)&& ~strcmp(maskname,par_civ2.maskname)% mask exist, not already read in civ1
-        mask=imread(par_civ2.maskname);
-    end
-    % caluclate velocity data (y and v in indices, reverse to y component)
-    [xtable ytable utable vtable ctable F] = civ (image1,image2,ibx2,iby2,isx2,isy2,shiftx,-shifty,PointCoord,str2num(par_civ1.rho),mask);
-    list_param=(fieldnames(par_civ1))';
-    list_remove={'pxcmx','pxcmy','npx','npy','gridflag','maskflag','term_a','term_b','T0'};
-    index=zeros(size(list_param));
-    for ilist=1:length(list_remove)
-        index=strcmp(list_remove{ilist},list_param);
-        if ~isempty(find(index,1))
-            list_param(index)=[];
-        end
-    end
-    for ilist=1:length(list_param)
-        Civ1_param{ilist}=['Civ1_' list_param{ilist}];
-        eval(['Data.Civ1_' list_param{ilist} '=Param.Civ1.' list_param{ilist} ';'])
-    end
-    if isfield(Data,'Civ1_gridname') && strcmp(Data.Civ1_gridname(1:6),'noFile')
-        Data.Civ1_gridname='';
-    end
-    if isfield(Data,'Civ1_maskname') && strcmp(Data.Civ1_maskname(1:6),'noFile')
-        Data.Civ1_maskname='';
-    end
-    Data.ListGlobalAttribute=[Data.ListGlobalAttribute Civ1_param {'Civ1_Time','Civ1_Dt'}];
-    Data.Civ1_Time=str2double(par_civ1.T0);
-    Data.Civ1_Dt=str2double(par_civ1.Dt);
-    Data.ListVarName={'Civ1_X','Civ1_Y','Civ1_U','Civ1_V','Civ1_C','Civ1_F'};%  cell array containing the names of the fields to record
-    Data.VarDimName={'nbvec1','nbvec1','nbvec1','nbvec1','nbvec1','nbvec1'};
-    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';
-    Data.Civ1_X=reshape(xtable,[],1);
-    Data.Civ1_Y=reshape(size(image1,1)-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);
-    Data.CivStage=Data.CivStage+1;
-end
-
-%% Fix2
-if isfield (Param,'Fix2')
-    ListFixParam=fieldnames(Param.Fix2);
-    for ilist=1:length(ListFixParam)
-        ParamName=ListFixParam{ilist};
-        ListName=['Fix1_' ParamName];
-        eval(['Data.ListGlobalAttribute=[Data.ListGlobalAttribute ''' ParamName '''];'])
-        eval(['Data.' ListName '=Param.Fix2.' ParamName ';'])
-    end
-    if test_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 {'nbvec2'}];
-        nbvar=length(Data.ListVarName);
-        Data.VarAttribute{nbvar}.Role='errorflag';    
-        Data.Civ2_FF=fix(Param.Fix2,Data.Civ2_F,Data.Civ2_C,Data.Civ2_U,Data.Civ2_V);
-        Data.CivStage=5;    
-    end
-    
-end   
-
-%% Patch2
-if isfield (Param,'Patch2')
-    Data.ListGlobalAttribute=[Data.ListGlobalAttribute {'Patch2_Rho','Patch2_Threshold','Patch2_SubDomain'}];
-    Data.Patch2_Rho=str2double(Param.Patch2.Rho);
-    Data.Patch2_Threshold=str2double(Param.Patch2.Threshold);
-    Data.Patch2_SubDomain=str2double(Param.Patch2.SubDomain);
-    Data.ListVarName=[Data.ListVarName {'Civ2_U_Diff','Civ2_V_Diff','Civ2_X_SubRange','Civ2_Y_SubRange','Civ2_NbSites','Civ2_X_tps','Civ2_Y_tps','Civ2_U_tps','Civ2_V_tps'}];
-    Data.VarDimName=[Data.VarDimName {'NbVec2','NbVec2',{'NbSubDomain2','Two'},{'NbSubDomain2','Two'},'NbSubDomain2',...
-             {'NbVec2Sub','NbSubDomain2'},{'NbVec2Sub','NbSubDomain2'},{'Nbtps2','NbSubDomain2'},{'Nbtps2','NbSubDomain2'}}];
-    nbvar=length(Data.ListVarName);
-    Data.VarAttribute{nbvar-1}.Role='vector_x';
-    Data.VarAttribute{nbvar}.Role='vector_y';
-    Data.Civ2_U_Diff=zeros(size(Data.Civ2_X));
-    Data.Civ2_V_Diff=zeros(size(Data.Civ2_X));
-    if isfield(Data,'Civ2_FF')
-        ind_good=find(Data.Civ2_FF==0);
-    else
-        ind_good=1:numel(Data.Civ2_X);
-    end
-    [Data.Civ2_X_SubRange,Data.Civ2_Y_SubRange,Data.Civ2_NbSites,FFres,Ures, Vres,Data.Civ2_X_tps,Data.Civ2_Y_tps,Data.Civ2_U_tps,Data.Civ2_V_tps]=...
-                            patch(Data.Civ2_X(ind_good)',Data.Civ2_Y(ind_good)',Data.Civ2_U(ind_good)',Data.Civ2_V(ind_good)',Data.Patch2_Rho,Data.Patch2_Threshold,Data.Patch2_SubDomain); 
-      Data.Civ2_U_Diff(ind_good)=Data.Civ2_U(ind_good)-Ures;
-      Data.Civ2_V_Diff(ind_good)=Data.Civ2_V(ind_good)-Vres;
-      Data.Civ2_FF(ind_good)=FFres;
-      Data.CivStage=6;                             
-end   
-
-%% write result in a netcdf file if requested
-if exist('ncfile','var')
-    errormsg=struct2nc(ncfile,Data);
-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
-Param
-
-%criterium on warn flags
-FlagName={'CheckFmin2','CheckF2','CheckF3','CheckF4'};
-FlagVal=[-2 2 3 4];
-for iflag=1:numel(FlagVal)
-    if Param.(CheckFlag(iflag))
-        FF=(FF==1| F==FlagVal(iflag));
-% if isfield (Param,'WarnFlags')
-%     for iflag=1:numel(Param.WarnFlags)
-%         FF=(FF==1| F==Param.WarnFlags(iflag));
-%     end
-% end
-    end
-end
-%criterium on correlation values
-if isfield (Param,'MinCorr')
-    FF=FF==1 | C<Param.MinCorr;
-end
-return
-
-
-if isfield (Param,'LowerBoundVel')&& ~isequal(Param.LowerBoundVel,0)
-    thresh=Param.LowerBoundVel*Param.LowerBoundVel;
-    FF=FF==1 | (U.*U+V.*V)<thresh;
-end
-if isfield (Param,'UpperBoundVel')&& ~isequal(Param.UpperBoundVel,0)
-    thresh=Param.UpperBoundVel*Param.UpperBoundVel;
-    FF=FF==1 | (U.*U+V.*V)>thresh;
-end
-if isfield(Param,'MaskName')
-   M=imread(Param.MaskName);
-   nxy=size(M);
-   M=reshape(M,1,[]);
-   rangx0=[0.5 nxy(2)-0.5];
-   rangy0=[0.5 nxy(1)-0.5];
-   vec_x1=X-U/2;%beginning points
-   vec_x2=X+U/2;%end points of vectors
-   vec_y1=Y-V/2;%beginning points
-   vec_y2=Y+V/2;%end points of vectors
-   indx=1+round((nxy(2)-1)*(vec_x1-rangx0(1))/(rangx0(2)-rangx0(1)));% image index x at abcissa vec_x1
-   indy=1+round((nxy(1)-1)*(vec_y1-rangy0(1))/(rangy0(2)-rangy0(1)));% image index y at ordinate vec_y1   
-   test_in=~(indx < 1 |indy < 1 | indx > nxy(2) |indy > nxy(1)); %=0 out of the mask image, 1 inside
-   indx=indx.*test_in+(1-test_in); %replace indx by 1 out of the mask range
-   indy=indy.*test_in+(1-test_in); %replace indy by 1 out of the mask range
-   ICOMB=((indx-1)*nxy(1)+(nxy(1)+1-indy));%determine the indices in the image reshaped in a Matlab vector
-   Mvalues=M(ICOMB);
-   flag7b=((20 < Mvalues) & (Mvalues < 200))| ~test_in';
-   indx=1+round((nxy(2)-1)*(vec_x2-rangx0(1))/(rangx0(2)-rangx0(1)));% image index x at abcissa vec_x2
-   indy=1+round((nxy(1)-1)*(vec_y2-rangy0(1))/(rangy0(2)-rangy0(1)));% image index y at ordinate vec_y2
-   test_in=~(indx < 1 |indy < 1 | indx > nxy(2) |indy > nxy(1)); %=0 out of the mask image, 1 inside
-   indx=indx.*test_in+(1-test_in); %replace indx by 1 out of the mask range
-   indy=indy.*test_in+(1-test_in); %replace indy by 1 out of the mask range
-   ICOMB=((indx-1)*nxy(1)+(nxy(1)+1-indy));%determine the indices in the image reshaped in a Matlab vector
-   Mvalues=M(ICOMB);
-   flag7e=((Mvalues > 20) & (Mvalues < 200))| ~test_in';
-   FF=FF==1 |(flag7b|flag7e)';
-end
-%    flag7=0;
-% end   
-
-
-FF=double(FF);
-% 
-% % criterium on velocity values
-% delta_u=Field.U;%default without ref file
-% delta_v=Field.V;
-% if exist('fileref','var') && ~isempty(fileref)
-%     if ~exist(fileref,'file')
-%         error='reference file not found in RUN_FIX.m';
-%         display(error);
-%         return
-%     end
-%     FieldRef=read_civxdata(fileref,[],fieldref);   
-%     if isfield(FieldRef,'FF')
-%         index_true=find(FieldRef.FF==0);
-%         FieldRef.X=FieldRef.X(index_true);
-%         FieldRef.Y=FieldRef.Y(index_true);
-%         FieldRef.U=FieldRef.U(index_true);
-%         FieldRef.V=FieldRef.V(index_true);
-%     end
-%     if ~isfield(FieldRef,'X') || ~isfield(FieldRef,'Y') || ~isfield(FieldRef,'U') || ~isfield(FieldRef,'V')
-%         error='reference file is not a velocity field in RUN_FIX.m '; %bad input file
-%         return
-%     end
-%     if length(FieldRef.X)<=1
-%         errordlg('reference field with one vector or less in RUN_FIX.m')
-%         return
-%     end
-%     vec_U_ref=griddata_uvmat(FieldRef.X,FieldRef.Y,FieldRef.U,Field.X,Field.Y);  %interpolate vectors in the ref field
-%     vec_V_ref=griddata_uvmat(FieldRef.X,FieldRef.Y,FieldRef.V,Field.X,Field.Y);  %interpolate vectors in the ref field to the positions  of the main field     
-%     delta_u=Field.U-vec_U_ref;%take the difference with the interpolated ref field
-%     delta_v=Field.V-vec_V_ref;
-% end
-% thresh_vel_x=thresh_vel; 
-% thresh_vel_y=thresh_vel; 
-% if isequal(inf_sup,1)
-%     flag5=abs(delta_u)<thresh_vel_x & abs(delta_v)<thresh_vel_y &(flag1~=1)&(flag2~=1)&(flag3~=1)&(flag4~=1);
-% elseif isequal(inf_sup,2)
-%     flag5=(abs(delta_u)>thresh_vel_x | abs(delta_v)>thresh_vel_y) &(flag1~=1)&(flag2~=1)&(flag3~=1)&(flag4~=1);
-% end
-% 
-%             % flag7 introduce a grey mask, matrix M
-
-% flagmagenta=flag1|flag2|flag3|flag4|flag5|flag7;
-% fixflag_unit=Field.FF-10*floor(Field.FF/10); %unity term of fix_flag
-
-
-
-%------------------------------------------------------------------------
-% patch function
-function [SubRangx,SubRangy,nbpoints,FF,U_smooth,V_smooth,X_tps,Y_tps,U_tps,V_tps] =patch(X,Y,U,V,Rho,Threshold,SubDomain)
-%subdomain decomposition
-warning off
-U=reshape(U,[],1);
-V=reshape(V,[],1);
-X=reshape(X,[],1);
-Y=reshape(Y,[],1);
-nbvec=numel(X);
-NbSubDomain=ceil(nbvec/SubDomain);
-MinX=min(X);
-MinY=min(Y);
-MaxX=max(X);
-MaxY=max(Y);
-RangX=MaxX-MinX;
-RangY=MaxY-MinY;
-AspectRatio=RangY/RangX;
-NbSubDomainX=max(floor(sqrt(NbSubDomain/AspectRatio)),1);
-NbSubDomainY=max(floor(sqrt(NbSubDomain*AspectRatio)),1);
-NbSubDomain=NbSubDomainX*NbSubDomainY;
-SizX=RangX/NbSubDomainX;%width of subdomains
-SizY=RangY/NbSubDomainY;%height of subdomains
-CentreX=linspace(MinX+SizX/2,MaxX-SizX/2,NbSubDomainX);
-CentreY=linspace(MinY+SizY/2,MaxY-SizY/2,NbSubDomainY);
-[CentreX,CentreY]=meshgrid(CentreX,CentreY);
-CentreY=reshape(CentreY,1,[]);
-CentreX=reshape(CentreX,1,[]);
-rho=SizX*SizY*Rho/1000000;%optimum rho increase as the area of the subdomain (division by 10^6 to reach good values with the default GUI input)
-U_tps_sub=zeros(length(X),NbSubDomain);%default spline
-V_tps_sub=zeros(length(X),NbSubDomain);%default spline
-U_smooth=zeros(length(X),1);
-V_smooth=zeros(length(X),1);
-
-nb_select=zeros(length(X),1);
-FF=zeros(length(X),1);
-test_empty=zeros(1,NbSubDomain);
-for isub=1:NbSubDomain
-    SubRangx(isub,:)=[CentreX(isub)-SizX/2 CentreX(isub)+SizX/2];
-    SubRangy(isub,:)=[CentreY(isub)-SizY/2 CentreY(isub)+SizY/2];
-    ind_sel_previous=[];
-    ind_sel=0;
-    while numel(ind_sel)>numel(ind_sel_previous) %increase the subdomain during four iterations at most
-        ind_sel_previous=ind_sel;
-        ind_sel=find(X>SubRangx(isub,1) & X<SubRangx(isub,2) & Y>SubRangy(isub,1) & Y<SubRangy(isub,2));
-        % if no vector in the subdomain, skip the subdomain
-        if isempty(ind_sel)
-            test_empty(isub)=1;    
-            U_tps(1,isub)=0;%define U_tps and V_tps by default
-            V_tps(1,isub)=0;
-            break
-            % if too few selected vectors, increase the subrange for next iteration
-        elseif numel(ind_sel)<SubDomain/4 && ~isequal( ind_sel,ind_sel_previous);
-            SubRangx(isub,1)=SubRangx(isub,1)-SizX/4;
-            SubRangx(isub,2)=SubRangx(isub,2)+SizX/4;
-            SubRangy(isub,1)=SubRangy(isub,1)-SizY/4;
-            SubRangy(isub,2)=SubRangy(isub,2)+SizY/4;
-        else
-            [U_smooth_sub,U_tps_sub]=tps_coeff(X(ind_sel),Y(ind_sel),U(ind_sel),rho);
-            [V_smooth_sub,V_tps_sub]=tps_coeff(X(ind_sel),Y(ind_sel),V(ind_sel),rho);
-            UDiff=U_smooth_sub-U(ind_sel);
-            VDiff=V_smooth_sub-V(ind_sel);
-            NormDiff=UDiff.*UDiff+VDiff.*VDiff;
-            FF(ind_sel)=20*(NormDiff>Threshold);%put FF value to 20 to identify the criterium of elimmination
-            ind_ind_sel=find(FF(ind_sel)==0); % select the indices of ind_sel corresponding to the remaining vectors
-            % no value exceeds threshold, the result is recorded
-            if isequal(numel(ind_ind_sel),numel(ind_sel))
-                U_smooth(ind_sel)=U_smooth(ind_sel)+U_smooth_sub;
-                V_smooth(ind_sel)=V_smooth(ind_sel)+V_smooth_sub;
-                nbpoints(isub)=numel(ind_sel);
-                X_tps(1:nbpoints(isub),isub)=X(ind_sel);
-                Y_tps(1:nbpoints(isub),isub)=Y(ind_sel);
-                U_tps(1:nbpoints(isub)+3,isub)=U_tps_sub;
-                V_tps(1:nbpoints(isub)+3,isub)=V_tps_sub;         
-                nb_select(ind_sel)=nb_select(ind_sel)+1;
-                 display('good')
-                break
-                % too few selected vectors, increase the subrange for next iteration
-            elseif numel(ind_ind_sel)<SubDomain/4 && ~isequal( ind_sel,ind_sel_previous);
-                SubRangx(isub,1)=SubRangx(isub,1)-SizX/4;
-                SubRangx(isub,2)=SubRangx(isub,2)+SizX/4;
-                SubRangy(isub,1)=SubRangy(isub,1)-SizY/4;
-                SubRangy(isub,2)=SubRangy(isub,2)+SizY/4;
-%                 display('fewsmooth')
-                % interpolation-smoothing is done again with the selected vectors
-            else
-                [U_smooth_sub,U_tps_sub]=tps_coeff(X(ind_sel(ind_ind_sel)),Y(ind_sel(ind_ind_sel)),U(ind_sel(ind_ind_sel)),rho);
-                [V_smooth_sub,V_tps_sub]=tps_coeff(X(ind_sel(ind_ind_sel)),Y(ind_sel(ind_ind_sel)),V(ind_sel(ind_ind_sel)),rho);
-                U_smooth(ind_sel(ind_ind_sel))=U_smooth(ind_sel(ind_ind_sel))+U_smooth_sub;
-                V_smooth(ind_sel(ind_ind_sel))=V_smooth(ind_sel(ind_ind_sel))+V_smooth_sub;
-                nbpoints(isub)=numel(ind_ind_sel);
-                X_tps(1:nbpoints(isub),isub)=X(ind_sel(ind_ind_sel));
-                Y_tps(1:nbpoints(isub),isub)=Y(ind_sel(ind_ind_sel));
-                U_tps(1:nbpoints(isub)+3,isub)=U_tps_sub;
-                V_tps(1:nbpoints(isub)+3,isub)=V_tps_sub;
-                nb_select(ind_sel(ind_ind_sel))=nb_select(ind_sel(ind_ind_sel))+1;
-                display('good2')
-                break
-            end
-        end
-    end
-end
-ind_empty=find(test_empty);
-%remove empty subdomains
-if ~isempty(ind_empty)
-    SubRangx(ind_empty,:)=[];
-    SubRangy(ind_empty,:)=[];
-    X_tps(:,ind_empty)=[];
-    Y_tps(:,ind_empty)=[];
-    U_tps(:,ind_empty)=[];
-    V_tps(:,ind_empty)=[];
-end
-nb_select(nb_select==0)=1;%ones(size(find(nb_select==0)));
-U_smooth=U_smooth./nb_select;
-V_smooth=V_smooth./nb_select;
-
-
-% '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:
-% 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=[];
-function [xtable ytable utable vtable ctable F result_conv errormsg] = civ (image1,image2,ibx2,iby2,isx2,isy2,shiftx,shifty, GridIndices, subpixfinder,mask)
-%this funtion performs the DCC PIV analysis. Recent window-deformation
-%methods perform better and will maybe be implemented in the future.
-nbvec=size(GridIndices,1);
-xtable=zeros(nbvec,1);
-ytable=xtable;
-utable=xtable;
-vtable=xtable;
-ctable=xtable;
-F=xtable;
-result_conv=[];
-errormsg='';
-%warning off %MATLAB:log:logOfZero
-[npy_ima npx_ima]=size(image1);
-if ~isequal(size(image2),[npy_ima npx_ima])
-    errormsg='image pair with unequal size';
-    return
-end
-
-%% mask
-testmask=0;
-image1=double(image1);
-image2=double(image2);
-if exist('mask','var') && ~isempty(mask)
-   testmask=1;
-   if ~isequal(size(mask),[npy_ima npx_ima])
-        errormsg='mask must be an image with the same size as the images';
-        return
-   end
-    % Convention for mask
-    % 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
-    test_noflux=(mask<100) ;
-    test_undefined=(mask<200 & mask>=100 );
-    image1(test_undefined)=min(min(image1));% put image to zero in the undefined  area
-    image2(test_undefined)=min(min(image2));% put image to zero in the undefined  area
-end
-
-%% calculate correlations: MAINLOOP on velocity vectors
-corrmax=0;
-sum_square=1;% default
-for ivec=1:nbvec 
-    iref=GridIndices(ivec,1);
-    jref=GridIndices(ivec,2);
-    testmask_ij=0;
-    test0=0;
-    if testmask
-        if mask(jref,iref)<=20
-           vector=[0 0];
-           test0=1;
-        else
-            mask_crop1=mask(jref-iby2:jref+iby2,iref-ibx2:iref+ibx2);
-            mask_crop2=mask(jref+shifty-isy2:jref+shifty+isy2,iref+shiftx-isx2:iref+shiftx+isx2);
-            if ~isempty(find(mask_crop1<=200 & mask_crop1>100,1)) || ~isempty(find(mask_crop2<=200 & mask_crop2>100,1));
-                testmask_ij=1;
-            end
-        end
-    end
-    if ~test0    
-        image1_crop=image1(jref-iby2:jref+iby2,iref-ibx2:iref+ibx2);%extract a subimage (correlation box) from images 1  
-        image2_crop=image2(jref+shifty-isy2:jref+shifty+isy2,iref+shiftx-isx2:iref+shiftx+isx2);%extract a larger subimage (search box) from image 2
-        image1_crop=image1_crop-mean(mean(image1_crop));%substract the mean
-        image2_crop=image2_crop-mean(mean(image2_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 subpixfinder==1
-                    [vector,F(ivec)] = SUBPIXGAUSS (result_conv,x,y);
-                elseif subpixfinder==2
-                    [vector,F(ivec)] = SUBPIX2DGAUSS (result_conv,x,y);
-                end
-                sum_square=sum(sum(image1_crop.*image1_crop));
-                ctable(ivec)=corrmax/sum_square;% correlation value
-%                 if vector(1)>shiftx+isx2-ibx2+subpixfinder || vector(2)>shifty+isy2-iby2+subpixfinder
-%                     F(ivec)=-2;%vector reaches the border of the search zone
-%                 end
-            catch ME
-                vector=[0 0]; %if something goes wrong with cross correlation.....
-                F(ivec)=3;
-            end
-        else
-            vector=[0 0]; %if something goes wrong with cross correlation.....
-            F(ivec)=3;
-        end
-        if testmask_ij
-            F(ivec)=3;
-        end
-    end
-    
-    %Create the vector matrix x, y, u, v
-    xtable(ivec)=iref+vector(1)/2;% convec flow (velocity taken at the point middle from imgae1 and 2)
-    ytable(ivec)=jref+vector(2)/2;
-    utable(ivec)=vector(1)+shiftx;
-    vtable(ivec)=vector(2)+shifty;
-end
-result_conv=result_conv*corrmax/(255*sum_square);% keep the last correlation matrix for output
-
-
-function [vector,F] = SUBPIXGAUSS (result_conv,x,y)
-vector=[0 0]; %default
-F=0;
-[npy,npx]=size(result_conv);
-
-% if (x <= (size(result_conv,1)-1)) && (y <= (size(result_conv,1)-1)) && (x >= 1) && (y >= 1)
-    %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-1 && y >= 1
-        f0 = log(result_conv(y,x));
-        f1 = real(log(result_conv(y-1,x)));
-        f2 = real(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 = real(log(result_conv(y,x-1)));
-        f2 = real(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];
-% else
-%     vector=[NaN NaN];
-% end
-
-function [vector,F] = SUBPIX2DGAUSS (result_conv,x,y)
-vector=[0 0]; %default
-F=-2;
-peaky=y;
-peakx=x;
-[npy,npx]=size(result_conv);
-if (x <= npx-1) && (y <= npy-1) && (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: 511515
-            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));
-            %c00(j+2,i+2)=(5-3*i^2-3*j^2)*log(result_conv_norm(maxY+j, maxX+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];
-
-
Index: /trunk/src/mouse_motion.m
===================================================================
--- /trunk/src/mouse_motion.m	(revision 314)
+++ /trunk/src/mouse_motion.m	(revision 315)
@@ -218,13 +218,13 @@
              % case of PIV correlation display
                     if test_piv
-                        %par=civ('read_param_civ1',hhciv);
                         par=read_GUI(hhciv.Civ1);
                         [dd,ind_pt]=min(abs(Field.X-xy(1,1))+abs(Field.Y-xy(1,2)));
                         xround=Field.X(ind_pt);
                         yround=Field.Y(ind_pt);
-                        par.PointCoord=[xround size(Field.A,1)-yround+1];
+                        par.Grid=[xround size(Field.A,1)-yround+1];
                         % mark the correlation box with a rectangle
-                        par.filename_ima_a=Field.A;
-                        par.filename_ima_b=Field.B;
+                        par.ImageA=Field.A;
+                        par.ImageB=Field.B;
+                        [par.ImageHeight,par.ImageWidth]=size(par.ImageA);
                         Param.Civ1=par;
                         ibx2=floor((par.Bx-1)/2);
@@ -249,7 +249,5 @@
                             set(hhhh,'Position',[xround-isx2+shiftx yround-isy2+shifty 2*isx2 2*isy2])
                         end
-                        %[xtable, ytable, utable, vtable, ctable, typevector, result_conv] = ...
-                         %               pivlab (Field.A,Field.B,ibx2,iby2,isx2,isy2,shiftx,shifty,[xround size(Field.A,1)-yround+1], 1, []);
-                        [Data,errormsg,result_conv]= civ_matlab(Param)
+                        [Data,errormsg,result_conv]= civ_matlab(Param);
                         rangx(1)=-(isx2-ibx2)+shiftx;
                         rangx(2)=isx2-ibx2+shiftx;
@@ -268,6 +266,5 @@
                                 set(0,'CurrentFigure',corrfig(1))
                                 AxeData.CurrentCorrImage=imagesc(rangx,-rangy,result_conv,[0 1]);
-                                AxeData.CurrentVector=line([0 Data.Civ1_U],[0 -Data.Civ1_V],'Tag','vector');
-                                
+                                AxeData.CurrentVector=line([0 Data.Civ1_U],[0 Data.Civ1_V],'Tag','vector');               
                                 colorbar
                                 set(haxes,'UserData',AxeData)
@@ -278,5 +275,5 @@
                             set(AxeData.CurrentCorrImage,'XData',rangx)
                             set(AxeData.CurrentCorrImage,'YData',-rangy)
-                            set(AxeData.CurrentVector,'XData',[0 Data.Civ1_U],'YData',[0 -Data.Civ1_V])
+                            set(AxeData.CurrentVector,'XData',[0 Data.Civ1_U],'YData',[0 Data.Civ1_V])
                         end
                     end
Index: unk/src/pivlab.m
===================================================================
--- /trunk/src/pivlab.m	(revision 314)
+++ 	(revision )
@@ -1,198 +1,0 @@
-% 'pivlab': function piv.m adapted from PIVlab http://pivlab.blogspot.com/
-%--------------------------------------------------------------------------
-% function [xtable ytable utable vtable typevector] = pivlab (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:
-% 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=[];
-function [xtable ytable utable vtable ctable F result_conv errormsg] = pivlab (image1,image2,ibx2,iby2,isx2,isy2,shiftx,shifty, GridIndices, subpixfinder,mask)
-%this funtion performs the DCC PIV analysis. Recent window-deformation
-%methods perform better and will maybe be implemented in the future.
-nbvec=size(GridIndices,1);
-xtable=zeros(nbvec,1);
-ytable=xtable;
-utable=xtable;
-vtable=xtable;
-ctable=xtable;
-F=xtable;
-result_conv=[];
-errormsg='';
-%warning off %MATLAB:log:logOfZero
-[npy_ima npx_ima]=size(image1);
-if ~isequal(size(image2),[npy_ima npx_ima])
-    errormsg='image pair with unequal size';
-    return
-end
-
-%% mask
-testmask=0;
-image1=double(image1);
-image2=double(image2);
-if exist('mask','var') && ~isempty(mask)
-   testmask=1;
-   if ~isequal(size(mask),[npy_ima npx_ima])
-        errormsg='mask must be an image with the same size as the images';
-        return
-   end
-    % Convention for mask
-    % 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
-    test_noflux=(mask<100) ;
-    test_undefined=(mask<200 & mask>=100 );
-    image1(test_undefined)=min(min(image1));% put image to zero in the undefined  area
-    image2(test_undefined)=min(min(image2));% put image to zero in the undefined  area
-end
-
-%% calculate correlations: MAINLOOP on velocity vectors
-corrmax=0;
-sum_square=1;% default
-for ivec=1:nbvec 
-    iref=GridIndices(ivec,1);
-    jref=GridIndices(ivec,2);
-    testmask_ij=0;
-    test0=0;
-    if testmask
-        if mask(jref,iref)<=20
-           vector=[0 0];
-           test0=1;
-        else
-            mask_crop1=mask(jref-iby2:jref+iby2,iref-ibx2:iref+ibx2);
-            mask_crop2=mask(jref+shifty-isy2:jref+shifty+isy2,iref+shiftx-isx2:iref+shiftx+isx2);
-            if ~isempty(find(mask_crop1<=200 & mask_crop1>100,1)) || ~isempty(find(mask_crop2<=200 & mask_crop2>100,1));
-                testmask_ij=1;
-            end
-        end
-    end
-    if ~test0    
-        image1_crop=image1(jref-iby2:jref+iby2,iref-ibx2:iref+ibx2);%extract a subimage (correlation box) from images 1  
-        image2_crop=image2(jref+shifty-isy2:jref+shifty+isy2,iref+shiftx-isx2:iref+shiftx+isx2);%extract a larger subimage (search box) from image 2
-        image1_crop=image1_crop-mean(mean(image1_crop));%substract the mean
-        image2_crop=image2_crop-mean(mean(image2_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 subpixfinder==1
-                    [vector,F(ivec)] = SUBPIXGAUSS (result_conv,x,y);
-                elseif subpixfinder==2
-                    [vector,F(ivec)] = SUBPIX2DGAUSS (result_conv,x,y);
-                end
-                sum_square=sum(sum(image1_crop.*image1_crop));
-                ctable(ivec)=corrmax/sum_square;% correlation value
-%                 if vector(1)>shiftx+isx2-ibx2+subpixfinder || vector(2)>shifty+isy2-iby2+subpixfinder
-%                     F(ivec)=-2;%vector reaches the border of the search zone
-%                 end
-            catch ME
-                vector=[0 0]; %if something goes wrong with cross correlation.....
-                F(ivec)=3;
-            end
-        else
-            vector=[0 0]; %if something goes wrong with cross correlation.....
-            F(ivec)=3;
-        end
-        if testmask_ij
-            F(ivec)=3;
-        end
-    end
-    
-    %Create the vector matrix x, y, u, v
-    xtable(ivec)=iref+vector(1)/2;% convec flow (velocity taken at the point middle from imgae1 and 2)
-    ytable(ivec)=jref+vector(2)/2;
-    utable(ivec)=vector(1)+shiftx;
-    vtable(ivec)=vector(2)+shifty;
-end
-result_conv=result_conv*corrmax/(255*sum_square);% keep the last correlation matrix for output
-
-
-function [vector,F] = SUBPIXGAUSS (result_conv,x,y)
-vector=[0 0]; %default
-F=0;
-[npy,npx]=size(result_conv);
-
-% if (x <= (size(result_conv,1)-1)) && (y <= (size(result_conv,1)-1)) && (x >= 1) && (y >= 1)
-    %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-1 && y >= 1
-        f0 = log(result_conv(y,x));
-        f1 = real(log(result_conv(y-1,x)));
-        f2 = real(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 = real(log(result_conv(y,x-1)));
-        f2 = real(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];
-% else
-%     vector=[NaN NaN];
-% end
-
-function [vector,F] = SUBPIX2DGAUSS (result_conv,x,y)
-vector=[0 0]; %default
-F=-2;
-peaky=y;
-peakx=x;
-[npy,npx]=size(result_conv);
-if (x <= npx-1) && (y <= npy-1) && (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: 511515
-            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));
-            %c00(j+2,i+2)=(5-3*i^2-3*j^2)*log(result_conv_norm(maxY+j, maxX+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];
Index: /trunk/src/set_col_vec.m
===================================================================
--- /trunk/src/set_col_vec.m	(revision 314)
+++ /trunk/src/set_col_vec.m	(revision 315)
@@ -10,7 +10,7 @@
             % colcode.CName: 'ima_cor','black','white',...
             % colcode.ListColorCode ='black', 'white', 'rgb','brg', '64 colors'
-            % colcode.FixedCbounds =0; thresholds scaling relative to min and max, =1 fixed thresholds
-            % colcode.MinC; min 
-            % colcode.MaxC; max
+            % colcode.CheckFixVecColor =0; thresholds scaling relative to min and max, =1 fixed thresholds
+            % colcode.MinVec; min 
+            % colcode.MaxVec; max
             % colcode.ColCode1: first threshold for rgb, relative to min (0) and max (1)
             % colcode.ColCode2: second threshold for rgb, relative to min (0) and max (1), 
@@ -19,5 +19,4 @@
 % vec_C: matlab vector representing the scalar setting the color
 function [colorlist,col_vec,colcode_out]=set_col_vec(colcode,vec_C)
-
 col_vec=[]; 
 colcode_out=colcode;%default
@@ -61,11 +60,11 @@
     col_vec=ones(size(vec_C));%all vectors at color#1
 elseif strcmp(colcode.ListColorCode,'rgb')|| strcmp(colcode.ListColorCode,'bgr')% 3 color representation
-    ind1=find(vec_C < ColCode1); % =1 for red vectors
+%    ind1=find(vec_C < ColCode1); % =1 for red vectors
     ind_green=find((vec_C >= ColCode1) & (vec_C < ColCode2));% =1 for green vectors
-    ind3=find(vec_C >= ColCode2);% =1 for blue vectors
+%     ind3=find(vec_C >= ColCode2);% =1 for blue vectors
     colorlist(2,:)=[0 1 0];%green
-    col_vec(ind1)=1;
+    col_vec(vec_C < ColCode1)=1;
     col_vec(ind_green)=2;
-    col_vec(ind3)=3;
+    col_vec(vec_C >= ColCode2)=3;
     if strcmp(colcode.ListColorCode,'rgb')
         colorlist(1,:)=[1 0 0];%red
Index: /trunk/src/set_grid.m
===================================================================
--- /trunk/src/set_grid.m	(revision 314)
+++ /trunk/src/set_grid.m	(revision 315)
@@ -32,8 +32,10 @@
                    'gui_LayoutFcn',  [] , ...
                    'gui_Callback',   []);
-if nargin & isstr(varargin{1})
+% if nargin & isstr(varargin{1})
+%     gui_State.gui_Callback = str2func(varargin{1});
+% end
+if nargin && ischar(varargin{1})
     gui_State.gui_Callback = str2func(varargin{1});
 end
-
 if nargout
     [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
Index: /trunk/src/uvmat.m
===================================================================
--- /trunk/src/uvmat.m	(revision 314)
+++ /trunk/src/uvmat.m	(revision 315)
@@ -932,11 +932,11 @@
 %% view the field  
 run0_Callback(hObject, eventdata, handles); %view field
-mask_test=get(handles.mask_test,'value');
+mask_test=get(handles.CheckMask,'value');
 if mask_test
-    MaskData=get(handles.mask_test,'UserData');
+    MaskData=get(handles.CheckMask,'UserData');
     if isfield(MaskData,'maskhandle') && ishandle(MaskData.maskhandle)
           delete(MaskData.maskhandle)    %delete old mask
     end
-    mask_test_Callback(hObject, eventdata, handles)
+    CheckMask_Callback(hObject, eventdata, handles)
 end
 
@@ -1453,9 +1453,9 @@
 
 %------------------------------------------------------------------------
-% --- Executes on button press in mask_test.
-function mask_test_Callback(hObject, eventdata, handles)
+% --- Executes on button press in CheckMask.
+function CheckMask_Callback(hObject, eventdata, handles)
 %------------------------------------------------------------------------
 %case of view mask selection
-if isequal(get(handles.mask_test,'Value'),1)
+if isequal(get(handles.CheckMask,'Value'),1)
     [FF,RootPath,FileBase]=read_file_boxes(handles);
     num_i1=stra2num(get(handles.i1,'String'));
@@ -1499,6 +1499,6 @@
                 set(handles.nb_slice,'String',Name(i+1:ind_mask-1));
                 set(handles.nb_slice,'BackgroundColor',[1 1 0])
-                set(handles.mask_test,'UserData',Mask);
-                set(handles.mask_test,'BackgroundColor',[1 1 0])
+                set(handles.CheckMask,'UserData',Mask);
+                set(handles.CheckMask,'BackgroundColor',[1 1 0])
                 if nbslice > 1
                     set(handles.slices,'value',1)
@@ -1522,6 +1522,6 @@
         Mask.Base=fullfile(RootDir,RootFile);
         Mask.NbSlice=1;
-        set(handles.mask_test,'UserData',Mask);
-        set(handles.mask_test,'BackgroundColor',[1 1 0])
+        set(handles.CheckMask,'UserData',Mask);
+        set(handles.CheckMask,'BackgroundColor',[1 1 0])
     end
     if isempty(errormsg)
@@ -1529,13 +1529,13 @@
     end
     if ~isempty(errormsg)
-            set(handles.mask_test,'Value',0)
-            set(handles.mask_test,'BackgroundColor',[0.7 0.7 0.7])
+            set(handles.CheckMask,'Value',0)
+            set(handles.CheckMask,'BackgroundColor',[0.7 0.7 0.7])
      end
-else 
-    MaskData=get(handles.mask_test,'UserData');
+else % desactivate mask display
+    MaskData=get(handles.CheckMask,'UserData');
     if isfield(MaskData,'maskhandle') && ishandle(MaskData.maskhandle)
           delete(MaskData.maskhandle)    
     end
-    set(handles.mask_test,'UserData',[])    
+    set(handles.CheckMask,'UserData',[])    
     UvData=get(handles.uvmat,'UserData');
     if isfield(UvData,'MaskName')
@@ -1543,5 +1543,5 @@
         set(handles.uvmat,'UserData',UvData)
     end
-    set(handles.mask_test,'BackgroundColor',[0.7 0.7 0.7])
+    set(handles.CheckMask,'BackgroundColor',[0.7 0.7 0.7])
 end
 
@@ -1550,5 +1550,5 @@
 %------------------------------------------------------------------------
 errormsg=[];%default
-MaskData=get(handles.mask_test,'UserData');
+MaskData=get(handles.CheckMask,'UserData');
 if isfield(MaskData,'maskhandle')&& ishandle(MaskData.maskhandle)
     uistack(MaskData.maskhandle,'top');
@@ -1556,5 +1556,5 @@
 num_i1_mask=mod(num_i1-1,MaskData.NbSlice)+1;
 MaskName=name_generator(MaskData.Base,num_i1_mask,num_j1,'.png',MaskData.NomType);
-huvmat=get(handles.mask_test,'parent');
+huvmat=get(handles.CheckMask,'parent');
 UvData=get(huvmat,'UserData');
 
@@ -1623,5 +1623,5 @@
             MaskData.maskhandle=image(Mask.AX,Mask.AY,imflag,'Tag','mask','HitTest','off','AlphaData',0.6*flagmask);
 %             set(MaskData.maskhandle,'AlphaData',0.6*flagmask)
-            set(handles.mask_test,'UserData',MaskData)
+            set(handles.CheckMask,'UserData',MaskData)
         end
     end
@@ -2556,11 +2556,5 @@
 
 % second projection object (view_field display)
-% IndexObj_2=get(handles.list_object_2,'Value');%selected projection object for the second view
-% if IndexObj_2==0
-%     IndexObj_2=1;
-% end
-%if isequal(get(handles.list_object_2,'Visible'),'on') && IndexObj_2 <= numel(UvData.Object)&& ~isempty(UvData.Object{IndexObj_2})
- if length( IndexObj)>=2
-%      IndexObj(2)=IndexObj_2;
+if length( IndexObj)>=2
     view_field_handle=findobj(allchild(0),'tag','view_field');%handles of the view_field GUI
     if ~isempty(view_field_handle)
@@ -2579,5 +2573,5 @@
     [ObjectData,errormsg]=proj_field(UvData.Field,UvData.Object{iobj});% project field on the object
     if testnewseries && isfield(ObjectData,'CoordUnit')&& isfield(PlotParam{imap},'Coordinates')
-        PlotParam{imap}=rmfield(PlotParam{imap}.Coordinates,'CheckFixEqual'); %set FixEqual to depend on the field (=1 if Data.CoordUnit=1 in plot_field)
+        PlotParam{imap}.Coordinates=rmfield(PlotParam{imap}.Coordinates,'CheckFixEqual'); %set FixEqual to depend on the field (=1 if Data.CoordUnit=1 in plot_field)
     end 
     if ~isempty(errormsg)
@@ -2651,5 +2645,5 @@
 
 %% update the mask
-if isequal(get(handles.mask_test,'Value'),1)%if the mask option is on
+if isequal(get(handles.CheckMask,'Value'),1)%if the mask option is on
    update_mask(handles,num_i1,num_i2);
 end
@@ -3818,5 +3812,5 @@
      UvData.Object=UvData.Object(1);
 end 
-list_object=get(handles.ListObject,'String');
+%list_object=get(handles.ListObject,'String');
 set(handles.ListObject,'Value',1)
 set(handles.ListObject,'String',{''})
@@ -3826,5 +3820,5 @@
 
 %delete mask if it is displayed 
-if isequal(get(handles.mask_test,'Value'),1)%if the mask option is on
+if isequal(get(handles.CheckMask,'Value'),1)%if the mask option is on
    UvData=rmfield(UvData,'MaskName'); %will impose mask refresh  
 end
@@ -3832,5 +3826,5 @@
 run0_Callback(hObject, eventdata, handles)
 
-%--------------------------------------------
+%------------------------------------------------------------------------
 function histo1_menu_Callback(hObject, eventdata, handles)
 %--------------------------------------------
@@ -3842,7 +3836,7 @@
 update_histo(handles.histo_u,huvmat,FieldName)
 
-%----------------------------------------------
+%------------------------------------------------------------------------
 function histo2_menu_Callback(hObject, eventdata, handles)
-%----------------------------------------------
+%------------------------------------------------------------------------
 %plot second histo
 huvmat=get(handles.histo2_menu,'parent');
@@ -3852,8 +3846,8 @@
 update_histo(handles.histo_v,huvmat,FieldName)
 
-
-%--------------------------------------------
+%------------------------------------------------------------------------
 %read the field .Fieldname stored in UvData and plot its histogram
 function update_histo(haxes,huvmat,FieldName)
+%------------------------------------------------------------------------
 UvData=get(huvmat,'UserData');
 if ~isfield(UvData.Field,FieldName)
@@ -4108,15 +4102,15 @@
 list=get(handles.ListColorCode,'String');
 ichoice=get(handles.ListColorCode,'Value');
-colcode.ColorCode=list{ichoice};
-colcode.MinC=str2num(get(handles.num_MinVec,'String'));
-colcode.MaxC=str2num(get(handles.num_MaxVec,'String'));
-test3color=strcmp(colcode.ColorCode,'rgb') || strcmp(colcode.ColorCode,'bgr');
+colcode.ListColorCode=list{ichoice};
+colcode.MinVec=str2num(get(handles.num_MinVec,'String'));
+colcode.MaxVec=str2num(get(handles.num_MaxVec,'String'));
+test3color=strcmp(colcode.ListColorCode,'rgb') || strcmp(colcode.ListColorCode,'bgr');
 if test3color
-    colcode.colcode1=str2num(get(handles.num_ColCode1,'String'));
-    colcode.colcode2=str2num(get(handles.num_ColCode2,'String'));
-end
-colcode.FixedCbounds=0;
-colcode.FixedCbounds=1;
-vec_C=colcode.MinC+(colcode.MaxC-colcode.MinC)*(0.5:width-0.5)/width;%sample of vec_C values from min to max
+    colcode.ColCode1=str2num(get(handles.num_ColCode1,'String'));
+    colcode.ColCode2=str2num(get(handles.num_ColCode2,'String'));
+end
+% colcode.FixedCbounds=0;
+%colcode.CheckFixVecColor=1;
+vec_C=colcode.MinVec+(colcode.MaxVec-colcode.MinVec)*(0.5:width-0.5)/width;%sample of vec_C values from min to max
 [colorlist,col_vec]=set_col_vec(colcode,vec_C);
 oneheight=ones(1,height);
@@ -4138,7 +4132,7 @@
 write_plot_param(handles,PlotParamOut); %update the auto plot parameters
 
-%-------------------------------------------------------------------
-% --- Executes on button press in grid.
-function grid_Callback(hObject, eventdata, handles)
+% %-------------------------------------------------------------------
+% % --- Executes on button press in grid.
+% function grid_Callback(hObject, eventdata, handles)
 
 
Index: /trunk/src/view_field.m
===================================================================
--- /trunk/src/view_field.m	(revision 314)
+++ /trunk/src/view_field.m	(revision 315)
@@ -280,5 +280,5 @@
 % --- Executes on button press in FixEqual.
 function CheckFixEqual_Callback(hObject, eventdata, handles)
-test=get(handles.FixEqual,'Value');
+test=get(handles.CheckFixEqual,'Value');
 if test
     set(handles.CheckFixEqual,'BackgroundColor',[1 1 0])
