Commits

Britton Smith  committed 64ee457

Fixing up simple recipes and moving to .py files.

  • Participants
  • Parent commits 8715d11

Comments (0)

Files changed (16)

File source/cookbook/arbitrary_vectors_on_slice.py

-from yt.mods import *
-
-# Load the dataset.
-pf = load(RedshiftOutput0005)
-
-# Create a plot collection for the dataset.
-# With no additional arguments, the center will be
-# the densest point in the box.
-pc = PlotCollection(pf)
-
-# Make a density slice in the x axis.
-p = pc.add_slice("Density", "x")
-
-# Draw a velocity vector every 16 pixels.
-p.modify["velocity"](factor=16)
-
-# Change the width of the image.
-pc.set_width(2.5, 'mpc')
-
-# Save the image.
-# Optionally, give a string as an argument
-# to name files with a keyword.
-pc.save()

File source/cookbook/multi_width_image.py

+from yt.mods import *
+
+# Load the dataset.
+pf = load("RedshiftOutput0005")
+
+# Create a plot collection for the dataset.
+# With no additional arguments, the center will be
+# the densest point in the box.
+pc = PlotCollection(pf)
+
+# Create a list of a couple of widths and units.
+widths = [(2, "mpc"),
+          (1000, 'kpc')]
+
+# Create some density slices.
+pc.add_slice("Density", "x")
+pc.add_slice("Density", "y")
+
+# Loop through the list of widths and units.
+for width, unit in widths:
+
+    # Set the width.
+    pc.set_width(width, unit)
+
+    # Get the min and max values within the image and
+    # set the limits accordingly.
+    vmin = min([p.norm.vmin for p in pc.plots])
+    vmax = max([p.norm.vmax for p in pc.plots])
+    pc.set_zlim(vmin,vmax)
+    
+    # Write out the image with a unique name.
+    pc.save("%s_%010d_%s" % (pf, width, unit))

File source/cookbook/multi_width_save.inc

-.. _cookbook-multi_width_save:
-
-Multi width save
-----------------
-
-This recipe shows a slightly-fancy way to save a couple plots at a lot of
-different widths, ensuring that across the plots we have the same min/max for
-the colorbar.
-
-The latest version of this recipe can be downloaded here: http://hg.yt-project.org/cookbook/raw/tip/recipes/multi_width_save.py .
-
-.. code-block:: python
-
-   from yt.mods import *
-   
-   fn = "RedshiftOutput0005" # parameter file to load
-   pf = load(fn) # load data
-   
-   pc = PlotCollection(pf, center=[0.5, 0.5, 0.5]) # We get our Plot Collection object
-   
-   # Note that when we save, we will be using string formatting to change all of
-   # the bits in here.  You can add more, or remove some, if you like.
-   fn = "%(bn)s_%(width)010i_%(unit)s" # template for image file names
-   
-   # Now let's set up the widths we want to use.
-   widths = [ (2, "mpc"), (1000, 'kpc')]
-   # We could add on more of these with:
-   #  widths += [ ... ]
-   
-   # Now we add a slice for x and y.
-   pc.add_slice("Density", 0)
-   pc.add_slice("Density", 1)
-   
-   # So for all of our widths, we will set the width of the plot and then make
-   # sure that our limits for the colorbar are the min/max across the three plots.
-   # Then we save!  Each saved file will have a descriptive name, so we can tell
-   # them apart.
-   
-   for width, unit in widths:
-       pc.set_width(width,unit)
-       vmin = min([p.norm.vmin for p in pc.plots])
-       vmax = max([p.norm.vmax for p in pc.plots])
-       pc.set_zlim(vmin,vmax)
-       # This is the string formatting we talked about earlier
-       d = {'bn':pf.basename, 'width':width, 'unit':unit}
-       pc.save(fn % d)
-   
-   
-
-.. rubric:: Sample Output
-
-.. image:: _multi_width_save/multi_width_save_RedshiftOutput0005_0000000002_mpc_Slice_x_Density.png
-   :width: 240
-   :target: ../_images/multi_width_save_RedshiftOutput0005_0000000002_mpc_Slice_x_Density.png
-.. image:: _multi_width_save/multi_width_save_RedshiftOutput0005_0000000002_mpc_Slice_y_Density.png
-   :width: 240
-   :target: ../_images/multi_width_save_RedshiftOutput0005_0000000002_mpc_Slice_y_Density.png
-.. image:: _multi_width_save/multi_width_save_RedshiftOutput0005_0000001000_kpc_Slice_x_Density.png
-   :width: 240
-   :target: ../_images/multi_width_save_RedshiftOutput0005_0000001000_kpc_Slice_x_Density.png
-.. image:: _multi_width_save/multi_width_save_RedshiftOutput0005_0000001000_kpc_Slice_y_Density.png
-   :width: 240
-   :target: ../_images/multi_width_save_RedshiftOutput0005_0000001000_kpc_Slice_y_Density.png
-
-

