Beispiel #1
0
    for i in logg_regions:
        if i[0] >= 4500:
            master_norm_array = master_norm_array * calculate_chisq_from_input_regions("norm",norm_spectrum,teff_ini,logg_ini,feh_ini,[i])
        else:
            master_norm_array = master_norm_array * calculate_chisq_from_input_regions("flux",flux_spectrum,teff_ini,logg_ini,feh_ini,[i])

    ### Apply more weighting to the logg axis by multiply with a logg vector
    teff_pos_min,logg_pos_min = spectype_functions.find_2d_max(master_norm_array)
    teff_min = teff_space[teff_pos_min]
    logg_min = logg_space[logg_pos_min]

    ### Crop the chisq space immediately surrounding the absolute min
    teff_space_cropped,logg_space_cropped,cropped_space =spectype_functions.chop_array(teff_space,logg_space,master_norm_array,teff_min,logg_min,500,1.0)

    ### Use scipy to fit a least sq 2D gaussian to the cropped region
    gauss_fit = spectype_functions.fitgaussian(cropped_space)
    gauss_width_logg = gauss_fit[3]

    for i in range(len(master_norm_array)):
        weight = gaussian_function(i,logg_pos_min,gauss_width_logg)
        for j in range(len(master_norm_array[i])):
            #master_norm_array[i,j] = master_norm_array[i,j]
            master_norm_array[i,j] = master_norm_array[i,j]*weight
            #master_norm_array[i,j] = weight
            #master_norm_array[i,j] = max(master_norm_array[i])

    # plt.clf()
    # plt.contourf(teff_space,logg_space,master_norm_array,20,cmap=plt.get_cmap("jet"))
    # plt.scatter(teff_min,logg_min,s=200,color="r",marker="x")
    # plt.xlabel("T_eff")
    # plt.ylabel("logg")
Beispiel #2
0
def loop_teff_logg(data_type,data_spectrum,teff,logg,feh,start_lambda,end_lambda):

    teff_space = []
    logg_space = []
    chisq_space = []

    teff_min = teff - 1000
    teff_max = teff + 1000

    if teff < 4500:
        teff_min = int(3500)

    if teff > 8000:
        teff_max = int(9000)

    shift = find_shift(data_type,data_spectrum,teff,logg,feh,start_lambda,end_lambda)

    ### Create grid
    teff_i = teff_min
    while teff_i <= teff_max:
        teff_space.append(teff_i)
        teff_i = teff_i + 250

    logg_i = 0.0
    while logg_i <= 5.0:
        logg_space.append(logg_i)
        logg_i = logg_i + 0.5

    teff_space = array(teff_space)
    logg_space = array(logg_space)

    (X,Y) = meshgrid(teff_space,logg_space)

    ### Create chisq array

    teff_i = teff_min
    while teff_i <= teff_max:
        logg_line = []
        logg_i = 0.0
        while logg_i <= 5.0:
            chisq_i = test_template(data_type,data_spectrum,teff_i,logg_i,0.0,start_lambda,end_lambda,shift)
            logg_line.append(chisq_i)
            logg_i = logg_i + 0.5
        chisq_space.append(logg_line)
        teff_i = teff_i + 250

    chisq_space = array(chisq_space)
    chisq_space = transpose(chisq_space)
    # chisq_space = spectype_functions.normalise_array(chisq_space)
    # chisq_space = log(chisq_space)

    ### Find min
    teff_min,logg_min = spectype_functions.find_2d_min(chisq_space)
    teff_min = teff_space[teff_min]
    logg_min = logg_space[logg_min]

    # ### Normalise the array
    #chisq_space = exp(- chisq_space **2 / 2)
    #chisq_space = spectype_functions.normalise_array(chisq_space)
    # print chisq_space

    ### Crop the chisq space immediately surrounding the absolute min
    teff_space_cropped,logg_space_cropped,cropped_space =spectype_functions.chop_array(teff_space,logg_space,chisq_space,teff_min,logg_min,500,1.0)
    #cropped_space = log(cropped_space)
    cropped_space = -1 *(cropped_space - cropped_space.max())

    ### Use scipy to fit a least sq 2D gaussian to the cropped region
    gauss_fit = spectype_functions.fitgaussian(cropped_space)
    gauss_height = gauss_fit[0]
    gauss_width_x = gauss_fit[3]
    gauss_width_y = gauss_fit[4]

    ### Check if the gaussian fit makes sense
    if gauss_height > 100 or gauss_width_x > 20 or gauss_width_y > 20:
        gauss_height = 0.00001
        gauss_width_x = 99999.
        gauss_width_y = 99999.

    # plt.clf()
    # plt.contourf(teff_space,logg_space,chisq_space,20,cmap=plt.get_cmap("jet"))
    # plt.scatter(teff_min,logg_min,s=50,color="r",marker="x")
    # plt.xlabel("T_eff")
    # plt.ylabel("logg")
    # plt.xlim(max(teff_space),min(teff_space))
    # plt.ylim(max(logg_space),min(logg_space))
    # plt.title(object_name +" " + str(start_lambda) + "-" + str(end_lambda) + "A, Teff=" + str(teff_min) + " logg=" + str(logg_min))
    # plt.show()

    return teff_space,logg_space,chisq_space,gauss_height,gauss_width_x,gauss_width_y
