Example #1
0
 def input_wrapper(*args,**kwargs):
     spec= args[0]
     specerr= args[1]
     if isinstance(specerr,str): # locID+APOGEE-ID; array
         ispec= apread.aspcapStar(spec,specerr,ext=1,header=False,
                                  aspcapWavegrid=True)
         ispecerr= apread.aspcapStar(spec,specerr,ext=2,header=False,
                                     aspcapWavegrid=True)
         spec= ispec
         specerr= ispecerr
     elif (isinstance(specerr,(list,numpy.ndarray)) \
               and isinstance(specerr[0],str)): # locID+APOGEE-ID; array
         aspcapBlu_start,aspcapGre_start,aspcapRed_start,aspcapTotal = _aspcapPixelLimits(dr=None)
         nspec= len(specerr)
         ispec= numpy.empty((nspec,aspcapTotal))
         ispecerr= numpy.empty((nspec,aspcapTotal))
         for ii in range(nspec):
             ispec[ii]= apread.aspcapStar(spec[ii],specerr[ii],ext=1,
                                          header=False,aspcapWavegrid=True)
             ispecerr[ii]= apread.aspcapStar(spec[ii],specerr[ii],ext=2,
                                             header=False,aspcapWavegrid=True)
         spec= ispec
         specerr= ispecerr
     elif isinstance(specerr,(list,numpy.ndarray)) \
             and isinstance(specerr[0],(float,numpy.float32,
                                        numpy.float64,numpy.ndarray)) \
         and ((len(specerr.shape) == 1 and len(specerr) == 8575)
              or (len(specerr.shape) == 2 and specerr.shape[1] == 8575)): #array on apStar grid
         spec= toAspcapGrid(spec)
         specerr= toAspcapGrid(specerr)
     return func(spec,specerr,*args[2:],**kwargs)
Example #2
0
 def input_wrapper(*args,**kwargs):
     spec= args[0]
     specerr= args[1]
     if isinstance(specerr,str): # locID+APOGEE-ID; array
         ispec= apread.aspcapStar(spec,specerr,ext=1,header=False,
                                  aspcapWavegrid=True)
         ispecerr= apread.aspcapStar(spec,specerr,ext=2,header=False,
                                     aspcapWavegrid=True)
         spec= ispec
         specerr= ispecerr
     elif (isinstance(specerr,(list,numpy.ndarray)) \
               and isinstance(specerr[0],str)): # locID+APOGEE-ID; array
         nspec= len(specerr)
         ispec= numpy.empty((nspec,7214))
         ispecerr= numpy.empty((nspec,7214))
         for ii in range(nspec):
             ispec[ii]= apread.aspcapStar(spec[ii],specerr[ii],ext=1,
                                          header=False,aspcapWavegrid=True)
             ispecerr[ii]= apread.aspcapStar(spec[ii],specerr[ii],ext=2,
                                             header=False,aspcapWavegrid=True)
         spec= ispec
         specerr= ispecerr
     elif isinstance(specerr,(list,numpy.ndarray)) \
             and isinstance(specerr[0],(float,numpy.float32,
                                        numpy.float64,numpy.ndarray)) \
         and ((len(specerr.shape) == 1 and len(specerr) == 8575)
              or (len(specerr.shape) == 2 and specerr.shape[1] == 8575)): #array on apStar grid
         spec= toAspcapGrid(spec)
         specerr= toAspcapGrid(specerr)
     return func(spec,specerr,*args[2:],**kwargs)
Example #3
0
def train_quadfit(\
    trainingfilename=os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                  'cannon','training',
                                  'training_apokasc_gc_ind_feh_fix.txt'),
    outfilename=os.path.join(os.path.dirname(os.path.realpath(__file__)),
                             'cannon','trained',
                             'trained_apokasc_gc_ind_feh_fix.txt'),
    baseline_labels=[4500.,2.,-0.3,0.05]):
    """
    NAME:
       train_quadfit
    PURPOSE:
       train a quadratic polynomial fit to training data
    INPUT:
       trainingfilename= name of the file that has the training data
       outfilename= name of the file that will hold the output (scatter is in file with .txt replaced by _scatter.txt)
       baseline_labels= baseline to subtract from the labels
    OUTPUT:
       (none; just writes the output to a file)
    HISTORY:
       2015-02-28 - Written - Bovy (IAS)
       2018-02-05 - Updated to account for changing detector ranges - Price-Jones (UofT)
    """
    # Read the training data
    loc_ids, ap_ids, labels = _read_training(trainingfilename)
    new_labels = (labels[0] - baseline_labels[0], )
    for ii in range(1, len(labels)):
        new_labels = new_labels + (labels[ii] - baseline_labels[ii], )
    labels = new_labels
    # Load the spectra for these data
    aspcapBlu_start, aspcapGre_start, aspcapRed_start, aspcapTotal = _aspcapPixelLimits(
        dr=None)
    spec = numpy.empty((len(loc_ids), aspcapTotal))
    specerr = numpy.empty((len(loc_ids), aspcapTotal))
    for ii in range(len(loc_ids)):
        spec[ii] = apread.aspcapStar(loc_ids[ii],
                                     ap_ids[ii],
                                     ext=1,
                                     header=False,
                                     aspcapWavegrid=True)
        specerr[ii] = apread.aspcapStar(loc_ids[ii],
                                        ap_ids[ii],
                                        ext=2,
                                        header=False,
                                        aspcapWavegrid=True)
    # Train
    qout = cannon.quadfit(spec, specerr, *labels)
    # Save to file
    numpy.savetxt(outfilename, qout[0])
    numpy.savetxt(outfilename.replace('.txt', '_scatter.txt'), qout[1])
    numpy.savetxt(outfilename.replace('.txt', '_baseline_labels.txt'),
                  baseline_labels)
    return None
Example #4
0
 def input_wrapper(*args,**kwargs):
     if len(args) >= 2 and isinstance(args[0],(list,numpy.ndarray)) \
             and isinstance(args[1],(list,numpy.ndarray)):
         # wavelength, spectrum
         return func(args[0],args[1],*args[2:],**kwargs)
     elif len(args) >= 1 and isinstance(args[0],(list,numpy.ndarray)):
         # spectrum on standard re-sampled wavelength grid
         lam=apStarWavegrid()
         if len(args[0]) == 7214: # Input is on ASPCAP grid
             spec= numpy.zeros(len(lam))
             spec[322:3242]= args[0][:2920]
             spec[3648:6048]= args[0][2920:5320]
             spec[6412:8306]= args[0][5320:]
         else:
             spec= args[0]
         return func(lam,spec,*args[1:],**kwargs)
     elif isinstance(args[0],(int,numpy.short,str)) \
             and isinstance(args[1],str):
         # location ID and APOGEE ID (loc ID can be string for 1m sample)
         if kwargs.get('apStar',False):
             spec, hdr= apread.apStar(args[0],args[1],header=True,
                                      ext=kwargs.pop('ext',1))
             spec= spec[numpy.amin([kwargs.pop('apStarIndx',1),
                                    len(spec)-1])]
         else: #aspcapStar
             spec, hdr= apread.aspcapStar(args[0],args[1],header=True,
                                          ext=kwargs.pop('ext',1))
         lam= 10.**numpy.arange(hdr['CRVAL1'],
                                hdr['CRVAL1']+len(spec)*hdr['CDELT1'],
                                hdr['CDELT1'])
         return func(lam,spec,*args[2:],**kwargs)
Example #5
0
 def input_wrapper(*args,**kwargs):
     if len(args) >= 2 and isinstance(args[0],(list,numpy.ndarray)) \
             and isinstance(args[1],(list,numpy.ndarray)):
         # wavelength, spectrum
         return func(args[0],args[1],*args[2:],**kwargs)
     elif len(args) >= 1 and isinstance(args[0],(list,numpy.ndarray)):
         # spectrum on standard re-sampled wavelength grid
         lam=apStarWavegrid()
         apStarBlu_lo,apStarBlu_hi,apStarGre_lo,apStarGre_hi,apStarRed_lo,apStarRed_hi = _apStarPixelLimits(dr=None)    
         aspcapBlu_start,aspcapGre_start,aspcapRed_start,aspcapTotal = _aspcapPixelLimits(dr=None)
         if len(args[0]) == aspcapTotal: # Input is on ASPCAP grid
             spec= numpy.zeros(len(lam))
             spec[apStarBlu_lo:apStarBlu_hi]= args[0][:aspcapGre_start]
             spec[apStarGre_lo:apStarGre_hi]= args[0][aspcapGre_start:aspcapRed_start]
             spec[apStarRed_lo:apStarRed_hi]= args[0][aspcapRed_start:]
         else:
             spec= args[0]
         return func(lam,spec,*args[1:],**kwargs)
     elif isinstance(args[0],(int,numpy.short,str)) \
             and isinstance(args[1],str):
         # location ID and APOGEE ID (loc ID can be string for 1m sample)
         if kwargs.get('apStar',False):
             spec, hdr= apread.apStar(args[0],args[1],header=True,
                                      ext=kwargs.pop('ext',1))
             spec= spec[numpy.amin([kwargs.pop('apStarIndx',1),
                                    len(spec)-1])]
         else: #aspcapStar
             spec, hdr= apread.aspcapStar(args[0],args[1],header=True,
                                          ext=kwargs.pop('ext',1))
         lam= 10.**numpy.arange(hdr['CRVAL1'],
                                hdr['CRVAL1']+len(spec)*hdr['CDELT1'],
                                hdr['CDELT1'])
         return func(lam,spec,*args[2:],**kwargs)
