Пример #1
0
def test_standards_present():
    for s in list(STDS_DWARF_SPEX_KEYS.keys()):
        sp = splat.Spectrum(STDS_DWARF_SPEX_KEYS[s])
        assert type(sp) == splat.Spectrum
    for s in list(STDS_SD_SPEX_KEYS.keys()):
        sp = splat.Spectrum(STDS_SD_SPEX_KEYS[s])
        assert type(sp) == splat.Spectrum
    for s in list(STDS_ESD_SPEX_KEYS.keys()):
        sp = splat.Spectrum(STDS_ESD_SPEX_KEYS[s])
        assert type(sp) == splat.Spectrum
    return
Пример #2
0
def readSpectrum(**kwargs):
    """ Simple demo for plotting Apogee spectra, including visits

    HDU0: master header with target information
    HDU1: spectra: combined and individual
    (Each spectrum has 8575 columns with the spectra, and 2+NVISITS rows.
    The first two rows are both combined spectra, with different weighting:
    first row uses pixel-based weighting, while second uses a more "global" weighting)
    HDU2: error spectra
    HDU3: mask spectra
    HDU4: sky spectra
    HDU5: sky error spectra
    HDU6: telluric spectra
    HDU7: telluric error spectra
    HDU8: table with LSF coefficients
    HDU9: table with RV/binary information

    According to https://goo.gl/rLXEuc
    """

    #not sure
    " conversion from pixel to wavelength, info available in the hdu header"
    crval = spectra.header['CRVAL1']
    cdelt = spectra.header['CDELT1']
    wave = np.array(
        pow(10, crval + cdelt * np.arange(spectra.header['NAXIS1'])) /
        10000) * u.micron  #microns
    # convert fluxes from  (10^-17 erg/s/cm^2/Ang) to  ( erg/s/cm^2/Mircon)

    spectras = [
        1e-13 * np.array(f) * u.erg / u.s / u.centimeter**2 / u.micron
        for f in spectra.data
    ]
    noises = [
        1e-13 * np.array(f) * u.erg / u.s / u.centimeter**2 / u.micron
        for f in noise.data
    ]
    skys = [
        1e-13 * np.array(f) * u.erg / u.s / u.centimeter**2 / u.micron
        for f in sky.data
    ]
    #create a splat Spectrum object just for the combine
    combined = splat.Spectrum(wave=wave,
                              flux=spectra.data[0],
                              noise=noise.data[0])
    #create APOGEE spectrum object
    sp= Spectrum(wave=combined.wave, combined=combined, noise=noises, sky=skys, visits= spectras, \
    nvisits = len(spectra.data))
    print

    return sp
Пример #3
0
 def test_plotBatch():
     data_folder = '/Users/adam/projects/splat/adddata/done/daniella/'
     files = glob.glob(data_folder + '*.fits')
     plotBatch(files,
               classify=True,
               output=out_folder + 'plotBatch_test1.pdf',
               telluric=True)
     plotBatch(data_folder + '*.fits',
               classify=True,
               output=out_folder + 'plotBatch_test2.pdf',
               noise=True)
     splist = [splat.Spectrum(file=f) for f in files]
     plotBatch(splist,
               classify=True,
               output=out_folder + 'plotBatch_test3.pdf',
               features=['h2o', 'feh', 'co'],
               legend=[s.name for s in splist])
     return
Пример #4
0
 def test_plotSequence():
     sp = splat.Spectrum(10001)
     splat.plotSequence(sp, output=out_folder + 'plotSequence_test1.pdf')
     splat.plotSequence(sp,
                        type_range=3,
                        output=out_folder + 'plotSequence_test2.png')
     data_folder = '/Users/adam/projects/splat/adddata/done/daniella/'
     files = glob.glob(data_folder + '*.fits')
     splat.plotSequence(files[0],
                        telluric=True,
                        stack=0.7,
                        spt='M0',
                        output=out_folder + 'plotSequence_test3.eps')
     sp = splat.getSpectrum(shortname='0415-0935')[0]
     splat.plotSequence(sp,
                        telluric=True,
                        stack=0.3,
                        output=out_folder + 'plotSequence_test4.eps')
     splat.plotSequence(sp, telluric=True, stack=0.3)
     return
