source: trunk/SRC/Interpolation/spl_keep_mean.pro @ 101

Last change on this file since 101 was 101, checked in by pinsard, 18 years ago

start to modify headers of Interpolation *.pro files for better idldoc output

  • Property svn:executable set to *
File size: 4.7 KB
Line 
1;------------------------------------------------------------
2;------------------------------------------------------------
3;------------------------------------------------------------
4;+
5; @file_comments
6;
7; Given the arrays X and Y, which tabulate a function (with the X[i]
8; AND Y[i] in ascending order), and given an input value X2, the
9; SPL_INCR function returns an interpolated value for the given values
10; of X2. The interpolation method is based on cubic spline, corrected
11; in a way that integral of the interpolated values is the same as the
12; integral of the input values. (-> for exemple to build daily data
13; from monthly mean and keep the monthly mean of the computed daily
14; data equa to the original values)
15;
16; @examples  y2 =  spl_keep_mean(x, y, x2)
17;
18;    @param x {in}{required}  An n-element (at least 2) input vector that specifies the
19;    tabulate points in a strict ascending order.
20;
21;    @param yin {in}{required}  an array with one element less than x. y[i] represents the
22;    mean value between x[i] and x[i+1]. if /GE0 is activated, y must
23;    have positive values.
24;
25;    @param x2 {in}{required}  The input values for which the interpolated values are
26;    desired. Its values must be strictly monotonically increasing.
27;
28;
29; @keyword    /GE0 to force that y2 is always GE than 0. In that case, y must
30;    also be GE than 0.
31;
32; @keyword    YP0 The first derivative of the interpolating function at the
33;    point X0. If YP0 is omitted, the second derivative at the
34;    boundary is set to zero, resulting in a "natural spline."
35;
36; @keyword    YPN_1 The first derivative of the interpolating function at the
37;    point Xn-1. If YPN_1 is omitted, the second derivative at the
38;    boundary is set to zero, resulting in a "natural spline."
39;
40; @returns
41;
42;    y2: the meean value between two consecutive values of x2. This
43;    array has one element less than y2. y2 has double precision.
44;
45; @restrictions
46;   It might be possible that y2 has very small negative values
47;   (amplitude smaller than 1.e-6)...
48;
49;
50; @examples
51;
52;    12 monthly values of precipitations into daily values:
53;
54;    yr1 = 1990
55;    yr2 = 1992
56;    nyr = yr2-yr1+1
57;    n1 = 12*nyr+1
58;    x = julday(1+findgen(n1), replicate(1, n1) $
59;           , replicate(yr1, n1), fltarr(n1))
60;    n2 = 365*nyr + total(leapyr(yr1+indgen(nyr))) + 1
61;    x2 = julday(replicate(1, n2), 1+findgen(n2) $
62;               , replicate(yr1, n2), fltarr(n2))
63;    y = abs(randomn(0, n1-1))
64;    y2 = spl_keep_mean(x, y, x2, /ge0)
65
66;    print, min(x, max = ma), ma
67;    print, min(x2, max = ma), ma
68;    print, vairdate([min(x, max = ma), ma])
69;    print, total(y*(x[1:n1-1]-x[0:n1-2]))
70;    print, total(y2*(x2[1:n2-1]-x2[0:n2-2]))
71;
72; @history
73;  Sebastien Masson (smasson\@lodyc.jussieu.fr): May 2005
74;-
75;------------------------------------------------------------
76;------------------------------------------------------------
77;------------------------------------------------------------
78FUNCTION spl_keep_mean, x, yin, x2, YP0 = yp0, YPN_1 = ypn_1, GE0 = ge0
79;
80;---------------------------------
81; check and initialisation ...
82;---------------------------------
83;
84  nx = n_elements(x)
85  ny = n_elements(yin)
86  nx2 = n_elements(x2)
87; x must have at least 2 elements
88  IF nx LT 2 THEN stop
89; x2 must have at least 2 elements
90  IF nx2 LT 2 THEN stop
91; x be monotonically increasing
92  IF min(x[1:nx-1]-x[0:nx-2]) LE 0 THEN stop
93; x2 be monotonically increasing
94  IF min(x2[1:nx2-1]-x2[0:nx2-2])  LE 0 THEN stop
95;
96;---------------------------------
97; compute the integral of y
98;---------------------------------
99; if spl_keep_mean is called by the user (and not by itself) we must compute
100; the integral of y. yin must have one element less than x
101  IF nx NE ny+1 THEN stop
102  y = double(yin)*double(x[1:nx-1]-x[0:nx-2])
103  y = [0.0d, temporary(y)]
104  y = total(temporary(y), /cumulative, /double)
105;
106;---------------------------------
107; compute the "spline" interpolation
108;---------------------------------
109;
110  IF keyword_set(ge0) THEN BEGIN
111; if the want that the interpolated values are always >= 0, we must
112; have yin >= 0.0d
113    IF min(yin) LT 0 THEN stop
114; call spl_incr
115    y2 = spl_incr(x, temporary(y), x2, yp0 = yp0, ypn_1 = ypn_1)
116  ENDIF ELSE BEGIN
117    yscd = spl_init(x, y, yp0 = yp0, ypn_1 = ypn_1, /double)
118    y2 = spl_interp(x, y, temporary(yscd), x2, /double)
119  ENDELSE
120;                ;
121;---------------------------------
122; Compute the derivative of y
123;---------------------------------
124;
125  yfrst = (y2[1:nx2-1]-y2[0:nx2-2])/(x2[1:nx2-1]-x2[0:nx2-2])
126; it can happen that we have very small negative values (-1.e-6 for ex)
127;  yfrst = 0.0d > temporary(yfrst)
128  RETURN, yfrst
129 
130;------------------------------------------------------------------
131;------------------------------------------------------------------
132;
133 END
Note: See TracBrowser for help on using the repository browser.