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

Last change on this file since 495 was 495, checked in by pinsard, 10 years ago

fix thanks to coding rules; typo; dupe empty lines; trailing blanks

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