[101] | 1 | ;+ |
---|
[232] | 2 | ; |
---|
[136] | 3 | ; @file_comments |
---|
[238] | 4 | ; extrapolate data (zinput) where maskinput equal 0 by filling step by |
---|
[295] | 5 | ; step the coastline points with the mean value of the 8 neighbors |
---|
| 6 | ; (weighted by their mask values). |
---|
[101] | 7 | ; |
---|
[226] | 8 | ; @categories |
---|
[157] | 9 | ; Interpolation |
---|
| 10 | ; |
---|
[202] | 11 | ; @param zinput {in}{required}{type=2d array} |
---|
[118] | 12 | ; data to be extrapolate |
---|
| 13 | ; |
---|
[202] | 14 | ; @param maskinput {in}{required}{type=2d array or -1} |
---|
| 15 | ; a 2D array, the land-sea mask of the output data (1 on ocean, 0 on land) |
---|
| 16 | ; put -1 if input data are not masked |
---|
[118] | 17 | ; |
---|
[271] | 18 | ; @param nb_iteration {in}{optional}{type=integer}{default=large enough to fill everything} |
---|
[242] | 19 | ; Maximum number of iterations done in the extrapolation process. If there |
---|
[202] | 20 | ; is no more masked values we exit extrapolate before reaching nb_iteration |
---|
[118] | 21 | ; |
---|
[261] | 22 | ; @keyword X_PERIODIC {type=scalar, 0 or 1}{default=0} |
---|
[202] | 23 | ; put 1 to specify that the data are periodic along x axis |
---|
[118] | 24 | ; |
---|
[202] | 25 | ; @keyword MINVAL {type=scalar}{default=not used} |
---|
| 26 | ; to specify a minimum value to the extrapolated values |
---|
| 27 | ; |
---|
| 28 | ; @keyword MAXVAL {type=scalar}{default=not used} |
---|
| 29 | ; to specify a maximum value to the extrapolated values |
---|
| 30 | ; |
---|
| 31 | ; @keyword GE0 {type=scalar 0 or 1}{default=0} |
---|
| 32 | ; put 1 to force the extrapolated values to be larger than 0, same as using minval=0. |
---|
| 33 | ; |
---|
[271] | 34 | ; @keyword |
---|
| 35 | ; _EXTRA to be able to call extrapolate with _extra keyword |
---|
| 36 | ; |
---|
[238] | 37 | ; @returns |
---|
| 38 | ; the extrapolated 2d array |
---|
[202] | 39 | ; |
---|
| 40 | ; @examples |
---|
| 41 | ; IDL> a=extrapolate(dist(jpi,jpj),tmask[*,*,0],/x_periodic) |
---|
| 42 | ; IDL> tvplus, a |
---|
| 43 | ; IDL> tvplus, a*(1-tmask[*,*,0]) |
---|
| 44 | ; get the coastline: |
---|
| 45 | ; IDL> a=extrapolate(tmask[*,*,0],tmask[*,*,0],1,/x_periodic) |
---|
| 46 | ; IDL> tvplus, a-tmask[*,*,0] |
---|
| 47 | ; |
---|
| 48 | ; @history |
---|
[292] | 49 | ; Originaly written by G. Roullet |
---|
[202] | 50 | ; Sebastien Masson (smasson\@lodyc.jussieu.fr) |
---|
| 51 | ; |
---|
[226] | 52 | ; @version |
---|
| 53 | ; $Id$ |
---|
[118] | 54 | ; |
---|
[101] | 55 | ;- |
---|
[327] | 56 | FUNCTION extrapolate, zinput, maskinput, nb_iteration, X_PERIODIC=x_periodic $ |
---|
| 57 | , MINVAL=minval, MAXVAL=maxval, GE0=ge0, _EXTRA=ex |
---|
[59] | 58 | ; |
---|
[125] | 59 | compile_opt idl2, strictarrsubs |
---|
[59] | 60 | ; |
---|
| 61 | ; check the number of iteration used in the extrapolation. |
---|
[271] | 62 | szin = size(zinput) |
---|
| 63 | IF szin[0] NE 2 THEN return, -1. ELSE szin = szin[1:2] |
---|
| 64 | nx = szin[0] |
---|
| 65 | ny = szin[1] |
---|
| 66 | IF n_elements(nb_iteration) EQ 0 THEN nb_iteration = max(szin) |
---|
[59] | 67 | IF nb_iteration EQ 0 THEN return, zinput |
---|
| 68 | ; take care of the boundary conditions... |
---|
[125] | 69 | ; |
---|
[59] | 70 | ; for the x direction, we put 2 additional columns at the left and |
---|
[125] | 71 | ; right side of the array. |
---|
[59] | 72 | ; for the y direction, we put 2 additional lines at the bottom and |
---|
[125] | 73 | ; top side of the array. |
---|
[59] | 74 | ; These changes allow us to use shift function without taking care of |
---|
| 75 | ; the x and y periodicity. |
---|
| 76 | ; |
---|
| 77 | ztmp = bytarr(nx+2, ny+2) |
---|
[202] | 78 | IF n_elements(maskinput) EQ 1 AND maskinput[0] EQ -1 THEN maskinput = replicate(1b, nx, ny) |
---|
[226] | 79 | IF n_elements(maskinput) NE nx*ny THEN BEGIN |
---|
[236] | 80 | ras = report('input grid mask do not have the good size') |
---|
[202] | 81 | return, -1 |
---|
| 82 | ENDIF |
---|
[59] | 83 | ztmp[1:nx, 1:ny] = byte(maskinput) |
---|
| 84 | msk = temporary(ztmp) |
---|
| 85 | ; |
---|
| 86 | ztmp = replicate(1.e20, nx+2, ny+2) |
---|
| 87 | ztmp[1:nx, 1:ny] = zinput |
---|
| 88 | if keyword_set(x_periodic) then begin |
---|
| 89 | ztmp[0, 1:ny] = zinput[nx-1, *] |
---|
| 90 | ztmp[nx+1, 1:ny] = zinput[0, *] |
---|
| 91 | ENDIF |
---|
| 92 | ; remove NaN points if there is some... |
---|
| 93 | nan = where(finite(ztmp) EQ 0, cnt_nan) |
---|
| 94 | IF cnt_nan NE 0 THEN ztmp[temporary(nan)] = 1.e20 |
---|
| 95 | z = temporary(ztmp) |
---|
| 96 | nx2 = nx+2 |
---|
| 97 | ny2 = ny+2 |
---|
| 98 | ;--------------------------------------------------------------- |
---|
| 99 | ;--------------------------------------------------------------- |
---|
[125] | 100 | ; extrapolation |
---|
[59] | 101 | ;--------------------------------------------------------------- |
---|
| 102 | sqrtinv = 1./sqrt(2) |
---|
| 103 | ; |
---|
[69] | 104 | cnt = 1 |
---|
[226] | 105 | ; When we look for the coastline, we don't want to select the |
---|
[59] | 106 | ; borderlines of the array. -> we force the value of the mask for |
---|
| 107 | ; those lines. |
---|
| 108 | msk[0, *] = 1b |
---|
| 109 | msk[nx+1, *] = 1b |
---|
| 110 | msk[*, 0] = 1b |
---|
| 111 | msk[*, ny+1] = 1b |
---|
| 112 | ; find the land points |
---|
| 113 | land = where(msk EQ 0, cnt_land) |
---|
| 114 | ;--------------------------------------------------------------- |
---|
| 115 | WHILE cnt LE nb_iteration AND cnt_land NE 0 DO BEGIN |
---|
| 116 | ;--------------------------------------------------------------- |
---|
[226] | 117 | ; find the coastline points... |
---|
[59] | 118 | ;--------------------------------------------------------------- |
---|
[238] | 119 | ; Once the land points list has been found, we change back the |
---|
[59] | 120 | ; mask values for the boundary conditions. |
---|
| 121 | msk[0, *] = 0b |
---|
| 122 | msk[nx+1, *] = 0b |
---|
| 123 | msk[*, 0] = 0b |
---|
| 124 | msk[*, ny+1] = 0b |
---|
| 125 | if keyword_set(x_periodic) then begin |
---|
| 126 | msk[0, *] = msk[nx, *] |
---|
| 127 | msk[nx+1, *] = msk[1, *] |
---|
| 128 | endif |
---|
| 129 | ; |
---|
[295] | 130 | ; we compute the weighted number of sea neighbors. |
---|
| 131 | ; those 4 neighbors have a weight of 1: |
---|
[125] | 132 | ; * |
---|
| 133 | ; *+* |
---|
| 134 | ; * |
---|
[59] | 135 | ; |
---|
[295] | 136 | ; those 4 neighbors have a weight of 1/sqrt(2): |
---|
[59] | 137 | ; |
---|
| 138 | ; * * |
---|
[125] | 139 | ; + |
---|
[59] | 140 | ; * * |
---|
| 141 | ; |
---|
| 142 | ; As we make sure that none of the land points are located on the |
---|
| 143 | ; border of the array, we can compute the weight without shift |
---|
| 144 | ; (faster). |
---|
| 145 | ; |
---|
| 146 | weight = msk[land+1]+msk[land-1]+msk[land+nx2]+msk[land-nx2] $ |
---|
| 147 | +sqrtinv*(msk[land+nx2+1]+msk[land+nx2-1] $ |
---|
| 148 | +msk[land-nx2+1]+msk[land-nx2-1]) |
---|
[295] | 149 | ; list all the points that have sea neighbors |
---|
[59] | 150 | ok = where(weight GT 0) |
---|
| 151 | ; the coastline points |
---|
| 152 | coast = land[ok] |
---|
[295] | 153 | ; their weighted number of sea neighbors. |
---|
[59] | 154 | weight = weight[temporary(ok)] |
---|
| 155 | ;--------------------------------------------------------------- |
---|
[226] | 156 | ; fill the coastline points |
---|
[59] | 157 | ;--------------------------------------------------------------- |
---|
| 158 | z = temporary(z)*msk |
---|
| 159 | ; |
---|
| 160 | zcoast = z[1+coast]+z[-1+coast]+z[nx2+coast]+z[-nx2+coast] $ |
---|
| 161 | +1./sqrt(2)*(z[nx2+1+coast]+z[nx2-1+coast] $ |
---|
| 162 | +z[-nx2+1+coast]+z[-nx2-1+coast]) |
---|
[125] | 163 | ; |
---|
[199] | 164 | IF keyword_set(ge0) THEN zcoast = 0. > temporary(zcoast) |
---|
[59] | 165 | IF n_elements(minval) NE 0 THEN zcoast = minval > temporary(zcoast) |
---|
| 166 | IF n_elements(maxval) NE 0 THEN zcoast = temporary(zcoast) < maxval |
---|
| 167 | z[coast] = temporary(zcoast)/temporary(weight) |
---|
[238] | 168 | ; we update the boundary conditions of z |
---|
[59] | 169 | if keyword_set(x_periodic) then begin |
---|
| 170 | z[0, *] = z[nx, *] |
---|
| 171 | z[nx+1, *] = z[1, *] |
---|
| 172 | endif |
---|
| 173 | ;--------------------------------------------------------------- |
---|
| 174 | ; we update the mask |
---|
| 175 | ;--------------------------------------------------------------- |
---|
| 176 | msk[temporary(coast)] = 1 |
---|
| 177 | ; |
---|
| 178 | cnt = cnt + 1 |
---|
[125] | 179 | ; When we look for the coast line, we don't want to select the |
---|
[59] | 180 | ; borderlines of the array. -> we force the value of the mask for |
---|
| 181 | ; those lines. |
---|
| 182 | msk[0, *] = 1b |
---|
| 183 | msk[nx+1, *] = 1b |
---|
| 184 | msk[*, 0] = 1b |
---|
| 185 | msk[*, ny+1] = 1b |
---|
| 186 | ; find the land points |
---|
| 187 | land = where(msk EQ 0, cnt_land) |
---|
| 188 | ; |
---|
| 189 | ENDWHILE |
---|
| 190 | ;--------------------------------------------------------------- |
---|
| 191 | ; we return the original size of the array |
---|
| 192 | ;--------------------------------------------------------------- |
---|
[125] | 193 | ; |
---|
[59] | 194 | return, z[1:nx, 1:ny] |
---|
[125] | 195 | END |
---|