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


 2  %


 3  % the two fields are subtstracted when of the same nature (scalar or


 4  % vector), if the coordinates do not coincide, the second field is


 5  % interpolated on the cooridintes of the first one


 6  %


 7  % when scalar and vectors are combined, the fields are just merged in a single matlab structure for common visualisation


[8]  8  %


[515]  9  % function SubData=sub_field(Field,XmlData,Field_1)


[8]  10  %


 11  % OUPUT:


 12  % SubData: structure representing the resulting field


 13  %


 14  % INPUT:


[179]  15  % Field: matlab structure representing the first field


 16  % Field_1:matlab structure representing the second field


[8]  17 


[515]  18  function SubData=sub_field(Field,XmlData,Field_1)


[405]  19 


[515]  20  SubData=[];


 21  if strcmp(Field,'*')


 22  return


 23  end


 24  if nargin<3


 25  SubData=Field;


 26  return


 27  end


[405]  28  %% global attributes


[515]  29  SubData.ListGlobalAttribute={};%default


 30  %transfer global attributes of Field


[8]  31  if isfield(Field,'ListGlobalAttribute')


 32  SubData.ListGlobalAttribute=Field.ListGlobalAttribute;


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


 34  AttrName=Field.ListGlobalAttribute{ilist};


[405]  35  SubData.(AttrName)=Field.(AttrName);


[8]  36  end


 37  end


[515]  38  %transfer global attributes of Field_1


[8]  39  if isfield(Field_1,'ListGlobalAttribute')


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


 41  AttrName=Field_1.ListGlobalAttribute{ilist};


[515]  42  AttrNameNew=AttrName;


 43  while ~isempty(find(strcmp(AttrNameNew,SubData.ListGlobalAttribute)))&&~isequal(Field_1.(AttrNameNew),Field.(AttrNameNew))


 44  AttrNameNew=[AttrNameNew '_1'];


[8]  45  end


[515]  46  if ~isfield(Field,AttrName)  ~isequal(Field_1.(AttrName),Field.(AttrName))


[548]  47  SubData.ListGlobalAttribute=[SubData.ListGlobalAttribute {AttrNameNew}];


 48  SubData.(AttrNameNew)=Field_1.(AttrName);


[8]  49  end


 50  end


 51  end


[405]  52 


 53  %% variables


[515]  54  %reproduce variables of the first field and list its dimensions


[8]  55  SubData.ListVarName=Field.ListVarName;


 56  SubData.VarDimName=Field.VarDimName;


 57  if isfield(Field,'VarAttribute')


 58  SubData.VarAttribute=Field.VarAttribute;


 59  end


[515]  60  ListDimName={};


 61  for ilist=1:numel(Field.ListVarName)


 62  VarName=Field.ListVarName{ilist};


 63  SubData.(VarName)=Field.(VarName);


 64  SubData.VarAttribute{ilist}.CheckSub=0;


 65  DimCell=Field.VarDimName{ilist};


 66  if ischar(DimCell)


 67  DimCell={DimCell};


 68  end


 69  for idim=1:numel(DimCell)


 70  if isempty(find(strcmp(DimCell{idim},ListDimName)))


 71  ListDimName=[ListDimName DimCell(idim)];


 72  end


 73  end


[8]  74  end


 75 


[515]  76  %% field request


 77  FieldRequest=cell(size(Field.ListVarName));


 78  for ilist=1:numel(Field.VarAttribute)


 79  if isfield(Field.VarAttribute{ilist},'FieldRequest')


 80  FieldRequest{ilist}=Field.VarAttribute{ilist}.FieldRequest;


[402]  81  end


 82  end


[515]  83  FieldRequest_1=cell(size(Field_1.ListVarName));


 84  for ilist=1:numel(Field_1.VarAttribute)


 85  if isfield(Field_1.VarAttribute{ilist},'FieldRequest')


 86  FieldRequest_1{ilist}=Field_1.VarAttribute{ilist}.FieldRequest;


[402]  87  end


 88  end


[8]  89 


[515]  90  %% rename the dimensions of the second field if identical to those of the first


 91  for ilist=1:numel(Field_1.VarDimName)


 92  DimCell=Field_1.VarDimName{ilist};


 93  if ischar(DimCell)


 94  DimCell={DimCell};


 95  end


 96  for idim=1:numel(DimCell)


 97  ind_dim=find(strcmp(DimCell{idim},ListDimName));


 98  if ~isempty(ind_dim)


 99  if ischar(Field_1.VarDimName{ilist})


 100  Field_1.VarDimName{ilist}=Field_1.VarDimName(ilist);


 101  end


 102  Field_1.VarDimName{ilist}{idim}=[ListDimName{ind_dim} '_1'];


 103  end


 104  end


[8]  105  end


 106 


[515]  107  %look for coordinates common to Field in Field_1


 108  ind_remove=zeros(size(Field_1.ListVarName));


 109  for ilist=1:numel(Field_1.ListVarName)


 110  if ischar(Field_1.VarDimName{ilist})numel(Field_1.VarDimName{ilist})==1


 111  OldDim=Field_1.VarDimName{ilist};


 112  if ischar(OldDim)


 113  OldDim=Field_1.VarDimName(ilist);


