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

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

correction of some *.pro using aspell list; introduction of default idldoc syntax

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