source: trunk/SRC/ToBeReviewed/CALCULS/moyenne.pro @ 440

Last change on this file since 440 was 440, checked in by smasson, 13 years ago

take into account the ssh in moyenne and grossemoyenne

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 24.8 KB
Line 
1;+
2;
3; @file_comments
4; averages a 2- or 3-d field over a selected
5;           geographical area and along one or more
6;           selected axes (x, y or z)
7;
8; @categories
9;
10; @param TAB {in}{required}
11; 2 or 3d field
12;
13; @param DIREC {in}{required}
14; 'x' 'y' 'z' 'xy' 'xz' 'yz' or 'xyz'
15;
16; @keyword BOXZOOM
17; [xmin,xmax,ymin,ymax (,(zmin,)zmax)] to more details, see <pro>domdef</pro>
18; boxzoom can have 5 forms:
19;  [vert2],
20;  [vert1, vert2],
21;  [lon1, lon2, lat1, lat2],
22;  [lon1, lon2, lat1, lat2, vert2],
23;  [lon1, lon2, lat1, lat2, vert1,vert2]
24;
25; @keyword KEEPBOTTOM {default=0}{type=scalar: 0 or 1}
26; used only with partial steps (key_partialstep /= 0). In partial
27; steps, bottom points are not located at the same depth => they
28; should not be averaged together along x and/or y direction if there
29; is no average along z. In this case bottom ponts are set to
30;!values.f_nan before doing any call to total.
31;
32; @keyword NAN
33; not a number, we activate it if we want to average without considerate some masked values of TAB.
34; If masked values of TAB are values consecrated by IDL(!values.f_nan), we just have to put NAN.
35; If masked values of TAB are valued a (a must be different of 1, corresponding to nan =
36; !values.f_nan and of 0, which desactivate nan). We have to put NAN=a.
37; Comment: In output, result points which are NAN will be valued a or !values.f_nan.
38;
39; @keyword NODOMDEF
40; We activate it if we do not want to pass in <pro>domdef</pro> even if the
41; keyword boxzoom is present (like when <pro>grossemoyenne</pro> is called
42; via <pro>checkfield</pro>)
43;
44; @keyword INTEGRATION
45; To make an integral rather than an average
46;
47; @keyword SSH
48; array giving the sea level elenation (in meter) that must be added
49; to the thickness of the first level when computing vertical mean
50;
51; @keyword WDEPTH
52; to specify that the field is at W depth instead of T
53; depth (automatically activated if vargrid eq 'W')
54;
55; @returns
56; An array
57;
58; @uses
59; <pro>common</pro>
60; <pro>domdef</pro>
61;
62; @restrictions
63; Put values corresponding to land at 1.e20
64;
65; @history
66;  Jerome Vialard (jv\@lodyc.jussieu.fr)
67;                       2/7/98
68;                       Sebastien Masson (smasson\@lodyc.jussieu.fr)
69;                       14/8/98
70;                       15/1/98
71;                       11/3/99 adaptation for NAN
72;                       28/7/99 Averages 1d arrays
73;
74; @version
75; $Id$
76;
77;-
78FUNCTION moyenne, tab, direc, BOXZOOM=boxzoom, INTEGRATION=integration, KEEPBOTTOM = keepbottom $
79                , NAN=nan, NODOMDEF=nodomdef, SSH = ssh, WDEPTH=wdepth, _EXTRA=ex
80;
81  compile_opt idl2, strictarrsubs
82;
83@cm_4mesh
84@cm_4data
85@cm_4cal
86  IF NOT keyword_set(key_forgetold) THEN BEGIN
87@updatenew
88@updatekwd
89  ENDIF
90;---------------------------------------------------------
91  tempsun = systime(1)          ; To key_performance
92;------------------------------------------------------------
93;I) preliminaries
94;------------------------------------------------------------
95  dirt = 0
96  dirx = 0
97  diry = 0
98  dirz = 0
99;------------------------------------------------------------
100; I.1) Directions(s) we follow to integrate
101;------------------------------------------------------------
102  if ( strpos(direc, 't') ge 0 ) then dirt = 1
103  if ( strpos(direc, 'x') ge 0 ) then dirx = 1
104  if ( strpos(direc, 'y') ge 0 ) then diry = 1
105  if ( strpos(direc, 'z') ge 0 ) then dirz = 1
106  if (dirx eq 0 and diry eq 0 and dirz eq 0) then return, tab
107;------------------------------------------------------------
108; I.2) verification of the input array's size
109;------------------------------------------------------------
110  taille = size(tab)
111  case 1 of
112    taille[0] eq 1 :dim = '1d'
113    taille[0] eq 2 :BEGIN
114      dim = '2d'
115      if dirx eq 0 and diry eq 0 then return, tab
116    END
117    taille[0] eq 3 :BEGIN
118      dim = '3d'
119      if dirx eq 0 and diry eq 0 and dirz eq 0 then return, tab
120    END
121    else : return, report('Array must have 2 or 3 dimensions if there is a time dimension use grossemoyenne')
122  endcase
123;------------------------------------------------------------
124;   I.3) Obtainment of scale's factors and of the mask on the subdomain concernedby the average.
125; Redefinition of the domain ajusted at boxzoom (at 6 elements)
126; This will allowed us to calculate only in the domain concerned by the average.
127; Domdef, followed by grid give us all arrays of the grid on the subdomain
128;------------------------------------------------------------
129  if keyword_set(boxzoom) then BEGIN
130    Case 1 Of
131      N_Elements(Boxzoom) Eq 1:bte = [lon1, lon2, lat1, lat2, 0., boxzoom[0]]
132      N_Elements(Boxzoom) Eq 2:bte = [lon1, lon2, lat1, lat2, boxzoom[0], boxzoom[1]]
133      N_Elements(Boxzoom) Eq 4:bte = [Boxzoom, vert1, vert2]
134      N_Elements(Boxzoom) Eq 5:bte = [Boxzoom[0:3], 0, Boxzoom[4]]
135      N_Elements(Boxzoom) Eq 6:bte = Boxzoom
136      Else: return, report('Bad definition of Boxzoom')
137    endcase
138    if NOT keyword_set(nodomdef) then BEGIN
139      savedbox = 1b
140      saveboxparam, 'boxparam4moyenne.dat'
141      domdef, bte, GRIDTYPE = vargrid, _extra = ex
142    ENDIF
143  ENDIF
144;---------------------------------------------------------------
145; attribution of the mask and of longitude and latitude arrays...
146;---------------------------------------------------------------
147  IF vargrid EQ 'W' THEN wdepth = 1
148  grille, mask, glam, gphi, gdep, nx, ny, nz, firstx, firsty, firstz, lastx, lasty, lastz, e1, e2, e3, WDEPTH = wdepth
149;---------------------------------------------------------------
150; check the use of ssh keyword...
151;---------------------------------------------------------------
152  IF keyword_set(ssh) AND dirz NE 0 AND firstz EQ 0 AND taille[0] GE 2 THEN BEGIN
153    szssh = size(ssh)
154    IF szssh[1] NE taille[1] OR szssh[2] NE taille[2] THEN $
155       return, report('ssh and input arrays must have the same size')
156    sshh = ssh
157  ENDIF
158;------------------------------------------------------------
159;------------------------------------------------------------
160; II) Case of the 1d array
161;------------------------------------------------------------
162;------------------------------------------------------------
163  if dim EQ '1d' then BEGIN
164    if n_elements(tab) NE nx*ny AND n_elements(tab) NE nx*ny*nz then BEGIN
165      if keyword_set(savedbox) THEN restoreboxparam, 'boxparam4moyenne.dat'
166      return, report('Probleme d''adequation entre les tailles du domaine et de la boxzoom.')
167    ENDIF
168    case 1 of
169      nx EQ 1 AND ny EQ 1:BEGIN ;vector following z
170        case n_elements(tab) of
171          jpk:res = tab[firstz:lastz]
172          nz:res = tab
173          ELSE:BEGIN
174            if keyword_set(savedbox) THEN restoreboxparam, 'boxparam4moyenne.dat'
175            return, report('Probleme d''adequation entre les tailles du domaine et de la boxzoom.')
176          END
177        ENDCASE
178        if dirz EQ 1 then BEGIN
179          dim = '3d'
180          taille = size(reform(res, nx, ny, nz))
181        ENDIF ELSE BEGIN
182          if keyword_set(savedbox) THEN restoreboxparam, 'boxparam4moyenne.dat'
183          return, res
184        ENDELSE
185      END
186      ny EQ 1:BEGIN             ;vector following x
187        case n_elements(tab) of
188          jpi:res = tab[firstx:lastx]
189          nx:res = tab
190          ELSE:BEGIN
191            if keyword_set(savedbox) THEN restoreboxparam, 'boxparam4moyenne.dat'
192            return, report('Probleme d''adequation entre les tailles du domaine et de la boxzoom.')
193          END
194        ENDCASE
195        if dirx EQ 1 then BEGIN
196          dim = '2d'
197          taille = size(reform(res, nx, ny))
198        ENDIF ELSE BEGIN
199          if keyword_set(savedbox) THEN restoreboxparam, 'boxparam4moyenne.dat'
200          return, res
201        ENDELSE
202      END
203      nx EQ 1:BEGIN             ;vector following y
204        case n_elements(tab) of
205          jpj:res = tab[firsty:lasty]
206          ny:res = tab
207          ELSE:BEGIN
208            if keyword_set(savedbox) THEN restoreboxparam, 'boxparam4moyenne.dat'
209            return, report('Probleme d''adequation entre les tailles du domaine et de la boxzoom.')
210          END
211        ENDCASE
212        if diry EQ 1 then BEGIN
213          dim = '2d'
214          taille = size(reform(res, nx, ny))
215        ENDIF ELSE BEGIN
216          if keyword_set(savedbox) THEN restoreboxparam, 'boxparam4moyenne.dat'
217          return, res
218        ENDELSE
219      END
220    endcase
221  endif
222;------------------------------------------------------------
223;------------------------------------------------------------
224; II) Case of the 2d array
225;------------------------------------------------------------
226;------------------------------------------------------------
227  if (dim eq '2d') then begin
228;---------------------------------------------------------------
229;   II.1) verification of the coherence of the array's size to average
230; Verification of the coherence between the array's size and the domain defined by domdef
231; The input array must have either the total domain's size (jpi,jpj) or this
232; one of the reduced domain (nx,ny)
233;---------------------------------------------------------------
234    case 1 of
235      taille[1] eq jpi and taille[2] eq jpj: $
236        res = tab[firstx:lastx, firsty:lasty]
237      taille[1] eq  nx and taille[2] eq  ny:res = tab
238      else:BEGIN
239        if keyword_set(savedbox) THEN restoreboxparam, 'boxparam4moyenne.dat'
240        return, report('Probleme d''adequation entre les tailles du domaine nx*ny '+strtrim(nx, 1)+'*'+strtrim(ny, 1)+' et du tableau '+strtrim(taille[1], 1)+'*'+strtrim(taille[2], 1))
241      END
242    ENDCASE
243    if keyword_set(nan) NE 0 then BEGIN
244      if nan NE 1 then BEGIN    ; If nan is not !values.f_nan
245; we put it at !values.f_nan
246        if abs(nan) LT 1e6 then notanumber = where(res EQ nan) $
247        ELSE notanumber = where(abs(res) GT abs(nan)/10.)
248        if notanumber[0] NE -1 then res[temporary(notanumber)] = !values.f_nan
249      ENDIF
250    ENDIF
251;---------------------------------------------------------------
252; Comment : WE HAVE TO BE CAREFUL ABOUT CASES WHERE THE DIMENSION TO AVERAGE = 1,
253; AND MAKE SURE THAT IT EXIST. THAT IS WHY WE USE reform(...,nx,ny,...) WHICH CAN
254; LOOK USELESS AT THE BEGINNING
255;---------------------------------------------------------------
256    if nx EQ 1 OR ny EQ 1 then BEGIN
257      res = reform(res, nx, ny, /over)
258      e1 =  reform(e1, nx, ny, /over)
259      e2 =  reform(e2, nx, ny, /over)
260    endif
261    if nx EQ 1 OR ny EQ 1 OR nz EQ 1 THEN $
262      mask =  reform(mask, nx, ny, nz, /over)
263;---------------------------------------------------------------
264; II.3) Different types of average
265;---------------------------------------------------------------
266    mask = mask[*, *, 0]
267    if keyword_set(nan) NE 0 then msknan = finite(res) ELSE msknan = -1
268    case 1 of
269      (dirx eq 1) and (diry eq 0) : begin
270        e = e1*mask
271        if keyword_set(integration) then divi = 1 $
272        else begin
273          divi = e
274          IF msknan[0] NE -1 THEN divi = temporary(divi)*msknan
275          if ny EQ 1 then divi = reform(divi, nx, ny, /over)
276          divi = total(divi, 1)
277        endelse
278        res = res*e
279        if ny EQ 1 then res = reform(res, nx, ny, /over)
280        res = total(res, 1, nan = nan)/(divi > 1.)
281        if msknan[0] NE -1 then begin
282          testnan  = msknan*mask
283          if ny EQ 1 then testnan = reform(testnan, nx, ny, /over)
284          testnan = total(testnan, 1)+(total(mask, 1) EQ 0)
285        endif
286      end
287      (dirx eq 0) and (diry eq 1) : begin
288        e = e2*mask
289        if keyword_set(integration) then divi = 1 $
290        else begin
291          divi = e
292          IF msknan[0] NE -1 THEN divi = temporary(divi)*msknan
293          if ny EQ 1 then divi = reform(divi, nx, ny, /over)
294          divi = total(divi, 2)
295        endelse
296        res = res*e
297        if ny EQ 1 then res = reform(res, nx, ny, /over)
298        res = total(res, 2, nan = nan)/(divi > 1.)
299        if msknan[0] NE -1 then begin
300          testnan  = msknan*mask
301          if ny EQ 1 then testnan  = reform(testnan, nx, ny, /over)
302          testnan = total(testnan, 2)+(total(mask, 2) EQ 0)
303        endif
304      end
305      (dirx eq 1) and (diry eq 1) : begin
306        if keyword_set(integration) then divi = 1 else BEGIN
307          IF msknan[0] NE -1 THEN divi = total(e1*e2*mask*msknan) $
308          ELSE divi = total(e1*e2*mask)
309        ENDELSE
310        res = total(res*e1*e2*mask, nan = nan)/(divi > 1.)
311        if msknan[0] NE -1 then begin
312          testnan  = msknan*mask
313          testnan = total(testnan)+(total(mask) EQ 0)
314        endif
315      end
316    endcase
317  endif
318;------------------------------------------------------------
319;------------------------------------------------------------
320; III) Case 3d arrays series (tab4d)
321;------------------------------------------------------------
322;------------------------------------------------------------
323  if (dim eq '3d') then begin
324;---------------------------------------------------------------
325; III.1) Verification of the coherence of the array to average size
326; Verification of the coherence between the array's size and the domain
327; defind by domdef
328; The input array must have either the total domain size (jpi,jpj,jpk)
329; or this one of the reduced domain (nx,ny,ny)
330;---------------------------------------------------------------
331    case 1 of
332      taille[1] eq jpi and taille[2] eq jpj and taille[3] eq jpk:BEGIN
333        res = tab[firstx:lastx, firsty:lasty, firstz:lastz]
334        IF keyword_set(sshh) THEN sshh = sshh[firstx:lastx, firsty:lasty]
335      END
336      taille[1] eq jpi and taille[2] eq jpj and taille[3] eq nz:BEGIN
337        res = tab[firstx:lastx, firsty:lasty, *]
338        IF keyword_set(sshh) THEN sshh = sshh[firstx:lastx, firsty:lasty]
339      END
340      taille[1] EQ  nx and taille[2] eq  ny and taille[3] eq nz :res = tab
341      taille[1] EQ  nx and taille[2] eq  ny and taille[3] eq jpk : $
342        res = tab[*, *, firstz:lastz]
343      else:BEGIN
344        if keyword_set(savedbox) THEN restoreboxparam, 'boxparam4moyenne.dat'
345        return, report('Probleme d''adequation entre les tailles du domaine nx*ny*nz '+strtrim(nx, 1)+'*'+strtrim(ny, 1)+'*'+strtrim(nz, 1)+' et du tableau '+strtrim(taille[1], 1)+'*'+strtrim(taille[2], 1)+'*'+strtrim(taille[3], 1))
346      END
347    endcase
348    if keyword_set(nan) NE 0 then BEGIN
349      if nan NE 1 then BEGIN    ; if nan is not !values.f_nan
350; we put it at !values.f_nan
351        if abs(nan) LT 1e6 then notanumber = where(res EQ nan) $
352        ELSE notanumber = where(abs(res) GT abs(nan)/10.)
353        if notanumber[0] NE -1 then res[temporary(notanumber)] = !values.f_nan
354      ENDIF
355    ENDIF
356;---------------------------------------------------------------
357; Comment : WE HAVE TO BE CAREFUL ABOUT CASES WHERE THE DIMENSION TO AVERAGE = 1,
358; AND MAKE SURE THAT IT EXIST. THAT IS WHY WE USE reform(...,nx,ny,...) WHICH CAN
359; LOOK USELESS AT THE BEGINNING
360;---------------------------------------------------------------
361    if nx EQ 1 OR ny EQ 1 OR nz EQ 1 then BEGIN
362      res = reform(res, nx, ny, nz, /over)
363      e1 =  reform(e1, nx, ny, /over)
364      e2 =  reform(e2, nx, ny, /over)
365    endif
366    if nx EQ 1 OR ny EQ 1 OR nz EQ 1 THEN $
367      mask =  reform(mask, nx, ny, nz, /over)
368    IF keyword_set(key_partialstep) THEN BEGIN
369; the top of the ocean floor is
370      IF vargrid EQ 'T' OR vargrid EQ 'W' THEN bottom = total(mask, 3) $
371      ELSE bottom = total(tmask[firstx:lastx, firsty:lasty, firstz:lastz], 3)
372; we suppress columns with only ocean or land
373      good = where(bottom NE 0 AND bottom NE nz)
374; the bottom of the ocean in 3D index is:
375      bottom = lindgen(nx*ny)+(temporary(bottom)-1L)*nx*ny
376      IF good[0] NE -1 THEN bottom = bottom[good] $
377      ELSE bottom = -1
378    ENDIF ELSE bottom = -1
379;---------------------------------------------------------------
380; III.2) different average types
381;---------------------------------------------------------------
382    if keyword_set(nan) NE 0 then msknan = finite(res) ELSE msknan = -1
383    case 1 of
384      (dirx eq 1) and (diry eq 0) and (dirz eq 0) : begin
385        e13 = e1[*]#replicate(1., nz)
386        e13 = reform(e13, nx, ny, nz, /over)
387        IF keyword_set(key_partialstep) AND bottom[0] NE -1 $
388          AND nx NE 1 AND NOT keyword_set(keepbottom) THEN BEGIN
389          IF msknan[0] EQ -1 THEN BEGIN
390            msknan = replicate(1b, nx, ny, nz)
391            nan = 1
392          endif
393          msknan[bottom] = 0
394          res[bottom] = !values.f_nan
395        ENDIF
396        if keyword_set(integration) then divi = 1 else begin
397          divi = e13*mask
398          IF msknan[0] NE -1 THEN divi = temporary(divi)*msknan
399          if nz EQ 1 then divi = reform(divi, nx, ny, nz, /over)
400          divi = total(divi, 1)
401        ENDELSE
402        res = res*e13*mask
403        if nz EQ 1 then res = reform(res, nx, ny, nz, /over)
404        res = total(res, 1, nan = nan)/(divi > 1.)
405        e13 = 1
406        if msknan[0] NE -1 then begin
407          testnan  = msknan*mask
408          if nz EQ 1 then testnan  = reform(testnan, nx, ny, nz, /over)
409          testnan = total(testnan, 1)+(total(mask, 1) EQ 0)
410        endif
411      end
412      (dirx eq 0) and (diry eq 1) and (dirz eq 0) : begin
413        e23 = e2[*]#replicate(1., nz)
414        e23 = reform(e23, nx, ny, nz, /over)
415        IF keyword_set(key_partialstep) AND bottom[0] NE -1 $
416          AND ny NE 1 AND NOT keyword_set(keepbottom) THEN BEGIN
417          IF msknan[0] EQ -1 THEN BEGIN
418            msknan = replicate(1b, nx, ny, nz)
419            nan = 1
420          endif
421          msknan[bottom] = 0
422          res[bottom] = !values.f_nan
423        ENDIF
424        if keyword_set(integration) then divi = 1 else begin
425          divi = e23*mask
426          IF msknan[0] NE -1 THEN divi = temporary(divi)*msknan
427          if nz EQ 1 then divi = reform(divi, nx, ny, nz, /over)
428          divi = total(divi, 2)
429        ENDELSE
430        res = res*e23*mask
431        if nz EQ 1 then res = reform(res, nx, ny, nz, /over)
432        res = total(res, 2, nan = nan)/(divi > 1.)
433        e23 = 1
434        if msknan[0] NE -1 then begin
435          testnan  = msknan*mask
436          if nz EQ 1 then testnan = reform(testnan, nx, ny, nz, /over)
437          testnan = total(testnan, 2)+(total(mask, 2) EQ 0)
438        endif
439      end
440      (dirx eq 0) and (diry eq 0) and (dirz eq 1) : begin
441        e33 = replicate(1, 1.*nx*ny)#e3
442        e33 = reform(e33, nx, ny, nz, /over)
443        IF keyword_set(sshh) THEN e33[*, *, 0] = e33[*, *, 0] + sshh
444        IF keyword_set(key_partialstep) AND bottom[0] NE -1 THEN BEGIN
445          IF keyword_set(wdepth) THEN $
446            e33[bottom] = (e3w_ps[firstx:lastx, firsty:lasty])[temporary(good)] $
447          ELSE e33[bottom] = (e3t_ps[firstx:lastx, firsty:lasty])[temporary(good)]
448        ENDIF
449        if keyword_set(integration) then divi = 1 else begin
450          divi = e33*mask
451          if msknan[0] NE -1 then divi = temporary(divi)*msknan
452          if nz EQ 1 then divi = reform(divi, nx, ny, nz, /over)
453          divi = total(divi, 3)
454        ENDELSE
455        res = res*e33*mask
456        if nz EQ 1 then res = reform(res, nx, ny, nz, /over)
457        res = total(res, 3, nan = nan)/(divi > 1.)
458        e33 = 1
459        if msknan[0] NE -1 then begin
460          testnan  = msknan*mask
461          if nz EQ 1 then testnan  = reform(testnan, nx, ny, nz, /over)
462          testnan = total(testnan, 3)+(total(mask, 3) EQ 0)
463        endif
464      end
465      (dirx eq 1) and (diry eq 1) and (dirz eq 0) : begin
466        e123 = (e1*e2)[*]#replicate(1., nz)
467        e123 = reform(e123, nx, ny, nz, /over)
468        IF keyword_set(key_partialstep) AND bottom[0] NE -1 $
469          AND nx*ny NE 1 AND NOT keyword_set(keepbottom) THEN BEGIN
470          IF msknan[0] EQ -1 THEN BEGIN
471            msknan = replicate(1b, nx, ny, nz)
472            nan = 1
473          endif
474          msknan[bottom] = 0
475          res[bottom] = !values.f_nan
476        ENDIF
477        if keyword_set(integration) then divi = 1 else BEGIN
478          divi = e123*mask
479          IF msknan[0] NE -1 THEN divi = temporary(divi)*msknan
480          if nz EQ 1 then divi = reform(divi, nx, ny, nz, /over)
481          divi = total(total(divi, 1), 1)
482        ENDELSE
483        res = res*e123*mask
484        if nz EQ 1 then res = reform(res, nx, ny, nz, /over)
485        res = total(total(res, 1, nan = nan), 1, nan = nan) / (divi > 1.)
486        e123 = 1
487        if msknan[0] NE -1 then begin
488          testnan  = msknan*mask
489          if nz EQ 1 then testnan  = reform(testnan, nx, ny, nz, /over)
490          testnan = total(total(testnan, 1), 1)+(total(total(mask, 1), 1) EQ 0)
491        endif
492      end
493      (dirx eq 1) and (diry eq 0) and (dirz eq 1) : begin
494        e133 = e1[*]#e3
495        e133 = reform(e133, nx, ny, nz, /over)
496        IF keyword_set(sshh) THEN e133[*, *, 0] = e133[*, *, 0] + sshh*e1
497        IF keyword_set(key_partialstep) AND bottom[0] NE -1 THEN BEGIN
498          IF keyword_set(wdepth) THEN $
499            e133[bottom] = (e1*e3w_ps[firstx:lastx, firsty:lasty])[temporary(good)] $
500          ELSE e133[bottom] = (e1*e3t_ps[firstx:lastx, firsty:lasty])[temporary(good)]
501        ENDIF
502        if keyword_set(integration) then divi = 1 else BEGIN
503          divi = e133*mask
504          if msknan[0] NE -1 then divi = temporary(divi)*msknan
505          if nz EQ 1 then divi = reform(divi, nx, ny, nz, /over)
506          divi = total(total(divi, 1), 2)
507        ENDELSE
508        res = res*e133*mask
509        if nz EQ 1 then res = reform(res, nx, ny, nz, /over)
510        res = total(total(res, 1, nan = nan), 2, nan = nan) / (divi > 1.)
511        e133 = 1
512        if msknan[0] NE -1 then begin
513          testnan  = msknan*mask
514          if nz EQ 1 then testnan  = reform(testnan, nx, ny, nz, /over)
515          testnan = total(total(testnan, 1), 2)+(total(total(mask, 1), 2) EQ 0)
516        endif
517      end
518      (dirx eq 0) and (diry eq 1) and (dirz eq 1) : begin
519        e233 = e2[*]#e3
520        e233 = reform(e233, nx, ny, nz, /over)
521        IF keyword_set(sshh) THEN e233[*, *, 0] = e233[*, *, 0] + sshh*e2
522        IF keyword_set(key_partialstep) AND bottom[0] NE -1 THEN BEGIN
523          IF keyword_set(wdepth) THEN $
524            e233[bottom] = (e2*e3w_ps[firstx:lastx, firsty:lasty])[temporary(good)] $
525          ELSE e233[bottom] = (e2*e3t_ps[firstx:lastx, firsty:lasty])[temporary(good)]
526        ENDIF
527        if keyword_set(integration) then divi = 1 else BEGIN
528          divi = e233*mask
529          if msknan[0] NE -1 then divi = temporary(divi)*msknan
530          if nz EQ 1 then divi = reform(divi, nx, ny, nz, /over)
531          divi = total(total(divi, 2), 2)
532        ENDELSE
533        res = res*e233*mask
534        if nz EQ 1 then res = reform(res, nx, ny, nz, /over)
535        res = total(total(res, 2, nan = nan), 2, nan = nan) / (divi > 1.)
536        e233 = 1
537        if msknan[0] NE -1 then begin
538          testnan  = msknan*mask
539          if nz EQ 1 then testnan  = reform(testnan, nx, ny, nz, /over)
540          testnan = total(total(testnan, 2), 2)+(total(total(mask, 2), 2) EQ 0)
541        endif
542      end
543      (dirx eq 1) and (diry eq 1) and (dirz eq 1) : begin
544        e1233 = (e1*e2)[*]#e3
545        e1233 = reform(e1233, nx, ny, nz, /over)
546        IF keyword_set(sshh) THEN e1233[*, *, 0] = e1233[*, *, 0] + sshh*e1*e2
547        IF keyword_set(key_partialstep) AND bottom[0] NE -1 THEN BEGIN
548          IF keyword_set(wdepth) THEN $
549            e1233[bottom] = (e1*e2*e3w_ps[firstx:lastx, firsty:lasty])[temporary(good)] $
550          ELSE e1233[bottom] = (e1*e2*e3t_ps[firstx:lastx, firsty:lasty])[temporary(good)]
551        ENDIF
552        if keyword_set(integration) then divi = 1 else BEGIN
553          if msknan[0] NE -1 then divi = total(e1233*mask*msknan) $
554          ELSE divi = total(e1233*mask)
555        ENDELSE
556        res = total(res*e1233*mask, nan = nan) / (divi > 1.)
557        e1233 = 1
558        if msknan[0] NE -1 then begin
559          testnan  = msknan*mask
560          testnan = total(testnan)+(total(mask) EQ 0)
561        endif
562      end
563    endcase
564  endif
565;------------------------------------------------------------
566;------------------------------------------------------------
567;IV ) finishing
568;------------------------------------------------------------
569;------------------------------------------------------------
570; IV.1) We mask land by a value equal to 1.e+20
571;------------------------------------------------------------
572  valmask = 1e+20
573  terre = where(divi EQ 0)
574  IF terre[0] NE -1 THEN BEGIN
575    res[terre] = 1e+20
576  ENDIF
577;------------------------------------------------------------
578; IV.2) We replace, when nan equal 1, !values.f_nan by nan
579;------------------------------------------------------------
580  if keyword_set(nan) NE 0 then BEGIN
581    puttonan = where(testnan EQ 0)
582    if puttonan[0] NE -1 then res[puttonan] = !values.f_nan
583    if nan NE 1 then BEGIN
584      notanumber = where(finite(res) eq 0)
585      if notanumber[0] NE -1 then res[notanumber] = nan
586    ENDIF
587  ENDIF
588;------------------------------------------------------------
589; IV.3) We replace in the domain whch was defined at the entry of average
590;------------------------------------------------------------
591  if keyword_set(savedbox) THEN restoreboxparam, 'boxparam4moyenne.dat'
592;------------------------------------------------------------
593  if keyword_set(key_performance) THEN print, 'temps moyenne', systime(1)-tempsun
594  return, res
595;------------------------------------------------------------
596;------------------------------------------------------------
597end
Note: See TracBrowser for help on using the repository browser.