source: trunk/SRC/Interpolation/compute_fromirr_bilinear_weigaddr.pro @ 238

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

improvements/corrections of some *.pro headers

  • Property svn:keywords set to Id
File size: 10.3 KB
Line 
1;+
2;
3; @file_comments
4; compute the weight and address needed to interpolate data from
5; an "irregular 2D grid" (defined as a grid made of quadrilateral cells)
6; to any grid using the bilinear method
7;
8; @categories
9; Interpolation
10;
11; @param olonin {in}{required}{type=2d array}
12; longitude of the input data
13;
14; @param olat {in}{required}{type=2d array}
15; latitude of the input data
16;
17; @param omsk {in}{required}{type=2d array or -1}
18; land/sea mask of the input data
19; put -1 if input data are not masked
20;
21; @param alonin {in}{required}{type=2d array}
22; longitude of the output data
23;
24; @param alat {in}{required}{type=2d array}
25; latitude of the output data
26;
27; @param amsk {in}{required}{type=2d array or -1}
28; land/sea mask of the output data
29; put -1 if output data are not masked
30;
31; @param weig {out}{type=2d array}
32; (see ADDR)
33;
34; @param addr {out}{type=2d array}
35; 2D arrays, weig and addr are the weight and addresses used to
36; perform the interpolation:
37;  dataout = total(weig*datain[addr], 1)
38;  dataout = reform(dataout, jpia, jpja, /over)
39;
40; @restrictions
41;  -  the input grid must be an "irregular 2D grid", defined as a grid made
42;  of quadrilateral cells which corners positions are defined with olonin and olat
43;  -  We supposed the data are located on a sphere, with a periodicity along
44;  the longitude
45;  -  to perform the bilinear interpolation within quadrilateral cells, we
46;  first morph the cell into a square cell and then compute the bilinear
47;  interpolation.
48;  -  if some corners of the cell are land points, their weight is set to 0
49;  and the weight is redistributed on the remaining "water" corners
50;  -  points located out of the southern and northern boundaries or in cells
51;  containing only land points are set the same value as their closest neighbor
52;
53; @history
54;  June 2006: Sebastien Masson (smasson\@lodyc.jussieu.fr)
55;
56; @version
57; $Id$
58;
59;-
60;
61PRO compute_fromirr_bilinear_weigaddr, olonin, olat, omsk, alonin, alat, amsk, weig, addr
62;
63  compile_opt idl2, strictarrsubs
64;
65  jpia = (size(alonin, /dimensions))[0]
66  jpja = (size(alonin, /dimensions))[1]
67;
68  jpio = (size(olonin, /dimensions))[0]
69  jpjo = (size(olonin, /dimensions))[1]
70; mask check
71  IF n_elements(omsk) EQ 1 AND omsk[0] EQ -1 THEN omsk = replicate(1b, jpio, jpjo)
72  IF n_elements(amsk) EQ 1 AND amsk[0] EQ -1 THEN amsk = replicate(1b, jpia, jpja)
73  IF n_elements(omsk) NE jpio*jpjo THEN BEGIN
74    ras = report('input grid mask do not have the good size')
75    stop
76  ENDIF
77  IF n_elements(amsk) NE jpia*jpja THEN BEGIN
78    ras = report('output grid mask do not have the good size')
79    stop
80  ENDIF
81;
82; longitude, between 0 and 360
83  alon = alonin MOD 360
84  out = where(alon LT 0)
85  IF out[0] NE -1 THEN alon[out] = alon[out]+360
86  olon = olonin MOD 360
87  out = where(olon LT 0)
88  IF out[0] NE -1 THEN olon[out] = olon[out]+360
89;
90; we work only on the ouput grid water points
91  awater = where(amsk EQ 1)
92  nawater = n_elements(awater)
93;
94; define all cells that have corners located at olon, olat
95; we define the cell with the address of all corners
96;
97;            3        2
98;             +------+
99;             |      |
100;             |      |
101;             |      |
102;             +------+
103;            0        1
104;
105  alladdr = lindgen(jpio, jpjo-1)
106  celladdr = lonarr(4, jpio*(jpjo-1))
107  celladdr[0, *] = alladdr
108  celladdr[1, *] = shift(alladdr, -1)
109  celladdr[2, *] = shift(alladdr + jpio, -1)
110  celladdr[3, *] = alladdr + jpio
111;
112; list the cells that have at least 1 ocean point as corner
113  good = where(total(omsk[celladdr], 1) GT 0)
114; keep only those cells
115  celladdr = celladdr[*, temporary(good)]
116;
117  xcell = olon[celladdr]
118  minxcell = min(xcell, dimension = 1, max = maxxcell)
119  ycell = olat[celladdr]
120  minycell = min(ycell, dimension = 1, max = maxycell)
121; foraddr: address of the ocean water cell associated to each atmosphere water point
122  foraddr = lonarr(nawater)
123; forweight: x/y position of the atmosphere water point in the ocean water cell
124  forweight = dblarr(nawater, 2)
125;
126; Loop on all the water point of the atmosphere
127; We look for which ocean water cell contains the atmosphere water point
128;
129  delta = max([(360./jpio), (180./jpjo)])* 4.
130  FOR n = 0L, nawater-1 DO BEGIN
131; control print
132    IF (n MOD 5000) EQ 0 THEN print, n
133; longitude and latitude of the atmosphere water point
134    xx = alon[awater[n]]
135    yy = alat[awater[n]]
136; 1) we reduce the number of ocean cells for which we will try to know if
137; xx,yy is inside.
138    CASE 1 OF
139; if we are near the norh pole
140      yy GE (90-delta):BEGIN
141        lat1 = 90-2*delta
142        good = where(maxycell GE lat1)
143        onsphere = 1
144      END
145; if we are near the longitude periodicity area
146      xx LE delta OR xx GE (360-delta):BEGIN
147        lat1 = yy-delta
148        lat2 = yy+delta
149        good = where((minxcell LE 2*delta OR maxxcell GE (360-2*delta)) AND maxycell GE lat1 AND minycell LE lat2)
150        onsphere = 1
151      END
152; other cases
153      ELSE:BEGIN
154        lon1 = xx-delta
155        lon2 = xx+delta
156        lat1 = yy-delta
157        lat2 = yy+delta
158        good = where(maxxcell GE lon1 AND minxcell LE lon2 AND maxycell GE lat1 AND minycell le lat2)
159; ORCA cases : orca grid is irregular only northward of 40N
160        CASE 1 OF
161          jpio EQ 92   AND (jpjo EQ 76   OR jpjo EQ 75   OR jpjo EQ 74  ):onsphere = yy GT 40
162          jpio EQ 180  AND (jpjo EQ 149  OR jpjo EQ 148  OR jpjo EQ 147 ):onsphere = yy GT 40
163          jpio EQ 720  AND (jpjo EQ 522  OR jpjo EQ 521  OR jpjo EQ 520 ):onsphere = yy GT 40
164          jpio EQ 1440 AND (jpjo EQ 1021 OR jpjo EQ 1020 OR jpjo EQ 1019):onsphere = yy GT 40
165          ELSE:onsphere = 1
166        ENDCASE
167      END
168    ENDCASE
169; we found a short list of possible ocean water cells containing the atmosphere water point
170    IF good[0] NE -1 THEN BEGIN
171; in which cell is located the atmosphere water point?
172; Warning! inquad use clockwise quadrilateral definition
173      ind = inquad(xx, yy $
174                   , xcell[0, good], ycell[0, good] $
175                   , xcell[3, good], ycell[3, good] $
176                   , xcell[2, good], ycell[2, good] $
177                   , xcell[1, good], ycell[1, good] $
178                   , onsphere = onsphere, newcoord = newcoord, /noprint)
179; keep only the first cell (if the atmospheric point was located in several ocean cells)
180      ind = ind[0]
181; we found one ocean water cell containing the atmosphere water point
182      IF ind NE -1 THEN BEGIN
183        ind = good[ind]
184; now, we morph the quadrilateral ocean cell into the reference square (0 -> 1)
185; in addition we get the coordinates of the atmospheric point in this new morphed square
186        IF onsphere THEN BEGIN
187; Warning! quadrilateral2square use anticlockwise quadrilateral definition
188          xy = quadrilateral2square(newcoord[0, 0], newcoord[1, 0] $
189                                    , newcoord[0, 3], newcoord[1, 3] $
190                                    , newcoord[0, 2], newcoord[1, 2] $
191                                    , newcoord[0, 1], newcoord[1, 1] $
192                                    , newcoord[0, 4], newcoord[1, 4])
193        ENDIF ELSE BEGIN
194          xy = quadrilateral2square(xcell[0, ind], ycell[0, ind] $
195                                    , xcell[1, ind], ycell[1, ind] $
196                                    , xcell[2, ind], ycell[2, ind] $
197                                    , xcell[3, ind], ycell[3, ind], xx, yy)
198        ENDELSE
199; take care of rounding errors...
200        zero = where(abs(xy) LT 1e-4)
201        IF zero[0] NE -1 THEN xy[zero] = 0
202        one = where(abs(1-xy) LT 1e-4)
203        IF one[0] NE -1 THEN xy[one] = 1
204; some checks...
205        tmpmsk = omsk[celladdr[*, ind]]
206        CASE 1 OF
207          xy[0] LT 0 OR xy[0] GT 1 : stop
208          xy[1] LT 0 OR xy[1] GT 1 : stop
209          xy[0] EQ 0 AND xy[1] EQ 0 AND tmpmsk[0] EQ 0 : foraddr[n] = -1
210          xy[0] EQ 1 AND xy[1] EQ 0 AND tmpmsk[1] EQ 0 : foraddr[n] = -1
211          xy[0] EQ 1 AND xy[1] EQ 1 AND tmpmsk[2] EQ 0 : foraddr[n] = -1
212          xy[0] EQ 0 AND xy[1] EQ 1 AND tmpmsk[3] EQ 0 : foraddr[n] = -1
213          xy[0] EQ 0 AND (tmpmsk[0]+tmpmsk[3]) EQ 0    : foraddr[n] = -1
214          xy[0] EQ 1 AND (tmpmsk[1]+tmpmsk[2]) EQ 0    : foraddr[n] = -1
215          xy[1] EQ 0 AND (tmpmsk[0]+tmpmsk[1]) EQ 0    : foraddr[n] = -1
216          xy[1] EQ 1 AND (tmpmsk[2]+tmpmsk[3]) EQ 0    : foraddr[n] = -1
217          ELSE: BEGIN
218; we keep its address
219        foraddr[n] = ind
220; keep the new coordinates
221            forweight[n, 0] = xy[0]
222            forweight[n, 1] = xy[1]
223          END
224        ENDCASE
225
226
227
228      ENDIF ELSE foraddr[n] = -1
229    ENDIF ELSE foraddr[n] = -1
230  ENDFOR
231; do we have some water atmospheric points that are not located in an water oceanic cell?
232  bad = where(foraddr EQ -1)
233  IF bad[0] NE -1 THEN BEGIN
234; yes?
235; we look for neighbor water atmospheric point located in water oceanic cell
236    badaddr = awater[bad]
237    good = where(foraddr NE -1)
238; list the atmospheric points located in water oceanic cell
239    goodaddr = awater[good]
240; there longitude and latitude
241    goodlon = alon[goodaddr]
242    goodlat = alat[goodaddr]
243; for all the bad points, look for a neighbor
244    neig = lonarr(n_elements(bad))
245    FOR i = 0, n_elements(bad)-1 DO BEGIN
246      neig[i] = (neighbor(alon[badaddr[i]], alat[badaddr[i]], goodlon, goodlat, /sphere))[0]
247    ENDFOR
248; get the address regarding foraddr
249    neig = good[neig]
250; associate each bad point with its neighbor (get its address and weight)
251    foraddr[bad] = foraddr[neig]
252    forweight[bad, *] = forweight[neig, *]
253  endif
254; transform the address of the ocean cell into the address of its 4 corners
255  newaaddr = celladdr[*, temporary(foraddr)]
256; now we compute the weight to give at each corner
257  newaweig = dblarr(4, nawater)
258  a = reform(forweight[*, 0], 1, nawater)
259  b = reform(forweight[*, 1], 1, nawater)
260  forweight =  -1 ; free memory
261  newaweig = [(1-a)*(1-b), (1-b)*a, a*b, (1-a)*b]
262  a = -1 &  b = -1 ; free memory
263; mask the weight to suppress the corner located on land
264  newaweig = newaweig*((omsk)[newaaddr])
265  totalweig = total(newaweig, 1)
266; for cell with some land corner,
267; we have to redistribute the weight on the reaining water corners
268; weights normalization
269  totalweig = total(newaweig, 1)
270  IF min(totalweig, max = ma) EQ 0 then stop
271  IF ma GT 1 then stop
272  newaweig = newaweig/(replicate(1., 4)#totalweig)
273  totalweig = total(newaweig, 1)
274
275; weights
276  weig = dblarr(4, jpia*jpja)
277  weig[*, awater] = temporary(newaweig)
278; address
279  addr = dblarr(4, jpia*jpja)
280  addr[*, awater] = temporary(newaaddr)
281;
282  RETURN
283END
Note: See TracBrowser for help on using the repository browser.