Beispiel #3
0
    def plot_spectrum(rms_data,input_spectrum):

        print "Plotting ",input_spectrum

        rms_data = functions.read_ascii(rms_data)
        rms_data = functions.read_table(rms_data)
        rms_data = transpose(rms_data)

        ### Find min
        for i in range(len(rms_data[0])):
            if rms_data[3][i] == min(rms_data[3]):
                teff_min = rms_data[0][i]
                logg_min = rms_data[1][i]
                feh_min = rms_data[2][i]
                break

        print teff_min,logg_min,feh_min,min(rms_data[3])

        teff_list = []
        logg_list = []
        rms_list = []

        for i in range(len(rms_data[0])):
            if rms_data[2][i] == feh_min:
                teff_list.append(rms_data[0][i])
                logg_list.append(rms_data[1][i])
                rms_list.append(rms_data[3][i])

        ### Create 2D space
        teff_space = arange(min(teff_list),max(teff_list)+250,250)
        logg_space = arange(min(logg_list),max(logg_list)+0.5,0.5)

        rms_space = zeros([len(teff_space),len(logg_space)])

        for i in range(len(rms_list)):
            x_index = int((teff_list[i] - min(teff_list)) / 250.)
            y_index = int((logg_list[i] - min(logg_list)) / 0.5)
            rms_space[x_index,y_index] = rms_list[i]


        ### Crop 2D space to perform gaussian fit for min
        teff_space_cropped,logg_space_cropped,rms_space_cropped=spectype_functions.chop_array(teff_space,logg_space,transpose(rms_space),teff_min,logg_min,250,0.5)
        rms_space_cropped = -1*(rms_space_cropped - rms_space_cropped.max())
        print rms_space_cropped
        try:
            gauss_fit = spectype_functions.fitgaussian(rms_space_cropped)
            teff_min_fit = min(teff_space_cropped) + gauss_fit[2] * 250
            logg_min_fit = min(logg_space_cropped) + gauss_fit[1] * 0.5
        except TypeError:
            print "Bad gaussian fit, using abs min"
            teff_min_fit = teff_min
            logg_min_fit = logg_min

        if teff_min_fit < 3500:
            teff_min_fit = 3500
        if teff_min_fit > 9000:
            teff_min_fit = 9000

        if logg_min_fit < 0.0:
            logg_min_fit = 0.0
        if logg_min_fit > 5.0:
            logg_min_fit = 5.0

        teff_min = int(spectype_functions.round_value(teff_min_fit,250.))
        logg_min = spectype_functions.round_value(logg_min_fit,0.5)

        print teff_min,logg_min

        ### Plot teff_logg space
        plt.figure(figsize=(7,5))
        plt.subplot(211)

        plt.title(object_name+" "+file_name+" "+str(int(round(teff_min_fit,0)))+" "+str(round(logg_min_fit,1))+" "+str(feh_min)+" \n RMS="+str(round(min(rms_data[3]),4)))

        v_min = min(rms_list)
        v_max = min(rms_list)+((max(rms_list)-min(rms_list))/3.)
        #v_max = max(rms_list)
        rms_space = clip(rms_space,v_min,v_max)

        cm = matplotlib.cm.get_cmap('jet')
        sc = plt.contourf(teff_space,logg_space,transpose(rms_space),100,cmap=cm)

        #sc = plt.scatter(teff_list, logg_list, c=rms_list, vmin=min(rms_list), vmax=(max(rms_list)-min(rms_list))/3+min(rms_list), s=150, cmap=cm,edgecolor="w")
        cbar = plt.colorbar(sc)
        cbar.ax.set_ylabel("RMS")

        plt.scatter(teff_min_fit,logg_min_fit,color="r",s=70,marker="+")

        spectype_functions.plot_isochrones(program_dir,"r-",1)
        plt.xlim(max(teff_list),min(teff_list))
        plt.ylim(max(logg_list),min(logg_list))
        #plt.xlim(max(teff_list)+250,min(teff_list)-250)
        #plt.ylim(max(logg_list)+.25,min(logg_list)-0.25)

        plt.xlabel("Teff (K)")
        plt.ylabel("Logg")

        ### Plot spectrum
        plt.subplot(212)
        data_spectrum = functions.read_ascii(input_spectrum)
        data_spectrum = functions.read_table(data_spectrum)
        data_spectrum = transpose(array(data_spectrum))
        data_spectrum = spectype_functions.normalise(data_spectrum,flux_normalise_w1,flux_normalise_w2)

        template_spectrum = "template_" + str(int(teff_min)) + "_" + str(logg_min) + "_" + str(feh_min)+".dat"
        template_spectrum = functions.read_ascii(model_path_flux+template_spectrum)
        template_spectrum = functions.read_table(template_spectrum)
        template_spectrum = transpose(array(template_spectrum))
        template_spectrum = spectype_functions.normalise(template_spectrum,flux_normalise_w1,flux_normalise_w2)
    
        data_wave = data_spectrum[0]
        data_flux = data_spectrum[1]
        template_wave = template_spectrum[0]
        template_flux = template_spectrum[1]

        c = 3.0 * 10**5

        data_wave = data_wave / ((vel_shift / c) + 1)

        data_interp = interpolate.splrep(data_wave,data_flux,s=0)
        data_flux = interpolate.splev(master_flux_wave,data_interp,der=0)

        template_interp = interpolate.splrep(template_wave,template_flux,s=0)
        template_flux = interpolate.splev(master_flux_wave,template_interp,der=0)


        plt.plot(master_flux_wave,data_flux,"b-",label="data")
        plt.plot(master_flux_wave,template_flux,"g-",label="template")
        plt.xlim(3600,5800)
        ylim_range = max(template_flux)-min(template_flux)
        plt.ylim(min(template_flux)-ylim_range*0.2,max(template_flux)+ylim_range*0.2)

        plt.legend(loc="lower right",ncol=2)

        plt.xlabel("Wavelength (A)")
        plt.ylabel("Normalised flux")

        os.system("rm "+file_path_reduced+"spectype_plots/"+object_name+"_"+file_name+".pdf")
        plt.savefig(file_path_reduced+"spectype_plots/"+object_name+"_"+file_name+".pdf")
        #plt.show()

        return teff_min_fit,logg_min_fit,feh_min