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"
def mag_plots(gmin=16.0, gmax=22.5): path = "/home/mnewby/Desktop/" #path="/home/newbym/Desktop/FTO-stars/" #files = [path+"MSTO_North_plus20.csv", path+"MSTO_South_minus20.csv"] #files = [path+"MSTO_North.csv", path+"MSTO_South.csv"] files = [path + "blue_all.csv"] out = [] for f in files: data = np.loadtxt(f, delimiter=",", skiprows=1) print "Loaded:", f pb = pr.Progressbar(steps=data.shape[0], prefix="Loading Stars:", suffix=None, symbol="#", active="=", brackets="[]", percent=True, size=40) for i in range(data.shape[0]): #if data[i,2] < ac.getr(16.0): continue #if data[i,2] > ac.getr(22.5): continue gmag = data[i, 3] # for BHB data #gmag = data[i,2] #for MSTO data if gmag < gmin: continue if gmag > gmax: continue lam, bet = (ac.lb2sgr(data[i, 0], data[i, 1], 30.0))[3:5] if lam > 170.0: #if bet > 10.0: continue #if bet < 2.5: continue if bet > -2.5: continue if bet < -10.0: continue else: #if bet < -2.5: continue #if bet > 5.0: continue if bet > -5.0: continue if bet < -12.5: continue out.append([lam, bet, gmag]) if i % 10000 == 0: pb.updatebar(float(i) / float(data.shape[0])) pb.endbar() print "Transformed coordinates:", f out = np.array(out) hist = pp.HistMaker(out[:, 0], out[:, 2], 0.5, 0.1, yarea=(16.0, 22.5), xarea=(0.0, 360.0)) hist.savehist(outfile="blue_faint_faint_hist.csv", fmt='%.1f') hist.yflip = 0 hist.xflip = 1 hist.labels = (r"$\Lambda$", r"$g_0$") hist.ticks = (sc.arange(20.0, 321.0, 20.0), sc.arange(16.0, 22.6, 0.5)) #hist.ticklabels = (None, ["16.0", "16.5", "17.0", "17.5", "18.0", "18.5", "19.0", "19.5", "20.0", # "20.5", "21.0", "21.5", "22.0", "22.5"]) hist.varea = (0.0, 30.0) pp.PlotHist(hist, imfile="blue_faint_faint.png", cbarO='horizontal') pp.PlotHist(hist, imfile="blue_faint_faint.ps", cbarO='horizontal')
def controlLoop(): print("Computing control...") controller = PID.PID(model.kP, model.kI, model.kD) progress_bar = progress.Progressbar(len(model.t) - 2) for i in range(len(model.t) - 1): progress_bar.printProgress(i) if i >= 1: # calculate starting on second cycle model.u[i + 1] = controller.update(model.x0[model.REFERENCE], model.sp[i], model.t[i + 1]) ts = [model.t[i], model.t[i + 1]] y = odeint(model.model, model.x0, ts, args=(model.u[i + 1], )) for j in range(len(model.VARS)): model.observables[j][i + 1] = y[-1][j] model.x0[j] = y[-1][j] print()
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"
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"
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
def sky_map(infile="/home/newbym2/Desktop/sdssN-stars/sgr_sky_data.csv", dumb=1): """Map a variable in Lambda, Beta """ if dumb == 1: data = np.loadtxt(infile, delimiter=",", skiprows=1) l, b = data[:, 0], data[:, 1] g_ext, r_ext = data[:, 4], data[:, 5] g_sky, r_sky = data[:, 6], data[:, 7] print np.ma.min(g_ext), np.ma.max(g_ext) lam, bet = ac.lb2sgr(l, b, 30.0)[3:5] plt.figure() sc = plt.scatter(lam, bet, c=g_ext, cmap=pp.spectral_wb, edgecolors='none', s=3, alpha=0.3, vmin=0, vmax=0.4) plt.colorbar(sc) plt.xlim(320, 20.0) plt.ylim(40.0, -70.0) else: path = "/home/newbym2/Desktop/FTO-stars/" files = glob.glob(path + "FTO_All*") files.append(path + "FTO_south_dirty.csv") x_range, y_range = (20, 320), (-70, 40) x_step, y_step = 0.5, 0.5 x_bins, y_bins = (x_range[1] - x_range[0]) / x_step, ( y_range[1] - y_range[0]) / y_step try: grid = np.loadtxt("reddening_out.txt") except IOError: grid = np.zeros((y_bins + 1, x_bins + 1), float) for f in files: data = np.loadtxt(f, delimiter=",", skiprows=1) pb = pr.Progressbar(steps=data.shape[0], prefix="Loading {0}".format( f.split("/")[-1]), suffix=None, symbol="#", active="=", brackets="[]", percent=True, size=40) for i in range(data.shape[0]): lam, bet = (ac.lb2sgr(data[i, 0], data[i, 1], 30.0))[3:5] gridx = (lam - x_range[0]) / x_step gridy = (bet - y_range[0]) / y_step if (gridx < 0) or (gridy < 0): continue if (gridx > x_bins + 1) or (gridy > y_bins + 1): continue if grid[gridy, gridx] < data[i, 13]: grid[gridy, gridx] = data[i, 13] if i % 1000 == 0: pb.updatebar(float(i) / float(data.shape[0])) pb.endbar() np.savetxt("reddening_out.txt", grid, fmt="%.2f") """ plot stuff """ grid = grid[:, ::-1] grid = grid[::-1, :] fig = plt.figure(dpi=120) ax1 = fig.add_subplot(111) im = ax1.imshow(grid, cmap=pp.spectral_wb, norm=None, aspect=None, interpolation='nearest', alpha=None, origin='lower', extent=None, vmin=0.0, vmax=2.0) cbar = plt.colorbar(mappable=im, aspect=30, orientation="horizontal") xpos = np.arange(0.0, 601.0, 20.0) xlab = [] for i in range(len(xpos)): if i % 3 == 0: xx = (xpos[i] * x_step) + x_range[0] xlab.append(str(xx).split(".")[0]) else: xlab.append("") plt.xticks(xpos, xlab[::-1]) ypos = np.arange(0.0, 221.0, 20.0) ylab = [] for i in range(len(ypos)): if i % 2 == 0: yy = (ypos[i] * y_step) + y_range[0] ylab.append(str(yy).split(".")[0]) else: ylab.append("") plt.yticks(ypos, ylab[::-1]) plt.show()
def lambet_profiles(path="/home/newbym2/Desktop/sdssN-stars/", glb="*bhbs_all*", lj=0, bj=1, gj=2, lbins=2.5, bbins=0.5, suffix="_bhbs", coords="sgr"): """ remixed from plot_profiles in utilities1 """ files = glob.glob(path + glb) print files data = [] g_cut_low, g_cut_high = 16.0, 22.5 pb = pr.Progressbar(steps=len(files), prefix="Loading Stars:", suffix=None, symbol="#", active="=", brackets="[]", percent=True, size=40) for f in files: count = 0 stripedata = np.loadtxt(f, delimiter=",", skiprows=1) for i in range(stripedata.shape[0]): if stripedata[i, gj] < g_cut_low: continue if stripedata[i, gj] > g_cut_high: continue #if ac.SDSS_primary(temp[0],temp[1],wedge,fmt="lb",low=9,high=27)==0: continue data.append( [stripedata[i, lj], stripedata[i, bj], stripedata[i, gj]]) pb.updatebar(float(files.index(f) + 1) / float(len(files))) data = np.array(data) if coords == "sgr": 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(data.shape[0]): count += 1 data[i, 0], data[i, 1] = ac.lb2sgr(data[i, 0], data[i, 1], 50.0)[3:5] #data[i,2] = data[i,2] if count % 1000 == 0: pb2.updatebar(float(count) / nStars) elif coords == "lbr": pass #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]) else: print "!!! Coordinate system not recognized !!!" sys.exit(2) 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 = (40.0, 130.0, lbins) #Ls = (200.0, 300.0, lbins) Ls = (201.25, 301.25, lbins) #for wedges offset by half a wedge 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 new.append([data[j, 0], data[j, 1], data[j, 2]]) if len(new) < 1: continue new = np.array(new) #np.savetxt("stars-lambda-"+("00"+str(i))[-2:]+".txt", new, fmt='%.6f') pb3.updatebar(float(i) / float(Lsteps)) Lhist, Ledges = np.histogram(new[:, 1], int(100 / bbins), (-60.0, 40.0)) #output to file outstuff = sc.array(zip(Ledges + (bbins / 2.0), Lhist)) np.savetxt(Lname + ".out", outstuff, fmt='%.2f') pb3.endbar() print "Ended Successfully"