source: trunk/src/calc_field_interp.m @ 650

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

bugs corrected: handles.TimeName? in uvmat, 0_OAR emptied for culter computations with series.

File size: 6.2 KB
Line 
1%'calc_field_interp': defines fields (velocity, vort, div...) from civ data and calculate them
2% for projection with linear interpolation
3%---------------------------------------------------------------------
4% [VarVal,ListVarName,VarAttribute,errormsg]=calc_field_interp(Coord,Data,FieldName,XI,YI)
5%
6% OUTPUT:
7% VarVal: array giving the values of the calculated field
8% ListVarName: corresponding list of variable names
9% VarAttribute: corresponding list of variable attributes, each term #ilist is of the form VarAttribute{ilist}.tag=value
10%
11% INPUT:
12% Coord(nbpoints,2): matrix of x,y coordinates of theinput data points
13% Data: inputfield structure
14% FieldName: string representing the field to calculate, or cell array of fields (as displayed in uvmat/FieldName)
15% XI, YI: set of x and y coordiantes where the fields need to be linearly interpolated
16
17function [VarVal,ListVarName,VarAttribute,errormsg]=calc_field_interp(Coord,Data,FieldName,XI,YI)
18
19%% initialization
20VarVal={};
21ListVarName={};
22VarAttribute={};
23errormsg='';
24InputVarList={};
25if ischar(FieldName),FieldName={FieldName};end
26check_skipped=zeros(size(FieldName));% default, =1 to mark the variables which can be calculated
27Operator=cell(size(FieldName));
28
29%% analyse the list of input fields: needed variables and requested operations
30for ilist=1:numel(FieldName)
31    Operator{ilist}='';%default empty operator (vec, norm,...)
32    r=regexp(FieldName{ilist},'(?<Operator>(^vec|^norm|^curl|^div|^strain))\((?<UName>.+),(?<VName>.+)\)$','names');% analyse the field name
33    if isempty(r) % the field name is a variable itself
34        ivar=find(strcmp(FieldName{ilist},Data.ListVarName));
35        if isempty(ivar)
36            check_skipped(ilist)=1; %variable not found
37        else
38            if isfield(Data.VarAttribute{ivar},'Role') &&...
39                (strcmp(Data.VarAttribute{ivar}.Role,'ancillary')||strcmp(Data.VarAttribute{ivar}.Role,'warnflag')||strcmp(Data.VarAttribute{ivar}.Role,'errorflag'))
40                check_skipped(ilist)=1; % ancillary variable, not interpolated     
41            elseif isempty(find(strcmp(FieldName{ilist},InputVarList), 1));
42                InputVarList=[InputVarList FieldName{ilist}];% the variable is added to the list of input variables if it is not already in the list
43            end
44        end
45    else
46        if ~isfield(Data,r.UName)||~isfield(Data,r.VName)%needed input variable not found
47            check_skipped(ilist)=1;
48        elseif strcmp(r.Operator,'curl')||strcmp(r.Operator,'div')||strcmp(r.Operator,'strain')
49            Operator{ilist}=r.Operator;
50            switch r.Operator
51                case 'curl'% case of CivX data format
52                    UName{ilist}='vort';
53                    Data.vort=Data.DjUi(:,1,2)-Data.DjUi(:,2,1);
54                case 'div'
55                    UName{ilist}='div';
56                    Data.div=Data.DjUi(:,1,1)+Data.DjUi(:,2,2);
57                case 'strain'
58                    UName{ilist}='strain';
59                    Data.strain=Data.DjUi(:,1,2)+Data.DjUi(:,2,1);
60            end
61            InputVarList=[InputVarList UName{ilist}]; %the variable is added to the list if it is not already in the list
62        else
63            UName{ilist}=r.UName;
64            VName{ilist}=r.VName;
65            if isempty(find(strcmp(r.UName,InputVarList)));
66                InputVarList=[InputVarList UName{ilist}]; %the variable is added to the list if it is not already in the list
67            end
68            if isempty(find(strcmp(r.VName,InputVarList), 1));
69                InputVarList=[InputVarList VName{ilist}]; %the variable is added to the list if it is not already in the list
70            end
71            Operator{ilist}=r.Operator;
72        end
73    end
74end
75
76%% create interpolator for each variable to interpolate
77if exist('XI','var')
78    for ilist=1:numel(InputVarList)
79        F.(InputVarList{ilist})=TriScatteredInterp(Coord,Data.(InputVarList{ilist}),'linear');
80    end
81end
82
83%% perform the linear interpolation for the requested variables
84for ilist=1:numel(FieldName)
85    if ~check_skipped(ilist)
86        nbvar=numel(ListVarName);
87        switch Operator{ilist}
88            case 'vec'
89                if exist('XI','var')
90                    VarVal{nbvar+1}=F.(UName{ilist})(XI,YI);
91                    VarVal{nbvar+2}=F.(VName{ilist})(XI,YI);
92                else
93                    VarVal{nbvar+1}=Data.(UName{ilist});
94                    VarVal{nbvar+2}=Data.(VName{ilist});
95                end
96                ListVarName{nbvar+1}=UName{ilist};
97                ListVarName{nbvar+2}=VName{ilist};
98                VarAttribute{nbvar+1}.Role='vector_x';
99                VarAttribute{nbvar+2}.Role='vector_y';
100            case 'norm'
101                if exist('XI','var')
102                    U2=F.(UName{ilist})(XI,YI).*F.(UName{ilist})(XI,YI);
103                    V2=F.(VName{ilist})(XI,YI).*F.(VName{ilist})(XI,YI);
104                else
105                    U2=Data.(UName{ilist}).*Data.(UName{ilist});
106                    V2=Data.(VName{ilist}).*Data.(VName{ilist});
107                end
108                VarVal{nbvar+1}=sqrt(U2+V2);
109                ListVarName{nbvar+1}='norm';
110                VarAttribute{nbvar+1}.Role='scalar';
111            case {'curl','div','strain'}
112                if exist('XI','var')
113                    VarVal{nbvar+1}=F.(UName{ilist})(XI,YI);
114                else
115                    VarVal{nbvar+1}=Data.(UName{ilist});
116                end
117                ListVarName{nbvar+1}=UName{ilist};
118                VarAttribute{nbvar+1}.Role='scalar';
119            otherwise
120                if ~isempty(FieldName{ilist})
121                    if exist('XI','var')
122                        VarVal{nbvar+1}=F.(FieldName{ilist})(XI,YI);
123                    else
124                        VarVal{nbvar+1}= Data.(FieldName{ilist});
125                    end
126                    ListVarName{nbvar+1}=FieldName{ilist};
127                    VarAttribute{nbvar+1}.Role='scalar';
128                end
129        end
130    end
131end
132
133%% put an error flag to indicate NaN data
134if exist('XI','var')&&~isempty(VarVal)
135    nbvar=numel(ListVarName);
136    ListVarName{nbvar+1}='FF';
137    VarVal{nbvar+1}=isnan(VarVal{nbvar});
138    VarAttribute{nbvar+1}.Role='errorflag';
139end
140
141
142
143
144
Note: See TracBrowser for help on using the repository browser.