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

Last change on this file since 134 was 134, checked in by navarro, 18 years ago

change *.pro file properties (del eof-style, del executable, set keywords Id

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