Example #6
0
 def input_wrapper(*args,**kwargs):
     if len(args) >= 2 and isinstance(args[0],(list,numpy.ndarray)) \
             and isinstance(args[1],(list,numpy.ndarray)):
         # wavelength, spectrum
         return func(args[0],args[1],*args[2:],**kwargs)
     elif len(args) >= 1 and isinstance(args[0],(list,numpy.ndarray)):
         # spectrum on standard re-sampled wavelength grid
         lam=apStarWavegrid()
         apStarBlu_lo,apStarBlu_hi,apStarGre_lo,apStarGre_hi,apStarRed_lo,apStarRed_hi = _apStarPixelLimits(dr=None)    
         aspcapBlu_start,aspcapGre_start,aspcapRed_start,aspcapTotal = _aspcapPixelLimits(dr=None)
         if len(args[0]) == aspcapTotal: # Input is on ASPCAP grid
             spec= numpy.zeros(len(lam))
             spec[apStarBlu_lo:apStarBlu_hi]= args[0][:aspcapGre_start]
             spec[apStarGre_lo:apStarGre_hi]= args[0][aspcapGre_start:aspcapRed_start]
             spec[apStarRed_lo:apStarRed_hi]= args[0][aspcapRed_start:]
         else:
             spec= args[0]
         return func(lam,spec,*args[1:],**kwargs)
     elif isinstance(args[0],(int,numpy.short,str)) \
             and isinstance(args[1],str):
         # location ID and APOGEE ID (loc ID can be string for 1m sample)
         if kwargs.get('apStar',False):
             spec, hdr= apread.apStar(args[0],args[1],header=True,
                                      ext=kwargs.pop('ext',1))
             spec= spec[numpy.amin([kwargs.pop('apStarIndx',1),
                                    len(spec)-1])]
         else: #aspcapStar
             spec, hdr= apread.aspcapStar(args[0],args[1],header=True,
                                          ext=kwargs.pop('ext',1))
         lam= 10.**numpy.arange(hdr['CRVAL1'],
                                hdr['CRVAL1']+len(spec)*hdr['CDELT1'],
                                hdr['CDELT1'])
         return func(lam,spec,*args[2:],**kwargs)
Example #7
0
def train_quadfit(\
    trainingfilename=os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                  'cannon','training',
                                  'training_apokasc_gc_ind_feh_fix.txt'),
    outfilename=os.path.join(os.path.dirname(os.path.realpath(__file__)),
                             'cannon','trained',
                             'trained_apokasc_gc_ind_feh_fix.txt'),
    baseline_labels=[4500.,2.,-0.3,0.05]):
    """
    NAME:
       train_quadfit
    PURPOSE:
       train a quadratic polynomial fit to training data
    INPUT:
       trainingfilename= name of the file that has the training data
       outfilename= name of the file that will hold the output (scatter is in file with .txt replaced by _scatter.txt)
       baseline_labels= baseline to subtract from the labels
    OUTPUT:
       (none; just writes the output to a file)
    HISTORY:
       2015-02-28 - Written - Bovy (IAS)
       2018-02-05 - Updated to account for changing detector ranges - Price-Jones (UofT)
    """
    # Read the training data
    loc_ids, ap_ids, labels= _read_training(trainingfilename)
    new_labels= (labels[0]-baseline_labels[0],)
    for ii in range(1,len(labels)):
        new_labels= new_labels+(labels[ii]-baseline_labels[ii],)
    labels= new_labels
    # Load the spectra for these data
    aspcapBlu_start,aspcapGre_start,aspcapRed_start,aspcapTotal = _aspcapPixelLimits(dr=None)
    spec= numpy.empty((len(loc_ids),aspcapTotal))
    specerr= numpy.empty((len(loc_ids),aspcapTotal))
    for ii in range(len(loc_ids)):
        spec[ii]= apread.aspcapStar(loc_ids[ii],ap_ids[ii],ext=1,header=False,
                                    aspcapWavegrid=True)
        specerr[ii]= apread.aspcapStar(loc_ids[ii],ap_ids[ii],ext=2,
                                       header=False,
                                       aspcapWavegrid=True)
    # Train
    qout= cannon.quadfit(spec,specerr,*labels)
    # Save to file
    numpy.savetxt(outfilename,qout[0])
    numpy.savetxt(outfilename.replace('.txt','_scatter.txt'),qout[1])
    numpy.savetxt(outfilename.replace('.txt','_baseline_labels.txt'),
                  baseline_labels)
    return None
Example #8
0
def measure_apogee(allStar, linelist_obj, output_fits=False, *args, **kwargs):
    if isinstance(linelist_obj, str):
        linelist_obj = linelist.Linelist(linelist_obj)
    loc_ids, apogee_ids = make_speclist(allStar)
    lams = splot.apStarWavegrid()
    ews = np.empty([np.shape(allStar)[0], np.shape(linelist_obj.labels)[0]])
    errs = np.empty([np.shape(allStar)[0], np.shape(linelist_obj.labels)[0]])
    try:
        dr = int(_DEFAULT_DR)
    except ValueError:
        dr = 16
    if dr <= 13:
        lockey = 'LOCATION_ID'
    else:
        lockey = 'FIELD'
    for i in tqdm(range(len(apogee_ids))):
        try:
            try:
                loc, id = str(allStar[lockey][i],
                              'utf-8'), str(allStar['APOGEE_ID'][i], 'utf-8')
            except (UnicodeDecodeError, AttributeError, TypeError):
                if isinstance(allStar[lockey][i], str):
                    loc, id = allStar[lockey][i], allStar['APOGEE_ID'][i]
                else:
                    raise IOError(
                        'Input allStar file seems to be incorrectly formatted, please check that the FIELD and APOGEE_ID fields are readable, and read in as either bytes or string'
                    )
            scope = allStar['TELESCOPE'][i]
            specs, hdr = apread.aspcapStar(loc, id, ext=1, telescope=scope)
            errspec, hdr = apread.aspcapStar(loc, id, ext=2, telescope=scope)
            spec = np.dstack([lams, specs, errspec])[0]
            out = equivalentwidths.measurelinelist(spec,
                                                   linelist_obj,
                                                   error=True,
                                                   *args,
                                                   **kwargs)
            ews[i], errs[i] = out[0], out[1]
        except IOError:
            print(
                'Spectrum missing from SAS? Check that your RESULTS_VERS is set correctly.'
            )
            ews[i], errs[i] = np.ones(np.shape(
                linelist_obj.labels)[0]) * np.nan, np.ones(
                    np.shape(linelist_obj.labels)[0]) * np.nan
    return ews, errs
def get_spectra_asp(data,ext = 1):
    """
    Returns aspcapStar spectra and header information for each object specified in data 
    
    data:    labels for a subset of the APOGEE survey
    """
    specs = np.zeros((len(data),7214),dtype = np.float32)
    hdrs = {}
    goodind = []
    badind = []
    for i in range(len(data)):
        try:
            specs[i] = apread.aspcapStar(data['LOCATION_ID'][i],data['APOGEE_ID'][i],ext = ext, header = False, aspcapWavegrid=True)
            goodind.append(i)
        except IOError as e:
            badind.append(i)
            print i,data['CLUSTER'][i],' File missing'
            continue
    if badind == []:
        return specs
    if badind != []:
        return (specs,(np.array(goodind),))
Example #10
0
 def input_wrapper(*args, **kwargs):
     if len(args) >= 2 and isinstance(args[0],(list,numpy.ndarray)) \
             and isinstance(args[1],(list,numpy.ndarray)):
         # wavelength, spectrum
         return func(args[0], args[1], *args[2:], **kwargs)
     elif len(args) >= 1 and isinstance(args[0], (list, numpy.ndarray)):
         # spectrum on standard re-sampled wavelength grid
         lam = apStarWavegrid()
         if len(args[0]) == 7214:  # Input is on ASPCAP grid
             spec = numpy.zeros(len(lam))
             spec[322:3242] = args[0][:2920]
             spec[3648:6048] = args[0][2920:5320]
             spec[6412:8306] = args[0][5320:]
         else:
             spec = args[0]
         return func(lam, spec, *args[1:], **kwargs)
     elif isinstance(args[0],(int,numpy.short,str)) \
             and isinstance(args[1],str):
         # location ID and APOGEE ID (loc ID can be string for 1m sample)
         if kwargs.get('apStar', False):
             spec, hdr = apread.apStar(args[0],
                                       args[1],
                                       header=True,
                                       ext=kwargs.pop('ext', 1))
             spec = spec[numpy.amin(
                 [kwargs.pop('apStarIndx', 1),
                  len(spec) - 1])]
         else:  #aspcapStar
             spec, hdr = apread.aspcapStar(args[0],
                                           args[1],
                                           header=True,
                                           ext=kwargs.pop('ext', 1))
         lam = 10.**numpy.arange(hdr['CRVAL1'],
                                 hdr['CRVAL1'] + len(spec) * hdr['CDELT1'],
                                 hdr['CDELT1'])
         return func(lam, spec, *args[2:], **kwargs)
