source: trunk/SRC/ToBeReviewed/CALCULS/remplit.pro @ 209

Last change on this file since 209 was 209, checked in by smasson, 17 years ago

bugfix + introduce C grid based on F, U and V points

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.2 KB
Line 
1;+
2; @file_comments
3;
4;
5; @categories
6;
7;
8; @param ZINPUT
9;
10;
11; @keyword NAN
12;
13;
14; @keyword NITER
15;
16;
17; @keyword BASIQUE
18;
19;
20; @keyword MASK
21;
22;
23; @keyword FILLXDIR
24;
25;
26; @keyword FILLYDIR
27;
28;
29; @keyword FILLVAL
30;
31;
32; @keyword _EXTRA
33; Used to pass your keywords
34;
35; @returns
36;
37;
38; @uses
39;
40;
41; @restrictions
42;
43;
44; @examples
45;
46;
47; @history
48;
49;
50; @version
51; $Id$
52;
53  ;;
54  ;; Extrapole zinout[jpi,jpj] sur les continents en utilisant les 4
55  ;; plus proches voisins masques oceaniquement et construit un nouveau
56;   masque
57  ;; contenant l'ancien masque oceanique PLUSles points extrapoles.
58  ;; Reitere le processus niter fois.
59  ;; C'est pas clair, essayez !
60  ;;
61  ;;
62;
63;    /Nan: to fill the point which have the value
64;    !values.f_nan. Whitout this keyword, these point are not filling
65;    and stays at !values.f_nan.
66;
67;
68; @todo seb
69;
70;-
71FUNCTION remplit, zinput, NAN = nan, NITER = niter, BASIQUE = basique, mask = mask, FILLXDIR = fillxdir, FILLYDIR = fillydir, FILLVAL = fillval, _extra = ex
72;
73  compile_opt idl2, strictarrsubs
74;
75@common
76  tempsun = systime(1)          ; pour key_performance
77; les points non remplis sont masques a valmask
78  IF n_elements(niter) EQ 0 THEN niter = 1
79  IF niter EQ 0 THEN return, zinput
80  z = zinput
81  if n_elements(key_gridtype) EQ 0 then key_gridtype = 'c'
82  oldkey_gridtype = key_gridtype
83; keep only the first letter of the grid type
84  key_gridtype = strmid(key_gridtype, 0, 1)
85  if keyword_set(basique) then begin
86    key_gridtype = 'c'
87    nx = (size(zinput))[1]
88    ny = (size(zinput))[2]
89    if NOT keyword_set(mask) then mmmask = basique ELSE mmmask = mask
90  ENDIF ELSE grille, mmmask, glam, gphi, gdep, nx, ny, nz, _extra = ex
91  if keyword_set(mask) then mmmask = mask
92;---------------------------------------------------------------
93  if (size(mmmask))[0] EQ 3 THEN mmmask = mmmask[*, *, 0]
94;
95  if n_elements(mmmask) EQ 1 then mmmask = replicate(1b, nx, ny)
96  if keyword_set(nan) then begin
97    nanpoint = where(finite(z) EQ 0)
98    if nanpoint[0] NE -1 then begin
99      mmmask[nanpoint] = 0b
100      z[nanpoint] = 0
101    endif
102  ENDIF
103  mmmask = byte(mmmask)
104;---------------------------------------------------------------
105; on ajoute un cadre de zero a z, mask, e1, e2
106; comme ca apres on peut faire des shifts ds tous les sens sans se
107; soucier des bords du domaine!
108;---------------------------------------------------------------
109  tempdeux = systime(1)         ; pour key_performance =2
110  nx2 = nx+2
111  case key_gridtype of
112    'c':BEGIN
113      ztmp = bytarr(nx+2, ny+2)
114      ztmp[1:nx, 1:ny] = mmmask
115      mmmask = temporary(ztmp)
116      ztmp = fltarr(nx+2, ny+2)
117      ztmp[1:nx, 1:ny] = z
118      if keyword_set(key_periodic) AND nx EQ jpi then begin
119        ztmp[0, 1:ny] = z[jpi-1, *]
120        ztmp[nx+1, 1:ny] = z[0, *]
121      endif
122      z = temporary(ztmp)
123    END
124    'e':BEGIN
125      ztmp = bytarr(nx+2, ny+4)
126      ztmp[1:nx, 2:ny+1] = mmmask
127      mmmask = temporary(ztmp)
128      ztmp = fltarr(nx+2, ny+4)
129      ztmp[1:nx, 2:ny+1] = z
130      if keyword_set(key_periodic) AND nx EQ jpi then begin
131        ztmp[0, 2:ny+1] = z[jpi-1, *]
132        ztmp[nx+1, 2:ny+1] = z[0, *]
133      endif
134      z = temporary(ztmp)
135    END
136  endcase
137  IF testvar(var = key_performance) EQ 2 THEN $
138    print, 'temps remplit: on ajoute un cadre de zero ', systime(1)-tempdeux
139;---------------------------------------------------------------
140;---------------------------------------------------------------
141; iteration
142;---------------------------------------------------------------
143;---------------------------------------------------------------
144  FOR n = 1, niter DO BEGIN
145; on trouve les points coast
146    tempdeux = systime(1)       ; pour key_performance =2
147; les points du bord du cadre ne doivent pas etre selectionnes comme
148; la coast
149    case key_gridtype of
150      'c':BEGIN
151        mmmask[0, *] = 1b
152        mmmask[nx+1, *] = 1b
153        mmmask[*, 0] = 1b
154        mmmask[*, ny+1] = 1b
155      END
156      'e':BEGIN
157        mmmask[0, *] = 1b
158        mmmask[nx+1, *] = 1b
159        mmmask[*, 0:1] = 1b
160        mmmask[*, ny+2:ny+3] = 1b
161      END
162    endcase
163; liste des points terre restant
164    IF keyword_set(fillxdir) THEN BEGIN
165; we stop if all the lines, that contains data, have been filled
166      test = total(mmmask[1:nx, *], 1)
167      IF total((test EQ 0)+(test EQ nx)) EQ ny+2 THEN GOTO, fini
168    ENDIF
169    IF keyword_set(fillydir) THEN BEGIN
170; we stop if all the columns, that contains data, have been filled
171      test = total(mmmask[*, 1:ny], 2)
172      IF total((test EQ 0)+(test EQ ny)) EQ nx+2 THEN GOTO, fini
173    ENDIF
174    land = where(mmmask EQ 0)
175    if land[0] EQ -1 then GOTO, fini
176; les points du bord du cadre doivent maintenant etre dans la terre
177    case key_gridtype of
178      'c':BEGIN
179        mmmask[0, *] = 0b
180        mmmask[nx+1, *] = 0b
181        mmmask[*, 0] = 0b
182        mmmask[*, ny+1] = 0b
183      END
184      'e':BEGIN
185        mmmask[0, *] = 0b
186        mmmask[nx+1, *] = 0b
187        mmmask[*, 0:1] = 0b
188        mmmask[*, ny+2:ny+3] = 0b
189      END
190    endcase
191    if keyword_set(key_periodic) AND nx EQ jpi then begin
192      mmmask[0, *] = mmmask[nx, *]
193      mmmask[nx+1, *] = mmmask[1, *]
194    endif
195; liste des voisins mer
196    case key_gridtype of
197      'c':BEGIN
198        CASE 1 OF
199          keyword_set(fillxdir):weight = mmmask[1+land]+mmmask[-1+land]
200          keyword_set(fillydir):weight = mmmask[nx2+land]+mmmask[-nx2+land]
201          ELSE:weight = mmmask[1+land]+mmmask[-1+land]+mmmask[nx2+land]+mmmask[-nx2+land] $
202            +1./sqrt(2)*(mmmask[nx2+1+land]+mmmask[nx2-1+land] $
203                         +mmmask[-nx2+1+land]+mmmask[-nx2-1+land])
204        ENDCASE
205      END
206      'e':BEGIN
207        shifted = glam[0, 0] LT glam[0, 1]
208        oddeven = (land/nx2+1-shifted) MOD 2
209        weight = mmmask[1+land]+mmmask[-1+land] $
210          +mmmask[2*nx2+land]+mmmask[-2*nx2+land] $
211          +sqrt(2)*(mmmask[-nx2+oddeven+land]+mmmask[-nx2-1+oddeven+land] $
212                    +mmmask[nx2+oddeven+land]+mmmask[nx2-1+oddeven+land])
213      END
214    endcase
215
216    ok = where(weight GT 0)
217    weight = weight[ok]
218    coast = land[temporary(ok)]
219;
220    IF testvar(var = key_performance) EQ 2 THEN $
221      print, 'temps remplit: trouver la coast ', systime(1)-tempdeux
222;---------------------------------------------------------------
223; remplissage des points coast
224;---------------------------------------------------------------
225    tempdeux = systime(1)       ; pour key_performance =2
226; on masque z
227    z = temporary(z)*mmmask
228;
229    case key_gridtype of
230      'c':BEGIN
231        CASE 1 OF
232          keyword_set(fillxdir):zcoast = z[1+coast]+z[-1+coast]
233          keyword_set(fillydir):zcoast = z[nx2+coast]+z[-nx2+coast]
234          ELSE:zcoast = z[1+coast]+z[-1+coast]+z[nx2+coast]+z[-nx2+coast] $
235            +1./sqrt(2)*(z[nx2+1+coast]+z[nx2-1+coast] $
236                         +z[-nx2+1+coast]+z[-nx2-1+coast])
237        ENDCASE
238      END
239      'e':BEGIN
240        oddeven = (coast/nx2+1-shifted) MOD 2
241        zcoast = z[1+coast]+z[-1+coast]+z[2*nx2+coast]+z[-2*nx2+coast] $
242          +sqrt(2)*(z[-nx2+oddeven+coast]+z[-nx2-1+oddeven+coast] $
243                    +z[nx2+oddeven+coast]+z[nx2-1+oddeven+coast])
244      END
245    endcase
246;   
247    z[coast] =  temporary(zcoast)/ temporary(weight)
248; we update the the boundary conditions of z
249    if keyword_set(key_periodic) AND nx EQ jpi then begin
250      z[0, *] = z[nx, *]
251      z[nx+1, *] = z[1, *]
252    endif
253;---------------------------------------------------------------
254; IV) on reduit le masque
255;---------------------------------------------------------------
256    mmmask[ temporary(coast)] = 1
257;
258    IF testvar(var = key_performance) EQ 2 THEN $
259      print, 'temps remplit: une iteration ', systime(1)-tempdeux
260  ENDFOR
261fini:
262;
263; on masque les valeurs sur les lands restantes
264;
265  IF n_elements(valmask) EQ 0 then valmask = 1e20
266  IF n_elements(fillval) EQ 0 THEN fillval = valmask
267  z = temporary(z)*mmmask + fillval*(1b-mmmask)
268;---------------------------------------------------------------
269; on redecoupe le tableau pour retirer le cadre!
270;---------------------------------------------------------------
271  case key_gridtype of
272    'c':BEGIN
273      z = z[1:nx, 1:ny]
274    END
275    'e':BEGIN
276      z = z[1:nx, 2:ny+1]
277    END
278  endcase
279;
280  key_gridtype = oldkey_gridtype
281;---------------------------------------------------------------
282  if keyword_set(key_performance) THEN print, 'temps remplit', systime(1)-tempsun
283  return, z
284END
285
Note: See TracBrowser for help on using the repository browser.