Пример #5
0
def test_catalog():
    """" a bunch of scripts """
    table = ascii.read('apogee_reduced.csv')
    for ind, locid, coords in zip(table['ASPCAP_ID'], table['LOCATION_ID'],
                                  table['APOGEE_ID']):
        downloadSpectra(ind, locid, coords)
    smaller = table['CATALOG', 'CATALOG_NAME', 'SPTYPE', 'APOGEE_ID', 'GLAT',
                    'J', 'TEFF']
    #    for row in smaller:
    #        print row

    l2 = readSpectrum(filename=HOME + 'apStar-r6-2M00452143+1634446.fits')
    m7 = readSpectrum(filename=HOME + 'apStar-r6-' + '2M07140394+3702459.fits')
    m6 = readSpectrum(filename=HOME + 'apStar-r6-' + '2M22400144+0532162.fits')
    #    m4=readSpectrum(filename=HOME+'apStar-r6-'+'2M05420897+1229252.fits')
    #m3=readSpectrum(filename=HOME+'apStar-r6-'+'2M10121768-0344441.fits')
    #    m2=readSpectrum(filename=HOME+'apStar-r6-'+'2M11052903+4331357.fits')
    l1 = readSpectrum(filename=HOME + 'apStar-r6-' + '2M06154934-0100415.fits')
    #f=plt.figure()
    #ax= f.add_subplot(111)
    #ax.set_xlim([1.5, 1.7])
    #ax.plot(l2.combined.wave, l2.combined.flux, label='$L2$)
    #sp=smooth(l2.combined, l2.sky)

    #splat.plotSpectrum(sp, xrange=[1.5, 1.55])
    spectra = [l2.combined, l1.combined, m7.combined, m6.combined]
    labels = [
        '$L2 \gamma $ J00452143+1634446', '$L1$ J06154934-0100415',
        r'$M7 \beta $ J07140394+3702459', '$M6$ J22400144+0532162'
    ]
    colors = ['r', 'g', 'b', 'k']
    counter = 0
    for f in glob.glob(HOME + '/*'):
        if counter < 8:
            try:
                sp = readSpectrum(filename=f)
                c = 'c'
                spectra.append(sp.combined)
                colors.append(c)
                labels.append('unknown')
            except:
                continue
        counter = counter + 1

    splat.plotSpectrum(spectra, filename='apogee_lines.pdf',\
    labels=labels, colors=colors,xrange=[1.603, 1.608], yrange=[0, 0],
    labelLocation='outside', ylabel='Normalized Flux+c', figsize=(12, 6))

    fig = plt.figure(figsize=(10, 6))
    ax1 = fig.add_subplot(111)
    #ax2 =fig.add_subplot(222)
    plot = ax1.scatter(table['J'] - table['H'],
                       table['J'],
                       c=table['GLAT'],
                       s=50,
                       cmap='rainbow')
    ax1.set_xlabel('J-H')
    ax1.set_ylabel('J')
    ax1.set_xlim(0.0, 1.5)
    ax1.set_ylim(4, 17)
    ax = plt.gca()
    ax.invert_yaxis()
    clb = fig.colorbar(plot)
    clb.ax.set_title('GLAT')
    plt.title('SAMPLE')
    #ax2.scatter(table['GLAT'], table['GLON'], s=50)
    plt.show()

    #smaller.more()
    #    for ind, locid, coords in zip(table['ASPCAP_ID'], table['LOCATION_ID'], table['APOGEE_ID']):
    #        downloadSpectra(ind,locid, coords)

    splat_names = [
        'J03444306+3137338', 'J05420897+1229252', 'J10121791-0344435',
        'J10285555+0050275', 'J11052903+4331357'
    ]
    #short_names=[createShortname(s) for s in splat_names]
    #print short_names
    tables = vstack(
        [splat.searchLibrary(designation=s)[0] for s in splat_names])
    spectra = [splat.Spectrum(s) for s in tables['DATA_FILE']]
    print tables['SPEX_TYPE']
    #=[splat.Spectrum(s) for s in spectra]
    #splat.plotSpectrum(spcs)
    # print short_names
    # print spectra

    return