def get_spectra(
    name, red_clump, location
):  ###Function to read the allStar file and get the spectra, correct spectra for
    ###small and large uncertainties, remove red clump stars
    """Return cluster data, spectra, spectral errors, photometric Teffs, and bitmask from APOGEE.
	
	If the data file for the specified cluster already exists locally, 
	import the data from the file (cluster data, spectra, spectral errors, bitmask).
	If the data file does not exist, obtain the APOGEE spectra from a specified cluster 
	from the allStar catalogue, replacing ASPCAP abundances with astroNN abundances.
	
	Parameters
	----------
	name : str
		Name of desired cluster (i.e. 'NGC 2682') 
	red_clump : str
		If the red clump stars in rcsample are to be removed, set to 'True'.  If all stars are to be used,
		set to 'False'.
	location : str
		If running locally, set to 'personal'.  If running on the server, set to 'server'.
	
	Returns
	-------
	apogee_cluster_data (all stars) or apogee_cluster_data_final (red clumps removed) : structured array
		All cluster data from APOGEE
	spectra_50 (all stars) or spectra_final (red clumps removed) : tuple
		Array of floats representing the cleaned-up fluxes in the APOGEE spectra with red clump stars removed
	spectra_err_50 (all stars) or spectra_err_final (red clumps removed) : tuple
		Array of floats representing the cleaned-up spectral errors from the APOGEE spectra with red clump stars 
		removed
	good_T (all stars) or T_final (red clumps removed) : tuple
		Array of floats representing the effective temperatures of the stars in the cluster
		between 4000K and 5000K
	full_bitmask (all stars) or bitmask_final (red clumps removed) : tuple
		Array of ints (1 or 0), cleaned in the same way as the spectra, representing the bad pixels 
		in the APOGEE_PIXMASK bitmask
	"""

    #Path, strip spaces in cluster name
    if location == 'personal':  ###If running on my Mac
        path = '/Users/chloecheng/Personal/' + str(name).replace(
            ' ', '') + '.hdf5'  ###Path to folder named after cluster
    elif location == 'server':  ###If running on the server
        path = '/geir_data/scr/ccheng/AST425/Personal/' + str(name).replace(
            ' ', '') + '.hdf5'  ###Path to cluster folder

    #If the data file for this cluster exists, save the data to variables and return them
    if glob.glob(path):  ###If the file exists
        if red_clump == 'False':  ###If we're keeping all of the stars, read in the data
            file = h5py.File(path, 'r')
            apogee_cluster_data = file['apogee_cluster_data'][()]
            spectra_50 = file['spectra'][()]
            spectra_err_50 = file['spectra_errs'][()]
            good_T = file['T'][()]
            full_bitmask = file['bitmask'][()]
            file.close()
            print(name,
                  ' complete.')  ###Notification that this function is done
            return apogee_cluster_data, spectra_50, spectra_err_50, good_T, full_bitmask

        elif red_clump == 'True':  ###If we're removing the red clumps, read in the data
            file = h5py.File(path, 'r')
            apogee_cluster_data_final = file['apogee_cluster_data'][()]
            spectra_final = file['spectra'][()]
            spectra_err_final = file['spectra_errs'][()]
            T_final = file['T'][()]
            bitmask_final = file['bitmask'][()]
            file.close()
            print(name,
                  ' complete.')  ###Notification that this function is done
            return apogee_cluster_data_final, spectra_final, spectra_err_final, T_final, bitmask_final

    #If the file does not exist, get the data from APOGEE
    else:  ###If the file does not exist
        #Get red clump stars from rcsample
        rc_data = rcsample(dr='14')  ###Get the rcsample data for DR14
        rc_stars = []  ###Empty list for the stars
        for i in range(len(rc_data)):  ###Iterate through the rcsample data
            if location == 'personal':  ###If running on Mac
                rc_stars.append(
                    rc_data[i][2])  ###Append just the names of the stars
            elif location == 'server':  ###If running on server
                rc_stars.append(
                    rc_data[i][2].decode('UTF-8')
                )  ###Append just the names of the stars (decode because on server the names are bitwise for some reason)
        rc_stars = np.array(
            rc_stars)  ###Make list of red clump star names into array

        #Read in APOGEE catalogue data, removing duplicated stars and replacing ASPCAP with astroNN abundances
        apogee_cat = apread.allStar(
            use_astroNN_abundances=True
        )  ###Read the allStar file, using the astroNN abundances
        unique_apoids, unique_inds = np.unique(
            apogee_cat['APOGEE_ID'], return_index=True)  ###Get the APOGEE IDs
        apogee_cat = apogee_cat[unique_inds]  ###Get the APOGEE IDs

        #Read in overall cluster information
        cls = afits.open('occam_cluster-DR14.fits')  ###Read in the OCCAM data
        cls = cls[1].data  ###Get the cluster information

        #Read in information about cluster members
        members = afits.open(
            'occam_member-DR14.fits')  ###Read in the OCCAM members data
        members = members[1].data  ###Get the member information

        #Select all members of a given cluster
        cluster_members = (members['CLUSTER'] == name) & (
            members['MEMBER_FLAG'] == 'GM'
        )  #second part of the mask indicates to only use giant stars
        member_list = members[
            cluster_members]  ###Make a list of all member stars in the cluster

        #Find APOGEE entries for that cluster
        #numpy.in1d finds the 1D intersection between two lists.
        #In this case we're matching using the unique APOGEE ID assigned to each star
        #The indices given by numpy.in1d are for the first argument, so in this case the apogee catalogue
        cluster_inds = np.in1d((apogee_cat['APOGEE_ID']).astype('U100'),
                               member_list['APOGEE_ID']
                               )  ###Get the indices of the cluster members
        apogee_cluster_data = apogee_cat[
            cluster_inds]  ###Get the allStar data for these members
        T = photometric_Teff(
            apogee_cluster_data
        )  ###Compute the photometric effective temperature

        #Mark red clump stars in the members of the cluster as NaNs
        cluster_stars = member_list[
            'APOGEE_ID']  ###Get a list of all the names of the member stars in the cluster
        cluster_marked = np.copy(
            cluster_stars
        )  ###Create a copy of this list to mark which stars are red clumps
        for i in range(len(cluster_stars)
                       ):  ###Iterate through all of the stars in the cluster
            for j in range(len(
                    rc_stars)):  ###Iterate through all of the rcsample stars
                if cluster_stars[i] in rc_stars[
                        j]:  ###If a cluster member is also a member of the rcsample stars
                    cluster_marked[
                        i] = np.nan  ###Replace the name of that star with a NaN to ignore it

        #Get spectra, spectral errors, and bitmask for each star - apStar
        #We can use the APOGEE package to read each star's spectrum
        #We'll read in the ASPCAP spectra, which have combined all of the visits for each star and removed the spaces between the spectra
        number_of_members = len(
            member_list)  ###Number of members in the cluster
        spectra = np.zeros((number_of_members,
                            7514))  ###Create an empty array to add the spectra
        spectra_errs = np.zeros(
            (number_of_members,
             7514))  ###Create an empty array to add the spectral errors
        bitmask = np.zeros((number_of_members,
                            7514))  ###Create an empty array to add the bitmask
        for s, star in enumerate(
                apogee_cluster_data):  ###Iterate through the allStar data
            spectra[s] = apread.aspcapStar(
                star['LOCATION_ID'],
                star['APOGEE_ID'],
                ext=1,
                header=False,
                dr='14',
                aspcapWavegrid=True)  ###Get the spectra
            spectra_errs[s] = apread.aspcapStar(
                star['LOCATION_ID'],
                star['APOGEE_ID'],
                ext=2,
                header=False,
                dr='14',
                aspcapWavegrid=True)  ###Get the spectral errors
            bitmask[s] = apread.apStar(
                star['LOCATION_ID'],
                star['APOGEE_ID'],
                ext=3,
                header=False,
                dr='14',
                aspcapWavegrid=True)[1]  ###Get the bitmask

        #Set all entries in bitmask to integers
        bitmask = bitmask.astype(
            int)  ###Set all entries in the bitmask to integers
        bitmask_flip = np.zeros_like(
            bitmask
        )  ###Create an empty array for the bitmask with flipped entries
        for i in range(
                len(spectra
                    )):  ###Iterate through the number of stars in the cluster
            for j in range(7514):  ###Iterate through the wavelength range
                if bitmask[i][j] == 0:  ###If the bitmask entry is set to 0
                    bitmask_flip[i][j] = 1  ###Set it to 1
                else:  ###If the bitmask entry is not set to 0
                    bitmask_flip[i][j] = 0  ###Set it to 0
        ###I do this part because the unmasked entries are always 0 in the original bitmask but I think before I was maybe adding in other values to include in the mask that may not have necessarily been 1 so I just set all masked bits to 0 and all unmasked bits to 1 (or maybe this just made more sense in my head for masked to be 0 and unmasked to be 1)

        #Remove empty spectra
        full_spectra = [
        ]  ###Empty list for the spectra sans empty ones, list not array because we don't know how many stars will be eliminated
        full_spectra_errs = [
        ]  ###Empty list for the spectral errors sans empty spectra
        full_bitmask = []  ###Empty list for bitmask sans empty spectra
        full_T = []  ###Empty list for temperatures sans empty spectra
        full_stars = []  ###Empty list for indices of stars sans empty spectra
        for i in range(len(spectra)):  ###Iterate through the number of stars
            if any(spectra[i, :] != 0
                   ):  ###For all of the rows whose entries are not all 0
                full_spectra.append(spectra[i])  ###Append those spectra
                full_spectra_errs.append(
                    spectra_errs[i])  ###Append those spectral errors
                full_bitmask.append(
                    bitmask_flip[i])  ###Append those bitmask rows
                full_T.append(T[i])  ###Append those temperatures
                full_stars.append(i)  ###Append the indices of those stars
        full_spectra = np.array(full_spectra)  ###Make list into array
        full_spectra_errs = np.array(
            full_spectra_errs)  ###Make list into array
        full_bitmask = np.array(full_bitmask)  ###Make list into array
        full_T = np.array(full_T)  ###Make list into array
        full_stars = np.array(full_stars)  ###Make list into array
        full_marked_stars = cluster_marked[
            full_stars]  ###Use array of stars left to index marked stars so we know which ones are red clump stars

        #Create array of NaNs to replace flagged values in spectra
        masked_spectra = np.empty_like(
            full_spectra
        )  ###Create an empty array that is the same shape as full_spectra
        masked_spectra_errs = np.empty_like(
            full_spectra_errs
        )  ###Create an empty array that is the same shape as full_spectra_errs
        masked_spectra[:] = np.nan  ###Set all of the entries to NaNs
        masked_spectra_errs[:] = np.nan  ###Set all of the entries to NaNs

        #Mask the spectra
        for i in range(
                len(full_spectra)):  ###Iterate through the number of stars
            for j in range(7514):  ###Iterate through the wavelength range
                if full_bitmask[i][
                        j] != 0:  ###If the bitmask is not 0 (i.e. if the bit is unmasked)
                    masked_spectra[i][j] = full_spectra[i][
                        j]  ###Retain the value of the unmasked spectra here
                    masked_spectra_errs[i][j] = full_spectra_errs[i][
                        j]  ###Retain the value of the unmasked spectral errors here
        ###All of the masked bits that were not captured by this if statement will remain NaNs and will thus be ignored

        #Cut stars that are outside of the temperature limits
        good_T_inds = (full_T > 4000) & (
            full_T < 5000
        )  ###Get the indices of the temperatures that are between 4000K and 5000K
        final_spectra = masked_spectra[
            good_T_inds]  ###Index the spectra to only keep stars that are within the temperature limits
        final_spectra_errs = masked_spectra_errs[
            good_T_inds]  ###Index the spectral errors to only keep stars within Teff limits
        good_T = full_T[
            good_T_inds]  ###Index the temperatures to keep only stars within Teff limits
        apogee_cluster_data = apogee_cluster_data[
            good_T_inds]  ###Index the allStar data to keep stars only within Teff limits
        full_bitmask = full_bitmask[
            good_T_inds]  ###Index the bitmask to keep stars only within Teff limits
        final_stars = full_marked_stars[
            good_T_inds]  ###Index the array of red-clump-marked stars to keep only those within Teff limits
        rgs = (final_stars != 'nan'
               )  #Get indices for final red giant stars to be used

        #Want an SNR of 200 so set those errors that have a larger SNR to have an SNR of 200
        spectra_err_200 = np.zeros_like(
            final_spectra_errs
        )  ###Create an empty array to add corrected spectral errors to - shape will not change, just altering values
        for i in range(len(final_spectra)):  ###Iterate through the stars
            for j in range(7514):  ###Iterate through wavelength range
                if final_spectra[i][j] / final_spectra_errs[i][
                        j] <= 200:  ###If errors are of a reasonable size
                    spectra_err_200[i][j] = final_spectra_errs[i][
                        j]  ###Leave them as they are
                else:  ###If errors are too small
                    spectra_err_200[i][j] = final_spectra[i][
                        j] / 200  ###Make them a bit bigger

        #Cut errors with SNR of less than 50
        spectra_50 = np.copy(
            final_spectra
        )  ###Create a copy of the spectra to cut large error pixels
        spectra_err_50 = np.copy(
            spectra_err_200
        )  ###Create a copy of the spectral errors to cut large error pixels

        for i in range(len(final_spectra)):  ###Iterate through stars
            for j in range(7514):  ###Iterate through wavelength range
                if final_spectra[i][j] / spectra_err_200[i][
                        j] <= 50:  ###If an error is too big
                    spectra_50[i][
                        j] = np.nan  ###Set the corresponding entry in the spectra to be a NaN, will be ignored
                    spectra_err_50[i][
                        j] = np.nan  ###Set the corresponding entry in the spectral errors to be a NaN, will be ignored

        #Cut red clumps
        logg = apogee_cluster_data[
            'LOGG']  ###Get the logg values for the cluster (all corrections have been applied)
        apogee_cluster_data_final = apogee_cluster_data[
            rgs]  ###Get the allStar data for the RGB stars only (no red clumps)
        spectra_final = spectra_50[
            rgs]  ###Get the spectra for the RGB stars only
        spectra_err_final = spectra_err_50[
            rgs]  ###Get the spectral errors for the RGB stars only
        T_final = good_T[rgs]  ###Get the temperatures for the RGB stars only
        bitmask_final = full_bitmask[
            rgs]  ###Get the bitmask for the RGB stars only

        if red_clump == 'False':  ###If we are looking at all of the stars, save all data before red clumps were cut to file
            #Write to file
            file = h5py.File(path, 'w')
            file['apogee_cluster_data'] = apogee_cluster_data
            file['spectra'] = spectra_50
            file['spectra_errs'] = spectra_err_50
            file['T'] = good_T
            file['bitmask'] = full_bitmask
            file.close()
            print(name, 'complete')  ###Notification that this function is done

            return apogee_cluster_data, spectra_50, spectra_err_50, good_T, full_bitmask

        elif red_clump == 'True':  ###If we are removing the red clump stars, save the data after red clumps cut to file
            #Write to file
            file = h5py.File(path, 'w')
            file['apogee_cluster_data'] = apogee_cluster_data_final
            file['spectra'] = spectra_final
            file['spectra_errs'] = spectra_err_final
            file['T'] = T_final
            file['bitmask'] = bitmask_final
            file.close()
            print(name, 'complete')  ###Notification that this function is done

            return apogee_cluster_data_final, spectra_final, spectra_err_final, T_final, bitmask_final
