source: trunk/src/sub_field.m @ 589

Last change on this file since 589 was 581, checked in by sommeria, 11 years ago

clean the transform field functions

File size: 7.4 KB
RevLine 
[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]18function SubData=sub_field(Field,XmlData,Field_1)
[405]19
[515]20SubData=[];
21if strcmp(Field,'*')
22    return
23end
24if nargin<3
25    SubData=Field;
26    return
27end
[405]28%% global attributes
[515]29SubData.ListGlobalAttribute={};%default
30%transfer global attributes of Field
[8]31if 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
37end
[515]38%transfer global attributes of Field_1
[8]39if 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
51end
[405]52
53%% variables
[515]54%reproduce variables of the first field and list its dimensions
[8]55SubData.ListVarName=Field.ListVarName;
56SubData.VarDimName=Field.VarDimName;
57if isfield(Field,'VarAttribute')
58    SubData.VarAttribute=Field.VarAttribute;
59end
[515]60ListDimName={};
61for 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]74end
75
[515]76%% field request
[581]77ProjModeRequest=cell(size(Field.ListVarName));
[515]78for ilist=1:numel(Field.VarAttribute)
[581]79    if isfield(Field.VarAttribute{ilist},'ProjModeRequest')
80        ProjModeRequest{ilist}=Field.VarAttribute{ilist}.ProjModeRequest;
[402]81    end
82end
[581]83ProjModeRequest_1=cell(size(Field_1.ListVarName));
[515]84for ilist=1:numel(Field_1.VarAttribute)
[581]85    if isfield(Field_1.VarAttribute{ilist},'ProjModeRequest')
86        ProjModeRequest_1{ilist}=Field_1.VarAttribute{ilist}.ProjModeRequest;
[402]87    end
88end
[8]89
[515]90%% rename the dimensions of the second field if identical to those of the first
91for 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]105end
106
[580]107%% look for coordinates common to Field in Field_1
[515]108ind_remove=zeros(size(Field_1.ListVarName));
109for 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)
[581]117            if (isempty(ProjModeRequest{i1})&&isempty(ProjModeRequest_1{ilist})||strcmp(ProjModeRequest{i1},ProjModeRequest_1{ilist})) && isequal(Field.(Field.ListVarName{i1}),VarVal)
[515]118               ind_remove(ilist)=1;
119               NewDim=Field.VarDimName{i1};
120               if ischar(NewDim)
121                   NewDim={NewDim};
122               end
[580]123               Field_1.VarDimName=regexprep_r(Field_1.VarDimName,['^' OldDim{1} '$'],NewDim{1});
[180]124            end
125        end
[8]126    end
127end
[515]128Field_1.ListVarName(find(ind_remove))=[];%removes these redondent coordinates
129Field_1.VarDimName(find(ind_remove))=[];
130Field_1.VarAttribute(find(ind_remove))=[];
[8]131
[580]132%% append the other variables of the second field, modifying their name if needed
[515]133for 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'];   
[580]140            if isfield(Field_1.VarAttribute{ilist},'FieldName')
141                Field_1.VarAttribute{ilist}.FieldName=regexprep_r(Field_1.VarAttribute{ilist}.FieldName,VarName,VarNameNew);
142            end
[8]143    end
[515]144        SubData.ListVarName=[SubData.ListVarName {VarNameNew}];
145        SubData.VarDimName=[SubData.VarDimName Field_1.VarDimName(ilist)];
146        SubData.(VarNameNew)=Field_1.(VarName);
147        SubData.VarAttribute=[SubData.VarAttribute Field_1.VarAttribute(ilist)];
148        SubData.VarAttribute{end}.CheckSub=1;% mark that the field needs to be substracted
149end
150
151%% substrat fields when possible
[548]152%[CellVarIndex,NbDim,CellVarType,errormsg]=find_field_cells(SubData);
[580]153[CellInfo,NbDim,errormsg]=find_field_cells(SubData);
[515]154ind_remove=zeros(size(SubData.ListVarName));
155ivar=[];
156ivar_1=[];
[548]157for 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
172end
[515]173for 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;
178end
179SubData.ListVarName(find(ind_remove))=[];
180SubData.VarDimName(find(ind_remove))=[];
181SubData.VarAttribute(find(ind_remove))=[];
[580]182%end
[520]183
[580]184function OutputCell=regexprep_r(InputCell,search_string,new_string)
[520]185for icell=1:numel(InputCell)
[580]186    OutputCell{icell}=regexprep(InputCell{icell},search_string,new_string);
[520]187end
[515]188       
189   
Note: See TracBrowser for help on using the repository browser.