Exemplo n.º 1
0
def generate_perturbation(num_stars, params, parameters, batch=1000, fail_quit=100,
                          fileout="Pertgen82.txt", detection=1, convolve=1, append=0, primary=0):
    """Density of perturbation added to background, as a function of position"""
    # Initialize file
    if append==0:
        out = open(fileout, 'w')
        out.write("# "+str(num_stars)+" stars, l,b,r \n")
        out.close()
    # Get integral
    tot_prob = get_max_prob_2(params, parameters)
    N_out, fails = 0, 0
    g_min, g_max = params.stripe[2][0], params.stripe[2][1]
    while N_out < num_stars:
        # Generate Points
        mu, nu, r = get_stripe_points(params.mu_lim, params.nu_lim, params.r_lim, batch)
        # Test points for inclusion
        x,y,z = co.GC2xyz(mu, nu, r, params.wedge)
        holder = []
        for i in range(len(mu)):
            rho = perturb_density(x[i],y[i],z[i], parameters)
            #print (rho / tot_prob), rho, tot_prob
            if (rho / tot_prob) > np.random.uniform():
                l,b,r1 = co.xyz2lbr(x[i], y[i], z[i])
                # Convolve
                if convolve==1:
                    r1 = star_convolution(r1, params.modfit)
                # Detection
                if detection==1:
                    m_g = co.getg(r1)
                    if np.random.uniform() > sigmoid_error(m_g, params.modfit):  continue
                if (co.getg(r1) < g_min) or (co.getg(r1) > g_max):  continue
                if primary == 1:
                    if co.SDSS_primary(l,b,wedge,fmt='lb',low=9,high=25) == 0:  continue
                # Add to keepers
                holder.append([round(l,6),round(b,6),round(r1,6)])
                N_out = N_out + 1
        # Failure code
        if len(holder) == 0:  fails = fails + 1
        if fails >= fail_quit:  break
        # Remove possible excess stars
        if N_out > num_stars:
            slice = -1*(N_out-num_stars)
            holder = holder[:slice]
            print "#---Sliced {0} stars to make quota".format(str(-1*slice))
            N_out = N_out + slice #Slice is negative
        # Add to dataset
        if len(holder) != 0:
            if fi.append_data(sc.array(holder), fileout, delimiter=" ") == 1:
                print "#---Perturbation Progress: {0} stars of {1} total stars generated".format(N_out, num_stars)
    if fails >= fail_quit:  
        print "!!! Perturbation generation FAILED due to overstepping empty batch limit: {0}".format(fail_quit)
    else:
        print "#---Perturbation generation succeeded, written as {0}, with {1} empty batches".format(fileout, fails)
    return fileout
Exemplo n.º 2
0
def make_lbr_MRT():
    import glob
    out = []
    stripes = [
        "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
        "21", "22", "23", "79", "82", "86"
    ]
    backs = glob.glob("./sep_lbr/Hold/*.txt")
    #for stripe in stripes:
    #infile = open("./sep_lbr/s1-"+stripe+".txt", "r")
    for back in backs:
        infile = open(back, "r")
        for line in infile:
            l = "  "
            holder = line.strip().split()
            l = l + holder[0]
            l = l + "\t" + holder[1]
            g = coor.getg(float(holder[2]))
            g = str(round(g, 2))
            if len(g) < 5:
                g = g + "0"
            l = l + "\t" + g
            out.append(l)
        infile.close()
    #outfile = open("sgr_separated_stars_MRT.txt", "w")
    outfile = open("back_separated_stars_MRT.txt", "w")
    for o in out:
        outfile.write(o + "\n")
    outfile.close()
Exemplo n.º 3
0
def make_lbr_MRT():
    import glob
    out = []
    stripes = ["09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
               "20", "21", "22", "23", "79", "82", "86"]
    backs = glob.glob("./sep_lbr/Hold/*.txt")
    #for stripe in stripes:
        #infile = open("./sep_lbr/s1-"+stripe+".txt", "r")
    for back in backs:
        infile = open(back, "r")
        for line in infile:
            l = "  "
            holder = line.strip().split()
            l = l + holder[0]
            l = l + "\t" + holder[1]
            g = coor.getg(float(holder[2]))
            g = str(round(g,2))
            if len(g) < 5:
                g = g + "0"
            l = l + "\t" + g 
            out.append(l)
        infile.close()
    #outfile = open("sgr_separated_stars_MRT.txt", "w")
    outfile = open("back_separated_stars_MRT.txt", "w")
    for o in out:
        outfile.write(o+"\n")
    outfile.close()
