splat/model.py
def loadTelluric(wave_range=None,ndata=None,linear=True,log=False,output='transmission',source='eso',folder='./',*args):
    '''
    Purpose: 

        Loads up telluric absorption spectrum from `Livingston and Wallace (1991) <http://adsabs.harvard.edu/abs/1991aass.book.....L>`_

    Required Inputs:

        A wavelength array must be input in one of the following manners:

            - as an list passed as an argument ``wave``; e.g., loadTelluric(wave)
            - by specifying the min and max wavelengths with ``wave_range`` and number of points with ``ndata``; e.g., loadTelluric(wave_range=[1.3,1.5],ndata=100)

    Optional Inputs:

        :param: **linear**: linear sampling over ``wave_range`` (default = True)
        :param: **log**: log-linear sampling over ``wave_range`` (default = False)
        :param: **folder**: set to folder containing the Livingston and Wallace files (default = SPLAT's reference folder)
        :param: **output**: set to one of the following possible outputs:
            - ``transmission`` (default) - a numpy array containing the transmission spectrum
            - ``spectrum`` - a Spectrum object containing the transmission spectrum

    Output:

        Either a numpy array or Spectrum object containing the transmission spectrum sampled 
        over the wavelength range provided, with values ranging from 0 to 1

    Example:

    >>> import splat.model as spmdl
    >>> trans = spmdl.loadTelluric(wave_range=[1.3,1.5],ndata=1000)
    >>> print(trans)
        [  9.89881739e-01   9.77062180e-01   9.64035135e-01   1.00051461e+00
           9.96495927e-01   9.96135086e-01   9.97309832e-01   9.17222383e-01
           8.20866597e-01   9.24702335e-01   9.97319517e-01   9.97450808e-01
           8.98421113e-01   9.98372247e-01   9.60017183e-01   9.98449332e-01
           9.94087424e-01   9.52683627e-01   9.87684348e-01   7.75109019e-01
           9.76381023e-01   9.89867274e-01   8.71284143e-01   8.79453464e-01
           8.85513893e-01   9.96684751e-01   9.89084643e-01   9.80117987e-01
           9.85237657e-01   9.93525954e-01   9.95844421e-01   7.88396747e-01
           9.82524939e-01   9.98155509e-01   9.96245824e-01   9.55002105e-01   ... 
    >>> trans = spmdl.loadTelluric(wave_range=[1.3,1.5],ndata=1000,output='spectrum')
    >>> trans.info()
        Telluric transmission spectrum

        If you use these data, please cite Livingston, W. & Wallace, L. (1991, UNKNOWN, , )
        bibcode = 1991aass.book.....L
    '''

# prep inputs
    bibcode = ''
    if len(args)>0: 
        wave = args[0]
        ndata = len(wave)
        wave_range = [numpy.min(wave),numpy.max(wave)]
    else:
        if (isinstance(wave_range,list) or isinstance(wave_range,numpy.ndarray)):
            if ndata==None:
                wave = wave_range
            else:
                if len(wave_range) >= 2:
                    if linear == True:
                        wave = numpy.linspace(wave_range[0],wave_range[1],ndata)
                    if log == True:
                        wave = numpy.logspace(wave_range[0],wave_range[1],ndata)
                else:
                    raise ValueError('\nCould not generate wavelength array with wave_range = {} and ndata = {}'.format(wave_range,ndata))
        else:
            raise ValueError('\nwave_range needs to be a list or numpy array')
    if isUnit(wave):
        wave = wave.to(DEFAULT_WAVE_UNIT)
        wave = wave.value
        wave_range = [numpy.min(wave),numpy.max(wave)]
    if not isinstance(wave,numpy.ndarray):
        wave = numpy.array(wave)
    if len(wave) < 2:
        raise ValueError('\nWavelength parameter {} should be a list of wavelengths'.format(wave))