File source/cookbook/offaxis_projection.py

+from yt.mods import *
+
+# Load the dataset.
+pf = load("RedshiftOutput0005")
+
+# Choose a center for the render.
+c = [0.5, 0.5, 0.5]
+
+# Choose a vector representing the viewing direction.
+L = [0.5, 0.2, 0.7]
+
+# Our "width" is the width of the image plane as well as the depth -- so we set
+# it to be 0.8 so we get almost the whole domain.  Note that corners may be
+# visible in the output image!
+W = 0.8
+
+# The number of pixels along one side of the image.
+# The final image will have Npixel^2 pixels.
+Npixels = 512
+
+# Create the off axis projection.
+# Setting no_ghost to False speeds up the process, but makes a
+# slighly lower quality image.
+image = off_axis_projection(pf, c, L, W, N, "Density", no_ghost=False)
+
+# Write out the final image and give it a name
+# relating to what our dataset is called.
+# We save the log of the values so that the colors do not span
+# many orders of magnitude.  Try it without and see what happens.
+write_image(na.log10(image), "%s_offaxis_projection.png" % pf)

File source/cookbook/overplot_particles.inc

-.. _cookbook-overplot_particles:
-
-Overplot particles
-------------------
-
-This is a simple recipe to show how to open a dataset, plot a projection
-through it, and add particles on top.  For more information see
-:ref:`callbacks`.
-
-The latest version of this recipe can be downloaded here: http://hg.yt-project.org/cookbook/raw/tip/recipes/overplot_particles.py .
-
-.. code-block:: python
-
-   from yt.mods import * # set up our namespace
-   
-   fn = "RedshiftOutput0005" # parameter file to load
-   
-   pf = load(fn) # load data
-   pc = PlotCollection(pf, center=[0.5,0.5,0.5]) # defaults to center at most dense point
-   p = pc.add_projection("Density", 0) # 0 = x-axis
-   p.modify["particles"](1.0) # 1.0 is the 'width' we want for our slab of
-                               # particles -- this governs the allowable locations
-                               # of particles that show up on the image
-                               # NOTE: we can also supply a *ptype* to cut based
-                               # on a given (integer) particle type
-   pc.set_width(1.0, '1') # change width of our plot to the full domain
-   pc.save(fn) # save all plots
-   
-
-.. rubric:: Sample Output
-
-.. image:: _overplot_particles/overplot_particles_RedshiftOutput0005_Projection_x_Density.png
-   :width: 240
-   :target: ../_images/overplot_particles_RedshiftOutput0005_Projection_x_Density.png
-
-

File source/cookbook/overplot_particles.py

+from yt.mods import *
+
+# Load the dataset.
+pf = load("RedshiftOutput0005")
+
+# Create a plot collection for the dataset.
+# With no additional arguments, the center will be
+# the densest point in the box.
+pc = PlotCollection(pf)
+
+# Make a density projection.
+p = pc.add_projection("Density", "x")
+
+# Modify the projection
+# The argument specifies the region along the line of sight
+# for which particles will be gathered.
+# 1.0 signifies the entire domain in the line of sight.
+p.modify["particles"](1.0)
+
+# Set the width of the plot to the whole volume.
+pc.set_width(1.0, 'unitary')
+
+# Save the image.
+# Optionally, give a string as an argument
+# to name files with a keyword.
+pc.save()
+

File source/cookbook/simple_volume_rendering.py

 # Load the dataset.
 pf = load("RedshiftOutput0005")
 
-# Create a plot collection for the dataset.
-# With no additional arguments, the center will be
-# the densest point in the box.
-pc = PlotCollection(pf)
-
 # Create a data container (like a sphere or region) that
 # represents the entire domain.
 dd = pf.h.all_data()

File source/cookbook/sum_mass_in_sphere.inc

-.. _cookbook-sum_mass_in_sphere:
-
-Sum mass in sphere
-------------------
-
-This recipe shows how to take a sphere, centered on the most dense point, and
-sum up the total mass in baryons and particles within that sphere.  Note that
-this recipe will take advantage of multiple CPUs if executed with mpirun and
-supplied the --parallel command line argument.  For more information, see
-:ref:`derived-quantities`.
-
-The latest version of this recipe can be downloaded here: http://hg.yt-project.org/cookbook/raw/tip/recipes/sum_mass_in_sphere.py .
-
-.. code-block:: python
-
-   from yt.mods import * # set up our namespace
-   
-   fn = "RedshiftOutput0005" # parameter file to load
-   
-   pf = load(fn) # load data
-   v, c = pf.h.find_max("Density")
-   sp = pf.h.sphere(c, 1.0/pf["mpc"])
-   
-   baryon_mass, particle_mass = sp.quantities["TotalQuantity"](
-           ["CellMassMsun", "ParticleMassMsun"], lazy_reader=True)
-   
-   print "Total mass in sphere is %0.5e (gas = %0.5e / particles = %0.5e)" % \
-               (baryon_mass + particle_mass, baryon_mass, particle_mass)
-   
-
-
-

