Package stistools :: Module r_util
[hide private]
[frames] | no frames]

Source Code for Module stistools.r_util

 1  from __future__ import division         # confidence high 
 2  import os 
 3  import os.path 
 4  import copy 
 5   
6 -def expandFileName (filename):
7 """Expand environment variable in a file name. 8 9 If the input file name begins with either a Unix-style or IRAF-style 10 environment variable (e.g. $lref/name_dqi.fits or lref$name_dqi.fits 11 respectively), this routine expands the variable and returns a complete 12 path name for the file. 13 14 @param filename: a file name, possibly including an environment variable 15 @type filename: string 16 17 @return: the file name with environment variable expanded 18 @rtype: string 19 """ 20 21 n = filename.find ("$") 22 if n == 0: 23 if filename != NOT_APPLICABLE: 24 # Unix-style file name. 25 filename = os.path.expandvars (filename) 26 elif n > 0: 27 # IRAF-style file name. 28 temp = "$" + filename[0:n] + os.sep + filename[n+1:] 29 filename = os.path.expandvars (temp) 30 # If filename contains "//", delete all of them. 31 double_sep = os.sep + os.sep 32 filename = filename.replace(double_sep,os.sep) 33 34 return filename
35
36 -def interpolate (x, values, xp):
37 """Interpolate. 38 39 Linear interpolation is used. If the specified indpendent variable 40 value xp is outside the range of the array x, the first (or last) 41 value in values will be returned. 42 43 @param x: array of independent variable values 44 @type x: a sequence object, e.g. an array, int or float 45 46 @param values: array of dependent variable values 47 @type values: a sequence object, e.g. an array (not character) 48 49 @param xp: independent variable value at which to interpolate 50 @type xp: int or float 51 52 @return: linearly interpolated value 53 @rtype: the same type as one element of values 54 """ 55 56 nvalues = len (values) 57 58 if nvalues == 1 or xp <= x.item(0): 59 value = copy.deepcopy (values[0]) 60 elif xp >= x.item(nvalues-1): 61 value = copy.deepcopy (values[nvalues-1]) 62 else: 63 # search for independent variable values that bracket the specified xp 64 for i in range (nvalues-1): 65 x0 = x.item(i) 66 x1 = x.item(i+1) 67 if xp >= x0 and xp <= x1: 68 if x0 == x1: 69 value = copy.deepcopy (values[i]) 70 else: 71 p = float (x1 - xp) 72 q = float (xp - x0) 73 value = (p * values[i] + q * values[i+1]) / (p + q) 74 break 75 76 return value
77