Exemplo n.º 4
0
def photo_spec_analysis():
    # l, b, r;  plate #, p_ra, p_dec, in Sgr, out Sgr
    path="/home/newbym2/Desktop/starfiles"
    wd="/home/newbym2/Dropbox/Research/sgrLetter/"
    files = glob.glob(path+"/stars*")
    plates = np.loadtxt(wd+"plate_stars.csv", delimiter=",")
    radius = 1.49
    for i, plate in enumerate(plates[:,0]):
        print "starting plate", str(plate).split(".")[0], plates[i,3], plates[i,4]
        p_ra, p_dec = plates[i,1], plates[i,2]
        pl, pb = ac.EqTolb(p_ra, p_dec)
        print pl, pb
        stars = []
        for f in files:
            data = open(f, "r") #np.loadtxt(f) #, delimiter="\t")
            print "    starting file", f
            wedge = int(f.split("-")[1].split(".")[0])
            skip = 0
            for line in data:
                if skip < 1:  skip =+ 1;  continue
                if line.strip()=="":  continue
                temp = line.split()
                ll, bb, g0 = float(temp[0]), float(temp[1]), ac.getg(float(temp[2]))                
                if g0 < 20.0:  continue
                if g0 > 20.5:  continue
                del_l, del_b = (ll-pl), (bb-pb)
                dd = ma.sqrt( (del_l*del_l) + (del_b*del_b) )
                if dd > radius: continue
                if ac.SDSS_primary(ll,bb,wedge,fmt="lb",low=9,high=27)==0:  continue
                stars.append([ll,bb,g0])
            data.close()
        svname = wd+"plate_"+str(plate).split(".")[0]+"_stars.csv"
        np.savetxt(svname, sc.array(stars), delimiter=",")
        print "saved {0}".format(svname)
Exemplo n.º 5
0
def make_sim_stream_plot(filein="stream_50shift.txt", RGB=0, imfile=None):
    """ Makes the plot for the simulated streams
        /home/newbym2/Dropbox/Research/sgrnorth_paper/sgr_separated_stars_MRT.txt"""
    folder = "/home/newbym2/Dropbox/Research/sgrLetter/"
    filename=folder + filein
    #file2="/home/newbym2/Dropbox/Research/sgrnorth_paper/sgr_separated_stars_MRT.txt"
    #file2="streamgen_sgr_sim.txt"
    #file2="streamgen_sgrfidprim.txt"
    file2 = folder+"streamgen_sfp2.txt"
    data = np.loadtxt(filename)
    data2 = []
    datasgr = np.loadtxt(file2)
    for i in range(len(data[:,0])):
        #data[i,0], data[i,1] = ac.lbToEq(data[i,0], data[i,1])
        #if ac.SDSS_primary(temp[0],temp[1],wedge,fmt="lb",low=9,high=27)==0:  continue
        data[i,0], data[i,1], data[i,2] = (ac.lb2sgr(data[i,0], data[i,1], data[i,2]))[3:]
        lam, bet = new_shift_sgr(data[i,0], data[i,1])  #move 2nd stream to new position
        data2.append([lam, bet, ac.getg(data[i,2], M=4.2)])
    data2 = sc.array(data2)
    for i in range(len(datasgr[:,0])):
        datasgr[i,0], datasgr[i,1] = (ac.lb2sgr(datasgr[i,0], datasgr[i,1], 10.0))[3:5]
        datasgr[i,2] = ac.getg(data[i,2], M=4.2)
    """
    data2 = np.loadtxt(file2)
    for i in range(len(data2[:,0])):
        #data2[i,0], data2[i,1] = ac.lbToEq(data2[i,0], data2[i,1])
        data2[i,0], data2[i,1] = (ac.lb2sgr(data2[i,0], data2[i,1], 10.0))[3:5]
    """
    if RGB==1:  
        data3 = np.concatenate((data2,datasgr), axis=0)
        #data3 = np.concatenate((data2,data), axis=0)
        RGB_plot(data3, imfile=imfile, mask_data="Bhist_sgr.csv", muddle=0)
    else:
        sky = pp.HistMaker(np.concatenate([data[:,0],data2[:,0]]), np.concatenate([data[:,1],data2[:,1]]),
            xsize=0.5, ysize=0.5, xarea=(120.0, 250.0), yarea=(-10.0, 50.0))
        sky.varea = (0.0,200.0)
        sky.H = sky.H + np.random.normal(60.0, 15.0, sky.H.shape)
        #for i in range(sky.H.shape[0]):
        #    if i < 14:   sky.H[i,:] = sky.H[i,:]*0.0; continue
        #    sky.H[i,:] = sky.H[i,:] + 45.0 - (0.5/1.0)*i
        pp.PlotHist(sky, "streamgen_bifExtra_radec.png")
        sky.savehist("streamgen_bifExtra.csv")
    print "Ended Successfully"
