1 | %'tps_coeff_field': calculate the thin plate spline (tps) coefficients within subdomains for a field structure |
---|
2 | %--------------------------------------------------------------------- |
---|
3 | % DataOut=tps_coeff_field(DataIn,checkall) |
---|
4 | % |
---|
5 | % OUTPUT: |
---|
6 | % DataOut: output field structure, reproducing the input field structure DataIn and adding the fields: |
---|
7 | % .Coord_tps |
---|
8 | % .[VarName '_tps'] for each eligible input variable VarName (scalar ofr vector components) |
---|
9 | % errormsg: error message, = '' by default |
---|
10 | % |
---|
11 | % INPUT: |
---|
12 | % DataIn: intput field structure |
---|
13 | % checkall:=1 if tps is needed for all fields (a projection mode interp_tps is needed), =0 otherwise |
---|
14 | % |
---|
15 | % called functions: |
---|
16 | % 'find_field_cells': analyse the input field structure, grouping the variables into 'fields' with common coordinates |
---|
17 | % 'set_subdomains': sort a set of points defined by scattered coordinates in subdomains, as needed for tps interpolation |
---|
18 | % 'tps_coeff': calculate the thin plate spline (tps) coefficients for a single domain. |
---|
19 | |
---|
20 | function [DataOut,errormsg]=tps_coeff_field(DataIn,checkall) |
---|
21 | DataOut=DataIn;%default |
---|
22 | SubDomainNbPoint=1000; %default, estimated nbre of data source points in a subdomain used for tps |
---|
23 | if isfield(DataIn,'SubDomain') |
---|
24 | SubDomainNbPoint=DataIn.SubDomain;%old convention |
---|
25 | end |
---|
26 | if isfield(DataIn,'SubDomainNbPoint') |
---|
27 | SubDomainNbPoint=DataIn.SubDomainNbPoint;% |
---|
28 | end |
---|
29 | [CellInfo,NbDimArray,errormsg]=find_field_cells(DataIn); |
---|
30 | if ~isempty(errormsg) |
---|
31 | errormsg=['tps_coeff_field/find_field_cells/' errormsg]; |
---|
32 | return |
---|
33 | end |
---|
34 | nbtps=0;% indicate the number of tps coordinate sets in the field structure (in general =1) |
---|
35 | |
---|
36 | for icell=1:numel(CellInfo); |
---|
37 | if NbDimArray(icell)>=2 && strcmp(CellInfo{icell}.CoordType,'scattered')% %if the coordinates are scattered |
---|
38 | NbCoord=NbDimArray(icell);% dimension of space |
---|
39 | nbtps=nbtps+1;% indicate the number of tps coordinate sets in the field structure (in general =1) |
---|
40 | X=DataIn.(DataIn.ListVarName{CellInfo{icell}.CoordIndex(end)});% value of x coordinate |
---|
41 | Y=DataIn.(DataIn.ListVarName{CellInfo{icell}.CoordIndex(end-1)});% value of x coordinate |
---|
42 | check_interp_tps=false(numel(CellInfo{icell}.VarIndex),1); |
---|
43 | %for ivar=1:numel(CellInfo{icell}.VarIndex) |
---|
44 | Index_interp=[]; |
---|
45 | if isfield(CellInfo{icell},'VarIndex_scalar') |
---|
46 | Index_interp=[Index_interp CellInfo{icell}.VarIndex_scalar]; |
---|
47 | end |
---|
48 | if isfield(CellInfo{icell},'VarIndex_vector_x') |
---|
49 | Index_interp=[Index_interp CellInfo{icell}.VarIndex_vector_x]; |
---|
50 | end |
---|
51 | if isfield(CellInfo{icell},'VarIndex_vector_y') |
---|
52 | Index_interp=[Index_interp CellInfo{icell}.VarIndex_vector_y]; |
---|
53 | end |
---|
54 | for ivar=Index_interp |
---|
55 | Attr=DataIn.VarAttribute{CellInfo{icell}.VarIndex(ivar)}; |
---|
56 | if ~isfield(Attr,'VarIndex_tps')&& (checkall || (isfield(Attr,'ProjModeRequest')&&strcmp(Attr.ProjModeRequest,'interp_tps'))) |
---|
57 | check_interp_tps(ivar)=1; |
---|
58 | end |
---|
59 | end |
---|
60 | VarIndexInterp=CellInfo{icell}.VarIndex(check_interp_tps);% indices of variables to interpolate through tps |
---|
61 | if ~isempty(VarIndexInterp) |
---|
62 | ListVarInterp=DataIn.ListVarName(VarIndexInterp); |
---|
63 | % exclude data points marked 'false' for interpolation |
---|
64 | if isfield(CellInfo{icell},'VarIndex_errorflag') |
---|
65 | FF=DataIn.(DataIn.ListVarName{CellInfo{icell}.VarIndex_errorflag});% error flag |
---|
66 | X=X(FF==0); |
---|
67 | Y=Y(FF==0); |
---|
68 | for ilist=1:numel(VarIndexInterp) |
---|
69 | DataIn.(ListVarInterp{ilist})=DataIn.(ListVarInterp{ilist})(FF==0); |
---|
70 | end |
---|
71 | end |
---|
72 | term=''; |
---|
73 | if nbtps>1 |
---|
74 | term=['_' num2str(nbtps-1)]; |
---|
75 | end |
---|
76 | ListNewVar=cell(1,numel(VarIndexInterp)+3); |
---|
77 | ListNewVar(1:3)={['SubRange' term],['NbCentre' term],['Coord_tps' term]}; |
---|
78 | for ilist=1:numel(VarIndexInterp) |
---|
79 | ListNewVar{ilist+3}=[ListVarInterp{ilist} '_tps' term]; |
---|
80 | end |
---|
81 | nbvar=numel(DataIn.ListVarName); |
---|
82 | DataOut.ListVarName=[DataIn.ListVarName ListNewVar]; |
---|
83 | %ListNewDim={['nb_tps' term],['nb_subdomain' term]}; |
---|
84 | DataOut.VarDimName=[DataIn.VarDimName {{'nb_coord','nb_bounds',['nb_subdomain' term]}} {['nb_subdomain' term]} ... |
---|
85 | {{['nb_tps' term],'nb_coord',['nb_subdomain' term]}}]; |
---|
86 | DataOut.VarAttribute{nbvar+3}.Role='coord_tps'; |
---|
87 | [SubRange,NbCentre,IndSelSubDomain] =set_subdomains([X Y],SubDomainNbPoint);% create subdomains for tps |
---|
88 | for isub=1:size(SubRange,3) |
---|
89 | ind_sel=IndSelSubDomain(1:NbCentre(isub),isub);% array indices selected for the subdomain |
---|
90 | DataOut.(['Coord_tps' term])(1:NbCentre(isub),1:2,isub)=[X(ind_sel) Y(ind_sel)]; |
---|
91 | DataOut.(['Coord_tps' term])(NbCentre(isub)+1:NbCentre(isub)+3,1:2,isub)=0;%matrix of zeros to complement the matrix Coord_tps (conveninent for file storage) |
---|
92 | %fill=zeros(NbCoord+1,NbCoord,size(SubRange,3)); %matrix of zeros to complement the matrix Data.Civ1_Coord_tps (conveninent for file storage) |
---|
93 | % fill=zeros(NbCoord+1,NbCoord+1,NbCoord); %matrix of zeros to complement the matrix Data.Civ1_Coord_tps (conveninent for file storage) |
---|
94 | % Coord_tps=cat(1,Coord_tps,fill); |
---|
95 | end |
---|
96 | for ivar=1:numel(VarIndexInterp) |
---|
97 | DataOut.VarDimName{nbvar+3+ivar}={['nb_tps' term],['nb_subdomain' term]}; |
---|
98 | DataOut.VarAttribute{nbvar+3+ivar}=DataIn.VarAttribute{CellInfo{icell}.VarIndex_vector_x};%reproduce attributes of velocity |
---|
99 | if ~isfield(DataIn.VarAttribute{VarIndexInterp(ivar)},'Role') |
---|
100 | DataOut.VarAttribute{nbvar+3+ivar}.Role='scalar_tps'; |
---|
101 | else |
---|
102 | DataOut.VarAttribute{nbvar+3+ivar}.Role=[DataIn.VarAttribute{VarIndexInterp(ivar)}.Role '_tps']; |
---|
103 | end |
---|
104 | DataOut.VarAttribute{VarIndexInterp(ivar)}.VarIndex_tps=nbvar+3+ivar;% indicate the tps correspondance in the source data |
---|
105 | end |
---|
106 | if isfield(DataOut,'ListDimName')%cleaning' |
---|
107 | DataOut=rmfield(DataOut,'ListDimName'); |
---|
108 | end |
---|
109 | if isfield(DataOut,'DimValue')%cleaning |
---|
110 | DataOut=rmfield(DataOut,'DimValue'); |
---|
111 | end |
---|
112 | DataOut.(['SubRange' term])=SubRange; |
---|
113 | DataOut.(['NbCentre' term])=NbCentre; |
---|
114 | % DataOut.(['Coord_tps' term])=Coord_tps; |
---|
115 | for ilist=1:numel(VarIndexInterp) |
---|
116 | % Var_tps=zeros(size(IndSelSubDomain)+[NbCoord+1 0]);%default spline |
---|
117 | for isub=1:size(SubRange,3) |
---|
118 | ind_sel=IndSelSubDomain(1:NbCentre(isub),isub);% array indices selected for the subdomain |
---|
119 | [tild,Var_tps(1:NbCentre(isub)+NbCoord+1,isub)]=tps_coeff([X(ind_sel) Y(ind_sel)],DataIn.(ListVarInterp{ilist})(ind_sel),0);%calculate the tps coeff in the subdomain |
---|
120 | end |
---|
121 | DataOut.(ListNewVar{ilist+3})=Var_tps; |
---|
122 | end |
---|
123 | end |
---|
124 | end |
---|
125 | end |
---|