/[MITgcm]/MITgcm_contrib/gael/matlab_class/gcmfaces_misc/gcmfaces_interp_coeffs.m
ViewVC logotype

Annotation of /MITgcm_contrib/gael/matlab_class/gcmfaces_misc/gcmfaces_interp_coeffs.m

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (hide annotations) (download)
Wed Apr 8 17:50:10 2015 UTC (10 years, 3 months ago) by gforget
Branch: MAIN
- gcmfaces_interp_coeffs.m : compute bilinear interpolation weights.
- gcmfaces_polygonangle.m : compute sum of polygon interior angles.
- gcmfaces_quadmap.m : compute bilinear interpolation coefficients.

1 gforget 1.1 function [prof_interp]=gcmfaces_interp_coeffs(prof_lon,prof_lat,varargin);
2     %[prof_interp]=gcmfaces_interp_coeffs(prof_lon,prof_lat);
3     %object: compute bilinear interpolation weights for prof_lon, prof_lat
4     %inputs: prof_lon, prof_lat are column vectors
5     %(optional) ni,nj is the MITgcm tile size (2 numbers total)
6     %outputs: prof_interp contains face and tile numbers,
7     % indices within tiles (within 0:ni+1,0:nj+1)
8     % and interpolation weights (between 0 and 1)
9     % of the four neighboring grid points.
10     %
11     %note: pathological cases (e.g. at edges) remain to be treated.
12     %example:
13     %prof=MITprof_load('ctd_feb2013.nc');
14     %[prof_interp]=gcmfaces_interp_coeffs(prof.prof_lon,prof.prof_lat);
15    
16     gcmfaces_global;
17    
18     doDisplay=0;
19     doVerbose=1; if myenv.verbose>=1; doVerbose=1; end;
20    
21     %set-up tile information (domain decomposition to ni,nj blocs)
22     if nargin<=2;
23     ni=30; nj=30;
24     else;
25     ni=varargin{1};
26     nj=varargin{2};
27     end;
28    
29     map_tile=gcmfaces_loc_tile(ni,nj);
30     loc_tile=gcmfaces_loc_tile(ni,nj,prof_lon,prof_lat);
31     prof_tile=loc_tile.tileNo;
32     list_tile=unique(prof_tile);
33    
34     %initialize output:
35     prof_interp.face=NaN*prof_lon;
36     prof_interp.tile=NaN*prof_lon;
37     prof_interp.i=NaN*repmat(prof_lon,[1 4]);
38     prof_interp.j=NaN*repmat(prof_lon,[1 4]);
39     prof_interp.w=NaN*repmat(prof_lon,[1 4]);
40    
41     %loop over tiles
42     for ii=1:length(list_tile);
43    
44     %1) determine face of current tile ...
45     tmp1=1*(map_tile==list_tile(ii));
46     tmp11=sum(sum(tmp1,1),2); tmp12=[];
47     for ff=1:tmp11.nFaces; tmp12=[tmp12,tmp11{ff}]; end;
48     iiFace=find(tmp12);
49     %... and its index range within face ...
50     tmp1=tmp1{iiFace};
51     tmp11=sum(tmp1,2);
52     iiMin=min(find(tmp11)); iiMax=max(find(tmp11));
53     tmp11=sum(tmp1,1);
54     jjMin=min(find(tmp11)); jjMax=max(find(tmp11));
55     %... as well as the list of profiles in tile
56     ii_prof=find(prof_tile==list_tile(ii));
57    
58     clear tmp*;
59    
60     %2) stereographic projection to current tile center:
61     ii0=floor((iiMin+iiMax)/2);
62     jj0=floor((jjMin+jjMax)/2);
63     XC0=mygrid.XC{iiFace}(ii0,jj0);
64     YC0=mygrid.YC{iiFace}(ii0,jj0);
65     %for grid locations:
66     [xx,yy]=gcmfaces_stereoproj(XC0,YC0);
67     %for profile locations
68     [prof_x,prof_y]=gcmfaces_stereoproj(XC0,YC0,prof_lon,prof_lat);
69    
70     % nrm=sqrt(prof_x.^2+prof_y.^2);
71     %ii_prof=find(nrm<tan(pi/4/2));%points inside of pi/4 cone
72    
73     %3) form array of grid cell quadrilaterals
74     xxx=exch_T_N(xx); yyy=exch_T_N(yy);
75     x_quad=[]; y_quad=[]; i_quad=[]; j_quad=[];
76     for pp=1:4;
77     switch pp;
78     case 1; di=0; dj=0;
79     case 2; di=1; dj=0;
80     case 3; di=1; dj=1;
81     case 4; di=0; dj=1;
82     end;
83     %note the shift in indices due to exchange above
84     tmpx=xxx{iiFace}(iiMin+di:iiMax+1+di,jjMin+dj:jjMax+1+dj);
85     tmpx=tmpx(:); x_quad=[x_quad tmpx];
86     tmpy=yyy{iiFace}(iiMin+di:iiMax+1+di,jjMin+dj:jjMax+1+dj);
87     tmpy=tmpy(:); y_quad=[y_quad tmpy];
88     %
89     tmpi=[0+di:iiMax-iiMin+1+di]'*ones(1,jjMax-jjMin+2);
90     tmpi=tmpi(:); i_quad=[i_quad tmpi];
91     tmpj=ones(jjMax-jjMin+2,1)*[0+dj:jjMax-jjMin+1+dj];
92     tmpj=tmpj(:); j_quad=[j_quad tmpj];
93     end;
94    
95     %4) associate profile locations with quadrilaterals
96     [angsum]=gcmfaces_polygonangle(x_quad,y_quad,prof_x(ii_prof),prof_y(ii_prof));
97     [II,JJ]=find(abs(angsum)>179);%+-360 for an interior point (+-180 for an edge point)
98     if length(unique(JJ))~=length(JJ)&doVerbose;
99     n0=num2str(length(JJ)-length(unique(JJ)));
100     warning(['multiple polygons (' n0 ')']);
101     end;
102     if length(unique(JJ))<length(ii_prof)&doVerbose;
103     n0=num2str(length(ii_prof)-length(unique(JJ)));
104     n1=num2str(length(ii_prof));
105     warning(['no polygon for ' n0 ' / ' n1]);
106     %the following will then remove the corresponding profiles form ii_prof
107     end;
108     [C,IA,IC] = unique(JJ);
109     %
110     ii_prof0=ii_prof;
111     ii_prof=ii_prof(C);%treated profiles
112     jj_prof=setdiff(ii_prof0,ii_prof);%un-treated profiles
113     %
114     ii_quad=II(IA);
115    
116     if doDisplay;
117     figureL;
118     xx_tile=xxx{iiFace}(iiMin:iiMax+2,jjMin:jjMax+2);
119     yy_tile=yyy{iiFace}(iiMin:iiMax+2,jjMin:jjMax+2);
120     pcolor(xx_tile,yy_tile,sqrt(xx_tile.^2+yy_tile.^2)); hold on;
121     cc=caxis; cc(2)=cc(2)*2; caxis(cc);
122     plot(prof_x(ii_prof),prof_y(ii_prof),'r.','MarkerSize',20);
123     plot(prof_x(jj_prof),prof_y(jj_prof),'k.','MarkerSize',60);
124     axis([-0.6 0.6 -0.6 0.6]/6);
125     end;
126    
127     if ~isempty(ii_prof);
128     %5) determine bi-linear interpolation weights:
129     px=x_quad(ii_quad,:);
130     py=y_quad(ii_quad,:);
131     ox=prof_x(ii_prof);
132     oy=prof_y(ii_prof);
133     [ow]=gcmfaces_quadmap(px,py,ox,oy);
134    
135     %to double check interpolation
136     % pw=squeeze(ow);
137     % oxInterp=sum(pw.*px,2);
138     % oyInterp=sum(pw.*py,2);
139    
140     %6) output interpolation specs:
141     prof_interp.face(ii_prof,1)=iiFace*(1+0*ii_quad);
142     prof_interp.tile(ii_prof,1)=list_tile(ii)*(1+0*ii_quad);
143     prof_interp.i(ii_prof,:)=i_quad(ii_quad,:);
144     prof_interp.j(ii_prof,:)=j_quad(ii_quad,:);
145     prof_interp.w(ii_prof,:)=squeeze(ow);
146     end;
147    
148     end;%for ii=1:length(list_tile);
149    
150     % if doVerbose;
151     n1=sum(~isnan(prof_interp.face));
152     n2=sum(isnan(prof_interp.face));
153     fprintf(['interpolated points: ' num2str(n1) '\n']);
154     fprintf(['un-treated points: ' num2str(n2) '\n']);
155     % end;
156    

  ViewVC Help
Powered by ViewVC 1.1.22