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

Last change on this file since 271 was 271, checked in by smasson, 17 years ago

bugfix for interpolation from ORCA2 without mask

  • Property svn:keywords set to Id
File size: 11.5 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
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 BEGIN
72    omsk = replicate(1b, jpio, jpjo)
73; if this is ORCA2 grid...
74    IF (jpio EQ 180 OR jpio EQ 182) AND (jpjo EQ 149  OR jpjo EQ 148  OR jpjo EQ 147 ) THEN BEGIN
75; we look for ill defined cells.
76      IF jpio EQ 182 THEN lontmp = olonin[1:180, *] ELSE lontmp = olonin
77; longitudinal size of the cells...
78      a = (lontmp-shift(lontmp, 1, 0))
79      d1 = 0.5 * max( [[[a]], [[360+a]]] MOD 360, dimension = 3)
80      a = (lontmp-shift(lontmp, 1, 1))
81      d2 = 0.5 * max( [[[a]], [[360+a]]] MOD 360, dimension = 3)
82      a = (shift(lontmp, 0, 1)-shift(lontmp, 1, 0))
83      d3 = 0.5 * max( [[[a]], [[360+a]]] MOD 360, dimension = 3)
84      a = (shift(lontmp, 0, 1)-shift(lontmp, 1, 1))
85      d4 = 0.5 * max( [[[a]], [[360+a]]] MOD 360, dimension = 3)
86      md = [[[d1]], [[d2]], [[d3]], [[d4]]]
87      bad = max(md, dimension = 3) GE 1.5
88      bad = (bad + shift(bad, -1, -1) + shift(bad, 0, -1) + shift(bad, -1, 0)) < 1
89      IF jpio EQ 182 THEN BEGIN
90        omsk[1:180, 80:120] = 1b - bad[*, 80:120]
91        omsk[0, *] = omsk[180, *]
92        omsk[181, *] = omsk[1, *]
93      ENDIF ELSE omsk[*, 80:120] = 1b - bad[*, 80:120]
94    ENDIF
95  ENDIF ELSE BEGIN
96    IF n_elements(omsk) NE jpio*jpjo THEN BEGIN
97      ras = report('input grid mask do not have the good size')
98      stop
99    ENDIF
100  ENDELSE
101  IF n_elements(amsk) EQ 1 AND amsk[0] EQ -1 THEN amsk = replicate(1b, jpia, jpja) ELSE BEGIN
102    IF n_elements(amsk) NE jpia*jpja THEN BEGIN
103      ras = report('output grid mask do not have the good size')
104      stop
105    ENDIF
106  ENDELSE
107;
108; longitude, between 0 and 360
109  alon = alonin MOD 360
110  out = where(alon LT 0)
111  IF out[0] NE -1 THEN alon[out] = alon[out]+360
112  olon = olonin MOD 360
113  out = where(olon LT 0)
114  IF out[0] NE -1 THEN olon[out] = olon[out]+360
115;
116; we work only on the ouput grid water points
117  awater = where(amsk EQ 1)
118  nawater = n_elements(awater)
119;
120; define all cells that have corners located at olon, olat
121; we define the cell with the address of all corners
122;
123;            3        2
124;             +------+
125;             |      |
126;             |      |
127;             |      |
128;             +------+
129;            0        1
130;
131  alladdr = lindgen(jpio, jpjo-1)
132  celladdr = lonarr(4, jpio*(jpjo-1))
133  celladdr[0, *] = alladdr
134  celladdr[1, *] = shift(alladdr, -1)
135  celladdr[2, *] = shift(alladdr + jpio, -1)
136  celladdr[3, *] = alladdr + jpio
137;
138; list the cells that have at least 1 ocean point as corner
139  good = where(total(omsk[celladdr], 1) GT 0)
140; keep only those cells
141  celladdr = celladdr[*, temporary(good)]
142;
143  xcell = olon[celladdr]
144  minxcell = min(xcell, dimension = 1, max = maxxcell)
145  ycell = olat[celladdr]
146  minycell = min(ycell, dimension = 1, max = maxycell)
147; foraddr: address of the ocean water cell associated to each atmosphere water point
148  foraddr = lonarr(nawater)
149; forweight: x/y position of the atmosphere water point in the ocean water cell
150  forweight = dblarr(nawater, 2)
151;
152; Loop on all the water point of the atmosphere
153; We look for which ocean water cell contains the atmosphere water point
154;
155  delta = max([(360./jpio), (180./jpjo)])* 4.
156  FOR n = 0L, nawater-1 DO BEGIN
157; control print
158    IF (n MOD 5000) EQ 0 THEN print, n
159; longitude and latitude of the atmosphere water point
160    xx = alon[awater[n]]
161    yy = alat[awater[n]]
162; 1) we reduce the number of ocean cells for which we will try to know if
163; xx,yy is inside.
164    CASE 1 OF
165; if we are near the norh pole
166      yy GE (90-delta):BEGIN
167        lat1 = 90-2*delta
168        good = where(maxycell GE lat1)
169        onsphere = 1
170      END
171; if we are near the longitude periodicity area
172      xx LE delta OR xx GE (360-delta):BEGIN
173        lat1 = yy-delta
174        lat2 = yy+delta
175        good = where((minxcell LE 2*delta OR maxxcell GE (360-2*delta)) AND maxycell GE lat1 AND minycell LE lat2)
176        onsphere = 1
177      END
178; other cases
179      ELSE:BEGIN
180        lon1 = xx-delta
181        lon2 = xx+delta
182        lat1 = yy-delta
183        lat2 = yy+delta
184        good = where(maxxcell GE lon1 AND minxcell LE lon2 AND maxycell GE lat1 AND minycell le lat2)
185; ORCA cases : orca grid is irregular only northward of 40N
186        CASE 1 OF
187          (jpio EQ 90 OR jpio EQ 92) AND (jpjo EQ 76   OR jpjo EQ 75   OR jpjo EQ 74  ):onsphere = yy GT 40
188          (jpio EQ 180 OR jpio EQ 182) AND (jpjo EQ 149  OR jpjo EQ 148  OR jpjo EQ 147 ):onsphere = yy GT 40
189          (jpio EQ 720 OR jpio EQ 722)  AND (jpjo EQ 522  OR jpjo EQ 521  OR jpjo EQ 520 ):onsphere = yy GT 40
190          (jpio EQ 1440 OR jpio EQ 1442) AND (jpjo EQ 1021 OR jpjo EQ 1020 OR jpjo EQ 1019):onsphere = yy GT 40
191          ELSE:onsphere = 1
192        ENDCASE
193      END
194    ENDCASE
195; we found a short list of possible ocean water cells containing the atmosphere water point
196    IF good[0] NE -1 THEN BEGIN
197; in which cell is located the atmosphere water point?
198; Warning! inquad use clockwise quadrilateral definition
199      ind = inquad(xx, yy $
200                   , xcell[0, good], ycell[0, good] $
201                   , xcell[3, good], ycell[3, good] $
202                   , xcell[2, good], ycell[2, good] $
203                   , xcell[1, good], ycell[1, good] $
204                   , onsphere = onsphere, newcoord = newcoord, /noprint)
205; keep only the first cell (if the atmospheric point was located in several ocean cells)
206      ind = ind[0]
207; we found one ocean water cell containing the atmosphere water point
208      IF ind NE -1 THEN BEGIN
209        ind = good[ind]
210; now, we morph the quadrilateral ocean cell into the reference square (0 -> 1)
211; in addition we get the coordinates of the atmospheric point in this new morphed square
212        IF onsphere THEN BEGIN
213; Warning! quadrilateral2square use anticlockwise quadrilateral definition
214          xy = quadrilateral2square(newcoord[0, 0], newcoord[1, 0] $
215                                    , newcoord[0, 3], newcoord[1, 3] $
216                                    , newcoord[0, 2], newcoord[1, 2] $
217                                    , newcoord[0, 1], newcoord[1, 1] $
218                                    , newcoord[0, 4], newcoord[1, 4])
219        ENDIF ELSE BEGIN
220          xy = quadrilateral2square(xcell[0, ind], ycell[0, ind] $
221                                    , xcell[1, ind], ycell[1, ind] $
222                                    , xcell[2, ind], ycell[2, ind] $
223                                    , xcell[3, ind], ycell[3, ind], xx, yy)
224        ENDELSE
225; take care of rounding errors...
226        zero = where(abs(xy) LT 1e-4)
227        IF zero[0] NE -1 THEN xy[zero] = 0
228        one = where(abs(1-xy) LT 1e-4)
229        IF one[0] NE -1 THEN xy[one] = 1
230; some checks...
231        tmpmsk = omsk[celladdr[*, ind]]
232        CASE 1 OF
233          xy[0] LT 0 OR xy[0] GT 1 : stop
234          xy[1] LT 0 OR xy[1] GT 1 : stop
235          xy[0] EQ 0 AND xy[1] EQ 0 AND tmpmsk[0] EQ 0 : foraddr[n] = -1
236          xy[0] EQ 1 AND xy[1] EQ 0 AND tmpmsk[1] EQ 0 : foraddr[n] = -1
237          xy[0] EQ 1 AND xy[1] EQ 1 AND tmpmsk[2] EQ 0 : foraddr[n] = -1
238          xy[0] EQ 0 AND xy[1] EQ 1 AND tmpmsk[3] EQ 0 : foraddr[n] = -1
239          xy[0] EQ 0 AND (tmpmsk[0]+tmpmsk[3]) EQ 0    : foraddr[n] = -1
240          xy[0] EQ 1 AND (tmpmsk[1]+tmpmsk[2]) EQ 0    : foraddr[n] = -1
241          xy[1] EQ 0 AND (tmpmsk[0]+tmpmsk[1]) EQ 0    : foraddr[n] = -1
242          xy[1] EQ 1 AND (tmpmsk[2]+tmpmsk[3]) EQ 0    : foraddr[n] = -1
243          ELSE: BEGIN
244; we keep its address
245            foraddr[n] = ind
246; keep the new coordinates
247            forweight[n, 0] = xy[0]
248            forweight[n, 1] = xy[1]
249          END
250        ENDCASE
251
252
253
254      ENDIF ELSE foraddr[n] = -1
255    ENDIF ELSE foraddr[n] = -1
256  ENDFOR
257; do we have some water atmospheric points that are not located in an water oceanic cell?
258  bad = where(foraddr EQ -1)
259  IF bad[0] NE -1 THEN BEGIN
260; yes?
261; we look for neighbor water atmospheric point located in water oceanic cell
262    badaddr = awater[bad]
263    good = where(foraddr NE -1)
264; list the atmospheric points located in water oceanic cell
265    goodaddr = awater[good]
266; there longitude and latitude
267    goodlon = alon[goodaddr]
268    goodlat = alat[goodaddr]
269; for all the bad points, look for a neighbor
270    neig = lonarr(n_elements(bad))
271    FOR i = 0, n_elements(bad)-1 DO BEGIN
272      neig[i] = (neighbor(alon[badaddr[i]], alat[badaddr[i]], goodlon, goodlat, /sphere))[0]
273    ENDFOR
274; get the address regarding foraddr
275    neig = good[neig]
276; associate each bad point with its neighbor (get its address and weight)
277    foraddr[bad] = foraddr[neig]
278    forweight[bad, *] = forweight[neig, *]
279  endif
280; transform the address of the ocean cell into the address of its 4 corners
281  newaaddr = celladdr[*, temporary(foraddr)]
282; now we compute the weight to give at each corner
283  newaweig = dblarr(4, nawater)
284  a = reform(forweight[*, 0], 1, nawater)
285  b = reform(forweight[*, 1], 1, nawater)
286  forweight =  -1               ; free memory
287  newaweig = [(1-a)*(1-b), (1-b)*a, a*b, (1-a)*b]
288  a = -1 &  b = -1              ; free memory
289; mask the weight to suppress the corner located on land
290  newaweig = newaweig*((omsk)[newaaddr])
291  totalweig = total(newaweig, 1)
292; for cell with some land corner,
293; we have to redistribute the weight on the reaining water corners
294; weights normalization
295  totalweig = total(newaweig, 1)
296  IF min(totalweig, max = ma) EQ 0 then stop
297  IF ma GT 1 then stop
298  newaweig = newaweig/(replicate(1., 4)#totalweig)
299  totalweig = total(newaweig, 1)
300
301; weights
302  weig = dblarr(4, jpia*jpja)
303  weig[*, awater] = temporary(newaweig)
304; address
305  addr = dblarr(4, jpia*jpja)
306  addr[*, awater] = temporary(newaaddr)
307;
308  RETURN
309END
Note: See TracBrowser for help on using the repository browser.