[202] | 1 | ;+ |
---|
| 2 | ; @file_comments |
---|
| 3 | ; similar to extrapolate but could to the job in a better way because the |
---|
| 4 | ; extrapolated values are smoothed... takes more time than extrapolate. |
---|
| 5 | ; extrapolate data where maskinput eq 0 by filling |
---|
| 6 | ; step by step the coastline points with the mean value of the 8 neighbourgs. |
---|
| 7 | ; |
---|
| 8 | ; @categories |
---|
| 9 | ; Interpolation |
---|
| 10 | ; |
---|
| 11 | ; @param in {in}{required}{type=2d array} |
---|
| 12 | ; data to be extrapolate |
---|
| 13 | ; |
---|
| 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 |
---|
| 17 | ; |
---|
| 18 | ; @keyword MINVAL {type=scalar}{default=not used} |
---|
| 19 | ; to specify a minimum value to the extrapolated values |
---|
| 20 | ; |
---|
| 21 | ; @keyword MAXVAL {type=scalar}{default=not used} |
---|
| 22 | ; to specify a maximum value to the extrapolated values |
---|
| 23 | ; |
---|
| 24 | ; @keyword GE0 {type=scalar 0 or 1}{default=0} |
---|
| 25 | ; put 1 to force the extrapolated values to be larger than 0, same as using minval=0. |
---|
| 26 | ; |
---|
| 27 | ; @returns |
---|
| 28 | ; the extrapolated array with no more masked values |
---|
| 29 | ; |
---|
| 30 | ; @restrictions |
---|
| 31 | ; You cannot specify the number of iterations done in the extrapolation process |
---|
| 32 | ; |
---|
| 33 | ; @examples |
---|
| 34 | ; IDL> a=extrapsmooth(dist(jpi,jpj),tmask[*,*,0],/x_periodic) |
---|
| 35 | ; IDL> tvplus, a |
---|
| 36 | ; compare to extrapolate result: |
---|
| 37 | ; IDL> b=extrapolate(dist(jpi,jpj),tmask[*,*,0],/x_periodic) |
---|
| 38 | ; IDL> tvplus, b, window = 1 |
---|
| 39 | ; |
---|
| 40 | ; @history |
---|
| 41 | ; January 2007: Sebastien Masson (smasson\@lodyc.jussieu.fr) |
---|
| 42 | ; |
---|
| 43 | ; @version |
---|
| 44 | ; $Id$ |
---|
| 45 | ;- |
---|
[231] | 46 | ; |
---|
[202] | 47 | FUNCTION extrapsmooth, in, mskin, x_periodic = x_periodic, MINVAL = minval, MAXVAL = maxval, GE0 = ge0 |
---|
| 48 | ; |
---|
[226] | 49 | compile_opt strictarr, strictarrsubs |
---|
| 50 | ; |
---|
| 51 | sz = size(reform(in)) |
---|
| 52 | IF sz[0] NE 2 THEN BEGIN |
---|
[202] | 53 | print, 'Input arrays must have 2 dimensions' |
---|
| 54 | return, -1 |
---|
| 55 | ENDIF |
---|
| 56 | nx = sz[1] |
---|
| 57 | ny = sz[2] |
---|
| 58 | IF n_elements(mskin) EQ 1 AND mskin[0] EQ -1 THEN mskin = replicate(1b, nx, ny) |
---|
[226] | 59 | IF n_elements(mskin) NE nx*ny THEN BEGIN |
---|
[202] | 60 | print, 'input grid mask do not have the good size' |
---|
| 61 | return, -1 |
---|
| 62 | ENDIF |
---|
| 63 | ; |
---|
| 64 | out = reform(in) |
---|
| 65 | whmsk = where(mskin EQ 0, nbr) |
---|
| 66 | IF nbr NE 0 THEN out[temporary(whmsk)] = !values.f_nan |
---|
[226] | 67 | ; add values on each side of the array to avoid boundary effects |
---|
[202] | 68 | nx2 = nx/2 |
---|
| 69 | ny2 = ny/2 |
---|
| 70 | add = replicate(!values.f_nan, nx, ny2) |
---|
| 71 | out = [[add], [temporary(out)], [add]] |
---|
| 72 | IF keyword_set(x_periodic) THEN BEGIN |
---|
| 73 | add1 = out[0:nx2, *] |
---|
| 74 | add2 = out[nx-nx2:nx-1, *] |
---|
| 75 | out = [add2, [temporary(out)], add1] |
---|
[226] | 76 | ENDIF ELSE BEGIN |
---|
[202] | 77 | add = replicate(!values.f_nan, nx2, ny+2*ny2) |
---|
| 78 | out = [add, [temporary(out)], add] |
---|
| 79 | ENDELSE |
---|
| 80 | ; |
---|
| 81 | msk0 = where(finite(out) EQ 0) |
---|
| 82 | nnan = total(finite(out, /nan)) |
---|
| 83 | i = 1 |
---|
| 84 | WHILE nnan NE 0 DO BEGIN |
---|
| 85 | tmp = smooth(out, 2*i + 1, /nan) |
---|
| 86 | ; find only the changed values that where on land |
---|
| 87 | new = where(finite(out) EQ 0 AND finite(tmp) EQ 1, nnew) |
---|
| 88 | IF nnew EQ 0 then nnan = 0 ELSE BEGIN |
---|
| 89 | IF keyword_set(ge0) THEN tmp = 0. > temporary(tmp) |
---|
| 90 | IF n_elements(minval) NE 0 THEN tmp = minval > temporary(tmp) |
---|
| 91 | IF n_elements(maxval) NE 0 THEN tmp = temporary(tmp) < maxval |
---|
| 92 | out[new] = tmp[new] |
---|
| 93 | i = i+1 |
---|
| 94 | nnan = total(finite(out, /nan)) |
---|
| 95 | ENDELSE |
---|
| 96 | ENDWHILE |
---|
| 97 | ; a final smooth is needed |
---|
| 98 | out = smooth(temporary(out), 5, /nan) |
---|
| 99 | IF keyword_set(ge0) THEN out = 0. > temporary(out) |
---|
| 100 | IF n_elements(minval) NE 0 THEN out = minval > temporary(out) |
---|
| 101 | IF n_elements(maxval) NE 0 THEN out = temporary(out) < maxval |
---|
| 102 | ; get back to the original domain |
---|
| 103 | out = (temporary(out))[nx2:nx+nx2-1, ny2:ny+ny2-1] |
---|
| 104 | ; put back the non-maskqed values |
---|
| 105 | whmsk = where(mskin NE 0) |
---|
| 106 | out[whmsk] = in[whmsk] |
---|
| 107 | ; |
---|
| 108 | return, out |
---|
| 109 | END |
---|