[8]  1  %'sub_field': combines two input fields


 2  %


 3  % function SubData=sub_field(Field,Field_1)


 4  %


 5  % OUPUT:


 6  % SubData: structure representing the resulting field


 7  %


 8  % INPUT:


 9  % UvData: main structure UvData associated to the uvmat GUI as 'UserData'


 10  % Field: cell of Matlab structures representing the input fields


 11  %


 12  %  TODO: need to be rationalized 


 13 


 14  function [SubData,errormsg]=sub_field(Field,Field_1)


 15  test_attr=0;


 16  if isfield(Field,'ListGlobalAttribute')


 17  SubData.ListGlobalAttribute=Field.ListGlobalAttribute;


 18  for ilist=1:numel(Field.ListGlobalAttribute)


 19  AttrName=Field.ListGlobalAttribute{ilist};


 20  eval(['SubData.' AttrName '=Field.' AttrName ';'])


 21  end


 22  test_attr=1;


 23  end


 24  if isfield(Field_1,'ListGlobalAttribute')


 25  for ilist=1:numel(Field_1.ListGlobalAttribute)


 26  test_1=1;


 27  AttrName=Field_1.ListGlobalAttribute{ilist};


 28  if test_attr


 29  for i_prev=1:numel(Field.ListGlobalAttribute)


 30  if isequal(Field.ListGlobalAttribute{i_prev},AttrName)


 31  test_1=0; %attribute already written


 32  eval(['Val=Field.' AttrName ';'])


 33  eval(['Val_1=Field_1.' AttrName ';'])


 34  if isequal(Val,Val_1)


 35  break% data already written


 36  else


 37  eval(['SubData.' AttrName '_1=Field_1.' AttrName ';'])


 38  end


 39  end


 40  end


 41  end


 42  if test_1


 43  eval(['SubData.' AttrName '=Field_1.' AttrName ';'])


 44  end


 45  end


 46  end


 47  SubData.ListVarName=Field.ListVarName;


 48  SubData.VarDimName=Field.VarDimName;


 49  if isfield(Field,'VarAttribute')


 50  SubData.VarAttribute=Field.VarAttribute;


 51  end


 52  %reproduce Field by default


 53  for ivar=1:numel(Field.ListVarName)


 54  VarName=Field.ListVarName{ivar};


 55  eval(['SubData.' VarName '=Field.' VarName ';'])


 56  end


 57 


 58  %fields


 59  [CellVarIndex,NbDim,VarTypeCell,errormsg]=find_field_indices(Field);


 60  if ~isempty(errormsg)


 61  errormsg=['invalid first input to sub_field:' errormsg];


 62  return


 63  end


 64  [CellVarIndex_1,NbDim_1,VarTypeCell_1,errormsg]=find_field_indices(Field_1);


 65  if ~isempty(errormsg)


 66  errormsg=['invalid second input to sub_field:' errormsg];


 67  return


 68  end


 69  iselect=find(NbDim==2);


 70  if ~isequal(numel(iselect),1)


 71  errormsg='invalid first input to sub_field: it must contain a single 2D field cell';


 72  return


 73  end


 74  iselect_1=find(NbDim_1==2);


 75  if ~isequal(numel(iselect_1),1)


 76  errormsg='invalid second input to sub_field: it must contain a single 2D field cell';


 77  return


 78  end


 79  % VarIndex=CellVarIndex{iselect};


 80  % VarIndex_1=CellVarIndex_1{iselect_1};


 81  VarType=VarTypeCell{iselect};


 82  VarType_1=VarTypeCell_1{iselect_1};


 83  testX=~isempty(VarType.coord_x)&& ~isempty(VarType.coord_y);%unstructured coordiantes


 84  testX_1=~isempty(VarType_1.coord_x)&& ~isempty(VarType_1.coord_y);%unstructured coordiantes


 85  testU=~isempty(VarType.vector_x)&& ~isempty(VarType.vector_y);%vector field


 86  testU_1=~isempty(VarType_1.vector_x)&& ~isempty(VarType_1.vector_y);%vector field


 87  % testfalse=~isempty(VarType.errorflag);


 88  testfalse_1=~isempty(VarType_1.errorflag);


 89  ivar_C=[VarType.scalar VarType.image VarType.color VarType.ancillary]; %defines index (indices) for the scalar or ancillary fields


 90  if numel(ivar_C)>1


 91  errormsg='too many scalar fields in the first input of sub_field.m';


 92  return


 93  end


 94  ivar_C_1=[VarType_1.scalar VarType_1.image VarType_1.color VarType_1.ancillary]; %defines index (indices) for the scalar or ancillary fields


 95  if numel(ivar_C_1)>1


 96  errormsg='too many scalar fields in the second input of sub_field.m';


 97  return


 98  end


 99 


 100  %substract two vector fields or two scalars


 101  if (testU && testU_1)  (~testU && ~testU_1)


 102  %check coincidence in positions


 103  %unstructured coordinates


