1 | FUNCTION extrapolate, zinput, maskinput, nb_iteration, x_periodic = x_periodic, MINVAL = minval, MAXVAL = maxval |
---|
2 | ; |
---|
3 | compile_opt strictarr, strictarrsubs |
---|
4 | ; |
---|
5 | ; extrapolate data (zinput) where maskinput eq 0 by filling step by |
---|
6 | ; step the coastline points with the mean value of the 8 neighbourgs. |
---|
7 | ; |
---|
8 | ; check the number of iteration used in the extrapolation. |
---|
9 | IF n_elements(nb_iteration) EQ 0 THEN nb_iteration = 10.E20 |
---|
10 | IF nb_iteration EQ 0 THEN return, zinput |
---|
11 | nx = (size(zinput))[1] |
---|
12 | ny = (size(zinput))[2] |
---|
13 | ; take care of the boundary conditions... |
---|
14 | ; |
---|
15 | ; for the x direction, we put 2 additional columns at the left and |
---|
16 | ; right side of the array. |
---|
17 | ; for the y direction, we put 2 additional lines at the bottom and |
---|
18 | ; top side of the array. |
---|
19 | ; These changes allow us to use shift function without taking care of |
---|
20 | ; the x and y periodicity. |
---|
21 | ; |
---|
22 | ztmp = bytarr(nx+2, ny+2) |
---|
23 | ztmp[1:nx, 1:ny] = byte(maskinput) |
---|
24 | msk = temporary(ztmp) |
---|
25 | ; |
---|
26 | ztmp = replicate(1.e20, nx+2, ny+2) |
---|
27 | ztmp[1:nx, 1:ny] = zinput |
---|
28 | if keyword_set(x_periodic) then begin |
---|
29 | ztmp[0, 1:ny] = zinput[nx-1, *] |
---|
30 | ztmp[nx+1, 1:ny] = zinput[0, *] |
---|
31 | ENDIF |
---|
32 | ; remove NaN points if there is some... |
---|
33 | nan = where(finite(ztmp) EQ 0, cnt_nan) |
---|
34 | IF cnt_nan NE 0 THEN ztmp[temporary(nan)] = 1.e20 |
---|
35 | z = temporary(ztmp) |
---|
36 | nx2 = nx+2 |
---|
37 | ny2 = ny+2 |
---|
38 | ;--------------------------------------------------------------- |
---|
39 | ;--------------------------------------------------------------- |
---|
40 | ; extrapolation |
---|
41 | ;--------------------------------------------------------------- |
---|
42 | sqrtinv = 1./sqrt(2) |
---|
43 | ; |
---|
44 | cnt = 1 |
---|
45 | ; When we look for the coast line, we don't whant to select the |
---|
46 | ; borderlines of the array. -> we force the value of the mask for |
---|
47 | ; those lines. |
---|
48 | msk[0, *] = 1b |
---|
49 | msk[nx+1, *] = 1b |
---|
50 | msk[*, 0] = 1b |
---|
51 | msk[*, ny+1] = 1b |
---|
52 | ; find the land points |
---|
53 | land = where(msk EQ 0, cnt_land) |
---|
54 | ;--------------------------------------------------------------- |
---|
55 | WHILE cnt LE nb_iteration AND cnt_land NE 0 DO BEGIN |
---|
56 | ;--------------------------------------------------------------- |
---|
57 | ; find the coast line points... |
---|
58 | ;--------------------------------------------------------------- |
---|
59 | ; Once the land points list has been found, we change back the the |
---|
60 | ; mask values for the boundary conditions. |
---|
61 | msk[0, *] = 0b |
---|
62 | msk[nx+1, *] = 0b |
---|
63 | msk[*, 0] = 0b |
---|
64 | msk[*, ny+1] = 0b |
---|
65 | if keyword_set(x_periodic) then begin |
---|
66 | msk[0, *] = msk[nx, *] |
---|
67 | msk[nx+1, *] = msk[1, *] |
---|
68 | endif |
---|
69 | ; |
---|
70 | ; we compute the weighted number of sea neighbourgs. |
---|
71 | ; those 4 neighbours have a weight of 1: |
---|
72 | ; * |
---|
73 | ; *+* |
---|
74 | ; * |
---|
75 | ; |
---|
76 | ; those 4 neighbours have a weight of 1/sqrt(2): |
---|
77 | ; |
---|
78 | ; * * |
---|
79 | ; + |
---|
80 | ; * * |
---|
81 | ; |
---|
82 | ; As we make sure that none of the land points are located on the |
---|
83 | ; border of the array, we can compute the weight without shift |
---|
84 | ; (faster). |
---|
85 | ; |
---|
86 | weight = msk[land+1]+msk[land-1]+msk[land+nx2]+msk[land-nx2] $ |
---|
87 | +sqrtinv*(msk[land+nx2+1]+msk[land+nx2-1] $ |
---|
88 | +msk[land-nx2+1]+msk[land-nx2-1]) |
---|
89 | ; list all the points that have sea neighbourgs |
---|
90 | ok = where(weight GT 0) |
---|
91 | ; the coastline points |
---|
92 | coast = land[ok] |
---|
93 | ; their weighted number of sea neighbourgs. |
---|
94 | weight = weight[temporary(ok)] |
---|
95 | ;--------------------------------------------------------------- |
---|
96 | ; fill the coastine points |
---|
97 | ;--------------------------------------------------------------- |
---|
98 | z = temporary(z)*msk |
---|
99 | ; |
---|
100 | zcoast = z[1+coast]+z[-1+coast]+z[nx2+coast]+z[-nx2+coast] $ |
---|
101 | +1./sqrt(2)*(z[nx2+1+coast]+z[nx2-1+coast] $ |
---|
102 | +z[-nx2+1+coast]+z[-nx2-1+coast]) |
---|
103 | ; |
---|
104 | IF n_elements(minval) NE 0 THEN zcoast = minval > temporary(zcoast) |
---|
105 | IF n_elements(maxval) NE 0 THEN zcoast = temporary(zcoast) < maxval |
---|
106 | z[coast] = temporary(zcoast)/temporary(weight) |
---|
107 | ; we update the the boundary conditions of z |
---|
108 | if keyword_set(x_periodic) then begin |
---|
109 | z[0, *] = z[nx, *] |
---|
110 | z[nx+1, *] = z[1, *] |
---|
111 | endif |
---|
112 | ;--------------------------------------------------------------- |
---|
113 | ; we update the mask |
---|
114 | ;--------------------------------------------------------------- |
---|
115 | msk[temporary(coast)] = 1 |
---|
116 | ; |
---|
117 | cnt = cnt + 1 |
---|
118 | ; When we look for the coast line, we don't whant to select the |
---|
119 | ; borderlines of the array. -> we force the value of the mask for |
---|
120 | ; those lines. |
---|
121 | msk[0, *] = 1b |
---|
122 | msk[nx+1, *] = 1b |
---|
123 | msk[*, 0] = 1b |
---|
124 | msk[*, ny+1] = 1b |
---|
125 | ; find the land points |
---|
126 | land = where(msk EQ 0, cnt_land) |
---|
127 | ; |
---|
128 | ENDWHILE |
---|
129 | ;--------------------------------------------------------------- |
---|
130 | ; we return the original size of the array |
---|
131 | ;--------------------------------------------------------------- |
---|
132 | |
---|
133 | return, z[1:nx, 1:ny] |
---|
134 | END |
---|
135 | |
---|