[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


[674]  28 


[405]  29  %% global attributes


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


 31  %transfer global attributes of Field


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


 33  SubData.ListGlobalAttribute=Field.ListGlobalAttribute;


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


 35  AttrName=Field.ListGlobalAttribute{ilist};


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


[8]  37  end


 38  end


[515]  39  %transfer global attributes of Field_1


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


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


 42  AttrName=Field_1.ListGlobalAttribute{ilist};


[515]  43  AttrNameNew=AttrName;


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


 45  AttrNameNew=[AttrNameNew '_1'];


[8]  46  end


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


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


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


[8]  50  end


 51  end


 52  end


[405]  53 


 54  %% variables


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


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


 57  SubData.VarDimName=Field.VarDimName;


 58  if isfield(Field,'VarAttribute')


 59  SubData.VarAttribute=Field.VarAttribute;


 60  end


[515]  61  ListDimName={};


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


 63  VarName=Field.ListVarName{ilist};


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


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


 66  DimCell=Field.VarDimName{ilist};


 67  if ischar(DimCell)


 68  DimCell={DimCell};


 69  end


 70  for idim=1:numel(DimCell)


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


 72  ListDimName=[ListDimName DimCell(idim)];


 73  end


 74  end


[8]  75  end


 76 


[515]  77  %% field request


[581]  78  ProjModeRequest=cell(size(Field.ListVarName));


[515]  79  for ilist=1:numel(Field.VarAttribute)


[581]  80  if isfield(Field.VarAttribute{ilist},'ProjModeRequest')


 81  ProjModeRequest{ilist}=Field.VarAttribute{ilist}.ProjModeRequest;


[402]  82  end


 83  end


[581]  84  ProjModeRequest_1=cell(size(Field_1.ListVarName));


[515]  85  for ilist=1:numel(Field_1.VarAttribute)


[581]  86  if isfield(Field_1.VarAttribute{ilist},'ProjModeRequest')


 87  ProjModeRequest_1{ilist}=Field_1.VarAttribute{ilist}.ProjModeRequest;


[402]  88  end


 89  end


[8]  90 


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


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


 93  DimCell=Field_1.VarDimName{ilist};


 94  if ischar(DimCell)


 95  DimCell={DimCell};


 96  end


 97  for idim=1:numel(DimCell)


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


 99  if ~isempty(ind_dim)


 100  if ischar(Field_1.VarDimName{ilist})


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


 102  end


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


 104  end


 105  end


[8]  106  end


 107 


[580]  108  %% look for coordinates common to Field in Field_1


[515]  109  ind_remove=zeros(size(Field_1.ListVarName));


[674]  110  % loop on the variables of the second field Field_1


[515]  111  for ilist=1:numel(Field_1.ListVarName)


[674]  112  % case of variable with a single dimension


 113  OldDimName=Field_1.VarDimName{ilist};


 114  if ischar(OldDimName), OldDimName={OldDimName}; end% transform char string to cell if relevant


 115  if numel(OldDimName)==1


 116  OldDim=Field_1.(Field_1.ListVarName{ilist});% get variable


 117  %look for the existence of the variable OldDim in the first field Field


[515]  118  for i1=1:numel(Field.ListVarName)


[674]  119  if isequal(Field.(Field.ListVarName{i1}),OldDim) &&...


 120  ((isempty(ProjModeRequest{i1}) && isempty(ProjModeRequest_1{ilist}))  strcmp(ProjModeRequest{i1},ProjModeRequest_1{ilist}))


[515]  121  ind_remove(ilist)=1;


[674]  122  NewDimName=Field.VarDimName{i1};


 123  if ischar(NewDimName), NewDimName={NewDimName}; end %transform char chain to cell if needed


 124  Field_1.VarDimName=regexprep_r(Field_1.VarDimName,['^' OldDimName{1} '$'],NewDimName{1});% change the var name of Field_1 to the corresponding var name of Field


[180]  125  end


 126  end


[8]  127  end


 128  end


[674]  129  Field_1.ListVarName(find(ind_remove))=[];%removes the redondent coordinate


[515]  130  Field_1.VarDimName(find(ind_remove))=[];


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


[8]  132 


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


[515]  134  for ilist=1:numel(Field_1.ListVarName)


 135  VarName=Field_1.ListVarName{ilist};


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


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


 138  VarNameNew=VarName;


 139  else % variable name exists in Field


 140  VarNameNew=[VarName '_1'];


[580]  141  if isfield(Field_1.VarAttribute{ilist},'FieldName')


 142  Field_1.VarAttribute{ilist}.FieldName=regexprep_r(Field_1.VarAttribute{ilist}.FieldName,VarName,VarNameNew);


 143  end


[8]  144  end


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


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


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


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


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


 150  end


 151 


 152  %% substrat fields when possible


[580]  153  [CellInfo,NbDim,errormsg]=find_field_cells(SubData);


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


 155  ivar=[];


 156  ivar_1=[];


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


 158  if ~isempty(CellInfo{icell})


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


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


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


[8]  162  end


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


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


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


[515]  166  end


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


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


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


[515]  170  end


[8]  171  end


 172  end


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


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


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


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


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


 178  end


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


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


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


[674]  182  'end'


[520]  183 


[580]  184  function OutputCell=regexprep_r(InputCell,search_string,new_string)


[713]  185  if ischar(InputCell); InputCell={InputCell}; end


[674]  186  OutputCell=InputCell;%default


[520]  187  for icell=1:numel(InputCell)


[580]  188  OutputCell{icell}=regexprep(InputCell{icell},search_string,new_string);


[520]  189  end


[515]  190 


 191 