[109]  104  if testX


 105  if testU % vector fields


 106  U_1_Name=Field_1.ListVarName{VarType_1.vector_x};


 107  V_1_Name=Field_1.ListVarName{VarType_1.vector_y};


 108  eval(['vec_U_1=Field_1.' U_1_Name ';'])


 109  eval(['vec_V_1=Field_1.' V_1_Name ';'])


 110  nbpoints_x_1=size(vec_U_1,2);


 111  nbpoints_y_1=size(vec_U_1,1);


 112  vec_U_1=reshape(vec_U_1,nbpoints_x_1*nbpoints_y_1,1);


 113  vec_V_1=reshape(vec_V_1,nbpoints_x_1*nbpoints_y_1,1);


 114  if testfalse_1


 115  vec_U_1=vec_U_1(indsel);


 116  vec_V_1=vec_V_1(indsel);


 117  end


 118  else %(~testU && ~testU_1)


 119  A_1_Name=Field_1.ListVarName{ivar_C_1};


 120  eval(['vec_A_1=Field_1.' A_1_Name ';'])


 121  nbpoints_x_1=size(vec_A_1,2);


 122  nbpoints_y_1=size(vec_A_1,1);%TODO: use a faster interpolation method for a regular grid (size(x)=2)


 123  vec_A_1=reshape(vec_A_1,nbpoints_x_1*nbpoints_y_1,1);


 124  if testfalse_1


 125  vec_A_1=vec_A_1(indsel);


 126  end


 127  end


[8]  128  XName=Field.ListVarName{VarType.coord_x};


 129  YName=Field.ListVarName{VarType.coord_y};


 130  eval(['vec_X=Field.' XName ';'])


 131  eval(['vec_Y=Field.' YName ';'])


 132  nbpoints=numel(vec_X);


[109]  133  vec_X=reshape(vec_X,nbpoints,1);


 134  vec_Y=reshape(vec_Y,nbpoints,1);


[8]  135  if testX_1 %unstructured coordinates for the second field


 136  X_1_Name=Field_1.ListVarName{VarType_1.coord_x};


 137  Y_1_Name=Field_1.ListVarName{VarType_1.coord_y};


 138  eval(['vec_X_1=Field_1.' X_1_Name ';'])


 139  eval(['vec_Y_1=Field_1.' Y_1_Name ';'])


[109]  140 


[8]  141  else %structured coordinates for the second field


 142  y_1_Name=Field_1.ListVarName{VarType_1.coord(1)};


 143  x_1_Name=Field_1.ListVarName{VarType_1.coord(2)};


 144  eval(['y_1=Field_1.' y_1_Name ';'])


[109]  145  eval(['x_1=Field_1.' x_1_Name ';'])


 146  if isequal(numel(x_1),2)


 147  x_1=linspace(x_1(1),x_1(2),nbpoints_x_1);


 148  end


 149  if isequal(numel(y_1),2)


 150  y_1=linspace(y_1(1),y_1(2),nbpoints_y_1);


 151  end