File source/cookbook/sum_mass_in_sphere.py

+from yt.mods import *
+
+# Load the dataset.
+pf = load("RedshiftOutput0005")
+
+# Create a 1 Mpc radius sphere, centered on the max density.
+sp = pf.h.sphere("max", (1.0, "mpc"))
+
+# Use the TotalQuantity derived quantity to sum up the
+# values of the CellMassMsun and ParticleMassMsun fields
+# within the sphere.
+baryon_mass, particle_mass = sp.quantities["TotalQuantity"](
+        ["CellMassMsun", "ParticleMassMsun"])
+
+print "Total mass in sphere is %0.5e (gas = %0.5e / particles = %0.5e)" % \
+            (baryon_mass + particle_mass, baryon_mass, particle_mass)

File source/cookbook/thin_slice_projection.inc

-.. _cookbook-thin_slice_projection:
-
-Thin slice projection
----------------------
-
-This is a simple recipe to show how to open a dataset and then take a
-weighted-average projection through it, but only through a very thin slice of
-the region.  For more information see :ref:`methods-projections`.
-
-The latest version of this recipe can be downloaded here: http://hg.yt-project.org/cookbook/raw/tip/recipes/thin_slice_projection.py .
-
-.. code-block:: python
-
-   from yt.mods import * # set up our namespace
-   
-   fn = "RedshiftOutput0005" # parameter file to load
-   
-   pf = load(fn) # load data
-   pc = PlotCollection(pf) # defaults to center at most dense point
-   
-   # The region object requires a center, a left edge, and a right edge.  We want
-   # a thin slice and we're projecting along x, so we'll create a region that
-   # fully covers the domain in y and z, but only a portion of it in x.
-   region = pf.h.region([0.3, 0.5, 0.5], [0.1, 0.0, 0.0], [0.5, 1.0, 1.0])
-   
-   pc.add_projection("Density", 0, weight_field="Density", data_source = region) # 0 = x-axis
-   pc.save(fn) # save all plots
-   
-
-.. rubric:: Sample Output
-
-.. image:: _thin_slice_projection/thin_slice_projection_RedshiftOutput0005_Projection_x_Density_Density.png
-   :width: 240
-   :target: ../_images/thin_slice_projection_RedshiftOutput0005_Projection_x_Density_Density.png
-
-

File source/cookbook/thin_slice_projection.py

+from yt.mods import *
+
+# Load the dataset.
+pf = load("RedshiftOutput0005")
+
+# Create a plot collection for the dataset.
+# With no additional arguments, the center will be
+# the densest point in the box.
+pc = PlotCollection(pf)
+
+# Create a region that is a subset of the entire volume.
+center =       [0.3, 0.5, 0.5]
+left_corner =  [0.1, 0.0, 0.0]
+right_corner = [0.5, 1.0, 1.0]
+region = pf.h.region(center, left_corner, right_corner)
+
+# Create a density projection and supply the region we have just created.
+# Only cells within the region will be included in the projection.
+# Try with another data container, like a sphere or disk.
+pc.add_projection("Density", "x", weight_field="Density", data_source=region)
+
+# Save the image with the keyword.
+pc.save("Thin_Slice")

File source/cookbook/time_series_phase.inc