Exemplo n.º 6
0
def make_total_plot(path="/home/newbym2/Desktop/starfiles", RGB=0, imfile=None, 
                    rcut=None, vrange=None, outfile=None):
    """ Makes a 2D histogram """
    files = glob.glob(path+"/stars*")
    print files
    data=[]
    pb = pr.Progressbar(steps=len(files), prefix="Loading Stars:", suffix=None,
        symbol="#", active="=", brackets="[]", percent=True, size=40)
    for f in files:
        wedge = int(f.split("-")[1].split(".")[0])
        count=0
        stripedata = open(f, "r")
        for line in stripedata:
            count = count + 1
            if count==1:  continue
            if line.strip()=="":  continue
            temp = line.split()
            for i in range(len(temp)):  temp[i] = float(temp[i])
            if rcut != None:
                if temp[2] < rcut[0]:  continue
                if temp[2] > rcut[1]:  continue
            if ac.SDSS_primary(temp[0],temp[1],wedge,fmt="lb",low=9,high=27)==0:  continue
            data.append(temp)
        stripedata.close()
        pb.updatebar(float(files.index(f)+1)/float(len(files)) )
    data = np.array(data)
    count, nStars, pb2 = 0, float(len(data[:,0])), pr.Progressbar(steps=100,
        prefix="Changing Coordinates:", suffix=None, symbol="#", active="=",
        brackets="[]", percent=True, size=40)
    for i in range(len(data[:,0])):
        count = count + 1
        #data[i,0], data[i,1] = ac.lb2GC(data[i,0], data[i,1], 15)
        #data[i,0], data[i,1] = ac.lbToEq(data[i,0], data[i,1])
        data[i,0], data[i,1] = (ac.lb2sgr(data[i,0], data[i,1], 10.0))[3:5]
        data[i,2] = ac.getg(data[i,2], 4.2)
        if count % 100 == 0:  pb2.updatebar(float(count)/nStars)
    pb2.endbar()
    if RGB==1:  RGB_plot(data, imfile=imfile)
    else:
        allsky = pp.HistMaker(data[:,0], data[:,1], xsize=0.5, ysize=0.5,
            #xarea=(200.0, 300.0), yarea=(-40.0, 30.0))
            xarea=(230.0, 255.0), yarea=(-10.0, 15.0))
        #allsky.scale = 'sqrt'
        allsky.cmap="bw"
        allsky.yflip = 1
        if vrange != None:  allsky.varea = (vrange[0], vrange[1])
        if outfile==None:  pp.PlotHist(allsky, "sgrall_GC.png")
        else:  pp.PlotHist(allsky, outfile)
        #allsky.savehist("SDSSnorthGC.csv")
    print "Ended Successfully"
Exemplo n.º 7
0
def star_convolution(r, modfit, mu=4.2, sigma=0.6):
    """ Convolve Stars based on turnoff distribution Using Rejection Sampling"""
    m_g = co.getg(r)
    if not modfit: 
        return co.getr(np.random.normal(m_g, 0.6));
    found = 0;
    sigma_l = .36
    sigma_r = ((0.52 / (1.0 + sc.exp(12.0 - r))) + 0.76)
    sigma = .36
    while not found:
        guess = np.random.uniform(- (.6 * 3.0), .6 * 3.0);
        if guess < 0.0: 
            sigma = sigma_l
        else: 
            sigma = sigma_r
        probability = sc.exp(-(guess * guess) / (2.0 * sigma * sigma) ) / (.5 * (sigma_r + sigma_l) * SQ2PI)
        if (np.random.uniform() < (probability)): 
            found = 1
    return co.getr(m_g + guess)
Exemplo n.º 8
0
def star_convolution(r, modfit, mu=4.2, sigma=0.6):
    """ Convolve Stars based on turnoff distribution Using Rejection Sampling"""
    m_g = co.getg(r)
    if not modfit:
        return co.getr(np.random.normal(m_g, 0.6))
    found = 0
    sigma_l = .36
    sigma_r = ((0.52 / (1.0 + sc.exp(12.0 - r))) + 0.76)
    sigma = .36
    while not found:
        guess = np.random.uniform(-(.6 * 3.0), .6 * 3.0)
        if guess < 0.0:
            sigma = sigma_l
        else:
            sigma = sigma_r
        probability = sc.exp(
            -(guess * guess) /
            (2.0 * sigma * sigma)) / (.5 * (sigma_r + sigma_l) * SQ2PI)
        if (np.random.uniform() < (probability)):
            found = 1
    return co.getr(m_g + guess)
Exemplo n.º 9
0
def plot_stripe_mug(data, wedge, name="out", mag=0):
    """ SPREAD OUT MORE!!! """
    #l, b = data[:,0], data[:,1]
    if mag == 1: g = data[:, 2]
    else: g = coor.getg(data[:, 2])
    ra, dec = coor.lbToEq(data[:, 0], data[:, 1])
    mu, nu = coor.EqToGC(ra, dec, wedge)
    x, y = g * sc.cos(mu * rad), g * sc.sin(mu * rad)
    x_bins = int((np.ma.max(x) - np.ma.min(x)) / 0.5) + 1
    y_bins = int((np.ma.max(y) - np.ma.min(y)) / 0.5) + 1
    H, x, y = np.histogram2d(x, y, [x_bins, y_bins])
    extent = [np.ma.min(y), np.ma.max(y), np.ma.max(x), np.ma.min(x)]
    plt.figure(1)
    sp = plt.subplot(111)
    #plt.polar([0.0, np.ma.min(mu)*rad, np.ma.max(mu)*rad, 0.0], [0.0, 60.0, 60.0, 0.0])
    plt.imshow(H, extent=extent, interpolation='nearest')
    plt.colorbar(orientation='horizontal')
    #plt.plot(x1,y1,'r-')
    plt.setp(sp.get_xticklabels(), visible=False)
    plt.setp(sp.get_yticklabels(), visible=False)
    plt.savefig((name + ".ps"), papertype='letter')
    plt.close('all')
