def generate_temperature_plot(rundir, distance, scale, sigma=None):
    gadgetparms = parse_gadget_parms(rundir + "snaps/gadget2.par")
    TimeBetSnapshot = gadgetparms['TimeBetSnapshot']

    print "Distance matrix:\n", distance
    # bestsnap = numpy.intersect1d(numpy.where(distance > 650), numpy.where(distance < 750))[0]
    bestsnap = numpy.nanargmin(numpy.abs(distance - 701))
    print "Bestsnap, distance =", bestsnap, distance[bestsnap]

    # snaprange = len(distance)
    snaprange = bestsnap + 3 if bestsnap + 3 < len(distance) else bestsnap
    for snap in range(snaprange):
        plot_temperature_along_merger_axis(rundir, snap, distance[snap], scale,
                                           TimeBetSnapshot, sigma)
Exemplo n.º 2
0
def make_all_hists(timestamp):
    rundir = "../runs/{0}/".format(timestamp)
    if not (os.path.isdir(rundir + "out") or os.path.exists(rundir + "out")):
        os.mkdir(rundir + "out")

    gadgetparms = parse_gadget_parms(rundir + "snaps/gadget2.par")
    TimeBetSnapshot = gadgetparms["TimeBetSnapshot"]

    snaps = sorted(glob.glob(rundir + "snaps/snapshot_*"),
                   key=os.path.getmtime)
    for snap in snaps:
        # e.g. "../runs/20160727T1112/snaps/snapshot_051"
        snapnr = snap.split('/')[-1].split('_')[-1]  # gets the 051
        if snapnr == "000" or snapnr == "033":
            plot_hist_gasvx(rundir, snapnr, TimeBetSnapshot)
Exemplo n.º 3
0
def plot_all(rundir):
    # For time counter
    gadgetparms = parse_gadget_parms(rundir+"snaps/gadget2.par")
    TimeBetSnapshot = gadgetparms['TimeBetSnapshot']

    toyclusterparms = [file for file in os.listdir(rundir+"ICs") if "par" in file][0]
    toyclusterparms = parse_toycluster_parms(toyclusterparms)
    particles = toyclusterparms['Ntotal']

    if particles > 2e6:
        print "Strap in, this could take a while :-) ..."

    files = sorted(glob.glob(rundir+"rotation2/*.fits.fz"), key=os.path.getmtime)

    with fits.open(files[0]) as f:
        header = f[0].header
        data = f[0].data

        # Set colourtable based on P-Smac module/flag (in fits header)
        for line in repr(header).split("\n"):
            if "Effect_Module" in line:
                module = line.strip().split("=")[-1].strip().split("/")[0]
            if "Effect_Flag" in line:
                flag = line.strip().split("=")[-1].strip().split("/")[0]
            if "XYSize" in line:  # Could also be obtained from gadgetparms
                scale = line.strip().split("=")[-1].strip().split("/")[0]
            if "Description" in line:  # For tile title: physical property
                name = line.strip().split("/")[-1].strip()
            # To see entire header, including comments starting with "/"
            # print line

        cmap = helix_tables(int(module.strip()), int(flag.strip()))

        scale = "[{0:.1f} Mpc]^2".format(float(scale)/1000)

        xlen = header['NAXIS1']
        ylen = header['NAXIS2']

    # files = [file for file in files if "xray_180_" in file]

    for filename in files:
        make_plot(filename, cmap, scale, xlen, ylen, name)
Exemplo n.º 4
0
    def __init__(self, arguments, discard_firstbins=True):
        self.timestamp = arguments.simulationID[0]
        self.observed = ObservedCluster(
            arguments.clustername) if arguments.clustername else None
        if arguments.clustername and arguments.clustername == "cygA" and discard_firstbins:
            print "WARNING: Discarding first three CygA bins.\n"
            self.observed.radius = self.observed.radius[3:]
            self.observed.binsize = self.observed.binsize[3:]
            self.observed.density = self.observed.density[3:]
            self.observed.density_std = self.observed.density_std[3:]
            self.observed.number_density = self.observed.number_density[3:]
            self.observed.number_density_std = self.observed.number_density_std[
                3:]
        if arguments.IC_only:
            # TODO: there is something different in the Toycluster rho
            # than in the Gadget output :(
            self.IC_only = True
            self.icdir = "../runs/{0}/ICs/".format(self.timestamp)
            self.snapdir = "../runs/{0}/ICs/".format(self.timestamp)
            self.snaps = ["../runs/{0}/ICs/IC_single_0".format(self.timestamp)]
        else:
            self.IC_only = False
            self.icdir = "../runs/{0}/ICs/".format(self.timestamp)
            self.snapdir = "../runs/{0}/snaps/".format(self.timestamp)
            self.snaps = sorted(glob.glob(
                "../runs/{0}/snaps/snapshot_*".format(self.timestamp)),
                                key=os.path.getmtime)
            print self.snaps, "\n"

        self.outdir = "../runs/{0}/out/".format(self.timestamp)
        if not (os.path.isdir(self.outdir) or os.path.exists(self.outdir)):
            os.mkdir(self.outdir)

        if self.IC_only:
            self.TimeBetSnapshot = 0
        else:
            # For time counter
            gadgetparms = parse_gadget_parms(self.snapdir + "gadget2.par")
            self.TimeBetSnapshot = gadgetparms['TimeBetSnapshot']
