Ejemplo n.º 1
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)
Ejemplo n.º 2
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"
Ejemplo n.º 3
0
def count_stars_in_rcut():
    rmin, rmax = ac.getr(16.0), ac.getr(23.5)
    print rmin, rmax
    path="/home/newbym2/Desktop/starfiles"
    files = glob.glob(path+"/stars*")
    total = 0
    for f in files:
        wedge = int(f.split("-")[1].split(".")[0])
        count = 0
        data = np.loadtxt(f, skiprows=1)
        for i in range(len(data[:,0])):
            if data[i,2] < rmin:  continue
            if data[i,2] > rmax:  continue
            if ac.SDSS_primary(data[i,0],data[i,1],wedge,fmt="lb",low=9,high=27)==0:  continue
            count = count + 1
            total = total + 1
        print count, len(data[:,2]), np.ma.min(data[:,2]), np.ma.max(data[:,2])
    print "Final Count:", total
Ejemplo n.º 4
0
def crotus_cut(path="/home/newbym2/Desktop/starfiles", cdata=None):
    if cdata == None:
        files = glob.glob(path+"/stars*")
        data=[]
        r_cut_low, r_cut_high = ac.getr(16.0), ac.getr(23.5) #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])
            sdata = np.loadtxt(f, skiprows=1)
            for i in range(sdata.shape[0]):
                if (sdata[i,2] < r_cut_low) or (sdata[i,2] > r_cut_high):  continue
                if ac.SDSS_primary(sdata[i,0],sdata[i,1],wedge,fmt="lb",low=9,high=27)==0:  continue
                lam, bet = (ac.lb2sgr(sdata[i,0], sdata[i,1], 10.0))[3:5]
                if (lam <230.0) or (lam > 255.0):  continue
                data.append([sdata[i,0], sdata[i,1], sdata[i,2], lam, bet])
            pb.updatebar(float(files.index(f)+1)/float(len(files)) )
        pb.endbar()
        data = np.array(data)
        np.savetxt("crotus_data.txt", data, fmt='%.6f')
    else:  
        data = []
        rdata = np.loadtxt(cdata)
        rlim0 = 28.5  #ac.getr(22.5)
        rlim1 = 45.0
        for i in range(rdata.shape[0]):
            if rdata[i,2] < rlim0:  continue
            if rdata[i,2] > rlim1:  continue
            data.append(rdata[i,:])
        data = np.array(data)
    #Now do analysis
    sky = pp.HistMaker(data[:,3], data[:,4], xsize=0.25, ysize=0.25, 
        xarea=(230.0, 255.0), yarea=(-10.0, 15.0))
    sky.varea = (0.0, 60.0)
    sky.cmap="color"
    #sky.scale = 'sqrt'
    sky.yflip = 1
    pp.PlotHist(sky, "crotus_cut.png")
    #sky.savehist("streamgen_bifExtra.csv")
    print "### - Done"
Ejemplo n.º 5
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"    
Ejemplo n.º 6
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
Ejemplo n.º 7
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