Exemplo n.º 10
0
def SDSS_eff(r):
    g0 = coor.getg(r, 4.2)
    bottom = sc.exp(1.6171*(g0-23.5877)) + 1.0
    return 0.9402/bottom
Exemplo n.º 11
0
import files as fi
import matplotlib
#matplotlib.use('PS')
import matplotlib.pyplot as plt
import functions as func
import astro_coordinates as coor

'''python script for quickly analyzing data.
Matthew Newby, November 15,2010'''

data = fi.read_data("Grillmair_2011_isochrone.dat")
data2 = fi.read_data("isochrone_8gyr_n1p0.dat")
data3 = fi.read_data("isochrone_7gyr_n0p8.dat")
data4 = fi.read_data("isochrone_6gyr_n0p8.dat")

g = coor.getg(9.7, data[:,8])
g_minus_r = data[:,8] - data[:,9]
g_minus_i = data[:,8] - data[:,10]

g2 = coor.getg(9.7, data2[:,8]) #+0.43
g3 = coor.getg(9.7, data3[:,8]) #+0.63
g4 = coor.getg(9.7, data4[:,8]) #+0.63

s1 = 0.41
s2 = 0.61
s3 = 0.52

M1 = 15.0 #coor.getM(15.0, 9.7)
M2 = 19.5 #coor.getM(19.5, 9.7)
M3 = 22.0 #coor.getM(22.0, 9.7)
Exemplo n.º 12
0
def plot_profiles(path="/home/newbym2/Desktop/starfiles", savewedge=False, suffix=""):
    files = glob.glob(path+"/stars*")
    print files
    suffix = "_rcut_IV"
    data=[]
    r_cut_low, r_cut_high = 40.0, ac.getr(22.5) #ac.getr(16.0), ac.getr(22.5) #20.0 OR 30.0, 45.0
    pb = pr.Progressbar(steps=len(files), prefix="Loading Stars:", suffix=None,
        symbol="#", active="=", brackets="[]", percent=True, size=40)
    for f in files:
        wedge = int(f.split("-")[1].split(".")[0])
        count=0
        stripedata = open(f, "r")
        for line in stripedata:
            count = count + 1
            if count==1:  continue
            if line.strip()=="":  continue
            temp = line.split()
            for i in range(len(temp)):  temp[i] = float(temp[i])
            if (temp[2] < r_cut_low) or (temp[2] > r_cut_high):  continue
            if ac.SDSS_primary(temp[0],temp[1],wedge,fmt="lb",low=9,high=27)==0:  continue
            data.append(temp)
        stripedata.close()
        pb.updatebar(float(files.index(f)+1)/float(len(files)) )
    datar = np.array(data)
    data = np.zeros(datar.shape, float)
    count, nStars, pb2 = 0, float(len(data[:,0])), pr.Progressbar(steps=100,
        prefix="Changing Coordinates:", suffix=None, symbol="#", active="=",
        brackets="[]", percent=True, size=40)
    for i in range(len(data[:,0])):
        count = count + 1
        #data[i,0], data[i,1] = ac.lb2GC(data[i,0], data[i,1], 15)
        #data[i,0], data[i,1] = ac.lbToEq(data[i,0], data[i,1])
        data[i,0], data[i,1] = (ac.lb2sgr(datar[i,0], datar[i,1], 30.0))[3:5]
        data[i,2] = ac.getg(datar[i,2], 4.2)
        if count % 100 == 0:  pb2.updatebar(float(count)/nStars)
    pb2.endbar()
    # loop over Lambda slices and plot Beta histograms
    pb3 = pr.Progressbar(steps=len(files), prefix="Making Slices:", suffix=None,
        symbol="#", active="=", brackets="[]", percent=True, size=40)
    Ls = (200.0, 300.0, 2.5)
    Lsteps = int((Ls[1]-Ls[0])/Ls[2])
    for i in range(Lsteps):
        Lname = str(Ls[0]+(Ls[2]*i) )[:6]+suffix
        new = []
        for j in range(len(data[:,0])):
            if data[j,0] < Ls[0]+(Ls[2]*i):  continue
            if data[j,0] > Ls[0]+(Ls[2]*(i+1)):  continue
            if savewedge:  
                new.append([datar[j,0], datar[j,1], data[j,2]])
                continue
            new.append(data[j,1])
        new = np.array(new)
        if savewedge:
            np.savetxt("stars-lambda-"+("00"+str(i))[-2:]+".txt", new, fmt='%.6f')
        else:
            Lhist, Ledges = np.histogram(new, 140, (-30.0, 40.0))
            #output to file
            outstuff = sc.array(zip(Ledges+0.25, Lhist))
            np.savetxt(Lname+".out", outstuff)
            #plot
            plt.figure(1)
            plt.bar(Ledges[:-1], Lhist, 0.5)
            plt.title(Lname)
            plt.xlabel("B")
            plt.savefig(Lname+".png")
            plt.close('all')
        pb3.updatebar(float(i)/float(Lsteps))
    print "Ended Successfully"    