def generate_distance_plots(rundir):
    # For time counter
    gadgetparms = parse_gadget_parms(rundir + "snaps/gadget2.par")
    TimeBetSnapshot = gadgetparms['TimeBetSnapshot']

    # toyclusterparms = parse_toycluster_parms(rundir+"ICs/ic_both_hybrid.par")
    # particles = toyclusterparms['Ntotal']

    projection = "z"
    projection = "_projection-{0}".format(projection)
    option = "xray-surface-brightness"

    xraycube = rundir + "analysis/" + option + projection + ".fits.fz"
    print "Simulation surface brightness:", xraycube
    with fits.open(xraycube) as f:
        header = f[0].header
        data = f[0].data

        # Set colourtable based on P-Smac module/flag (in fits header)
        for line in repr(header).split("\n"):
            if "Effect_Module" in line:
                module = line.strip().split("=")[-1].strip().split("/")[0]
            if "Effect_Flag" in line:
                flag = line.strip().split("=")[-1].strip().split("/")[0]
            if "XYSize" in line:  # Could also be obtained from gadgetparms
                scale = line.strip().split("=")[-1].strip().split("/")[0]
        cmap = helix_tables(int(module.strip()), int(flag.strip()))

    number_of_snapshots = header['NAXIS3']
    distance = numpy.zeros(number_of_snapshots)
    x = numpy.zeros(number_of_snapshots, dtype=object)
    y = numpy.zeros(number_of_snapshots, dtype=object)

    for n in range(number_of_snapshots):
        distance[n], x[n], y[n] = plot_snapshot(rundir, data[n], n, scale,
                                                cmap, TimeBetSnapshot)

    return distance, x, y, scale