Пример #6
0
    def __init__(self, **kwargs):

        self._wave = kwargs.get('wave', None)
        self._flux = kwargs.get('flux', None)
        self._contam = kwargs.get('contam', None)
        self._noise = kwargs.get('noise', None)
        self._snr = None
        self._empty_flag = None
        self._splat_spectrum = None
        self._filename = None
        self._filepath = None
        self._snr_histogram = None
        self._spectrum_image_path = None
        self.two_d_spectrum = None
        self._survey = None
        self._sensitivity = None
        self._spectrum_image_path = None
        self._indices = None
        self._spectral_type = kwargs.get('spt', None)
        self._best_fit_line = None
        self.is_ucd = kwargs.get('is_ucd', False)  #flag for UCD candidates

        #load spectrum if given filename

        if 'filepath' in kwargs: self._filepath = kwargs.get('filepath', None)
        if 'filename' in kwargs: self._filename = kwargs.get('filename', None)
        if 'name' in kwargs: self._filename = kwargs.get('name', None)

        #print (return_path(self._filename))
        if (self._filename is not None):
            self.filename = self._filename

        if (self._filepath is not None):
            self.filepath = self._filepath

        if (self._wave
                is not None) and (self.filepath is None) and (not self.is_ucd):
            self._compute_snr()
            self._splat_spectrum = splat.Spectrum(wave=self._wave,
                                                  flux=self._flux,
                                                  noise=self._noise,
                                                  instrument='WFC3')
            self._best_fit_line = fit_a_line(self)
            ftest = f_test(self)
            for key in ftest.keys():
                setattr(self, key, ftest[key])
            self.normalize()

        if self._spectral_type is not None:
            self.spectral_type = self._spectral_type

        #keep a copy of this object as an attribute
        #read the file locallly for UCDs
        if (self._wave is None) and self.is_ucd:
            row = (UCD_SPECTRA[UCD_SPECTRA.grism_id == self._filename]).iloc[0]
            self._wave = row.wave
            self._flux = row.flux
            self._noise = row.noise
            self._contam = row.contam
            self._compute_snr()
            self._splat_spectrum = splat.Spectrum(wave=self._wave,
                                                  flux=self._flux,
                                                  noise=self._noise,
                                                  instrument='WFC3')
            self._best_fit_line = fit_a_line(self)
            ftest = f_test(self)
            for key in ftest.keys():
                setattr(self, key, ftest[key])
            self.normalize()

        self.original = copy.deepcopy(self)
Пример #7
0
 def splat_spectrum(self):
     return splat.Spectrum(wave=self._wave,
                           flux=self._flux,
                           noise=self._noise,
                           instrument='WFC3-G141')
Пример #8
0
def load_spectra():
    #'/Users/guillaumeshippee/Desktop/splat-master/reference/Spectra/10001_10443.fits' #change
    if request.method == 'GET':
        return render_template('input.html', error='')
    else:
        #		for k in list(request.form.keys()):
        #			print(k,request.form[k])

        # search by file "upload"
        if request.form['submit'] == 'Load File':
            try:
                path = request.form['path']
                sp = splat.Spectrum(file=str(path))
                sp = [sp]
            except:
                return render_template(
                    'input.html', error="\n\nProblem with file upload button")

# search by file path specification
        if request.form['submit'] == 'Load File ':
            try:
                path = request.form['path']
                sp = splat.Spectrum(file=str(path))
                sp = [sp]
            except:
                return render_template(
                    'input.html',
                    error="\n\nProblem with file path specification")

# search by spectrum key
        if request.form['submit'] == 'Load by ID':
            try:
                sp = splat.Spectrum(int(str(request.form['key'])))
                sp = [sp]
            except:
                return render_template(
                    'input.html', error="\n\nProblem with key specification")

# search by date observed
        if request.form['submit'] == 'Load by Date':
            try:
                sp = splat.getSpectrum(date=str(request.form['date']))
            except:
                return render_template(
                    'input.html', error="\n\nProblem with key specification")

# search by shortname
        elif request.form['submit'] == 'Load by Shortname':
            try:
                sp = splat.getSpectrum(
                    shortname=str(request.form['shortname']))
            except:
                return render_template(
                    'input.html',
                    error="\n\nProblem with specifying file by shortname")

