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

Last change on this file since 226 was 226, checked in by pinsard, 17 years ago

corrections of some misspellings in some *.pro

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