Exemplo n.º 6
0
def find_snapshot(rundir):
    """ Find the most likely snapshot number based on the X-ray observation.

    From the X-ray observation we find the two peaks of emission are separated
    by 700 seconds of arc. """

    # For time counter
    gadgetparms = parse_gadget_parms(rundir + "snaps/gadget2.par")
    TimeBetSnapshot = gadgetparms['TimeBetSnapshot']

    # toyclusterparms = parse_toycluster_parms(rundir+"ICs/ic_both_hybrid.par")
    # particles = toyclusterparms['Ntotal']

    projection = "z"
    projection = "_projection-{0}".format(projection)
    option = "xray-surface-brightness"

    xraycube = rundir + "analysis/" + option + projection + ".fits.fz"
    print xraycube
    with fits.open(xraycube) as f:
        header = f[0].header
        data = f[0].data

        # Set colourtable based on P-Smac module/flag (in fits header)
        for line in repr(header).split("\n"):
            if "Effect_Module" in line:
                module = line.strip().split("=")[-1].strip().split("/")[0]
            if "Effect_Flag" in line:
                flag = line.strip().split("=")[-1].strip().split("/")[0]
            if "XYSize" in line:  # Could also be obtained from gadgetparms
                scale = line.strip().split("=")[-1].strip().split("/")[0]
        cmap = helix_tables(module.strip(), flag.strip())

        # To see entire header, including comments starting with "/"
        # print line

    print "Effect_Module", module
    print "Effect_Flag", flag

    number_of_snapshots = header['NAXIS3']
    xlen = int(header['NAXIS1'])
    ylen = int(header['NAXIS2'])
    pixelscale = float(scale) / int(xlen)

    print "# snapshots =", number_of_snapshots
    print "BoxSize     =", scale
    print "xlen        =", xlen
    print "ylen        =", ylen
    print "kpc/pixel   =", pixelscale

    # https://stackoverflow.com/questions/9111711
    neighborhood_size = 2**(-5) * xlen
    threshold = 0

    fig = pyplot.figure(figsize=(12, 9))
    fig.gca().set_xlim(0, 0.75 * xlen)
    fig.gca().set_ylim(0, 0.75 * ylen)
    im = pyplot.imshow(numpy.log(data[0][0.125 * xlen:0.875 * xlen,
                                         0.125 * ylen:0.875 * ylen]),
                       cmap=cmap)
    hank = fig.gca().plot(0, 0, "ro")
    scale_text = "[{0:.1f} Mpc]^2".format(float(scale) / 1000)
    pad = 4
    pyplot.text(xlen - pad,
                pad,
                scale_text,
                color="white",
                size=42,
                horizontalalignment="right",
                verticalalignment="bottom")

    def init():
        print "hank"
        im.set_data(numpy.zeros((xlen, ylen)))
        global hank
        hank = fig.gca().plot(0, 0, "ro")
        for n in range(len(hank)):
            hank.pop(n).remove()
        return [im]

    def animate(i):
        global hank
        for n in range(len(hank)):
            hank.pop(n).remove()
        snapshot = numpy.log(data[i])
        #borderless_snapshot = snapshot[]
        snapshot = snapshot[0.125 * xlen:0.875 * xlen,
                            0.125 * ylen:0.875 * ylen]
        data_max = scipy.ndimage.filters.maximum_filter(
            snapshot, neighborhood_size)
        maxima = (snapshot == data_max)
        data_min = scipy.ndimage.filters.minimum_filter(
            snapshot, neighborhood_size)
        diff = ((data_max - data_min) > threshold)
        maxima[diff == 0] = 0

        labeled, num_objects = scipy.ndimage.label(maxima)
        slices = scipy.ndimage.find_objects(labeled)
        x, y = [], []
        for dy, dx in slices:
            x_center = (dx.start + dx.stop - 1) / 2
            x.append(x_center)
            y_center = (dy.start + dy.stop - 1) / 2
            y.append(y_center)

        print "Snapshot:", i
        for a, b in zip(x, y):
            print "  (x, y) = ({0:3d}, {1:3d})".format(a, b)
        if len(x) == 2:
            distance = numpy.sqrt(p2(x[0] - x[1]) + p2(y[0] - y[1]))
            print "  distance = {0:<5.2f}".format(distance)
            print "  distance = {0:<5.2f} kpc".format(distance * pixelscale)
        im.set_data(snapshot)
        if i < number_of_snapshots - 1:
            hank = fig.gca().plot(x, y, "ro")
        return [im]

    anim = animation.FuncAnimation(fig,
                                   animate,
                                   init_func=init,
                                   frames=number_of_snapshots,
                                   interval=500,
                                   repeat_delay=5000)
    pyplot.show()

    return

    pad = 4  # number of pixels padding for text placement (of tile titles)
    for n in range(number_of_snapshots):
        # Set up four-panel plot, stitched together
        pyplot.figure(figsize=(16, 16))
        pyplot.style.use(["dark_background"])
        gs1 = gridspec.GridSpec(2, 2)
        #gs1 = gridspec.GridSpec(1, 1)
        gs1.update(wspace=0, hspace=0)  # remove spacing between axes.

        for i in range(4):  # gridspec indexes start at 0
            #for i in range(1):  # gridspec indexes start at 0
            ax = pyplot.subplot(gs1[i])
            pyplot.axis('on')
            ax.set_xticklabels([])
            ax.set_yticklabels([])
            # NB: breaks when using pyplot.show(), save only!
            ax.set_aspect('equal')

            # Plot every panel. TODO: change way log/sqrt scheme is chosen
            # Now all properties look good in log stretch, but DM density
            # seems to look better using sqrt scaling!
            if "dm" not in chosen[i]:
                ax.imshow(numpy.log(data[n]), cmap=cmap[i])
            else:
                ax.imshow(numpy.sqrt(data[n]), cmap=cmap[i])
            # Tile text: name of physical property
            pyplot.text(pad if i % 2 == 0 else xlen - pad,
                        pad,
                        names[i],
                        color="white",
                        size=42,
                        horizontalalignment="left" if i % 2 == 0 else "right",
                        verticalalignment="top")
            # pyplot.text(pad if i%2==0 else xlen-pad, ylen-pad,
            #             names[i], color="white", size=42,
            #             horizontalalignment="left" if i%2==0 else "right",
            #             verticalalignment="bottom")
        # Image scale (lives in lower right corner of tile 3)
        pyplot.text(xlen - pad,
                    ylen - pad,
                    scale,
                    color="white",
                    size=42,
                    horizontalalignment="right",
                    verticalalignment="bottom")

        # pyplot.suptitle("T = {0:05.2f} Myr".format(0.05*n),
        pyplot.suptitle("T = {0:04.2f} Gyr".format(TimeBetSnapshot * n),
                        color="white",
                        size=52,
                        y=0.95)
        pyplot.tight_layout()
        pyplot.savefig(rundir +
                       "out/snapshot{0}_{1:03d}.png".format(projection, n))
        pyplot.close()

        print_progressbar(n, number_of_snapshots)
