Home > . > sub_field.m

# sub_field

## PURPOSE

'sub_field': combines two input fields + mask for uvmat

## SYNOPSIS

function SubData=sub_field(UvData,Field);

## DESCRIPTION

    'sub_field': combines two input fields + mask for uvmat
    -----------------------------------------------------------------------
     function SubData=sub_field(UvData,Field)

     OUPUT:
     SubData: structure representing the resulting field

     INPUT:
     UvData: main structure UvData associated to the uvmat GUI as 'UserData'
     Field: cell of Matlab structures representing the input fields

        -- TODO: need to be rationalized --

## CROSS-REFERENCE INFORMATION

This function calls:

- calc_field 'calc_field': defines fields (velocity, vort, div...) from civx data and calculate them
- griddata_uvmat 'griddata_uvmat': function griddata_uvmat(vec2_X,vec2_Y,vec2_U,vec_X,vec_Y,'linear')
- proj_grid 'proj_grid': project fields with unstructured coordinantes on a regular grid

This function is called by:

- uvmat 'uvmat': function associated with the GUI 'uvmat.fig' for images and data field visualization

## SOURCE CODE

    0001 %'sub_field': combines two input fields + mask for uvmat
    0002 %-----------------------------------------------------------------------
    0003 % function SubData=sub_field(UvData,Field)
    0004 %
    0005 % OUPUT:
    0006 % SubData: structure representing the resulting field
    0007 %
    0008 % INPUT:
    0009 % UvData: main structure UvData associated to the uvmat GUI as 'UserData'
    0010 % Field: cell of Matlab structures representing the input fields
    0011 %
    0012 %    -- TODO: need to be rationalized --
    0013
    0014 function SubData=sub_field(UvData,Field);
    0015
    0016 SubData=UvData;%default
    0017 if isfield(SubData,'ListVarName')
    0018     SubData=rmfield(SubData,'ListVarName');
    0019 end
    0020 if ~exist('Field','var')
    0021     return
    0022 end
    0023 if ~iscell(Field)
    0024     return
    0025 end
    0026 nbfield=length(Field);
    0027 if nbfield==1 & isfield(Field{1},'ListVarName')
    0028     SubData=Field{1};
    0029     if isfield(UvData,'Object')
    0030         SubData.Object=UvData.Object;
    0031     end
    0032     if isfield(UvData,'XmlData')
    0033         SubData.XmlData=UvData.XmlData;
    0034     end
    0035     return
    0036 end
    0037
    0038 %transfer error messages
    0039 testerror=0;
    0040 if ~isfield(SubData,'Txt')
    0041     for ifield=1:nbfield
    0042         if isfield(Field{ifield},'Txt')
    0043             SubData.Txt=Field{ifield}.Txt;
    0044             testerror=1;
    0045         end
    0046     end
    0047 end
    0048
    0049 %fields
    0050 if nbfield >=1
    0051    if isfield(Field{1},'dt')
    0052        SubData.dt=Field{1}.dt;
    0053    elseif isfield(SubData,'dt')
    0054        SubData=rmfield(SubData,'dt');
    0055    end
    0056    if isfield(Field{1},'CoordType')
    0057        SubData.CoordType=Field{1}.CoordType;
    0058        %A FAIRE: Case of Field{2}.CoordType different
    0059    end
    0060     %background scalar
    0061     if isfield(Field{1},'AName')
    0062         SubData.AName=Field{1}.AName;
    0063         if nbfield >=2 & isfield(Field{2},'AName')& ~isfield(Field{2},'A')
    0064             SubData.Txt='ERROR: no substraction of background scalar';
    0065         end
    0066     elseif nbfield>=2 & isfield(Field{2},'AName')
    0067         SubData.AName=Field{2}.AName;%transfer the second scalar or image field
    0068     elseif isfield(SubData,'AName')
    0069         SubData=rmfield(SubData,'AName');
    0070     end
    0071     testA=0;
    0072     if isfield(Field{1},'A')& isfield(Field{1},'AX') & isfield(Field{1},'AY')
    0073         SubData.A=Field{1}.A;
    0074         SubData.AX=Field{1}.AX;
    0075         SubData.AY=Field{1}.AY;
    0076         if nbfield >=2 & isfield(Field{2},'A')& isfield(Field{2},'AX') & isfield(Field{2},'AY')
    0077             if isequal(Field{2}.AX,Field{1}.AX) & isequal(Field{2}.AY,Field{1}.AY) &...
    0078                               isequal(size(Field{2}.A),size(Field{1}.A))
    0079                 SubData.A=double(Field{1}.A)-double(Field{2}.A);
    0080                 if isa(Field{1}.A,'uint8')
    0081                     SubData.A=int8(SubData.A);
    0082                 elseif isa(Field{1}.A,'uint16')
    0083                     SubData.A=int16(SubData.A);%signed integer
    0084                 end
    0085             else
    0086                 SubData.Txt='ERROR in image substraction: unequal image sizes';
    0087             end
    0088         end
    0089         testA=1;
    0090     elseif nbfield>=2 & isfield(Field{2},'A')& isfield(Field{2},'AX') & isfield(Field{2},'AY')
    0091         SubData.A=Field{2}.A;%transfer the second scalar or image field
    0092         SubData.AX=Field{2}.AX;
    0093         SubData.AY=Field{2}.AY;
    0094         testA=1;
    0095     end
    0096     if ~isfield(SubData,'NbDim')
    0097         SubData.NbDim=2;%nbdim=2 by default
    0098     end
    0099 %          if ~testA
    0100         %coordinates
    0101         ivel=1;
    0102         if isfield(Field{1},'X')
    0103             SubData.X=Field{1}.X;
    0104         elseif nbfield>=2 & isfield(Field{2},'X')
    0105             SubData.X=Field{2}.X;
    0106             ivel=2;
    0107         elseif isfield(SubData,'X')
    0108             SubData=rmfield(SubData,'X');
    0109         end
    0110         if isfield(Field{ivel},'Y')
    0111             SubData.Y=Field{ivel}.Y;
    0112         elseif isfield(SubData,'Y')
    0113             SubData=rmfield(SubData,'Y');
    0114         end
    0115         if isfield(Field{ivel},'Z')
    0116             SubData.Z=Field{ivel}.Z;
    0117             %don't remove Z (set by uvmat)
    0118         end
    0119
    0120         %velocity components and derived scalar
    0121         if isfield(Field{ivel},'U')
    0122             SubData.U=Field{ivel}.U;
    0123         elseif isfield(SubData,'U')
    0124             SubData=rmfield(SubData,'U');
    0125         end
    0126         if isfield(Field{ivel},'V')
    0127             SubData.V=Field{ivel}.V;
    0128         elseif isfield(SubData,'V')
    0129             SubData=rmfield(SubData,'V');
    0130         end
    0131         if isfield(Field{ivel},'W')
    0132             SubData.W=Field{ivel}.W;
    0133         elseif isfield(SubData,'W')
    0134             SubData=rmfield(SubData,'W');
    0135         end
    0136 %          else
    0137 %              ivel=1;
    0138 %         end
    0139     if nbfield>=2 & isfield(Field{2},'X') & ivel==1 %two velocity fields exist
    0140         test_A=0;
    0141         if isfield(Field{1},'A') ||(isfield(Field{1},'AName') && ~isempty(Field{1}.AName)) %if the first field is for a scalar
    0142             if ~isfield(Field{1},'A')
    0143                 test_A=1;
    0144                 SubData.A=calc_field(Field{1}.AName,Field{1});
    0145                 SubData.AX=Field{1}.X;
    0146                 SubData.AY=Field{1}.Y;
    0147                 SubData.AName=Field{1}.AName;
    0148                 %remove false vectors
    0149                 if isfield(Field{1},'FF')
    0150                     ind_sel=find(Field{1}.FF==0);%good indices
    0151                     SubData.A=SubData.A(ind_sel);
    0152                     SubData.AX=SubData.AX(ind_sel);
    0153                     SubData.AY=SubData.AY(ind_sel);
    0154                 end
    0155                 [SubData.A,SubData.AX,SubData.AY]=...
    0156                 proj_grid(SubData.AX,SubData.AY,SubData.A,[],[],'np>256');
    0157             end
    0158         end
    0159         if isfield(Field{2},'A')||(isfield(Field{2},'AName') && ~isempty(Field{2}.AName) )%if the second field is for a scalar
    0160             testA=1;
    0161             if ~isfield(Field{2},'A')
    0162                 A=calc_field(Field{2}.AName,Field{2});
    0163                 AX=Field{2}.X;
    0164                 AY=Field{2}.Y;
    0165                 AName=Field{2}.AName;
    0166                 %remove false vectors
    0167                 if isfield(Field{2},'FF')
    0168                     ind_sel=find(Field{2}.FF==0);%good indices
    0169                     A=A(ind_sel);
    0170                     AX=AX(ind_sel);
    0171                     AY=AY(ind_sel);
    0172                 end
    0173                 if test_A==1;%substract the two scalars
    0174                     if SubData.NbDim==3
    0175                         SubData.Txt='ERROR: no substraction of 3D fields';
    0176                     elseif isequal(AName,SubData.AName)
    0177                         [A,AX,AY]=proj_grid(AX,AY,A,SubData.AX,SubData.AY,size(SubData.A));
    0178                         SubData.A=SubData.A-A;
    0179                     else
    0180                         SubData.Txt='ERROR: attempt to substract two scalars of different kind';
    0181                     end
    0182                 else
    0183                     [SubData.A,SubData.AX,SubData.AY]=proj_grid(AX,AY,A,[],[],'np>256');
    0184                     SubData.AName=AName;
    0185                     test_A=1;
    0186                 end
    0187              end
    0188          end
    0189          if test_A==0% substract the velocity fields
    0190             %A REVOIR cas 3 composantes 2D
    0191             if isfield(Field{2},'FF')& ~isempty(Field{2}.FF)
    0192                 index2=find(Field{2}.FF==0);
    0193                 Field{2}.X=Field{2}.X(index2);
    0194                 Field{2}.Y=Field{2}.Y(index2);
    0195                 Field{2}.U=Field{2}.U(index2);
    0196                 Field{2}.V=Field{2}.V(index2);
    0197             end
    0198             if  ~isequal(Field{1}.X,Field{2}.X) | ~isequal(Field{1}.Y,Field{2}.Y); % if the vector positions are not the same
    0199                 Field{2}.U=griddata_uvmat(Field{2}.X,Field{2}.Y,Field{2}.U,Field{1}.X,Field{1}.Y);  %interpolate vectors in the second field
    0200                 Field{2}.V=griddata_uvmat(Field{2}.X,Field{2}.Y,Field{2}.V,Field{1}.X,Field{1}.Y); %  to the positions  of the first field
    0201             end
    0202             SubData.U=Field{1}.U-Field{2}.U;% take the interpolated first field - the second field
    0203             SubData.V=Field{1}.V-Field{2}.V;
    0204         end
    0205     end
    0206     %remove scalar data if no AName
    0207     if ~testA%~isfield(SubData,'AName')
    0208         if isfield(SubData,'A')
    0209             SubData=rmfield(SubData,'A');
    0210         end
    0211         if isfield(SubData,'AX')
    0212             SubData=rmfield(SubData,'AX');
    0213         end
    0214         if isfield(SubData,'AY')
    0215             SubData=rmfield(SubData,'AY');
    0216         end
    0217     end
    0218     if isfield(Field{ivel},'C')
    0219         SubData.C=Field{ivel}.C;
    0220     elseif isfield(SubData,'C')
    0221         SubData=rmfield(SubData,'C');
    0222     end
    0223     if isfield(Field{ivel},'F')
    0224         SubData.F=Field{ivel}.F;
    0225     elseif isfield(SubData,'F')
    0226         SubData=rmfield(SubData,'F');
    0227     end
    0228     if isfield(Field{ivel},'FF')
    0229         SubData.FF=Field{ivel}.FF;
    0230     elseif isfield(SubData,'FF')
    0231         SubData=rmfield(SubData,'FF');
    0232     end
    0233     %velocity derivatives
    0234     if isfield(Field{ivel},'DjUi')
    0235         SubData.DjUi=Field{ivel}.DjUi;
    0236     elseif isfield(SubData,'DjUi')
    0237         SubData=rmfield(SubData,'DjUi');
    0238     end
    0239 end