[8]  152  [vec_X_1,vec_Y_1]=meshgrid(x_1,y_1);


 153  end


[109]  154  vec_X_1=reshape(vec_X_1,nbpoints_x_1*nbpoints_y_1,1);


 155  vec_Y_1=reshape(vec_Y_1,nbpoints_x_1*nbpoints_y_1,1);


[8]  156  if testfalse_1


 157  FFName_1=Field_1.ListVarName{VarType_1.errorflag};


 158  eval(['vec_FF_1=Field_1.' FFName_1 ';'])


[109]  159  vec_FF_1=reshape(vec_FF_1,nbpoints_1,1);


[8]  160  indsel=find(~vec_FF_1);


 161  vec_X_1=vec_X_1(indsel);


 162  vec_Y_1=vec_Y_1(indsel);


 163  end


 164  if ~isequal(vec_X_1,vec_X) && ~isequal(vec_Y_1,vec_Y) % if the unstructured positions are not the same


 165  if testU


 166  vec_U_1=griddata_uvmat(vec_X_1,vec_Y_1,vec_U_1,vec_X,vec_Y); %interpolate vectors in the second field


 167  vec_V_1=griddata_uvmat(vec_X_1,vec_Y_1,vec_V_1,vec_X,vec_Y); %interpolate vectors in the second field


 168  else


[109]  169  vec_A_1=griddata_uvmat(vec_X_1,vec_Y_1,double(vec_A_1),vec_X,vec_Y); %interpolate vectors in the second field


[8]  170  end


 171  end


 172  if testU


 173  UName=Field.ListVarName{VarType.vector_x};


 174  VName=Field.ListVarName{VarType.vector_y};


 175  eval(['vec_U=Field.' UName ';'])


 176  eval(['vec_V=Field.' VName ';'])


[109]  177  vec_U=reshape(vec_U,numel(vec_U),1);


 178  vec_V=reshape(vec_V,numel(vec_V),1);


[8]  179  eval(['SubData.' UName '=vec_Uvec_U_1;'])


 180  eval(['SubData.' VName '=vec_Vvec_V_1;'])


 181  else


 182  AName=Field.ListVarName{ivar_C};


[109]  183  size(Field.vort)


