"""
This is a recipe to sit inside a directory and calculate a quantity for all of
the outputs in that directory.

If run with mpirun and the --parallel flag, this will take advantage of
multiple processors.
"""
from yt.mods import * # set up our namespace

# First set up our times and quantities lists
times = []
values = []

# this means get all the parameter files that it can autodetect and then supply
# them as parameter file objects to the loop.
for pf in all_pfs(max_depth=2):
    # Get the current time, convert to years from code units
    times.append(pf["InitialTime"] * pf["years"])

    # Now get a box containing the entire dataset
    data = pf.h.all_data()
    # Now we calculate the average.  The first argument is the quantity to
    # average, the second is the weight.
    # "lazy_reader" has two meanings -- the first is that it will try to
    # operate on each individual grid, rather than a flattened array of all the
    # data.  The second is that it will also distribute grids across multiple
    # processors, if multiple processors are in use.
    val = data.quantities["WeightedAverageQuantity"](
            "Temperature", "CellVolume", lazy_reader=True)
    values.append(val)

# Now we have our values and our time.  We can plot this in pylab!

import pylab
pylab.semilogy(times, values, '-x')
pylab.xlabel(r"$Time [years]$")
pylab.ylabel(r"$\mathrm{H}^{+}\/\/\mathrm{Fraction}$")
pylab.savefig("average_HII_fraction.png")