Exemplo n.º 13
0
def perturb_density(x, y, z, parameters):
    """Density due to added galaxy-centered error profile"""
    r = sc.sqrt(x * x + y * y + z * z)
    g = co.getg(r)
    exponent = (g * parameters[1]) + parameters[2]
    return (parameters[0] + sc.exp(exponent))
Exemplo n.º 14
0
def stream_gen(params, sn, N_stars, batch=1000, fileout="streamgen82.txt",
                       detection=1, convolve=1, append=0):
    """ sn is stream number"""
    # Initialize file
    if append==0:
        out = open(fileout, 'w')
        out.write("# "+str(N_stars)+" stars, l,b,r,flag \n")
        out.close()
    # Get constants
    mu,R,theta,phi,sigma,wedge = \
        params.mu[sn],params.R[sn],params.theta[sn],params.phi[sn],params.sigma[sn], params.wedge
    u_min, u_max = twg.get_stream_length(params, sn, accuracy=0.00001)
    nu_min, nu_max = params.nu_lim[0], params.nu_lim[1]
    mu_min, mu_max = params.mu_lim[0], params.mu_lim[1]
    g_min, g_max = params.stripe[2][0], params.stripe[2][1]
    print "# - Generating Stream {0}, using parameters {1}, {2}, {3}, {4}, {5}".format(
        sn, mu, R, theta, phi, sigma)
    X_out, Y_out = 0, 0 # X_out is stars detected in stripe, Y_out is stars generated that should be there.
    while X_out < N_stars:
        mu_killed, nu_killed, mu_saved = 0,0,0
        u,v,w = twg.generate_stream(batch, u_min, u_max, sigma)
        holder = []
        for i in range(len(u)):
            mu1, nu1, r1 = coor.streamToGC(u[i],v[i],w[i],mu,R,theta,phi,wedge)
            l,b,r1 = coor.GC2lbr(mu1, nu1, r1, wedge)
            # Convolve
            if convolve==1:
                r1 = twg.star_convolution(r1)
            # Test nu, kill if invalid
            if (nu1 < nu_min) or (nu1 > nu_max):
                nu_killed=nu_killed+1
                #Y_out=Y_out+1   # COMMENT
                #holder.append([round(l,6),round(b,6),round(r1,6), 1])  # COMMENT
                continue
            # Test mu, keep in Y if out of bounds
            if (mu_max > 360.0):
                if (mu1 > (mu_max-360.0)) and (mu1 < mu_min):
                    mu_killed=mu_killed+1
                    Y_out=Y_out+1
                    holder.append([round(l,6),round(b,6),round(r1,6), 1])
                    continue
            else:
                if (mu1 < mu_min) or (mu1 > mu_max):
                    mu_killed=mu_killed+1
                    Y_out=Y_out+1
                    holder.append([round(l,6),round(b,6),round(r1,6), 1])
                    continue
            # Detection
            if detection==1:
                m_g = coor.getg(r1)
                if np.random.uniform() > (twg.sigmoid_error(m_g)):
                    Y_out=Y_out+1
                    holder.append([round(l,6),round(b,6),round(r1,6), 1])
                    continue
            # test g-limits
            if (coor.getg(r1) < g_min) or (coor.getg(r1) > g_max):
                Y_out=Y_out+1
                holder.append([round(l,6),round(b,6),round(r1,6), 1])
                continue
            # When a point passes all the tests add it to the set
            #co.stream2xyz(u[i],v[i],w[i],mu,R,theta,phi,wedge)
            holder.append([round(l,6),round(b,6),round(r1,6), 0])
            X_out = X_out + 1
        if X_out > N_stars:
            slice = -1*(X_out-N_stars)
            holder = holder[:slice]
            print "#---Sliced {0} stars to make quota".format(str(-1*slice))
            X_out = X_out + slice #Slice is negative
        #append X and Y to files
        if len(holder) != 0:
            if fi.append_data(sc.array(holder), fileout, delimiter=" ") == 1:
                print "#---Stream Progress: {0} stars of stars generated out of {1} detected".format(Y_out+X_out, N_stars)
                print "# !!! - out of mu: {0}, nu_killed: {1}".format(mu_killed, nu_killed)
    print "#---Stream {0} generation succeeded, written as {1}".format(sn, fileout)
    print " ### -----  Stream {0} Complete ----- ### \n"
    return fileout