def get_spectra(name, red_clump, location):
	"""Return cluster data, spectra, spectral errors, photometric Teffs, and bitmask from APOGEE.
	
	If the data file for the specified cluster already exists locally, 
	import the data from the file (cluster data, spectra, spectral errors, bitmask).
	If the data file does not exist, obtain the APOGEE spectra from a specified cluster 
	from the allStar catalogue, replacing ASPCAP abundances with astroNN abundances.
	
	Parameters
	----------
	name : str
		Name of desired cluster (i.e. 'NGC 2682') 
	red_clump : str
		If the red clump stars in rcsample are to be removed, set to 'True'.  If all stars are to be used,
		set to 'False'.
	location : str
		If running locally, set to 'personal'.  If running on the server, set to 'server'.
	
	Returns
	-------
	apogee_cluster_data (all stars) or apogee_cluster_data_final (red clumps removed) : structured array
		All cluster data from APOGEE
	spectra_50 (all stars) or spectra_final (red clumps removed) : tuple
		Array of floats representing the cleaned-up fluxes in the APOGEE spectra with red clump stars removed
	spectra_err_50 (all stars) or spectra_err_final (red clumps removed) : tuple
		Array of floats representing the cleaned-up spectral errors from the APOGEE spectra with red clump stars 
		removed
	good_T (all stars) or T_final (red clumps removed) : tuple
		Array of floats representing the effective temperatures of the stars in the cluster
		between 4000K and 5000K
	full_bitmask (all stars) or bitmask_final (red clumps removed) : tuple
		Array of ints (1 or 0), cleaned in the same way as the spectra, representing the bad pixels 
		in the APOGEE_PIXMASK bitmask
	"""
	
	#Path, strip spaces in cluster name
	if location == 'personal':
		path = '/Users/chloecheng/Personal/' + str(name).replace(' ', '') + '.hdf5'
	elif location == 'server':
		path = '/geir_data/scr/ccheng/AST425/Personal/' + str(name).replace(' ', '') + '.hdf5' 
		
	#If the data file for this cluster exists, save the data to variables
	if glob.glob(path):
		if red_clump == 'False':
			file = h5py.File(path, 'r')
			apogee_cluster_data = file['apogee_cluster_data'][()]
			spectra_50 = file['spectra'][()]
			spectra_err_50 = file['spectra_errs'][()]
			good_T = file['T'][()]
			full_bitmask = file['bitmask'][()]
			file.close()
			print(name, ' complete.')
			return apogee_cluster_data, spectra_50, spectra_err_50, good_T, full_bitmask
		
		elif red_clump == 'True':
			file = h5py.File(path, 'r')
			apogee_cluster_data_final = file['apogee_cluster_data'][()]
			spectra_final = file['spectra'][()]
			spectra_err_final = file['spectra_errs'][()]
			T_final = file['T'][()]
			bitmask_final = file['bitmask'][()]
			file.close()
			print(name, ' complete.')
			return apogee_cluster_data_final, spectra_final, spectra_err_final, T_final, bitmask_final
		
	#If the file does not exist, get the data from APOGEE
	else:
		#Get red clump stars from rcsample
		rc_data = rcsample(dr='14')
		rc_stars = []
		for i in range(len(rc_data)):
			#rc_stars.append(rc_data[i][2]) - REMOVE IN FINAL VERSION
			rc_stars.append(rc_data[i][2].decode('UTF-8'))
		rc_stars = np.array(rc_stars)
	
		#Read in APOGEE catalogue data, removing duplicated stars and replacing ASPCAP with astroNN abundances
		apogee_cat = apread.allStar(use_astroNN_abundances=True)
		unique_apoids,unique_inds = np.unique(apogee_cat['APOGEE_ID'],return_index=True)
		apogee_cat = apogee_cat[unique_inds]
		
		#Read in overall cluster information
		cls = afits.open('occam_cluster-DR14.fits')
		cls = cls[1].data
		
		#Read in information about cluster members
		members = afits.open('occam_member-DR14.fits')
		members = members[1].data
		
		#Select all members of a given cluster
		cluster_members = (members['CLUSTER']==name) & (members['MEMBER_FLAG']=='GM') #second part of the mask indicates to only use giant stars
		member_list = members[cluster_members]
		
		#Find APOGEE entries for that cluster
		#numpy.in1d finds the 1D intersection between two lists. 
		#In this case we're matching using the unique APOGEE ID assigned to each star
		#The indices given by numpy.in1d are for the first argument, so in this case the apogee catalogue
		cluster_inds = np.in1d((apogee_cat['APOGEE_ID']).astype('U100'),member_list['APOGEE_ID'])
		apogee_cluster_data = apogee_cat[cluster_inds]
		T = photometric_Teff(apogee_cluster_data)
		
		#Mark red clump stars in the members of the cluster as NaNs
		cluster_stars = member_list['APOGEE_ID']
		cluster_marked = np.copy(cluster_stars)
		for i in range(len(cluster_stars)):
			for j in range(len(rc_stars)):
				if cluster_stars[i] == rc_stars[j]:
					cluster_marked[i] = np.nan
		
		#Get spectra, spectral errors, and bitmask for each star - apStar
		#We can use the APOGEE package to read each star's spectrum
		#We'll read in the ASPCAP spectra, which have combined all of the visits for each star and removed the spaces between the spectra
		number_of_members = len(member_list)
		spectra = np.zeros((number_of_members, 7514))
		spectra_errs = np.zeros((number_of_members, 7514))
		bitmask = np.zeros((number_of_members, 7514))
		for s,star in enumerate(apogee_cluster_data):
			spectra[s] = apread.aspcapStar(star['LOCATION_ID'],star['APOGEE_ID'],ext=1,header=False,dr='14',aspcapWavegrid=True)
			spectra_errs[s] = apread.aspcapStar(star['LOCATION_ID'],star['APOGEE_ID'],ext=2,header=False,dr='14',aspcapWavegrid=True)
			bitmask[s] = apread.apStar(star['LOCATION_ID'],star['APOGEE_ID'],ext=3,header=False,dr='14', aspcapWavegrid=True)[1]
		
		#Set all entries in bitmask to integers	
		bitmask = bitmask.astype(int)
		bitmask_flip = np.zeros_like(bitmask)
		for i in range(len(spectra)):
			for j in range(7514):
				if bitmask[i][j] == 0:
					bitmask_flip[i][j] = 1
				else:
					bitmask_flip[i][j] = 0
					
		#Remove empty spectra
		full_spectra = []
		full_spectra_errs = []
		full_bitmask = []
		full_T = [] 
		full_stars = [] 
		for i in range(len(spectra)):
			if any(spectra[i,:] != 0):
				full_spectra.append(spectra[i])
				full_spectra_errs.append(spectra_errs[i])
				full_bitmask.append(bitmask_flip[i])
				full_T.append(T[i]) 
				full_stars.append(i) 
		full_spectra = np.array(full_spectra)
		full_spectra_errs = np.array(full_spectra_errs)
		full_bitmask = np.array(full_bitmask)
		full_T = np.array(full_T) 
		full_stars = np.array(full_stars) 
		full_marked_stars = cluster_marked[full_stars] 
		
		#Create array of NaNs to replace flagged values in spectra
		masked_spectra = np.empty_like(full_spectra)
		masked_spectra_errs = np.empty_like(full_spectra_errs)
		masked_spectra[:] = np.nan
		masked_spectra_errs[:] = np.nan
		
		#Mask the spectra
		for i in range(len(full_spectra)):
			for j in range(7514):
				if full_bitmask[i][j] != 0:
					masked_spectra[i][j] = full_spectra[i][j]
					masked_spectra_errs[i][j] = full_spectra_errs[i][j]
					
		#Cut stars that are outside of the temperature limits 
		good_T_inds = (full_T > 4000) & (full_T < 5000)
		final_spectra = masked_spectra[good_T_inds]
		final_spectra_errs = masked_spectra_errs[good_T_inds]
		good_T = full_T[good_T_inds]
		apogee_cluster_data = apogee_cluster_data[good_T_inds]
		full_bitmask = full_bitmask[good_T_inds]
		final_stars = full_marked_stars[good_T_inds] 
		rgs = (final_stars != 'nan') #Get indices for final red giant stars to be used
		
		#Want an SNR of 200 so set those errors that have a larger SNR to have an SNR of 200
		spectra_err_200 = np.zeros_like(final_spectra_errs)
		for i in range(len(final_spectra)):
			for j in range(7514):
				if final_spectra[i][j]/final_spectra_errs[i][j] <= 200:
					spectra_err_200[i][j] = final_spectra_errs[i][j]
				else:
					spectra_err_200[i][j] = final_spectra[i][j]/200
					
		#Cut errors with SNR of less than 50
		spectra_50 = np.copy(final_spectra)
		spectra_err_50 = np.copy(spectra_err_200)
		
		for i in range(len(final_spectra)):
			for j in range(7514):
				if final_spectra[i][j]/spectra_err_200[i][j] <= 50:
					spectra_50[i][j] = np.nan
					spectra_err_50[i][j] = np.nan
		
		#Cut red clumps
		logg = apogee_cluster_data['LOGG']
		apogee_cluster_data_final = apogee_cluster_data[rgs]
		spectra_final = spectra_50[rgs]
		spectra_err_final = spectra_err_50[rgs]
		T_final = good_T[rgs]
		bitmask_final = full_bitmask[rgs]
		
		if red_clump == 'False':
			#Write to file
			file = h5py.File(path, 'w')
			file['apogee_cluster_data'] = apogee_cluster_data
			file['spectra'] = spectra_50
			file['spectra_errs'] = spectra_err_50
			file['T'] = good_T
			file['bitmask'] = full_bitmask
			file.close()
			print(name, 'complete')
			
			return apogee_cluster_data, spectra_50, spectra_err_50, good_T, full_bitmask
		
		elif red_clump == 'True':
			#Write to file 
			file = h5py.File(path, 'w')
			file['apogee_cluster_data'] = apogee_cluster_data_final
			file['spectra'] = spectra_final
			file['spectra_errs'] = spectra_err_final
			file['T'] = T_final
			file['bitmask'] = bitmask_final
			file.close()
			print(name, 'complete')
			
			return apogee_cluster_data_final, spectra_final, spectra_err_final, T_final, bitmask_final
