source: trunk/src/sub_field.m @ 518

Last change on this file since 518 was 515, checked in by sommeria, 12 years ago

improvement of calc-field and combination of two fields

File size: 6.9 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
77FieldRequest=cell(size(Field.ListVarName));
78for ilist=1:numel(Field.VarAttribute)
79    if isfield(Field.VarAttribute{ilist},'FieldRequest')
80        FieldRequest{ilist}=Field.VarAttribute{ilist}.FieldRequest;
81    end
82end
83FieldRequest_1=cell(size(Field_1.ListVarName));
84for ilist=1:numel(Field_1.VarAttribute)
85    if isfield(Field_1.VarAttribute{ilist},'FieldRequest')
86        FieldRequest_1{ilist}=Field_1.VarAttribute{ilist}.FieldRequest;
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(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});
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    end
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
146end
147
148%append the other variables of the second field, modifying their name if needed
149
150%% substrat fields when possible
151[CellVarIndex,NbDim,CellVarType,errormsg]=find_field_cells(SubData);
152ind_remove=zeros(size(SubData.ListVarName));
153ivar=[];
154ivar_1=[];
155for icell=1:numel(CellVarIndex)
156    if ~isempty(CellVarIndex{icell})
157        if isfield(CellVarType{icell},'scalar') && numel(CellVarType{icell}.scalar)==2 && SubData.VarAttribute{CellVarType{icell}.scalar(2)}.CheckSub;
158            ivar=[ivar CellVarType{icell}.scalar(1)];
159            ivar_1=[ivar_1 CellVarType{icell}.scalar(2)];
160        end
161        if isfield(CellVarType{icell},'vector_x') && numel(CellVarType{icell}.vector_x)==2 && SubData.VarAttribute{CellVarType{icell}.vector_x(2)}.CheckSub;
162            ivar=[ivar CellVarType{icell}.vector_x(1)];
163            ivar_1=[ivar_1 CellVarType{icell}.vector_x(2)];
164        end
165        if isfield(CellVarType{icell},'vector_y') && numel(CellVarType{icell}.vector_y)==2 && SubData.VarAttribute{CellVarType{icell}.vector_y(2)}.CheckSub;
166            ivar=[ivar CellVarType{icell}.vector_y(1)];
167            ivar_1=[ivar_1 CellVarType{icell}.vector_y(2)];
168        end
169    end
170end
171for imod=1:numel(ivar)
172        VarName=SubData.ListVarName{ivar(imod)};
173        VarName_1=SubData.ListVarName{ivar_1(imod)};
174        SubData.(VarName)=SubData.(VarName)-SubData.(VarName_1);
175        ind_remove(ivar_1(imod))=1;
176end
177SubData.ListVarName(find(ind_remove))=[];
178SubData.VarDimName(find(ind_remove))=[];
179SubData.VarAttribute(find(ind_remove))=[];
180       
181   
Note: See TracBrowser for help on using the repository browser.