Exemplo n.º 15
0
def generate_perturbation(num_stars,
                          params,
                          parameters,
                          batch=1000,
                          fail_quit=100,
                          fileout="Pertgen82.txt",
                          detection=1,
                          convolve=1,
                          append=0,
                          primary=0):
    """Density of perturbation added to background, as a function of position"""
    # Initialize file
    if append == 0:
        out = open(fileout, 'w')
        out.write("# " + str(num_stars) + " stars, l,b,r \n")
        out.close()
    # Get integral
    tot_prob = get_max_prob_2(params, parameters)
    N_out, fails = 0, 0
    g_min, g_max = params.stripe[2][0], params.stripe[2][1]
    while N_out < num_stars:
        # Generate Points
        mu, nu, r = get_stripe_points(params.mu_lim, params.nu_lim,
                                      params.r_lim, batch)
        # Test points for inclusion
        x, y, z = co.GC2xyz(mu, nu, r, params.wedge)
        holder = []
        for i in range(len(mu)):
            rho = perturb_density(x[i], y[i], z[i], parameters)
            #print (rho / tot_prob), rho, tot_prob
            if (rho / tot_prob) > np.random.uniform():
                l, b, r1 = co.xyz2lbr(x[i], y[i], z[i])
                # Convolve
                if convolve == 1:
                    r1 = star_convolution(r1, params.modfit)
                # Detection
                if detection == 1:
                    m_g = co.getg(r1)
                    if np.random.uniform() > sigmoid_error(m_g, params.modfit):
                        continue
                if (co.getg(r1) < g_min) or (co.getg(r1) > g_max): continue
                if primary == 1:
                    if co.SDSS_primary(l, b, wedge, fmt='lb', low=9,
                                       high=25) == 0:
                        continue
                # Add to keepers
                holder.append([round(l, 6), round(b, 6), round(r1, 6)])
                N_out = N_out + 1
        # Failure code
        if len(holder) == 0: fails = fails + 1
        if fails >= fail_quit: break
        # Remove possible excess stars
        if N_out > num_stars:
            slice = -1 * (N_out - num_stars)
            holder = holder[:slice]
            print("#---Sliced {0} stars to make quota".format(str(-1 * slice)))
            N_out = N_out + slice  #Slice is negative
        # Add to dataset
        if len(holder) != 0:
            if fi.append_data(sc.array(holder), fileout, delimiter=" ") == 1:
                print(
                    "#---Perturbation Progress: {0} stars of {1} total stars generated"
                    .format(N_out, num_stars))
    if fails >= fail_quit:
        print(
            "!!! Perturbation generation FAILED due to overstepping empty batch limit: {0}"
            .format(fail_quit))
    else:
        print(
            "#---Perturbation generation succeeded, written as {0}, with {1} empty batches"
            .format(fileout, fails))
    return fileout
Exemplo n.º 16
0
def stream_into_stripe(params,
                       sn,
                       N_stars,
                       batch=1000,
                       fileout="streamgen82.txt",
                       detection=1,
                       convolve=1,
                       append=0,
                       progbar=1,
                       primary=0):
    """ sn is stream number"""
    # Initialize file
    if append == 0:
        out = open(fileout, 'w')
        out.write("# " + str(N_stars) + " stars, l,b,r \n")
        out.close()
    # Get constants
    mu,R,theta,phi,sigma,wedge = \
        params.mu[sn],params.R[sn],params.theta[sn],params.phi[sn],params.sigma[sn], params.wedge
    print("Stream Parameters", mu, R, theta, phi, sigma, wedge)
    u_min, u_max = get_stream_length(params, sn, accuracy=0.0001)
    nu_min, nu_max = params.nu_lim[0], params.nu_lim[1]
    mu_min, mu_max = params.mu_lim[0], params.mu_lim[1]
    g_min, g_max = params.stripe[2][0], params.stripe[2][1]
    #print "# - Generating Stream {0}, using parameters {1}, {2}, {3}, {4}, {5}".format(
    #    sn, mu, R, theta, phi, sigma)
    N_out = 0
    pb = pr.Progressbar(steps=N_stars,
                        prefix="Stream {0} progress:".format(sn),
                        suffix="Generating {0} Stars".format(N_stars),
                        symbol="#",
                        active="=",
                        brackets="[]",
                        percent=True,
                        size=40)
    while N_out < N_stars:
        mu_killed, nu_killed, mu_saved = 0, 0, 0
        u, v, w = generate_stream(batch, u_min, u_max, sigma)
        holder = []
        for i in range(len(u)):
            mu1, nu1, r1 = co.streamToGC(u[i], v[i], w[i], mu, R, theta * deg,
                                         phi * deg, wedge)
            if (nu1 < nu_min) or (nu1 > nu_max):
                nu_killed = nu_killed + 1
                continue
            if (mu_max > 360.0):
                if (mu1 > (mu_max - 360.0)) and (mu1 < mu_min):
                    mu_killed = mu_killed + 1
                    continue
            else:
                if (mu1 < mu_min) or (mu1 > mu_max):
                    mu_killed = mu_killed + 1
                    continue
            if primary == 1:
                if co.SDSS_primary(mu1, nu1, wedge, low=9, high=25) == 0:
                    continue
            # Convolve
            if convolve == 1:
                r1 = star_convolution(r1, params.modfit)
            # Detection
            if detection == 1:
                m_g = co.getg(r1)
                if np.random.uniform() > sigmoid_error(m_g, params.modfit):
                    continue
            if (co.getg(r1) < g_min) or (co.getg(r1) > g_max): continue
            # When a point passes all the testsm add it to the set
            l, b, r1 = co.GC2lbr(mu1, nu1, r1, wedge)
            #co.stream2xyz(u[i],v[i],w[i],mu,R,theta,phi,wedge)
            holder.append([round(l, 6), round(b, 6), round(r1, 6)])
            N_out = N_out + 1
        if N_out > N_stars:
            slice = -1 * (N_out - N_stars)
            holder = holder[:slice]
            #print "#---Sliced {0} stars to make quota".format(str(-1*slice))
            N_out = N_out + slice  #Slice is negative
        #append to file
        if len(holder) != 0:
            if fi.append_data(sc.array(holder), fileout, delimiter=" ") == 1:
                #print "#---Stream Progress: {0} stars of {1} total stars generated".format(N_out, N_stars)
                #print "# !!! - mu killed: {0}, mu_saved: {1}, nu_killed: {2}".format(mu_killed, mu_saved, nu_killed)
                pb.updatebar(float(N_out) / float(N_stars))
    print("#---Stream {0} generation succeeded, written as {1}".format(
        sn, fileout))
    return fileout