Example #13
0
def plot_afe_spectra(savename,plotname):
    # Load the data
    data= define_rcsample.get_rcsample()
    data= data[data['SNR'] > 200.]
    fehindx= (data['FE_H'] <= -0.35)*(data['FE_H'] > -0.45)
    fehdata= data[fehindx]
    # First compute the residuals and do the EM-PCA smoothing
    if not os.path.exists(savename):
        nspec= len(fehdata)
        spec= numpy.zeros((nspec,7214))
        specerr= numpy.zeros((nspec,7214))
        for ii in range(nspec):
            sys.stdout.write('\r'+"Loading spectrum %i / %i ...\r" % (ii+1,nspec))
            sys.stdout.flush()
            spec[ii]= apread.aspcapStar(fehdata['LOCATION_ID'][ii],
                                        fehdata['APOGEE_ID'][ii],
                                        ext=1,header=False,aspcapWavegrid=True)
            specerr[ii]= apread.aspcapStar(fehdata['LOCATION_ID'][ii],
                                           fehdata['APOGEE_ID'][ii],
                                           ext=2,header=False,
                                           aspcapWavegrid=True)
        teffs= fehdata['FPARAM'][:,paramIndx('teff')]
        loggs= fehdata['FPARAM'][:,paramIndx('logg')]
        metals= fehdata[define_rcsample._FEHTAG]
        cf, s, r= apcannon.quadfit(spec,specerr,
                                   teffs-4800.,loggs-2.85,metals+0.3,
                                   return_residuals=True)
        pr= numpy.zeros_like(r)
        # Deal w/ bad data
        _MAXERR= 0.02
        npca= 8
        pca_input= r
        pca_weights= (1./specerr**2.)
        pca_weights[pca_weights < 1./_MAXERR**2.]= 0. 
        nanIndx= numpy.isnan(pca_input) + numpy.isnan(pca_weights)
        pca_weights[nanIndx]= 0. 
        pca_input[nanIndx]= 0.
        # Run EM-PCA
        m= empca.empca(pca_input,pca_weights,nvec=npca,niter=25)#,silent=False)
        for jj in range(nspec):
            for kk in range(npca):
                pr[jj]+= m.coeff[jj,kk]*m.eigvec[kk]
        save_pickles(savename,pr,r,cf)
    else:
        with open(savename,'rb') as savefile:
            pr= pickle.load(savefile)
    # Now plot the various elements
    colormap= cm.seismic
    colorFunc= lambda afe: afe/0.25
    widths= [3.5,2.]
    yranges= [[-0.05,0.02],[-0.03,0.01]]
    for ee, elem in enumerate(['S','Ca1']):
        for ii in range(5):
            tindx= (fehdata[define_rcsample._AFETAG] > ii*0.05-0.025)\
                *(fehdata[define_rcsample._AFETAG] <= (ii+1)*0.05-0.025)
            args= (apstack.median(pr[tindx][:12]),elem,)
            kwargs= {'markLines':ii==4,
                     'yrange':yranges[ee],
                     'ylabel':'',
                     'cleanZero':False,
                     'zorder':int(numpy.floor(numpy.random.uniform()*5)),
                     'color':colormap(colorFunc(ii*0.05)),
                     'overplot':ii>0,
                     'fig_width':widths[ee]}
            if ii>0: kwargs.pop('fig_width')
            splot.windows(*args,**kwargs)
        bovy_plot.bovy_end_print(plotname.replace('ELEM',
                                                  elem.lower().capitalize()))
    # Also do Mg
    for ii in range(5):
        tindx= (fehdata[define_rcsample._AFETAG] > ii*0.05-0.025)\
            *(fehdata[define_rcsample._AFETAG] <= (ii+1)*0.05-0.025)
        args= (apstack.median(pr[tindx][:12]),)
        kwargs={'startindxs':[3012,3120,3990],
                'endindxs':[3083,3158,4012],
                'yrange':[-0.05,0.02],
                'ylabel':'',
                'cleanZero':False,
                '_markwav':[15745.017,15753.189,15770.055,15958.836],
                'zorder':int(numpy.floor(numpy.random.uniform()*5)),
                'color':colormap(colorFunc(ii*0.05)),
                'overplot':ii>0,
                'fig_width':4.5,
                'markLines':True}
        if ii>0: kwargs.pop('fig_width')
        if ii != 4: 
            kwargs.pop('_markwav')
            kwargs.pop('markLines')
        kwargs['_startendskip']= 0
        kwargs['_noxticks']= True
        kwargs['_labelwav']= True
        splot.waveregions(*args,**kwargs)                          
        bovy_plot.bovy_text(r'$\mathrm{Mg}$',
                            top_left=True,fontsize=10,backgroundcolor='w')
    bovy_plot.bovy_end_print(plotname.replace('ELEM','Mg'))
    # Also do Si
    for ii in range(5):
        tindx= (fehdata[define_rcsample._AFETAG] > ii*0.05-0.025)\
            *(fehdata[define_rcsample._AFETAG] <= (ii+1)*0.05-0.025)
        args= (apstack.median(pr[tindx][:12]),)
        kwargs={'startindxs':[4469, 4624,5171, 7205, 7843],
                'endindxs':[4488, 4644,5182, 7243, 7871],
                'yrange':[-0.05,0.02],
                'ylabel':'',
                'cleanZero':False,
                '_markwav':apwindow.lines('Si'),
                'zorder':int(numpy.floor(numpy.random.uniform()*5)),
                'color':colormap(colorFunc(ii*0.05)),
                'overplot':ii>0,
                'fig_width':6.,
                'markLines':True}
        if ii>0: kwargs.pop('fig_width')
        if ii != 4: 
            kwargs.pop('_markwav')
            kwargs.pop('markLines')
        kwargs['_startendskip']= 0
        kwargs['_noxticks']= True
        kwargs['_labelwav']= True
        splot.waveregions(*args,**kwargs)                          
        bovy_plot.bovy_text(r'$\mathrm{Si}$',
                            top_left=True,fontsize=10,backgroundcolor='w')
    bovy_plot.bovy_end_print(plotname.replace('ELEM','Si2'))
    # Also do Oxygen
    for ii in range(5):
        tindx= (fehdata[define_rcsample._AFETAG] > ii*0.05-0.025)\
            *(fehdata[define_rcsample._AFETAG] <= (ii+1)*0.05-0.025)
        args= (apstack.median(pr[tindx][:12]),)
        kwargs={'startlams':[15558,16242,16536,16720],
                'endlams':[15566,16250,16544,16728],
                'yrange':[-0.05,0.02],
                'ylabel':'',
                'cleanZero':False,
                '_markwav':[15562,16246,16539,16723.5],
                'zorder':int(numpy.floor(numpy.random.uniform()*5)),
                'color':colormap(colorFunc(ii*0.05)),
                'overplot':ii>0,
                'fig_width':5.,
                'markLines':True}
        if ii>0: kwargs.pop('fig_width')
        if ii != 4: 
            kwargs.pop('_markwav')
            kwargs.pop('markLines')
        kwargs['_startendskip']= 0
        kwargs['_noxticks']= True
        kwargs['_labelwav']= True
        splot.waveregions(*args,**kwargs)                          
        bovy_plot.bovy_text(r'$\mathrm{O}$',
                            top_left=True,fontsize=10,backgroundcolor='w')
    bovy_plot.bovy_end_print(plotname.replace('ELEM','O'))
    # Also do Ti
    for ii in range(5):
        tindx= (fehdata[define_rcsample._AFETAG] > ii*0.05-0.025)\
            *(fehdata[define_rcsample._AFETAG] <= (ii+1)*0.05-0.025)
        args= (apstack.median(pr[tindx][:12]),)
        kwargs={'startindxs':[1116,2100,2899],
                'endindxs':[1146,2124,2922],
                'yrange':[-0.05,0.02],
                'ylabel':'',
                'cleanZero':False,
                '_markwav':apwindow.lines('Ti'),
                'zorder':int(numpy.floor(numpy.random.uniform()*5)),
                'color':colormap(colorFunc(ii*0.05)),
                'overplot':ii>0,
                'fig_width':3.5,
                'markLines':True}
        if ii>0: kwargs.pop('fig_width')
        if ii != 4: 
            kwargs.pop('_markwav')
            kwargs.pop('markLines')
        kwargs['_startendskip']= 0
        kwargs['_noxticks']= True
        kwargs['_labelwav']= True
        splot.waveregions(*args,**kwargs)                          
        bovy_plot.bovy_text(r'$\mathrm{Ti}$',
                            top_left=True,fontsize=10,backgroundcolor='w')
    bovy_plot.bovy_end_print(plotname.replace('ELEM','Ti'))
    return None