# search by name
        elif request.form['submit'] == 'Load by Name':
            try:
                sp = splat.getSpectrum(name=str(request.form['name']))
            except:
                return render_template(
                    'input.html',
                    error="\n\nProblem with specifying file by name")

# search by options
        elif request.form['submit'] == 'Load by Options':
            sp1 = request.form['sp1']
            sp2 = request.form['sp2']
            mag1 = request.form['mag1']
            mag2 = request.form['mag2']

            if sp2 == '':
                sp = sp1
            elif sp1 == '':
                sp2 = sp1
            elif sp1 == '' and sp2 == '':
                sp = ''
            else:
                sp = [sp1, sp2]

            if mag2 == '':
                mag = mag1
            elif sp1 == '':
                mag2 = mag1
            elif sp1 == '' and sp2 == '':
                mag = ''
            else:
                mag = [mag1, mag2]

            kwargs = {
                'spt': sp,
                'jmag': mag,
                'snr': request.form['snr'],
                'date': request.form['date']
            }
            kwargs = {k: v for k, v in kwargs.items() if v}

            try:
                sp = splat.getSpectrum(**kwargs)
            except:
                return render_template('input.html',
                                       error="\n\nProblem with option search")

# lucky pull
        elif request.form['submit'] == 'Get Lucky!':
            sp = splat.getSpectrum(lucky=True)

        if len(sp) == 0:
            return render_template(
                'input.html',
                error="\n\nNo spectra matched search constratins")

        try:
            tab = []

            for s in sp:
                spectral_type = splat.classifyByStandard(s)[0]
                mpl_fig = splot.plotSpectrum(s, web=True, uncertainty=True)[0]
                bokehfig = mpl.to_bokeh(fig=mpl_fig)
                bokehfig.set(x_range=Range1d(.8, 2.4),
                             y_range=Range1d(0,
                                             s.fluxMax().value * 1.2))
                #				sys.stdout = open("out1.txt", "w")
                #				sys.stdout = sys.__stdout__
                #				with open("out1.txt", "r") as f:
                #					content = f.read()
                content = s.info(printout=False)
                #				print(content)
                p = Paragraph(text=content)
                widget = VBox(bokehfig, p)
                tab.append(Panel(child=widget, title=str(s.name)))


#				tab.append(Panel(child=widget, title=str(spectral_type)+ " Star"))

            plottabs = Tabs(tabs=tab)
            script, div_dict = components({"plot": tab})
        except:
            return render_template('input.html',
                                   error="\n\nProblem Plotting Spectra")

        return render_template('output.html',
                               star_type=spectral_type,
                               script=script,
                               div=div_dict)
Пример #9
0
def plotSequence(*args, **kwargs):
    '''
    :Purpose: Compares a spectrum to a sequence of standards a batch of spectra into a 2x2 set of PDF files, with options of overplotting comparison spectra, including best-match spectral standards. 

    :param input: A single or list of Spectrum objects or filenames, or the glob search string for a set of files (e.g., '/Data/myspectra/*.fits'). 
    :type input: required

    :param type_range: Number of subtypes to consider above and below best-fit spectral type 
    :type type_range: optional, default = 2
    :param spt: Default spectral type for source; this input skips classifyByStandard
    :type spt: optional, default = None
    :param output: Filename for output; full path should be include if not saving to current directory. If blank, plot is shown on screen
    :type output: optional, default = None (screen display)

    Relevant parameters for plotSpectrum may also be passed

    :Example:
       >>> import glob, splat
       >>> files = glob.glob('/home/mydata/*.fits')
       >>> sp = splat.plotBatch(files,classify=True,output='comparison.pdf')
       >>> sp = splat.plotBatch('/home/mydata/*.fits',classify=True,output='comparison.pdf')
       >>> sp = splat.plotBatch([splat.Spectrum(file=f) for f in files],classify=True,output='comparison.pdf')
       All three of these commands produce the same result
    '''

    # check inputs
    if len(args) == 0:
        raise ValueError(
            '\nNeed to provide a spectrum object or filename for plotStandardSequence'
        )

    parameters = ['type_range']
    splat.checkKeys(kwargs, parameters, forcekey=False)
    type_range = kwargs.get('type_range', 2)
    kwargs['stack'] = kwargs.get('stack', 0.5)
    #    kwargs['legendLocation']=kwargs.get('legendLocation','outside')
    kwargs['telluric'] = kwargs.get('telluric', True)
    kwargs['method'] = kwargs.get('method', 'kirkpatrick')
    #    kwargs['color']=kwargs.get('color','r')
    kwargs['colorComparison'] = kwargs.get('colorComparison', 'k')
    kwargs['colorScheme'] = kwargs.get('colorScheme', 'winter')
    kwargs['figsize'] = kwargs.get('figsize',
                                   [10, 10 * type_range * kwargs['stack']])
    kwargs['fontscale'] = kwargs.get('fontscale', 1.5)

    # process input
    if isinstance(args[0], str):
        if len(glob.glob(args[0])) == 0:
            raise ValueError(
                '\nCannot find input file {} - make sure full path is included'
                .format(args[0]))
        try:
            sp = splat.Spectrum(file=args[0])
        except:
            raise ValueError(
                '\nCould not read in file {} - make sure the file is correctly formatted'
                .format(args[0]))
    elif isinstance(args[0], splat.Spectrum):
        sp = copy.deepcopy(args[0])
    else:
        raise ValueError('\nInput should be a Spectrum object or filename')
    sp.normalize()

    # classify by comparison to standards
    spt = kwargs.get('spt', splat.classifyByStandard(sp, **kwargs)[0])
    if not isinstance(spt, str):
        spt = splat.typeToNum(spt)