[8]  184  eval(['SubData.' AName '=Field.' AName 'vec_A_1;'])


 185  end


 186  else %structured coordiantes


 187  XName=Field.ListVarName{VarType.coord(2)};


 188  YName=Field.ListVarName{VarType.coord(1)};


 189  eval(['x=Field.' XName ';'])


 190  eval(['y=Field.' YName ';'])


 191  if testX_1 %unstructured coordinates for the second field


 192  errormsg='the second input scalar is not on a regular grid: comparison option not implemented';


 193  return


 194  else


 195  XName_1=Field.ListVarName{VarType_1.coord(2)};


 196  YName_1=Field.ListVarName{VarType_1.coord(1)};


 197  eval(['x_1=Field_1.' XName_1 ';'])


 198  eval(['y_1=Field_1.' YName_1 ';'])


 199  end


 200  if testU % vector fields


 201  UName=Field.ListVarName{VarType.vector_x};


 202  VName=Field.ListVarName{VarType.vector_y};


 203  U_1_Name=Field_1.ListVarName{VarType_1.vector_x};


 204  V_1_Name=Field_1.ListVarName{VarType_1.vector_y};


 205  eval(['U_1=Field_1.' U_1_Name ';'])


 206  eval(['V_1=Field_1.' V_1_Name ';'])


 207  if ~isequal(x_1,x)~isequal(y_1,y)


 208  [X_1,Y_1]=meshgrid(x_1,y_1);


 209  U_1 =interp2(X_1,Y_1,U_1,x,y');


 210  V_1 =interp2(X_1,Y_1,V_1,x,y');


 211  end


[89]  212  eval(['SubData.' UName '=Field.' UName 'U_1;'])


 213  eval(['SubData.' VName '=Field.' VName 'V_1;'])


[8]  214  else


 215  AName=Field.ListVarName{ivar_C};


 216  A_1_Name=Field_1.ListVarName{ivar_C_1};


 217  eval(['A_1=double(Field_1.' A_1_Name ');'])


 218  if ~isequal(x_1,x)~isequal(y_1,y)


 219  [X_1,Y_1]=meshgrid(x_1,y_1);


 220  A_1 =interp2(X_1,Y_1,A_1,x,y');


 221  end


 222  eval(['SubData.' AName '=double(Field.' AName ')A_1;'])


 223  end


 224  end


 225  end


 226 


 227  % merge a vector field and a scalar as second input


 228  if testU && ~testU_1


 229  AName_1=Field_1.ListVarName{ivar_C_1};


 230  if isfield(Field_1,'VarAttribute') && numel(Field_1.VarAttribute)>=ivar_C_1


 231  AAttr=Field_1.VarAttribute{ivar_C_1} ;


 232  else


 233  AAttr=[];


 234  end


 235  if testX_1 %unstructured coordinate


 236  XName_1=Field_1.ListVarName{VarType_1.coord_x};


 237  YName_1=Field_1.ListVarName{VarType_1.coord_y};


[86]  238  %SubData.ListVarName=[SubData.ListVarName {XName_1} {YName_1}];


[8]  239  DimCell=Field_1.VarDimName([VarType_1.coord_x VarType_1.coord_y ]);


 240  if isfield(Field_1,'VarAttribute')


 241  if numel(Field_1.VarAttribute)>=VarType_1.coord_x


 242  XAttr=Field_1.VarAttribute{VarType_1.coord_x} ;


 243  else


 244  XAttr=[];


 245  end


 246  if numel(Field_1.VarAttribute)>=VarType_1.coord_y


 247  YAttr=Field_1.VarAttribute{VarType_1.coord_y} ;


 248  else


 249  YAttr=[];


 250  end


 251  SubData.VarAttribute=[SubData.VarAttribute {XAttr} {YAttr}];


 252  end


 253  else


 254  XName_1=Field_1.ListVarName{VarType_1.coord(2)};


 255  YName_1=Field_1.ListVarName{VarType_1.coord(1)};


 256  % DimCell=[{YName_1} {XName_1}];


 257  if isfield(Field_1,'VarAttribute')


 258  if numel(Field_1.VarAttribute)>=VarType_1.coord(2)


 259  XAttr=Field_1.VarAttribute{VarType_1.coord(2)} ;


 260  else


 261  XAttr=[];


 262  end


 263  if numel(Field_1.VarAttribute)>=VarType_1.coord(1)


 264  YAttr=Field_1.VarAttribute{VarType_1.coord(1)} ;


 265  else


 266  YAttr=[];


 267  end


 268  SubData.VarAttribute=[SubData.VarAttribute {YAttr} {XAttr}];


 269  end


 270  end


 271  %look for previously used variable names


 272  XName_1_1=XName_1;%default


 273  YName_1_1=YName_1;%default


 274  AName_1_1=AName_1;%default


 275  for iprev=1:numel(SubData.ListVarName)


 276  switch SubData.ListVarName{iprev}


 277  case XName_1


 278  XName_1_1=[XName_1 '_1'];


 279  case YName_1


 280  YName_1_1=[YName_1 '_1'];


 281  case AName_1


 282  AName_1_1=[AName_1 '_1'];


 283  end


 284  end


 285  if ~testX_1


 286  DimCell=[{XName_1_1} {YName_1_1}];


 287  end


 288  SubData.ListVarName=[SubData.ListVarName {XName_1_1} {YName_1_1} {AName_1_1}];


 289  DimCell=[DimCell Field_1.VarDimName(ivar_C_1)]; %(TODO: check for dimension names)


 290  SubData.VarDimName=[SubData.VarDimName DimCell];


 291  if isfield(Field_1,'VarAttribute')


 292  SubData.VarAttribute=[SubData.VarAttribute {AAttr}];


 293  end


 294  eval(['SubData.' XName_1_1 '=Field_1.' XName_1 ';'])


 295  eval(['SubData.' YName_1_1 '=Field_1.' YName_1 ';'])


 296  eval(['SubData.' AName_1_1 '=Field_1.' AName_1 ';'])


 297  end


 298 


 299  %merge a scalar as the first input and a vector field as second input


 300  if ~testU && testU_1


 301  UName_1=Field_1.ListVarName{VarType_1.vector_x};


 302  VName_1=Field_1.ListVarName{VarType_1.vector_y};


 303  UAttr=Field_1.VarAttribute{VarType_1.vector_x};


 304  VAttr=Field_1.VarAttribute{VarType_1.vector_y};


 305  if testX_1 %unstructured coordinate for the second field


 306  XName_1=Field_1.ListVarName{VarType_1.coord_x};


 307  YName_1=Field_1.ListVarName{VarType_1.coord_y};


 308 


 309  XAttr=Field_1.VarAttribute{VarType_1.coord_x};


 310  YAttr=Field_1.VarAttribute{VarType_1.coord_y};


 311  % SubData.ListVarName=[SubData.ListVarName {XName_1} {YName_1}];


 312  DimCell=Field_1.VarDimName([VarType_1.coord_x VarType_1.coord_y ]);


 313  else


 314  XName_1=Field_1.ListVarName{VarType_1.coord(2)};


 315  YName_1=Field_1.ListVarName{VarType_1.coord(1)};


 316  if numel(Field_1.VarAttribute)>=VarType_1.coord(2)


 317  XAttr=Field_1.VarAttribute{VarType_1.coord(2)};


 318  else


 319  XAttr=[];


 320  end


 321  if numel(Field_1.VarAttribute)>=VarType_1.coord(1)


 322  YAttr=Field_1.VarAttribute{VarType_1.coord(1)};


 323  else


 324  YAttr=[];


 325  end


 326  end


 327  %check for the existence of the same variable name


 328  XName_1_1=XName_1; %default


 329  YName_1_1=YName_1; %default


 330  UName_1_1=UName_1; %default


 331  VName_1_1=VName_1; %default


 332  for iprev=1:numel(SubData.ListVarName)


 333  switch SubData.ListVarName{iprev}


 334  case XName_1


 335  XName_1_1=[XName_1 '_1'];


 336  case YName_1


 337  YName_1_1=[YName_1 '_1'];


 338  case UName_1


 339  UName_1_1=[UName_1 '_1'];


 340  case VName_1


 341  VName_1_1=[VName_1 '_1'];


 342  end


 343  end


 344  if ~testX_1


 345  DimCell=[{XName_1_1} {YName_1_1}];


 346  end


 347  SubData.ListVarName=[SubData.ListVarName {XName_1_1} {YName_1_1} {UName_1_1} {VName_1_1}];


 348  DimCell=[DimCell Field_1.VarDimName([VarType_1.vector_x VarType_1.vector_y ])];


 349  SubData.VarDimName=[SubData.VarDimName DimCell];


 350  if ~(isfield(SubData,'VarAttribute') && numel(SubData.VarAttribute)==numel(SubData.ListVarName))


 351  for ivar=numel(SubData.VarAttribute)+1:numel(SubData.ListVarName)4


 352  SubData.VarAttribute{ivar}=[];


 353  end


 354  end


 355  SubData.VarAttribute=[SubData.VarAttribute {XAttr} {YAttr} {UAttr} {VAttr}];


 356  eval(['SubData.' XName_1_1 '=Field_1.' XName_1 ';'])


 357  eval(['SubData.' YName_1_1 '=Field_1.' YName_1 ';'])


 358  eval(['SubData.' UName_1_1 '=Field_1.' UName_1 ';'])


 359  eval(['SubData.' VName_1_1 '=Field_1.' VName_1 ';'])


 360  end


 361 


 362 