Example #14
0
nan_ranges = [(nan_vals[i] + 1, nan_vals[i+1]) for i in range(len(nan_vals) - 1) if nan_vals[i+1]!=nan_vals[i]+1]

print(nan_ranges)
specChunk = spec[0][nan_ranges[0][0]:nan_ranges[0][1]]
errChunk = err[0][nan_ranges[0][0]:nan_ranges[0][1]]'''

aspec= apread.apStar(locationID, apogeeID, ext=1, header=False)[1]
aspecerr= apread.apStar(locationID, apogeeID, ext=2, header=False)[1]
# Input needs to be (nspec,nwave)
aspec= np.reshape(aspec,(1,len(aspec)))
aspecerr= np.reshape(aspecerr,(1,len(aspecerr)))
# Fit the continuum
from apogee.spec import continuum
cont= continuum.fit(aspec,aspecerr,type='aspcap')

cspec= apread.aspcapStar(locationID, apogeeID,ext=1,header=False)
import apogee.spec.plot as splot
splot.waveregions(aspec[0]/cont[0])
splot.waveregions(cspec,overplot=True)

'''params = ferre.fit(locationID, apogeeID,
							teff=teff1, fixteff=True,
							logg=logg, fixlogg=True,
							metals=metals, fixmetals=False,
							am=am, fixam=False,
							nm=nm, fixnm=False,
							cm=cm, fixcm=False,
							verbose=True)
