from yt.mods import *
import re
import numpy as np

# Load the dataset.
fn, = glob.glob('RedshiftOutput????') 
output = int(re.sub(r'\D',"",fn))
pf = load(fn)

# Create a data container (like a sphere or region) that
# represents the entire domain.
#dd = pf.h.all_data()
f=open("../FOF/RedshiftOutput%04i-halos.dat"%output,'r')
line=f.readline();line=f.readline()
f.close()
c=np.array([float(line.split()[0]),float(line.split()[1]),float(line.split()[2])])

rvir=float(line.split()[8])
z=pf["CosmologyCurrentRedshift"]

wkpc = rvir*10 #in proper kpc 
#tkpc = 0.6 # in proper kpc
width =wkpc*(1.0+z)/40000.0 #in kpc
#thick =tkpc*(1.0+z)/40000.0 #in kpc
#c=[0.5,0.5,0.5]
RL=c-width/2 #RL=[0.425, 0.425, 0.425]
RR=c+width/2 #RR=[0.575, 0.575, 0.575]
dd = pf.h.region(c, RL, RR)



# Get the minimum and maximum densities.
#mi, ma = dd.quantities["Extrema"]("Density")[0]
mi=7e-30
ma=7e-22

# Create a transfer function to map field values to colors.
# We bump up our minimum to cut out some of the background fluid
tf = ColorTransferFunction((na.log10(mi)+2.0, na.log10(ma)))

# Add three guassians, evenly spaced between the min and
# max specified above with widths of 0.02 and using the
# gist_stern colormap.
tf.add_layers(3, w=0.02, colormap="gist_stern")

# Choose a vector representing the viewing direction.
L = [0.5, 0.5, 0.5]

# Set the width of the image.
# Decreasing or increasing this value
# results in a zoom in or out.
W = 1.0

# The number of pixels along one side of the image.
# The final image will have Npixel^2 pixels.
Npixels = 512

# Create a camera object.
# This object creates the images and
# can be moved and rotated.
cam = pf.h.camera(c, L, W, Npixels, tf, le=RL, re=RR)

# Create a snapshot.
# The return value of this function could also be accepted, modified (or saved
# for later manipulation) and then put written out using write_bitmap.
# clip_ratio applies a maximum to the function, which is set to that value
# times the .std() of the array.
cam.snapshot("%s_volume_rendered.png" % pf, clip_ratio=8.0)
