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