source: trunk/src/sub_field.m @ 764

Last change on this file since 764 was 713, checked in by sommeria, 11 years ago

improving figure extracting and keyboard callback

File size: 7.7 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
29%% global attributes
30SubData.ListGlobalAttribute={};%default
31%transfer global attributes of Field
32if isfield(Field,'ListGlobalAttribute')
33    SubData.ListGlobalAttribute=Field.ListGlobalAttribute;
34    for ilist=1:numel(Field.ListGlobalAttribute)
35        AttrName=Field.ListGlobalAttribute{ilist};
36        SubData.(AttrName)=Field.(AttrName);
37    end
38end
39%transfer global attributes of Field_1
40if isfield(Field_1,'ListGlobalAttribute')
41    for ilist=1:numel(Field_1.ListGlobalAttribute)
42        AttrName=Field_1.ListGlobalAttribute{ilist};
43        AttrNameNew=AttrName;
44        while ~isempty(find(strcmp(AttrNameNew,SubData.ListGlobalAttribute)))&&~isequal(Field_1.(AttrNameNew),Field.(AttrNameNew))
45            AttrNameNew=[AttrNameNew '_1'];
46        end
47        if ~isfield(Field,AttrName) || ~isequal(Field_1.(AttrName),Field.(AttrName))
48            SubData.ListGlobalAttribute=[SubData.ListGlobalAttribute {AttrNameNew}];
49            SubData.(AttrNameNew)=Field_1.(AttrName);
50        end
51    end
52end
53
54%% variables
55%reproduce variables of the first field and list its dimensions
56SubData.ListVarName=Field.ListVarName;
57SubData.VarDimName=Field.VarDimName;
58if isfield(Field,'VarAttribute')
59    SubData.VarAttribute=Field.VarAttribute;
60end
61ListDimName={};
62for 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
75end
76
77%% field request
78ProjModeRequest=cell(size(Field.ListVarName));
79for ilist=1:numel(Field.VarAttribute)
80    if isfield(Field.VarAttribute{ilist},'ProjModeRequest')
81        ProjModeRequest{ilist}=Field.VarAttribute{ilist}.ProjModeRequest;
82    end
83end
84ProjModeRequest_1=cell(size(Field_1.ListVarName));
85for ilist=1:numel(Field_1.VarAttribute)
86    if isfield(Field_1.VarAttribute{ilist},'ProjModeRequest')
87        ProjModeRequest_1{ilist}=Field_1.VarAttribute{ilist}.ProjModeRequest;
88    end
89end
90
91%% rename the dimensions of the second field if identical to those of the first
92for 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
106end
107
108%% look for coordinates common to Field in Field_1
109ind_remove=zeros(size(Field_1.ListVarName));
110% loop on the variables of the second field Field_1
111for ilist=1:numel(Field_1.ListVarName)
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
118        for i1=1:numel(Field.ListVarName)
119            if  isequal(Field.(Field.ListVarName{i1}),OldDim) &&...
120                   ((isempty(ProjModeRequest{i1}) && isempty(ProjModeRequest_1{ilist}))  || strcmp(ProjModeRequest{i1},ProjModeRequest_1{ilist}))               
121               ind_remove(ilist)=1;
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
125            end
126        end
127    end
128end
129Field_1.ListVarName(find(ind_remove))=[];%removes the redondent coordinate
130Field_1.VarDimName(find(ind_remove))=[];
131Field_1.VarAttribute(find(ind_remove))=[];
132
133%% append the other variables of the second field, modifying their name if needed
134for 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'];   
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
144    end
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
150end
151
152%% substrat fields when possible
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)
185if ischar(InputCell); InputCell={InputCell}; end
186OutputCell=InputCell;%default
187for icell=1:numel(InputCell)
188    OutputCell{icell}=regexprep(InputCell{icell},search_string,new_string);
189end
190       
191   
Note: See TracBrowser for help on using the repository browser.