'''

'''mspec1= ferre.interpolate(params[0][1],params[0][2],params[0][3],params[0][4],params[0][5],params[0][6])
def bovy_metallicity_gradient(plotfilename,savefilename,largewave=False):
    # First read the RC catalog and cut it to stars near the plane
    data= apread.rcsample()
    if _HIZ:
        indx= (numpy.fabs(data['RC_GALZ']) > 0.6)*(data['METALS'] > -1000.)
    else:
        indx= (numpy.fabs(data['RC_GALZ']) < 0.25)*(data['METALS'] > -1000.)
    data= data[indx]
    # Now go through bins in R
    Rmin, Rmax, dR= 5.5, 13., 0.1
    Rs= numpy.arange(Rmin+dR/2.,Rmax+dR/2.,dR)
    nR= len(Rs)
    # Read one spectrum to learn the size of the array
    spec, hdr= apread.aspcapStar(4424,'2M00025587+5849278',ext=1)
    if os.path.exists(savefilename):
        # Reload previously calculated median spectrum
        savefile= open(savefilename,'rb')
        median_spec= pickle.load(savefile)
        savefile.close()
    else:
        median_spec= numpy.zeros((len(spec),nR))
        # Now run through all the spectra and get the median
        tot= 0
        for ii in range(nR):
            indx= (data['RC_GALR'] >= Rs[ii]-dR/2.)\
                *(data['RC_GALR'] < Rs[ii]+dR/2.)
            tot+= numpy.sum(indx)
            print numpy.sum(indx), tot
            allspec= numpy.empty((len(spec),numpy.sum(indx)))
            for jj in range(numpy.sum(indx)):
                specdata= \
                    apread.aspcapStar(data['LOCATION_ID'][indx][jj],
                                      data['APOGEE_ID'][indx][jj],
                                      ext=1,header=False)
                specerr= \
                    apread.aspcapStar(data['LOCATION_ID'][indx][jj],
                                      data['APOGEE_ID'][indx][jj],
                                      ext=2,header=False)
                allspec[:,jj]= specdata
                allspec[specerr > 1.,jj]= numpy.nan
                allspec[specerr == 0.,jj]= numpy.nan
            for jj in range(len(spec)):
                if _PLOTMAD:
                    median_spec[jj,ii]= \
                        numpy.median(numpy.fabs(allspec[jj,True-numpy.isnan(allspec[jj,:])]-numpy.median(allspec[jj,True-numpy.isnan(allspec[jj,:])])))
                else:
                    median_spec[jj,ii]= \
                        numpy.median(allspec[jj,
                                             True-numpy.isnan(allspec[jj,:])])
        save_pickles(savefilename,median_spec)
    # Wavelengths
    wave= 10.**(numpy.arange(hdr['CRVAL1'],
                             hdr['CRVAL1']+len(spec)*hdr['CDELT1'],
                             hdr['CDELT1']))
    # Normalization, first calculate the spectrum near Ro
    absmax= 0.98
    absindx= (median_spec > absmax)*\
        numpy.tile(( (wave < 15929.)+(wave > 15929.5) ),(nR,1)).T
    median_spec[absindx]= absmax #focus on real absorption lines, except for V
    rospec= numpy.zeros(len(spec))
    roindx= numpy.argmin(numpy.fabs(Rs-8.))
    for jj in range(len(spec)):
        rospec[jj]= numpy.median(median_spec[jj,roindx-3:roindx+4])
    if not _PLOTMAD:
        # Normalization by spectrum at Ro
        roindx= numpy.argmin(numpy.fabs(Rs-8.))
        for jj in range(nR):
            # Normalize by the solar radius
            median_spec[:,jj]/= rospec
        median_spec-= 1. 
        vmin=-0.035
        vmax=0.035
        vmin=-0.04
        vmax=0.04
    # Now plot
    if False:
        startindx, endindx= 3652, 4100#3915
    if largewave:
        startindx, endindx= 7000,7600#7375, 7889
    else:
        startindx, endindx= 2500, 3100
    # Make N separate plots showing different wavelength regions
    bovy_plot.bovy_print(fig_width=8.4,fig_height=3.,
                         axes_labelsize=10,text_fontsize=9,legend_fontsize=9,
                         xtick_labelsize=8,ytick_labelsize=8)
    startindxs= [322,1790,2707,3852,4738,5820,7187] #DIB is at 818
    endindxs= [590,1940,2857,4021,5068,5955,7400]
    nregions= len(startindxs)
    # Calculate the width of the plot
    dx= numpy.array([endindxs[ii]-startindxs[ii] for ii in range(nregions)],
                    dtype='float')
    specdx= numpy.sum(dx) # for later
    dx/= numpy.sum(dx)
    totdx= 0.85
    skipdx= 0.015
    dx*= (totdx-(nregions-1)*skipdx)
    for ii in range(nregions):
        # Setup the axes
        if ii == 0:
            left, bottom, width, height= 0.1, 0.1, dx[ii],0.8
        else:
            left, bottom, width, height= 0.1+numpy.cumsum(dx)[ii-1]+skipdx*ii,\
                0.1, dx[ii], 0.8
        thisax= pyplot.axes([left,bottom,width,height])
        fig= pyplot.gcf()
        fig.sca(thisax)
        startindx, endindx= startindxs[ii], endindxs[ii]
        aspect= (hdr['CRVAL1']+(endindx-0.5)*hdr['CDELT1']-hdr['CRVAL1']-(startindx-0.5)*hdr['CDELT1'])\
            /(Rs[-1]+dR/2.-Rs[0]+dR/2.)
        aspect/= dx[ii]*5.
        yrange= [Rs[0]-dR/2.,Rs[-1]+dR/2.]
        xrange=[hdr['CRVAL1']+(startindx-0.5)*hdr['CDELT1']-numpy.log10(_LAMBDANORM),\
                    hdr['CRVAL1']+(endindx-0.5)*hdr['CDELT1']-numpy.log10(_LAMBDANORM)]
        extent= [xrange[0],xrange[1],yrange[0],yrange[1]]
        pyplot.imshow(-median_spec[startindx:endindx,:].T,
                       origin='lower',cmap='coolwarm',
                       vmin=vmin,vmax=vmax,
                       extent=extent,
                       interpolation='nearest',
                       #interpolation='bicubic',
                       aspect=aspect)
        pyplot.axis(extent)
        #pyplot.xticks(rotation=-45.)
        pyplot.xlim(xrange[0],xrange[1])
        pyplot.ylim(yrange[0],yrange[1])
        thisax.xaxis.set_major_locator(ticker.MultipleLocator(0.001))
        bovy_plot._add_ticks()
        if ii > 0:
            nullfmt   = NullFormatter()         # no labels
            thisax.yaxis.set_major_formatter(nullfmt)
        else:
            pyplot.ylabel(r'$R\,(\mathrm{kpc})$')
        # Remove spines between different wavelength regions
        if ii == 0:
            thisax.spines['right'].set_visible(False)
            thisax.tick_params(right=False,which='both')
        elif ii == (nregions-1):
            thisax.spines['left'].set_visible(False)
            thisax.tick_params(labelleft='off')
            thisax.tick_params(left=False,which='both')
        else:
            thisax.spines['left'].set_visible(False)
            thisax.spines['right'].set_visible(False)
            thisax.tick_params(labelleft='off')
            thisax.tick_params(left=False,which='both')
            thisax.tick_params(right=False,which='both')
        # Plot cut-out markers
        d = .015 # how big to make the diagonal lines in axes coordinates
        kwargs = dict(transform=thisax.transAxes, color='k', clip_on=False)
        slope= 1./(dx[ii]+0.2*skipdx)/3.
        if ii == 0:
            thisax.plot((1-slope*d,1+slope*d),(-d,+d), **kwargs)
            thisax.plot((1-slope*d,1+slope*d),(1-d,1+d), **kwargs)
        elif ii == (nregions-1):
            thisax.plot((-slope*d,+slope*d),(-d,+d), **kwargs)
            thisax.plot((-slope*d,+slope*d),(1-d,1+d), **kwargs)
        else:
            thisax.plot((1-slope*d,1+slope*d),(-d,+d), **kwargs)
            thisax.plot((1-slope*d,1+slope*d),(1-d,1+d), **kwargs)
            thisax.plot((-slope*d,+slope*d),(-d,+d), **kwargs)
            thisax.plot((-slope*d,+slope*d),(1-d,1+d), **kwargs)
        # Draw solar line
        if ii == (nregions-1):
            xend= hdr['CRVAL1']+(endindx-0.5)*hdr['CDELT1']-numpy.log10(_LAMBDANORM)
            # Total wavelength span, incl. skipdx parts
            totaldx= hdr['CDELT1']*specdx*\
                (1.+(nregions-1)*skipdx/(totdx-(nregions-1)*skipdx))
            thisax.plot((xend-totaldx,xend),(8.,8.),
                        color='k',ls='--',lw=1.5,marker='None',clip_on=False)
        # Label the lines
        _label_all_lines(wave[startindx],wave[endindx])
    # Add the x-axis label
    thisax= pyplot.axes([0.1,0.175,0.85,0.65])
    pyplot.gcf().sca(thisax)
    thisax.spines['left'].set_visible(False)
    thisax.spines['right'].set_visible(False)
    thisax.spines['bottom'].set_visible(False)
    thisax.spines['top'].set_visible(False)
    thisax.tick_params(labelleft='off')
    thisax.tick_params(left=False,which='both')
    thisax.tick_params(right=False,which='both')
    thisax.tick_params(labelbottom='off')
    thisax.tick_params(bottom=False,which='both')
    thisax.tick_params(top=False,which='both')
    pyplot.xlabel(r'$\log \big(\lambda / 15,000 \AA\big)$')
    thisax.set_zorder(-1)
    bovy_plot.bovy_end_print(plotfilename,dpi=600)
    return None
