source: trunk/SRC/Interpolation/compute_fromreg_bilinear_weigaddr.pro @ 200

Last change on this file since 200 was 163, checked in by navarro, 18 years ago

header improvements : type of parameters and keywords, default values, spell checking + idldoc assistant (IDL online_help)

  • Property svn:keywords set to Id
File size: 7.4 KB
Line 
1;+
2; @file_comments
3; compute the weight and address needed to interpolate data from a
4; "regular grid" to any grid using the bilinear method
5;
6; @categories
7; Interpolation
8;
9; @param alonin{in}{required}
10; longitude of the input data
11;
12; @param alatin {in}{required}
13; latitude of the input data
14;
15; @param olonin {in}{required}
16; longitude of the output data
17;
18; @param olat {in}{required}
19; latitude of the output data
20;
21; @keyword NONORTHERNLINE
22; activate if you don't want to take into
23; account the northen line of the input data when performing the interpolation.
24;
25; @keyword NOSOUTHERNLINE
26; activate if you don't want to take into
27; account the southern line of the input data when performing the interpolation.
28;
29; @param weig {out}
30; @param addr {out}
31; 2D arrays, weig and addr are the weight and addresses used to
32; perform the interpolation:
33;  dataout = total(weig*datain[addr], 1)
34;  dataout = reform(dataout, jpio, jpjo, /over)
35;
36; @restrictions
37;  - the input grid must be a "regular grid", defined as a grid for which each
38;  longitudes lines have the same latitude and each latitudes columns have the
39;  same longitude.
40;  - We supposed the data are located on a sphere, with a periodicity along
41;  the longitude.
42;  - points located out of the southern and northern boundaries are interpolated
43;  using a linear interpolation only along the longitudinal direction.
44;
45; @history
46;  November 2005: Sebastien Masson (smasson\@lodyc.jussieu.fr)
47;
48; @version $Id$
49;
50;-
51;
52PRO compute_fromreg_bilinear_weigaddr, alonin, alatin, olonin, olat, weig, addr $
53  , NONORTHERNLINE = nonorthernline, NOSOUTHERNLINE = nosouthernline
54;
55  compile_opt idl2, strictarrsubs
56;
57  alon = alonin
58  alat = alatin
59  olon = olonin
60;
61  jpia = n_elements(alon)
62  jpja = n_elements(alat)
63;
64  jpio = (size(olon, /dimensions))[0]
65  jpjo = (size(olon, /dimensions))[1]
66;
67; alon
68  minalon = min(alon,  max = maxalon)
69  IF maxalon-minalon GE 360. THEN stop
70; alon must be monotonically increasing
71  IF array_equal(sort(alon), lindgen(jpia)) NE 1 THEN BEGIN
72    shiftx = -(where(alon EQ min(alon)))[0]
73    alon = shift(alon, shiftx)
74    IF array_equal(sort(alon), lindgen(jpia)) NE 1 THEN stop
75  ENDIF ELSE shiftx = 0
76; for longitude periodic boundary condition we add the fist
77; column on the right side of the array and
78  alon = [alon, alon[0]+360.]
79  jpia = jpia+1L
80; alat
81  revy = alat[0] GT alat[1]
82  IF revy THEN alat = reverse(alat)
83; alat must be monotonically increasing
84  IF array_equal(sort(alat), lindgen(jpja)) NE 1 THEN stop
85;
86  if keyword_set(nonorthernline) then BEGIN
87    jpja = jpja - 1L
88    alat = alat[0: jpja-1L]
89  ENDIF
90  if keyword_set(nosouthernline) then BEGIN
91    alat = alat[1: jpja-1L]
92    jpja = jpja - 1L
93  ENDIF
94; olon between minalon et minalon+360
95  out = where(olon LT minalon)
96  WHILE out[0] NE -1 DO BEGIN
97    olon[out] = olon[out]+360.
98    out = where(olon LT minalon)
99  ENDWHILE
100  out = where(olon GE minalon+360.)
101  WHILE out[0] NE -1 DO BEGIN
102    olon[out] = olon[out]- 360.
103    out = where(olon GE minalon+360.)
104  ENDWHILE
105; make sure that all values of olon are located within values of alon
106  IF min(olon, max = ma) LT minalon THEN stop
107  IF ma GE minalon+360. THEN stop
108;
109; we want to do biliear interpolation => for each ocean point, we must
110; find in which atm cell it is located.
111; if the ocean point is out of the atm grid, we use closest neighbor
112; interpolation
113;
114; for each T point of oce grid, we find in which armospheric cell it is
115; located.
116; As the atmospheric grid is regular, we can use inrecgrid instead
117; of inquad.
118  pos = inrecgrid(olon, olat, alon[0:jpia-2L], alat[0:jpja-2L] $
119                  , checkout = [alon[jpia-1L], alat[jpja-1L]], /output2d)
120; checks...
121; for longitude, each ocean points must be located in atm cell.
122  IF (where(pos[0, *] EQ -1))[0] NE -1 THEN stop
123; no ocean point should be located westward of the left boundary of the
124; atm cell in which it is supposed to be located
125  IF total(olon LT alon[pos[0, *]]) NE 0 THEN stop
126; no ocean point should be located eastward of the right boundary of the
127; atm cell in which it is supposed to be located
128  IF total(olon GT alon[pos[0, *]+1]) NE 0 THEN stop
129;
130; we use bilinear interpolation
131;
132; we change the coordinates of each ocean points to fit into a
133; rectangle defined by:
134;
135;  y2 *------------*
136;     |            |
137;     |            |
138;     |            |
139;  y1 *------------*
140;     x1          x2
141;
142;    X = (x-x1)/(x2-x1)
143;    Y = (y-y1)/(y2-y1)
144;
145  indx = pos[0, *]
146  indy = (temporary(pos))[1, *]
147; points located out of the atmospheric grid...(too much northward or southward)
148  bad = where(indy EQ -1)
149  indy = 0 > indy
150;
151  IF max(indx) GT jpia-2 THEN stop ; checks...
152  IF max(indy) GT jpja-2 THEN stop ; checks...
153; x coordinates of the atm cell
154  x1 = alon[indx]
155  x2 = alon[indx+1]
156; new x coordinates of the ocean points in each cell
157  divi = temporary(x2)-x1
158  glamnew = (olon-x1)/temporary(divi)
159  x1 = -1 ; free memory
160  olon = -1 ; free memory
161; y coordinates of the atm cell
162  y1 = alat[indy]
163  y2 = alat[indy+1]             ;
164; new y coordinates of the ocean points in each cell
165  divi = temporary(y2)-y1
166  zero = where(divi EQ 0)
167  IF zero[0] NE -1 THEN divi[zero] = 1.
168  gphinew = (olat-y1)/temporary(divi)
169  y1 = -1 ; free memory
170; checks...
171  IF min(glamnew) LT 0 THEN stop
172  IF max(glamnew) GT 1 THEN stop
173;
174; weight and address array used for bilinear interpolation.
175  xaddr = lonarr(4, jpio*jpjo)
176  xaddr[0, *] = indx
177  xaddr[1, *] = indx + 1L
178  xaddr[2, *] = indx + 1L
179  xaddr[3, *] = indx
180;
181  yaddr = lonarr(4, jpio*jpjo)
182  yaddr[0, *] = indy
183  yaddr[1, *] = indy
184  yaddr[2, *] = indy + 1L
185  yaddr[3, *] = indy + 1L
186; compute the weight for the bilinear interpolation.
187  weig = fltarr(4, jpio*jpjo)
188  weig[0, *] = (1.-glamnew) * (1.-gphinew)
189  weig[1, *] =     glamnew  * (1.-gphinew)
190  weig[2, *] =     glamnew  *     gphinew
191  weig[3, *] = (1.-glamnew) *     gphinew
192; free memory
193  gphinew = -1
194  IF bad[0] EQ -1 THEN glamnew = -1 ELSE glamnew = (temporary(glamnew))[bad]
195; we work now on the "bad" points
196; linear interpolation only along the longitudinal direction
197  IF bad[0] NE -1 THEN BEGIN
198    ybad = olat[bad]
199; the ocean points that are not located into an atm cell should be
200; located northward of the northern boundary of the atm grid
201;      or southward of the southern boundary of the atm grid
202    IF total(ybad GE min(alat) AND ybad LE max(alat)) GE 1 THEN stop
203;
204    weig[0, bad] = (1.-glamnew)
205    weig[1, bad] = temporary(glamnew)
206    weig[2, bad] = 0.
207    weig[3, bad] = 0.
208    south = where(ybad LT alat[0])
209    IF south[0] NE -1 THEN yaddr[*, bad[temporary(south)]] = 0L
210    north = where(ybad GT alat[jpja-1])
211    IF north[0] NE -1 THEN yaddr[*, bad[temporary(north)]] = 0L
212    ybad = -1 & bad = -1 ; free memory
213  ENDIF
214; check totalweight = 1
215  totalweig = abs(1-total(weig, 1))
216  IF (where(temporary(totalweig) GE 1.e-5))[0] NE -1 THEN stop
217;
218; come back to the original atm grid without longitudinal overlap.
219;
220  jpia = jpia-1L
221  xaddr = temporary(xaddr) MOD jpia
222; take into account shiftx if needed
223  IF shiftx NE 0 THEN xaddr = (temporary(xaddr) - shiftx) MOD jpia
224; take into account nosouthernline and nonorthernline
225  if keyword_set(nosouthernline) then BEGIN
226    yaddr = temporary(yaddr) + 1L
227    jpja = jpja + 1L
228  ENDIF
229  if keyword_set(nonorthernline) then jpja = jpja + 1L
230; take into account revy if needed
231  IF revy EQ 1 THEN yaddr = jpja - 1L - temporary(yaddr)
232;                         ;
233  addr = temporary(yaddr)*jpia + temporary(xaddr)
234;
235  return
236end
237
Note: See TracBrowser for help on using the repository browser.