1 |
gforget |
1.2 |
function [fldOut]=gcmfaces_remap_3d(lon,lat,depIn,fldIn,nDblRes); |
2 |
gforget |
1.1 |
%object: use bin average to remap a lat-lon-depth field to mygrid.mskC |
3 |
|
|
%input: lon,lat,dep,fld are the gridded product arrays (2D,2D,1D,3D) |
4 |
|
|
%assumption:fld should show NaN for missing values |
5 |
|
|
|
6 |
|
|
gcmfaces_global; |
7 |
|
|
|
8 |
|
|
%initiate triangulation: |
9 |
|
|
gcmfaces_bindata; |
10 |
|
|
|
11 |
|
|
%vertical levels: |
12 |
|
|
nrIn=length(depIn); |
13 |
|
|
depOut=-mygrid.RC; |
14 |
|
|
nrOut=length(depOut); |
15 |
|
|
|
16 |
gforget |
1.2 |
if isempty(whos('nDblRes')); nDblRes=3; end; |
17 |
|
|
|
18 |
gforget |
1.1 |
%corresponding model mask: chosen to avoid vertical extrapolation later |
19 |
|
|
% 1) from model just above |
20 |
|
|
mskC=repmat(0*mygrid.XC,[1 1 nrIn]); |
21 |
|
|
KK2=zeros(1,nrIn); |
22 |
|
|
for kk=1:nrIn; |
23 |
|
|
kk2=max(find(depOut<=depIn(kk)));%model level just above |
24 |
|
|
if isempty(kk2); kk2=1; end;%pathological case |
25 |
|
|
KK2(kk)=kk2; |
26 |
|
|
mskC(:,:,kk)=mygrid.mskC(:,:,kk2); |
27 |
|
|
end; |
28 |
|
|
% 2) shift to above atlas level mask |
29 |
|
|
for kk=nrIn:-1:2; |
30 |
|
|
mskC(:,:,kk)=mskC(:,:,kk-1);%atlas level just above |
31 |
|
|
end; |
32 |
|
|
|
33 |
|
|
%test vertical interpolation to mygrid.RC of the mask: |
34 |
|
|
% => the printed result should be 0 |
35 |
|
|
if 0; |
36 |
|
|
mskCout=atlas_interp_vert(mskC,depIn,depOut); |
37 |
|
|
aa=convert2gcmfaces(isnan(mskCout)&~isnan(mygrid.mskC)); |
38 |
|
|
nansum(aa(:)) |
39 |
|
|
end; |
40 |
|
|
|
41 |
|
|
%horizontal mapping to mygrid.XC, mygrid.YC, mskC: |
42 |
|
|
fld1=mskC; |
43 |
|
|
tocs=[]; |
44 |
|
|
for kk=1:nrIn; |
45 |
|
|
tic; |
46 |
gforget |
1.2 |
fld1(:,:,kk)=gcmfaces_remap_2d(lon,lat,fldIn(:,:,kk),nDblRes,mskC(:,:,kk)); |
47 |
gforget |
1.1 |
tocs=[tocs;toc]; |
48 |
|
|
[kk nrIn mean(tocs)] |
49 |
|
|
end; |
50 |
|
|
|
51 |
|
|
%vertical interpolation to mygrid.RC |
52 |
|
|
fld2=atlas_interp_vert(fld1,depIn,depOut); |
53 |
|
|
|
54 |
|
|
|
55 |
|
|
%final mask handling: |
56 |
|
|
%1) remask with mygrid.mskC (mskC was wider by design) |
57 |
|
|
msk1=mygrid.mskC; tmp1=msk1.*fld2; |
58 |
|
|
%2) extrapolate vertically (some deep canions may still miss data) |
59 |
|
|
fldOut=atlas_extrap_vert(tmp1,msk1,depOut); |
60 |
|
|
|
61 |
|
|
|
62 |
|
|
function [fldOut]=atlas_interp_vert(fldIn,depthIn,depthOut); |
63 |
|
|
%vertical interpolation |
64 |
|
|
|
65 |
|
|
gcmfaces_global; |
66 |
|
|
|
67 |
|
|
nrIn=length(depthIn); |
68 |
|
|
nrOut=length(depthOut); |
69 |
|
|
|
70 |
|
|
%map depthIn to depthOut: |
71 |
|
|
coeff=interp1(depthIn,[1:nrIn],depthOut); |
72 |
|
|
tmp1=find(isnan(coeff)); coeff(tmp1)=nrIn-0.01; %quick fix |
73 |
|
|
|
74 |
|
|
%vertical interpolation to mygrid.RC: |
75 |
|
|
fldOut=repmat(0*mygrid.XC,[1 1 nrOut]); |
76 |
|
|
for kk=1:nrOut; |
77 |
|
|
tmp1=coeff(kk); tmp2=floor(tmp1); tmp1=tmp1-tmp2; |
78 |
|
|
if tmp2==nrIn; |
79 |
|
|
tmp3=fldIn(:,:,tmp2); |
80 |
|
|
else; |
81 |
|
|
tmp3=(1-tmp1)*fldIn(:,:,tmp2)+tmp1*fldIn(:,:,tmp2+1); |
82 |
|
|
end; |
83 |
|
|
fldOut(:,:,kk)=tmp3; |
84 |
|
|
end; |
85 |
|
|
|
86 |
|
|
function [fldOut]=atlas_extrap_vert(fldIn,mskOut,depthIn); |
87 |
|
|
%vertical extrapolation when needed |
88 |
|
|
|
89 |
|
|
fldOut=convert2array(fldIn); |
90 |
|
|
mskOut=convert2array(mskOut); |
91 |
|
|
nrIn=length(depthIn); |
92 |
|
|
|
93 |
|
|
for kk=2:nrIn; |
94 |
|
|
tmp_kk=fldOut(:,:,kk); |
95 |
|
|
msk_kk=mskOut(:,:,kk); |
96 |
|
|
[II,JJ]=find(isnan(tmp_kk)&~isnan(msk_kk)); |
97 |
|
|
if ~isempty(II)&kk==2; |
98 |
|
|
tmp_kkM1=fldOut(:,:,kk-1); |
99 |
|
|
tmp1=find(isnan(tmp_kk)&~isnan(msk_kk)&~isnan(tmp_kkM1)); |
100 |
|
|
tmp_kk(tmp1)=tmp_kkM1(tmp1); |
101 |
|
|
fldOut(:,:,kk)=tmp_kk; |
102 |
|
|
elseif ~isempty(II); |
103 |
|
|
tmp_kkM1=fldOut(:,:,kk-1); |
104 |
|
|
tmp_kkM2=fldOut(:,:,kk-2); |
105 |
|
|
coeff=(depthIn(kk)-depthIn(kk-1))/(depthIn(kk-1)-depthIn(kk-2)); |
106 |
|
|
tmp_kk_x=tmp_kkM1+coeff*(tmp_kkM1-tmp_kkM2); |
107 |
|
|
tmp1=find(isnan(tmp_kk)&~isnan(msk_kk)&~isnan(tmp_kk_x)); |
108 |
|
|
tmp_kk(tmp1)=tmp_kk_x(tmp1); |
109 |
|
|
fldOut(:,:,kk)=tmp_kk; |
110 |
|
|
end; |
111 |
|
|
end; |
112 |
|
|
|
113 |
|
|
fldOut=convert2array(fldOut); |
114 |
|
|
|