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
Line 
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%-----------------------------------------------------------------------
9% function SubData=sub_field(Field,XmlData,Field_1)
10%
11% OUPUT:
12% SubData: structure representing the resulting field
13%
14% INPUT:
15% Field: matlab structure representing the first field
16% Field_1:matlab structure representing the second field
17
18function SubData=sub_field(Field,XmlData,Field_1)
19
20SubData=[];
21if strcmp(Field,'*')
22    return
23end
24if nargin<3
25    SubData=Field;
26    return
27end
28%% global attributes
29SubData.ListGlobalAttribute={};%default
30%transfer global attributes of Field
31if isfield(Field,'ListGlobalAttribute')
32    SubData.ListGlobalAttribute=Field.ListGlobalAttribute;
33    for ilist=1:numel(Field.ListGlobalAttribute)
34        AttrName=Field.ListGlobalAttribute{ilist};
35        SubData.(AttrName)=Field.(AttrName);
36    end
37end
38%transfer global attributes of Field_1
39if isfield(Field_1,'ListGlobalAttribute')
40    for ilist=1:numel(Field_1.ListGlobalAttribute)
41        AttrName=Field_1.ListGlobalAttribute{ilist};
42        AttrNameNew=AttrName;
43        while ~isempty(find(strcmp(AttrNameNew,SubData.ListGlobalAttribute)))&&~isequal(Field_1.(AttrNameNew),Field.(AttrNameNew))
44            AttrNameNew=[AttrNameNew '_1'];
45        end
46        if ~isfield(Field,AttrName) || ~isequal(Field_1.(AttrName),Field.(AttrName))
47            SubData.ListGlobalAttribute=[SubData.ListGlobalAttribute {AttrNameNew}];
48            SubData.(AttrNameNew)=Field_1.(AttrName);
49        end
50    end
51end
52
53%% variables
54%reproduce variables of the first field and list its dimensions
55SubData.ListVarName=Field.ListVarName;
56SubData.VarDimName=Field.VarDimName;
57if isfield(Field,'VarAttribute')
58    SubData.VarAttribute=Field.VarAttribute;
59end
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
74end
75
76%% field request
77ProjModeRequest=cell(size(Field.ListVarName));
78for ilist=1:numel(Field.VarAttribute)
79    if isfield(Field.VarAttribute{ilist},'ProjModeRequest')
80        ProjModeRequest{ilist}=Field.VarAttribute{ilist}.ProjModeRequest;
81    end
82end
83ProjModeRequest_1=cell(size(Field_1.ListVarName));
84for ilist=1:numel(Field_1.VarAttribute)
85    if isfield(Field_1.VarAttribute{ilist},'ProjModeRequest')
86        ProjModeRequest_1{ilist}=Field_1.VarAttribute{ilist}.ProjModeRequest;
87    end
88end
89
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
105end
106
107%% look for coordinates common to Field in Field_1
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);
114        end
115        VarVal=Field_1.(Field_1.ListVarName{ilist});
116        for i1=1:numel(Field.ListVarName)
117            if (isempty(ProjModeRequest{i1})&&isempty(ProjModeRequest_1{ilist})||strcmp(ProjModeRequest{i1},ProjModeRequest_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});
124            end
125        end
126    end
127end
128Field_1.ListVarName(find(ind_remove))=[];%removes these redondent coordinates
129Field_1.VarDimName(find(ind_remove))=[];
130Field_1.VarAttribute(find(ind_remove))=[];
131
132%% append the other variables of the second field, modifying their name if needed
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'];   
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
143    end
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
152%[CellVarIndex,NbDim,CellVarType,errormsg]=find_field_cells(SubData);
153[CellInfo,NbDim,errormsg]=find_field_cells(SubData);
154ind_remove=zeros(size(SubData.ListVarName));
155ivar=[];
156ivar_1=[];
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)];
162        end
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)];
166        end
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)];
170        end
171    end
172end
173for imod=1:numel(ivar)
174        VarName=SubData.ListVarName{ivar(imod)};
175        VarName_1=SubData.ListVarName{ivar_1(imod)};
176        SubData.(VarName)=double(SubData.(VarName))-double(SubData.(VarName_1));
177        ind_remove(ivar_1(imod))=1;
178end
179SubData.ListVarName(find(ind_remove))=[];
180SubData.VarDimName(find(ind_remove))=[];
181SubData.VarAttribute(find(ind_remove))=[];
182%end
183
184function OutputCell=regexprep_r(InputCell,search_string,new_string)
185for icell=1:numel(InputCell)
186    OutputCell{icell}=regexprep(InputCell{icell},search_string,new_string);
187end
188       
189   
Note: See TracBrowser for help on using the repository browser.