Source

yt_map / run.py

from yt.mods import *
#pf = load("/home/mturk/Research/data/JHK-DD0030/galaxy0030")
#pf = load("/home/mturk/Research/data/DD0250/DD0250")
pf = load("/home/mturk/Research/data/bfield_j64/DD0053/DD0053")
proj = pf.h.proj(2, "Density", weight_field="Density")
TS = 256
import tempfile
from yt.visualization.image_writer import apply_colormap
from yt.utilities.amr_utils import write_png_to_file

# We say 256 pixels for each tile, and we have 

import bottle
bottle.debug(True)

mi = na.log10(proj["Density"][proj["Density"]>0].min())
ma = na.log10(proj["Density"][proj["Density"]>0].max())

def get_realistic_bounds(field, pdx, width):
    # We don't want to scale to anything that's as big or bigger than the
    # possible viewing area.  This should be unique across tiles on this level.
    min_dx = width / TS
    ff = na.log10(field[(pdx > min_dx / 64.0) & (pdx < width * 2.0)])
    return ff.min(), ff.max()

@bottle.route("/map/:L/:x/:y.png")
def map(L, x, y):
    dd = TS / (128 * 2.0**int(L))
    relx = int(x) * dd
    rely = int(y) * dd
    DW = (pf.domain_left_edge + pf.domain_right_edge)/2.0
    xp = pf.domain_left_edge[0] + relx * DW[0]
    yp = pf.domain_left_edge[1] + rely * DW[1]
    frb = FixedResolutionBuffer(proj, (xp, xp+dd*DW[0], yp, yp+dd*DW[1]), (256, 256))
    tf = tempfile.TemporaryFile()
    cmi, cma = get_realistic_bounds(proj["Density"], proj["pdx"], dd*DW[0])
    to_plot = apply_colormap(na.log10(frb["Density"]), color_bounds = (cmi, cma))
    print "=="
    print 'rel', dd, relx, rely
    print 'x', xp, xp+dd*DW[0]
    print 'y', yp, yp+dd*DW[1]
    print 'mi', cmi, frb["Density"].min()
    print 'ma', cma, frb["Density"].max()
    write_png_to_file(to_plot, tf)
    tf.seek(0)
    return tf.read()

@bottle.route("/")
@bottle.route("/index.html")
def index():
    return bottle.static_file("index.html", root=".")

@bottle.route("/static/:filename#.+#")
def static(filename):
    return bottle.static_file(filename, root=".")

bottle.run(server='rocket')