1 |
gforget |
1.1 |
function [Vq]=gcmfaces_interp_1d(xDim,X,V,Xq,varargin); |
2 |
|
|
%[val]=gcmfaces_interp_1d(xDim,X,fld,Xq,varargin); |
3 |
|
|
%object: linearly interpolate field along xDim dimension |
4 |
|
|
% (>3 for gcmfaces objects) |
5 |
|
|
%inputs: xDim is the dimension along which to interpolate |
6 |
|
|
% X is the original position vector |
7 |
|
|
% V is a gcmfaces object with at least |
8 |
|
|
% Xq is the target position vector |
9 |
|
|
%optional: varargin ... |
10 |
|
|
%outputs: Vq is the interpolated field |
11 |
|
|
|
12 |
|
|
gcmfaces_global; |
13 |
|
|
|
14 |
|
|
%change format if needed |
15 |
|
|
isagcmfaces=isa(V,'gcmfaces'); |
16 |
|
|
if isagcmfaces; V=convert2gcmfaces(V); end; |
17 |
|
|
|
18 |
|
|
%move interpolation dimension to first |
19 |
|
|
ndim=length(size(V)); |
20 |
|
|
tmp1=circshift([1:ndim],[0 1-xDim]); |
21 |
|
|
V=permute(V,tmp1); |
22 |
|
|
if size(X,1)==1; X=X'; end; |
23 |
|
|
if size(Xq,1)==1; Xq=Xq'; end; |
24 |
|
|
|
25 |
|
|
%the interpolation itself |
26 |
|
|
Kq=interp1(X,[1:length(X)]',Xq,'linear'); |
27 |
|
|
Kqne=interp1(X,[1:length(X)]',Xq,'nearest','extrap'); |
28 |
|
|
|
29 |
|
|
%use bilinear; then extrapolate with nearest neighbor |
30 |
|
|
Vq=NaN*repmat(V(1,:,:),[length(Xq) 1 1]); |
31 |
|
|
for kk=1:length(Xq); |
32 |
|
|
if ~isnan(Kq(kk)); |
33 |
|
|
k0=floor(Kq(kk)); |
34 |
|
|
k1=min(k0+1,length(X)); |
35 |
|
|
a0=Kq(kk)-k0; |
36 |
|
|
tmp1=(1-a0)*V(k0,:,:)+a0*V(k1,:,:); |
37 |
|
|
else; |
38 |
|
|
tmp1=NaN*V(1,:,:); |
39 |
|
|
end; |
40 |
|
|
tmp2=V(Kqne(kk),:,:); |
41 |
|
|
tmp1(isnan(tmp1))=tmp2(isnan(tmp1)); |
42 |
|
|
Vq(kk,:,:)=tmp1; |
43 |
|
|
end; |
44 |
|
|
|
45 |
|
|
%move interpolation dimension to first |
46 |
|
|
ndim=length(size(Vq)); |
47 |
|
|
tmp1=circshift([1:ndim],[0 xDim-1]); |
48 |
|
|
Vq=permute(Vq,tmp1); |
49 |
|
|
|
50 |
|
|
%change format if needed |
51 |
|
|
if isagcmfaces; Vq=convert2gcmfaces(Vq); end; |
52 |
|
|
|