Exemplo n.º 17
0
def stream_into_stripe(params, sn, N_stars, batch=1000, fileout="streamgen82.txt",
                       detection=1, convolve=1, append=0, progbar=1, primary=0):
    """ sn is stream number"""
    # Initialize file
    if append==0:
        out = open(fileout, 'w')
        out.write("# "+str(N_stars)+" stars, l,b,r \n")
        out.close()
    # Get constants
    mu,R,theta,phi,sigma,wedge = \
        params.mu[sn],params.R[sn],params.theta[sn],params.phi[sn],params.sigma[sn], params.wedge
    print "Stream Parameters", mu, R, theta, phi, sigma, wedge
    u_min, u_max = get_stream_length(params, sn, accuracy=0.0001)
    nu_min, nu_max = params.nu_lim[0], params.nu_lim[1]
    mu_min, mu_max = params.mu_lim[0], params.mu_lim[1]
    g_min, g_max = params.stripe[2][0], params.stripe[2][1]
    #print "# - Generating Stream {0}, using parameters {1}, {2}, {3}, {4}, {5}".format(
    #    sn, mu, R, theta, phi, sigma)
    N_out = 0
    pb = pr.Progressbar(steps=N_stars, prefix="Stream {0} progress:".format(sn), 
        suffix="Generating {0} Stars".format(N_stars), symbol="#", 
                active="=", brackets="[]", percent=True, size=40)
    while N_out < N_stars:
        mu_killed, nu_killed, mu_saved = 0,0,0
        u,v,w = generate_stream(batch, u_min, u_max, sigma)
        holder = []
        for i in range(len(u)):
            mu1, nu1, r1 = co.streamToGC(u[i],v[i],w[i],mu,R,theta*deg,phi*deg,wedge)
            if (nu1 < nu_min) or (nu1 > nu_max):  nu_killed=nu_killed+1; continue
            if (mu_max > 360.0):
                if (mu1 > (mu_max-360.0)) and (mu1 < mu_min):  mu_killed=mu_killed+1; continue
            else:
                if (mu1 < mu_min) or (mu1 > mu_max):  mu_killed=mu_killed+1; continue
            if primary == 1:
                if co.SDSS_primary(mu1,nu1,wedge,low=9,high=25) == 0:  continue
            # Convolve
            if convolve==1:
                r1 = star_convolution(r1, params.modfit)
            # Detection
            if detection==1:
                m_g = co.getg(r1)
                if np.random.uniform() > sigmoid_error(m_g, params.modfit):  continue
            if (co.getg(r1) < g_min) or (co.getg(r1) > g_max): continue
            # When a point passes all the testsm add it to the set
            l,b,r1 = co.GC2lbr(mu1, nu1, r1, wedge)
            #co.stream2xyz(u[i],v[i],w[i],mu,R,theta,phi,wedge)
            holder.append([round(l,6),round(b,6),round(r1,6)])
            N_out = N_out + 1
        if N_out > N_stars:
            slice = -1*(N_out-N_stars)
            holder = holder[:slice]
            #print "#---Sliced {0} stars to make quota".format(str(-1*slice))
            N_out = N_out + slice #Slice is negative
        #append to file
        if len(holder) != 0:
            if fi.append_data(sc.array(holder), fileout, delimiter=" ") == 1:
                #print "#---Stream Progress: {0} stars of {1} total stars generated".format(N_out, N_stars)
                #print "# !!! - mu killed: {0}, mu_saved: {1}, nu_killed: {2}".format(mu_killed, mu_saved, nu_killed)
                pb.updatebar(float(N_out)/float(N_stars))
    print "#---Stream {0} generation succeeded, written as {1}".format(sn, fileout)
    return fileout
Exemplo n.º 18
0
def perturb_density(x,y,z, parameters):
    """Density due to added galaxy-centered error profile"""
    r = sc.sqrt(x*x + y*y + z*z)
    g = co.getg(r)
    exponent = (g*parameters[1]) + parameters[2]
    return (parameters[0] + sc.exp(exponent))
