<s> """<STR_LIT>""" <EOL> from __future__ import print_function , division <EOL> import numpy as np <EOL> from astropy import units as u <EOL> class xypoint : <EOL> def __init__ ( self , size = <NUM_LIT:100> , x = None , y = None , cont = None , err = None , array = None ) : <EOL> """<STR_LIT>""" <EOL> if array != None : <EOL> self . x = array [ : , <NUM_LIT:0> ] <EOL> self . y = array [ : , <NUM_LIT:1> ] <EOL> self . cont = array [ : , <NUM_LIT:2> ] <EOL> self . err = array [ : , <NUM_LIT:3> ] <EOL> return <EOL> if x is not None : <EOL> size = x . size <EOL> if y is not None : <EOL> size = y . size <EOL> if cont is not None : <EOL> size = cont . size <EOL> if err is not None : <EOL> size = err . size <EOL> if x is None : <EOL> self . x = np . arange ( size , dtype = '<STR_LIT>' ) <EOL> else : <EOL> self . x = x . copy ( ) <EOL> if y is None : <EOL> self . y = np . zeros ( size ) <EOL> else : <EOL> self . y = y . copy ( ) <EOL> if cont is None : <EOL> self . cont = np . ones ( size ) <EOL> else : <EOL> self . cont = cont . copy ( ) <EOL> if err is None : <EOL> self . err = np . ones ( self . y . size ) * <NUM_LIT> <EOL> self . err [ self . y > <NUM_LIT:0> ] = np . sqrt ( self . y [ self . y > <NUM_LIT:0> ] ) <EOL> else : <EOL> self . err = err . copy ( ) <EOL> self . err [ self . err <= <NUM_LIT:0> ] = <NUM_LIT> <EOL> def copy ( self ) : <EOL> copy = xypoint ( self . x . size ) <EOL> copy . x = self . x . copy ( ) <EOL> copy . y = self . y . copy ( ) <EOL> copy . cont = self . cont . copy ( ) <EOL> copy . err = self . err . copy ( ) <EOL> return copy <EOL> def size ( self ) : <EOL> return self . x . size <EOL> def output ( self , outfilename ) : <EOL> np . savetxt ( outfilename , np . transpose ( ( self . x , self . y , self . cont , self . err ) ) ) <EOL> def __getitem__ ( self , index ) : <EOL> if isinstance ( index , slice ) : <EOL> start = max ( <NUM_LIT:0> , index . start ) <EOL> stop = min ( index . stop , self . size ( ) ) <EOL> if index . step == None : <EOL> step = <NUM_LIT:1> <EOL> else : <EOL> step = index . step <EOL> x = np . array ( [ self . x [ i ] for i in range ( start , stop , step ) ] ) <EOL> y = np . array ( [ self . y [ i ] for i in range ( start , stop , step ) ] ) <EOL> cont = np . array ( [ self . cont [ i ] for i in range ( start , stop , step ) ] ) <EOL> err = np . array ( [ self . err [ i ] for i in range ( start , stop , step ) ] ) <EOL> return xypoint ( x = x , y = y , cont = cont , err = err ) <EOL> elif isinstance ( index , list ) or isinstance ( index , tuple ) or isinstance ( index , np . ndarray ) : <EOL> x = self . x [ index ] <EOL> y = self . y [ index ] <EOL> cont = self . cont [ index ] <EOL> err = self . err [ index ] <EOL> return xypoint ( x = x , y = y , cont = cont , err = err ) <EOL> else : <EOL> return [ self . x [ index ] , self . y [ index ] , self . cont [ index ] , self . err [ index ] ] <EOL> def __len__ ( self ) : <EOL> return self . size ( ) <EOL> def toarray ( self , norm = False ) : <EOL> """<STR_LIT>""" <EOL> if norm : <EOL> return np . array ( ( self . x , self . y / self . cont ) ) . transpose ( ) <EOL> else : <EOL> return np . array ( ( self . x , self . y , self . cont , self . err ) ) . transpose ( ) <EOL> def strip_units ( self ) : <EOL> """<STR_LIT>""" <EOL> xunits = self . x . unit if isinstance ( self . x , u . Quantity ) else <NUM_LIT:1.0> <EOL> yunits = self . y . unit if isinstance ( self . y , u . Quantity ) else <NUM_LIT:1.0> <EOL> x = self . x . value if isinstance ( self . x , u . Quantity ) else self . x <EOL> y = self . y . value if isinstance ( self . y , u . Quantity ) else self . y <EOL> err = self . err . value if isinstance ( self . err , u . Quantity ) else self . err <EOL> cont = self . cont . value if isinstance ( self . cont , u . Quantity ) else self . cont <EOL> return xypoint ( x = x , y = y , cont = cont , err = err ) , xunits , yunits <EOL> def CombineXYpoints ( xypts , snr = None , xspacing = None , numpoints = None , interp_order = <NUM_LIT:3> ) : <EOL> """<STR_LIT>""" <EOL> from scipy . interpolate import InterpolatedUnivariateSpline <EOL> if snr is None or type ( snr ) != list : <EOL> snr = [ <NUM_LIT:1.0> ] * len ( xypts ) <EOL> first = np . min ( [ o . x [ <NUM_LIT:0> ] for o in xypts ] ) <EOL> last = np . max ( [ o . x [ - <NUM_LIT:1> ] for o in xypts ] ) <EOL> avg_spacing = np . mean ( [ ( o . x [ - <NUM_LIT:1> ] - o . x [ <NUM_LIT:0> ] ) / float ( o . size ( ) - <NUM_LIT:1> ) for o in xypts ] ) <EOL> if xspacing is None and numpoints is None : <EOL> xspacing = avg_spacing <EOL> if numpoints is None : <EOL> if xspacing is None : <EOL> xspacing = avg_spacing <EOL> numpoints = ( last - first ) / xspacing <EOL> x = np . linspace ( first , last , numpoints ) <EOL> full_array = xypoint ( x = x , y = np . zeros ( x . size ) ) <EOL> numvals = np . zeros ( x . size , dtype = np . float ) <EOL> normalization = <NUM_LIT:0.0> <EOL> for xypt in xypts : <EOL> interpolator = InterpolatedUnivariateSpline ( xypt . x , xypt . y / xypt . cont , k = interp_order ) <EOL> left = np . searchsorted ( full_array . x , xypt . x [ <NUM_LIT:0> ] ) <EOL> right = np . searchsorted ( full_array . x , xypt . x [ - <NUM_LIT:1> ] , side = '<STR_LIT:right>' ) <EOL> if right < xypt . size ( ) : <EOL> right += <NUM_LIT:1> <EOL> numvals [ left : right ] += <NUM_LIT:1.0> <EOL> full_array . y [ left : right ] += interpolator ( full_array . x [ left : right ] ) <EOL> full_array . y [ numvals > <NUM_LIT:0> ] /= numvals [ numvals > <NUM_LIT:0> ] <EOL> return full_array </s>
