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