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

Last change on this file since 325 was 325, checked in by pinsard, 16 years ago

modification of some headers (+some corrections) to prepare usage of the new idldoc

  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
1;+
2;
3; @file_comments
4; Given the arrays X and Y, which tabulate a function (with the X[i]
5; AND Y[i] in ascending order), and given an input value X2, the
6; <pro>spl_incr</pro> function returns an interpolated value for the given values
7; of X2. The interpolation method is based on cubic spline, corrected
8; in a way that integral of the interpolated values is the same as the
9; integral of the input values. (-> for example to build daily data
10; from monthly mean and keep the monthly mean of the computed daily
11; data equal to the original values)
12;
13; @param x {in}{required}
14; An n-elements (at least 2) input vector that specifies the tabulate points in
15; a strict ascending order.
16;
17; @param yin {in}{required}{type=array}
18; an array with one element less than x. y[i] represents the
19; mean value between x[i] and x[i+1]. if /GE0 is activated, y must
20; have positive values.
21;
22; @param x2 {in}{required}
23; The input values for which the interpolated values are desired.
24; Its values must be strictly monotonically increasing.
25;
26; @keyword GE0
27; to force that y2 is always GE than 0. In that case, y must also be GE than 0.
28;
29; @keyword YP0
30; The first derivative of the interpolating function at the
31; point X0. If YP0 is omitted, the second derivative at the
32; boundary is set to zero, resulting in a "natural spline."
33;
34; @keyword YPN_1
35; The first derivative of the interpolating function at the
36; point Xn-1. If YPN_1 is omitted, the second derivative at the
37; boundary is set to zero, resulting in a "natural spline."
38;
39; @returns
40; y2: the mean value between two consecutive values of x2. This
41; array has one element less than y2. y2 has double precision.
42;
43; @restrictions
44; It might be possible that y2 has very small negative values
45; (amplitude smaller than 1.e-6)...
46;
47; @examples
48;
49;    12 monthly values of precipitations into daily values:
50;
51; IDL> yr1 = 1990
52; IDL> yr2 = 1992
53; IDL> nyr = yr2-yr1+1
54; IDL> n1 = 12*nyr+1
55; IDL> x = julday(1+findgen(n1), replicate(1, n1) $
56; IDL>        , replicate(yr1, n1), fltarr(n1))
57; IDL> n2 = 365*nyr + total(leapyr(yr1+indgen(nyr))) + 1
58; IDL> x2 = julday(replicate(1, n2), 1+findgen(n2) $
59; IDL>            , replicate(yr1, n2), fltarr(n2))
60; IDL> y = abs(randomn(0, n1-1))
61; IDL> y2 = spl_keep_mean(x, y, x2, /ge0)
62
63; IDL> print, min(x, max = ma), ma
64; IDL> print, min(x2, max = ma), ma
65; IDL> print, vairdate([min(x, max = ma), ma])
66; IDL> print, total(y*(x[1:n1-1]-x[0:n1-2]))
67; IDL> print, total(y2*(x2[1:n2-1]-x2[0:n2-2]))
68;
69; @history
70;  Sebastien Masson (smasson\@lodyc.jussieu.fr): May 2005
71;
72; @version
73; $Id$
74;
75;-
76FUNCTION spl_keep_mean, x, yin, x2, YP0 = yp0, YPN_1 = ypn_1, GE0 = ge0
77;
78  compile_opt idl2, strictarrsubs
79;
80;---------------------------------
81; check and initialization ...
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 END
Note: See TracBrowser for help on using the repository browser.