<jupyter_start><jupyter_text>First the object list will be defined, organized, and seperated into categories<jupyter_code># Create a list of all of the fits file directories
obj_list = glob.glob('/Users/hitom/Downloads/Academics/UCSC Classes/Spring 2019/ASTR136/Star_Clusters_Project_data/*.fits')
obj_list
# This will organize the obj_list by the index of the fits file before separation
Organized_obj_list = ['' for x in range(len(obj_list))]
for j in range(len(Organized_obj_list)):
    try:
        n = int(obj_list[j][94:97])
    except:
        try:
            n = int(obj_list[j][94:96])
        except:
            n = int(obj_list[j][94:95])
    Organized_obj_list[n-1] = obj_list[j]
Organized_obj_list
# Seperate the fits files by categories
Biases = {}
Blue_Domes = {}
Violet_Domes = {}
Darks = {}
IC1B = {}
IC1V = {}
IC2B = {}
IC2V = {}
IC3B = {}
IC3V = {}
IC4B = {}
IC4V = {}
IC5B = {}
IC7B = {}
IC7V = {}
IC8B = {}
IC8V = {}
IC9B = {}
IC9V = {}
for i in range(len(Organized_obj_list)):
    if(i<10):
        Biases[i] = Organized_obj_list[i]
    elif(10<=i<25):
        Blue_Domes[i-10] = Organized_obj_list[i]
    elif(25<=i<41):
        Violet_Domes[i-25] = Organized_obj_list[i]
    elif(41<=i<50):
        Darks[i-41] = Organized_obj_list[i]
    elif(50<=i<168):
        continue
    elif(168<=i<173):
        IC1B[i-168] = Organized_obj_list[i]
    elif(173<=i<178):
        IC1V[i-173] = Organized_obj_list[i]
    elif(178<=i<183):
        IC2B[i-178] = Organized_obj_list[i]
    elif(183<=i<188):
        IC2V[i-183] = Organized_obj_list[i]
    elif(188<=i<193):
        IC3B[i-188] = Organized_obj_list[i]
    elif(193<=i<198):
        IC3V[i-193] = Organized_obj_list[i]
    elif(198<=i<203):
        IC4B[i-198] = Organized_obj_list[i]
    elif(203<=i<208):
        IC4V[i-203] = Organized_obj_list[i]
    elif(208<=i<213):
        IC5B[i-208] = Organized_obj_list[i]
    elif(213<=i<219):
        continue
    elif(219<=i<224):
        IC7B[i-219] = Organized_obj_list[i]
    elif(224<=i<229):
        IC7V[i-224] = Organized_obj_list[i]
    elif(229<=i<234):
        IC8B[i-229] = Organized_obj_list[i]
    elif(234<=i<239):
        IC8V[i-234] = Organized_obj_list[i]
    elif(239<=i<244):
        IC9B[i-239] = Organized_obj_list[i]
    elif(244<=i<249):
        IC9V[i-244] = Organized_obj_list[i]
    elif(249<=i<251):
        continue
    elif(i==251):
        SDSSB = Organized_obj_list[i]
    elif(i==252):
        SDSSV = Organized_obj_list[i]<jupyter_output><empty_output><jupyter_text>Define a few functions to make handling the arrays easier<jupyter_code># This returns the header of the fits file
def header(array,index):
    header = fits.open(array[index])[0].header
    return header
# This returns the array of the fits file
def data(array,*args):
    index = 0
    for num in args:
        index = num
    try:
        data = fits.open(array[index])[0].data
    except:
        data = fits.open(array)[0].data
    return data<jupyter_output><empty_output><jupyter_text>Now the bias frames will be averaged to get the bias of the telescope<jupyter_code>def Bias(array,filename):
    # First add all of the bias frames in a temporary array
    Temp = np.zeros(data(array,0).shape)
    for i in range(len(array)):
        Temp = data(array,i) + Temp
    # Now the temporary array will be averaged for the bias of the telescope
    Average_Bias = Temp[0:1024,0:1024]/len(array)
    # Now the bias frame will be saved as fits files according to the 'filename' input
    # Note: try and except is used so it still runs when a bias still exists
    try:
        hdu = fits.PrimaryHDU(Average_Bias)
        hdul = fits.HDUList([hdu])
        hdul.writeto(filename)
    except:
        pass
    return Average_Bias