def get_spectra(name, red_clump, location):
    """Return cluster data, spectra, spectral errors, photometric Teffs, and bitmask from APOGEE.
	
	If the data file for the specified cluster already exists locally, 
	import the data from the file (cluster data, spectra, spectral errors, bitmask).
	If the data file does not exist, obtain the APOGEE spectra from a specified cluster 
	from the allStar catalogue, replacing ASPCAP abundances with astroNN abundances.
	
	Parameters
	----------
	name : str
		Name of desired cluster (i.e. 'PJ_26') 
	red_clump : bool
		If the red clump stars in rcsample are to be removed, set to True.  If all stars are to be used,
		set to False.
	
	Returns
	-------
	cluster_data_full (all stars) or cluster_data (red clumps removed) : structured array
		All cluster data from APOGEE
	cluster_spectra_full (all stars) or cluster_spectra (red clumps removed) : tuple
		Array of floats representing the cleaned-up fluxes in the APOGEE spectra with red clump stars removed
	cluster_spectra_errs_full (all stars) or cluster_spectra_errs (red clumps removed) : tuple
		Array of floats representing the cleaned-up spectral errors from the APOGEE spectra with red clump stars 
		removed
	cluster_T_full (all stars) or cluster_T (red clumps removed) : tuple
		Array of floats representing the effective temperatures of the stars in the cluster
		between 4000K and 5000K
	full_bitmask (all stars) or bitmask_final (red clumps removed) : tuple
		Array of ints (1 or 0), cleaned in the same way as the spectra, representing the bad pixels 
		in the APOGEE_PIXMASK bitmask
	"""

    if location == 'personal':
        path = '/Users/chloecheng/Personal/' + str(name) + '.hdf5'
    elif location == 'server':
        path = '/geir_data/scr/ccheng/AST425/Personal/' + str(name) + '.hdf5'

    #If the data file for this cluster exists, save the data to variables
    if glob.glob(path):
        if red_clump == 'False':
            file = h5py.File(path, 'r')
            cluster_data_full = file['apogee_cluster_data'][()]
            cluster_spectra_full = file['spectra'][()]
            cluster_spectra_errs_full = file['spectra_errs'][()]
            cluster_T_full = file['T'][()]
            full_bitmask = file['bitmask'][()]
            file.close()
            print(name, ' complete.')
            return cluster_data_full, cluster_spectra_full, cluster_spectra_errs_full, cluster_T_full, full_bitmask

        elif red_clump == 'True':
            file = h5py.File(path, 'r')
            cluster_data = file['apogee_cluster_data'][()]
            cluster_spectra = file['spectra'][()]
            cluster_spectra_errs = file['spectra_errs'][()]
            cluster_T = file['T'][()]
            bitmask_final = file['bitmask'][()]
            file.close()
            print(name, ' complete.')
            return cluster_data, cluster_spectra, cluster_spectra_errs, cluster_T, bitmask_final

    #If the file does not exist
    else:
        #Get red clump stars from rcsample
        rc_data = rcsample(dr='14')
        rc_stars = []
        for i in range(len(rc_data)):
            if location == 'personal':
                rc_stars.append(rc_data[i][2])
            elif location == 'server':
                rc_stars.append(rc_data[i][2].decode('UTF-8'))
        rc_stars = np.array(rc_stars)

        #Read in PJ catalogue data
        if location == 'personal':
            apogee_cluster_data = np.load(
                '/Users/chloecheng/Personal/published_clusters.npy')
        elif location == 'server':
            apogee_cluster_data = np.load(
                '/geir_data/scr/ccheng/AST425/Personal/published_clusters.npy')

        #Get temperatures
        #T = photometric_Teff(apogee_cluster_data)
        T = apogee_cluster_data['TEFF']

        #Get spectra for each star
        number_of_members = 360
        spectra = np.zeros((number_of_members, 7514))
        spectra_errs = np.zeros((number_of_members, 7514))
        bitmask = np.zeros((number_of_members, 7514))
        missing_spectra = []
        stars = []
        for s, star in enumerate(apogee_cluster_data):
            loc = star['FIELD'].decode('utf-8')
            apo = star['APOGEE_ID'].decode('utf-8')
            stars.append(apo)
            try:
                spectra[s] = apread.aspcapStar(
                    loc,
                    apo,
                    ext=1,
                    header=False,
                    dr='16',
                    aspcapWavegrid=True,
                    telescope=star['TELESCOPE'].decode('utf-8'))
                spectra_errs[s] = apread.aspcapStar(
                    loc,
                    apo,
                    ext=2,
                    header=False,
                    dr='16',
                    aspcapWavegrid=True,
                    telescope=star['TELESCOPE'].decode('utf-8'))
                bitmask[s] = apread.apStar(
                    loc,
                    apo,
                    ext=3,
                    header=False,
                    dr='16',
                    aspcapWavegrid=True,
                    telescope=star['TELESCOPE'].decode('utf-8'))[1]
            #If the spectrum is missing, set bitmask to value that will be removed
            except OSError:
                bitmask[s] = -1.0
                missing_spec.append(s)
                print('missing ', star['APOGEE_ID'].decode("utf-8"))

        #Mark red clump stars
        PJ_stars = np.array(stars)
        PJ_marked = np.copy(PJ_stars)
        for i in range(len(PJ_stars)):
            for j in range(len(rc_stars)):
                if PJ_stars[i] == rc_stars[j]:
                    PJ_marked[i] = np.nan

        #Set all entries in bitmask to integers
        bitmask = bitmask.astype(int)
        bitmask_flip = np.zeros_like(bitmask)
        for i in range(len(spectra)):
            for j in range(7514):
                if bitmask[i][j] == 0:
                    bitmask_flip[i][j] = 1
                else:
                    bitmask_flip[i][j] = 0

        #Remove empty spectra
        full_spectra = []
        full_spectra_errs = []
        full_bitmask = []
        full_stars = []
        full_T = []
        for i in range(len(spectra)):
            if any(spectra[i, :] != 0):
                full_spectra.append(spectra[i])
                full_spectra_errs.append(spectra_errs[i])
                full_bitmask.append(bitmask_flip[i])
                full_stars.append(i)
                full_T.append(T[i])
        full_spectra = np.array(full_spectra)
        full_spectra_errs = np.array(full_spectra_errs)
        full_bitmask = np.array(full_bitmask)
        full_stars = np.array(full_stars)
        full_T = np.array(full_T)
        full_marked_stars = PJ_marked[full_stars]

        #Create array of nans to replace flagged values in spectra
        masked_spectra = np.empty_like(full_spectra)
        masked_spectra_errs = np.empty_like(full_spectra_errs)
        masked_spectra[:] = np.nan
        masked_spectra_errs[:] = np.nan

        #Mask the spectra
        for i in range(len(full_spectra)):
            for j in range(7514):
                if full_bitmask[i][j] != 0:
                    masked_spectra[i][j] = full_spectra[i][j]
                    masked_spectra_errs[i][j] = full_spectra_errs[i][j]

        #Cut stars that are outside of the temperature limits
        good_T_inds = (full_T > 4000) & (full_T < 5000)
        final_spectra = masked_spectra[good_T_inds]
        final_spectra_errs = masked_spectra_errs[good_T_inds]
        good_T = full_T[good_T_inds]
        apogee_cluster_data = apogee_cluster_data[good_T_inds]
        full_bitmask = full_bitmask[good_T_inds]
        final_stars = full_marked_stars[good_T_inds]  #ADDED
        rgs = (final_stars != 'nan')  #ADDED

        #Want an SNR of 200 so set those errors that have a larger SNR to have an SNR of 200
        spectra_err_200 = np.zeros_like(final_spectra_errs)
        for i in range(len(final_spectra)):
            for j in range(7514):
                if final_spectra[i][j] / final_spectra_errs[i][j] <= 200:
                    spectra_err_200[i][j] = final_spectra_errs[i][j]
                else:
                    spectra_err_200[i][j] = final_spectra[i][j] / 200

        #Cut errors with SNR of less than 50
        spectra_50 = np.copy(final_spectra)
        spectra_err_50 = np.copy(spectra_err_200)
        for i in range(len(final_spectra)):
            for j in range(7514):
                if final_spectra[i][j] / spectra_err_200[i][j] <= 50:
                    spectra_50[i][j] = np.nan
                    spectra_err_50[i][j] = np.nan

        #Separate out individual clusters
        cluster_ids = apogee_cluster_data['CLUSTER_ID']
        PJ_26 = []
        PJ_95 = []
        PJ_471 = []
        PJ_162 = []
        PJ_398 = []
        PJ_151 = []
        PJ_230 = []
        PJ_939 = []
        PJ_262 = []
        PJ_289 = []
        PJ_359 = []
        PJ_396 = []
        PJ_899 = []
        PJ_189 = []
        PJ_574 = []
        PJ_641 = []
        PJ_679 = []
        PJ_1976 = []
        PJ_88 = []
        PJ_1349 = []
        PJ_1811 = []

        for i in range(len(apogee_cluster_data)):
            if cluster_ids[i] == 26:
                PJ_26.append(i)
            elif cluster_ids[i] == 95:
                PJ_95.append(i)
            elif cluster_ids[i] == 471:
                PJ_471.append(i)
            elif cluster_ids[i] == 162:
                PJ_162.append(i)
            elif cluster_ids[i] == 398:
                PJ_398.append(i)
            elif cluster_ids[i] == 151:
                PJ_151.append(i)
            elif cluster_ids[i] == 230:
                PJ_230.append(i)
            elif cluster_ids[i] == 939:
                PJ_939.append(i)
            elif cluster_ids[i] == 262:
                PJ_262.append(i)
            elif cluster_ids[i] == 289:
                PJ_289.append(i)
            elif cluster_ids[i] == 359:
                PJ_359.append(i)
            elif cluster_ids[i] == 396:
                PJ_396.append(i)
            elif cluster_ids[i] == 899:
                PJ_899.append(i)
            elif cluster_ids[i] == 189:
                PJ_189.append(i)
            elif cluster_ids[i] == 574:
                PJ_574.append(i)
            elif cluster_ids[i] == 641:
                PJ_641.append(i)
            elif cluster_ids[i] == 679:
                PJ_679.append(i)
            elif cluster_ids[i] == 1976:
                PJ_1976.append(i)
            elif cluster_ids[i] == 88:
                PJ_88.append(i)
            elif cluster_ids[i] == 1349:
                PJ_1349.append(i)
            elif cluster_ids[i] == 1811:
                PJ_1811.append(i)

        cluster_dict = {
            'PJ_26': PJ_26,
            'PJ_95': PJ_95,
            'PJ_471': PJ_471,
            'PJ_162': PJ_162,
            'PJ_398': PJ_398,
            'PJ_151': PJ_151,
            'PJ_230': PJ_230,
            'PJ_939': PJ_939,
            'PJ_262': PJ_262,
            'PJ_289': PJ_289,
            'PJ_359': PJ_359,
            'PJ_396': PJ_396,
            'PJ_899': PJ_899,
            'PJ_189': PJ_189,
            'PJ_574': PJ_574,
            'PJ_641': PJ_641,
            'PJ_679': PJ_679,
            'PJ_1976': PJ_1976,
            'PJ_88': PJ_88,
            'PJ_1349': PJ_1349,
            'PJ_1811': PJ_1811
        }

        cluster_data_full = apogee_cluster_data[cluster_dict[name]]
        cluster_spectra_full = spectra_50[cluster_dict[name]]
        cluster_spectra_errs_full = spectra_err_50[cluster_dict[name]]
        cluster_T_full = good_T[cluster_dict[name]]

        #Cut red clump stars
        cluster_rgs = rgs[cluster_dict[name]]
        cluster_data = cluster_data_full[cluster_rgs]
        cluster_spectra = cluster_spectra_full[cluster_rgs]
        cluster_spectra_errs = cluster_spectra_errs_full[cluster_rgs]
        cluster_T = cluster_T_full[cluster_rgs]
        bitmask_final = full_bitmask[rgs]

        if red_clump == 'False':
            #Write to file
            file = h5py.File(path, 'w')
            file['apogee_cluster_data'] = cluster_data_full
            file['spectra'] = cluster_spectra_full
            file['spectra_errs'] = cluster_spectra_errs_full
            file['T'] = cluster_T_full
            file['bitmask'] = full_bitmask
            file.close()
            print(name, 'complete')

            return cluster_data_full, cluster_spectra_full, cluster_spectra_errs_full, cluster_T_full, full_bitmask

        elif red_clump == 'True':
            #Write to file
            file = h5py.File(path, 'w')
            file['apogee_cluster_data'] = cluster_data
            file['spectra'] = cluster_spectra
            file['spectra_errs'] = cluster_spectra_errs
            file['T'] = cluster_T
            file['bitmask'] = bitmask_final
            file.close()
            print(name, 'complete')

            return cluster_data, cluster_spectra, cluster_spectra_errs, cluster_T, bitmask_final