# Livingston & Wallace (1991) spectrum
    if source.lower()=='livingston' or source.lower()=='solar atlas':  
        bibcode = '1991aass.book.....L'
        folder=splat.SPLAT_PATH+splat.TELLURIC_MODEL_FOLDER+'/SolAtlas/'

# prep files
        tfiles = glob.glob(folder+'wn*')
        tfiles.reverse()
        tfiles = numpy.array(tfiles)
        tfwv = numpy.array([1.e4/float(f[len(folder+'wn'):]) for f in tfiles])
# select only those files in the range of wavelengths
#    tfiles = tfiles[
        w = numpy.where(numpy.logical_and(tfwv > numpy.min(wave),tfwv < numpy.max(wave)))
        tfiles_use = tfiles[w]
        if w[0][0] > 0: tfiles_use = numpy.append(tfiles_use,tfiles[w[0][0]-1])
        if w[0][0] < len(tfiles)-1: tfiles_use = numpy.append(tfiles_use,tfiles[w[0][-1]+1])

# generate raw wavelength and transmission list
        twave = []
        trans = []
        for f in tfiles_use:
            dp = pandas.read_csv(f,delimiter='\s+',names=['wavenum','flux','atm','total'])
            twave.extend([1.e4/w for w in dp['wavenum']])
            trans.extend([float(x) for x in dp['atm']])
        trans = numpy.array([x for (y,x) in sorted(zip(twave,trans))])
#    trans = numpy.array(trans)
        twave = numpy.array(sorted(twave))

# NEED CODE HERE TO REMOVE DUPLICATES

# Moehler et al. (2014) Paranal spectrum R = 60,000
    elif source.lower()=='eso' or source.lower()=='moehler' or source.lower()=='paranal':  
        bibcode = '2014A&A...568A...9M'
        folder=splat.SPLAT_PATH+splat.TELLURIC_MODEL_FOLDER+'/ESO/'
        tfiles=glob.glob(folder+'*.fits')

# COULD PUT A CHOICE HERE OF TELLURIC SPECTRA IF WE DOWNLOAD MORE        
        hdu = fits.open(tfiles[0])
        tbl = hdu[1].data
        hdu.close()
        twave = tbl['lam']
        trans = tbl['plstrans']
        trans = numpy.array([x for (y,x) in sorted(zip(twave,trans))])
        twave = numpy.array(sorted(twave))

# NEED TO PUT IN OPTION FOR USER SUPPLIED TELLURIC TRANSMISSION HERE        
    else:
        raise ValueError('\nNeed to provide source for telluric transmission of "ESO" or "Solar Atlas"')

# resample onto desired wavelength scale via numerical integration
    if ndata == None:
        trans_sampled = trans[numpy.where(numpy.logical_and(twave >= numpy.min(wave_range),twave <= numpy.max(wave_range)))]
        wave = twave[numpy.where(numpy.logical_and(twave >= numpy.min(wave_range),twave <= numpy.max(wave_range)))]
# something bad is happening at this step
    else:
        wave = numpy.array(sorted(wave))
        trans_sampled = reMap(twave,trans,wave)
    trans_sampled=trans_sampled*(u.m/u.m)