Master_Bias = Bias(Biases,'UCO_Lick_Nickel_Telescope_Master_Bias.fits')
Master_Bias<jupyter_output><empty_output><jupyter_text>Next the dark frames will be reduced and combined.<jupyter_code>def Reduce_Darks(array,filename):
    # First the darks will be reduced to per second images, given that they all have an exposure time of 30 seconds.
    # They will simultaneously be added into an array and combined.
    Temp = {}
    Reduced_Darks = np.zeros(Master_Bias.shape)
    ExpTime = 30 #seconds
    for i in range(len(array)):
        Temp[i] = data(array,i)[0:1024,0:1024] - Master_Bias
        Reduced_Darks = Temp[i]/ExpTime + Reduced_Darks
    # Now they will all be averaged into a single dark frame.
    Reduced_Dark = Reduced_Darks/len(array)
    # Now the reduced dark will be saved as fits files according to the 'filename' input
    try:
        hdu = fits.PrimaryHDU(Reduced_Dark)
        hdul = fits.HDUList([hdu])
        hdul.writeto('{0}.fits'.format(filename))
    except:
        pass
    return Reduced_Dark
Master_Dark = Reduce_Darks(Darks,'UCO_Lick_Nickel_Telescope_Master_Dark')
Master_Dark<jupyter_output><empty_output><jupyter_text>Now the dome flats will be reduced and combined.<jupyter_code>def Reduce_Domes(array,filename,string):
    # First the domes will be reduced to per second images, given that they all have an exposure time of 30 seconds.
    # They will simultaneously be added into an array and combined.
    Temp = {}
    Reduced_Dome = np.zeros(Master_Bias.shape)
    if string=='Blue':
        ExpTime = 30 #seconds
    elif string=='Violet':
        ExpTime = 3 #seconds
    for i in range(len(array)):
        Temp[i] = data(array,i)[0:1024,0:1024] - Master_Bias
        Reduced_Dome = Temp[i] + Reduced_Dome - ExpTime*Master_Dark
    # Now the reduced dome will be normalized and saved.
    Master_Dome = Reduced_Dome[0:1024,0:1024]/len(array)
    try:
        hdu = fits.PrimaryHDU(Master_Dome)
        hdul = fits.HDUList([hdu])
        hdul.writeto('{0}.fits'.format(filename))
    except:
        pass
    return Master_Dome
Master_Blue_Dome = Reduce_Domes(Blue_Domes,'UCO_Lick_Nickel_Telescope_Master_Dome_B','Blue')
Master_Blue_Dome
Master_Violet_Dome = Reduce_Domes(Violet_Domes,'UCO_Lick_Nickel_Telescope_Master_Dome_V','Violet')
Master_Violet_Dome<jupyter_output><empty_output><jupyter_text>Now the clusters will be reduced individually so they can be aligned in AstroImagej and then recombined.<jupyter_code>def Reduce_Blue_Cluster(array,filename):
    # First the clusters will be reduced to per second images, given that they all have an exposure time of 30 seconds.
    Temp = {}
    Reduced_Clusters = {}
    Master_Clusters = {}
    ExpTime = 30 #seconds
    for i in range(len(array)):
        Temp[i] = data(array,i)[0:1024,0:1024] - Master_Bias
        Reduced_Clusters[i] = (Temp[i] - ExpTime*Master_Dark)/(Master_Blue_Dome/np.max(Master_Blue_Dome))
        Master_Clusters[i] = Reduced_Clusters[i]
    # Now the reduced clusters will be saved.
    try:
        for i in range(len(array)):
            hdu = fits.PrimaryHDU(Master_Clusters[i])
            hdul = fits.HDUList([hdu])
            hdul.writeto('{0}_{1}.fits'.format(filename,i))
    except:
        pass
    return Master_Clusters
def Reduce_Violet_Cluster(array,filename):
    # First the clusters will be reduced to per second images, given that they all have an exposure time of 30 seconds.
    Temp = {}
    Reduced_Clusters = {}
    Master_Clusters = {}
    ExpTime = 30 #seconds
    for i in range(len(array)):
        Temp[i] = data(array,i)[0:1024,0:1024] - Master_Bias
        Reduced_Clusters[i] = (Temp[i] - ExpTime*Master_Dark)/(Master_Violet_Dome/np.max(Master_Violet_Dome))
        Master_Clusters[i] = Reduced_Clusters[i]
    # Now the reduced clusters will be saved.
    try:
        for i in range(len(array)):
            hdu = fits.PrimaryHDU(Master_Clusters[i])
            hdul = fits.HDUList([hdu])
            hdul.writeto('{0}_{1}.fits'.format(filename,i))
    except:
        pass
    return Master_Clusters