Exemplo n.º 19
0
def stream_gen(params,
               sn,
               N_stars,
               batch=1000,
               fileout="streamgen82.txt",
               detection=1,
               convolve=1,
               append=0):
    """ sn is stream number"""
    # Initialize file
    if append == 0:
        out = open(fileout, 'w')
        out.write("# " + str(N_stars) + " stars, l,b,r,flag \n")
        out.close()
    # Get constants
    mu,R,theta,phi,sigma,wedge = \
        params.mu[sn],params.R[sn],params.theta[sn],params.phi[sn],params.sigma[sn], params.wedge
    u_min, u_max = twg.get_stream_length(params, sn, accuracy=0.00001)
    nu_min, nu_max = params.nu_lim[0], params.nu_lim[1]
    mu_min, mu_max = params.mu_lim[0], params.mu_lim[1]
    g_min, g_max = params.stripe[2][0], params.stripe[2][1]
    print "# - Generating Stream {0}, using parameters {1}, {2}, {3}, {4}, {5}".format(
        sn, mu, R, theta, phi, sigma)
    X_out, Y_out = 0, 0  # X_out is stars detected in stripe, Y_out is stars generated that should be there.
    while X_out < N_stars:
        mu_killed, nu_killed, mu_saved = 0, 0, 0
        u, v, w = twg.generate_stream(batch, u_min, u_max, sigma)
        holder = []
        for i in range(len(u)):
            mu1, nu1, r1 = coor.streamToGC(u[i], v[i], w[i], mu, R, theta, phi,
                                           wedge)
            l, b, r1 = coor.GC2lbr(mu1, nu1, r1, wedge)
            # Convolve
            if convolve == 1:
                r1 = twg.star_convolution(r1)
            # Test nu, kill if invalid
            if (nu1 < nu_min) or (nu1 > nu_max):
                nu_killed = nu_killed + 1
                #Y_out=Y_out+1   # COMMENT
                #holder.append([round(l,6),round(b,6),round(r1,6), 1])  # COMMENT
                continue
            # Test mu, keep in Y if out of bounds
            if (mu_max > 360.0):
                if (mu1 > (mu_max - 360.0)) and (mu1 < mu_min):
                    mu_killed = mu_killed + 1
                    Y_out = Y_out + 1
                    holder.append([round(l, 6), round(b, 6), round(r1, 6), 1])
                    continue
            else:
                if (mu1 < mu_min) or (mu1 > mu_max):
                    mu_killed = mu_killed + 1
                    Y_out = Y_out + 1
                    holder.append([round(l, 6), round(b, 6), round(r1, 6), 1])
                    continue
            # Detection
            if detection == 1:
                m_g = coor.getg(r1)
                if np.random.uniform() > (twg.sigmoid_error(m_g)):
                    Y_out = Y_out + 1
                    holder.append([round(l, 6), round(b, 6), round(r1, 6), 1])
                    continue
            # test g-limits
            if (coor.getg(r1) < g_min) or (coor.getg(r1) > g_max):
                Y_out = Y_out + 1
                holder.append([round(l, 6), round(b, 6), round(r1, 6), 1])
                continue
            # When a point passes all the tests add it to the set
            #co.stream2xyz(u[i],v[i],w[i],mu,R,theta,phi,wedge)
            holder.append([round(l, 6), round(b, 6), round(r1, 6), 0])
            X_out = X_out + 1
        if X_out > N_stars:
            slice = -1 * (X_out - N_stars)
            holder = holder[:slice]
            print "#---Sliced {0} stars to make quota".format(str(-1 * slice))
            X_out = X_out + slice  #Slice is negative
        #append X and Y to files
        if len(holder) != 0:
            if fi.append_data(sc.array(holder), fileout, delimiter=" ") == 1:
                print "#---Stream Progress: {0} stars of stars generated out of {1} detected".format(
                    Y_out + X_out, N_stars)
                print "# !!! - out of mu: {0}, nu_killed: {1}".format(
                    mu_killed, nu_killed)
    print "#---Stream {0} generation succeeded, written as {1}".format(
        sn, fileout)
    print " ### -----  Stream {0} Complete ----- ### \n"
    return fileout
Exemplo n.º 20
0
import scipy as sc
import files as fi
import matplotlib
#matplotlib.use('PS')
import matplotlib.pyplot as plt
import functions as func
import astro_coordinates as coor
'''python script for quickly analyzing data.
Matthew Newby, November 15,2010'''

data = fi.read_data("Grillmair_2011_isochrone.dat")
data2 = fi.read_data("isochrone_8gyr_n1p0.dat")
data3 = fi.read_data("isochrone_7gyr_n0p8.dat")
data4 = fi.read_data("isochrone_6gyr_n0p8.dat")

g = coor.getg(9.7, data[:, 8])
g_minus_r = data[:, 8] - data[:, 9]
g_minus_i = data[:, 8] - data[:, 10]

g2 = coor.getg(9.7, data2[:, 8])  #+0.43
g3 = coor.getg(9.7, data3[:, 8])  #+0.63
g4 = coor.getg(9.7, data4[:, 8])  #+0.63

s1 = 0.41
s2 = 0.61
s3 = 0.52

M1 = 15.0  #coor.getM(15.0, 9.7)
M2 = 19.5  #coor.getM(19.5, 9.7)
M3 = 22.0  #coor.getM(22.0, 9.7)
Exemplo n.º 21
0
def SDSS_eff(r):
    g0 = coor.getg(r, 4.2)
    bottom = sc.exp(1.6171 * (g0 - 23.5877)) + 1.0
    return 0.9402 / bottom