[8]  114  end


[515]  115  VarVal=Field_1.(Field_1.ListVarName{ilist});


 116  for i1=1:numel(Field.ListVarName)


 117  if (isempty(FieldRequest{i1})&&isempty(FieldRequest_1{ilist})strcmp(FieldRequest{i1},FieldRequest_1{ilist})) && isequal(Field.(Field.ListVarName{i1}),VarVal)


 118  ind_remove(ilist)=1;


 119  NewDim=Field.VarDimName{i1};


 120  if ischar(NewDim)


 121  NewDim={NewDim};


 122  end


 123  Field_1.VarDimName=regexprep_r(Field_1.VarDimName,OldDim{1},NewDim{1});


[180]  124  end


 125  end


[8]  126  end


 127  end


[515]  128  Field_1.ListVarName(find(ind_remove))=[];%removes these redondent coordinates


 129  Field_1.VarDimName(find(ind_remove))=[];


 130  Field_1.VarAttribute(find(ind_remove))=[];


[8]  131 


[515]  132  %append the other variables of the second field, modifying their name if needed


 133  for ilist=1:numel(Field_1.ListVarName)


 134  VarName=Field_1.ListVarName{ilist};


 135  ind_prev=find(strcmp(VarName,Field.ListVarName));


 136  if isempty(ind_prev)% variable name does not exist in Field


 137  VarNameNew=VarName;


 138  else % variable name exists in Field


 139  VarNameNew=[VarName '_1'];


[8]  140  end


[515]  141  SubData.ListVarName=[SubData.ListVarName {VarNameNew}];


 142  SubData.VarDimName=[SubData.VarDimName Field_1.VarDimName(ilist)];


 143  SubData.(VarNameNew)=Field_1.(VarName);


 144  SubData.VarAttribute=[SubData.VarAttribute Field_1.VarAttribute(ilist)];


 145  SubData.VarAttribute{end}.CheckSub=1;% mark that the field needs to be substracted


 146  end


 147 


 148  %append the other variables of the second field, modifying their name if needed


 149 


 150  %% substrat fields when possible


[548]  151  %[CellVarIndex,NbDim,CellVarType,errormsg]=find_field_cells(SubData);


 152  [CellInfo,NbDim,errormsg]=find_field_cells(SubData)


[515]  153  ind_remove=zeros(size(SubData.ListVarName));


 154  ivar=[];


 155  ivar_1=[];


[548]  156  for icell=1:numel(CellInfo)


 157  if ~isempty(CellInfo{icell})


 158  if isfield(CellInfo{icell},'VarIndex_scalar') && numel(CellInfo{icell}.VarIndex_scalar)==2 && SubData.VarAttribute{CellInfo{icell}.VarIndex_scalar(2)}.CheckSub;


 159  ivar=[ivar CellInfo{icell}.VarIndex_scalar(1)];


 160  ivar_1=[ivar_1 CellInfo{icell}.VarIndex_scalar(2)];


[8]  161  end


[548]  162  if isfield(CellInfo{icell},'VarIndex_vector_x') && numel(CellInfo{icell}.VarIndex_vector_x)==2 && SubData.VarAttribute{CellInfo{icell}.VarIndex_vector_x(2)}.CheckSub;


 163  ivar=[ivar CellInfo{icell}.VarIndex_vector_x(1)];


 164  ivar_1=[ivar_1 CellInfo{icell}.VarIndex_vector_x(2)];


[515]  165  end


[548]  166  if isfield(CellInfo{icell},'VarIndex_vector_y') && numel(CellInfo{icell}.VarIndex_vector_y)==2 && SubData.VarAttribute{CellInfo{icell}.VarIndex_vector_y(2)}.CheckSub;


 167  ivar=[ivar CellInfo{icell}.VarIndex_vector_y(1)];


 168  ivar_1=[ivar_1 CellInfo{icell}.VarIndex_vector_y(2)];


[515]  169  end


[8]  170  end


 171  end


[515]  172  for imod=1:numel(ivar)


 173  VarName=SubData.ListVarName{ivar(imod)};


 174  VarName_1=SubData.ListVarName{ivar_1(imod)};


[548]  175  SubData.(VarName)=double(SubData.(VarName))double(SubData.(VarName_1));


[515]  176  ind_remove(ivar_1(imod))=1;


 177  end


 178  SubData.ListVarName(find(ind_remove))=[];


 179  SubData.VarDimName(find(ind_remove))=[];


 180  SubData.VarAttribute(find(ind_remove))=[];


[520]  181 


 182  function OutputCell=regexprep_r(InputCell,dimname,dimname_new)


 183  for icell=1:numel(InputCell)


 184  OutputCell{icell}=regexprep(InputCell{icell},['^' dimname '$'],dimname_new);


 185  end


[515]  186 


 187 