-.. _cookbook-time_series_phase:
-
-Time series phase
------------------
-
-This is a recipe to sit inside a directory and plot a phase diagram for every
-one of the outputs in that directory.
-
-If run with mpirun and the --parallel flag, this will take advantage of
-multiple processors.
-
-The latest version of this recipe can be downloaded here: http://hg.yt-project.org/cookbook/raw/tip/recipes/time_series_phase.py .
-
-.. code-block:: python
-
-   from yt.mods import * # set up our namespace
-   
-   # 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):
-       # We create a plot collection to hold our plot
-       # If we don't specify the center, it will look for one -- but we don't
-       # really care where it's centered for this plot.
-       pc = PlotCollection(pf, center=[0.5, 0.5, 0.5])
-   
-       # Now we add a phase plot of a sphere with radius 1.0 in code units.
-       # If your domain is not 0..1, then this may not cover it completely.
-       p = pc.add_phase_sphere(1.0, '1', ["Density", "Temperature", "CellMassMsun"],
-                           weight=None, lazy_reader=True,
-                           x_bins=128, x_bounds = (1e-32, 1e-24),
-                           y_bins=128, y_bounds = (1e2, 1e7))
-       # We've over-specified things -- but this will help ensure we have constant
-       # bounds.  lazy_reader gives it the go-ahead to run in parallel, and we
-       # have asked for 128 bins from 1e-32 .. 1e-24 in Density-space and 128 bins
-       # between 1e2 and 1e7 in Temperature space.  This will lead to very fine
-       # points of much lower mass, which is okay.  You can reduce the number of
-       # bins to get more mass in each bin.  Additionally, weight=None means that
-       # no averaging is done -- it just gets summed up, so the value of each bin
-       # will be all the mass residing within that bin.
-   
-       # Nowe let's add a title with some fun information.  p is the plot we were
-       # handed previously.  We will add the name of the parameter file and the
-       # current redshift.
-       p.modify["title"]("%s (z = %0.2f)" % (pf, pf["CosmologyCurrentRedshift"]))
-   
-       # Now let's save it out.
-       pc.save()#"%s" % pf)
-   
-
-.. rubric:: Sample Output
-
-.. image:: _time_series_phase/time_series_phase_RedshiftOutput0005_Profile2D_0_Density_Temperature_CellMassMsun.png
-   :width: 240
-   :target: ../_images/time_series_phase_RedshiftOutput0005_Profile2D_0_Density_Temperature_CellMassMsun.png
-
-

File source/cookbook/time_series_phase.py

+from yt.mods import *
+
+# Loop over a list of autodetected datasets.
+for pf in all_pfs(max_depth=2):
+
+    # Create a plot collection at the domain center.
+    pc = PlotCollection(pf, "c")
+
+    # Create a 2D profile of the total mass in bins of
+    # density and temperature.
+    # Setting weight to None will calculate a sum.
+    # Setting weight to a field will calculate an average
+    # weighted by that field.
+    # The radius of the sphere is set to be the size of the domain.
+    # This will make sure the entire volume is included in the calculation.
+    # The first and second arguments are radius and units.  '1' means code units.
+    # We also manually set the number of bins and the range.
+    p = pc.add_phase_sphere(1.0, '1', ["Density", "Temperature", "CellMassMsun"],
+                        weight=None,
+                        x_bins=128, x_bounds = (1e-32, 1e-24),
+                        y_bins=128, y_bounds = (1e2, 1e7))
+
+    # Add a title to the image.
+    p.modify["title"]("%s (z = %0.2f)" % (pf, pf["CosmologyCurrentRedshift"]))
+
+    # Save the image.
+    # Optionally, give a string as an argument
+    # to name files with a keyword.
+    pc.save()

File source/cookbook/time_series_quantity.inc

-.. _cookbook-time_series_quantity:
-
-Time series quantity
---------------------
-
-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.
-
-The latest version of this recipe can be downloaded here: http://hg.yt-project.org/cookbook/raw/tip/recipes/time_series_quantity.py .
-
-.. code-block:: python
-
-   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")
-   
-
-.. rubric:: Sample Output
-
-.. image:: _time_series_quantity/time_series_quantity_average_HII_fraction.png
-   :width: 240
-   :target: ../_images/time_series_quantity_average_HII_fraction.png
-
-

File source/cookbook/time_series_quantity.py

+from yt.mods import *
+
+# Create lists for the times and values.
+times = []
+values = []
+
+# Loop over a list of autodetected datasets.
+for pf in all_pfs(max_depth=2):
+    
+    # Get the current time, convert to years from code units
+    # Add it to the list.
+    times.append(pf["InitialTime"] * pf["years"])
+
+    # Create a container representing the entire domain.
+    data = pf.h.all_data()
+    
+    # Use the WeightedAverageQuantity derived quantity to
+    # calculate the average temperature, weighted by volume.
+    val = data.quantities["WeightedAverageQuantity"](
+            "Temperature", "CellVolume")
+    
+    # Add the value to the list.
+    values.append(val)
+
+# Use matplotlib to plot the values.
+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")

File source/cookbook/velocity_vectors_on_slice.py

+from yt.mods import *
+
+# Load the dataset.
+pf = load(RedshiftOutput0005)
+
+# Create a plot collection for the dataset.
+# With no additional arguments, the center will be
+# the densest point in the box.
+pc = PlotCollection(pf)
+
+# Make a density slice in the x axis.
+p = pc.add_slice("Density", "x")
+
+# Draw a velocity vector every 16 pixels.
+p.modify["velocity"](factor=16)
+
+# Change the width of the image.
+pc.set_width(2.5, 'mpc')
+
+# Save the image.
+# Optionally, give a string as an argument
+# to name files with a keyword.
+pc.save()