source: trunk/SRC/ToBeReviewed/CALCULS/grossemoyenne.pro

Last change on this file was 495, checked in by pinsard, 10 years ago

fix thanks to coding rules; typo; dupe empty lines; trailing blanks

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