# return data
    if 'spec' in output.lower():
        mkwargs = {
        'wave': wave,
        'flux': trans_sampled,
        'noise': [numpy.nan for t in trans_sampled],
        'name': 'Telluric transmission',
        'funit': u.m/u.m,
        'wunit': DEFAULT_WAVE_UNIT,
        'istransmission': True
        }
        if bibcode != '': mkwargs['bibcode'] = bibcode
        atm = Spectrum(**mkwargs)
        atm.funit = u.m/u.m
        return atm
    else: 
        return numpy.array(trans_sampled)

    Purpose: 

        Loads up telluric absorption spectrum from `Livingston and Wallace (1991) <http://adsabs.harvard.edu/abs/1991aass.book.....L>`_

    Required Inputs:

        A wavelength array must be input in one of the following manners:

            - as an list passed as an argument ``wave``; e.g., loadTelluric(wave)
            - by specifying the min and max wavelengths with ``wave_range`` and number of points with ``ndata``; e.g., loadTelluric(wave_range=[1.3,1.5],ndata=100)

    Optional Inputs:

        :param: **linear**: linear sampling over ``wave_range`` (default = True)
        :param: **log**: log-linear sampling over ``wave_range`` (default = False)
        :param: **folder**: set to folder containing the Livingston and Wallace files (default = SPLAT's reference folder)
        :param: **output**: set to one of the following possible outputs:
            - ``transmission`` (default) - a numpy array containing the transmission spectrum
            - ``spectrum`` - a Spectrum object containing the transmission spectrum

    Output:

        Either a numpy array or Spectrum object containing the transmission spectrum sampled 
        over the wavelength range provided, with values ranging from 0 to 1

    Example:

    >>> import splat.model as spmdl
    >>> trans = spmdl.loadTelluric(wave_range=[1.3,1.5],ndata=1000)
    >>> print(trans)
        [  9.89881739e-01   9.77062180e-01   9.64035135e-01   1.00051461e+00
           9.96495927e-01   9.96135086e-01   9.97309832e-01   9.17222383e-01
           8.20866597e-01   9.24702335e-01   9.97319517e-01   9.97450808e-01
           8.98421113e-01   9.98372247e-01   9.60017183e-01   9.98449332e-01
           9.94087424e-01   9.52683627e-01   9.87684348e-01   7.75109019e-01
           9.76381023e-01   9.89867274e-01   8.71284143e-01   8.79453464e-01
           8.85513893e-01   9.96684751e-01   9.89084643e-01   9.80117987e-01
           9.85237657e-01   9.93525954e-01   9.95844421e-01   7.88396747e-01
           9.82524939e-01   9.98155509e-01   9.96245824e-01   9.55002105e-01   ... 
    >>> trans = spmdl.loadTelluric(wave_range=[1.3,1.5],ndata=1000,output='spectrum')
    >>> trans.info()
        Telluric transmission spectrum

        If you use these data, please cite Livingston, W. & Wallace, L. (1991, UNKNOWN, , )
        bibcode = 1991aass.book.....L
    
Loads up telluric absorption spectrum from `Livingston and Wallace (1991) `_

Required Inputs.

A wavelength array must be input in one of the following manners.



Optional Inputs.





Either a numpy array or Spectrum object containing the transmission spectrum sampled
over the wavelength range provided, with values ranging from 0 to 1
Loads up telluric absorption spectrum from ` Livingston and Wallace ( 1991 ) ` _ Required Inputs . A wavelength array must be input in one of the following manners . Optional Inputs . Either a numpy array or Spectrum object containing the transmission spectrum sampled over the wavelength range provided with values ranging from 0 to 1
def loadTelluric(wave_range=None,ndata=None,linear=True,log=False,output='transmission',source='eso',folder='./',*args):
    bibcode = ''
    if len(args)>0: 
        wave = args[0]
        ndata = len(wave)
        wave_range = [numpy.min(wave),numpy.max(wave)]
    else:
        if (isinstance(wave_range,list) or isinstance(wave_range,numpy.ndarray)):
            if ndata==None:
                wave = wave_range
            else:
                if len(wave_range) >= 2:
                    if linear == True:
                        wave = numpy.linspace(wave_range[0],wave_range[1],ndata)
                    if log == True:
                        wave = numpy.logspace(wave_range[0],wave_range[1],ndata)
                else:
                    raise ValueError('\nCould not generate wavelength array with wave_range = {} and ndata = {}'.format(wave_range,ndata))
        else:
            raise ValueError('\nwave_range needs to be a list or numpy array')
    if isUnit(wave):
        wave = wave.to(DEFAULT_WAVE_UNIT)
        wave = wave.value
        wave_range = [numpy.min(wave),numpy.max(wave)]
    if not isinstance(wave,numpy.ndarray):
        wave = numpy.array(wave)
    if len(wave) < 2:
        raise ValueError('\nWavelength parameter {} should be a list of wavelengths'.format(wave))
    if source.lower()=='livingston' or source.lower()=='solar atlas':  
        bibcode = '1991aass.book.....L'
        folder=splat.SPLAT_PATH+splat.TELLURIC_MODEL_FOLDER+'/SolAtlas/'
        tfiles = glob.glob(folder+'wn*')
        tfiles.reverse()
        tfiles = numpy.array(tfiles)
        tfwv = numpy.array([1.e4/float(f[len(folder+'wn'):]) for f in tfiles])
        w = numpy.where(numpy.logical_and(tfwv > numpy.min(wave),tfwv < numpy.max(wave)))
        tfiles_use = tfiles[w]
        if w[0][0] > 0: tfiles_use = numpy.append(tfiles_use,tfiles[w[0][0]-1])
        if w[0][0] < len(tfiles)-1: tfiles_use = numpy.append(tfiles_use,tfiles[w[0][-1]+1])
        twave = []
        trans = []
        for f in tfiles_use:
            dp = pandas.read_csv(f,delimiter='\s+',names=['wavenum','flux','atm','total'])
            twave.extend([1.e4/w for w in dp['wavenum']])
            trans.extend([float(x) for x in dp['atm']])
        trans = numpy.array([x for (y,x) in sorted(zip(twave,trans))])
        twave = numpy.array(sorted(twave))
    elif source.lower()=='eso' or source.lower()=='moehler' or source.lower()=='paranal':  
        bibcode = '2014A&A...568A...9M'
        folder=splat.SPLAT_PATH+splat.TELLURIC_MODEL_FOLDER+'/ESO/'
        tfiles=glob.glob(folder+'*.fits')
        hdu = fits.open(tfiles[0])
        tbl = hdu[1].data
        hdu.close()
        twave = tbl['lam']
        trans = tbl['plstrans']
        trans = numpy.array([x for (y,x) in sorted(zip(twave,trans))])
        twave = numpy.array(sorted(twave))
    else:
        raise ValueError('\nNeed to provide source for telluric transmission of "ESO" or "Solar Atlas"')
    if ndata == None:
        trans_sampled = trans[numpy.where(numpy.logical_and(twave >= numpy.min(wave_range),twave <= numpy.max(wave_range)))]
        wave = twave[numpy.where(numpy.logical_and(twave >= numpy.min(wave_range),twave <= numpy.max(wave_range)))]
    else:
        wave = numpy.array(sorted(wave))
        trans_sampled = reMap(twave,trans,wave)
    trans_sampled=trans_sampled*(u.m/u.m)
    if 'spec' in output.lower():
        mkwargs = {
        'wave': wave,
        'flux': trans_sampled,
        'noise': [numpy.nan for t in trans_sampled],
        'name': 'Telluric transmission',
        'funit': u.m/u.m,
        'wunit': DEFAULT_WAVE_UNIT,
        'istransmission': True
        }
        if bibcode != '': mkwargs['bibcode'] = bibcode
        atm = Spectrum(**mkwargs)
        atm.funit = u.m/u.m
        return atm
    else: 
        return numpy.array(trans_sampled)
def loadTelluric ( wave_range = None , ndata = None , linear = True , log = False , output = 'transmission' , source = 'eso' , folder = './' , * args ) : bibcode = '' if len ( args ) > 0 : wave = args [ 0 ] ndata = len ( wave ) wave_range = [ numpy . min ( wave ) , numpy . max ( wave ) ] else : if ( isinstance ( wave_range , list ) or isinstance ( wave_range , numpy . ndarray ) ) : if ndata == None : wave = wave_range else : if len ( wave_range ) >= 2 : if linear == True : wave = numpy . linspace ( wave_range [ 0 ] , wave_range [ 1 ] , ndata ) if log == True : wave = numpy . logspace ( wave_range [ 0 ] , wave_range [ 1 ] , ndata ) else : raise ValueError ( '\nCould not generate wavelength array with wave_range = {} and ndata = {}' . format ( wave_range , ndata ) ) else : raise ValueError ( '\nwave_range needs to be a list or numpy array' ) if isUnit ( wave ) : wave = wave . to ( DEFAULT_WAVE_UNIT ) wave = wave . value wave_range = [ numpy . min ( wave ) , numpy . max ( wave ) ] if not isinstance ( wave , numpy . ndarray ) : wave = numpy . array ( wave ) if len ( wave ) < 2 : raise ValueError ( '\nWavelength parameter {} should be a list of wavelengths' . format ( wave ) ) if source . lower ( ) == 'livingston' or source . lower ( ) == 'solar atlas' : bibcode = '1991aass.book.....L' folder = splat . SPLAT_PATH + splat . TELLURIC_MODEL_FOLDER + '/SolAtlas/' tfiles = glob . glob ( folder + 'wn*' ) tfiles . reverse ( ) tfiles = numpy . array ( tfiles ) tfwv = numpy . array ( [ 1.e4 / float ( f [ len ( folder + 'wn' ) : ] ) for f in tfiles ] ) w = numpy . where ( numpy . logical_and ( tfwv > numpy . min ( wave ) , tfwv < numpy . max ( wave ) ) ) tfiles_use = tfiles [ w ] if w [ 0 ] [ 0 ] > 0 : tfiles_use = numpy . append ( tfiles_use , tfiles [ w [ 0 ] [ 0 ] - 1 ] ) if w [ 0 ] [ 0 ] < len ( tfiles ) - 1 : tfiles_use = numpy . append ( tfiles_use , tfiles [ w [ 0 ] [ - 1 ] + 1 ] ) twave = [ ] trans = [ ] for f in tfiles_use : dp = pandas . read_csv ( f , delimiter = '\s+' , names = [ 'wavenum' , 'flux' , 'atm' , 'total' ] ) twave . extend ( [ 1.e4 / w for w in dp [ 'wavenum' ] ] ) trans . extend ( [ float ( x ) for x in dp [ 'atm' ] ] ) trans = numpy . array ( [ x for ( y , x ) in sorted ( zip ( twave , trans ) ) ] ) twave = numpy . array ( sorted ( twave ) ) elif source . lower ( ) == 'eso' or source . lower ( ) == 'moehler' or source . lower ( ) == 'paranal' : bibcode = '2014A&A...568A...9M' folder = splat . SPLAT_PATH + splat . TELLURIC_MODEL_FOLDER + '/ESO/' tfiles = glob . glob ( folder + '*.fits' ) hdu = fits . open ( tfiles [ 0 ] ) tbl = hdu [ 1 ] . data hdu . close ( ) twave = tbl [ 'lam' ] trans = tbl [ 'plstrans' ] trans = numpy . array ( [ x for ( y , x ) in sorted ( zip ( twave , trans ) ) ] ) twave = numpy . array ( sorted ( twave ) ) else : raise ValueError ( '\nNeed to provide source for telluric transmission of "ESO" or "Solar Atlas"' ) if ndata == None : trans_sampled = trans [ numpy . where ( numpy . logical_and ( twave >= numpy . min ( wave_range ) , twave <= numpy . max ( wave_range ) ) ) ] wave = twave [ numpy . where ( numpy . logical_and ( twave >= numpy . min ( wave_range ) , twave <= numpy . max ( wave_range ) ) ) ] else : wave = numpy . array ( sorted ( wave ) ) trans_sampled = reMap ( twave , trans , wave ) trans_sampled = trans_sampled * ( u . m / u . m ) if 'spec' in output . lower ( ) : mkwargs = { 'wave' : wave , 'flux' : trans_sampled , 'noise' : [ numpy . nan for t in trans_sampled ] , 'name' : 'Telluric transmission' , 'funit' : u . m / u . m , 'wunit' : DEFAULT_WAVE_UNIT , 'istransmission' : True } if bibcode != '' : mkwargs [ 'bibcode' ] = bibcode atm = Spectrum ( ** mkwargs ) atm . funit = u . m / u . m return atm else : return numpy . array ( trans_sampled )
Purpose:
Loads up telluric absorption spectrum from `Livingston and Wallace (1991) <http://adsabs.harvard.edu/abs/1991aass.book.....L>`_
Purpose : Loads up telluric absorption spectrum from ` Livingston and Wallace ( 1991 ) <http : // adsabs . harvard . edu / abs / 1991aass . book ..... L > ` _
'''
    Purpose: 

        Loads up telluric absorption spectrum from `Livingston and Wallace (1991) <http://adsabs.harvard.edu/abs/1991aass.book.....L>`_

    Required Inputs:

        A wavelength array must be input in one of the following manners:

            - as an list passed as an argument ``wave``; e.g., loadTelluric(wave)
            - by specifying the min and max wavelengths with ``wave_range`` and number of points with ``ndata``; e.g., loadTelluric(wave_range=[1.3,1.5],ndata=100)

    Optional Inputs:

        :param: **linear**: linear sampling over ``wave_range`` (default = True)
        :param: **log**: log-linear sampling over ``wave_range`` (default = False)
        :param: **folder**: set to folder containing the Livingston and Wallace files (default = SPLAT's reference folder)
        :param: **output**: set to one of the following possible outputs:
            - ``transmission`` (default) - a numpy array containing the transmission spectrum
            - ``spectrum`` - a Spectrum object containing the transmission spectrum

    Output:

        Either a numpy array or Spectrum object containing the transmission spectrum sampled 
        over the wavelength range provided, with values ranging from 0 to 1

    Example:

    >>> import splat.model as spmdl
    >>> trans = spmdl.loadTelluric(wave_range=[1.3,1.5],ndata=1000)
    >>> print(trans)
        [  9.89881739e-01   9.77062180e-01   9.64035135e-01   1.00051461e+00
           9.96495927e-01   9.96135086e-01   9.97309832e-01   9.17222383e-01
           8.20866597e-01   9.24702335e-01   9.97319517e-01   9.97450808e-01
           8.98421113e-01   9.98372247e-01   9.60017183e-01   9.98449332e-01
           9.94087424e-01   9.52683627e-01   9.87684348e-01   7.75109019e-01
           9.76381023e-01   9.89867274e-01   8.71284143e-01   8.79453464e-01
           8.85513893e-01   9.96684751e-01   9.89084643e-01   9.80117987e-01
           9.85237657e-01   9.93525954e-01   9.95844421e-01   7.88396747e-01
           9.82524939e-01   9.98155509e-01   9.96245824e-01   9.55002105e-01   ... 
    >>> trans = spmdl.loadTelluric(wave_range=[1.3,1.5],ndata=1000,output='spectrum')
    >>> trans.info()
        Telluric transmission spectrum

        If you use these data, please cite Livingston, W. & Wallace, L. (1991, UNKNOWN, , )
        bibcode = 1991aass.book.....L
    ''' # prep inputs # Livingston & Wallace (1991) spectrum # prep files # select only those files in the range of wavelengths #    tfiles = tfiles[ # generate raw wavelength and transmission list #    trans = numpy.array(trans) # NEED CODE HERE TO REMOVE DUPLICATES # Moehler et al. (2014) Paranal spectrum R = 60,000 # COULD PUT A CHOICE HERE OF TELLURIC SPECTRA IF WE DOWNLOAD MORE         # NEED TO PUT IN OPTION FOR USER SUPPLIED TELLURIC TRANSMISSION HERE         # resample onto desired wavelength scale via numerical integration # something bad is happening at this step # return data
wave_range
ndata
linear
log
output
source
folder
examples
None