#!/usr/bin/env python # Timothy S. Hamilton # April 19, 2021 # Python script to open SDSS "Optical Spectra Per-Object Lite Files" from FITS format # and save them as text files. # Each file is the co-added spectrum for a single target. # For more info on these spectra, see https://www.sdss.org/dr12/data_access/bulk/ # To run, put this script in the directory with your FITS files. # Then on the command line, write the script name, followed by the spectrum FITS filename, such as this: # SDSS_spec_ascii.py spec-1446-53080-0129.fits # The output text file will be the same filename, ending with .txt. # The text file can be opened into a spreadsheet for analysis. import pyfits # This package allows you to open and read FITS files. import sys # This package lets us read the input filename as a command-line argument: # sys.argv is a list of all command-line parameters. # sys.argv[0] is the Python script's name # sys.argv[1] is the 1st parameter after the script name if len(sys.argv) != 2: # Check that the user input the spectrum's FITS filename as a command-line argument raise ValueError('\n\t Include the spectrum filename as a command-line argument.\n\t For example: ./SDSS_spec_ascii.py spec-1446-53080-0129.fits') # Quits and gives this error message if they didn't give the FITS filename. infilename=sys.argv[1] # Takes the FITS filename from the command-line arguments. outfilename=infilename.split('.fits')[0]+'.txt' # Strips off the .fits suffix and appends .txt # to make the output filename. hdulist = pyfits.open(infilename) # Open list of Header Data Units (HDU, or "extensions") of the FITS file: # HDU 0 contains the general header information # HDU 1 contains the spectrum bunit = hdulist[0].header['BUNIT'] # Brightness unit used for the flux mjd = hdulist[0].header['MJD'] # Modified Julian Date of the observation plateid = hdulist[0].header['PLATEID'] # Spectrum plate ID number fiberid = hdulist[0].header['FIBERID'] # Optical fiber ID number spec_id = hdulist[0].header['SPEC_ID'] # Unique ID number of this spectrum--can be used to look spectrum up ra = hdulist[0].header['PLUG_RA'] # Right Ascension of target (degrees)--can be used to look up target dec = hdulist[0].header['PLUG_DEC'] # Declination of target (degrees)--can be used to look up target nwave = hdulist[1].header['NAXIS2'] # Number of wavelength points data = hdulist[1].data # This array holds the data for each wavelength flux=[] # Make an empty list to store fluxes wave=[] # Make an empty list to store wavelengths for i in range(nwave): # Loop over the entries for each wavelength flux.append(data[i][0]) # 1st element (index 0) of each entry is flux; append to the list wave.append(10.**(data[i][1])) # 2nd element (index 1) is log10(wavelength); append to the list hdulist.close() # Close the list of HDUs outfile = open(outfilename, 'w') # Open output file to save in text format outfile.write('# SDSS Spectrum id (SpecObjID): '+spec_id+'\n') # Write header info to the text file... outfile.write('# Plate ID: '+str(plateid)+'\n') outfile.write('# MJD: '+str(mjd)+'\n') outfile.write('# Fiber ID: '+str(fiberid)+'\n') outfile.write('# RA: '+str(ra)+' degrees\n') outfile.write('# Dec: '+str(dec)+' degrees\n') outfile.write('# Number of data points: '+str(nwave)+'\n') outfile.write('# Column 1--wavelength in Angstroms\n') # Describe the data columns outfile.write('# Column 2--flux in units of '+bunit+'\n') # for i in range(nwave): # Loop over the wavelengths outfile.write(str(wave[i])+'\t'+str(flux[i])+'\n') # Write the wavelength and flux outfile.close() # Close and save the output file