#Reduce_Blue_Cluster(IC1B,'UCO_Lick_Nickel_Telescope_IC1B')
#Reduce_Blue_Cluster(IC2B,'UCO_Lick_Nickel_Telescope_IC2B')
#Reduce_Blue_Cluster(IC3B,'UCO_Lick_Nickel_Telescope_IC3B')
#Reduce_Blue_Cluster(IC4B,'UCO_Lick_Nickel_Telescope_IC4B')
#Reduce_Blue_Cluster(IC5B,'UCO_Lick_Nickel_Telescope_IC5B')
#Reduce_Blue_Cluster(IC7B,'UCO_Lick_Nickel_Telescope_IC7B')
#Reduce_Blue_Cluster(IC8B,'UCO_Lick_Nickel_Telescope_IC8B')
#Reduce_Blue_Cluster(IC9B,'UCO_Lick_Nickel_Telescope_IC9B')
#Reduce_Violet_Cluster(IC1V,'UCO_Lick_Nickel_Telescope_IC1V')
#Reduce_Violet_Cluster(IC2V,'UCO_Lick_Nickel_Telescope_IC2V')
#Reduce_Violet_Cluster(IC3V,'UCO_Lick_Nickel_Telescope_IC3V')
#Reduce_Violet_Cluster(IC4V,'UCO_Lick_Nickel_Telescope_IC4V')
#Reduce_Violet_Cluster(IC7V,'UCO_Lick_Nickel_Telescope_IC7V')
#Reduce_Violet_Cluster(IC8V,'UCO_Lick_Nickel_Telescope_IC8V')
#Reduce_Violet_Cluster(IC9V,'UCO_Lick_Nickel_Telescope_IC9V')
# After aligning the reduced images in AstroImagej
Aligned_Blues = glob.glob('/Users/hitom/ASTR136_Code/Aligned_Blues/*.fits')
Aligned_Violets = glob.glob('/Users/hitom/ASTR136_Code/Aligned_Violets/*.fits')
# This will combine the blue and violet images
def Combine_Images(array,filename,letter):
    Combined_Arrays = {}
    for i in range(9):
        Temp = {}
        if(letter=='V' and i==4):
            continue
        elif(i==5):
            continue
        elif(i>5):
            if(letter=='V'):
                for j in range(5):
                    Temp[j] = data(array[5*i+j-10])
                Combined_Arrays[i] = Temp[0] + Temp[1] + Temp[2] + Temp[3] + Temp[4]
                try:
                    hdu = fits.PrimaryHDU(Combined_Arrays[i])
                    hdul = fits.HDUList([hdu])
                    hdul.writeto('{0}_IC{1}{2}_Master.fits'.format(filename,i+1,letter))
                except:
                    pass
            else:
                for j in range(5):
                    Temp[j] = data(array[5*i+j-5])
                Combined_Arrays[i] = Temp[0] + Temp[1] + Temp[2] + Temp[3] + Temp[4]
                try:
                    hdu = fits.PrimaryHDU(Combined_Arrays[i])
                    hdul = fits.HDUList([hdu])
                    hdul.writeto('{0}_IC{1}{2}_Master.fits'.format(filename,i+1,letter))
                except:
                    pass
        else:
            for j in range(5):
                Temp[j] = data(array[5*i+j])
            Combined_Arrays[i] = Temp[0] + Temp[1] + Temp[2] + Temp[3] + Temp[4]
            try:
                hdu = fits.PrimaryHDU(Combined_Arrays[i])
                hdul = fits.HDUList([hdu])
                hdul.writeto('{0}_IC{1}{2}_Master.fits'.format(filename,i+1,letter))
            except:
                pass
    return Combined_Arrays
Combined_Blues = Combine_Images(Aligned_Blues,'UCO_Lick_Nickel_Telescope','B')
Combined_Violets = Combine_Images(Aligned_Violets,'UCO_Lick_Nickel_Telescope','V')
Combined_Blues<jupyter_output><empty_output><jupyter_text>Now the standard star images will be reduced<jupyter_code>def Reduce_Standard_Star(array,filename,letter):
    # First the standard stars will be reduced
    ExpTime = 25 #seconds
    Temp = data(array)[0:1024,0:1024] - Master_Bias
    Reduced_Star = (Temp - Master_Dark*ExpTime)
    if letter=='B':
        Master_Star = Reduced_Star/(Master_Blue_Dome/np.max(Master_Blue_Dome))
    elif letter=='V':
        Master_Star = Reduced_Star/(Master_Violet_Dome/np.max(Master_Violet_Dome))
    # Now the reduced star will be saved.
    try:
        hdu = fits.PrimaryHDU(Reduced_Star)
        hdul = fits.HDUList([hdu])
        hdul.writeto('{0}.fits'.format(filename))
    except:
        pass
    return Reduced_Star