# produce range of standards for plot
    stdnum = numpy.arange(numpy.floor(splat.typeToNum(spt) - type_range),
                          numpy.ceil(splat.typeToNum(spt) + type_range) + 1)
    if numpy.max(stdnum) > 39:
        stdnum -= (numpy.max(stdnum) - 39)
    if numpy.min(stdnum) < 10:
        stdnum += (10 - numpy.min(stdnum))
    stdspt = [splat.typeToNum(i) for i in stdnum]
    stds = [splat.SPEX_STDS[s] for s in stdspt]
    stdlabels = ['{} Standard'.format(s) for s in stdspt]
    plotlist = []
    labels = []
    colors = []
    for i, stdsp in enumerate(stds):
        plotlist.append([stdsp, sp])
        labels.extend([])
    fig = splat.plotSpectrum(stds, comparison=sp, labels=stdlabels, **kwargs)

    return fig
Пример #10
0
def plotBatch(*args, **kwargs):
    '''
    :Purpose: Plots a batch of spectra into a 2x2 set of PDF files, with options of overplotting comparison spectra, including best-match spectral standards. 

    :param input: A single or list of Spectrum objects or filenames, or the glob search string for a set of files (e.g., '/Data/myspectra/*.fits'). 
    :type input: required

    :param output: Filename for PDF file output; full path should be include if not saving to current directory
    :type output: optional, default = 'spectra_plot.pdf'
    :param comparisons: list of Spectrum objects or filenames for comparison spectra. If comparisons list is shorter than source list, then last comparison source will be repeated. If the comparisons list is longer, the list will be truncated. 
    :type comparisons: optional, default = None
    :param classify: Set to True to classify sources based on comparison to MLT spectral standards following the method of `Kirkpatrick et al. (2010) <http://adsabs.harvard.edu/abs/2010ApJS..190..100K>`_. This option normalizes the spectra by default
    :type classify: optional, default = False
    :param normalize: Set to True to normalize source and (if passed) comparison spectra.
    :type normalize: optional, default = False
    :param legend: Set to list of legends for plots. The number of legends should equal the number of sources and comparisons (if passed) in an alternating sequence. T
    :type legend: optional, default = displays file name for source and object name for comparison (if passed)

    Relevant parameters for plotSpectrum may also be passed

    :Example:
       >>> import glob, splat
       >>> files = glob.glob('/home/mydata/*.fits')
       >>> sp = splat.plotBatch(files,classify=True,output='comparison.pdf')
       >>> sp = splat.plotBatch('/home/mydata/*.fits',classify=True,output='comparison.pdf')
       >>> sp = splat.plotBatch([splat.Spectrum(file=f) for f in files],classify=True,output='comparison.pdf')
       All three of these commands produce the same result
    '''

    # keyword check
    parameters = ['output', 'comparisons', 'classify', 'normalize', 'legend']
    splat.checkKeys(kwargs, parameters, forcekey=False)
    kwargs['layout'] = kwargs.get('layout', [2, 2])
    kwargs['fontscale'] = kwargs.get('fontscale', 0.7)

    # input check
    if len(args) == 0:
        print(
            '\nNeed to provide a list of spectra or filenames, or a file search string, to use plotBatch'
        )
        return
    kwargs['output'] = kwargs.get('output', 'spectra_plot.pdf')

    # break out a list
    if isinstance(args[0], list):
        inputlist = args[0]
    else:
        inputlist = args