Exemplo n.º 7
0
def make_video(rundir, chosen, projection):
    """ Make a video of four physical properties: the chosen options """

    if not (os.path.isdir(rundir + "out") or os.path.exists(rundir + "out")):
        os.mkdir(rundir + "out")

    projection = "_projection-{0}".format(projection)

    # For time counter
    gadgetparms = parse_gadget_parms(rundir + "snaps/gadget2.par")
    TimeBetSnapshot = gadgetparms['TimeBetSnapshot']

    toyclusterparms = parse_toycluster_parms(rundir + "ICs/ic_both_hybrid.par")
    particles = toyclusterparms['Ntotal']

    if particles > 2e6:
        print "Strap in, this could take a while :-) ..."

    headers = []
    data = []
    cmap = []
    names = []
    for option in chosen:
        with fits.open(rundir + "analysis/" + option + projection +
                       ".fits.fz") as f:
            headers.append(f[0].header)
            data.append(f[0].data)

            # Set colourtable based on P-Smac module/flag (in fits header)
            for line in repr(f[0].header).split("\n"):
                if "Effect_Module" in line:
                    module = line.strip().split("=")[-1].strip().split("/")[0]
                if "Effect_Flag" in line:
                    flag = line.strip().split("=")[-1].strip().split("/")[0]
                if "XYSize" in line:  # Could also be obtained from gadgetparms
                    scale = line.strip().split("=")[-1].strip().split("/")[0]
                if "Description" in line:  # For tile title: physical property
                    names.append(line.strip().split("/")[-1])
            cmap.append(helix_tables(module.strip(), flag.strip()))

            # To see entire header, including comments starting with "/"
            # print line

    scale = "[{0:.1f} Mpc]^2".format(float(scale) / 1000)

    number_of_snapshots = headers[0]['NAXIS3']
    xlen = headers[0]['NAXIS1']
    ylen = headers[0]['NAXIS2']

    pad = 4  # number of pixels padding for text placement (of tile titles)
    for n in range(number_of_snapshots):
        # Set up four-panel plot, stitched together
        pyplot.figure(figsize=(16, 16))
        pyplot.style.use(["dark_background"])
        gs1 = gridspec.GridSpec(2, 2)
        #gs1 = gridspec.GridSpec(1, 1)
        gs1.update(wspace=0, hspace=0)  # remove spacing between axes.

        for i in range(4):  # gridspec indexes start at 0
            #for i in range(1):  # gridspec indexes start at 0
            ax = pyplot.subplot(gs1[i])
            pyplot.axis('on')
            ax.set_xticklabels([])
            ax.set_yticklabels([])
            # NB: breaks when using pyplot.show(), save only!
            ax.set_aspect('equal')

            # Plot every panel. TODO: change way log/sqrt scheme is chosen
            # Now all properties look good in log stretch, but DM density
            # seems to look better using sqrt scaling!
            if "dm" not in chosen[i]:
                ax.imshow(numpy.log(data[i][n]), cmap=cmap[i])
            else:
                ax.imshow(numpy.sqrt(data[i][n]), cmap=cmap[i])
            # Tile text: name of physical property
            pyplot.text(pad if i % 2 == 0 else xlen - pad,
                        pad,
                        names[i],
                        color="white",
                        size=42,
                        horizontalalignment="left" if i % 2 == 0 else "right",
                        verticalalignment="top")
            # pyplot.text(pad if i%2==0 else xlen-pad, ylen-pad,
            #             names[i], color="white", size=42,
            #             horizontalalignment="left" if i%2==0 else "right",
            #             verticalalignment="bottom")
        # Image scale (lives in lower right corner of tile 3)
        pyplot.text(xlen - pad,
                    ylen - pad,
                    scale,
                    color="white",
                    size=42,
                    horizontalalignment="right",
                    verticalalignment="bottom")

        # pyplot.suptitle("T = {0:05.2f} Myr".format(0.05*n),
        pyplot.suptitle("T = {0:04.2f} Gyr".format(TimeBetSnapshot * n),
                        color="white",
                        size=52,
                        y=0.95)
        pyplot.tight_layout()
        pyplot.savefig(rundir +
                       "out/snapshot{0}_{1:03d}.png".format(projection, n))
        pyplot.close()

        print_progressbar(n, number_of_snapshots)