source: trunk/SRC/ToBeReviewed/STATISTICS/skewness_4d.pro @ 335

Last change on this file since 335 was 335, checked in by smasson, 16 years ago

add Michel developments

  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1;+
2;
3; @file_comments
4;
5; @categories
6; Statistics
7;
8; @param X {in}{required}{type=array}
9; An Array which last dimension is the time dimension so
10; size n.
11;
12; @param NT
13;
14; @keyword DOUBLE
15; If set to a non-zero value, computations are done in
16; double precision arithmetic.
17;
18; @hidden
19;
20; @version
21; $Id$
22;
23;-
24FUNCTION Skewness_Num, X, nT, Double = Double,  NAN =  nan
25; Compute the numerator of the skewness expression
26;
27
28  compile_opt idl2, strictarrsubs
29
30  TimeDim = size(X, /n_dimensions)
31  Xmean = NAN ? TOTAL(X, TimeDim, Double = Double,  NAN =  nan) / TOTAL(FINITE(X), TimeDim) : $
32   TOTAL(X, TimeDim, Double = Double) / nT   
33  one = double ? 1.0d : 1.0
34  Xmean = Xmean[*]#replicate(one, nT)
35  res = TOTAL( (X-Xmean)^3, TimeDim, Double = Double, NAN =  nan)
36
37  RETURN, res
38
39END
40;+
41; @file_comments
42; Same function as SKEWNESS but accept array (until 4
43; dimension) for input and perform  the skewness
44; along the time dimension which must be the last
45; one of the input array.
46;
47; @categories
48; Statistics
49;
50; @param X {in}{required}{type=array}
51; An Array which last dimension is the time dimension so
52; size n.
53;
54; @keyword DOUBLE
55; If set to a non-zero value, computations are done in
56; double precision arithmetic.
57;
58; @keyword NVAL
59; A named variable that, on exit, contains the number of valid
60; observations (not NAN)
61;
62; @examples
63;
64; @history
65; May 2007 Michel Kolasinski (michel.kolasinski@locean-ipsl.upmc.fr)
66;
67; Based on the  a_timecorrelate procedure of IDL
68; INTRODUCTION TO STATISTICAL TIME SERIES
69; Wayne A. Fuller
70; ISBN 0-471-28715-6
71;
72; @version
73; $Id$
74;
75;-
76
77FUNCTION skewness_4d, X, DOUBLE = Double,  NVAL =  nval
78;
79   compile_opt idl2, strictarrsubs
80;
81; Compute the skewness from 1d to 4d vectors
82
83   ON_ERROR, 2
84
85   XDim = SIZE(X, /dimensions)
86   XNDim = SIZE(X, /n_dimensions)
87   nT = XDim[XNDim-1]
88
89; Keyword NAN activated if needed
90; Keyword NVAL not compulsory.
91
92   NAN = ( (WHERE(FINITE(X) EQ 0 ))[0] NE -1 ) ? 1 : 0
93;We can retrieve the matrix of real lenghts of time-series   
94   nTreal = ( (WHERE(FINITE(X) EQ 0 ))[0] NE -1 ) ?  TOTAL(FINITE(X),  XNDim) : nT
95
96   IF ARG_PRESENT(NVAL) THEN nval = nTreal
97
98; Check length.
99   IF (WHERE(nTreal LE 1))[0] NE -1 THEN $
100    MESSAGE,  "Matrix of length of time-series must contain 2 or more elements"   
101
102; If the DOUBLE keyword is not set then the internal precision and
103; result are identical to the type of input.
104   type = SIZE(X, /TYPE)
105   useDouble = (N_ELEMENTS(Double) eq 1) ? KEYWORD_SET(Double) : $
106    (type eq 5)
107
108; Type of outputs according to the type of data input
109   case XNDim of
110      1: Skew = useDouble ? DBLARR(1) : FLTARR(1)
111      2: Skew = useDouble ? DBLARR(XDim[0]) : FLTARR(XDim[0])
112      3: Skew = useDouble ? DBLARR(XDim[0], XDim[1]) : FLTARR(XDim[0], XDim[1])
113      4: Skew = useDouble ? DBLARR(XDim[0], XDim[1], XDim[2]) : FLTARR(XDim[0], XDim[1], XDim[2])
114   endcase
115; Compute standard deviation
116; nTreal might be a matrix
117   std = a_timecorrelate(X, 0, /covariance)
118   std = sqrt(std)
119   zero = where(std EQ 0)
120
121   if zero[0] NE -1 then STOP,  $
122    'Cannot compute skewness since there are zeros in the matrix of standard deviations !'
123
124; Problem with high masked values (x^3 makes NaN when x is high)
125; Threshold put on the values of the tab
126   idx_std = WHERE (std GT 1.0e+10)
127   X = X < 1.0e+10
128   std = std < 1.0e+10
129
130; Compute skewness
131   Skew = Skewness_Num(X, nT, Double = useDouble, NAN = nan) / (nTreal*std^3)
132   IF idx_std[0] NE -1 THEN Skew[idx_std] = valmask
133
134   return, useDouble ? Skew : FLOAT(Skew)
135
136END
137
Note: See TracBrowser for help on using the repository browser.