# if filenames, read in each file to a spectrum object
    if isinstance(inputlist[0], str):
        # try a glob search string
        files = glob.glob(inputlist[0])
        if len(files) > 1 or (len(files) == 1
                              and inputlist[0].find('*') != -1):
            inputlist = files
# try reading in files into Spectrum object
        try:
            splist = [splat.Spectrum(file=f) for f in inputlist]
        except:
            raise ValueError(
                '\nCould not read in list of files - make sure the full path is specified and the files are correctly formatted'
            )

# if filenames, read in each file to a spectrum object
    elif isinstance(inputlist[0], splat.Spectrum):
        splist = copy.deepcopy(inputlist)
    else:
        print('\nInput should be list of Spectra objects or filenames')
        return

# comparison files are present
    compflag = False
    complist = []
    if kwargs.get('comparisons', False) != False:
        comp = kwargs.get('comparisons')
        if not isinstance(comp, list):
            comp = [comp]
        if len(comp) < len(splist):
            while len(comp) < len(splist):
                comp.append(comp[-1])
        if isinstance(comp[0], str):
            try:
                complist = [splat.Spectrum(file=f) for f in comp]
                compflag = True
            except:
                print(
                    '\nCould not read in comparison files: ignoring comparisons'
                )
        if isinstance(comp[0], splat.Spectrum):
            complist = comp
            compflag = True

# set comparison files to be standards for spectral classification
    if kwargs.get('classify', False):
        complist = []
        for sp in splist:
            spt = splat.classifyByStandard(sp, method='kirkpatrick')
            complist.append(splat.SPEX_STDS[spt[0]])
        compflag = True
        kwargs['normalize'] = kwargs.get('normalize', True)

# normalize if desired
    if kwargs.get('normalize', False):
        tmp = [sp.normalize() for sp in splist]
        if compflag == True:
            tmp = [sp.normalize() for sp in complist]

# prep for plotting
    plotlist = []
    clist = []
    for i, sp in enumerate(splist):
        if compflag == True:
            plotlist.append([sp, complist[i]])
            clist.extend(['k', 'r'])
        else:
            plotlist.append([sp])
            clist.extend(['k'])

# manage legends
    if kwargs.get('legend', False) != False:
        legends = kwargs.get('legend')
        if not isinstance(legends, list):
            legends = [legends]
        if len(legends) < (len(splist) + len(complist)):
            # guess: just left out the comparison legends
            if len(complist) > 0 and len(legends) == len(splist):
                legtmp = []
                for i, l in enumerate(legends):
                    legtmp.extend([l, '{}'.format(complist[i].name)])
                legends = legtmp
            else:
                # otherwise: pad the remaining legends with the last legend (pairs)
                while len(legends) < (len(splist) + len(complist)):
                    if compflag:
                        legends.extend([legends[-2], legends[-1]])
                    else:
                        legends.extend([legends[-1]])
        if len(legends) > (len(splist) + len(complist)):
            legends = legends[0:(len(splist) + len(complist))]
    else:
        legends = []
        for i, sp in enumerate(splist):
            if compflag == True:
                legends.extend([
                    '{}'.format(os.path.basename(sp.filename)),
                    '{}'.format(complist[i].name)
                ])
            else:
                legends.extend(['{}'.format(os.path.basename(sp.filename))])


# generate pdf
    splat.plotSpectrum(plotlist,
                       multiplot=True,
                       multipage=True,
                       legends=legends,
                       colors=clist,
                       **kwargs)

    return