Master_SDSSB = Reduce_Standard_Star(SDSSB,'UCO_Lick_Nickel_Telescope_SDSSB','B')
Master_SDSSV = Reduce_Standard_Star(SDSSV,'UCO_Lick_Nickel_Telescope_SDSSV','V')
np.median(Master_SDSSB)
np.median(Master_SDSSV)
def Correct_Blues(array1,array2,filename):
    Median_Correction = np.median(array2) - np.median(array1)
    full = np.full(array1.shape,Median_Correction)
    array_new = array1 + full
    try:
        hdu = fits.PrimaryHDU(array_new)
        hdul = fits.HDUList([hdu])
        hdul.writeto('{0}.fits'.format(filename))
    except:
        pass
Correct_Blues(Master_SDSSB,Master_SDSSV,'Corrected_UCO_Lick_Nickel_Telescope_SDSSB')
Correct_Blues(Combined_Blues[0],Combined_Violets[0],'Corrected_UCO_Lick_Nickel_Telescope_IC1B')
Correct_Blues(Combined_Blues[1],Combined_Violets[1],'Corrected_UCO_Lick_Nickel_Telescope_IC2B')
Correct_Blues(Combined_Blues[2],Combined_Violets[2],'Corrected_UCO_Lick_Nickel_Telescope_IC3B')
Correct_Blues(Combined_Blues[3],Combined_Violets[3],'Corrected_UCO_Lick_Nickel_Telescope_IC4B')
Correct_Blues(Combined_Blues[4],Combined_Violets[3],'Corrected_UCO_Lick_Nickel_Telescope_IC5B')
Correct_Blues(Combined_Blues[6],Combined_Violets[6],'Corrected_UCO_Lick_Nickel_Telescope_IC7B')
Correct_Blues(Combined_Blues[7],Combined_Violets[7],'Corrected_UCO_Lick_Nickel_Telescope_IC8B')
Correct_Blues(Combined_Blues[8],Combined_Violets[8],'Corrected_UCO_Lick_Nickel_Telescope_IC9B')<jupyter_output><empty_output><jupyter_text>Now the fits files will be shown and run object detection on. The standard star will be used to determine magnitude and color by using the standard star as a value such that counts/counts_SS = ratio for the magnitude. The color will be created by subtracting the magnitude from the standard star magnitude of 1. After the object detection, aperature photometry is run to find the fluxes of the objects that will be used to find the magnitudes of the objects.<jupyter_code># This masks the region between 1025 and 1056 since this only contains bias
def masker(array):
    masker = np.ones(array.shape)
    array_2 = np.zeros((1024,1024))
    masker[0:1024,0:1024] = array_2
    masked_array = ma.array(array,mask=masker)
    return masked_array
# Create a function to show the image
def imshow(array):
    # First the image will be masked
    fits_file = masker(array)
    # Now the images will be shown
    m, s = np.mean(fits_file), np.std(fits_file)
    plt.figure(figsize=(7,7))
    plt.imshow(fits_file, interpolation='nearest', cmap='gray', vmin=m-s, vmax=m+s, origin='lower')
    plt.xlim(0,1024)
    plt.colorbar();
imshow(Combined_Blues[0])
def object_detector(array,detection_threshold):
    # This is the masked array
    Image = masker(array)
    
    # This will detect the objects in the image using a detection threshold of 1.5 sigma with sigma being the RMS
    objects = sep.extract(array,detection_threshold,err=np.std(Image))
    
    # Now the background subtracted image will be plotted
    fig, ax = plt.subplots(figsize=(7,7))
    m, s = np.mean(Image), np.std(Image)
    im = ax.imshow(Image, interpolation='nearest', cmap='gray',vmin=m-s, vmax=m+s, origin='lower')
    plt.xlim(0,1024)

    # plot an ellipse for each object
    for i in range(len(objects)):
        e = Ellipse(xy=(objects['x'][i], objects['y'][i]),width=6*objects['a'][i],\
                    height=6*objects['b'][i],angle=objects['theta'][i] * 180. / np.pi)
        e.set_facecolor('none')
        e.set_edgecolor('red')
        ax.add_artist(e)
        
    flux, fluxerr, flag = sep.sum_circle(array, objects['x'], objects['y'],
                                     3.0, err=np.std(Image), gain=1.0)
    # show the objects results:
    for i in range(len(objects)):
        print("object {:d}: flux = {:f} +/- {:f}".format(i, flux[i], fluxerr[i]))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11771995043370508, 'fraction_numerical': 0.03202243527033197, 'mean_word_length': 3.603422395676974, 'pattern_counts': {'":': 0, '<': 76, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '337071', 'license': 'MIT', 'n_tokens_mistral': 5901, 'n_tokens_neox': 5519, 'n_words': 1067, 'path': 'Star_Cluster_Data_Reduction.ipynb', 'repo_name': 'tiseever/Adv.-Astro.-Lab-Code', 'size': '15333'}