[yt-svn] commit/yt: 46 new changesets

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Fri Nov 8 08:33:45 PST 2013


46 new commits in yt:

https://bitbucket.org/yt_analysis/yt/commits/6edcf3419260/
Changeset:   6edcf3419260
Branch:      yt
User:        jzuhone
Date:        2013-08-05 17:50:54
Summary:     First commit of X-Ray photon simulator
Affected #:  1 file

diff -r f936432ed45df5f59f1691ec56f64ee31bb85a46 -r 6edcf3419260ddfe2b921b13bdc3ff63384b6bc3 yt/analysis_modules/synthetic_xray_obs/xray_photon_sim.py
--- /dev/null
+++ b/yt/analysis_modules/synthetic_xray_obs/xray_photon_sim.py
@@ -0,0 +1,665 @@
+import numpy as np
+from yt.funcs import *
+from yt.utilities.physical_constants import mp, clight, cm_per_kpc, cm_per_mpc, \
+     cm_per_km, K_per_keV, erg_per_keV
+from yt.utilities.cosmology import Cosmology
+from yt.utilities.orientation import Orientation
+from yt.data_objects.api import add_field
+import os
+import h5py
+
+try:
+    import pyfits
+except ImportError:
+    try:
+        import astropy.io.fits as pyfits
+    except ImportError:
+        mylog.warning("You don't have pyFITS installed. Writing to FITS files won't be available.")
+    
+try:
+    import xspec
+except ImportError:
+    mylog.warning("You don't have PyXSpec installed. Some models won't be available.")
+
+N_TBIN = 10000
+TMIN = 8.08e-2
+TMAX = 50.
+FOUR_PI = 4.*np.pi
+
+def _emission_measure(field, data):
+    if data.has_field_parameter("X_H"):
+        X_H = data.get_field_parameter("X_H")
+    else:
+        X_H = 0.75
+    return (data["Density"]/mp)*(data["CellMass"]/mp)*0.5*(1.+X_H)*X_H
+
+def _emission_measure_density(field, data):
+    if data.has_field_parameter("X_H"):
+        X_H = data.get_field_parameter("X_H")
+    else:
+        X_H = 0.75            
+    return (data["Density"]/mp)*(data["Density"]/mp)*0.5*(1.+X_H)*X_H
+
+add_field("EmissionMeasure", function=_emission_measure)
+add_field("EMDensity", function=_emission_measure_density)
+
+class PhotonModel(object):
+
+    def __init__(self, emin, emax, nchan):
+        self.emin = emin
+        self.emax = emax
+        self.nchan = nchan
+        self.ebins = np.linspace(emin, emax, nchan+1)
+
+    def prepare(self):
+        pass
+    
+    def get_spectrum(self):
+        pass
+    
+class XSpecThermalModel(PhotonModel):
+    
+    def __init__(self, model_name, emin, emax, nchan):
+        self.model_name = model_name
+        PhotonModel.__init__(self, emin, emax, nchan)
+                
+    def prepare(self):
+        xspec.Xset.chatter = 0
+        xspec.AllModels.setEnergies("%f %f %d lin" % (self.emin, self.emax, self.nchan))
+        self.model = xspec.Model(self.model_name)
+        
+    def get_spectrum(self, kT, Zmet):
+
+        m = getattr(self.model,self.model_name)
+        m.kT = kT 
+        m.Abundanc = Zmet
+        m.norm = 1.0
+        m.Redshift = 0.0        
+        return 1.0e-14*np.array(self.model.values(0))
+
+class XSpecAbsorbModel(PhotonModel):
+
+     def __init__(self, model_name, nH, emin=0.01, emax=50.0, nchan=100000):
+         self.model_name = model_name
+         self.nH = nH
+         PhotonModel.__init__(self, emin, emax, nchan)
+         
+     def prepare(self):
+         xspec.Xset.chatter = 0
+         xspec.AllModels.setEnergies("%f %f %d lin" % (self.emin, self.emax, self.nchan))
+         self.model = xspec.Model(self.model_name+"*powerlaw")
+         self.model.powerlaw.norm = self.nchan/(self.emax-self.emin)
+         self.model.powerlaw.PhoIndex = 0.0
+                                                       
+     def get_spectrum(self):
+         m = getattr(self.model,self.model_name)
+         m.nH = self.nH
+         return np.array(self.model.values(0))
+    
+class ApecTableModel(PhotonModel):
+
+    def __init__(self, filename):
+        if not os.path.exists(filename):
+            raise IOError("File does not exist: %s." % filename)
+        self.filename = filename
+        f = h5py.File(self.filename,"r")
+        self.T_vals = f["kT"][:]
+        self.Z_vals = f["Zmet"][:]
+        emin        = f["emin"].value
+        emax        = f["emax"].value
+        self.spec_table = f["spectrum"][:,:,:]
+        nchan = self.spec_table.shape[-1]
+        f.close()
+        self.dT = self.T_vals[1]-self.T_vals[0]
+        self.dZ = self.Z_vals[1]-self.Z_vals[0]
+        PhotonModel.__init__(self, emin, emax, nchan)
+                
+    def prepare(self):
+        pass
+    
+    def get_spectrum(self, kT, Zmet):
+        iz = np.searchsorted(self.Z_vals, Zmet)-1
+        dz = (Zmet-self.Z_vals[iz])/self.dZ
+        it = np.searchsorted(self.T_vals, kT)-1
+        dt = (kT-self.T_vals[it])/self.dT
+        spec = self.spec_table[it+1,iz+1,:]*dt*dz + \
+               self.spec_table[it,iz,:]*(1.-dt)*(1.-dz) + \
+               self.spec_table[it,iz+1,:]*(1.-dt)*dz + \
+               self.spec_table[it+1,iz,:]*dt*(1.-dz)
+        return spec
+
+class AbsorbTableModel(PhotonModel):
+
+    def __init__(self, filename):
+        if not os.path.exists(filename):
+            raise IOError("File does not exist: %s." % filename)
+        self.filename = filename
+        f = h5py.File(self.filename,"r")
+        emin = f["emin"].value
+        emax = f["emax"].value
+        self.abs = f["spectrum"][:]
+        nchan = self.abs.shape[0]
+        f.close()
+        PhotonModel.__init__(self, emin, emax, nchan)
+        
+    def prepare(self):
+        pass
+
+    def get_spectrum(self):
+        return self.abs ** nH
+            
+class XRayPhotonList(object):
+
+    def __init__(self, photons = None):
+        if photons is None: photons = {}
+        self.photons = photons
+        
+    @classmethod
+    def from_file(cls, filename):
+        """
+        Initialize a XRayPhotonList from an HDF5 file given by filename.
+        """
+        photons = {}
+        
+        f = h5py.File(filename, "r")
+
+        photons["FiducialExposureTime"] = f["/fid_exp_time"].value
+        photons["FiducialArea"] = f["/fid_area"].value
+        photons["Redshift"] = f["/redshift"].value
+        photons["Hubble0"] = f["/hubble"].value
+        photons["OmegaMatter"] = f["/omega_matter"].value
+        photons["OmegaLambda"] = f["/omega_lambda"].value                    
+        photons["AngularDiameterDistance"] = f["/d_a"].value
+                        
+        photons["x"] = f["/x"][:]
+        photons["y"] = f["/y"][:]
+        photons["z"] = f["/z"][:]
+        photons["dx"] = f["/dx"][:]
+        photons["vx"] = f["/vx"][:]
+        photons["vy"] = f["/vy"][:]
+        photons["vz"] = f["/vz"][:]
+        photons["NumberOfPhotons"] = f["/num_photons"][:].astype("uint64")
+
+        photons["Energy"] = f["/energy"][:]
+        
+        f.close()
+        
+        return cls(photons)
+
+    @classmethod
+    def from_scratch(cls, cell_data, redshift, eff_A, exp_time, emission_model,
+                     ctr="c", X_H=0.75, Zmet=0.3, cosmology=None):
+        """
+        Initialize a XRayPhotonList from a data container. 
+        """
+        pf = cell_data.pf
+
+        vol_scale = pf.units["cm"]**(-3)/np.prod(pf.domain_width)
+
+        if cosmology is None:
+            cosmo = Cosmology(HubbleConstantNow=71., OmegaMatterNow=0.27,
+                              OmegaLambdaNow=0.73)
+        else:
+            cosmo = cosmology
+            
+        D_A = cosmo.AngularDiameterDistance(0.0,redshift)*cm_per_mpc
+        cosmo_fac = 1.0/(FOUR_PI*D_A*D_A*(1.+redshift)**3)
+
+        idxs = np.argsort(cell_data["Temperature"])
+        dshape = idxs.shape
+
+        cell_data.set_field_parameter("X_H", X_H)
+
+        if ctr == "c":
+            src_ctr = pf.domain_center
+        elif ctr == "max":
+            src_ctr = pf.h.find_max("Density")[-1]
+        elif iterable(ctr):
+            src_ctr = ctr
+                    
+        kT_bins = np.linspace(TMIN, max(cell_data["TempkeV"][idxs][-1],
+                                        TMAX), num=N_TBIN+1)
+        dkT = kT_bins[1]-kT_bins[0]
+        kT_idxs = np.digitize(cell_data["TempkeV"][idxs], kT_bins)
+        kT_idxs = np.minimum(np.maximum(1, kT_idxs), N_TBIN) - 1
+        bcounts = np.bincount(kT_idxs).astype("int")
+        bcounts = bcounts[bcounts > 0]
+        kT_idxs = np.unique(kT_idxs)
+
+        emission_model.prepare()
+        energy = emission_model.ebins
+        de = energy[1]-energy[0]
+        emid = 0.5*(energy[1:]+energy[:-1])
+
+        cell_em = cell_data["EmissionMeasure"][idxs]*vol_scale
+        cell_vol = cell_data["CellVolume"][idxs]*vol_scale
+        cell_emd = cell_data["EMDensity"][idxs]
+
+        energies = []
+        number_of_photons = np.zeros(dshape, dtype='uint64')
+
+        pbar = get_pbar("Generating Photons", dshape[0])
+        n = int(0)
+            
+        for i,ikT in enumerate(kT_idxs):
+            
+            ncells = int(bcounts[i])
+            
+            kT = kT_bins[ikT] + 0.5*dkT
+            
+            em_sum = cell_em[n:n+ncells].sum()
+            vol_sum = cell_vol[n:n+ncells].sum()
+            em_avg = em_sum / vol_sum
+            
+            tot_norm = cosmo_fac*em_sum / vol_scale
+            
+            spec = emission_model.get_spectrum(kT, Zmet)
+            spec *= tot_norm
+            cumspec = np.cumsum(spec)
+            counts = cumspec[:]/cumspec[-1]
+            tot_ph = cumspec[-1]*eff_A*exp_time
+
+            for icell in xrange(n,n+ncells):
+
+                cell_norm = tot_ph * (cell_emd[icell]/em_avg) * (cell_vol[icell]/vol_sum)                    
+                cell_Nph = int(cell_norm) + int(np.modf(cell_norm)[0] >= np.random.random())
+                
+                if cell_Nph > 0:
+                    number_of_photons[icell] = cell_Nph                    
+                    randvec = np.random.uniform(low=counts[0], high=counts[-1], size=cell_Nph)
+                    randvec.sort()
+                    eidxs = np.searchsorted(counts, randvec)-1
+                    cell_e = emid[eidxs]+de*(randvec-counts[eidxs])/(counts[eidxs+1]-counts[eidxs])
+                    energies.append(cell_e)
+
+                pbar.update(icell)
+                
+            n += ncells
+            
+        pbar.finish()
+                        
+        active_cells = np.where(number_of_photons > 0)[0]
+        num_active_cells = len(active_cells)
+
+        photons = {}
+        photons["x"] = (cell_data["x"][idxs][active_cells]-src_ctr[0])*pf.units["kpc"]
+        photons["y"] = (cell_data["y"][idxs][active_cells]-src_ctr[1])*pf.units["kpc"]
+        photons["z"] = (cell_data["z"][idxs][active_cells]-src_ctr[2])*pf.units["kpc"]
+        photons["vx"] = cell_data["x-velocity"][idxs][active_cells]/cm_per_km
+        photons["vy"] = cell_data["y-velocity"][idxs][active_cells]/cm_per_km
+        photons["vz"] = cell_data["z-velocity"][idxs][active_cells]/cm_per_km
+        photons["dx"] = cell_data["dx"][idxs][active_cells]*pf.units["kpc"]
+        photons["NumberOfPhotons"] = number_of_photons[active_cells]
+        photons["Energy"] = np.concatenate(energies)
+
+        photons["FiducialExposureTime"] = exp_time
+        photons["FiducialArea"] = eff_A
+        photons["Redshift"] = redshift
+        photons["Hubble0"] = cosmo.HubbleConstantNow
+        photons["OmegaMatter"] = cosmo.OmegaMatterNow
+        photons["OmegaLambda"] = cosmo.OmegaLambdaNow
+        photons["AngularDiameterDistance"] = D_A
+        
+        return cls(photons)
+            
+    def write_h5_file(self, photonfile):
+
+        f = h5py.File(photonfile, "w")
+
+        # Scalars
+       
+        f.create_dataset("fid_area", data=self.photons["FiducialArea"])
+        f.create_dataset("fid_exp_time", data=self.photons["FiducialExposureTime"])
+        f.create_dataset("redshift", data=self.photons["Redshift"])
+        f.create_dataset("omega_matter", data=self.photons["OmegaMatter"])
+        f.create_dataset("omega_lambda", data=self.photons["OmegaLambda"])
+        f.create_dataset("hubble", data=self.photons["Hubble0"])
+        f.create_dataset("d_a", data=self.photons["AngularDiameterDistance"])
+        
+        # Arrays
+
+        f.create_dataset("x", data=self.photons["x"])
+        f.create_dataset("y", data=self.photons["y"])
+        f.create_dataset("z", data=self.photons["z"])
+        f.create_dataset("vx", data=self.photons["vx"])
+        f.create_dataset("vy", data=self.photons["vy"])
+        f.create_dataset("vz", data=self.photons["vz"])
+        f.create_dataset("dx", data=self.photons["dx"])
+        f.create_dataset("num_photons", data=self.photons["NumberOfPhotons"])
+        f.create_dataset("energy", data=self.photons["Energy"])
+                
+        f.close()
+    
+    def project_photons(self, L, area_new=None, texp_new=None, 
+                        absorb_model=None):
+        """
+        Project photons 
+        """
+        dx = self.photons["dx"]
+        
+        L /= np.sqrt(np.dot(L, L))
+        vecs = np.identity(3)
+        t = np.cross(L, vecs).sum(axis=1)
+        ax = t.argmax()
+        north = np.cross(L, vecs[ax,:]).ravel()
+        orient = Orientation(L, north_vector=north)
+        
+        x_hat = orient.unit_vectors[0]
+        y_hat = orient.unit_vectors[1]
+        z_hat = orient.unit_vectors[2]
+
+        n_ph = self.photons["NumberOfPhotons"]
+        num_cells = len(n_ph)
+        n_ph_tot = n_ph.sum()
+        
+        if texp_new is None and area_new is None:
+            n_obs = n_ph
+        else:
+            if texp_new is None:
+                Tratio = 1.
+            else:
+                Tratio = texp_new/self.photons["FiducialExposureTime"]
+            if area_new is None:
+                Aratio = 1.
+            else:
+                Aratio = area_new/self.photons["FiducialArea"]
+            fak = Aratio*Tratio
+            if fak > 1:
+                raise ValueError("Spectrum scaling factor = %g, cannot be greater than unity." % (fak))
+            n_obs = np.uint64(n_ph*fak)
+            
+        n_obs_tot = n_obs.sum()
+
+        mylog.info("Total number of observed photons (without absoprtion): %d" % (n_obs_tot))
+        
+        x = np.random.uniform(low=-0.5,high=0.5,size=n_obs_tot)
+        y = np.random.uniform(low=-0.5,high=0.5,size=n_obs_tot)
+        z = np.random.uniform(low=-0.5,high=0.5,size=n_obs_tot)
+
+        vz = self.photons["vx"]*z_hat[0] + \
+             self.photons["vy"]*z_hat[1] + \
+             self.photons["vz"]*z_hat[2]
+        shift = -vz * cm_per_km / clight
+        shift = np.sqrt((1.-shift)/(1.+shift))
+        
+        cells = np.concatenate([i*np.ones((n_ph[i]),dtype='uint64') for i in xrange(num_cells)])
+        if n_obs_tot == n_ph_tot:
+            idxs = np.arange(n_ph_tot,dtype='uint64')
+            obs_cells = cells
+        else:
+            idxs = np.random.choice(n_ph_tot, size=n_obs_tot, replace=False)
+            obs_cells = cells[idxs]
+        x *= dx[obs_cells]
+        y *= dx[obs_cells]
+        z *= dx[obs_cells]
+        x += self.photons["x"][obs_cells]
+        y += self.photons["y"][obs_cells]
+        z += self.photons["z"][obs_cells]  
+        eobs = self.photons["Energy"][idxs]*shift[obs_cells]
+        
+        xsky = x*x_hat[0] + y*x_hat[1] + z*x_hat[2]
+        ysky = x*y_hat[0] + y*y_hat[1] + z*y_hat[2]
+        eobs /= (1.+self.photons["Redshift"])
+                 
+        if absorb_model is None:
+            not_abs = np.ones(eobs.shape, dtype='bool')
+        else:
+            mylog.info("Absorbing.")
+            absorb_model.prepare()
+            energy = absorb_model.ebins
+            de = energy[1]-energy[0]
+            emid = 0.5*(energy[1:]+energy[:-1])
+            aspec = absorb_model.get_spectrum()
+            aspec_max = aspec.max()
+            eidxs = np.searchsorted(emid, eobs)-1
+            dx = (eobs-emid[eidxs])/de
+            absorb = aspec[eidxs]*(1.-dx) + aspec[eidxs+1]*dx
+            randvec = aspec_max*np.random.random(eobs.shape)
+            not_abs = np.where(randvec < absorb)[0]
+                        
+        D_A = self.photons["AngularDiameterDistance"] / cm_per_kpc
+
+        events = {}
+
+        events["xsky"] = np.rad2deg(xsky[not_abs]/D_A)*3600.
+        events["ysky"] = np.rad2deg(ysky[not_abs]/D_A)*3600.
+        events["eobs"] = eobs[not_abs]
+
+        if texp_new is None:
+            events["ExposureTime"] = self.photons["FiducialExposureTime"]
+        else:
+            events["ExposureTime"] = texp_new
+        if area_new is None:
+            events["Area"] = self.photons["FiducialArea"]
+        else:
+            events["Area"] = area_new
+        events["Hubble0"] = self.photons["Hubble0"]
+        events["Redshift"] = self.photons["Redshift"]
+        events["OmegaMatter"] = self.photons["OmegaMatter"]
+        events["OmegaLambda"] = self.photons["OmegaLambda"] 
+        
+        return XRayEventList(events)
+
+class XRayEventList(object) :
+
+    def __init__(self, events = None) :
+
+        if events is None : events = {}
+        self.events = events
+        self.num_events = events["xsky"].shape[0]
+        
+    @classmethod
+    def from_h5_file(cls, h5file) :
+        """
+        Initialize a XRayEventList from a HDF5 file with filename h5file.
+        """
+        events = {}
+        
+        f = h5py.File(h5file, "r")
+
+        events["ExposureTime"] = f["/exp_time"].value
+        events["Area"] = f["/area"].value
+        events["Hubble0"] = f["/hubble"].value
+        events["Redshift"] = f["/redshift"].value
+        events["OmegaMatter"] = f["/omega_matter"].value
+        events["OmegaLambda"] = f["/omega_lambda"].value
+        
+        events["xsky"] = f["/xsky"][:]
+        events["ysky"] = f["/ysky"][:]
+        events["eobs"] = f["/eobs"][:]
+        
+        f.close()
+        
+        return cls(events)
+
+    @classmethod
+    def from_fits_file(cls, fitsfile) :
+        """
+        Initialize a XRayEventList from a FITS file with filename fitsfile.
+        """
+        hdulist = pyfits.open(fitsfile)
+
+        tblhdu = hdulist[1]
+
+        events = {}
+        
+        events["ExposureTime"] = tblhdu.header["EXPOSURE"]
+        events["Area"] = tblhdu.header["AREA"]
+        events["Hubble0"] = tblhdu.header["HUBBLE"]
+        events["Redshift"] = tblhdu.header["REDSHIFT"]
+        events["OmegaMatter"] = tblhdu.header["OMEGA_M"]
+        events["OmegaLambda"] = tblhdu.header["OMEGA_L"]
+
+        events["xsky"] = tblhdu.data.field("POS_X")
+        events["ysky"] = tblhdu.data.field("POS_Y")
+        events["eobs"] = tblhdu.data.field("ENERGY")
+        
+        return cls(events)
+
+    def write_fits_file(self, fitsfile, clobber=False):
+        """
+        Write events to a FITS binary table file.
+        """
+        col1 = pyfits.Column(name='ENERGY', format='E',
+                             array=self.events["eobs"])
+        col2 = pyfits.Column(name='XSKY', format='D',
+                             array=self.events["xsky"])
+        col3 = pyfits.Column(name='YSKY', format='D',
+                             array=self.events["ysky"])
+        
+        coldefs = pyfits.ColDefs([col1, col2, col3])
+        
+        tbhdu = pyfits.new_table(coldefs)
+
+        tbhdu.header.update("EXPOSURE", self.events["ExposureTime"])
+        tbhdu.header.update("AREA", self.events["Area"])
+        tbhdu.header.update("HUBBLE", self.events["Hubble0"])
+        tbhdu.header.update("OMEGA_M", self.events["OmegaMatter"])
+        tbhdu.header.update("OMEGA_L", self.events["OmegaLambda"])
+        
+        tbhdu.writeto(fitsfile, clobber=clobber)
+                
+    def write_simput_file(self, prefix, clobber=False, e_min=None, e_max=None) :
+
+        if e_min is None:
+            e_min = 0.0
+        if e_max is None:
+            e_max = 100.0
+
+        idxs = np.logical_and(self.events["eobs"] >= e_min, self.events["eobs"] <= e_max)
+        flux = erg_per_keV*np.sum(self.events["eobs"][idxs])/self.events["ExposureTime"]/self.events["Area"]
+        
+        col1 = pyfits.Column(name='ENERGY', format='E',
+                             array=self.events["eobs"])
+        col2 = pyfits.Column(name='DEC', format='D',
+                             array=self.events["ysky"]/3600.)
+        col3 = pyfits.Column(name='RA', format='D',
+                             array=self.events["xsky"]/3600.)
+
+        coldefs = pyfits.ColDefs([col1, col2, col3])
+
+        tbhdu = pyfits.new_table(coldefs)
+        tbhdu.update_ext_name("PHLIST")
+
+        tbhdu.header.update("HDUCLASS", "HEASARC/SIMPUT")
+        tbhdu.header.update("HDUCLAS1", "PHOTONS")
+        tbhdu.header.update("HDUVERS", "1.1.0")
+        tbhdu.header.update("EXTVER", 1)
+        tbhdu.header.update("REFRA", 0.0)
+        tbhdu.header.update("REFDEC", 0.0)
+        tbhdu.header.update("TUNIT1", "keV")
+        tbhdu.header.update("TUNIT2", "deg")
+        tbhdu.header.update("TUNIT3", "deg")                
+
+        phfile = prefix+"_phlist.fits"
+
+        tbhdu.writeto(phfile, clobber=clobber)
+
+        col1 = pyfits.Column(name='SRC_ID', format='J', array=np.array([1]).astype("int32"))
+        col2 = pyfits.Column(name='RA', format='D', array=np.array([0.0]))
+        col3 = pyfits.Column(name='DEC', format='D', array=np.array([0.0]))
+        col4 = pyfits.Column(name='E_MIN', format='D', array=np.array([e_min]))
+        col5 = pyfits.Column(name='E_MAX', format='D', array=np.array([e_max]))
+        col6 = pyfits.Column(name='FLUX', format='D', array=np.array([flux]))
+        col7 = pyfits.Column(name='SPECTRUM', format='80A', array=np.array([phfile+"[PHLIST,1]"]))
+        col8 = pyfits.Column(name='IMAGE', format='80A', array=np.array([phfile+"[PHLIST,1]"]))
+                        
+        coldefs = pyfits.ColDefs([col1, col2, col3, col4, col5, col6, col7, col8])
+        
+        wrhdu = pyfits.new_table(coldefs)
+        wrhdu.update_ext_name("SRC_CAT")
+                                
+        wrhdu.header.update("HDUCLASS", "HEASARC")
+        wrhdu.header.update("HDUCLAS1", "SIMPUT")
+        wrhdu.header.update("HDUCLAS2", "SRC_CAT")        
+        wrhdu.header.update("HDUVERS", "1.1.0")
+        wrhdu.header.update("RADECSYS", "FK5")
+        wrhdu.header.update("EQUINOX", 2000.0)
+        wrhdu.header.update("TUNIT2", "deg")
+        wrhdu.header.update("TUNIT3", "deg")
+        wrhdu.header.update("TUNIT4", "keV")
+        wrhdu.header.update("TUNIT5", "keV")
+        wrhdu.header.update("TUNIT6", "erg/s/cm**2")
+
+        simputfile = prefix+"_simput.fits"
+                
+        wrhdu.writeto(simputfile, clobber=clobber)
+
+    def write_h5_file(self, h5file) :
+        """
+        Write a XRayEventList to the HDF5 file given by h5file.
+        """
+        f = h5py.File(h5file, "w")
+
+        f.create_dataset("/exp_time", data=self.events["ExposureTime"])
+        f.create_dataset("/area", data=self.events["Area"])
+        f.create_dataset("/redshift", data=self.events["Redshift"])
+        f.create_dataset("/hubble", data=self.events["Hubble0"])
+        f.create_dataset("/omega_matter", data=self.events["OmegaMatter"])
+        f.create_dataset("/omega_lambda", data=self.events["OmegaLambda"])        
+        f.create_dataset("/xsky", data=self.events["xsky"])
+        f.create_dataset("/ysky", data=self.events["ysky"])
+        f.create_dataset("/eobs", data=self.events["eobs"])
+                        
+        f.close()
+
+    def write_fits_image(self, imagefile, width, nx, clobber=False, gzip_file=False,
+                         emin=None, emax=None) :
+        
+        if emin is None:
+            mask_emin = np.ones((self.num_events), dtype='bool')
+        else :
+            mask_emin = self.events["eobs"] > emin
+        if emax is None:
+            mask_emax = np.ones((self.num_events), dtype='bool')
+        else :
+            mask_emax = self.events["eobs"] < emax
+
+        mask = np.logical_and(mask_emin, mask_emax)
+
+        dx_pixel = width/nx
+        xmin = -0.5*nx*dx_pixel
+        xmax = 0.5*nx*dx_pixel
+        xbins = np.linspace(xmin, xmax, 2*nx+1, endpoint=True)
+        
+        H, xedges, yedges = np.histogram2d(self.events["xsky"][mask],
+                                           self.events["ysky"][mask],
+                                           bins=[xbins,xbins])
+        
+        hdu = pyfits.PrimaryHDU(H.T)
+
+        hdu.header.update('WCSNAMEP', "PHYSICAL")
+        hdu.header.update("MTYPE1", "SKY")
+        hdu.header.update("MFORM1", "X,Y")
+        hdu.header.update("CTYPE1P", "LINEAR")
+        hdu.header.update("CTYPE2P", "LINEAR")
+        hdu.header.update("CRPIX1P", 0.5)
+        hdu.header.update("CRPIX2P", 0.5)
+        hdu.header.update("CRVAL1P", xmin)
+        hdu.header.update("CRVAL2P", xmin)
+        hdu.header.update("CDELT1P", dx_pixel)
+        hdu.header.update("CDELT2P", dx_pixel)
+        hdu.header.update("EXPOSURE", self.events["ExposureTime"])
+        
+        hdu.writeto(imagefile, clobber=clobber)
+
+        if (gzip_file) :
+            clob = ""
+            if (clobber) : clob="-f"
+            os.system("gzip "+clob+" %s.fits" % (prefix))
+                                    
+    def write_spectrum(self, specfile, emin, emax, nchan, clobber=False) :
+        
+        spec, ee = np.histogram(self.events["eobs"], bins=nchan, range=(emin, emax))
+
+        de = ee[1]-ee[0]
+        emid = 0.5*(ee[1:]+ee[:-1])
+        
+        col1 = pyfits.Column(name='ENERGY', format='1E', array=emid)
+        col2 = pyfits.Column(name='COUNTS', format='1E', array=spec)
+        
+        coldefs = pyfits.ColDefs([col1, col2])
+        
+        tbhdu = pyfits.new_table(coldefs)
+        
+        tbhdu.writeto(specfile, clobber=clobber)


https://bitbucket.org/yt_analysis/yt/commits/db7d37a85891/
Changeset:   db7d37a85891
Branch:      yt
User:        jzuhone
Date:        2013-08-06 04:13:15
Summary:     1) Cosmetic changes.
2) Option for spreading out photons throughout the cell using a Gaussian.
3) WCS coordinates for FITS images.
Affected #:  2 files

diff -r 6edcf3419260ddfe2b921b13bdc3ff63384b6bc3 -r db7d37a858913daaa99045bf1ae97a5b51236ae8 yt/analysis_modules/api.py
--- a/yt/analysis_modules/api.py
+++ b/yt/analysis_modules/api.py
@@ -120,3 +120,9 @@
 
 from .radmc3d_export.api import \
     RadMC3DWriter
+
+from .synthetic_xray_obs.api import \
+     XRayPhotonList, \
+     XRayEventList, \
+     XSpecThermalModel, \
+     XSpecAbsorbModel

diff -r 6edcf3419260ddfe2b921b13bdc3ff63384b6bc3 -r db7d37a858913daaa99045bf1ae97a5b51236ae8 yt/analysis_modules/synthetic_xray_obs/xray_photon_sim.py
--- a/yt/analysis_modules/synthetic_xray_obs/xray_photon_sim.py
+++ b/yt/analysis_modules/synthetic_xray_obs/xray_photon_sim.py
@@ -96,7 +96,7 @@
          m.nH = self.nH
          return np.array(self.model.values(0))
     
-class ApecTableModel(PhotonModel):
+class TableApecModel(PhotonModel):
 
     def __init__(self, filename):
         if not os.path.exists(filename):
@@ -128,7 +128,7 @@
                self.spec_table[it+1,iz,:]*dt*(1.-dz)
         return spec
 
-class AbsorbTableModel(PhotonModel):
+class TableAbsorbModel(PhotonModel):
 
     def __init__(self, filename):
         if not os.path.exists(filename):
@@ -188,7 +188,7 @@
 
     @classmethod
     def from_scratch(cls, cell_data, redshift, eff_A, exp_time, emission_model,
-                     ctr="c", X_H=0.75, Zmet=0.3, cosmology=None):
+                     center="c", X_H=0.75, Zmet=0.3, cosmology=None):
         """
         Initialize a XRayPhotonList from a data container. 
         """
@@ -210,12 +210,12 @@
 
         cell_data.set_field_parameter("X_H", X_H)
 
-        if ctr == "c":
+        if center == "c":
             src_ctr = pf.domain_center
-        elif ctr == "max":
+        elif center == "max":
             src_ctr = pf.h.find_max("Density")[-1]
-        elif iterable(ctr):
-            src_ctr = ctr
+        elif iterable(center):
+            src_ctr = center
                     
         kT_bins = np.linspace(TMIN, max(cell_data["TempkeV"][idxs][-1],
                                         TMAX), num=N_TBIN+1)
@@ -331,7 +331,7 @@
         f.close()
     
     def project_photons(self, L, area_new=None, texp_new=None, 
-                        absorb_model=None):
+                        absorb_model=None, smoothing=False):
         """
         Project photons 
         """
@@ -370,11 +370,16 @@
             
         n_obs_tot = n_obs.sum()
 
-        mylog.info("Total number of observed photons (without absoprtion): %d" % (n_obs_tot))
-        
-        x = np.random.uniform(low=-0.5,high=0.5,size=n_obs_tot)
-        y = np.random.uniform(low=-0.5,high=0.5,size=n_obs_tot)
-        z = np.random.uniform(low=-0.5,high=0.5,size=n_obs_tot)
+        mylog.info("Total number of observed photons (without absorption): %d" % (n_obs_tot))
+
+        if smoothing:
+            x = np.random.normal(scale=0.5,size=n_obs_tot)
+            y = np.random.normal(scale=0.5,size=n_obs_tot)
+            z = np.random.normal(scale=0.5,size=n_obs_tot)
+        else:
+            x = np.random.uniform(low=-0.5,high=0.5,size=n_obs_tot)
+            y = np.random.uniform(low=-0.5,high=0.5,size=n_obs_tot)
+            z = np.random.uniform(low=-0.5,high=0.5,size=n_obs_tot)
 
         vz = self.photons["vx"]*z_hat[0] + \
              self.photons["vy"]*z_hat[1] + \
@@ -449,7 +454,7 @@
         self.num_events = events["xsky"].shape[0]
         
     @classmethod
-    def from_h5_file(cls, h5file) :
+    def from_h5_file(cls, h5file):
         """
         Initialize a XRayEventList from a HDF5 file with filename h5file.
         """
@@ -473,7 +478,7 @@
         return cls(events)
 
     @classmethod
-    def from_fits_file(cls, fitsfile) :
+    def from_fits_file(cls, fitsfile):
         """
         Initialize a XRayEventList from a FITS file with filename fitsfile.
         """
@@ -519,7 +524,7 @@
         
         tbhdu.writeto(fitsfile, clobber=clobber)
                 
-    def write_simput_file(self, prefix, clobber=False, e_min=None, e_max=None) :
+    def write_simput_file(self, prefix, clobber=False, e_min=None, e_max=None):
 
         if e_min is None:
             e_min = 0.0
@@ -585,7 +590,7 @@
                 
         wrhdu.writeto(simputfile, clobber=clobber)
 
-    def write_h5_file(self, h5file) :
+    def write_h5_file(self, h5file):
         """
         Write a XRayEventList to the HDF5 file given by h5file.
         """
@@ -603,24 +608,25 @@
                         
         f.close()
 
-    def write_fits_image(self, imagefile, width, nx, clobber=False, gzip_file=False,
-                         emin=None, emax=None) :
+    def write_fits_image(self, imagefile, width, nx, center,
+                         clobber=False, gzip_file=False,
+                         emin=None, emax=None):
         
         if emin is None:
             mask_emin = np.ones((self.num_events), dtype='bool')
-        else :
+        else:
             mask_emin = self.events["eobs"] > emin
         if emax is None:
             mask_emax = np.ones((self.num_events), dtype='bool')
-        else :
+        else:
             mask_emax = self.events["eobs"] < emax
 
         mask = np.logical_and(mask_emin, mask_emax)
 
         dx_pixel = width/nx
-        xmin = -0.5*nx*dx_pixel
-        xmax = 0.5*nx*dx_pixel
-        xbins = np.linspace(xmin, xmax, 2*nx+1, endpoint=True)
+        xmin = -0.5*width
+        xmax = -xmin
+        xbins = np.linspace(xmin, xmax, nx+1, endpoint=True)
         
         H, xedges, yedges = np.histogram2d(self.events["xsky"][mask],
                                            self.events["ysky"][mask],
@@ -628,27 +634,28 @@
         
         hdu = pyfits.PrimaryHDU(H.T)
 
-        hdu.header.update('WCSNAMEP', "PHYSICAL")
-        hdu.header.update("MTYPE1", "SKY")
-        hdu.header.update("MFORM1", "X,Y")
-        hdu.header.update("CTYPE1P", "LINEAR")
-        hdu.header.update("CTYPE2P", "LINEAR")
-        hdu.header.update("CRPIX1P", 0.5)
-        hdu.header.update("CRPIX2P", 0.5)
-        hdu.header.update("CRVAL1P", xmin)
-        hdu.header.update("CRVAL2P", xmin)
-        hdu.header.update("CDELT1P", dx_pixel)
-        hdu.header.update("CDELT2P", dx_pixel)
+        hdu.header.update("MTYPE1", "EQPOS")
+        hdu.header.update("MFORM1", "RA,DEC")
+        hdu.header.update("CTYPE1", "RA---TAN")
+        hdu.header.update("CTYPE2", "DEC--TAN")
+        hdu.header.update("CRPIX1", 0.5*(nx+1))
+        hdu.header.update("CRPIX2", 0.5*(nx+1))                
+        hdu.header.update("CRVAL1", center[0])
+        hdu.header.update("CRVAL2", center[1])
+        hdu.header.update("CUNIT1", "deg")
+        hdu.header.update("CUNIT2", "deg")
+        hdu.header.update("CDELT1", -dx_pixel/3600.)
+        hdu.header.update("CDELT2", dx_pixel/3600.)
         hdu.header.update("EXPOSURE", self.events["ExposureTime"])
         
         hdu.writeto(imagefile, clobber=clobber)
 
-        if (gzip_file) :
+        if (gzip_file):
             clob = ""
             if (clobber) : clob="-f"
             os.system("gzip "+clob+" %s.fits" % (prefix))
                                     
-    def write_spectrum(self, specfile, emin, emax, nchan, clobber=False) :
+    def write_spectrum(self, specfile, emin, emax, nchan, clobber=False):
         
         spec, ee = np.histogram(self.events["eobs"], bins=nchan, range=(emin, emax))
 


https://bitbucket.org/yt_analysis/yt/commits/018276404e26/
Changeset:   018276404e26
Branch:      yt
User:        jzuhone
Date:        2013-08-09 16:15:12
Summary:     Needed hooks for the API
Affected #:  3 files

diff -r db7d37a858913daaa99045bf1ae97a5b51236ae8 -r 018276404e26f8ca54f8af2722d3afed30cf05bc yt/analysis_modules/api.py
--- a/yt/analysis_modules/api.py
+++ b/yt/analysis_modules/api.py
@@ -125,4 +125,7 @@
      XRayPhotonList, \
      XRayEventList, \
      XSpecThermalModel, \
-     XSpecAbsorbModel
+     XSpecAbsorbModel, \
+     TableApecModel, \
+     TableAbsorbModel
+     

diff -r db7d37a858913daaa99045bf1ae97a5b51236ae8 -r 018276404e26f8ca54f8af2722d3afed30cf05bc yt/analysis_modules/synthetic_xray_obs/api.py
--- /dev/null
+++ b/yt/analysis_modules/synthetic_xray_obs/api.py
@@ -0,0 +1,35 @@
+"""
+API for spectral_integrator
+
+Author: John ZuHone <jzuhone at gmail.com>
+Affiliation: NASA/GSFC
+Homepage: http://yt-project.org/
+License:
+  Copyright (C) 2010-2011 Matthew Turk.  All Rights Reserved.
+
+  This file is part of yt.
+
+  yt is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 3 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+"""
+
+from .photon_simulator import \
+     XRayPhotonList, \
+     XRayEventList
+
+from .photon_models import \
+     XSpecThermalModel, \
+     XSpecAbsorbModel, \
+     TableApecModel, \
+     TableAbsorbModel


https://bitbucket.org/yt_analysis/yt/commits/63429073a730/
Changeset:   63429073a730
Branch:      yt
User:        jzuhone
Date:        2013-08-09 16:15:43
Summary:     Moved the photon models to a seperate file
Affected #:  1 file

diff -r 018276404e26f8ca54f8af2722d3afed30cf05bc -r 63429073a730e523995f9bee38c947579aa06ca5 yt/analysis_modules/synthetic_xray_obs/photon_models.py
--- /dev/null
+++ b/yt/analysis_modules/synthetic_xray_obs/photon_models.py
@@ -0,0 +1,115 @@
+import numpy as np
+from yt.funcs import *
+import h5py
+
+try:
+    import xspec
+except ImportError:
+    mylog.warning("You don't have PyXSpec installed. Some models won't be available.")
+                
+class PhotonModel(object):
+
+    def __init__(self, emin, emax, nchan):
+        self.emin = emin
+        self.emax = emax
+        self.nchan = nchan
+        self.ebins = np.linspace(emin, emax, nchan+1)
+        
+    def prepare(self):
+        pass
+    
+    def get_spectrum(self):
+        pass
+                                                        
+class XSpecThermalModel(PhotonModel):
+
+    def __init__(self, model_name, emin, emax, nchan):
+        self.model_name = model_name
+        PhotonModel.__init__(self, emin, emax, nchan)
+        
+    def prepare(self):
+        xspec.Xset.chatter = 0
+        xspec.AllModels.setEnergies("%f %f %d lin" %
+                                    (self.emin, self.emax, self.nchan))
+        self.model = xspec.Model(self.model_name)
+        
+    def get_spectrum(self, kT, Zmet):
+        m = getattr(self.model,self.model_name)
+        m.kT = kT
+        m.Abundanc = Zmet
+        m.norm = 1.0
+        m.Redshift = 0.0
+        return 1.0e-14*np.array(self.model.values(0))
+    
+class XSpecAbsorbModel(PhotonModel):
+
+    def __init__(self, model_name, nH, emin=0.01, emax=50.0, nchan=100000):
+        self.model_name = model_name
+        self.nH = nH
+        PhotonModel.__init__(self, emin, emax, nchan)
+        
+    def prepare(self):
+        xspec.Xset.chatter = 0
+        xspec.AllModels.setEnergies("%f %f %d lin" %
+                                    (self.emin, self.emax, self.nchan))
+        self.model = xspec.Model(self.model_name+"*powerlaw")
+        self.model.powerlaw.norm = self.nchan/(self.emax-self.emin)
+        self.model.powerlaw.PhoIndex = 0.0
+
+    def get_spectrum(self):
+        m = getattr(self.model,self.model_name)
+        m.nH = self.nH
+        return np.array(self.model.values(0))
+
+class TableApecModel(PhotonModel):
+
+    def __init__(self, filename):
+        if not os.path.exists(filename):
+            raise IOError("File does not exist: %s." % filename)
+        self.filename = filename
+        f = h5py.File(self.filename,"r")
+        self.T_vals = f["kT"][:]
+        self.Z_vals = f["Zmet"][:]
+        emin        = f["emin"].value
+        emax        = f["emax"].value
+        self.spec_table = f["spectrum"][:,:,:]
+        nchan = self.spec_table.shape[-1]
+        f.close()
+        self.dT = self.T_vals[1]-self.T_vals[0]
+        self.dZ = self.Z_vals[1]-self.Z_vals[0]
+        PhotonModel.__init__(self, emin, emax, nchan)
+        
+    def prepare(self):
+        pass
+
+    def get_spectrum(self, kT, Zmet):
+        iz = np.searchsorted(self.Z_vals, Zmet)-1
+        dz = (Zmet-self.Z_vals[iz])/self.dZ
+        it = np.searchsorted(self.T_vals, kT)-1
+        dt = (kT-self.T_vals[it])/self.dT
+        spec = self.spec_table[it+1,iz+1,:]*dt*dz + \
+               self.spec_table[it,iz,:]*(1.-dt)*(1.-dz) + \
+               self.spec_table[it,iz+1,:]*(1.-dt)*dz + \
+               self.spec_table[it+1,iz,:]*dt*(1.-dz)
+        return spec
+    
+class TableAbsorbModel(PhotonModel):
+
+    def __init__(self, filename):
+        if not os.path.exists(filename):
+            raise IOError("File does not exist: %s." % filename)
+        self.filename = filename
+        f = h5py.File(self.filename,"r")
+        emin = f["emin"].value
+        emax = f["emax"].value
+        self.abs = f["spectrum"][:]
+        nchan = self.abs.shape[0]
+        f.close()
+        PhotonModel.__init__(self, emin, emax, nchan)
+                                                                    
+    def prepare(self):
+        pass
+
+    def get_spectrum(self):
+        return self.abs ** nH
+    


https://bitbucket.org/yt_analysis/yt/commits/731d70670f4d/
Changeset:   731d70670f4d
Branch:      yt
User:        jzuhone
Date:        2013-08-09 16:17:06
Summary:     Moved the photon simulator to a new file. Added optional energy-dependent effective area. Added WCS coordinates to FITS images.
Affected #:  2 files

diff -r 63429073a730e523995f9bee38c947579aa06ca5 -r 731d70670f4d7a160ac0f7b9aae1b7c2af4e44e8 yt/analysis_modules/synthetic_xray_obs/photon_simulator.py
--- /dev/null
+++ b/yt/analysis_modules/synthetic_xray_obs/photon_simulator.py
@@ -0,0 +1,589 @@
+import numpy as np
+from yt.funcs import *
+from yt.utilities.physical_constants import mp, clight, cm_per_kpc, cm_per_mpc, \
+     cm_per_km, K_per_keV, erg_per_keV
+from yt.utilities.cosmology import Cosmology
+from yt.utilities.orientation import Orientation
+from yt.data_objects.api import add_field
+
+import os
+import h5py
+
+try:
+    import pyfits
+except ImportError:
+    try:
+        import astropy.io.fits as pyfits
+    except ImportError:
+        mylog.warning("You don't have pyFITS installed. Writing to and reading from FITS files won't be available.")
+    
+N_TBIN = 10000
+TMIN = 8.08e-2
+TMAX = 50.
+FOUR_PI = 4.*np.pi
+
+def _emission_measure(field, data):
+    if data.has_field_parameter("X_H"):
+        X_H = data.get_field_parameter("X_H")
+    else:
+        X_H = 0.75
+    return (data["Density"]/mp)*(data["CellMass"]/mp)*0.5*(1.+X_H)*X_H
+
+def _emission_measure_density(field, data):
+    if data.has_field_parameter("X_H"):
+        X_H = data.get_field_parameter("X_H")
+    else:
+        X_H = 0.75            
+    return (data["Density"]/mp)*(data["Density"]/mp)*0.5*(1.+X_H)*X_H
+
+add_field("EmissionMeasure", function=_emission_measure)
+add_field("EMDensity", function=_emission_measure_density)
+
+class XRayPhotonList(object):
+
+    def __init__(self, photons = None):
+        if photons is None: photons = {}
+        self.photons = photons
+        
+    @classmethod
+    def from_file(cls, filename):
+        """
+        Initialize a XRayPhotonList from an HDF5 file given by filename.
+        """
+        photons = {}
+        
+        f = h5py.File(filename, "r")
+
+        photons["FiducialExposureTime"] = f["/fid_exp_time"].value
+        photons["FiducialArea"] = f["/fid_area"].value
+        photons["Redshift"] = f["/redshift"].value
+        photons["Hubble0"] = f["/hubble"].value
+        photons["OmegaMatter"] = f["/omega_matter"].value
+        photons["OmegaLambda"] = f["/omega_lambda"].value                    
+        photons["AngularDiameterDistance"] = f["/d_a"].value
+                        
+        photons["x"] = f["/x"][:]
+        photons["y"] = f["/y"][:]
+        photons["z"] = f["/z"][:]
+        photons["dx"] = f["/dx"][:]
+        photons["vx"] = f["/vx"][:]
+        photons["vy"] = f["/vy"][:]
+        photons["vz"] = f["/vz"][:]
+        photons["NumberOfPhotons"] = f["/num_photons"][:].astype("uint64")
+
+        photons["Energy"] = f["/energy"][:]
+        
+        f.close()
+        
+        return cls(photons)
+
+    @classmethod
+    def from_scratch(cls, cell_data, redshift, eff_A, exp_time, emission_model,
+                     center="c", X_H=0.75, Zmet=0.3, cosmology=None):
+        """
+        Initialize a XRayPhotonList from a data container. 
+        """
+        pf = cell_data.pf
+
+        vol_scale = pf.units["cm"]**(-3)/np.prod(pf.domain_width)
+
+        if cosmology is None:
+            cosmo = Cosmology(HubbleConstantNow=71., OmegaMatterNow=0.27,
+                              OmegaLambdaNow=0.73)
+        else:
+            cosmo = cosmology
+            
+        D_A = cosmo.AngularDiameterDistance(0.0,redshift)*cm_per_mpc
+        cosmo_fac = 1.0/(FOUR_PI*D_A*D_A*(1.+redshift)**3)
+
+        idxs = np.argsort(cell_data["Temperature"])
+        dshape = idxs.shape
+
+        cell_data.set_field_parameter("X_H", X_H)
+
+        if center == "c":
+            src_ctr = pf.domain_center
+        elif center == "max":
+            src_ctr = pf.h.find_max("Density")[-1]
+        elif iterable(center):
+            src_ctr = center
+                    
+        kT_bins = np.linspace(TMIN, max(cell_data["TempkeV"][idxs][-1],
+                                        TMAX), num=N_TBIN+1)
+        dkT = kT_bins[1]-kT_bins[0]
+        kT_idxs = np.digitize(cell_data["TempkeV"][idxs], kT_bins)
+        kT_idxs = np.minimum(np.maximum(1, kT_idxs), N_TBIN) - 1
+        bcounts = np.bincount(kT_idxs).astype("int")
+        bcounts = bcounts[bcounts > 0]
+        kT_idxs = np.unique(kT_idxs)
+
+        emission_model.prepare()
+        energy = emission_model.ebins
+        de = energy[1]-energy[0]
+        emid = 0.5*(energy[1:]+energy[:-1])
+
+        cell_em = cell_data["EmissionMeasure"][idxs]*vol_scale
+        cell_vol = cell_data["CellVolume"][idxs]*vol_scale
+        cell_emd = cell_data["EMDensity"][idxs]
+
+        energies = []
+        number_of_photons = np.zeros(dshape, dtype='uint64')
+
+        pbar = get_pbar("Generating Photons", dshape[0])
+        n = int(0)
+            
+        for i,ikT in enumerate(kT_idxs):
+            
+            ncells = int(bcounts[i])
+            
+            kT = kT_bins[ikT] + 0.5*dkT
+            
+            em_sum = cell_em[n:n+ncells].sum()
+            vol_sum = cell_vol[n:n+ncells].sum()
+            em_avg = em_sum / vol_sum
+            
+            tot_norm = cosmo_fac*em_sum / vol_scale
+            
+            spec = emission_model.get_spectrum(kT, Zmet)
+            spec *= tot_norm
+            cumspec = np.cumsum(spec)
+            counts = cumspec[:]/cumspec[-1]
+            tot_ph = cumspec[-1]*eff_A*exp_time
+
+            for icell in xrange(n,n+ncells):
+
+                cell_norm = tot_ph * (cell_emd[icell]/em_avg) * (cell_vol[icell]/vol_sum)                    
+                cell_Nph = int(cell_norm) + int(np.modf(cell_norm)[0] >= np.random.random())
+                
+                if cell_Nph > 0:
+                    number_of_photons[icell] = cell_Nph                    
+                    randvec = np.random.uniform(low=counts[0], high=counts[-1], size=cell_Nph)
+                    randvec.sort()
+                    eidxs = np.searchsorted(counts, randvec)-1
+                    cell_e = emid[eidxs]+de*(randvec-counts[eidxs])/(counts[eidxs+1]-counts[eidxs])
+                    energies.append(cell_e)
+
+                pbar.update(icell)
+                
+            n += ncells
+            
+        pbar.finish()
+                        
+        active_cells = np.where(number_of_photons > 0)[0]
+        num_active_cells = len(active_cells)
+
+        photons = {}
+        photons["x"] = (cell_data["x"][idxs][active_cells]-src_ctr[0])*pf.units["kpc"]
+        photons["y"] = (cell_data["y"][idxs][active_cells]-src_ctr[1])*pf.units["kpc"]
+        photons["z"] = (cell_data["z"][idxs][active_cells]-src_ctr[2])*pf.units["kpc"]
+        photons["vx"] = cell_data["x-velocity"][idxs][active_cells]/cm_per_km
+        photons["vy"] = cell_data["y-velocity"][idxs][active_cells]/cm_per_km
+        photons["vz"] = cell_data["z-velocity"][idxs][active_cells]/cm_per_km
+        photons["dx"] = cell_data["dx"][idxs][active_cells]*pf.units["kpc"]
+        photons["NumberOfPhotons"] = number_of_photons[active_cells]
+        photons["Energy"] = np.concatenate(energies)
+
+        photons["FiducialExposureTime"] = exp_time
+        photons["FiducialArea"] = eff_A
+        photons["Redshift"] = redshift
+        photons["Hubble0"] = cosmo.HubbleConstantNow
+        photons["OmegaMatter"] = cosmo.OmegaMatterNow
+        photons["OmegaLambda"] = cosmo.OmegaLambdaNow
+        photons["AngularDiameterDistance"] = D_A
+        
+        return cls(photons)
+            
+    def write_h5_file(self, photonfile):
+
+        f = h5py.File(photonfile, "w")
+
+        # Scalars
+       
+        f.create_dataset("fid_area", data=self.photons["FiducialArea"])
+        f.create_dataset("fid_exp_time", data=self.photons["FiducialExposureTime"])
+        f.create_dataset("redshift", data=self.photons["Redshift"])
+        f.create_dataset("omega_matter", data=self.photons["OmegaMatter"])
+        f.create_dataset("omega_lambda", data=self.photons["OmegaLambda"])
+        f.create_dataset("hubble", data=self.photons["Hubble0"])
+        f.create_dataset("d_a", data=self.photons["AngularDiameterDistance"])
+        
+        # Arrays
+
+        f.create_dataset("x", data=self.photons["x"])
+        f.create_dataset("y", data=self.photons["y"])
+        f.create_dataset("z", data=self.photons["z"])
+        f.create_dataset("vx", data=self.photons["vx"])
+        f.create_dataset("vy", data=self.photons["vy"])
+        f.create_dataset("vz", data=self.photons["vz"])
+        f.create_dataset("dx", data=self.photons["dx"])
+        f.create_dataset("num_photons", data=self.photons["NumberOfPhotons"])
+        f.create_dataset("energy", data=self.photons["Energy"])
+                
+        f.close()
+    
+    def project_photons(self, L, area_new=None, texp_new=None, 
+                        absorb_model=None, smoothing=False):
+        """
+        Project photons 
+        """
+        dx = self.photons["dx"]
+        
+        L /= np.sqrt(np.dot(L, L))
+        vecs = np.identity(3)
+        t = np.cross(L, vecs).sum(axis=1)
+        ax = t.argmax()
+        north = np.cross(L, vecs[ax,:]).ravel()
+        orient = Orientation(L, north_vector=north)
+        
+        x_hat = orient.unit_vectors[0]
+        y_hat = orient.unit_vectors[1]
+        z_hat = orient.unit_vectors[2]
+
+        n_ph = self.photons["NumberOfPhotons"]
+        num_cells = len(n_ph)
+        n_ph_tot = n_ph.sum()
+
+        eff_area = None
+        
+        if texp_new is None and area_new is None:
+            n_obs = n_ph
+        else:
+            if texp_new is None:
+                Tratio = 1.
+            else:
+                Tratio = texp_new/self.photons["FiducialExposureTime"]
+            if area_new is None:
+                Aratio = 1.
+            elif isinstance(area_new, basestring):
+                mylog.info("Using energy-dependent effective area.")
+                f = pyfits.open(area_new)
+                elo = f[1].data.field("ENERG_LO")
+                ehi = f[1].data.field("ENERG_HI")
+                eff_area = f[1].data.field("SPECRESP")
+                f.close()
+                Aratio = eff_area.max()/self.photons["FiducialArea"]
+            else:
+                mylog.info("Using constant effective area.")
+                Aratio = area_new/self.photons["FiducialArea"]
+            fak = Aratio*Tratio
+            if fak > 1:
+                raise ValueError("Spectrum scaling factor = %g, cannot be greater than unity." % (fak))
+            n_obs = np.uint64(n_ph*fak)
+                            
+        n_obs_tot = n_obs.sum()
+
+        mylog.info("Total number of photons to use: %d" % (n_obs_tot))
+
+        if smoothing:
+            x = np.random.normal(scale=0.5,size=n_obs_tot)
+            y = np.random.normal(scale=0.5,size=n_obs_tot)
+            z = np.random.normal(scale=0.5,size=n_obs_tot)
+        else:
+            x = np.random.uniform(low=-0.5,high=0.5,size=n_obs_tot)
+            y = np.random.uniform(low=-0.5,high=0.5,size=n_obs_tot)
+            z = np.random.uniform(low=-0.5,high=0.5,size=n_obs_tot)
+
+        vz = self.photons["vx"]*z_hat[0] + \
+             self.photons["vy"]*z_hat[1] + \
+             self.photons["vz"]*z_hat[2]
+        shift = -vz*cm_per_km/clight
+        shift = np.sqrt((1.-shift)/(1.+shift))
+        
+        cells = np.concatenate([i*np.ones((n_ph[i]),dtype='uint64') for i in xrange(num_cells)])
+        if n_obs_tot == n_ph_tot:
+            idxs = np.arange(n_ph_tot,dtype='uint64')
+            obs_cells = cells
+        else:
+            idxs = np.random.choice(n_ph_tot, size=n_obs_tot, replace=False)
+            obs_cells = cells[idxs]
+        x *= dx[obs_cells]
+        y *= dx[obs_cells]
+        z *= dx[obs_cells]
+        x += self.photons["x"][obs_cells]
+        y += self.photons["y"][obs_cells]
+        z += self.photons["z"][obs_cells]  
+        eobs = self.photons["Energy"][idxs]*shift[obs_cells]
+        
+        xsky = x*x_hat[0] + y*x_hat[1] + z*x_hat[2]
+        ysky = x*y_hat[0] + y*y_hat[1] + z*y_hat[2]
+        eobs /= (1.+self.photons["Redshift"])
+                 
+        if absorb_model is None:
+            not_abs = np.ones(eobs.shape, dtype='bool')
+        else:
+            mylog.info("Absorbing.")
+            absorb_model.prepare()
+            energy = absorb_model.ebins
+            de = energy[1]-energy[0]
+            emid = 0.5*(energy[1:]+energy[:-1])
+            aspec = absorb_model.get_spectrum()
+            eidxs = np.searchsorted(emid, eobs)-1
+            dx = (eobs-emid[eidxs])/de
+            absorb = aspec[eidxs]*(1.-dx) + aspec[eidxs+1]*dx
+            randvec = aspec.max()*np.random.random(eobs.shape)
+            not_abs = randvec < absorb
+
+        if eff_area is None:
+            detected = np.ones(eobs.shape, dtype='bool')
+        else:
+            mylog.info("Applying energy-dependent effective area.")
+            earf = 0.5*(elo+ehi)
+            de = earf[1]-earf[0]            
+            eidxs = np.searchsorted(earf, eobs)-1
+            dx = (eobs-earf[eidxs])/de
+            earea = eff_area[eidxs]*(1.-dx) + eff_area[eidxs+1]*dx
+            randvec = eff_area.max()*np.random.random(eobs.shape)
+            detected = randvec < earea
+        
+        all_obs = np.logical_and(not_abs, detected)
+
+        mylog.info("Total number of observed photons: %d" % (all_obs.sum()))
+                    
+        D_A = self.photons["AngularDiameterDistance"] / cm_per_kpc
+
+        events = {}
+
+        events["xsky"] = np.rad2deg(xsky[all_obs]/D_A)*3600.
+        events["ysky"] = np.rad2deg(ysky[all_obs]/D_A)*3600.
+        events["eobs"] = eobs[all_obs]
+
+        if texp_new is None:
+            events["ExposureTime"] = self.photons["FiducialExposureTime"]
+        else:
+            events["ExposureTime"] = texp_new
+        if area_new is None:
+            events["Area"] = self.photons["FiducialArea"]
+        else:
+            events["Area"] = area_new
+        events["Hubble0"] = self.photons["Hubble0"]
+        events["Redshift"] = self.photons["Redshift"]
+        events["OmegaMatter"] = self.photons["OmegaMatter"]
+        events["OmegaLambda"] = self.photons["OmegaLambda"] 
+        
+        return XRayEventList(events)
+
+class XRayEventList(object) :
+
+    def __init__(self, events = None) :
+
+        if events is None : events = {}
+        self.events = events
+        self.num_events = events["xsky"].shape[0]
+        
+    @classmethod
+    def from_h5_file(cls, h5file):
+        """
+        Initialize a XRayEventList from a HDF5 file with filename h5file.
+        """
+        events = {}
+        
+        f = h5py.File(h5file, "r")
+
+        events["ExposureTime"] = f["/exp_time"].value
+        events["Area"] = f["/area"].value
+        events["Hubble0"] = f["/hubble"].value
+        events["Redshift"] = f["/redshift"].value
+        events["OmegaMatter"] = f["/omega_matter"].value
+        events["OmegaLambda"] = f["/omega_lambda"].value
+        
+        events["xsky"] = f["/xsky"][:]
+        events["ysky"] = f["/ysky"][:]
+        events["eobs"] = f["/eobs"][:]
+        
+        f.close()
+        
+        return cls(events)
+
+    @classmethod
+    def from_fits_file(cls, fitsfile):
+        """
+        Initialize a XRayEventList from a FITS file with filename fitsfile.
+        """
+        hdulist = pyfits.open(fitsfile)
+
+        tblhdu = hdulist[1]
+
+        events = {}
+        
+        events["ExposureTime"] = tblhdu.header["EXPOSURE"]
+        events["Area"] = tblhdu.header["AREA"]
+        events["Hubble0"] = tblhdu.header["HUBBLE"]
+        events["Redshift"] = tblhdu.header["REDSHIFT"]
+        events["OmegaMatter"] = tblhdu.header["OMEGA_M"]
+        events["OmegaLambda"] = tblhdu.header["OMEGA_L"]
+
+        events["xsky"] = tblhdu.data.field("POS_X")
+        events["ysky"] = tblhdu.data.field("POS_Y")
+        events["eobs"] = tblhdu.data.field("ENERGY")
+        
+        return cls(events)
+
+    def write_fits_file(self, fitsfile, clobber=False):
+        """
+        Write events to a FITS binary table file.
+        """
+        col1 = pyfits.Column(name='ENERGY', format='E',
+                             array=self.events["eobs"])
+        col2 = pyfits.Column(name='XSKY', format='D',
+                             array=self.events["xsky"])
+        col3 = pyfits.Column(name='YSKY', format='D',
+                             array=self.events["ysky"])
+        
+        coldefs = pyfits.ColDefs([col1, col2, col3])
+        
+        tbhdu = pyfits.new_table(coldefs)
+
+        tbhdu.header.update("EXPOSURE", self.events["ExposureTime"])
+        tbhdu.header.update("AREA", self.events["Area"])
+        tbhdu.header.update("HUBBLE", self.events["Hubble0"])
+        tbhdu.header.update("OMEGA_M", self.events["OmegaMatter"])
+        tbhdu.header.update("OMEGA_L", self.events["OmegaLambda"])
+        
+        tbhdu.writeto(fitsfile, clobber=clobber)
+                
+    def write_simput_file(self, prefix, clobber=False, e_min=None, e_max=None):
+
+        if e_min is None:
+            e_min = 0.0
+        if e_max is None:
+            e_max = 100.0
+
+        idxs = np.logical_and(self.events["eobs"] >= e_min, self.events["eobs"] <= e_max)
+        flux = erg_per_keV*np.sum(self.events["eobs"][idxs])/self.events["ExposureTime"]/self.events["Area"]
+        
+        col1 = pyfits.Column(name='ENERGY', format='E',
+                             array=self.events["eobs"])
+        col2 = pyfits.Column(name='DEC', format='D',
+                             array=self.events["ysky"]/3600.)
+        col3 = pyfits.Column(name='RA', format='D',
+                             array=self.events["xsky"]/3600.)
+
+        coldefs = pyfits.ColDefs([col1, col2, col3])
+
+        tbhdu = pyfits.new_table(coldefs)
+        tbhdu.update_ext_name("PHLIST")
+
+        tbhdu.header.update("HDUCLASS", "HEASARC/SIMPUT")
+        tbhdu.header.update("HDUCLAS1", "PHOTONS")
+        tbhdu.header.update("HDUVERS", "1.1.0")
+        tbhdu.header.update("EXTVER", 1)
+        tbhdu.header.update("REFRA", 0.0)
+        tbhdu.header.update("REFDEC", 0.0)
+        tbhdu.header.update("TUNIT1", "keV")
+        tbhdu.header.update("TUNIT2", "deg")
+        tbhdu.header.update("TUNIT3", "deg")                
+
+        phfile = prefix+"_phlist.fits"
+
+        tbhdu.writeto(phfile, clobber=clobber)
+
+        col1 = pyfits.Column(name='SRC_ID', format='J', array=np.array([1]).astype("int32"))
+        col2 = pyfits.Column(name='RA', format='D', array=np.array([0.0]))
+        col3 = pyfits.Column(name='DEC', format='D', array=np.array([0.0]))
+        col4 = pyfits.Column(name='E_MIN', format='D', array=np.array([e_min]))
+        col5 = pyfits.Column(name='E_MAX', format='D', array=np.array([e_max]))
+        col6 = pyfits.Column(name='FLUX', format='D', array=np.array([flux]))
+        col7 = pyfits.Column(name='SPECTRUM', format='80A', array=np.array([phfile+"[PHLIST,1]"]))
+        col8 = pyfits.Column(name='IMAGE', format='80A', array=np.array([phfile+"[PHLIST,1]"]))
+                        
+        coldefs = pyfits.ColDefs([col1, col2, col3, col4, col5, col6, col7, col8])
+        
+        wrhdu = pyfits.new_table(coldefs)
+        wrhdu.update_ext_name("SRC_CAT")
+                                
+        wrhdu.header.update("HDUCLASS", "HEASARC")
+        wrhdu.header.update("HDUCLAS1", "SIMPUT")
+        wrhdu.header.update("HDUCLAS2", "SRC_CAT")        
+        wrhdu.header.update("HDUVERS", "1.1.0")
+        wrhdu.header.update("RADECSYS", "FK5")
+        wrhdu.header.update("EQUINOX", 2000.0)
+        wrhdu.header.update("TUNIT2", "deg")
+        wrhdu.header.update("TUNIT3", "deg")
+        wrhdu.header.update("TUNIT4", "keV")
+        wrhdu.header.update("TUNIT5", "keV")
+        wrhdu.header.update("TUNIT6", "erg/s/cm**2")
+
+        simputfile = prefix+"_simput.fits"
+                
+        wrhdu.writeto(simputfile, clobber=clobber)
+
+    def write_h5_file(self, h5file):
+        """
+        Write a XRayEventList to the HDF5 file given by h5file.
+        """
+        f = h5py.File(h5file, "w")
+
+        f.create_dataset("/exp_time", data=self.events["ExposureTime"])
+        f.create_dataset("/area", data=self.events["Area"])
+        f.create_dataset("/redshift", data=self.events["Redshift"])
+        f.create_dataset("/hubble", data=self.events["Hubble0"])
+        f.create_dataset("/omega_matter", data=self.events["OmegaMatter"])
+        f.create_dataset("/omega_lambda", data=self.events["OmegaLambda"])        
+        f.create_dataset("/xsky", data=self.events["xsky"])
+        f.create_dataset("/ysky", data=self.events["ysky"])
+        f.create_dataset("/eobs", data=self.events["eobs"])
+                        
+        f.close()
+
+    def write_fits_image(self, imagefile, width, nx, center,
+                         clobber=False, gzip_file=False,
+                         emin=None, emax=None):
+        
+        if emin is None:
+            mask_emin = np.ones((self.num_events), dtype='bool')
+        else:
+            mask_emin = self.events["eobs"] > emin
+        if emax is None:
+            mask_emax = np.ones((self.num_events), dtype='bool')
+        else:
+            mask_emax = self.events["eobs"] < emax
+
+        mask = np.logical_and(mask_emin, mask_emax)
+
+        dx_pixel = width/nx
+        xmin = -0.5*width
+        xmax = -xmin
+        xbins = np.linspace(xmin, xmax, nx+1, endpoint=True)
+        
+        H, xedges, yedges = np.histogram2d(self.events["xsky"][mask],
+                                           self.events["ysky"][mask],
+                                           bins=[xbins,xbins])
+        
+        hdu = pyfits.PrimaryHDU(H.T[::-1,::])
+
+        hdu.header.update("MTYPE1", "EQPOS")
+        hdu.header.update("MFORM1", "RA,DEC")
+        hdu.header.update("CTYPE1", "RA---TAN")
+        hdu.header.update("CTYPE2", "DEC--TAN")
+        hdu.header.update("CRPIX1", 0.5*(nx+1))
+        hdu.header.update("CRPIX2", 0.5*(nx+1))                
+        hdu.header.update("CRVAL1", center[0])
+        hdu.header.update("CRVAL2", center[1])
+        hdu.header.update("CUNIT1", "deg")
+        hdu.header.update("CUNIT2", "deg")
+        hdu.header.update("CDELT1", -dx_pixel/3600.)
+        hdu.header.update("CDELT2", dx_pixel/3600.)
+        hdu.header.update("EXPOSURE", self.events["ExposureTime"])
+        
+        hdu.writeto(imagefile, clobber=clobber)
+
+        if (gzip_file):
+            clob = ""
+            if (clobber) : clob="-f"
+            os.system("gzip "+clob+" %s.fits" % (prefix))
+                                    
+    def write_spectrum(self, specfile, emin, emax, nchan, clobber=False):
+        
+        spec, ee = np.histogram(self.events["eobs"], bins=nchan, range=(emin, emax))
+
+        de = ee[1]-ee[0]
+        emid = 0.5*(ee[1:]+ee[:-1])
+        
+        col1 = pyfits.Column(name='ENERGY', format='1E', array=emid)
+        col2 = pyfits.Column(name='COUNTS', format='1E', array=spec)
+        
+        coldefs = pyfits.ColDefs([col1, col2])
+        
+        tbhdu = pyfits.new_table(coldefs)
+        
+        tbhdu.writeto(specfile, clobber=clobber)

diff -r 63429073a730e523995f9bee38c947579aa06ca5 -r 731d70670f4d7a160ac0f7b9aae1b7c2af4e44e8 yt/analysis_modules/synthetic_xray_obs/xray_photon_sim.py
--- a/yt/analysis_modules/synthetic_xray_obs/xray_photon_sim.py
+++ /dev/null
@@ -1,672 +0,0 @@
-import numpy as np
-from yt.funcs import *
-from yt.utilities.physical_constants import mp, clight, cm_per_kpc, cm_per_mpc, \
-     cm_per_km, K_per_keV, erg_per_keV
-from yt.utilities.cosmology import Cosmology
-from yt.utilities.orientation import Orientation
-from yt.data_objects.api import add_field
-import os
-import h5py
-
-try:
-    import pyfits
-except ImportError:
-    try:
-        import astropy.io.fits as pyfits
-    except ImportError:
-        mylog.warning("You don't have pyFITS installed. Writing to FITS files won't be available.")
-    
-try:
-    import xspec
-except ImportError:
-    mylog.warning("You don't have PyXSpec installed. Some models won't be available.")
-
-N_TBIN = 10000
-TMIN = 8.08e-2
-TMAX = 50.
-FOUR_PI = 4.*np.pi
-
-def _emission_measure(field, data):
-    if data.has_field_parameter("X_H"):
-        X_H = data.get_field_parameter("X_H")
-    else:
-        X_H = 0.75
-    return (data["Density"]/mp)*(data["CellMass"]/mp)*0.5*(1.+X_H)*X_H
-
-def _emission_measure_density(field, data):
-    if data.has_field_parameter("X_H"):
-        X_H = data.get_field_parameter("X_H")
-    else:
-        X_H = 0.75            
-    return (data["Density"]/mp)*(data["Density"]/mp)*0.5*(1.+X_H)*X_H
-
-add_field("EmissionMeasure", function=_emission_measure)
-add_field("EMDensity", function=_emission_measure_density)
-
-class PhotonModel(object):
-
-    def __init__(self, emin, emax, nchan):
-        self.emin = emin
-        self.emax = emax
-        self.nchan = nchan
-        self.ebins = np.linspace(emin, emax, nchan+1)
-
-    def prepare(self):
-        pass
-    
-    def get_spectrum(self):
-        pass
-    
-class XSpecThermalModel(PhotonModel):
-    
-    def __init__(self, model_name, emin, emax, nchan):
-        self.model_name = model_name
-        PhotonModel.__init__(self, emin, emax, nchan)
-                
-    def prepare(self):
-        xspec.Xset.chatter = 0
-        xspec.AllModels.setEnergies("%f %f %d lin" % (self.emin, self.emax, self.nchan))
-        self.model = xspec.Model(self.model_name)
-        
-    def get_spectrum(self, kT, Zmet):
-
-        m = getattr(self.model,self.model_name)
-        m.kT = kT 
-        m.Abundanc = Zmet
-        m.norm = 1.0
-        m.Redshift = 0.0        
-        return 1.0e-14*np.array(self.model.values(0))
-
-class XSpecAbsorbModel(PhotonModel):
-
-     def __init__(self, model_name, nH, emin=0.01, emax=50.0, nchan=100000):
-         self.model_name = model_name
-         self.nH = nH
-         PhotonModel.__init__(self, emin, emax, nchan)
-         
-     def prepare(self):
-         xspec.Xset.chatter = 0
-         xspec.AllModels.setEnergies("%f %f %d lin" % (self.emin, self.emax, self.nchan))
-         self.model = xspec.Model(self.model_name+"*powerlaw")
-         self.model.powerlaw.norm = self.nchan/(self.emax-self.emin)
-         self.model.powerlaw.PhoIndex = 0.0
-                                                       
-     def get_spectrum(self):
-         m = getattr(self.model,self.model_name)
-         m.nH = self.nH
-         return np.array(self.model.values(0))
-    
-class TableApecModel(PhotonModel):
-
-    def __init__(self, filename):
-        if not os.path.exists(filename):
-            raise IOError("File does not exist: %s." % filename)
-        self.filename = filename
-        f = h5py.File(self.filename,"r")
-        self.T_vals = f["kT"][:]
-        self.Z_vals = f["Zmet"][:]
-        emin        = f["emin"].value
-        emax        = f["emax"].value
-        self.spec_table = f["spectrum"][:,:,:]
-        nchan = self.spec_table.shape[-1]
-        f.close()
-        self.dT = self.T_vals[1]-self.T_vals[0]
-        self.dZ = self.Z_vals[1]-self.Z_vals[0]
-        PhotonModel.__init__(self, emin, emax, nchan)
-                
-    def prepare(self):
-        pass
-    
-    def get_spectrum(self, kT, Zmet):
-        iz = np.searchsorted(self.Z_vals, Zmet)-1
-        dz = (Zmet-self.Z_vals[iz])/self.dZ
-        it = np.searchsorted(self.T_vals, kT)-1
-        dt = (kT-self.T_vals[it])/self.dT
-        spec = self.spec_table[it+1,iz+1,:]*dt*dz + \
-               self.spec_table[it,iz,:]*(1.-dt)*(1.-dz) + \
-               self.spec_table[it,iz+1,:]*(1.-dt)*dz + \
-               self.spec_table[it+1,iz,:]*dt*(1.-dz)
-        return spec
-
-class TableAbsorbModel(PhotonModel):
-
-    def __init__(self, filename):
-        if not os.path.exists(filename):
-            raise IOError("File does not exist: %s." % filename)
-        self.filename = filename
-        f = h5py.File(self.filename,"r")
-        emin = f["emin"].value
-        emax = f["emax"].value
-        self.abs = f["spectrum"][:]
-        nchan = self.abs.shape[0]
-        f.close()
-        PhotonModel.__init__(self, emin, emax, nchan)
-        
-    def prepare(self):
-        pass
-
-    def get_spectrum(self):
-        return self.abs ** nH
-            
-class XRayPhotonList(object):
-
-    def __init__(self, photons = None):
-        if photons is None: photons = {}
-        self.photons = photons
-        
-    @classmethod
-    def from_file(cls, filename):
-        """
-        Initialize a XRayPhotonList from an HDF5 file given by filename.
-        """
-        photons = {}
-        
-        f = h5py.File(filename, "r")
-
-        photons["FiducialExposureTime"] = f["/fid_exp_time"].value
-        photons["FiducialArea"] = f["/fid_area"].value
-        photons["Redshift"] = f["/redshift"].value
-        photons["Hubble0"] = f["/hubble"].value
-        photons["OmegaMatter"] = f["/omega_matter"].value
-        photons["OmegaLambda"] = f["/omega_lambda"].value                    
-        photons["AngularDiameterDistance"] = f["/d_a"].value
-                        
-        photons["x"] = f["/x"][:]
-        photons["y"] = f["/y"][:]
-        photons["z"] = f["/z"][:]
-        photons["dx"] = f["/dx"][:]
-        photons["vx"] = f["/vx"][:]
-        photons["vy"] = f["/vy"][:]
-        photons["vz"] = f["/vz"][:]
-        photons["NumberOfPhotons"] = f["/num_photons"][:].astype("uint64")
-
-        photons["Energy"] = f["/energy"][:]
-        
-        f.close()
-        
-        return cls(photons)
-
-    @classmethod
-    def from_scratch(cls, cell_data, redshift, eff_A, exp_time, emission_model,
-                     center="c", X_H=0.75, Zmet=0.3, cosmology=None):
-        """
-        Initialize a XRayPhotonList from a data container. 
-        """
-        pf = cell_data.pf
-
-        vol_scale = pf.units["cm"]**(-3)/np.prod(pf.domain_width)
-
-        if cosmology is None:
-            cosmo = Cosmology(HubbleConstantNow=71., OmegaMatterNow=0.27,
-                              OmegaLambdaNow=0.73)
-        else:
-            cosmo = cosmology
-            
-        D_A = cosmo.AngularDiameterDistance(0.0,redshift)*cm_per_mpc
-        cosmo_fac = 1.0/(FOUR_PI*D_A*D_A*(1.+redshift)**3)
-
-        idxs = np.argsort(cell_data["Temperature"])
-        dshape = idxs.shape
-
-        cell_data.set_field_parameter("X_H", X_H)
-
-        if center == "c":
-            src_ctr = pf.domain_center
-        elif center == "max":
-            src_ctr = pf.h.find_max("Density")[-1]
-        elif iterable(center):
-            src_ctr = center
-                    
-        kT_bins = np.linspace(TMIN, max(cell_data["TempkeV"][idxs][-1],
-                                        TMAX), num=N_TBIN+1)
-        dkT = kT_bins[1]-kT_bins[0]
-        kT_idxs = np.digitize(cell_data["TempkeV"][idxs], kT_bins)
-        kT_idxs = np.minimum(np.maximum(1, kT_idxs), N_TBIN) - 1
-        bcounts = np.bincount(kT_idxs).astype("int")
-        bcounts = bcounts[bcounts > 0]
-        kT_idxs = np.unique(kT_idxs)
-
-        emission_model.prepare()
-        energy = emission_model.ebins
-        de = energy[1]-energy[0]
-        emid = 0.5*(energy[1:]+energy[:-1])
-
-        cell_em = cell_data["EmissionMeasure"][idxs]*vol_scale
-        cell_vol = cell_data["CellVolume"][idxs]*vol_scale
-        cell_emd = cell_data["EMDensity"][idxs]
-
-        energies = []
-        number_of_photons = np.zeros(dshape, dtype='uint64')
-
-        pbar = get_pbar("Generating Photons", dshape[0])
-        n = int(0)
-            
-        for i,ikT in enumerate(kT_idxs):
-            
-            ncells = int(bcounts[i])
-            
-            kT = kT_bins[ikT] + 0.5*dkT
-            
-            em_sum = cell_em[n:n+ncells].sum()
-            vol_sum = cell_vol[n:n+ncells].sum()
-            em_avg = em_sum / vol_sum
-            
-            tot_norm = cosmo_fac*em_sum / vol_scale
-            
-            spec = emission_model.get_spectrum(kT, Zmet)
-            spec *= tot_norm
-            cumspec = np.cumsum(spec)
-            counts = cumspec[:]/cumspec[-1]
-            tot_ph = cumspec[-1]*eff_A*exp_time
-
-            for icell in xrange(n,n+ncells):
-
-                cell_norm = tot_ph * (cell_emd[icell]/em_avg) * (cell_vol[icell]/vol_sum)                    
-                cell_Nph = int(cell_norm) + int(np.modf(cell_norm)[0] >= np.random.random())
-                
-                if cell_Nph > 0:
-                    number_of_photons[icell] = cell_Nph                    
-                    randvec = np.random.uniform(low=counts[0], high=counts[-1], size=cell_Nph)
-                    randvec.sort()
-                    eidxs = np.searchsorted(counts, randvec)-1
-                    cell_e = emid[eidxs]+de*(randvec-counts[eidxs])/(counts[eidxs+1]-counts[eidxs])
-                    energies.append(cell_e)
-
-                pbar.update(icell)
-                
-            n += ncells
-            
-        pbar.finish()
-                        
-        active_cells = np.where(number_of_photons > 0)[0]
-        num_active_cells = len(active_cells)
-
-        photons = {}
-        photons["x"] = (cell_data["x"][idxs][active_cells]-src_ctr[0])*pf.units["kpc"]
-        photons["y"] = (cell_data["y"][idxs][active_cells]-src_ctr[1])*pf.units["kpc"]
-        photons["z"] = (cell_data["z"][idxs][active_cells]-src_ctr[2])*pf.units["kpc"]
-        photons["vx"] = cell_data["x-velocity"][idxs][active_cells]/cm_per_km
-        photons["vy"] = cell_data["y-velocity"][idxs][active_cells]/cm_per_km
-        photons["vz"] = cell_data["z-velocity"][idxs][active_cells]/cm_per_km
-        photons["dx"] = cell_data["dx"][idxs][active_cells]*pf.units["kpc"]
-        photons["NumberOfPhotons"] = number_of_photons[active_cells]
-        photons["Energy"] = np.concatenate(energies)
-
-        photons["FiducialExposureTime"] = exp_time
-        photons["FiducialArea"] = eff_A
-        photons["Redshift"] = redshift
-        photons["Hubble0"] = cosmo.HubbleConstantNow
-        photons["OmegaMatter"] = cosmo.OmegaMatterNow
-        photons["OmegaLambda"] = cosmo.OmegaLambdaNow
-        photons["AngularDiameterDistance"] = D_A
-        
-        return cls(photons)
-            
-    def write_h5_file(self, photonfile):
-
-        f = h5py.File(photonfile, "w")
-
-        # Scalars
-       
-        f.create_dataset("fid_area", data=self.photons["FiducialArea"])
-        f.create_dataset("fid_exp_time", data=self.photons["FiducialExposureTime"])
-        f.create_dataset("redshift", data=self.photons["Redshift"])
-        f.create_dataset("omega_matter", data=self.photons["OmegaMatter"])
-        f.create_dataset("omega_lambda", data=self.photons["OmegaLambda"])
-        f.create_dataset("hubble", data=self.photons["Hubble0"])
-        f.create_dataset("d_a", data=self.photons["AngularDiameterDistance"])
-        
-        # Arrays
-
-        f.create_dataset("x", data=self.photons["x"])
-        f.create_dataset("y", data=self.photons["y"])
-        f.create_dataset("z", data=self.photons["z"])
-        f.create_dataset("vx", data=self.photons["vx"])
-        f.create_dataset("vy", data=self.photons["vy"])
-        f.create_dataset("vz", data=self.photons["vz"])
-        f.create_dataset("dx", data=self.photons["dx"])
-        f.create_dataset("num_photons", data=self.photons["NumberOfPhotons"])
-        f.create_dataset("energy", data=self.photons["Energy"])
-                
-        f.close()
-    
-    def project_photons(self, L, area_new=None, texp_new=None, 
-                        absorb_model=None, smoothing=False):
-        """
-        Project photons 
-        """
-        dx = self.photons["dx"]
-        
-        L /= np.sqrt(np.dot(L, L))
-        vecs = np.identity(3)
-        t = np.cross(L, vecs).sum(axis=1)
-        ax = t.argmax()
-        north = np.cross(L, vecs[ax,:]).ravel()
-        orient = Orientation(L, north_vector=north)
-        
-        x_hat = orient.unit_vectors[0]
-        y_hat = orient.unit_vectors[1]
-        z_hat = orient.unit_vectors[2]
-
-        n_ph = self.photons["NumberOfPhotons"]
-        num_cells = len(n_ph)
-        n_ph_tot = n_ph.sum()
-        
-        if texp_new is None and area_new is None:
-            n_obs = n_ph
-        else:
-            if texp_new is None:
-                Tratio = 1.
-            else:
-                Tratio = texp_new/self.photons["FiducialExposureTime"]
-            if area_new is None:
-                Aratio = 1.
-            else:
-                Aratio = area_new/self.photons["FiducialArea"]
-            fak = Aratio*Tratio
-            if fak > 1:
-                raise ValueError("Spectrum scaling factor = %g, cannot be greater than unity." % (fak))
-            n_obs = np.uint64(n_ph*fak)
-            
-        n_obs_tot = n_obs.sum()
-
-        mylog.info("Total number of observed photons (without absorption): %d" % (n_obs_tot))
-
-        if smoothing:
-            x = np.random.normal(scale=0.5,size=n_obs_tot)
-            y = np.random.normal(scale=0.5,size=n_obs_tot)
-            z = np.random.normal(scale=0.5,size=n_obs_tot)
-        else:
-            x = np.random.uniform(low=-0.5,high=0.5,size=n_obs_tot)
-            y = np.random.uniform(low=-0.5,high=0.5,size=n_obs_tot)
-            z = np.random.uniform(low=-0.5,high=0.5,size=n_obs_tot)
-
-        vz = self.photons["vx"]*z_hat[0] + \
-             self.photons["vy"]*z_hat[1] + \
-             self.photons["vz"]*z_hat[2]
-        shift = -vz * cm_per_km / clight
-        shift = np.sqrt((1.-shift)/(1.+shift))
-        
-        cells = np.concatenate([i*np.ones((n_ph[i]),dtype='uint64') for i in xrange(num_cells)])
-        if n_obs_tot == n_ph_tot:
-            idxs = np.arange(n_ph_tot,dtype='uint64')
-            obs_cells = cells
-        else:
-            idxs = np.random.choice(n_ph_tot, size=n_obs_tot, replace=False)
-            obs_cells = cells[idxs]
-        x *= dx[obs_cells]
-        y *= dx[obs_cells]
-        z *= dx[obs_cells]
-        x += self.photons["x"][obs_cells]
-        y += self.photons["y"][obs_cells]
-        z += self.photons["z"][obs_cells]  
-        eobs = self.photons["Energy"][idxs]*shift[obs_cells]
-        
-        xsky = x*x_hat[0] + y*x_hat[1] + z*x_hat[2]
-        ysky = x*y_hat[0] + y*y_hat[1] + z*y_hat[2]
-        eobs /= (1.+self.photons["Redshift"])
-                 
-        if absorb_model is None:
-            not_abs = np.ones(eobs.shape, dtype='bool')
-        else:
-            mylog.info("Absorbing.")
-            absorb_model.prepare()
-            energy = absorb_model.ebins
-            de = energy[1]-energy[0]
-            emid = 0.5*(energy[1:]+energy[:-1])
-            aspec = absorb_model.get_spectrum()
-            aspec_max = aspec.max()
-            eidxs = np.searchsorted(emid, eobs)-1
-            dx = (eobs-emid[eidxs])/de
-            absorb = aspec[eidxs]*(1.-dx) + aspec[eidxs+1]*dx
-            randvec = aspec_max*np.random.random(eobs.shape)
-            not_abs = np.where(randvec < absorb)[0]
-                        
-        D_A = self.photons["AngularDiameterDistance"] / cm_per_kpc
-
-        events = {}
-
-        events["xsky"] = np.rad2deg(xsky[not_abs]/D_A)*3600.
-        events["ysky"] = np.rad2deg(ysky[not_abs]/D_A)*3600.
-        events["eobs"] = eobs[not_abs]
-
-        if texp_new is None:
-            events["ExposureTime"] = self.photons["FiducialExposureTime"]
-        else:
-            events["ExposureTime"] = texp_new
-        if area_new is None:
-            events["Area"] = self.photons["FiducialArea"]
-        else:
-            events["Area"] = area_new
-        events["Hubble0"] = self.photons["Hubble0"]
-        events["Redshift"] = self.photons["Redshift"]
-        events["OmegaMatter"] = self.photons["OmegaMatter"]
-        events["OmegaLambda"] = self.photons["OmegaLambda"] 
-        
-        return XRayEventList(events)
-
-class XRayEventList(object) :
-
-    def __init__(self, events = None) :
-
-        if events is None : events = {}
-        self.events = events
-        self.num_events = events["xsky"].shape[0]
-        
-    @classmethod
-    def from_h5_file(cls, h5file):
-        """
-        Initialize a XRayEventList from a HDF5 file with filename h5file.
-        """
-        events = {}
-        
-        f = h5py.File(h5file, "r")
-
-        events["ExposureTime"] = f["/exp_time"].value
-        events["Area"] = f["/area"].value
-        events["Hubble0"] = f["/hubble"].value
-        events["Redshift"] = f["/redshift"].value
-        events["OmegaMatter"] = f["/omega_matter"].value
-        events["OmegaLambda"] = f["/omega_lambda"].value
-        
-        events["xsky"] = f["/xsky"][:]
-        events["ysky"] = f["/ysky"][:]
-        events["eobs"] = f["/eobs"][:]
-        
-        f.close()
-        
-        return cls(events)
-
-    @classmethod
-    def from_fits_file(cls, fitsfile):
-        """
-        Initialize a XRayEventList from a FITS file with filename fitsfile.
-        """
-        hdulist = pyfits.open(fitsfile)
-
-        tblhdu = hdulist[1]
-
-        events = {}
-        
-        events["ExposureTime"] = tblhdu.header["EXPOSURE"]
-        events["Area"] = tblhdu.header["AREA"]
-        events["Hubble0"] = tblhdu.header["HUBBLE"]
-        events["Redshift"] = tblhdu.header["REDSHIFT"]
-        events["OmegaMatter"] = tblhdu.header["OMEGA_M"]
-        events["OmegaLambda"] = tblhdu.header["OMEGA_L"]
-
-        events["xsky"] = tblhdu.data.field("POS_X")
-        events["ysky"] = tblhdu.data.field("POS_Y")
-        events["eobs"] = tblhdu.data.field("ENERGY")
-        
-        return cls(events)
-
-    def write_fits_file(self, fitsfile, clobber=False):
-        """
-        Write events to a FITS binary table file.
-        """
-        col1 = pyfits.Column(name='ENERGY', format='E',
-                             array=self.events["eobs"])
-        col2 = pyfits.Column(name='XSKY', format='D',
-                             array=self.events["xsky"])
-        col3 = pyfits.Column(name='YSKY', format='D',
-                             array=self.events["ysky"])
-        
-        coldefs = pyfits.ColDefs([col1, col2, col3])
-        
-        tbhdu = pyfits.new_table(coldefs)
-
-        tbhdu.header.update("EXPOSURE", self.events["ExposureTime"])
-        tbhdu.header.update("AREA", self.events["Area"])
-        tbhdu.header.update("HUBBLE", self.events["Hubble0"])
-        tbhdu.header.update("OMEGA_M", self.events["OmegaMatter"])
-        tbhdu.header.update("OMEGA_L", self.events["OmegaLambda"])
-        
-        tbhdu.writeto(fitsfile, clobber=clobber)
-                
-    def write_simput_file(self, prefix, clobber=False, e_min=None, e_max=None):
-
-        if e_min is None:
-            e_min = 0.0
-        if e_max is None:
-            e_max = 100.0
-
-        idxs = np.logical_and(self.events["eobs"] >= e_min, self.events["eobs"] <= e_max)
-        flux = erg_per_keV*np.sum(self.events["eobs"][idxs])/self.events["ExposureTime"]/self.events["Area"]
-        
-        col1 = pyfits.Column(name='ENERGY', format='E',
-                             array=self.events["eobs"])
-        col2 = pyfits.Column(name='DEC', format='D',
-                             array=self.events["ysky"]/3600.)
-        col3 = pyfits.Column(name='RA', format='D',
-                             array=self.events["xsky"]/3600.)
-
-        coldefs = pyfits.ColDefs([col1, col2, col3])
-
-        tbhdu = pyfits.new_table(coldefs)
-        tbhdu.update_ext_name("PHLIST")
-
-        tbhdu.header.update("HDUCLASS", "HEASARC/SIMPUT")
-        tbhdu.header.update("HDUCLAS1", "PHOTONS")
-        tbhdu.header.update("HDUVERS", "1.1.0")
-        tbhdu.header.update("EXTVER", 1)
-        tbhdu.header.update("REFRA", 0.0)
-        tbhdu.header.update("REFDEC", 0.0)
-        tbhdu.header.update("TUNIT1", "keV")
-        tbhdu.header.update("TUNIT2", "deg")
-        tbhdu.header.update("TUNIT3", "deg")                
-
-        phfile = prefix+"_phlist.fits"
-
-        tbhdu.writeto(phfile, clobber=clobber)
-
-        col1 = pyfits.Column(name='SRC_ID', format='J', array=np.array([1]).astype("int32"))
-        col2 = pyfits.Column(name='RA', format='D', array=np.array([0.0]))
-        col3 = pyfits.Column(name='DEC', format='D', array=np.array([0.0]))
-        col4 = pyfits.Column(name='E_MIN', format='D', array=np.array([e_min]))
-        col5 = pyfits.Column(name='E_MAX', format='D', array=np.array([e_max]))
-        col6 = pyfits.Column(name='FLUX', format='D', array=np.array([flux]))
-        col7 = pyfits.Column(name='SPECTRUM', format='80A', array=np.array([phfile+"[PHLIST,1]"]))
-        col8 = pyfits.Column(name='IMAGE', format='80A', array=np.array([phfile+"[PHLIST,1]"]))
-                        
-        coldefs = pyfits.ColDefs([col1, col2, col3, col4, col5, col6, col7, col8])
-        
-        wrhdu = pyfits.new_table(coldefs)
-        wrhdu.update_ext_name("SRC_CAT")
-                                
-        wrhdu.header.update("HDUCLASS", "HEASARC")
-        wrhdu.header.update("HDUCLAS1", "SIMPUT")
-        wrhdu.header.update("HDUCLAS2", "SRC_CAT")        
-        wrhdu.header.update("HDUVERS", "1.1.0")
-        wrhdu.header.update("RADECSYS", "FK5")
-        wrhdu.header.update("EQUINOX", 2000.0)
-        wrhdu.header.update("TUNIT2", "deg")
-        wrhdu.header.update("TUNIT3", "deg")
-        wrhdu.header.update("TUNIT4", "keV")
-        wrhdu.header.update("TUNIT5", "keV")
-        wrhdu.header.update("TUNIT6", "erg/s/cm**2")
-
-        simputfile = prefix+"_simput.fits"
-                
-        wrhdu.writeto(simputfile, clobber=clobber)
-
-    def write_h5_file(self, h5file):
-        """
-        Write a XRayEventList to the HDF5 file given by h5file.
-        """
-        f = h5py.File(h5file, "w")
-
-        f.create_dataset("/exp_time", data=self.events["ExposureTime"])
-        f.create_dataset("/area", data=self.events["Area"])
-        f.create_dataset("/redshift", data=self.events["Redshift"])
-        f.create_dataset("/hubble", data=self.events["Hubble0"])
-        f.create_dataset("/omega_matter", data=self.events["OmegaMatter"])
-        f.create_dataset("/omega_lambda", data=self.events["OmegaLambda"])        
-        f.create_dataset("/xsky", data=self.events["xsky"])
-        f.create_dataset("/ysky", data=self.events["ysky"])
-        f.create_dataset("/eobs", data=self.events["eobs"])
-                        
-        f.close()
-
-    def write_fits_image(self, imagefile, width, nx, center,
-                         clobber=False, gzip_file=False,
-                         emin=None, emax=None):
-        
-        if emin is None:
-            mask_emin = np.ones((self.num_events), dtype='bool')
-        else:
-            mask_emin = self.events["eobs"] > emin
-        if emax is None:
-            mask_emax = np.ones((self.num_events), dtype='bool')
-        else:
-            mask_emax = self.events["eobs"] < emax
-
-        mask = np.logical_and(mask_emin, mask_emax)
-
-        dx_pixel = width/nx
-        xmin = -0.5*width
-        xmax = -xmin
-        xbins = np.linspace(xmin, xmax, nx+1, endpoint=True)
-        
-        H, xedges, yedges = np.histogram2d(self.events["xsky"][mask],
-                                           self.events["ysky"][mask],
-                                           bins=[xbins,xbins])
-        
-        hdu = pyfits.PrimaryHDU(H.T)
-
-        hdu.header.update("MTYPE1", "EQPOS")
-        hdu.header.update("MFORM1", "RA,DEC")
-        hdu.header.update("CTYPE1", "RA---TAN")
-        hdu.header.update("CTYPE2", "DEC--TAN")
-        hdu.header.update("CRPIX1", 0.5*(nx+1))
-        hdu.header.update("CRPIX2", 0.5*(nx+1))                
-        hdu.header.update("CRVAL1", center[0])
-        hdu.header.update("CRVAL2", center[1])
-        hdu.header.update("CUNIT1", "deg")
-        hdu.header.update("CUNIT2", "deg")
-        hdu.header.update("CDELT1", -dx_pixel/3600.)
-        hdu.header.update("CDELT2", dx_pixel/3600.)
-        hdu.header.update("EXPOSURE", self.events["ExposureTime"])
-        
-        hdu.writeto(imagefile, clobber=clobber)
-
-        if (gzip_file):
-            clob = ""
-            if (clobber) : clob="-f"
-            os.system("gzip "+clob+" %s.fits" % (prefix))
-                                    
-    def write_spectrum(self, specfile, emin, emax, nchan, clobber=False):
-        
-        spec, ee = np.histogram(self.events["eobs"], bins=nchan, range=(emin, emax))
-
-        de = ee[1]-ee[0]
-        emid = 0.5*(ee[1:]+ee[:-1])
-        
-        col1 = pyfits.Column(name='ENERGY', format='1E', array=emid)
-        col2 = pyfits.Column(name='COUNTS', format='1E', array=spec)
-        
-        coldefs = pyfits.ColDefs([col1, col2])
-        
-        tbhdu = pyfits.new_table(coldefs)
-        
-        tbhdu.writeto(specfile, clobber=clobber)


https://bitbucket.org/yt_analysis/yt/commits/afcd50e4fe64/
Changeset:   afcd50e4fe64
Branch:      yt
User:        jzuhone
Date:        2013-08-13 18:27:08
Summary:     1) Some cosmetic changes.
2) Some more docstrings.
3) PSF smoothing should only happen after photons are projected. Optionally allow user to specify sigma of PSF.
4) Some beginnings of parallelization.
Affected #:  1 file

diff -r 731d70670f4d7a160ac0f7b9aae1b7c2af4e44e8 -r afcd50e4fe64e023c3b1e290ed2b424170840f41 yt/analysis_modules/synthetic_xray_obs/photon_simulator.py
--- a/yt/analysis_modules/synthetic_xray_obs/photon_simulator.py
+++ b/yt/analysis_modules/synthetic_xray_obs/photon_simulator.py
@@ -1,9 +1,11 @@
 import numpy as np
 from yt.funcs import *
-from yt.utilities.physical_constants import mp, clight, cm_per_kpc, cm_per_mpc, \
-     cm_per_km, K_per_keV, erg_per_keV
+from yt.utilities.physical_constants import mp, clight, cm_per_kpc, \
+     cm_per_mpc, cm_per_km, K_per_keV, erg_per_keV
 from yt.utilities.cosmology import Cosmology
 from yt.utilities.orientation import Orientation
+from yt.utilities.parallel_tools.parallel_analysis_interface import \
+     parallel_objects
 from yt.data_objects.api import add_field
 
 import os
@@ -15,7 +17,8 @@
     try:
         import astropy.io.fits as pyfits
     except ImportError:
-        mylog.warning("You don't have pyFITS installed. Writing to and reading from FITS files won't be available.")
+        mylog.warning("You don't have pyFITS installed. " + 
+                      "Writing to and reading from FITS files won't be available.")
     
 N_TBIN = 10000
 TMIN = 8.08e-2
@@ -78,13 +81,18 @@
         return cls(photons)
 
     @classmethod
-    def from_scratch(cls, cell_data, redshift, eff_A, exp_time, emission_model,
-                     center="c", X_H=0.75, Zmet=0.3, cosmology=None):
+    def from_scratch(cls, cell_data, redshift, eff_A,
+                     exp_time, emission_model, center="c",
+                     X_H=0.75, Zmet=0.3, cosmology=None):
         """
         Initialize a XRayPhotonList from a data container. 
         """
         pf = cell_data.pf
 
+        comm = communication_system.communicators[-1]
+        my_rank = comm.rank
+        my_size = comm.size
+        
         vol_scale = pf.units["cm"]**(-3)/np.prod(pf.domain_width)
 
         if cosmology is None:
@@ -133,9 +141,8 @@
         n = int(0)
             
         for i,ikT in enumerate(kT_idxs):
-            
+
             ncells = int(bcounts[i])
-            
             kT = kT_bins[ikT] + 0.5*dkT
             
             em_sum = cell_em[n:n+ncells].sum()
@@ -162,13 +169,13 @@
                     eidxs = np.searchsorted(counts, randvec)-1
                     cell_e = emid[eidxs]+de*(randvec-counts[eidxs])/(counts[eidxs+1]-counts[eidxs])
                     energies.append(cell_e)
-
+            
                 pbar.update(icell)
                 
             n += ncells
             
         pbar.finish()
-                        
+            
         active_cells = np.where(number_of_photons > 0)[0]
         num_active_cells = len(active_cells)
 
@@ -190,7 +197,7 @@
         photons["OmegaMatter"] = cosmo.OmegaMatterNow
         photons["OmegaLambda"] = cosmo.OmegaLambdaNow
         photons["AngularDiameterDistance"] = D_A
-        
+
         return cls(photons)
             
     def write_h5_file(self, photonfile):
@@ -222,9 +229,9 @@
         f.close()
     
     def project_photons(self, L, area_new=None, texp_new=None, 
-                        absorb_model=None, smoothing=False):
+                        absorb_model=None, psf_sigma=None):
         """
-        Project photons 
+        Projects photons onto an image plane given a line of sight. 
         """
         dx = self.photons["dx"]
         
@@ -239,6 +246,8 @@
         y_hat = orient.unit_vectors[1]
         z_hat = orient.unit_vectors[2]
 
+        D_A = self.photons["AngularDiameterDistance"] / cm_per_kpc
+        
         n_ph = self.photons["NumberOfPhotons"]
         num_cells = len(n_ph)
         n_ph_tot = n_ph.sum()
@@ -274,15 +283,10 @@
 
         mylog.info("Total number of photons to use: %d" % (n_obs_tot))
 
-        if smoothing:
-            x = np.random.normal(scale=0.5,size=n_obs_tot)
-            y = np.random.normal(scale=0.5,size=n_obs_tot)
-            z = np.random.normal(scale=0.5,size=n_obs_tot)
-        else:
-            x = np.random.uniform(low=-0.5,high=0.5,size=n_obs_tot)
-            y = np.random.uniform(low=-0.5,high=0.5,size=n_obs_tot)
-            z = np.random.uniform(low=-0.5,high=0.5,size=n_obs_tot)
-
+        x = np.random.uniform(low=-0.5,high=0.5,size=n_obs_tot)
+        y = np.random.uniform(low=-0.5,high=0.5,size=n_obs_tot)
+        z = np.random.uniform(low=-0.5,high=0.5,size=n_obs_tot)
+                    
         vz = self.photons["vx"]*z_hat[0] + \
              self.photons["vy"]*z_hat[1] + \
              self.photons["vz"]*z_hat[2]
@@ -296,9 +300,7 @@
         else:
             idxs = np.random.choice(n_ph_tot, size=n_obs_tot, replace=False)
             obs_cells = cells[idxs]
-        x *= dx[obs_cells]
-        y *= dx[obs_cells]
-        z *= dx[obs_cells]
+
         x += self.photons["x"][obs_cells]
         y += self.photons["y"][obs_cells]
         z += self.photons["z"][obs_cells]  
@@ -337,16 +339,20 @@
         
         all_obs = np.logical_and(not_abs, detected)
 
-        mylog.info("Total number of observed photons: %d" % (all_obs.sum()))
+        num_events = all_obs.sum()
+        
+        mylog.info("Total number of observed photons: %d" % (num_events))
                     
-        D_A = self.photons["AngularDiameterDistance"] / cm_per_kpc
-
         events = {}
 
         events["xsky"] = np.rad2deg(xsky[all_obs]/D_A)*3600.
         events["ysky"] = np.rad2deg(ysky[all_obs]/D_A)*3600.
         events["eobs"] = eobs[all_obs]
 
+        if psf_sigma is not None:
+            events["xsky"] += np.random.normal(sigma=psf_sigma)
+            events["ysky"] += np.random.normal(sigma=psf_sigma)
+            
         if texp_new is None:
             events["ExposureTime"] = self.photons["FiducialExposureTime"]
         else:
@@ -418,6 +424,9 @@
         
         return cls(events)
 
+    def convolve_with_response(self, respfile):
+        pass
+    
     def write_fits_file(self, fitsfile, clobber=False):
         """
         Write events to a FITS binary table file.
@@ -442,7 +451,9 @@
         tbhdu.writeto(fitsfile, clobber=clobber)
                 
     def write_simput_file(self, prefix, clobber=False, e_min=None, e_max=None):
-
+        """
+        Write events to a SIMPUT file.
+        """
         if e_min is None:
             e_min = 0.0
         if e_max is None:
@@ -528,7 +539,9 @@
     def write_fits_image(self, imagefile, width, nx, center,
                          clobber=False, gzip_file=False,
                          emin=None, emax=None):
-        
+        """
+        Generate a image by binning X-ray counts and write it to a FITS file.
+        """
         if emin is None:
             mask_emin = np.ones((self.num_events), dtype='bool')
         else:


https://bitbucket.org/yt_analysis/yt/commits/5205413667f8/
Changeset:   5205413667f8
Branch:      yt
User:        jzuhone
Date:        2013-08-19 19:04:04
Summary:     Three bugs:

1) Didn't import communication_system
2) The number of observed photons should be determined by the fraction over all, not by cell.
   Otherwise we get roundoff errors.
3) Took out the dx's by accident in project_photons
Affected #:  2 files

diff -r afcd50e4fe64e023c3b1e290ed2b424170840f41 -r 5205413667f8a2a66d03976a8bfb952d179feee8 yt/analysis_modules/synthetic_xray_obs/photon_models.py
--- a/yt/analysis_modules/synthetic_xray_obs/photon_models.py
+++ b/yt/analysis_modules/synthetic_xray_obs/photon_models.py
@@ -14,6 +14,7 @@
         self.emax = emax
         self.nchan = nchan
         self.ebins = np.linspace(emin, emax, nchan+1)
+        self.de = self.ebins[1]-self.ebins[0]
         
     def prepare(self):
         pass

diff -r afcd50e4fe64e023c3b1e290ed2b424170840f41 -r 5205413667f8a2a66d03976a8bfb952d179feee8 yt/analysis_modules/synthetic_xray_obs/photon_simulator.py
--- a/yt/analysis_modules/synthetic_xray_obs/photon_simulator.py
+++ b/yt/analysis_modules/synthetic_xray_obs/photon_simulator.py
@@ -5,7 +5,7 @@
 from yt.utilities.cosmology import Cosmology
 from yt.utilities.orientation import Orientation
 from yt.utilities.parallel_tools.parallel_analysis_interface import \
-     parallel_objects
+     communication_system
 from yt.data_objects.api import add_field
 
 import os
@@ -255,7 +255,7 @@
         eff_area = None
         
         if texp_new is None and area_new is None:
-            n_obs = n_ph
+            n_obs_tot = n_ph_tot
         else:
             if texp_new is None:
                 Tratio = 1.
@@ -277,10 +277,8 @@
             fak = Aratio*Tratio
             if fak > 1:
                 raise ValueError("Spectrum scaling factor = %g, cannot be greater than unity." % (fak))
-            n_obs = np.uint64(n_ph*fak)
-                            
-        n_obs_tot = n_obs.sum()
-
+            n_obs_tot = np.uint64(n_ph_tot*fak)
+            
         mylog.info("Total number of photons to use: %d" % (n_obs_tot))
 
         x = np.random.uniform(low=-0.5,high=0.5,size=n_obs_tot)
@@ -301,6 +299,9 @@
             idxs = np.random.choice(n_ph_tot, size=n_obs_tot, replace=False)
             obs_cells = cells[idxs]
 
+        x *= dx[obs_cells]
+        y *= dx[obs_cells]
+        z *= dx[obs_cells]
         x += self.photons["x"][obs_cells]
         y += self.photons["y"][obs_cells]
         z += self.photons["z"][obs_cells]  


https://bitbucket.org/yt_analysis/yt/commits/f46dfdc34a29/
Changeset:   f46dfdc34a29
Branch:      yt
User:        jzuhone
Date:        2013-08-21 19:18:47
Summary:     Merged yt_analysis/yt into yt
Affected #:  40 files

diff -r 5205413667f8a2a66d03976a8bfb952d179feee8 -r f46dfdc34a292631bd520fb3af8c878cec975ba1 MANIFEST.in
--- a/MANIFEST.in
+++ b/MANIFEST.in
@@ -1,3 +1,4 @@
 include distribute_setup.py README* CREDITS FUNDING LICENSE.txt
 recursive-include yt/gui/reason/html *.html *.png *.ico *.js
 recursive-include yt *.pyx *.pxd *.hh *.h README*
+recursive-include yt/utilities/kdtree *.f90 *.v Makefile LICENSE
\ No newline at end of file

diff -r 5205413667f8a2a66d03976a8bfb952d179feee8 -r f46dfdc34a292631bd520fb3af8c878cec975ba1 doc/install_script.sh
--- a/doc/install_script.sh
+++ b/doc/install_script.sh
@@ -473,11 +473,18 @@
 function do_setup_py
 {
     [ -e $1/done ] && return
-    echo "Installing $1 (arguments: '$*')"
-    [ ! -e $1/extracted ] && tar xfz $1.tar.gz
-    touch $1/extracted
-    cd $1
-    if [ ! -z `echo $1 | grep h5py` ]
+    LIB=$1
+    shift
+    if [ -z "$@" ]
+    then
+        echo "Installing $LIB"
+    else
+        echo "Installing $LIB (arguments: '$@')"
+    fi
+    [ ! -e $LIB/extracted ] && tar xfz $LIB.tar.gz
+    touch $LIB/extracted
+    cd $LIB
+    if [ ! -z `echo $LIB | grep h5py` ]
     then
         shift
 	( ${DEST_DIR}/bin/python2.7 setup.py build --hdf5=${HDF5_DIR} $* 2>&1 ) 1>> ${LOG_FILE} || do_exit
@@ -519,8 +526,8 @@
 
 function get_ytproject
 {
+    [ -e $1 ] && return
     echo "Downloading $1 from yt-project.org"
-    [ -e $1 ] && return
     ${GETFILE} "http://yt-project.org/dependencies/$1" || do_exit
     ( ${SHASUM} -c $1.sha512 2>&1 ) 1>> ${LOG_FILE} || do_exit
 }
@@ -551,67 +558,93 @@
 mkdir -p ${DEST_DIR}/src
 cd ${DEST_DIR}/src
 
+CYTHON='Cython-0.19.1'
+FORTHON='Forthon-0.8.11'
+PYX='PyX-0.12.1'
+PYTHON='Python-2.7.5'
+BZLIB='bzip2-1.0.6'
+FREETYPE_VER='freetype-2.4.12'
+H5PY='h5py-2.1.3'
+HDF5='hdf5-1.8.11'
+IPYTHON='ipython-1.0.0'
+LAPACK='lapack-3.4.2'
+PNG=libpng-1.6.3
+MATPLOTLIB='matplotlib-1.3.0'
+MERCURIAL='mercurial-2.7'
+NOSE='nose-1.3.0'
+NUMPY='numpy-1.7.1'
+PYTHON_HGLIB='python-hglib-1.0'
+PYZMQ='pyzmq-13.1.0'
+ROCKSTAR='rockstar-0.99.6'
+SCIPY='scipy-0.12.0'
+SQLITE='sqlite-autoconf-3071700'
+SYMPY='sympy-0.7.3'
+TORNADO='tornado-3.1'
+ZEROMQ='zeromq-3.2.3'
+ZLIB='zlib-1.2.8'
+
 # Now we dump all our SHA512 files out.
-echo 'fb85d71bb4f80b35f0d0f1735c650dd75c5f84b05635ddf91d6241ff103b5a49158c5b851a20c15e05425f6dde32a4971b35fcbd7445f61865b4d61ffd1fbfa1  Cython-0.18.tar.gz' > Cython-0.18.tar.gz.sha512
+echo '9dcdda5b2ee2e63c2d3755245b7b4ed2f4592455f40feb6f8e86503195d9474559094ed27e789ab1c086d09da0bb21c4fe844af0e32a7d47c81ff59979b18ca0  Cython-0.19.1.tar.gz' > Cython-0.19.1.tar.gz.sha512
+echo '3f53d0b474bfd79fea2536d0a9197eaef6c0927e95f2f9fd52dbd6c1d46409d0e649c21ac418d8f7767a9f10fe6114b516e06f2be4b06aec3ab5bdebc8768220  Forthon-0.8.11.tar.gz' > Forthon-0.8.11.tar.gz.sha512
 echo '4941f5aa21aff3743546495fb073c10d2657ff42b2aff401903498638093d0e31e344cce778980f28a7170c6d29eab72ac074277b9d4088376e8692dc71e55c1  PyX-0.12.1.tar.gz' > PyX-0.12.1.tar.gz.sha512
-echo '3349152c47ed2b63c5c9aabcfa92b8497ea9d71ca551fd721e827fcb8f91ff9fbbee6bba8f8cb2dea185701b8798878b4b2435c1496b63d4b4a37c624a625299  Python-2.7.4.tgz' > Python-2.7.4.tgz.sha512
+echo 'd6580eb170b36ad50f3a30023fe6ca60234156af91ccb3971b0b0983119b86f3a9f6c717a515c3c6cb72b3dcbf1d02695c6d0b92745f460b46a3defd3ff6ef2f  Python-2.7.5.tgz' > Python-2.7.5.tgz.sha512
+echo '172f2bc671145ebb0add2669c117863db35851fb3bdb192006cd710d4d038e0037497eb39a6d01091cb923f71a7e8982a77b6e80bf71d6275d5d83a363c8d7e5  rockstar-0.99.6.tar.gz' > rockstar-0.99.6.tar.gz.sha512
+echo '276bd9c061ec9a27d478b33078a86f93164ee2da72210e12e2c9da71dcffeb64767e4460b93f257302b09328eda8655e93c4b9ae85e74472869afbeae35ca71e  blas.tar.gz' > blas.tar.gz.sha512
 echo '00ace5438cfa0c577e5f578d8a808613187eff5217c35164ffe044fbafdfec9e98f4192c02a7d67e01e5a5ccced630583ad1003c37697219b0f147343a3fdd12  bzip2-1.0.6.tar.gz' > bzip2-1.0.6.tar.gz.sha512
 echo 'a296dfcaef7e853e58eed4e24b37c4fa29cfc6ac688def048480f4bb384b9e37ca447faf96eec7b378fd764ba291713f03ac464581d62275e28eb2ec99110ab6  reason-js-20120623.zip' > reason-js-20120623.zip.sha512
-echo 'b46c93d76f8ce09c94765b20b2eeadf71207671f1131777de178b3727c235b4dd77f6e60d62442b96648c3c6749e9e4c1194c1b02af7e946576be09e1ff7ada3  freetype-2.4.11.tar.gz' > freetype-2.4.11.tar.gz.sha512
-echo '15ca0209e8d8f172cb0708a2de946fbbde8551d9bebc4a95fa7ae31558457a7f43249d5289d7675490c577deb4e0153698fd2407644078bf30bd5ab10135fce3  h5py-2.1.2.tar.gz' > h5py-2.1.2.tar.gz.sha512
-echo 'c68a425bacaa7441037910b9166f25b89e1387776a7749a5350793f89b1690350df5f018060c31d03686e7c3ed2aa848bd2b945c96350dc3b6322e087934783a  hdf5-1.8.9.tar.gz' > hdf5-1.8.9.tar.gz.sha512
-echo 'b2b53ed358bacab9e8d63a51f17bd5f121ece60a1d7c53e8a8eb08ad8b1e4393a8d7a86eec06e2efc62348114f0d84c0a3dfc805e68e6edd93b20401962b3554  libpng-1.6.1.tar.gz' > libpng-1.6.1.tar.gz.sha512
-echo '497f91725eaf361bdb9bdf38db2bff5068a77038f1536df193db64c9b887e3b0d967486daee722eda6e2c4e60f034eee030673e53d07bf0db0f3f7c0ef3bd208  matplotlib-1.2.1.tar.gz' > matplotlib-1.2.1.tar.gz.sha512
-echo '928fdeaaf0eaec80adbd8765521de9666ab56aaa2101fb9ab2cb392d8b29475d3b052d89652ff9b67522cfcc6cd958717ac715f51b0573ee088e9a595f29afe2  mercurial-2.5.4.tar.gz' > mercurial-2.5.4.tar.gz.sha512
-echo 'a485daa556f6c76003de1dbb3e42b3daeee0a320c69c81b31a7d2ebbc2cf8ab8e96c214a4758e5e7bf814295dc1d6aa563092b714db7e719678d8462135861a8  numpy-1.7.0.tar.gz' > numpy-1.7.0.tar.gz.sha512
-echo '293d78d14a9347cb83e1a644e5f3e4447ed6fc21642c51683e5495dda08d2312194a73d1fc3c1d78287e33ed065aa251ecbaa7c0ea9189456c1702e96d78becd  sqlite-autoconf-3071601.tar.gz' > sqlite-autoconf-3071601.tar.gz.sha512
-echo 'b1c073ad26684e354f7c522c14655840592e03872bc0a94690f89cae2ff88f146fce1dad252ff27a889dac4a32ff9f8ab63ba940671f9da89e9ba3e19f1bf58d  zlib-1.2.7.tar.gz' > zlib-1.2.7.tar.gz.sha512
-echo '05ac335727a2c3036f31a2506fdd2615aa436bfbe2f81799fe6c51bffe2591ad6a8427f3b25c34e7e709fb4e7607a0589dc7a22185c1f9b894e90de6711a88aa  ipython-0.13.1.tar.gz' > ipython-0.13.1.tar.gz.sha512
-echo 'b9d061ca49e54ea917e0aed2b2a48faef33061dbf6d17eae7f8c3fff0b35ca883e7324f6cb24bda542443f669dcd5748037a5f2309f4c359d68adef520894865  zeromq-3.2.2.tar.gz' > zeromq-3.2.2.tar.gz.sha512
-echo '852fce8a8308c4e1e4b19c77add2b2055ca2ba570b28e8364888df490af92b860c72e860adfb075b3405a9ceb62f343889f20a8711c9353a7d9059adee910f83  pyzmq-13.0.2.tar.gz' > pyzmq-13.0.2.tar.gz.sha512
-echo '303bd3fbea22be57fddf7df78ddf5a783d355a0c8071b1363250daafc20232ddd28eedc44aa1194f4a7afd82f9396628c5bb06819e02b065b6a1b1ae8a7c19e1  tornado-3.0.tar.gz' > tornado-3.0.tar.gz.sha512
-echo '3f53d0b474bfd79fea2536d0a9197eaef6c0927e95f2f9fd52dbd6c1d46409d0e649c21ac418d8f7767a9f10fe6114b516e06f2be4b06aec3ab5bdebc8768220  Forthon-0.8.11.tar.gz' > Forthon-0.8.11.tar.gz.sha512
-echo 'c13116c1f0547000cc565e15774687b9e884f8b74fb62a84e578408a868a84961704839065ae4f21b662e87f2aaedf6ea424ea58dfa9d3d73c06281f806d15dd  nose-1.2.1.tar.gz' > nose-1.2.1.tar.gz.sha512
-echo 'd67de9567256e6f1649e4f3f7dfee63371d5f00fd3fd4f92426198f862e97c57f70e827d19f4e5e1929ad85ef2ce7aa5a0596b101cafdac71672e97dc115b397  python-hglib-0.3.tar.gz' > python-hglib-0.3.tar.gz.sha512
-echo 'ffc602eb346717286b3d0a6770c60b03b578b3cf70ebd12f9e8b1c8c39cdb12ef219ddaa041d7929351a6b02dbb8caf1821b5452d95aae95034cbf4bc9904a7a  sympy-0.7.2.tar.gz' > sympy-0.7.2.tar.gz.sha512
+echo '609a68a3675087e0cc95268574f31e104549daa48efe15a25a33b8e269a93b4bd160f4c3e8178dca9c950ef5ca514b039d6fd1b45db6af57f25342464d0429ce  freetype-2.4.12.tar.gz' > freetype-2.4.12.tar.gz.sha512
+echo '2eb7030f8559ff5cb06333223d98fda5b3a663b6f4a026949d1c423aa9a869d824e612ed5e1851f3bf830d645eea1a768414f73731c23ab4d406da26014fe202  h5py-2.1.3.tar.gz' > h5py-2.1.3.tar.gz.sha512
+echo 'e9db26baa297c8ed10f1ca4a3fcb12d6985c6542e34c18d48b2022db73014f054c8b8434f3df70dcf44631f38b016e8050701d52744953d0fced3272d7b6b3c1  hdf5-1.8.11.tar.gz' > hdf5-1.8.11.tar.gz.sha512
+echo '1b309c08009583e66d1725a2d2051e6de934db246129568fa6d5ba33ad6babd3b443e7c2782d817128d2b112e21bcdd71e66be34fbd528badd900f1d0ed3db56  ipython-1.0.0.tar.gz' > ipython-1.0.0.tar.gz.sha512
+echo '8770214491e31f0a7a3efaade90eee7b0eb20a8a6ab635c5f854d78263f59a1849133c14ef5123d01023f0110cbb9fc6f818da053c01277914ae81473430a952  lapack-3.4.2.tar.gz' > lapack-3.4.2.tar.gz.sha512
+echo '887582e5a22e4cde338aa8fec7a89f6dd31f2f02b8842735f00f970f64582333fa03401cea6d01704083403c7e8b7ebc26655468ce930165673b33efa4bcd586  libpng-1.6.3.tar.gz' > libpng-1.6.3.tar.gz.sha512
+echo '990e3a155ca7a9d329c41a43b44a9625f717205e81157c668a8f3f2ad5459ed3fed8c9bd85e7f81c509e0628d2192a262d4aa30c8bfc348bb67ed60a0362505a  matplotlib-1.3.0.tar.gz' > matplotlib-1.3.0.tar.gz.sha512
+echo 'e425778edb0f71c34e719e04561ee3de37feaa1be4d60b94c780aebdbe6d41f8f4ab15103a8bbe8894ebeb228c42f0e2cd41b8db840f8384e1cd7cd2d5b67b97  mercurial-2.7.tar.gz' > mercurial-2.7.tar.gz.sha512
+echo 'a3b8060e415560a868599224449a3af636d24a060f1381990b175dcd12f30249edd181179d23aea06b0c755ff3dc821b7a15ed8840f7855530479587d4d814f4  nose-1.3.0.tar.gz' > nose-1.3.0.tar.gz.sha512
+echo 'd58177f3971b6d07baf6f81a2088ba371c7e43ea64ee7ada261da97c6d725b4bd4927122ac373c55383254e4e31691939276dab08a79a238bfa55172a3eff684  numpy-1.7.1.tar.gz' > numpy-1.7.1.tar.gz.sha512
+echo '9c0a61299779aff613131aaabbc255c8648f0fa7ab1806af53f19fbdcece0c8a68ddca7880d25b926d67ff1b9201954b207919fb09f6a290acb078e8bbed7b68  python-hglib-1.0.tar.gz' > python-hglib-1.0.tar.gz.sha512
+echo 'c65013293dd4049af5db009fdf7b6890a3c6b1e12dd588b58fb5f5a5fef7286935851fb7a530e03ea16f28de48b964e50f48bbf87d34545fd23b80dd4380476b  pyzmq-13.1.0.tar.gz' > pyzmq-13.1.0.tar.gz.sha512
 echo '172f2bc671145ebb0add2669c117863db35851fb3bdb192006cd710d4d038e0037497eb39a6d01091cb923f71a7e8982a77b6e80bf71d6275d5d83a363c8d7e5  rockstar-0.99.6.tar.gz' > rockstar-0.99.6.tar.gz.sha512
-echo 'd4fdd62f2db5285cd133649bd1bfa5175cb9da8304323abd74e0ef1207d55e6152f0f944da1da75f73e9dafb0f3bb14efba3c0526c732c348a653e0bd223ccfa  scipy-0.11.0.tar.gz' > scipy-0.11.0.tar.gz.sha512
-echo '276bd9c061ec9a27d478b33078a86f93164ee2da72210e12e2c9da71dcffeb64767e4460b93f257302b09328eda8655e93c4b9ae85e74472869afbeae35ca71e  blas.tar.gz' > blas.tar.gz.sha512
-echo '8770214491e31f0a7a3efaade90eee7b0eb20a8a6ab635c5f854d78263f59a1849133c14ef5123d01023f0110cbb9fc6f818da053c01277914ae81473430a952  lapack-3.4.2.tar.gz' > lapack-3.4.2.tar.gz.sha512
+echo '80c8e137c3ccba86575d4263e144ba2c4684b94b5cd620e200f094c92d4e118ea6a631d27bdb259b0869771dfaeeae68c0fdd37fdd740b9027ee185026e921d4  scipy-0.12.0.tar.gz' > scipy-0.12.0.tar.gz.sha512
+echo '96f3e51b46741450bc6b63779c10ebb4a7066860fe544385d64d1eda52592e376a589ef282ace2e1df73df61c10eab1a0d793abbdaf770e60289494d4bf3bcb4  sqlite-autoconf-3071700.tar.gz' > sqlite-autoconf-3071700.tar.gz.sha512
+echo '2992baa3edfb4e1842fb642abf0bf0fc0bf56fc183aab8fed6b3c42fbea928fa110ede7fdddea2d63fc5953e8d304b04da433dc811134fadefb1eecc326121b8  sympy-0.7.3.tar.gz' > sympy-0.7.3.tar.gz.sha512
+echo '101544db6c97beeadc5a02b2ef79edefa0a07e129840ace2e4aa451f3976002a273606bcdc12d6cef5c22ff4c1c9dcf60abccfdee4cbef8e3f957cd25c0430cf  tornado-3.1.tar.gz' > tornado-3.1.tar.gz.sha512
+echo '34ffb6aa645f62bd1158a8f2888bf92929ccf90917a6c50ed51ed1240732f498522e164d1536f26480c87ad5457fe614a93bf0e15f2f89b0b168e64a30de68ca  zeromq-3.2.3.tar.gz' > zeromq-3.2.3.tar.gz.sha512
+echo 'ece209d4c7ec0cb58ede791444dc754e0d10811cbbdebe3df61c0fd9f9f9867c1c3ccd5f1827f847c005e24eef34fb5bf87b5d3f894d75da04f1797538290e4a  zlib-1.2.8.tar.gz' > zlib-1.2.8.tar.gz.sha512
 # Individual processes
-[ -z "$HDF5_DIR" ] && get_ytproject hdf5-1.8.9.tar.gz
-[ $INST_ZLIB -eq 1 ] && get_ytproject zlib-1.2.7.tar.gz
-[ $INST_BZLIB -eq 1 ] && get_ytproject bzip2-1.0.6.tar.gz
-[ $INST_PNG -eq 1 ] && get_ytproject libpng-1.6.1.tar.gz
-[ $INST_FTYPE -eq 1 ] && get_ytproject freetype-2.4.11.tar.gz
-[ $INST_SQLITE3 -eq 1 ] && get_ytproject sqlite-autoconf-3071601.tar.gz
-[ $INST_PYX -eq 1 ] && get_ytproject PyX-0.12.1.tar.gz
-[ $INST_0MQ -eq 1 ] && get_ytproject zeromq-3.2.2.tar.gz
-[ $INST_0MQ -eq 1 ] && get_ytproject pyzmq-13.0.2.tar.gz
-[ $INST_0MQ -eq 1 ] && get_ytproject tornado-3.0.tar.gz
-[ $INST_SCIPY -eq 1 ] && get_ytproject scipy-0.11.0.tar.gz
+[ -z "$HDF5_DIR" ] && get_ytproject $HDF5.tar.gz
+[ $INST_ZLIB -eq 1 ] && get_ytproject $ZLIB.tar.gz
+[ $INST_BZLIB -eq 1 ] && get_ytproject $BZLIB.tar.gz
+[ $INST_PNG -eq 1 ] && get_ytproject $PNG.tar.gz
+[ $INST_FTYPE -eq 1 ] && get_ytproject $FREETYPE_VER.tar.gz
+[ $INST_SQLITE3 -eq 1 ] && get_ytproject $SQLITE.tar.gz
+[ $INST_PYX -eq 1 ] && get_ytproject $PYX.tar.gz
+[ $INST_0MQ -eq 1 ] && get_ytproject $ZEROMQ.tar.gz
+[ $INST_0MQ -eq 1 ] && get_ytproject $PYZMQ.tar.gz
+[ $INST_0MQ -eq 1 ] && get_ytproject $TORNADO.tar.gz
+[ $INST_SCIPY -eq 1 ] && get_ytproject $SCIPY.tar.gz
 [ $INST_SCIPY -eq 1 ] && get_ytproject blas.tar.gz
-[ $INST_SCIPY -eq 1 ] && get_ytproject lapack-3.4.2.tar.gz
-get_ytproject Python-2.7.4.tgz
-get_ytproject numpy-1.7.0.tar.gz
-get_ytproject matplotlib-1.2.1.tar.gz
-get_ytproject mercurial-2.5.4.tar.gz
-get_ytproject ipython-0.13.1.tar.gz
-get_ytproject h5py-2.1.2.tar.gz
-get_ytproject Cython-0.18.tar.gz
+[ $INST_SCIPY -eq 1 ] && get_ytproject $LAPACK.tar.gz
+get_ytproject $PYTHON.tgz
+get_ytproject $NUMPY.tar.gz
+get_ytproject $MATPLOTLIB.tar.gz
+get_ytproject $MERCURIAL.tar.gz
+get_ytproject $IPYTHON.tar.gz
+get_ytproject $H5PY.tar.gz
+get_ytproject $CYTHON.tar.gz
 get_ytproject reason-js-20120623.zip
-get_ytproject Forthon-0.8.11.tar.gz
-get_ytproject nose-1.2.1.tar.gz
-get_ytproject python-hglib-0.3.tar.gz
-get_ytproject sympy-0.7.2.tar.gz
-get_ytproject rockstar-0.99.6.tar.gz
+get_ytproject $FORTHON.tar.gz
+get_ytproject $NOSE.tar.gz
+get_ytproject $PYTHON_HGLIB.tar.gz
+get_ytproject $SYMPY.tar.gz
+get_ytproject $ROCKSTAR.tar.gz
 if [ $INST_BZLIB -eq 1 ]
 then
-    if [ ! -e bzip2-1.0.6/done ]
+    if [ ! -e $BZLIB/done ]
     then
-        [ ! -e bzip2-1.0.6 ] && tar xfz bzip2-1.0.6.tar.gz
+        [ ! -e $BZLIB ] && tar xfz $BZLIB.tar.gz
         echo "Installing BZLIB"
-        cd bzip2-1.0.6
+        cd $BZLIB
         if [ `uname` = "Darwin" ]
         then
             if [ -z "${CC}" ]
@@ -634,11 +667,11 @@
 
 if [ $INST_ZLIB -eq 1 ]
 then
-    if [ ! -e zlib-1.2.7/done ]
+    if [ ! -e $ZLIB/done ]
     then
-        [ ! -e zlib-1.2.7 ] && tar xfz zlib-1.2.7.tar.gz
+        [ ! -e $ZLIB ] && tar xfz $ZLIB.tar.gz
         echo "Installing ZLIB"
-        cd zlib-1.2.7
+        cd $ZLIB
         ( ./configure --shared --prefix=${DEST_DIR}/ 2>&1 ) 1>> ${LOG_FILE} || do_exit
         ( make install 2>&1 ) 1>> ${LOG_FILE} || do_exit
         ( make clean 2>&1) 1>> ${LOG_FILE} || do_exit
@@ -652,11 +685,11 @@
 
 if [ $INST_PNG -eq 1 ]
 then
-    if [ ! -e libpng-1.6.1/done ]
+    if [ ! -e $PNG/done ]
     then
-        [ ! -e libpng-1.6.1 ] && tar xfz libpng-1.6.1.tar.gz
+        [ ! -e $PNG ] && tar xfz $PNG.tar.gz
         echo "Installing PNG"
-        cd libpng-1.6.1
+        cd $PNG
         ( ./configure CPPFLAGS=-I${DEST_DIR}/include CFLAGS=-I${DEST_DIR}/include --prefix=${DEST_DIR}/ 2>&1 ) 1>> ${LOG_FILE} || do_exit
         ( make install 2>&1 ) 1>> ${LOG_FILE} || do_exit
         ( make clean 2>&1) 1>> ${LOG_FILE} || do_exit
@@ -670,13 +703,14 @@
 
 if [ $INST_FTYPE -eq 1 ]
 then
-    if [ ! -e freetype-2.4.11/done ]
+    if [ ! -e $FREETYPE_VER/done ]
     then
-        [ ! -e freetype-2.4.11 ] && tar xfz freetype-2.4.11.tar.gz
+        [ ! -e $FREETYPE_VER ] && tar xfz $FREETYPE_VER.tar.gz
         echo "Installing FreeType2"
-        cd freetype-2.4.11
+        cd $FREETYPE_VER
         ( ./configure CFLAGS=-I${DEST_DIR}/include --prefix=${DEST_DIR}/ 2>&1 ) 1>> ${LOG_FILE} || do_exit
-        ( make install 2>&1 ) 1>> ${LOG_FILE} || do_exit
+        ( make 2>&1 ) 1>> ${LOG_FILE} || do_exit
+		( make install 2>&1 ) 1>> ${LOG_FILE} || do_exit
         ( make clean 2>&1) 1>> ${LOG_FILE} || do_exit
         touch done
         cd ..
@@ -688,11 +722,11 @@
 
 if [ -z "$HDF5_DIR" ]
 then
-    if [ ! -e hdf5-1.8.9/done ]
+    if [ ! -e $HDF5/done ]
     then
-        [ ! -e hdf5-1.8.9 ] && tar xfz hdf5-1.8.9.tar.gz
+        [ ! -e $HDF5 ] && tar xfz $HDF5.tar.gz
         echo "Installing HDF5"
-        cd hdf5-1.8.9
+        cd $HDF5
         ( ./configure --prefix=${DEST_DIR}/ --enable-shared 2>&1 ) 1>> ${LOG_FILE} || do_exit
         ( make ${MAKE_PROCS} install 2>&1 ) 1>> ${LOG_FILE} || do_exit
         ( make clean 2>&1) 1>> ${LOG_FILE} || do_exit
@@ -707,11 +741,11 @@
 
 if [ $INST_SQLITE3 -eq 1 ]
 then
-    if [ ! -e sqlite-autoconf-3071601/done ]
+    if [ ! -e $SQLITE/done ]
     then
-        [ ! -e sqlite-autoconf-3071601 ] && tar xfz sqlite-autoconf-3071601.tar.gz
+        [ ! -e $SQLITE ] && tar xfz $SQLITE.tar.gz
         echo "Installing SQLite3"
-        cd sqlite-autoconf-3071601
+        cd $SQLITE
         ( ./configure --prefix=${DEST_DIR}/ 2>&1 ) 1>> ${LOG_FILE} || do_exit
         ( make ${MAKE_PROCS} install 2>&1 ) 1>> ${LOG_FILE} || do_exit
         ( make clean 2>&1) 1>> ${LOG_FILE} || do_exit
@@ -720,11 +754,11 @@
     fi
 fi
 
-if [ ! -e Python-2.7.4/done ]
+if [ ! -e $PYTHON/done ]
 then
     echo "Installing Python.  This may take a while, but don't worry.  yt loves you."
-    [ ! -e Python-2.7.4 ] && tar xfz Python-2.7.4.tgz
-    cd Python-2.7.4
+    [ ! -e $PYTHON ] && tar xfz $PYTHON.tgz
+    cd $PYTHON
     ( ./configure --prefix=${DEST_DIR}/ ${PYCONF_ARGS} 2>&1 ) 1>> ${LOG_FILE} || do_exit
 
     ( make ${MAKE_PROCS} 2>&1 ) 1>> ${LOG_FILE} || do_exit
@@ -739,7 +773,7 @@
 
 if [ $INST_HG -eq 1 ]
 then
-    do_setup_py mercurial-2.5.4
+    do_setup_py $MERCURIAL
     export HG_EXEC=${DEST_DIR}/bin/hg
 else
     # We assume that hg can be found in the path.
@@ -788,9 +822,9 @@
 
 if [ $INST_SCIPY -eq 0 ]
 then
-    do_setup_py numpy-1.7.0 ${NUMPY_ARGS}
+    do_setup_py $NUMPY ${NUMPY_ARGS}
 else
-    if [ ! -e scipy-0.11.0/done ]
+    if [ ! -e $SCIPY/done ]
     then
 	if [ ! -e BLAS/done ]
 	then
@@ -798,27 +832,27 @@
 	    echo "Building BLAS"
 	    cd BLAS
 	    gfortran -O2 -fPIC -fno-second-underscore -c *.f
-	    ar r libfblas.a *.o 1>> ${LOG_FILE}
-	    ranlib libfblas.a 1>> ${LOG_FILE}
+	    ( ar r libfblas.a *.o 2>&1 ) 1>> ${LOG_FILE}
+	    ( ranlib libfblas.a 2>&1 ) 1>> ${LOG_FILE}
 	    rm -rf *.o
 	    touch done
 	    cd ..
 	fi
-	if [ ! -e lapack-3.4.2/done ]
+	if [ ! -e $LAPACK/done ]
 	then
-	    tar xfz lapack-3.4.2.tar.gz
+	    tar xfz $LAPACK.tar.gz
 	    echo "Building LAPACK"
-	    cd lapack-3.4.2/
+	    cd $LAPACK/
 	    cp INSTALL/make.inc.gfortran make.inc
-	    make lapacklib OPTS="-fPIC -O2" NOOPT="-fPIC -O0" CFLAGS=-fPIC LDFLAGS=-fPIC 1>> ${LOG_FILE} || do_exit
+	    ( make lapacklib OPTS="-fPIC -O2" NOOPT="-fPIC -O0" CFLAGS=-fPIC LDFLAGS=-fPIC 2>&1 ) 1>> ${LOG_FILE} || do_exit
 	    touch done
 	    cd ..
 	fi
     fi
     export BLAS=$PWD/BLAS/libfblas.a
-    export LAPACK=$PWD/lapack-3.4.2/liblapack.a
-    do_setup_py numpy-1.7.0 ${NUMPY_ARGS}
-    do_setup_py scipy-0.11.0 ${NUMPY_ARGS}
+    export LAPACK=$PWD/$LAPACK/liblapack.a
+    do_setup_py $NUMPY ${NUMPY_ARGS}
+    do_setup_py $SCIPY ${NUMPY_ARGS}
 fi
 
 if [ -n "${MPL_SUPP_LDFLAGS}" ]
@@ -840,10 +874,10 @@
     echo "Setting CFLAGS ${CFLAGS}"
 fi
 # Now we set up the basedir for matplotlib:
-mkdir -p ${DEST_DIR}/src/matplotlib-1.2.1
-echo "[directories]" >> ${DEST_DIR}/src/matplotlib-1.2.1/setup.cfg
-echo "basedirlist = ${DEST_DIR}" >> ${DEST_DIR}/src/matplotlib-1.2.1/setup.cfg
-do_setup_py matplotlib-1.2.1
+mkdir -p ${DEST_DIR}/src/$MATPLOTLIB
+echo "[directories]" >> ${DEST_DIR}/src/$MATPLOTLIB/setup.cfg
+echo "basedirlist = ${DEST_DIR}" >> ${DEST_DIR}/src/$MATPLOTLIB/setup.cfg
+do_setup_py $MATPLOTLIB
 if [ -n "${OLD_LDFLAGS}" ]
 then
     export LDFLAG=${OLD_LDFLAGS}
@@ -855,36 +889,36 @@
 # Now we do our IPython installation, which has two optional dependencies.
 if [ $INST_0MQ -eq 1 ]
 then
-    if [ ! -e zeromq-3.2.2/done ]
+    if [ ! -e $ZEROMQ/done ]
     then
-        [ ! -e zeromq-3.2.2 ] && tar xfz zeromq-3.2.2.tar.gz
+        [ ! -e $ZEROMQ ] && tar xfz $ZEROMQ.tar.gz
         echo "Installing ZeroMQ"
-        cd zeromq-3.2.2
+        cd $ZEROMQ
         ( ./configure --prefix=${DEST_DIR}/ 2>&1 ) 1>> ${LOG_FILE} || do_exit
         ( make install 2>&1 ) 1>> ${LOG_FILE} || do_exit
         ( make clean 2>&1) 1>> ${LOG_FILE} || do_exit
         touch done
         cd ..
     fi
-    do_setup_py pyzmq-13.0.2 --zmq=${DEST_DIR}
-    do_setup_py tornado-3.0
+    do_setup_py $PYZMQ --zmq=${DEST_DIR}
+    do_setup_py $TORNADO
 fi
 
-do_setup_py ipython-0.13.1
-do_setup_py h5py-2.1.2
-do_setup_py Cython-0.18
-do_setup_py Forthon-0.8.11
-do_setup_py nose-1.2.1
-do_setup_py python-hglib-0.3
-do_setup_py sympy-0.7.2
-[ $INST_PYX -eq 1 ] && do_setup_py PyX-0.12.1
+do_setup_py $IPYTHON
+do_setup_py $H5PY
+do_setup_py $CYTHON
+do_setup_py $FORTHON
+do_setup_py $NOSE
+do_setup_py $PYTHON_HGLIB
+do_setup_py $SYMPY
+[ $INST_PYX -eq 1 ] && do_setup_py $PYX
 
 # Now we build Rockstar and set its environment variable.
 if [ $INST_ROCKSTAR -eq 1 ]
 then
     if [ ! -e Rockstar/done ]
     then
-        [ ! -e Rockstar ] && tar xfz rockstar-0.99.6.tar.gz
+        [ ! -e Rockstar ] && tar xfz $ROCKSTAR.tar.gz
         echo "Building Rockstar"
         cd Rockstar
         ( make lib 2>&1 ) 1>> ${LOG_FILE} || do_exit
@@ -909,10 +943,10 @@
 touch done
 cd $MY_PWD
 
-if !(${DEST_DIR}/bin/python2.7 -c "import readline" >> ${LOG_FILE})
+if !( ( ${DEST_DIR}/bin/python2.7 -c "import readline" 2>&1 )>> ${LOG_FILE})
 then
     echo "Installing pure-python readline"
-    ${DEST_DIR}/bin/pip install readline 1>> ${LOG_FILE}
+    ( ${DEST_DIR}/bin/pip install readline 2>&1 ) 1>> ${LOG_FILE}
 fi
 
 if [ $INST_ENZO -eq 1 ]

diff -r 5205413667f8a2a66d03976a8bfb952d179feee8 -r f46dfdc34a292631bd520fb3af8c878cec975ba1 yt/__init__.py
--- a/yt/__init__.py
+++ b/yt/__init__.py
@@ -96,7 +96,7 @@
     if answer_big_data:
         nose_argv.append('--answer-big-data')
     log_suppress = ytcfg.getboolean("yt","suppressStreamLogging")
-    ytcfg["yt","suppressStreamLogging"] = 'True'
+    ytcfg.set("yt","suppressStreamLogging", 'True')
     initial_dir = os.getcwd()
     yt_file = os.path.abspath(__file__)
     yt_dir = os.path.dirname(yt_file)
@@ -105,4 +105,4 @@
         nose.run(argv=nose_argv)
     finally:
         os.chdir(initial_dir)
-        ytcfg["yt","suppressStreamLogging"] = log_suppress
+        ytcfg.set("yt","suppressStreamLogging", str(log_suppress))

diff -r 5205413667f8a2a66d03976a8bfb952d179feee8 -r f46dfdc34a292631bd520fb3af8c878cec975ba1 yt/analysis_modules/absorption_spectrum/absorption_spectrum_fit.py
--- /dev/null
+++ b/yt/analysis_modules/absorption_spectrum/absorption_spectrum_fit.py
@@ -0,0 +1,809 @@
+from scipy import optimize
+import numpy as na
+import h5py
+from yt.analysis_modules.absorption_spectrum.absorption_line \
+        import voigt
+
+
+def generate_total_fit(x, fluxData, orderFits, speciesDicts, 
+        minError=1E-5, complexLim=.999,
+        fitLim=.99, minLength=3, 
+        maxLength=1000, splitLim=.99,
+        output_file=None):
+
+    """
+    This function is designed to fit an absorption spectrum by breaking 
+    the spectrum up into absorption complexes, and iteratively adding
+    and optimizing voigt profiles to each complex.
+
+    Parameters
+    ----------
+    x : (N) ndarray
+        1d array of wavelengths
+    fluxData : (N) ndarray
+        array of flux corresponding to the wavelengths given
+        in x. (needs to be the same size as x)
+    orderFits : list
+        list of the names of the species in the order that they 
+        should be fit. Names should correspond to the names of the species
+        given in speciesDicts. (ex: ['lya','OVI'])
+    speciesDicts : dictionary
+        Dictionary of dictionaries (I'm addicted to dictionaries, I
+        confess). Top level keys should be the names of all the species given
+        in orderFits. The entries should be dictionaries containing all 
+        relevant parameters needed to create an absorption line of a given 
+        species (f,Gamma,lambda0) as well as max and min values for parameters
+        to be fit
+    complexLim : float, optional
+        Maximum flux to start the edge of an absorption complex. Different 
+        from fitLim because it decides extent of a complex rather than 
+        whether or not a complex is accepted. 
+    fitLim : float,optional
+        Maximum flux where the level of absorption will trigger 
+        identification of the region as an absorption complex. Default = .98.
+        (ex: for a minSize=.98, a region where all the flux is between 1.0 and
+        .99 will not be separated out to be fit as an absorbing complex, but
+        a region that contains a point where the flux is .97 will be fit
+        as an absorbing complex.)
+    minLength : int, optional
+        number of cells required for a complex to be included. 
+        default is 3 cells.
+    maxLength : int, optional
+        number of cells required for a complex to be split up. Default
+        is 1000 cells.
+    splitLim : float, optional
+        if attempting to split a region for being larger than maxlength
+        the point of the split must have a flux greater than splitLim 
+        (ie: absorption greater than splitLim). Default= .99.
+    output_file : string, optional
+        location to save the results of the fit. 
+
+    Returns
+    -------
+    allSpeciesLines : dictionary
+        Dictionary of dictionaries representing the fit lines. 
+        Top level keys are the species given in orderFits and the corresponding
+        entries are dictionaries with the keys 'N','b','z', and 'group#'. 
+        Each of these corresponds to a list of the parameters for every
+        accepted fitted line. (ie: N[0],b[0],z[0] will create a line that
+        fits some part of the absorption spectrum). 'group#' is a similar list
+        but identifies which absorbing complex each line belongs to. Lines
+        with the same group# were fit at the same time. group#'s do not
+        correlate between species (ie: an lya line with group number 1 and
+        an OVI line with group number 1 were not fit together and do
+        not necessarily correspond to the same region)
+    yFit : (N) ndarray
+        array of flux corresponding to the combination of all fitted
+        absorption profiles. Same size as x.
+    """
+
+    #Empty dictionary for fitted lines
+    allSpeciesLines = {}
+
+    #Wavelength of beginning of array, wavelength resolution
+    x0,xRes=x[0],x[1]-x[0]
+
+    #Empty fit without any lines
+    yFit = na.ones(len(fluxData))
+
+    #Find all regions where lines/groups of lines are present
+    cBounds = _find_complexes(x, fluxData, fitLim=fitLim,
+            complexLim=complexLim, minLength=minLength,
+            maxLength=maxLength, splitLim=splitLim)
+
+    #Fit all species one at a time in given order from low to high wavelength
+    for species in orderFits:
+        speciesDict = speciesDicts[species]
+        speciesLines = {'N':na.array([]),
+                        'b':na.array([]),
+                        'z':na.array([]),
+                        'group#':na.array([])}
+
+        #Set up wavelengths for species
+        initWl = speciesDict['wavelength'][0]
+
+        for b_i,b in enumerate(cBounds):
+            xBounded=x[b[1]:b[2]]
+            yDatBounded=fluxData[b[1]:b[2]]
+            yFitBounded=yFit[b[1]:b[2]]
+
+            #Find init redshift
+            z=(xBounded[yDatBounded.argmin()]-initWl)/initWl
+
+            #Check if any flux at partner sites
+            if not _line_exists(speciesDict['wavelength'],
+                    fluxData,z,x0,xRes,fitLim): 
+                continue 
+
+            #Fit Using complex tools
+            newLinesP,flag=_complex_fit(xBounded,yDatBounded,yFitBounded,
+                    z,fitLim,minError*(b[2]-b[1]),speciesDict)
+
+            #Check existence of partner lines if applicable
+            newLinesP = _remove_unaccepted_partners(newLinesP, x, fluxData, 
+                    b, minError*(b[2]-b[1]),
+                    x0, xRes, speciesDict)
+
+            #If flagged as a bad fit, species is lyman alpha,
+            #   and it may be a saturated line, use special tools
+            if flag and species=='lya' and min(yDatBounded)<.1:
+                newLinesP=_large_flag_fit(xBounded,yDatBounded,
+                        yFitBounded,z,speciesDict,
+                        minSize,minError*(b[2]-b[1]))
+
+            #Adjust total current fit
+            yFit=yFit*_gen_flux_lines(x,newLinesP,speciesDict)
+
+            #Add new group to all fitted lines
+            if na.size(newLinesP)>0:
+                speciesLines['N']=na.append(speciesLines['N'],newLinesP[:,0])
+                speciesLines['b']=na.append(speciesLines['b'],newLinesP[:,1])
+                speciesLines['z']=na.append(speciesLines['z'],newLinesP[:,2])
+                groupNums = b_i*na.ones(na.size(newLinesP[:,0]))
+                speciesLines['group#']=na.append(speciesLines['group#'],groupNums)
+
+        allSpeciesLines[species]=speciesLines
+
+    if output_file:
+        _output_fit(allSpeciesLines, output_file)
+
+    return (allSpeciesLines,yFit)
+
+def _complex_fit(x, yDat, yFit, initz, minSize, errBound, speciesDict, 
+        initP=None):
+    """ Fit an absorption complex by iteratively adding and optimizing
+    voigt profiles.
+    
+    A complex is defined as a region where some number of lines may be present,
+    or a region of non zero of absorption. Lines are iteratively added
+    and optimized until the difference between the flux generated using
+    the optimized parameters has a least squares difference between the 
+    desired flux profile less than the error bound.
+
+    Parameters
+    ----------
+    x : (N) ndarray
+        array of wavelength
+    ydat : (N) ndarray
+        array of desired flux profile to be fitted for the wavelength
+        space given by x. Same size as x.
+    yFit : (N) ndarray
+        array of flux profile fitted for the wavelength
+        space given by x already. Same size as x.
+    initz : float
+        redshift to try putting first line at 
+        (maximum absorption for region)
+    minsize : float
+        minimum absorption allowed for a line to still count as a line
+        given in normalized flux (ie: for minSize=.9, only lines with minimum
+        flux less than .9 will be fitted)
+    errbound : float
+        maximum total error allowed for an acceptable fit
+    speciesDict : dictionary
+        dictionary containing all relevant parameters needed
+        to create an absorption line of a given species (f,Gamma,lambda0)
+        as well as max and min values for parameters to be fit
+    initP : (,3,) ndarray
+        initial guess to try for line parameters to fit the region. Used
+        by large_flag_fit. Default = None, and initial guess generated
+        automatically.
+
+    Returns
+    -------
+    linesP : (3,) ndarray
+        Array of best parameters if a good enough fit is found in 
+        the form [[N1,b1,z1], [N2,b2,z2],...]
+    flag : bool
+        boolean value indicating the success of the fit (True if unsuccessful)
+    """
+
+    #Setup initial line guesses
+    if initP==None: #Regular fit
+        initP = [0,0,0] 
+        if min(yDat)<.5: #Large lines get larger initial guess 
+            initP[0] = 10**16
+        elif min(yDat)>.9: #Small lines get smaller initial guess
+            initP[0] = 10**12.5
+        else:
+            initP[0] = speciesDict['init_N']
+        initP[1] = speciesDict['init_b']
+        initP[2]=initz
+        initP=na.array([initP])
+
+    linesP = initP
+
+    #For generating new z guesses
+    wl0 = speciesDict['wavelength'][0]
+
+    #Check if first line exists still
+    if min(yDat-yFit+1)>minSize: 
+        return [],False
+    
+    #Values to proceed through first run
+    errSq,prevErrSq=1,1000
+
+    while True:
+        #Initial parameter guess from joining parameters from all lines
+        #   in lines into a single array
+        initP = linesP.flatten()
+
+        #Optimize line
+        fitP,success=optimize.leastsq(_voigt_error,initP,
+                args=(x,yDat,yFit,speciesDict),
+                epsfcn=1E-10,maxfev=1000)
+
+        #Set results of optimization
+        linesP = na.reshape(fitP,(-1,3))
+
+        #Generate difference between current best fit and data
+        yNewFit=_gen_flux_lines(x,linesP,speciesDict)
+        dif = yFit*yNewFit-yDat
+
+        #Sum to get idea of goodness of fit
+        errSq=sum(dif**2)
+
+        #If good enough, break
+        if errSq < errBound: 
+            break
+
+        #If last fit was worse, reject the last line and revert to last fit
+        if errSq > prevErrSq*10:
+            #If its still pretty damn bad, cut losses and try flag fit tools
+            if prevErrSq >1E2*errBound and speciesDict['name']=='HI lya':
+                return [],True
+            else:
+                yNewFit=_gen_flux_lines(x,prevLinesP,speciesDict)
+                break
+
+        #If too many lines 
+        if na.shape(linesP)[0]>8 or na.size(linesP)+3>=len(x):
+            #If its fitable by flag tools and still bad, use flag tools
+            if errSq >1E2*errBound and speciesDict['name']=='HI lya':
+                return [],True
+            else:
+                break 
+
+        #Store previous data in case reject next fit
+        prevErrSq = errSq
+        prevLinesP = linesP
+
+
+        #Set up initial condition for new line
+        newP = [0,0,0] 
+        if min(dif)<.1:
+            newP[0]=10**12
+        elif min(dif)>.9:
+            newP[0]=10**16
+        else:
+            newP[0]=10**14
+        newP[1] = speciesDict['init_b']
+        newP[2]=(x[dif.argmax()]-wl0)/wl0
+        linesP=na.append(linesP,[newP],axis=0)
+
+
+    #Check the parameters of all lines to see if they fall in an
+    #   acceptable range, as given in dict ref
+    remove=[]
+    for i,p in enumerate(linesP):
+        check=_check_params(na.array([p]),speciesDict)
+        if check: 
+            remove.append(i)
+    linesP = na.delete(linesP,remove,axis=0)
+
+    return linesP,False
+
+def _large_flag_fit(x, yDat, yFit, initz, speciesDict, minSize, errBound):
+    """
+    Attempts to more robustly fit saturated lyman alpha regions that have
+    not converged to satisfactory fits using the standard tools.
+
+    Uses a preselected sample of a wide range of initial parameter guesses
+    designed to fit saturated lines (see get_test_lines).
+
+    Parameters
+    ----------
+    x : (N) ndarray
+        array of wavelength
+    ydat : (N) ndarray
+        array of desired flux profile to be fitted for the wavelength
+        space given by x. Same size as x.
+    yFit : (N) ndarray
+        array of flux profile fitted for the wavelength
+        space given by x already. Same size as x.
+    initz : float
+        redshift to try putting first line at 
+        (maximum absorption for region)
+    speciesDict : dictionary
+        dictionary containing all relevant parameters needed
+        to create an absorption line of a given species (f,Gamma,lambda0)
+        as well as max and min values for parameters to be fit
+    minsize : float
+        minimum absorption allowed for a line to still count as a line
+        given in normalized flux (ie: for minSize=.9, only lines with minimum
+        flux less than .9 will be fitted)
+    errbound : float
+        maximum total error allowed for an acceptable fit
+
+    Returns
+    -------
+    bestP : (3,) ndarray
+        array of best parameters if a good enough fit is found in 
+        the form [[N1,b1,z1], [N2,b2,z2],...]  
+    """
+
+    #Set up some initial line guesses
+    lineTests = _get_test_lines(initz)
+
+    #Keep track of the lowest achieved error
+    bestError = 1000 
+
+    #Iterate through test line guesses
+    for initLines in lineTests:
+        if initLines[1,0]==0:
+            initLines = na.delete(initLines,1,axis=0)
+
+        #Do fitting with initLines as first guess
+        linesP,flag=_complex_fit(x,yDat,yFit,initz,
+                minSize,errBound,speciesDict,initP=initLines)
+
+        #Find error of last fit
+        yNewFit=_gen_flux_lines(x,linesP,speciesDict)
+        dif = yFit*yNewFit-yDat
+        errSq=sum(dif**2)
+
+        #If error lower, keep track of the lines used to make that fit
+        if errSq < bestError:
+            bestError = errSq
+            bestP = linesP
+
+    if bestError>10*errBound*len(x): 
+        return []
+    else:
+        return bestP
+
+def _get_test_lines(initz):
+    """
+    Returns a 3d numpy array of lines to test as initial guesses for difficult
+    to fit lyman alpha absorbers that are saturated. 
+    
+    The array is 3d because
+    the first dimension gives separate initial guesses, the second dimension
+    has multiple lines for the same guess (trying a broad line plus a 
+    saturated line) and the 3d dimension contains the 3 fit parameters (N,b,z)
+
+    Parameters
+    ----------
+    initz : float
+        redshift to give all the test lines
+
+    Returns
+    -------
+    testP : (,3,) ndarray
+        numpy array of the form 
+        [[[N1a,b1a,z1a], [N1b,b1b,z1b]], [[N2a,b2,z2a],...] ...]
+    """
+
+    #Set up a bunch of empty lines
+    testP = na.zeros((10,2,3))
+
+    testP[0,0,:]=[1E18,20,initz]
+    testP[1,0,:]=[1E18,40,initz]
+    testP[2,0,:]=[1E16,5, initz]
+    testP[3,0,:]=[1E16,20,initz]
+    testP[4,0,:]=[1E16,80,initz]
+
+    testP[5,0,:]=[1E18,20,initz]
+    testP[6,0,:]=[1E18,40,initz]
+    testP[7,0,:]=[1E16,5, initz]
+    testP[8,0,:]=[1E16,20,initz]
+    testP[9,0,:]=[1E16,80,initz]
+
+    testP[5,1,:]=[1E13,100,initz]
+    testP[6,1,:]=[1E13,100,initz]
+    testP[7,1,:]=[1E13,100,initz]
+    testP[8,1,:]=[1E13,100,initz]
+    testP[9,1,:]=[1E13,100,initz]
+
+    return testP
+
+def _get_bounds(z, b, wl, x0, xRes):
+    """ 
+    Gets the indices of range of wavelength that the wavelength wl is in 
+    with the size of some initial wavelength range.
+
+    Used for checking if species with multiple lines (as in the OVI doublet)
+    fit all lines appropriately.
+
+    Parameters
+    ----------
+    z : float
+        redshift
+    b : (3) ndarray/list
+        initial bounds in form [i0,i1,i2] where i0 is the index of the 
+        minimum flux for the complex, i1 is index of the lower wavelength 
+        edge of the complex, and i2 is the index of the higher wavelength
+        edge of the complex.
+    wl : float
+        unredshifted wavelength of the peak of the new region 
+    x0 : float
+        wavelength of the index 0
+    xRes : float
+        difference in wavelength for two consecutive indices
+    
+    Returns
+    -------
+    indices : (2) tuple
+        Tuple (i1,i2) where i1 is the index of the lower wavelength bound of 
+        the new region and i2 is the index of the higher wavelength bound of
+        the new region
+    """
+
+    r=[-b[1]+100+b[0],b[2]+100-b[0]]
+    redWl = (z+1)*wl
+    iRedWl=int((redWl-x0)/xRes)
+    indices = (iRedWl-r[0],iRedWl+r[1])
+
+    return indices
+
+def _remove_unaccepted_partners(linesP, x, y, b, errBound, 
+        x0, xRes, speciesDict):
+    """
+    Given a set of parameters [N,b,z] that form multiple lines for a given
+    species (as in the OVI doublet), remove any set of parameters where
+    not all transition wavelengths have a line that matches the fit.
+
+    (ex: if a fit is determined based on the first line of the OVI doublet,
+    but the given parameters give a bad fit of the wavelength space of
+    the second line then that set of parameters is removed from the array
+    of line parameters.)
+
+    Parameters
+    ----------
+    linesP : (3,) ndarray
+        array giving sets of line parameters in 
+        form [[N1, b1, z1], ...]
+    x : (N) ndarray
+        wavelength array [nm]
+    y : (N) ndarray
+        normalized flux array of original data
+    b : (3) tuple/list/ndarray
+        indices that give the bounds of the original region so that another 
+        region of similar size can be used to determine the goodness
+        of fit of the other wavelengths
+    errBound : float
+        size of the error that is appropriate for a given region, 
+        adjusted to account for the size of the region.
+
+    Returns
+    -------
+    linesP : (3,) ndarray
+        array similar to linesP that only contains lines with
+        appropriate fits of all transition wavelengths.
+    """
+
+    #List of lines to remove
+    removeLines=[]
+
+    #Iterate through all sets of line parameters
+    for i,p in enumerate(linesP):
+
+        #iterate over all transition wavelengths
+        for wl in speciesDict['wavelength']:
+
+            #Get the bounds of a similar sized region around the
+            #   appropriate wavelength, and then get the appropriate
+            #   region of wavelength and flux
+            lb = _get_bounds(p[2],b,wl,x0,xRes)
+            xb,yb=x[lb[0]:lb[1]],y[lb[0]:lb[1]]
+
+            #Generate a fit and find the difference to data
+            yFitb=_gen_flux_lines(xb,na.array([p]),speciesDict)
+            dif =yb-yFitb
+
+            #Only counts as an error if line is too big ---------------<
+            dif = [k for k in dif if k>0]
+            err = sum(dif)
+
+            #If the fit is too bad then add the line to list of removed lines
+            if err > errBound*1E2:
+                removeLines.append(i)
+                break
+
+    #Remove all bad line fits
+    linesP = na.delete(linesP,removeLines,axis=0)
+
+    return linesP 
+
+
+
+def _line_exists(wavelengths, y, z, x0, xRes,fluxMin):
+    """For a group of lines finds if the there is some change in flux greater
+    than some minimum at the same redshift with different initial wavelengths
+
+    Parameters
+    ----------
+    wavelengths : (N) ndarray
+        array of initial wavelengths to check
+    y : (N) ndarray
+        flux array to check
+    x0 : float
+        wavelength of the first value in y
+    xRes : float
+        difference in wavelength between consecutive cells in flux array
+    fluxMin : float
+        maximum flux to count as a line existing. 
+
+    Returns
+    -------
+
+    flag : boolean 
+        value indicating whether all lines exist. True if all lines exist
+    """
+
+    #Iterate through initial wavelengths
+    for wl in wavelengths:
+        #Redshifted wavelength
+        redWl = (z+1)*wl
+
+        #Index of the redshifted wavelength
+        indexRedWl = (redWl-x0)/xRes
+
+        #Check if surpasses minimum absorption bound
+        if y[int(indexRedWl)]>fluxMin:
+            return False
+
+    return True
+
+def _find_complexes(x, yDat, complexLim=.999, fitLim=.99,
+        minLength =3, maxLength=1000, splitLim=.99):
+    """Breaks up the wavelength space into groups
+    where there is some absorption. 
+
+    Parameters
+    ----------
+    x : (N) ndarray
+        array of wavelengths
+    yDat : (N) ndarray
+        array of flux corresponding to the wavelengths given
+        in x. (needs to be the same size as x)
+    complexLim : float, optional
+        Maximum flux to start the edge of an absorption complex. Different 
+        from fitLim because it decides extent of a complex rather than 
+        whether or not a complex is accepted. 
+    fitLim : float,optional
+        Maximum flux where the level of absorption will trigger 
+        identification of the region as an absorption complex. Default = .98.
+        (ex: for a minSize=.98, a region where all the flux is between 1.0 and
+        .99 will not be separated out to be fit as an absorbing complex, but
+        a region that contains a point where the flux is .97 will be fit
+        as an absorbing complex.)
+    minLength : int, optional
+        number of cells required for a complex to be included. 
+        default is 3 cells.
+    maxLength : int, optional
+        number of cells required for a complex to be split up. Default
+        is 1000 cells.
+    splitLim : float, optional
+        if attempting to split a region for being larger than maxlength
+        the point of the split must have a flux greater than splitLim 
+        (ie: absorption greater than splitLim). Default= .99.
+
+    Returns
+    -------
+    cBounds : (3,) 
+        list of bounds in the form [[i0,i1,i2],...] where i0 is the 
+        index of the maximum flux for a complex, i1 is the index of the
+        beginning of the complex, and i2 is the index of the end of the 
+        complex. Indexes refer to the indices of x and yDat.
+    """
+
+    #Initialize empty list of bounds
+    cBounds=[]
+
+    #Iterate through cells of flux
+    i=0
+    while (i<len(x)):
+
+        #Start tracking at a region that surpasses flux of edge
+        if yDat[i]<complexLim:
+
+            #Iterate through until reach next edge
+            j=0
+            while yDat[i+j]<complexLim: j=j+1
+
+            #Check if the complex is big enough
+            if j >minLength:
+
+                #Check if there is enough absorption for the complex to
+                #   be included
+                cPeak = yDat[i:i+j].argmin()
+                if yDat[cPeak+i]<fitLim:
+                    cBounds.append([cPeak+i,i,i+j])
+
+            i=i+j
+        i=i+1
+
+    i=0
+    #Iterate through the bounds
+    while i < len(cBounds):
+        b=cBounds[i]
+
+        #Check if the region needs to be divided
+        if b[2]-b[1]>maxLength:
+
+            #Find the minimum absorption in the middle two quartiles of
+            #   the large complex
+            q=(b[2]-b[1])/4
+            cut = yDat[b[1]+q:b[2]-q].argmax()+b[1]+q
+
+            #Only break it up if the minimum absorption is actually low enough
+            if yDat[cut]>splitLim:
+
+                #Get the new two peaks
+                b1Peak = yDat[b[1]:cut].argmin()+b[1]
+                b2Peak = yDat[cut:b[2]].argmin()+cut
+
+                #add the two regions separately
+                cBounds.insert(i+1,[b1Peak,b[1],cut])
+                cBounds.insert(i+2,[b2Peak,cut,b[2]])
+
+                #Remove the original region
+                cBounds.pop(i)
+                i=i+1
+        i=i+1
+
+    return cBounds
+
+def _gen_flux_lines(x, linesP, speciesDict):
+    """
+    Calculates the normalized flux for a region of wavelength space
+    generated by a set of absorption lines.
+
+    Parameters
+    ----------
+    x : (N) ndarray
+        Array of wavelength
+    linesP: (3,) ndarray
+        Array giving sets of line parameters in 
+        form [[N1, b1, z1], ...]
+    speciesDict : dictionary
+        Dictionary containing all relevant parameters needed
+        to create an absorption line of a given species (f,Gamma,lambda0)
+
+    Returns
+    -------
+    flux : (N) ndarray
+        Array of normalized flux generated by the line parameters
+        given in linesP over the wavelength space given in x. Same size as x.
+    """
+    y=0
+    for p in linesP:
+        for i in range(speciesDict['numLines']):
+            f=speciesDict['f'][i]
+            g=speciesDict['Gamma'][i]
+            wl=speciesDict['wavelength'][i]
+            y = y+ _gen_tau(x,p,f,g,wl)
+    flux = na.exp(-y)
+    return flux
+
+def _gen_tau(t, p, f, Gamma, lambda_unshifted):
+    """This calculates a flux distribution for given parameters using the yt
+    voigt profile generator"""
+    N,b,z= p
+    
+    #Calculating quantities
+    tau_o = 1.4973614E-15*N*f*lambda_unshifted/b
+    a=7.95774715459E-15*Gamma*lambda_unshifted/b
+    x=299792.458/b*(lambda_unshifted*(1+z)/t-1)
+    
+    H = na.zeros(len(x))
+    H = voigt(a,x)
+    
+    tau = tau_o*H
+
+    return tau
+
+def _voigt_error(pTotal, x, yDat, yFit, speciesDict):
+    """
+    Gives the error of each point  used to optimize the fit of a group
+        of absorption lines to a given flux profile.
+
+        If the parameters are not in the acceptable range as defined
+        in speciesDict, the first value of the error array will
+        contain a large value (999), to prevent the optimizer from running
+        into negative number problems.
+
+    Parameters
+    ----------
+    pTotal : (3,) ndarray 
+        Array with form [[N1, b1, z1], ...] 
+    x : (N) ndarray
+        array of wavelengths [nm]
+    yDat : (N) ndarray
+        desired normalized flux from fits of lines in wavelength
+        space given by x
+    yFit : (N) ndarray
+        previous fit over the wavelength space given by x.
+    speciesDict : dictionary
+        dictionary containing all relevant parameters needed
+        to create an absorption line of a given species (f,Gamma,lambda0)
+        as well as max and min values for parameters to be fit
+
+    Returns
+    -------
+    error : (N) ndarray
+        the difference between the fit generated by the parameters
+        given in pTotal multiplied by the previous fit and the desired
+        flux profile, w/ first index modified appropriately for bad 
+        parameter choices
+    """
+
+    pTotal.shape = (-1,3)
+    yNewFit = _gen_flux_lines(x,pTotal,speciesDict)
+
+    error = yDat-yFit*yNewFit
+    error[0] = _check_params(pTotal,speciesDict)
+
+    return error
+
+def _check_params(p, speciesDict):
+    """
+    Check to see if any of the parameters in p fall outside the range 
+        given in speciesDict.
+
+    Parameters
+    ----------
+    p : (3,) ndarray
+        array with form [[N1, b1, z1], ...] 
+    speciesDict : dictionary
+        dictionary with properties giving the max and min
+        values appropriate for each parameter N,b, and z.
+
+    Returns
+    -------
+    check : int
+        0 if all values are fine
+        999 if any values fall outside acceptable range
+    """
+    check = 0
+    if any(p[:,0] > speciesDict['maxN']) or\
+          any(p[:,0] < speciesDict['minN']) or\
+          any(p[:,1] > speciesDict['maxb']) or\
+          any(p[:,1] < speciesDict['minb']) or\
+          any(p[:,2] > speciesDict['maxz']) or\
+          any(p[:,2] < speciesDict['minz']):
+              check = 999
+    return check
+
+
+def _output_fit(lineDic, file_name = 'spectrum_fit.h5'):
+    """
+    This function is designed to output the parameters of the series
+    of lines used to fit an absorption spectrum. 
+
+    The dataset contains entries in the form species/N, species/b
+    species/z, and species/complex. The ith entry in each of the datasets
+    is the fitted parameter for the ith line fitted to the spectrum for
+    the given species. The species names come from the fitted line
+    dictionary.
+
+    Parameters
+    ----------
+    lineDic : dictionary
+        Dictionary of dictionaries representing the fit lines. 
+        Top level keys are the species given in orderFits and the corresponding
+        entries are dictionaries with the keys 'N','b','z', and 'group#'. 
+        Each of these corresponds to a list of the parameters for every
+        accepted fitted line. 
+    fileName : string, optional
+        Name of the file to output fit to. Default = 'spectrum_fit.h5'
+
+    """
+    f = h5py.File(file_name, 'w')
+    for ion, params in lineDic.iteritems():
+        f.create_dataset("{0}/N".format(ion),data=params['N'])
+        f.create_dataset("{0}/b".format(ion),data=params['b'])
+        f.create_dataset("{0}/z".format(ion),data=params['z'])
+        f.create_dataset("{0}/complex".format(ion),data=params['group#'])
+    print 'Writing spectrum fit to {0}'.format(file_name)
+

diff -r 5205413667f8a2a66d03976a8bfb952d179feee8 -r f46dfdc34a292631bd520fb3af8c878cec975ba1 yt/analysis_modules/absorption_spectrum/api.py
--- a/yt/analysis_modules/absorption_spectrum/api.py
+++ b/yt/analysis_modules/absorption_spectrum/api.py
@@ -30,3 +30,6 @@
 
 from .absorption_spectrum import \
     AbsorptionSpectrum
+
+from .absorption_spectrum_fit import \
+    generate_total_fit

diff -r 5205413667f8a2a66d03976a8bfb952d179feee8 -r f46dfdc34a292631bd520fb3af8c878cec975ba1 yt/analysis_modules/halo_finding/halo_objects.py
--- a/yt/analysis_modules/halo_finding/halo_objects.py
+++ b/yt/analysis_modules/halo_finding/halo_objects.py
@@ -1061,8 +1061,9 @@
     def __init__(self, data_source, dm_only=True, redshift=-1):
         """
         Run hop on *data_source* with a given density *threshold*.  If
-        *dm_only* is True (default), only run it on the dark matter particles, otherwise
-        on all particles.  Returns an iterable collection of *HopGroup* items.
+        *dm_only* is True (default), only run it on the dark matter particles, 
+        otherwise on all particles.  Returns an iterable collection of 
+        *HopGroup* items.
         """
         self._data_source = data_source
         self.dm_only = dm_only

diff -r 5205413667f8a2a66d03976a8bfb952d179feee8 -r f46dfdc34a292631bd520fb3af8c878cec975ba1 yt/config.py
--- a/yt/config.py
+++ b/yt/config.py
@@ -62,7 +62,7 @@
     notebook_password = '',
     answer_testing_tolerance = '3',
     answer_testing_bitwise = 'False',
-    gold_standard_filename = 'gold009',
+    gold_standard_filename = 'gold010',
     local_standard_filename = 'local001',
     sketchfab_api_key = 'None'
     )

diff -r 5205413667f8a2a66d03976a8bfb952d179feee8 -r f46dfdc34a292631bd520fb3af8c878cec975ba1 yt/data_objects/setup.py
--- a/yt/data_objects/setup.py
+++ b/yt/data_objects/setup.py
@@ -8,7 +8,7 @@
 def configuration(parent_package='', top_path=None):
     from numpy.distutils.misc_util import Configuration
     config = Configuration('data_objects', parent_package, top_path)
+    config.add_subpackage("tests")
     config.make_config_py()  # installs __config__.py
-    config.add_subpackage("tests")
     #config.make_svn_version_py()
     return config

diff -r 5205413667f8a2a66d03976a8bfb952d179feee8 -r f46dfdc34a292631bd520fb3af8c878cec975ba1 yt/data_objects/static_output.py
--- a/yt/data_objects/static_output.py
+++ b/yt/data_objects/static_output.py
@@ -70,6 +70,8 @@
             obj = object.__new__(cls)
             if obj._skip_cache is False:
                 _cached_pfs[apath] = obj
+        else:
+            obj = _cached_pfs[apath]
         return obj
 
     def __init__(self, filename, data_style=None, file_style=None):

diff -r 5205413667f8a2a66d03976a8bfb952d179feee8 -r f46dfdc34a292631bd520fb3af8c878cec975ba1 yt/data_objects/tests/test_cutting_plane.py
--- a/yt/data_objects/tests/test_cutting_plane.py
+++ b/yt/data_objects/tests/test_cutting_plane.py
@@ -1,5 +1,6 @@
 from yt.testing import *
 import os
+import tempfile
 
 def setup():
     from yt.config import ytcfg
@@ -7,7 +8,10 @@
 
 def teardown_func(fns):
     for fn in fns:
-        os.remove(fn)
+        try:
+            os.remove(fn)
+        except OSError:
+            pass
 
 def test_cutting_plane():
     for nprocs in [8, 1]:
@@ -23,7 +27,9 @@
         yield assert_equal, cut["Ones"].min(), 1.0
         yield assert_equal, cut["Ones"].max(), 1.0
         pw = cut.to_pw()
-        fns += pw.save()
+        tmpfd, tmpname = tempfile.mkstemp(suffix='.png')
+        os.close(tmpfd)
+        fns += pw.save(name=tmpname)
         frb = cut.to_frb((1.0,'unitary'), 64)
         for cut_field in ['Ones', 'Density']:
             yield assert_equal, frb[cut_field].info['data_source'], \

diff -r 5205413667f8a2a66d03976a8bfb952d179feee8 -r f46dfdc34a292631bd520fb3af8c878cec975ba1 yt/data_objects/tests/test_image_array.py
--- a/yt/data_objects/tests/test_image_array.py
+++ b/yt/data_objects/tests/test_image_array.py
@@ -1,130 +1,94 @@
-from yt.testing import *
-from yt.data_objects.image_array import ImageArray
 import numpy as np
 import os
 import tempfile
 import shutil
+import unittest
+from yt.data_objects.image_array import ImageArray
+from yt.testing import \
+    assert_equal
+
 
 def setup():
     from yt.config import ytcfg
-    ytcfg["yt","__withintesting"] = "True"
-    np.seterr(all = 'ignore')
+    ytcfg["yt", "__withintesting"] = "True"
+    np.seterr(all='ignore')
+
+
+def dummy_image(kstep, nlayers):
+    im = np.zeros([64, 128, nlayers])
+    for i in xrange(im.shape[0]):
+        for k in xrange(im.shape[2]):
+            im[i, :, k] = np.linspace(0.0, kstep * k, im.shape[1])
+    return im
+
 
 def test_rgba_rescale():
-    im = np.zeros([64,128,4])
-    for i in xrange(im.shape[0]):
-        for k in xrange(im.shape[2]):
-            im[i,:,k] = np.linspace(0.,10.*k, im.shape[1])
-    im_arr = ImageArray(im)
+    im_arr = ImageArray(dummy_image(10.0, 4))
 
     new_im = im_arr.rescale(inline=False)
-    yield assert_equal, im_arr[:,:,:3].max(), 2*10.
-    yield assert_equal, im_arr[:,:,3].max(), 3*10.
-    yield assert_equal, new_im[:,:,:3].sum(axis=2).max(), 1.0 
-    yield assert_equal, new_im[:,:,3].max(), 1.0
+    yield assert_equal, im_arr[:, :, :3].max(), 2 * 10.
+    yield assert_equal, im_arr[:, :, 3].max(), 3 * 10.
+    yield assert_equal, new_im[:, :, :3].sum(axis=2).max(), 1.0
+    yield assert_equal, new_im[:, :, 3].max(), 1.0
 
     im_arr.rescale()
-    yield assert_equal, im_arr[:,:,:3].sum(axis=2).max(), 1.0
-    yield assert_equal, im_arr[:,:,3].max(), 1.0
+    yield assert_equal, im_arr[:, :, :3].sum(axis=2).max(), 1.0
+    yield assert_equal, im_arr[:, :, 3].max(), 1.0
 
-def test_image_array_hdf5():
-    # Perform I/O in safe place instead of yt main dir
-    tmpdir = tempfile.mkdtemp()
-    curdir = os.getcwd()
-    os.chdir(tmpdir)
 
-    im = np.zeros([64,128,3])
-    for i in xrange(im.shape[0]):
-        for k in xrange(im.shape[2]):
-            im[i,:,k] = np.linspace(0.,0.3*k, im.shape[1])
+class TestImageArray(unittest.TestCase):
 
-    myinfo = {'field':'dinosaurs', 'east_vector':np.array([1.,0.,0.]), 
-        'north_vector':np.array([0.,0.,1.]), 'normal_vector':np.array([0.,1.,0.]),  
-        'width':0.245, 'units':'cm', 'type':'rendering'}
+    tmpdir = None
+    curdir = None
 
-    im_arr = ImageArray(im, info=myinfo)
-    im_arr.save('test_3d_ImageArray')
+    def setUp(self):
+        self.tmpdir = tempfile.mkdtemp()
+        self.curdir = os.getcwd()
+        os.chdir(self.tmpdir)
 
-    im = np.zeros([64,128])
-    for i in xrange(im.shape[0]):
-        im[i,:] = np.linspace(0.,0.3*k, im.shape[1])
+    def test_image_array_hdf5(self):
+        myinfo = {'field': 'dinosaurs', 'east_vector': np.array([1., 0., 0.]),
+                  'north_vector': np.array([0., 0., 1.]),
+                  'normal_vector': np.array([0., 1., 0.]),
+                  'width': 0.245, 'units': 'cm', 'type': 'rendering'}
 
-    myinfo = {'field':'dinosaurs', 'east_vector':np.array([1.,0.,0.]), 
-        'north_vector':np.array([0.,0.,1.]), 'normal_vector':np.array([0.,1.,0.]),  
-        'width':0.245, 'units':'cm', 'type':'rendering'}
+        im_arr = ImageArray(dummy_image(0.3, 3), info=myinfo)
+        im_arr.save('test_3d_ImageArray')
 
-    im_arr = ImageArray(im, info=myinfo)
-    im_arr.save('test_2d_ImageArray')
+        im = np.zeros([64, 128])
+        for i in xrange(im.shape[0]):
+            im[i, :] = np.linspace(0., 0.3 * 2, im.shape[1])
 
-    os.chdir(curdir)
-    # clean up
-    shutil.rmtree(tmpdir)
+        myinfo = {'field': 'dinosaurs', 'east_vector': np.array([1., 0., 0.]),
+                  'north_vector': np.array([0., 0., 1.]),
+                  'normal_vector': np.array([0., 1., 0.]),
+                  'width': 0.245, 'units': 'cm', 'type': 'rendering'}
 
-def test_image_array_rgb_png():
-    # Perform I/O in safe place instead of yt main dir
-    tmpdir = tempfile.mkdtemp()
-    curdir = os.getcwd()
-    os.chdir(tmpdir)
+        im_arr = ImageArray(im, info=myinfo)
+        im_arr.save('test_2d_ImageArray')
 
-    im = np.zeros([64,128,3])
-    for i in xrange(im.shape[0]):
-        for k in xrange(im.shape[2]):
-            im[i,:,k] = np.linspace(0.,10.*k, im.shape[1])
+    def test_image_array_rgb_png(self):
+        im_arr = ImageArray(dummy_image(10.0, 3))
+        im_arr.write_png('standard.png')
 
-    im_arr = ImageArray(im)
-    im_arr.write_png('standard.png')
+    def test_image_array_rgba_png(self):
+        im_arr = ImageArray(dummy_image(10.0, 4))
+        im_arr.write_png('standard.png')
+        im_arr.write_png('non-scaled.png', rescale=False)
+        im_arr.write_png('black_bg.png', background='black')
+        im_arr.write_png('white_bg.png', background='white')
+        im_arr.write_png('green_bg.png', background=[0., 1., 0., 1.])
+        im_arr.write_png('transparent_bg.png', background=None)
 
-def test_image_array_rgba_png():
-    # Perform I/O in safe place instead of yt main dir
-    tmpdir = tempfile.mkdtemp()
-    curdir = os.getcwd()
-    os.chdir(tmpdir)
+    def test_image_array_background(self):
+        im_arr = ImageArray(dummy_image(10.0, 4))
+        im_arr.rescale()
+        new_im = im_arr.add_background_color([1., 0., 0., 1.], inline=False)
+        new_im.write_png('red_bg.png')
+        im_arr.add_background_color('black')
+        im_arr.write_png('black_bg2.png')
 
-    im = np.zeros([64,128,4])
-    for i in xrange(im.shape[0]):
-        for k in xrange(im.shape[2]):
-            im[i,:,k] = np.linspace(0.,10.*k, im.shape[1])
-
-    im_arr = ImageArray(im)
-    im_arr.write_png('standard.png')
-    im_arr.write_png('non-scaled.png', rescale=False)
-    im_arr.write_png('black_bg.png', background='black')
-    im_arr.write_png('white_bg.png', background='white')
-    im_arr.write_png('green_bg.png', background=[0.,1.,0.,1.])
-    im_arr.write_png('transparent_bg.png', background=None)
-
-
-def test_image_array_background():
-    # Perform I/O in safe place instead of yt main dir
-    tmpdir = tempfile.mkdtemp()
-    curdir = os.getcwd()
-    os.chdir(tmpdir)
-
-    im = np.zeros([64,128,4])
-    for i in xrange(im.shape[0]):
-        for k in xrange(im.shape[2]):
-            im[i,:,k] = np.linspace(0.,10.*k, im.shape[1])
-
-    im_arr = ImageArray(im)
-    im_arr.rescale()
-    new_im = im_arr.add_background_color([1.,0.,0.,1.], inline=False)
-    new_im.write_png('red_bg.png')
-    im_arr.add_background_color('black')
-    im_arr.write_png('black_bg2.png')
- 
-    os.chdir(curdir)
-    # clean up
-    shutil.rmtree(tmpdir)
-
-
-
-
-
-
-
-
-
-
-
-
-
+    def tearDown(self):
+        os.chdir(self.curdir)
+        # clean up
+        shutil.rmtree(self.tmpdir)

diff -r 5205413667f8a2a66d03976a8bfb952d179feee8 -r f46dfdc34a292631bd520fb3af8c878cec975ba1 yt/data_objects/tests/test_projection.py
--- a/yt/data_objects/tests/test_projection.py
+++ b/yt/data_objects/tests/test_projection.py
@@ -1,5 +1,6 @@
 from yt.testing import *
 import os
+import tempfile
 
 def setup():
     from yt.config import ytcfg
@@ -7,7 +8,10 @@
 
 def teardown_func(fns):
     for fn in fns:
-        os.remove(fn)
+        try:
+            os.remove(fn)
+        except OSError:
+            pass
 
 def test_projection():
     for nprocs in [8, 1]:
@@ -37,7 +41,9 @@
                 yield assert_equal, np.unique(proj["pdx"]), 1.0/(dims[xax]*2.0)
                 yield assert_equal, np.unique(proj["pdy"]), 1.0/(dims[yax]*2.0)
                 pw = proj.to_pw()
-                fns += pw.save()
+                tmpfd, tmpname = tempfile.mkstemp(suffix='.png')
+                os.close(tmpfd)
+                fns += pw.save(name=tmpname)
                 frb = proj.to_frb((1.0,'unitary'), 64)
                 for proj_field in ['Ones', 'Density']:
                     yield assert_equal, frb[proj_field].info['data_source'], \

diff -r 5205413667f8a2a66d03976a8bfb952d179feee8 -r f46dfdc34a292631bd520fb3af8c878cec975ba1 yt/data_objects/tests/test_slice.py
--- a/yt/data_objects/tests/test_slice.py
+++ b/yt/data_objects/tests/test_slice.py
@@ -27,6 +27,7 @@
 """
 import os
 import numpy as np
+import tempfile
 from nose.tools import raises
 from yt.testing import \
     fake_random_pf, assert_equal, assert_array_equal
@@ -42,7 +43,10 @@
 
 def teardown_func(fns):
     for fn in fns:
-        os.remove(fn)
+        try:
+            os.remove(fn)
+        except OSError:
+            pass
 
 
 def test_slice():
@@ -72,7 +76,9 @@
                 yield assert_equal, np.unique(slc["pdx"]), 0.5 / dims[xax]
                 yield assert_equal, np.unique(slc["pdy"]), 0.5 / dims[yax]
                 pw = slc.to_pw()
-                fns += pw.save()
+                tmpfd, tmpname = tempfile.mkstemp(suffix='.png')
+                os.close(tmpfd)
+                fns += pw.save(name=tmpname)
                 frb = slc.to_frb((1.0, 'unitary'), 64)
                 for slc_field in ['Ones', 'Density']:
                     yield assert_equal, frb[slc_field].info['data_source'], \

diff -r 5205413667f8a2a66d03976a8bfb952d179feee8 -r f46dfdc34a292631bd520fb3af8c878cec975ba1 yt/extern/__init__.py
--- /dev/null
+++ b/yt/extern/__init__.py
@@ -0,0 +1,4 @@
+"""
+This packages contains python packages that are bundled with yt
+and are developed by 3rd party upstream.
+"""

diff -r 5205413667f8a2a66d03976a8bfb952d179feee8 -r f46dfdc34a292631bd520fb3af8c878cec975ba1 yt/extern/parameterized.py
--- /dev/null
+++ b/yt/extern/parameterized.py
@@ -0,0 +1,226 @@
+import re
+import inspect
+from functools import wraps
+from collections import namedtuple
+
+from nose.tools import nottest
+from unittest import TestCase
+
+from . import six
+
+if six.PY3:
+    def new_instancemethod(f, *args):
+        return f
+else:
+    import new
+    new_instancemethod = new.instancemethod
+
+_param = namedtuple("param", "args kwargs")
+
+class param(_param):
+    """ Represents a single parameter to a test case.
+
+        For example::
+
+            >>> p = param("foo", bar=16)
+            >>> p
+            param("foo", bar=16)
+            >>> p.args
+            ('foo', )
+            >>> p.kwargs
+            {'bar': 16}
+
+        Intended to be used as an argument to ``@parameterized``::
+
+            @parameterized([
+                param("foo", bar=16),
+            ])
+            def test_stuff(foo, bar=16):
+                pass
+        """
+
+    def __new__(cls, *args , **kwargs):
+        return _param.__new__(cls, args, kwargs)
+
+    @classmethod
+    def explicit(cls, args=None, kwargs=None):
+        """ Creates a ``param`` by explicitly specifying ``args`` and
+            ``kwargs``::
+
+                >>> param.explicit([1,2,3])
+                param(*(1, 2, 3))
+                >>> param.explicit(kwargs={"foo": 42})
+                param(*(), **{"foo": "42"})
+            """
+        args = args or ()
+        kwargs = kwargs or {}
+        return cls(*args, **kwargs)
+
+    @classmethod
+    def from_decorator(cls, args):
+        """ Returns an instance of ``param()`` for ``@parameterized`` argument
+            ``args``::
+
+                >>> param.from_decorator((42, ))
+                param(args=(42, ), kwargs={})
+                >>> param.from_decorator("foo")
+                param(args=("foo", ), kwargs={})
+            """
+        if isinstance(args, param):
+            return args
+        if isinstance(args, six.string_types):
+            args = (args, )
+        return cls(*args)
+
+    def __repr__(self):
+        return "param(*%r, **%r)" %self
+
+class parameterized(object):
+    """ Parameterize a test case::
+
+            class TestInt(object):
+                @parameterized([
+                    ("A", 10),
+                    ("F", 15),
+                    param("10", 42, base=42)
+                ])
+                def test_int(self, input, expected, base=16):
+                    actual = int(input, base=base)
+                    assert_equal(actual, expected)
+
+            @parameterized([
+                (2, 3, 5)
+                (3, 5, 8),
+            ])
+            def test_add(a, b, expected):
+                assert_equal(a + b, expected)
+        """
+
+    def __init__(self, input):
+        self.get_input = self.input_as_callable(input)
+
+    def __call__(self, test_func):
+        self.assert_not_in_testcase_subclass()
+
+        @wraps(test_func)
+        def parameterized_helper_method(test_self=None):
+            f = test_func
+            if test_self is not None:
+                # If we are a test method (which we suppose to be true if we
+                # are being passed a "self" argument), we first need to create
+                # an instance method, attach it to the instance of the test
+                # class, then pull it back off to turn it into a bound method.
+                # If we don't do this, Nose gets cranky.
+                f = self.make_bound_method(test_self, test_func)
+            # Note: because nose is so very picky, the more obvious
+            # ``return self.yield_nose_tuples(f)`` won't work here.
+            for nose_tuple in self.yield_nose_tuples(f):
+                yield nose_tuple
+
+        test_func.__name__ = "_helper_for_%s" %(test_func.__name__, )
+        parameterized_helper_method.parameterized_input = input
+        parameterized_helper_method.parameterized_func = test_func
+        return parameterized_helper_method
+
+    def yield_nose_tuples(self, func):
+        for args in self.get_input():
+            p = param.from_decorator(args)
+            # ... then yield that as a tuple. If those steps aren't
+            # followed precicely, Nose gets upset and doesn't run the test
+            # or doesn't run setup methods.
+            yield self.param_as_nose_tuple(p, func)
+
+    def param_as_nose_tuple(self, p, func):
+        nose_func = func
+        nose_args = p.args
+        if p.kwargs:
+            nose_func = wraps(func)(lambda args, kwargs: func(*args, **kwargs))
+            nose_args = (p.args, p.kwargs)
+        return (nose_func, ) + nose_args
+
+    def make_bound_method(self, instance, func):
+        cls = type(instance)
+        im_f = new_instancemethod(func, None, cls)
+        setattr(cls, func.__name__, im_f)
+        return getattr(instance, func.__name__)
+
+    def assert_not_in_testcase_subclass(self):
+        parent_classes = self._terrible_magic_get_defining_classes()
+        if any(issubclass(cls, TestCase) for cls in parent_classes):
+            raise Exception("Warning: '@parameterized' tests won't work "
+                            "inside subclasses of 'TestCase' - use "
+                            "'@parameterized.expand' instead")
+
+    def _terrible_magic_get_defining_classes(self):
+        """ Returns the set of parent classes of the class currently being defined.
+            Will likely only work if called from the ``parameterized`` decorator.
+            This function is entirely @brandon_rhodes's fault, as he suggested
+            the implementation: http://stackoverflow.com/a/8793684/71522
+            """
+        stack = inspect.stack()
+        if len(stack) <= 4:
+            return []
+        frame = stack[4]
+        code_context = frame[4] and frame[4][0].strip()
+        if not (code_context and code_context.startswith("class ")):
+            return []
+        _, parents = code_context.split("(", 1)
+        parents, _ = parents.rsplit(")", 1)
+        return eval("[" + parents + "]", frame[0].f_globals, frame[0].f_locals)
+
+    @classmethod
+    def input_as_callable(cls, input):
+        if callable(input):
+            return lambda: cls.check_input_values(input())
+        input_values = cls.check_input_values(input)
+        return lambda: input_values
+
+    @classmethod
+    def check_input_values(cls, input_values):
+        if not hasattr(input_values, "__iter__"):
+            raise ValueError("expected iterable input; got %r" %(input, ))
+        return input_values
+
+    @classmethod
+    def expand(cls, input):
+        """ A "brute force" method of parameterizing test cases. Creates new
+            test cases and injects them into the namespace that the wrapped
+            function is being defined in. Useful for parameterizing tests in
+            subclasses of 'UnitTest', where Nose test generators don't work.
+
+            >>> @parameterized.expand([("foo", 1, 2)])
+            ... def test_add1(name, input, expected):
+            ...     actual = add1(input)
+            ...     assert_equal(actual, expected)
+            ...
+            >>> locals()
+            ... 'test_add1_foo_0': <function ...> ...
+            >>>
+            """
+
+        def parameterized_expand_wrapper(f):
+            stack = inspect.stack()
+            frame = stack[1]
+            frame_locals = frame[0].f_locals
+
+            base_name = f.__name__
+            get_input = cls.input_as_callable(input)
+            for num, args in enumerate(get_input()):
+                p = param.from_decorator(args)
+                name_suffix = "_%s" %(num, )
+                if len(p.args) > 0 and isinstance(p.args[0], six.string_types):
+                    name_suffix += "_" + cls.to_safe_name(p.args[0])
+                name = base_name + name_suffix
+                frame_locals[name] = cls.param_as_standalone_func(p, f, name)
+            return nottest(f)
+        return parameterized_expand_wrapper
+
+    @classmethod
+    def param_as_standalone_func(cls, p, func, name):
+        standalone_func = lambda *a: func(*(a + p.args), **p.kwargs)
+        standalone_func.__name__ = name
+        return standalone_func
+
+    @classmethod
+    def to_safe_name(cls, s):
+        return str(re.sub("[^a-zA-Z0-9_]", "", s))

This diff is so big that we needed to truncate the remainder.

https://bitbucket.org/yt_analysis/yt/commits/aee1f39c321a/
Changeset:   aee1f39c321a
Branch:      yt
User:        jzuhone
Date:        2013-08-22 15:16:41
Summary:     Lots of changes:

1) Implemented parallelism. The cells in the data object are split up amongst the processors in the first stage, and in the second stage it is the photons.
2) Optimizations for photon projection.
3) Added some dict-like attributes and methods to both classes.
4) Added the option to change the redshift at the time of projection.
5) Added a class method to join two XRayEventList objects together.
Affected #:  1 file

diff -r f46dfdc34a292631bd520fb3af8c878cec975ba1 -r aee1f39c321a841f1ab1e01817905a10b3558d55 yt/analysis_modules/synthetic_xray_obs/photon_simulator.py
--- a/yt/analysis_modules/synthetic_xray_obs/photon_simulator.py
+++ b/yt/analysis_modules/synthetic_xray_obs/photon_simulator.py
@@ -5,7 +5,7 @@
 from yt.utilities.cosmology import Cosmology
 from yt.utilities.orientation import Orientation
 from yt.utilities.parallel_tools.parallel_analysis_interface import \
-     communication_system
+     communication_system, parallel_root_only, get_mpi_type, parallel_capable
 from yt.data_objects.api import add_field
 
 import os
@@ -25,74 +25,111 @@
 TMAX = 50.
 FOUR_PI = 4.*np.pi
 
-def _emission_measure(field, data):
-    if data.has_field_parameter("X_H"):
-        X_H = data.get_field_parameter("X_H")
-    else:
-        X_H = 0.75
-    return (data["Density"]/mp)*(data["CellMass"]/mp)*0.5*(1.+X_H)*X_H
-
-def _emission_measure_density(field, data):
-    if data.has_field_parameter("X_H"):
-        X_H = data.get_field_parameter("X_H")
-    else:
-        X_H = 0.75            
-    return (data["Density"]/mp)*(data["Density"]/mp)*0.5*(1.+X_H)*X_H
-
-add_field("EmissionMeasure", function=_emission_measure)
-add_field("EMDensity", function=_emission_measure_density)
-
 class XRayPhotonList(object):
 
-    def __init__(self, photons = None):
+    def __init__(self, photons=None, comm=None, cosmo=None, p_bins=None):
         if photons is None: photons = {}
         self.photons = photons
+        self.comm = comm
+        self.cosmo = cosmo
+        self.p_bins = p_bins
+        self.num_cells = len(photons["x"])
         
+    def keys(self):
+        return self.photons.keys()
+    
+    def items(self):
+        ret = []
+        for k, v in self.photons.items():
+            if k == "Energy":
+                ret.append((k, self[k]))
+            else:
+                ret.append((k,v))
+        return ret
+    
+    def values(self):
+        ret = []
+        for k, v in self.photons.items():
+            if k == "Energy":
+                ret.append(self[k])
+            else:
+                ret.append(v)
+        return ret
+                                
+    def __getitem__(self, key):
+        if key == "Energy":
+            return [self.photons["Energy"][self.p_bins[i]:self.p_bins[i+1]]
+                    for i in xrange(self.num_cells)]
+        else:
+            return self.photons[key]
+    
     @classmethod
-    def from_file(cls, filename):
+    def from_file(cls, filename, cosmology=None):
         """
         Initialize a XRayPhotonList from an HDF5 file given by filename.
         """
         photons = {}
-        
+
+        comm = communication_system.communicators[-1]
+                
         f = h5py.File(filename, "r")
 
         photons["FiducialExposureTime"] = f["/fid_exp_time"].value
         photons["FiducialArea"] = f["/fid_area"].value
-        photons["Redshift"] = f["/redshift"].value
+        photons["FiducialRedshift"] = f["/fid_redshift"].value
         photons["Hubble0"] = f["/hubble"].value
         photons["OmegaMatter"] = f["/omega_matter"].value
         photons["OmegaLambda"] = f["/omega_lambda"].value                    
-        photons["AngularDiameterDistance"] = f["/d_a"].value
-                        
-        photons["x"] = f["/x"][:]
-        photons["y"] = f["/y"][:]
-        photons["z"] = f["/z"][:]
-        photons["dx"] = f["/dx"][:]
-        photons["vx"] = f["/vx"][:]
-        photons["vy"] = f["/vy"][:]
-        photons["vz"] = f["/vz"][:]
-        photons["NumberOfPhotons"] = f["/num_photons"][:].astype("uint64")
+        photons["FiducialAngularDiameterDistance"] = f["/fid_d_a"].value
 
-        photons["Energy"] = f["/energy"][:]
+        num_cells = f["/x"][:].shape[0]
+        start_c = comm.rank*num_cells/comm.size
+        end_c = (comm.rank+1)*num_cells/comm.size
+        
+        photons["x"] = f["/x"][start_c:end_c]
+        photons["y"] = f["/y"][start_c:end_c]
+        photons["z"] = f["/z"][start_c:end_c]
+        photons["dx"] = f["/dx"][start_c:end_c]
+        photons["vx"] = f["/vx"][start_c:end_c]
+        photons["vy"] = f["/vy"][start_c:end_c]
+        photons["vz"] = f["/vz"][start_c:end_c]
+
+        n_ph = f["/num_photons"][:]
+        
+        if comm.rank == 0:
+            start_e = np.uint64(0)
+        else:
+            start_e = n_ph[:start_c].sum()
+        end_e = start_e + np.uint64(n_ph[start_c:end_c].sum())
+
+        photons["NumberOfPhotons"] = n_ph[start_c:end_c]
+
+        p_bins = np.cumsum(photons["NumberOfPhotons"])
+        p_bins = np.insert(p_bins, 0, [np.uint64(0)])
+        
+        photons["Energy"] = f["/energy"][start_e:end_e]
         
         f.close()
-        
-        return cls(photons)
+
+        if cosmology is None:
+            cosmo = Cosmology(HubbleConstantNow=71., OmegaMatterNow=0.27,
+                              OmegaLambdaNow=0.73)
+        else:
+            cosmo = cosmology
+                                        
+        return cls(photons=photons, comm=comm, cosmo=cosmo, p_bins=p_bins)
 
     @classmethod
-    def from_scratch(cls, cell_data, redshift, eff_A,
+    def from_scratch(cls, data_source, redshift, eff_A,
                      exp_time, emission_model, center="c",
                      X_H=0.75, Zmet=0.3, cosmology=None):
         """
         Initialize a XRayPhotonList from a data container. 
         """
-        pf = cell_data.pf
+        pf = data_source.pf
 
         comm = communication_system.communicators[-1]
-        my_rank = comm.rank
-        my_size = comm.size
-        
+                
         vol_scale = pf.units["cm"]**(-3)/np.prod(pf.domain_width)
 
         if cosmology is None:
@@ -100,15 +137,37 @@
                               OmegaLambdaNow=0.73)
         else:
             cosmo = cosmology
-            
+        
         D_A = cosmo.AngularDiameterDistance(0.0,redshift)*cm_per_mpc
         cosmo_fac = 1.0/(FOUR_PI*D_A*D_A*(1.+redshift)**3)
 
-        idxs = np.argsort(cell_data["Temperature"])
+        num_cells = data_source["Temperature"].shape[0]
+        start_c = comm.rank*num_cells/comm.size
+        end_c = (comm.rank+1)*num_cells/comm.size
+
+        kT = data_source["Temperature"][start_c:end_c].copy()/K_per_keV
+        vol = data_source["CellVolume"][start_c:end_c].copy()
+        dx = data_source["dx"][start_c:end_c].copy()
+        EM = (data_source["Density"][start_c:end_c].copy()/mp)**2
+        EM *= 0.5*(1.+X_H)*X_H*vol
+        
+        data_source.clear_data()
+        
+        x = data_source["x"][start_c:end_c].copy()
+        y = data_source["y"][start_c:end_c].copy()
+        z = data_source["z"][start_c:end_c].copy()
+
+        data_source.clear_data()
+                
+        vx = data_source["x-velocity"][start_c:end_c].copy()
+        vy = data_source["y-velocity"][start_c:end_c].copy()
+        vz = data_source["z-velocity"][start_c:end_c].copy()
+        
+        data_source.clear_data()
+                
+        idxs = np.argsort(kT)
         dshape = idxs.shape
 
-        cell_data.set_field_parameter("X_H", X_H)
-
         if center == "c":
             src_ctr = pf.domain_center
         elif center == "max":
@@ -116,49 +175,56 @@
         elif iterable(center):
             src_ctr = center
                     
-        kT_bins = np.linspace(TMIN, max(cell_data["TempkeV"][idxs][-1],
-                                        TMAX), num=N_TBIN+1)
+        kT_bins = np.linspace(TMIN, max(kT[idxs][-1], TMAX), num=N_TBIN+1)
         dkT = kT_bins[1]-kT_bins[0]
-        kT_idxs = np.digitize(cell_data["TempkeV"][idxs], kT_bins)
+        kT_idxs = np.digitize(kT[idxs], kT_bins)
         kT_idxs = np.minimum(np.maximum(1, kT_idxs), N_TBIN) - 1
         bcounts = np.bincount(kT_idxs).astype("int")
         bcounts = bcounts[bcounts > 0]
+        n = int(0)
+        bcell = []
+        ecell = []
+        for bcount in bcounts:
+            bcell.append(n)
+            ecell.append(n+bcount)
+            n += bcount
         kT_idxs = np.unique(kT_idxs)
 
         emission_model.prepare()
         energy = emission_model.ebins
-        de = energy[1]-energy[0]
+        de = emission_model.de
         emid = 0.5*(energy[1:]+energy[:-1])
+        
+        cell_em = EM[idxs]*vol_scale
+        cell_vol = vol[idxs]*vol_scale
+        cell_emd = cell_em/cell_vol
+        
+        number_of_photons = np.zeros(dshape, dtype='uint64')
+        energies = []
+                                
+        pbar = get_pbar("Generating Photons", dshape[0])
 
-        cell_em = cell_data["EmissionMeasure"][idxs]*vol_scale
-        cell_vol = cell_data["CellVolume"][idxs]*vol_scale
-        cell_emd = cell_data["EMDensity"][idxs]
-
-        energies = []
-        number_of_photons = np.zeros(dshape, dtype='uint64')
-
-        pbar = get_pbar("Generating Photons", dshape[0])
-        n = int(0)
+        for i, ikT in enumerate(kT_idxs):
             
-        for i,ikT in enumerate(kT_idxs):
-
             ncells = int(bcounts[i])
+            ibegin = bcell[i]
+            iend = ecell[i]
             kT = kT_bins[ikT] + 0.5*dkT
             
-            em_sum = cell_em[n:n+ncells].sum()
-            vol_sum = cell_vol[n:n+ncells].sum()
-            em_avg = em_sum / vol_sum
+            em_sum = cell_em[ibegin:iend].sum()
+            vol_sum = cell_vol[ibegin:iend].sum()
+            em_avg = em_sum/vol_sum
             
-            tot_norm = cosmo_fac*em_sum / vol_scale
+            tot_norm = cosmo_fac*em_sum/vol_scale
             
             spec = emission_model.get_spectrum(kT, Zmet)
             spec *= tot_norm
             cumspec = np.cumsum(spec)
             counts = cumspec[:]/cumspec[-1]
             tot_ph = cumspec[-1]*eff_A*exp_time
-
-            for icell in xrange(n,n+ncells):
-
+            
+            for icell in xrange(ibegin, iend):
+                
                 cell_norm = tot_ph * (cell_emd[icell]/em_avg) * (cell_vol[icell]/vol_sum)                    
                 cell_Nph = int(cell_norm) + int(np.modf(cell_norm)[0] >= np.random.random())
                 
@@ -169,70 +235,149 @@
                     eidxs = np.searchsorted(counts, randvec)-1
                     cell_e = emid[eidxs]+de*(randvec-counts[eidxs])/(counts[eidxs+1]-counts[eidxs])
                     energies.append(cell_e)
-            
+                            
                 pbar.update(icell)
-                
-            n += ncells
             
         pbar.finish()
-            
-        active_cells = np.where(number_of_photons > 0)[0]
-        num_active_cells = len(active_cells)
 
+        del cell_emd, cell_vol, cell_em
+
+        active_cells = number_of_photons > 0
+        idxs = idxs[active_cells]
+        
         photons = {}
-        photons["x"] = (cell_data["x"][idxs][active_cells]-src_ctr[0])*pf.units["kpc"]
-        photons["y"] = (cell_data["y"][idxs][active_cells]-src_ctr[1])*pf.units["kpc"]
-        photons["z"] = (cell_data["z"][idxs][active_cells]-src_ctr[2])*pf.units["kpc"]
-        photons["vx"] = cell_data["x-velocity"][idxs][active_cells]/cm_per_km
-        photons["vy"] = cell_data["y-velocity"][idxs][active_cells]/cm_per_km
-        photons["vz"] = cell_data["z-velocity"][idxs][active_cells]/cm_per_km
-        photons["dx"] = cell_data["dx"][idxs][active_cells]*pf.units["kpc"]
+        photons["x"] = (x[idxs]-src_ctr[0])*pf.units["kpc"]
+        photons["y"] = (y[idxs]-src_ctr[1])*pf.units["kpc"]
+        photons["z"] = (z[idxs]-src_ctr[2])*pf.units["kpc"]
+        photons["vx"] = vx[idxs]/cm_per_km
+        photons["vy"] = vy[idxs]/cm_per_km
+        photons["vz"] = vz[idxs]/cm_per_km
+        photons["dx"] = dx[idxs]*pf.units["kpc"]
         photons["NumberOfPhotons"] = number_of_photons[active_cells]
         photons["Energy"] = np.concatenate(energies)
-
+                
         photons["FiducialExposureTime"] = exp_time
         photons["FiducialArea"] = eff_A
-        photons["Redshift"] = redshift
+        photons["FiducialRedshift"] = redshift
         photons["Hubble0"] = cosmo.HubbleConstantNow
         photons["OmegaMatter"] = cosmo.OmegaMatterNow
         photons["OmegaLambda"] = cosmo.OmegaLambdaNow
-        photons["AngularDiameterDistance"] = D_A
+        photons["FiducialAngularDiameterDistance"] = D_A/cm_per_mpc
 
-        return cls(photons)
-            
+        p_bins = np.cumsum(photons["NumberOfPhotons"])
+        p_bins = np.insert(p_bins, 0, [np.uint64(0)])
+        
+        return cls(photons=photons, comm=comm, cosmo=cosmo, p_bins=p_bins)
+
     def write_h5_file(self, photonfile):
 
-        f = h5py.File(photonfile, "w")
+        if parallel_capable:
+            
+            mpi_long = get_mpi_type("int64")
+            mpi_double = get_mpi_type("float64")
+        
+            local_num_cells = len(self.photons["x"])
+            sizes_c = self.comm.comm.gather(local_num_cells, root=0)
+            
+            local_num_photons = self.photons["NumberOfPhotons"].sum()
+            sizes_p = self.comm.comm.gather(local_num_photons, root=0)
+            
+            if self.comm.rank == 0:
+                num_cells = sum(sizes_c)
+                num_photons = sum(sizes_p)        
+                disps_c = [sum(sizes_c[:i]) for i in range(len(sizes_c))]
+                disps_p = [sum(sizes_p[:i]) for i in range(len(sizes_p))]
+                x = np.zeros((num_cells))
+                y = np.zeros((num_cells))
+                z = np.zeros((num_cells))
+                vx = np.zeros((num_cells))
+                vy = np.zeros((num_cells))
+                vz = np.zeros((num_cells))
+                dx = np.zeros((num_cells))
+                n_ph = np.zeros((num_cells), dtype="uint64")
+                e = np.zeros((num_photons))
+            else:
+                sizes_c = []
+                sizes_p = []
+                disps_c = []
+                disps_p = []
+                x = np.empty([])
+                y = np.empty([])
+                z = np.empty([])
+                vx = np.empty([])
+                vy = np.empty([])
+                vz = np.empty([])
+                dx = np.empty([])
+                n_ph = np.empty([])
+                e = np.empty([])
+                                                
+            self.comm.comm.Gatherv([self.photons["x"], local_num_cells, mpi_double],
+                                   [x, (sizes_c, disps_c), mpi_double], root=0)
+            self.comm.comm.Gatherv([self.photons["y"], local_num_cells, mpi_double],
+                                   [y, (sizes_c, disps_c), mpi_double], root=0)
+            self.comm.comm.Gatherv([self.photons["z"], local_num_cells, mpi_double],
+                                   [z, (sizes_c, disps_c), mpi_double], root=0)
+            self.comm.comm.Gatherv([self.photons["vx"], local_num_cells, mpi_double],
+                                   [vx, (sizes_c, disps_c), mpi_double], root=0)
+            self.comm.comm.Gatherv([self.photons["vy"], local_num_cells, mpi_double],
+                                   [vy, (sizes_c, disps_c), mpi_double], root=0)
+            self.comm.comm.Gatherv([self.photons["vz"], local_num_cells, mpi_double],
+                                   [vz, (sizes_c, disps_c), mpi_double], root=0)
+            self.comm.comm.Gatherv([self.photons["dx"], local_num_cells, mpi_double],
+                                   [dx, (sizes_c, disps_c), mpi_double], root=0)
+            self.comm.comm.Gatherv([self.photons["NumberOfPhotons"], local_num_cells, mpi_long],
+                                   [n_ph, (sizes_c, disps_c), mpi_long], root=0)
+            self.comm.comm.Gatherv([self.photons["Energy"], local_num_photons, mpi_double],
+                                   [e, (sizes_p, disps_p), mpi_double], root=0) 
 
-        # Scalars
+        else:
+
+            x = self.photons["x"]
+            y = self.photons["y"]
+            z = self.photons["z"]
+            vx = self.photons["vx"]
+            vy = self.photons["vy"]
+            vz = self.photons["vz"]
+            dx = self.photons["dx"]
+            n_ph = self.photons["NumberOfPhotons"]
+            e = self.photons["Energy"]
+                                                
+        if self.comm.rank == 0:
+            
+            f = h5py.File(photonfile, "w")
+
+            # Scalars
        
-        f.create_dataset("fid_area", data=self.photons["FiducialArea"])
-        f.create_dataset("fid_exp_time", data=self.photons["FiducialExposureTime"])
-        f.create_dataset("redshift", data=self.photons["Redshift"])
-        f.create_dataset("omega_matter", data=self.photons["OmegaMatter"])
-        f.create_dataset("omega_lambda", data=self.photons["OmegaLambda"])
-        f.create_dataset("hubble", data=self.photons["Hubble0"])
-        f.create_dataset("d_a", data=self.photons["AngularDiameterDistance"])
+            f.create_dataset("fid_area", data=self.photons["FiducialArea"])
+            f.create_dataset("fid_exp_time", data=self.photons["FiducialExposureTime"])
+            f.create_dataset("fid_redshift", data=self.photons["FiducialRedshift"])
+            f.create_dataset("omega_matter", data=self.photons["OmegaMatter"])
+            f.create_dataset("omega_lambda", data=self.photons["OmegaLambda"])
+            f.create_dataset("hubble", data=self.photons["Hubble0"])
+            f.create_dataset("fid_d_a", data=self.photons["FiducialAngularDiameterDistance"])
         
-        # Arrays
+            # Arrays
 
-        f.create_dataset("x", data=self.photons["x"])
-        f.create_dataset("y", data=self.photons["y"])
-        f.create_dataset("z", data=self.photons["z"])
-        f.create_dataset("vx", data=self.photons["vx"])
-        f.create_dataset("vy", data=self.photons["vy"])
-        f.create_dataset("vz", data=self.photons["vz"])
-        f.create_dataset("dx", data=self.photons["dx"])
-        f.create_dataset("num_photons", data=self.photons["NumberOfPhotons"])
-        f.create_dataset("energy", data=self.photons["Energy"])
-                
-        f.close()
-    
+            f.create_dataset("x", data=x)
+            f.create_dataset("y", data=y)
+            f.create_dataset("z", data=z)
+            f.create_dataset("vx", data=vx)
+            f.create_dataset("vy", data=vy)
+            f.create_dataset("vz", data=vz)
+            f.create_dataset("dx", data=dx)
+            f.create_dataset("num_photons", data=n_ph)
+            f.create_dataset("energy", data=e)
+
+            f.close()
+
+        self.comm.barrier()
+
     def project_photons(self, L, area_new=None, texp_new=None, 
-                        absorb_model=None, psf_sigma=None):
+                        redshift_new=None, absorb_model=None, psf_sigma=None):
         """
         Projects photons onto an image plane given a line of sight. 
         """
+
         dx = self.photons["dx"]
         
         L /= np.sqrt(np.dot(L, L))
@@ -246,16 +391,14 @@
         y_hat = orient.unit_vectors[1]
         z_hat = orient.unit_vectors[2]
 
-        D_A = self.photons["AngularDiameterDistance"] / cm_per_kpc
-        
         n_ph = self.photons["NumberOfPhotons"]
         num_cells = len(n_ph)
         n_ph_tot = n_ph.sum()
-
+        
         eff_area = None
         
-        if texp_new is None and area_new is None:
-            n_obs_tot = n_ph_tot
+        if texp_new is None and area_new is None and redshift_new is None:
+            my_n_obs = n_ph_tot
         else:
             if texp_new is None:
                 Tratio = 1.
@@ -274,50 +417,60 @@
             else:
                 mylog.info("Using constant effective area.")
                 Aratio = area_new/self.photons["FiducialArea"]
-            fak = Aratio*Tratio
+            if redshift_new is None:
+                Zratio = 1.
+                zobs = self.photons["FiducialRedshift"]
+                D_A = self.photons["FiducialAngularDiameterDistance"]*1000.                    
+            else:
+                zobs = redshift_new
+                fid_D_A = self.photons["FiducialAngularDiameterDistance"]*1000.
+                D_A = self.cosmo.AngularDiameterDistance(0.0,zobs)*1000.
+                Zratio = fid_D_A*fid_D_A*(1.+self.photons["FiducialRedshift"]**3) / \
+                         (D_A*D_A*(1.+zobs)**3)
+            fak = Aratio*Tratio*Zratio
             if fak > 1:
                 raise ValueError("Spectrum scaling factor = %g, cannot be greater than unity." % (fak))
-            n_obs_tot = np.uint64(n_ph_tot*fak)
-            
-        mylog.info("Total number of photons to use: %d" % (n_obs_tot))
+            my_n_obs = np.uint64(n_ph_tot*fak)
 
-        x = np.random.uniform(low=-0.5,high=0.5,size=n_obs_tot)
-        y = np.random.uniform(low=-0.5,high=0.5,size=n_obs_tot)
-        z = np.random.uniform(low=-0.5,high=0.5,size=n_obs_tot)
+        n_obs_all = self.comm.mpi_allreduce(my_n_obs)
+        if self.comm.rank == 0: mylog.info("Total number of photons to use: %d" % (n_obs_all))
+        
+        x = np.random.uniform(low=-0.5,high=0.5,size=my_n_obs)
+        y = np.random.uniform(low=-0.5,high=0.5,size=my_n_obs)
+        z = np.random.uniform(low=-0.5,high=0.5,size=my_n_obs)
                     
         vz = self.photons["vx"]*z_hat[0] + \
              self.photons["vy"]*z_hat[1] + \
              self.photons["vz"]*z_hat[2]
         shift = -vz*cm_per_km/clight
         shift = np.sqrt((1.-shift)/(1.+shift))
-        
-        cells = np.concatenate([i*np.ones((n_ph[i]),dtype='uint64') for i in xrange(num_cells)])
-        if n_obs_tot == n_ph_tot:
-            idxs = np.arange(n_ph_tot,dtype='uint64')
-            obs_cells = cells
+
+        if my_n_obs == n_ph_tot:
+            idxs = np.arange(my_n_obs,dtype='uint64')
         else:
-            idxs = np.random.choice(n_ph_tot, size=n_obs_tot, replace=False)
-            obs_cells = cells[idxs]
+            idxs = np.random.permutation(n_ph_tot)[:my_n_obs].astype("uint64")
+        obs_cells = np.searchsorted(self.p_bins, idxs, side='right')-1
+        delta = dx[obs_cells]
 
-        x *= dx[obs_cells]
-        y *= dx[obs_cells]
-        z *= dx[obs_cells]
+        x *= delta
+        y *= delta
+        z *= delta
         x += self.photons["x"][obs_cells]
         y += self.photons["y"][obs_cells]
         z += self.photons["z"][obs_cells]  
         eobs = self.photons["Energy"][idxs]*shift[obs_cells]
-        
+
         xsky = x*x_hat[0] + y*x_hat[1] + z*x_hat[2]
         ysky = x*y_hat[0] + y*y_hat[1] + z*y_hat[2]
-        eobs /= (1.+self.photons["Redshift"])
-                 
+        eobs /= (1.+zobs)
+
         if absorb_model is None:
             not_abs = np.ones(eobs.shape, dtype='bool')
         else:
             mylog.info("Absorbing.")
             absorb_model.prepare()
             energy = absorb_model.ebins
-            de = energy[1]-energy[0]
+            de = absorb_model.de
             emid = 0.5*(energy[1:]+energy[:-1])
             aspec = absorb_model.get_spectrum()
             eidxs = np.searchsorted(emid, eobs)-1
@@ -338,22 +491,24 @@
             randvec = eff_area.max()*np.random.random(eobs.shape)
             detected = randvec < earea
         
-        all_obs = np.logical_and(not_abs, detected)
-
-        num_events = all_obs.sum()
-        
-        mylog.info("Total number of observed photons: %d" % (num_events))
+        detected = np.logical_and(not_abs, detected)
                     
         events = {}
 
-        events["xsky"] = np.rad2deg(xsky[all_obs]/D_A)*3600.
-        events["ysky"] = np.rad2deg(ysky[all_obs]/D_A)*3600.
-        events["eobs"] = eobs[all_obs]
+        events["xsky"] = np.rad2deg(xsky[detected]/D_A)*3600.
+        events["ysky"] = np.rad2deg(ysky[detected]/D_A)*3600.
+        events["eobs"] = eobs[detected]
 
         if psf_sigma is not None:
             events["xsky"] += np.random.normal(sigma=psf_sigma)
             events["ysky"] += np.random.normal(sigma=psf_sigma)
+
+        events = self.comm.par_combine_object(events, datatype="dict", op="cat")
+        
+        num_events = len(events["xsky"])
             
+        if self.comm.rank == 0: mylog.info("Total number of observed photons: %d" % (num_events))
+                        
         if texp_new is None:
             events["ExposureTime"] = self.photons["FiducialExposureTime"]
         else:
@@ -363,10 +518,11 @@
         else:
             events["Area"] = area_new
         events["Hubble0"] = self.photons["Hubble0"]
-        events["Redshift"] = self.photons["Redshift"]
+        events["Redshift"] = zobs
         events["OmegaMatter"] = self.photons["OmegaMatter"]
         events["OmegaLambda"] = self.photons["OmegaLambda"] 
-        
+        events["AngularDiameterDistance"] = D_A/1000.
+                
         return XRayEventList(events)
 
 class XRayEventList(object) :
@@ -376,6 +532,18 @@
         if events is None : events = {}
         self.events = events
         self.num_events = events["xsky"].shape[0]
+
+    def keys(self):
+        return self.events.keys()
+
+    def items(self):
+        return self.events.items()
+
+    def values(self):
+        return self.events.values()
+    
+    def __getitem__(self,key):
+        return self.events[key]
         
     @classmethod
     def from_h5_file(cls, h5file):
@@ -392,6 +560,7 @@
         events["Redshift"] = f["/redshift"].value
         events["OmegaMatter"] = f["/omega_matter"].value
         events["OmegaLambda"] = f["/omega_lambda"].value
+        events["AngularDiameterDistance"] = f["/d_a"].value
         
         events["xsky"] = f["/xsky"][:]
         events["ysky"] = f["/ysky"][:]
@@ -418,16 +587,30 @@
         events["Redshift"] = tblhdu.header["REDSHIFT"]
         events["OmegaMatter"] = tblhdu.header["OMEGA_M"]
         events["OmegaLambda"] = tblhdu.header["OMEGA_L"]
-
+        events["AngularDiameterDistance"] = tblhdu.header["D_A"]
+        
         events["xsky"] = tblhdu.data.field("POS_X")
         events["ysky"] = tblhdu.data.field("POS_Y")
         events["eobs"] = tblhdu.data.field("ENERGY")
         
         return cls(events)
 
+    @classmethod
+    def join_events(cls, events1, events2):
+        events = {}
+        for item1, item2 in zip(events1.items(), events2.items()):
+            k1, v1 = item1
+            k2, v2 = item2
+            if isinstance(v1, np.ndarray):
+                events[k1] = np.concatenate([v1,v2])
+            else:
+                events[k1] = v1            
+        return cls(events)
+    
     def convolve_with_response(self, respfile):
         pass
-    
+
+    @parallel_root_only
     def write_fits_file(self, fitsfile, clobber=False):
         """
         Write events to a FITS binary table file.
@@ -448,9 +631,11 @@
         tbhdu.header.update("HUBBLE", self.events["Hubble0"])
         tbhdu.header.update("OMEGA_M", self.events["OmegaMatter"])
         tbhdu.header.update("OMEGA_L", self.events["OmegaLambda"])
-        
+        tbhdu.header.update("D_A", self.evenets["AngularDiameterDistance"])
+                
         tbhdu.writeto(fitsfile, clobber=clobber)
-                
+
+    @parallel_root_only    
     def write_simput_file(self, prefix, clobber=False, e_min=None, e_max=None):
         """
         Write events to a SIMPUT file.
@@ -519,6 +704,7 @@
                 
         wrhdu.writeto(simputfile, clobber=clobber)
 
+    @parallel_root_only
     def write_h5_file(self, h5file):
         """
         Write a XRayEventList to the HDF5 file given by h5file.
@@ -531,12 +717,14 @@
         f.create_dataset("/hubble", data=self.events["Hubble0"])
         f.create_dataset("/omega_matter", data=self.events["OmegaMatter"])
         f.create_dataset("/omega_lambda", data=self.events["OmegaLambda"])        
+        f.create_dataset("/d_a", data=self.events["AngularDiameterDistance"])        
         f.create_dataset("/xsky", data=self.events["xsky"])
         f.create_dataset("/ysky", data=self.events["ysky"])
         f.create_dataset("/eobs", data=self.events["eobs"])
                         
         f.close()
 
+    @parallel_root_only
     def write_fits_image(self, imagefile, width, nx, center,
                          clobber=False, gzip_file=False,
                          emin=None, emax=None):
@@ -586,6 +774,7 @@
             if (clobber) : clob="-f"
             os.system("gzip "+clob+" %s.fits" % (prefix))
                                     
+    @parallel_root_only
     def write_spectrum(self, specfile, emin, emax, nchan, clobber=False):
         
         spec, ee = np.histogram(self.events["eobs"], bins=nchan, range=(emin, emax))


https://bitbucket.org/yt_analysis/yt/commits/623221c09038/
Changeset:   623221c09038
Branch:      yt
User:        jzuhone
Date:        2013-08-24 15:28:46
Summary:     XSpec-free APEC model.
Affected #:  1 file

diff -r aee1f39c321a841f1ab1e01817905a10b3558d55 -r 623221c090387db6a55c9ed9c94023090527b939 yt/analysis_modules/synthetic_xray_obs/photon_models.py
--- a/yt/analysis_modules/synthetic_xray_obs/photon_models.py
+++ b/yt/analysis_modules/synthetic_xray_obs/photon_models.py
@@ -1,12 +1,24 @@
 import numpy as np
+import os
 from yt.funcs import *
 import h5py
-
+try:
+    import pyfits
+except:
+    try:
+        import astropy.io.fits as pyfits
+    except:
+        mylog.error("Your mom doesn't have pyFITS")
 try:
     import xspec
 except ImportError:
     mylog.warning("You don't have PyXSpec installed. Some models won't be available.")
-                
+from scipy.integrate import cumtrapz
+from scipy import stats
+from yt.utilities.physical_constants import hcgs, clight, erg_per_keV, amu_cgs
+
+hc = 1.0e8*hcgs*clight/erg_per_keV
+
 class PhotonModel(object):
 
     def __init__(self, emin, emax, nchan):
@@ -64,36 +76,123 @@
 
 class TableApecModel(PhotonModel):
 
-    def __init__(self, filename):
-        if not os.path.exists(filename):
-            raise IOError("File does not exist: %s." % filename)
-        self.filename = filename
-        f = h5py.File(self.filename,"r")
-        self.T_vals = f["kT"][:]
-        self.Z_vals = f["Zmet"][:]
-        emin        = f["emin"].value
-        emax        = f["emax"].value
-        self.spec_table = f["spectrum"][:,:,:]
-        nchan = self.spec_table.shape[-1]
-        f.close()
-        self.dT = self.T_vals[1]-self.T_vals[0]
-        self.dZ = self.Z_vals[1]-self.Z_vals[0]
+    def __init__(self, apec_root, emin, emax, nchan,
+                 apec_vers="2.0.2", thermal_broad=False):
+        self.apec_root = apec_root
+        self.apec_prefix = "apec_v"+apec_vers
+        self.cocofile = os.path.join(self.apec_root,
+                                     self.apec_prefix+"_coco.fits")
+        self.linefile = os.path.join(self.apec_root,
+                                     self.apec_prefix+"_line.fits")
         PhotonModel.__init__(self, emin, emax, nchan)
+        self.wvbins = hc/self.ebins[::-1]
+        # H, He, and trace elements
+        self.cosmic_elem = [1,2,3,4,5,9,11,15,17,19,21,22,23,24,25,27,29,30]
+        # Non-trace metals
+        self.metal_elem = [6,7,8,10,12,13,14,16,18,20,26,28]
+        self.thermal_broad = thermal_broad
+        self.A = np.array([0.0,1.00794,4.00262,6.941,9.012182,10.811,
+                           12.0107,14.0067,15.9994,18.9984,20.1797,
+                           22.9898,24.3050,26.9815,28.0855,30.9738,
+                           32.0650,35.4530,39.9480,39.0983,40.0780,
+                           44.9559,47.8670,50.9415,51.9961,54.9380,
+                           55.8450,58.9332,58.6934,63.5460,65.3800])
         
     def prepare(self):
-        pass
+        try:
+            self.line_handle = pyfits.open(self.linefile)
+        except IOError:
+            mylog.error("LINE file %s does not exist" % (self.linefile))
+        try:
+            self.coco_handle = pyfits.open(self.cocofile)
+        except IOError:
+            mylog.error("COCO file %s does not exist" % (self.cocofile))
+        self.Tvals = self.line_handle[1].data.field("kT")
+        self.dTvals = np.diff(self.Tvals)
+        self.minlam = self.wvbins.min()
+        self.maxlam = self.wvbins.max()
+
+    def expand_E_grid(self, Eedges, Ncont, Econt, cont):
+        # linearly interpolate onto wvedges
+        # All energies should be in keV
+        newx = np.array(Econt[:Ncont])
+        newx = np.append(newx,Eedges)
+        newxargs = newx.argsort()
+        newx.sort()
+        newy = np.interp(newx, Econt, cont)
+        # simple integration
+        ct = cumtrapz(newy,newx)
+        ret = np.zeros((self.nchan))
+        iinew = np.where(newxargs==Ncont)[0][0]
+        for i in range(self.nchan):
+            ind = i + 1 + Ncont
+            iiold = iinew
+            iinew = np.where(newxargs==ind)[0][0]
+            ret[i] = ct[iinew-1]-ct[iiold-1]
+        return ret
+
+    def make_spectrum(self, element, tindex):
+        
+        tmpspec = np.zeros((self.nchan))
+        
+        i = np.where((self.line_handle[tindex].data.field('element')==element) &
+                     (self.line_handle[tindex].data.field('lambda') > self.minlam) &
+                     (self.line_handle[tindex].data.field('lambda') < self.maxlam))[0]
+
+        vec = np.zeros((self.nchan))
+        E0 = hc/self.line_handle[tindex].data.field('lambda')[i]
+        amp = self.line_handle[tindex].data.field('epsilon')[i]
+        if self.thermal_broad:
+            vec = np.zeros((self.nchan))
+            sigma = E0*np.sqrt(self.Tvals[tindex]*erg_per_keV/(self.A[element]*amu_cgs))/clight
+            for E, sig, a in zip(E0, sigma, amp):
+                cdf = stats.norm(E,sig).cdf(self.ebins)
+                vec += np.diff(cdf)*a
+        else:
+            ie = np.searchsorted(self.ebins, E0, side='right')-1
+            for i,a in zip(ie,amp): vec[i] += a
+        tmpspec += vec
+
+        ind = np.where((self.coco_handle[tindex].data.field('Z')==element) &
+                       (self.coco_handle[tindex].data.field('rmJ')==0))[0]
+        if len(ind)==0:
+            return tmpspec
+        else:
+            ind=ind[0]
+                                                    
+        n_cont=self.coco_handle[tindex].data.field('N_Cont')[ind]
+        e_cont=self.coco_handle[tindex].data.field('E_Cont')[ind][:n_cont]
+        continuum = self.coco_handle[tindex].data.field('Continuum')[ind][:n_cont]
+        
+        tmpspec += self.expand_E_grid(self.ebins, n_cont, e_cont, continuum)
+        
+        n_pseudo=self.coco_handle[tindex].data.field('N_Pseudo')[ind]
+        e_pseudo=self.coco_handle[tindex].data.field('E_Pseudo')[ind][:n_pseudo]
+        pseudo = self.coco_handle[tindex].data.field('Pseudo')[ind][:n_pseudo]
+        
+        tmpspec += self.expand_E_grid(self.ebins, n_pseudo, e_pseudo, pseudo)
+                                                        
+        return tmpspec
 
     def get_spectrum(self, kT, Zmet):
-        iz = np.searchsorted(self.Z_vals, Zmet)-1
-        dz = (Zmet-self.Z_vals[iz])/self.dZ
-        it = np.searchsorted(self.T_vals, kT)-1
-        dt = (kT-self.T_vals[it])/self.dT
-        spec = self.spec_table[it+1,iz+1,:]*dt*dz + \
-               self.spec_table[it,iz,:]*(1.-dt)*(1.-dz) + \
-               self.spec_table[it,iz+1,:]*(1.-dt)*dz + \
-               self.spec_table[it+1,iz,:]*dt*(1.-dz)
-        return spec
-    
+        cspec_l = np.zeros((self.nchan))
+        mspec_l = np.zeros((self.nchan))
+        cspec_r = np.zeros((self.nchan))
+        mspec_r = np.zeros((self.nchan))
+        tindex = np.searchsorted(self.Tvals, kT)-1
+        dT = (kT-self.Tvals[tindex])/self.dTvals[tindex]
+        # First do H,He, and trace elements
+        for elem in self.cosmic_elem:
+            cspec_l += self.make_spectrum(elem, tindex+2)
+            cspec_r += self.make_spectrum(elem, tindex+3)            
+        # Next do the metals
+        for elem in self.metal_elem:
+            mspec_l += self.make_spectrum(elem, tindex+2)
+            mspec_r += self.make_spectrum(elem, tindex+3)
+        cosmic_spec = cspec_l*(1.-dT)+cspec_r*dT
+        metal_spec = mspec_l*(1.-dT)+mspec_r*dT        
+        return cosmic_spec+Zmet*metal_spec
+
 class TableAbsorbModel(PhotonModel):
 
     def __init__(self, filename):
@@ -101,9 +200,9 @@
             raise IOError("File does not exist: %s." % filename)
         self.filename = filename
         f = h5py.File(self.filename,"r")
-        emin = f["emin"].value
-        emax = f["emax"].value
-        self.abs = f["spectrum"][:]
+        emin = f["energ_lo"][:].min()
+        emax = f["energ_hi"][:].max()
+        self.abs = f["absorb_coeff"][:]
         nchan = self.abs.shape[0]
         f.close()
         PhotonModel.__init__(self, emin, emax, nchan)


https://bitbucket.org/yt_analysis/yt/commits/532068b4baf0/
Changeset:   532068b4baf0
Branch:      yt
User:        jzuhone
Date:        2013-08-26 02:25:44
Summary:     1) Some name changes
2) Don't need to store cosmological parameters
3)
Affected #:  5 files

diff -r 623221c090387db6a55c9ed9c94023090527b939 -r 532068b4baf0c6281fb5ed45981629760df1cbcc yt/analysis_modules/api.py
--- a/yt/analysis_modules/api.py
+++ b/yt/analysis_modules/api.py
@@ -122,8 +122,8 @@
     RadMC3DWriter
 
 from .synthetic_xray_obs.api import \
-     XRayPhotonList, \
-     XRayEventList, \
+     PhotonList, \
+     EventList, \
      XSpecThermalModel, \
      XSpecAbsorbModel, \
      TableApecModel, \

diff -r 623221c090387db6a55c9ed9c94023090527b939 -r 532068b4baf0c6281fb5ed45981629760df1cbcc yt/analysis_modules/synthetic_xray_obs/api.py
--- a/yt/analysis_modules/synthetic_xray_obs/api.py
+++ b/yt/analysis_modules/synthetic_xray_obs/api.py
@@ -25,8 +25,8 @@
 """
 
 from .photon_simulator import \
-     XRayPhotonList, \
-     XRayEventList
+     PhotonList, \
+     EventList
 
 from .photon_models import \
      XSpecThermalModel, \

diff -r 623221c090387db6a55c9ed9c94023090527b939 -r 532068b4baf0c6281fb5ed45981629760df1cbcc yt/analysis_modules/synthetic_xray_obs/photon_simulator.py
--- a/yt/analysis_modules/synthetic_xray_obs/photon_simulator.py
+++ b/yt/analysis_modules/synthetic_xray_obs/photon_simulator.py
@@ -25,12 +25,13 @@
 TMAX = 50.
 FOUR_PI = 4.*np.pi
 
-class XRayPhotonList(object):
+comm = communication_system.communicators[-1]
+        
+class PhotonList(object):
 
-    def __init__(self, photons=None, comm=None, cosmo=None, p_bins=None):
+    def __init__(self, photons=None, cosmo=None, p_bins=None):
         if photons is None: photons = {}
         self.photons = photons
-        self.comm = comm
         self.cosmo = cosmo
         self.p_bins = p_bins
         self.num_cells = len(photons["x"])
@@ -66,20 +67,15 @@
     @classmethod
     def from_file(cls, filename, cosmology=None):
         """
-        Initialize a XRayPhotonList from an HDF5 file given by filename.
+        Initialize a PhotonList from an HDF5 file given by filename.
         """
         photons = {}
 
-        comm = communication_system.communicators[-1]
-                
         f = h5py.File(filename, "r")
 
         photons["FiducialExposureTime"] = f["/fid_exp_time"].value
         photons["FiducialArea"] = f["/fid_area"].value
         photons["FiducialRedshift"] = f["/fid_redshift"].value
-        photons["Hubble0"] = f["/hubble"].value
-        photons["OmegaMatter"] = f["/omega_matter"].value
-        photons["OmegaLambda"] = f["/omega_lambda"].value                    
         photons["FiducialAngularDiameterDistance"] = f["/fid_d_a"].value
 
         num_cells = f["/x"][:].shape[0]
@@ -110,36 +106,32 @@
         photons["Energy"] = f["/energy"][start_e:end_e]
         
         f.close()
+                                        
+        return cls(photons=photons, cosmo=cosmology, p_bins=p_bins)
 
-        if cosmology is None:
+    @classmethod
+    def from_thermal_model(cls, data_source, redshift, eff_A,
+                           exp_time, emission_model, center="c",
+                           X_H=0.75, Zmet=0.3, dist=None, cosmology=None):
+        """
+        Initialize a PhotonList from a data container. 
+        """
+        pf = data_source.pf
+                
+        vol_scale = pf.units["cm"]**(-3)/np.prod(pf.domain_width)
+
+        if cosmology is None and dist is None:
             cosmo = Cosmology(HubbleConstantNow=71., OmegaMatterNow=0.27,
                               OmegaLambdaNow=0.73)
         else:
-            cosmo = cosmology
-                                        
-        return cls(photons=photons, comm=comm, cosmo=cosmo, p_bins=p_bins)
-
-    @classmethod
-    def from_scratch(cls, data_source, redshift, eff_A,
-                     exp_time, emission_model, center="c",
-                     X_H=0.75, Zmet=0.3, cosmology=None):
-        """
-        Initialize a XRayPhotonList from a data container. 
-        """
-        pf = data_source.pf
-
-        comm = communication_system.communicators[-1]
-                
-        vol_scale = pf.units["cm"]**(-3)/np.prod(pf.domain_width)
-
-        if cosmology is None:
-            cosmo = Cosmology(HubbleConstantNow=71., OmegaMatterNow=0.27,
-                              OmegaLambdaNow=0.73)
-        else:
-            cosmo = cosmology
-        
-        D_A = cosmo.AngularDiameterDistance(0.0,redshift)*cm_per_mpc
-        cosmo_fac = 1.0/(FOUR_PI*D_A*D_A*(1.+redshift)**3)
+            if cosmology is None:
+                D_A = dist*cm_per_mpc
+                cosmo = None
+            else:
+                cosmo = cosmology
+        if cosmo is not None:
+            D_A = cosmo.AngularDiameterDistance(0.0,redshift)*cm_per_mpc
+        dist_fac = 1.0/(FOUR_PI*D_A*D_A*(1.+redshift)**3)
 
         num_cells = data_source["Temperature"].shape[0]
         start_c = comm.rank*num_cells/comm.size
@@ -215,7 +207,7 @@
             vol_sum = cell_vol[ibegin:iend].sum()
             em_avg = em_sum/vol_sum
             
-            tot_norm = cosmo_fac*em_sum/vol_scale
+            tot_norm = dist_fac*em_sum/vol_scale
             
             spec = emission_model.get_spectrum(kT, Zmet)
             spec *= tot_norm
@@ -259,16 +251,43 @@
         photons["FiducialExposureTime"] = exp_time
         photons["FiducialArea"] = eff_A
         photons["FiducialRedshift"] = redshift
-        photons["Hubble0"] = cosmo.HubbleConstantNow
-        photons["OmegaMatter"] = cosmo.OmegaMatterNow
-        photons["OmegaLambda"] = cosmo.OmegaLambdaNow
         photons["FiducialAngularDiameterDistance"] = D_A/cm_per_mpc
 
         p_bins = np.cumsum(photons["NumberOfPhotons"])
         p_bins = np.insert(p_bins, 0, [np.uint64(0)])
         
-        return cls(photons=photons, comm=comm, cosmo=cosmo, p_bins=p_bins)
+        return cls(photons=photons, cosmo=cosmo, p_bins=p_bins)
 
+    @classmethod
+    def from_user_model(cls, data_source, redshift, eff_A,
+                        exp_time, user_function, parameters={}
+                        dist=None, cosmology=None):
+
+        if cosmology is None and dist is None:
+            cosmo = Cosmology(HubbleConstantNow=71., OmegaMatterNow=0.27,
+                              OmegaLambdaNow=0.73)
+        else:
+            if cosmology is None:
+                D_A = dist*cm_per_mpc
+                cosmo = None
+            else:
+                cosmo = cosmology
+        if cosmo is not None:
+            D_A = cosmo.AngularDiameterDistance(0.0,redshift)*cm_per_mpc
+                    
+        photons = user_function(data_source, redshift, eff_A,
+                                exp_time, D_A, parameters)
+        
+        photons["FiducialExposureTime"] = exp_time
+        photons["FiducialArea"] = eff_A
+        photons["FiducialRedshift"] = redshift
+        photons["FiducialAngularDiameterDistance"] = D_A
+
+        p_bins = np.cumsum(photons["NumberOfPhotons"])
+        p_bins = np.insert(p_bins, 0, [np.uint64(0)])
+                        
+        return cls(photons=photons, cosmo=cosmo, p_bins=p_bins)
+        
     def write_h5_file(self, photonfile):
 
         if parallel_capable:
@@ -277,12 +296,12 @@
             mpi_double = get_mpi_type("float64")
         
             local_num_cells = len(self.photons["x"])
-            sizes_c = self.comm.comm.gather(local_num_cells, root=0)
+            sizes_c = comm.comm.gather(local_num_cells, root=0)
             
             local_num_photons = self.photons["NumberOfPhotons"].sum()
-            sizes_p = self.comm.comm.gather(local_num_photons, root=0)
+            sizes_p = comm.comm.gather(local_num_photons, root=0)
             
-            if self.comm.rank == 0:
+            if comm.rank == 0:
                 num_cells = sum(sizes_c)
                 num_photons = sum(sizes_p)        
                 disps_c = [sum(sizes_c[:i]) for i in range(len(sizes_c))]
@@ -311,24 +330,24 @@
                 n_ph = np.empty([])
                 e = np.empty([])
                                                 
-            self.comm.comm.Gatherv([self.photons["x"], local_num_cells, mpi_double],
-                                   [x, (sizes_c, disps_c), mpi_double], root=0)
-            self.comm.comm.Gatherv([self.photons["y"], local_num_cells, mpi_double],
-                                   [y, (sizes_c, disps_c), mpi_double], root=0)
-            self.comm.comm.Gatherv([self.photons["z"], local_num_cells, mpi_double],
-                                   [z, (sizes_c, disps_c), mpi_double], root=0)
-            self.comm.comm.Gatherv([self.photons["vx"], local_num_cells, mpi_double],
-                                   [vx, (sizes_c, disps_c), mpi_double], root=0)
-            self.comm.comm.Gatherv([self.photons["vy"], local_num_cells, mpi_double],
-                                   [vy, (sizes_c, disps_c), mpi_double], root=0)
-            self.comm.comm.Gatherv([self.photons["vz"], local_num_cells, mpi_double],
-                                   [vz, (sizes_c, disps_c), mpi_double], root=0)
-            self.comm.comm.Gatherv([self.photons["dx"], local_num_cells, mpi_double],
-                                   [dx, (sizes_c, disps_c), mpi_double], root=0)
-            self.comm.comm.Gatherv([self.photons["NumberOfPhotons"], local_num_cells, mpi_long],
-                                   [n_ph, (sizes_c, disps_c), mpi_long], root=0)
-            self.comm.comm.Gatherv([self.photons["Energy"], local_num_photons, mpi_double],
-                                   [e, (sizes_p, disps_p), mpi_double], root=0) 
+            comm.comm.Gatherv([self.photons["x"], local_num_cells, mpi_double],
+                              [x, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["y"], local_num_cells, mpi_double],
+                              [y, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["z"], local_num_cells, mpi_double],
+                              [z, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["vx"], local_num_cells, mpi_double],
+                              [vx, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["vy"], local_num_cells, mpi_double],
+                              [vy, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["vz"], local_num_cells, mpi_double],
+                              [vz, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["dx"], local_num_cells, mpi_double],
+                              [dx, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["NumberOfPhotons"], local_num_cells, mpi_long],
+                              [n_ph, (sizes_c, disps_c), mpi_long], root=0)
+            comm.comm.Gatherv([self.photons["Energy"], local_num_photons, mpi_double],
+                              [e, (sizes_p, disps_p), mpi_double], root=0) 
 
         else:
 
@@ -342,7 +361,7 @@
             n_ph = self.photons["NumberOfPhotons"]
             e = self.photons["Energy"]
                                                 
-        if self.comm.rank == 0:
+        if comm.rank == 0:
             
             f = h5py.File(photonfile, "w")
 
@@ -351,9 +370,6 @@
             f.create_dataset("fid_area", data=self.photons["FiducialArea"])
             f.create_dataset("fid_exp_time", data=self.photons["FiducialExposureTime"])
             f.create_dataset("fid_redshift", data=self.photons["FiducialRedshift"])
-            f.create_dataset("omega_matter", data=self.photons["OmegaMatter"])
-            f.create_dataset("omega_lambda", data=self.photons["OmegaLambda"])
-            f.create_dataset("hubble", data=self.photons["Hubble0"])
             f.create_dataset("fid_d_a", data=self.photons["FiducialAngularDiameterDistance"])
         
             # Arrays
@@ -370,14 +386,21 @@
 
             f.close()
 
-        self.comm.barrier()
+        comm.barrier()
 
     def project_photons(self, L, area_new=None, texp_new=None, 
-                        redshift_new=None, absorb_model=None, psf_sigma=None):
+                        redshift_new=None, dist_new=None,
+                        absorb_model=None, psf_sigma=None):
         """
         Projects photons onto an image plane given a line of sight. 
         """
 
+        if redshift_new is not None and dist_new is not None:
+            mylog.error("You may specify a new redshift or distance, but not both!")
+
+        if redshift_new is not Done and self.cosmo is None:
+            mylog.error("Specified a new redshift, but no cosmology!")
+            
         dx = self.photons["dx"]
         
         L /= np.sqrt(np.dot(L, L))
@@ -397,7 +420,8 @@
         
         eff_area = None
         
-        if texp_new is None and area_new is None and redshift_new is None:
+        if (texp_new is None and area_new is None and
+            redshift_new is None and dist_new is None):
             my_n_obs = n_ph_tot
         else:
             if texp_new is None:
@@ -417,23 +441,27 @@
             else:
                 mylog.info("Using constant effective area.")
                 Aratio = area_new/self.photons["FiducialArea"]
-            if redshift_new is None:
-                Zratio = 1.
+            if redshift_new is None and dist_new is None:
+                Dratio = 1.
                 zobs = self.photons["FiducialRedshift"]
                 D_A = self.photons["FiducialAngularDiameterDistance"]*1000.                    
             else:
-                zobs = redshift_new
+                if redshift_new is None:
+                    zobs = self.photons["FiducialRedshift"]
+                    D_A = dist_new*1000.
+                else:
+                    zobs = redshift_new
+                    D_A = self.cosmo.AngularDiameterDistance(0.0,zobs)*1000.
                 fid_D_A = self.photons["FiducialAngularDiameterDistance"]*1000.
-                D_A = self.cosmo.AngularDiameterDistance(0.0,zobs)*1000.
-                Zratio = fid_D_A*fid_D_A*(1.+self.photons["FiducialRedshift"]**3) / \
+                Dratio = fid_D_A*fid_D_A*(1.+self.photons["FiducialRedshift"]**3) / \
                          (D_A*D_A*(1.+zobs)**3)
-            fak = Aratio*Tratio*Zratio
+            fak = Aratio*Tratio*Dratio
             if fak > 1:
                 raise ValueError("Spectrum scaling factor = %g, cannot be greater than unity." % (fak))
             my_n_obs = np.uint64(n_ph_tot*fak)
 
-        n_obs_all = self.comm.mpi_allreduce(my_n_obs)
-        if self.comm.rank == 0: mylog.info("Total number of photons to use: %d" % (n_obs_all))
+        n_obs_all = comm.mpi_allreduce(my_n_obs)
+        if comm.rank == 0: mylog.info("Total number of photons to use: %d" % (n_obs_all))
         
         x = np.random.uniform(low=-0.5,high=0.5,size=my_n_obs)
         y = np.random.uniform(low=-0.5,high=0.5,size=my_n_obs)
@@ -503,11 +531,11 @@
             events["xsky"] += np.random.normal(sigma=psf_sigma)
             events["ysky"] += np.random.normal(sigma=psf_sigma)
 
-        events = self.comm.par_combine_object(events, datatype="dict", op="cat")
+        events = comm.par_combine_object(events, datatype="dict", op="cat")
         
         num_events = len(events["xsky"])
             
-        if self.comm.rank == 0: mylog.info("Total number of observed photons: %d" % (num_events))
+        if comm.rank == 0: mylog.info("Total number of observed photons: %d" % (num_events))
                         
         if texp_new is None:
             events["ExposureTime"] = self.photons["FiducialExposureTime"]
@@ -517,15 +545,12 @@
             events["Area"] = self.photons["FiducialArea"]
         else:
             events["Area"] = area_new
-        events["Hubble0"] = self.photons["Hubble0"]
         events["Redshift"] = zobs
-        events["OmegaMatter"] = self.photons["OmegaMatter"]
-        events["OmegaLambda"] = self.photons["OmegaLambda"] 
         events["AngularDiameterDistance"] = D_A/1000.
                 
-        return XRayEventList(events)
+        return EventList(events)
 
-class XRayEventList(object) :
+class EventList(object) :
 
     def __init__(self, events = None) :
 
@@ -548,7 +573,7 @@
     @classmethod
     def from_h5_file(cls, h5file):
         """
-        Initialize a XRayEventList from a HDF5 file with filename h5file.
+        Initialize an EventList from a HDF5 file with filename h5file.
         """
         events = {}
         
@@ -556,10 +581,7 @@
 
         events["ExposureTime"] = f["/exp_time"].value
         events["Area"] = f["/area"].value
-        events["Hubble0"] = f["/hubble"].value
         events["Redshift"] = f["/redshift"].value
-        events["OmegaMatter"] = f["/omega_matter"].value
-        events["OmegaLambda"] = f["/omega_lambda"].value
         events["AngularDiameterDistance"] = f["/d_a"].value
         
         events["xsky"] = f["/xsky"][:]
@@ -573,7 +595,7 @@
     @classmethod
     def from_fits_file(cls, fitsfile):
         """
-        Initialize a XRayEventList from a FITS file with filename fitsfile.
+        Initialize an EventList from a FITS file with filename fitsfile.
         """
         hdulist = pyfits.open(fitsfile)
 
@@ -583,10 +605,7 @@
         
         events["ExposureTime"] = tblhdu.header["EXPOSURE"]
         events["Area"] = tblhdu.header["AREA"]
-        events["Hubble0"] = tblhdu.header["HUBBLE"]
         events["Redshift"] = tblhdu.header["REDSHIFT"]
-        events["OmegaMatter"] = tblhdu.header["OMEGA_M"]
-        events["OmegaLambda"] = tblhdu.header["OMEGA_L"]
         events["AngularDiameterDistance"] = tblhdu.header["D_A"]
         
         events["xsky"] = tblhdu.data.field("POS_X")
@@ -628,9 +647,6 @@
 
         tbhdu.header.update("EXPOSURE", self.events["ExposureTime"])
         tbhdu.header.update("AREA", self.events["Area"])
-        tbhdu.header.update("HUBBLE", self.events["Hubble0"])
-        tbhdu.header.update("OMEGA_M", self.events["OmegaMatter"])
-        tbhdu.header.update("OMEGA_L", self.events["OmegaLambda"])
         tbhdu.header.update("D_A", self.evenets["AngularDiameterDistance"])
                 
         tbhdu.writeto(fitsfile, clobber=clobber)
@@ -707,16 +723,13 @@
     @parallel_root_only
     def write_h5_file(self, h5file):
         """
-        Write a XRayEventList to the HDF5 file given by h5file.
+        Write an EventList to the HDF5 file given by h5file.
         """
         f = h5py.File(h5file, "w")
 
         f.create_dataset("/exp_time", data=self.events["ExposureTime"])
         f.create_dataset("/area", data=self.events["Area"])
         f.create_dataset("/redshift", data=self.events["Redshift"])
-        f.create_dataset("/hubble", data=self.events["Hubble0"])
-        f.create_dataset("/omega_matter", data=self.events["OmegaMatter"])
-        f.create_dataset("/omega_lambda", data=self.events["OmegaLambda"])        
         f.create_dataset("/d_a", data=self.events["AngularDiameterDistance"])        
         f.create_dataset("/xsky", data=self.events["xsky"])
         f.create_dataset("/ysky", data=self.events["ysky"])
@@ -751,7 +764,7 @@
                                            self.events["ysky"][mask],
                                            bins=[xbins,xbins])
         
-        hdu = pyfits.PrimaryHDU(H.T[::-1,::])
+        hdu = pyfits.PrimaryHDU(H.T[::,::])
 
         hdu.header.update("MTYPE1", "EQPOS")
         hdu.header.update("MFORM1", "RA,DEC")

diff -r 623221c090387db6a55c9ed9c94023090527b939 -r 532068b4baf0c6281fb5ed45981629760df1cbcc yt/visualization/fixed_resolution.py
--- a/yt/visualization/fixed_resolution.py
+++ b/yt/visualization/fixed_resolution.py
@@ -273,8 +273,9 @@
             output.create_dataset(field,data=self[field])
         output.close()
 
-    def export_fits(self, filename_prefix, fields = None, clobber=False,
-                    other_keys=None, gzip_file=False, units="1"):
+    def export_fits(self, filename_prefix, fields=None, clobber=False,
+                    other_keys=None, gzip_file=False, units="1",
+                    sky_center=(0.0,0.0), D_A=None):
 
         """
         This will export a set of FITS images of either the fields specified
@@ -294,7 +295,7 @@
         Parameters
         ----------
         filename_prefix : string
-            This prefix will be prepended to every FITS file name.
+            This prefix will be prepended to the FITS file name.
         fields : list of strings
             These fields will be pixelized and output.
         clobber : boolean
@@ -302,14 +303,37 @@
         other_keys : dictionary, optional
             A set of header keys and values to write into the FITS header.
         gzip_file : boolean, optional
-            gzip the file after writing, default False
+            Gzip the file after writing, default False
         units : string, optional
-            the length units that the coordinates are written in, default '1'
+            The length units that the coordinates are written in, default '1'.
+            If units are set to "sky" then assume that sky coordinates are
+            requested.
+        sky_center : array_like, optional
+            Center of the image in (ra,dec) in degrees if sky coordinates are
+            requested.
+        D_A : float or tuple, optional
+            Angular diameter distance, given in code units as a float or
+            a tuple containing the value and the length unit. Required if
+            using sky coordinates.
         """
-        
-        import pyfits
+
+        try:
+            import pyfits
+        except:
+            try:
+                import astropy.io.fits as pyfits
+            except:
+                mylog.error("You don't have pyFITS or AstroPy installed!")
+                
         from os import system
-        
+
+        if units == "deg" and D_A is None:
+            mylog.error("Sky coordinates require an angular diameter distance. Please specify D_A.")
+
+        if iterable(D_A):
+            dist = D_A[0]/self.pf.units[D_A[1]]
+        else:
+            dist = D_A
         extra_fields = ['x','y','z','px','py','pz','pdx','pdy','pdz','weight_field']
         if filename_prefix.endswith('.fits'): filename_prefix=filename_prefix[:-5]
         if fields is None: 
@@ -317,71 +341,79 @@
                       if field not in extra_fields]
 
         nx, ny = self.buff_size
-        dx = (self.bounds[1]-self.bounds[0])/nx*self.pf[units]
-        dy = (self.bounds[3]-self.bounds[2])/ny*self.pf[units]
-        xmin = self.bounds[0]*self.pf[units]
-        ymin = self.bounds[2]*self.pf[units]
+        dx = (self.bounds[1]-self.bounds[0])/nx
+        dy = (self.bounds[3]-self.bounds[2])/ny
+        if units == "sky":
+            dx = np.rad2deg(dx/dist)
+            dy = np.rad2deg(dy/dist)
+        else:
+            dx *= self.pf.units[units]
+            dy *= self.pf.units[units]
+            xmin = self.bounds[0]*self.pf.units[units]
+            ymin = self.bounds[2]*self.pf.units[units]
         simtime = self.pf.current_time
 
-        hdus = []
+        hdus = [pyfits.PrimaryHDU()]
 
-        first = True
+        print "HAHAH"
         
         for field in fields:
 
-            if (first) :
-                hdu = pyfits.PrimaryHDU(self[field])
-                first = False
-            else :
-                hdu = pyfits.ImageHDU(self[field])
+            hdu = pyfits.ImageHDU(self[field])
                 
             if self.data_source.has_key('weight_field'):
                 weightname = self.data_source._weight
-                if weightname is None: weightname = 'None'
-                field = field +'_'+weightname
-
+                if weightname is not None:
+                    hdu.header.update("Weight", weightname)
+                    
             hdu.header.update("Field", field)
             hdu.header.update("Time", simtime)
 
-            hdu.header.update('WCSNAMEP', "PHYSICAL")            
-            hdu.header.update('CTYPE1P', "LINEAR")
-            hdu.header.update('CTYPE2P', "LINEAR")
-            hdu.header.update('CRPIX1P', 0.5)
-            hdu.header.update('CRPIX2P', 0.5)
-            hdu.header.update('CRVAL1P', xmin)
-            hdu.header.update('CRVAL2P', ymin)
-            hdu.header.update('CDELT1P', dx)
-            hdu.header.update('CDELT2P', dy)
-                    
-            hdu.header.update('CTYPE1', "LINEAR")
-            hdu.header.update('CTYPE2', "LINEAR")                                
-            hdu.header.update('CUNIT1', units)
-            hdu.header.update('CUNIT2', units)
-            hdu.header.update('CRPIX1', 0.5)
-            hdu.header.update('CRPIX2', 0.5)
-            hdu.header.update('CRVAL1', xmin)
-            hdu.header.update('CRVAL2', ymin)
-            hdu.header.update('CDELT1', dx)
-            hdu.header.update('CDELT2', dy)
-
-            if (other_keys is not None) :
+            if units == "sky":
+                hdu.header.update("MTYPE1", "EQPOS")
+                hdu.header.update("MFORM1", "RA,DEC")
+                hdu.header.update("CTYPE1", "RA---TAN")
+                hdu.header.update("CTYPE2", "DEC--TAN")
+                hdu.header.update("CRPIX1", 0.5*(nx+1))
+                hdu.header.update("CRPIX2", 0.5*(ny+1))
+                hdu.header.update("CRVAL1", sky_center[0])
+                hdu.header.update("CRVAL2", sky_center[1])
+                hdu.header.update("CUNIT1", "deg")
+                hdu.header.update("CUNIT2", "deg")
+                hdu.header.update("CDELT1", -dx)
+                hdu.header.update("CDELT2", dy)
+            else:
+                hdu.header.update('CTYPE1', "LINEAR")
+                hdu.header.update('CTYPE2', "LINEAR")
+                hdu.header.update('CUNIT1', units)
+                hdu.header.update('CUNIT2', units)
+                hdu.header.update('CRPIX1', 0.5)
+                hdu.header.update('CRPIX2', 0.5)
+                hdu.header.update('CRVAL1', xmin)
+                hdu.header.update('CRVAL2', ymin)
+                hdu.header.update('CDELT1', dx)
+                hdu.header.update('CDELT2', dy)
+                                                                    
+            if other_keys is not None:
 
                 for k,v in other_keys.items() :
 
                     hdu.header.update(k,v)
 
             hdus.append(hdu)
-
-            del hdu
             
         hdulist = pyfits.HDUList(hdus)
 
         hdulist.writeto("%s.fits" % (filename_prefix), clobber=clobber)
+
+        print "YOURMOM"
         
-        if (gzip_file) :
+        if gzip_file:
             clob = ""
             if (clobber) : clob = "-f"
             system("gzip "+clob+" %s.fits" % (filename_prefix))
+
+        print "pffft"
         
     def open_in_ds9(self, field, take_log=True):
         """

diff -r 623221c090387db6a55c9ed9c94023090527b939 -r 532068b4baf0c6281fb5ed45981629760df1cbcc yt/visualization/image_writer.py
--- a/yt/visualization/image_writer.py
+++ b/yt/visualization/image_writer.py
@@ -438,14 +438,15 @@
     return filename
 
 
-def write_fits(image, filename_prefix, clobber=True, coords=None, gzip_file=False) :
+def write_fits(image, filename_prefix, clobber=True, coords=None,
+               other_keys=None, gzip_file=False) :
     """
     This will export a FITS image of a floating point array. The output filename is
     *filename_prefix*. If clobber is set to True, this will overwrite any existing
     FITS file.
     
-    This requires the *pyfits* module, which is a standalone module
-    provided by STSci to interface with FITS-format files.
+    This requires the *pyfits* module, which comes as a standalone module
+    provided by STSci or as a part of the AstroPy package.
     """
     r"""Write out a floating point array directly to a FITS file, optionally
     adding coordinates. 
@@ -461,46 +462,59 @@
         If the file exists, this governs whether we will overwrite.
     coords : dictionary, optional
         A set of header keys and values to write to the FITS header to set up
-        a coordinate system. 
+        a coordinate system.
+        "type": the type of coordinate system, default is "LINEAR"
+        "units": the length units
+        "xctr","yctr": the center of the image
+        "dx","dy": the pixel width in each direction
+    other_keys : dictionary, optional
+        A set of header keys and values to write into the FITS header.            
     gzip_file : boolean, optional
         gzip the file after writing, default False
     """
-    
-    import pyfits
+
+    try:
+        import pyfits
+    except:
+        try:
+            import astropy.io.fits as pyfits
+        except:
+            mylog.error("You don't have pyFITS or AstroPy installed!")
+            
     from os import system
     
     if filename_prefix.endswith('.fits'): filename_prefix=filename_prefix[:-5]
     
-    hdu = pyfits.PrimaryHDU(image)
+    hdu = pyfits.ImageHDU(image)
+    
+    if coords is not None:
 
-    if (coords is not None) :
-
-        hdu.header.update('WCSNAMEP', "PHYSICAL")
-        hdu.header.update('CTYPE1P', "LINEAR")
-        hdu.header.update('CTYPE2P', "LINEAR")
-        hdu.header.update('CRPIX1P', 0.5)
-        hdu.header.update('CRPIX2P', 0.5)
-        hdu.header.update('CRVAL1P', coords["xmin"])
-        hdu.header.update('CRVAL2P', coords["ymin"])
-        hdu.header.update('CDELT1P', coords["dx"])
-        hdu.header.update('CDELT2P', coords["dy"])
-        
-        hdu.header.update('CTYPE1', "LINEAR")
-        hdu.header.update('CTYPE2', "LINEAR")
+        if not coords.has_key("type"):
+            type = "LINEAR"
+        else:
+            type = coords["type"]
+            
+        hdu.header.update('CTYPE1', type)
+        hdu.header.update('CTYPE2', type)
         hdu.header.update('CUNIT1', coords["units"])
         hdu.header.update('CUNIT2', coords["units"])
-        hdu.header.update('CRPIX1', 0.5)
-        hdu.header.update('CRPIX2', 0.5)
-        hdu.header.update('CRVAL1', coords["xmin"])
-        hdu.header.update('CRVAL2', coords["ymin"])
+        hdu.header.update('CRPIX1', 0.5*(nx+1))
+        hdu.header.update('CRPIX2', 0.5*(ny+1))
+        hdu.header.update('CRVAL1', coords["xctr"])
+        hdu.header.update('CRVAL2', coords["yctr"])
         hdu.header.update('CDELT1', coords["dx"])
         hdu.header.update('CDELT2', coords["dy"])
 
-    hdu.writeto("%s.fits" % (filename_prefix), clobber=clobber)
+    if other_keys is not None:
+        for k,v in other_keys.items():
+            hdu.header.update(k,v)
+                                    
+    hdulist = pyfits.HDUList([pyfits.PrimaryHDU(),hdu])
+    hdulist.writeto("%s.fits" % (filename_prefix), clobber=clobber)
 
-    if (gzip_file) :
+    if gzip_file:
         clob = ""
-        if (clobber) : clob="-f"
+        if clobber: clob="-f"
         system("gzip "+clob+" %s.fits" % (filename_prefix))
 
 def display_in_notebook(image, max_val=None):


https://bitbucket.org/yt_analysis/yt/commits/69a1540c12bd/
Changeset:   69a1540c12bd
Branch:      yt
User:        jzuhone
Date:        2013-08-26 02:30:42
Summary:     Move these to a new directory
Affected #:  9 files

diff -r 532068b4baf0c6281fb5ed45981629760df1cbcc -r 69a1540c12bd707961330376eb64c47a03c9052f yt/analysis_modules/api.py
--- a/yt/analysis_modules/api.py
+++ b/yt/analysis_modules/api.py
@@ -121,7 +121,7 @@
 from .radmc3d_export.api import \
     RadMC3DWriter
 
-from .synthetic_xray_obs.api import \
+from .synthetic_obs.api import \
      PhotonList, \
      EventList, \
      XSpecThermalModel, \

diff -r 532068b4baf0c6281fb5ed45981629760df1cbcc -r 69a1540c12bd707961330376eb64c47a03c9052f yt/analysis_modules/synthetic_obs/api.py
--- /dev/null
+++ b/yt/analysis_modules/synthetic_obs/api.py
@@ -0,0 +1,35 @@
+"""
+API for synthetic_obs
+
+Author: John ZuHone <jzuhone at gmail.com>
+Affiliation: NASA/GSFC
+Homepage: http://yt-project.org/
+License:
+  Copyright (C) 2010-2011 Matthew Turk.  All Rights Reserved.
+
+  This file is part of yt.
+
+  yt is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 3 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+"""
+
+from .photon_simulator import \
+     PhotonList, \
+     EventList
+
+from .photon_models import \
+     XSpecThermalModel, \
+     XSpecAbsorbModel, \
+     TableApecModel, \
+     TableAbsorbModel

diff -r 532068b4baf0c6281fb5ed45981629760df1cbcc -r 69a1540c12bd707961330376eb64c47a03c9052f yt/analysis_modules/synthetic_obs/photon_models.py
--- /dev/null
+++ b/yt/analysis_modules/synthetic_obs/photon_models.py
@@ -0,0 +1,215 @@
+import numpy as np
+import os
+from yt.funcs import *
+import h5py
+try:
+    import pyfits
+except:
+    try:
+        import astropy.io.fits as pyfits
+    except:
+        mylog.error("Your mom doesn't have pyFITS")
+try:
+    import xspec
+except ImportError:
+    mylog.warning("You don't have PyXSpec installed. Some models won't be available.")
+from scipy.integrate import cumtrapz
+from scipy import stats
+from yt.utilities.physical_constants import hcgs, clight, erg_per_keV, amu_cgs
+
+hc = 1.0e8*hcgs*clight/erg_per_keV
+
+class PhotonModel(object):
+
+    def __init__(self, emin, emax, nchan):
+        self.emin = emin
+        self.emax = emax
+        self.nchan = nchan
+        self.ebins = np.linspace(emin, emax, nchan+1)
+        self.de = self.ebins[1]-self.ebins[0]
+        
+    def prepare(self):
+        pass
+    
+    def get_spectrum(self):
+        pass
+                                                        
+class XSpecThermalModel(PhotonModel):
+
+    def __init__(self, model_name, emin, emax, nchan):
+        self.model_name = model_name
+        PhotonModel.__init__(self, emin, emax, nchan)
+        
+    def prepare(self):
+        xspec.Xset.chatter = 0
+        xspec.AllModels.setEnergies("%f %f %d lin" %
+                                    (self.emin, self.emax, self.nchan))
+        self.model = xspec.Model(self.model_name)
+        
+    def get_spectrum(self, kT, Zmet):
+        m = getattr(self.model,self.model_name)
+        m.kT = kT
+        m.Abundanc = Zmet
+        m.norm = 1.0
+        m.Redshift = 0.0
+        return 1.0e-14*np.array(self.model.values(0))
+    
+class XSpecAbsorbModel(PhotonModel):
+
+    def __init__(self, model_name, nH, emin=0.01, emax=50.0, nchan=100000):
+        self.model_name = model_name
+        self.nH = nH
+        PhotonModel.__init__(self, emin, emax, nchan)
+        
+    def prepare(self):
+        xspec.Xset.chatter = 0
+        xspec.AllModels.setEnergies("%f %f %d lin" %
+                                    (self.emin, self.emax, self.nchan))
+        self.model = xspec.Model(self.model_name+"*powerlaw")
+        self.model.powerlaw.norm = self.nchan/(self.emax-self.emin)
+        self.model.powerlaw.PhoIndex = 0.0
+
+    def get_spectrum(self):
+        m = getattr(self.model,self.model_name)
+        m.nH = self.nH
+        return np.array(self.model.values(0))
+
+class TableApecModel(PhotonModel):
+
+    def __init__(self, apec_root, emin, emax, nchan,
+                 apec_vers="2.0.2", thermal_broad=False):
+        self.apec_root = apec_root
+        self.apec_prefix = "apec_v"+apec_vers
+        self.cocofile = os.path.join(self.apec_root,
+                                     self.apec_prefix+"_coco.fits")
+        self.linefile = os.path.join(self.apec_root,
+                                     self.apec_prefix+"_line.fits")
+        PhotonModel.__init__(self, emin, emax, nchan)
+        self.wvbins = hc/self.ebins[::-1]
+        # H, He, and trace elements
+        self.cosmic_elem = [1,2,3,4,5,9,11,15,17,19,21,22,23,24,25,27,29,30]
+        # Non-trace metals
+        self.metal_elem = [6,7,8,10,12,13,14,16,18,20,26,28]
+        self.thermal_broad = thermal_broad
+        self.A = np.array([0.0,1.00794,4.00262,6.941,9.012182,10.811,
+                           12.0107,14.0067,15.9994,18.9984,20.1797,
+                           22.9898,24.3050,26.9815,28.0855,30.9738,
+                           32.0650,35.4530,39.9480,39.0983,40.0780,
+                           44.9559,47.8670,50.9415,51.9961,54.9380,
+                           55.8450,58.9332,58.6934,63.5460,65.3800])
+        
+    def prepare(self):
+        try:
+            self.line_handle = pyfits.open(self.linefile)
+        except IOError:
+            mylog.error("LINE file %s does not exist" % (self.linefile))
+        try:
+            self.coco_handle = pyfits.open(self.cocofile)
+        except IOError:
+            mylog.error("COCO file %s does not exist" % (self.cocofile))
+        self.Tvals = self.line_handle[1].data.field("kT")
+        self.dTvals = np.diff(self.Tvals)
+        self.minlam = self.wvbins.min()
+        self.maxlam = self.wvbins.max()
+
+    def expand_E_grid(self, Eedges, Ncont, Econt, cont):
+        # linearly interpolate onto wvedges
+        # All energies should be in keV
+        newx = np.array(Econt[:Ncont])
+        newx = np.append(newx,Eedges)
+        newxargs = newx.argsort()
+        newx.sort()
+        newy = np.interp(newx, Econt, cont)
+        # simple integration
+        ct = cumtrapz(newy,newx)
+        ret = np.zeros((self.nchan))
+        iinew = np.where(newxargs==Ncont)[0][0]
+        for i in range(self.nchan):
+            ind = i + 1 + Ncont
+            iiold = iinew
+            iinew = np.where(newxargs==ind)[0][0]
+            ret[i] = ct[iinew-1]-ct[iiold-1]
+        return ret
+
+    def make_spectrum(self, element, tindex):
+        
+        tmpspec = np.zeros((self.nchan))
+        
+        i = np.where((self.line_handle[tindex].data.field('element')==element) &
+                     (self.line_handle[tindex].data.field('lambda') > self.minlam) &
+                     (self.line_handle[tindex].data.field('lambda') < self.maxlam))[0]
+
+        vec = np.zeros((self.nchan))
+        E0 = hc/self.line_handle[tindex].data.field('lambda')[i]
+        amp = self.line_handle[tindex].data.field('epsilon')[i]
+        if self.thermal_broad:
+            vec = np.zeros((self.nchan))
+            sigma = E0*np.sqrt(self.Tvals[tindex]*erg_per_keV/(self.A[element]*amu_cgs))/clight
+            for E, sig, a in zip(E0, sigma, amp):
+                cdf = stats.norm(E,sig).cdf(self.ebins)
+                vec += np.diff(cdf)*a
+        else:
+            ie = np.searchsorted(self.ebins, E0, side='right')-1
+            for i,a in zip(ie,amp): vec[i] += a
+        tmpspec += vec
+
+        ind = np.where((self.coco_handle[tindex].data.field('Z')==element) &
+                       (self.coco_handle[tindex].data.field('rmJ')==0))[0]
+        if len(ind)==0:
+            return tmpspec
+        else:
+            ind=ind[0]
+                                                    
+        n_cont=self.coco_handle[tindex].data.field('N_Cont')[ind]
+        e_cont=self.coco_handle[tindex].data.field('E_Cont')[ind][:n_cont]
+        continuum = self.coco_handle[tindex].data.field('Continuum')[ind][:n_cont]
+        
+        tmpspec += self.expand_E_grid(self.ebins, n_cont, e_cont, continuum)
+        
+        n_pseudo=self.coco_handle[tindex].data.field('N_Pseudo')[ind]
+        e_pseudo=self.coco_handle[tindex].data.field('E_Pseudo')[ind][:n_pseudo]
+        pseudo = self.coco_handle[tindex].data.field('Pseudo')[ind][:n_pseudo]
+        
+        tmpspec += self.expand_E_grid(self.ebins, n_pseudo, e_pseudo, pseudo)
+                                                        
+        return tmpspec
+
+    def get_spectrum(self, kT, Zmet):
+        cspec_l = np.zeros((self.nchan))
+        mspec_l = np.zeros((self.nchan))
+        cspec_r = np.zeros((self.nchan))
+        mspec_r = np.zeros((self.nchan))
+        tindex = np.searchsorted(self.Tvals, kT)-1
+        dT = (kT-self.Tvals[tindex])/self.dTvals[tindex]
+        # First do H,He, and trace elements
+        for elem in self.cosmic_elem:
+            cspec_l += self.make_spectrum(elem, tindex+2)
+            cspec_r += self.make_spectrum(elem, tindex+3)            
+        # Next do the metals
+        for elem in self.metal_elem:
+            mspec_l += self.make_spectrum(elem, tindex+2)
+            mspec_r += self.make_spectrum(elem, tindex+3)
+        cosmic_spec = cspec_l*(1.-dT)+cspec_r*dT
+        metal_spec = mspec_l*(1.-dT)+mspec_r*dT        
+        return cosmic_spec+Zmet*metal_spec
+
+class TableAbsorbModel(PhotonModel):
+
+    def __init__(self, filename):
+        if not os.path.exists(filename):
+            raise IOError("File does not exist: %s." % filename)
+        self.filename = filename
+        f = h5py.File(self.filename,"r")
+        emin = f["energ_lo"][:].min()
+        emax = f["energ_hi"][:].max()
+        self.abs = f["absorb_coeff"][:]
+        nchan = self.abs.shape[0]
+        f.close()
+        PhotonModel.__init__(self, emin, emax, nchan)
+                                                                    
+    def prepare(self):
+        pass
+
+    def get_spectrum(self):
+        return self.abs ** nH
+    

diff -r 532068b4baf0c6281fb5ed45981629760df1cbcc -r 69a1540c12bd707961330376eb64c47a03c9052f yt/analysis_modules/synthetic_obs/photon_simulator.py
--- /dev/null
+++ b/yt/analysis_modules/synthetic_obs/photon_simulator.py
@@ -0,0 +1,805 @@
+import numpy as np
+from yt.funcs import *
+from yt.utilities.physical_constants import mp, clight, cm_per_kpc, \
+     cm_per_mpc, cm_per_km, K_per_keV, erg_per_keV
+from yt.utilities.cosmology import Cosmology
+from yt.utilities.orientation import Orientation
+from yt.utilities.parallel_tools.parallel_analysis_interface import \
+     communication_system, parallel_root_only, get_mpi_type, parallel_capable
+from yt.data_objects.api import add_field
+
+import os
+import h5py
+
+try:
+    import pyfits
+except ImportError:
+    try:
+        import astropy.io.fits as pyfits
+    except ImportError:
+        mylog.warning("You don't have pyFITS installed. " + 
+                      "Writing to and reading from FITS files won't be available.")
+    
+N_TBIN = 10000
+TMIN = 8.08e-2
+TMAX = 50.
+FOUR_PI = 4.*np.pi
+
+comm = communication_system.communicators[-1]
+        
+class PhotonList(object):
+
+    def __init__(self, photons=None, cosmo=None, p_bins=None):
+        if photons is None: photons = {}
+        self.photons = photons
+        self.cosmo = cosmo
+        self.p_bins = p_bins
+        self.num_cells = len(photons["x"])
+        
+    def keys(self):
+        return self.photons.keys()
+    
+    def items(self):
+        ret = []
+        for k, v in self.photons.items():
+            if k == "Energy":
+                ret.append((k, self[k]))
+            else:
+                ret.append((k,v))
+        return ret
+    
+    def values(self):
+        ret = []
+        for k, v in self.photons.items():
+            if k == "Energy":
+                ret.append(self[k])
+            else:
+                ret.append(v)
+        return ret
+                                
+    def __getitem__(self, key):
+        if key == "Energy":
+            return [self.photons["Energy"][self.p_bins[i]:self.p_bins[i+1]]
+                    for i in xrange(self.num_cells)]
+        else:
+            return self.photons[key]
+    
+    @classmethod
+    def from_file(cls, filename, cosmology=None):
+        """
+        Initialize a PhotonList from an HDF5 file given by filename.
+        """
+        photons = {}
+
+        f = h5py.File(filename, "r")
+
+        photons["FiducialExposureTime"] = f["/fid_exp_time"].value
+        photons["FiducialArea"] = f["/fid_area"].value
+        photons["FiducialRedshift"] = f["/fid_redshift"].value
+        photons["FiducialAngularDiameterDistance"] = f["/fid_d_a"].value
+
+        num_cells = f["/x"][:].shape[0]
+        start_c = comm.rank*num_cells/comm.size
+        end_c = (comm.rank+1)*num_cells/comm.size
+        
+        photons["x"] = f["/x"][start_c:end_c]
+        photons["y"] = f["/y"][start_c:end_c]
+        photons["z"] = f["/z"][start_c:end_c]
+        photons["dx"] = f["/dx"][start_c:end_c]
+        photons["vx"] = f["/vx"][start_c:end_c]
+        photons["vy"] = f["/vy"][start_c:end_c]
+        photons["vz"] = f["/vz"][start_c:end_c]
+
+        n_ph = f["/num_photons"][:]
+        
+        if comm.rank == 0:
+            start_e = np.uint64(0)
+        else:
+            start_e = n_ph[:start_c].sum()
+        end_e = start_e + np.uint64(n_ph[start_c:end_c].sum())
+
+        photons["NumberOfPhotons"] = n_ph[start_c:end_c]
+
+        p_bins = np.cumsum(photons["NumberOfPhotons"])
+        p_bins = np.insert(p_bins, 0, [np.uint64(0)])
+        
+        photons["Energy"] = f["/energy"][start_e:end_e]
+        
+        f.close()
+                                        
+        return cls(photons=photons, cosmo=cosmology, p_bins=p_bins)
+
+    @classmethod
+    def from_thermal_model(cls, data_source, redshift, eff_A,
+                           exp_time, emission_model, center="c",
+                           X_H=0.75, Zmet=0.3, dist=None, cosmology=None):
+        """
+        Initialize a PhotonList from a data container. 
+        """
+        pf = data_source.pf
+                
+        vol_scale = pf.units["cm"]**(-3)/np.prod(pf.domain_width)
+
+        if cosmology is None and dist is None:
+            cosmo = Cosmology(HubbleConstantNow=71., OmegaMatterNow=0.27,
+                              OmegaLambdaNow=0.73)
+        else:
+            if cosmology is None:
+                D_A = dist*cm_per_mpc
+                cosmo = None
+            else:
+                cosmo = cosmology
+        if cosmo is not None:
+            D_A = cosmo.AngularDiameterDistance(0.0,redshift)*cm_per_mpc
+        dist_fac = 1.0/(FOUR_PI*D_A*D_A*(1.+redshift)**3)
+
+        num_cells = data_source["Temperature"].shape[0]
+        start_c = comm.rank*num_cells/comm.size
+        end_c = (comm.rank+1)*num_cells/comm.size
+
+        kT = data_source["Temperature"][start_c:end_c].copy()/K_per_keV
+        vol = data_source["CellVolume"][start_c:end_c].copy()
+        dx = data_source["dx"][start_c:end_c].copy()
+        EM = (data_source["Density"][start_c:end_c].copy()/mp)**2
+        EM *= 0.5*(1.+X_H)*X_H*vol
+        
+        data_source.clear_data()
+        
+        x = data_source["x"][start_c:end_c].copy()
+        y = data_source["y"][start_c:end_c].copy()
+        z = data_source["z"][start_c:end_c].copy()
+
+        data_source.clear_data()
+                
+        vx = data_source["x-velocity"][start_c:end_c].copy()
+        vy = data_source["y-velocity"][start_c:end_c].copy()
+        vz = data_source["z-velocity"][start_c:end_c].copy()
+        
+        data_source.clear_data()
+                
+        idxs = np.argsort(kT)
+        dshape = idxs.shape
+
+        if center == "c":
+            src_ctr = pf.domain_center
+        elif center == "max":
+            src_ctr = pf.h.find_max("Density")[-1]
+        elif iterable(center):
+            src_ctr = center
+                    
+        kT_bins = np.linspace(TMIN, max(kT[idxs][-1], TMAX), num=N_TBIN+1)
+        dkT = kT_bins[1]-kT_bins[0]
+        kT_idxs = np.digitize(kT[idxs], kT_bins)
+        kT_idxs = np.minimum(np.maximum(1, kT_idxs), N_TBIN) - 1
+        bcounts = np.bincount(kT_idxs).astype("int")
+        bcounts = bcounts[bcounts > 0]
+        n = int(0)
+        bcell = []
+        ecell = []
+        for bcount in bcounts:
+            bcell.append(n)
+            ecell.append(n+bcount)
+            n += bcount
+        kT_idxs = np.unique(kT_idxs)
+
+        emission_model.prepare()
+        energy = emission_model.ebins
+        de = emission_model.de
+        emid = 0.5*(energy[1:]+energy[:-1])
+        
+        cell_em = EM[idxs]*vol_scale
+        cell_vol = vol[idxs]*vol_scale
+        cell_emd = cell_em/cell_vol
+        
+        number_of_photons = np.zeros(dshape, dtype='uint64')
+        energies = []
+                                
+        pbar = get_pbar("Generating Photons", dshape[0])
+
+        for i, ikT in enumerate(kT_idxs):
+            
+            ncells = int(bcounts[i])
+            ibegin = bcell[i]
+            iend = ecell[i]
+            kT = kT_bins[ikT] + 0.5*dkT
+            
+            em_sum = cell_em[ibegin:iend].sum()
+            vol_sum = cell_vol[ibegin:iend].sum()
+            em_avg = em_sum/vol_sum
+            
+            tot_norm = dist_fac*em_sum/vol_scale
+            
+            spec = emission_model.get_spectrum(kT, Zmet)
+            spec *= tot_norm
+            cumspec = np.cumsum(spec)
+            counts = cumspec[:]/cumspec[-1]
+            tot_ph = cumspec[-1]*eff_A*exp_time
+            
+            for icell in xrange(ibegin, iend):
+                
+                cell_norm = tot_ph * (cell_emd[icell]/em_avg) * (cell_vol[icell]/vol_sum)                    
+                cell_Nph = int(cell_norm) + int(np.modf(cell_norm)[0] >= np.random.random())
+                
+                if cell_Nph > 0:
+                    number_of_photons[icell] = cell_Nph                    
+                    randvec = np.random.uniform(low=counts[0], high=counts[-1], size=cell_Nph)
+                    randvec.sort()
+                    eidxs = np.searchsorted(counts, randvec)-1
+                    cell_e = emid[eidxs]+de*(randvec-counts[eidxs])/(counts[eidxs+1]-counts[eidxs])
+                    energies.append(cell_e)
+                            
+                pbar.update(icell)
+            
+        pbar.finish()
+
+        del cell_emd, cell_vol, cell_em
+
+        active_cells = number_of_photons > 0
+        idxs = idxs[active_cells]
+        
+        photons = {}
+        photons["x"] = (x[idxs]-src_ctr[0])*pf.units["kpc"]
+        photons["y"] = (y[idxs]-src_ctr[1])*pf.units["kpc"]
+        photons["z"] = (z[idxs]-src_ctr[2])*pf.units["kpc"]
+        photons["vx"] = vx[idxs]/cm_per_km
+        photons["vy"] = vy[idxs]/cm_per_km
+        photons["vz"] = vz[idxs]/cm_per_km
+        photons["dx"] = dx[idxs]*pf.units["kpc"]
+        photons["NumberOfPhotons"] = number_of_photons[active_cells]
+        photons["Energy"] = np.concatenate(energies)
+                
+        photons["FiducialExposureTime"] = exp_time
+        photons["FiducialArea"] = eff_A
+        photons["FiducialRedshift"] = redshift
+        photons["FiducialAngularDiameterDistance"] = D_A/cm_per_mpc
+
+        p_bins = np.cumsum(photons["NumberOfPhotons"])
+        p_bins = np.insert(p_bins, 0, [np.uint64(0)])
+        
+        return cls(photons=photons, cosmo=cosmo, p_bins=p_bins)
+
+    @classmethod
+    def from_user_model(cls, data_source, redshift, eff_A,
+                        exp_time, user_function, parameters={}
+                        dist=None, cosmology=None):
+
+        if cosmology is None and dist is None:
+            cosmo = Cosmology(HubbleConstantNow=71., OmegaMatterNow=0.27,
+                              OmegaLambdaNow=0.73)
+        else:
+            if cosmology is None:
+                D_A = dist*cm_per_mpc
+                cosmo = None
+            else:
+                cosmo = cosmology
+        if cosmo is not None:
+            D_A = cosmo.AngularDiameterDistance(0.0,redshift)*cm_per_mpc
+                    
+        photons = user_function(data_source, redshift, eff_A,
+                                exp_time, D_A, parameters)
+        
+        photons["FiducialExposureTime"] = exp_time
+        photons["FiducialArea"] = eff_A
+        photons["FiducialRedshift"] = redshift
+        photons["FiducialAngularDiameterDistance"] = D_A
+
+        p_bins = np.cumsum(photons["NumberOfPhotons"])
+        p_bins = np.insert(p_bins, 0, [np.uint64(0)])
+                        
+        return cls(photons=photons, cosmo=cosmo, p_bins=p_bins)
+        
+    def write_h5_file(self, photonfile):
+
+        if parallel_capable:
+            
+            mpi_long = get_mpi_type("int64")
+            mpi_double = get_mpi_type("float64")
+        
+            local_num_cells = len(self.photons["x"])
+            sizes_c = comm.comm.gather(local_num_cells, root=0)
+            
+            local_num_photons = self.photons["NumberOfPhotons"].sum()
+            sizes_p = comm.comm.gather(local_num_photons, root=0)
+            
+            if comm.rank == 0:
+                num_cells = sum(sizes_c)
+                num_photons = sum(sizes_p)        
+                disps_c = [sum(sizes_c[:i]) for i in range(len(sizes_c))]
+                disps_p = [sum(sizes_p[:i]) for i in range(len(sizes_p))]
+                x = np.zeros((num_cells))
+                y = np.zeros((num_cells))
+                z = np.zeros((num_cells))
+                vx = np.zeros((num_cells))
+                vy = np.zeros((num_cells))
+                vz = np.zeros((num_cells))
+                dx = np.zeros((num_cells))
+                n_ph = np.zeros((num_cells), dtype="uint64")
+                e = np.zeros((num_photons))
+            else:
+                sizes_c = []
+                sizes_p = []
+                disps_c = []
+                disps_p = []
+                x = np.empty([])
+                y = np.empty([])
+                z = np.empty([])
+                vx = np.empty([])
+                vy = np.empty([])
+                vz = np.empty([])
+                dx = np.empty([])
+                n_ph = np.empty([])
+                e = np.empty([])
+                                                
+            comm.comm.Gatherv([self.photons["x"], local_num_cells, mpi_double],
+                              [x, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["y"], local_num_cells, mpi_double],
+                              [y, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["z"], local_num_cells, mpi_double],
+                              [z, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["vx"], local_num_cells, mpi_double],
+                              [vx, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["vy"], local_num_cells, mpi_double],
+                              [vy, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["vz"], local_num_cells, mpi_double],
+                              [vz, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["dx"], local_num_cells, mpi_double],
+                              [dx, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["NumberOfPhotons"], local_num_cells, mpi_long],
+                              [n_ph, (sizes_c, disps_c), mpi_long], root=0)
+            comm.comm.Gatherv([self.photons["Energy"], local_num_photons, mpi_double],
+                              [e, (sizes_p, disps_p), mpi_double], root=0) 
+
+        else:
+
+            x = self.photons["x"]
+            y = self.photons["y"]
+            z = self.photons["z"]
+            vx = self.photons["vx"]
+            vy = self.photons["vy"]
+            vz = self.photons["vz"]
+            dx = self.photons["dx"]
+            n_ph = self.photons["NumberOfPhotons"]
+            e = self.photons["Energy"]
+                                                
+        if comm.rank == 0:
+            
+            f = h5py.File(photonfile, "w")
+
+            # Scalars
+       
+            f.create_dataset("fid_area", data=self.photons["FiducialArea"])
+            f.create_dataset("fid_exp_time", data=self.photons["FiducialExposureTime"])
+            f.create_dataset("fid_redshift", data=self.photons["FiducialRedshift"])
+            f.create_dataset("fid_d_a", data=self.photons["FiducialAngularDiameterDistance"])
+        
+            # Arrays
+
+            f.create_dataset("x", data=x)
+            f.create_dataset("y", data=y)
+            f.create_dataset("z", data=z)
+            f.create_dataset("vx", data=vx)
+            f.create_dataset("vy", data=vy)
+            f.create_dataset("vz", data=vz)
+            f.create_dataset("dx", data=dx)
+            f.create_dataset("num_photons", data=n_ph)
+            f.create_dataset("energy", data=e)
+
+            f.close()
+
+        comm.barrier()
+
+    def project_photons(self, L, area_new=None, texp_new=None, 
+                        redshift_new=None, dist_new=None,
+                        absorb_model=None, psf_sigma=None):
+        """
+        Projects photons onto an image plane given a line of sight. 
+        """
+
+        if redshift_new is not None and dist_new is not None:
+            mylog.error("You may specify a new redshift or distance, but not both!")
+
+        if redshift_new is not Done and self.cosmo is None:
+            mylog.error("Specified a new redshift, but no cosmology!")
+            
+        dx = self.photons["dx"]
+        
+        L /= np.sqrt(np.dot(L, L))
+        vecs = np.identity(3)
+        t = np.cross(L, vecs).sum(axis=1)
+        ax = t.argmax()
+        north = np.cross(L, vecs[ax,:]).ravel()
+        orient = Orientation(L, north_vector=north)
+        
+        x_hat = orient.unit_vectors[0]
+        y_hat = orient.unit_vectors[1]
+        z_hat = orient.unit_vectors[2]
+
+        n_ph = self.photons["NumberOfPhotons"]
+        num_cells = len(n_ph)
+        n_ph_tot = n_ph.sum()
+        
+        eff_area = None
+        
+        if (texp_new is None and area_new is None and
+            redshift_new is None and dist_new is None):
+            my_n_obs = n_ph_tot
+        else:
+            if texp_new is None:
+                Tratio = 1.
+            else:
+                Tratio = texp_new/self.photons["FiducialExposureTime"]
+            if area_new is None:
+                Aratio = 1.
+            elif isinstance(area_new, basestring):
+                mylog.info("Using energy-dependent effective area.")
+                f = pyfits.open(area_new)
+                elo = f[1].data.field("ENERG_LO")
+                ehi = f[1].data.field("ENERG_HI")
+                eff_area = f[1].data.field("SPECRESP")
+                f.close()
+                Aratio = eff_area.max()/self.photons["FiducialArea"]
+            else:
+                mylog.info("Using constant effective area.")
+                Aratio = area_new/self.photons["FiducialArea"]
+            if redshift_new is None and dist_new is None:
+                Dratio = 1.
+                zobs = self.photons["FiducialRedshift"]
+                D_A = self.photons["FiducialAngularDiameterDistance"]*1000.                    
+            else:
+                if redshift_new is None:
+                    zobs = self.photons["FiducialRedshift"]
+                    D_A = dist_new*1000.
+                else:
+                    zobs = redshift_new
+                    D_A = self.cosmo.AngularDiameterDistance(0.0,zobs)*1000.
+                fid_D_A = self.photons["FiducialAngularDiameterDistance"]*1000.
+                Dratio = fid_D_A*fid_D_A*(1.+self.photons["FiducialRedshift"]**3) / \
+                         (D_A*D_A*(1.+zobs)**3)
+            fak = Aratio*Tratio*Dratio
+            if fak > 1:
+                raise ValueError("Spectrum scaling factor = %g, cannot be greater than unity." % (fak))
+            my_n_obs = np.uint64(n_ph_tot*fak)
+
+        n_obs_all = comm.mpi_allreduce(my_n_obs)
+        if comm.rank == 0: mylog.info("Total number of photons to use: %d" % (n_obs_all))
+        
+        x = np.random.uniform(low=-0.5,high=0.5,size=my_n_obs)
+        y = np.random.uniform(low=-0.5,high=0.5,size=my_n_obs)
+        z = np.random.uniform(low=-0.5,high=0.5,size=my_n_obs)
+                    
+        vz = self.photons["vx"]*z_hat[0] + \
+             self.photons["vy"]*z_hat[1] + \
+             self.photons["vz"]*z_hat[2]
+        shift = -vz*cm_per_km/clight
+        shift = np.sqrt((1.-shift)/(1.+shift))
+
+        if my_n_obs == n_ph_tot:
+            idxs = np.arange(my_n_obs,dtype='uint64')
+        else:
+            idxs = np.random.permutation(n_ph_tot)[:my_n_obs].astype("uint64")
+        obs_cells = np.searchsorted(self.p_bins, idxs, side='right')-1
+        delta = dx[obs_cells]
+
+        x *= delta
+        y *= delta
+        z *= delta
+        x += self.photons["x"][obs_cells]
+        y += self.photons["y"][obs_cells]
+        z += self.photons["z"][obs_cells]  
+        eobs = self.photons["Energy"][idxs]*shift[obs_cells]
+
+        xsky = x*x_hat[0] + y*x_hat[1] + z*x_hat[2]
+        ysky = x*y_hat[0] + y*y_hat[1] + z*y_hat[2]
+        eobs /= (1.+zobs)
+
+        if absorb_model is None:
+            not_abs = np.ones(eobs.shape, dtype='bool')
+        else:
+            mylog.info("Absorbing.")
+            absorb_model.prepare()
+            energy = absorb_model.ebins
+            de = absorb_model.de
+            emid = 0.5*(energy[1:]+energy[:-1])
+            aspec = absorb_model.get_spectrum()
+            eidxs = np.searchsorted(emid, eobs)-1
+            dx = (eobs-emid[eidxs])/de
+            absorb = aspec[eidxs]*(1.-dx) + aspec[eidxs+1]*dx
+            randvec = aspec.max()*np.random.random(eobs.shape)
+            not_abs = randvec < absorb
+
+        if eff_area is None:
+            detected = np.ones(eobs.shape, dtype='bool')
+        else:
+            mylog.info("Applying energy-dependent effective area.")
+            earf = 0.5*(elo+ehi)
+            de = earf[1]-earf[0]            
+            eidxs = np.searchsorted(earf, eobs)-1
+            dx = (eobs-earf[eidxs])/de
+            earea = eff_area[eidxs]*(1.-dx) + eff_area[eidxs+1]*dx
+            randvec = eff_area.max()*np.random.random(eobs.shape)
+            detected = randvec < earea
+        
+        detected = np.logical_and(not_abs, detected)
+                    
+        events = {}
+
+        events["xsky"] = np.rad2deg(xsky[detected]/D_A)*3600.
+        events["ysky"] = np.rad2deg(ysky[detected]/D_A)*3600.
+        events["eobs"] = eobs[detected]
+
+        if psf_sigma is not None:
+            events["xsky"] += np.random.normal(sigma=psf_sigma)
+            events["ysky"] += np.random.normal(sigma=psf_sigma)
+
+        events = comm.par_combine_object(events, datatype="dict", op="cat")
+        
+        num_events = len(events["xsky"])
+            
+        if comm.rank == 0: mylog.info("Total number of observed photons: %d" % (num_events))
+                        
+        if texp_new is None:
+            events["ExposureTime"] = self.photons["FiducialExposureTime"]
+        else:
+            events["ExposureTime"] = texp_new
+        if area_new is None:
+            events["Area"] = self.photons["FiducialArea"]
+        else:
+            events["Area"] = area_new
+        events["Redshift"] = zobs
+        events["AngularDiameterDistance"] = D_A/1000.
+                
+        return EventList(events)
+
+class EventList(object) :
+
+    def __init__(self, events = None) :
+
+        if events is None : events = {}
+        self.events = events
+        self.num_events = events["xsky"].shape[0]
+
+    def keys(self):
+        return self.events.keys()
+
+    def items(self):
+        return self.events.items()
+
+    def values(self):
+        return self.events.values()
+    
+    def __getitem__(self,key):
+        return self.events[key]
+        
+    @classmethod
+    def from_h5_file(cls, h5file):
+        """
+        Initialize an EventList from a HDF5 file with filename h5file.
+        """
+        events = {}
+        
+        f = h5py.File(h5file, "r")
+
+        events["ExposureTime"] = f["/exp_time"].value
+        events["Area"] = f["/area"].value
+        events["Redshift"] = f["/redshift"].value
+        events["AngularDiameterDistance"] = f["/d_a"].value
+        
+        events["xsky"] = f["/xsky"][:]
+        events["ysky"] = f["/ysky"][:]
+        events["eobs"] = f["/eobs"][:]
+        
+        f.close()
+        
+        return cls(events)
+
+    @classmethod
+    def from_fits_file(cls, fitsfile):
+        """
+        Initialize an EventList from a FITS file with filename fitsfile.
+        """
+        hdulist = pyfits.open(fitsfile)
+
+        tblhdu = hdulist[1]
+
+        events = {}
+        
+        events["ExposureTime"] = tblhdu.header["EXPOSURE"]
+        events["Area"] = tblhdu.header["AREA"]
+        events["Redshift"] = tblhdu.header["REDSHIFT"]
+        events["AngularDiameterDistance"] = tblhdu.header["D_A"]
+        
+        events["xsky"] = tblhdu.data.field("POS_X")
+        events["ysky"] = tblhdu.data.field("POS_Y")
+        events["eobs"] = tblhdu.data.field("ENERGY")
+        
+        return cls(events)
+
+    @classmethod
+    def join_events(cls, events1, events2):
+        events = {}
+        for item1, item2 in zip(events1.items(), events2.items()):
+            k1, v1 = item1
+            k2, v2 = item2
+            if isinstance(v1, np.ndarray):
+                events[k1] = np.concatenate([v1,v2])
+            else:
+                events[k1] = v1            
+        return cls(events)
+    
+    def convolve_with_response(self, respfile):
+        pass
+
+    @parallel_root_only
+    def write_fits_file(self, fitsfile, clobber=False):
+        """
+        Write events to a FITS binary table file.
+        """
+        col1 = pyfits.Column(name='ENERGY', format='E',
+                             array=self.events["eobs"])
+        col2 = pyfits.Column(name='XSKY', format='D',
+                             array=self.events["xsky"])
+        col3 = pyfits.Column(name='YSKY', format='D',
+                             array=self.events["ysky"])
+        
+        coldefs = pyfits.ColDefs([col1, col2, col3])
+        
+        tbhdu = pyfits.new_table(coldefs)
+
+        tbhdu.header.update("EXPOSURE", self.events["ExposureTime"])
+        tbhdu.header.update("AREA", self.events["Area"])
+        tbhdu.header.update("D_A", self.evenets["AngularDiameterDistance"])
+                
+        tbhdu.writeto(fitsfile, clobber=clobber)
+
+    @parallel_root_only    
+    def write_simput_file(self, prefix, clobber=False, e_min=None, e_max=None):
+        """
+        Write events to a SIMPUT file.
+        """
+        if e_min is None:
+            e_min = 0.0
+        if e_max is None:
+            e_max = 100.0
+
+        idxs = np.logical_and(self.events["eobs"] >= e_min, self.events["eobs"] <= e_max)
+        flux = erg_per_keV*np.sum(self.events["eobs"][idxs])/self.events["ExposureTime"]/self.events["Area"]
+        
+        col1 = pyfits.Column(name='ENERGY', format='E',
+                             array=self.events["eobs"])
+        col2 = pyfits.Column(name='DEC', format='D',
+                             array=self.events["ysky"]/3600.)
+        col3 = pyfits.Column(name='RA', format='D',
+                             array=self.events["xsky"]/3600.)
+
+        coldefs = pyfits.ColDefs([col1, col2, col3])
+
+        tbhdu = pyfits.new_table(coldefs)
+        tbhdu.update_ext_name("PHLIST")
+
+        tbhdu.header.update("HDUCLASS", "HEASARC/SIMPUT")
+        tbhdu.header.update("HDUCLAS1", "PHOTONS")
+        tbhdu.header.update("HDUVERS", "1.1.0")
+        tbhdu.header.update("EXTVER", 1)
+        tbhdu.header.update("REFRA", 0.0)
+        tbhdu.header.update("REFDEC", 0.0)
+        tbhdu.header.update("TUNIT1", "keV")
+        tbhdu.header.update("TUNIT2", "deg")
+        tbhdu.header.update("TUNIT3", "deg")                
+
+        phfile = prefix+"_phlist.fits"
+
+        tbhdu.writeto(phfile, clobber=clobber)
+
+        col1 = pyfits.Column(name='SRC_ID', format='J', array=np.array([1]).astype("int32"))
+        col2 = pyfits.Column(name='RA', format='D', array=np.array([0.0]))
+        col3 = pyfits.Column(name='DEC', format='D', array=np.array([0.0]))
+        col4 = pyfits.Column(name='E_MIN', format='D', array=np.array([e_min]))
+        col5 = pyfits.Column(name='E_MAX', format='D', array=np.array([e_max]))
+        col6 = pyfits.Column(name='FLUX', format='D', array=np.array([flux]))
+        col7 = pyfits.Column(name='SPECTRUM', format='80A', array=np.array([phfile+"[PHLIST,1]"]))
+        col8 = pyfits.Column(name='IMAGE', format='80A', array=np.array([phfile+"[PHLIST,1]"]))
+                        
+        coldefs = pyfits.ColDefs([col1, col2, col3, col4, col5, col6, col7, col8])
+        
+        wrhdu = pyfits.new_table(coldefs)
+        wrhdu.update_ext_name("SRC_CAT")
+                                
+        wrhdu.header.update("HDUCLASS", "HEASARC")
+        wrhdu.header.update("HDUCLAS1", "SIMPUT")
+        wrhdu.header.update("HDUCLAS2", "SRC_CAT")        
+        wrhdu.header.update("HDUVERS", "1.1.0")
+        wrhdu.header.update("RADECSYS", "FK5")
+        wrhdu.header.update("EQUINOX", 2000.0)
+        wrhdu.header.update("TUNIT2", "deg")
+        wrhdu.header.update("TUNIT3", "deg")
+        wrhdu.header.update("TUNIT4", "keV")
+        wrhdu.header.update("TUNIT5", "keV")
+        wrhdu.header.update("TUNIT6", "erg/s/cm**2")
+
+        simputfile = prefix+"_simput.fits"
+                
+        wrhdu.writeto(simputfile, clobber=clobber)
+
+    @parallel_root_only
+    def write_h5_file(self, h5file):
+        """
+        Write an EventList to the HDF5 file given by h5file.
+        """
+        f = h5py.File(h5file, "w")
+
+        f.create_dataset("/exp_time", data=self.events["ExposureTime"])
+        f.create_dataset("/area", data=self.events["Area"])
+        f.create_dataset("/redshift", data=self.events["Redshift"])
+        f.create_dataset("/d_a", data=self.events["AngularDiameterDistance"])        
+        f.create_dataset("/xsky", data=self.events["xsky"])
+        f.create_dataset("/ysky", data=self.events["ysky"])
+        f.create_dataset("/eobs", data=self.events["eobs"])
+                        
+        f.close()
+
+    @parallel_root_only
+    def write_fits_image(self, imagefile, width, nx, center,
+                         clobber=False, gzip_file=False,
+                         emin=None, emax=None):
+        """
+        Generate a image by binning X-ray counts and write it to a FITS file.
+        """
+        if emin is None:
+            mask_emin = np.ones((self.num_events), dtype='bool')
+        else:
+            mask_emin = self.events["eobs"] > emin
+        if emax is None:
+            mask_emax = np.ones((self.num_events), dtype='bool')
+        else:
+            mask_emax = self.events["eobs"] < emax
+
+        mask = np.logical_and(mask_emin, mask_emax)
+
+        dx_pixel = width/nx
+        xmin = -0.5*width
+        xmax = -xmin
+        xbins = np.linspace(xmin, xmax, nx+1, endpoint=True)
+        
+        H, xedges, yedges = np.histogram2d(self.events["xsky"][mask],
+                                           self.events["ysky"][mask],
+                                           bins=[xbins,xbins])
+        
+        hdu = pyfits.PrimaryHDU(H.T[::,::])
+
+        hdu.header.update("MTYPE1", "EQPOS")
+        hdu.header.update("MFORM1", "RA,DEC")
+        hdu.header.update("CTYPE1", "RA---TAN")
+        hdu.header.update("CTYPE2", "DEC--TAN")
+        hdu.header.update("CRPIX1", 0.5*(nx+1))
+        hdu.header.update("CRPIX2", 0.5*(nx+1))                
+        hdu.header.update("CRVAL1", center[0])
+        hdu.header.update("CRVAL2", center[1])
+        hdu.header.update("CUNIT1", "deg")
+        hdu.header.update("CUNIT2", "deg")
+        hdu.header.update("CDELT1", -dx_pixel/3600.)
+        hdu.header.update("CDELT2", dx_pixel/3600.)
+        hdu.header.update("EXPOSURE", self.events["ExposureTime"])
+        
+        hdu.writeto(imagefile, clobber=clobber)
+
+        if (gzip_file):
+            clob = ""
+            if (clobber) : clob="-f"
+            os.system("gzip "+clob+" %s.fits" % (prefix))
+                                    
+    @parallel_root_only
+    def write_spectrum(self, specfile, emin, emax, nchan, clobber=False):
+        
+        spec, ee = np.histogram(self.events["eobs"], bins=nchan, range=(emin, emax))
+
+        de = ee[1]-ee[0]
+        emid = 0.5*(ee[1:]+ee[:-1])
+        
+        col1 = pyfits.Column(name='ENERGY', format='1E', array=emid)
+        col2 = pyfits.Column(name='COUNTS', format='1E', array=spec)
+        
+        coldefs = pyfits.ColDefs([col1, col2])
+        
+        tbhdu = pyfits.new_table(coldefs)
+        
+        tbhdu.writeto(specfile, clobber=clobber)

diff -r 532068b4baf0c6281fb5ed45981629760df1cbcc -r 69a1540c12bd707961330376eb64c47a03c9052f yt/analysis_modules/synthetic_xray_obs/api.py
--- a/yt/analysis_modules/synthetic_xray_obs/api.py
+++ /dev/null
@@ -1,35 +0,0 @@
-"""
-API for spectral_integrator
-
-Author: John ZuHone <jzuhone at gmail.com>
-Affiliation: NASA/GSFC
-Homepage: http://yt-project.org/
-License:
-  Copyright (C) 2010-2011 Matthew Turk.  All Rights Reserved.
-
-  This file is part of yt.
-
-  yt is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 3 of the License, or
-  (at your option) any later version.
-
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-"""
-
-from .photon_simulator import \
-     PhotonList, \
-     EventList
-
-from .photon_models import \
-     XSpecThermalModel, \
-     XSpecAbsorbModel, \
-     TableApecModel, \
-     TableAbsorbModel

diff -r 532068b4baf0c6281fb5ed45981629760df1cbcc -r 69a1540c12bd707961330376eb64c47a03c9052f yt/analysis_modules/synthetic_xray_obs/photon_models.py
--- a/yt/analysis_modules/synthetic_xray_obs/photon_models.py
+++ /dev/null
@@ -1,215 +0,0 @@
-import numpy as np
-import os
-from yt.funcs import *
-import h5py
-try:
-    import pyfits
-except:
-    try:
-        import astropy.io.fits as pyfits
-    except:
-        mylog.error("Your mom doesn't have pyFITS")
-try:
-    import xspec
-except ImportError:
-    mylog.warning("You don't have PyXSpec installed. Some models won't be available.")
-from scipy.integrate import cumtrapz
-from scipy import stats
-from yt.utilities.physical_constants import hcgs, clight, erg_per_keV, amu_cgs
-
-hc = 1.0e8*hcgs*clight/erg_per_keV
-
-class PhotonModel(object):
-
-    def __init__(self, emin, emax, nchan):
-        self.emin = emin
-        self.emax = emax
-        self.nchan = nchan
-        self.ebins = np.linspace(emin, emax, nchan+1)
-        self.de = self.ebins[1]-self.ebins[0]
-        
-    def prepare(self):
-        pass
-    
-    def get_spectrum(self):
-        pass
-                                                        
-class XSpecThermalModel(PhotonModel):
-
-    def __init__(self, model_name, emin, emax, nchan):
-        self.model_name = model_name
-        PhotonModel.__init__(self, emin, emax, nchan)
-        
-    def prepare(self):
-        xspec.Xset.chatter = 0
-        xspec.AllModels.setEnergies("%f %f %d lin" %
-                                    (self.emin, self.emax, self.nchan))
-        self.model = xspec.Model(self.model_name)
-        
-    def get_spectrum(self, kT, Zmet):
-        m = getattr(self.model,self.model_name)
-        m.kT = kT
-        m.Abundanc = Zmet
-        m.norm = 1.0
-        m.Redshift = 0.0
-        return 1.0e-14*np.array(self.model.values(0))
-    
-class XSpecAbsorbModel(PhotonModel):
-
-    def __init__(self, model_name, nH, emin=0.01, emax=50.0, nchan=100000):
-        self.model_name = model_name
-        self.nH = nH
-        PhotonModel.__init__(self, emin, emax, nchan)
-        
-    def prepare(self):
-        xspec.Xset.chatter = 0
-        xspec.AllModels.setEnergies("%f %f %d lin" %
-                                    (self.emin, self.emax, self.nchan))
-        self.model = xspec.Model(self.model_name+"*powerlaw")
-        self.model.powerlaw.norm = self.nchan/(self.emax-self.emin)
-        self.model.powerlaw.PhoIndex = 0.0
-
-    def get_spectrum(self):
-        m = getattr(self.model,self.model_name)
-        m.nH = self.nH
-        return np.array(self.model.values(0))
-
-class TableApecModel(PhotonModel):
-
-    def __init__(self, apec_root, emin, emax, nchan,
-                 apec_vers="2.0.2", thermal_broad=False):
-        self.apec_root = apec_root
-        self.apec_prefix = "apec_v"+apec_vers
-        self.cocofile = os.path.join(self.apec_root,
-                                     self.apec_prefix+"_coco.fits")
-        self.linefile = os.path.join(self.apec_root,
-                                     self.apec_prefix+"_line.fits")
-        PhotonModel.__init__(self, emin, emax, nchan)
-        self.wvbins = hc/self.ebins[::-1]
-        # H, He, and trace elements
-        self.cosmic_elem = [1,2,3,4,5,9,11,15,17,19,21,22,23,24,25,27,29,30]
-        # Non-trace metals
-        self.metal_elem = [6,7,8,10,12,13,14,16,18,20,26,28]
-        self.thermal_broad = thermal_broad
-        self.A = np.array([0.0,1.00794,4.00262,6.941,9.012182,10.811,
-                           12.0107,14.0067,15.9994,18.9984,20.1797,
-                           22.9898,24.3050,26.9815,28.0855,30.9738,
-                           32.0650,35.4530,39.9480,39.0983,40.0780,
-                           44.9559,47.8670,50.9415,51.9961,54.9380,
-                           55.8450,58.9332,58.6934,63.5460,65.3800])
-        
-    def prepare(self):
-        try:
-            self.line_handle = pyfits.open(self.linefile)
-        except IOError:
-            mylog.error("LINE file %s does not exist" % (self.linefile))
-        try:
-            self.coco_handle = pyfits.open(self.cocofile)
-        except IOError:
-            mylog.error("COCO file %s does not exist" % (self.cocofile))
-        self.Tvals = self.line_handle[1].data.field("kT")
-        self.dTvals = np.diff(self.Tvals)
-        self.minlam = self.wvbins.min()
-        self.maxlam = self.wvbins.max()
-
-    def expand_E_grid(self, Eedges, Ncont, Econt, cont):
-        # linearly interpolate onto wvedges
-        # All energies should be in keV
-        newx = np.array(Econt[:Ncont])
-        newx = np.append(newx,Eedges)
-        newxargs = newx.argsort()
-        newx.sort()
-        newy = np.interp(newx, Econt, cont)
-        # simple integration
-        ct = cumtrapz(newy,newx)
-        ret = np.zeros((self.nchan))
-        iinew = np.where(newxargs==Ncont)[0][0]
-        for i in range(self.nchan):
-            ind = i + 1 + Ncont
-            iiold = iinew
-            iinew = np.where(newxargs==ind)[0][0]
-            ret[i] = ct[iinew-1]-ct[iiold-1]
-        return ret
-
-    def make_spectrum(self, element, tindex):
-        
-        tmpspec = np.zeros((self.nchan))
-        
-        i = np.where((self.line_handle[tindex].data.field('element')==element) &
-                     (self.line_handle[tindex].data.field('lambda') > self.minlam) &
-                     (self.line_handle[tindex].data.field('lambda') < self.maxlam))[0]
-
-        vec = np.zeros((self.nchan))
-        E0 = hc/self.line_handle[tindex].data.field('lambda')[i]
-        amp = self.line_handle[tindex].data.field('epsilon')[i]
-        if self.thermal_broad:
-            vec = np.zeros((self.nchan))
-            sigma = E0*np.sqrt(self.Tvals[tindex]*erg_per_keV/(self.A[element]*amu_cgs))/clight
-            for E, sig, a in zip(E0, sigma, amp):
-                cdf = stats.norm(E,sig).cdf(self.ebins)
-                vec += np.diff(cdf)*a
-        else:
-            ie = np.searchsorted(self.ebins, E0, side='right')-1
-            for i,a in zip(ie,amp): vec[i] += a
-        tmpspec += vec
-
-        ind = np.where((self.coco_handle[tindex].data.field('Z')==element) &
-                       (self.coco_handle[tindex].data.field('rmJ')==0))[0]
-        if len(ind)==0:
-            return tmpspec
-        else:
-            ind=ind[0]
-                                                    
-        n_cont=self.coco_handle[tindex].data.field('N_Cont')[ind]
-        e_cont=self.coco_handle[tindex].data.field('E_Cont')[ind][:n_cont]
-        continuum = self.coco_handle[tindex].data.field('Continuum')[ind][:n_cont]
-        
-        tmpspec += self.expand_E_grid(self.ebins, n_cont, e_cont, continuum)
-        
-        n_pseudo=self.coco_handle[tindex].data.field('N_Pseudo')[ind]
-        e_pseudo=self.coco_handle[tindex].data.field('E_Pseudo')[ind][:n_pseudo]
-        pseudo = self.coco_handle[tindex].data.field('Pseudo')[ind][:n_pseudo]
-        
-        tmpspec += self.expand_E_grid(self.ebins, n_pseudo, e_pseudo, pseudo)
-                                                        
-        return tmpspec
-
-    def get_spectrum(self, kT, Zmet):
-        cspec_l = np.zeros((self.nchan))
-        mspec_l = np.zeros((self.nchan))
-        cspec_r = np.zeros((self.nchan))
-        mspec_r = np.zeros((self.nchan))
-        tindex = np.searchsorted(self.Tvals, kT)-1
-        dT = (kT-self.Tvals[tindex])/self.dTvals[tindex]
-        # First do H,He, and trace elements
-        for elem in self.cosmic_elem:
-            cspec_l += self.make_spectrum(elem, tindex+2)
-            cspec_r += self.make_spectrum(elem, tindex+3)            
-        # Next do the metals
-        for elem in self.metal_elem:
-            mspec_l += self.make_spectrum(elem, tindex+2)
-            mspec_r += self.make_spectrum(elem, tindex+3)
-        cosmic_spec = cspec_l*(1.-dT)+cspec_r*dT
-        metal_spec = mspec_l*(1.-dT)+mspec_r*dT        
-        return cosmic_spec+Zmet*metal_spec
-
-class TableAbsorbModel(PhotonModel):
-
-    def __init__(self, filename):
-        if not os.path.exists(filename):
-            raise IOError("File does not exist: %s." % filename)
-        self.filename = filename
-        f = h5py.File(self.filename,"r")
-        emin = f["energ_lo"][:].min()
-        emax = f["energ_hi"][:].max()
-        self.abs = f["absorb_coeff"][:]
-        nchan = self.abs.shape[0]
-        f.close()
-        PhotonModel.__init__(self, emin, emax, nchan)
-                                                                    
-    def prepare(self):
-        pass
-
-    def get_spectrum(self):
-        return self.abs ** nH
-    

This diff is so big that we needed to truncate the remainder.

https://bitbucket.org/yt_analysis/yt/commits/ae20d1dcfca2/
Changeset:   ae20d1dcfca2
Branch:      yt
User:        jzuhone
Date:        2013-08-26 23:27:55
Summary:     Optimizations. Also fixing the table absorption model.
Affected #:  2 files

diff -r 69a1540c12bd707961330376eb64c47a03c9052f -r ae20d1dcfca20449e10ee75381713555a95b0b41 yt/analysis_modules/synthetic_obs/photon_models.py
--- a/yt/analysis_modules/synthetic_obs/photon_models.py
+++ b/yt/analysis_modules/synthetic_obs/photon_models.py
@@ -195,21 +195,21 @@
 
 class TableAbsorbModel(PhotonModel):
 
-    def __init__(self, filename):
+    def __init__(self, filename, nH):
         if not os.path.exists(filename):
             raise IOError("File does not exist: %s." % filename)
         self.filename = filename
         f = h5py.File(self.filename,"r")
         emin = f["energ_lo"][:].min()
         emax = f["energ_hi"][:].max()
-        self.abs = f["absorb_coeff"][:]
-        nchan = self.abs.shape[0]
+        self.sigma = f["cross_section"][:]
+        nchan = self.sigma.shape[0]
         f.close()
         PhotonModel.__init__(self, emin, emax, nchan)
-                                                                    
+        self.nH = nH
+        
     def prepare(self):
         pass
-
+        
     def get_spectrum(self):
-        return self.abs ** nH
-    
+        return np.exp(-self.sigma*self.nH)

diff -r 69a1540c12bd707961330376eb64c47a03c9052f -r ae20d1dcfca20449e10ee75381713555a95b0b41 yt/analysis_modules/synthetic_obs/photon_simulator.py
--- a/yt/analysis_modules/synthetic_obs/photon_simulator.py
+++ b/yt/analysis_modules/synthetic_obs/photon_simulator.py
@@ -121,8 +121,7 @@
         vol_scale = pf.units["cm"]**(-3)/np.prod(pf.domain_width)
 
         if cosmology is None and dist is None:
-            cosmo = Cosmology(HubbleConstantNow=71., OmegaMatterNow=0.27,
-                              OmegaLambdaNow=0.73)
+            cosmo = Cosmology()
         else:
             if cosmology is None:
                 D_A = dist*cm_per_mpc
@@ -193,7 +192,9 @@
         
         number_of_photons = np.zeros(dshape, dtype='uint64')
         energies = []
-                                
+        
+        u = np.random.random((ecell[-1]))
+        
         pbar = get_pbar("Generating Photons", dshape[0])
 
         for i, ikT in enumerate(kT_idxs):
@@ -204,21 +205,18 @@
             kT = kT_bins[ikT] + 0.5*dkT
             
             em_sum = cell_em[ibegin:iend].sum()
-            vol_sum = cell_vol[ibegin:iend].sum()
-            em_avg = em_sum/vol_sum
-            
             tot_norm = dist_fac*em_sum/vol_scale
-            
+
             spec = emission_model.get_spectrum(kT, Zmet)
             spec *= tot_norm
             cumspec = np.cumsum(spec)
             counts = cumspec[:]/cumspec[-1]
             tot_ph = cumspec[-1]*eff_A*exp_time
-            
+
             for icell in xrange(ibegin, iend):
                 
-                cell_norm = tot_ph * (cell_emd[icell]/em_avg) * (cell_vol[icell]/vol_sum)                    
-                cell_Nph = int(cell_norm) + int(np.modf(cell_norm)[0] >= np.random.random())
+                cell_norm = tot_ph*cell_em[icell]/em_sum
+                cell_Nph = np.uint64(cell_norm) + np.uint64(np.modf(cell_norm)[0] >= u[icell])
                 
                 if cell_Nph > 0:
                     number_of_photons[icell] = cell_Nph                    
@@ -232,7 +230,7 @@
             
         pbar.finish()
 
-        del cell_emd, cell_vol, cell_em
+        del cell_vol, cell_em
 
         active_cells = number_of_photons > 0
         idxs = idxs[active_cells]
@@ -260,7 +258,7 @@
 
     @classmethod
     def from_user_model(cls, data_source, redshift, eff_A,
-                        exp_time, user_function, parameters={}
+                        exp_time, user_function, parameters={},
                         dist=None, cosmology=None):
 
         if cosmology is None and dist is None:
@@ -398,7 +396,7 @@
         if redshift_new is not None and dist_new is not None:
             mylog.error("You may specify a new redshift or distance, but not both!")
 
-        if redshift_new is not Done and self.cosmo is None:
+        if redshift_new is not None and self.cosmo is None:
             mylog.error("Specified a new redshift, but no cosmology!")
             
         dx = self.photons["dx"]
@@ -423,6 +421,8 @@
         if (texp_new is None and area_new is None and
             redshift_new is None and dist_new is None):
             my_n_obs = n_ph_tot
+            zobs = self.photons["FiducialRedshift"]
+            D_A = self.photons["FiducialAngularDiameterDistance"]*1000.
         else:
             if texp_new is None:
                 Tratio = 1.
@@ -764,8 +764,8 @@
                                            self.events["ysky"][mask],
                                            bins=[xbins,xbins])
         
-        hdu = pyfits.PrimaryHDU(H.T[::,::])
-
+        hdu = pyfits.PrimaryHDU(H.T)
+        
         hdu.header.update("MTYPE1", "EQPOS")
         hdu.header.update("MFORM1", "RA,DEC")
         hdu.header.update("CTYPE1", "RA---TAN")


https://bitbucket.org/yt_analysis/yt/commits/6d16489c1b2f/
Changeset:   6d16489c1b2f
Branch:      yt
User:        jzuhone
Date:        2013-09-06 00:09:28
Summary:     Small change for TableAbsorbModel
Affected #:  1 file

diff -r ae20d1dcfca20449e10ee75381713555a95b0b41 -r 6d16489c1b2f21e4223c643056614e74b91596e3 yt/analysis_modules/synthetic_obs/photon_models.py
--- a/yt/analysis_modules/synthetic_obs/photon_models.py
+++ b/yt/analysis_modules/synthetic_obs/photon_models.py
@@ -171,7 +171,7 @@
         pseudo = self.coco_handle[tindex].data.field('Pseudo')[ind][:n_pseudo]
         
         tmpspec += self.expand_E_grid(self.ebins, n_pseudo, e_pseudo, pseudo)
-                                                        
+
         return tmpspec
 
     def get_spectrum(self, kT, Zmet):
@@ -200,13 +200,13 @@
             raise IOError("File does not exist: %s." % filename)
         self.filename = filename
         f = h5py.File(self.filename,"r")
-        emin = f["energ_lo"][:].min()
-        emax = f["energ_hi"][:].max()
+        emin = f["energy"][:].min()
+        emax = f["energy"][:].max()
         self.sigma = f["cross_section"][:]
         nchan = self.sigma.shape[0]
         f.close()
         PhotonModel.__init__(self, emin, emax, nchan)
-        self.nH = nH
+        self.nH = nH*1.0e22
         
     def prepare(self):
         pass


https://bitbucket.org/yt_analysis/yt/commits/397a8506bd1f/
Changeset:   397a8506bd1f
Branch:      yt
User:        jzuhone
Date:        2013-09-06 00:11:28
Summary:     Full support for response files and writing spectra.

Some optimizations.
Affected #:  1 file

diff -r 6d16489c1b2f21e4223c643056614e74b91596e3 -r 397a8506bd1fa9602ad27c52a34e573ce040a576 yt/analysis_modules/synthetic_obs/photon_simulator.py
--- a/yt/analysis_modules/synthetic_obs/photon_simulator.py
+++ b/yt/analysis_modules/synthetic_obs/photon_simulator.py
@@ -6,7 +6,6 @@
 from yt.utilities.orientation import Orientation
 from yt.utilities.parallel_tools.parallel_analysis_interface import \
      communication_system, parallel_root_only, get_mpi_type, parallel_capable
-from yt.data_objects.api import add_field
 
 import os
 import h5py
@@ -23,7 +22,6 @@
 N_TBIN = 10000
 TMIN = 8.08e-2
 TMAX = 50.
-FOUR_PI = 4.*np.pi
 
 comm = communication_system.communicators[-1]
         
@@ -130,8 +128,8 @@
                 cosmo = cosmology
         if cosmo is not None:
             D_A = cosmo.AngularDiameterDistance(0.0,redshift)*cm_per_mpc
-        dist_fac = 1.0/(FOUR_PI*D_A*D_A*(1.+redshift)**3)
-
+        dist_fac = 1.0/(4.*np.pi*D_A*D_A*(1.+redshift)**3)
+        
         num_cells = data_source["Temperature"].shape[0]
         start_c = comm.rank*num_cells/comm.size
         end_c = (comm.rank+1)*num_cells/comm.size
@@ -188,12 +186,11 @@
         
         cell_em = EM[idxs]*vol_scale
         cell_vol = vol[idxs]*vol_scale
-        cell_emd = cell_em/cell_vol
         
         number_of_photons = np.zeros(dshape, dtype='uint64')
         energies = []
         
-        u = np.random.random((ecell[-1]))
+        u = np.random.random(cell_em.shape)
         
         pbar = get_pbar("Generating Photons", dshape[0])
 
@@ -211,6 +208,7 @@
             spec *= tot_norm
             cumspec = np.cumsum(spec)
             counts = cumspec[:]/cumspec[-1]
+            counts = np.insert(counts, 0, 0.0)
             tot_ph = cumspec[-1]*eff_A*exp_time
 
             for icell in xrange(ibegin, iend):
@@ -222,8 +220,7 @@
                     number_of_photons[icell] = cell_Nph                    
                     randvec = np.random.uniform(low=counts[0], high=counts[-1], size=cell_Nph)
                     randvec.sort()
-                    eidxs = np.searchsorted(counts, randvec)-1
-                    cell_e = emid[eidxs]+de*(randvec-counts[eidxs])/(counts[eidxs+1]-counts[eidxs])
+                    cell_e = np.interp(randvec, counts, energy)
                     energies.append(cell_e)
                             
                 pbar.update(icell)
@@ -487,23 +484,20 @@
         y += self.photons["y"][obs_cells]
         z += self.photons["z"][obs_cells]  
         eobs = self.photons["Energy"][idxs]*shift[obs_cells]
-
+            
         xsky = x*x_hat[0] + y*x_hat[1] + z*x_hat[2]
         ysky = x*y_hat[0] + y*y_hat[1] + z*y_hat[2]
         eobs /= (1.+zobs)
-
+        
         if absorb_model is None:
             not_abs = np.ones(eobs.shape, dtype='bool')
         else:
             mylog.info("Absorbing.")
             absorb_model.prepare()
             energy = absorb_model.ebins
-            de = absorb_model.de
             emid = 0.5*(energy[1:]+energy[:-1])
             aspec = absorb_model.get_spectrum()
-            eidxs = np.searchsorted(emid, eobs)-1
-            dx = (eobs-emid[eidxs])/de
-            absorb = aspec[eidxs]*(1.-dx) + aspec[eidxs+1]*dx
+            absorb = np.interp(eobs, emid, aspec, left=0.0, right=0.0)
             randvec = aspec.max()*np.random.random(eobs.shape)
             not_abs = randvec < absorb
 
@@ -512,10 +506,7 @@
         else:
             mylog.info("Applying energy-dependent effective area.")
             earf = 0.5*(elo+ehi)
-            de = earf[1]-earf[0]            
-            eidxs = np.searchsorted(earf, eobs)-1
-            dx = (eobs-earf[eidxs])/de
-            earea = eff_area[eidxs]*(1.-dx) + eff_area[eidxs+1]*dx
+            earea = np.interp(eobs, earf, eff_area, left=0.0, right=0.0)
             randvec = eff_area.max()*np.random.random(eobs.shape)
             detected = randvec < earea
         
@@ -547,7 +538,9 @@
             events["Area"] = area_new
         events["Redshift"] = zobs
         events["AngularDiameterDistance"] = D_A/1000.
-                
+        if isinstance(area_new, basestring):
+            events["ARF"] = area_new
+
         return EventList(events)
 
 class EventList(object) :
@@ -583,10 +576,22 @@
         events["Area"] = f["/area"].value
         events["Redshift"] = f["/redshift"].value
         events["AngularDiameterDistance"] = f["/d_a"].value
-        
+        if "rmf" in f:
+            events["RMF"] = f["/rmf"].value
+        if "arf" in f:
+            events["ARF"] = f["/arf"].value
+        if "channel_type" in f:
+            events["ChannelType"] = f["/channel_type"].value
+        if "telescope" in f:
+            events["Telescope"] = f["/telescope"].value
+        if "instrument" in f:
+            events["Instrument"] = f["/instrument"].value
+                            
         events["xsky"] = f["/xsky"][:]
         events["ysky"] = f["/ysky"][:]
         events["eobs"] = f["/eobs"][:]
+        if "chan" in f:
+            events["chan"] = f["/chan"][:]
         
         f.close()
         
@@ -602,16 +607,28 @@
         tblhdu = hdulist[1]
 
         events = {}
-        
+
         events["ExposureTime"] = tblhdu.header["EXPOSURE"]
         events["Area"] = tblhdu.header["AREA"]
         events["Redshift"] = tblhdu.header["REDSHIFT"]
         events["AngularDiameterDistance"] = tblhdu.header["D_A"]
-        
+        if "RMF" in tblhdu.header:
+            events["RMF"] = tblhdu["RMF"]
+        if "ARF" in tblhdu.header:
+            events["ARF"] = tblhdu["ARF"]
+        if "CHANTYPE" in tblhdu.header:
+            events["ChannelType"] = tblhdu["CHANTYPE"]
+        if "TELESCOP" in tblhdu.header:
+            events["Telescope"] = tblhdu["TELESCOP"]
+        if "INSTRUME" in tblhdu.header:
+            events["Instrument"] = tblhdu["INSTRUME"]
+                            
         events["xsky"] = tblhdu.data.field("POS_X")
         events["ysky"] = tblhdu.data.field("POS_Y")
         events["eobs"] = tblhdu.data.field("ENERGY")
-        
+        if "CHANNEL" in tblhdu.columns.names:
+            events["chan"] = tblhdu.data.field("CHANNEL")
+                    
         return cls(events)
 
     @classmethod
@@ -625,30 +642,112 @@
             else:
                 events[k1] = v1            
         return cls(events)
-    
+
     def convolve_with_response(self, respfile):
-        pass
+        
+        mylog.info("Reading response matrix file (RMF): %s" % (respfile))
+        
+        hdulist = pyfits.open(respfile)
 
+        tblhdu = hdulist[1]
+        n_de = len(tblhdu.data["ENERG_LO"])
+        mylog.info("Number of Energy Bins: %d" % (n_de))
+        de = tblhdu.data["ENERG_HI"] - tblhdu.data["ENERG_LO"]
+
+        mylog.info("Energy limits: %g %g" % (min(tblhdu.data["ENERG_LO"]), max(tblhdu.data["ENERG_HI"] )))
+        
+        tblhdu2 = hdulist[2]
+        n_ch = len(tblhdu2.data["CHANNEL"])
+        mylog.info("Number of Channels: %d" % (n_ch))
+        
+        eidxs = np.argsort(self.events["eobs"])
+
+        phEE = self.events["eobs"][eidxs]
+        phXX = self.events["xsky"][eidxs]
+        phYY = self.events["ysky"][eidxs]
+
+        detectedChannels = []
+        pindex = 0
+
+        # run through all photon energies and find which bin they go in
+        k = 0
+        fcurr = 0
+        last = len(phEE)-1
+
+        pbar = get_pbar("Scattering energies with RMF:", n_de)
+        
+        for low,high in zip(tblhdu.data["ENERG_LO"],tblhdu.data["ENERG_HI"]):
+            # weight function for probabilities from RMF
+            weights = tblhdu.data[k]["MATRIX"][:]
+            weights /= weights.sum()
+            # build channel number list associated to array value, there are groups of channels in rmfs with nonzero probabilities
+            trueChannel = []
+            for start,nchan in zip(tblhdu.data[k]["F_CHAN"],tblhdu.data[k]["N_CHAN"]):
+                end = start + nchan
+                for j in range(start,end):
+                    trueChannel.append(j)
+            for q in range(fcurr,last):
+                if phEE[q]  >= low and phEE[q] < high:
+                    channelInd = np.random.choice(len(weights), p=weights)
+                    fcurr +=1
+                    detectedChannels.append(trueChannel[channelInd])
+                if phEE[q] >= high:
+                    break
+            pbar.update(k)
+            k+=1
+        pbar.finish()
+        
+        dchannel = np.array(detectedChannels)
+
+        self.events["xsky"] = phXX
+        self.events["ysky"] = phYY
+        self.events["eobs"] = phEE
+        self.events["chan"] = dchannel.astype(int)
+        self.events["RMF"] = respfile
+        self.events["ChannelType"] = tblhdu.header["CHANTYPE"]
+        self.events["Telescope"] = tblhdu.header["TELESCOP"]
+        self.events["Instrument"] = tblhdu.header["INSTRUME"]
+        
     @parallel_root_only
     def write_fits_file(self, fitsfile, clobber=False):
         """
         Write events to a FITS binary table file.
         """
+
+        cols = []
+
         col1 = pyfits.Column(name='ENERGY', format='E',
                              array=self.events["eobs"])
         col2 = pyfits.Column(name='XSKY', format='D',
                              array=self.events["xsky"])
         col3 = pyfits.Column(name='YSKY', format='D',
                              array=self.events["ysky"])
-        
-        coldefs = pyfits.ColDefs([col1, col2, col3])
-        
+
+        cols = [col1, col2, col3]
+
+        if "chan" in self.events:
+            col4 = pyfits.Column(name='CHANNEL', format='1J',
+                                 array=self.events["chan"])
+            cols.append(col4)
+            
+        coldefs = pyfits.ColDefs(cols)
         tbhdu = pyfits.new_table(coldefs)
 
         tbhdu.header.update("EXPOSURE", self.events["ExposureTime"])
         tbhdu.header.update("AREA", self.events["Area"])
-        tbhdu.header.update("D_A", self.evenets["AngularDiameterDistance"])
-                
+        tbhdu.header.update("D_A", self.events["AngularDiameterDistance"])
+        tbhdu.header.update("REDSHIFT", self.events["Redshift"])
+        if "RMF" in self.events:
+            tbhdu.header.update("RMF", self.events["RMF"])
+        if "ARF" in self.events:
+            tbhdu.header.update("ARF", self.events["ARF"])
+        if "ChannelType" in self.events:
+            tbhdu.header.update("CHANTYPE", self.events["ChannelType"])
+        if "Telescope" in self.events:
+            tbhdu.header.update("TELESCOP", self.events["Telescope"])
+        if "Instrument" in self.events:
+            tbhdu.header.update("INSTRUME", self.events["Instrument"])
+            
         tbhdu.writeto(fitsfile, clobber=clobber)
 
     @parallel_root_only    
@@ -731,10 +830,23 @@
         f.create_dataset("/area", data=self.events["Area"])
         f.create_dataset("/redshift", data=self.events["Redshift"])
         f.create_dataset("/d_a", data=self.events["AngularDiameterDistance"])        
+        if "ARF" in self.events:
+            f.create_dataset("/arf", data=self.events["ARF"])
+        if "RMF" in self.events:
+            f.create_dataset("/rmf", data=self.events["RMF"])
+        if "ChannelType" in self.events:
+            f.create_dataset("/channel_type", data=self.events["ChannelType"])
+        if "Telescope" in self.events:
+            f.create_dataset("/telescope", data=self.events["Telescope"])
+        if "Instrument" in self.events:
+            f.create_dataset("/instrument", data=self.events["Instrument"])
+                            
         f.create_dataset("/xsky", data=self.events["xsky"])
         f.create_dataset("/ysky", data=self.events["ysky"])
         f.create_dataset("/eobs", data=self.events["eobs"])
-                        
+        if "chan" in self.events:
+            f.create_dataset("/chan", data=self.events["chan"])                  
+        
         f.close()
 
     @parallel_root_only
@@ -782,24 +894,78 @@
         
         hdu.writeto(imagefile, clobber=clobber)
 
-        if (gzip_file):
+        if gzip_file:
             clob = ""
-            if (clobber) : clob="-f"
+            if clobber: clob="-f"
             os.system("gzip "+clob+" %s.fits" % (prefix))
                                     
     @parallel_root_only
-    def write_spectrum(self, specfile, emin, emax, nchan, clobber=False):
+    def write_spectrum(self, specfile, emin=0.1, emax=10.0, nchan=2000, clobber=False):
+
+        if "chan" in self.events:
+            spectype = self.events["ChannelType"]
+            espec = self.events["chan"]
+        else:
+            spectype = "energy"
+            espec = self.events["eobs"]
+            
+        if spectype == "PI":
+            bins = 1024
+            range = (0.5, 1024.5)
+        else:
+            bins = nchan
+            range = (emin, emax)
+            
+        spec, ee = np.histogram(espec, bins=bins, range=range)
+
+        emid = 0.5*(ee[1:]+ee[:-1])
+
+        col1 = pyfits.Column(name='CHANNEL', format='1J', array=np.arange(spec.shape[0], dtype='int32')+1)
+        col2 = pyfits.Column(name=spectype.upper(), format='1D', array=emid)
+        col3 = pyfits.Column(name='COUNTS', format='1J', array=spec.astype("int32"))
+        col4 = pyfits.Column(name='COUNT_RATE', format='1D', array=spec/self.events["ExposureTime"])
         
-        spec, ee = np.histogram(self.events["eobs"], bins=nchan, range=(emin, emax))
-
-        de = ee[1]-ee[0]
-        emid = 0.5*(ee[1:]+ee[:-1])
-        
-        col1 = pyfits.Column(name='ENERGY', format='1E', array=emid)
-        col2 = pyfits.Column(name='COUNTS', format='1E', array=spec)
-        
-        coldefs = pyfits.ColDefs([col1, col2])
+        coldefs = pyfits.ColDefs([col1, col2, col3, col4])
         
         tbhdu = pyfits.new_table(coldefs)
+        tbhdu.update_ext_name("SPECTRUM")
+
+        tbhdu.header.update("DETCHANS", spec.shape[0])
+        tbhdu.header.update("TOTCTS", spec.sum())
+        tbhdu.header.update("EXPOSURE", self.events["ExposureTime"])
+        tbhdu.header.update("LIVETIME", self.events["ExposureTime"])        
+        tbhdu.header.update("CONTENT", spectype)
+        tbhdu.header.update("HDUCLASS", "OGIP")
+        tbhdu.header.update("HDUCLAS1", "SPECTRUM")
+        tbhdu.header.update("HDUCLAS2", "TOTAL")
+        tbhdu.header.update("HDUCLAS3", "TYPE:I")
+        tbhdu.header.update("HDUCLAS4", "COUNT")
+        tbhdu.header.update("HDUVERS", "1.1.0")
+        tbhdu.header.update("HDUVERS1", "1.1.0")
+        tbhdu.header.update("CHANTYPE", spectype)
+        tbhdu.header.update("BACKFILE", "none")
+        tbhdu.header.update("CORRFILE", "none")
+        tbhdu.header.update("POISSERR", True)
+        if self.events.has_key("RMF"):
+            tbhdu.header.update("RESPFILE", self.events["RMF"])
+        else:
+            tbhdu.header.update("RESPFILE", "none")
+        if self.events.has_key("ARF"):
+            tbhdu.header.update("ANCRFILE", self.events["ARF"])
+        else:        
+            tbhdu.header.update("ANCRFILE", "none")
+        if self.events.has_key("Telescope"):
+            tbhdu.header.update("TELESCOP", self.events["Telescope"])
+        else:
+            tbhdu.header.update("TELESCOP", "none")
+        if self.events.has_key("Instrument"):
+            tbhdu.header.update("INSTRUME", self.events["Instrument"])
+        else:
+            tbhdu.header.update("INSTRUME", "none")
+        tbhdu.header.update("AREASCAL", 1.0)
+        tbhdu.header.update("CORRSCAL", 0.0)
+        tbhdu.header.update("BACKSCAL", 1.0)
+                                
+        hdulist = pyfits.HDUList([pyfits.PrimaryHDU(), tbhdu])
         
-        tbhdu.writeto(specfile, clobber=clobber)
+        hdulist.writeto(specfile, clobber=clobber)


https://bitbucket.org/yt_analysis/yt/commits/3e1b38d17244/
Changeset:   3e1b38d17244
Branch:      yt
User:        jzuhone
Date:        2013-09-10 05:54:36
Summary:     1) A bug fix for the APEC table model. Now everything matches up with what XSPEC says.
2) Separate spectra for cosmic abundances and metal abundances, so that spatially-dependent metallicity can be supported.
Affected #:  1 file

diff -r 397a8506bd1fa9602ad27c52a34e573ce040a576 -r 3e1b38d172448a4ad04bb1f9483c08e55f0c6028 yt/analysis_modules/synthetic_obs/photon_models.py
--- a/yt/analysis_modules/synthetic_obs/photon_models.py
+++ b/yt/analysis_modules/synthetic_obs/photon_models.py
@@ -1,3 +1,29 @@
+"""
+
+Spectral models for generating photons
+
+Author: John ZuHone <jzuhone at gmail.com>
+Affiliation: NASA/GSFC
+Homepage: http://yt-project.org/
+License:
+Copyright (C) 2010-2011 Matthew Turk.  All Rights Reserved.
+
+This file is part of yt.
+
+yt is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+"""
+
 import numpy as np
 import os
 from yt.funcs import *
@@ -8,13 +34,17 @@
     try:
         import astropy.io.fits as pyfits
     except:
-        mylog.error("Your mom doesn't have pyFITS")
+        mylog.error("You don't have pyFITS installed. The APEC table model won't be available.")
 try:
     import xspec
 except ImportError:
     mylog.warning("You don't have PyXSpec installed. Some models won't be available.")
-from scipy.integrate import cumtrapz
-from scipy import stats
+try:
+    from scipy.integrate import cumtrapz
+    from scipy import stats
+except ImportError:
+    mylog.warning("You don't have SciPy installed. The APEC table model won't be avabilable.")
+    
 from yt.utilities.physical_constants import hcgs, clight, erg_per_keV, amu_cgs
 
 hc = 1.0e8*hcgs*clight/erg_per_keV
@@ -26,7 +56,8 @@
         self.emax = emax
         self.nchan = nchan
         self.ebins = np.linspace(emin, emax, nchan+1)
-        self.de = self.ebins[1]-self.ebins[0]
+        self.de = np.diff(self.ebins)
+        self.emid = 0.5*(self.ebins[1:]+self.ebins[:-1])
         
     def prepare(self):
         pass
@@ -45,15 +76,25 @@
         xspec.AllModels.setEnergies("%f %f %d lin" %
                                     (self.emin, self.emax, self.nchan))
         self.model = xspec.Model(self.model_name)
+        if self.model_name == "bremss":
+            self.norm = 3.02e-15
+        else:
+            self.norm = 1.0e-14
         
-    def get_spectrum(self, kT, Zmet):
+    def get_spectrum(self, kT):
         m = getattr(self.model,self.model_name)
         m.kT = kT
-        m.Abundanc = Zmet
+        m.Abundanc = 0.0
         m.norm = 1.0
         m.Redshift = 0.0
-        return 1.0e-14*np.array(self.model.values(0))
-    
+        cosmic_spec = self.norm*np.array(self.model.values(0))
+        m.Abundanc = 1.0
+        if self.model_name == "bremss":
+            metal_spec = np.zeros((self.nchan))
+        else:
+            metal_spec = self.norm*np.array(self.model.values(0)) - cosmic_spec
+        return cosmic_spec, metal_spec
+        
 class XSpecAbsorbModel(PhotonModel):
 
     def __init__(self, model_name, nH, emin=0.01, emax=50.0, nchan=100000):
@@ -111,26 +152,7 @@
         self.dTvals = np.diff(self.Tvals)
         self.minlam = self.wvbins.min()
         self.maxlam = self.wvbins.max()
-
-    def expand_E_grid(self, Eedges, Ncont, Econt, cont):
-        # linearly interpolate onto wvedges
-        # All energies should be in keV
-        newx = np.array(Econt[:Ncont])
-        newx = np.append(newx,Eedges)
-        newxargs = newx.argsort()
-        newx.sort()
-        newy = np.interp(newx, Econt, cont)
-        # simple integration
-        ct = cumtrapz(newy,newx)
-        ret = np.zeros((self.nchan))
-        iinew = np.where(newxargs==Ncont)[0][0]
-        for i in range(self.nchan):
-            ind = i + 1 + Ncont
-            iiold = iinew
-            iinew = np.where(newxargs==ind)[0][0]
-            ret[i] = ct[iinew-1]-ct[iiold-1]
-        return ret
-
+    
     def make_spectrum(self, element, tindex):
         
         tmpspec = np.zeros((self.nchan))
@@ -163,18 +185,18 @@
         n_cont=self.coco_handle[tindex].data.field('N_Cont')[ind]
         e_cont=self.coco_handle[tindex].data.field('E_Cont')[ind][:n_cont]
         continuum = self.coco_handle[tindex].data.field('Continuum')[ind][:n_cont]
-        
-        tmpspec += self.expand_E_grid(self.ebins, n_cont, e_cont, continuum)
+
+        tmpspec += np.interp(self.emid, e_cont, continuum)*self.de
         
         n_pseudo=self.coco_handle[tindex].data.field('N_Pseudo')[ind]
         e_pseudo=self.coco_handle[tindex].data.field('E_Pseudo')[ind][:n_pseudo]
         pseudo = self.coco_handle[tindex].data.field('Pseudo')[ind][:n_pseudo]
         
-        tmpspec += self.expand_E_grid(self.ebins, n_pseudo, e_pseudo, pseudo)
-
+        tmpspec += np.interp(self.emid, e_pseudo, pseudo)*self.de
+        
         return tmpspec
 
-    def get_spectrum(self, kT, Zmet):
+    def get_spectrum(self, kT):
         cspec_l = np.zeros((self.nchan))
         mspec_l = np.zeros((self.nchan))
         cspec_r = np.zeros((self.nchan))
@@ -191,7 +213,7 @@
             mspec_r += self.make_spectrum(elem, tindex+3)
         cosmic_spec = cspec_l*(1.-dT)+cspec_r*dT
         metal_spec = mspec_l*(1.-dT)+mspec_r*dT        
-        return cosmic_spec+Zmet*metal_spec
+        return cosmic_spec, metal_spec
 
 class TableAbsorbModel(PhotonModel):
 


https://bitbucket.org/yt_analysis/yt/commits/67fe5d0822d4/
Changeset:   67fe5d0822d4
Branch:      yt
User:        jzuhone
Date:        2013-09-10 05:55:34
Summary:     Support for spatially-dependent metallicity. Also warning messages for response usage.
Affected #:  2 files

diff -r 3e1b38d172448a4ad04bb1f9483c08e55f0c6028 -r 67fe5d0822d4084e1253da0016377248fed330b2 yt/analysis_modules/synthetic_obs/api.py
--- a/yt/analysis_modules/synthetic_obs/api.py
+++ b/yt/analysis_modules/synthetic_obs/api.py
@@ -1,5 +1,5 @@
 """
-API for synthetic_obs
+API for yt.analysis_modules.synthetic_obs
 
 Author: John ZuHone <jzuhone at gmail.com>
 Affiliation: NASA/GSFC
@@ -21,7 +21,6 @@
 
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
 """
 
 from .photon_simulator import \

diff -r 3e1b38d172448a4ad04bb1f9483c08e55f0c6028 -r 67fe5d0822d4084e1253da0016377248fed330b2 yt/analysis_modules/synthetic_obs/photon_simulator.py
--- a/yt/analysis_modules/synthetic_obs/photon_simulator.py
+++ b/yt/analysis_modules/synthetic_obs/photon_simulator.py
@@ -1,4 +1,30 @@
+"""
+Classes for generating lists of photons and detected events
+
+Author: John ZuHone <jzuhone at gmail.com>
+Affiliation: NASA/GSFC
+Homepage: http://yt-project.org/
+License:
+Copyright (C) 2010-2011 Matthew Turk.  All Rights Reserved.
+
+This file is part of yt.
+
+yt is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+"""
+
 import numpy as np
+from numpy.testing import assert_allclose
 from yt.funcs import *
 from yt.utilities.physical_constants import mp, clight, cm_per_kpc, \
      cm_per_mpc, cm_per_km, K_per_keV, erg_per_keV
@@ -151,7 +177,12 @@
         vx = data_source["x-velocity"][start_c:end_c].copy()
         vy = data_source["y-velocity"][start_c:end_c].copy()
         vz = data_source["z-velocity"][start_c:end_c].copy()
-        
+
+        if isinstance(Zmet, basestring):
+            metalZ = data_source[zmet][start_c:end_c].copy()
+        else:
+            metalZ = Zmet*np.ones(EM.shape)
+            
         data_source.clear_data()
                 
         idxs = np.argsort(kT)
@@ -181,9 +212,7 @@
 
         emission_model.prepare()
         energy = emission_model.ebins
-        de = emission_model.de
-        emid = 0.5*(energy[1:]+energy[:-1])
-        
+              
         cell_em = EM[idxs]*vol_scale
         cell_vol = vol[idxs]*vol_scale
         
@@ -201,34 +230,47 @@
             iend = ecell[i]
             kT = kT_bins[ikT] + 0.5*dkT
             
-            em_sum = cell_em[ibegin:iend].sum()
-            tot_norm = dist_fac*em_sum/vol_scale
+            em_sum_c = cell_em[ibegin:iend].sum()
+            em_sum_m = (metalZ[ibegin:iend]*cell_em[ibegin:iend]).sum() 
 
-            spec = emission_model.get_spectrum(kT, Zmet)
-            spec *= tot_norm
-            cumspec = np.cumsum(spec)
-            counts = cumspec[:]/cumspec[-1]
-            counts = np.insert(counts, 0, 0.0)
-            tot_ph = cumspec[-1]*eff_A*exp_time
+            cspec, mspec = emission_model.get_spectrum(kT)
+            cspec *= dist_fac*em_sum_c/vol_scale
+            mspec *= dist_fac*em_sum_m/vol_scale
+            
+            cumspec_c = np.cumsum(cspec)
+            counts_c = cumspec_c[:]/cumspec_c[-1]
+            counts_c = np.insert(counts_c, 0, 0.0)
+            tot_ph_c = cumspec_c[-1]*eff_A*exp_time
 
+            cumspec_m = np.cumsum(mspec)
+            counts_m = cumspec_m[:]/cumspec_m[-1]
+            counts_m = np.insert(counts_m, 0, 0.0)
+            tot_ph_m = cumspec_m[-1]*eff_A*exp_time
+            
             for icell in xrange(ibegin, iend):
                 
-                cell_norm = tot_ph*cell_em[icell]/em_sum
-                cell_Nph = np.uint64(cell_norm) + np.uint64(np.modf(cell_norm)[0] >= u[icell])
+                cell_norm_c = tot_ph_c*cell_em[icell]/em_sum_c
+                cell_n_c = np.uint64(cell_norm_c) + np.uint64(np.modf(cell_norm_c)[0] >= u[icell])
+
+                cell_norm_m = tot_ph_m*metalZ[icell]*cell_em[icell]/em_sum_m
+                cell_n_m = np.uint64(cell_norm_m) + np.uint64(np.modf(cell_norm_m)[0] >= u[icell])
+                                                
+                cell_n = cell_n_c + cell_n_m
                 
-                if cell_Nph > 0:
-                    number_of_photons[icell] = cell_Nph                    
-                    randvec = np.random.uniform(low=counts[0], high=counts[-1], size=cell_Nph)
-                    randvec.sort()
-                    cell_e = np.interp(randvec, counts, energy)
-                    energies.append(cell_e)
-                            
+                if cell_n > 0:
+                    number_of_photons[icell] = cell_n                    
+                    randvec_c = np.random.uniform(size=cell_n_c)
+                    randvec_c.sort()
+                    randvec_m = np.random.uniform(size=cell_n_m)
+                    randvec_m.sort()
+                    cell_e_c = np.interp(randvec_c, counts_c, energy)
+                    cell_e_m = np.interp(randvec_m, counts_m, energy)
+                    energies.append(np.concatenate([cell_e_c,cell_e_m]))
+                    
                 pbar.update(icell)
             
         pbar.finish()
 
-        del cell_vol, cell_em
-
         active_cells = number_of_photons > 0
         idxs = idxs[active_cells]
         
@@ -391,7 +433,8 @@
         """
 
         if redshift_new is not None and dist_new is not None:
-            mylog.error("You may specify a new redshift or distance, but not both!")
+            mylog.error("You may specify a new redshift or distance, "+
+                        "but not both!")
 
         if redshift_new is not None and self.cosmo is None:
             mylog.error("Specified a new redshift, but no cosmology!")
@@ -494,8 +537,7 @@
         else:
             mylog.info("Absorbing.")
             absorb_model.prepare()
-            energy = absorb_model.ebins
-            emid = 0.5*(energy[1:]+energy[:-1])
+            emid = absorb_model.emid
             aspec = absorb_model.get_spectrum()
             absorb = np.interp(eobs, emid, aspec, left=0.0, right=0.0)
             randvec = aspec.max()*np.random.random(eobs.shape)
@@ -644,7 +686,12 @@
         return cls(events)
 
     def convolve_with_response(self, respfile):
-        
+
+        if not "ARF" in self.events:
+            mylog.warning("Photons have not been processed with an"+
+                          " auxiliary response file. Spectral fitting"+
+                          " may be inaccurate.")
+
         mylog.info("Reading response matrix file (RMF): %s" % (respfile))
         
         hdulist = pyfits.open(respfile)
@@ -654,8 +701,22 @@
         mylog.info("Number of Energy Bins: %d" % (n_de))
         de = tblhdu.data["ENERG_HI"] - tblhdu.data["ENERG_LO"]
 
-        mylog.info("Energy limits: %g %g" % (min(tblhdu.data["ENERG_LO"]), max(tblhdu.data["ENERG_HI"] )))
-        
+        mylog.info("Energy limits: %g %g" % (min(tblhdu.data["ENERG_LO"]),
+                                             max(tblhdu.data["ENERG_HI"] )))
+
+        if "ARF" in self.events:
+            f = pyfits.open(self.events["ARF"])
+            elo = f[1].data.field("ENERG_LO")
+            ehi = f[1].data.field("ENERG_HI")
+            f.close()
+            try:
+                assert_allclose(elo, tblhdu.data["ENERG_LO"])
+                assert_allclose(ehi, tblhdu.data["ENERG_HI"])
+            except AssertionError:
+                mylog.warning("Energy binning does not match for "+
+                              "ARF and RMF. This will make spectral"+
+                              "fitting difficult.")
+                                              
         tblhdu2 = hdulist[2]
         n_ch = len(tblhdu2.data["CHANNEL"])
         mylog.info("Number of Channels: %d" % (n_ch))
@@ -680,9 +741,11 @@
             # weight function for probabilities from RMF
             weights = tblhdu.data[k]["MATRIX"][:]
             weights /= weights.sum()
-            # build channel number list associated to array value, there are groups of channels in rmfs with nonzero probabilities
+            # build channel number list associated to array value,
+            # there are groups of channels in rmfs with nonzero probabilities
             trueChannel = []
-            for start,nchan in zip(tblhdu.data[k]["F_CHAN"],tblhdu.data[k]["N_CHAN"]):
+            for start,nchan in zip(tblhdu.data[k]["F_CHAN"],
+                                   tblhdu.data[k]["N_CHAN"]):
                 end = start + nchan
                 for j in range(start,end):
                     trueChannel.append(j)


https://bitbucket.org/yt_analysis/yt/commits/c26245ca3795/
Changeset:   c26245ca3795
Branch:      yt
User:        jzuhone
Date:        2013-09-21 23:37:50
Summary:     Merged yt_analysis/yt into yt
Affected #:  432 files

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 .hgchurn
--- a/.hgchurn
+++ b/.hgchurn
@@ -17,3 +17,4 @@
 tabel = tabel at slac.stanford.edu
 sername=kayleanelson = kaylea.nelson at yale.edu
 kayleanelson = kaylea.nelson at yale.edu
+jcforbes at ucsc.edu = jforbes at ucolick.org

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 .hgtags
--- a/.hgtags
+++ b/.hgtags
@@ -5156,3 +5156,4 @@
 0000000000000000000000000000000000000000 mpi-opaque
 f15825659f5af3ce64aaad30062aff3603cbfb66 hop callback
 0000000000000000000000000000000000000000 hop callback
+079e456c38a87676472a458210077e2be325dc85 last_gplv3

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 CITATION
--- /dev/null
+++ b/CITATION
@@ -0,0 +1,31 @@
+To cite yt in publications, please use:
+
+Turk, M. J., Smith, B. D., Oishi, J. S., et al. 2011, ApJS, 192, 9
+
+In the body of the text, please add a footnote to the yt webpage:
+
+http://yt-project.org/
+
+For LaTex and BibTex users:
+
+\bibitem[Turk et al.(2011)]{2011ApJS..192....9T} Turk, M.~J., Smith, B.~D.,
+Oishi, J.~S., et al.\ 2011, \apjs, 192, 9
+
+ at ARTICLE{2011ApJS..192....9T,
+   author = {{Turk}, M.~J. and {Smith}, B.~D. and {Oishi}, J.~S. and {Skory}, S. and
+{Skillman}, S.~W. and {Abel}, T. and {Norman}, M.~L.},
+    title = "{yt: A Multi-code Analysis Toolkit for Astrophysical Simulation Data}",
+  journal = {\apjs},
+archivePrefix = "arXiv",
+   eprint = {1011.3514},
+ primaryClass = "astro-ph.IM",
+ keywords = {cosmology: theory, methods: data analysis, methods: numerical},
+     year = 2011,
+    month = jan,
+   volume = 192,
+      eid = {9},
+    pages = {9},
+      doi = {10.1088/0067-0049/192/1/9},
+   adsurl = {http://adsabs.harvard.edu/abs/2011ApJS..192....9T},
+  adsnote = {Provided by the SAO/NASA Astrophysics Data System}
+}

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 COPYING.txt
--- /dev/null
+++ b/COPYING.txt
@@ -0,0 +1,81 @@
+===============================
+ The yt project licensing terms
+===============================
+
+yt is licensed under the terms of the Modified BSD License (also known as New
+or Revised BSD), as follows:
+
+Copyright (c) 2013-, yt Development Team
+Copyright (c) 2006-2013, Matthew Turk <matthewturk at gmail.com>
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+Redistributions of source code must retain the above copyright notice, this
+list of conditions and the following disclaimer.
+
+Redistributions in binary form must reproduce the above copyright notice, this
+list of conditions and the following disclaimer in the documentation and/or
+other materials provided with the distribution.
+
+Neither the name of the yt Development Team nor the names of its
+contributors may be used to endorse or promote products derived from this
+software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+About the yt Development Team
+-----------------------------
+
+Matthew Turk began yt in 2006 and remains the project lead.  Over time yt has
+grown to include contributions from a large number of individuals from many
+diverse institutions, scientific, and technical backgrounds.
+
+Until the fall of 2013, yt was licensed under the GPLv3.  However, with consent
+from all developers and on a public mailing list, yt has been relicensed under
+the BSD 3-clause under a shared copyright model.  For more information, see:
+http://lists.spacepope.org/pipermail/yt-dev-spacepope.org/2013-July/003239.html
+All versions of yt prior to this licensing change are available under the
+GPLv3; all subsequent versions are available under the BSD 3-clause license.
+
+The yt Development Team is the set of all contributors to the yt project.  This
+includes all of the yt subprojects.
+
+The core team that coordinates development on BitBucket can be found here:
+http://bitbucket.org/yt_analysis/ 
+
+
+Our Copyright Policy
+--------------------
+
+yt uses a shared copyright model. Each contributor maintains copyright
+over their contributions to yt. But, it is important to note that these
+contributions are typically only changes to the repositories. Thus, the yt
+source code, in its entirety is not the copyright of any single person or
+institution.  Instead, it is the collective copyright of the entire yt
+Development Team.  If individual contributors want to maintain a record of what
+changes/contributions they have specific copyright on, they should indicate
+their copyright in the commit message of the change, when they commit the
+change to one of the yt repositories.
+
+With this in mind, the following banner should be used in any source code file
+to indicate the copyright and license terms:
+
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 CREDITS
--- a/CREDITS
+++ b/CREDITS
@@ -1,51 +1,55 @@
-YT is a group effort.
+yt is a group effort.
 
-Contributors:                   Tom Abel (tabel at stanford.edu)
-				David Collins (dcollins at physics.ucsd.edu)
-				Brian Crosby (crosby.bd at gmail.com)
-				Andrew Cunningham (ajcunn at gmail.com)
-				Nathan Goldbaum (goldbaum at ucolick.org)
-				Markus Haider (markus.haider at uibk.ac.at)
-				Cameron Hummels (chummels at gmail.com)
-				Christian Karch (chiffre at posteo.de)
-				Ji-hoon Kim (me at jihoonkim.org)
-				Steffen Klemer (sklemer at phys.uni-goettingen.de)
-				Kacper Kowalik (xarthisius.kk at gmail.com)
-				Michael Kuhlen (mqk at astro.berkeley.edu)
-				Eve Lee (elee at cita.utoronto.ca)
-				Yuan Li (yuan at astro.columbia.edu)
-				Chris Malone (chris.m.malone at gmail.com)
-				Josh Maloney (joshua.moloney at colorado.edu)
-				Chris Moody (cemoody at ucsc.edu)
-				Andrew Myers (atmyers at astro.berkeley.edu)
-				Jeff Oishi (jsoishi at gmail.com)
-				Jean-Claude Passy (jcpassy at uvic.ca)
-				Mark Richardson (Mark.L.Richardson at asu.edu)
-				Thomas Robitaille (thomas.robitaille at gmail.com)
-				Anna Rosen (rosen at ucolick.org)
-				Anthony Scopatz (scopatz at gmail.com)
-				Devin Silvia (devin.silvia at colorado.edu)
-				Sam Skillman (samskillman at gmail.com)
-				Stephen Skory (s at skory.us)
-				Britton Smith (brittonsmith at gmail.com)
-				Geoffrey So (gsiisg at gmail.com)
-				Casey Stark (caseywstark at gmail.com)
-				Elizabeth Tasker (tasker at astro1.sci.hokudai.ac.jp)
-				Stephanie Tonnesen (stonnes at gmail.com)
-				Matthew Turk (matthewturk at gmail.com)
-				Rich Wagner (rwagner at physics.ucsd.edu)
-				John Wise (jwise at physics.gatech.edu)
-				John ZuHone (jzuhone at gmail.com)
+Contributors:   
+                Tom Abel (tabel at stanford.edu)
+                David Collins (dcollins at physics.ucsd.edu)
+                Brian Crosby (crosby.bd at gmail.com)
+                Andrew Cunningham (ajcunn at gmail.com)
+                Hilary Egan (hilaryye at gmail.com)
+                John Forces (jforbes at ucolick.org)
+                Nathan Goldbaum (goldbaum at ucolick.org)
+                Markus Haider (markus.haider at uibk.ac.at)
+                Cameron Hummels (chummels at gmail.com)
+                Christian Karch (chiffre at posteo.de)
+                Ji-hoon Kim (me at jihoonkim.org)
+                Steffen Klemer (sklemer at phys.uni-goettingen.de)
+                Kacper Kowalik (xarthisius.kk at gmail.com)
+                Michael Kuhlen (mqk at astro.berkeley.edu)
+                Eve Lee (elee at cita.utoronto.ca)
+                Sam Leitner (sam.leitner at gmail.com)
+                Yuan Li (yuan at astro.columbia.edu)
+                Chris Malone (chris.m.malone at gmail.com)
+                Josh Maloney (joshua.moloney at colorado.edu)
+                Chris Moody (cemoody at ucsc.edu)
+                Andrew Myers (atmyers at astro.berkeley.edu)
+                Jill Naiman (jnaiman at ucolick.org)
+                Kaylea Nelson (kaylea.nelson at yale.edu)
+                Jeff Oishi (jsoishi at gmail.com)
+                Jean-Claude Passy (jcpassy at uvic.ca)
+                Mark Richardson (Mark.L.Richardson at asu.edu)
+                Thomas Robitaille (thomas.robitaille at gmail.com)
+                Anna Rosen (rosen at ucolick.org)
+                Douglas Rudd (drudd at uchicago.edu)
+                Anthony Scopatz (scopatz at gmail.com)
+                Noel Scudder (noel.scudder at stonybrook.edu)
+                Devin Silvia (devin.silvia at colorado.edu)
+                Sam Skillman (samskillman at gmail.com)
+                Stephen Skory (s at skory.us)
+                Britton Smith (brittonsmith at gmail.com)
+                Geoffrey So (gsiisg at gmail.com)
+                Casey Stark (caseywstark at gmail.com)
+                Elizabeth Tasker (tasker at astro1.sci.hokudai.ac.jp)
+                Stephanie Tonnesen (stonnes at gmail.com)
+                Matthew Turk (matthewturk at gmail.com)
+                Rich Wagner (rwagner at physics.ucsd.edu)
+                Andrew Wetzel (andrew.wetzel at yale.edu)
+                John Wise (jwise at physics.gatech.edu)
+                John ZuHone (jzuhone at gmail.com)
 
-We also include the Delaunay Triangulation module written by Robert Kern of
-Enthought, the cmdln.py module by Trent Mick, and the progressbar module by
+Several items included in the yt/extern directory were written by other
+individuals and may bear their own license, including the progressbar module by
 Nilton Volpato.  The PasteBin interface code (as well as the PasteBin itself)
-was written by the Pocoo collective (pocoo.org).  The RamsesRead++ library was
-developed by Oliver Hahn.  yt also includes a slightly-modified version of
-libconfig (http://www.hyperrealm.com/libconfig/) and an unmodified version of
-several routines from HEALpix (http://healpix.jpl.nasa.gov/).
-
-Large parts of development of yt were guided by discussions with Tom Abel, Ralf
-Kaehler, Mike Norman and Greg Bryan.
+was written by the Pocoo collective (pocoo.org).  
+developed by Oliver Hahn.  
 
 Thanks to everyone for all your contributions!

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 FUNDING
--- a/FUNDING
+++ /dev/null
@@ -1,35 +0,0 @@
-The development of yt has benefited from funding from many different sources
-and institutions.  Here is an incomplete list of these sources:
-
-  * NSF grant OCI-1048505
-  * NSF grant AST-0239709 
-  * NSF grant AST-0707474
-  * NSF grant AST-0708960
-  * NSF grant AST-0808184
-  * NSF grant AST-0807215 
-  * NSF grant AST-0807312
-  * NSF grant AST-0807075
-  * NSF grant AST-0908199
-  * NSF grant AST-0908553 
-  * NASA grant ATFP NNX08-AH26G
-  * NASA grant ATFP NNX09-AD80G
-  * NASA grant ATFP NNZ07-AG77G
-  * DOE Computational Science Graduate Fellowship under grant number DE-FG02-97ER25308
-
-Additionally, development of yt has benefited from the hospitality and hosting
-of the following institutions:
-
-  * Columbia University
-  * Harvard-Smithsonian Center for Astrophysics
-  * Institute for Advanced Study
-  * Kavli Institute for Particle Astrophysics and Cosmology
-  * Kavli Institute for Theoretical Physics
-  * Los Alamos National Lab
-  * Michigan State University
-  * Princeton University
-  * Stanford University
-  * University of California High-Performance Astro-Computing Center
-  * University of California at Berkeley
-  * University of California at San Diego
-  * University of California at Santa Cruz
-  * University of Colorado at Boulder

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 LICENSE.txt
--- a/LICENSE.txt
+++ /dev/null
@@ -1,674 +0,0 @@
-                    GNU GENERAL PUBLIC LICENSE
-                       Version 3, 29 June 2007
-
- Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
- Everyone is permitted to copy and distribute verbatim copies
- of this license document, but changing it is not allowed.
-
-                            Preamble
-
-  The GNU General Public License is a free, copyleft license for
-software and other kinds of works.
-
-  The licenses for most software and other practical works are designed
-to take away your freedom to share and change the works.  By contrast,
-the GNU General Public License is intended to guarantee your freedom to
-share and change all versions of a program--to make sure it remains free
-software for all its users.  We, the Free Software Foundation, use the
-GNU General Public License for most of our software; it applies also to
-any other work released this way by its authors.  You can apply it to
-your programs, too.
-
-  When we speak of free software, we are referring to freedom, not
-price.  Our General Public Licenses are designed to make sure that you
-have the freedom to distribute copies of free software (and charge for
-them if you wish), that you receive source code or can get it if you
-want it, that you can change the software or use pieces of it in new
-free programs, and that you know you can do these things.
-
-  To protect your rights, we need to prevent others from denying you
-these rights or asking you to surrender the rights.  Therefore, you have
-certain responsibilities if you distribute copies of the software, or if
-you modify it: responsibilities to respect the freedom of others.
-
-  For example, if you distribute copies of such a program, whether
-gratis or for a fee, you must pass on to the recipients the same
-freedoms that you received.  You must make sure that they, too, receive
-or can get the source code.  And you must show them these terms so they
-know their rights.
-
-  Developers that use the GNU GPL protect your rights with two steps:
-(1) assert copyright on the software, and (2) offer you this License
-giving you legal permission to copy, distribute and/or modify it.
-
-  For the developers' and authors' protection, the GPL clearly explains
-that there is no warranty for this free software.  For both users' and
-authors' sake, the GPL requires that modified versions be marked as
-changed, so that their problems will not be attributed erroneously to
-authors of previous versions.
-
-  Some devices are designed to deny users access to install or run
-modified versions of the software inside them, although the manufacturer
-can do so.  This is fundamentally incompatible with the aim of
-protecting users' freedom to change the software.  The systematic
-pattern of such abuse occurs in the area of products for individuals to
-use, which is precisely where it is most unacceptable.  Therefore, we
-have designed this version of the GPL to prohibit the practice for those
-products.  If such problems arise substantially in other domains, we
-stand ready to extend this provision to those domains in future versions
-of the GPL, as needed to protect the freedom of users.
-
-  Finally, every program is threatened constantly by software patents.
-States should not allow patents to restrict development and use of
-software on general-purpose computers, but in those that do, we wish to
-avoid the special danger that patents applied to a free program could
-make it effectively proprietary.  To prevent this, the GPL assures that
-patents cannot be used to render the program non-free.
-
-  The precise terms and conditions for copying, distribution and
-modification follow.
-
-                       TERMS AND CONDITIONS
-
-  0. Definitions.
-
-  "This License" refers to version 3 of the GNU General Public License.
-
-  "Copyright" also means copyright-like laws that apply to other kinds of
-works, such as semiconductor masks.
-
-  "The Program" refers to any copyrightable work licensed under this
-License.  Each licensee is addressed as "you".  "Licensees" and
-"recipients" may be individuals or organizations.
-
-  To "modify" a work means to copy from or adapt all or part of the work
-in a fashion requiring copyright permission, other than the making of an
-exact copy.  The resulting work is called a "modified version" of the
-earlier work or a work "based on" the earlier work.
-
-  A "covered work" means either the unmodified Program or a work based
-on the Program.
-
-  To "propagate" a work means to do anything with it that, without
-permission, would make you directly or secondarily liable for
-infringement under applicable copyright law, except executing it on a
-computer or modifying a private copy.  Propagation includes copying,
-distribution (with or without modification), making available to the
-public, and in some countries other activities as well.
-
-  To "convey" a work means any kind of propagation that enables other
-parties to make or receive copies.  Mere interaction with a user through
-a computer network, with no transfer of a copy, is not conveying.
-
-  An interactive user interface displays "Appropriate Legal Notices"
-to the extent that it includes a convenient and prominently visible
-feature that (1) displays an appropriate copyright notice, and (2)
-tells the user that there is no warranty for the work (except to the
-extent that warranties are provided), that licensees may convey the
-work under this License, and how to view a copy of this License.  If
-the interface presents a list of user commands or options, such as a
-menu, a prominent item in the list meets this criterion.
-
-  1. Source Code.
-
-  The "source code" for a work means the preferred form of the work
-for making modifications to it.  "Object code" means any non-source
-form of a work.
-
-  A "Standard Interface" means an interface that either is an official
-standard defined by a recognized standards body, or, in the case of
-interfaces specified for a particular programming language, one that
-is widely used among developers working in that language.
-
-  The "System Libraries" of an executable work include anything, other
-than the work as a whole, that (a) is included in the normal form of
-packaging a Major Component, but which is not part of that Major
-Component, and (b) serves only to enable use of the work with that
-Major Component, or to implement a Standard Interface for which an
-implementation is available to the public in source code form.  A
-"Major Component", in this context, means a major essential component
-(kernel, window system, and so on) of the specific operating system
-(if any) on which the executable work runs, or a compiler used to
-produce the work, or an object code interpreter used to run it.
-
-  The "Corresponding Source" for a work in object code form means all
-the source code needed to generate, install, and (for an executable
-work) run the object code and to modify the work, including scripts to
-control those activities.  However, it does not include the work's
-System Libraries, or general-purpose tools or generally available free
-programs which are used unmodified in performing those activities but
-which are not part of the work.  For example, Corresponding Source
-includes interface definition files associated with source files for
-the work, and the source code for shared libraries and dynamically
-linked subprograms that the work is specifically designed to require,
-such as by intimate data communication or control flow between those
-subprograms and other parts of the work.
-
-  The Corresponding Source need not include anything that users
-can regenerate automatically from other parts of the Corresponding
-Source.
-
-  The Corresponding Source for a work in source code form is that
-same work.
-
-  2. Basic Permissions.
-
-  All rights granted under this License are granted for the term of
-copyright on the Program, and are irrevocable provided the stated
-conditions are met.  This License explicitly affirms your unlimited
-permission to run the unmodified Program.  The output from running a
-covered work is covered by this License only if the output, given its
-content, constitutes a covered work.  This License acknowledges your
-rights of fair use or other equivalent, as provided by copyright law.
-
-  You may make, run and propagate covered works that you do not
-convey, without conditions so long as your license otherwise remains
-in force.  You may convey covered works to others for the sole purpose
-of having them make modifications exclusively for you, or provide you
-with facilities for running those works, provided that you comply with
-the terms of this License in conveying all material for which you do
-not control copyright.  Those thus making or running the covered works
-for you must do so exclusively on your behalf, under your direction
-and control, on terms that prohibit them from making any copies of
-your copyrighted material outside their relationship with you.
-
-  Conveying under any other circumstances is permitted solely under
-the conditions stated below.  Sublicensing is not allowed; section 10
-makes it unnecessary.
-
-  3. Protecting Users' Legal Rights From Anti-Circumvention Law.
-
-  No covered work shall be deemed part of an effective technological
-measure under any applicable law fulfilling obligations under article
-11 of the WIPO copyright treaty adopted on 20 December 1996, or
-similar laws prohibiting or restricting circumvention of such
-measures.
-
-  When you convey a covered work, you waive any legal power to forbid
-circumvention of technological measures to the extent such circumvention
-is effected by exercising rights under this License with respect to
-the covered work, and you disclaim any intention to limit operation or
-modification of the work as a means of enforcing, against the work's
-users, your or third parties' legal rights to forbid circumvention of
-technological measures.
-
-  4. Conveying Verbatim Copies.
-
-  You may convey verbatim copies of the Program's source code as you
-receive it, in any medium, provided that you conspicuously and
-appropriately publish on each copy an appropriate copyright notice;
-keep intact all notices stating that this License and any
-non-permissive terms added in accord with section 7 apply to the code;
-keep intact all notices of the absence of any warranty; and give all
-recipients a copy of this License along with the Program.
-
-  You may charge any price or no price for each copy that you convey,
-and you may offer support or warranty protection for a fee.
-
-  5. Conveying Modified Source Versions.
-
-  You may convey a work based on the Program, or the modifications to
-produce it from the Program, in the form of source code under the
-terms of section 4, provided that you also meet all of these conditions:
-
-    a) The work must carry prominent notices stating that you modified
-    it, and giving a relevant date.
-
-    b) The work must carry prominent notices stating that it is
-    released under this License and any conditions added under section
-    7.  This requirement modifies the requirement in section 4 to
-    "keep intact all notices".
-
-    c) You must license the entire work, as a whole, under this
-    License to anyone who comes into possession of a copy.  This
-    License will therefore apply, along with any applicable section 7
-    additional terms, to the whole of the work, and all its parts,
-    regardless of how they are packaged.  This License gives no
-    permission to license the work in any other way, but it does not
-    invalidate such permission if you have separately received it.
-
-    d) If the work has interactive user interfaces, each must display
-    Appropriate Legal Notices; however, if the Program has interactive
-    interfaces that do not display Appropriate Legal Notices, your
-    work need not make them do so.
-
-  A compilation of a covered work with other separate and independent
-works, which are not by their nature extensions of the covered work,
-and which are not combined with it such as to form a larger program,
-in or on a volume of a storage or distribution medium, is called an
-"aggregate" if the compilation and its resulting copyright are not
-used to limit the access or legal rights of the compilation's users
-beyond what the individual works permit.  Inclusion of a covered work
-in an aggregate does not cause this License to apply to the other
-parts of the aggregate.
-
-  6. Conveying Non-Source Forms.
-
-  You may convey a covered work in object code form under the terms
-of sections 4 and 5, provided that you also convey the
-machine-readable Corresponding Source under the terms of this License,
-in one of these ways:
-
-    a) Convey the object code in, or embodied in, a physical product
-    (including a physical distribution medium), accompanied by the
-    Corresponding Source fixed on a durable physical medium
-    customarily used for software interchange.
-
-    b) Convey the object code in, or embodied in, a physical product
-    (including a physical distribution medium), accompanied by a
-    written offer, valid for at least three years and valid for as
-    long as you offer spare parts or customer support for that product
-    model, to give anyone who possesses the object code either (1) a
-    copy of the Corresponding Source for all the software in the
-    product that is covered by this License, on a durable physical
-    medium customarily used for software interchange, for a price no
-    more than your reasonable cost of physically performing this
-    conveying of source, or (2) access to copy the
-    Corresponding Source from a network server at no charge.
-
-    c) Convey individual copies of the object code with a copy of the
-    written offer to provide the Corresponding Source.  This
-    alternative is allowed only occasionally and noncommercially, and
-    only if you received the object code with such an offer, in accord
-    with subsection 6b.
-
-    d) Convey the object code by offering access from a designated
-    place (gratis or for a charge), and offer equivalent access to the
-    Corresponding Source in the same way through the same place at no
-    further charge.  You need not require recipients to copy the
-    Corresponding Source along with the object code.  If the place to
-    copy the object code is a network server, the Corresponding Source
-    may be on a different server (operated by you or a third party)
-    that supports equivalent copying facilities, provided you maintain
-    clear directions next to the object code saying where to find the
-    Corresponding Source.  Regardless of what server hosts the
-    Corresponding Source, you remain obligated to ensure that it is
-    available for as long as needed to satisfy these requirements.
-
-    e) Convey the object code using peer-to-peer transmission, provided
-    you inform other peers where the object code and Corresponding
-    Source of the work are being offered to the general public at no
-    charge under subsection 6d.
-
-  A separable portion of the object code, whose source code is excluded
-from the Corresponding Source as a System Library, need not be
-included in conveying the object code work.
-
-  A "User Product" is either (1) a "consumer product", which means any
-tangible personal property which is normally used for personal, family,
-or household purposes, or (2) anything designed or sold for incorporation
-into a dwelling.  In determining whether a product is a consumer product,
-doubtful cases shall be resolved in favor of coverage.  For a particular
-product received by a particular user, "normally used" refers to a
-typical or common use of that class of product, regardless of the status
-of the particular user or of the way in which the particular user
-actually uses, or expects or is expected to use, the product.  A product
-is a consumer product regardless of whether the product has substantial
-commercial, industrial or non-consumer uses, unless such uses represent
-the only significant mode of use of the product.
-
-  "Installation Information" for a User Product means any methods,
-procedures, authorization keys, or other information required to install
-and execute modified versions of a covered work in that User Product from
-a modified version of its Corresponding Source.  The information must
-suffice to ensure that the continued functioning of the modified object
-code is in no case prevented or interfered with solely because
-modification has been made.
-
-  If you convey an object code work under this section in, or with, or
-specifically for use in, a User Product, and the conveying occurs as
-part of a transaction in which the right of possession and use of the
-User Product is transferred to the recipient in perpetuity or for a
-fixed term (regardless of how the transaction is characterized), the
-Corresponding Source conveyed under this section must be accompanied
-by the Installation Information.  But this requirement does not apply
-if neither you nor any third party retains the ability to install
-modified object code on the User Product (for example, the work has
-been installed in ROM).
-
-  The requirement to provide Installation Information does not include a
-requirement to continue to provide support service, warranty, or updates
-for a work that has been modified or installed by the recipient, or for
-the User Product in which it has been modified or installed.  Access to a
-network may be denied when the modification itself materially and
-adversely affects the operation of the network or violates the rules and
-protocols for communication across the network.
-
-  Corresponding Source conveyed, and Installation Information provided,
-in accord with this section must be in a format that is publicly
-documented (and with an implementation available to the public in
-source code form), and must require no special password or key for
-unpacking, reading or copying.
-
-  7. Additional Terms.
-
-  "Additional permissions" are terms that supplement the terms of this
-License by making exceptions from one or more of its conditions.
-Additional permissions that are applicable to the entire Program shall
-be treated as though they were included in this License, to the extent
-that they are valid under applicable law.  If additional permissions
-apply only to part of the Program, that part may be used separately
-under those permissions, but the entire Program remains governed by
-this License without regard to the additional permissions.
-
-  When you convey a copy of a covered work, you may at your option
-remove any additional permissions from that copy, or from any part of
-it.  (Additional permissions may be written to require their own
-removal in certain cases when you modify the work.)  You may place
-additional permissions on material, added by you to a covered work,
-for which you have or can give appropriate copyright permission.
-
-  Notwithstanding any other provision of this License, for material you
-add to a covered work, you may (if authorized by the copyright holders of
-that material) supplement the terms of this License with terms:
-
-    a) Disclaiming warranty or limiting liability differently from the
-    terms of sections 15 and 16 of this License; or
-
-    b) Requiring preservation of specified reasonable legal notices or
-    author attributions in that material or in the Appropriate Legal
-    Notices displayed by works containing it; or
-
-    c) Prohibiting misrepresentation of the origin of that material, or
-    requiring that modified versions of such material be marked in
-    reasonable ways as different from the original version; or
-
-    d) Limiting the use for publicity purposes of names of licensors or
-    authors of the material; or
-
-    e) Declining to grant rights under trademark law for use of some
-    trade names, trademarks, or service marks; or
-
-    f) Requiring indemnification of licensors and authors of that
-    material by anyone who conveys the material (or modified versions of
-    it) with contractual assumptions of liability to the recipient, for
-    any liability that these contractual assumptions directly impose on
-    those licensors and authors.
-
-  All other non-permissive additional terms are considered "further
-restrictions" within the meaning of section 10.  If the Program as you
-received it, or any part of it, contains a notice stating that it is
-governed by this License along with a term that is a further
-restriction, you may remove that term.  If a license document contains
-a further restriction but permits relicensing or conveying under this
-License, you may add to a covered work material governed by the terms
-of that license document, provided that the further restriction does
-not survive such relicensing or conveying.
-
-  If you add terms to a covered work in accord with this section, you
-must place, in the relevant source files, a statement of the
-additional terms that apply to those files, or a notice indicating
-where to find the applicable terms.
-
-  Additional terms, permissive or non-permissive, may be stated in the
-form of a separately written license, or stated as exceptions;
-the above requirements apply either way.
-
-  8. Termination.
-
-  You may not propagate or modify a covered work except as expressly
-provided under this License.  Any attempt otherwise to propagate or
-modify it is void, and will automatically terminate your rights under
-this License (including any patent licenses granted under the third
-paragraph of section 11).
-
-  However, if you cease all violation of this License, then your
-license from a particular copyright holder is reinstated (a)
-provisionally, unless and until the copyright holder explicitly and
-finally terminates your license, and (b) permanently, if the copyright
-holder fails to notify you of the violation by some reasonable means
-prior to 60 days after the cessation.
-
-  Moreover, your license from a particular copyright holder is
-reinstated permanently if the copyright holder notifies you of the
-violation by some reasonable means, this is the first time you have
-received notice of violation of this License (for any work) from that
-copyright holder, and you cure the violation prior to 30 days after
-your receipt of the notice.
-
-  Termination of your rights under this section does not terminate the
-licenses of parties who have received copies or rights from you under
-this License.  If your rights have been terminated and not permanently
-reinstated, you do not qualify to receive new licenses for the same
-material under section 10.
-
-  9. Acceptance Not Required for Having Copies.
-
-  You are not required to accept this License in order to receive or
-run a copy of the Program.  Ancillary propagation of a covered work
-occurring solely as a consequence of using peer-to-peer transmission
-to receive a copy likewise does not require acceptance.  However,
-nothing other than this License grants you permission to propagate or
-modify any covered work.  These actions infringe copyright if you do
-not accept this License.  Therefore, by modifying or propagating a
-covered work, you indicate your acceptance of this License to do so.
-
-  10. Automatic Licensing of Downstream Recipients.
-
-  Each time you convey a covered work, the recipient automatically
-receives a license from the original licensors, to run, modify and
-propagate that work, subject to this License.  You are not responsible
-for enforcing compliance by third parties with this License.
-
-  An "entity transaction" is a transaction transferring control of an
-organization, or substantially all assets of one, or subdividing an
-organization, or merging organizations.  If propagation of a covered
-work results from an entity transaction, each party to that
-transaction who receives a copy of the work also receives whatever
-licenses to the work the party's predecessor in interest had or could
-give under the previous paragraph, plus a right to possession of the
-Corresponding Source of the work from the predecessor in interest, if
-the predecessor has it or can get it with reasonable efforts.
-
-  You may not impose any further restrictions on the exercise of the
-rights granted or affirmed under this License.  For example, you may
-not impose a license fee, royalty, or other charge for exercise of
-rights granted under this License, and you may not initiate litigation
-(including a cross-claim or counterclaim in a lawsuit) alleging that
-any patent claim is infringed by making, using, selling, offering for
-sale, or importing the Program or any portion of it.
-
-  11. Patents.
-
-  A "contributor" is a copyright holder who authorizes use under this
-License of the Program or a work on which the Program is based.  The
-work thus licensed is called the contributor's "contributor version".
-
-  A contributor's "essential patent claims" are all patent claims
-owned or controlled by the contributor, whether already acquired or
-hereafter acquired, that would be infringed by some manner, permitted
-by this License, of making, using, or selling its contributor version,
-but do not include claims that would be infringed only as a
-consequence of further modification of the contributor version.  For
-purposes of this definition, "control" includes the right to grant
-patent sublicenses in a manner consistent with the requirements of
-this License.
-
-  Each contributor grants you a non-exclusive, worldwide, royalty-free
-patent license under the contributor's essential patent claims, to
-make, use, sell, offer for sale, import and otherwise run, modify and
-propagate the contents of its contributor version.
-
-  In the following three paragraphs, a "patent license" is any express
-agreement or commitment, however denominated, not to enforce a patent
-(such as an express permission to practice a patent or covenant not to
-sue for patent infringement).  To "grant" such a patent license to a
-party means to make such an agreement or commitment not to enforce a
-patent against the party.
-
-  If you convey a covered work, knowingly relying on a patent license,
-and the Corresponding Source of the work is not available for anyone
-to copy, free of charge and under the terms of this License, through a
-publicly available network server or other readily accessible means,
-then you must either (1) cause the Corresponding Source to be so
-available, or (2) arrange to deprive yourself of the benefit of the
-patent license for this particular work, or (3) arrange, in a manner
-consistent with the requirements of this License, to extend the patent
-license to downstream recipients.  "Knowingly relying" means you have
-actual knowledge that, but for the patent license, your conveying the
-covered work in a country, or your recipient's use of the covered work
-in a country, would infringe one or more identifiable patents in that
-country that you have reason to believe are valid.
-
-  If, pursuant to or in connection with a single transaction or
-arrangement, you convey, or propagate by procuring conveyance of, a
-covered work, and grant a patent license to some of the parties
-receiving the covered work authorizing them to use, propagate, modify
-or convey a specific copy of the covered work, then the patent license
-you grant is automatically extended to all recipients of the covered
-work and works based on it.
-
-  A patent license is "discriminatory" if it does not include within
-the scope of its coverage, prohibits the exercise of, or is
-conditioned on the non-exercise of one or more of the rights that are
-specifically granted under this License.  You may not convey a covered
-work if you are a party to an arrangement with a third party that is
-in the business of distributing software, under which you make payment
-to the third party based on the extent of your activity of conveying
-the work, and under which the third party grants, to any of the
-parties who would receive the covered work from you, a discriminatory
-patent license (a) in connection with copies of the covered work
-conveyed by you (or copies made from those copies), or (b) primarily
-for and in connection with specific products or compilations that
-contain the covered work, unless you entered into that arrangement,
-or that patent license was granted, prior to 28 March 2007.
-
-  Nothing in this License shall be construed as excluding or limiting
-any implied license or other defenses to infringement that may
-otherwise be available to you under applicable patent law.
-
-  12. No Surrender of Others' Freedom.
-
-  If conditions are imposed on you (whether by court order, agreement or
-otherwise) that contradict the conditions of this License, they do not
-excuse you from the conditions of this License.  If you cannot convey a
-covered work so as to satisfy simultaneously your obligations under this
-License and any other pertinent obligations, then as a consequence you may
-not convey it at all.  For example, if you agree to terms that obligate you
-to collect a royalty for further conveying from those to whom you convey
-the Program, the only way you could satisfy both those terms and this
-License would be to refrain entirely from conveying the Program.
-
-  13. Use with the GNU Affero General Public License.
-
-  Notwithstanding any other provision of this License, you have
-permission to link or combine any covered work with a work licensed
-under version 3 of the GNU Affero General Public License into a single
-combined work, and to convey the resulting work.  The terms of this
-License will continue to apply to the part which is the covered work,
-but the special requirements of the GNU Affero General Public License,
-section 13, concerning interaction through a network will apply to the
-combination as such.
-
-  14. Revised Versions of this License.
-
-  The Free Software Foundation may publish revised and/or new versions of
-the GNU General Public License from time to time.  Such new versions will
-be similar in spirit to the present version, but may differ in detail to
-address new problems or concerns.
-
-  Each version is given a distinguishing version number.  If the
-Program specifies that a certain numbered version of the GNU General
-Public License "or any later version" applies to it, you have the
-option of following the terms and conditions either of that numbered
-version or of any later version published by the Free Software
-Foundation.  If the Program does not specify a version number of the
-GNU General Public License, you may choose any version ever published
-by the Free Software Foundation.
-
-  If the Program specifies that a proxy can decide which future
-versions of the GNU General Public License can be used, that proxy's
-public statement of acceptance of a version permanently authorizes you
-to choose that version for the Program.
-
-  Later license versions may give you additional or different
-permissions.  However, no additional obligations are imposed on any
-author or copyright holder as a result of your choosing to follow a
-later version.
-
-  15. Disclaimer of Warranty.
-
-  THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
-APPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
-HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
-OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
-THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
-PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
-IS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
-ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
-
-  16. Limitation of Liability.
-
-  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
-WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
-THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
-GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
-USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
-DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
-PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
-EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGES.
-
-  17. Interpretation of Sections 15 and 16.
-
-  If the disclaimer of warranty and limitation of liability provided
-above cannot be given local legal effect according to their terms,
-reviewing courts shall apply local law that most closely approximates
-an absolute waiver of all civil liability in connection with the
-Program, unless a warranty or assumption of liability accompanies a
-copy of the Program in return for a fee.
-
-                     END OF TERMS AND CONDITIONS
-
-            How to Apply These Terms to Your New Programs
-
-  If you develop a new program, and you want it to be of the greatest
-possible use to the public, the best way to achieve this is to make it
-free software which everyone can redistribute and change under these terms.
-
-  To do so, attach the following notices to the program.  It is safest
-to attach them to the start of each source file to most effectively
-state the exclusion of warranty; and each file should have at least
-the "copyright" line and a pointer to where the full notice is found.
-
-    <one line to give the program's name and a brief idea of what it does.>
-    Copyright (C) <year><name of author>
-
-    This program is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-Also add information on how to contact you by electronic and paper mail.
-
-  If the program does terminal interaction, make it output a short
-notice like this when it starts in an interactive mode:
-
-    <program>  Copyright (C) <year><name of author>
-    This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
-    This is free software, and you are welcome to redistribute it
-    under certain conditions; type `show c' for details.
-
-The hypothetical commands `show w' and `show c' should show the appropriate
-parts of the General Public License.  Of course, your program's commands
-might be different; for a GUI interface, you would use an "about box".
-
-  You should also get your employer (if you work as a programmer) or school,
-if any, to sign a "copyright disclaimer" for the program, if necessary.
-For more information on this, and how to apply and follow the GNU GPL, see
-<http://www.gnu.org/licenses/>.
-
-  The GNU General Public License does not permit incorporating your program
-into proprietary programs.  If your program is a subroutine library, you
-may consider it more useful to permit linking proprietary applications with
-the library.  If this is what you want to do, use the GNU Lesser General
-Public License instead of this License.  But first, please read
-<http://www.gnu.org/philosophy/why-not-lgpl.html>.

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 doc/how_to_develop_yt.txt
--- a/doc/how_to_develop_yt.txt
+++ b/doc/how_to_develop_yt.txt
@@ -25,7 +25,7 @@
 Licenses
 --------
 
-All code in yt should be under the GPL-3 (preferred) or a compatible license.
+All code in yt should be under the BSD 3-clause license.
 
 How To Get The Source Code
 --------------------------

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 doc/install_script.sh
--- a/doc/install_script.sh
+++ b/doc/install_script.sh
@@ -419,7 +419,7 @@
 echo "be installing ZeroMQ"
 
 printf "%-15s = %s so I " "INST_ROCKSTAR" "${INST_ROCKSTAR}"
-get_willwont ${INST_0MQ}
+get_willwont ${INST_ROCKSTAR}
 echo "be installing Rockstar"
 
 echo
@@ -877,6 +877,11 @@
 mkdir -p ${DEST_DIR}/src/$MATPLOTLIB
 echo "[directories]" >> ${DEST_DIR}/src/$MATPLOTLIB/setup.cfg
 echo "basedirlist = ${DEST_DIR}" >> ${DEST_DIR}/src/$MATPLOTLIB/setup.cfg
+if [ `uname` = "Darwin" ]
+then
+   echo "[gui_support]" >> ${DEST_DIR}/src/$MATPLOTLIB/setup.cfg
+   echo "macosx = False" >> ${DEST_DIR}/src/$MATPLOTLIB/setup.cfg
+fi
 do_setup_py $MATPLOTLIB
 if [ -n "${OLD_LDFLAGS}" ]
 then

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 setup.py
--- a/setup.py
+++ b/setup.py
@@ -248,7 +248,7 @@
         classifiers=["Development Status :: 5 - Production/Stable",
                      "Environment :: Console",
                      "Intended Audience :: Science/Research",
-                     "License :: OSI Approved :: GNU General Public License (GPL)",
+                     "License :: OSI Approved :: BSD License",
                      "Operating System :: MacOS :: MacOS X",
                      "Operating System :: POSIX :: AIX",
                      "Operating System :: POSIX :: Linux",
@@ -269,7 +269,7 @@
         author="Matthew J. Turk",
         author_email="matthewturk at gmail.com",
         url="http://yt-project.org/",
-        license="GPL-3",
+        license="BSD",
         configuration=configuration,
         zip_safe=False,
         data_files=REASON_FILES,

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 yt/__init__.py
--- a/yt/__init__.py
+++ b/yt/__init__.py
@@ -60,27 +60,17 @@
 All broadly useful code that doesn't clearly fit in one of the other
 categories goes here.
 
-Author: Matthew Turk <matthewturk at gmail.com>
-Affiliation: KIPAC/SLAC/Stanford
-Homepage: http://yt-project.org/
-License:
-  Copyright (C) 2007-2011 Matthew Turk.  All Rights Reserved.
 
-  This file is part of yt.
 
-  yt is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 3 of the License, or
-  (at your option) any later version.
+"""
 
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-"""
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
 
 __version__ = "2.5-dev"
 

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 yt/analysis_modules/absorption_spectrum/__init__.py
--- a/yt/analysis_modules/absorption_spectrum/__init__.py
+++ b/yt/analysis_modules/absorption_spectrum/__init__.py
@@ -1,24 +1,14 @@
 """
 Import stuff for light cone generator.
 
-Author: Britton Smith <brittons at origins.colorado.edu>
-Affiliation: CASA/University of CO, Boulder
-Homepage: http://yt-project.org/
-License:
-  Copyright (C) 2008-2011 Britton Smith.  All Rights Reserved.
 
-  This file is part of yt.
 
-  yt is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 3 of the License, or
-  (at your option) any later version.
+"""
 
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-"""
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 yt/analysis_modules/absorption_spectrum/absorption_line.py
--- a/yt/analysis_modules/absorption_spectrum/absorption_line.py
+++ b/yt/analysis_modules/absorption_spectrum/absorption_line.py
@@ -1,27 +1,17 @@
 """
 Absorption line generating functions.
 
-Author: Britton Smith <brittonsmith at gmail.com>
-Affiliation: Michigan State University
-Homepage: http://yt-project.org/
-License:
-  Copyright (C) 2008-2011 Britton Smith.  All Rights Reserved.
 
-  This file is part of yt.
 
-  yt is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 3 of the License, or
-  (at your option) any later version.
+"""
 
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-"""
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
 
 import numpy as np
 

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 yt/analysis_modules/absorption_spectrum/absorption_spectrum.py
--- a/yt/analysis_modules/absorption_spectrum/absorption_spectrum.py
+++ b/yt/analysis_modules/absorption_spectrum/absorption_spectrum.py
@@ -1,27 +1,17 @@
 """
 AbsorptionSpectrum class and member functions.
 
-Author: Britton Smith <brittonsmith at gmail.com>
-Affiliation: Michigan State University
-Homepage: http://yt-project.org/
-License:
-  Copyright (C) 2008-2011 Britton Smith.  All Rights Reserved.
 
-  This file is part of yt.
 
-  yt is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 3 of the License, or
-  (at your option) any later version.
+"""
 
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-"""
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
 
 import h5py
 import numpy as np

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 yt/analysis_modules/absorption_spectrum/api.py
--- a/yt/analysis_modules/absorption_spectrum/api.py
+++ b/yt/analysis_modules/absorption_spectrum/api.py
@@ -1,33 +1,18 @@
 """
 API for absorption_spectrum
 
-Author: Matthew Turk <matthewturk at gmail.com>
-Affiliation: UCSD
-Author: J.S. Oishi <jsoishi at gmail.com>
-Affiliation: KIPAC/SLAC/Stanford
-Author: Britton Smith <brittonsmith at gmail.com>
-Affiliation: MSU
-Homepage: http://yt-project.org/
-License:
-  Copyright (C) 2010-2011 Matthew Turk.  All Rights Reserved.
 
-  This file is part of yt.
-
-  yt is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 3 of the License, or
-  (at your option) any later version.
-
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 """
 
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
 from .absorption_spectrum import \
     AbsorptionSpectrum
 

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 yt/analysis_modules/api.py
--- a/yt/analysis_modules/api.py
+++ b/yt/analysis_modules/api.py
@@ -1,33 +1,18 @@
 """
 API for yt.analysis_modules
 
-Author: Matthew Turk <matthewturk at gmail.com>
-Affiliation: UCSD
-Author: J.S. Oishi <jsoishi at gmail.com>
-Affiliation: KIPAC/SLAC/Stanford
-Author: Britton Smith <brittonsmith at gmail.com>
-Affiliation: MSU
-Homepage: http://yt-project.org/
-License:
-  Copyright (C) 2010-2011 Matthew Turk.  All Rights Reserved.
 
-  This file is part of yt.
-
-  yt is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 3 of the License, or
-  (at your option) any later version.
-
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 """
 
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
 from .absorption_spectrum.api import \
     AbsorptionSpectrum
 

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 yt/analysis_modules/coordinate_transformation/api.py
--- a/yt/analysis_modules/coordinate_transformation/api.py
+++ b/yt/analysis_modules/coordinate_transformation/api.py
@@ -1,32 +1,17 @@
 """
 API for coordinate_transformation
 
-Author: Matthew Turk <matthewturk at gmail.com>
-Affiliation: UCSD
-Author: J.S. Oishi <jsoishi at gmail.com>
-Affiliation: KIPAC/SLAC/Stanford
-Author: Britton Smith <brittonsmith at gmail.com>
-Affiliation: MSU
-Homepage: http://yt-project.org/
-License:
-  Copyright (C) 2010-2011 Matthew Turk.  All Rights Reserved.
 
-  This file is part of yt.
-
-  yt is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 3 of the License, or
-  (at your option) any later version.
-
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 """
 
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
 from .transforms import \
     spherical_regrid

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 yt/analysis_modules/coordinate_transformation/transforms.py
--- a/yt/analysis_modules/coordinate_transformation/transforms.py
+++ b/yt/analysis_modules/coordinate_transformation/transforms.py
@@ -1,29 +1,17 @@
 """
 Transformations between coordinate systems
 
-Author: Matthew Turk <matthewturk at gmail.com>
-Affiliation: KIPAC/SLAC/Stanford
-Author: JS Oishi <jsoishi at astro.berkeley.edu>
-Organization: UC Berkeley
-Homepage: http://yt-project.org/
-License:
-  Copyright (C) 2007-2011 Matthew Turk, J. S. Oishi.  All Rights Reserved.
 
-  This file is part of yt.
 
-  yt is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 3 of the License, or
-  (at your option) any later version.
+"""
 
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-"""
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
 
 import numpy as np
 from yt.funcs import *
@@ -65,7 +53,7 @@
     new_grid['handled'] = np.zeros(new_grid['x'].shape, dtype='bool')
     for field in fields:
         new_grid[field] = np.zeros(new_grid['x'].shape, dtype='float64')
-    grid_order = np.argsort(data_source.gridLevels)
+    grid_order = np.argsort(data_source.grid_levels[:,0])
     ng = len(data_source._grids)
 
     for i,grid in enumerate(data_source._grids[grid_order][::-1]):

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 yt/analysis_modules/cosmological_observation/api.py
--- a/yt/analysis_modules/cosmological_observation/api.py
+++ b/yt/analysis_modules/cosmological_observation/api.py
@@ -1,29 +1,18 @@
 """
 API for cosmology analysis.
 
-Author: Britton Smith <brittonsmith at gmail.com>
-Affiliation: Michigan State University
-Homepage: http://yt-project.org/
-License:
-  Copyright (C) 2010-2011 Matthew Turk.  All Rights Reserved.
 
-  This file is part of yt.
-
-  yt is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 3 of the License, or
-  (at your option) any later version.
-
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 """
 
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
 from .cosmology_splice import \
     CosmologySplice
 

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 yt/analysis_modules/cosmological_observation/cosmology_splice.py
--- a/yt/analysis_modules/cosmological_observation/cosmology_splice.py
+++ b/yt/analysis_modules/cosmological_observation/cosmology_splice.py
@@ -1,27 +1,17 @@
 """
 CosmologyTimeSeries class and member functions.
 
-Author: Britton Smith <brittonsmith at gmail.com>
-Affiliation: Michigan State University
-Homepage: http://yt-project.org/
-License:
-  Copyright (C) 2008-2012 Britton Smith.  All Rights Reserved.
 
-  This file is part of yt.
 
-  yt is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 3 of the License, or
-  (at your option) any later version.
+"""
 
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-"""
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
 
 import numpy as np
 

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 yt/analysis_modules/cosmological_observation/light_cone/__init__.py
--- a/yt/analysis_modules/cosmological_observation/light_cone/__init__.py
+++ b/yt/analysis_modules/cosmological_observation/light_cone/__init__.py
@@ -1,24 +1,14 @@
 """
 Import stuff for light cone generator.
 
-Author: Britton Smith <brittons at origins.colorado.edu>
-Affiliation: CASA/University of CO, Boulder
-Homepage: http://yt-project.org/
-License:
-  Copyright (C) 2008-2011 Britton Smith.  All Rights Reserved.
 
-  This file is part of yt.
 
-  yt is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 3 of the License, or
-  (at your option) any later version.
+"""
 
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-"""
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 yt/analysis_modules/cosmological_observation/light_cone/api.py
--- a/yt/analysis_modules/cosmological_observation/light_cone/api.py
+++ b/yt/analysis_modules/cosmological_observation/light_cone/api.py
@@ -1,33 +1,18 @@
 """
 API for lightcone
 
-Author: Matthew Turk <matthewturk at gmail.com>
-Affiliation: UCSD
-Author: J.S. Oishi <jsoishi at gmail.com>
-Affiliation: KIPAC/SLAC/Stanford
-Author: Britton Smith <brittonsmith at gmail.com>
-Affiliation: MSU
-Homepage: http://yt-project.org/
-License:
-  Copyright (C) 2010-2011 Matthew Turk.  All Rights Reserved.
 
-  This file is part of yt.
-
-  yt is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 3 of the License, or
-  (at your option) any later version.
-
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 """
 
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
 from .light_cone import \
     LightCone
 

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 yt/analysis_modules/cosmological_observation/light_cone/common_n_volume.py
--- a/yt/analysis_modules/cosmological_observation/light_cone/common_n_volume.py
+++ b/yt/analysis_modules/cosmological_observation/light_cone/common_n_volume.py
@@ -2,27 +2,17 @@
 Function to calculate volume in common between two n-cubes, with optional
 periodic boundary conditions.
 
-Author: Britton Smith <brittons at origins.colorado.edu>
-Affiliation: CASA/University of CO, Boulder
-Homepage: http://yt-project.org/
-License:
-  Copyright (C) 2008-2011 Britton Smith.  All Rights Reserved.
 
-  This file is part of yt.
 
-  yt is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 3 of the License, or
-  (at your option) any later version.
+"""
 
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-"""
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
 
 import numpy as np
 

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 yt/analysis_modules/cosmological_observation/light_cone/halo_mask.py
--- a/yt/analysis_modules/cosmological_observation/light_cone/halo_mask.py
+++ b/yt/analysis_modules/cosmological_observation/light_cone/halo_mask.py
@@ -1,27 +1,17 @@
 """
 Light cone halo mask functions.
 
-Author: Britton Smith <brittons at origins.colorado.edu>
-Affiliation: CASA/University of CO, Boulder
-Homepage: http://yt-project.org/
-License:
-  Copyright (C) 2008-2011 Britton Smith.  All Rights Reserved.
 
-  This file is part of yt.
 
-  yt is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 3 of the License, or
-  (at your option) any later version.
+"""
 
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-"""
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
 
 import copy
 import h5py

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 yt/analysis_modules/cosmological_observation/light_cone/light_cone.py
--- a/yt/analysis_modules/cosmological_observation/light_cone/light_cone.py
+++ b/yt/analysis_modules/cosmological_observation/light_cone/light_cone.py
@@ -1,27 +1,17 @@
 """
 LightCone class and member functions.
 
-Author: Britton Smith <brittons at origins.colorado.edu>
-Affiliation: CASA/University of CO, Boulder
-Homepage: http://yt-project.org/
-License:
-  Copyright (C) 2008-2012 Britton Smith.  All Rights Reserved.
 
-  This file is part of yt.
 
-  yt is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 3 of the License, or
-  (at your option) any later version.
+"""
 
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-"""
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
 
 import copy
 import h5py

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 yt/analysis_modules/cosmological_observation/light_cone/light_cone_projection.py
--- a/yt/analysis_modules/cosmological_observation/light_cone/light_cone_projection.py
+++ b/yt/analysis_modules/cosmological_observation/light_cone/light_cone_projection.py
@@ -1,27 +1,17 @@
 """
 Create randomly centered, tiled projections to be used in light cones.
 
-Author: Britton Smith <brittons at origins.colorado.edu>
-Affiliation: CASA/University of CO, Boulder
-Homepage: http://yt-project.org/
-License:
-  Copyright (C) 2008-2011 Britton Smith.  All Rights Reserved.
 
-  This file is part of yt.
 
-  yt is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 3 of the License, or
-  (at your option) any later version.
+"""
 
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-"""
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
 
 import numpy as np
 import copy

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 yt/analysis_modules/cosmological_observation/light_cone/unique_solution.py
--- a/yt/analysis_modules/cosmological_observation/light_cone/unique_solution.py
+++ b/yt/analysis_modules/cosmological_observation/light_cone/unique_solution.py
@@ -1,27 +1,17 @@
 """
 Functions to generate unique light cone solutions.
 
-Author: Britton Smith <brittons at origins.colorado.edu>
-Affiliation: CASA/University of CO, Boulder
-Homepage: http://yt-project.org/
-License:
-  Copyright (C) 2008-2011 Britton Smith.  All Rights Reserved.
 
-  This file is part of yt.
 
-  yt is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 3 of the License, or
-  (at your option) any later version.
+"""
 
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-"""
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
 
 import copy
 import numpy as np

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 yt/analysis_modules/cosmological_observation/light_ray/api.py
--- a/yt/analysis_modules/cosmological_observation/light_ray/api.py
+++ b/yt/analysis_modules/cosmological_observation/light_ray/api.py
@@ -1,32 +1,17 @@
 """
 API for light_ray
 
-Author: Matthew Turk <matthewturk at gmail.com>
-Affiliation: UCSD
-Author: J.S. Oishi <jsoishi at gmail.com>
-Affiliation: KIPAC/SLAC/Stanford
-Author: Britton Smith <brittonsmith at gmail.com>
-Affiliation: MSU
-Homepage: http://yt-project.org/
-License:
-  Copyright (C) 2010-2011 Matthew Turk.  All Rights Reserved.
 
-  This file is part of yt.
-
-  yt is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 3 of the License, or
-  (at your option) any later version.
-
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 """
 
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
 from .light_ray import \
     LightRay

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 yt/analysis_modules/cosmological_observation/light_ray/light_ray.py
--- a/yt/analysis_modules/cosmological_observation/light_ray/light_ray.py
+++ b/yt/analysis_modules/cosmological_observation/light_ray/light_ray.py
@@ -1,27 +1,17 @@
 """
 LightRay class and member functions.
 
-Author: Britton Smith <brittons at origins.colorado.edu>
-Affiliation: CASA/University of CO, Boulder
-Homepage: http://yt-project.org/
-License:
-  Copyright (C) 2008-2012 Britton Smith.  All Rights Reserved.
 
-  This file is part of yt.
 
-  yt is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 3 of the License, or
-  (at your option) any later version.
+"""
 
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-"""
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
 
 import copy
 import h5py

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 yt/analysis_modules/halo_finding/api.py
--- a/yt/analysis_modules/halo_finding/api.py
+++ b/yt/analysis_modules/halo_finding/api.py
@@ -1,33 +1,18 @@
 """
 API for halo_finding
 
-Author: Matthew Turk <matthewturk at gmail.com>
-Affiliation: UCSD
-Author: J.S. Oishi <jsoishi at gmail.com>
-Affiliation: KIPAC/SLAC/Stanford
-Author: Britton Smith <brittonsmith at gmail.com>
-Affiliation: MSU
-Homepage: http://yt-project.org/
-License:
-  Copyright (C) 2010-2011 Matthew Turk.  All Rights Reserved.
 
-  This file is part of yt.
-
-  yt is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 3 of the License, or
-  (at your option) any later version.
-
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 """
 
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
 from halo_objects import \
     Halo, \
     HOPHalo, \

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 yt/analysis_modules/halo_finding/fof/EnzoFOF.c
--- a/yt/analysis_modules/halo_finding/fof/EnzoFOF.c
+++ b/yt/analysis_modules/halo_finding/fof/EnzoFOF.c
@@ -1,22 +1,10 @@
-/************************************************************************
-* Copyright (C) 2008-2011 Matthew Turk.  All Rights Reserved.
-*
-* This file is part of yt.
-*
-* yt is free software; you can redistribute it and/or modify
-* it under the terms of the GNU General Public License as published by
-* the Free Software Foundation; either version 3 of the License, or
-* (at your option) any later version.
-*
-* This program is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-* GNU General Public License for more details.
-*
-* You should have received a copy of the GNU General Public License
-* along with this program.  If not, see <http://www.gnu.org/licenses/>.
-*
-************************************************************************/
+/*******************************************************************************
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+*******************************************************************************/
 
 //
 // EnzoFOF

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 yt/analysis_modules/halo_finding/halo_objects.py
--- a/yt/analysis_modules/halo_finding/halo_objects.py
+++ b/yt/analysis_modules/halo_finding/halo_objects.py
@@ -1,31 +1,17 @@
 """
 HOP-output data handling
 
-Author: Matthew Turk <matthewturk at gmail.com>
-Affiliation: KIPAC/SLAC/Stanford
-Author: Stephen Skory <s at skory.us>
-Affiliation: UCSD Physics/CASS
-Author: Geoffrey So <gsiisg at gmail.com> (Ellipsoidal functions)
-Affiliation: UCSD Physics/CASS
-Homepage: http://yt-project.org/
-License:
-  Copyright (C) 2008-2011 Matthew Turk.  All Rights Reserved.
 
-  This file is part of yt.
 
-  yt is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 3 of the License, or
-  (at your option) any later version.
+"""
 
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-"""
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
 
 import gc
 import h5py

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 yt/analysis_modules/halo_finding/hop/EnzoHop.c
--- a/yt/analysis_modules/halo_finding/hop/EnzoHop.c
+++ b/yt/analysis_modules/halo_finding/hop/EnzoHop.c
@@ -1,22 +1,10 @@
-/************************************************************************
-* Copyright (C) 2008-2011 Matthew Turk.  All Rights Reserved.
-*
-* This file is part of yt.
-*
-* yt is free software; you can redistribute it and/or modify
-* it under the terms of the GNU General Public License as published by
-* the Free Software Foundation; either version 3 of the License, or
-* (at your option) any later version.
-*
-* This program is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-* GNU General Public License for more details.
-*
-* You should have received a copy of the GNU General Public License
-* along with this program.  If not, see <http://www.gnu.org/licenses/>.
-*
-************************************************************************/
+/*******************************************************************************
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+*******************************************************************************/
 
 //
 // EnzoHop

diff -r 67fe5d0822d4084e1253da0016377248fed330b2 -r c26245ca37956e67e2a0f31712cb68a0f614e587 yt/analysis_modules/halo_finding/parallel_hop/parallel_hop_interface.py
--- a/yt/analysis_modules/halo_finding/parallel_hop/parallel_hop_interface.py
+++ b/yt/analysis_modules/halo_finding/parallel_hop/parallel_hop_interface.py
@@ -1,27 +1,17 @@
 """
 A implementation of the HOP algorithm that runs in parallel.
 
-Author: Stephen Skory <s at skory.us>
-Affiliation: UCSD/CASS
-Homepage: http://yt-project.org/
-License:
-  Copyright (C) 2008-2011 Stephen Skory.  All Rights Reserved.
 
-  This file is part of yt.
 
-  yt is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 3 of the License, or
-  (at your option) any later version.
+"""
 
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-"""
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
 
 from collections import defaultdict
 import itertools, sys

This diff is so big that we needed to truncate the remainder.

https://bitbucket.org/yt_analysis/yt/commits/fc0a5589baef/
Changeset:   fc0a5589baef
Branch:      yt
User:        jzuhone
Date:        2013-10-03 12:47:25
Summary:     Bugfix
Affected #:  1 file

diff -r c26245ca37956e67e2a0f31712cb68a0f614e587 -r fc0a5589baefa917510de521d40a774d12ced716 yt/analysis_modules/synthetic_obs/photon_models.py
--- a/yt/analysis_modules/synthetic_obs/photon_models.py
+++ b/yt/analysis_modules/synthetic_obs/photon_models.py
@@ -222,8 +222,8 @@
             raise IOError("File does not exist: %s." % filename)
         self.filename = filename
         f = h5py.File(self.filename,"r")
-        emin = f["energy"][:].min()
-        emax = f["energy"][:].max()
+        emin = f["energ_lo"][:].min()
+        emax = f["energ_hi"][:].max()
         self.sigma = f["cross_section"][:]
         nchan = self.sigma.shape[0]
         f.close()


https://bitbucket.org/yt_analysis/yt/commits/6dc7cbaa53a5/
Changeset:   6dc7cbaa53a5
Branch:      yt
User:        jzuhone
Date:        2013-10-03 22:12:29
Summary:     This wasn't a bug
Affected #:  1 file

diff -r fc0a5589baefa917510de521d40a774d12ced716 -r 6dc7cbaa53a5b652fdf5d98dc592058ed5b9fc38 yt/analysis_modules/synthetic_obs/photon_models.py
--- a/yt/analysis_modules/synthetic_obs/photon_models.py
+++ b/yt/analysis_modules/synthetic_obs/photon_models.py
@@ -222,8 +222,8 @@
             raise IOError("File does not exist: %s." % filename)
         self.filename = filename
         f = h5py.File(self.filename,"r")
-        emin = f["energ_lo"][:].min()
-        emax = f["energ_hi"][:].max()
+        emin = f["energy"][:].min()
+        emax = f["energy"][:].max()
         self.sigma = f["cross_section"][:]
         nchan = self.sigma.shape[0]
         f.close()


https://bitbucket.org/yt_analysis/yt/commits/b82372f98adb/
Changeset:   b82372f98adb
Branch:      yt
User:        jzuhone
Date:        2013-10-03 22:16:51
Summary:     Set up FITS header keywords for the events file so it can be read by CIAO and ds9.
Affected #:  1 file

diff -r 6dc7cbaa53a5b652fdf5d98dc592058ed5b9fc38 -r b82372f98adb5aab07bf7f74bf074afb34b7c024 yt/analysis_modules/synthetic_obs/photon_simulator.py
--- a/yt/analysis_modules/synthetic_obs/photon_simulator.py
+++ b/yt/analysis_modules/synthetic_obs/photon_simulator.py
@@ -37,14 +37,11 @@
 import h5py
 
 try:
-    import pyfits
+    import astropy.io.fits as pyfits
+    import astropy.wcs as pywcs
 except ImportError:
-    try:
-        import astropy.io.fits as pyfits
-    except ImportError:
-        mylog.warning("You don't have pyFITS installed. " + 
-                      "Writing to and reading from FITS files won't be available.")
-    
+    mylog.error("You don't have AstroPy installed.")
+                   
 N_TBIN = 10000
 TMIN = 8.08e-2
 TMAX = 50.
@@ -101,7 +98,8 @@
         photons["FiducialArea"] = f["/fid_area"].value
         photons["FiducialRedshift"] = f["/fid_redshift"].value
         photons["FiducialAngularDiameterDistance"] = f["/fid_d_a"].value
-
+        photons["DomainDimension"] = f["/domain_dimension"].value
+        
         num_cells = f["/x"][:].shape[0]
         start_c = comm.rank*num_cells/comm.size
         end_c = (comm.rank+1)*num_cells/comm.size
@@ -289,7 +287,8 @@
         photons["FiducialArea"] = eff_A
         photons["FiducialRedshift"] = redshift
         photons["FiducialAngularDiameterDistance"] = D_A/cm_per_mpc
-
+        photons["DomainDimension"] = np.max((pf.domain_dimensions*(2**pf.h.max_level)))
+        
         p_bins = np.cumsum(photons["NumberOfPhotons"])
         p_bins = np.insert(p_bins, 0, [np.uint64(0)])
         
@@ -300,6 +299,8 @@
                         exp_time, user_function, parameters={},
                         dist=None, cosmology=None):
 
+        pf = data_source.pf
+        
         if cosmology is None and dist is None:
             cosmo = Cosmology(HubbleConstantNow=71., OmegaMatterNow=0.27,
                               OmegaLambdaNow=0.73)
@@ -319,6 +320,7 @@
         photons["FiducialArea"] = eff_A
         photons["FiducialRedshift"] = redshift
         photons["FiducialAngularDiameterDistance"] = D_A
+        photons["DomainDimension"] = np.max((pf.domain_dimensions*(2**pf.h.max_level)))
 
         p_bins = np.cumsum(photons["NumberOfPhotons"])
         p_bins = np.insert(p_bins, 0, [np.uint64(0)])
@@ -408,7 +410,8 @@
             f.create_dataset("fid_exp_time", data=self.photons["FiducialExposureTime"])
             f.create_dataset("fid_redshift", data=self.photons["FiducialRedshift"])
             f.create_dataset("fid_d_a", data=self.photons["FiducialAngularDiameterDistance"])
-        
+            f.create_dataset("domain_dimension", data=self.photons["DomainDimension"])
+
             # Arrays
 
             f.create_dataset("x", data=x)
@@ -427,7 +430,8 @@
 
     def project_photons(self, L, area_new=None, texp_new=None, 
                         redshift_new=None, dist_new=None,
-                        absorb_model=None, psf_sigma=None):
+                        absorb_model=None, psf_sigma=None,
+                        sky_center=None):
         """
         Projects photons onto an image plane given a line of sight. 
         """
@@ -438,9 +442,15 @@
 
         if redshift_new is not None and self.cosmo is None:
             mylog.error("Specified a new redshift, but no cosmology!")
-            
+
+        if sky_center is None:
+             sky_center = np.array([30.,45.])
+
         dx = self.photons["dx"]
-        
+        nx = self.photons["DomainDimension"]
+        if psf_sigma is not None:
+             psf_sigma /= 3600.
+             
         L /= np.sqrt(np.dot(L, L))
         vecs = np.identity(3)
         t = np.cross(L, vecs).sum(axis=1)
@@ -542,7 +552,7 @@
             absorb = np.interp(eobs, emid, aspec, left=0.0, right=0.0)
             randvec = aspec.max()*np.random.random(eobs.shape)
             not_abs = randvec < absorb
-
+        
         if eff_area is None:
             detected = np.ones(eobs.shape, dtype='bool')
         else:
@@ -556,14 +566,23 @@
                     
         events = {}
 
-        events["xsky"] = np.rad2deg(xsky[detected]/D_A)*3600.
-        events["ysky"] = np.rad2deg(ysky[detected]/D_A)*3600.
+        dtheta = dx.min()/D_A
+        
+        events["xpix"] = xsky[detected]/dx.min() + 0.5*(nx+1) 
+        events["ypix"] = ysky[detected]/dx.min() + 0.5*(nx+1)
+        if psf_sigma is not None:
+            events["xpix"] += np.random.normal(sigma=psf_sigma/dtheta)
+            events["ypix"] += np.random.normal(sigma=psf_sigma/dtheta)
+        w = pywcs.WCS(naxis=2)
+        w.wcs.crpix = [0.5*(nx+1)]*2
+        w.wcs.crval = sky_center
+        w.wcs.cdelt = [-dtheta, dtheta]
+        w.wcs.ctype = ["RA---TAN","DEC--TAN"]
+        w.wcs.cunit = ["deg"]*2
+        events["xsky"], events["ysky"] = w.wcs_pix2world(events["xpix"], events["ypix"],
+                                                         1, ra_dec_order=True)
         events["eobs"] = eobs[detected]
 
-        if psf_sigma is not None:
-            events["xsky"] += np.random.normal(sigma=psf_sigma)
-            events["ysky"] += np.random.normal(sigma=psf_sigma)
-
         events = comm.par_combine_object(events, datatype="dict", op="cat")
         
         num_events = len(events["xsky"])
@@ -582,7 +601,10 @@
         events["AngularDiameterDistance"] = D_A/1000.
         if isinstance(area_new, basestring):
             events["ARF"] = area_new
-
+        events["sky_center"] = np.array(sky_center)
+        events["pix_center"] = np.array([0.5*(nx+1)]*2)
+        events["dtheta"] = dtheta
+        
         return EventList(events)
 
 class EventList(object) :
@@ -592,7 +614,7 @@
         if events is None : events = {}
         self.events = events
         self.num_events = events["xsky"].shape[0]
-
+        
     def keys(self):
         return self.events.keys()
 
@@ -631,9 +653,16 @@
                             
         events["xsky"] = f["/xsky"][:]
         events["ysky"] = f["/ysky"][:]
+        events["xpix"] = f["/xpix"][:]
+        events["ypix"] = f["/ypix"][:]
         events["eobs"] = f["/eobs"][:]
-        if "chan" in f:
-            events["chan"] = f["/chan"][:]
+        if "pi" in f:
+            events["PI"] = f["/pi"][:]
+        if "pha" in f:
+            events["PHA"] = f["/pha"][:]
+        events["sky_center"] = f["/sky_center"][:]
+        events["dtheta"] = f["/dtheta"][:]
+        events["pix_center"] = f["/pix_center"][:]
         
         f.close()
         
@@ -646,8 +675,8 @@
         """
         hdulist = pyfits.open(fitsfile)
 
-        tblhdu = hdulist[1]
-
+        tblhdu = hdulist["EVENTS"]
+        
         events = {}
 
         events["ExposureTime"] = tblhdu.header["EXPOSURE"]
@@ -664,13 +693,19 @@
             events["Telescope"] = tblhdu["TELESCOP"]
         if "INSTRUME" in tblhdu.header:
             events["Instrument"] = tblhdu["INSTRUME"]
-                            
-        events["xsky"] = tblhdu.data.field("POS_X")
-        events["ysky"] = tblhdu.data.field("POS_Y")
-        events["eobs"] = tblhdu.data.field("ENERGY")
-        if "CHANNEL" in tblhdu.columns.names:
-            events["chan"] = tblhdu.data.field("CHANNEL")
-                    
+        events["sky_center"] = np.array([tblhdu["TCRVL2"],tblhdu["TCRVL3"]])
+        events["pix_center"] = np.array([tblhdu["TCRVL2"],tblhdu["TCRVL3"]])
+        events["dtheta"] = tblhdu["TCRVL3"]
+        events["xpix"] = tblhdu.data.field("X")
+        events["ypix"] = tblhdu.data.field("Y")
+        events["xsky"] = tblhdu.data.field("XSKY")
+        events["ysky"] = tblhdu.data.field("YSKY")
+        events["eobs"] = tblhdu.data.field("ENERGY")/1000. # Convert to keV
+        if "PI" in tblhdu.columns.names:
+            events["PI"] = tblhdu.data.field("PI")
+        if "PHA" in tblhdu.columns.names:
+            events["PHA"] = tblhdu.data.field("PHA")
+        
         return cls(events)
 
     @classmethod
@@ -724,8 +759,10 @@
         eidxs = np.argsort(self.events["eobs"])
 
         phEE = self.events["eobs"][eidxs]
-        phXX = self.events["xsky"][eidxs]
-        phYY = self.events["ysky"][eidxs]
+        phXS = self.events["xsky"][eidxs]
+        phYS = self.events["ysky"][eidxs]
+        phXP = self.events["xpix"][eidxs]
+        phYP = self.events["ypix"][eidxs]
 
         detectedChannels = []
         pindex = 0
@@ -762,10 +799,12 @@
         
         dchannel = np.array(detectedChannels)
 
-        self.events["xsky"] = phXX
-        self.events["ysky"] = phYY
+        self.events["xsky"] = phXS
+        self.events["ysky"] = phYS
+        self.events["xpix"] = phXP
+        self.events["ypix"] = phYP
         self.events["eobs"] = phEE
-        self.events["chan"] = dchannel.astype(int)
+        self.events[tblhdu.header["CHANTYPE"]] = dchannel.astype(int)
         self.events["RMF"] = respfile
         self.events["ChannelType"] = tblhdu.header["CHANTYPE"]
         self.events["Telescope"] = tblhdu.header["TELESCOP"]
@@ -776,30 +815,55 @@
         """
         Write events to a FITS binary table file.
         """
-
+        
         cols = []
 
-        col1 = pyfits.Column(name='ENERGY', format='E',
-                             array=self.events["eobs"])
-        col2 = pyfits.Column(name='XSKY', format='D',
+        col1 = pyfits.Column(name='ENERGY', format='E', unit='eV',
+                             array=self.events["eobs"]*1000.)
+        col2 = pyfits.Column(name='X', format='D', unit='pixel',
+                             array=self.events["xpix"])
+        col3 = pyfits.Column(name='Y', format='D', unit='pixel',
+                             array=self.events["ypix"])
+        col4 = pyfits.Column(name='XSKY', format='D', unit='deg',
                              array=self.events["xsky"])
-        col3 = pyfits.Column(name='YSKY', format='D',
+        col5 = pyfits.Column(name='YSKY', format='D', unit='deg',
                              array=self.events["ysky"])
 
-        cols = [col1, col2, col3]
+        cols = [col1, col2, col3, col4, col5]
 
-        if "chan" in self.events:
-            col4 = pyfits.Column(name='CHANNEL', format='1J',
-                                 array=self.events["chan"])
-            cols.append(col4)
+        if self.events.has_key("ChannelType"):
+             chantype = self.events["ChannelType"]
+             if chantype == "PHA":
+                  cunit="adu"
+             elif chantype == "PI":
+                  cunit="Chan"
+             col6 = pyfits.Column(name=chantype.upper(), format='1J',
+                                  unit=cunit, array=self.events[chantype])
+             cols.append(col6)
             
         coldefs = pyfits.ColDefs(cols)
         tbhdu = pyfits.new_table(coldefs)
+        tbhdu.update_ext_name("EVENTS")
 
+        tbhdu.header.update("TCTYP2", "RA---TAN")
+        tbhdu.header.update("TCTYP3", "DEC--TAN")
+        tbhdu.header.update("TCRVL2", self.events["sky_center"][0])
+        tbhdu.header.update("TCRVL3", self.events["sky_center"][1])
+        tbhdu.header.update("TCDLT2", -self.events["dtheta"])
+        tbhdu.header.update("TCDLT3", self.events["dtheta"])
+        tbhdu.header.update("TCRPX2", self.events["pix_center"][0])
+        tbhdu.header.update("TCRPX3", self.events["pix_center"][1])
+        tbhdu.header.update("TLMIN2", 0.5)
+        tbhdu.header.update("TLMIN3", 0.5)
+        tbhdu.header.update("TLMAX2", 2.*self.events["pix_center"][0]-0.5)
+        tbhdu.header.update("TLMAX3", 2.*self.events["pix_center"][1]-0.5)
         tbhdu.header.update("EXPOSURE", self.events["ExposureTime"])
         tbhdu.header.update("AREA", self.events["Area"])
         tbhdu.header.update("D_A", self.events["AngularDiameterDistance"])
         tbhdu.header.update("REDSHIFT", self.events["Redshift"])
+        tbhdu.header.update("HDUVERS", "1.1.0")
+        tbhdu.header.update("RADECSYS", "FK5")
+        tbhdu.header.update("EQUINOX", 2000.0)
         if "RMF" in self.events:
             tbhdu.header.update("RMF", self.events["RMF"])
         if "ARF" in self.events:
@@ -829,9 +893,9 @@
         col1 = pyfits.Column(name='ENERGY', format='E',
                              array=self.events["eobs"])
         col2 = pyfits.Column(name='DEC', format='D',
-                             array=self.events["ysky"]/3600.)
+                             array=self.events["ysky"])
         col3 = pyfits.Column(name='RA', format='D',
-                             array=self.events["xsky"]/3600.)
+                             array=self.events["xsky"])
 
         coldefs = pyfits.ColDefs([col1, col2, col3])
 
@@ -853,8 +917,8 @@
         tbhdu.writeto(phfile, clobber=clobber)
 
         col1 = pyfits.Column(name='SRC_ID', format='J', array=np.array([1]).astype("int32"))
-        col2 = pyfits.Column(name='RA', format='D', array=np.array([0.0]))
-        col3 = pyfits.Column(name='DEC', format='D', array=np.array([0.0]))
+        col2 = pyfits.Column(name='RA', format='D', array=np.array([self.center[0]]))
+        col3 = pyfits.Column(name='DEC', format='D', array=np.array([self.center[1]]))
         col4 = pyfits.Column(name='E_MIN', format='D', array=np.array([e_min]))
         col5 = pyfits.Column(name='E_MAX', format='D', array=np.array([e_max]))
         col6 = pyfits.Column(name='FLUX', format='D', array=np.array([flux]))
@@ -906,15 +970,21 @@
                             
         f.create_dataset("/xsky", data=self.events["xsky"])
         f.create_dataset("/ysky", data=self.events["ysky"])
+        f.create_dataset("/xpix", data=self.events["xpix"])
+        f.create_dataset("/ypix", data=self.events["ypix"])
         f.create_dataset("/eobs", data=self.events["eobs"])
-        if "chan" in self.events:
-            f.create_dataset("/chan", data=self.events["chan"])                  
-        
+        if "PI" in self.events:
+            f.create_dataset("/pi", data=self.events["PI"])                  
+        if "PHA" in self.events:
+            f.create_dataset("/pha", data=self.events["PHA"])                  
+        f.create_dataset("/sky_center", data=self.events["sky_center"])
+        f.create_dataset("/pix_center", data=self.events["pix_center"])
+        f.create_dataset("/dtheta", data=self.events["dtheta"])
+
         f.close()
 
     @parallel_root_only
-    def write_fits_image(self, imagefile, width, nx, center,
-                         clobber=False, gzip_file=False,
+    def write_fits_image(self, imagefile, clobber=False,
                          emin=None, emax=None):
         """
         Generate a image by binning X-ray counts and write it to a FITS file.
@@ -930,14 +1000,15 @@
 
         mask = np.logical_and(mask_emin, mask_emax)
 
-        dx_pixel = width/nx
-        xmin = -0.5*width
-        xmax = -xmin
-        xbins = np.linspace(xmin, xmax, nx+1, endpoint=True)
+        nx = int(2*self.events["pix_center"][0]-1.)
+        ny = int(2*self.events["pix_center"][1]-1.)
         
-        H, xedges, yedges = np.histogram2d(self.events["xsky"][mask],
-                                           self.events["ysky"][mask],
-                                           bins=[xbins,xbins])
+        xbins = np.linspace(0.5, float(nx)+0.5, nx+1, endpoint=True)
+        ybins = np.linspace(0.5, float(ny)+0.5, ny+1, endpoint=True)
+
+        H, xedges, yedges = np.histogram2d(self.events["xpix"][mask],
+                                           self.events["ypix"][mask],
+                                           bins=[xbins,ybins])
         
         hdu = pyfits.PrimaryHDU(H.T)
         
@@ -947,20 +1018,15 @@
         hdu.header.update("CTYPE2", "DEC--TAN")
         hdu.header.update("CRPIX1", 0.5*(nx+1))
         hdu.header.update("CRPIX2", 0.5*(nx+1))                
-        hdu.header.update("CRVAL1", center[0])
-        hdu.header.update("CRVAL2", center[1])
+        hdu.header.update("CRVAL1", self.events["sky_center"][0])
+        hdu.header.update("CRVAL2", self.events["sky_center"][1])
         hdu.header.update("CUNIT1", "deg")
         hdu.header.update("CUNIT2", "deg")
-        hdu.header.update("CDELT1", -dx_pixel/3600.)
-        hdu.header.update("CDELT2", dx_pixel/3600.)
+        hdu.header.update("CDELT1", -self.events["dtheta"])
+        hdu.header.update("CDELT2", self.events["dtheta"])
         hdu.header.update("EXPOSURE", self.events["ExposureTime"])
         
         hdu.writeto(imagefile, clobber=clobber)
-
-        if gzip_file:
-            clob = ""
-            if clobber: clob="-f"
-            os.system("gzip "+clob+" %s.fits" % (prefix))
                                     
     @parallel_root_only
     def write_spectrum(self, specfile, emin=0.1, emax=10.0, nchan=2000, clobber=False):


https://bitbucket.org/yt_analysis/yt/commits/d32903e208fe/
Changeset:   d32903e208fe
Branch:      yt
User:        jzuhone
Date:        2013-10-07 18:40:49
Summary:     Trying to get the events files in the same format as observations
Affected #:  1 file

diff -r b82372f98adb5aab07bf7f74bf074afb34b7c024 -r d32903e208fe59a4456348ec507078969eb89e3e yt/analysis_modules/synthetic_obs/photon_simulator.py
--- a/yt/analysis_modules/synthetic_obs/photon_simulator.py
+++ b/yt/analysis_modules/synthetic_obs/photon_simulator.py
@@ -845,6 +845,10 @@
         tbhdu = pyfits.new_table(coldefs)
         tbhdu.update_ext_name("EVENTS")
 
+        tbhdu.header.update("MTYPE1", "sky")
+        tbhdu.header.update("MFORM1", "x,y")        
+        tbhdu.header.update("MTYPE2", "EQPOS")
+        tbhdu.header.update("MFORM2", "RA,DEC")
         tbhdu.header.update("TCTYP2", "RA---TAN")
         tbhdu.header.update("TCTYP3", "DEC--TAN")
         tbhdu.header.update("TCRVL2", self.events["sky_center"][0])


https://bitbucket.org/yt_analysis/yt/commits/f9e54cd9a76a/
Changeset:   f9e54cd9a76a
Branch:      yt
User:        jzuhone
Date:        2013-10-11 16:20:28
Summary:     1) Docstrings.
2) Made the way spectra are generated more consistent.
3) Small changes to the way cosmology is handled.
4) Generalized treatment for RMFs.
Affected #:  2 files

diff -r d32903e208fe59a4456348ec507078969eb89e3e -r f9e54cd9a76ad809cda2d6388d238876b1ea97c6 yt/analysis_modules/synthetic_obs/photon_models.py
--- a/yt/analysis_modules/synthetic_obs/photon_models.py
+++ b/yt/analysis_modules/synthetic_obs/photon_models.py
@@ -1,40 +1,24 @@
+"""
+Photon emission and absoprtion models for use with the
+photon simulator.
 """
 
-Spectral models for generating photons
-
-Author: John ZuHone <jzuhone at gmail.com>
-Affiliation: NASA/GSFC
-Homepage: http://yt-project.org/
-License:
-Copyright (C) 2010-2011 Matthew Turk.  All Rights Reserved.
-
-This file is part of yt.
-
-yt is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 3 of the License, or
-(at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
-"""
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
 
 import numpy as np
 import os
 from yt.funcs import *
 import h5py
 try:
-    import pyfits
+    import astropy.io.fits as pyfits
 except:
-    try:
-        import astropy.io.fits as pyfits
-    except:
-        mylog.error("You don't have pyFITS installed. The APEC table model won't be available.")
+    mylog.warning("You don't have AstroPy installed. The APEC table model won't be available.")
 try:
     import xspec
 except ImportError:
@@ -66,12 +50,33 @@
         pass
                                                         
 class XSpecThermalModel(PhotonModel):
+    r"""
+    Initialize a thermal gas emission model from PyXspec.
+    
+    Parameters
+    ----------
 
+    model_name : string
+        The name of the thermal emission model.
+    emin : float
+        The minimum energy for the spectral model.
+    emax : float
+        The maximum energy for the spectral model.
+    nchan : integer
+        The number of channels in the spectral model.
+
+    Examples
+    --------
+    >>> mekal_model = XSpecThermalModel("mekal", 0.05, 50.0, 1000)
+    """
     def __init__(self, model_name, emin, emax, nchan):
         self.model_name = model_name
         PhotonModel.__init__(self, emin, emax, nchan)
         
     def prepare(self):
+        """
+        Prepare the thermal model for execution.
+        """
         xspec.Xset.chatter = 0
         xspec.AllModels.setEnergies("%f %f %d lin" %
                                     (self.emin, self.emax, self.nchan))
@@ -82,6 +87,9 @@
             self.norm = 1.0e-14
         
     def get_spectrum(self, kT):
+        """
+        Get the thermal emission spectrum given a temperature *kT* in keV. 
+        """
         m = getattr(self.model,self.model_name)
         m.kT = kT
         m.Abundanc = 0.0
@@ -96,13 +104,36 @@
         return cosmic_spec, metal_spec
         
 class XSpecAbsorbModel(PhotonModel):
+    r"""
+    Initialize an absorption model from PyXspec.
+    
+    Parameters
+    ----------
 
+    model_name : string
+        The name of the absorption model.
+    nH : float
+        The foreground column density *nH* in units of 10^22 cm^{-2}.
+    emin : float, optional
+        The minimum energy for the spectral model.
+    emax : float, optional
+        The maximum energy for the spectral model.
+    nchan : integer, optional
+        The number of channels in the spectral model.
+
+    Examples
+    --------
+    >>> abs_model = XSpecAbsorbModel("wabs", 0.1)
+    """
     def __init__(self, model_name, nH, emin=0.01, emax=50.0, nchan=100000):
         self.model_name = model_name
         self.nH = nH
         PhotonModel.__init__(self, emin, emax, nchan)
         
     def prepare(self):
+        """
+        Prepare the absorption model for execution.
+        """
         xspec.Xset.chatter = 0
         xspec.AllModels.setEnergies("%f %f %d lin" %
                                     (self.emin, self.emax, self.nchan))
@@ -111,12 +142,44 @@
         self.model.powerlaw.PhoIndex = 0.0
 
     def get_spectrum(self):
+        """
+        Get the absorption spectrum.
+        """
         m = getattr(self.model,self.model_name)
         m.nH = self.nH
         return np.array(self.model.values(0))
 
 class TableApecModel(PhotonModel):
+    r"""
+    Initialize a thermal gas emission model from the AtomDB APEC tables
+    available at http://www.atomdb.org. This code borrows heavily from Python
+    routines used to read the APEC tables developed by Adam Foster at the
+    CfA (afoster at cfa.harvard.edu). 
+    
+    Parameters
+    ----------
 
+    apec_root : string
+        The directory root where the APEC model files are stored.
+    emin : float
+        The minimum energy for the spectral model.
+    emax : float
+        The maximum energy for the spectral model.
+    nchan : integer
+        The number of channels in the spectral model.
+    apec_vers : string, optional
+        The version identifier string for the APEC files, e.g.
+        "2.0.2"
+    thermal_broad : boolean, optional
+        Whether to apply thermal broadening to spectral lines. Only should
+        be used if you are attemping to simulate a high-spectral resolution
+        detector.
+
+    Examples
+    --------
+    >>> apec_model = TableApecModel("/Users/jzuhone/Data/atomdb_v2.0.2/", 0.05, 50.0,
+                                    1000, thermal_broad=True)
+    """
     def __init__(self, apec_root, emin, emax, nchan,
                  apec_vers="2.0.2", thermal_broad=False):
         self.apec_root = apec_root
@@ -140,6 +203,9 @@
                            55.8450,58.9332,58.6934,63.5460,65.3800])
         
     def prepare(self):
+        """
+        Prepare the thermal model for execution.
+        """
         try:
             self.line_handle = pyfits.open(self.linefile)
         except IOError:
@@ -153,7 +219,7 @@
         self.minlam = self.wvbins.min()
         self.maxlam = self.wvbins.max()
     
-    def make_spectrum(self, element, tindex):
+    def _make_spectrum(self, element, tindex):
         
         tmpspec = np.zeros((self.nchan))
         
@@ -197,6 +263,9 @@
         return tmpspec
 
     def get_spectrum(self, kT):
+        """
+        Get the thermal emission spectrum given a temperature *kT* in keV. 
+        """
         cspec_l = np.zeros((self.nchan))
         mspec_l = np.zeros((self.nchan))
         cspec_r = np.zeros((self.nchan))
@@ -205,17 +274,32 @@
         dT = (kT-self.Tvals[tindex])/self.dTvals[tindex]
         # First do H,He, and trace elements
         for elem in self.cosmic_elem:
-            cspec_l += self.make_spectrum(elem, tindex+2)
-            cspec_r += self.make_spectrum(elem, tindex+3)            
+            cspec_l += self._make_spectrum(elem, tindex+2)
+            cspec_r += self._make_spectrum(elem, tindex+3)            
         # Next do the metals
         for elem in self.metal_elem:
-            mspec_l += self.make_spectrum(elem, tindex+2)
-            mspec_r += self.make_spectrum(elem, tindex+3)
+            mspec_l += self._make_spectrum(elem, tindex+2)
+            mspec_r += self._make_spectrum(elem, tindex+3)
         cosmic_spec = cspec_l*(1.-dT)+cspec_r*dT
         metal_spec = mspec_l*(1.-dT)+mspec_r*dT        
         return cosmic_spec, metal_spec
 
 class TableAbsorbModel(PhotonModel):
+    r"""
+    Initialize an absorption model from a table stored in an HDF5 file.
+    
+    Parameters
+    ----------
+
+    filename : string
+        The name of the table file.
+    nH : float
+        The foreground column density *nH* in units of 10^22 cm^{-2}.
+
+    Examples
+    --------
+    >>> abs_model = XSpecAbsorbModel("wabs", 0.1)
+    """
 
     def __init__(self, filename, nH):
         if not os.path.exists(filename):
@@ -231,7 +315,13 @@
         self.nH = nH*1.0e22
         
     def prepare(self):
+        """
+        Prepare the absorption model for execution.
+        """
         pass
         
     def get_spectrum(self):
+        """
+        Get the absorption spectrum.
+        """
         return np.exp(-self.sigma*self.nH)

diff -r d32903e208fe59a4456348ec507078969eb89e3e -r f9e54cd9a76ad809cda2d6388d238876b1ea97c6 yt/analysis_modules/synthetic_obs/photon_simulator.py
--- a/yt/analysis_modules/synthetic_obs/photon_simulator.py
+++ b/yt/analysis_modules/synthetic_obs/photon_simulator.py
@@ -1,27 +1,14 @@
 """
 Classes for generating lists of photons and detected events
+"""
 
-Author: John ZuHone <jzuhone at gmail.com>
-Affiliation: NASA/GSFC
-Homepage: http://yt-project.org/
-License:
-Copyright (C) 2010-2011 Matthew Turk.  All Rights Reserved.
-
-This file is part of yt.
-
-yt is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 3 of the License, or
-(at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
-"""
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
 
 import numpy as np
 from numpy.testing import assert_allclose
@@ -33,14 +20,14 @@
 from yt.utilities.parallel_tools.parallel_analysis_interface import \
      communication_system, parallel_root_only, get_mpi_type, parallel_capable
 
-import os
 import h5py
 
 try:
     import astropy.io.fits as pyfits
     import astropy.wcs as pywcs
 except ImportError:
-    mylog.error("You don't have AstroPy installed.")
+    mylog.warning("You don't have AstroPy installed. " +
+                  "You will be unable to write FITS events files or images.")
                    
 N_TBIN = 10000
 TMIN = 8.08e-2
@@ -86,9 +73,9 @@
             return self.photons[key]
     
     @classmethod
-    def from_file(cls, filename, cosmology=None):
-        """
-        Initialize a PhotonList from an HDF5 file given by filename.
+    def from_file(cls, filename):
+        r"""
+        Initialize a PhotonList from the HDF5 file *filename*.
         """
         photons = {}
 
@@ -99,7 +86,10 @@
         photons["FiducialRedshift"] = f["/fid_redshift"].value
         photons["FiducialAngularDiameterDistance"] = f["/fid_d_a"].value
         photons["DomainDimension"] = f["/domain_dimension"].value
-        
+        photons["HubbleConstant"] = f["/hubble"].value
+        photons["OmegaMatter"] = f["/omega_matter"].value
+        photons["OmegaLambda"] = f["/omega_lambda"].value
+
         num_cells = f["/x"][:].shape[0]
         start_c = comm.rank*num_cells/comm.size
         end_c = (comm.rank+1)*num_cells/comm.size
@@ -128,30 +118,73 @@
         photons["Energy"] = f["/energy"][start_e:end_e]
         
         f.close()
-                                        
-        return cls(photons=photons, cosmo=cosmology, p_bins=p_bins)
+
+        cosmo = Cosmology(HubbleConstantNow=photons["HubbleConstant"],
+                          OmegaMatterNow=photons["OmegaMatter"],
+                          OmegaLambdaNow=photons["OmegaLambda"])
+        
+        return cls(photons=photons, cosmo=cosmo, p_bins=p_bins)
 
     @classmethod
-    def from_thermal_model(cls, data_source, redshift, eff_A,
+    def from_thermal_model(cls, data_source, redshift, area,
                            exp_time, emission_model, center="c",
                            X_H=0.75, Zmet=0.3, dist=None, cosmology=None):
-        """
-        Initialize a PhotonList from a data container. 
+        r"""
+        Initialize a PhotonList from a thermal plasma. 
+
+        Parameters
+        ----------
+
+        data_source : `yt.data_objects.api.AMRData`
+            The data source from which the photons will be generated.
+        redshift : float
+            The cosmological redshift for the photons.
+        area : float
+            The collecting area to determine the number of photons in cm^2.
+        exp_time : float
+            The exposure time to determine the number of photons in seconds.
+        emission_model : `yt.analysis_modules.photon_simulator.PhotonModel`
+            The thermal emission model from which to draw the photon samples
+        center : string or array_like, optional
+            The origin of the photons. Accepts "c", "max", or a coordinate. 
+        X_H : float, optional
+            The hydrogen mass fraction.
+        Zmet : float, optional
+            The metallicity of the gas. If there is a "Metallicity" field
+            this parameter is ignored.
+        dist : float, optional
+            The angular diameter distance in Mpc, used for nearby sources.
+            This may be optionally supplied instead of it being determined
+            from the *redshift* and given *cosmology*.
+        cosmology : `yt.utilities.cosmology.Cosmology`, optional
+            Cosmological information. If not supplied, it assumes \LambdaCDM with
+            the default yt parameters.
+
+        Examples
+        --------
+
+        >>> redshift = 0.1
+        >>> area = 6000.0
+        >>> time = 2.0e5
+        >>> sp = pf.h.sphere("c", (1.0, "mpc"))
+        >>> apec_model = XSpecThermalModel("apec", 0.05, 50.0, 1000)
+        >>> my_photons = PhotonList.from_thermal_model(sp, redshift, area,
+        ...                                            time, apec_model)
+
         """
         pf = data_source.pf
                 
         vol_scale = pf.units["cm"]**(-3)/np.prod(pf.domain_width)
 
-        if cosmology is None and dist is None:
+        if cosmology is None:
             cosmo = Cosmology()
         else:
-            if cosmology is None:
-                D_A = dist*cm_per_mpc
-                cosmo = None
-            else:
-                cosmo = cosmology
-        if cosmo is not None:
+            cosmo = cosmology
+        if dist is None:
             D_A = cosmo.AngularDiameterDistance(0.0,redshift)*cm_per_mpc
+        else:
+            D_A = dist*cm_per_mpc
+            redshift = 0.0
         dist_fac = 1.0/(4.*np.pi*D_A*D_A*(1.+redshift)**3)
         
         num_cells = data_source["Temperature"].shape[0]
@@ -163,6 +196,8 @@
         dx = data_source["dx"][start_c:end_c].copy()
         EM = (data_source["Density"][start_c:end_c].copy()/mp)**2
         EM *= 0.5*(1.+X_H)*X_H*vol
+
+        print EM.sum()
         
         data_source.clear_data()
         
@@ -232,18 +267,19 @@
             em_sum_m = (metalZ[ibegin:iend]*cell_em[ibegin:iend]).sum() 
 
             cspec, mspec = emission_model.get_spectrum(kT)
+            print (cspec+0.3*mspec).max(), kT
             cspec *= dist_fac*em_sum_c/vol_scale
             mspec *= dist_fac*em_sum_m/vol_scale
             
             cumspec_c = np.cumsum(cspec)
             counts_c = cumspec_c[:]/cumspec_c[-1]
             counts_c = np.insert(counts_c, 0, 0.0)
-            tot_ph_c = cumspec_c[-1]*eff_A*exp_time
+            tot_ph_c = cumspec_c[-1]*area*exp_time
 
             cumspec_m = np.cumsum(mspec)
             counts_m = cumspec_m[:]/cumspec_m[-1]
             counts_m = np.insert(counts_m, 0, 0.0)
-            tot_ph_m = cumspec_m[-1]*eff_A*exp_time
+            tot_ph_m = cumspec_m[-1]*area*exp_time
             
             for icell in xrange(ibegin, iend):
                 
@@ -284,43 +320,90 @@
         photons["Energy"] = np.concatenate(energies)
                 
         photons["FiducialExposureTime"] = exp_time
-        photons["FiducialArea"] = eff_A
+        photons["FiducialArea"] = area
         photons["FiducialRedshift"] = redshift
         photons["FiducialAngularDiameterDistance"] = D_A/cm_per_mpc
         photons["DomainDimension"] = np.max((pf.domain_dimensions*(2**pf.h.max_level)))
-        
+        photons["HubbleConstant"] = cosmo.HubbleConstantNow
+        photons["OmegaMatter"] = cosmo.OmegaMatterNow
+        photons["OmegaLambda"] = cosmo.OmegaLambdaNow
+
         p_bins = np.cumsum(photons["NumberOfPhotons"])
         p_bins = np.insert(p_bins, 0, [np.uint64(0)])
         
         return cls(photons=photons, cosmo=cosmo, p_bins=p_bins)
 
     @classmethod
-    def from_user_model(cls, data_source, redshift, eff_A,
-                        exp_time, user_function, parameters={},
+    def from_user_model(cls, data_source, redshift, area,
+                        exp_time, user_function, parameters=None,
                         dist=None, cosmology=None):
+        """
+        Initialize a PhotonList from a user-provided model.  
+
+        Parameters
+        ----------
+
+        data_source : `yt.data_objects.api.AMRData`
+            The data source from which the photons will be generated.
+        redshift : float
+            The cosmological redshift for the photons.
+        area : float
+            The collecting area to determine the number of photons in cm^2.
+        exp_time : float
+            The exposure time to determine the number of photons in seconds.
+        user_function : function
+            A function that takes the *data_source* and any parameters and
+            generates the photons. 
+        parameters : dict, optional
+            A dictionary of parameters to be passed to the user function. 
+        dist : float, optional
+            The angular diameter distance in Mpc, used for nearby sources.
+            This may be optionally supplied instead of it being determined
+            from the *redshift* and given *cosmology*.
+        cosmology : `yt.utilities.cosmology.Cosmology`, optional
+            Cosmological information. If not supplied, it assumes \LambdaCDM with
+            the default yt parameters.
+
+        Examples
+        --------
+
+        >>> def powerlaw_func(source, redshift, area, exp_time, D_A, norm, index, E0):
+        ...
+        ...     spec = norm*source["Density"]*(/E0
+        >>> redshift = 0.02
+        >>> area = 6000.0
+        >>> time = 2.0e5
+        >>> dd = pf.h.all_data
+        >>> my_photons = PhotonList.from_user_model(dd, redshift, area,
+        ...                                         time, powerlaw_func)
+
+        """
 
         pf = data_source.pf
-        
-        if cosmology is None and dist is None:
-            cosmo = Cosmology(HubbleConstantNow=71., OmegaMatterNow=0.27,
-                              OmegaLambdaNow=0.73)
+
+        if parameters is None:
+             parameters = {}
+        if cosmology is None:
+            cosmo = Cosmology()
         else:
-            if cosmology is None:
-                D_A = dist*cm_per_mpc
-                cosmo = None
-            else:
-                cosmo = cosmology
-        if cosmo is not None:
+            cosmo = cosmology
+        if dist is None:
             D_A = cosmo.AngularDiameterDistance(0.0,redshift)*cm_per_mpc
+        else:
+            D_A = dist*cm_per_mpc
+            redshift = 0.0
                     
-        photons = user_function(data_source, redshift, eff_A,
+        photons = user_function(data_source, redshift, area,
                                 exp_time, D_A, parameters)
         
         photons["FiducialExposureTime"] = exp_time
-        photons["FiducialArea"] = eff_A
+        photons["FiducialArea"] = area
         photons["FiducialRedshift"] = redshift
         photons["FiducialAngularDiameterDistance"] = D_A
         photons["DomainDimension"] = np.max((pf.domain_dimensions*(2**pf.h.max_level)))
+        photons["HubbleConstant"] = cosmo.HubbleConstantNow
+        photons["OmegaMatter"] = cosmo.OmegaMatterNow
+        photons["OmegaLambda"] = cosmo.OmegaLambdaNow
 
         p_bins = np.cumsum(photons["NumberOfPhotons"])
         p_bins = np.insert(p_bins, 0, [np.uint64(0)])
@@ -328,7 +411,9 @@
         return cls(photons=photons, cosmo=cosmo, p_bins=p_bins)
         
     def write_h5_file(self, photonfile):
-
+        """
+        Write the photons to the HDF5 file *photonfile*.
+        """
         if parallel_capable:
             
             mpi_long = get_mpi_type("int64")
@@ -409,6 +494,9 @@
             f.create_dataset("fid_area", data=self.photons["FiducialArea"])
             f.create_dataset("fid_exp_time", data=self.photons["FiducialExposureTime"])
             f.create_dataset("fid_redshift", data=self.photons["FiducialRedshift"])
+            f.create_dataset("hubble", data=self.photons["HubbleConstant"])
+            f.create_dataset("omega_matter", data=self.photons["OmegaMatter"])
+            f.create_dataset("omega_lambda", data=self.photons["OmegaLambda"])
             f.create_dataset("fid_d_a", data=self.photons["FiducialAngularDiameterDistance"])
             f.create_dataset("domain_dimension", data=self.photons["DomainDimension"])
 
@@ -432,17 +520,46 @@
                         redshift_new=None, dist_new=None,
                         absorb_model=None, psf_sigma=None,
                         sky_center=None):
-        """
-        Projects photons onto an image plane given a line of sight. 
+        r"""
+        Projects photons onto an image plane given a line of sight.
+
+        Parameters
+        ----------
+
+        L : array_like
+            Normal vector to the plane of projection.
+        area_new : float or filename, optional
+            New value for the effective area of the detector. 
+            Either a single float value or a standard ARF file
+            containing the effective area as a function of energy.
+        texp_new : float, optional
+            The new value for the exposure time.
+        redshift_new : float, optional
+            The new value for the cosmological redshift.
+        dist_new : float, optional
+            The new value for the angular diameter distance, used for nearby
+            objects. If this is not set it will be determined from the cosmological
+            redshift.
+        absorb_model : 'yt.analysis_modules.photon_simulator.PhotonModel`, optional
+            A model for galactic absorption.
+        psf_sigma : float, optional
+            Quick-and-dirty psf simulation using Gaussian smoothing with
+            standard deviation *psf_sigma* in degrees. 
+        sky_center : array_like, optional
+            Center RA, Dec of the events in degrees.
+
+        Examples
+        --------
+        >>> L = np.array([0.1,-0.2,0.3])
+        >>> events = my_photons.project_photons(L, area_new="sim_arf.fits",
+        ...                                     redshift_new=0.05,
+        ...                                     psf_sigma=0.01)
         """
 
         if redshift_new is not None and dist_new is not None:
             mylog.error("You may specify a new redshift or distance, "+
                         "but not both!")
-
-        if redshift_new is not None and self.cosmo is None:
-            mylog.error("Specified a new redshift, but no cosmology!")
-
+        
         if sky_center is None:
              sky_center = np.array([30.,45.])
 
@@ -450,7 +567,8 @@
         nx = self.photons["DomainDimension"]
         if psf_sigma is not None:
              psf_sigma /= 3600.
-             
+
+        L = np.array(L)
         L /= np.sqrt(np.dot(L, L))
         vecs = np.identity(3)
         t = np.cross(L, vecs).sum(axis=1)
@@ -497,7 +615,7 @@
                 D_A = self.photons["FiducialAngularDiameterDistance"]*1000.                    
             else:
                 if redshift_new is None:
-                    zobs = self.photons["FiducialRedshift"]
+                    zobs = 0.0
                     D_A = dist_new*1000.
                 else:
                     zobs = redshift_new
@@ -630,7 +748,7 @@
     @classmethod
     def from_h5_file(cls, h5file):
         """
-        Initialize an EventList from a HDF5 file with filename h5file.
+        Initialize an EventList from a HDF5 file with filename *h5file*.
         """
         events = {}
         
@@ -661,7 +779,7 @@
         if "pha" in f:
             events["PHA"] = f["/pha"][:]
         events["sky_center"] = f["/sky_center"][:]
-        events["dtheta"] = f["/dtheta"][:]
+        events["dtheta"] = f["/dtheta"].value
         events["pix_center"] = f["/pix_center"][:]
         
         f.close()
@@ -671,7 +789,7 @@
     @classmethod
     def from_fits_file(cls, fitsfile):
         """
-        Initialize an EventList from a FITS file with filename fitsfile.
+        Initialize an EventList from a FITS file with filename *fitsfile*.
         """
         hdulist = pyfits.open(fitsfile)
 
@@ -710,6 +828,9 @@
 
     @classmethod
     def join_events(cls, events1, events2):
+        """
+        Join two sets of events, *events1* and *events2*.
+        """
         events = {}
         for item1, item2 in zip(events1.items(), events2.items()):
             k1, v1 = item1
@@ -721,7 +842,10 @@
         return cls(events)
 
     def convolve_with_response(self, respfile):
-
+        """
+        Convolve the events with a RMF file *respfile*.
+        """
+        mylog.warning("This routine has not been tested to work with all RMFs. YMMV.")
         if not "ARF" in self.events:
             mylog.warning("Photons have not been processed with an"+
                           " auxiliary response file. Spectral fitting"+
@@ -731,13 +855,13 @@
         
         hdulist = pyfits.open(respfile)
 
-        tblhdu = hdulist[1]
+        tblhdu = hdulist["MATRIX"]
         n_de = len(tblhdu.data["ENERG_LO"])
         mylog.info("Number of Energy Bins: %d" % (n_de))
         de = tblhdu.data["ENERG_HI"] - tblhdu.data["ENERG_LO"]
 
         mylog.info("Energy limits: %g %g" % (min(tblhdu.data["ENERG_LO"]),
-                                             max(tblhdu.data["ENERG_HI"] )))
+                                             max(tblhdu.data["ENERG_HI"])))
 
         if "ARF" in self.events:
             f = pyfits.open(self.events["ARF"])
@@ -745,14 +869,14 @@
             ehi = f[1].data.field("ENERG_HI")
             f.close()
             try:
-                assert_allclose(elo, tblhdu.data["ENERG_LO"])
-                assert_allclose(ehi, tblhdu.data["ENERG_HI"])
+                assert_allclose(elo, tblhdu.data["ENERG_LO"], rtol=1.0e-6)
+                assert_allclose(ehi, tblhdu.data["ENERG_HI"], rtol=1.0e-6)
             except AssertionError:
                 mylog.warning("Energy binning does not match for "+
-                              "ARF and RMF. This will make spectral"+
+                              "ARF and RMF. This may make spectral "+
                               "fitting difficult.")
                                               
-        tblhdu2 = hdulist[2]
+        tblhdu2 = hdulist["EBOUNDS"]
         n_ch = len(tblhdu2.data["CHANNEL"])
         mylog.info("Number of Channels: %d" % (n_ch))
         
@@ -781,8 +905,12 @@
             # build channel number list associated to array value,
             # there are groups of channels in rmfs with nonzero probabilities
             trueChannel = []
-            for start,nchan in zip(tblhdu.data[k]["F_CHAN"],
-                                   tblhdu.data[k]["N_CHAN"]):
+            f_chan = tblhdu.data[k]["F_CHAN"]
+            n_chan = tblhdu.data[k]["N_CHAN"]
+            if not iterable(f_chan):
+                 f_chan = [f_chan]
+                 n_chan = [n_chan]
+            for start,nchan in zip(f_chan, n_chan):
                 end = start + nchan
                 for j in range(start,end):
                     trueChannel.append(j)
@@ -813,7 +941,8 @@
     @parallel_root_only
     def write_fits_file(self, fitsfile, clobber=False):
         """
-        Write events to a FITS binary table file.
+        Write events to a FITS binary table file with filename *fitsfile*.
+        Set *clobber* to True if you need to overwrite a previous file.
         """
         
         cols = []
@@ -882,16 +1011,34 @@
         tbhdu.writeto(fitsfile, clobber=clobber)
 
     @parallel_root_only    
-    def write_simput_file(self, prefix, clobber=False, e_min=None, e_max=None):
+    def write_simput_file(self, prefix, clobber=False, emin=None, emax=None):
+        r"""
+        Write events to a SIMPUT file that may be read by the SIMX instrument
+        simulator.
+
+        Parameters
+        ----------
+
+        prefix : string
+            The filename prefix.
+        clobber : boolean, optional
+            Set to True to overwrite previous files.
+        e_min : float, optional
+            The minimum energy of the photons to save in keV.
+        e_max : float, optional
+            The maximum energy of the photons to save in keV.
         """
-        Write events to a SIMPUT file.
-        """
-        if e_min is None:
-            e_min = 0.0
-        if e_max is None:
-            e_max = 100.0
 
-        idxs = np.logical_and(self.events["eobs"] >= e_min, self.events["eobs"] <= e_max)
+        if isinstance(self.events["Area"], basestring):
+             mylog.error("Writing SIMPUT files is only supported if you didn't convolve with an ARF.")
+             raise TypeError
+        
+        if emin is None:
+            emin = self.events["eobs"].min()*0.95
+        if emax is None:
+            emax = self.events["eobs"].max()*1.05
+
+        idxs = np.logical_and(self.events["eobs"] >= emin, self.events["eobs"] <= emax)
         flux = erg_per_keV*np.sum(self.events["eobs"][idxs])/self.events["ExposureTime"]/self.events["Area"]
         
         col1 = pyfits.Column(name='ENERGY', format='E',
@@ -923,8 +1070,8 @@
         col1 = pyfits.Column(name='SRC_ID', format='J', array=np.array([1]).astype("int32"))
         col2 = pyfits.Column(name='RA', format='D', array=np.array([self.center[0]]))
         col3 = pyfits.Column(name='DEC', format='D', array=np.array([self.center[1]]))
-        col4 = pyfits.Column(name='E_MIN', format='D', array=np.array([e_min]))
-        col5 = pyfits.Column(name='E_MAX', format='D', array=np.array([e_max]))
+        col4 = pyfits.Column(name='E_MIN', format='D', array=np.array([emin]))
+        col5 = pyfits.Column(name='E_MAX', format='D', array=np.array([emax]))
         col6 = pyfits.Column(name='FLUX', format='D', array=np.array([flux]))
         col7 = pyfits.Column(name='SPECTRUM', format='80A', array=np.array([phfile+"[PHLIST,1]"]))
         col8 = pyfits.Column(name='IMAGE', format='80A', array=np.array([phfile+"[PHLIST,1]"]))
@@ -953,7 +1100,7 @@
     @parallel_root_only
     def write_h5_file(self, h5file):
         """
-        Write an EventList to the HDF5 file given by h5file.
+        Write an EventList to the HDF5 file given by *h5file*.
         """
         f = h5py.File(h5file, "w")
 
@@ -990,8 +1137,20 @@
     @parallel_root_only
     def write_fits_image(self, imagefile, clobber=False,
                          emin=None, emax=None):
-        """
+        r"""
         Generate a image by binning X-ray counts and write it to a FITS file.
+
+        Parameters
+        ----------
+
+        imagefile : string
+            The name of the image file to write.
+        clobber : boolean, optional
+            Set to True to overwrite a previous file.
+        emin : float, optional
+            The minimum energy of the photons to put in the image, in keV.
+        emax : float, optional
+            The maximum energy of the photons to put in the image, in keV.
         """
         if emin is None:
             mask_emin = np.ones((self.num_events), dtype='bool')
@@ -1033,28 +1192,63 @@
         hdu.writeto(imagefile, clobber=clobber)
                                     
     @parallel_root_only
-    def write_spectrum(self, specfile, emin=0.1, emax=10.0, nchan=2000, clobber=False):
+    def write_spectrum(self, specfile, energy_bins=False, emin=0.1,
+                       emax=10.0, nchan=2000, clobber=False):
+        r"""
+        Bin event energies into a spectrum and write it to a FITS binary table. Can bin
+        on energy or channel, the latter only if the photons were convolved with an
+        RMF. In that case, the spectral binning will be determined by the RMF binning.
 
-        if "chan" in self.events:
-            spectype = self.events["ChannelType"]
-            espec = self.events["chan"]
-        else:
+        Parameters
+        ----------
+
+        specfile : string
+            The name of the FITS file to be written.
+        energy_bins : boolean, optional
+            Bin on energy or channel. 
+        emin : float, optional
+            The minimum energy of the spectral bins in keV. Only used if binning on energy.
+        emax : float, optional
+            The maximum energy of the spectral bins in keV. Only used if binning on energy.
+        nchan : integer, optional
+            The number of channels. Only used if binning on energy.
+        """
+
+        if energy_bins:
             spectype = "energy"
             espec = self.events["eobs"]
+            range = (emin, emax)
+            spec, ee = np.histogram(espec, bins=nchan, range=range)
+            bins = 0.5*(ee[1:]+ee[:-1])
+        else:
+            if not self.events.has_key("ChannelType"):
+                mylog.error("These events were not convolved with an RMF. Set energy_bins=True.")
+                raise KeyError
+            spectype = self.events["ChannelType"]
+            espec = self.events[spectype]
+            f = pyfits.open(self.events["RMF"])
+            nchan = int(f[1].header["DETCHANS"])
+            try:
+                cmin = int(f[1].header["TLMIN4"])
+            except KeyError:
+                mylog.warning("Cannot determine minimum allowed value for channel. " +
+                              "Setting to 0, which may be wrong.")
+                cmin = int(0)
+            try:
+                cmax = int(f[1].header["TLMAX4"])
+            except KeyError:
+                mylog.warning("Cannot determine maximum allowed value for channel. " +
+                              "Setting to DETCHANS, which may be wrong.")
+                cmax = int(nchan)
+            f.close()
+            minlength = nchan
+            if cmin == 1: minlength += 1
+            spec = np.bincount(self.events[spectype],minlength=minlength)
+            if cmin == 1: spec = spec[1:]
+            bins = np.arange(nchan).astype("int32")+cmin
             
-        if spectype == "PI":
-            bins = 1024
-            range = (0.5, 1024.5)
-        else:
-            bins = nchan
-            range = (emin, emax)
-            
-        spec, ee = np.histogram(espec, bins=bins, range=range)
-
-        emid = 0.5*(ee[1:]+ee[:-1])
-
-        col1 = pyfits.Column(name='CHANNEL', format='1J', array=np.arange(spec.shape[0], dtype='int32')+1)
-        col2 = pyfits.Column(name=spectype.upper(), format='1D', array=emid)
+        col1 = pyfits.Column(name='CHANNEL', format='1J', array=bins)
+        col2 = pyfits.Column(name=spectype.upper(), format='1D', array=bins.astype("float64"))
         col3 = pyfits.Column(name='COUNTS', format='1J', array=spec.astype("int32"))
         col4 = pyfits.Column(name='COUNT_RATE', format='1D', array=spec/self.events["ExposureTime"])
         
@@ -1063,41 +1257,42 @@
         tbhdu = pyfits.new_table(coldefs)
         tbhdu.update_ext_name("SPECTRUM")
 
-        tbhdu.header.update("DETCHANS", spec.shape[0])
-        tbhdu.header.update("TOTCTS", spec.sum())
-        tbhdu.header.update("EXPOSURE", self.events["ExposureTime"])
-        tbhdu.header.update("LIVETIME", self.events["ExposureTime"])        
-        tbhdu.header.update("CONTENT", spectype)
-        tbhdu.header.update("HDUCLASS", "OGIP")
-        tbhdu.header.update("HDUCLAS1", "SPECTRUM")
-        tbhdu.header.update("HDUCLAS2", "TOTAL")
-        tbhdu.header.update("HDUCLAS3", "TYPE:I")
-        tbhdu.header.update("HDUCLAS4", "COUNT")
-        tbhdu.header.update("HDUVERS", "1.1.0")
-        tbhdu.header.update("HDUVERS1", "1.1.0")
-        tbhdu.header.update("CHANTYPE", spectype)
-        tbhdu.header.update("BACKFILE", "none")
-        tbhdu.header.update("CORRFILE", "none")
-        tbhdu.header.update("POISSERR", True)
-        if self.events.has_key("RMF"):
-            tbhdu.header.update("RESPFILE", self.events["RMF"])
-        else:
-            tbhdu.header.update("RESPFILE", "none")
-        if self.events.has_key("ARF"):
-            tbhdu.header.update("ANCRFILE", self.events["ARF"])
-        else:        
-            tbhdu.header.update("ANCRFILE", "none")
-        if self.events.has_key("Telescope"):
-            tbhdu.header.update("TELESCOP", self.events["Telescope"])
-        else:
-            tbhdu.header.update("TELESCOP", "none")
-        if self.events.has_key("Instrument"):
-            tbhdu.header.update("INSTRUME", self.events["Instrument"])
-        else:
-            tbhdu.header.update("INSTRUME", "none")
-        tbhdu.header.update("AREASCAL", 1.0)
-        tbhdu.header.update("CORRSCAL", 0.0)
-        tbhdu.header.update("BACKSCAL", 1.0)
+        if not energy_bins:
+            tbhdu.header.update("DETCHANS", spec.shape[0])
+            tbhdu.header.update("TOTCTS", spec.sum())
+            tbhdu.header.update("EXPOSURE", self.events["ExposureTime"])
+            tbhdu.header.update("LIVETIME", self.events["ExposureTime"])        
+            tbhdu.header.update("CONTENT", spectype)
+            tbhdu.header.update("HDUCLASS", "OGIP")
+            tbhdu.header.update("HDUCLAS1", "SPECTRUM")
+            tbhdu.header.update("HDUCLAS2", "TOTAL")
+            tbhdu.header.update("HDUCLAS3", "TYPE:I")
+            tbhdu.header.update("HDUCLAS4", "COUNT")
+            tbhdu.header.update("HDUVERS", "1.1.0")
+            tbhdu.header.update("HDUVERS1", "1.1.0")
+            tbhdu.header.update("CHANTYPE", spectype)
+            tbhdu.header.update("BACKFILE", "none")
+            tbhdu.header.update("CORRFILE", "none")
+            tbhdu.header.update("POISSERR", True)
+            if self.events.has_key("RMF"):
+                 tbhdu.header.update("RESPFILE", self.events["RMF"])
+            else:
+                 tbhdu.header.update("RESPFILE", "none")
+            if self.events.has_key("ARF"):
+                tbhdu.header.update("ANCRFILE", self.events["ARF"])
+            else:        
+                tbhdu.header.update("ANCRFILE", "none")
+            if self.events.has_key("Telescope"):
+                tbhdu.header.update("TELESCOP", self.events["Telescope"])
+            else:
+                tbhdu.header.update("TELESCOP", "none")
+            if self.events.has_key("Instrument"):
+                tbhdu.header.update("INSTRUME", self.events["Instrument"])
+            else:
+                tbhdu.header.update("INSTRUME", "none")
+            tbhdu.header.update("AREASCAL", 1.0)
+            tbhdu.header.update("CORRSCAL", 0.0)
+            tbhdu.header.update("BACKSCAL", 1.0)
                                 
         hdulist = pyfits.HDUList([pyfits.PrimaryHDU(), tbhdu])
         


https://bitbucket.org/yt_analysis/yt/commits/611ad31fbbde/
Changeset:   611ad31fbbde
Branch:      yt
User:        jzuhone
Date:        2013-10-11 16:22:49
Summary:     Moved things to a new directory
Affected #:  9 files

diff -r f9e54cd9a76ad809cda2d6388d238876b1ea97c6 -r 611ad31fbbde5f5d9a0ff38c0220516592fc1dc8 yt/analysis_modules/api.py
--- a/yt/analysis_modules/api.py
+++ b/yt/analysis_modules/api.py
@@ -106,7 +106,7 @@
 from .radmc3d_export.api import \
     RadMC3DWriter
 
-from .synthetic_obs.api import \
+from .photon_simulator.api import \
      PhotonList, \
      EventList, \
      XSpecThermalModel, \

diff -r f9e54cd9a76ad809cda2d6388d238876b1ea97c6 -r 611ad31fbbde5f5d9a0ff38c0220516592fc1dc8 yt/analysis_modules/photon_simulator/api.py
--- /dev/null
+++ b/yt/analysis_modules/photon_simulator/api.py
@@ -0,0 +1,21 @@
+"""
+API for yt.analysis_modules.photon_simulator.
+"""
+
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
+from .photon_simulator import \
+     PhotonList, \
+     EventList
+
+from .photon_models import \
+     XSpecThermalModel, \
+     XSpecAbsorbModel, \
+     TableApecModel, \
+     TableAbsorbModel

diff -r f9e54cd9a76ad809cda2d6388d238876b1ea97c6 -r 611ad31fbbde5f5d9a0ff38c0220516592fc1dc8 yt/analysis_modules/photon_simulator/photon_models.py
--- /dev/null
+++ b/yt/analysis_modules/photon_simulator/photon_models.py
@@ -0,0 +1,327 @@
+"""
+Photon emission and absoprtion models for use with the
+photon simulator.
+"""
+
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
+import numpy as np
+import os
+from yt.funcs import *
+import h5py
+try:
+    import astropy.io.fits as pyfits
+except:
+    mylog.warning("You don't have AstroPy installed. The APEC table model won't be available.")
+try:
+    import xspec
+except ImportError:
+    mylog.warning("You don't have PyXSpec installed. Some models won't be available.")
+try:
+    from scipy.integrate import cumtrapz
+    from scipy import stats
+except ImportError:
+    mylog.warning("You don't have SciPy installed. The APEC table model won't be avabilable.")
+    
+from yt.utilities.physical_constants import hcgs, clight, erg_per_keV, amu_cgs
+
+hc = 1.0e8*hcgs*clight/erg_per_keV
+
+class PhotonModel(object):
+
+    def __init__(self, emin, emax, nchan):
+        self.emin = emin
+        self.emax = emax
+        self.nchan = nchan
+        self.ebins = np.linspace(emin, emax, nchan+1)
+        self.de = np.diff(self.ebins)
+        self.emid = 0.5*(self.ebins[1:]+self.ebins[:-1])
+        
+    def prepare(self):
+        pass
+    
+    def get_spectrum(self):
+        pass
+                                                        
+class XSpecThermalModel(PhotonModel):
+    r"""
+    Initialize a thermal gas emission model from PyXspec.
+    
+    Parameters
+    ----------
+
+    model_name : string
+        The name of the thermal emission model.
+    emin : float
+        The minimum energy for the spectral model.
+    emax : float
+        The maximum energy for the spectral model.
+    nchan : integer
+        The number of channels in the spectral model.
+
+    Examples
+    --------
+    >>> mekal_model = XSpecThermalModel("mekal", 0.05, 50.0, 1000)
+    """
+    def __init__(self, model_name, emin, emax, nchan):
+        self.model_name = model_name
+        PhotonModel.__init__(self, emin, emax, nchan)
+        
+    def prepare(self):
+        """
+        Prepare the thermal model for execution.
+        """
+        xspec.Xset.chatter = 0
+        xspec.AllModels.setEnergies("%f %f %d lin" %
+                                    (self.emin, self.emax, self.nchan))
+        self.model = xspec.Model(self.model_name)
+        if self.model_name == "bremss":
+            self.norm = 3.02e-15
+        else:
+            self.norm = 1.0e-14
+        
+    def get_spectrum(self, kT):
+        """
+        Get the thermal emission spectrum given a temperature *kT* in keV. 
+        """
+        m = getattr(self.model,self.model_name)
+        m.kT = kT
+        m.Abundanc = 0.0
+        m.norm = 1.0
+        m.Redshift = 0.0
+        cosmic_spec = self.norm*np.array(self.model.values(0))
+        m.Abundanc = 1.0
+        if self.model_name == "bremss":
+            metal_spec = np.zeros((self.nchan))
+        else:
+            metal_spec = self.norm*np.array(self.model.values(0)) - cosmic_spec
+        return cosmic_spec, metal_spec
+        
+class XSpecAbsorbModel(PhotonModel):
+    r"""
+    Initialize an absorption model from PyXspec.
+    
+    Parameters
+    ----------
+
+    model_name : string
+        The name of the absorption model.
+    nH : float
+        The foreground column density *nH* in units of 10^22 cm^{-2}.
+    emin : float, optional
+        The minimum energy for the spectral model.
+    emax : float, optional
+        The maximum energy for the spectral model.
+    nchan : integer, optional
+        The number of channels in the spectral model.
+
+    Examples
+    --------
+    >>> abs_model = XSpecAbsorbModel("wabs", 0.1)
+    """
+    def __init__(self, model_name, nH, emin=0.01, emax=50.0, nchan=100000):
+        self.model_name = model_name
+        self.nH = nH
+        PhotonModel.__init__(self, emin, emax, nchan)
+        
+    def prepare(self):
+        """
+        Prepare the absorption model for execution.
+        """
+        xspec.Xset.chatter = 0
+        xspec.AllModels.setEnergies("%f %f %d lin" %
+                                    (self.emin, self.emax, self.nchan))
+        self.model = xspec.Model(self.model_name+"*powerlaw")
+        self.model.powerlaw.norm = self.nchan/(self.emax-self.emin)
+        self.model.powerlaw.PhoIndex = 0.0
+
+    def get_spectrum(self):
+        """
+        Get the absorption spectrum.
+        """
+        m = getattr(self.model,self.model_name)
+        m.nH = self.nH
+        return np.array(self.model.values(0))
+
+class TableApecModel(PhotonModel):
+    r"""
+    Initialize a thermal gas emission model from the AtomDB APEC tables
+    available at http://www.atomdb.org. This code borrows heavily from Python
+    routines used to read the APEC tables developed by Adam Foster at the
+    CfA (afoster at cfa.harvard.edu). 
+    
+    Parameters
+    ----------
+
+    apec_root : string
+        The directory root where the APEC model files are stored.
+    emin : float
+        The minimum energy for the spectral model.
+    emax : float
+        The maximum energy for the spectral model.
+    nchan : integer
+        The number of channels in the spectral model.
+    apec_vers : string, optional
+        The version identifier string for the APEC files, e.g.
+        "2.0.2"
+    thermal_broad : boolean, optional
+        Whether to apply thermal broadening to spectral lines. Only should
+        be used if you are attemping to simulate a high-spectral resolution
+        detector.
+
+    Examples
+    --------
+    >>> apec_model = TableApecModel("/Users/jzuhone/Data/atomdb_v2.0.2/", 0.05, 50.0,
+                                    1000, thermal_broad=True)
+    """
+    def __init__(self, apec_root, emin, emax, nchan,
+                 apec_vers="2.0.2", thermal_broad=False):
+        self.apec_root = apec_root
+        self.apec_prefix = "apec_v"+apec_vers
+        self.cocofile = os.path.join(self.apec_root,
+                                     self.apec_prefix+"_coco.fits")
+        self.linefile = os.path.join(self.apec_root,
+                                     self.apec_prefix+"_line.fits")
+        PhotonModel.__init__(self, emin, emax, nchan)
+        self.wvbins = hc/self.ebins[::-1]
+        # H, He, and trace elements
+        self.cosmic_elem = [1,2,3,4,5,9,11,15,17,19,21,22,23,24,25,27,29,30]
+        # Non-trace metals
+        self.metal_elem = [6,7,8,10,12,13,14,16,18,20,26,28]
+        self.thermal_broad = thermal_broad
+        self.A = np.array([0.0,1.00794,4.00262,6.941,9.012182,10.811,
+                           12.0107,14.0067,15.9994,18.9984,20.1797,
+                           22.9898,24.3050,26.9815,28.0855,30.9738,
+                           32.0650,35.4530,39.9480,39.0983,40.0780,
+                           44.9559,47.8670,50.9415,51.9961,54.9380,
+                           55.8450,58.9332,58.6934,63.5460,65.3800])
+        
+    def prepare(self):
+        """
+        Prepare the thermal model for execution.
+        """
+        try:
+            self.line_handle = pyfits.open(self.linefile)
+        except IOError:
+            mylog.error("LINE file %s does not exist" % (self.linefile))
+        try:
+            self.coco_handle = pyfits.open(self.cocofile)
+        except IOError:
+            mylog.error("COCO file %s does not exist" % (self.cocofile))
+        self.Tvals = self.line_handle[1].data.field("kT")
+        self.dTvals = np.diff(self.Tvals)
+        self.minlam = self.wvbins.min()
+        self.maxlam = self.wvbins.max()
+    
+    def _make_spectrum(self, element, tindex):
+        
+        tmpspec = np.zeros((self.nchan))
+        
+        i = np.where((self.line_handle[tindex].data.field('element')==element) &
+                     (self.line_handle[tindex].data.field('lambda') > self.minlam) &
+                     (self.line_handle[tindex].data.field('lambda') < self.maxlam))[0]
+
+        vec = np.zeros((self.nchan))
+        E0 = hc/self.line_handle[tindex].data.field('lambda')[i]
+        amp = self.line_handle[tindex].data.field('epsilon')[i]
+        if self.thermal_broad:
+            vec = np.zeros((self.nchan))
+            sigma = E0*np.sqrt(self.Tvals[tindex]*erg_per_keV/(self.A[element]*amu_cgs))/clight
+            for E, sig, a in zip(E0, sigma, amp):
+                cdf = stats.norm(E,sig).cdf(self.ebins)
+                vec += np.diff(cdf)*a
+        else:
+            ie = np.searchsorted(self.ebins, E0, side='right')-1
+            for i,a in zip(ie,amp): vec[i] += a
+        tmpspec += vec
+
+        ind = np.where((self.coco_handle[tindex].data.field('Z')==element) &
+                       (self.coco_handle[tindex].data.field('rmJ')==0))[0]
+        if len(ind)==0:
+            return tmpspec
+        else:
+            ind=ind[0]
+                                                    
+        n_cont=self.coco_handle[tindex].data.field('N_Cont')[ind]
+        e_cont=self.coco_handle[tindex].data.field('E_Cont')[ind][:n_cont]
+        continuum = self.coco_handle[tindex].data.field('Continuum')[ind][:n_cont]
+
+        tmpspec += np.interp(self.emid, e_cont, continuum)*self.de
+        
+        n_pseudo=self.coco_handle[tindex].data.field('N_Pseudo')[ind]
+        e_pseudo=self.coco_handle[tindex].data.field('E_Pseudo')[ind][:n_pseudo]
+        pseudo = self.coco_handle[tindex].data.field('Pseudo')[ind][:n_pseudo]
+        
+        tmpspec += np.interp(self.emid, e_pseudo, pseudo)*self.de
+        
+        return tmpspec
+
+    def get_spectrum(self, kT):
+        """
+        Get the thermal emission spectrum given a temperature *kT* in keV. 
+        """
+        cspec_l = np.zeros((self.nchan))
+        mspec_l = np.zeros((self.nchan))
+        cspec_r = np.zeros((self.nchan))
+        mspec_r = np.zeros((self.nchan))
+        tindex = np.searchsorted(self.Tvals, kT)-1
+        dT = (kT-self.Tvals[tindex])/self.dTvals[tindex]
+        # First do H,He, and trace elements
+        for elem in self.cosmic_elem:
+            cspec_l += self._make_spectrum(elem, tindex+2)
+            cspec_r += self._make_spectrum(elem, tindex+3)            
+        # Next do the metals
+        for elem in self.metal_elem:
+            mspec_l += self._make_spectrum(elem, tindex+2)
+            mspec_r += self._make_spectrum(elem, tindex+3)
+        cosmic_spec = cspec_l*(1.-dT)+cspec_r*dT
+        metal_spec = mspec_l*(1.-dT)+mspec_r*dT        
+        return cosmic_spec, metal_spec
+
+class TableAbsorbModel(PhotonModel):
+    r"""
+    Initialize an absorption model from a table stored in an HDF5 file.
+    
+    Parameters
+    ----------
+
+    filename : string
+        The name of the table file.
+    nH : float
+        The foreground column density *nH* in units of 10^22 cm^{-2}.
+
+    Examples
+    --------
+    >>> abs_model = XSpecAbsorbModel("wabs", 0.1)
+    """
+
+    def __init__(self, filename, nH):
+        if not os.path.exists(filename):
+            raise IOError("File does not exist: %s." % filename)
+        self.filename = filename
+        f = h5py.File(self.filename,"r")
+        emin = f["energy"][:].min()
+        emax = f["energy"][:].max()
+        self.sigma = f["cross_section"][:]
+        nchan = self.sigma.shape[0]
+        f.close()
+        PhotonModel.__init__(self, emin, emax, nchan)
+        self.nH = nH*1.0e22
+        
+    def prepare(self):
+        """
+        Prepare the absorption model for execution.
+        """
+        pass
+        
+    def get_spectrum(self):
+        """
+        Get the absorption spectrum.
+        """
+        return np.exp(-self.sigma*self.nH)

diff -r f9e54cd9a76ad809cda2d6388d238876b1ea97c6 -r 611ad31fbbde5f5d9a0ff38c0220516592fc1dc8 yt/analysis_modules/photon_simulator/photon_simulator.py
--- /dev/null
+++ b/yt/analysis_modules/photon_simulator/photon_simulator.py
@@ -0,0 +1,1299 @@
+"""
+Classes for generating lists of photons and detected events
+"""
+
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
+import numpy as np
+from numpy.testing import assert_allclose
+from yt.funcs import *
+from yt.utilities.physical_constants import mp, clight, cm_per_kpc, \
+     cm_per_mpc, cm_per_km, K_per_keV, erg_per_keV
+from yt.utilities.cosmology import Cosmology
+from yt.utilities.orientation import Orientation
+from yt.utilities.parallel_tools.parallel_analysis_interface import \
+     communication_system, parallel_root_only, get_mpi_type, parallel_capable
+
+import h5py
+
+try:
+    import astropy.io.fits as pyfits
+    import astropy.wcs as pywcs
+except ImportError:
+    mylog.warning("You don't have AstroPy installed. " +
+                  "You will be unable to write FITS events files or images.")
+                   
+N_TBIN = 10000
+TMIN = 8.08e-2
+TMAX = 50.
+
+comm = communication_system.communicators[-1]
+        
+class PhotonList(object):
+
+    def __init__(self, photons=None, cosmo=None, p_bins=None):
+        if photons is None: photons = {}
+        self.photons = photons
+        self.cosmo = cosmo
+        self.p_bins = p_bins
+        self.num_cells = len(photons["x"])
+        
+    def keys(self):
+        return self.photons.keys()
+    
+    def items(self):
+        ret = []
+        for k, v in self.photons.items():
+            if k == "Energy":
+                ret.append((k, self[k]))
+            else:
+                ret.append((k,v))
+        return ret
+    
+    def values(self):
+        ret = []
+        for k, v in self.photons.items():
+            if k == "Energy":
+                ret.append(self[k])
+            else:
+                ret.append(v)
+        return ret
+                                
+    def __getitem__(self, key):
+        if key == "Energy":
+            return [self.photons["Energy"][self.p_bins[i]:self.p_bins[i+1]]
+                    for i in xrange(self.num_cells)]
+        else:
+            return self.photons[key]
+    
+    @classmethod
+    def from_file(cls, filename):
+        r"""
+        Initialize a PhotonList from the HDF5 file *filename*.
+        """
+        photons = {}
+
+        f = h5py.File(filename, "r")
+
+        photons["FiducialExposureTime"] = f["/fid_exp_time"].value
+        photons["FiducialArea"] = f["/fid_area"].value
+        photons["FiducialRedshift"] = f["/fid_redshift"].value
+        photons["FiducialAngularDiameterDistance"] = f["/fid_d_a"].value
+        photons["DomainDimension"] = f["/domain_dimension"].value
+        photons["HubbleConstant"] = f["/hubble"].value
+        photons["OmegaMatter"] = f["/omega_matter"].value
+        photons["OmegaLambda"] = f["/omega_lambda"].value
+
+        num_cells = f["/x"][:].shape[0]
+        start_c = comm.rank*num_cells/comm.size
+        end_c = (comm.rank+1)*num_cells/comm.size
+        
+        photons["x"] = f["/x"][start_c:end_c]
+        photons["y"] = f["/y"][start_c:end_c]
+        photons["z"] = f["/z"][start_c:end_c]
+        photons["dx"] = f["/dx"][start_c:end_c]
+        photons["vx"] = f["/vx"][start_c:end_c]
+        photons["vy"] = f["/vy"][start_c:end_c]
+        photons["vz"] = f["/vz"][start_c:end_c]
+
+        n_ph = f["/num_photons"][:]
+        
+        if comm.rank == 0:
+            start_e = np.uint64(0)
+        else:
+            start_e = n_ph[:start_c].sum()
+        end_e = start_e + np.uint64(n_ph[start_c:end_c].sum())
+
+        photons["NumberOfPhotons"] = n_ph[start_c:end_c]
+
+        p_bins = np.cumsum(photons["NumberOfPhotons"])
+        p_bins = np.insert(p_bins, 0, [np.uint64(0)])
+        
+        photons["Energy"] = f["/energy"][start_e:end_e]
+        
+        f.close()
+
+        cosmo = Cosmology(HubbleConstantNow=photons["HubbleConstant"],
+                          OmegaMatterNow=photons["OmegaMatter"],
+                          OmegaLambdaNow=photons["OmegaLambda"])
+        
+        return cls(photons=photons, cosmo=cosmo, p_bins=p_bins)
+
+    @classmethod
+    def from_thermal_model(cls, data_source, redshift, area,
+                           exp_time, emission_model, center="c",
+                           X_H=0.75, Zmet=0.3, dist=None, cosmology=None):
+        r"""
+        Initialize a PhotonList from a thermal plasma. 
+
+        Parameters
+        ----------
+
+        data_source : `yt.data_objects.api.AMRData`
+            The data source from which the photons will be generated.
+        redshift : float
+            The cosmological redshift for the photons.
+        area : float
+            The collecting area to determine the number of photons in cm^2.
+        exp_time : float
+            The exposure time to determine the number of photons in seconds.
+        emission_model : `yt.analysis_modules.photon_simulator.PhotonModel`
+            The thermal emission model from which to draw the photon samples
+        center : string or array_like, optional
+            The origin of the photons. Accepts "c", "max", or a coordinate. 
+        X_H : float, optional
+            The hydrogen mass fraction.
+        Zmet : float, optional
+            The metallicity of the gas. If there is a "Metallicity" field
+            this parameter is ignored.
+        dist : float, optional
+            The angular diameter distance in Mpc, used for nearby sources.
+            This may be optionally supplied instead of it being determined
+            from the *redshift* and given *cosmology*.
+        cosmology : `yt.utilities.cosmology.Cosmology`, optional
+            Cosmological information. If not supplied, it assumes \LambdaCDM with
+            the default yt parameters.
+
+        Examples
+        --------
+
+        >>> redshift = 0.1
+        >>> area = 6000.0
+        >>> time = 2.0e5
+        >>> sp = pf.h.sphere("c", (1.0, "mpc"))
+        >>> apec_model = XSpecThermalModel("apec", 0.05, 50.0, 1000)
+        >>> my_photons = PhotonList.from_thermal_model(sp, redshift, area,
+        ...                                            time, apec_model)
+
+        """
+        pf = data_source.pf
+                
+        vol_scale = pf.units["cm"]**(-3)/np.prod(pf.domain_width)
+
+        if cosmology is None:
+            cosmo = Cosmology()
+        else:
+            cosmo = cosmology
+        if dist is None:
+            D_A = cosmo.AngularDiameterDistance(0.0,redshift)*cm_per_mpc
+        else:
+            D_A = dist*cm_per_mpc
+            redshift = 0.0
+        dist_fac = 1.0/(4.*np.pi*D_A*D_A*(1.+redshift)**3)
+        
+        num_cells = data_source["Temperature"].shape[0]
+        start_c = comm.rank*num_cells/comm.size
+        end_c = (comm.rank+1)*num_cells/comm.size
+
+        kT = data_source["Temperature"][start_c:end_c].copy()/K_per_keV
+        vol = data_source["CellVolume"][start_c:end_c].copy()
+        dx = data_source["dx"][start_c:end_c].copy()
+        EM = (data_source["Density"][start_c:end_c].copy()/mp)**2
+        EM *= 0.5*(1.+X_H)*X_H*vol
+
+        print EM.sum()
+        
+        data_source.clear_data()
+        
+        x = data_source["x"][start_c:end_c].copy()
+        y = data_source["y"][start_c:end_c].copy()
+        z = data_source["z"][start_c:end_c].copy()
+
+        data_source.clear_data()
+                
+        vx = data_source["x-velocity"][start_c:end_c].copy()
+        vy = data_source["y-velocity"][start_c:end_c].copy()
+        vz = data_source["z-velocity"][start_c:end_c].copy()
+
+        if isinstance(Zmet, basestring):
+            metalZ = data_source[zmet][start_c:end_c].copy()
+        else:
+            metalZ = Zmet*np.ones(EM.shape)
+            
+        data_source.clear_data()
+                
+        idxs = np.argsort(kT)
+        dshape = idxs.shape
+
+        if center == "c":
+            src_ctr = pf.domain_center
+        elif center == "max":
+            src_ctr = pf.h.find_max("Density")[-1]
+        elif iterable(center):
+            src_ctr = center
+                    
+        kT_bins = np.linspace(TMIN, max(kT[idxs][-1], TMAX), num=N_TBIN+1)
+        dkT = kT_bins[1]-kT_bins[0]
+        kT_idxs = np.digitize(kT[idxs], kT_bins)
+        kT_idxs = np.minimum(np.maximum(1, kT_idxs), N_TBIN) - 1
+        bcounts = np.bincount(kT_idxs).astype("int")
+        bcounts = bcounts[bcounts > 0]
+        n = int(0)
+        bcell = []
+        ecell = []
+        for bcount in bcounts:
+            bcell.append(n)
+            ecell.append(n+bcount)
+            n += bcount
+        kT_idxs = np.unique(kT_idxs)
+
+        emission_model.prepare()
+        energy = emission_model.ebins
+              
+        cell_em = EM[idxs]*vol_scale
+        cell_vol = vol[idxs]*vol_scale
+        
+        number_of_photons = np.zeros(dshape, dtype='uint64')
+        energies = []
+        
+        u = np.random.random(cell_em.shape)
+        
+        pbar = get_pbar("Generating Photons", dshape[0])
+
+        for i, ikT in enumerate(kT_idxs):
+            
+            ncells = int(bcounts[i])
+            ibegin = bcell[i]
+            iend = ecell[i]
+            kT = kT_bins[ikT] + 0.5*dkT
+            
+            em_sum_c = cell_em[ibegin:iend].sum()
+            em_sum_m = (metalZ[ibegin:iend]*cell_em[ibegin:iend]).sum() 
+
+            cspec, mspec = emission_model.get_spectrum(kT)
+            print (cspec+0.3*mspec).max(), kT
+            cspec *= dist_fac*em_sum_c/vol_scale
+            mspec *= dist_fac*em_sum_m/vol_scale
+            
+            cumspec_c = np.cumsum(cspec)
+            counts_c = cumspec_c[:]/cumspec_c[-1]
+            counts_c = np.insert(counts_c, 0, 0.0)
+            tot_ph_c = cumspec_c[-1]*area*exp_time
+
+            cumspec_m = np.cumsum(mspec)
+            counts_m = cumspec_m[:]/cumspec_m[-1]
+            counts_m = np.insert(counts_m, 0, 0.0)
+            tot_ph_m = cumspec_m[-1]*area*exp_time
+            
+            for icell in xrange(ibegin, iend):
+                
+                cell_norm_c = tot_ph_c*cell_em[icell]/em_sum_c
+                cell_n_c = np.uint64(cell_norm_c) + np.uint64(np.modf(cell_norm_c)[0] >= u[icell])
+
+                cell_norm_m = tot_ph_m*metalZ[icell]*cell_em[icell]/em_sum_m
+                cell_n_m = np.uint64(cell_norm_m) + np.uint64(np.modf(cell_norm_m)[0] >= u[icell])
+                                                
+                cell_n = cell_n_c + cell_n_m
+                
+                if cell_n > 0:
+                    number_of_photons[icell] = cell_n                    
+                    randvec_c = np.random.uniform(size=cell_n_c)
+                    randvec_c.sort()
+                    randvec_m = np.random.uniform(size=cell_n_m)
+                    randvec_m.sort()
+                    cell_e_c = np.interp(randvec_c, counts_c, energy)
+                    cell_e_m = np.interp(randvec_m, counts_m, energy)
+                    energies.append(np.concatenate([cell_e_c,cell_e_m]))
+                    
+                pbar.update(icell)
+            
+        pbar.finish()
+
+        active_cells = number_of_photons > 0
+        idxs = idxs[active_cells]
+        
+        photons = {}
+        photons["x"] = (x[idxs]-src_ctr[0])*pf.units["kpc"]
+        photons["y"] = (y[idxs]-src_ctr[1])*pf.units["kpc"]
+        photons["z"] = (z[idxs]-src_ctr[2])*pf.units["kpc"]
+        photons["vx"] = vx[idxs]/cm_per_km
+        photons["vy"] = vy[idxs]/cm_per_km
+        photons["vz"] = vz[idxs]/cm_per_km
+        photons["dx"] = dx[idxs]*pf.units["kpc"]
+        photons["NumberOfPhotons"] = number_of_photons[active_cells]
+        photons["Energy"] = np.concatenate(energies)
+                
+        photons["FiducialExposureTime"] = exp_time
+        photons["FiducialArea"] = area
+        photons["FiducialRedshift"] = redshift
+        photons["FiducialAngularDiameterDistance"] = D_A/cm_per_mpc
+        photons["DomainDimension"] = np.max((pf.domain_dimensions*(2**pf.h.max_level)))
+        photons["HubbleConstant"] = cosmo.HubbleConstantNow
+        photons["OmegaMatter"] = cosmo.OmegaMatterNow
+        photons["OmegaLambda"] = cosmo.OmegaLambdaNow
+
+        p_bins = np.cumsum(photons["NumberOfPhotons"])
+        p_bins = np.insert(p_bins, 0, [np.uint64(0)])
+        
+        return cls(photons=photons, cosmo=cosmo, p_bins=p_bins)
+
+    @classmethod
+    def from_user_model(cls, data_source, redshift, area,
+                        exp_time, user_function, parameters=None,
+                        dist=None, cosmology=None):
+        """
+        Initialize a PhotonList from a user-provided model.  
+
+        Parameters
+        ----------
+
+        data_source : `yt.data_objects.api.AMRData`
+            The data source from which the photons will be generated.
+        redshift : float
+            The cosmological redshift for the photons.
+        area : float
+            The collecting area to determine the number of photons in cm^2.
+        exp_time : float
+            The exposure time to determine the number of photons in seconds.
+        user_function : function
+            A function that takes the *data_source* and any parameters and
+            generates the photons. 
+        parameters : dict, optional
+            A dictionary of parameters to be passed to the user function. 
+        dist : float, optional
+            The angular diameter distance in Mpc, used for nearby sources.
+            This may be optionally supplied instead of it being determined
+            from the *redshift* and given *cosmology*.
+        cosmology : `yt.utilities.cosmology.Cosmology`, optional
+            Cosmological information. If not supplied, it assumes \LambdaCDM with
+            the default yt parameters.
+
+        Examples
+        --------
+
+        >>> def powerlaw_func(source, redshift, area, exp_time, D_A, norm, index, E0):
+        ...
+        ...     spec = norm*source["Density"]*(/E0
+        >>> redshift = 0.02
+        >>> area = 6000.0
+        >>> time = 2.0e5
+        >>> dd = pf.h.all_data
+        >>> my_photons = PhotonList.from_user_model(dd, redshift, area,
+        ...                                         time, powerlaw_func)
+
+        """
+
+        pf = data_source.pf
+
+        if parameters is None:
+             parameters = {}
+        if cosmology is None:
+            cosmo = Cosmology()
+        else:
+            cosmo = cosmology
+        if dist is None:
+            D_A = cosmo.AngularDiameterDistance(0.0,redshift)*cm_per_mpc
+        else:
+            D_A = dist*cm_per_mpc
+            redshift = 0.0
+                    
+        photons = user_function(data_source, redshift, area,
+                                exp_time, D_A, parameters)
+        
+        photons["FiducialExposureTime"] = exp_time
+        photons["FiducialArea"] = area
+        photons["FiducialRedshift"] = redshift
+        photons["FiducialAngularDiameterDistance"] = D_A
+        photons["DomainDimension"] = np.max((pf.domain_dimensions*(2**pf.h.max_level)))
+        photons["HubbleConstant"] = cosmo.HubbleConstantNow
+        photons["OmegaMatter"] = cosmo.OmegaMatterNow
+        photons["OmegaLambda"] = cosmo.OmegaLambdaNow
+
+        p_bins = np.cumsum(photons["NumberOfPhotons"])
+        p_bins = np.insert(p_bins, 0, [np.uint64(0)])
+                        
+        return cls(photons=photons, cosmo=cosmo, p_bins=p_bins)
+        
+    def write_h5_file(self, photonfile):
+        """
+        Write the photons to the HDF5 file *photonfile*.
+        """
+        if parallel_capable:
+            
+            mpi_long = get_mpi_type("int64")
+            mpi_double = get_mpi_type("float64")
+        
+            local_num_cells = len(self.photons["x"])
+            sizes_c = comm.comm.gather(local_num_cells, root=0)
+            
+            local_num_photons = self.photons["NumberOfPhotons"].sum()
+            sizes_p = comm.comm.gather(local_num_photons, root=0)
+            
+            if comm.rank == 0:
+                num_cells = sum(sizes_c)
+                num_photons = sum(sizes_p)        
+                disps_c = [sum(sizes_c[:i]) for i in range(len(sizes_c))]
+                disps_p = [sum(sizes_p[:i]) for i in range(len(sizes_p))]
+                x = np.zeros((num_cells))
+                y = np.zeros((num_cells))
+                z = np.zeros((num_cells))
+                vx = np.zeros((num_cells))
+                vy = np.zeros((num_cells))
+                vz = np.zeros((num_cells))
+                dx = np.zeros((num_cells))
+                n_ph = np.zeros((num_cells), dtype="uint64")
+                e = np.zeros((num_photons))
+            else:
+                sizes_c = []
+                sizes_p = []
+                disps_c = []
+                disps_p = []
+                x = np.empty([])
+                y = np.empty([])
+                z = np.empty([])
+                vx = np.empty([])
+                vy = np.empty([])
+                vz = np.empty([])
+                dx = np.empty([])
+                n_ph = np.empty([])
+                e = np.empty([])
+                                                
+            comm.comm.Gatherv([self.photons["x"], local_num_cells, mpi_double],
+                              [x, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["y"], local_num_cells, mpi_double],
+                              [y, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["z"], local_num_cells, mpi_double],
+                              [z, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["vx"], local_num_cells, mpi_double],
+                              [vx, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["vy"], local_num_cells, mpi_double],
+                              [vy, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["vz"], local_num_cells, mpi_double],
+                              [vz, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["dx"], local_num_cells, mpi_double],
+                              [dx, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["NumberOfPhotons"], local_num_cells, mpi_long],
+                              [n_ph, (sizes_c, disps_c), mpi_long], root=0)
+            comm.comm.Gatherv([self.photons["Energy"], local_num_photons, mpi_double],
+                              [e, (sizes_p, disps_p), mpi_double], root=0) 
+
+        else:
+
+            x = self.photons["x"]
+            y = self.photons["y"]
+            z = self.photons["z"]
+            vx = self.photons["vx"]
+            vy = self.photons["vy"]
+            vz = self.photons["vz"]
+            dx = self.photons["dx"]
+            n_ph = self.photons["NumberOfPhotons"]
+            e = self.photons["Energy"]
+                                                
+        if comm.rank == 0:
+            
+            f = h5py.File(photonfile, "w")
+
+            # Scalars
+       
+            f.create_dataset("fid_area", data=self.photons["FiducialArea"])
+            f.create_dataset("fid_exp_time", data=self.photons["FiducialExposureTime"])
+            f.create_dataset("fid_redshift", data=self.photons["FiducialRedshift"])
+            f.create_dataset("hubble", data=self.photons["HubbleConstant"])
+            f.create_dataset("omega_matter", data=self.photons["OmegaMatter"])
+            f.create_dataset("omega_lambda", data=self.photons["OmegaLambda"])
+            f.create_dataset("fid_d_a", data=self.photons["FiducialAngularDiameterDistance"])
+            f.create_dataset("domain_dimension", data=self.photons["DomainDimension"])
+
+            # Arrays
+
+            f.create_dataset("x", data=x)
+            f.create_dataset("y", data=y)
+            f.create_dataset("z", data=z)
+            f.create_dataset("vx", data=vx)
+            f.create_dataset("vy", data=vy)
+            f.create_dataset("vz", data=vz)
+            f.create_dataset("dx", data=dx)
+            f.create_dataset("num_photons", data=n_ph)
+            f.create_dataset("energy", data=e)
+
+            f.close()
+
+        comm.barrier()
+
+    def project_photons(self, L, area_new=None, texp_new=None, 
+                        redshift_new=None, dist_new=None,
+                        absorb_model=None, psf_sigma=None,
+                        sky_center=None):
+        r"""
+        Projects photons onto an image plane given a line of sight.
+
+        Parameters
+        ----------
+
+        L : array_like
+            Normal vector to the plane of projection.
+        area_new : float or filename, optional
+            New value for the effective area of the detector. 
+            Either a single float value or a standard ARF file
+            containing the effective area as a function of energy.
+        texp_new : float, optional
+            The new value for the exposure time.
+        redshift_new : float, optional
+            The new value for the cosmological redshift.
+        dist_new : float, optional
+            The new value for the angular diameter distance, used for nearby
+            objects. If this is not set it will be determined from the cosmological
+            redshift.
+        absorb_model : 'yt.analysis_modules.photon_simulator.PhotonModel`, optional
+            A model for galactic absorption.
+        psf_sigma : float, optional
+            Quick-and-dirty psf simulation using Gaussian smoothing with
+            standard deviation *psf_sigma* in degrees. 
+        sky_center : array_like, optional
+            Center RA, Dec of the events in degrees.
+
+        Examples
+        --------
+        >>> L = np.array([0.1,-0.2,0.3])
+        >>> events = my_photons.project_photons(L, area_new="sim_arf.fits",
+        ...                                     redshift_new=0.05,
+        ...                                     psf_sigma=0.01)
+        """
+
+        if redshift_new is not None and dist_new is not None:
+            mylog.error("You may specify a new redshift or distance, "+
+                        "but not both!")
+        
+        if sky_center is None:
+             sky_center = np.array([30.,45.])
+
+        dx = self.photons["dx"]
+        nx = self.photons["DomainDimension"]
+        if psf_sigma is not None:
+             psf_sigma /= 3600.
+
+        L = np.array(L)
+        L /= np.sqrt(np.dot(L, L))
+        vecs = np.identity(3)
+        t = np.cross(L, vecs).sum(axis=1)
+        ax = t.argmax()
+        north = np.cross(L, vecs[ax,:]).ravel()
+        orient = Orientation(L, north_vector=north)
+        
+        x_hat = orient.unit_vectors[0]
+        y_hat = orient.unit_vectors[1]
+        z_hat = orient.unit_vectors[2]
+
+        n_ph = self.photons["NumberOfPhotons"]
+        num_cells = len(n_ph)
+        n_ph_tot = n_ph.sum()
+        
+        eff_area = None
+        
+        if (texp_new is None and area_new is None and
+            redshift_new is None and dist_new is None):
+            my_n_obs = n_ph_tot
+            zobs = self.photons["FiducialRedshift"]
+            D_A = self.photons["FiducialAngularDiameterDistance"]*1000.
+        else:
+            if texp_new is None:
+                Tratio = 1.
+            else:
+                Tratio = texp_new/self.photons["FiducialExposureTime"]
+            if area_new is None:
+                Aratio = 1.
+            elif isinstance(area_new, basestring):
+                mylog.info("Using energy-dependent effective area.")
+                f = pyfits.open(area_new)
+                elo = f[1].data.field("ENERG_LO")
+                ehi = f[1].data.field("ENERG_HI")
+                eff_area = f[1].data.field("SPECRESP")
+                f.close()
+                Aratio = eff_area.max()/self.photons["FiducialArea"]
+            else:
+                mylog.info("Using constant effective area.")
+                Aratio = area_new/self.photons["FiducialArea"]
+            if redshift_new is None and dist_new is None:
+                Dratio = 1.
+                zobs = self.photons["FiducialRedshift"]
+                D_A = self.photons["FiducialAngularDiameterDistance"]*1000.                    
+            else:
+                if redshift_new is None:
+                    zobs = 0.0
+                    D_A = dist_new*1000.
+                else:
+                    zobs = redshift_new
+                    D_A = self.cosmo.AngularDiameterDistance(0.0,zobs)*1000.
+                fid_D_A = self.photons["FiducialAngularDiameterDistance"]*1000.
+                Dratio = fid_D_A*fid_D_A*(1.+self.photons["FiducialRedshift"]**3) / \
+                         (D_A*D_A*(1.+zobs)**3)
+            fak = Aratio*Tratio*Dratio
+            if fak > 1:
+                raise ValueError("Spectrum scaling factor = %g, cannot be greater than unity." % (fak))
+            my_n_obs = np.uint64(n_ph_tot*fak)
+
+        n_obs_all = comm.mpi_allreduce(my_n_obs)
+        if comm.rank == 0: mylog.info("Total number of photons to use: %d" % (n_obs_all))
+        
+        x = np.random.uniform(low=-0.5,high=0.5,size=my_n_obs)
+        y = np.random.uniform(low=-0.5,high=0.5,size=my_n_obs)
+        z = np.random.uniform(low=-0.5,high=0.5,size=my_n_obs)
+                    
+        vz = self.photons["vx"]*z_hat[0] + \
+             self.photons["vy"]*z_hat[1] + \
+             self.photons["vz"]*z_hat[2]
+        shift = -vz*cm_per_km/clight
+        shift = np.sqrt((1.-shift)/(1.+shift))
+
+        if my_n_obs == n_ph_tot:
+            idxs = np.arange(my_n_obs,dtype='uint64')
+        else:
+            idxs = np.random.permutation(n_ph_tot)[:my_n_obs].astype("uint64")
+        obs_cells = np.searchsorted(self.p_bins, idxs, side='right')-1
+        delta = dx[obs_cells]
+
+        x *= delta
+        y *= delta
+        z *= delta
+        x += self.photons["x"][obs_cells]
+        y += self.photons["y"][obs_cells]
+        z += self.photons["z"][obs_cells]  
+        eobs = self.photons["Energy"][idxs]*shift[obs_cells]
+            
+        xsky = x*x_hat[0] + y*x_hat[1] + z*x_hat[2]
+        ysky = x*y_hat[0] + y*y_hat[1] + z*y_hat[2]
+        eobs /= (1.+zobs)
+        
+        if absorb_model is None:
+            not_abs = np.ones(eobs.shape, dtype='bool')
+        else:
+            mylog.info("Absorbing.")
+            absorb_model.prepare()
+            emid = absorb_model.emid
+            aspec = absorb_model.get_spectrum()
+            absorb = np.interp(eobs, emid, aspec, left=0.0, right=0.0)
+            randvec = aspec.max()*np.random.random(eobs.shape)
+            not_abs = randvec < absorb
+        
+        if eff_area is None:
+            detected = np.ones(eobs.shape, dtype='bool')
+        else:
+            mylog.info("Applying energy-dependent effective area.")
+            earf = 0.5*(elo+ehi)
+            earea = np.interp(eobs, earf, eff_area, left=0.0, right=0.0)
+            randvec = eff_area.max()*np.random.random(eobs.shape)
+            detected = randvec < earea
+        
+        detected = np.logical_and(not_abs, detected)
+                    
+        events = {}
+
+        dtheta = dx.min()/D_A
+        
+        events["xpix"] = xsky[detected]/dx.min() + 0.5*(nx+1) 
+        events["ypix"] = ysky[detected]/dx.min() + 0.5*(nx+1)
+        if psf_sigma is not None:
+            events["xpix"] += np.random.normal(sigma=psf_sigma/dtheta)
+            events["ypix"] += np.random.normal(sigma=psf_sigma/dtheta)
+        w = pywcs.WCS(naxis=2)
+        w.wcs.crpix = [0.5*(nx+1)]*2
+        w.wcs.crval = sky_center
+        w.wcs.cdelt = [-dtheta, dtheta]
+        w.wcs.ctype = ["RA---TAN","DEC--TAN"]
+        w.wcs.cunit = ["deg"]*2
+        events["xsky"], events["ysky"] = w.wcs_pix2world(events["xpix"], events["ypix"],
+                                                         1, ra_dec_order=True)
+        events["eobs"] = eobs[detected]
+
+        events = comm.par_combine_object(events, datatype="dict", op="cat")
+        
+        num_events = len(events["xsky"])
+            
+        if comm.rank == 0: mylog.info("Total number of observed photons: %d" % (num_events))
+                        
+        if texp_new is None:
+            events["ExposureTime"] = self.photons["FiducialExposureTime"]
+        else:
+            events["ExposureTime"] = texp_new
+        if area_new is None:
+            events["Area"] = self.photons["FiducialArea"]
+        else:
+            events["Area"] = area_new
+        events["Redshift"] = zobs
+        events["AngularDiameterDistance"] = D_A/1000.
+        if isinstance(area_new, basestring):
+            events["ARF"] = area_new
+        events["sky_center"] = np.array(sky_center)
+        events["pix_center"] = np.array([0.5*(nx+1)]*2)
+        events["dtheta"] = dtheta
+        
+        return EventList(events)
+
+class EventList(object) :
+
+    def __init__(self, events = None) :
+
+        if events is None : events = {}
+        self.events = events
+        self.num_events = events["xsky"].shape[0]
+        
+    def keys(self):
+        return self.events.keys()
+
+    def items(self):
+        return self.events.items()
+
+    def values(self):
+        return self.events.values()
+    
+    def __getitem__(self,key):
+        return self.events[key]
+        
+    @classmethod
+    def from_h5_file(cls, h5file):
+        """
+        Initialize an EventList from a HDF5 file with filename *h5file*.
+        """
+        events = {}
+        
+        f = h5py.File(h5file, "r")
+
+        events["ExposureTime"] = f["/exp_time"].value
+        events["Area"] = f["/area"].value
+        events["Redshift"] = f["/redshift"].value
+        events["AngularDiameterDistance"] = f["/d_a"].value
+        if "rmf" in f:
+            events["RMF"] = f["/rmf"].value
+        if "arf" in f:
+            events["ARF"] = f["/arf"].value
+        if "channel_type" in f:
+            events["ChannelType"] = f["/channel_type"].value
+        if "telescope" in f:
+            events["Telescope"] = f["/telescope"].value
+        if "instrument" in f:
+            events["Instrument"] = f["/instrument"].value
+                            
+        events["xsky"] = f["/xsky"][:]
+        events["ysky"] = f["/ysky"][:]
+        events["xpix"] = f["/xpix"][:]
+        events["ypix"] = f["/ypix"][:]
+        events["eobs"] = f["/eobs"][:]
+        if "pi" in f:
+            events["PI"] = f["/pi"][:]
+        if "pha" in f:
+            events["PHA"] = f["/pha"][:]
+        events["sky_center"] = f["/sky_center"][:]
+        events["dtheta"] = f["/dtheta"].value
+        events["pix_center"] = f["/pix_center"][:]
+        
+        f.close()
+        
+        return cls(events)
+
+    @classmethod
+    def from_fits_file(cls, fitsfile):
+        """
+        Initialize an EventList from a FITS file with filename *fitsfile*.
+        """
+        hdulist = pyfits.open(fitsfile)
+
+        tblhdu = hdulist["EVENTS"]
+        
+        events = {}
+
+        events["ExposureTime"] = tblhdu.header["EXPOSURE"]
+        events["Area"] = tblhdu.header["AREA"]
+        events["Redshift"] = tblhdu.header["REDSHIFT"]
+        events["AngularDiameterDistance"] = tblhdu.header["D_A"]
+        if "RMF" in tblhdu.header:
+            events["RMF"] = tblhdu["RMF"]
+        if "ARF" in tblhdu.header:
+            events["ARF"] = tblhdu["ARF"]
+        if "CHANTYPE" in tblhdu.header:
+            events["ChannelType"] = tblhdu["CHANTYPE"]
+        if "TELESCOP" in tblhdu.header:
+            events["Telescope"] = tblhdu["TELESCOP"]
+        if "INSTRUME" in tblhdu.header:
+            events["Instrument"] = tblhdu["INSTRUME"]
+        events["sky_center"] = np.array([tblhdu["TCRVL2"],tblhdu["TCRVL3"]])
+        events["pix_center"] = np.array([tblhdu["TCRVL2"],tblhdu["TCRVL3"]])
+        events["dtheta"] = tblhdu["TCRVL3"]
+        events["xpix"] = tblhdu.data.field("X")
+        events["ypix"] = tblhdu.data.field("Y")
+        events["xsky"] = tblhdu.data.field("XSKY")
+        events["ysky"] = tblhdu.data.field("YSKY")
+        events["eobs"] = tblhdu.data.field("ENERGY")/1000. # Convert to keV
+        if "PI" in tblhdu.columns.names:
+            events["PI"] = tblhdu.data.field("PI")
+        if "PHA" in tblhdu.columns.names:
+            events["PHA"] = tblhdu.data.field("PHA")
+        
+        return cls(events)
+
+    @classmethod
+    def join_events(cls, events1, events2):
+        """
+        Join two sets of events, *events1* and *events2*.
+        """
+        events = {}
+        for item1, item2 in zip(events1.items(), events2.items()):
+            k1, v1 = item1
+            k2, v2 = item2
+            if isinstance(v1, np.ndarray):
+                events[k1] = np.concatenate([v1,v2])
+            else:
+                events[k1] = v1            
+        return cls(events)
+
+    def convolve_with_response(self, respfile):
+        """
+        Convolve the events with a RMF file *respfile*.
+        """
+        mylog.warning("This routine has not been tested to work with all RMFs. YMMV.")
+        if not "ARF" in self.events:
+            mylog.warning("Photons have not been processed with an"+
+                          " auxiliary response file. Spectral fitting"+
+                          " may be inaccurate.")
+
+        mylog.info("Reading response matrix file (RMF): %s" % (respfile))
+        
+        hdulist = pyfits.open(respfile)
+
+        tblhdu = hdulist["MATRIX"]
+        n_de = len(tblhdu.data["ENERG_LO"])
+        mylog.info("Number of Energy Bins: %d" % (n_de))
+        de = tblhdu.data["ENERG_HI"] - tblhdu.data["ENERG_LO"]
+
+        mylog.info("Energy limits: %g %g" % (min(tblhdu.data["ENERG_LO"]),
+                                             max(tblhdu.data["ENERG_HI"])))
+
+        if "ARF" in self.events:
+            f = pyfits.open(self.events["ARF"])
+            elo = f[1].data.field("ENERG_LO")
+            ehi = f[1].data.field("ENERG_HI")
+            f.close()
+            try:
+                assert_allclose(elo, tblhdu.data["ENERG_LO"], rtol=1.0e-6)
+                assert_allclose(ehi, tblhdu.data["ENERG_HI"], rtol=1.0e-6)
+            except AssertionError:
+                mylog.warning("Energy binning does not match for "+
+                              "ARF and RMF. This may make spectral "+
+                              "fitting difficult.")
+                                              
+        tblhdu2 = hdulist["EBOUNDS"]
+        n_ch = len(tblhdu2.data["CHANNEL"])
+        mylog.info("Number of Channels: %d" % (n_ch))
+        
+        eidxs = np.argsort(self.events["eobs"])
+
+        phEE = self.events["eobs"][eidxs]
+        phXS = self.events["xsky"][eidxs]
+        phYS = self.events["ysky"][eidxs]
+        phXP = self.events["xpix"][eidxs]
+        phYP = self.events["ypix"][eidxs]
+
+        detectedChannels = []
+        pindex = 0
+
+        # run through all photon energies and find which bin they go in
+        k = 0
+        fcurr = 0
+        last = len(phEE)-1
+
+        pbar = get_pbar("Scattering energies with RMF:", n_de)
+        
+        for low,high in zip(tblhdu.data["ENERG_LO"],tblhdu.data["ENERG_HI"]):
+            # weight function for probabilities from RMF
+            weights = tblhdu.data[k]["MATRIX"][:]
+            weights /= weights.sum()
+            # build channel number list associated to array value,
+            # there are groups of channels in rmfs with nonzero probabilities
+            trueChannel = []
+            f_chan = tblhdu.data[k]["F_CHAN"]
+            n_chan = tblhdu.data[k]["N_CHAN"]
+            if not iterable(f_chan):
+                 f_chan = [f_chan]
+                 n_chan = [n_chan]
+            for start,nchan in zip(f_chan, n_chan):
+                end = start + nchan
+                for j in range(start,end):
+                    trueChannel.append(j)
+            for q in range(fcurr,last):
+                if phEE[q]  >= low and phEE[q] < high:
+                    channelInd = np.random.choice(len(weights), p=weights)
+                    fcurr +=1
+                    detectedChannels.append(trueChannel[channelInd])
+                if phEE[q] >= high:
+                    break
+            pbar.update(k)
+            k+=1
+        pbar.finish()
+        
+        dchannel = np.array(detectedChannels)
+
+        self.events["xsky"] = phXS
+        self.events["ysky"] = phYS
+        self.events["xpix"] = phXP
+        self.events["ypix"] = phYP
+        self.events["eobs"] = phEE
+        self.events[tblhdu.header["CHANTYPE"]] = dchannel.astype(int)
+        self.events["RMF"] = respfile
+        self.events["ChannelType"] = tblhdu.header["CHANTYPE"]
+        self.events["Telescope"] = tblhdu.header["TELESCOP"]
+        self.events["Instrument"] = tblhdu.header["INSTRUME"]
+        
+    @parallel_root_only
+    def write_fits_file(self, fitsfile, clobber=False):
+        """
+        Write events to a FITS binary table file with filename *fitsfile*.
+        Set *clobber* to True if you need to overwrite a previous file.
+        """
+        
+        cols = []
+
+        col1 = pyfits.Column(name='ENERGY', format='E', unit='eV',
+                             array=self.events["eobs"]*1000.)
+        col2 = pyfits.Column(name='X', format='D', unit='pixel',
+                             array=self.events["xpix"])
+        col3 = pyfits.Column(name='Y', format='D', unit='pixel',
+                             array=self.events["ypix"])
+        col4 = pyfits.Column(name='XSKY', format='D', unit='deg',
+                             array=self.events["xsky"])
+        col5 = pyfits.Column(name='YSKY', format='D', unit='deg',
+                             array=self.events["ysky"])
+
+        cols = [col1, col2, col3, col4, col5]
+
+        if self.events.has_key("ChannelType"):
+             chantype = self.events["ChannelType"]
+             if chantype == "PHA":
+                  cunit="adu"
+             elif chantype == "PI":
+                  cunit="Chan"
+             col6 = pyfits.Column(name=chantype.upper(), format='1J',
+                                  unit=cunit, array=self.events[chantype])
+             cols.append(col6)
+            
+        coldefs = pyfits.ColDefs(cols)
+        tbhdu = pyfits.new_table(coldefs)
+        tbhdu.update_ext_name("EVENTS")
+
+        tbhdu.header.update("MTYPE1", "sky")
+        tbhdu.header.update("MFORM1", "x,y")        
+        tbhdu.header.update("MTYPE2", "EQPOS")
+        tbhdu.header.update("MFORM2", "RA,DEC")
+        tbhdu.header.update("TCTYP2", "RA---TAN")
+        tbhdu.header.update("TCTYP3", "DEC--TAN")
+        tbhdu.header.update("TCRVL2", self.events["sky_center"][0])
+        tbhdu.header.update("TCRVL3", self.events["sky_center"][1])
+        tbhdu.header.update("TCDLT2", -self.events["dtheta"])
+        tbhdu.header.update("TCDLT3", self.events["dtheta"])
+        tbhdu.header.update("TCRPX2", self.events["pix_center"][0])
+        tbhdu.header.update("TCRPX3", self.events["pix_center"][1])
+        tbhdu.header.update("TLMIN2", 0.5)
+        tbhdu.header.update("TLMIN3", 0.5)
+        tbhdu.header.update("TLMAX2", 2.*self.events["pix_center"][0]-0.5)
+        tbhdu.header.update("TLMAX3", 2.*self.events["pix_center"][1]-0.5)
+        tbhdu.header.update("EXPOSURE", self.events["ExposureTime"])
+        tbhdu.header.update("AREA", self.events["Area"])
+        tbhdu.header.update("D_A", self.events["AngularDiameterDistance"])
+        tbhdu.header.update("REDSHIFT", self.events["Redshift"])
+        tbhdu.header.update("HDUVERS", "1.1.0")
+        tbhdu.header.update("RADECSYS", "FK5")
+        tbhdu.header.update("EQUINOX", 2000.0)
+        if "RMF" in self.events:
+            tbhdu.header.update("RMF", self.events["RMF"])
+        if "ARF" in self.events:
+            tbhdu.header.update("ARF", self.events["ARF"])
+        if "ChannelType" in self.events:
+            tbhdu.header.update("CHANTYPE", self.events["ChannelType"])
+        if "Telescope" in self.events:
+            tbhdu.header.update("TELESCOP", self.events["Telescope"])
+        if "Instrument" in self.events:
+            tbhdu.header.update("INSTRUME", self.events["Instrument"])
+            
+        tbhdu.writeto(fitsfile, clobber=clobber)
+
+    @parallel_root_only    
+    def write_simput_file(self, prefix, clobber=False, emin=None, emax=None):
+        r"""
+        Write events to a SIMPUT file that may be read by the SIMX instrument
+        simulator.
+
+        Parameters
+        ----------
+
+        prefix : string
+            The filename prefix.
+        clobber : boolean, optional
+            Set to True to overwrite previous files.
+        e_min : float, optional
+            The minimum energy of the photons to save in keV.
+        e_max : float, optional
+            The maximum energy of the photons to save in keV.
+        """
+
+        if isinstance(self.events["Area"], basestring):
+             mylog.error("Writing SIMPUT files is only supported if you didn't convolve with an ARF.")
+             raise TypeError
+        
+        if emin is None:
+            emin = self.events["eobs"].min()*0.95
+        if emax is None:
+            emax = self.events["eobs"].max()*1.05
+
+        idxs = np.logical_and(self.events["eobs"] >= emin, self.events["eobs"] <= emax)
+        flux = erg_per_keV*np.sum(self.events["eobs"][idxs])/self.events["ExposureTime"]/self.events["Area"]
+        
+        col1 = pyfits.Column(name='ENERGY', format='E',
+                             array=self.events["eobs"])
+        col2 = pyfits.Column(name='DEC', format='D',
+                             array=self.events["ysky"])
+        col3 = pyfits.Column(name='RA', format='D',
+                             array=self.events["xsky"])
+
+        coldefs = pyfits.ColDefs([col1, col2, col3])
+
+        tbhdu = pyfits.new_table(coldefs)
+        tbhdu.update_ext_name("PHLIST")
+
+        tbhdu.header.update("HDUCLASS", "HEASARC/SIMPUT")
+        tbhdu.header.update("HDUCLAS1", "PHOTONS")
+        tbhdu.header.update("HDUVERS", "1.1.0")
+        tbhdu.header.update("EXTVER", 1)
+        tbhdu.header.update("REFRA", 0.0)
+        tbhdu.header.update("REFDEC", 0.0)
+        tbhdu.header.update("TUNIT1", "keV")
+        tbhdu.header.update("TUNIT2", "deg")
+        tbhdu.header.update("TUNIT3", "deg")                
+
+        phfile = prefix+"_phlist.fits"
+
+        tbhdu.writeto(phfile, clobber=clobber)
+
+        col1 = pyfits.Column(name='SRC_ID', format='J', array=np.array([1]).astype("int32"))
+        col2 = pyfits.Column(name='RA', format='D', array=np.array([self.center[0]]))
+        col3 = pyfits.Column(name='DEC', format='D', array=np.array([self.center[1]]))
+        col4 = pyfits.Column(name='E_MIN', format='D', array=np.array([emin]))
+        col5 = pyfits.Column(name='E_MAX', format='D', array=np.array([emax]))
+        col6 = pyfits.Column(name='FLUX', format='D', array=np.array([flux]))
+        col7 = pyfits.Column(name='SPECTRUM', format='80A', array=np.array([phfile+"[PHLIST,1]"]))
+        col8 = pyfits.Column(name='IMAGE', format='80A', array=np.array([phfile+"[PHLIST,1]"]))
+                        
+        coldefs = pyfits.ColDefs([col1, col2, col3, col4, col5, col6, col7, col8])
+        
+        wrhdu = pyfits.new_table(coldefs)
+        wrhdu.update_ext_name("SRC_CAT")
+                                
+        wrhdu.header.update("HDUCLASS", "HEASARC")
+        wrhdu.header.update("HDUCLAS1", "SIMPUT")
+        wrhdu.header.update("HDUCLAS2", "SRC_CAT")        
+        wrhdu.header.update("HDUVERS", "1.1.0")
+        wrhdu.header.update("RADECSYS", "FK5")
+        wrhdu.header.update("EQUINOX", 2000.0)
+        wrhdu.header.update("TUNIT2", "deg")
+        wrhdu.header.update("TUNIT3", "deg")
+        wrhdu.header.update("TUNIT4", "keV")
+        wrhdu.header.update("TUNIT5", "keV")
+        wrhdu.header.update("TUNIT6", "erg/s/cm**2")
+
+        simputfile = prefix+"_simput.fits"
+                
+        wrhdu.writeto(simputfile, clobber=clobber)
+
+    @parallel_root_only
+    def write_h5_file(self, h5file):
+        """
+        Write an EventList to the HDF5 file given by *h5file*.
+        """
+        f = h5py.File(h5file, "w")
+
+        f.create_dataset("/exp_time", data=self.events["ExposureTime"])
+        f.create_dataset("/area", data=self.events["Area"])
+        f.create_dataset("/redshift", data=self.events["Redshift"])
+        f.create_dataset("/d_a", data=self.events["AngularDiameterDistance"])        
+        if "ARF" in self.events:
+            f.create_dataset("/arf", data=self.events["ARF"])
+        if "RMF" in self.events:
+            f.create_dataset("/rmf", data=self.events["RMF"])
+        if "ChannelType" in self.events:
+            f.create_dataset("/channel_type", data=self.events["ChannelType"])
+        if "Telescope" in self.events:
+            f.create_dataset("/telescope", data=self.events["Telescope"])
+        if "Instrument" in self.events:
+            f.create_dataset("/instrument", data=self.events["Instrument"])
+                            
+        f.create_dataset("/xsky", data=self.events["xsky"])
+        f.create_dataset("/ysky", data=self.events["ysky"])
+        f.create_dataset("/xpix", data=self.events["xpix"])
+        f.create_dataset("/ypix", data=self.events["ypix"])
+        f.create_dataset("/eobs", data=self.events["eobs"])
+        if "PI" in self.events:
+            f.create_dataset("/pi", data=self.events["PI"])                  
+        if "PHA" in self.events:
+            f.create_dataset("/pha", data=self.events["PHA"])                  
+        f.create_dataset("/sky_center", data=self.events["sky_center"])
+        f.create_dataset("/pix_center", data=self.events["pix_center"])
+        f.create_dataset("/dtheta", data=self.events["dtheta"])
+
+        f.close()
+
+    @parallel_root_only
+    def write_fits_image(self, imagefile, clobber=False,
+                         emin=None, emax=None):
+        r"""
+        Generate a image by binning X-ray counts and write it to a FITS file.
+
+        Parameters
+        ----------
+
+        imagefile : string
+            The name of the image file to write.
+        clobber : boolean, optional
+            Set to True to overwrite a previous file.
+        emin : float, optional
+            The minimum energy of the photons to put in the image, in keV.
+        emax : float, optional
+            The maximum energy of the photons to put in the image, in keV.
+        """
+        if emin is None:
+            mask_emin = np.ones((self.num_events), dtype='bool')
+        else:
+            mask_emin = self.events["eobs"] > emin
+        if emax is None:
+            mask_emax = np.ones((self.num_events), dtype='bool')
+        else:
+            mask_emax = self.events["eobs"] < emax
+
+        mask = np.logical_and(mask_emin, mask_emax)
+
+        nx = int(2*self.events["pix_center"][0]-1.)
+        ny = int(2*self.events["pix_center"][1]-1.)
+        
+        xbins = np.linspace(0.5, float(nx)+0.5, nx+1, endpoint=True)
+        ybins = np.linspace(0.5, float(ny)+0.5, ny+1, endpoint=True)
+
+        H, xedges, yedges = np.histogram2d(self.events["xpix"][mask],
+                                           self.events["ypix"][mask],
+                                           bins=[xbins,ybins])
+        
+        hdu = pyfits.PrimaryHDU(H.T)
+        
+        hdu.header.update("MTYPE1", "EQPOS")
+        hdu.header.update("MFORM1", "RA,DEC")
+        hdu.header.update("CTYPE1", "RA---TAN")
+        hdu.header.update("CTYPE2", "DEC--TAN")
+        hdu.header.update("CRPIX1", 0.5*(nx+1))
+        hdu.header.update("CRPIX2", 0.5*(nx+1))                
+        hdu.header.update("CRVAL1", self.events["sky_center"][0])
+        hdu.header.update("CRVAL2", self.events["sky_center"][1])
+        hdu.header.update("CUNIT1", "deg")
+        hdu.header.update("CUNIT2", "deg")
+        hdu.header.update("CDELT1", -self.events["dtheta"])
+        hdu.header.update("CDELT2", self.events["dtheta"])
+        hdu.header.update("EXPOSURE", self.events["ExposureTime"])
+        
+        hdu.writeto(imagefile, clobber=clobber)
+                                    
+    @parallel_root_only
+    def write_spectrum(self, specfile, energy_bins=False, emin=0.1,
+                       emax=10.0, nchan=2000, clobber=False):
+        r"""
+        Bin event energies into a spectrum and write it to a FITS binary table. Can bin
+        on energy or channel, the latter only if the photons were convolved with an
+        RMF. In that case, the spectral binning will be determined by the RMF binning.
+
+        Parameters
+        ----------
+
+        specfile : string
+            The name of the FITS file to be written.
+        energy_bins : boolean, optional
+            Bin on energy or channel. 
+        emin : float, optional
+            The minimum energy of the spectral bins in keV. Only used if binning on energy.
+        emax : float, optional
+            The maximum energy of the spectral bins in keV. Only used if binning on energy.
+        nchan : integer, optional
+            The number of channels. Only used if binning on energy.
+        """
+
+        if energy_bins:
+            spectype = "energy"
+            espec = self.events["eobs"]
+            range = (emin, emax)
+            spec, ee = np.histogram(espec, bins=nchan, range=range)
+            bins = 0.5*(ee[1:]+ee[:-1])
+        else:
+            if not self.events.has_key("ChannelType"):
+                mylog.error("These events were not convolved with an RMF. Set energy_bins=True.")
+                raise KeyError
+            spectype = self.events["ChannelType"]
+            espec = self.events[spectype]
+            f = pyfits.open(self.events["RMF"])
+            nchan = int(f[1].header["DETCHANS"])
+            try:
+                cmin = int(f[1].header["TLMIN4"])
+            except KeyError:
+                mylog.warning("Cannot determine minimum allowed value for channel. " +
+                              "Setting to 0, which may be wrong.")
+                cmin = int(0)
+            try:
+                cmax = int(f[1].header["TLMAX4"])
+            except KeyError:
+                mylog.warning("Cannot determine maximum allowed value for channel. " +
+                              "Setting to DETCHANS, which may be wrong.")
+                cmax = int(nchan)
+            f.close()
+            minlength = nchan
+            if cmin == 1: minlength += 1
+            spec = np.bincount(self.events[spectype],minlength=minlength)
+            if cmin == 1: spec = spec[1:]
+            bins = np.arange(nchan).astype("int32")+cmin
+            
+        col1 = pyfits.Column(name='CHANNEL', format='1J', array=bins)
+        col2 = pyfits.Column(name=spectype.upper(), format='1D', array=bins.astype("float64"))
+        col3 = pyfits.Column(name='COUNTS', format='1J', array=spec.astype("int32"))
+        col4 = pyfits.Column(name='COUNT_RATE', format='1D', array=spec/self.events["ExposureTime"])
+        
+        coldefs = pyfits.ColDefs([col1, col2, col3, col4])
+        
+        tbhdu = pyfits.new_table(coldefs)
+        tbhdu.update_ext_name("SPECTRUM")
+
+        if not energy_bins:
+            tbhdu.header.update("DETCHANS", spec.shape[0])
+            tbhdu.header.update("TOTCTS", spec.sum())
+            tbhdu.header.update("EXPOSURE", self.events["ExposureTime"])
+            tbhdu.header.update("LIVETIME", self.events["ExposureTime"])        
+            tbhdu.header.update("CONTENT", spectype)
+            tbhdu.header.update("HDUCLASS", "OGIP")
+            tbhdu.header.update("HDUCLAS1", "SPECTRUM")
+            tbhdu.header.update("HDUCLAS2", "TOTAL")
+            tbhdu.header.update("HDUCLAS3", "TYPE:I")
+            tbhdu.header.update("HDUCLAS4", "COUNT")
+            tbhdu.header.update("HDUVERS", "1.1.0")
+            tbhdu.header.update("HDUVERS1", "1.1.0")
+            tbhdu.header.update("CHANTYPE", spectype)
+            tbhdu.header.update("BACKFILE", "none")
+            tbhdu.header.update("CORRFILE", "none")
+            tbhdu.header.update("POISSERR", True)
+            if self.events.has_key("RMF"):
+                 tbhdu.header.update("RESPFILE", self.events["RMF"])
+            else:
+                 tbhdu.header.update("RESPFILE", "none")
+            if self.events.has_key("ARF"):
+                tbhdu.header.update("ANCRFILE", self.events["ARF"])
+            else:        
+                tbhdu.header.update("ANCRFILE", "none")
+            if self.events.has_key("Telescope"):
+                tbhdu.header.update("TELESCOP", self.events["Telescope"])
+            else:
+                tbhdu.header.update("TELESCOP", "none")
+            if self.events.has_key("Instrument"):
+                tbhdu.header.update("INSTRUME", self.events["Instrument"])
+            else:
+                tbhdu.header.update("INSTRUME", "none")
+            tbhdu.header.update("AREASCAL", 1.0)
+            tbhdu.header.update("CORRSCAL", 0.0)
+            tbhdu.header.update("BACKSCAL", 1.0)
+                                
+        hdulist = pyfits.HDUList([pyfits.PrimaryHDU(), tbhdu])
+        
+        hdulist.writeto(specfile, clobber=clobber)

diff -r f9e54cd9a76ad809cda2d6388d238876b1ea97c6 -r 611ad31fbbde5f5d9a0ff38c0220516592fc1dc8 yt/analysis_modules/synthetic_obs/api.py
--- a/yt/analysis_modules/synthetic_obs/api.py
+++ /dev/null
@@ -1,34 +0,0 @@
-"""
-API for yt.analysis_modules.synthetic_obs
-
-Author: John ZuHone <jzuhone at gmail.com>
-Affiliation: NASA/GSFC
-Homepage: http://yt-project.org/
-License:
-  Copyright (C) 2010-2011 Matthew Turk.  All Rights Reserved.
-
-  This file is part of yt.
-
-  yt is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 3 of the License, or
-  (at your option) any later version.
-
-  This program is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-  GNU General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-"""
-
-from .photon_simulator import \
-     PhotonList, \
-     EventList
-
-from .photon_models import \
-     XSpecThermalModel, \
-     XSpecAbsorbModel, \
-     TableApecModel, \
-     TableAbsorbModel

This diff is so big that we needed to truncate the remainder.

https://bitbucket.org/yt_analysis/yt/commits/9e805ff77fe9/
Changeset:   9e805ff77fe9
Branch:      yt
User:        jzuhone
Date:        2013-10-11 17:50:09
Summary:     1) Making dist and dist_new tuples.
2) Refactor of from_user_model to make it simpler.
Affected #:  1 file

diff -r 611ad31fbbde5f5d9a0ff38c0220516592fc1dc8 -r 9e805ff77fe9e610ad2724e611006bb2066c920d yt/analysis_modules/photon_simulator/photon_simulator.py
--- a/yt/analysis_modules/photon_simulator/photon_simulator.py
+++ b/yt/analysis_modules/photon_simulator/photon_simulator.py
@@ -152,10 +152,10 @@
         Zmet : float, optional
             The metallicity of the gas. If there is a "Metallicity" field
             this parameter is ignored.
-        dist : float, optional
-            The angular diameter distance in Mpc, used for nearby sources.
-            This may be optionally supplied instead of it being determined
-            from the *redshift* and given *cosmology*.
+        dist : tuple, optional
+            The angular diameter distance in the form (value, unit), used
+            mainly for nearby sources. This may be optionally supplied
+            instead of it being determined from the *redshift* and given *cosmology*.
         cosmology : `yt.utilities.cosmology.Cosmology`, optional
             Cosmological information. If not supplied, it assumes \LambdaCDM with
             the default yt parameters.
@@ -183,7 +183,7 @@
         if dist is None:
             D_A = cosmo.AngularDiameterDistance(0.0,redshift)*cm_per_mpc
         else:
-            D_A = dist*cm_per_mpc
+            D_A = dist[0]*pf.units["cm"]/pf.units[dist[1]]
             redshift = 0.0
         dist_fac = 1.0/(4.*np.pi*D_A*D_A*(1.+redshift)**3)
         
@@ -196,8 +196,6 @@
         dx = data_source["dx"][start_c:end_c].copy()
         EM = (data_source["Density"][start_c:end_c].copy()/mp)**2
         EM *= 0.5*(1.+X_H)*X_H*vol
-
-        print EM.sum()
         
         data_source.clear_data()
         
@@ -267,7 +265,6 @@
             em_sum_m = (metalZ[ibegin:iend]*cell_em[ibegin:iend]).sum() 
 
             cspec, mspec = emission_model.get_spectrum(kT)
-            print (cspec+0.3*mspec).max(), kT
             cspec *= dist_fac*em_sum_c/vol_scale
             mspec *= dist_fac*em_sum_m/vol_scale
             
@@ -338,7 +335,10 @@
                         exp_time, user_function, parameters=None,
                         dist=None, cosmology=None):
         """
-        Initialize a PhotonList from a user-provided model.  
+        Initialize a PhotonList from a user-provided model. The idea is
+        to give the user full flexibility. The redshift, collecting area,
+        exposure time, and cosmology are stored in the `photons` dictionary which
+        is passed to the user function. 
 
         Parameters
         ----------
@@ -352,14 +352,15 @@
         exp_time : float
             The exposure time to determine the number of photons in seconds.
         user_function : function
-            A function that takes the *data_source* and any parameters and
-            generates the photons. 
+            A function that takes the *data_source*, the photons dictionary,
+            and the *parameters* dictionary and generates the photons.
+            Must be of the form: user_function(data_source, photons, parameters)
         parameters : dict, optional
             A dictionary of parameters to be passed to the user function. 
-        dist : float, optional
-            The angular diameter distance in Mpc, used for nearby sources.
-            This may be optionally supplied instead of it being determined
-            from the *redshift* and given *cosmology*.
+        dist : tuple, optional
+            The angular diameter distance in the form (value, unit), used
+            mainly for nearby sources. This may be optionally supplied
+            instead of it being determined from the *redshift* and given *cosmology*.
         cosmology : `yt.utilities.cosmology.Cosmology`, optional
             Cosmological information. If not supplied, it assumes \LambdaCDM with
             the default yt parameters.
@@ -367,12 +368,39 @@
         Examples
         --------
 
-        >>> def powerlaw_func(source, redshift, area, exp_time, D_A, norm, index, E0):
+        This is a simple example where a point source with a single line emission
+        spectrum of photons is created. More complicated examples which actually
+        create photons based on the fields in the dataset could be created. 
+
+        >>> from scipy.stats import powerlaw
+        >>> def powerlaw_func(source, photons, parameters):
         ...
-        ...     spec = norm*source["Density"]*(/E0
-        >>> redshift = 0.02
+        ...     pf = source.pf
+        ... 
+        ...     num_photons = parameters["num_photons"]
+        ...     E0  = parameters["line_energy"]
+        ...     sigE = parameters["line_sigma"]
+        ...
+        ...     energies = norm.rvs(loc=E0, scale=sigE, size=num_photons)
+        ...     
+        ...     photons["x"] = np.zeros((1)) # Place everything in the center cell
+        ...     photons["y"] = np.zeros((1))
+        ...     photons["z"] = np.zeros((1))
+        ...     photons["vx"] = np.zeros((1))
+        ...     photons["vy"] = np.zeros((1))
+        ...     photons["vz"] = 100.*np.ones((1))
+        ...     photons["dx"] = source["dx"][0]*pf.units["kpc"]*np.ones((1)) 
+        ...     photons["NumberOfPhotons"] = num_photons*np.ones((1))
+        ...     photons["Energy"] = np.array(energies)
+        >>>
+        >>> redshift = 0.05
         >>> area = 6000.0
         >>> time = 2.0e5
+        >>> parameters = {"num_photons" : 10000, "line_energy" : 5.0,
+        ...               "line_sigma" : 0.1}
+        >>> ddims = (128,128,128)
+        >>> random_data = {"Density":np.random.random(ddims)}
+        >>> pf = load_uniform_grid(random_data, ddims)
         >>> dd = pf.h.all_data
         >>> my_photons = PhotonList.from_user_model(dd, redshift, area,
         ...                                         time, powerlaw_func)
@@ -390,12 +418,9 @@
         if dist is None:
             D_A = cosmo.AngularDiameterDistance(0.0,redshift)*cm_per_mpc
         else:
-            D_A = dist*cm_per_mpc
+            D_A = dist[0]*pf.units["cm"]/pf.units[dist[1]]
             redshift = 0.0
-                    
-        photons = user_function(data_source, redshift, area,
-                                exp_time, D_A, parameters)
-        
+            
         photons["FiducialExposureTime"] = exp_time
         photons["FiducialArea"] = area
         photons["FiducialRedshift"] = redshift
@@ -405,6 +430,8 @@
         photons["OmegaMatter"] = cosmo.OmegaMatterNow
         photons["OmegaLambda"] = cosmo.OmegaLambdaNow
 
+        user_function(data_source, photons, parameters)
+        
         p_bins = np.cumsum(photons["NumberOfPhotons"])
         p_bins = np.insert(p_bins, 0, [np.uint64(0)])
                         
@@ -536,10 +563,10 @@
             The new value for the exposure time.
         redshift_new : float, optional
             The new value for the cosmological redshift.
-        dist_new : float, optional
-            The new value for the angular diameter distance, used for nearby
-            objects. If this is not set it will be determined from the cosmological
-            redshift.
+        dist_new : tuple, optional
+            The new value for the angular diameter distance in the form
+            (value, unit), used mainly for nearby sources. This may be optionally supplied
+            instead of it being determined from the cosmology.
         absorb_model : 'yt.analysis_modules.photon_simulator.PhotonModel`, optional
             A model for galactic absorption.
         psf_sigma : float, optional
@@ -616,7 +643,7 @@
             else:
                 if redshift_new is None:
                     zobs = 0.0
-                    D_A = dist_new*1000.
+                    D_A = dist[0]*self.pf.units["kpc"]/self.pf.units[dist[1]]
                 else:
                     zobs = redshift_new
                     D_A = self.cosmo.AngularDiameterDistance(0.0,zobs)*1000.


https://bitbucket.org/yt_analysis/yt/commits/2401beed3ce5/
Changeset:   2401beed3ce5
Branch:      yt
User:        jzuhone
Date:        2013-10-18 17:17:57
Summary:     Some minor fixes to the domain dimension, still trying to get the Astro-H RMF to work...
Affected #:  1 file

diff -r 9e805ff77fe9e610ad2724e611006bb2066c920d -r 2401beed3ce5220ddaa46dfe4f83162c8115603b yt/analysis_modules/photon_simulator/photon_simulator.py
--- a/yt/analysis_modules/photon_simulator/photon_simulator.py
+++ b/yt/analysis_modules/photon_simulator/photon_simulator.py
@@ -320,11 +320,19 @@
         photons["FiducialArea"] = area
         photons["FiducialRedshift"] = redshift
         photons["FiducialAngularDiameterDistance"] = D_A/cm_per_mpc
-        photons["DomainDimension"] = np.max((pf.domain_dimensions*(2**pf.h.max_level)))
         photons["HubbleConstant"] = cosmo.HubbleConstantNow
         photons["OmegaMatter"] = cosmo.OmegaMatterNow
         photons["OmegaLambda"] = cosmo.OmegaLambdaNow
 
+        domain_dimension = 0
+        for ax in "xyz":
+             pos = data_source[ax]
+             delta = data_source["d%s"%(ax)]
+             le = np.min(pos-0.5*delta)
+             re = np.max(pos+0.5*delta)
+             domain_dimension = max(domain_dimension, int((re-le)/delta.min()))
+        photons["DomainDimension"] = domain_dimension
+        
         p_bins = np.cumsum(photons["NumberOfPhotons"])
         p_bins = np.insert(p_bins, 0, [np.uint64(0)])
         
@@ -378,8 +386,8 @@
         ...     pf = source.pf
         ... 
         ...     num_photons = parameters["num_photons"]
-        ...     E0  = parameters["line_energy"]
-        ...     sigE = parameters["line_sigma"]
+        ...     E0  = parameters["line_energy"] # Energies are in keV
+        ...     sigE = parameters["line_sigma"] 
         ...
         ...     energies = norm.rvs(loc=E0, scale=sigE, size=num_photons)
         ...     
@@ -425,11 +433,19 @@
         photons["FiducialArea"] = area
         photons["FiducialRedshift"] = redshift
         photons["FiducialAngularDiameterDistance"] = D_A
-        photons["DomainDimension"] = np.max((pf.domain_dimensions*(2**pf.h.max_level)))
         photons["HubbleConstant"] = cosmo.HubbleConstantNow
         photons["OmegaMatter"] = cosmo.OmegaMatterNow
         photons["OmegaLambda"] = cosmo.OmegaLambdaNow
 
+        domain_dimension = 0
+        for ax in "xyz":
+             pos = data_source[ax]
+             delta = data_source["d%s"%(ax)]
+             le = np.min(pos-0.5*delta)
+             re = np.max(pos+0.5*delta)
+             domain_dimension = max(domain_dimension, int((re-le)/delta.min()))
+        photons["DomainDimension"] = domain_dimension
+
         user_function(data_source, photons, parameters)
         
         p_bins = np.cumsum(photons["NumberOfPhotons"])
@@ -628,9 +644,9 @@
             elif isinstance(area_new, basestring):
                 mylog.info("Using energy-dependent effective area.")
                 f = pyfits.open(area_new)
-                elo = f[1].data.field("ENERG_LO")
-                ehi = f[1].data.field("ENERG_HI")
-                eff_area = f[1].data.field("SPECRESP")
+                elo = f["SPECRESP"].data.field("ENERG_LO")
+                ehi = f["SPECRESP"].data.field("ENERG_HI")
+                eff_area = np.nan_to_num(f["SPECRESP"].data.field("SPECRESP"))
                 f.close()
                 Aratio = eff_area.max()/self.photons["FiducialArea"]
             else:
@@ -892,8 +908,8 @@
 
         if "ARF" in self.events:
             f = pyfits.open(self.events["ARF"])
-            elo = f[1].data.field("ENERG_LO")
-            ehi = f[1].data.field("ENERG_HI")
+            elo = f["SPECRESP"].data.field("ENERG_LO")
+            ehi = f["SPECRESP"].data.field("ENERG_HI")
             f.close()
             try:
                 assert_allclose(elo, tblhdu.data["ENERG_LO"], rtol=1.0e-6)
@@ -927,27 +943,33 @@
         
         for low,high in zip(tblhdu.data["ENERG_LO"],tblhdu.data["ENERG_HI"]):
             # weight function for probabilities from RMF
-            weights = tblhdu.data[k]["MATRIX"][:]
+            weights = np.nan_to_num(tblhdu.data[k]["MATRIX"][:])
             weights /= weights.sum()
             # build channel number list associated to array value,
             # there are groups of channels in rmfs with nonzero probabilities
             trueChannel = []
-            f_chan = tblhdu.data[k]["F_CHAN"]
-            n_chan = tblhdu.data[k]["N_CHAN"]
+            f_chan = np.nan_to_num(tblhdu.data["F_CHAN"][k])
+            n_chan = np.nan_to_num(tblhdu.data["N_CHAN"][k])
+            n_grp = np.nan_to_num(tblhdu.data["N_CHAN"][k])
             if not iterable(f_chan):
-                 f_chan = [f_chan]
-                 n_chan = [n_chan]
+                f_chan = [f_chan]
+                n_chan = [n_chan]
+                n_grp  = [n_grp]
             for start,nchan in zip(f_chan, n_chan):
                 end = start + nchan
-                for j in range(start,end):
-                    trueChannel.append(j)
-            for q in range(fcurr,last):
-                if phEE[q]  >= low and phEE[q] < high:
-                    channelInd = np.random.choice(len(weights), p=weights)
-                    fcurr +=1
-                    detectedChannels.append(trueChannel[channelInd])
-                if phEE[q] >= high:
-                    break
+                if start == end:
+                    trueChannel.append(start)
+                else:
+                    for j in range(start,end):
+                        trueChannel.append(j)
+            if len(trueChannel) > 0:
+                for q in range(fcurr,last):
+                    if phEE[q] >= low and phEE[q] < high:
+                        channelInd = np.random.choice(len(weights), p=weights)
+                        fcurr +=1
+                        detectedChannels.append(trueChannel[channelInd])
+                    if phEE[q] >= high:
+                        break
             pbar.update(k)
             k+=1
         pbar.finish()


https://bitbucket.org/yt_analysis/yt/commits/0db5fb190064/
Changeset:   0db5fb190064
Branch:      yt
User:        jzuhone
Date:        2013-10-18 22:20:10
Summary:     Rearranging some things
Affected #:  1 file

diff -r 2401beed3ce5220ddaa46dfe4f83162c8115603b -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b yt/analysis_modules/photon_simulator/photon_simulator.py
--- a/yt/analysis_modules/photon_simulator/photon_simulator.py
+++ b/yt/analysis_modules/photon_simulator/photon_simulator.py
@@ -19,6 +19,7 @@
 from yt.utilities.orientation import Orientation
 from yt.utilities.parallel_tools.parallel_analysis_interface import \
      communication_system, parallel_root_only, get_mpi_type, parallel_capable
+from IPython import embed
 
 import h5py
 
@@ -731,6 +732,10 @@
         
         events["xpix"] = xsky[detected]/dx.min() + 0.5*(nx+1) 
         events["ypix"] = ysky[detected]/dx.min() + 0.5*(nx+1)
+        events["eobs"] = eobs[detected]
+
+        events = comm.par_combine_object(events, datatype="dict", op="cat")
+
         if psf_sigma is not None:
             events["xpix"] += np.random.normal(sigma=psf_sigma/dtheta)
             events["ypix"] += np.random.normal(sigma=psf_sigma/dtheta)
@@ -742,10 +747,7 @@
         w.wcs.cunit = ["deg"]*2
         events["xsky"], events["ysky"] = w.wcs_pix2world(events["xpix"], events["ypix"],
                                                          1, ra_dec_order=True)
-        events["eobs"] = eobs[detected]
-
-        events = comm.par_combine_object(events, datatype="dict", op="cat")
-        
+                                                                                            
         num_events = len(events["xsky"])
             
         if comm.rank == 0: mylog.info("Total number of observed photons: %d" % (num_events))


https://bitbucket.org/yt_analysis/yt/commits/8eead056ca7b/
Changeset:   8eead056ca7b
Branch:      yt
User:        jzuhone
Date:        2013-10-18 22:31:02
Summary:     Merging
Affected #:  40 files

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b doc/get_yt.sh
--- /dev/null
+++ b/doc/get_yt.sh
@@ -0,0 +1,358 @@
+#
+# Hi there!  Welcome to the yt installation script.
+#
+# This script is designed to create a fully isolated Python installation
+# with the dependencies you need to run yt.
+#
+# This script is based on Conda, a distribution mechanism from Continuum
+# Analytics.  The process is as follows:
+#
+#  1. Download the appropriate Conda installation package
+#  2. Install Conda into the specified directory
+#  3. Install yt-specific dependencies
+#  4. Install yt
+#
+# There are a few options listed below, but by default, this will install
+# everything.  At the end, it will tell you what to do to use yt.
+#
+# By default this will install yt from source.
+#
+# If you experience problems, please visit the Help section at 
+# http://yt-project.org.
+#
+DEST_SUFFIX="yt-conda"
+DEST_DIR="`pwd`/${DEST_SUFFIX/ /}"   # Installation location
+BRANCH="yt" # This is the branch to which we will forcibly update.
+INST_YT_SOURCE=1 # Do we do a source install of yt?
+
+##################################################################
+#                                                                #
+# You will likely not have to modify anything below this region. #
+#                                                                #
+##################################################################
+
+LOG_FILE="`pwd`/yt_install.log"
+
+# Here is the idiom for redirecting to the log file:
+# ( SOMECOMMAND 2>&1 ) 1>> ${LOG_FILE} || do_exit
+
+MINICONDA_URLBASE="http://repo.continuum.io/miniconda"
+MINICONDA_VERSION="1.9.1"
+YT_RECIPE_REPO="https://bitbucket.org/yt_analysis/yt_conda/raw/default"
+
+function do_exit
+{
+    echo "********************************************"
+    echo "        FAILURE REPORT:"
+    echo "********************************************"
+    echo
+    tail -n 10 ${LOG_FILE}
+    echo
+    echo "********************************************"
+    echo "********************************************"
+    echo "Failure.  Check ${LOG_FILE}.  The last 10 lines are above."
+    exit 1
+}
+
+function log_cmd
+{
+    echo "EXECUTING:" >> ${LOG_FILE}
+    echo "  $*" >> ${LOG_FILE}
+    ( $* 2>&1 ) 1>> ${LOG_FILE} || do_exit
+}
+
+function get_ytproject
+{
+    [ -e $1 ] && return
+    echo "Downloading $1 from yt-project.org"
+    ${GETFILE} "http://yt-project.org/dependencies/$1" || do_exit
+    ( ${SHASUM} -c $1.sha512 2>&1 ) 1>> ${LOG_FILE} || do_exit
+}
+
+function get_ytdata
+{
+    echo "Downloading $1 from yt-project.org"
+    [ -e $1 ] && return
+    ${GETFILE} "http://yt-project.org/data/$1" || do_exit
+    ( ${SHASUM} -c $1.sha512 2>&1 ) 1>> ${LOG_FILE} || do_exit
+}
+
+function get_ytrecipe {
+    RDIR=${DEST_DIR}/src/yt-recipes/$1
+    mkdir -p ${RDIR}
+    pushd ${RDIR}
+    log_cmd ${GETFILE} ${YT_RECIPE_REPO}/$1/meta.yaml
+    log_cmd ${GETFILE} ${YT_RECIPE_REPO}/$1/build.sh
+    NEW_PKG=`conda build --output ${RDIR}`
+    log_cmd conda build --no-binstar-upload ${RDIR}
+    log_cmd conda install ${NEW_PKG}
+    popd
+}
+
+
+echo
+echo
+echo "========================================================================"
+echo
+echo "Hi there!  This is the yt installation script.  We're going to download"
+echo "some stuff and install it to create a self-contained, isolated"
+echo "environment for yt to run within."
+echo
+echo "This will install Miniconda from Continuum Analytics, the necessary"
+echo "packages to run yt, and create a self-contained environment for you to"
+echo "use yt.  Additionally, Conda itself provides the ability to install"
+echo "many other packages that can be used for other purposes."
+echo
+MYOS=`uname -s`       # A guess at the OS
+if [ "${MYOS##Darwin}" != "${MYOS}" ]
+then
+  echo "Looks like you're running on Mac OSX."
+  echo
+  echo "NOTE: you must have the Xcode command line tools installed."
+  echo
+  echo "The instructions for obtaining these tools varies according"
+  echo "to your exact OS version.  On older versions of OS X, you"
+  echo "must register for an account on the apple developer tools"
+  echo "website: https://developer.apple.com/downloads to obtain the"
+  echo "download link."
+  echo
+  echo "We have gathered some additional instructions for each"
+  echo "version of OS X below. If you have trouble installing yt"
+  echo "after following these instructions, don't hesitate to contact"
+  echo "the yt user's e-mail list."
+  echo
+  echo "You can see which version of OSX you are running by clicking"
+  echo "'About This Mac' in the apple menu on the left hand side of"
+  echo "menu bar.  We're assuming that you've installed all operating"
+  echo "system updates; if you have an older version, we suggest"
+  echo "running software update and installing all available updates."
+  echo
+  echo "OS X 10.5.8: search for and download Xcode 3.1.4 from the"
+  echo "Apple developer tools website."
+  echo
+  echo "OS X 10.6.8: search for and download Xcode 3.2 from the Apple"
+  echo "developer tools website.  You can either download the"
+  echo "Xcode 3.2.2 Developer Tools package (744 MB) and then use"
+  echo "Software Update to update to XCode 3.2.6 or"
+  echo "alternatively, you can download the Xcode 3.2.6/iOS SDK"
+  echo "bundle (4.1 GB)."
+  echo
+  echo "OS X 10.7.5: download Xcode 4.2 from the mac app store"
+  echo "(search for Xcode)."
+  echo "Alternatively, download the Xcode command line tools from"
+  echo "the Apple developer tools website."
+  echo
+  echo "OS X 10.8.2: download Xcode 4.6.1 from the mac app store."
+  echo "(search for Xcode)."
+  echo "Additionally, you will have to manually install the Xcode"
+  echo "command line tools, see:"
+  echo "http://stackoverflow.com/questions/9353444"
+  echo "Alternatively, download the Xcode command line tools from"
+  echo "the Apple developer tools website."
+  echo
+  echo "NOTE: It's possible that the installation will fail, if so,"
+  echo "please set the following environment variables, remove any"
+  echo "broken installation tree, and re-run this script verbatim."
+  echo
+  echo "$ export CC=gcc"
+  echo "$ export CXX=g++"
+  echo
+  MINICONDA_OS="MacOSX-x86_64"
+fi
+if [ "${MYOS##Linux}" != "${MYOS}" ]
+then
+  echo "Looks like you're on Linux."
+  echo
+  echo "Please make sure you have the developer tools for your OS installed."
+  echo
+  if [ -f /etc/SuSE-release ] && [ `grep --count SUSE /etc/SuSE-release` -gt 0 ]
+  then
+    echo "Looks like you're on an OpenSUSE-compatible machine."
+    echo
+    echo "You need to have these packages installed:"
+    echo
+    echo "  * devel_C_C++"
+    echo "  * libopenssl-devel"
+    echo "  * libuuid-devel"
+    echo "  * zip"
+    echo "  * gcc-c++"
+    echo "  * chrpath"
+    echo
+    echo "You can accomplish this by executing:"
+    echo
+    echo "$ sudo zypper install -t pattern devel_C_C++"
+    echo "$ sudo zypper install gcc-c++ libopenssl-devel libuuid-devel zip"
+    echo "$ sudo zypper install chrpath"
+  fi
+  if [ -f /etc/lsb-release ] && [ `grep --count buntu /etc/lsb-release` -gt 0 ]
+  then
+    echo "Looks like you're on an Ubuntu-compatible machine."
+    echo
+    echo "You need to have these packages installed:"
+    echo
+    echo "  * libssl-dev"
+    echo "  * build-essential"
+    echo "  * libncurses5"
+    echo "  * libncurses5-dev"
+    echo "  * zip"
+    echo "  * uuid-dev"
+    echo "  * chrpath"
+    echo
+    echo "You can accomplish this by executing:"
+    echo
+    echo "$ sudo apt-get install libssl-dev build-essential libncurses5 libncurses5-dev zip uuid-dev chrpath"
+    echo
+  fi
+  echo
+  echo "If you are running on a supercomputer or other module-enabled"
+  echo "system, please make sure that the GNU module has been loaded."
+  echo
+  if [ "${MYOS##x86_64}" != "${MYOS}" ]
+  then
+    MINICONDA_OS="Linux-x86_64"
+  elif [ "${MYOS##i386}" != "${MYOS}" ]
+  then
+    MINICONDA_OS="Linux-x86"
+  else
+    echo "Not sure which type of Linux you're on.  Going with x86_64."
+    MINICONDA_OS="Linux-x86_64"
+  fi
+fi
+echo
+echo "If you'd rather not continue, hit Ctrl-C."
+echo
+echo "========================================================================"
+echo
+read -p "[hit enter] "
+echo
+echo "Awesome!  Here we go."
+echo
+
+MINICONDA_PKG=Miniconda-${MINICONDA_VERSION}-${MINICONDA_OS}.sh
+
+if type -P wget &>/dev/null
+then
+    echo "Using wget"
+    export GETFILE="wget -nv"
+else
+    echo "Using curl"
+    export GETFILE="curl -sSO"
+fi
+
+echo
+echo "Downloading ${MINICONDA_URLBASE}/${MINICONDA_PKG}"
+echo "Downloading ${MINICONDA_URLBASE}/${MINICONDA_PKG}" >> ${LOG_FILE}
+echo
+
+${GETFILE} ${MINICONDA_URLBASE}/${MINICONDA_PKG} || do_exit
+
+echo "Installing the Miniconda python environment."
+
+log_cmd bash ./${MINICONDA_PKG} -b -p $DEST_DIR
+
+# I don't think we need OR want this anymore:
+#export LD_LIBRARY_PATH=${DEST_DIR}/lib:$LD_LIBRARY_PATH
+
+# This we *do* need.
+export PATH=${DEST_DIR}/bin:$PATH
+
+echo "Installing the necessary packages for yt."
+echo "This may take a while, but don't worry.  yt loves you."
+
+declare -a YT_DEPS
+YT_DEPS+=('python')
+YT_DEPS+=('distribute')
+YT_DEPS+=('libpng')
+YT_DEPS+=('freetype')
+YT_DEPS+=('hdf5')
+YT_DEPS+=('numpy')
+YT_DEPS+=('pygments')
+YT_DEPS+=('jinja2')
+YT_DEPS+=('tornado')
+YT_DEPS+=('pyzmq')
+YT_DEPS+=('ipython')
+YT_DEPS+=('sphinx')
+YT_DEPS+=('h5py')
+YT_DEPS+=('matplotlib')
+YT_DEPS+=('cython')
+
+# Here is our dependency list for yt
+log_cmd conda config --system --add channels http://repo.continuum.io/pkgs/free
+log_cmd conda config --system --add channels http://repo.continuum.io/pkgs/dev
+log_cmd conda config --system --add channels http://repo.continuum.io/pkgs/gpl
+log_cmd conda update --yes conda
+
+echo "Current dependencies: ${YT_DEPS[@]}"
+log_cmd echo "DEPENDENCIES" ${YT_DEPS[@]}
+log_cmd conda install --yes ${YT_DEPS[@]}
+
+echo "Installing mercurial."
+get_ytrecipe mercurial
+
+if [ $INST_YT_SOURCE -eq 0 ]
+then
+  echo "Installing yt as a package."
+  get_ytrecipe yt
+else
+  # We do a source install.
+  YT_DIR="${DEST_DIR}/src/yt-hg"
+  export PNG_DIR=${DEST_DIR}
+  export FTYPE_DIR=${DEST_DIR}
+  export HDF5_DIR=${DEST_DIR}
+  log_cmd hg clone -r ${BRANCH} https://bitbucket.org/yt_analysis/yt ${YT_DIR}
+  pushd ${YT_DIR}
+  echo $DEST_DIR > hdf5.cfg
+  log_cmd python setup.py develop
+  popd
+  log_cmd cp ${YT_DIR}/doc/activate ${DEST_DIR}/bin/activate 
+  log_cmd sed -i.bak -e "s,__YT_DIR__,${DEST_DIR}," ${DEST_DIR}/bin/activate
+  log_cmd cp ${YT_DIR}/doc/activate.csh ${DEST_DIR}/bin/activate.csh
+  log_cmd sed -i.bak -e "s,__YT_DIR__,${DEST_DIR}," ${DEST_DIR}/bin/activate.csh
+fi
+
+echo
+echo
+echo "========================================================================"
+echo
+echo "yt and the Conda system are now installed in $DEST_DIR ."
+echo
+if [ $INST_YT_SOURCE -eq 0 ]
+then
+  echo "You must now modify your PATH variable by prepending:"
+  echo 
+  echo "   $DEST_DIR/bin"
+  echo
+  echo "For example, if you use bash, place something like this at the end"
+  echo "of your ~/.bashrc :"
+  echo
+  echo "   export PATH=$DEST_DIR/bin:$PATH"
+else
+  echo "To run from this new installation, use the activate script for this "
+  echo "environment."
+  echo
+  echo "    $ source $DEST_DIR/bin/activate"
+  echo
+  echo "This modifies the environment variables YT_DEST, PATH, PYTHONPATH, and"
+  echo "LD_LIBRARY_PATH to match your new yt install.  If you use csh, just"
+  echo "append .csh to the above."
+fi
+echo
+echo "To get started with yt, check out the orientation:"
+echo
+echo "    http://yt-project.org/doc/orientation/"
+echo
+echo "or just activate your environment and run 'yt serve' to bring up the"
+echo "yt GUI."
+echo
+echo "For support, see the website and join the mailing list:"
+echo
+echo "    http://yt-project.org/"
+echo "    http://yt-project.org/data/      (Sample data)"
+echo "    http://yt-project.org/doc/       (Docs)"
+echo
+echo "    http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org"
+echo
+echo "========================================================================"
+echo
+echo "Oh, look at me, still talking when there's science to do!"
+echo "Good luck, and email the user list if you run into any problems."

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b doc/install_script.sh
--- a/doc/install_script.sh
+++ b/doc/install_script.sh
@@ -918,6 +918,8 @@
 do_setup_py $SYMPY
 [ $INST_PYX -eq 1 ] && do_setup_py $PYX
 
+( ${DEST_DIR}/bin/pip install jinja2 2>&1 ) 1>> ${LOG_FILE}
+
 # Now we build Rockstar and set its environment variable.
 if [ $INST_ROCKSTAR -eq 1 ]
 then

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b yt/analysis_modules/api.py
--- a/yt/analysis_modules/api.py
+++ b/yt/analysis_modules/api.py
@@ -103,6 +103,8 @@
     TwoPointFunctions, \
     FcnSet
 
+from .sunyaev_zeldovich.api import SZProjection
+
 from .radmc3d_export.api import \
     RadMC3DWriter
 
@@ -113,4 +115,3 @@
      XSpecAbsorbModel, \
      TableApecModel, \
      TableAbsorbModel
-     

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b yt/analysis_modules/setup.py
--- a/yt/analysis_modules/setup.py
+++ b/yt/analysis_modules/setup.py
@@ -21,4 +21,5 @@
     config.add_subpackage("star_analysis")
     config.add_subpackage("two_point_functions")
     config.add_subpackage("radmc3d_export")
+    config.add_subpackage("sunyaev_zeldovich")    
     return config

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b yt/analysis_modules/sunyaev_zeldovich/api.py
--- /dev/null
+++ b/yt/analysis_modules/sunyaev_zeldovich/api.py
@@ -0,0 +1,12 @@
+"""
+API for sunyaev_zeldovich
+"""
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
+from projection import SZProjection

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b yt/analysis_modules/sunyaev_zeldovich/projection.py
--- /dev/null
+++ b/yt/analysis_modules/sunyaev_zeldovich/projection.py
@@ -0,0 +1,349 @@
+"""
+Projection class for the Sunyaev-Zeldovich effect. Requires SZpack (at least
+version 1.1.1) to be downloaded and installed:
+
+http://www.chluba.de/SZpack/
+
+For details on the computations involved please refer to the following references:
+
+Chluba, Nagai, Sazonov, Nelson, MNRAS, 2012, arXiv:1205.5778
+Chluba, Switzer, Nagai, Nelson, MNRAS, 2012, arXiv:1211.3206 
+"""
+
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
+from yt.utilities.physical_constants import sigma_thompson, clight, hcgs, kboltz, mh, Tcmb
+from yt.data_objects.image_array import ImageArray
+from yt.data_objects.field_info_container import add_field
+from yt.funcs import fix_axis, mylog, iterable, get_pbar
+from yt.utilities.definitions import inv_axis_names
+from yt.visualization.image_writer import write_fits, write_projection
+from yt.visualization.volume_rendering.camera import off_axis_projection
+from yt.utilities.parallel_tools.parallel_analysis_interface import \
+     communication_system, parallel_root_only
+import numpy as np
+
+I0 = 2*(kboltz*Tcmb)**3/((hcgs*clight)**2)*1.0e17
+        
+try:
+    import SZpack
+except ImportError:
+    pass
+
+vlist = "xyz"
+
+def _t_squared(field, data):
+    return data["Density"]*data["TempkeV"]*data["TempkeV"]
+add_field("TSquared", function=_t_squared)
+
+def _beta_perp_squared(field, data):
+    return data["Density"]*data["VelocityMagnitude"]**2/clight/clight - data["BetaParSquared"]
+add_field("BetaPerpSquared", function=_beta_perp_squared)
+
+def _beta_par_squared(field, data):
+    return data["BetaPar"]**2/data["Density"]
+add_field("BetaParSquared", function=_beta_par_squared)
+
+def _t_beta_par(field, data):
+    return data["TempkeV"]*data["BetaPar"]
+add_field("TBetaPar", function=_t_beta_par)
+
+def _t_sz(field, data):
+    return data["Density"]*data["TempkeV"]
+add_field("TeSZ", function=_t_sz)
+
+class SZProjection(object):
+    r""" Initialize a SZProjection object.
+
+    Parameters
+    ----------
+    pf : parameter_file
+        The parameter file.
+    freqs : array_like
+        The frequencies (in GHz) at which to compute the SZ spectral distortion.
+    mue : float, optional
+        Mean molecular weight for determining the electron number density.
+    high_order : boolean, optional
+        Should we calculate high-order moments of velocity and temperature?
+
+    Examples
+    --------
+    >>> freqs = [90., 180., 240.]
+    >>> szprj = SZProjection(pf, freqs, high_order=True)
+    """
+    def __init__(self, pf, freqs, mue=1.143, high_order=False):
+            
+        self.pf = pf
+        self.num_freqs = len(freqs)
+        self.high_order = high_order
+        self.freqs = np.array(freqs)
+        self.mueinv = 1./mue
+        self.xinit = hcgs*self.freqs*1.0e9/(kboltz*Tcmb)
+        self.freq_fields = ["%d_GHz" % (int(freq)) for freq in freqs]
+        self.data = {}
+
+        self.units = {}
+        self.units["TeSZ"] = r"$\mathrm{keV}$"
+        self.units["Tau"] = None
+
+        self.display_names = {}
+        self.display_names["TeSZ"] = r"$\mathrm{T_e}$"
+        self.display_names["Tau"] = r"$\mathrm{\tau}$"
+
+        for f, field in zip(self.freqs, self.freq_fields):
+            self.units[field] = r"$\mathrm{MJy\ sr^{-1}}$"
+            self.display_names[field] = r"$\mathrm{\Delta{I}_{%d\ GHz}}$" % (int(f))
+            
+    def on_axis(self, axis, center="c", width=(1, "unitary"), nx=800, source=None):
+        r""" Make an on-axis projection of the SZ signal.
+
+        Parameters
+        ----------
+        axis : integer or string
+            The axis of the simulation domain along which to make the SZprojection.
+        center : array_like or string, optional
+            The center of the projection.
+        width : float or tuple
+            The width of the projection.
+        nx : integer, optional
+            The dimensions on a side of the projection image.
+        source : yt.data_objects.api.AMRData, optional
+            If specified, this will be the data source used for selecting regions to project.
+
+        Examples
+        --------
+        >>> szprj.on_axis("y", center="max", width=(1.0, "mpc"), source=my_sphere)
+        """
+        axis = fix_axis(axis)
+
+        def _beta_par(field, data):
+            axis = data.get_field_parameter("axis")
+            vpar = data["Density"]*data["%s-velocity" % (vlist[axis])]
+            return vpar/clight
+        add_field("BetaPar", function=_beta_par)    
+
+        proj = self.pf.h.proj(axis, "Density", source=source)
+        proj.set_field_parameter("axis", axis)
+        frb = proj.to_frb(width, nx)
+        dens = frb["Density"]
+        Te = frb["TeSZ"]/dens
+        bpar = frb["BetaPar"]/dens
+        omega1 = frb["TSquared"]/dens/(Te*Te) - 1.
+        bperp2 = np.zeros((nx,nx))
+        sigma1 = np.zeros((nx,nx))
+        kappa1 = np.zeros((nx,nx))                                    
+        if self.high_order:
+            bperp2 = frb["BetaPerpSquared"]/dens
+            sigma1 = frb["TBetaPar"]/dens/Te - bpar
+            kappa1 = frb["BetaParSquared"]/dens - bpar*bpar
+        tau = sigma_thompson*dens*self.mueinv/mh
+
+        nx,ny = frb.buff_size
+        self.bounds = frb.bounds
+        self.dx = (frb.bounds[1]-frb.bounds[0])/nx
+        self.dy = (frb.bounds[3]-frb.bounds[2])/ny
+        self.nx = nx
+        
+        self._compute_intensity(tau, Te, bpar, omega1, sigma1, kappa1, bperp2)
+                                                                                                                
+    def off_axis(self, L, center="c", width=(1, "unitary"), nx=800, source=None):
+        r""" Make an off-axis projection of the SZ signal.
+        
+        Parameters
+        ----------
+        L : array_like
+            The normal vector of the projection. 
+        center : array_like or string, optional
+            The center of the projection.
+        width : float or tuple
+            The width of the projection.
+        nx : integer, optional
+            The dimensions on a side of the projection image.
+        source : yt.data_objects.api.AMRData, optional
+            If specified, this will be the data source used for selecting regions to project.
+            Currently unsupported in yt 2.x.
+                    
+        Examples
+        --------
+        >>> L = np.array([0.5, 1.0, 0.75])
+        >>> szprj.off_axis(L, center="c", width=(2.0, "mpc"))
+        """
+        if iterable(width):
+            w = width[0]/self.pf.units[width[1]]
+        else:
+            w = width
+        if center == "c":
+            ctr = self.pf.domain_center
+        elif center == "max":
+            ctr = self.pf.h.find_max("Density")
+        else:
+            ctr = center
+
+        if source is not None:
+            mylog.error("Source argument is not currently supported for off-axis S-Z projections.")
+            raise NotImplementedError
+                
+        def _beta_par(field, data):
+            vpar = data["Density"]*(data["x-velocity"]*L[0]+
+                                    data["y-velocity"]*L[1]+
+                                    data["z-velocity"]*L[2])
+            return vpar/clight
+        add_field("BetaPar", function=_beta_par)
+
+        dens    = off_axis_projection(self.pf, ctr, L, w, nx, "Density")
+        Te      = off_axis_projection(self.pf, ctr, L, w, nx, "TeSZ")/dens
+        bpar    = off_axis_projection(self.pf, ctr, L, w, nx, "BetaPar")/dens
+        omega1  = off_axis_projection(self.pf, ctr, L, w, nx, "TSquared")/dens
+        omega1  = omega1/(Te*Te) - 1.
+        if self.high_order:
+            bperp2  = off_axis_projection(self.pf, ctr, L, w, nx, "BetaPerpSquared")/dens
+            sigma1  = off_axis_projection(self.pf, ctr, L, w, nx, "TBetaPar")/dens
+            sigma1  = sigma1/Te - bpar
+            kappa1  = off_axis_projection(self.pf, ctr, L, w, nx, "BetaParSquared")/dens
+            kappa1 -= bpar
+        else:
+            bperp2 = np.zeros((nx,nx))
+            sigma1 = np.zeros((nx,nx))
+            kappa1 = np.zeros((nx,nx))
+        tau = sigma_thompson*dens*self.mueinv/mh
+
+        self.bounds = np.array([-0.5*w, 0.5*w, -0.5*w, 0.5*w])
+        self.dx = w/nx
+        self.dy = w/nx
+        self.nx = nx
+
+        self._compute_intensity(tau, Te, bpar, omega1, sigma1, kappa1, bperp2)
+
+    def _compute_intensity(self, tau, Te, bpar, omega1, sigma1, kappa1, bperp2):
+
+        # Bad hack, but we get NaNs if we don't do something like this
+        small_beta = np.abs(bpar) < 1.0e-20
+        bpar[small_beta] = 1.0e-20
+                                                                   
+        comm = communication_system.communicators[-1]
+
+        nx, ny = self.nx,self.nx
+        signal = np.zeros((self.num_freqs,nx,ny))
+        xo = np.zeros((self.num_freqs))
+        
+        k = int(0)
+
+        start_i = comm.rank*nx/comm.size
+        end_i = (comm.rank+1)*nx/comm.size
+                        
+        pbar = get_pbar("Computing SZ signal.", nx*nx)
+
+        for i in xrange(start_i, end_i):
+            for j in xrange(ny):
+                xo[:] = self.xinit[:]
+                SZpack.compute_combo_means(xo, tau[i,j], Te[i,j],
+                                           bpar[i,j], omega1[i,j],
+                                           sigma1[i,j], kappa1[i,j], bperp2[i,j])
+                signal[:,i,j] = xo[:]
+                pbar.update(k)
+                k += 1
+
+        signal = comm.mpi_allreduce(signal)
+        
+        pbar.finish()
+                
+        for i, field in enumerate(self.freq_fields):
+            self.data[field] = ImageArray(I0*self.xinit[i]**3*signal[i,:,:])
+        self.data["Tau"] = ImageArray(tau)
+        self.data["TeSZ"] = ImageArray(Te)
+
+    @parallel_root_only
+    def write_fits(self, filename_prefix, clobber=True):
+        r""" Export images to a FITS file. Writes the SZ distortion in all
+        specified frequencies as well as the mass-weighted temperature and the
+        optical depth. Distance units are in kpc.  
+        
+        Parameters
+        ----------
+        filename_prefix : string
+            The prefix of the FITS filename.
+        clobber : boolean, optional
+            If the file already exists, do we overwrite?
+                    
+        Examples
+        --------
+        >>> szprj.write_fits("SZbullet", clobber=False)
+        """
+        coords = {}
+        coords["dx"] = self.dx*self.pf.units["kpc"]
+        coords["dy"] = self.dy*self.pf.units["kpc"]
+        coords["xctr"] = 0.0
+        coords["yctr"] = 0.0
+        coords["units"] = "kpc"
+        other_keys = {"Time" : self.pf.current_time}
+        write_fits(self.data, filename_prefix, clobber=clobber, coords=coords,
+                   other_keys=other_keys)
+
+    @parallel_root_only
+    def write_png(self, filename_prefix):
+        r""" Export images to PNG files. Writes the SZ distortion in all
+        specified frequencies as well as the mass-weighted temperature and the
+        optical depth. Distance units are in kpc. 
+        
+        Parameters
+        ----------
+        filename_prefix : string
+            The prefix of the image filenames.
+                
+        Examples
+        --------
+        >>> szprj.write_png("SZsloshing")
+        """     
+        extent = tuple([bound*self.pf.units["kpc"] for bound in self.bounds])
+        for field, image in self.items():
+            filename=filename_prefix+"_"+field+".png"
+            label = self.display_names[field]
+            if self.units[field] is not None:
+                label += " ("+self.units[field]+")"
+            write_projection(image, filename, colorbar_label=label, take_log=False,
+                             extent=extent, xlabel=r"$\mathrm{x\ (kpc)}$",
+                             ylabel=r"$\mathrm{y\ (kpc)}$")
+
+    @parallel_root_only
+    def write_hdf5(self, filename):
+        r"""Export the set of S-Z fields to a set of HDF5 datasets.
+        
+        Parameters
+        ----------
+        filename : string
+            This file will be opened in "write" mode.
+        
+        Examples
+        --------
+        >>> szprj.write_hdf5("SZsloshing.h5")                        
+        """
+        import h5py
+        f = h5py.File(filename, "w")
+        for field, data in self.items():
+            f.create_dataset(field,data=data)
+        f.close()
+   
+    def keys(self):
+        return self.data.keys()
+
+    def items(self):
+        return self.data.items()
+
+    def values(self):
+        return self.data.values()
+    
+    def has_key(self, key):
+        return key in self.data.keys()
+
+    def __getitem__(self, key):
+        return self.data[key]
+
+    @property
+    def shape(self):
+        return (self.nx,self.nx)

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b yt/analysis_modules/sunyaev_zeldovich/setup.py
--- /dev/null
+++ b/yt/analysis_modules/sunyaev_zeldovich/setup.py
@@ -0,0 +1,14 @@
+#!/usr/bin/env python
+import setuptools
+import os
+import sys
+import os.path
+
+
+def configuration(parent_package='', top_path=None):
+    from numpy.distutils.misc_util import Configuration
+    config = Configuration('sunyaev_zeldovich', parent_package, top_path)
+    config.add_subpackage("tests")
+    config.make_config_py()  # installs __config__.py
+    #config.make_svn_version_py()
+    return config

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b yt/analysis_modules/sunyaev_zeldovich/tests/test_projection.py
--- /dev/null
+++ b/yt/analysis_modules/sunyaev_zeldovich/tests/test_projection.py
@@ -0,0 +1,139 @@
+"""
+Unit test the sunyaev_zeldovich analysis module.
+"""
+
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
+from yt.frontends.stream.api import load_uniform_grid
+from yt.funcs import get_pbar, mylog
+from yt.utilities.physical_constants import cm_per_kpc, K_per_keV, \
+     mh, cm_per_km, kboltz, Tcmb, hcgs, clight, sigma_thompson
+from yt.testing import *
+from yt.utilities.answer_testing.framework import requires_pf, \
+     GenericArrayTest, data_dir_load, GenericImageTest
+try:
+    from yt.analysis_modules.sunyaev_zeldovich.projection import SZProjection, I0
+except ImportError:
+    pass
+import numpy as np
+try:
+    import SZpack
+except ImportError:
+    pass
+
+mue = 1./0.88
+freqs = np.array([30., 90., 240.])
+
+def setup():
+    """Test specific setup."""
+    from yt.config import ytcfg
+    ytcfg["yt", "__withintesting"] = "True"
+
+def full_szpack3d(pf, xo):
+    data = pf.h.grids[0]
+    dz = pf.h.get_smallest_dx()*pf.units["cm"]
+    nx,ny,nz = data["Density"].shape
+    dn = np.zeros((nx,ny,nz))
+    Dtau = sigma_thompson*data["Density"]/(mh*mue)*dz
+    Te = data["Temperature"]/K_per_keV
+    betac = data["z-velocity"]/clight
+    pbar = get_pbar("Computing 3-D cell-by-cell S-Z signal for comparison.", nx)
+    for i in xrange(nx):
+        pbar.update(i)
+        for j in xrange(ny):
+            for k in xrange(nz):
+                dn[i,j,k] = SZpack.compute_3d(xo, Dtau[i,j,k],
+                                              Te[i,j,k], betac[i,j,k],
+                                              1.0, 0.0, 0.0, 1.0e-5)
+    pbar.finish()
+    return I0*xo**3*np.sum(dn, axis=2)
+
+def setup_cluster():
+
+    R = 1000.
+    r_c = 100.
+    rho_c = 1.673e-26
+    beta = 1.
+    T0 = 4.
+    nx,ny,nz = 16,16,16
+    c = 0.17
+    a_c = 30.
+    a = 200.
+    v0 = 300.*cm_per_km
+    ddims = (nx,ny,nz)
+
+    x, y, z = np.mgrid[-R:R:nx*1j,
+                       -R:R:ny*1j,
+                       -R:R:nz*1j]
+
+    r = np.sqrt(x**2+y**2+z**2)
+
+    dens = np.zeros(ddims)
+    dens = rho_c*(1.+(r/r_c)**2)**(-1.5*beta)
+    temp = T0*K_per_keV/(1.+r/a)*(c+r/a_c)/(1.+r/a_c)
+    velz = v0*temp/(T0*K_per_keV)
+
+    data = {}
+    data["Density"] = dens
+    data["Temperature"] = temp
+    data["x-velocity"] = np.zeros(ddims)
+    data["y-velocity"] = np.zeros(ddims)
+    data["z-velocity"] = velz
+
+    bbox = np.array([[-0.5,0.5],[-0.5,0.5],[-0.5,0.5]])
+
+    L = 2*R*cm_per_kpc
+    dl = L/nz
+
+    pf = load_uniform_grid(data, ddims, L, bbox=bbox)
+
+    return pf
+
+ at requires_module("SZpack")
+def test_projection():
+    pf = setup_cluster()
+    nx,ny,nz = pf.domain_dimensions
+    xinit = 1.0e9*hcgs*freqs/(kboltz*Tcmb)
+    szprj = SZProjection(pf, freqs, mue=mue, high_order=True)
+    szprj.on_axis(2, nx=nx)
+    deltaI = np.zeros((3,nx,ny))
+    for i in xrange(3):
+        deltaI[i,:,:] = full_szpack3d(pf, xinit[i])
+        yield assert_almost_equal, deltaI[i,:,:], szprj["%d_GHz" % int(freqs[i])], 6
+
+M7 = "DD0010/moving7_0010"
+ at requires_module("SZpack")
+ at requires_pf(M7)
+def test_M7_onaxis():
+    pf = data_dir_load(M7)
+    szprj = SZProjection(pf, freqs)
+    szprj.on_axis(2, nx=100)
+    def onaxis_array_func():
+        return szprj.data
+    def onaxis_image_func(filename_prefix):
+        szprj.write_png(filename_prefix)
+    for test in [GenericArrayTest(pf, onaxis_array_func),
+                 GenericImageTest(pf, onaxis_image_func, 3)]:
+        test_M7_onaxis.__name__ = test.description
+        yield test
+
+ at requires_module("SZpack")
+ at requires_pf(M7)
+def test_M7_offaxis():
+    pf = data_dir_load(M7)
+    szprj = SZProjection(pf, freqs)
+    szprj.off_axis(np.array([0.1,-0.2,0.4]), nx=100)
+    def offaxis_array_func():
+        return szprj.data
+    def offaxis_image_func(filename_prefix):
+        szprj.write_png(filename_prefix)
+    for test in [GenericArrayTest(pf, offaxis_array_func),
+                 GenericImageTest(pf, offaxis_image_func, 3)]:
+        test_M7_offaxis.__name__ = test.description
+        yield test

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b yt/analysis_modules/two_point_functions/two_point_functions.py
--- a/yt/analysis_modules/two_point_functions/two_point_functions.py
+++ b/yt/analysis_modules/two_point_functions/two_point_functions.py
@@ -499,7 +499,7 @@
             points[:, 2] = points[:, 2] / self.period[2]
             fKD.qv_many = points.T
             fKD.nn_tags = np.asfortranarray(np.empty((1, points.shape[0]), dtype='int64'))
-            find_many_nn_nearest_neighbors()
+            fKD.find_many_nn_nearest_neighbors()
             # The -1 is for fortran counting.
             n = fKD.nn_tags[0,:] - 1
         return n

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b yt/data_objects/hierarchy.py
--- a/yt/data_objects/hierarchy.py
+++ b/yt/data_objects/hierarchy.py
@@ -395,6 +395,23 @@
           [self.grid_left_edge[:,0], self.grid_right_edge[:,1], self.grid_right_edge[:,2]],
         ], dtype='float64')
 
+    def lock_grids_to_parents(self):
+        r"""This function locks grid edges to their parents.
+
+        This is useful in cases where the grid structure may be somewhat
+        irregular, or where setting the left and right edges is a lossy
+        process.  It is designed to correct situations where left/right edges
+        may be set slightly incorrectly, resulting in discontinuities in images
+        and the like.
+        """
+        mylog.info("Locking grids to parents.")
+        for i, g in enumerate(self.grids):
+            si = g.get_global_startindex()
+            g.LeftEdge = self.pf.domain_left_edge + g.dds * si
+            g.RightEdge = g.LeftEdge + g.ActiveDimensions * g.dds
+            self.grid_left_edge[i,:] = g.LeftEdge
+            self.grid_right_edge[i,:] = g.RightEdge
+
     def print_stats(self):
         """
         Prints out (stdout) relevant information about the simulation

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b yt/data_objects/tests/test_fields.py
--- a/yt/data_objects/tests/test_fields.py
+++ b/yt/data_objects/tests/test_fields.py
@@ -86,6 +86,9 @@
         if field.startswith("particle"): continue
         if field.startswith("CIC"): continue
         if field.startswith("WeakLensingConvergence"): continue
+        if field.startswith("BetaPar"): continue
+        if field.startswith("TBetaPar"): continue
+        if field.startswith("BetaPerp"): continue
         if FieldInfo[field].particle_type: continue
         for nproc in [1, 4, 8]:
             yield TestFieldAccess(field, nproc)

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b yt/frontends/athena/data_structures.py
--- a/yt/frontends/athena/data_structures.py
+++ b/yt/frontends/athena/data_structures.py
@@ -197,13 +197,18 @@
             raise TypeError
 
         # Need to determine how many grids: self.num_grids
-        dname = self.hierarchy_filename
-        gridlistread = glob.glob('id*/%s-id*%s' % (dname[4:-9],dname[-9:] ))
+        dataset_dir = os.path.dirname(self.hierarchy_filename)
+        dname = os.path.split(self.hierarchy_filename)[-1]
+        if dataset_dir.endswith("id0"):
+            dname = "id0/"+dname
+            dataset_dir = dataset_dir[:-3]
+                        
+        gridlistread = glob.glob(os.path.join(dataset_dir, 'id*/%s-id*%s' % (dname[4:-9],dname[-9:])))
         gridlistread.insert(0,self.hierarchy_filename)
         if 'id0' in dname :
-            gridlistread += glob.glob('id*/lev*/%s*-lev*%s' % (dname[4:-9],dname[-9:]))
+            gridlistread += glob.glob(os.path.join(dataset_dir, 'id*/lev*/%s*-lev*%s' % (dname[4:-9],dname[-9:])))
         else :
-            gridlistread += glob.glob('lev*/%s*-lev*%s' % (dname[:-9],dname[-9:]))
+            gridlistread += glob.glob(os.path.join(dataset_dir, 'lev*/%s*-lev*%s' % (dname[:-9],dname[-9:])))
         self.num_grids = len(gridlistread)
         dxs=[]
         self.grids = np.empty(self.num_grids, dtype='object')
@@ -426,12 +431,17 @@
         else:
             self.periodicity = (True,)*self.dimensionality
 
-        dname = self.parameter_filename
-        gridlistread = glob.glob('id*/%s-id*%s' % (dname[4:-9],dname[-9:] ))
+        dataset_dir = os.path.dirname(self.parameter_filename)
+        dname = os.path.split(self.parameter_filename)[-1]
+        if dataset_dir.endswith("id0"):
+            dname = "id0/"+dname
+            dataset_dir = dataset_dir[:-3]
+            
+        gridlistread = glob.glob(os.path.join(dataset_dir, 'id*/%s-id*%s' % (dname[4:-9],dname[-9:])))
         if 'id0' in dname :
-            gridlistread += glob.glob('id*/lev*/%s*-lev*%s' % (dname[4:-9],dname[-9:]))
+            gridlistread += glob.glob(os.path.join(dataset_dir, 'id*/lev*/%s*-lev*%s' % (dname[4:-9],dname[-9:])))
         else :
-            gridlistread += glob.glob('lev*/%s*-lev*%s' % (dname[:-9],dname[-9:]))
+            gridlistread += glob.glob(os.path.join(dataset_dir, 'lev*/%s*-lev*%s' % (dname[:-9],dname[-9:])))
         self.nvtk = len(gridlistread)+1 
 
         self.current_redshift = self.omega_lambda = self.omega_matter = \

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b yt/frontends/chombo/tests/test_outputs.py
--- a/yt/frontends/chombo/tests/test_outputs.py
+++ b/yt/frontends/chombo/tests/test_outputs.py
@@ -29,6 +29,7 @@
     pf = data_dir_load(gc)
     yield assert_equal, str(pf), "data.0077.3d.hdf5"
     for test in small_patch_amr(gc, _fields):
+        test_gc.__name__ = test.description
         yield test
 
 tb = "TurbBoxLowRes/data.0005.3d.hdf5"
@@ -37,4 +38,5 @@
     pf = data_dir_load(tb)
     yield assert_equal, str(pf), "data.0005.3d.hdf5"
     for test in small_patch_amr(tb, _fields):
+        test_tb.__name__ = test.description
         yield test

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b yt/frontends/enzo/data_structures.py
--- a/yt/frontends/enzo/data_structures.py
+++ b/yt/frontends/enzo/data_structures.py
@@ -40,6 +40,7 @@
 from yt.utilities.definitions import \
     mpc_conversion, sec_conversion
 from yt.utilities import hdf5_light_reader
+from yt.utilities.io_handler import io_registry
 from yt.utilities.logger import ytLogger as mylog
 
 from .definitions import parameterDict
@@ -131,10 +132,11 @@
 
     def retrieve_ghost_zones(self, n_zones, fields, all_levels=False,
                              smoothed=False):
-        # We ignore smoothed in this case.
-        if n_zones > 3:
+        NGZ = self.pf.parameters.get("NumberOfGhostZones", 3)
+        if n_zones > NGZ:
             return EnzoGrid.retrieve_ghost_zones(
                 self, n_zones, fields, all_levels, smoothed)
+
         # ----- Below is mostly the original code, except we remove the field
         # ----- access section
         # We will attempt this by creating a datacube that is exactly bigger
@@ -162,7 +164,12 @@
                 level, new_left_edge, **kwargs)
         # ----- This is EnzoGrid.get_data, duplicated here mostly for
         # ----  efficiency's sake.
-        sl = [slice(3 - n_zones, -(3 - n_zones)) for i in range(3)]
+        start_zone = NGZ - n_zones
+        if start_zone == 0:
+            end_zone = None
+        else:
+            end_zone = -(NGZ - n_zones)
+        sl = [slice(start_zone, end_zone) for i in range(3)]
         if fields is None: return cube
         for field in ensure_list(fields):
             if field in self.hierarchy.field_list:
@@ -543,6 +550,9 @@
                     result[p] = result[p][0:max_num]
         return result
 
+    def _setup_data_io(self):
+            self.io = io_registry[self.data_style](self.parameter_file)
+
 
 class EnzoHierarchyInMemory(EnzoHierarchy):
 

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b yt/frontends/enzo/io.py
--- a/yt/frontends/enzo/io.py
+++ b/yt/frontends/enzo/io.py
@@ -32,6 +32,10 @@
 
     _data_style = "enzo_hdf4"
 
+    def __init__(self, pf, *args, **kwargs):
+        BaseIOHandler.__init__(self, *args, **kwargs)
+        self.pf = pf
+
     def modify(self, field):
         return field.swapaxes(0,2)
 
@@ -61,6 +65,10 @@
     _data_style = "enzo_hdf5"
     _particle_reader = True
 
+    def __init__(self, pf, *args, **kwargs):
+        BaseIOHandler.__init__(self, *args, **kwargs)
+        self.pf = pf
+
     def _read_field_names(self, grid):
         """
         Returns a list of fields associated with the filename
@@ -90,6 +98,10 @@
     _data_style = "enzo_packed_3d"
     _particle_reader = True
 
+    def __init__(self, pf, *args, **kwargs):
+        BaseIOHandler.__init__(self, *args, **kwargs)
+        self.pf = pf
+
     def _read_particles(self, fields, rtype, args, grid_list, enclosed,
                         conv_factors):
         filenames = [g.filename for g in grid_list]
@@ -144,10 +156,18 @@
 class IOHandlerPackedHDF5GhostZones(IOHandlerPackedHDF5):
     _data_style = "enzo_packed_3d_gz"
 
+    def __init__(self, pf, *args, **kwargs):
+        BaseIOHandler.__init__(self, *args, **kwargs)
+        self.pf = pf
+
     def modify(self, field):
+        NGZ = self.pf.parameters.get("NumberOfGhostZones", 3)
+        sl =  (slice(NGZ,-NGZ),
+               slice(NGZ,-NGZ),
+               slice(NGZ,-NGZ))
         if len(field.shape) < 3:
             return field
-        tr = field[3:-3,3:-3,3:-3].swapaxes(0,2)
+        tr = field[sl].swapaxes(0,2)
         return tr.copy() # To ensure contiguous
 
     def _read_raw_data_set(self, grid, field):
@@ -158,7 +178,7 @@
 
     _data_style = "enzo_inline"
 
-    def __init__(self, ghost_zones=3):
+    def __init__(self, pf, ghost_zones=3):
         import enzo
         self.enzo = enzo
         self.grids_in_memory = enzo.grid_data
@@ -166,6 +186,7 @@
         self.my_slice = (slice(ghost_zones,-ghost_zones),
                       slice(ghost_zones,-ghost_zones),
                       slice(ghost_zones,-ghost_zones))
+        self.pf = pf
         BaseIOHandler.__init__(self)
 
     def _read_data(self, grid, field):
@@ -210,6 +231,10 @@
     _data_style = "enzo_packed_2d"
     _particle_reader = False
 
+    def __init__(self, pf, *args, **kwargs):
+        BaseIOHandler.__init__(self, *args, **kwargs)
+        self.pf = pf
+
     def _read_data(self, grid, field):
         return hdf5_light_reader.ReadData(grid.filename,
             "/Grid%08i/%s" % (grid.id, field)).transpose()[:,:,None]
@@ -228,6 +253,10 @@
     _data_style = "enzo_packed_1d"
     _particle_reader = False
 
+    def __init__(self, pf, *args, **kwargs):
+        BaseIOHandler.__init__(self, *args, **kwargs)
+        self.pf = pf
+
     def _read_data(self, grid, field):
         return hdf5_light_reader.ReadData(grid.filename,
             "/Grid%08i/%s" % (grid.id, field)).transpose()[:,None,None]

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b yt/frontends/enzo/tests/test_outputs.py
--- a/yt/frontends/enzo/tests/test_outputs.py
+++ b/yt/frontends/enzo/tests/test_outputs.py
@@ -30,6 +30,7 @@
     pf = data_dir_load(m7)
     yield assert_equal, str(pf), "moving7_0010"
     for test in small_patch_amr(m7, _fields):
+        test_moving7.__name__ = test.description
         yield test
 
 g30 = "IsolatedGalaxy/galaxy0030/galaxy0030"
@@ -38,4 +39,5 @@
     pf = data_dir_load(g30)
     yield assert_equal, str(pf), "galaxy0030"
     for test in big_patch_amr(g30, _fields):
+        test_galaxy0030.__name__ = test.description
         yield test

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b yt/frontends/flash/tests/test_outputs.py
--- a/yt/frontends/flash/tests/test_outputs.py
+++ b/yt/frontends/flash/tests/test_outputs.py
@@ -29,6 +29,7 @@
     pf = data_dir_load(sloshing)
     yield assert_equal, str(pf), "sloshing_low_res_hdf5_plt_cnt_0300"
     for test in small_patch_amr(sloshing, _fields):
+        test_sloshing.__name__ = test.description
         yield test
 
 _fields_2d = ("Temperature", "Density")
@@ -39,4 +40,5 @@
     pf = data_dir_load(wt)
     yield assert_equal, str(pf), "windtunnel_4lev_hdf5_plt_cnt_0030"
     for test in small_patch_amr(wt, _fields_2d):
+        test_wind_tunnel.__name__ = test.description
         yield test

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b yt/frontends/orion/tests/test_outputs.py
--- a/yt/frontends/orion/tests/test_outputs.py
+++ b/yt/frontends/orion/tests/test_outputs.py
@@ -29,6 +29,7 @@
     pf = data_dir_load(radadvect)
     yield assert_equal, str(pf), "plt00000"
     for test in small_patch_amr(radadvect, _fields):
+        test_radadvect.__name__ = test.description
         yield test
 
 rt = "RadTube/plt00500"
@@ -37,4 +38,5 @@
     pf = data_dir_load(rt)
     yield assert_equal, str(pf), "plt00500"
     for test in small_patch_amr(rt, _fields):
+        test_radtube.__name__ = test.description
         yield test

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b yt/funcs.py
--- a/yt/funcs.py
+++ b/yt/funcs.py
@@ -625,3 +625,38 @@
         return
     if not os.path.exists(my_dir):
         only_on_root(os.makedirs, my_dir)
+
+ at contextlib.contextmanager
+def memory_checker(interval = 15):
+    r"""This is a context manager that monitors memory usage.
+
+    Parameters
+    ----------
+    interval : int
+        The number of seconds between printing the current memory usage in
+        gigabytes of the current Python interpreter.
+
+    Examples
+    --------
+
+    >>> with memory_checker(10):
+    ...     arr = np.zeros(1024*1024*1024, dtype="float64")
+    ...     time.sleep(15)
+    ...     del arr
+    """
+    import threading
+    class MemoryChecker(threading.Thread):
+        def __init__(self, event, interval):
+            self.event = event
+            self.interval = interval
+            threading.Thread.__init__(self)
+
+        def run(self):
+            while not self.event.wait(self.interval):
+                print "MEMORY: %0.3e gb" % (get_memory_usage()/1024.)
+
+    e = threading.Event()
+    mem_check = MemoryChecker(e, interval)
+    mem_check.start()
+    yield
+    e.set()

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b yt/gui/reason/extdirect_router.py
--- a/yt/gui/reason/extdirect_router.py
+++ b/yt/gui/reason/extdirect_router.py
@@ -9,6 +9,13 @@
 This code was released under the BSD License.
 """
 
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
 import inspect
 
 class DirectException(Exception):
@@ -186,12 +193,4 @@
 
 
 
-"""
 
-#-----------------------------------------------------------------------------
-# Copyright (c) 2013, yt Development Team.
-#
-# Distributed under the terms of the Modified BSD License.
-#
-# The full license is in the file COPYING.txt, distributed with this software.
-#-----------------------------------------------------------------------------

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b yt/testing.py
--- a/yt/testing.py
+++ b/yt/testing.py
@@ -14,6 +14,7 @@
 
 import itertools as it
 import numpy as np
+import importlib
 from yt.funcs import *
 from numpy.testing import assert_array_equal, assert_almost_equal, \
     assert_approx_equal, assert_array_almost_equal, assert_equal, \
@@ -251,3 +252,23 @@
                     list_of_kwarg_dicts[i][key] = keywords[key][0]
 
     return list_of_kwarg_dicts
+
+def requires_module(module):
+    """
+    Decorator that takes a module name as an argument and tries to import it.
+    If the module imports without issue, the function is returned, but if not, 
+    a null function is returned. This is so tests that depend on certain modules
+    being imported will not fail if the module is not installed on the testing
+    platform.
+    """
+    def ffalse(func):
+        return lambda: None
+    def ftrue(func):
+        return func
+    try:
+        importlib.import_module(module)
+    except ImportError:
+        return ffalse
+    else:
+        return ftrue
+    

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b yt/utilities/answer_testing/framework.py
--- a/yt/utilities/answer_testing/framework.py
+++ b/yt/utilities/answer_testing/framework.py
@@ -24,6 +24,7 @@
 import shelve
 import zlib
 import tempfile
+import glob
 
 from matplotlib.testing.compare import compare_images
 from nose.plugins import Plugin
@@ -576,6 +577,16 @@
         for newc, oldc in zip(new_result["children"], old_result["children"]):
             assert(newp == oldp)
 
+def compare_image_lists(new_result, old_result, decimals):
+    fns = ['old.png', 'new.png']
+    num_images = len(old_result)
+    assert(num_images > 0)
+    for i in xrange(num_images):
+        mpimg.imsave(fns[0], np.loads(zlib.decompress(old_result[i])))
+        mpimg.imsave(fns[1], np.loads(zlib.decompress(new_result[i])))
+        assert compare_images(fns[0], fns[1], 10**(decimals)) == None
+        for fn in fns: os.remove(fn)
+            
 class PlotWindowAttributeTest(AnswerTestingTest):
     _type_name = "PlotWindowAttribute"
     _attrs = ('plot_type', 'plot_field', 'plot_axis', 'attr_name', 'attr_args')
@@ -603,12 +614,71 @@
         return [zlib.compress(image.dumps())]
 
     def compare(self, new_result, old_result):
-        fns = ['old.png', 'new.png']
-        mpimg.imsave(fns[0], np.loads(zlib.decompress(old_result[0])))
-        mpimg.imsave(fns[1], np.loads(zlib.decompress(new_result[0])))
-        assert compare_images(fns[0], fns[1], 10**(-self.decimals)) == None
-        for fn in fns: os.remove(fn)
+        compare_image_lists(new_result, old_result, self.decimals)
+        
+class GenericArrayTest(AnswerTestingTest):
+    _type_name = "GenericArray"
+    _attrs = ('array_func_name','args','kwargs')
+    def __init__(self, pf_fn, array_func, args=None, kwargs=None, decimals=None):
+        super(GenericArrayTest, self).__init__(pf_fn)
+        self.array_func = array_func
+        self.array_func_name = array_func.func_name
+        self.args = args
+        self.kwargs = kwargs
+        self.decimals = decimals
+    def run(self):
+        if self.args is None:
+            args = []
+        else:
+            args = self.args
+        if self.kwargs is None:
+            kwargs = {}
+        else:
+            kwargs = self.kwargs
+        return self.array_func(*args, **kwargs)
+    def compare(self, new_result, old_result):
+        assert_equal(len(new_result), len(old_result),
+                                          err_msg="Number of outputs not equal.",
+                                          verbose=True)
+        for k in new_result:
+            if self.decimals is None:
+                assert_equal(new_result[k], old_result[k])
+            else:
+                assert_allclose(new_result[k], old_result[k], 10**(-self.decimals))
 
+class GenericImageTest(AnswerTestingTest):
+    _type_name = "GenericImage"
+    _attrs = ('image_func_name','args','kwargs')
+    def __init__(self, pf_fn, image_func, decimals, args=None, kwargs=None):
+        super(GenericImageTest, self).__init__(pf_fn)
+        self.image_func = image_func
+        self.image_func_name = image_func.func_name
+        self.args = args
+        self.kwargs = kwargs
+        self.decimals = decimals
+    def run(self):
+        if self.args is None:
+            args = []
+        else:
+            args = self.args
+        if self.kwargs is None:
+            kwargs = {}
+        else:
+            kwargs = self.kwargs
+        comp_imgs = []
+        tmpdir = tempfile.mkdtemp()
+        image_prefix = os.path.join(tmpdir,"test_img")
+        self.image_func(image_prefix, *args, **kwargs)
+        imgs = glob.glob(image_prefix+"*")
+        assert(len(imgs) > 0)
+        for img in imgs:
+            img_data = mpimg.imread(img)
+            os.remove(img)
+            comp_imgs.append(zlib.compress(img_data.dumps()))
+        return comp_imgs
+    def compare(self, new_result, old_result):
+        compare_image_lists(new_result, old_result, self.decimals)
+        
 def requires_pf(pf_fn, big_data = False):
     def ffalse(func):
         return lambda: None

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b yt/utilities/physical_constants.py
--- a/yt/utilities/physical_constants.py
+++ b/yt/utilities/physical_constants.py
@@ -8,7 +8,7 @@
 # http://physics.nist.gov/cuu/Constants/index.html
 
 # Masses
-mass_electron_cgs = 9.109382-28  # g
+mass_electron_cgs = 9.109382e-28  # g
 amu_cgs           = 1.660538921e-24  # g
 mass_hydrogen_cgs = 1.007947*amu_cgs  # g
 mass_sun_cgs = 1.98841586e33  # g
@@ -84,6 +84,7 @@
 erg_per_keV = erg_per_eV * 1.0e3
 K_per_keV = erg_per_keV / boltzmann_constant_cgs
 keV_per_K = 1.0 / K_per_keV
+Tcmb = 2.726 # Current CMB temperature
 
 #Short cuts
 G = gravitational_constant_cgs

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b yt/visualization/api.py
--- a/yt/visualization/api.py
+++ b/yt/visualization/api.py
@@ -20,8 +20,7 @@
 from plot_collection import \
     PlotCollection, \
     PlotCollectionInteractive, \
-    concatenate_pdfs, \
-    get_multi_plot
+    concatenate_pdfs
 
 from fixed_resolution import \
     FixedResolutionBuffer, \
@@ -54,5 +53,7 @@
     OffAxisSlicePlot, \
     ProjectionPlot, \
     OffAxisProjectionPlot
-    
 
+from base_plot_types import \
+    get_multi_plot
+

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b yt/visualization/base_plot_types.py
--- a/yt/visualization/base_plot_types.py
+++ b/yt/visualization/base_plot_types.py
@@ -94,3 +94,86 @@
         canvas.print_figure(f)
         f.seek(0)
         return f.read()
+
+def get_multi_plot(nx, ny, colorbar = 'vertical', bw = 4, dpi=300,
+                   cbar_padding = 0.4):
+    r"""Construct a multiple axes plot object, with or without a colorbar, into
+    which multiple plots may be inserted.
+
+    This will create a set of :class:`matplotlib.axes.Axes`, all lined up into
+    a grid, which are then returned to the user and which can be used to plot
+    multiple plots on a single figure.
+
+    Parameters
+    ----------
+    nx : int
+        Number of axes to create along the x-direction
+    ny : int
+        Number of axes to create along the y-direction
+    colorbar : {'vertical', 'horizontal', None}, optional
+        Should Axes objects for colorbars be allocated, and if so, should they
+        correspond to the horizontal or vertical set of axes?
+    bw : number
+        The base height/width of an axes object inside the figure, in inches
+    dpi : number
+        The dots per inch fed into the Figure instantiation
+
+    Returns
+    -------
+    fig : :class:`matplotlib.figure.Figure`
+        The figure created inside which the axes reside
+    tr : list of list of :class:`matplotlib.axes.Axes` objects
+        This is a list, where the inner list is along the x-axis and the outer
+        is along the y-axis
+    cbars : list of :class:`matplotlib.axes.Axes` objects
+        Each of these is an axes onto which a colorbar can be placed.
+
+    Notes
+    -----
+    This is a simple implementation for a common use case.  Viewing the source
+    can be instructure, and is encouraged to see how to generate more
+    complicated or more specific sets of multiplots for your own purposes.
+    """
+    hf, wf = 1.0/ny, 1.0/nx
+    fudge_x = fudge_y = 1.0
+    if colorbar is None:
+        fudge_x = fudge_y = 1.0
+    elif colorbar.lower() == 'vertical':
+        fudge_x = nx/(cbar_padding+nx)
+        fudge_y = 1.0
+    elif colorbar.lower() == 'horizontal':
+        fudge_x = 1.0
+        fudge_y = ny/(cbar_padding+ny)
+    fig = matplotlib.figure.Figure((bw*nx/fudge_x, bw*ny/fudge_y), dpi=dpi)
+    from _mpl_imports import FigureCanvasAgg
+    fig.set_canvas(FigureCanvasAgg(fig))
+    fig.subplots_adjust(wspace=0.0, hspace=0.0,
+                        top=1.0, bottom=0.0,
+                        left=0.0, right=1.0)
+    tr = []
+    for j in range(ny):
+        tr.append([])
+        for i in range(nx):
+            left = i*wf*fudge_x
+            bottom = fudge_y*(1.0-(j+1)*hf) + (1.0-fudge_y)
+            ax = fig.add_axes([left, bottom, wf*fudge_x, hf*fudge_y])
+            tr[-1].append(ax)
+    cbars = []
+    if colorbar is None:
+        pass
+    elif colorbar.lower() == 'horizontal':
+        for i in range(nx):
+            # left, bottom, width, height
+            # Here we want 0.10 on each side of the colorbar
+            # We want it to be 0.05 tall
+            # And we want a buffer of 0.15
+            ax = fig.add_axes([wf*(i+0.10)*fudge_x, hf*fudge_y*0.20,
+                               wf*(1-0.20)*fudge_x, hf*fudge_y*0.05])
+            cbars.append(ax)
+    elif colorbar.lower() == 'vertical':
+        for j in range(ny):
+            ax = fig.add_axes([wf*(nx+0.05)*fudge_x, hf*fudge_y*(ny-(j+0.95)),
+                               wf*fudge_x*0.05, hf*fudge_y*0.90])
+            ax.clear()
+            cbars.append(ax)
+    return fig, tr, cbars

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b yt/visualization/fixed_resolution.py
--- a/yt/visualization/fixed_resolution.py
+++ b/yt/visualization/fixed_resolution.py
@@ -19,6 +19,7 @@
     y_dict, \
     axis_names
 from .volume_rendering.api import off_axis_projection
+from image_writer import write_fits
 from yt.data_objects.image_array import ImageArray
 import _MPL
 import numpy as np
@@ -264,23 +265,11 @@
         output.close()
 
     def export_fits(self, filename_prefix, fields=None, clobber=False,
-                    other_keys=None, gzip_file=False, units="1",
-                    sky_center=(0.0,0.0), D_A=None):
-
-        """
-        This will export a set of FITS images of either the fields specified
-        or all the fields already in the object.  The output filename is
-        *filename_prefix*. If clobber is set to True, this will overwrite any
-        existing FITS file.
-
-        This requires the *pyfits* module, which is a standalone module
-        provided by STSci to interface with FITS-format files.
-        """
+                    other_keys=None, units="cm", sky_center=(0.0,0.0), D_A=None):
         r"""Export a set of pixelized fields to a FITS file.
 
         This will export a set of FITS images of either the fields specified
-        or all the fields already in the object.  The output filename is the
-        the specified prefix.
+        or all the fields already in the object.
 
         Parameters
         ----------
@@ -292,15 +281,12 @@
             If the file exists, this governs whether we will overwrite.
         other_keys : dictionary, optional
             A set of header keys and values to write into the FITS header.
-        gzip_file : boolean, optional
-            Gzip the file after writing, default False
         units : string, optional
-            The length units that the coordinates are written in, default '1'.
-            If units are set to "sky" then assume that sky coordinates are
-            requested.
+            the length units that the coordinates are written in, default 'cm'
+            If units are set to "deg" then assume that sky coordinates are
         sky_center : array_like, optional
-            Center of the image in (ra,dec) in degrees if sky coordinates are
-            requested.
+            Center of the image in (ra,dec) in degrees if sky coordinates
+            (units="deg") are requested.
         D_A : float or tuple, optional
             Angular diameter distance, given in code units as a float or
             a tuple containing the value and the length unit. Required if
@@ -308,103 +294,58 @@
         """
 
         try:
-            import pyfits
+            import astropy.io.fits as pyfits
         except:
-            try:
-                import astropy.io.fits as pyfits
-            except:
-                mylog.error("You don't have pyFITS or AstroPy installed!")
-                
+            mylog.error("You don't have AstroPy installed!")
+            raise ImportError
+        
         from os import system
 
         if units == "deg" and D_A is None:
-            mylog.error("Sky coordinates require an angular diameter distance. Please specify D_A.")
-
+            mylog.error("Sky coordinates require an angular diameter distance. Please specify D_A.")    
+            raise ValueError
+    
         if iterable(D_A):
             dist = D_A[0]/self.pf.units[D_A[1]]
         else:
             dist = D_A
+
+        if other_keys is None:
+            hdu_keys = {}
+        else:
+            hdu_keys = other_keys
+
         extra_fields = ['x','y','z','px','py','pz','pdx','pdy','pdz','weight_field']
-        if filename_prefix.endswith('.fits'): filename_prefix=filename_prefix[:-5]
         if fields is None: 
             fields = [field for field in self.data_source.fields 
                       if field not in extra_fields]
 
+        coords = {}
         nx, ny = self.buff_size
         dx = (self.bounds[1]-self.bounds[0])/nx
         dy = (self.bounds[3]-self.bounds[2])/ny
-        if units == "sky":
-            dx = np.rad2deg(dx/dist)
-            dy = np.rad2deg(dy/dist)
+        if units == "deg":  
+            coords["dx"] = -np.rad2deg(dx/dist)
+            coords["dy"] = np.rad2deg(dy/dist)
+            coords["xctr"] = sky_center[0]
+            coords["yctr"] = sky_center[1]
+            hdu_keys["MTYPE1"] = "EQPOS"
+            hdu_keys["MFORM1"] = "RA,DEC"
+            hdu_keys["CTYPE1"] = "RA---TAN"
+            hdu_keys["CTYPE2"] = "DEC--TAN"
         else:
-            dx *= self.pf.units[units]
-            dy *= self.pf.units[units]
-            xmin = self.bounds[0]*self.pf.units[units]
-            ymin = self.bounds[2]*self.pf.units[units]
-        simtime = self.pf.current_time
+            coords["dx"] = dx*self.pf.units[units]
+            coords["dy"] = dy*self.pf.units[units]
+            coords["xctr"] = 0.5*(self.bounds[0]+self.bounds[1])*self.pf.units[units]
+            coords["yctr"] = 0.5*(self.bounds[2]+self.bounds[3])*self.pf.units[units]
+        coords["units"] = units
+        
+        hdu_keys["Time"] = self.pf.current_time
 
-        hdus = [pyfits.PrimaryHDU()]
+        data = dict([(field,self[field]) for field in fields])
+        write_fits(data, filename_prefix, clobber=clobber, coords=coords,
+                   other_keys=hdu_keys)
 
-        print "HAHAH"
-        
-        for field in fields:
-
-            hdu = pyfits.ImageHDU(self[field])
-                
-            if self.data_source.has_key('weight_field'):
-                weightname = self.data_source._weight
-                if weightname is not None:
-                    hdu.header.update("Weight", weightname)
-                    
-            hdu.header.update("Field", field)
-            hdu.header.update("Time", simtime)
-
-            if units == "sky":
-                hdu.header.update("MTYPE1", "EQPOS")
-                hdu.header.update("MFORM1", "RA,DEC")
-                hdu.header.update("CTYPE1", "RA---TAN")
-                hdu.header.update("CTYPE2", "DEC--TAN")
-                hdu.header.update("CRPIX1", 0.5*(nx+1))
-                hdu.header.update("CRPIX2", 0.5*(ny+1))
-                hdu.header.update("CRVAL1", sky_center[0])
-                hdu.header.update("CRVAL2", sky_center[1])
-                hdu.header.update("CUNIT1", "deg")
-                hdu.header.update("CUNIT2", "deg")
-                hdu.header.update("CDELT1", -dx)
-                hdu.header.update("CDELT2", dy)
-            else:
-                hdu.header.update('CTYPE1', "LINEAR")
-                hdu.header.update('CTYPE2', "LINEAR")
-                hdu.header.update('CUNIT1', units)
-                hdu.header.update('CUNIT2', units)
-                hdu.header.update('CRPIX1', 0.5)
-                hdu.header.update('CRPIX2', 0.5)
-                hdu.header.update('CRVAL1', xmin)
-                hdu.header.update('CRVAL2', ymin)
-                hdu.header.update('CDELT1', dx)
-                hdu.header.update('CDELT2', dy)
-                                                                    
-            if other_keys is not None:
-
-                for k,v in other_keys.items() :
-
-                    hdu.header.update(k,v)
-
-            hdus.append(hdu)
-            
-        hdulist = pyfits.HDUList(hdus)
-
-        hdulist.writeto("%s.fits" % (filename_prefix), clobber=clobber)
-
-        print "YOURMOM"
-        
-        if gzip_file:
-            clob = ""
-            if (clobber) : clob = "-f"
-            system("gzip "+clob+" %s.fits" % (filename_prefix))
-
-        print "pffft"
-        
     def open_in_ds9(self, field, take_log=True):
         """
         This will open a given field in the DS9 viewer.

diff -r 0db5fb190064346fd2b60d46c5e0c4c242dcd58b -r 8eead056ca7b757cdab52429c33a9985d7efc05b yt/visualization/image_writer.py
--- a/yt/visualization/image_writer.py
+++ b/yt/visualization/image_writer.py
@@ -333,7 +333,8 @@
 
 def write_projection(data, filename, colorbar=True, colorbar_label=None, 
                      title=None, limits=None, take_log=True, figsize=(8,6),
-                     dpi=100, cmap_name='algae'):
+                     dpi=100, cmap_name='algae', extent=None, xlabel=None,
+                     ylabel=None):
     r"""Write a projection or volume rendering to disk with a variety of 
     pretty parameters such as limits, title, colorbar, etc.  write_projection
     uses the standard matplotlib interface to create the figure.  N.B. This code
@@ -392,16 +393,22 @@
     # Create the figure and paint the data on
     fig = matplotlib.figure.Figure(figsize=figsize)
     ax = fig.add_subplot(111)
-    fig.tight_layout()
-
-    cax = ax.imshow(data, vmin=limits[0], vmax=limits[1], norm=norm, cmap=cmap_name)
+    
+    cax = ax.imshow(data, vmin=limits[0], vmax=limits[1], norm=norm,
+                    extent=extent, cmap=cmap_name)
     
     if title:
         ax.set_title(title)
 
+    if xlabel:
+        ax.set_xlabel(xlabel)
+    if ylabel:
+        ax.set_ylabel(ylabel)
+
     # Suppress the x and y pixel counts
-    ax.set_xticks(())
-    ax.set_yticks(())
+    if extent is None:
+        ax.set_xticks(())
+        ax.set_yticks(())
 
     # Add a color bar and label if requested
     if colorbar:
@@ -409,6 +416,8 @@
         if colorbar_label:
             cbar.ax.set_ylabel(colorbar_label)
 
+    fig.tight_layout()
+        
     suffix = get_image_suffix(filename)
 
     if suffix == '':
@@ -430,83 +439,73 @@
 
 
 def write_fits(image, filename_prefix, clobber=True, coords=None,
-               other_keys=None, gzip_file=False) :
-    """
-    This will export a FITS image of a floating point array. The output filename is
-    *filename_prefix*. If clobber is set to True, this will overwrite any existing
-    FITS file.
-    
-    This requires the *pyfits* module, which comes as a standalone module
-    provided by STSci or as a part of the AstroPy package.
-    """
-    r"""Write out a floating point array directly to a FITS file, optionally
-    adding coordinates. 
+               other_keys=None):
+    r"""Write out floating point arrays directly to a FITS file, optionally
+    adding coordinates and header keywords.
         
     Parameters
     ----------
-    image : array_like
-        This is an (unscaled) array of floating point values, shape (N,N,) to save
-        in a FITS file.
+    image : array_like, or dict of array_like objects
+        This is either an (unscaled) array of floating point values, or a dict of
+        such arrays, shape (N,N,) to save in a FITS file. 
     filename_prefix : string
         This prefix will be prepended to every FITS file name.
     clobber : boolean
         If the file exists, this governs whether we will overwrite.
     coords : dictionary, optional
         A set of header keys and values to write to the FITS header to set up
-        a coordinate system.
-        "type": the type of coordinate system, default is "LINEAR"
+        a coordinate system, which is assumed to be linear unless specified otherwise
+        in *other_keys*
         "units": the length units
         "xctr","yctr": the center of the image
-        "dx","dy": the pixel width in each direction
+        "dx","dy": the pixel width in each direction                                                
     other_keys : dictionary, optional
-        A set of header keys and values to write into the FITS header.            
-    gzip_file : boolean, optional
-        gzip the file after writing, default False
+        A set of header keys and values to write into the FITS header.    
     """
 
     try:
-        import pyfits
+        import astropy.io.fits as pyfits
     except:
-        try:
-            import astropy.io.fits as pyfits
-        except:
-            mylog.error("You don't have pyFITS or AstroPy installed!")
-            
-    from os import system
+        mylog.error("You don't have AstroPy installed!")
+        raise ImportError
     
-    if filename_prefix.endswith('.fits'): filename_prefix=filename_prefix[:-5]
-    
-    hdu = pyfits.ImageHDU(image)
-    
-    if coords is not None:
+    try:
+        image.keys()
+        image_dict = image
+    except:
+        image_dict = dict(yt_data=image)
 
-        if not coords.has_key("type"):
-            type = "LINEAR"
-        else:
-            type = coords["type"]
-            
-        hdu.header.update('CTYPE1', type)
-        hdu.header.update('CTYPE2', type)
-        hdu.header.update('CUNIT1', coords["units"])
-        hdu.header.update('CUNIT2', coords["units"])
-        hdu.header.update('CRPIX1', 0.5*(nx+1))
-        hdu.header.update('CRPIX2', 0.5*(ny+1))
-        hdu.header.update('CRVAL1', coords["xctr"])
-        hdu.header.update('CRVAL2', coords["yctr"])
-        hdu.header.update('CDELT1', coords["dx"])
-        hdu.header.update('CDELT2', coords["dy"])
+    hdulist = [pyfits.PrimaryHDU()]
 
-    if other_keys is not None:
-        for k,v in other_keys.items():
-            hdu.header.update(k,v)
+    for key in image_dict.keys():
+
+        mylog.info("Writing image block \"%s\"" % (key))
+        hdu = pyfits.ImageHDU(image_dict[key])
+        hdu.update_ext_name(key)
+        
+        if coords is not None:
+            nx, ny = image_dict[key].shape
+            hdu.header.update('CUNIT1', coords["units"])
+            hdu.header.update('CUNIT2', coords["units"])
+            hdu.header.update('CRPIX1', 0.5*(nx+1))
+            hdu.header.update('CRPIX2', 0.5*(ny+1))
+            hdu.header.update('CRVAL1', coords["xctr"])
+            hdu.header.update('CRVAL2', coords["yctr"])
+            hdu.header.update('CDELT1', coords["dx"])
+            hdu.header.update('CDELT2', coords["dy"])
+            # These are the defaults, but will get overwritten if
+            # the caller has specified them
+            hdu.header.update('CTYPE1', "LINEAR")
+            hdu.header.update('CTYPE2', "LINEAR")
                                     
-    hdulist = pyfits.HDUList([pyfits.PrimaryHDU(),hdu])
-    hdulist.writeto("%s.fits" % (filename_prefix), clobber=clobber)
+        if other_keys is not None:
+            for k,v in other_keys.items():
+                hdu.header.update(k,v)
 
-    if gzip_file:
-        clob = ""
-        if clobber: clob="-f"
-        system("gzip "+clob+" %s.fits" % (filename_prefix))
+        hdulist.append(hdu)
+
+    hdulist = pyfits.HDUList(hdulist)
+    hdulist.writeto("%s.fits" % (filename_prefix), clobber=clobber)                    
 
 def display_in_notebook(image, max_val=None):
     """

This diff is so big that we needed to truncate the remainder.

https://bitbucket.org/yt_analysis/yt/commits/5c2196a9b9a6/
Changeset:   5c2196a9b9a6
Branch:      yt
User:        jzuhone
Date:        2013-10-18 22:33:55
Summary:     Quick doc fix
Affected #:  1 file

diff -r 8eead056ca7b757cdab52429c33a9985d7efc05b -r 5c2196a9b9a62563d2f984c080e0cbf3bd6c835c yt/visualization/fixed_resolution.py
--- a/yt/visualization/fixed_resolution.py
+++ b/yt/visualization/fixed_resolution.py
@@ -284,6 +284,7 @@
         units : string, optional
             the length units that the coordinates are written in, default 'cm'
             If units are set to "deg" then assume that sky coordinates are
+            requested.
         sky_center : array_like, optional
             Center of the image in (ra,dec) in degrees if sky coordinates
             (units="deg") are requested.


https://bitbucket.org/yt_analysis/yt/commits/0bd6cac9ce33/
Changeset:   0bd6cac9ce33
Branch:      yt
User:        jzuhone
Date:        2013-10-22 18:59:53
Summary:     Refactoring photon_simulator.
Affected #:  4 files

diff -r 5c2196a9b9a62563d2f984c080e0cbf3bd6c835c -r 0bd6cac9ce338d1c750b98df38296f0d807324e2 yt/analysis_modules/photon_simulator/api.py
--- a/yt/analysis_modules/photon_simulator/api.py
+++ b/yt/analysis_modules/photon_simulator/api.py
@@ -10,11 +10,15 @@
 # The full license is in the file COPYING.txt, distributed with this software.
 #-----------------------------------------------------------------------------
 
+from .photon_models import \
+     ThermalModel
+
 from .photon_simulator import \
      PhotonList, \
      EventList
 
-from .photon_models import \
+from .spectral_models import \
+     SpectralModel, \
      XSpecThermalModel, \
      XSpecAbsorbModel, \
      TableApecModel, \

diff -r 5c2196a9b9a62563d2f984c080e0cbf3bd6c835c -r 0bd6cac9ce338d1c750b98df38296f0d807324e2 yt/analysis_modules/photon_simulator/photon_models.py
--- a/yt/analysis_modules/photon_simulator/photon_models.py
+++ /dev/null
@@ -1,327 +0,0 @@
-"""
-Photon emission and absoprtion models for use with the
-photon simulator.
-"""
-
-#-----------------------------------------------------------------------------
-# Copyright (c) 2013, yt Development Team.
-#
-# Distributed under the terms of the Modified BSD License.
-#
-# The full license is in the file COPYING.txt, distributed with this software.
-#-----------------------------------------------------------------------------
-
-import numpy as np
-import os
-from yt.funcs import *
-import h5py
-try:
-    import astropy.io.fits as pyfits
-except:
-    mylog.warning("You don't have AstroPy installed. The APEC table model won't be available.")
-try:
-    import xspec
-except ImportError:
-    mylog.warning("You don't have PyXSpec installed. Some models won't be available.")
-try:
-    from scipy.integrate import cumtrapz
-    from scipy import stats
-except ImportError:
-    mylog.warning("You don't have SciPy installed. The APEC table model won't be avabilable.")
-    
-from yt.utilities.physical_constants import hcgs, clight, erg_per_keV, amu_cgs
-
-hc = 1.0e8*hcgs*clight/erg_per_keV
-
-class PhotonModel(object):
-
-    def __init__(self, emin, emax, nchan):
-        self.emin = emin
-        self.emax = emax
-        self.nchan = nchan
-        self.ebins = np.linspace(emin, emax, nchan+1)
-        self.de = np.diff(self.ebins)
-        self.emid = 0.5*(self.ebins[1:]+self.ebins[:-1])
-        
-    def prepare(self):
-        pass
-    
-    def get_spectrum(self):
-        pass
-                                                        
-class XSpecThermalModel(PhotonModel):
-    r"""
-    Initialize a thermal gas emission model from PyXspec.
-    
-    Parameters
-    ----------
-
-    model_name : string
-        The name of the thermal emission model.
-    emin : float
-        The minimum energy for the spectral model.
-    emax : float
-        The maximum energy for the spectral model.
-    nchan : integer
-        The number of channels in the spectral model.
-
-    Examples
-    --------
-    >>> mekal_model = XSpecThermalModel("mekal", 0.05, 50.0, 1000)
-    """
-    def __init__(self, model_name, emin, emax, nchan):
-        self.model_name = model_name
-        PhotonModel.__init__(self, emin, emax, nchan)
-        
-    def prepare(self):
-        """
-        Prepare the thermal model for execution.
-        """
-        xspec.Xset.chatter = 0
-        xspec.AllModels.setEnergies("%f %f %d lin" %
-                                    (self.emin, self.emax, self.nchan))
-        self.model = xspec.Model(self.model_name)
-        if self.model_name == "bremss":
-            self.norm = 3.02e-15
-        else:
-            self.norm = 1.0e-14
-        
-    def get_spectrum(self, kT):
-        """
-        Get the thermal emission spectrum given a temperature *kT* in keV. 
-        """
-        m = getattr(self.model,self.model_name)
-        m.kT = kT
-        m.Abundanc = 0.0
-        m.norm = 1.0
-        m.Redshift = 0.0
-        cosmic_spec = self.norm*np.array(self.model.values(0))
-        m.Abundanc = 1.0
-        if self.model_name == "bremss":
-            metal_spec = np.zeros((self.nchan))
-        else:
-            metal_spec = self.norm*np.array(self.model.values(0)) - cosmic_spec
-        return cosmic_spec, metal_spec
-        
-class XSpecAbsorbModel(PhotonModel):
-    r"""
-    Initialize an absorption model from PyXspec.
-    
-    Parameters
-    ----------
-
-    model_name : string
-        The name of the absorption model.
-    nH : float
-        The foreground column density *nH* in units of 10^22 cm^{-2}.
-    emin : float, optional
-        The minimum energy for the spectral model.
-    emax : float, optional
-        The maximum energy for the spectral model.
-    nchan : integer, optional
-        The number of channels in the spectral model.
-
-    Examples
-    --------
-    >>> abs_model = XSpecAbsorbModel("wabs", 0.1)
-    """
-    def __init__(self, model_name, nH, emin=0.01, emax=50.0, nchan=100000):
-        self.model_name = model_name
-        self.nH = nH
-        PhotonModel.__init__(self, emin, emax, nchan)
-        
-    def prepare(self):
-        """
-        Prepare the absorption model for execution.
-        """
-        xspec.Xset.chatter = 0
-        xspec.AllModels.setEnergies("%f %f %d lin" %
-                                    (self.emin, self.emax, self.nchan))
-        self.model = xspec.Model(self.model_name+"*powerlaw")
-        self.model.powerlaw.norm = self.nchan/(self.emax-self.emin)
-        self.model.powerlaw.PhoIndex = 0.0
-
-    def get_spectrum(self):
-        """
-        Get the absorption spectrum.
-        """
-        m = getattr(self.model,self.model_name)
-        m.nH = self.nH
-        return np.array(self.model.values(0))
-
-class TableApecModel(PhotonModel):
-    r"""
-    Initialize a thermal gas emission model from the AtomDB APEC tables
-    available at http://www.atomdb.org. This code borrows heavily from Python
-    routines used to read the APEC tables developed by Adam Foster at the
-    CfA (afoster at cfa.harvard.edu). 
-    
-    Parameters
-    ----------
-
-    apec_root : string
-        The directory root where the APEC model files are stored.
-    emin : float
-        The minimum energy for the spectral model.
-    emax : float
-        The maximum energy for the spectral model.
-    nchan : integer
-        The number of channels in the spectral model.
-    apec_vers : string, optional
-        The version identifier string for the APEC files, e.g.
-        "2.0.2"
-    thermal_broad : boolean, optional
-        Whether to apply thermal broadening to spectral lines. Only should
-        be used if you are attemping to simulate a high-spectral resolution
-        detector.
-
-    Examples
-    --------
-    >>> apec_model = TableApecModel("/Users/jzuhone/Data/atomdb_v2.0.2/", 0.05, 50.0,
-                                    1000, thermal_broad=True)
-    """
-    def __init__(self, apec_root, emin, emax, nchan,
-                 apec_vers="2.0.2", thermal_broad=False):
-        self.apec_root = apec_root
-        self.apec_prefix = "apec_v"+apec_vers
-        self.cocofile = os.path.join(self.apec_root,
-                                     self.apec_prefix+"_coco.fits")
-        self.linefile = os.path.join(self.apec_root,
-                                     self.apec_prefix+"_line.fits")
-        PhotonModel.__init__(self, emin, emax, nchan)
-        self.wvbins = hc/self.ebins[::-1]
-        # H, He, and trace elements
-        self.cosmic_elem = [1,2,3,4,5,9,11,15,17,19,21,22,23,24,25,27,29,30]
-        # Non-trace metals
-        self.metal_elem = [6,7,8,10,12,13,14,16,18,20,26,28]
-        self.thermal_broad = thermal_broad
-        self.A = np.array([0.0,1.00794,4.00262,6.941,9.012182,10.811,
-                           12.0107,14.0067,15.9994,18.9984,20.1797,
-                           22.9898,24.3050,26.9815,28.0855,30.9738,
-                           32.0650,35.4530,39.9480,39.0983,40.0780,
-                           44.9559,47.8670,50.9415,51.9961,54.9380,
-                           55.8450,58.9332,58.6934,63.5460,65.3800])
-        
-    def prepare(self):
-        """
-        Prepare the thermal model for execution.
-        """
-        try:
-            self.line_handle = pyfits.open(self.linefile)
-        except IOError:
-            mylog.error("LINE file %s does not exist" % (self.linefile))
-        try:
-            self.coco_handle = pyfits.open(self.cocofile)
-        except IOError:
-            mylog.error("COCO file %s does not exist" % (self.cocofile))
-        self.Tvals = self.line_handle[1].data.field("kT")
-        self.dTvals = np.diff(self.Tvals)
-        self.minlam = self.wvbins.min()
-        self.maxlam = self.wvbins.max()
-    
-    def _make_spectrum(self, element, tindex):
-        
-        tmpspec = np.zeros((self.nchan))
-        
-        i = np.where((self.line_handle[tindex].data.field('element')==element) &
-                     (self.line_handle[tindex].data.field('lambda') > self.minlam) &
-                     (self.line_handle[tindex].data.field('lambda') < self.maxlam))[0]
-
-        vec = np.zeros((self.nchan))
-        E0 = hc/self.line_handle[tindex].data.field('lambda')[i]
-        amp = self.line_handle[tindex].data.field('epsilon')[i]
-        if self.thermal_broad:
-            vec = np.zeros((self.nchan))
-            sigma = E0*np.sqrt(self.Tvals[tindex]*erg_per_keV/(self.A[element]*amu_cgs))/clight
-            for E, sig, a in zip(E0, sigma, amp):
-                cdf = stats.norm(E,sig).cdf(self.ebins)
-                vec += np.diff(cdf)*a
-        else:
-            ie = np.searchsorted(self.ebins, E0, side='right')-1
-            for i,a in zip(ie,amp): vec[i] += a
-        tmpspec += vec
-
-        ind = np.where((self.coco_handle[tindex].data.field('Z')==element) &
-                       (self.coco_handle[tindex].data.field('rmJ')==0))[0]
-        if len(ind)==0:
-            return tmpspec
-        else:
-            ind=ind[0]
-                                                    
-        n_cont=self.coco_handle[tindex].data.field('N_Cont')[ind]
-        e_cont=self.coco_handle[tindex].data.field('E_Cont')[ind][:n_cont]
-        continuum = self.coco_handle[tindex].data.field('Continuum')[ind][:n_cont]
-
-        tmpspec += np.interp(self.emid, e_cont, continuum)*self.de
-        
-        n_pseudo=self.coco_handle[tindex].data.field('N_Pseudo')[ind]
-        e_pseudo=self.coco_handle[tindex].data.field('E_Pseudo')[ind][:n_pseudo]
-        pseudo = self.coco_handle[tindex].data.field('Pseudo')[ind][:n_pseudo]
-        
-        tmpspec += np.interp(self.emid, e_pseudo, pseudo)*self.de
-        
-        return tmpspec
-
-    def get_spectrum(self, kT):
-        """
-        Get the thermal emission spectrum given a temperature *kT* in keV. 
-        """
-        cspec_l = np.zeros((self.nchan))
-        mspec_l = np.zeros((self.nchan))
-        cspec_r = np.zeros((self.nchan))
-        mspec_r = np.zeros((self.nchan))
-        tindex = np.searchsorted(self.Tvals, kT)-1
-        dT = (kT-self.Tvals[tindex])/self.dTvals[tindex]
-        # First do H,He, and trace elements
-        for elem in self.cosmic_elem:
-            cspec_l += self._make_spectrum(elem, tindex+2)
-            cspec_r += self._make_spectrum(elem, tindex+3)            
-        # Next do the metals
-        for elem in self.metal_elem:
-            mspec_l += self._make_spectrum(elem, tindex+2)
-            mspec_r += self._make_spectrum(elem, tindex+3)
-        cosmic_spec = cspec_l*(1.-dT)+cspec_r*dT
-        metal_spec = mspec_l*(1.-dT)+mspec_r*dT        
-        return cosmic_spec, metal_spec
-
-class TableAbsorbModel(PhotonModel):
-    r"""
-    Initialize an absorption model from a table stored in an HDF5 file.
-    
-    Parameters
-    ----------
-
-    filename : string
-        The name of the table file.
-    nH : float
-        The foreground column density *nH* in units of 10^22 cm^{-2}.
-
-    Examples
-    --------
-    >>> abs_model = XSpecAbsorbModel("wabs", 0.1)
-    """
-
-    def __init__(self, filename, nH):
-        if not os.path.exists(filename):
-            raise IOError("File does not exist: %s." % filename)
-        self.filename = filename
-        f = h5py.File(self.filename,"r")
-        emin = f["energy"][:].min()
-        emax = f["energy"][:].max()
-        self.sigma = f["cross_section"][:]
-        nchan = self.sigma.shape[0]
-        f.close()
-        PhotonModel.__init__(self, emin, emax, nchan)
-        self.nH = nH*1.0e22
-        
-    def prepare(self):
-        """
-        Prepare the absorption model for execution.
-        """
-        pass
-        
-    def get_spectrum(self):
-        """
-        Get the absorption spectrum.
-        """
-        return np.exp(-self.sigma*self.nH)

diff -r 5c2196a9b9a62563d2f984c080e0cbf3bd6c835c -r 0bd6cac9ce338d1c750b98df38296f0d807324e2 yt/analysis_modules/photon_simulator/photon_simulator.py
--- a/yt/analysis_modules/photon_simulator/photon_simulator.py
+++ b/yt/analysis_modules/photon_simulator/photon_simulator.py
@@ -1,5 +1,12 @@
 """
 Classes for generating lists of photons and detected events
+The algorithms used here are based off of the method used by the
+PHOX code (http://www.mpa-garching.mpg.de/~kdolag/Phox/),
+developed by Veronica Biffi and Klaus Dolag. References for
+PHOX may be found at:
+
+Biffi et al 2012: http://adsabs.harvard.edu/abs/2012MNRAS.420.3545B
+Biffi et al 2013: http://adsabs.harvard.edu/abs/2013MNRAS.428.1395B
 """
 
 #-----------------------------------------------------------------------------
@@ -18,8 +25,8 @@
 from yt.utilities.cosmology import Cosmology
 from yt.utilities.orientation import Orientation
 from yt.utilities.parallel_tools.parallel_analysis_interface import \
-     communication_system, parallel_root_only, get_mpi_type, parallel_capable
-from IPython import embed
+     communication_system, parallel_root_only, get_mpi_type, \
+     op_names, parallel_capable
 
 import h5py
 
@@ -27,20 +34,19 @@
     import astropy.io.fits as pyfits
     import astropy.wcs as pywcs
 except ImportError:
-    mylog.warning("You don't have AstroPy installed. " +
-                  "You will be unable to write FITS events files or images.")
-                   
+    pass
+
 N_TBIN = 10000
 TMIN = 8.08e-2
 TMAX = 50.
 
 comm = communication_system.communicators[-1]
-        
+    
 class PhotonList(object):
-
-    def __init__(self, photons=None, cosmo=None, p_bins=None):
-        if photons is None: photons = {}
+                                                                                                                                                                                                                                                            
+    def __init__(self, photons, parameters, cosmo, p_bins):
         self.photons = photons
+        self.parameters = parameters
         self.cosmo = cosmo
         self.p_bins = p_bins
         self.num_cells = len(photons["x"])
@@ -78,18 +84,21 @@
         r"""
         Initialize a PhotonList from the HDF5 file *filename*.
         """
+
         photons = {}
-
+        parameters = {}
+        
         f = h5py.File(filename, "r")
 
-        photons["FiducialExposureTime"] = f["/fid_exp_time"].value
-        photons["FiducialArea"] = f["/fid_area"].value
-        photons["FiducialRedshift"] = f["/fid_redshift"].value
-        photons["FiducialAngularDiameterDistance"] = f["/fid_d_a"].value
-        photons["DomainDimension"] = f["/domain_dimension"].value
-        photons["HubbleConstant"] = f["/hubble"].value
-        photons["OmegaMatter"] = f["/omega_matter"].value
-        photons["OmegaLambda"] = f["/omega_lambda"].value
+        parameters["FiducialExposureTime"] = f["/fid_exp_time"].value
+        parameters["FiducialArea"] = f["/fid_area"].value
+        parameters["FiducialRedshift"] = f["/fid_redshift"].value
+        parameters["FiducialAngularDiameterDistance"] = f["/fid_d_a"].value
+        parameters["Dimension"] = f["/dimension"].value
+        parameters["Width"] = f["/width"].value
+        parameters["HubbleConstant"] = f["/hubble"].value
+        parameters["OmegaMatter"] = f["/omega_matter"].value
+        parameters["OmegaLambda"] = f["/omega_lambda"].value
 
         num_cells = f["/x"][:].shape[0]
         start_c = comm.rank*num_cells/comm.size
@@ -120,12 +129,12 @@
         
         f.close()
 
-        cosmo = Cosmology(HubbleConstantNow=photons["HubbleConstant"],
-                          OmegaMatterNow=photons["OmegaMatter"],
-                          OmegaLambdaNow=photons["OmegaLambda"])
+        cosmo = Cosmology(HubbleConstantNow=parameters["HubbleConstant"],
+                          OmegaMatterNow=parameters["OmegaMatter"],
+                          OmegaLambdaNow=parameters["OmegaLambda"])
+
+        return cls(photons, parameters, cosmo, p_bins)
         
-        return cls(photons=photons, cosmo=cosmo, p_bins=p_bins)
-
     @classmethod
     def from_thermal_model(cls, data_source, redshift, area,
                            exp_time, emission_model, center="c",
@@ -187,7 +196,14 @@
             D_A = dist[0]*pf.units["cm"]/pf.units[dist[1]]
             redshift = 0.0
         dist_fac = 1.0/(4.*np.pi*D_A*D_A*(1.+redshift)**3)
-        
+
+        if center == "c":
+            src_ctr = pf.domain_center
+        elif center == "max":
+            src_ctr = pf.h.find_max("Density")[-1]
+        elif iterable(center):
+            src_ctr = center
+                                                        
         num_cells = data_source["Temperature"].shape[0]
         start_c = comm.rank*num_cells/comm.size
         end_c = (comm.rank+1)*num_cells/comm.size
@@ -219,13 +235,6 @@
                 
         idxs = np.argsort(kT)
         dshape = idxs.shape
-
-        if center == "c":
-            src_ctr = pf.domain_center
-        elif center == "max":
-            src_ctr = pf.h.find_max("Density")[-1]
-        elif iterable(center):
-            src_ctr = center
                     
         kT_bins = np.linspace(TMIN, max(kT[idxs][-1], TMAX), num=N_TBIN+1)
         dkT = kT_bins[1]-kT_bins[0]
@@ -307,6 +316,8 @@
         idxs = idxs[active_cells]
         
         photons = {}
+        parameters = {}
+        
         photons["x"] = (x[idxs]-src_ctr[0])*pf.units["kpc"]
         photons["y"] = (y[idxs]-src_ctr[1])*pf.units["kpc"]
         photons["z"] = (z[idxs]-src_ctr[2])*pf.units["kpc"]
@@ -317,32 +328,35 @@
         photons["NumberOfPhotons"] = number_of_photons[active_cells]
         photons["Energy"] = np.concatenate(energies)
                 
-        photons["FiducialExposureTime"] = exp_time
-        photons["FiducialArea"] = area
-        photons["FiducialRedshift"] = redshift
-        photons["FiducialAngularDiameterDistance"] = D_A/cm_per_mpc
-        photons["HubbleConstant"] = cosmo.HubbleConstantNow
-        photons["OmegaMatter"] = cosmo.OmegaMatterNow
-        photons["OmegaLambda"] = cosmo.OmegaLambdaNow
+        parameters["FiducialExposureTime"] = exp_time
+        parameters["FiducialArea"] = area
+        parameters["FiducialRedshift"] = redshift
+        parameters["FiducialAngularDiameterDistance"] = D_A/cm_per_mpc
+        parameters["HubbleConstant"] = cosmo.HubbleConstantNow
+        parameters["OmegaMatter"] = cosmo.OmegaMatterNow
+        parameters["OmegaLambda"] = cosmo.OmegaLambdaNow
 
-        domain_dimension = 0
-        for ax in "xyz":
-             pos = data_source[ax]
-             delta = data_source["d%s"%(ax)]
-             le = np.min(pos-0.5*delta)
-             re = np.max(pos+0.5*delta)
-             domain_dimension = max(domain_dimension, int((re-le)/delta.min()))
-        photons["DomainDimension"] = domain_dimension
-        
+        dimension = 0
+        width = 0.0
+        for i, ax in enumerate("xyz"):
+            pos = data_source[ax]
+            delta = data_source["d%s"%(ax)]
+            le = np.min(pos-0.5*delta)
+            re = np.max(pos+0.5*delta)
+            width = 2.*max(width, re-src_ctr[i], src_ctr[i]-le)
+            dimension = max(dimension, int(width/delta.min()))
+        parameters["Dimension"] = dimension
+        parameters["Width"] = width*pf.units["kpc"]
+                
         p_bins = np.cumsum(photons["NumberOfPhotons"])
         p_bins = np.insert(p_bins, 0, [np.uint64(0)])
-        
-        return cls(photons=photons, cosmo=cosmo, p_bins=p_bins)
-
+    
+        return cls(photons, parameters, cosmo, p_bins)
+    
     @classmethod
-    def from_user_model(cls, data_source, redshift, area,
-                        exp_time, user_function, parameters=None,
-                        dist=None, cosmology=None):
+    def from_scratch(cls, data_source, redshift, area,
+                     exp_time, gen_func, parameters=None,
+                     center="c", dist=None, cosmology=None):
         """
         Initialize a PhotonList from a user-provided model. The idea is
         to give the user full flexibility. The redshift, collecting area,
@@ -366,6 +380,8 @@
             Must be of the form: user_function(data_source, photons, parameters)
         parameters : dict, optional
             A dictionary of parameters to be passed to the user function. 
+        center : string or array_like, optional
+            The origin of the photons. Accepts "c", "max", or a coordinate.                
         dist : tuple, optional
             The angular diameter distance in the form (value, unit), used
             mainly for nearby sources. This may be optionally supplied
@@ -382,7 +398,7 @@
         create photons based on the fields in the dataset could be created. 
 
         >>> from scipy.stats import powerlaw
-        >>> def powerlaw_func(source, photons, parameters):
+        >>> def line_func(source, photons, parameters):
         ...
         ...     pf = source.pf
         ... 
@@ -412,7 +428,7 @@
         >>> pf = load_uniform_grid(random_data, ddims)
         >>> dd = pf.h.all_data
         >>> my_photons = PhotonList.from_user_model(dd, redshift, area,
-        ...                                         time, powerlaw_func)
+        ...                                         time, line_func)
 
         """
 
@@ -429,30 +445,40 @@
         else:
             D_A = dist[0]*pf.units["cm"]/pf.units[dist[1]]
             redshift = 0.0
-            
-        photons["FiducialExposureTime"] = exp_time
-        photons["FiducialArea"] = area
-        photons["FiducialRedshift"] = redshift
-        photons["FiducialAngularDiameterDistance"] = D_A
-        photons["HubbleConstant"] = cosmo.HubbleConstantNow
-        photons["OmegaMatter"] = cosmo.OmegaMatterNow
-        photons["OmegaLambda"] = cosmo.OmegaLambdaNow
 
-        domain_dimension = 0
-        for ax in "xyz":
-             pos = data_source[ax]
-             delta = data_source["d%s"%(ax)]
-             le = np.min(pos-0.5*delta)
-             re = np.max(pos+0.5*delta)
-             domain_dimension = max(domain_dimension, int((re-le)/delta.min()))
-        photons["DomainDimension"] = domain_dimension
+        if center == "c":
+            src_ctr = pf.domain_center
+        elif center == "max":
+            src_ctr = pf.h.find_max("Density")[-1]
+        elif iterable(center):
+            src_ctr = center
+                                                            
+        parameters["FiducialExposureTime"] = exp_time
+        parameters["FiducialArea"] = area
+        parameters["FiducialRedshift"] = redshift
+        parameters["FiducialAngularDiameterDistance"] = D_A
+        parameters["HubbleConstant"] = cosmo.HubbleConstantNow
+        parameters["OmegaMatter"] = cosmo.OmegaMatterNow
+        parameters["OmegaLambda"] = cosmo.OmegaLambdaNow
 
-        user_function(data_source, photons, parameters)
+        dimension = 0
+        width = 0.0
+        for i, ax in enumerate("xyz"):
+            pos = data_source[ax]
+            delta = data_source["d%s"%(ax)]
+            le = np.min(pos-0.5*delta)
+            re = np.max(pos+0.5*delta)
+            width = 2.*max(width, re-src_ctr[i], src_ctr[i]-le)
+            dimension = max(dimension, int(width/delta.min()))
+        parameters["Dimension"] = dimension
+        parameters["Width"] = width*pf.units["kpc"]
+                
+        photons = photon_model(data_source, parameters)
         
         p_bins = np.cumsum(photons["NumberOfPhotons"])
         p_bins = np.insert(p_bins, 0, [np.uint64(0)])
                         
-        return cls(photons=photons, cosmo=cosmo, p_bins=p_bins)
+        return cls(photons, parameters, cosmo, p_bins)
         
     def write_h5_file(self, photonfile):
         """
@@ -535,15 +561,16 @@
 
             # Scalars
        
-            f.create_dataset("fid_area", data=self.photons["FiducialArea"])
-            f.create_dataset("fid_exp_time", data=self.photons["FiducialExposureTime"])
-            f.create_dataset("fid_redshift", data=self.photons["FiducialRedshift"])
-            f.create_dataset("hubble", data=self.photons["HubbleConstant"])
-            f.create_dataset("omega_matter", data=self.photons["OmegaMatter"])
-            f.create_dataset("omega_lambda", data=self.photons["OmegaLambda"])
-            f.create_dataset("fid_d_a", data=self.photons["FiducialAngularDiameterDistance"])
-            f.create_dataset("domain_dimension", data=self.photons["DomainDimension"])
-
+            f.create_dataset("fid_area", data=self.parameters["FiducialArea"])
+            f.create_dataset("fid_exp_time", data=self.parameters["FiducialExposureTime"])
+            f.create_dataset("fid_redshift", data=self.parameters["FiducialRedshift"])
+            f.create_dataset("hubble", data=self.parameters["HubbleConstant"])
+            f.create_dataset("omega_matter", data=self.parameters["OmegaMatter"])
+            f.create_dataset("omega_lambda", data=self.parameters["OmegaLambda"])
+            f.create_dataset("fid_d_a", data=self.parameters["FiducialAngularDiameterDistance"])
+            f.create_dataset("dimension", data=self.parameters["Dimension"])
+            f.create_dataset("width", data=self.parameters["Width"])
+                        
             # Arrays
 
             f.create_dataset("x", data=x)
@@ -608,7 +635,7 @@
              sky_center = np.array([30.,45.])
 
         dx = self.photons["dx"]
-        nx = self.photons["DomainDimension"]
+        nx = self.parameters["Dimension"]
         if psf_sigma is not None:
              psf_sigma /= 3600.
 
@@ -633,30 +660,31 @@
         if (texp_new is None and area_new is None and
             redshift_new is None and dist_new is None):
             my_n_obs = n_ph_tot
-            zobs = self.photons["FiducialRedshift"]
-            D_A = self.photons["FiducialAngularDiameterDistance"]*1000.
+            zobs = self.parameters["FiducialRedshift"]
+            D_A = self.parameters["FiducialAngularDiameterDistance"]*1000.
         else:
             if texp_new is None:
                 Tratio = 1.
             else:
-                Tratio = texp_new/self.photons["FiducialExposureTime"]
+                Tratio = texp_new/self.parameters["FiducialExposureTime"]
             if area_new is None:
                 Aratio = 1.
             elif isinstance(area_new, basestring):
-                mylog.info("Using energy-dependent effective area.")
+                if comm.rank == 0:
+                    mylog.info("Using energy-dependent effective area.")
                 f = pyfits.open(area_new)
                 elo = f["SPECRESP"].data.field("ENERG_LO")
                 ehi = f["SPECRESP"].data.field("ENERG_HI")
                 eff_area = np.nan_to_num(f["SPECRESP"].data.field("SPECRESP"))
                 f.close()
-                Aratio = eff_area.max()/self.photons["FiducialArea"]
+                Aratio = eff_area.max()/self.parameters["FiducialArea"]
             else:
                 mylog.info("Using constant effective area.")
-                Aratio = area_new/self.photons["FiducialArea"]
+                Aratio = area_new/self.parameters["FiducialArea"]
             if redshift_new is None and dist_new is None:
                 Dratio = 1.
-                zobs = self.photons["FiducialRedshift"]
-                D_A = self.photons["FiducialAngularDiameterDistance"]*1000.                    
+                zobs = self.parameters["FiducialRedshift"]
+                D_A = self.parameters["FiducialAngularDiameterDistance"]*1000.                    
             else:
                 if redshift_new is None:
                     zobs = 0.0
@@ -664,8 +692,8 @@
                 else:
                     zobs = redshift_new
                     D_A = self.cosmo.AngularDiameterDistance(0.0,zobs)*1000.
-                fid_D_A = self.photons["FiducialAngularDiameterDistance"]*1000.
-                Dratio = fid_D_A*fid_D_A*(1.+self.photons["FiducialRedshift"]**3) / \
+                fid_D_A = self.parameters["FiducialAngularDiameterDistance"]*1000.
+                Dratio = fid_D_A*fid_D_A*(1.+self.parameters["FiducialRedshift"]**3) / \
                          (D_A*D_A*(1.+zobs)**3)
             fak = Aratio*Tratio*Dratio
             if fak > 1:
@@ -673,7 +701,8 @@
             my_n_obs = np.uint64(n_ph_tot*fak)
 
         n_obs_all = comm.mpi_allreduce(my_n_obs)
-        if comm.rank == 0: mylog.info("Total number of photons to use: %d" % (n_obs_all))
+        if comm.rank == 0:
+            mylog.info("Total number of photons to use: %d" % (n_obs_all))
         
         x = np.random.uniform(low=-0.5,high=0.5,size=my_n_obs)
         y = np.random.uniform(low=-0.5,high=0.5,size=my_n_obs)
@@ -728,10 +757,11 @@
                     
         events = {}
 
-        dtheta = dx.min()/D_A
+        dx_min = self.parameters["Width"]/self.parameters["Dimension"]
+        dtheta = np.rad2deg(dx_min/D_A)
         
-        events["xpix"] = xsky[detected]/dx.min() + 0.5*(nx+1) 
-        events["ypix"] = ysky[detected]/dx.min() + 0.5*(nx+1)
+        events["xpix"] = xsky[detected]/dx_min + 0.5*(nx+1) 
+        events["ypix"] = ysky[detected]/dx_min + 0.5*(nx+1)
         events["eobs"] = eobs[detected]
 
         events = comm.par_combine_object(events, datatype="dict", op="cat")
@@ -739,48 +769,51 @@
         if psf_sigma is not None:
             events["xpix"] += np.random.normal(sigma=psf_sigma/dtheta)
             events["ypix"] += np.random.normal(sigma=psf_sigma/dtheta)
-        w = pywcs.WCS(naxis=2)
-        w.wcs.crpix = [0.5*(nx+1)]*2
-        w.wcs.crval = sky_center
-        w.wcs.cdelt = [-dtheta, dtheta]
-        w.wcs.ctype = ["RA---TAN","DEC--TAN"]
-        w.wcs.cunit = ["deg"]*2
-        events["xsky"], events["ysky"] = w.wcs_pix2world(events["xpix"], events["ypix"],
-                                                         1, ra_dec_order=True)
-                                                                                            
-        num_events = len(events["xsky"])
+        
+        num_events = len(events["xpix"])
             
         if comm.rank == 0: mylog.info("Total number of observed photons: %d" % (num_events))
-                        
+
+        parameters = {}
+        
         if texp_new is None:
-            events["ExposureTime"] = self.photons["FiducialExposureTime"]
+            parameters["ExposureTime"] = self.parameters["FiducialExposureTime"]
         else:
-            events["ExposureTime"] = texp_new
+            parameters["ExposureTime"] = texp_new
         if area_new is None:
-            events["Area"] = self.photons["FiducialArea"]
+            parameters["Area"] = self.parameters["FiducialArea"]
         else:
-            events["Area"] = area_new
-        events["Redshift"] = zobs
-        events["AngularDiameterDistance"] = D_A/1000.
+            parameters["Area"] = area_new
+        parameters["Redshift"] = zobs
+        parameters["AngularDiameterDistance"] = D_A/1000.
         if isinstance(area_new, basestring):
-            events["ARF"] = area_new
-        events["sky_center"] = np.array(sky_center)
-        events["pix_center"] = np.array([0.5*(nx+1)]*2)
-        events["dtheta"] = dtheta
+            parameters["ARF"] = area_new
+        parameters["sky_center"] = np.array(sky_center)
+        parameters["pix_center"] = np.array([0.5*(nx+1)]*2)
+        parameters["dtheta"] = dtheta
         
-        return EventList(events)
+        return EventList(events, parameters)
 
 class EventList(object) :
 
-    def __init__(self, events = None) :
+    def __init__(self, events, parameters) :
 
-        if events is None : events = {}
         self.events = events
-        self.num_events = events["xsky"].shape[0]
+        self.parameters = parameters
+        self.num_events = events["xpix"].shape[0]
+        self.wcs = pywcs.WCS(naxis=2)
+        self.wcs.wcs.crpix = parameters["pix_center"]
+        self.wcs.wcs.crval = parameters["sky_center"]
+        self.wcs.wcs.cdelt = [-parameters["dtheta"], parameters["dtheta"]]
+        self.wcs.wcs.ctype = ["RA---TAN","DEC--TAN"]
+        self.wcs.wcs.cunit = ["deg"]*2                                                
         
     def keys(self):
         return self.events.keys()
 
+    def has_key(self, key):
+        return key in self.keys()
+    
     def items(self):
         return self.events.items()
 
@@ -788,6 +821,14 @@
         return self.events.values()
     
     def __getitem__(self,key):
+
+        if key == "xsky" or key == "ysky":
+            if not self.has_key(key):
+                (self.events["xsky"],
+                 self.events["ysky"]) = \
+                 self.wcs.wcs_pix2world(events["xpix"], events["ypix"],
+                                        1, ra_dec_order=True)
+                        
         return self.events[key]
         
     @classmethod
@@ -796,26 +837,25 @@
         Initialize an EventList from a HDF5 file with filename *h5file*.
         """
         events = {}
+        parameters = {}
         
         f = h5py.File(h5file, "r")
 
-        events["ExposureTime"] = f["/exp_time"].value
-        events["Area"] = f["/area"].value
-        events["Redshift"] = f["/redshift"].value
-        events["AngularDiameterDistance"] = f["/d_a"].value
+        parameters["ExposureTime"] = f["/exp_time"].value
+        parameters["Area"] = f["/area"].value
+        parameters["Redshift"] = f["/redshift"].value
+        parameters["AngularDiameterDistance"] = f["/d_a"].value
         if "rmf" in f:
-            events["RMF"] = f["/rmf"].value
+            parameters["RMF"] = f["/rmf"].value
         if "arf" in f:
-            events["ARF"] = f["/arf"].value
+            parameters["ARF"] = f["/arf"].value
         if "channel_type" in f:
-            events["ChannelType"] = f["/channel_type"].value
+            parameters["ChannelType"] = f["/channel_type"].value
         if "telescope" in f:
-            events["Telescope"] = f["/telescope"].value
+            parameters["Telescope"] = f["/telescope"].value
         if "instrument" in f:
-            events["Instrument"] = f["/instrument"].value
-                            
-        events["xsky"] = f["/xsky"][:]
-        events["ysky"] = f["/ysky"][:]
+            parameters["Instrument"] = f["/instrument"].value
+
         events["xpix"] = f["/xpix"][:]
         events["ypix"] = f["/ypix"][:]
         events["eobs"] = f["/eobs"][:]
@@ -823,13 +863,13 @@
             events["PI"] = f["/pi"][:]
         if "pha" in f:
             events["PHA"] = f["/pha"][:]
-        events["sky_center"] = f["/sky_center"][:]
-        events["dtheta"] = f["/dtheta"].value
-        events["pix_center"] = f["/pix_center"][:]
+        parameters["sky_center"] = f["/sky_center"][:]
+        parameters["dtheta"] = f["/dtheta"].value
+        parameters["pix_center"] = f["/pix_center"][:]
         
         f.close()
         
-        return cls(events)
+        return cls(events, parameters)
 
     @classmethod
     def from_fits_file(cls, fitsfile):
@@ -841,35 +881,34 @@
         tblhdu = hdulist["EVENTS"]
         
         events = {}
-
-        events["ExposureTime"] = tblhdu.header["EXPOSURE"]
-        events["Area"] = tblhdu.header["AREA"]
-        events["Redshift"] = tblhdu.header["REDSHIFT"]
-        events["AngularDiameterDistance"] = tblhdu.header["D_A"]
+        parameters = {}
+        
+        parameters["ExposureTime"] = tblhdu.header["EXPOSURE"]
+        parameters["Area"] = tblhdu.header["AREA"]
+        parameters["Redshift"] = tblhdu.header["REDSHIFT"]
+        parameters["AngularDiameterDistance"] = tblhdu.header["D_A"]
         if "RMF" in tblhdu.header:
-            events["RMF"] = tblhdu["RMF"]
+            parameters["RMF"] = tblhdu["RMF"]
         if "ARF" in tblhdu.header:
-            events["ARF"] = tblhdu["ARF"]
+            parameters["ARF"] = tblhdu["ARF"]
         if "CHANTYPE" in tblhdu.header:
-            events["ChannelType"] = tblhdu["CHANTYPE"]
+            parameters["ChannelType"] = tblhdu["CHANTYPE"]
         if "TELESCOP" in tblhdu.header:
-            events["Telescope"] = tblhdu["TELESCOP"]
+            parameters["Telescope"] = tblhdu["TELESCOP"]
         if "INSTRUME" in tblhdu.header:
-            events["Instrument"] = tblhdu["INSTRUME"]
-        events["sky_center"] = np.array([tblhdu["TCRVL2"],tblhdu["TCRVL3"]])
-        events["pix_center"] = np.array([tblhdu["TCRVL2"],tblhdu["TCRVL3"]])
-        events["dtheta"] = tblhdu["TCRVL3"]
+            parameters["Instrument"] = tblhdu["INSTRUME"]
+        parameters["sky_center"] = np.array([tblhdu["TCRVL2"],tblhdu["TCRVL3"]])
+        parameters["pix_center"] = np.array([tblhdu["TCRVL2"],tblhdu["TCRVL3"]])
+        parameters["dtheta"] = tblhdu["TCRVL3"]
         events["xpix"] = tblhdu.data.field("X")
         events["ypix"] = tblhdu.data.field("Y")
-        events["xsky"] = tblhdu.data.field("XSKY")
-        events["ysky"] = tblhdu.data.field("YSKY")
         events["eobs"] = tblhdu.data.field("ENERGY")/1000. # Convert to keV
         if "PI" in tblhdu.columns.names:
             events["PI"] = tblhdu.data.field("PI")
         if "PHA" in tblhdu.columns.names:
             events["PHA"] = tblhdu.data.field("PHA")
         
-        return cls(events)
+        return cls(events, parameters)
 
     @classmethod
     def join_events(cls, events1, events2):
@@ -880,18 +919,16 @@
         for item1, item2 in zip(events1.items(), events2.items()):
             k1, v1 = item1
             k2, v2 = item2
-            if isinstance(v1, np.ndarray):
-                events[k1] = np.concatenate([v1,v2])
-            else:
-                events[k1] = v1            
-        return cls(events)
+            events[k1] = np.concatenate([v1,v2])
+        
+        return cls(events, events1.parameters)
 
     def convolve_with_response(self, respfile):
         """
         Convolve the events with a RMF file *respfile*.
         """
         mylog.warning("This routine has not been tested to work with all RMFs. YMMV.")
-        if not "ARF" in self.events:
+        if not "ARF" in self.parameters:
             mylog.warning("Photons have not been processed with an"+
                           " auxiliary response file. Spectral fitting"+
                           " may be inaccurate.")
@@ -908,8 +945,8 @@
         mylog.info("Energy limits: %g %g" % (min(tblhdu.data["ENERG_LO"]),
                                              max(tblhdu.data["ENERG_HI"])))
 
-        if "ARF" in self.events:
-            f = pyfits.open(self.events["ARF"])
+        if "ARF" in self.parameters:
+            f = pyfits.open(self.parameters["ARF"])
             elo = f["SPECRESP"].data.field("ENERG_LO")
             ehi = f["SPECRESP"].data.field("ENERG_HI")
             f.close()
@@ -928,10 +965,8 @@
         eidxs = np.argsort(self.events["eobs"])
 
         phEE = self.events["eobs"][eidxs]
-        phXS = self.events["xsky"][eidxs]
-        phYS = self.events["ysky"][eidxs]
-        phXP = self.events["xpix"][eidxs]
-        phYP = self.events["ypix"][eidxs]
+        phXX = self.events["xpix"][eidxs]
+        phYY = self.events["ypix"][eidxs]
 
         detectedChannels = []
         pindex = 0
@@ -978,16 +1013,14 @@
         
         dchannel = np.array(detectedChannels)
 
-        self.events["xsky"] = phXS
-        self.events["ysky"] = phYS
-        self.events["xpix"] = phXP
-        self.events["ypix"] = phYP
+        self.events["xpix"] = phXX
+        self.events["ypix"] = phYY
         self.events["eobs"] = phEE
         self.events[tblhdu.header["CHANTYPE"]] = dchannel.astype(int)
-        self.events["RMF"] = respfile
-        self.events["ChannelType"] = tblhdu.header["CHANTYPE"]
-        self.events["Telescope"] = tblhdu.header["TELESCOP"]
-        self.events["Instrument"] = tblhdu.header["INSTRUME"]
+        self.parameters["RMF"] = respfile
+        self.parameters["ChannelType"] = tblhdu.header["CHANTYPE"]
+        self.parameters["Telescope"] = tblhdu.header["TELESCOP"]
+        self.parameters["Instrument"] = tblhdu.header["INSTRUME"]
         
     @parallel_root_only
     def write_fits_file(self, fitsfile, clobber=False):
@@ -1004,22 +1037,18 @@
                              array=self.events["xpix"])
         col3 = pyfits.Column(name='Y', format='D', unit='pixel',
                              array=self.events["ypix"])
-        col4 = pyfits.Column(name='XSKY', format='D', unit='deg',
-                             array=self.events["xsky"])
-        col5 = pyfits.Column(name='YSKY', format='D', unit='deg',
-                             array=self.events["ysky"])
 
-        cols = [col1, col2, col3, col4, col5]
+        cols = [col1, col2, col3]
 
-        if self.events.has_key("ChannelType"):
-             chantype = self.events["ChannelType"]
+        if self.parameters.has_key("ChannelType"):
+             chantype = self.parameters["ChannelType"]
              if chantype == "PHA":
                   cunit="adu"
              elif chantype == "PI":
                   cunit="Chan"
-             col6 = pyfits.Column(name=chantype.upper(), format='1J',
+             col4 = pyfits.Column(name=chantype.upper(), format='1J',
                                   unit=cunit, array=self.events[chantype])
-             cols.append(col6)
+             cols.append(col4)
             
         coldefs = pyfits.ColDefs(cols)
         tbhdu = pyfits.new_table(coldefs)
@@ -1031,33 +1060,33 @@
         tbhdu.header.update("MFORM2", "RA,DEC")
         tbhdu.header.update("TCTYP2", "RA---TAN")
         tbhdu.header.update("TCTYP3", "DEC--TAN")
-        tbhdu.header.update("TCRVL2", self.events["sky_center"][0])
-        tbhdu.header.update("TCRVL3", self.events["sky_center"][1])
-        tbhdu.header.update("TCDLT2", -self.events["dtheta"])
-        tbhdu.header.update("TCDLT3", self.events["dtheta"])
-        tbhdu.header.update("TCRPX2", self.events["pix_center"][0])
-        tbhdu.header.update("TCRPX3", self.events["pix_center"][1])
+        tbhdu.header.update("TCRVL2", self.parameters["sky_center"][0])
+        tbhdu.header.update("TCRVL3", self.parameters["sky_center"][1])
+        tbhdu.header.update("TCDLT2", -self.parameters["dtheta"])
+        tbhdu.header.update("TCDLT3", self.parameters["dtheta"])
+        tbhdu.header.update("TCRPX2", self.parameters["pix_center"][0])
+        tbhdu.header.update("TCRPX3", self.parameters["pix_center"][1])
         tbhdu.header.update("TLMIN2", 0.5)
         tbhdu.header.update("TLMIN3", 0.5)
-        tbhdu.header.update("TLMAX2", 2.*self.events["pix_center"][0]-0.5)
-        tbhdu.header.update("TLMAX3", 2.*self.events["pix_center"][1]-0.5)
-        tbhdu.header.update("EXPOSURE", self.events["ExposureTime"])
-        tbhdu.header.update("AREA", self.events["Area"])
-        tbhdu.header.update("D_A", self.events["AngularDiameterDistance"])
-        tbhdu.header.update("REDSHIFT", self.events["Redshift"])
+        tbhdu.header.update("TLMAX2", 2.*self.parameters["pix_center"][0]-0.5)
+        tbhdu.header.update("TLMAX3", 2.*self.parameters["pix_center"][1]-0.5)
+        tbhdu.header.update("EXPOSURE", self.parameters["ExposureTime"])
+        tbhdu.header.update("AREA", self.parameters["Area"])
+        tbhdu.header.update("D_A", self.parameters["AngularDiameterDistance"])
+        tbhdu.header.update("REDSHIFT", self.parameters["Redshift"])
         tbhdu.header.update("HDUVERS", "1.1.0")
         tbhdu.header.update("RADECSYS", "FK5")
         tbhdu.header.update("EQUINOX", 2000.0)
-        if "RMF" in self.events:
-            tbhdu.header.update("RMF", self.events["RMF"])
-        if "ARF" in self.events:
-            tbhdu.header.update("ARF", self.events["ARF"])
-        if "ChannelType" in self.events:
-            tbhdu.header.update("CHANTYPE", self.events["ChannelType"])
-        if "Telescope" in self.events:
-            tbhdu.header.update("TELESCOP", self.events["Telescope"])
-        if "Instrument" in self.events:
-            tbhdu.header.update("INSTRUME", self.events["Instrument"])
+        if "RMF" in self.parameters:
+            tbhdu.header.update("RMF", self.parameters["RMF"])
+        if "ARF" in self.parameters:
+            tbhdu.header.update("ARF", self.parameters["ARF"])
+        if "ChannelType" in self.parameters:
+            tbhdu.header.update("CHANTYPE", self.parameters["ChannelType"])
+        if "Telescope" in self.parameters:
+            tbhdu.header.update("TELESCOP", self.parameters["Telescope"])
+        if "Instrument" in self.parameters:
+            tbhdu.header.update("INSTRUME", self.parameters["Instrument"])
             
         tbhdu.writeto(fitsfile, clobber=clobber)
 
@@ -1080,7 +1109,7 @@
             The maximum energy of the photons to save in keV.
         """
 
-        if isinstance(self.events["Area"], basestring):
+        if isinstance(self.parameters["Area"], basestring):
              mylog.error("Writing SIMPUT files is only supported if you didn't convolve with an ARF.")
              raise TypeError
         
@@ -1090,14 +1119,15 @@
             emax = self.events["eobs"].max()*1.05
 
         idxs = np.logical_and(self.events["eobs"] >= emin, self.events["eobs"] <= emax)
-        flux = erg_per_keV*np.sum(self.events["eobs"][idxs])/self.events["ExposureTime"]/self.events["Area"]
+        flux = erg_per_keV*np.sum(self.events["eobs"][idxs]) / \
+               self.parameters["ExposureTime"]/self.parameters["Area"]
         
         col1 = pyfits.Column(name='ENERGY', format='E',
-                             array=self.events["eobs"])
+                             array=self["eobs"])
         col2 = pyfits.Column(name='DEC', format='D',
-                             array=self.events["ysky"])
+                             array=self["xsky"])
         col3 = pyfits.Column(name='RA', format='D',
-                             array=self.events["xsky"])
+                             array=self["ysky"])
 
         coldefs = pyfits.ColDefs([col1, col2, col3])
 
@@ -1119,8 +1149,8 @@
         tbhdu.writeto(phfile, clobber=clobber)
 
         col1 = pyfits.Column(name='SRC_ID', format='J', array=np.array([1]).astype("int32"))
-        col2 = pyfits.Column(name='RA', format='D', array=np.array([self.center[0]]))
-        col3 = pyfits.Column(name='DEC', format='D', array=np.array([self.center[1]]))
+        col2 = pyfits.Column(name='RA', format='D', array=np.array([self.parameters["sky_center"][0]]))
+        col3 = pyfits.Column(name='DEC', format='D', array=np.array([self.parameters["sky_center"][1]]))
         col4 = pyfits.Column(name='E_MIN', format='D', array=np.array([emin]))
         col5 = pyfits.Column(name='E_MAX', format='D', array=np.array([emax]))
         col6 = pyfits.Column(name='FLUX', format='D', array=np.array([flux]))
@@ -1155,23 +1185,21 @@
         """
         f = h5py.File(h5file, "w")
 
-        f.create_dataset("/exp_time", data=self.events["ExposureTime"])
-        f.create_dataset("/area", data=self.events["Area"])
-        f.create_dataset("/redshift", data=self.events["Redshift"])
-        f.create_dataset("/d_a", data=self.events["AngularDiameterDistance"])        
-        if "ARF" in self.events:
-            f.create_dataset("/arf", data=self.events["ARF"])
-        if "RMF" in self.events:
-            f.create_dataset("/rmf", data=self.events["RMF"])
-        if "ChannelType" in self.events:
-            f.create_dataset("/channel_type", data=self.events["ChannelType"])
-        if "Telescope" in self.events:
-            f.create_dataset("/telescope", data=self.events["Telescope"])
-        if "Instrument" in self.events:
-            f.create_dataset("/instrument", data=self.events["Instrument"])
+        f.create_dataset("/exp_time", data=self.parameters["ExposureTime"])
+        f.create_dataset("/area", data=self.parameters["Area"])
+        f.create_dataset("/redshift", data=self.parameters["Redshift"])
+        f.create_dataset("/d_a", data=self.parameters["AngularDiameterDistance"])        
+        if "ARF" in self.parameters:
+            f.create_dataset("/arf", data=self.parameters["ARF"])
+        if "RMF" in self.parameters:
+            f.create_dataset("/rmf", data=self.parameters["RMF"])
+        if "ChannelType" in self.parameters:
+            f.create_dataset("/channel_type", data=self.parameters["ChannelType"])
+        if "Telescope" in self.parameters:
+            f.create_dataset("/telescope", data=self.parameters["Telescope"])
+        if "Instrument" in self.parameters:
+            f.create_dataset("/instrument", data=self.parameters["Instrument"])
                             
-        f.create_dataset("/xsky", data=self.events["xsky"])
-        f.create_dataset("/ysky", data=self.events["ysky"])
         f.create_dataset("/xpix", data=self.events["xpix"])
         f.create_dataset("/ypix", data=self.events["ypix"])
         f.create_dataset("/eobs", data=self.events["eobs"])
@@ -1179,9 +1207,9 @@
             f.create_dataset("/pi", data=self.events["PI"])                  
         if "PHA" in self.events:
             f.create_dataset("/pha", data=self.events["PHA"])                  
-        f.create_dataset("/sky_center", data=self.events["sky_center"])
-        f.create_dataset("/pix_center", data=self.events["pix_center"])
-        f.create_dataset("/dtheta", data=self.events["dtheta"])
+        f.create_dataset("/sky_center", data=self.parameters["sky_center"])
+        f.create_dataset("/pix_center", data=self.parameters["pix_center"])
+        f.create_dataset("/dtheta", data=self.parameters["dtheta"])
 
         f.close()
 
@@ -1214,8 +1242,8 @@
 
         mask = np.logical_and(mask_emin, mask_emax)
 
-        nx = int(2*self.events["pix_center"][0]-1.)
-        ny = int(2*self.events["pix_center"][1]-1.)
+        nx = int(2*self.parameters["pix_center"][0]-1.)
+        ny = int(2*self.parameters["pix_center"][1]-1.)
         
         xbins = np.linspace(0.5, float(nx)+0.5, nx+1, endpoint=True)
         ybins = np.linspace(0.5, float(ny)+0.5, ny+1, endpoint=True)
@@ -1232,13 +1260,13 @@
         hdu.header.update("CTYPE2", "DEC--TAN")
         hdu.header.update("CRPIX1", 0.5*(nx+1))
         hdu.header.update("CRPIX2", 0.5*(nx+1))                
-        hdu.header.update("CRVAL1", self.events["sky_center"][0])
-        hdu.header.update("CRVAL2", self.events["sky_center"][1])
+        hdu.header.update("CRVAL1", self.parameters["sky_center"][0])
+        hdu.header.update("CRVAL2", self.parameters["sky_center"][1])
         hdu.header.update("CUNIT1", "deg")
         hdu.header.update("CUNIT2", "deg")
-        hdu.header.update("CDELT1", -self.events["dtheta"])
-        hdu.header.update("CDELT2", self.events["dtheta"])
-        hdu.header.update("EXPOSURE", self.events["ExposureTime"])
+        hdu.header.update("CDELT1", -self.parameters["dtheta"])
+        hdu.header.update("CDELT2", self.parameters["dtheta"])
+        hdu.header.update("EXPOSURE", self.parameters["ExposureTime"])
         
         hdu.writeto(imagefile, clobber=clobber)
                                     
@@ -1272,12 +1300,12 @@
             spec, ee = np.histogram(espec, bins=nchan, range=range)
             bins = 0.5*(ee[1:]+ee[:-1])
         else:
-            if not self.events.has_key("ChannelType"):
+            if not self.parameters.has_key("ChannelType"):
                 mylog.error("These events were not convolved with an RMF. Set energy_bins=True.")
                 raise KeyError
-            spectype = self.events["ChannelType"]
+            spectype = self.parameters["ChannelType"]
             espec = self.events[spectype]
-            f = pyfits.open(self.events["RMF"])
+            f = pyfits.open(self.parameters["RMF"])
             nchan = int(f[1].header["DETCHANS"])
             try:
                 cmin = int(f[1].header["TLMIN4"])
@@ -1301,7 +1329,7 @@
         col1 = pyfits.Column(name='CHANNEL', format='1J', array=bins)
         col2 = pyfits.Column(name=spectype.upper(), format='1D', array=bins.astype("float64"))
         col3 = pyfits.Column(name='COUNTS', format='1J', array=spec.astype("int32"))
-        col4 = pyfits.Column(name='COUNT_RATE', format='1D', array=spec/self.events["ExposureTime"])
+        col4 = pyfits.Column(name='COUNT_RATE', format='1D', array=spec/self.parameters["ExposureTime"])
         
         coldefs = pyfits.ColDefs([col1, col2, col3, col4])
         
@@ -1311,8 +1339,8 @@
         if not energy_bins:
             tbhdu.header.update("DETCHANS", spec.shape[0])
             tbhdu.header.update("TOTCTS", spec.sum())
-            tbhdu.header.update("EXPOSURE", self.events["ExposureTime"])
-            tbhdu.header.update("LIVETIME", self.events["ExposureTime"])        
+            tbhdu.header.update("EXPOSURE", self.parameters["ExposureTime"])
+            tbhdu.header.update("LIVETIME", self.parameters["ExposureTime"])        
             tbhdu.header.update("CONTENT", spectype)
             tbhdu.header.update("HDUCLASS", "OGIP")
             tbhdu.header.update("HDUCLAS1", "SPECTRUM")
@@ -1325,20 +1353,20 @@
             tbhdu.header.update("BACKFILE", "none")
             tbhdu.header.update("CORRFILE", "none")
             tbhdu.header.update("POISSERR", True)
-            if self.events.has_key("RMF"):
-                 tbhdu.header.update("RESPFILE", self.events["RMF"])
+            if self.parameters.has_key("RMF"):
+                 tbhdu.header.update("RESPFILE", self.parameters["RMF"])
             else:
                  tbhdu.header.update("RESPFILE", "none")
-            if self.events.has_key("ARF"):
-                tbhdu.header.update("ANCRFILE", self.events["ARF"])
+            if self.parameters.has_key("ARF"):
+                tbhdu.header.update("ANCRFILE", self.parameters["ARF"])
             else:        
                 tbhdu.header.update("ANCRFILE", "none")
-            if self.events.has_key("Telescope"):
-                tbhdu.header.update("TELESCOP", self.events["Telescope"])
+            if self.parameters.has_key("Telescope"):
+                tbhdu.header.update("TELESCOP", self.parameters["Telescope"])
             else:
                 tbhdu.header.update("TELESCOP", "none")
-            if self.events.has_key("Instrument"):
-                tbhdu.header.update("INSTRUME", self.events["Instrument"])
+            if self.parameters.has_key("Instrument"):
+                tbhdu.header.update("INSTRUME", self.parameters["Instrument"])
             else:
                 tbhdu.header.update("INSTRUME", "none")
             tbhdu.header.update("AREASCAL", 1.0)

diff -r 5c2196a9b9a62563d2f984c080e0cbf3bd6c835c -r 0bd6cac9ce338d1c750b98df38296f0d807324e2 yt/analysis_modules/photon_simulator/spectral_models.py
--- /dev/null
+++ b/yt/analysis_modules/photon_simulator/spectral_models.py
@@ -0,0 +1,321 @@
+"""
+Photon emission and absoprtion models for use with the
+photon simulator.
+"""
+
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
+import numpy as np
+import os
+from yt.funcs import *
+import h5py
+try:
+    import astropy.io.fits as pyfits
+    import xspec
+    from scipy.integrate import cumtrapz
+    from scipy import stats        
+except ImportError:
+    pass
+    
+from yt.utilities.physical_constants import hcgs, clight, erg_per_keV, amu_cgs
+
+hc = 1.0e8*hcgs*clight/erg_per_keV
+
+class SpectralModel(object):
+
+    def __init__(self, emin, emax, nchan):
+        self.emin = emin
+        self.emax = emax
+        self.nchan = nchan
+        self.ebins = np.linspace(emin, emax, nchan+1)
+        self.de = np.diff(self.ebins)
+        self.emid = 0.5*(self.ebins[1:]+self.ebins[:-1])
+        
+    def prepare(self):
+        pass
+    
+    def get_spectrum(self):
+        pass
+                                                        
+class XSpecThermalModel(SpectralModel):
+    r"""
+    Initialize a thermal gas emission model from PyXspec.
+    
+    Parameters
+    ----------
+
+    model_name : string
+        The name of the thermal emission model.
+    emin : float
+        The minimum energy for the spectral model.
+    emax : float
+        The maximum energy for the spectral model.
+    nchan : integer
+        The number of channels in the spectral model.
+
+    Examples
+    --------
+    >>> mekal_model = XSpecThermalModel("mekal", 0.05, 50.0, 1000)
+    """
+    def __init__(self, model_name, emin, emax, nchan):
+        self.model_name = model_name
+        PhotonModel.__init__(self, emin, emax, nchan)
+        
+    def prepare(self):
+        """
+        Prepare the thermal model for execution.
+        """
+        xspec.Xset.chatter = 0
+        xspec.AllModels.setEnergies("%f %f %d lin" %
+                                    (self.emin, self.emax, self.nchan))
+        self.model = xspec.Model(self.model_name)
+        if self.model_name == "bremss":
+            self.norm = 3.02e-15
+        else:
+            self.norm = 1.0e-14
+        
+    def get_spectrum(self, kT):
+        """
+        Get the thermal emission spectrum given a temperature *kT* in keV. 
+        """
+        m = getattr(self.model,self.model_name)
+        m.kT = kT
+        m.Abundanc = 0.0
+        m.norm = 1.0
+        m.Redshift = 0.0
+        cosmic_spec = self.norm*np.array(self.model.values(0))
+        m.Abundanc = 1.0
+        if self.model_name == "bremss":
+            metal_spec = np.zeros((self.nchan))
+        else:
+            metal_spec = self.norm*np.array(self.model.values(0)) - cosmic_spec
+        return cosmic_spec, metal_spec
+        
+class XSpecAbsorbModel(SpectralModel):
+    r"""
+    Initialize an absorption model from PyXspec.
+    
+    Parameters
+    ----------
+
+    model_name : string
+        The name of the absorption model.
+    nH : float
+        The foreground column density *nH* in units of 10^22 cm^{-2}.
+    emin : float, optional
+        The minimum energy for the spectral model.
+    emax : float, optional
+        The maximum energy for the spectral model.
+    nchan : integer, optional
+        The number of channels in the spectral model.
+
+    Examples
+    --------
+    >>> abs_model = XSpecAbsorbModel("wabs", 0.1)
+    """
+    def __init__(self, model_name, nH, emin=0.01, emax=50.0, nchan=100000):
+        self.model_name = model_name
+        self.nH = nH
+        PhotonModel.__init__(self, emin, emax, nchan)
+        
+    def prepare(self):
+        """
+        Prepare the absorption model for execution.
+        """
+        xspec.Xset.chatter = 0
+        xspec.AllModels.setEnergies("%f %f %d lin" %
+                                    (self.emin, self.emax, self.nchan))
+        self.model = xspec.Model(self.model_name+"*powerlaw")
+        self.model.powerlaw.norm = self.nchan/(self.emax-self.emin)
+        self.model.powerlaw.PhoIndex = 0.0
+
+    def get_spectrum(self):
+        """
+        Get the absorption spectrum.
+        """
+        m = getattr(self.model,self.model_name)
+        m.nH = self.nH
+        return np.array(self.model.values(0))
+
+class TableApecModel(SpectralModel):
+    r"""
+    Initialize a thermal gas emission model from the AtomDB APEC tables
+    available at http://www.atomdb.org. This code borrows heavily from Python
+    routines used to read the APEC tables developed by Adam Foster at the
+    CfA (afoster at cfa.harvard.edu). 
+    
+    Parameters
+    ----------
+
+    apec_root : string
+        The directory root where the APEC model files are stored.
+    emin : float
+        The minimum energy for the spectral model.
+    emax : float
+        The maximum energy for the spectral model.
+    nchan : integer
+        The number of channels in the spectral model.
+    apec_vers : string, optional
+        The version identifier string for the APEC files, e.g.
+        "2.0.2"
+    thermal_broad : boolean, optional
+        Whether to apply thermal broadening to spectral lines. Only should
+        be used if you are attemping to simulate a high-spectral resolution
+        detector.
+
+    Examples
+    --------
+    >>> apec_model = TableApecModel("/Users/jzuhone/Data/atomdb_v2.0.2/", 0.05, 50.0,
+                                    1000, thermal_broad=True)
+    """
+    def __init__(self, apec_root, emin, emax, nchan,
+                 apec_vers="2.0.2", thermal_broad=False):
+        self.apec_root = apec_root
+        self.apec_prefix = "apec_v"+apec_vers
+        self.cocofile = os.path.join(self.apec_root,
+                                     self.apec_prefix+"_coco.fits")
+        self.linefile = os.path.join(self.apec_root,
+                                     self.apec_prefix+"_line.fits")
+        PhotonModel.__init__(self, emin, emax, nchan)
+        self.wvbins = hc/self.ebins[::-1]
+        # H, He, and trace elements
+        self.cosmic_elem = [1,2,3,4,5,9,11,15,17,19,21,22,23,24,25,27,29,30]
+        # Non-trace metals
+        self.metal_elem = [6,7,8,10,12,13,14,16,18,20,26,28]
+        self.thermal_broad = thermal_broad
+        self.A = np.array([0.0,1.00794,4.00262,6.941,9.012182,10.811,
+                           12.0107,14.0067,15.9994,18.9984,20.1797,
+                           22.9898,24.3050,26.9815,28.0855,30.9738,
+                           32.0650,35.4530,39.9480,39.0983,40.0780,
+                           44.9559,47.8670,50.9415,51.9961,54.9380,
+                           55.8450,58.9332,58.6934,63.5460,65.3800])
+        
+    def prepare(self):
+        """
+        Prepare the thermal model for execution.
+        """
+        try:
+            self.line_handle = pyfits.open(self.linefile)
+        except IOError:
+            mylog.error("LINE file %s does not exist" % (self.linefile))
+        try:
+            self.coco_handle = pyfits.open(self.cocofile)
+        except IOError:
+            mylog.error("COCO file %s does not exist" % (self.cocofile))
+        self.Tvals = self.line_handle[1].data.field("kT")
+        self.dTvals = np.diff(self.Tvals)
+        self.minlam = self.wvbins.min()
+        self.maxlam = self.wvbins.max()
+    
+    def _make_spectrum(self, element, tindex):
+        
+        tmpspec = np.zeros((self.nchan))
+        
+        i = np.where((self.line_handle[tindex].data.field('element')==element) &
+                     (self.line_handle[tindex].data.field('lambda') > self.minlam) &
+                     (self.line_handle[tindex].data.field('lambda') < self.maxlam))[0]
+
+        vec = np.zeros((self.nchan))
+        E0 = hc/self.line_handle[tindex].data.field('lambda')[i]
+        amp = self.line_handle[tindex].data.field('epsilon')[i]
+        if self.thermal_broad:
+            vec = np.zeros((self.nchan))
+            sigma = E0*np.sqrt(self.Tvals[tindex]*erg_per_keV/(self.A[element]*amu_cgs))/clight
+            for E, sig, a in zip(E0, sigma, amp):
+                cdf = stats.norm(E,sig).cdf(self.ebins)
+                vec += np.diff(cdf)*a
+        else:
+            ie = np.searchsorted(self.ebins, E0, side='right')-1
+            for i,a in zip(ie,amp): vec[i] += a
+        tmpspec += vec
+
+        ind = np.where((self.coco_handle[tindex].data.field('Z')==element) &
+                       (self.coco_handle[tindex].data.field('rmJ')==0))[0]
+        if len(ind)==0:
+            return tmpspec
+        else:
+            ind=ind[0]
+                                                    
+        n_cont=self.coco_handle[tindex].data.field('N_Cont')[ind]
+        e_cont=self.coco_handle[tindex].data.field('E_Cont')[ind][:n_cont]
+        continuum = self.coco_handle[tindex].data.field('Continuum')[ind][:n_cont]
+
+        tmpspec += np.interp(self.emid, e_cont, continuum)*self.de
+        
+        n_pseudo=self.coco_handle[tindex].data.field('N_Pseudo')[ind]
+        e_pseudo=self.coco_handle[tindex].data.field('E_Pseudo')[ind][:n_pseudo]
+        pseudo = self.coco_handle[tindex].data.field('Pseudo')[ind][:n_pseudo]
+        
+        tmpspec += np.interp(self.emid, e_pseudo, pseudo)*self.de
+        
+        return tmpspec
+
+    def get_spectrum(self, kT):
+        """
+        Get the thermal emission spectrum given a temperature *kT* in keV. 
+        """
+        cspec_l = np.zeros((self.nchan))
+        mspec_l = np.zeros((self.nchan))
+        cspec_r = np.zeros((self.nchan))
+        mspec_r = np.zeros((self.nchan))
+        tindex = np.searchsorted(self.Tvals, kT)-1
+        dT = (kT-self.Tvals[tindex])/self.dTvals[tindex]
+        # First do H,He, and trace elements
+        for elem in self.cosmic_elem:
+            cspec_l += self._make_spectrum(elem, tindex+2)
+            cspec_r += self._make_spectrum(elem, tindex+3)            
+        # Next do the metals
+        for elem in self.metal_elem:
+            mspec_l += self._make_spectrum(elem, tindex+2)
+            mspec_r += self._make_spectrum(elem, tindex+3)
+        cosmic_spec = cspec_l*(1.-dT)+cspec_r*dT
+        metal_spec = mspec_l*(1.-dT)+mspec_r*dT        
+        return cosmic_spec, metal_spec
+
+class TableAbsorbModel(SpectralModel):
+    r"""
+    Initialize an absorption model from a table stored in an HDF5 file.
+    
+    Parameters
+    ----------
+
+    filename : string
+        The name of the table file.
+    nH : float
+        The foreground column density *nH* in units of 10^22 cm^{-2}.
+
+    Examples
+    --------
+    >>> abs_model = XSpecAbsorbModel("wabs", 0.1)
+    """
+
+    def __init__(self, filename, nH):
+        if not os.path.exists(filename):
+            raise IOError("File does not exist: %s." % filename)
+        self.filename = filename
+        f = h5py.File(self.filename,"r")
+        emin = f["energy"][:].min()
+        emax = f["energy"][:].max()
+        self.sigma = f["cross_section"][:]
+        nchan = self.sigma.shape[0]
+        f.close()
+        PhotonModel.__init__(self, emin, emax, nchan)
+        self.nH = nH*1.0e22
+        
+    def prepare(self):
+        """
+        Prepare the absorption model for execution.
+        """
+        pass
+        
+    def get_spectrum(self):
+        """
+        Get the absorption spectrum.
+        """
+        return np.exp(-self.sigma*self.nH)


https://bitbucket.org/yt_analysis/yt/commits/1a6425fa862b/
Changeset:   1a6425fa862b
Branch:      yt
User:        jzuhone
Date:        2013-10-22 19:08:11
Summary:     Continued photon_simulator refactor.
Affected #:  3 files

diff -r 0bd6cac9ce338d1c750b98df38296f0d807324e2 -r 1a6425fa862b3c61f208955f52bb8868db8be07a yt/analysis_modules/photon_simulator/api.py
--- a/yt/analysis_modules/photon_simulator/api.py
+++ b/yt/analysis_modules/photon_simulator/api.py
@@ -11,7 +11,8 @@
 #-----------------------------------------------------------------------------
 
 from .photon_models import \
-     ThermalModel
+     PhotonModel, \
+     ThermalPhotonModel
 
 from .photon_simulator import \
      PhotonList, \

diff -r 0bd6cac9ce338d1c750b98df38296f0d807324e2 -r 1a6425fa862b3c61f208955f52bb8868db8be07a yt/analysis_modules/photon_simulator/photon_models.py
--- /dev/null
+++ b/yt/analysis_modules/photon_simulator/photon_models.py
@@ -0,0 +1,181 @@
+"""
+Classes for specific photon models
+
+The algorithms used here are based off of the method used by the
+PHOX code (http://www.mpa-garching.mpg.de/~kdolag/Phox/),
+developed by Veronica Biffi and Klaus Dolag. References for
+PHOX may be found at:
+
+Biffi et al 2012: http://adsabs.harvard.edu/abs/2012MNRAS.420.3545B
+Biffi et al 2013: http://adsabs.harvard.edu/abs/2013MNRAS.428.1395B
+"""
+
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
+import numpy as np
+from yt.funcs import *
+from yt.utilities.physical_constants import \
+     mp, cm_per_km, K_per_keV
+from yt.utilities.parallel_tools.parallel_analysis_interface import \
+     communication_system
+
+N_TBIN = 10000
+TMIN = 8.08e-2
+TMAX = 50.
+
+comm = communication_system.communicators[-1]
+
+class PhotonModel(object):
+
+    def __init__(self):
+        pass
+
+    def __call__(self, data_source, parameters):
+        photons = {}
+        return photons
+
+class ThermalPhotonModel(PhotonModel):
+
+    def __init__(self, X_H, Zmet, spectral_model):
+        self.X_H = X_H
+        self.Zmet = Zmet
+        self.spectral_model = spectral_model
+
+    def __call__(self, data_source, parameters):
+        
+        pf = data_source.pf
+        
+        vol_scale = pf.units["cm"]**(-3)/np.prod(pf.domain_width)
+        
+        num_cells = data_source["Temperature"].shape[0]
+        start_c = comm.rank*num_cells/comm.size
+        end_c = (comm.rank+1)*num_cells/comm.size
+        
+        kT = data_source["Temperature"][start_c:end_c].copy()/K_per_keV
+        vol = data_source["CellVolume"][start_c:end_c].copy()
+        dx = data_source["dx"][start_c:end_c].copy()
+        EM = (data_source["Density"][start_c:end_c].copy()/mp)**2
+        EM *= 0.5*(1.+self.X_H)*self.X_H*vol
+    
+        data_source.clear_data()
+    
+        x = data_source["x"][start_c:end_c].copy()
+        y = data_source["y"][start_c:end_c].copy()
+        z = data_source["z"][start_c:end_c].copy()
+    
+        data_source.clear_data()
+        
+        vx = data_source["x-velocity"][start_c:end_c].copy()
+        vy = data_source["y-velocity"][start_c:end_c].copy()
+        vz = data_source["z-velocity"][start_c:end_c].copy()
+    
+        if isinstance(self.Zmet, basestring):
+            metalZ = data_source[self.Zmet][start_c:end_c].copy()
+        else:
+            metalZ = self.Zmet*np.ones(EM.shape)
+        
+        data_source.clear_data()
+
+        idxs = np.argsort(kT)
+        dshape = idxs.shape
+
+        kT_bins = np.linspace(TMIN, max(kT[idxs][-1], TMAX), num=N_TBIN+1)
+        dkT = kT_bins[1]-kT_bins[0]
+        kT_idxs = np.digitize(kT[idxs], kT_bins)
+        kT_idxs = np.minimum(np.maximum(1, kT_idxs), N_TBIN) - 1
+        bcounts = np.bincount(kT_idxs).astype("int")
+        bcounts = bcounts[bcounts > 0]
+        n = int(0)
+        bcell = []
+        ecell = []
+        for bcount in bcounts:
+            bcell.append(n)
+            ecell.append(n+bcount)
+            n += bcount
+        kT_idxs = np.unique(kT_idxs)
+        
+        self.spectral_model.prepare()
+        energy = self.spectral_model.ebins
+    
+        cell_em = EM[idxs]*vol_scale
+        cell_vol = vol[idxs]*vol_scale
+    
+        number_of_photons = np.zeros(dshape, dtype='uint64')
+        energies = []
+    
+        u = np.random.random(cell_em.shape)
+        
+        pbar = get_pbar("Generating Photons", dshape[0])
+
+        for i, ikT in enumerate(kT_idxs):
+
+            ncells = int(bcounts[i])
+            ibegin = bcell[i]
+            iend = ecell[i]
+            kT = kT_bins[ikT] + 0.5*dkT
+        
+            em_sum_c = cell_em[ibegin:iend].sum()
+            em_sum_m = (metalZ[ibegin:iend]*cell_em[ibegin:iend]).sum()
+            
+            cspec, mspec = self.spectral_model.get_spectrum(kT)
+            cspec *= dist_fac*em_sum_c/vol_scale
+            mspec *= dist_fac*em_sum_m/vol_scale
+        
+            cumspec_c = np.cumsum(cspec)
+            counts_c = cumspec_c[:]/cumspec_c[-1]
+            counts_c = np.insert(counts_c, 0, 0.0)
+            tot_ph_c = cumspec_c[-1]*area*exp_time
+
+            cumspec_m = np.cumsum(mspec)
+            counts_m = cumspec_m[:]/cumspec_m[-1]
+            counts_m = np.insert(counts_m, 0, 0.0)
+            tot_ph_m = cumspec_m[-1]*area*exp_time
+        
+            for icell in xrange(ibegin, iend):
+            
+                cell_norm_c = tot_ph_c*cell_em[icell]/em_sum_c
+                cell_n_c = np.uint64(cell_norm_c) + np.uint64(np.modf(cell_norm_c)[0] >= u[icell])
+            
+                cell_norm_m = tot_ph_m*metalZ[icell]*cell_em[icell]/em_sum_m
+                cell_n_m = np.uint64(cell_norm_m) + np.uint64(np.modf(cell_norm_m)[0] >= u[icell])
+            
+                cell_n = cell_n_c + cell_n_m
+
+                if cell_n > 0:
+                    number_of_photons[icell] = cell_n
+                    randvec_c = np.random.uniform(size=cell_n_c)
+                    randvec_c.sort()
+                    randvec_m = np.random.uniform(size=cell_n_m)
+                    randvec_m.sort()
+                    cell_e_c = np.interp(randvec_c, counts_c, energy)
+                    cell_e_m = np.interp(randvec_m, counts_m, energy)
+                    energies.append(np.concatenate([cell_e_c,cell_e_m]))
+                
+                pbar.update(icell)
+
+        pbar.finish()
+            
+        active_cells = number_of_photons > 0
+        idxs = idxs[active_cells]
+        
+        photons = {}
+
+        src_ctr = parameters["center"]
+        
+        photons["x"] = (x[idxs]-src_ctr[0])*pf.units["kpc"]
+        photons["y"] = (y[idxs]-src_ctr[1])*pf.units["kpc"]
+        photons["z"] = (z[idxs]-src_ctr[2])*pf.units["kpc"]
+        photons["vx"] = vx[idxs]/cm_per_km
+        photons["vy"] = vy[idxs]/cm_per_km
+        photons["vz"] = vz[idxs]/cm_per_km
+        photons["dx"] = dx[idxs]*pf.units["kpc"]
+        photons["NumberOfPhotons"] = number_of_photons[active_cells]
+        photons["Energy"] = np.concatenate(energies)
+    
+        return photons

diff -r 0bd6cac9ce338d1c750b98df38296f0d807324e2 -r 1a6425fa862b3c61f208955f52bb8868db8be07a yt/analysis_modules/photon_simulator/photon_simulator.py
--- a/yt/analysis_modules/photon_simulator/photon_simulator.py
+++ b/yt/analysis_modules/photon_simulator/photon_simulator.py
@@ -42,8 +42,7 @@
 
 comm = communication_system.communicators[-1]
     
-class PhotonList(object):
-                                                                                                                                                                                                                                                            
+class PhotonList(object):                                                                                                                                                                                                                                                            
     def __init__(self, photons, parameters, cosmo, p_bins):
         self.photons = photons
         self.parameters = parameters
@@ -356,7 +355,7 @@
     @classmethod
     def from_scratch(cls, data_source, redshift, area,
                      exp_time, gen_func, parameters=None,
-                     center="c", dist=None, cosmology=None):
+                     center=None, dist=None, cosmology=None):
         """
         Initialize a PhotonList from a user-provided model. The idea is
         to give the user full flexibility. The redshift, collecting area,
@@ -447,12 +446,14 @@
             redshift = 0.0
 
         if center == "c":
-            src_ctr = pf.domain_center
+            parameters["center"] = pf.domain_center
         elif center == "max":
-            src_ctr = pf.h.find_max("Density")[-1]
+            parameters["center"] = pf.h.find_max("Density")[-1]
         elif iterable(center):
-            src_ctr = center
-                                                            
+            parameters["center"] = center
+        elif center is None:
+            center = data_source.get_field_parameter("center")
+            
         parameters["FiducialExposureTime"] = exp_time
         parameters["FiducialArea"] = area
         parameters["FiducialRedshift"] = redshift


https://bitbucket.org/yt_analysis/yt/commits/36257401694f/
Changeset:   36257401694f
Branch:      yt
User:        jzuhone
Date:        2013-10-22 19:08:29
Summary:     Photon_simulator refactor.
Affected #:  1 file

diff -r 1a6425fa862b3c61f208955f52bb8868db8be07a -r 36257401694f0b66f49b975ef82e31b3e597d46a yt/analysis_modules/api.py
--- a/yt/analysis_modules/api.py
+++ b/yt/analysis_modules/api.py
@@ -111,7 +111,10 @@
 from .photon_simulator.api import \
      PhotonList, \
      EventList, \
+     SpectralModel, \
      XSpecThermalModel, \
      XSpecAbsorbModel, \
      TableApecModel, \
-     TableAbsorbModel
+     TableAbsorbModel, \
+     PhotonModel, \
+     ThermalPhotonModel


https://bitbucket.org/yt_analysis/yt/commits/68f4d30a279a/
Changeset:   68f4d30a279a
Branch:      yt
User:        jzuhone
Date:        2013-10-22 20:54:32
Summary:     Finished photon_simulator refactor
Affected #:  3 files

diff -r 36257401694f0b66f49b975ef82e31b3e597d46a -r 68f4d30a279aa9ba3cdfd0d857c6b7d7c06807d1 yt/analysis_modules/photon_simulator/photon_models.py
--- a/yt/analysis_modules/photon_simulator/photon_models.py
+++ b/yt/analysis_modules/photon_simulator/photon_models.py
@@ -21,7 +21,7 @@
 import numpy as np
 from yt.funcs import *
 from yt.utilities.physical_constants import \
-     mp, cm_per_km, K_per_keV
+     mp, cm_per_km, K_per_keV, cm_per_mpc
 from yt.utilities.parallel_tools.parallel_analysis_interface import \
      communication_system
 
@@ -42,7 +42,7 @@
 
 class ThermalPhotonModel(PhotonModel):
 
-    def __init__(self, X_H, Zmet, spectral_model):
+    def __init__(self, spectral_model, X_H=0.75, Zmet=0.3):
         self.X_H = X_H
         self.Zmet = Zmet
         self.spectral_model = spectral_model
@@ -50,7 +50,13 @@
     def __call__(self, data_source, parameters):
         
         pf = data_source.pf
-        
+
+        exp_time = parameters["FiducialExposureTime"]
+        area = parameters["FiducialArea"]
+        redshift = parameters["FiducialRedshift"]
+        D_A = parameters["FiducialAngularDiameterDistance"]*cm_per_mpc
+        dist_fac = 1.0/(4.*np.pi*D_A*D_A*(1.+redshift)**3)
+                
         vol_scale = pf.units["cm"]**(-3)/np.prod(pf.domain_width)
         
         num_cells = data_source["Temperature"].shape[0]

diff -r 36257401694f0b66f49b975ef82e31b3e597d46a -r 68f4d30a279aa9ba3cdfd0d857c6b7d7c06807d1 yt/analysis_modules/photon_simulator/photon_simulator.py
--- a/yt/analysis_modules/photon_simulator/photon_simulator.py
+++ b/yt/analysis_modules/photon_simulator/photon_simulator.py
@@ -20,8 +20,8 @@
 import numpy as np
 from numpy.testing import assert_allclose
 from yt.funcs import *
-from yt.utilities.physical_constants import mp, clight, cm_per_kpc, \
-     cm_per_mpc, cm_per_km, K_per_keV, erg_per_keV
+from yt.utilities.physical_constants import clight, \
+     cm_per_km, erg_per_keV
 from yt.utilities.cosmology import Cosmology
 from yt.utilities.orientation import Orientation
 from yt.utilities.parallel_tools.parallel_analysis_interface import \
@@ -36,10 +36,6 @@
 except ImportError:
     pass
 
-N_TBIN = 10000
-TMIN = 8.08e-2
-TMAX = 50.
-
 comm = communication_system.communicators[-1]
     
 class PhotonList(object):                                                                                                                                                                                                                                                            
@@ -133,13 +129,15 @@
                           OmegaLambdaNow=parameters["OmegaLambda"])
 
         return cls(photons, parameters, cosmo, p_bins)
-        
+    
     @classmethod
-    def from_thermal_model(cls, data_source, redshift, area,
-                           exp_time, emission_model, center="c",
-                           X_H=0.75, Zmet=0.3, dist=None, cosmology=None):
-        r"""
-        Initialize a PhotonList from a thermal plasma. 
+    def from_scratch(cls, data_source, redshift, area,
+                     exp_time, photon_model, parameters=None,
+                     center=None, dist=None, cosmology=None):
+        """
+        Initialize a PhotonList from a photon model. The redshift, collecting area,
+        exposure time, and cosmology are stored in the *parameters* dictionary which
+        is passed to the *photon_model* function. 
 
         Parameters
         ----------
@@ -152,231 +150,10 @@
             The collecting area to determine the number of photons in cm^2.
         exp_time : float
             The exposure time to determine the number of photons in seconds.
-        emission_model : `yt.analysis_modules.photon_simulator.PhotonModel`
-            The thermal emission model from which to draw the photon samples
-        center : string or array_like, optional
-            The origin of the photons. Accepts "c", "max", or a coordinate. 
-        X_H : float, optional
-            The hydrogen mass fraction.
-        Zmet : float, optional
-            The metallicity of the gas. If there is a "Metallicity" field
-            this parameter is ignored.
-        dist : tuple, optional
-            The angular diameter distance in the form (value, unit), used
-            mainly for nearby sources. This may be optionally supplied
-            instead of it being determined from the *redshift* and given *cosmology*.
-        cosmology : `yt.utilities.cosmology.Cosmology`, optional
-            Cosmological information. If not supplied, it assumes \LambdaCDM with
-            the default yt parameters.
-
-        Examples
-        --------
-
-        >>> redshift = 0.1
-        >>> area = 6000.0
-        >>> time = 2.0e5
-        >>> sp = pf.h.sphere("c", (1.0, "mpc"))
-        >>> apec_model = XSpecThermalModel("apec", 0.05, 50.0, 1000)
-        >>> my_photons = PhotonList.from_thermal_model(sp, redshift, area,
-        ...                                            time, apec_model)
-
-        """
-        pf = data_source.pf
-                
-        vol_scale = pf.units["cm"]**(-3)/np.prod(pf.domain_width)
-
-        if cosmology is None:
-            cosmo = Cosmology()
-        else:
-            cosmo = cosmology
-        if dist is None:
-            D_A = cosmo.AngularDiameterDistance(0.0,redshift)*cm_per_mpc
-        else:
-            D_A = dist[0]*pf.units["cm"]/pf.units[dist[1]]
-            redshift = 0.0
-        dist_fac = 1.0/(4.*np.pi*D_A*D_A*(1.+redshift)**3)
-
-        if center == "c":
-            src_ctr = pf.domain_center
-        elif center == "max":
-            src_ctr = pf.h.find_max("Density")[-1]
-        elif iterable(center):
-            src_ctr = center
-                                                        
-        num_cells = data_source["Temperature"].shape[0]
-        start_c = comm.rank*num_cells/comm.size
-        end_c = (comm.rank+1)*num_cells/comm.size
-
-        kT = data_source["Temperature"][start_c:end_c].copy()/K_per_keV
-        vol = data_source["CellVolume"][start_c:end_c].copy()
-        dx = data_source["dx"][start_c:end_c].copy()
-        EM = (data_source["Density"][start_c:end_c].copy()/mp)**2
-        EM *= 0.5*(1.+X_H)*X_H*vol
-        
-        data_source.clear_data()
-        
-        x = data_source["x"][start_c:end_c].copy()
-        y = data_source["y"][start_c:end_c].copy()
-        z = data_source["z"][start_c:end_c].copy()
-
-        data_source.clear_data()
-                
-        vx = data_source["x-velocity"][start_c:end_c].copy()
-        vy = data_source["y-velocity"][start_c:end_c].copy()
-        vz = data_source["z-velocity"][start_c:end_c].copy()
-
-        if isinstance(Zmet, basestring):
-            metalZ = data_source[zmet][start_c:end_c].copy()
-        else:
-            metalZ = Zmet*np.ones(EM.shape)
-            
-        data_source.clear_data()
-                
-        idxs = np.argsort(kT)
-        dshape = idxs.shape
-                    
-        kT_bins = np.linspace(TMIN, max(kT[idxs][-1], TMAX), num=N_TBIN+1)
-        dkT = kT_bins[1]-kT_bins[0]
-        kT_idxs = np.digitize(kT[idxs], kT_bins)
-        kT_idxs = np.minimum(np.maximum(1, kT_idxs), N_TBIN) - 1
-        bcounts = np.bincount(kT_idxs).astype("int")
-        bcounts = bcounts[bcounts > 0]
-        n = int(0)
-        bcell = []
-        ecell = []
-        for bcount in bcounts:
-            bcell.append(n)
-            ecell.append(n+bcount)
-            n += bcount
-        kT_idxs = np.unique(kT_idxs)
-
-        emission_model.prepare()
-        energy = emission_model.ebins
-              
-        cell_em = EM[idxs]*vol_scale
-        cell_vol = vol[idxs]*vol_scale
-        
-        number_of_photons = np.zeros(dshape, dtype='uint64')
-        energies = []
-        
-        u = np.random.random(cell_em.shape)
-        
-        pbar = get_pbar("Generating Photons", dshape[0])
-
-        for i, ikT in enumerate(kT_idxs):
-            
-            ncells = int(bcounts[i])
-            ibegin = bcell[i]
-            iend = ecell[i]
-            kT = kT_bins[ikT] + 0.5*dkT
-            
-            em_sum_c = cell_em[ibegin:iend].sum()
-            em_sum_m = (metalZ[ibegin:iend]*cell_em[ibegin:iend]).sum() 
-
-            cspec, mspec = emission_model.get_spectrum(kT)
-            cspec *= dist_fac*em_sum_c/vol_scale
-            mspec *= dist_fac*em_sum_m/vol_scale
-            
-            cumspec_c = np.cumsum(cspec)
-            counts_c = cumspec_c[:]/cumspec_c[-1]
-            counts_c = np.insert(counts_c, 0, 0.0)
-            tot_ph_c = cumspec_c[-1]*area*exp_time
-
-            cumspec_m = np.cumsum(mspec)
-            counts_m = cumspec_m[:]/cumspec_m[-1]
-            counts_m = np.insert(counts_m, 0, 0.0)
-            tot_ph_m = cumspec_m[-1]*area*exp_time
-            
-            for icell in xrange(ibegin, iend):
-                
-                cell_norm_c = tot_ph_c*cell_em[icell]/em_sum_c
-                cell_n_c = np.uint64(cell_norm_c) + np.uint64(np.modf(cell_norm_c)[0] >= u[icell])
-
-                cell_norm_m = tot_ph_m*metalZ[icell]*cell_em[icell]/em_sum_m
-                cell_n_m = np.uint64(cell_norm_m) + np.uint64(np.modf(cell_norm_m)[0] >= u[icell])
-                                                
-                cell_n = cell_n_c + cell_n_m
-                
-                if cell_n > 0:
-                    number_of_photons[icell] = cell_n                    
-                    randvec_c = np.random.uniform(size=cell_n_c)
-                    randvec_c.sort()
-                    randvec_m = np.random.uniform(size=cell_n_m)
-                    randvec_m.sort()
-                    cell_e_c = np.interp(randvec_c, counts_c, energy)
-                    cell_e_m = np.interp(randvec_m, counts_m, energy)
-                    energies.append(np.concatenate([cell_e_c,cell_e_m]))
-                    
-                pbar.update(icell)
-            
-        pbar.finish()
-
-        active_cells = number_of_photons > 0
-        idxs = idxs[active_cells]
-        
-        photons = {}
-        parameters = {}
-        
-        photons["x"] = (x[idxs]-src_ctr[0])*pf.units["kpc"]
-        photons["y"] = (y[idxs]-src_ctr[1])*pf.units["kpc"]
-        photons["z"] = (z[idxs]-src_ctr[2])*pf.units["kpc"]
-        photons["vx"] = vx[idxs]/cm_per_km
-        photons["vy"] = vy[idxs]/cm_per_km
-        photons["vz"] = vz[idxs]/cm_per_km
-        photons["dx"] = dx[idxs]*pf.units["kpc"]
-        photons["NumberOfPhotons"] = number_of_photons[active_cells]
-        photons["Energy"] = np.concatenate(energies)
-                
-        parameters["FiducialExposureTime"] = exp_time
-        parameters["FiducialArea"] = area
-        parameters["FiducialRedshift"] = redshift
-        parameters["FiducialAngularDiameterDistance"] = D_A/cm_per_mpc
-        parameters["HubbleConstant"] = cosmo.HubbleConstantNow
-        parameters["OmegaMatter"] = cosmo.OmegaMatterNow
-        parameters["OmegaLambda"] = cosmo.OmegaLambdaNow
-
-        dimension = 0
-        width = 0.0
-        for i, ax in enumerate("xyz"):
-            pos = data_source[ax]
-            delta = data_source["d%s"%(ax)]
-            le = np.min(pos-0.5*delta)
-            re = np.max(pos+0.5*delta)
-            width = 2.*max(width, re-src_ctr[i], src_ctr[i]-le)
-            dimension = max(dimension, int(width/delta.min()))
-        parameters["Dimension"] = dimension
-        parameters["Width"] = width*pf.units["kpc"]
-                
-        p_bins = np.cumsum(photons["NumberOfPhotons"])
-        p_bins = np.insert(p_bins, 0, [np.uint64(0)])
-    
-        return cls(photons, parameters, cosmo, p_bins)
-    
-    @classmethod
-    def from_scratch(cls, data_source, redshift, area,
-                     exp_time, gen_func, parameters=None,
-                     center=None, dist=None, cosmology=None):
-        """
-        Initialize a PhotonList from a user-provided model. The idea is
-        to give the user full flexibility. The redshift, collecting area,
-        exposure time, and cosmology are stored in the `photons` dictionary which
-        is passed to the user function. 
-
-        Parameters
-        ----------
-
-        data_source : `yt.data_objects.api.AMRData`
-            The data source from which the photons will be generated.
-        redshift : float
-            The cosmological redshift for the photons.
-        area : float
-            The collecting area to determine the number of photons in cm^2.
-        exp_time : float
-            The exposure time to determine the number of photons in seconds.
-        user_function : function
-            A function that takes the *data_source*, the photons dictionary,
-            and the *parameters* dictionary and generates the photons.
-            Must be of the form: user_function(data_source, photons, parameters)
+        photon_model : function
+            A function that takes the *data_source* and the *parameters*
+            dictionary and returns a *photons* dictionary. Must be of the
+            form: photon_model(data_source, parameters)
         parameters : dict, optional
             A dictionary of parameters to be passed to the user function. 
         center : string or array_like, optional
@@ -440,9 +217,9 @@
         else:
             cosmo = cosmology
         if dist is None:
-            D_A = cosmo.AngularDiameterDistance(0.0,redshift)*cm_per_mpc
+            D_A = cosmo.AngularDiameterDistance(0.0,redshift)
         else:
-            D_A = dist[0]*pf.units["cm"]/pf.units[dist[1]]
+            D_A = dist[0]*pf.units["mpc"]/pf.units[dist[1]]
             redshift = 0.0
 
         if center == "c":
@@ -452,7 +229,7 @@
         elif iterable(center):
             parameters["center"] = center
         elif center is None:
-            center = data_source.get_field_parameter("center")
+            parameters["center"] = data_source.get_field_parameter("center")
             
         parameters["FiducialExposureTime"] = exp_time
         parameters["FiducialArea"] = area
@@ -469,7 +246,7 @@
             delta = data_source["d%s"%(ax)]
             le = np.min(pos-0.5*delta)
             re = np.max(pos+0.5*delta)
-            width = 2.*max(width, re-src_ctr[i], src_ctr[i]-le)
+            width = 2.*max(width, re-parameters["center"][i], parameters["center"][i]-le)
             dimension = max(dimension, int(width/delta.min()))
         parameters["Dimension"] = dimension
         parameters["Width"] = width*pf.units["kpc"]

diff -r 36257401694f0b66f49b975ef82e31b3e597d46a -r 68f4d30a279aa9ba3cdfd0d857c6b7d7c06807d1 yt/analysis_modules/photon_simulator/spectral_models.py
--- a/yt/analysis_modules/photon_simulator/spectral_models.py
+++ b/yt/analysis_modules/photon_simulator/spectral_models.py
@@ -65,7 +65,7 @@
     """
     def __init__(self, model_name, emin, emax, nchan):
         self.model_name = model_name
-        PhotonModel.__init__(self, emin, emax, nchan)
+        SpectralModel.__init__(self, emin, emax, nchan)
         
     def prepare(self):
         """
@@ -122,7 +122,7 @@
     def __init__(self, model_name, nH, emin=0.01, emax=50.0, nchan=100000):
         self.model_name = model_name
         self.nH = nH
-        PhotonModel.__init__(self, emin, emax, nchan)
+        SpectralModel.__init__(self, emin, emax, nchan)
         
     def prepare(self):
         """
@@ -182,7 +182,7 @@
                                      self.apec_prefix+"_coco.fits")
         self.linefile = os.path.join(self.apec_root,
                                      self.apec_prefix+"_line.fits")
-        PhotonModel.__init__(self, emin, emax, nchan)
+        SpectralModel.__init__(self, emin, emax, nchan)
         self.wvbins = hc/self.ebins[::-1]
         # H, He, and trace elements
         self.cosmic_elem = [1,2,3,4,5,9,11,15,17,19,21,22,23,24,25,27,29,30]
@@ -305,7 +305,7 @@
         self.sigma = f["cross_section"][:]
         nchan = self.sigma.shape[0]
         f.close()
-        PhotonModel.__init__(self, emin, emax, nchan)
+        SpectralModel.__init__(self, emin, emax, nchan)
         self.nH = nH*1.0e22
         
     def prepare(self):


https://bitbucket.org/yt_analysis/yt/commits/4c3c72fe56bc/
Changeset:   4c3c72fe56bc
Branch:      yt
User:        jzuhone
Date:        2013-10-23 20:51:03
Summary:     Fix for un-normalized response files
Affected #:  1 file

diff -r 68f4d30a279aa9ba3cdfd0d857c6b7d7c06807d1 -r 4c3c72fe56bc198d21a3d7626ab53e5eb2926ac9 yt/analysis_modules/photon_simulator/photon_simulator.py
--- a/yt/analysis_modules/photon_simulator/photon_simulator.py
+++ b/yt/analysis_modules/photon_simulator/photon_simulator.py
@@ -37,8 +37,9 @@
     pass
 
 comm = communication_system.communicators[-1]
-    
-class PhotonList(object):                                                                                                                                                                                                                                                            
+
+class PhotonList(object):
+
     def __init__(self, photons, parameters, cosmo, p_bins):
         self.photons = photons
         self.parameters = parameters
@@ -368,7 +369,7 @@
     def project_photons(self, L, area_new=None, texp_new=None, 
                         redshift_new=None, dist_new=None,
                         absorb_model=None, psf_sigma=None,
-                        sky_center=None):
+                        sky_center=None, responses=None):
         r"""
         Projects photons onto an image plane given a line of sight.
 
@@ -434,7 +435,14 @@
         n_ph_tot = n_ph.sum()
         
         eff_area = None
+
+        parameters = {}
         
+        if responses is not None:
+            parameters["ARF"] = responses[0]
+            parameters["RMF"] = responses[1]
+            area_new = parameters["ARF"]
+            
         if (texp_new is None and area_new is None and
             redshift_new is None and dist_new is None):
             my_n_obs = n_ph_tot
@@ -449,11 +457,13 @@
                 Aratio = 1.
             elif isinstance(area_new, basestring):
                 if comm.rank == 0:
-                    mylog.info("Using energy-dependent effective area.")
+                    mylog.info("Using energy-dependent effective area: %s" % (parameters["ARF"]))
                 f = pyfits.open(area_new)
                 elo = f["SPECRESP"].data.field("ENERG_LO")
                 ehi = f["SPECRESP"].data.field("ENERG_HI")
                 eff_area = np.nan_to_num(f["SPECRESP"].data.field("SPECRESP"))
+                weights = self._normalize_arf(parameters["RMF"])
+                eff_area *= weights
                 f.close()
                 Aratio = eff_area.max()/self.parameters["FiducialArea"]
             else:
@@ -552,8 +562,10 @@
             
         if comm.rank == 0: mylog.info("Total number of observed photons: %d" % (num_events))
 
-        parameters = {}
-        
+        if responses is not None:
+            events, info = self._convolve_with_rmf(parameters["RMF"], events)
+            for k, v in info.items(): parameters[k] = v
+                
         if texp_new is None:
             parameters["ExposureTime"] = self.parameters["FiducialExposureTime"]
         else:
@@ -564,14 +576,101 @@
             parameters["Area"] = area_new
         parameters["Redshift"] = zobs
         parameters["AngularDiameterDistance"] = D_A/1000.
-        if isinstance(area_new, basestring):
-            parameters["ARF"] = area_new
         parameters["sky_center"] = np.array(sky_center)
         parameters["pix_center"] = np.array([0.5*(nx+1)]*2)
         parameters["dtheta"] = dtheta
         
         return EventList(events, parameters)
 
+    def _normalize_arf(self, respfile):
+        rmf = pyfits.open(respfile)
+        table = rmf["MATRIX"]
+        weights = np.array([w.sum() for w in table.data["MATRIX"]])
+        rmf.close()
+        return weights
+
+    def _convolve_with_rmf(self, respfile, events):
+        """
+        Convolve the events with a RMF file.
+        """
+        mylog.warning("This routine has not been tested to work with all RMFs. YMMV.")
+        mylog.info("Reading response matrix file (RMF): %s" % (respfile))
+        
+        hdulist = pyfits.open(respfile)
+
+        tblhdu = hdulist["MATRIX"]
+        n_de = len(tblhdu.data["ENERG_LO"])
+        mylog.info("Number of energy bins in RMF: %d" % (n_de))
+        de = tblhdu.data["ENERG_HI"] - tblhdu.data["ENERG_LO"]
+        mylog.info("Energy limits: %g %g" % (min(tblhdu.data["ENERG_LO"]),
+                                             max(tblhdu.data["ENERG_HI"])))
+
+        tblhdu2 = hdulist["EBOUNDS"]
+        n_ch = len(tblhdu2.data["CHANNEL"])
+        mylog.info("Number of channels in RMF: %d" % (n_ch))
+        
+        eidxs = np.argsort(events["eobs"])
+
+        phEE = events["eobs"][eidxs]
+        phXX = events["xpix"][eidxs]
+        phYY = events["ypix"][eidxs]
+
+        detectedChannels = []
+        pindex = 0
+
+        # run through all photon energies and find which bin they go in
+        k = 0
+        fcurr = 0
+        last = len(phEE)-1
+
+        pbar = get_pbar("Scattering energies with RMF:", n_de)
+        
+        for low,high in zip(tblhdu.data["ENERG_LO"],tblhdu.data["ENERG_HI"]):
+            # weight function for probabilities from RMF
+            weights = np.nan_to_num(tblhdu.data[k]["MATRIX"][:])
+            weights /= weights.sum()
+            # build channel number list associated to array value,
+            # there are groups of channels in rmfs with nonzero probabilities
+            trueChannel = []
+            f_chan = np.nan_to_num(tblhdu.data["F_CHAN"][k])
+            n_chan = np.nan_to_num(tblhdu.data["N_CHAN"][k])
+            n_grp = np.nan_to_num(tblhdu.data["N_CHAN"][k])
+            if not iterable(f_chan):
+                f_chan = [f_chan]
+                n_chan = [n_chan]
+                n_grp  = [n_grp]
+            for start,nchan in zip(f_chan, n_chan):
+                end = start + nchan
+                if start == end:
+                    trueChannel.append(start)
+                else:
+                    for j in range(start,end):
+                        trueChannel.append(j)
+            if len(trueChannel) > 0:
+                for q in range(fcurr,last):
+                    if phEE[q] >= low and phEE[q] < high:
+                        channelInd = np.random.choice(len(weights), p=weights)
+                        fcurr +=1
+                        detectedChannels.append(trueChannel[channelInd])
+                    if phEE[q] >= high:
+                        break
+            pbar.update(k)
+            k+=1
+        pbar.finish()
+        
+        dchannel = np.array(detectedChannels)
+
+        events["xpix"] = phXX
+        events["ypix"] = phYY
+        events["eobs"] = phEE
+        events[tblhdu.header["CHANTYPE"]] = dchannel.astype(int)
+
+        info = {"ChannelType" : tblhdu.header["CHANTYPE"],
+                "Telescope" : tblhdu.header["TELESCOP"],
+                "Instrument" : tblhdu.header["INSTRUME"]}
+        
+        return events, info
+    
 class EventList(object) :
 
     def __init__(self, events, parameters) :
@@ -700,7 +799,7 @@
             events[k1] = np.concatenate([v1,v2])
         
         return cls(events, events1.parameters)
-
+        
     def convolve_with_response(self, respfile):
         """
         Convolve the events with a RMF file *respfile*.
@@ -723,19 +822,6 @@
         mylog.info("Energy limits: %g %g" % (min(tblhdu.data["ENERG_LO"]),
                                              max(tblhdu.data["ENERG_HI"])))
 
-        if "ARF" in self.parameters:
-            f = pyfits.open(self.parameters["ARF"])
-            elo = f["SPECRESP"].data.field("ENERG_LO")
-            ehi = f["SPECRESP"].data.field("ENERG_HI")
-            f.close()
-            try:
-                assert_allclose(elo, tblhdu.data["ENERG_LO"], rtol=1.0e-6)
-                assert_allclose(ehi, tblhdu.data["ENERG_HI"], rtol=1.0e-6)
-            except AssertionError:
-                mylog.warning("Energy binning does not match for "+
-                              "ARF and RMF. This may make spectral "+
-                              "fitting difficult.")
-                                              
         tblhdu2 = hdulist["EBOUNDS"]
         n_ch = len(tblhdu2.data["CHANNEL"])
         mylog.info("Number of Channels: %d" % (n_ch))


https://bitbucket.org/yt_analysis/yt/commits/eb8ffe9c5849/
Changeset:   eb8ffe9c5849
Branch:      yt
User:        jzuhone
Date:        2013-10-30 21:53:12
Summary:     Answer and unit tests for photon_simulator. Also, data_dir_load should take keywords.
Affected #:  5 files

diff -r 4c3c72fe56bc198d21a3d7626ab53e5eb2926ac9 -r eb8ffe9c584951fbf0f00e90d49f77c7b59a0e71 yt/analysis_modules/photon_simulator/setup.py
--- /dev/null
+++ b/yt/analysis_modules/photon_simulator/setup.py
@@ -0,0 +1,14 @@
+#!/usr/bin/env python
+import setuptools
+import os
+import sys
+import os.path
+
+
+def configuration(parent_package='', top_path=None):
+    from numpy.distutils.misc_util import Configuration
+    config = Configuration('photon_simulator', parent_package, top_path)
+    config.add_subpackage("tests")
+    config.make_config_py()  # installs __config__.py
+    #config.make_svn_version_py()
+    return config

diff -r 4c3c72fe56bc198d21a3d7626ab53e5eb2926ac9 -r eb8ffe9c584951fbf0f00e90d49f77c7b59a0e71 yt/analysis_modules/photon_simulator/tests/test_beta_model.py
--- /dev/null
+++ b/yt/analysis_modules/photon_simulator/tests/test_beta_model.py
@@ -0,0 +1,117 @@
+"""
+Unit test the photon_simulator analysis module.
+"""
+
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
+from yt.frontends.stream.api import load_uniform_grid
+from yt.testing import *
+from yt.utilities.physical_constants import cm_per_kpc, \
+     K_per_keV, cm_per_mpc, mp
+from yt.utilities.cosmology import Cosmology
+from yt.analysis_modules.api import PhotonList, EventList, \
+     XSpecThermalModel, XSpecAbsorbModel, ThermalPhotonModel
+import os
+import xspec
+
+def setup():
+    """Test specific setup."""
+    from yt.config import ytcfg
+    ytcfg["yt", "__withintesting"] = "True"
+                
+ at requires_module("xspec")
+def test_beta_model():
+
+    # Set up the beta model and stream dataset
+    R = 1000.
+    r_c = 100.
+    rho_c = 1.673e-26
+    beta = 1.
+    T = 4.
+    nx = 256
+    nH = 0.1
+    Zmet = 0.3
+    X_H = 0.75
+    nenp0 = (rho_c/mp)**2*0.5*(1.+X_H)*X_H
+
+    ddims = (nx,nx,nx)
+    
+    x, y, z = np.mgrid[-R:R:nx*1j,
+                       -R:R:nx*1j,
+                       -R:R:nx*1j]
+    
+    r = np.sqrt(x**2+y**2+z**2)
+
+    dens = np.zeros(ddims)
+    dens[r <= R] = rho_c*(1.+(r[r <= R]/r_c)**2)**(-1.5*beta)
+    dens[r > R] = 0.0
+    temp = T*K_per_keV*np.ones(ddims)
+
+    data = {}
+    data["Density"] = dens
+    data["Temperature"] = temp
+    data["x-velocity"] = np.zeros(ddims)
+    data["y-velocity"] = np.zeros(ddims)
+    data["z-velocity"] = np.zeros(ddims)
+
+    bbox = np.array([[-0.5,0.5],[-0.5,0.5],[-0.5,0.5]])
+    
+    pf = load_uniform_grid(data, ddims, 2*R*cm_per_kpc, bbox=bbox)
+
+    # Grab a sphere data object
+    
+    sphere = pf.h.sphere(pf.domain_center, 1.0/pf["mpc"])
+
+    # Create the photons
+
+    ARF = os.environ["YT_DATA_DIR"]+"xray_data/chandra_ACIS-S3_onaxis_arf.fits"
+    RMF = os.environ["YT_DATA_DIR"]+"xray_data/chandra_ACIS-S3_onaxis_rmf.fits"
+                    
+    A = 6000.
+    exp_time = 1.0e5
+    redshift = 0.05
+    cosmo = Cosmology()
+    DA = cosmo.AngularDiameterDistance(0.0,redshift)*cm_per_mpc
+    EM = 4.*np.pi*nenp0*0.196022123*(r_c*cm_per_kpc)**3
+    norm = 1.0e-14*EM/(4.*np.pi*DA**2*(1.+redshift)**2)
+
+    apec_model = XSpecThermalModel("apec", 0.01, 20.0, 10000)
+    abs_model  = XSpecAbsorbModel("TBabs", nH)
+        
+    thermal_model = ThermalPhotonModel(apec_model, Zmet=Zmet)
+    photons = PhotonList.from_scratch(sphere, redshift, A, exp_time,
+                                      thermal_model, cosmology=cosmo)
+    
+    events = photons.project_photons([0.0,0.0,1.0],
+                                     responses=[ARF,RMF],
+                                     absorb_model=abs_model)
+    events.write_spectrum("spec_chandra.fits", clobber=True)
+
+    # Now fit the resulting spectrum
+    
+    spec = xspec.Spectrum("spec_chandra.fits")
+    xspec.Fit.statMethod = "cstat"
+    
+    spec.ignore("**-0.5")
+    spec.ignore("7.0-**")
+    
+    m = xspec.Model("tbabs*apec")
+    m.TBabs.nH = 0.09
+    m.apec.kT = 5.
+    m.apec.Abundanc = 0.2
+    m.apec.Abundanc.frozen = False
+    m.apec.Redshift = redshift
+    
+    xspec.Fit.renorm()
+    xspec.Fit.perform()
+    xspec.Fit.error("1-3,5")
+    
+    assert(T > m.apec.kT.error[0] and T < m.apec.kT.error[1])
+    assert(Zmet > m.apec.Abundanc.error[0] and Zmet < m.apec.Abundanc.error[1])
+    assert(norm > m.apec.norm.error[0] and norm < m.apec.norm.error[1])

diff -r 4c3c72fe56bc198d21a3d7626ab53e5eb2926ac9 -r eb8ffe9c584951fbf0f00e90d49f77c7b59a0e71 yt/analysis_modules/photon_simulator/tests/test_cluster.py
--- /dev/null
+++ b/yt/analysis_modules/photon_simulator/tests/test_cluster.py
@@ -0,0 +1,66 @@
+"""
+Answer test the photon_simulator analysis module.
+"""
+
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
+from yt.testing import *
+from yt.analysis_modules.photon_simulator.api import *
+from yt.utilities.answer_testing.framework import requires_pf, \
+     GenericArrayTest, data_dir_load
+import numpy as np
+
+def setup():
+    """Test specific setup."""
+    from yt.config import ytcfg
+    ytcfg["yt", "__withintesting"] = "True"
+
+MHD = "MHDSloshing/virgo_low_res.0054.vtk"
+ at requires_module("xspec")
+ at requires_pf(MHD)
+def test_cluster():
+    pf = data_dir_load(MHD, parameters={"TimeUnits":3.1557e13,
+                                        "LengthUnits":3.0856e24,
+                                        "DensityUnits":6.770424595218825e-27})
+
+    A = 3000.
+    exp_time = 3.0e5
+    redshift = 0.02
+    
+    apec_model = XSpecThermalModel("apec", 0.1, 20.0, 2000)
+    tbabs_model = XSpecAbsorbModel("TBabs", 0.1)
+
+    ARF = os.environ["YT_DATA_DIR"]+"/xray_data/chandra_ACIS-S3_onaxis_arf.fits"
+    RMF = os.environ["YT_DATA_DIR"]+"/xray_data/chandra_ACIS-S3_onaxis_rmf.fits"
+            
+    sphere = pf.h.sphere("c", (0.25, "mpc"))
+
+    thermal_model = ThermalPhotonModel(apec_model, Zmet=0.3)
+    photons = PhotonList.from_scratch(sphere, redshift, A, exp_time,
+                                      thermal_model, cosmology=cosmo)
+    
+    events = photons.project_photons([0.0,0.0,1.0],
+                                     responses=[ARF,RMF],
+                                     absorb_model=abs_model)
+    
+    for k,v in photons.items():
+        if isinstance(v,np.ndarray):
+            def photons_test(v): return v
+            test = GenericArrayTest(pf, photons_test)
+            test_cluster.__name__ = test.description
+            yield test
+
+    for k,v in events.items():
+        if isinstance(v,np.ndarray):
+            def events_test(v): return v
+            test = GenericArrayTest(pf, events_test)
+            test_cluster.__name__ = test.description
+            yield test
+            
+            

diff -r 4c3c72fe56bc198d21a3d7626ab53e5eb2926ac9 -r eb8ffe9c584951fbf0f00e90d49f77c7b59a0e71 yt/utilities/answer_testing/framework.py
--- a/yt/utilities/answer_testing/framework.py
+++ b/yt/utilities/answer_testing/framework.py
@@ -266,13 +266,13 @@
             return False
     return AnswerTestingTest.result_storage is not None
 
-def data_dir_load(pf_fn):
+def data_dir_load(pf_fn, **kwargs):
     path = ytcfg.get("yt", "test_data_dir")
     if isinstance(pf_fn, StaticOutput): return pf_fn
     if not os.path.isdir(path):
         return False
     with temp_cwd(path):
-        pf = load(pf_fn)
+        pf = load(pf_fn, kwargs)
         pf.h
         return pf
 


https://bitbucket.org/yt_analysis/yt/commits/00fcfa114a79/
Changeset:   00fcfa114a79
Branch:      yt
User:        jzuhone
Date:        2013-10-31 19:57:47
Summary:     More working on tests
Affected #:  2 files

diff -r eb8ffe9c584951fbf0f00e90d49f77c7b59a0e71 -r 00fcfa114a79d4f9ded15b75cfd8e67154410760 yt/analysis_modules/photon_simulator/tests/test_beta_model.py
--- a/yt/analysis_modules/photon_simulator/tests/test_beta_model.py
+++ b/yt/analysis_modules/photon_simulator/tests/test_beta_model.py
@@ -19,6 +19,7 @@
      XSpecThermalModel, XSpecAbsorbModel, ThermalPhotonModel
 import os
 import xspec
+import gc
 
 def setup():
     """Test specific setup."""
@@ -27,7 +28,6 @@
                 
 @requires_module("xspec")
 def test_beta_model():
-
     # Set up the beta model and stream dataset
     R = 1000.
     r_c = 100.
@@ -70,9 +70,14 @@
 
     # Create the photons
 
-    ARF = os.environ["YT_DATA_DIR"]+"xray_data/chandra_ACIS-S3_onaxis_arf.fits"
-    RMF = os.environ["YT_DATA_DIR"]+"xray_data/chandra_ACIS-S3_onaxis_rmf.fits"
-                    
+    # XSPEC is buggy so for this test we have to do it this way
+    
+    ARF = "chandra_ACIS-S3_onaxis_arf.fits"
+    RMF = "chandra_ACIS-S3_onaxis_rmf.fits"
+            
+    os.system("cp "+ os.environ["YT_DATA_DIR"]+"xray_data/"+ARF+" "+os.getcwd())
+    os.system("cp "+ os.environ["YT_DATA_DIR"]+"xray_data/"+RMF+" "+os.getcwd())
+    
     A = 6000.
     exp_time = 1.0e5
     redshift = 0.05
@@ -93,8 +98,12 @@
                                      absorb_model=abs_model)
     events.write_spectrum("spec_chandra.fits", clobber=True)
 
+    del photons, events
+
+    gc.collect()
+    
     # Now fit the resulting spectrum
-    
+
     spec = xspec.Spectrum("spec_chandra.fits")
     xspec.Fit.statMethod = "cstat"
     
@@ -115,3 +124,6 @@
     assert(T > m.apec.kT.error[0] and T < m.apec.kT.error[1])
     assert(Zmet > m.apec.Abundanc.error[0] and Zmet < m.apec.Abundanc.error[1])
     assert(norm > m.apec.norm.error[0] and norm < m.apec.norm.error[1])
+
+if __name__ == "__main__":
+    test_beta_model()

diff -r eb8ffe9c584951fbf0f00e90d49f77c7b59a0e71 -r 00fcfa114a79d4f9ded15b75cfd8e67154410760 yt/analysis_modules/photon_simulator/tests/test_cluster.py
--- a/yt/analysis_modules/photon_simulator/tests/test_cluster.py
+++ b/yt/analysis_modules/photon_simulator/tests/test_cluster.py
@@ -25,6 +25,7 @@
 @requires_module("xspec")
 @requires_pf(MHD)
 def test_cluster():
+    np.random.seed(seed=0x4d3d3d3)
     pf = data_dir_load(MHD, parameters={"TimeUnits":3.1557e13,
                                         "LengthUnits":3.0856e24,
                                         "DensityUnits":6.770424595218825e-27})
@@ -50,17 +51,15 @@
                                      absorb_model=abs_model)
     
     for k,v in photons.items():
-        if isinstance(v,np.ndarray):
-            def photons_test(v): return v
-            test = GenericArrayTest(pf, photons_test)
-            test_cluster.__name__ = test.description
-            yield test
+        def photons_test(v): return v
+        test = GenericArrayTest(pf, photons_test)
+        test_cluster.__name__ = test.description
+        yield test
 
     for k,v in events.items():
-        if isinstance(v,np.ndarray):
-            def events_test(v): return v
-            test = GenericArrayTest(pf, events_test)
-            test_cluster.__name__ = test.description
-            yield test
+        def events_test(v): return v
+        test = GenericArrayTest(pf, events_test)
+        test_cluster.__name__ = test.description
+        yield test
             
             


https://bitbucket.org/yt_analysis/yt/commits/04756963b865/
Changeset:   04756963b865
Branch:      yt
User:        jzuhone
Date:        2013-10-31 13:33:49
Summary:     Some naming issues... adding more docstrings
Affected #:  2 files

diff -r eb8ffe9c584951fbf0f00e90d49f77c7b59a0e71 -r 04756963b86593ba03e6334988f2fc08a2e32034 yt/analysis_modules/photon_simulator/photon_models.py
--- a/yt/analysis_modules/photon_simulator/photon_models.py
+++ b/yt/analysis_modules/photon_simulator/photon_models.py
@@ -41,7 +41,21 @@
         return photons
 
 class ThermalPhotonModel(PhotonModel):
+    r"""
+    Initialize a ThermalPhotonModel from a thermal spectrum. 
+    
+    Parameters
+    ----------
 
+    spectral_model : `SpectralModel`
+        A thermal spectral model instance, either of `XSpecThermalModel`
+        or `TableApecModel`. 
+    X_H : float, optional
+        The hydrogen mass fraction.
+    Zmet : float or string, optional
+        The metallicity. If a float, assumes a constant metallicity throughout.
+        If a string, is taken to be the name of the metallicity field.
+    """
     def __init__(self, spectral_model, X_H=0.75, Zmet=0.3):
         self.X_H = X_H
         self.Zmet = Zmet

diff -r eb8ffe9c584951fbf0f00e90d49f77c7b59a0e71 -r 04756963b86593ba03e6334988f2fc08a2e32034 yt/analysis_modules/photon_simulator/photon_simulator.py
--- a/yt/analysis_modules/photon_simulator/photon_simulator.py
+++ b/yt/analysis_modules/photon_simulator/photon_simulator.py
@@ -135,7 +135,7 @@
     def from_scratch(cls, data_source, redshift, area,
                      exp_time, photon_model, parameters=None,
                      center=None, dist=None, cosmology=None):
-        """
+        r"""
         Initialize a PhotonList from a photon model. The redshift, collecting area,
         exposure time, and cosmology are stored in the *parameters* dictionary which
         is passed to the *photon_model* function. 
@@ -170,12 +170,41 @@
         Examples
         --------
 
-        This is a simple example where a point source with a single line emission
+        This is the simplest possible example, where we call the built-in thermal model:
+
+        >>> thermal_model = ThermalPhotonModel(apec_model, Zmet=0.3)
+        >>> redshift = 0.05
+        >>> area = 6000.0
+        >>> time = 2.0e5
+        >>> sp = pf.h.sphere("c", (500., "kpc"))
+        >>> my_photons = PhotonList.from_user_model(sp, redshift, area,
+        ...                                         time, thermal_model)
+
+        If you wish to make your own photon model function, it must take as its
+        arguments the *data_source* and the *parameters* dictionary. However you
+        determine them, the *photons* dict needs to have the following items, corresponding
+        to cells which have photons:
+
+        "x" : the x-position of the cell relative to the source center in kpc, NumPy array of floats
+        "y" : the y-position of the cell relative to the source center in kpc, NumPy array of floats
+        "z" : the z-position of the cell relative to the source center in kpc, NumPy array of floats
+        "vx" : the x-velocity of the cell in km/s, NumPy array of floats
+        "vy" : the y-velocity of the cell in km/s, NumPy array of floats
+        "vz" : the z-velocity of the cell in km/s, NumPy array of floats
+        "dx" : the width of the cell in kpc, NumPy array of floats
+        "NumberOfPhotons" : the number of photons in the cell, NumPy array of integers
+        "Energy" : the source rest-frame energies of the photons, NumPy array of floats
+
+        The last array is not the same size as the others because it contains the energies in all of
+        the cells in a single 1-D array. The first photons["NumberOfPhotons"][0] elements are
+        for the first cell, the next photons["NumberOfPhotons"][1] are for the second cell, and so on.
+
+        The following is a simple example where a point source with a single line emission
         spectrum of photons is created. More complicated examples which actually
         create photons based on the fields in the dataset could be created. 
 
         >>> from scipy.stats import powerlaw
-        >>> def line_func(source, photons, parameters):
+        >>> def line_func(source, parameters):
         ...
         ...     pf = source.pf
         ... 
@@ -247,10 +276,10 @@
             delta = data_source["d%s"%(ax)]
             le = np.min(pos-0.5*delta)
             re = np.max(pos+0.5*delta)
-            width = 2.*max(width, re-parameters["center"][i], parameters["center"][i]-le)
+            width = max(width, re-parameters["center"][i], parameters["center"][i]-le)
             dimension = max(dimension, int(width/delta.min()))
-        parameters["Dimension"] = dimension
-        parameters["Width"] = width*pf.units["kpc"]
+        parameters["Dimension"] = 2*dimension
+        parameters["Width"] = 2.*width*pf.units["kpc"]
                 
         photons = photon_model(data_source, parameters)
         
@@ -366,7 +395,7 @@
 
         comm.barrier()
 
-    def project_photons(self, L, area_new=None, texp_new=None, 
+    def project_photons(self, L, area_new=None, exp_time_new=None, 
                         redshift_new=None, dist_new=None,
                         absorb_model=None, psf_sigma=None,
                         sky_center=None, responses=None):
@@ -382,7 +411,7 @@
             New value for the effective area of the detector. 
             Either a single float value or a standard ARF file
             containing the effective area as a function of energy.
-        texp_new : float, optional
+        exp_time_new : float, optional
             The new value for the exposure time.
         redshift_new : float, optional
             The new value for the cosmological redshift.
@@ -443,16 +472,16 @@
             parameters["RMF"] = responses[1]
             area_new = parameters["ARF"]
             
-        if (texp_new is None and area_new is None and
+        if (exp_time_new is None and area_new is None and
             redshift_new is None and dist_new is None):
             my_n_obs = n_ph_tot
             zobs = self.parameters["FiducialRedshift"]
             D_A = self.parameters["FiducialAngularDiameterDistance"]*1000.
         else:
-            if texp_new is None:
+            if exp_time_new is None:
                 Tratio = 1.
             else:
-                Tratio = texp_new/self.parameters["FiducialExposureTime"]
+                Tratio = exp_time_new/self.parameters["FiducialExposureTime"]
             if area_new is None:
                 Aratio = 1.
             elif isinstance(area_new, basestring):
@@ -566,10 +595,10 @@
             events, info = self._convolve_with_rmf(parameters["RMF"], events)
             for k, v in info.items(): parameters[k] = v
                 
-        if texp_new is None:
+        if exp_time_new is None:
             parameters["ExposureTime"] = self.parameters["FiducialExposureTime"]
         else:
-            parameters["ExposureTime"] = texp_new
+            parameters["ExposureTime"] = exp_time_new
         if area_new is None:
             parameters["Area"] = self.parameters["FiducialArea"]
         else:


https://bitbucket.org/yt_analysis/yt/commits/b32a1ca3a074/
Changeset:   b32a1ca3a074
Branch:      yt
User:        jzuhone
Date:        2013-10-31 13:37:27
Summary:     Merging
Affected #:  48 files

diff -r 04756963b86593ba03e6334988f2fc08a2e32034 -r b32a1ca3a07428a81d427edef5ecf9b935be9c33 MANIFEST.in
--- a/MANIFEST.in
+++ b/MANIFEST.in
@@ -1,4 +1,4 @@
-include distribute_setup.py README* CREDITS FUNDING LICENSE.txt
+include distribute_setup.py README* CREDITS COPYING.txt CITATION
 recursive-include yt/gui/reason/html *.html *.png *.ico *.js
-recursive-include yt *.pyx *.pxd *.hh *.h README*
+recursive-include yt *.pyx *.pxd *.h README*
 recursive-include yt/utilities/kdtree *.f90 *.v Makefile LICENSE
\ No newline at end of file

diff -r 04756963b86593ba03e6334988f2fc08a2e32034 -r b32a1ca3a07428a81d427edef5ecf9b935be9c33 README
--- a/README
+++ b/README
@@ -1,11 +1,12 @@
-Hi there!  You've just downloaded yt, an analysis tool for astrophysical
-simulation datasets, generated by simulation platforms like Enzo, Orion, FLASH,
-Nyx, MAESTRO, ART and Ramses.  It's written in python and heavily leverages
-both NumPy and Matplotlib for fast arrays and visualization, respectively.
+Hi there!  You've just downloaded yt, an analysis tool for scientific
+datasets, generated on a variety of data platforms.  It's written in 
+python and heavily leverages both NumPy and Matplotlib for fast arrays and 
+visualization, respectively.
 
 Full documentation and a user community can be found at:
 
 http://yt-project.org/
+
 http://yt-project.org/doc/
 
 If you have used Python before, and are comfortable with installing packages,
@@ -16,9 +17,7 @@
 doc/install_script.sh .  You will have to set the destination directory, and
 there are options available, but it should be straightforward.
 
-In case of any problems, please email the yt-users mailing list, and if you're
-interested in helping out, see the developer documentation:
-
-http://yt-project.org/doc/advanced/developing.html
+For more information on installation, what to do if you run into problems, or 
+ways to help development, please visit our website.
 
 Enjoy!

diff -r 04756963b86593ba03e6334988f2fc08a2e32034 -r b32a1ca3a07428a81d427edef5ecf9b935be9c33 yt/analysis_modules/absorption_spectrum/absorption_spectrum_fit.py
--- a/yt/analysis_modules/absorption_spectrum/absorption_spectrum_fit.py
+++ b/yt/analysis_modules/absorption_spectrum/absorption_spectrum_fit.py
@@ -86,6 +86,10 @@
     #Empty fit without any lines
     yFit = na.ones(len(fluxData))
 
+    #Force the first and last flux pixel to be 1 to prevent OOB
+    fluxData[0]=1
+    fluxData[-1]=1
+
     #Find all regions where lines/groups of lines are present
     cBounds = _find_complexes(x, fluxData, fitLim=fitLim,
             complexLim=complexLim, minLength=minLength,
@@ -120,9 +124,10 @@
                     z,fitLim,minError*(b[2]-b[1]),speciesDict)
 
             #Check existence of partner lines if applicable
-            newLinesP = _remove_unaccepted_partners(newLinesP, x, fluxData, 
-                    b, minError*(b[2]-b[1]),
-                    x0, xRes, speciesDict)
+            if len(speciesDict['wavelength']) != 1:
+                newLinesP = _remove_unaccepted_partners(newLinesP, x, fluxData, 
+                        b, minError*(b[2]-b[1]),
+                        x0, xRes, speciesDict)
 
             #If flagged as a bad fit, species is lyman alpha,
             #   and it may be a saturated line, use special tools
@@ -548,6 +553,10 @@
         #Index of the redshifted wavelength
         indexRedWl = (redWl-x0)/xRes
 
+        #Check to see if even in flux range
+        if indexRedWl > len(y):
+            return False
+
         #Check if surpasses minimum absorption bound
         if y[int(indexRedWl)]>fluxMin:
             return False

diff -r 04756963b86593ba03e6334988f2fc08a2e32034 -r b32a1ca3a07428a81d427edef5ecf9b935be9c33 yt/analysis_modules/api.py
--- a/yt/analysis_modules/api.py
+++ b/yt/analysis_modules/api.py
@@ -108,6 +108,9 @@
 from .radmc3d_export.api import \
     RadMC3DWriter
 
+from .particle_trajectories.api import \
+    ParticleTrajectories
+
 from .photon_simulator.api import \
      PhotonList, \
      EventList, \

diff -r 04756963b86593ba03e6334988f2fc08a2e32034 -r b32a1ca3a07428a81d427edef5ecf9b935be9c33 yt/analysis_modules/particle_trajectories/api.py
--- /dev/null
+++ b/yt/analysis_modules/particle_trajectories/api.py
@@ -0,0 +1,12 @@
+"""
+API for particle_trajectories
+"""
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
+from particle_trajectories import ParticleTrajectories

diff -r 04756963b86593ba03e6334988f2fc08a2e32034 -r b32a1ca3a07428a81d427edef5ecf9b935be9c33 yt/analysis_modules/particle_trajectories/particle_trajectories.py
--- /dev/null
+++ b/yt/analysis_modules/particle_trajectories/particle_trajectories.py
@@ -0,0 +1,329 @@
+"""
+Particle trajectories
+"""
+
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
+from yt.data_objects.data_containers import YTFieldData
+from yt.data_objects.time_series import TimeSeriesData
+from yt.utilities.lib import CICSample_3
+from yt.funcs import *
+
+import numpy as np
+import h5py
+
+class ParticleTrajectories(object):
+    r"""A collection of particle trajectories in time over a series of
+    parameter files. 
+
+    The ParticleTrajectories object contains a collection of
+    particle trajectories for a specified set of particle indices. 
+    
+    Parameters
+    ----------
+    filenames : list of strings
+        A time-sorted list of filenames to construct the TimeSeriesData
+        object.
+    indices : array_like
+        An integer array of particle indices whose trajectories we
+        want to track. If they are not sorted they will be sorted.
+    fields : list of strings, optional
+        A set of fields that is retrieved when the trajectory
+        collection is instantiated.
+        Default : None (will default to the fields 'particle_position_x',
+        'particle_position_y', 'particle_position_z')
+
+    Examples
+    ________
+    >>> from yt.mods import *
+    >>> my_fns = glob.glob("orbit_hdf5_chk_00[0-9][0-9]")
+    >>> my_fns.sort()
+    >>> fields = ["particle_position_x", "particle_position_y",
+    >>>           "particle_position_z", "particle_velocity_x",
+    >>>           "particle_velocity_y", "particle_velocity_z"]
+    >>> pf = load(my_fns[0])
+    >>> init_sphere = pf.h.sphere(pf.domain_center, (.5, "unitary"))
+    >>> indices = init_sphere["particle_index"].astype("int")
+    >>> trajs = ParticleTrajectories(my_fns, indices, fields=fields)
+    >>> for t in trajs :
+    >>>     print t["particle_velocity_x"].max(), t["particle_velocity_x"].min()
+
+    Notes
+    -----
+    As of this time only particle trajectories that are complete over the
+    set of specified parameter files are supported. If any particle's history
+    ends for some reason (e.g. leaving the simulation domain or being actively
+    destroyed), the whole trajectory collection of which it is a set must end
+    at or before the particle's last timestep. This is a limitation we hope to
+    lift at some point in the future.     
+    """
+    def __init__(self, filenames, indices, fields=None) :
+
+        indices.sort() # Just in case the caller wasn't careful
+        
+        self.field_data = YTFieldData()
+        self.pfs = TimeSeriesData.from_filenames(filenames)
+        self.masks = []
+        self.sorts = []
+        self.indices = indices
+        self.num_indices = len(indices)
+        self.num_steps = len(filenames)
+        self.times = []
+
+        # Default fields 
+        
+        if fields is None: fields = []
+
+        # Must ALWAYS have these fields
+        
+        fields = fields + ["particle_position_x",
+                           "particle_position_y",
+                           "particle_position_z"]
+
+        # Set up the derived field list and the particle field list
+        # so that if the requested field is a particle field, we'll
+        # just copy the field over, but if the field is a grid field,
+        # we will first interpolate the field to the particle positions
+        # and then return the field. 
+
+        pf = self.pfs[0]
+        self.derived_field_list = pf.h.derived_field_list
+        self.particle_fields = [field for field in self.derived_field_list
+                                if pf.field_info[field].particle_type]
+
+        """
+        The following loops through the parameter files
+        and performs two tasks. The first is to isolate
+        the particles with the correct indices, and the
+        second is to create a sorted list of these particles.
+        We also make a list of the current time from each file. 
+        Right now, the code assumes (and checks for) the
+        particle indices existing in each dataset, a limitation I
+        would like to lift at some point since some codes
+        (e.g., FLASH) destroy particles leaving the domain.
+        """
+        
+        for pf in self.pfs:
+            dd = pf.h.all_data()
+            newtags = dd["particle_index"].astype("int")
+            if not np.all(np.in1d(indices, newtags, assume_unique=True)):
+                print "Not all requested particle ids contained in this dataset!"
+                raise IndexError
+            mask = np.in1d(newtags, indices, assume_unique=True)
+            sorts = np.argsort(newtags[mask])
+            self.masks.append(mask)            
+            self.sorts.append(sorts)
+            self.times.append(pf.current_time)
+
+        self.times = np.array(self.times)
+
+        # Now instantiate the requested fields 
+        for field in fields:
+            self._get_data(field)
+            
+    def has_key(self, key):
+        return (key in self.field_data)
+    
+    def keys(self):
+        return self.field_data.keys()
+
+    def __getitem__(self, key):
+        """
+        Get the field associated with key,
+        checking to make sure it is a particle field.
+        """
+        if key == "particle_time":
+            return self.times
+        if not self.field_data.has_key(key):
+            self._get_data(key)
+        return self.field_data[key]
+    
+    def __setitem__(self, key, val):
+        """
+        Sets a field to be some other value.
+        """
+        self.field_data[key] = val
+                        
+    def __delitem__(self, key):
+        """
+        Delete the field from the trajectory
+        """
+        del self.field_data[key]
+
+    def __iter__(self):
+        """
+        This iterates over the trajectories for
+        the different particles, returning dicts
+        of fields for each trajectory
+        """
+        for idx in xrange(self.num_indices):
+            traj = {}
+            traj["particle_index"] = self.indices[idx]
+            traj["particle_time"] = self.times
+            for field in self.field_data.keys():
+                traj[field] = self[field][idx,:]
+            yield traj
+            
+    def __len__(self):
+        """
+        The number of individual trajectories
+        """
+        return self.num_indices
+
+    def add_fields(self, fields):
+        """
+        Add a list of fields to an existing trajectory
+
+        Parameters
+        ----------
+        fields : list of strings
+            A list of fields to be added to the current trajectory
+            collection.
+
+        Examples
+        ________
+        >>> from yt.mods import *
+        >>> trajs = ParticleTrajectories(my_fns, indices)
+        >>> trajs.add_fields(["particle_mass", "particle_gpot"])
+        """
+        for field in fields:
+            if not self.field_data.has_key(field):
+                self._get_data(field)
+                
+    def _get_data(self, field):
+        """
+        Get a field to include in the trajectory collection.
+        The trajectory collection itself is a dict of 2D numpy arrays,
+        with shape (num_indices, num_steps)
+        """
+        if not self.field_data.has_key(field):
+            particles = np.empty((0))
+            step = int(0)
+            for pf, mask, sort in zip(self.pfs, self.masks, self.sorts):
+                if field in self.particle_fields:
+                    # This is easy... just get the particle fields
+                    dd = pf.h.all_data()
+                    pfield = dd[field][mask]
+                    particles = np.append(particles, pfield[sort])
+                else:
+                    # This is hard... must loop over grids
+                    pfield = np.zeros((self.num_indices))
+                    x = self["particle_position_x"][:,step]
+                    y = self["particle_position_y"][:,step]
+                    z = self["particle_position_z"][:,step]
+                    particle_grids, particle_grid_inds = pf.h.find_points(x,y,z)
+                    for grid in particle_grids:
+                        cube = grid.retrieve_ghost_zones(1, [field])
+                        CICSample_3(x,y,z,pfield,
+                                    self.num_indices,
+                                    cube[field],
+                                    np.array(grid.LeftEdge).astype(np.float64),
+                                    np.array(grid.ActiveDimensions).astype(np.int32),
+                                    np.float64(grid['dx']))
+                    particles = np.append(particles, pfield)
+                step += 1
+            self[field] = particles.reshape(self.num_steps,
+                                            self.num_indices).transpose()
+        return self.field_data[field]
+
+    def trajectory_from_index(self, index):
+        """
+        Retrieve a single trajectory corresponding to a specific particle
+        index
+
+        Parameters
+        ----------
+        index : int
+            This defines which particle trajectory from the
+            ParticleTrajectories object will be returned.
+
+        Returns
+        -------
+        A dictionary corresponding to the particle's trajectory and the
+        fields along that trajectory
+
+        Examples
+        --------
+        >>> from yt.mods import *
+        >>> import matplotlib.pylab as pl
+        >>> trajs = ParticleTrajectories(my_fns, indices)
+        >>> traj = trajs.trajectory_from_index(indices[0])
+        >>> pl.plot(traj["particle_time"], traj["particle_position_x"], "-x")
+        >>> pl.savefig("orbit")
+        """
+        mask = np.in1d(self.indices, (index,), assume_unique=True)
+        if not np.any(mask):
+            print "The particle index %d is not in the list!" % (index)
+            raise IndexError
+        fields = [field for field in sorted(self.field_data.keys())]
+        traj = {}
+        traj["particle_time"] = self.times
+        traj["particle_index"] = index
+        for field in fields:
+            traj[field] = self[field][mask,:][0]
+        return traj
+
+    def write_out(self, filename_base):
+        """
+        Write out particle trajectories to tab-separated ASCII files (one
+        for each trajectory) with the field names in the file header. Each
+        file is named with a basename and the index number.
+
+        Parameters
+        ----------
+        filename_base : string
+            The prefix for the outputted ASCII files.
+
+        Examples
+        --------
+        >>> from yt.mods import *
+        >>> trajs = ParticleTrajectories(my_fns, indices)
+        >>> trajs.write_out("orbit_trajectory")       
+        """
+        fields = [field for field in sorted(self.field_data.keys())]
+        num_fields = len(fields)
+        first_str = "# particle_time\t" + "\t".join(fields)+"\n"
+        template_str = "%g\t"*num_fields+"%g\n"
+        for ix in xrange(self.num_indices):
+            outlines = [first_str]
+            for it in xrange(self.num_steps):
+                outlines.append(template_str %
+                                tuple([self.times[it]]+[self[field][ix,it] for field in fields]))
+            fid = open(filename_base + "_%d.dat" % self.indices[ix], "w")
+            fid.writelines(outlines)
+            fid.close()
+            del fid
+            
+    def write_out_h5(self, filename):
+        """
+        Write out all the particle trajectories to a single HDF5 file
+        that contains the indices, the times, and the 2D array for each
+        field individually
+
+        Parameters
+        ----------
+
+        filename : string
+            The output filename for the HDF5 file
+
+        Examples
+        --------
+
+        >>> from yt.mods import *
+        >>> trajs = ParticleTrajectories(my_fns, indices)
+        >>> trajs.write_out_h5("orbit_trajectories")                
+        """
+        fid = h5py.File(filename, "w")
+        fields = [field for field in sorted(self.field_data.keys())]
+        fid.create_dataset("particle_indices", dtype=np.int32,
+                           data=self.indices)
+        fid.create_dataset("particle_time", data=self.times)
+        for field in fields:
+            fid.create_dataset("%s" % field, data=self[field])
+        fid.close()

diff -r 04756963b86593ba03e6334988f2fc08a2e32034 -r b32a1ca3a07428a81d427edef5ecf9b935be9c33 yt/analysis_modules/particle_trajectories/setup.py
--- /dev/null
+++ b/yt/analysis_modules/particle_trajectories/setup.py
@@ -0,0 +1,13 @@
+#!/usr/bin/env python
+import setuptools
+import os
+import sys
+import os.path
+
+def configuration(parent_package='', top_path=None):
+    from numpy.distutils.misc_util import Configuration
+    config = Configuration('particle_trajectories', parent_package, top_path)
+    #config.add_subpackage("tests")
+    config.make_config_py()  # installs __config__.py
+    #config.make_svn_version_py()
+    return config

diff -r 04756963b86593ba03e6334988f2fc08a2e32034 -r b32a1ca3a07428a81d427edef5ecf9b935be9c33 yt/analysis_modules/sunyaev_zeldovich/projection.py
--- a/yt/analysis_modules/sunyaev_zeldovich/projection.py
+++ b/yt/analysis_modules/sunyaev_zeldovich/projection.py
@@ -27,6 +27,7 @@
 from yt.visualization.volume_rendering.camera import off_axis_projection
 from yt.utilities.parallel_tools.parallel_analysis_interface import \
      communication_system, parallel_root_only
+from yt.visualization.plot_window import StandardCenter
 import numpy as np
 
 I0 = 2*(kboltz*Tcmb)**3/((hcgs*clight)**2)*1.0e17
@@ -122,13 +123,20 @@
         """
         axis = fix_axis(axis)
 
+        if center == "c":
+            ctr = self.pf.domain_center
+        elif center == "max":
+            v, ctr = self.pf.h.find_max("Density")
+        else:
+            ctr = center
+
         def _beta_par(field, data):
             axis = data.get_field_parameter("axis")
             vpar = data["Density"]*data["%s-velocity" % (vlist[axis])]
             return vpar/clight
         add_field("BetaPar", function=_beta_par)    
 
-        proj = self.pf.h.proj(axis, "Density", source=source)
+        proj = self.pf.h.proj(axis, "Density", center=ctr, source=source)
         proj.set_field_parameter("axis", axis)
         frb = proj.to_frb(width, nx)
         dens = frb["Density"]
@@ -181,7 +189,7 @@
         if center == "c":
             ctr = self.pf.domain_center
         elif center == "max":
-            ctr = self.pf.h.find_max("Density")
+            v, ctr = self.pf.h.find_max("Density")
         else:
             ctr = center
 
@@ -259,21 +267,21 @@
         self.data["TeSZ"] = ImageArray(Te)
 
     @parallel_root_only
-    def write_fits(self, filename_prefix, clobber=True):
+    def write_fits(self, filename, clobber=True):
         r""" Export images to a FITS file. Writes the SZ distortion in all
         specified frequencies as well as the mass-weighted temperature and the
         optical depth. Distance units are in kpc.  
         
         Parameters
         ----------
-        filename_prefix : string
-            The prefix of the FITS filename.
+        filename : string
+            The name of the FITS file to be written. 
         clobber : boolean, optional
             If the file already exists, do we overwrite?
                     
         Examples
         --------
-        >>> szprj.write_fits("SZbullet", clobber=False)
+        >>> szprj.write_fits("SZbullet.fits", clobber=False)
         """
         coords = {}
         coords["dx"] = self.dx*self.pf.units["kpc"]
@@ -282,11 +290,12 @@
         coords["yctr"] = 0.0
         coords["units"] = "kpc"
         other_keys = {"Time" : self.pf.current_time}
-        write_fits(self.data, filename_prefix, clobber=clobber, coords=coords,
+        write_fits(self.data, filename, clobber=clobber, coords=coords,
                    other_keys=other_keys)
 
     @parallel_root_only
-    def write_png(self, filename_prefix):
+    def write_png(self, filename_prefix, cmap_name="algae",
+                  log_fields=None):
         r""" Export images to PNG files. Writes the SZ distortion in all
         specified frequencies as well as the mass-weighted temperature and the
         optical depth. Distance units are in kpc. 
@@ -299,17 +308,64 @@
         Examples
         --------
         >>> szprj.write_png("SZsloshing")
-        """     
+        """
+        import matplotlib
+        import matplotlib.pyplot as plt
+        if log_fields is None: log_fields = {}
+        ticks_font = matplotlib.font_manager.FontProperties(family='serif',size=16)
         extent = tuple([bound*self.pf.units["kpc"] for bound in self.bounds])
         for field, image in self.items():
-            filename=filename_prefix+"_"+field+".png"
-            label = self.display_names[field]
+            data = image.copy()
+            vmin, vmax = image.min(), image.max()
+            negative = False
+            crossover = False
+            if vmin < 0 and vmax < 0:
+                data *= -1
+                negative = True                                        
+            if log_fields.has_key(field):
+                log_field = log_fields[field]
+            else:
+                log_field = True
+            if log_field:
+                formatter = matplotlib.ticker.LogFormatterMathtext()        
+                norm = matplotlib.colors.LogNorm()
+                if vmin < 0 and vmax > 0:
+                    crossover = True
+                    linthresh = min(vmax, -vmin)/100.
+                    norm=matplotlib.colors.SymLogNorm(linthresh,
+                                                      vmin=vmin, vmax=vmax)
+            else:
+                norm = None
+                formatter = None
+            filename = filename_prefix+"_"+field+".png"
+            cbar_label = self.display_names[field]
             if self.units[field] is not None:
-                label += " ("+self.units[field]+")"
-            write_projection(image, filename, colorbar_label=label, take_log=False,
-                             extent=extent, xlabel=r"$\mathrm{x\ (kpc)}$",
-                             ylabel=r"$\mathrm{y\ (kpc)}$")
-
+                cbar_label += " ("+self.units[field]+")"
+            fig = plt.figure(figsize=(10.0,8.0))
+            ax = fig.add_subplot(111)
+            cax = ax.imshow(data, norm=norm, extent=extent, cmap=cmap_name, origin="lower")
+            for label in ax.get_xticklabels():
+                label.set_fontproperties(ticks_font)
+            for label in ax.get_yticklabels():
+                label.set_fontproperties(ticks_font)                      
+            ax.set_xlabel(r"$\mathrm{x\ (kpc)}$", fontsize=16)
+            ax.set_ylabel(r"$\mathrm{y\ (kpc)}$", fontsize=16)
+            cbar = fig.colorbar(cax, format=formatter)
+            cbar.ax.set_ylabel(cbar_label, fontsize=16)
+            if negative:
+                cbar.ax.set_yticklabels(["-"+label.get_text()
+                                         for label in cbar.ax.get_yticklabels()])
+            if crossover:
+                yticks = list(-10**np.arange(np.floor(np.log10(-vmin)),
+                                             np.rint(np.log10(linthresh))-1, -1)) + [0] + \
+                         list(10**np.arange(np.rint(np.log10(linthresh)),
+                                            np.ceil(np.log10(vmax))+1))
+                cbar.set_ticks(yticks)
+            for label in cbar.ax.get_yticklabels():
+                label.set_fontproperties(ticks_font)                 
+            fig.tight_layout()
+            plt.savefig(filename)
+            
     @parallel_root_only
     def write_hdf5(self, filename):
         r"""Export the set of S-Z fields to a set of HDF5 datasets.

diff -r 04756963b86593ba03e6334988f2fc08a2e32034 -r b32a1ca3a07428a81d427edef5ecf9b935be9c33 yt/data_objects/api.py
--- a/yt/data_objects/api.py
+++ b/yt/data_objects/api.py
@@ -1,8 +1,5 @@
 """
 API for yt.data_objects
-
-
-
 """
 
 #-----------------------------------------------------------------------------
@@ -72,5 +69,3 @@
     add_grad, \
     derived_field
 
-from particle_trajectories import \
-    ParticleTrajectoryCollection

diff -r 04756963b86593ba03e6334988f2fc08a2e32034 -r b32a1ca3a07428a81d427edef5ecf9b935be9c33 yt/data_objects/data_containers.py
--- a/yt/data_objects/data_containers.py
+++ b/yt/data_objects/data_containers.py
@@ -642,7 +642,7 @@
     --------
 
     >>> pf = load("RedshiftOutput0005")
-    >>> ray = pf.h._ray((0.2, 0.74, 0.11), (0.4, 0.91, 0.31))
+    >>> ray = pf.h.ray((0.2, 0.74, 0.11), (0.4, 0.91, 0.31))
     >>> print ray["Density"], ray["t"], ray["dts"]
     """
     _type_name = "ray"
@@ -1864,7 +1864,7 @@
         new_buf.append(self.comm.mpi_allreduce(buf.pop(0), op=op))
         tree = self._get_tree(len(fields))
         tree.frombuffer(new_buf[0], new_buf[1], new_buf[2], merge_style)
-        coord_data, field_data, weight_data, dxs = [], [], [], []
+        coord_data, field_data, weight_data, dxs, dys = [], [], [], [], []
         for level in range(0, self._max_level + 1):
             npos, nvals, nwvals = tree.get_all_from_level(level, False)
             coord_data.append(npos)
@@ -1873,10 +1873,12 @@
             weight_data.append(nwvals)
             gs = self.source.select_grids(level)
             if len(gs) > 0:
-                ds = gs[0].dds[0]
+                dx = gs[0].dds[x_dict[self.axis]]
+                dy = gs[0].dds[y_dict[self.axis]]
             else:
-                ds = 0.0
-            dxs.append(np.ones(nvals.shape[0], dtype='float64') * ds)
+                dx = dy = 0.0
+            dxs.append(np.ones(nvals.shape[0], dtype='float64') * dx)
+            dys.append(np.ones(nvals.shape[0], dtype='float64') * dy)
         coord_data = np.concatenate(coord_data, axis=0).transpose()
         field_data = np.concatenate(field_data, axis=0).transpose()
         if self._weight is None:
@@ -1884,17 +1886,19 @@
             field_data *= convs[:,None]
         weight_data = np.concatenate(weight_data, axis=0).transpose()
         dxs = np.concatenate(dxs, axis=0).transpose()
+        dys = np.concatenate(dys, axis=0).transpose()
         # We now convert to half-widths and center-points
         data = {}
         data['pdx'] = dxs
+        data['pdy'] = dys
         ox = self.pf.domain_left_edge[x_dict[self.axis]]
         oy = self.pf.domain_left_edge[y_dict[self.axis]]
         data['px'] = (coord_data[0,:]+0.5) * data['pdx'] + ox
-        data['py'] = (coord_data[1,:]+0.5) * data['pdx'] + oy
+        data['py'] = (coord_data[1,:]+0.5) * data['pdy'] + oy
         data['weight_field'] = weight_data
         del coord_data
         data['pdx'] *= 0.5
-        data['pdy'] = data['pdx'] # generalization is out the window!
+        data['pdy'] *= 0.5
         data['fields'] = field_data
         # Now we run the finalizer, which is ignored if we don't need it
         field_data = np.vsplit(data.pop('fields'), len(fields))
@@ -3687,6 +3691,7 @@
                            fields=fields, pf=pf, **kwargs)
         self.left_edge = np.array(left_edge)
         self.level = level
+        dims = np.array(dims)
         rdx = self.pf.domain_dimensions*self.pf.refine_by**level
         rdx[np.where(dims - 2 * num_ghost_zones <= 1)] = 1   # issue 602
         self.dds = self.pf.domain_width / rdx.astype("float64")

diff -r 04756963b86593ba03e6334988f2fc08a2e32034 -r b32a1ca3a07428a81d427edef5ecf9b935be9c33 yt/data_objects/field_info_container.py
--- a/yt/data_objects/field_info_container.py
+++ b/yt/data_objects/field_info_container.py
@@ -190,7 +190,7 @@
         return "(%s)" % (self.missing_parameters)
 
 class FieldDetector(defaultdict):
-    Level = 1
+    Level = level = 1
     NumberOfParticles = 1
     _read_exception = None
     _id_offset = 0

diff -r 04756963b86593ba03e6334988f2fc08a2e32034 -r b32a1ca3a07428a81d427edef5ecf9b935be9c33 yt/data_objects/particle_trajectories.py
--- a/yt/data_objects/particle_trajectories.py
+++ /dev/null
@@ -1,380 +0,0 @@
-"""
-
-
-"""
-
-#-----------------------------------------------------------------------------
-# Copyright (c) 2013, yt Development Team.
-#
-# Distributed under the terms of the Modified BSD License.
-#
-# The full license is in the file COPYING.txt, distributed with this software.
-#-----------------------------------------------------------------------------
-
-from yt.data_objects.data_containers import YTFieldData
-from yt.data_objects.time_series import TimeSeriesData
-from yt.utilities.lib import sample_field_at_positions
-from yt.funcs import *
-
-import numpy as np
-import h5py
-
-class ParticleTrajectoryCollection(object) :
-
-    r"""A collection of particle trajectories in time over a series of
-    parameter files. 
-
-    The ParticleTrajectoryCollection object contains a collection of
-    particle trajectories for a specified set of particle indices. 
-    
-    Parameters
-    ----------
-    filenames : list of strings
-        A time-sorted list of filenames to construct the TimeSeriesData
-        object.
-    indices : array_like
-        An integer array of particle indices whose trajectories we
-        want to track. If they are not sorted they will be sorted.
-    fields : list of strings, optional
-        A set of fields that is retrieved when the trajectory
-        collection is instantiated.
-        Default : None (will default to the fields 'particle_position_x',
-        'particle_position_y', 'particle_position_z')
-
-    Examples
-    ________
-    >>> from yt.mods import *
-    >>> my_fns = glob.glob("orbit_hdf5_chk_00[0-9][0-9]")
-    >>> my_fns.sort()
-    >>> fields = ["particle_position_x", "particle_position_y",
-    >>>           "particle_position_z", "particle_velocity_x",
-    >>>           "particle_velocity_y", "particle_velocity_z"]
-    >>> pf = load(my_fns[0])
-    >>> init_sphere = pf.h.sphere(pf.domain_center, (.5, "unitary"))
-    >>> indices = init_sphere["particle_index"].astype("int")
-    >>> trajs = ParticleTrajectoryCollection(my_fns, indices, fields=fields)
-    >>> for t in trajs :
-    >>>     print t["particle_velocity_x"].max(), t["particle_velocity_x"].min()
-
-    Notes
-    -----
-    As of this time only particle trajectories that are complete over the
-    set of specified parameter files are supported. If any particle's history
-    ends for some reason (e.g. leaving the simulation domain or being actively
-    destroyed), the whole trajectory collection of which it is a set must end
-    at or before the particle's last timestep. This is a limitation we hope to
-    lift at some point in the future.     
-    """
-    def __init__(self, filenames, indices, fields = None) :
-
-        indices.sort() # Just in case the caller wasn't careful
-        
-        self.field_data = YTFieldData()
-        self.pfs = TimeSeriesData.from_filenames(filenames)
-        self.masks = []
-        self.sorts = []
-        self.indices = indices
-        self.num_indices = len(indices)
-        self.num_steps = len(filenames)
-        self.times = []
-
-        # Default fields 
-        
-        if fields is None : fields = []
-
-        # Must ALWAYS have these fields
-        
-        fields = fields + ["particle_position_x",
-                           "particle_position_y",
-                           "particle_position_z"]
-
-        """
-        The following loops through the parameter files
-        and performs two tasks. The first is to isolate
-        the particles with the correct indices, and the
-        second is to create a sorted list of these particles.
-        We also make a list of the current time from each file. 
-        Right now, the code assumes (and checks for) the
-        particle indices existing in each file, a limitation I
-        would like to lift at some point since some codes
-        (e.g., FLASH) destroy particles leaving the domain.
-        """
-        
-        for pf in self.pfs :
-            dd = pf.h.all_data()
-            newtags = dd["particle_index"].astype("int")
-            if not np.all(np.in1d(indices, newtags, assume_unique=True)) :
-                print "Not all requested particle ids contained in this file!"
-                raise IndexError
-            mask = np.in1d(newtags, indices, assume_unique=True)
-            sorts = np.argsort(newtags[mask])
-            self.masks.append(mask)            
-            self.sorts.append(sorts)
-            self.times.append(pf.current_time)
-
-        self.times = np.array(self.times)
-
-        # Set up the derived field list and the particle field list
-        # so that if the requested field is a particle field, we'll
-        # just copy the field over, but if the field is a grid field,
-        # we will first copy the field over to the particle positions
-        # and then return the field. 
-
-        self.derived_field_list = self.pfs[0].h.derived_field_list
-        self.particle_fields = [field for field in self.derived_field_list
-                                if self.pfs[0].field_info[field].particle_type]
-
-        # Now instantiate the requested fields 
-        for field in fields :
-
-            self._get_data(field)
-            
-    def has_key(self, key) :
-
-        return (key in self.field_data)
-    
-    def keys(self) :
-
-        return self.field_data.keys()
-
-    def __getitem__(self, key) :
-        """
-        Get the field associated with key,
-        checking to make sure it is a particle field.
-        """
-
-        if not self.field_data.has_key(key) :
-
-            self._get_data(key)
-
-        return self.field_data[key]
-    
-    def __setitem__(self, key, val):
-        """
-        Sets a field to be some other value.
-        """
-        self.field_data[key] = val
-                        
-    def __delitem__(self, key) :
-        """
-        Delete the field from the trajectory
-        """
-        del self.field_data[key]
-
-    def __iter__(self) :
-
-        """
-        This iterates over the trajectories for
-        the different particles, returning dicts
-        of fields for each trajectory
-        """
-        for idx in xrange(self.num_indices) :
-            traj = {}
-            traj["particle_index"] = self.indices[idx]
-            traj["particle_time"] = self.times
-            for field in self.field_data.keys() :
-                traj[field] = self[field][idx,:]
-            yield traj
-            
-    def __len__(self) :
-
-        """
-        The number of individual trajectories
-        """
-        return self.num_indices
-
-    def add_fields(self, fields) :
-
-        """
-        Add a list of fields to an existing trajectory
-
-        Parameters
-        ----------
-        fields : list of strings
-            A list of fields to be added to the current trajectory
-            collection.
-
-        Examples
-        ________
-        >>> from yt.mods import *
-        >>> trajs = ParticleTrajectoryCollection(my_fns, indices)
-        >>> trajs.add_fields(["particle_mass", "particle_gpot"])
-        """
-        
-        for field in fields :
-
-            if not self.field_data.has_key(field):
-
-                self._get_data(field)
-                
-    def _get_data(self, field) :
-
-        """
-        Get a field to include in the trajectory collection.
-        The trajectory collection itself is a dict of 2D numpy arrays,
-        with shape (num_indices, num_steps)
-        """
-        
-        if not self.field_data.has_key(field):
-            
-            particles = np.empty((0))
-
-            step = int(0)
-                
-            for pf, mask, sort in zip(self.pfs, self.masks, self.sorts) :
-                                    
-                if field in self.particle_fields :
-
-                    # This is easy... just get the particle fields
-
-                    dd = pf.h.all_data()
-                    pfield = dd[field][mask]
-                    particles = np.append(particles, pfield[sort])
-
-                else :
-
-                    # This is hard... must loop over grids
-
-                    pfield = np.zeros((self.num_indices))
-                    x = self["particle_position_x"][:,step]
-                    y = self["particle_position_y"][:,step]
-                    z = self["particle_position_z"][:,step]
-
-                    leaf_grids = [g for g in pf.h.grids if len(g.Children) == 0]
-                        
-                    for grid in leaf_grids :
-
-                        pfield += sample_field_at_positions(grid[field],
-                                                            grid.LeftEdge,
-                                                            grid.RightEdge,
-                                                            x, y, z)
-
-                    particles = np.append(particles, pfield)
-
-                step += 1
-                
-            self[field] = particles.reshape(self.num_steps,
-                                            self.num_indices).transpose()
-
-        return self.field_data[field]
-
-    def trajectory_from_index(self, index) :
-
-        """
-        Retrieve a single trajectory corresponding to a specific particle
-        index
-
-        Parameters
-        ----------
-        index : int
-            This defines which particle trajectory from the
-            ParticleTrajectoryCollection object will be returned.
-
-        Returns
-        -------
-        A dictionary corresponding to the particle's trajectory and the
-        fields along that trajectory
-
-        Examples
-        --------
-        >>> from yt.mods import *
-        >>> import matplotlib.pylab as pl
-        >>> trajs = ParticleTrajectoryCollection(my_fns, indices)
-        >>> traj = trajs.trajectory_from_index(indices[0])
-        >>> pl.plot(traj["particle_time"], traj["particle_position_x"], "-x")
-        >>> pl.savefig("orbit")
-        """
-        
-        mask = np.in1d(self.indices, (index,), assume_unique=True)
-
-        if not np.any(mask) :
-            print "The particle index %d is not in the list!" % (index)
-            raise IndexError
-
-        fields = [field for field in sorted(self.field_data.keys())]
-                                
-        traj = {}
-
-        traj["particle_time"] = self.times
-        traj["particle_index"] = index
-        
-        for field in fields :
-
-            traj[field] = self[field][mask,:][0]
-
-        return traj
-
-    def write_out(self, filename_base) :
-
-        """
-        Write out particle trajectories to tab-separated ASCII files (one
-        for each trajectory) with the field names in the file header. Each
-        file is named with a basename and the index number.
-
-        Parameters
-        ----------
-        filename_base : string
-            The prefix for the outputted ASCII files.
-
-        Examples
-        --------
-        >>> from yt.mods import *
-        >>> trajs = ParticleTrajectoryCollection(my_fns, indices)
-        >>> trajs.write_out("orbit_trajectory")       
-        """
-        
-        fields = [field for field in sorted(self.field_data.keys())]
-
-        num_fields = len(fields)
-
-        first_str = "# particle_time\t" + "\t".join(fields)+"\n"
-        
-        template_str = "%g\t"*num_fields+"%g\n"
-        
-        for ix in xrange(self.num_indices) :
-
-            outlines = [first_str]
-
-            for it in xrange(self.num_steps) :
-                outlines.append(template_str %
-                                tuple([self.times[it]]+[self[field][ix,it] for field in fields]))
-            
-            fid = open(filename_base + "_%d.dat" % self.indices[ix], "w")
-            fid.writelines(outlines)
-            fid.close()
-            del fid
-            
-    def write_out_h5(self, filename) :
-
-        """
-        Write out all the particle trajectories to a single HDF5 file
-        that contains the indices, the times, and the 2D array for each
-        field individually
-
-        Parameters
-        ----------
-
-        filename : string
-            The output filename for the HDF5 file
-
-        Examples
-        --------
-
-        >>> from yt.mods import *
-        >>> trajs = ParticleTrajectoryCollection(my_fns, indices)
-        >>> trajs.write_out_h5("orbit_trajectories")                
-        """
-        
-        fid = h5py.File(filename, "w")
-
-        fields = [field for field in sorted(self.field_data.keys())]
-        
-        fid.create_dataset("particle_indices", dtype=np.int32,
-                           data=self.indices)
-        fid.create_dataset("particle_time", data=self.times)
-        
-        for field in fields :
-
-            fid.create_dataset("%s" % field, data=self[field])
-                        
-        fid.close()

diff -r 04756963b86593ba03e6334988f2fc08a2e32034 -r b32a1ca3a07428a81d427edef5ecf9b935be9c33 yt/data_objects/time_series.py
--- a/yt/data_objects/time_series.py
+++ b/yt/data_objects/time_series.py
@@ -13,10 +13,11 @@
 # The full license is in the file COPYING.txt, distributed with this software.
 #-----------------------------------------------------------------------------
 
-import inspect, functools, weakref, glob, types
+import inspect, functools, weakref, glob, types, os
 
 from yt.funcs import *
 from yt.convenience import load
+from yt.config import ytcfg
 from .data_containers import data_object_registry
 from .analyzer_objects import create_quantity_proxy, \
     analysis_task_registry, AnalysisTask
@@ -250,10 +251,17 @@
         """
         
         if isinstance(filenames, types.StringTypes):
-            filenames = glob.glob(filenames)
+            if len(glob.glob(filenames)) == 0:
+                data_dir = ytcfg.get("yt", "test_data_dir")
+                pattern = os.path.join(data_dir, filenames)
+                td_filenames = glob.glob(pattern)
+                if len(td_filenames) > 0:
+                    filenames = td_filenames
+                else:
+                    raise YTOutputNotIdentified(filenames, {})
+            else:
+                filenames = glob.glob(filenames)
             filenames.sort()
-        if len(filenames) == 0:
-            raise YTOutputNotIdentified(filenames, {})
         obj = cls(filenames[:], parallel = parallel, **kwargs)
         return obj
 

diff -r 04756963b86593ba03e6334988f2fc08a2e32034 -r b32a1ca3a07428a81d427edef5ecf9b935be9c33 yt/data_objects/universal_fields.py
--- a/yt/data_objects/universal_fields.py
+++ b/yt/data_objects/universal_fields.py
@@ -1124,6 +1124,182 @@
           units=r"\rm{s}^{-2}",
           convert_function=_convertVorticitySquared)
 
+def _Shear(field, data):
+    """
+    Shear is defined as [(dvx/dy + dvy/dx)^2 + (dvz/dy + dvy/dz)^2 +
+                         (dvx/dz + dvz/dx)^2 ]^(0.5)
+    where dvx/dy = [vx(j-1) - vx(j+1)]/[2dy]
+    and is in units of s^(-1)
+    (it's just like vorticity except add the derivative pairs instead
+     of subtracting them)
+    """
+    # We need to set up stencils
+    if data.pf["HydroMethod"] == 2:
+        sl_left = slice(None,-2,None)
+        sl_right = slice(1,-1,None)
+        div_fac = 1.0
+    else:
+        sl_left = slice(None,-2,None)
+        sl_right = slice(2,None,None)
+        div_fac = 2.0
+    new_field = np.zeros(data["x-velocity"].shape)
+    if data.pf.dimensionality > 1:
+        dvydx = (data["y-velocity"][sl_right,1:-1,1:-1] -
+                data["y-velocity"][sl_left,1:-1,1:-1]) \
+                / (div_fac*data["dx"].flat[0])
+        dvxdy = (data["x-velocity"][1:-1,sl_right,1:-1] -
+                data["x-velocity"][1:-1,sl_left,1:-1]) \
+                / (div_fac*data["dy"].flat[0])
+        new_field[1:-1,1:-1,1:-1] += (dvydx + dvxdy)**2.0
+        del dvydx, dvxdy
+    if data.pf.dimensionality > 2:
+        dvzdy = (data["z-velocity"][1:-1,sl_right,1:-1] -
+                data["z-velocity"][1:-1,sl_left,1:-1]) \
+                / (div_fac*data["dy"].flat[0])
+        dvydz = (data["y-velocity"][1:-1,1:-1,sl_right] -
+                data["y-velocity"][1:-1,1:-1,sl_left]) \
+                / (div_fac*data["dz"].flat[0])
+        new_field[1:-1,1:-1,1:-1] += (dvzdy + dvydz)**2.0
+        del dvzdy, dvydz
+        dvxdz = (data["x-velocity"][1:-1,1:-1,sl_right] -
+                data["x-velocity"][1:-1,1:-1,sl_left]) \
+                / (div_fac*data["dz"].flat[0])
+        dvzdx = (data["z-velocity"][sl_right,1:-1,1:-1] -
+                data["z-velocity"][sl_left,1:-1,1:-1]) \
+                / (div_fac*data["dx"].flat[0])
+        new_field[1:-1,1:-1,1:-1] += (dvxdz + dvzdx)**2.0
+        del dvxdz, dvzdx
+    new_field = new_field**0.5
+    new_field = np.abs(new_field)
+    return new_field
+def _convertShear(data):
+    return data.convert("cm")**-1.0
+add_field("Shear", function=_Shear,
+          validators=[ValidateSpatial(1,
+              ["x-velocity","y-velocity","z-velocity"])],
+          units=r"\rm{s}^{-1}",
+          convert_function=_convertShear, take_log=False)
+
+def _ShearCriterion(field, data):
+    """
+    Shear is defined as [(dvx/dy + dvy/dx)^2 + (dvz/dy + dvy/dz)^2 +
+                         (dvx/dz + dvz/dx)^2 ]^(0.5)
+    where dvx/dy = [vx(j-1) - vx(j+1)]/[2dy]
+    and is in units of s^(-1)
+    (it's just like vorticity except add the derivative pairs instead
+     of subtracting them)
+
+    Divide by c_s to leave Shear in units of cm**-1, which 
+    can be compared against the inverse of the local cell size (1/dx) 
+    to determine if refinement should occur.
+    """
+    # We need to set up stencils
+    if data.pf["HydroMethod"] == 2:
+        sl_left = slice(None,-2,None)
+        sl_right = slice(1,-1,None)
+        div_fac = 1.0
+    else:
+        sl_left = slice(None,-2,None)
+        sl_right = slice(2,None,None)
+        div_fac = 2.0
+    new_field = np.zeros(data["x-velocity"].shape)
+    if data.pf.dimensionality > 1:
+        dvydx = (data["y-velocity"][sl_right,1:-1,1:-1] -
+                data["y-velocity"][sl_left,1:-1,1:-1]) \
+                / (div_fac*data["dx"].flat[0])
+        dvxdy = (data["x-velocity"][1:-1,sl_right,1:-1] -
+                data["x-velocity"][1:-1,sl_left,1:-1]) \
+                / (div_fac*data["dy"].flat[0])
+        new_field[1:-1,1:-1,1:-1] += (dvydx + dvxdy)**2.0
+        del dvydx, dvxdy
+    if data.pf.dimensionality > 2:
+        dvzdy = (data["z-velocity"][1:-1,sl_right,1:-1] -
+                data["z-velocity"][1:-1,sl_left,1:-1]) \
+                / (div_fac*data["dy"].flat[0])
+        dvydz = (data["y-velocity"][1:-1,1:-1,sl_right] -
+                data["y-velocity"][1:-1,1:-1,sl_left]) \
+                / (div_fac*data["dz"].flat[0])
+        new_field[1:-1,1:-1,1:-1] += (dvzdy + dvydz)**2.0
+        del dvzdy, dvydz
+        dvxdz = (data["x-velocity"][1:-1,1:-1,sl_right] -
+                data["x-velocity"][1:-1,1:-1,sl_left]) \
+                / (div_fac*data["dz"].flat[0])
+        dvzdx = (data["z-velocity"][sl_right,1:-1,1:-1] -
+                data["z-velocity"][sl_left,1:-1,1:-1]) \
+                / (div_fac*data["dx"].flat[0])
+        new_field[1:-1,1:-1,1:-1] += (dvxdz + dvzdx)**2.0
+        del dvxdz, dvzdx
+    new_field /= data["SoundSpeed"]**2.0
+    new_field = new_field**(0.5)
+    new_field = np.abs(new_field)
+    return new_field
+
+def _convertShearCriterion(data):
+    return data.convert("cm")**-1.0
+add_field("ShearCriterion", function=_ShearCriterion,
+          validators=[ValidateSpatial(1,
+              ["x-velocity","y-velocity","z-velocity", "SoundSpeed"])],
+          units=r"\rm{cm}^{-1}",
+          convert_function=_convertShearCriterion, take_log=False)
+
+def _ShearMach(field, data):
+    """
+    Dimensionless Shear (ShearMach) is defined nearly the same as shear, 
+    except that it is scaled by the local dx/dy/dz and the local sound speed.
+    So it results in a unitless quantity that is effectively measuring 
+    shear in mach number.  
+
+    In order to avoid discontinuities created by multiplying by dx/dy/dz at
+    grid refinement boundaries, we also multiply by 2**GridLevel.
+
+    Shear (Mach) = [(dvx + dvy)^2 + (dvz + dvy)^2 +
+                    (dvx + dvz)^2  ]^(0.5) / c_sound
+    """
+    # We need to set up stencils
+    if data.pf["HydroMethod"] == 2:
+        sl_left = slice(None,-2,None)
+        sl_right = slice(1,-1,None)
+        div_fac = 1.0
+    else:
+        sl_left = slice(None,-2,None)
+        sl_right = slice(2,None,None)
+        div_fac = 2.0
+    new_field = np.zeros(data["x-velocity"].shape)
+    if data.pf.dimensionality > 1:
+        dvydx = (data["y-velocity"][sl_right,1:-1,1:-1] -
+                data["y-velocity"][sl_left,1:-1,1:-1]) \
+                / (div_fac)
+        dvxdy = (data["x-velocity"][1:-1,sl_right,1:-1] -
+                data["x-velocity"][1:-1,sl_left,1:-1]) \
+                / (div_fac)
+        new_field[1:-1,1:-1,1:-1] += (dvydx + dvxdy)**2.0
+        del dvydx, dvxdy
+    if data.pf.dimensionality > 2:
+        dvzdy = (data["z-velocity"][1:-1,sl_right,1:-1] -
+                data["z-velocity"][1:-1,sl_left,1:-1]) \
+                / (div_fac)
+        dvydz = (data["y-velocity"][1:-1,1:-1,sl_right] -
+                data["y-velocity"][1:-1,1:-1,sl_left]) \
+                / (div_fac)
+        new_field[1:-1,1:-1,1:-1] += (dvzdy + dvydz)**2.0
+        del dvzdy, dvydz
+        dvxdz = (data["x-velocity"][1:-1,1:-1,sl_right] -
+                data["x-velocity"][1:-1,1:-1,sl_left]) \
+                / (div_fac)
+        dvzdx = (data["z-velocity"][sl_right,1:-1,1:-1] -
+                data["z-velocity"][sl_left,1:-1,1:-1]) \
+                / (div_fac)
+        new_field[1:-1,1:-1,1:-1] += (dvxdz + dvzdx)**2.0
+        del dvxdz, dvzdx
+    new_field *= ((2.0**data.level)/data["SoundSpeed"])**2.0
+    new_field = new_field**0.5
+    new_field = np.abs(new_field)
+    return new_field
+add_field("ShearMach", function=_ShearMach,
+          validators=[ValidateSpatial(1,
+              ["x-velocity","y-velocity","z-velocity","SoundSpeed"])],
+          units=r"\rm{Mach}",take_log=False)
+
 def _gradPressureX(field, data):
     # We need to set up stencils
     if data.pf["HydroMethod"] == 2:

diff -r 04756963b86593ba03e6334988f2fc08a2e32034 -r b32a1ca3a07428a81d427edef5ecf9b935be9c33 yt/frontends/art/__init__.py
--- a/yt/frontends/art/__init__.py
+++ /dev/null
@@ -1,14 +0,0 @@
-"""
-API for yt.frontends.art
-
-
-
-"""
-
-#-----------------------------------------------------------------------------
-# Copyright (c) 2013, yt Development Team.
-#
-# Distributed under the terms of the Modified BSD License.
-#
-# The full license is in the file COPYING.txt, distributed with this software.
-#-----------------------------------------------------------------------------

diff -r 04756963b86593ba03e6334988f2fc08a2e32034 -r b32a1ca3a07428a81d427edef5ecf9b935be9c33 yt/frontends/art/api.py
--- a/yt/frontends/art/api.py
+++ /dev/null
@@ -1,26 +0,0 @@
-"""
-API for yt.frontends.art
-
-
-
-"""
-
-#-----------------------------------------------------------------------------
-# Copyright (c) 2013, yt Development Team.
-#
-# Distributed under the terms of the Modified BSD License.
-#
-# The full license is in the file COPYING.txt, distributed with this software.
-#-----------------------------------------------------------------------------
-
-from .data_structures import \
-      ARTGrid, \
-      ARTHierarchy, \
-      ARTStaticOutput
-
-from .fields import \
-      ARTFieldInfo, \
-      add_art_field
-
-from .io import \
-      IOHandlerART

This diff is so big that we needed to truncate the remainder.

https://bitbucket.org/yt_analysis/yt/commits/8e79a40695ba/
Changeset:   8e79a40695ba
Branch:      yt
User:        jzuhone
Date:        2013-10-31 19:56:37
Summary:     Docstring edits
Affected #:  1 file

diff -r b32a1ca3a07428a81d427edef5ecf9b935be9c33 -r 8e79a40695bad7ef8f21448eb46f67936b824adb yt/analysis_modules/photon_simulator/photon_simulator.py
--- a/yt/analysis_modules/photon_simulator/photon_simulator.py
+++ b/yt/analysis_modules/photon_simulator/photon_simulator.py
@@ -407,10 +407,9 @@
 
         L : array_like
             Normal vector to the plane of projection.
-        area_new : float or filename, optional
-            New value for the effective area of the detector. 
-            Either a single float value or a standard ARF file
-            containing the effective area as a function of energy.
+        area_new : float, optional
+            New value for the effective area of the detector. If *responses*
+            are specified the value of this keyword is ignored.
         exp_time_new : float, optional
             The new value for the exposure time.
         redshift_new : float, optional
@@ -426,6 +425,8 @@
             standard deviation *psf_sigma* in degrees. 
         sky_center : array_like, optional
             Center RA, Dec of the events in degrees.
+        responses : list of strings, optional
+            The names of the ARF and RMF files to convolve the photons with.
 
         Examples
         --------
@@ -713,7 +714,11 @@
         self.wcs.wcs.cdelt = [-parameters["dtheta"], parameters["dtheta"]]
         self.wcs.wcs.ctype = ["RA---TAN","DEC--TAN"]
         self.wcs.wcs.cunit = ["deg"]*2                                                
-        
+        (self.events["xsky"],
+         self.events["ysky"]) = \
+         self.wcs.wcs_pix2world(self.events["xpix"], self.events["ypix"],
+                                1, ra_dec_order=True)
+
     def keys(self):
         return self.events.keys()
 
@@ -726,17 +731,12 @@
     def values(self):
         return self.events.values()
     
-    def __getitem__(self,key):
+    def __getitem__(self,key):                        
+        return self.events[key]
 
-        if key == "xsky" or key == "ysky":
-            if not self.has_key(key):
-                (self.events["xsky"],
-                 self.events["ysky"]) = \
-                 self.wcs.wcs_pix2world(events["xpix"], events["ypix"],
-                                        1, ra_dec_order=True)
-                        
-        return self.events[key]
-        
+    def __repr__(self):
+        return self.events.__repr__()
+   
     @classmethod
     def from_h5_file(cls, h5file):
         """
@@ -828,93 +828,7 @@
             events[k1] = np.concatenate([v1,v2])
         
         return cls(events, events1.parameters)
-        
-    def convolve_with_response(self, respfile):
-        """
-        Convolve the events with a RMF file *respfile*.
-        """
-        mylog.warning("This routine has not been tested to work with all RMFs. YMMV.")
-        if not "ARF" in self.parameters:
-            mylog.warning("Photons have not been processed with an"+
-                          " auxiliary response file. Spectral fitting"+
-                          " may be inaccurate.")
-
-        mylog.info("Reading response matrix file (RMF): %s" % (respfile))
-        
-        hdulist = pyfits.open(respfile)
-
-        tblhdu = hdulist["MATRIX"]
-        n_de = len(tblhdu.data["ENERG_LO"])
-        mylog.info("Number of Energy Bins: %d" % (n_de))
-        de = tblhdu.data["ENERG_HI"] - tblhdu.data["ENERG_LO"]
-
-        mylog.info("Energy limits: %g %g" % (min(tblhdu.data["ENERG_LO"]),
-                                             max(tblhdu.data["ENERG_HI"])))
-
-        tblhdu2 = hdulist["EBOUNDS"]
-        n_ch = len(tblhdu2.data["CHANNEL"])
-        mylog.info("Number of Channels: %d" % (n_ch))
-        
-        eidxs = np.argsort(self.events["eobs"])
-
-        phEE = self.events["eobs"][eidxs]
-        phXX = self.events["xpix"][eidxs]
-        phYY = self.events["ypix"][eidxs]
-
-        detectedChannels = []
-        pindex = 0
-
-        # run through all photon energies and find which bin they go in
-        k = 0
-        fcurr = 0
-        last = len(phEE)-1
-
-        pbar = get_pbar("Scattering energies with RMF:", n_de)
-        
-        for low,high in zip(tblhdu.data["ENERG_LO"],tblhdu.data["ENERG_HI"]):
-            # weight function for probabilities from RMF
-            weights = np.nan_to_num(tblhdu.data[k]["MATRIX"][:])
-            weights /= weights.sum()
-            # build channel number list associated to array value,
-            # there are groups of channels in rmfs with nonzero probabilities
-            trueChannel = []
-            f_chan = np.nan_to_num(tblhdu.data["F_CHAN"][k])
-            n_chan = np.nan_to_num(tblhdu.data["N_CHAN"][k])
-            n_grp = np.nan_to_num(tblhdu.data["N_CHAN"][k])
-            if not iterable(f_chan):
-                f_chan = [f_chan]
-                n_chan = [n_chan]
-                n_grp  = [n_grp]
-            for start,nchan in zip(f_chan, n_chan):
-                end = start + nchan
-                if start == end:
-                    trueChannel.append(start)
-                else:
-                    for j in range(start,end):
-                        trueChannel.append(j)
-            if len(trueChannel) > 0:
-                for q in range(fcurr,last):
-                    if phEE[q] >= low and phEE[q] < high:
-                        channelInd = np.random.choice(len(weights), p=weights)
-                        fcurr +=1
-                        detectedChannels.append(trueChannel[channelInd])
-                    if phEE[q] >= high:
-                        break
-            pbar.update(k)
-            k+=1
-        pbar.finish()
-        
-        dchannel = np.array(detectedChannels)
-
-        self.events["xpix"] = phXX
-        self.events["ypix"] = phYY
-        self.events["eobs"] = phEE
-        self.events[tblhdu.header["CHANTYPE"]] = dchannel.astype(int)
-        self.parameters["RMF"] = respfile
-        self.parameters["ChannelType"] = tblhdu.header["CHANTYPE"]
-        self.parameters["Telescope"] = tblhdu.header["TELESCOP"]
-        self.parameters["Instrument"] = tblhdu.header["INSTRUME"]
-        
+                
     @parallel_root_only
     def write_fits_file(self, fitsfile, clobber=False):
         """
@@ -1095,6 +1009,8 @@
                             
         f.create_dataset("/xpix", data=self.events["xpix"])
         f.create_dataset("/ypix", data=self.events["ypix"])
+        f.create_dataset("/xsky", data=self.events["xsky"])
+        f.create_dataset("/ysky", data=self.events["ysky"])
         f.create_dataset("/eobs", data=self.events["eobs"])
         if "PI" in self.events:
             f.create_dataset("/pi", data=self.events["PI"])                  


https://bitbucket.org/yt_analysis/yt/commits/d4ce479df4be/
Changeset:   d4ce479df4be
Branch:      yt
User:        jzuhone
Date:        2013-10-31 19:58:21
Summary:     Merging
Affected #:  2 files

diff -r 8e79a40695bad7ef8f21448eb46f67936b824adb -r d4ce479df4becafd5e981e2b641c791f2ff100f9 yt/analysis_modules/photon_simulator/tests/test_beta_model.py
--- a/yt/analysis_modules/photon_simulator/tests/test_beta_model.py
+++ b/yt/analysis_modules/photon_simulator/tests/test_beta_model.py
@@ -19,6 +19,7 @@
      XSpecThermalModel, XSpecAbsorbModel, ThermalPhotonModel
 import os
 import xspec
+import gc
 
 def setup():
     """Test specific setup."""
@@ -27,7 +28,6 @@
                 
 @requires_module("xspec")
 def test_beta_model():
-
     # Set up the beta model and stream dataset
     R = 1000.
     r_c = 100.
@@ -70,9 +70,14 @@
 
     # Create the photons
 
-    ARF = os.environ["YT_DATA_DIR"]+"xray_data/chandra_ACIS-S3_onaxis_arf.fits"
-    RMF = os.environ["YT_DATA_DIR"]+"xray_data/chandra_ACIS-S3_onaxis_rmf.fits"
-                    
+    # XSPEC is buggy so for this test we have to do it this way
+    
+    ARF = "chandra_ACIS-S3_onaxis_arf.fits"
+    RMF = "chandra_ACIS-S3_onaxis_rmf.fits"
+            
+    os.system("cp "+ os.environ["YT_DATA_DIR"]+"xray_data/"+ARF+" "+os.getcwd())
+    os.system("cp "+ os.environ["YT_DATA_DIR"]+"xray_data/"+RMF+" "+os.getcwd())
+    
     A = 6000.
     exp_time = 1.0e5
     redshift = 0.05
@@ -93,8 +98,12 @@
                                      absorb_model=abs_model)
     events.write_spectrum("spec_chandra.fits", clobber=True)
 
+    del photons, events
+
+    gc.collect()
+    
     # Now fit the resulting spectrum
-    
+
     spec = xspec.Spectrum("spec_chandra.fits")
     xspec.Fit.statMethod = "cstat"
     
@@ -115,3 +124,6 @@
     assert(T > m.apec.kT.error[0] and T < m.apec.kT.error[1])
     assert(Zmet > m.apec.Abundanc.error[0] and Zmet < m.apec.Abundanc.error[1])
     assert(norm > m.apec.norm.error[0] and norm < m.apec.norm.error[1])
+
+if __name__ == "__main__":
+    test_beta_model()

diff -r 8e79a40695bad7ef8f21448eb46f67936b824adb -r d4ce479df4becafd5e981e2b641c791f2ff100f9 yt/analysis_modules/photon_simulator/tests/test_cluster.py
--- a/yt/analysis_modules/photon_simulator/tests/test_cluster.py
+++ b/yt/analysis_modules/photon_simulator/tests/test_cluster.py
@@ -25,6 +25,7 @@
 @requires_module("xspec")
 @requires_pf(MHD)
 def test_cluster():
+    np.random.seed(seed=0x4d3d3d3)
     pf = data_dir_load(MHD, parameters={"TimeUnits":3.1557e13,
                                         "LengthUnits":3.0856e24,
                                         "DensityUnits":6.770424595218825e-27})
@@ -50,17 +51,15 @@
                                      absorb_model=abs_model)
     
     for k,v in photons.items():
-        if isinstance(v,np.ndarray):
-            def photons_test(v): return v
-            test = GenericArrayTest(pf, photons_test)
-            test_cluster.__name__ = test.description
-            yield test
+        def photons_test(v): return v
+        test = GenericArrayTest(pf, photons_test)
+        test_cluster.__name__ = test.description
+        yield test
 
     for k,v in events.items():
-        if isinstance(v,np.ndarray):
-            def events_test(v): return v
-            test = GenericArrayTest(pf, events_test)
-            test_cluster.__name__ = test.description
-            yield test
+        def events_test(v): return v
+        test = GenericArrayTest(pf, events_test)
+        test_cluster.__name__ = test.description
+        yield test
             
             


https://bitbucket.org/yt_analysis/yt/commits/3bd4130784fa/
Changeset:   3bd4130784fa
Branch:      yt
User:        jzuhone
Date:        2013-10-31 20:21:47
Summary:     Updating references
Affected #:  2 files

diff -r d4ce479df4becafd5e981e2b641c791f2ff100f9 -r 3bd4130784fa169d84fec5060bc0e121d3831893 yt/analysis_modules/photon_simulator/photon_models.py
--- a/yt/analysis_modules/photon_simulator/photon_models.py
+++ b/yt/analysis_modules/photon_simulator/photon_models.py
@@ -6,8 +6,12 @@
 developed by Veronica Biffi and Klaus Dolag. References for
 PHOX may be found at:
 
-Biffi et al 2012: http://adsabs.harvard.edu/abs/2012MNRAS.420.3545B
-Biffi et al 2013: http://adsabs.harvard.edu/abs/2013MNRAS.428.1395B
+Biffi, V., Dolag, K., Bohringer, H., & Lemson, G. 2012, MNRAS, 420, 3545
+http://adsabs.harvard.edu/abs/2012MNRAS.420.3545B
+
+Biffi, V., Dolag, K., Bohringer, H. 2013, MNRAS, 428, 1395
+http://adsabs.harvard.edu/abs/2013MNRAS.428.1395B
+
 """
 
 #-----------------------------------------------------------------------------

diff -r d4ce479df4becafd5e981e2b641c791f2ff100f9 -r 3bd4130784fa169d84fec5060bc0e121d3831893 yt/analysis_modules/photon_simulator/photon_simulator.py
--- a/yt/analysis_modules/photon_simulator/photon_simulator.py
+++ b/yt/analysis_modules/photon_simulator/photon_simulator.py
@@ -5,8 +5,11 @@
 developed by Veronica Biffi and Klaus Dolag. References for
 PHOX may be found at:
 
-Biffi et al 2012: http://adsabs.harvard.edu/abs/2012MNRAS.420.3545B
-Biffi et al 2013: http://adsabs.harvard.edu/abs/2013MNRAS.428.1395B
+Biffi, V., Dolag, K., Bohringer, H., & Lemson, G. 2012, MNRAS, 420, 3545
+http://adsabs.harvard.edu/abs/2012MNRAS.420.3545B
+
+Biffi, V., Dolag, K., Bohringer, H. 2013, MNRAS, 428, 1395
+http://adsabs.harvard.edu/abs/2013MNRAS.428.1395B
 """
 
 #-----------------------------------------------------------------------------


https://bitbucket.org/yt_analysis/yt/commits/752ff7b58153/
Changeset:   752ff7b58153
Branch:      yt
User:        jzuhone
Date:        2013-11-01 17:51:21
Summary:     Return no spectrum for T outside bounds
Affected #:  1 file

diff -r 3bd4130784fa169d84fec5060bc0e121d3831893 -r 752ff7b58153c3891022e28726ccd4f120e6d56d yt/analysis_modules/photon_simulator/spectral_models.py
--- a/yt/analysis_modules/photon_simulator/spectral_models.py
+++ b/yt/analysis_modules/photon_simulator/spectral_models.py
@@ -265,6 +265,8 @@
         cspec_r = np.zeros((self.nchan))
         mspec_r = np.zeros((self.nchan))
         tindex = np.searchsorted(self.Tvals, kT)-1
+        if tindex >= self.Tvals.shape[0]-1 or tindex < 0:
+            return cspec_l, mspec_l
         dT = (kT-self.Tvals[tindex])/self.dTvals[tindex]
         # First do H,He, and trace elements
         for elem in self.cosmic_elem:


https://bitbucket.org/yt_analysis/yt/commits/686454e9aa94/
Changeset:   686454e9aa94
Branch:      yt
User:        jzuhone
Date:        2013-11-01 19:50:22
Summary:     Made the answer testing simpler and faster, removed the unit test because it will be a bear to run, and is really not necessary.

Added a "requires_file" function so that if auxiliary files are not present the test simply passes.
Affected #:  4 files

diff -r 752ff7b58153c3891022e28726ccd4f120e6d56d -r 686454e9aa9471d2627b7e34dd4d04e68874b4fe yt/analysis_modules/photon_simulator/tests/test_beta_model.py
--- a/yt/analysis_modules/photon_simulator/tests/test_beta_model.py
+++ /dev/null
@@ -1,129 +0,0 @@
-"""
-Unit test the photon_simulator analysis module.
-"""
-
-#-----------------------------------------------------------------------------
-# Copyright (c) 2013, yt Development Team.
-#
-# Distributed under the terms of the Modified BSD License.
-#
-# The full license is in the file COPYING.txt, distributed with this software.
-#-----------------------------------------------------------------------------
-
-from yt.frontends.stream.api import load_uniform_grid
-from yt.testing import *
-from yt.utilities.physical_constants import cm_per_kpc, \
-     K_per_keV, cm_per_mpc, mp
-from yt.utilities.cosmology import Cosmology
-from yt.analysis_modules.api import PhotonList, EventList, \
-     XSpecThermalModel, XSpecAbsorbModel, ThermalPhotonModel
-import os
-import xspec
-import gc
-
-def setup():
-    """Test specific setup."""
-    from yt.config import ytcfg
-    ytcfg["yt", "__withintesting"] = "True"
-                
- at requires_module("xspec")
-def test_beta_model():
-    # Set up the beta model and stream dataset
-    R = 1000.
-    r_c = 100.
-    rho_c = 1.673e-26
-    beta = 1.
-    T = 4.
-    nx = 256
-    nH = 0.1
-    Zmet = 0.3
-    X_H = 0.75
-    nenp0 = (rho_c/mp)**2*0.5*(1.+X_H)*X_H
-
-    ddims = (nx,nx,nx)
-    
-    x, y, z = np.mgrid[-R:R:nx*1j,
-                       -R:R:nx*1j,
-                       -R:R:nx*1j]
-    
-    r = np.sqrt(x**2+y**2+z**2)
-
-    dens = np.zeros(ddims)
-    dens[r <= R] = rho_c*(1.+(r[r <= R]/r_c)**2)**(-1.5*beta)
-    dens[r > R] = 0.0
-    temp = T*K_per_keV*np.ones(ddims)
-
-    data = {}
-    data["Density"] = dens
-    data["Temperature"] = temp
-    data["x-velocity"] = np.zeros(ddims)
-    data["y-velocity"] = np.zeros(ddims)
-    data["z-velocity"] = np.zeros(ddims)
-
-    bbox = np.array([[-0.5,0.5],[-0.5,0.5],[-0.5,0.5]])
-    
-    pf = load_uniform_grid(data, ddims, 2*R*cm_per_kpc, bbox=bbox)
-
-    # Grab a sphere data object
-    
-    sphere = pf.h.sphere(pf.domain_center, 1.0/pf["mpc"])
-
-    # Create the photons
-
-    # XSPEC is buggy so for this test we have to do it this way
-    
-    ARF = "chandra_ACIS-S3_onaxis_arf.fits"
-    RMF = "chandra_ACIS-S3_onaxis_rmf.fits"
-            
-    os.system("cp "+ os.environ["YT_DATA_DIR"]+"xray_data/"+ARF+" "+os.getcwd())
-    os.system("cp "+ os.environ["YT_DATA_DIR"]+"xray_data/"+RMF+" "+os.getcwd())
-    
-    A = 6000.
-    exp_time = 1.0e5
-    redshift = 0.05
-    cosmo = Cosmology()
-    DA = cosmo.AngularDiameterDistance(0.0,redshift)*cm_per_mpc
-    EM = 4.*np.pi*nenp0*0.196022123*(r_c*cm_per_kpc)**3
-    norm = 1.0e-14*EM/(4.*np.pi*DA**2*(1.+redshift)**2)
-
-    apec_model = XSpecThermalModel("apec", 0.01, 20.0, 10000)
-    abs_model  = XSpecAbsorbModel("TBabs", nH)
-        
-    thermal_model = ThermalPhotonModel(apec_model, Zmet=Zmet)
-    photons = PhotonList.from_scratch(sphere, redshift, A, exp_time,
-                                      thermal_model, cosmology=cosmo)
-    
-    events = photons.project_photons([0.0,0.0,1.0],
-                                     responses=[ARF,RMF],
-                                     absorb_model=abs_model)
-    events.write_spectrum("spec_chandra.fits", clobber=True)
-
-    del photons, events
-
-    gc.collect()
-    
-    # Now fit the resulting spectrum
-
-    spec = xspec.Spectrum("spec_chandra.fits")
-    xspec.Fit.statMethod = "cstat"
-    
-    spec.ignore("**-0.5")
-    spec.ignore("7.0-**")
-    
-    m = xspec.Model("tbabs*apec")
-    m.TBabs.nH = 0.09
-    m.apec.kT = 5.
-    m.apec.Abundanc = 0.2
-    m.apec.Abundanc.frozen = False
-    m.apec.Redshift = redshift
-    
-    xspec.Fit.renorm()
-    xspec.Fit.perform()
-    xspec.Fit.error("1-3,5")
-    
-    assert(T > m.apec.kT.error[0] and T < m.apec.kT.error[1])
-    assert(Zmet > m.apec.Abundanc.error[0] and Zmet < m.apec.Abundanc.error[1])
-    assert(norm > m.apec.norm.error[0] and norm < m.apec.norm.error[1])
-
-if __name__ == "__main__":
-    test_beta_model()

diff -r 752ff7b58153c3891022e28726ccd4f120e6d56d -r 686454e9aa9471d2627b7e34dd4d04e68874b4fe yt/analysis_modules/photon_simulator/tests/test_cluster.py
--- a/yt/analysis_modules/photon_simulator/tests/test_cluster.py
+++ b/yt/analysis_modules/photon_simulator/tests/test_cluster.py
@@ -17,49 +17,46 @@
 import numpy as np
 
 def setup():
-    """Test specific setup."""
     from yt.config import ytcfg
     ytcfg["yt", "__withintesting"] = "True"
 
-MHD = "MHDSloshing/virgo_low_res.0054.vtk"
- at requires_module("xspec")
- at requires_pf(MHD)
-def test_cluster():
+ETC = "enzo_tiny_cosmology/DD0046/DD0046"
+APEC = os.environ["YT_DATA_DIR"]+"/xray_data/atomdb_v2.0.2"
+TBABS = os.environ["YT_DATA_DIR"]+"/xray_data/tbabs_table.h5"
+ARF = os.environ["YT_DATA_DIR"]+"/xray_data/chandra_ACIS-S3_onaxis_arf.fits"
+RMF = os.environ["YT_DATA_DIR"]+"/xray_data/chandra_ACIS-S3_onaxis_rmf.fits"
+        
+ at requires_pf(ETC)
+ at requires_file(APEC)
+ at requires_file(TBABS)
+ at requires_file(ARF)
+ at requires_file(RMF)
+def test_etc():
+
     np.random.seed(seed=0x4d3d3d3)
-    pf = data_dir_load(MHD, parameters={"TimeUnits":3.1557e13,
-                                        "LengthUnits":3.0856e24,
-                                        "DensityUnits":6.770424595218825e-27})
 
+    pf = data_dir_load(ETC)
     A = 3000.
-    exp_time = 3.0e5
-    redshift = 0.02
+    exp_time = 1.0e5
+    redshift = 0.1
     
-    apec_model = XSpecThermalModel("apec", 0.1, 20.0, 2000)
-    tbabs_model = XSpecAbsorbModel("TBabs", 0.1)
+    apec_model = TableApecModel(APEC, 0.1, 20.0, 2000)
+    tbabs_model = TableAbsorbModel(TBABS, 0.1)
 
-    ARF = os.environ["YT_DATA_DIR"]+"/xray_data/chandra_ACIS-S3_onaxis_arf.fits"
-    RMF = os.environ["YT_DATA_DIR"]+"/xray_data/chandra_ACIS-S3_onaxis_rmf.fits"
-            
-    sphere = pf.h.sphere("c", (0.25, "mpc"))
+    sphere = pf.h.sphere("max", (0.5, "mpc"))
 
     thermal_model = ThermalPhotonModel(apec_model, Zmet=0.3)
     photons = PhotonList.from_scratch(sphere, redshift, A, exp_time,
-                                      thermal_model, cosmology=cosmo)
+                                      thermal_model)
     
     events = photons.project_photons([0.0,0.0,1.0],
                                      responses=[ARF,RMF],
-                                     absorb_model=abs_model)
-    
-    for k,v in photons.items():
-        def photons_test(v): return v
-        test = GenericArrayTest(pf, photons_test)
-        test_cluster.__name__ = test.description
+                                     absorb_model=tbabs_model)
+
+    def photons_test(): return photons.photons
+    def events_test(): return events.events
+
+    for test in [GenericArrayTest(pf, photons_test),
+                 GenericArrayTest(pf, events_test)]:
+        test_etc.__name__ = test.description
         yield test
-
-    for k,v in events.items():
-        def events_test(v): return v
-        test = GenericArrayTest(pf, events_test)
-        test_cluster.__name__ = test.description
-        yield test
-            
-            

diff -r 752ff7b58153c3891022e28726ccd4f120e6d56d -r 686454e9aa9471d2627b7e34dd4d04e68874b4fe yt/testing.py
--- a/yt/testing.py
+++ b/yt/testing.py
@@ -15,6 +15,7 @@
 import itertools as it
 import numpy as np
 import importlib
+import os
 from yt.funcs import *
 from numpy.testing import assert_array_equal, assert_almost_equal, \
     assert_approx_equal, assert_array_almost_equal, assert_equal, \
@@ -272,3 +273,14 @@
     else:
         return ftrue
     
+
+def requires_file(req_file):
+    def ffalse(func):
+        return lambda: None
+    def ftrue(func):
+        return func
+    if os.path.exists(req_file):
+        return ftrue
+    else:
+        return ffalse
+                                        

diff -r 752ff7b58153c3891022e28726ccd4f120e6d56d -r 686454e9aa9471d2627b7e34dd4d04e68874b4fe yt/utilities/answer_testing/framework.py
--- a/yt/utilities/answer_testing/framework.py
+++ b/yt/utilities/answer_testing/framework.py
@@ -266,13 +266,13 @@
             return False
     return AnswerTestingTest.result_storage is not None
 
-def data_dir_load(pf_fn, **kwargs):
+def data_dir_load(pf_fn):
     path = ytcfg.get("yt", "test_data_dir")
     if isinstance(pf_fn, StaticOutput): return pf_fn
     if not os.path.isdir(path):
         return False
     with temp_cwd(path):
-        pf = load(pf_fn, kwargs)
+        pf = load(pf_fn)
         pf.h
         return pf
 


https://bitbucket.org/yt_analysis/yt/commits/eb969b869c15/
Changeset:   eb969b869c15
Branch:      yt
User:        jzuhone
Date:        2013-11-01 22:41:07
Summary:     Updated answer tests to use test_data_dir from the configuration
Affected #:  3 files

diff -r 686454e9aa9471d2627b7e34dd4d04e68874b4fe -r eb969b869c157ba8f9d5496efaf6db0df2a2e727 yt/analysis_modules/photon_simulator/tests/test_cluster.py
--- a/yt/analysis_modules/photon_simulator/tests/test_cluster.py
+++ b/yt/analysis_modules/photon_simulator/tests/test_cluster.py
@@ -11,6 +11,7 @@
 #-----------------------------------------------------------------------------
 
 from yt.testing import *
+from yt.config import ytcfg
 from yt.analysis_modules.photon_simulator.api import *
 from yt.utilities.answer_testing.framework import requires_pf, \
      GenericArrayTest, data_dir_load
@@ -20,12 +21,14 @@
     from yt.config import ytcfg
     ytcfg["yt", "__withintesting"] = "True"
 
-ETC = "enzo_tiny_cosmology/DD0046/DD0046"
-APEC = os.environ["YT_DATA_DIR"]+"/xray_data/atomdb_v2.0.2"
-TBABS = os.environ["YT_DATA_DIR"]+"/xray_data/tbabs_table.h5"
-ARF = os.environ["YT_DATA_DIR"]+"/xray_data/chandra_ACIS-S3_onaxis_arf.fits"
-RMF = os.environ["YT_DATA_DIR"]+"/xray_data/chandra_ACIS-S3_onaxis_rmf.fits"
-        
+test_dir = ytcfg.get("yt", "test_data_dir")
+
+ETC = test_dir+"/enzo_tiny_cosmology/DD0046/DD0046"
+APEC = test_dir+"/xray_data/atomdb_v2.0.2"
+TBABS = test_dir+"/xray_data/tbabs_table.h5"
+ARF = test_dir+"/xray_data/chandra_ACIS-S3_onaxis_arf.fits"
+RMF = test_dir+"/xray_data/chandra_ACIS-S3_onaxis_rmf.fits"
+
 @requires_pf(ETC)
 @requires_file(APEC)
 @requires_file(TBABS)

diff -r 686454e9aa9471d2627b7e34dd4d04e68874b4fe -r eb969b869c157ba8f9d5496efaf6db0df2a2e727 yt/testing.py
--- a/yt/testing.py
+++ b/yt/testing.py
@@ -17,6 +17,7 @@
 import importlib
 import os
 from yt.funcs import *
+from yt.config import ytcfg
 from numpy.testing import assert_array_equal, assert_almost_equal, \
     assert_approx_equal, assert_array_almost_equal, assert_equal, \
     assert_array_less, assert_string_equal, assert_array_almost_equal_nulp,\
@@ -273,8 +274,8 @@
     else:
         return ftrue
     
-
 def requires_file(req_file):
+    path = ytcfg.get("yt", "test_data_dir")
     def ffalse(func):
         return lambda: None
     def ftrue(func):
@@ -282,5 +283,8 @@
     if os.path.exists(req_file):
         return ftrue
     else:
-        return ffalse
+        if os.path.exists(os.path.join(path,req_file)):
+            return ftrue
+        else:
+            return ffalse
                                         

diff -r 686454e9aa9471d2627b7e34dd4d04e68874b4fe -r eb969b869c157ba8f9d5496efaf6db0df2a2e727 yt/visualization/fixed_resolution.py
--- a/yt/visualization/fixed_resolution.py
+++ b/yt/visualization/fixed_resolution.py
@@ -300,8 +300,6 @@
             mylog.error("You don't have AstroPy installed!")
             raise ImportError
         
-        from os import system
-
         if units == "deg" and D_A is None:
             mylog.error("Sky coordinates require an angular diameter distance. Please specify D_A.")    
             raise ValueError


https://bitbucket.org/yt_analysis/yt/commits/718059a11627/
Changeset:   718059a11627
Branch:      yt
User:        jzuhone
Date:        2013-11-05 15:17:55
Summary:     Grabbing cosmology from the parameter file if it has it.
Affected #:  1 file

diff -r eb969b869c157ba8f9d5496efaf6db0df2a2e727 -r 718059a11627949ecdbafbefed67d699c9a04e66 yt/analysis_modules/photon_simulator/photon_simulator.py
--- a/yt/analysis_modules/photon_simulator/photon_simulator.py
+++ b/yt/analysis_modules/photon_simulator/photon_simulator.py
@@ -167,8 +167,9 @@
             mainly for nearby sources. This may be optionally supplied
             instead of it being determined from the *redshift* and given *cosmology*.
         cosmology : `yt.utilities.cosmology.Cosmology`, optional
-            Cosmological information. If not supplied, it assumes \LambdaCDM with
-            the default yt parameters.
+            Cosmological information. If not supplied, we try to get
+            the cosmology from the parameter file. Otherwise, \LambdaCDM with
+            the default yt parameters is assumed.
 
         Examples
         --------
@@ -246,9 +247,21 @@
         if parameters is None:
              parameters = {}
         if cosmology is None:
-            cosmo = Cosmology()
+            hubble = getattr(pf, "hubble_constant", None)
+            omega_m = getattr(pf, "omega_matter", None)
+            omega_l = getattr(pf, "omega_lambda", None)
+            if hubble is not None and \
+               omega_m is not None and \
+               omega_l is not None:
+                cosmo = Cosmology(HubbleConstantNow=100.*hubble,
+                                  OmegaMatterNow=omega_m,
+                                  OmegaLambdaNow=omega_l)
+            else:
+                cosmo = Cosmology()
         else:
             cosmo = cosmology
+        mylog.info("Cosmology: H0 = %g, omega_matter = %g, omega_lambda = %g" %
+                   (cosmo.HubbleConstantNow, cosmo.OmegaMatterNow, cosmo.OmegaLambdaNow))
         if dist is None:
             D_A = cosmo.AngularDiameterDistance(0.0,redshift)
         else:


https://bitbucket.org/yt_analysis/yt/commits/58580d4b9d81/
Changeset:   58580d4b9d81
Branch:      yt
User:        MatthewTurk
Date:        2013-11-08 17:33:35
Summary:     Merged in jzuhone/yt-xray (pull request #629)

Generating Mock X-ray Photons in yt
Affected #:  13 files

diff -r 6cf2b25b9cf21f6759a3161dff30f7cdcdcf0c91 -r 58580d4b9d81dfd7ab4f4ee4ee189ac1c055ef57 yt/analysis_modules/api.py
--- a/yt/analysis_modules/api.py
+++ b/yt/analysis_modules/api.py
@@ -110,3 +110,14 @@
 
 from .particle_trajectories.api import \
     ParticleTrajectories
+
+from .photon_simulator.api import \
+     PhotonList, \
+     EventList, \
+     SpectralModel, \
+     XSpecThermalModel, \
+     XSpecAbsorbModel, \
+     TableApecModel, \
+     TableAbsorbModel, \
+     PhotonModel, \
+     ThermalPhotonModel

diff -r 6cf2b25b9cf21f6759a3161dff30f7cdcdcf0c91 -r 58580d4b9d81dfd7ab4f4ee4ee189ac1c055ef57 yt/analysis_modules/photon_simulator/api.py
--- /dev/null
+++ b/yt/analysis_modules/photon_simulator/api.py
@@ -0,0 +1,26 @@
+"""
+API for yt.analysis_modules.photon_simulator.
+"""
+
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
+from .photon_models import \
+     PhotonModel, \
+     ThermalPhotonModel
+
+from .photon_simulator import \
+     PhotonList, \
+     EventList
+
+from .spectral_models import \
+     SpectralModel, \
+     XSpecThermalModel, \
+     XSpecAbsorbModel, \
+     TableApecModel, \
+     TableAbsorbModel

diff -r 6cf2b25b9cf21f6759a3161dff30f7cdcdcf0c91 -r 58580d4b9d81dfd7ab4f4ee4ee189ac1c055ef57 yt/analysis_modules/photon_simulator/photon_models.py
--- /dev/null
+++ b/yt/analysis_modules/photon_simulator/photon_models.py
@@ -0,0 +1,205 @@
+"""
+Classes for specific photon models
+
+The algorithms used here are based off of the method used by the
+PHOX code (http://www.mpa-garching.mpg.de/~kdolag/Phox/),
+developed by Veronica Biffi and Klaus Dolag. References for
+PHOX may be found at:
+
+Biffi, V., Dolag, K., Bohringer, H., & Lemson, G. 2012, MNRAS, 420, 3545
+http://adsabs.harvard.edu/abs/2012MNRAS.420.3545B
+
+Biffi, V., Dolag, K., Bohringer, H. 2013, MNRAS, 428, 1395
+http://adsabs.harvard.edu/abs/2013MNRAS.428.1395B
+
+"""
+
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
+import numpy as np
+from yt.funcs import *
+from yt.utilities.physical_constants import \
+     mp, cm_per_km, K_per_keV, cm_per_mpc
+from yt.utilities.parallel_tools.parallel_analysis_interface import \
+     communication_system
+
+N_TBIN = 10000
+TMIN = 8.08e-2
+TMAX = 50.
+
+comm = communication_system.communicators[-1]
+
+class PhotonModel(object):
+
+    def __init__(self):
+        pass
+
+    def __call__(self, data_source, parameters):
+        photons = {}
+        return photons
+
+class ThermalPhotonModel(PhotonModel):
+    r"""
+    Initialize a ThermalPhotonModel from a thermal spectrum. 
+    
+    Parameters
+    ----------
+
+    spectral_model : `SpectralModel`
+        A thermal spectral model instance, either of `XSpecThermalModel`
+        or `TableApecModel`. 
+    X_H : float, optional
+        The hydrogen mass fraction.
+    Zmet : float or string, optional
+        The metallicity. If a float, assumes a constant metallicity throughout.
+        If a string, is taken to be the name of the metallicity field.
+    """
+    def __init__(self, spectral_model, X_H=0.75, Zmet=0.3):
+        self.X_H = X_H
+        self.Zmet = Zmet
+        self.spectral_model = spectral_model
+
+    def __call__(self, data_source, parameters):
+        
+        pf = data_source.pf
+
+        exp_time = parameters["FiducialExposureTime"]
+        area = parameters["FiducialArea"]
+        redshift = parameters["FiducialRedshift"]
+        D_A = parameters["FiducialAngularDiameterDistance"]*cm_per_mpc
+        dist_fac = 1.0/(4.*np.pi*D_A*D_A*(1.+redshift)**3)
+                
+        vol_scale = pf.units["cm"]**(-3)/np.prod(pf.domain_width)
+        
+        num_cells = data_source["Temperature"].shape[0]
+        start_c = comm.rank*num_cells/comm.size
+        end_c = (comm.rank+1)*num_cells/comm.size
+        
+        kT = data_source["Temperature"][start_c:end_c].copy()/K_per_keV
+        vol = data_source["CellVolume"][start_c:end_c].copy()
+        dx = data_source["dx"][start_c:end_c].copy()
+        EM = (data_source["Density"][start_c:end_c].copy()/mp)**2
+        EM *= 0.5*(1.+self.X_H)*self.X_H*vol
+    
+        data_source.clear_data()
+    
+        x = data_source["x"][start_c:end_c].copy()
+        y = data_source["y"][start_c:end_c].copy()
+        z = data_source["z"][start_c:end_c].copy()
+    
+        data_source.clear_data()
+        
+        vx = data_source["x-velocity"][start_c:end_c].copy()
+        vy = data_source["y-velocity"][start_c:end_c].copy()
+        vz = data_source["z-velocity"][start_c:end_c].copy()
+    
+        if isinstance(self.Zmet, basestring):
+            metalZ = data_source[self.Zmet][start_c:end_c].copy()
+        else:
+            metalZ = self.Zmet*np.ones(EM.shape)
+        
+        data_source.clear_data()
+
+        idxs = np.argsort(kT)
+        dshape = idxs.shape
+
+        kT_bins = np.linspace(TMIN, max(kT[idxs][-1], TMAX), num=N_TBIN+1)
+        dkT = kT_bins[1]-kT_bins[0]
+        kT_idxs = np.digitize(kT[idxs], kT_bins)
+        kT_idxs = np.minimum(np.maximum(1, kT_idxs), N_TBIN) - 1
+        bcounts = np.bincount(kT_idxs).astype("int")
+        bcounts = bcounts[bcounts > 0]
+        n = int(0)
+        bcell = []
+        ecell = []
+        for bcount in bcounts:
+            bcell.append(n)
+            ecell.append(n+bcount)
+            n += bcount
+        kT_idxs = np.unique(kT_idxs)
+        
+        self.spectral_model.prepare()
+        energy = self.spectral_model.ebins
+    
+        cell_em = EM[idxs]*vol_scale
+        cell_vol = vol[idxs]*vol_scale
+    
+        number_of_photons = np.zeros(dshape, dtype='uint64')
+        energies = []
+    
+        u = np.random.random(cell_em.shape)
+        
+        pbar = get_pbar("Generating Photons", dshape[0])
+
+        for i, ikT in enumerate(kT_idxs):
+
+            ncells = int(bcounts[i])
+            ibegin = bcell[i]
+            iend = ecell[i]
+            kT = kT_bins[ikT] + 0.5*dkT
+        
+            em_sum_c = cell_em[ibegin:iend].sum()
+            em_sum_m = (metalZ[ibegin:iend]*cell_em[ibegin:iend]).sum()
+            
+            cspec, mspec = self.spectral_model.get_spectrum(kT)
+            cspec *= dist_fac*em_sum_c/vol_scale
+            mspec *= dist_fac*em_sum_m/vol_scale
+        
+            cumspec_c = np.cumsum(cspec)
+            counts_c = cumspec_c[:]/cumspec_c[-1]
+            counts_c = np.insert(counts_c, 0, 0.0)
+            tot_ph_c = cumspec_c[-1]*area*exp_time
+
+            cumspec_m = np.cumsum(mspec)
+            counts_m = cumspec_m[:]/cumspec_m[-1]
+            counts_m = np.insert(counts_m, 0, 0.0)
+            tot_ph_m = cumspec_m[-1]*area*exp_time
+        
+            for icell in xrange(ibegin, iend):
+            
+                cell_norm_c = tot_ph_c*cell_em[icell]/em_sum_c
+                cell_n_c = np.uint64(cell_norm_c) + np.uint64(np.modf(cell_norm_c)[0] >= u[icell])
+            
+                cell_norm_m = tot_ph_m*metalZ[icell]*cell_em[icell]/em_sum_m
+                cell_n_m = np.uint64(cell_norm_m) + np.uint64(np.modf(cell_norm_m)[0] >= u[icell])
+            
+                cell_n = cell_n_c + cell_n_m
+
+                if cell_n > 0:
+                    number_of_photons[icell] = cell_n
+                    randvec_c = np.random.uniform(size=cell_n_c)
+                    randvec_c.sort()
+                    randvec_m = np.random.uniform(size=cell_n_m)
+                    randvec_m.sort()
+                    cell_e_c = np.interp(randvec_c, counts_c, energy)
+                    cell_e_m = np.interp(randvec_m, counts_m, energy)
+                    energies.append(np.concatenate([cell_e_c,cell_e_m]))
+                
+                pbar.update(icell)
+
+        pbar.finish()
+            
+        active_cells = number_of_photons > 0
+        idxs = idxs[active_cells]
+        
+        photons = {}
+
+        src_ctr = parameters["center"]
+        
+        photons["x"] = (x[idxs]-src_ctr[0])*pf.units["kpc"]
+        photons["y"] = (y[idxs]-src_ctr[1])*pf.units["kpc"]
+        photons["z"] = (z[idxs]-src_ctr[2])*pf.units["kpc"]
+        photons["vx"] = vx[idxs]/cm_per_km
+        photons["vy"] = vy[idxs]/cm_per_km
+        photons["vz"] = vz[idxs]/cm_per_km
+        photons["dx"] = dx[idxs]*pf.units["kpc"]
+        photons["NumberOfPhotons"] = number_of_photons[active_cells]
+        photons["Energy"] = np.concatenate(energies)
+    
+        return photons

diff -r 6cf2b25b9cf21f6759a3161dff30f7cdcdcf0c91 -r 58580d4b9d81dfd7ab4f4ee4ee189ac1c055ef57 yt/analysis_modules/photon_simulator/photon_simulator.py
--- /dev/null
+++ b/yt/analysis_modules/photon_simulator/photon_simulator.py
@@ -0,0 +1,1203 @@
+"""
+Classes for generating lists of photons and detected events
+The algorithms used here are based off of the method used by the
+PHOX code (http://www.mpa-garching.mpg.de/~kdolag/Phox/),
+developed by Veronica Biffi and Klaus Dolag. References for
+PHOX may be found at:
+
+Biffi, V., Dolag, K., Bohringer, H., & Lemson, G. 2012, MNRAS, 420, 3545
+http://adsabs.harvard.edu/abs/2012MNRAS.420.3545B
+
+Biffi, V., Dolag, K., Bohringer, H. 2013, MNRAS, 428, 1395
+http://adsabs.harvard.edu/abs/2013MNRAS.428.1395B
+"""
+
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
+import numpy as np
+from numpy.testing import assert_allclose
+from yt.funcs import *
+from yt.utilities.physical_constants import clight, \
+     cm_per_km, erg_per_keV
+from yt.utilities.cosmology import Cosmology
+from yt.utilities.orientation import Orientation
+from yt.utilities.parallel_tools.parallel_analysis_interface import \
+     communication_system, parallel_root_only, get_mpi_type, \
+     op_names, parallel_capable
+
+import h5py
+
+try:
+    import astropy.io.fits as pyfits
+    import astropy.wcs as pywcs
+except ImportError:
+    pass
+
+comm = communication_system.communicators[-1]
+
+class PhotonList(object):
+
+    def __init__(self, photons, parameters, cosmo, p_bins):
+        self.photons = photons
+        self.parameters = parameters
+        self.cosmo = cosmo
+        self.p_bins = p_bins
+        self.num_cells = len(photons["x"])
+        
+    def keys(self):
+        return self.photons.keys()
+    
+    def items(self):
+        ret = []
+        for k, v in self.photons.items():
+            if k == "Energy":
+                ret.append((k, self[k]))
+            else:
+                ret.append((k,v))
+        return ret
+    
+    def values(self):
+        ret = []
+        for k, v in self.photons.items():
+            if k == "Energy":
+                ret.append(self[k])
+            else:
+                ret.append(v)
+        return ret
+                                
+    def __getitem__(self, key):
+        if key == "Energy":
+            return [self.photons["Energy"][self.p_bins[i]:self.p_bins[i+1]]
+                    for i in xrange(self.num_cells)]
+        else:
+            return self.photons[key]
+    
+    @classmethod
+    def from_file(cls, filename):
+        r"""
+        Initialize a PhotonList from the HDF5 file *filename*.
+        """
+
+        photons = {}
+        parameters = {}
+        
+        f = h5py.File(filename, "r")
+
+        parameters["FiducialExposureTime"] = f["/fid_exp_time"].value
+        parameters["FiducialArea"] = f["/fid_area"].value
+        parameters["FiducialRedshift"] = f["/fid_redshift"].value
+        parameters["FiducialAngularDiameterDistance"] = f["/fid_d_a"].value
+        parameters["Dimension"] = f["/dimension"].value
+        parameters["Width"] = f["/width"].value
+        parameters["HubbleConstant"] = f["/hubble"].value
+        parameters["OmegaMatter"] = f["/omega_matter"].value
+        parameters["OmegaLambda"] = f["/omega_lambda"].value
+
+        num_cells = f["/x"][:].shape[0]
+        start_c = comm.rank*num_cells/comm.size
+        end_c = (comm.rank+1)*num_cells/comm.size
+        
+        photons["x"] = f["/x"][start_c:end_c]
+        photons["y"] = f["/y"][start_c:end_c]
+        photons["z"] = f["/z"][start_c:end_c]
+        photons["dx"] = f["/dx"][start_c:end_c]
+        photons["vx"] = f["/vx"][start_c:end_c]
+        photons["vy"] = f["/vy"][start_c:end_c]
+        photons["vz"] = f["/vz"][start_c:end_c]
+
+        n_ph = f["/num_photons"][:]
+        
+        if comm.rank == 0:
+            start_e = np.uint64(0)
+        else:
+            start_e = n_ph[:start_c].sum()
+        end_e = start_e + np.uint64(n_ph[start_c:end_c].sum())
+
+        photons["NumberOfPhotons"] = n_ph[start_c:end_c]
+
+        p_bins = np.cumsum(photons["NumberOfPhotons"])
+        p_bins = np.insert(p_bins, 0, [np.uint64(0)])
+        
+        photons["Energy"] = f["/energy"][start_e:end_e]
+        
+        f.close()
+
+        cosmo = Cosmology(HubbleConstantNow=parameters["HubbleConstant"],
+                          OmegaMatterNow=parameters["OmegaMatter"],
+                          OmegaLambdaNow=parameters["OmegaLambda"])
+
+        return cls(photons, parameters, cosmo, p_bins)
+    
+    @classmethod
+    def from_scratch(cls, data_source, redshift, area,
+                     exp_time, photon_model, parameters=None,
+                     center=None, dist=None, cosmology=None):
+        r"""
+        Initialize a PhotonList from a photon model. The redshift, collecting area,
+        exposure time, and cosmology are stored in the *parameters* dictionary which
+        is passed to the *photon_model* function. 
+
+        Parameters
+        ----------
+
+        data_source : `yt.data_objects.api.AMRData`
+            The data source from which the photons will be generated.
+        redshift : float
+            The cosmological redshift for the photons.
+        area : float
+            The collecting area to determine the number of photons in cm^2.
+        exp_time : float
+            The exposure time to determine the number of photons in seconds.
+        photon_model : function
+            A function that takes the *data_source* and the *parameters*
+            dictionary and returns a *photons* dictionary. Must be of the
+            form: photon_model(data_source, parameters)
+        parameters : dict, optional
+            A dictionary of parameters to be passed to the user function. 
+        center : string or array_like, optional
+            The origin of the photons. Accepts "c", "max", or a coordinate.                
+        dist : tuple, optional
+            The angular diameter distance in the form (value, unit), used
+            mainly for nearby sources. This may be optionally supplied
+            instead of it being determined from the *redshift* and given *cosmology*.
+        cosmology : `yt.utilities.cosmology.Cosmology`, optional
+            Cosmological information. If not supplied, we try to get
+            the cosmology from the parameter file. Otherwise, \LambdaCDM with
+            the default yt parameters is assumed.
+
+        Examples
+        --------
+
+        This is the simplest possible example, where we call the built-in thermal model:
+
+        >>> thermal_model = ThermalPhotonModel(apec_model, Zmet=0.3)
+        >>> redshift = 0.05
+        >>> area = 6000.0
+        >>> time = 2.0e5
+        >>> sp = pf.h.sphere("c", (500., "kpc"))
+        >>> my_photons = PhotonList.from_user_model(sp, redshift, area,
+        ...                                         time, thermal_model)
+
+        If you wish to make your own photon model function, it must take as its
+        arguments the *data_source* and the *parameters* dictionary. However you
+        determine them, the *photons* dict needs to have the following items, corresponding
+        to cells which have photons:
+
+        "x" : the x-position of the cell relative to the source center in kpc, NumPy array of floats
+        "y" : the y-position of the cell relative to the source center in kpc, NumPy array of floats
+        "z" : the z-position of the cell relative to the source center in kpc, NumPy array of floats
+        "vx" : the x-velocity of the cell in km/s, NumPy array of floats
+        "vy" : the y-velocity of the cell in km/s, NumPy array of floats
+        "vz" : the z-velocity of the cell in km/s, NumPy array of floats
+        "dx" : the width of the cell in kpc, NumPy array of floats
+        "NumberOfPhotons" : the number of photons in the cell, NumPy array of integers
+        "Energy" : the source rest-frame energies of the photons, NumPy array of floats
+
+        The last array is not the same size as the others because it contains the energies in all of
+        the cells in a single 1-D array. The first photons["NumberOfPhotons"][0] elements are
+        for the first cell, the next photons["NumberOfPhotons"][1] are for the second cell, and so on.
+
+        The following is a simple example where a point source with a single line emission
+        spectrum of photons is created. More complicated examples which actually
+        create photons based on the fields in the dataset could be created. 
+
+        >>> from scipy.stats import powerlaw
+        >>> def line_func(source, parameters):
+        ...
+        ...     pf = source.pf
+        ... 
+        ...     num_photons = parameters["num_photons"]
+        ...     E0  = parameters["line_energy"] # Energies are in keV
+        ...     sigE = parameters["line_sigma"] 
+        ...
+        ...     energies = norm.rvs(loc=E0, scale=sigE, size=num_photons)
+        ...     
+        ...     photons["x"] = np.zeros((1)) # Place everything in the center cell
+        ...     photons["y"] = np.zeros((1))
+        ...     photons["z"] = np.zeros((1))
+        ...     photons["vx"] = np.zeros((1))
+        ...     photons["vy"] = np.zeros((1))
+        ...     photons["vz"] = 100.*np.ones((1))
+        ...     photons["dx"] = source["dx"][0]*pf.units["kpc"]*np.ones((1)) 
+        ...     photons["NumberOfPhotons"] = num_photons*np.ones((1))
+        ...     photons["Energy"] = np.array(energies)
+        >>>
+        >>> redshift = 0.05
+        >>> area = 6000.0
+        >>> time = 2.0e5
+        >>> parameters = {"num_photons" : 10000, "line_energy" : 5.0,
+        ...               "line_sigma" : 0.1}
+        >>> ddims = (128,128,128)
+        >>> random_data = {"Density":np.random.random(ddims)}
+        >>> pf = load_uniform_grid(random_data, ddims)
+        >>> dd = pf.h.all_data
+        >>> my_photons = PhotonList.from_user_model(dd, redshift, area,
+        ...                                         time, line_func)
+
+        """
+
+        pf = data_source.pf
+
+        if parameters is None:
+             parameters = {}
+        if cosmology is None:
+            hubble = getattr(pf, "hubble_constant", None)
+            omega_m = getattr(pf, "omega_matter", None)
+            omega_l = getattr(pf, "omega_lambda", None)
+            if hubble is not None and \
+               omega_m is not None and \
+               omega_l is not None:
+                cosmo = Cosmology(HubbleConstantNow=100.*hubble,
+                                  OmegaMatterNow=omega_m,
+                                  OmegaLambdaNow=omega_l)
+            else:
+                cosmo = Cosmology()
+        else:
+            cosmo = cosmology
+        mylog.info("Cosmology: H0 = %g, omega_matter = %g, omega_lambda = %g" %
+                   (cosmo.HubbleConstantNow, cosmo.OmegaMatterNow, cosmo.OmegaLambdaNow))
+        if dist is None:
+            D_A = cosmo.AngularDiameterDistance(0.0,redshift)
+        else:
+            D_A = dist[0]*pf.units["mpc"]/pf.units[dist[1]]
+            redshift = 0.0
+
+        if center == "c":
+            parameters["center"] = pf.domain_center
+        elif center == "max":
+            parameters["center"] = pf.h.find_max("Density")[-1]
+        elif iterable(center):
+            parameters["center"] = center
+        elif center is None:
+            parameters["center"] = data_source.get_field_parameter("center")
+            
+        parameters["FiducialExposureTime"] = exp_time
+        parameters["FiducialArea"] = area
+        parameters["FiducialRedshift"] = redshift
+        parameters["FiducialAngularDiameterDistance"] = D_A
+        parameters["HubbleConstant"] = cosmo.HubbleConstantNow
+        parameters["OmegaMatter"] = cosmo.OmegaMatterNow
+        parameters["OmegaLambda"] = cosmo.OmegaLambdaNow
+
+        dimension = 0
+        width = 0.0
+        for i, ax in enumerate("xyz"):
+            pos = data_source[ax]
+            delta = data_source["d%s"%(ax)]
+            le = np.min(pos-0.5*delta)
+            re = np.max(pos+0.5*delta)
+            width = max(width, re-parameters["center"][i], parameters["center"][i]-le)
+            dimension = max(dimension, int(width/delta.min()))
+        parameters["Dimension"] = 2*dimension
+        parameters["Width"] = 2.*width*pf.units["kpc"]
+                
+        photons = photon_model(data_source, parameters)
+        
+        p_bins = np.cumsum(photons["NumberOfPhotons"])
+        p_bins = np.insert(p_bins, 0, [np.uint64(0)])
+                        
+        return cls(photons, parameters, cosmo, p_bins)
+        
+    def write_h5_file(self, photonfile):
+        """
+        Write the photons to the HDF5 file *photonfile*.
+        """
+        if parallel_capable:
+            
+            mpi_long = get_mpi_type("int64")
+            mpi_double = get_mpi_type("float64")
+        
+            local_num_cells = len(self.photons["x"])
+            sizes_c = comm.comm.gather(local_num_cells, root=0)
+            
+            local_num_photons = self.photons["NumberOfPhotons"].sum()
+            sizes_p = comm.comm.gather(local_num_photons, root=0)
+            
+            if comm.rank == 0:
+                num_cells = sum(sizes_c)
+                num_photons = sum(sizes_p)        
+                disps_c = [sum(sizes_c[:i]) for i in range(len(sizes_c))]
+                disps_p = [sum(sizes_p[:i]) for i in range(len(sizes_p))]
+                x = np.zeros((num_cells))
+                y = np.zeros((num_cells))
+                z = np.zeros((num_cells))
+                vx = np.zeros((num_cells))
+                vy = np.zeros((num_cells))
+                vz = np.zeros((num_cells))
+                dx = np.zeros((num_cells))
+                n_ph = np.zeros((num_cells), dtype="uint64")
+                e = np.zeros((num_photons))
+            else:
+                sizes_c = []
+                sizes_p = []
+                disps_c = []
+                disps_p = []
+                x = np.empty([])
+                y = np.empty([])
+                z = np.empty([])
+                vx = np.empty([])
+                vy = np.empty([])
+                vz = np.empty([])
+                dx = np.empty([])
+                n_ph = np.empty([])
+                e = np.empty([])
+                                                
+            comm.comm.Gatherv([self.photons["x"], local_num_cells, mpi_double],
+                              [x, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["y"], local_num_cells, mpi_double],
+                              [y, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["z"], local_num_cells, mpi_double],
+                              [z, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["vx"], local_num_cells, mpi_double],
+                              [vx, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["vy"], local_num_cells, mpi_double],
+                              [vy, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["vz"], local_num_cells, mpi_double],
+                              [vz, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["dx"], local_num_cells, mpi_double],
+                              [dx, (sizes_c, disps_c), mpi_double], root=0)
+            comm.comm.Gatherv([self.photons["NumberOfPhotons"], local_num_cells, mpi_long],
+                              [n_ph, (sizes_c, disps_c), mpi_long], root=0)
+            comm.comm.Gatherv([self.photons["Energy"], local_num_photons, mpi_double],
+                              [e, (sizes_p, disps_p), mpi_double], root=0) 
+
+        else:
+
+            x = self.photons["x"]
+            y = self.photons["y"]
+            z = self.photons["z"]
+            vx = self.photons["vx"]
+            vy = self.photons["vy"]
+            vz = self.photons["vz"]
+            dx = self.photons["dx"]
+            n_ph = self.photons["NumberOfPhotons"]
+            e = self.photons["Energy"]
+                                                
+        if comm.rank == 0:
+            
+            f = h5py.File(photonfile, "w")
+
+            # Scalars
+       
+            f.create_dataset("fid_area", data=self.parameters["FiducialArea"])
+            f.create_dataset("fid_exp_time", data=self.parameters["FiducialExposureTime"])
+            f.create_dataset("fid_redshift", data=self.parameters["FiducialRedshift"])
+            f.create_dataset("hubble", data=self.parameters["HubbleConstant"])
+            f.create_dataset("omega_matter", data=self.parameters["OmegaMatter"])
+            f.create_dataset("omega_lambda", data=self.parameters["OmegaLambda"])
+            f.create_dataset("fid_d_a", data=self.parameters["FiducialAngularDiameterDistance"])
+            f.create_dataset("dimension", data=self.parameters["Dimension"])
+            f.create_dataset("width", data=self.parameters["Width"])
+                        
+            # Arrays
+
+            f.create_dataset("x", data=x)
+            f.create_dataset("y", data=y)
+            f.create_dataset("z", data=z)
+            f.create_dataset("vx", data=vx)
+            f.create_dataset("vy", data=vy)
+            f.create_dataset("vz", data=vz)
+            f.create_dataset("dx", data=dx)
+            f.create_dataset("num_photons", data=n_ph)
+            f.create_dataset("energy", data=e)
+
+            f.close()
+
+        comm.barrier()
+
+    def project_photons(self, L, area_new=None, exp_time_new=None, 
+                        redshift_new=None, dist_new=None,
+                        absorb_model=None, psf_sigma=None,
+                        sky_center=None, responses=None):
+        r"""
+        Projects photons onto an image plane given a line of sight.
+
+        Parameters
+        ----------
+
+        L : array_like
+            Normal vector to the plane of projection.
+        area_new : float, optional
+            New value for the effective area of the detector. If *responses*
+            are specified the value of this keyword is ignored.
+        exp_time_new : float, optional
+            The new value for the exposure time.
+        redshift_new : float, optional
+            The new value for the cosmological redshift.
+        dist_new : tuple, optional
+            The new value for the angular diameter distance in the form
+            (value, unit), used mainly for nearby sources. This may be optionally supplied
+            instead of it being determined from the cosmology.
+        absorb_model : 'yt.analysis_modules.photon_simulator.PhotonModel`, optional
+            A model for galactic absorption.
+        psf_sigma : float, optional
+            Quick-and-dirty psf simulation using Gaussian smoothing with
+            standard deviation *psf_sigma* in degrees. 
+        sky_center : array_like, optional
+            Center RA, Dec of the events in degrees.
+        responses : list of strings, optional
+            The names of the ARF and RMF files to convolve the photons with.
+
+        Examples
+        --------
+        >>> L = np.array([0.1,-0.2,0.3])
+        >>> events = my_photons.project_photons(L, area_new="sim_arf.fits",
+        ...                                     redshift_new=0.05,
+        ...                                     psf_sigma=0.01)
+        """
+
+        if redshift_new is not None and dist_new is not None:
+            mylog.error("You may specify a new redshift or distance, "+
+                        "but not both!")
+        
+        if sky_center is None:
+             sky_center = np.array([30.,45.])
+
+        dx = self.photons["dx"]
+        nx = self.parameters["Dimension"]
+        if psf_sigma is not None:
+             psf_sigma /= 3600.
+
+        L = np.array(L)
+        L /= np.sqrt(np.dot(L, L))
+        vecs = np.identity(3)
+        t = np.cross(L, vecs).sum(axis=1)
+        ax = t.argmax()
+        north = np.cross(L, vecs[ax,:]).ravel()
+        orient = Orientation(L, north_vector=north)
+        
+        x_hat = orient.unit_vectors[0]
+        y_hat = orient.unit_vectors[1]
+        z_hat = orient.unit_vectors[2]
+
+        n_ph = self.photons["NumberOfPhotons"]
+        num_cells = len(n_ph)
+        n_ph_tot = n_ph.sum()
+        
+        eff_area = None
+
+        parameters = {}
+        
+        if responses is not None:
+            parameters["ARF"] = responses[0]
+            parameters["RMF"] = responses[1]
+            area_new = parameters["ARF"]
+            
+        if (exp_time_new is None and area_new is None and
+            redshift_new is None and dist_new is None):
+            my_n_obs = n_ph_tot
+            zobs = self.parameters["FiducialRedshift"]
+            D_A = self.parameters["FiducialAngularDiameterDistance"]*1000.
+        else:
+            if exp_time_new is None:
+                Tratio = 1.
+            else:
+                Tratio = exp_time_new/self.parameters["FiducialExposureTime"]
+            if area_new is None:
+                Aratio = 1.
+            elif isinstance(area_new, basestring):
+                if comm.rank == 0:
+                    mylog.info("Using energy-dependent effective area: %s" % (parameters["ARF"]))
+                f = pyfits.open(area_new)
+                elo = f["SPECRESP"].data.field("ENERG_LO")
+                ehi = f["SPECRESP"].data.field("ENERG_HI")
+                eff_area = np.nan_to_num(f["SPECRESP"].data.field("SPECRESP"))
+                weights = self._normalize_arf(parameters["RMF"])
+                eff_area *= weights
+                f.close()
+                Aratio = eff_area.max()/self.parameters["FiducialArea"]
+            else:
+                mylog.info("Using constant effective area.")
+                Aratio = area_new/self.parameters["FiducialArea"]
+            if redshift_new is None and dist_new is None:
+                Dratio = 1.
+                zobs = self.parameters["FiducialRedshift"]
+                D_A = self.parameters["FiducialAngularDiameterDistance"]*1000.                    
+            else:
+                if redshift_new is None:
+                    zobs = 0.0
+                    D_A = dist[0]*self.pf.units["kpc"]/self.pf.units[dist[1]]
+                else:
+                    zobs = redshift_new
+                    D_A = self.cosmo.AngularDiameterDistance(0.0,zobs)*1000.
+                fid_D_A = self.parameters["FiducialAngularDiameterDistance"]*1000.
+                Dratio = fid_D_A*fid_D_A*(1.+self.parameters["FiducialRedshift"]**3) / \
+                         (D_A*D_A*(1.+zobs)**3)
+            fak = Aratio*Tratio*Dratio
+            if fak > 1:
+                raise ValueError("Spectrum scaling factor = %g, cannot be greater than unity." % (fak))
+            my_n_obs = np.uint64(n_ph_tot*fak)
+
+        n_obs_all = comm.mpi_allreduce(my_n_obs)
+        if comm.rank == 0:
+            mylog.info("Total number of photons to use: %d" % (n_obs_all))
+        
+        x = np.random.uniform(low=-0.5,high=0.5,size=my_n_obs)
+        y = np.random.uniform(low=-0.5,high=0.5,size=my_n_obs)
+        z = np.random.uniform(low=-0.5,high=0.5,size=my_n_obs)
+                    
+        vz = self.photons["vx"]*z_hat[0] + \
+             self.photons["vy"]*z_hat[1] + \
+             self.photons["vz"]*z_hat[2]
+        shift = -vz*cm_per_km/clight
+        shift = np.sqrt((1.-shift)/(1.+shift))
+
+        if my_n_obs == n_ph_tot:
+            idxs = np.arange(my_n_obs,dtype='uint64')
+        else:
+            idxs = np.random.permutation(n_ph_tot)[:my_n_obs].astype("uint64")
+        obs_cells = np.searchsorted(self.p_bins, idxs, side='right')-1
+        delta = dx[obs_cells]
+
+        x *= delta
+        y *= delta
+        z *= delta
+        x += self.photons["x"][obs_cells]
+        y += self.photons["y"][obs_cells]
+        z += self.photons["z"][obs_cells]  
+        eobs = self.photons["Energy"][idxs]*shift[obs_cells]
+            
+        xsky = x*x_hat[0] + y*x_hat[1] + z*x_hat[2]
+        ysky = x*y_hat[0] + y*y_hat[1] + z*y_hat[2]
+        eobs /= (1.+zobs)
+        
+        if absorb_model is None:
+            not_abs = np.ones(eobs.shape, dtype='bool')
+        else:
+            mylog.info("Absorbing.")
+            absorb_model.prepare()
+            emid = absorb_model.emid
+            aspec = absorb_model.get_spectrum()
+            absorb = np.interp(eobs, emid, aspec, left=0.0, right=0.0)
+            randvec = aspec.max()*np.random.random(eobs.shape)
+            not_abs = randvec < absorb
+        
+        if eff_area is None:
+            detected = np.ones(eobs.shape, dtype='bool')
+        else:
+            mylog.info("Applying energy-dependent effective area.")
+            earf = 0.5*(elo+ehi)
+            earea = np.interp(eobs, earf, eff_area, left=0.0, right=0.0)
+            randvec = eff_area.max()*np.random.random(eobs.shape)
+            detected = randvec < earea
+        
+        detected = np.logical_and(not_abs, detected)
+                    
+        events = {}
+
+        dx_min = self.parameters["Width"]/self.parameters["Dimension"]
+        dtheta = np.rad2deg(dx_min/D_A)
+        
+        events["xpix"] = xsky[detected]/dx_min + 0.5*(nx+1) 
+        events["ypix"] = ysky[detected]/dx_min + 0.5*(nx+1)
+        events["eobs"] = eobs[detected]
+
+        events = comm.par_combine_object(events, datatype="dict", op="cat")
+
+        if psf_sigma is not None:
+            events["xpix"] += np.random.normal(sigma=psf_sigma/dtheta)
+            events["ypix"] += np.random.normal(sigma=psf_sigma/dtheta)
+        
+        num_events = len(events["xpix"])
+            
+        if comm.rank == 0: mylog.info("Total number of observed photons: %d" % (num_events))
+
+        if responses is not None:
+            events, info = self._convolve_with_rmf(parameters["RMF"], events)
+            for k, v in info.items(): parameters[k] = v
+                
+        if exp_time_new is None:
+            parameters["ExposureTime"] = self.parameters["FiducialExposureTime"]
+        else:
+            parameters["ExposureTime"] = exp_time_new
+        if area_new is None:
+            parameters["Area"] = self.parameters["FiducialArea"]
+        else:
+            parameters["Area"] = area_new
+        parameters["Redshift"] = zobs
+        parameters["AngularDiameterDistance"] = D_A/1000.
+        parameters["sky_center"] = np.array(sky_center)
+        parameters["pix_center"] = np.array([0.5*(nx+1)]*2)
+        parameters["dtheta"] = dtheta
+        
+        return EventList(events, parameters)
+
+    def _normalize_arf(self, respfile):
+        rmf = pyfits.open(respfile)
+        table = rmf["MATRIX"]
+        weights = np.array([w.sum() for w in table.data["MATRIX"]])
+        rmf.close()
+        return weights
+
+    def _convolve_with_rmf(self, respfile, events):
+        """
+        Convolve the events with a RMF file.
+        """
+        mylog.warning("This routine has not been tested to work with all RMFs. YMMV.")
+        mylog.info("Reading response matrix file (RMF): %s" % (respfile))
+        
+        hdulist = pyfits.open(respfile)
+
+        tblhdu = hdulist["MATRIX"]
+        n_de = len(tblhdu.data["ENERG_LO"])
+        mylog.info("Number of energy bins in RMF: %d" % (n_de))
+        de = tblhdu.data["ENERG_HI"] - tblhdu.data["ENERG_LO"]
+        mylog.info("Energy limits: %g %g" % (min(tblhdu.data["ENERG_LO"]),
+                                             max(tblhdu.data["ENERG_HI"])))
+
+        tblhdu2 = hdulist["EBOUNDS"]
+        n_ch = len(tblhdu2.data["CHANNEL"])
+        mylog.info("Number of channels in RMF: %d" % (n_ch))
+        
+        eidxs = np.argsort(events["eobs"])
+
+        phEE = events["eobs"][eidxs]
+        phXX = events["xpix"][eidxs]
+        phYY = events["ypix"][eidxs]
+
+        detectedChannels = []
+        pindex = 0
+
+        # run through all photon energies and find which bin they go in
+        k = 0
+        fcurr = 0
+        last = len(phEE)-1
+
+        pbar = get_pbar("Scattering energies with RMF:", n_de)
+        
+        for low,high in zip(tblhdu.data["ENERG_LO"],tblhdu.data["ENERG_HI"]):
+            # weight function for probabilities from RMF
+            weights = np.nan_to_num(tblhdu.data[k]["MATRIX"][:])
+            weights /= weights.sum()
+            # build channel number list associated to array value,
+            # there are groups of channels in rmfs with nonzero probabilities
+            trueChannel = []
+            f_chan = np.nan_to_num(tblhdu.data["F_CHAN"][k])
+            n_chan = np.nan_to_num(tblhdu.data["N_CHAN"][k])
+            n_grp = np.nan_to_num(tblhdu.data["N_CHAN"][k])
+            if not iterable(f_chan):
+                f_chan = [f_chan]
+                n_chan = [n_chan]
+                n_grp  = [n_grp]
+            for start,nchan in zip(f_chan, n_chan):
+                end = start + nchan
+                if start == end:
+                    trueChannel.append(start)
+                else:
+                    for j in range(start,end):
+                        trueChannel.append(j)
+            if len(trueChannel) > 0:
+                for q in range(fcurr,last):
+                    if phEE[q] >= low and phEE[q] < high:
+                        channelInd = np.random.choice(len(weights), p=weights)
+                        fcurr +=1
+                        detectedChannels.append(trueChannel[channelInd])
+                    if phEE[q] >= high:
+                        break
+            pbar.update(k)
+            k+=1
+        pbar.finish()
+        
+        dchannel = np.array(detectedChannels)
+
+        events["xpix"] = phXX
+        events["ypix"] = phYY
+        events["eobs"] = phEE
+        events[tblhdu.header["CHANTYPE"]] = dchannel.astype(int)
+
+        info = {"ChannelType" : tblhdu.header["CHANTYPE"],
+                "Telescope" : tblhdu.header["TELESCOP"],
+                "Instrument" : tblhdu.header["INSTRUME"]}
+        
+        return events, info
+    
+class EventList(object) :
+
+    def __init__(self, events, parameters) :
+
+        self.events = events
+        self.parameters = parameters
+        self.num_events = events["xpix"].shape[0]
+        self.wcs = pywcs.WCS(naxis=2)
+        self.wcs.wcs.crpix = parameters["pix_center"]
+        self.wcs.wcs.crval = parameters["sky_center"]
+        self.wcs.wcs.cdelt = [-parameters["dtheta"], parameters["dtheta"]]
+        self.wcs.wcs.ctype = ["RA---TAN","DEC--TAN"]
+        self.wcs.wcs.cunit = ["deg"]*2                                                
+        (self.events["xsky"],
+         self.events["ysky"]) = \
+         self.wcs.wcs_pix2world(self.events["xpix"], self.events["ypix"],
+                                1, ra_dec_order=True)
+
+    def keys(self):
+        return self.events.keys()
+
+    def has_key(self, key):
+        return key in self.keys()
+    
+    def items(self):
+        return self.events.items()
+
+    def values(self):
+        return self.events.values()
+    
+    def __getitem__(self,key):                        
+        return self.events[key]
+
+    def __repr__(self):
+        return self.events.__repr__()
+   
+    @classmethod
+    def from_h5_file(cls, h5file):
+        """
+        Initialize an EventList from a HDF5 file with filename *h5file*.
+        """
+        events = {}
+        parameters = {}
+        
+        f = h5py.File(h5file, "r")
+
+        parameters["ExposureTime"] = f["/exp_time"].value
+        parameters["Area"] = f["/area"].value
+        parameters["Redshift"] = f["/redshift"].value
+        parameters["AngularDiameterDistance"] = f["/d_a"].value
+        if "rmf" in f:
+            parameters["RMF"] = f["/rmf"].value
+        if "arf" in f:
+            parameters["ARF"] = f["/arf"].value
+        if "channel_type" in f:
+            parameters["ChannelType"] = f["/channel_type"].value
+        if "telescope" in f:
+            parameters["Telescope"] = f["/telescope"].value
+        if "instrument" in f:
+            parameters["Instrument"] = f["/instrument"].value
+
+        events["xpix"] = f["/xpix"][:]
+        events["ypix"] = f["/ypix"][:]
+        events["eobs"] = f["/eobs"][:]
+        if "pi" in f:
+            events["PI"] = f["/pi"][:]
+        if "pha" in f:
+            events["PHA"] = f["/pha"][:]
+        parameters["sky_center"] = f["/sky_center"][:]
+        parameters["dtheta"] = f["/dtheta"].value
+        parameters["pix_center"] = f["/pix_center"][:]
+        
+        f.close()
+        
+        return cls(events, parameters)
+
+    @classmethod
+    def from_fits_file(cls, fitsfile):
+        """
+        Initialize an EventList from a FITS file with filename *fitsfile*.
+        """
+        hdulist = pyfits.open(fitsfile)
+
+        tblhdu = hdulist["EVENTS"]
+        
+        events = {}
+        parameters = {}
+        
+        parameters["ExposureTime"] = tblhdu.header["EXPOSURE"]
+        parameters["Area"] = tblhdu.header["AREA"]
+        parameters["Redshift"] = tblhdu.header["REDSHIFT"]
+        parameters["AngularDiameterDistance"] = tblhdu.header["D_A"]
+        if "RMF" in tblhdu.header:
+            parameters["RMF"] = tblhdu["RMF"]
+        if "ARF" in tblhdu.header:
+            parameters["ARF"] = tblhdu["ARF"]
+        if "CHANTYPE" in tblhdu.header:
+            parameters["ChannelType"] = tblhdu["CHANTYPE"]
+        if "TELESCOP" in tblhdu.header:
+            parameters["Telescope"] = tblhdu["TELESCOP"]
+        if "INSTRUME" in tblhdu.header:
+            parameters["Instrument"] = tblhdu["INSTRUME"]
+        parameters["sky_center"] = np.array([tblhdu["TCRVL2"],tblhdu["TCRVL3"]])
+        parameters["pix_center"] = np.array([tblhdu["TCRVL2"],tblhdu["TCRVL3"]])
+        parameters["dtheta"] = tblhdu["TCRVL3"]
+        events["xpix"] = tblhdu.data.field("X")
+        events["ypix"] = tblhdu.data.field("Y")
+        events["eobs"] = tblhdu.data.field("ENERGY")/1000. # Convert to keV
+        if "PI" in tblhdu.columns.names:
+            events["PI"] = tblhdu.data.field("PI")
+        if "PHA" in tblhdu.columns.names:
+            events["PHA"] = tblhdu.data.field("PHA")
+        
+        return cls(events, parameters)
+
+    @classmethod
+    def join_events(cls, events1, events2):
+        """
+        Join two sets of events, *events1* and *events2*.
+        """
+        events = {}
+        for item1, item2 in zip(events1.items(), events2.items()):
+            k1, v1 = item1
+            k2, v2 = item2
+            events[k1] = np.concatenate([v1,v2])
+        
+        return cls(events, events1.parameters)
+                
+    @parallel_root_only
+    def write_fits_file(self, fitsfile, clobber=False):
+        """
+        Write events to a FITS binary table file with filename *fitsfile*.
+        Set *clobber* to True if you need to overwrite a previous file.
+        """
+        
+        cols = []
+
+        col1 = pyfits.Column(name='ENERGY', format='E', unit='eV',
+                             array=self.events["eobs"]*1000.)
+        col2 = pyfits.Column(name='X', format='D', unit='pixel',
+                             array=self.events["xpix"])
+        col3 = pyfits.Column(name='Y', format='D', unit='pixel',
+                             array=self.events["ypix"])
+
+        cols = [col1, col2, col3]
+
+        if self.parameters.has_key("ChannelType"):
+             chantype = self.parameters["ChannelType"]
+             if chantype == "PHA":
+                  cunit="adu"
+             elif chantype == "PI":
+                  cunit="Chan"
+             col4 = pyfits.Column(name=chantype.upper(), format='1J',
+                                  unit=cunit, array=self.events[chantype])
+             cols.append(col4)
+            
+        coldefs = pyfits.ColDefs(cols)
+        tbhdu = pyfits.new_table(coldefs)
+        tbhdu.update_ext_name("EVENTS")
+
+        tbhdu.header.update("MTYPE1", "sky")
+        tbhdu.header.update("MFORM1", "x,y")        
+        tbhdu.header.update("MTYPE2", "EQPOS")
+        tbhdu.header.update("MFORM2", "RA,DEC")
+        tbhdu.header.update("TCTYP2", "RA---TAN")
+        tbhdu.header.update("TCTYP3", "DEC--TAN")
+        tbhdu.header.update("TCRVL2", self.parameters["sky_center"][0])
+        tbhdu.header.update("TCRVL3", self.parameters["sky_center"][1])
+        tbhdu.header.update("TCDLT2", -self.parameters["dtheta"])
+        tbhdu.header.update("TCDLT3", self.parameters["dtheta"])
+        tbhdu.header.update("TCRPX2", self.parameters["pix_center"][0])
+        tbhdu.header.update("TCRPX3", self.parameters["pix_center"][1])
+        tbhdu.header.update("TLMIN2", 0.5)
+        tbhdu.header.update("TLMIN3", 0.5)
+        tbhdu.header.update("TLMAX2", 2.*self.parameters["pix_center"][0]-0.5)
+        tbhdu.header.update("TLMAX3", 2.*self.parameters["pix_center"][1]-0.5)
+        tbhdu.header.update("EXPOSURE", self.parameters["ExposureTime"])
+        tbhdu.header.update("AREA", self.parameters["Area"])
+        tbhdu.header.update("D_A", self.parameters["AngularDiameterDistance"])
+        tbhdu.header.update("REDSHIFT", self.parameters["Redshift"])
+        tbhdu.header.update("HDUVERS", "1.1.0")
+        tbhdu.header.update("RADECSYS", "FK5")
+        tbhdu.header.update("EQUINOX", 2000.0)
+        if "RMF" in self.parameters:
+            tbhdu.header.update("RMF", self.parameters["RMF"])
+        if "ARF" in self.parameters:
+            tbhdu.header.update("ARF", self.parameters["ARF"])
+        if "ChannelType" in self.parameters:
+            tbhdu.header.update("CHANTYPE", self.parameters["ChannelType"])
+        if "Telescope" in self.parameters:
+            tbhdu.header.update("TELESCOP", self.parameters["Telescope"])
+        if "Instrument" in self.parameters:
+            tbhdu.header.update("INSTRUME", self.parameters["Instrument"])
+            
+        tbhdu.writeto(fitsfile, clobber=clobber)
+
+    @parallel_root_only    
+    def write_simput_file(self, prefix, clobber=False, emin=None, emax=None):
+        r"""
+        Write events to a SIMPUT file that may be read by the SIMX instrument
+        simulator.
+
+        Parameters
+        ----------
+
+        prefix : string
+            The filename prefix.
+        clobber : boolean, optional
+            Set to True to overwrite previous files.
+        e_min : float, optional
+            The minimum energy of the photons to save in keV.
+        e_max : float, optional
+            The maximum energy of the photons to save in keV.
+        """
+
+        if isinstance(self.parameters["Area"], basestring):
+             mylog.error("Writing SIMPUT files is only supported if you didn't convolve with an ARF.")
+             raise TypeError
+        
+        if emin is None:
+            emin = self.events["eobs"].min()*0.95
+        if emax is None:
+            emax = self.events["eobs"].max()*1.05
+
+        idxs = np.logical_and(self.events["eobs"] >= emin, self.events["eobs"] <= emax)
+        flux = erg_per_keV*np.sum(self.events["eobs"][idxs]) / \
+               self.parameters["ExposureTime"]/self.parameters["Area"]
+        
+        col1 = pyfits.Column(name='ENERGY', format='E',
+                             array=self["eobs"])
+        col2 = pyfits.Column(name='DEC', format='D',
+                             array=self["xsky"])
+        col3 = pyfits.Column(name='RA', format='D',
+                             array=self["ysky"])
+
+        coldefs = pyfits.ColDefs([col1, col2, col3])
+
+        tbhdu = pyfits.new_table(coldefs)
+        tbhdu.update_ext_name("PHLIST")
+
+        tbhdu.header.update("HDUCLASS", "HEASARC/SIMPUT")
+        tbhdu.header.update("HDUCLAS1", "PHOTONS")
+        tbhdu.header.update("HDUVERS", "1.1.0")
+        tbhdu.header.update("EXTVER", 1)
+        tbhdu.header.update("REFRA", 0.0)
+        tbhdu.header.update("REFDEC", 0.0)
+        tbhdu.header.update("TUNIT1", "keV")
+        tbhdu.header.update("TUNIT2", "deg")
+        tbhdu.header.update("TUNIT3", "deg")                
+
+        phfile = prefix+"_phlist.fits"
+
+        tbhdu.writeto(phfile, clobber=clobber)
+
+        col1 = pyfits.Column(name='SRC_ID', format='J', array=np.array([1]).astype("int32"))
+        col2 = pyfits.Column(name='RA', format='D', array=np.array([self.parameters["sky_center"][0]]))
+        col3 = pyfits.Column(name='DEC', format='D', array=np.array([self.parameters["sky_center"][1]]))
+        col4 = pyfits.Column(name='E_MIN', format='D', array=np.array([emin]))
+        col5 = pyfits.Column(name='E_MAX', format='D', array=np.array([emax]))
+        col6 = pyfits.Column(name='FLUX', format='D', array=np.array([flux]))
+        col7 = pyfits.Column(name='SPECTRUM', format='80A', array=np.array([phfile+"[PHLIST,1]"]))
+        col8 = pyfits.Column(name='IMAGE', format='80A', array=np.array([phfile+"[PHLIST,1]"]))
+                        
+        coldefs = pyfits.ColDefs([col1, col2, col3, col4, col5, col6, col7, col8])
+        
+        wrhdu = pyfits.new_table(coldefs)
+        wrhdu.update_ext_name("SRC_CAT")
+                                
+        wrhdu.header.update("HDUCLASS", "HEASARC")
+        wrhdu.header.update("HDUCLAS1", "SIMPUT")
+        wrhdu.header.update("HDUCLAS2", "SRC_CAT")        
+        wrhdu.header.update("HDUVERS", "1.1.0")
+        wrhdu.header.update("RADECSYS", "FK5")
+        wrhdu.header.update("EQUINOX", 2000.0)
+        wrhdu.header.update("TUNIT2", "deg")
+        wrhdu.header.update("TUNIT3", "deg")
+        wrhdu.header.update("TUNIT4", "keV")
+        wrhdu.header.update("TUNIT5", "keV")
+        wrhdu.header.update("TUNIT6", "erg/s/cm**2")
+
+        simputfile = prefix+"_simput.fits"
+                
+        wrhdu.writeto(simputfile, clobber=clobber)
+
+    @parallel_root_only
+    def write_h5_file(self, h5file):
+        """
+        Write an EventList to the HDF5 file given by *h5file*.
+        """
+        f = h5py.File(h5file, "w")
+
+        f.create_dataset("/exp_time", data=self.parameters["ExposureTime"])
+        f.create_dataset("/area", data=self.parameters["Area"])
+        f.create_dataset("/redshift", data=self.parameters["Redshift"])
+        f.create_dataset("/d_a", data=self.parameters["AngularDiameterDistance"])        
+        if "ARF" in self.parameters:
+            f.create_dataset("/arf", data=self.parameters["ARF"])
+        if "RMF" in self.parameters:
+            f.create_dataset("/rmf", data=self.parameters["RMF"])
+        if "ChannelType" in self.parameters:
+            f.create_dataset("/channel_type", data=self.parameters["ChannelType"])
+        if "Telescope" in self.parameters:
+            f.create_dataset("/telescope", data=self.parameters["Telescope"])
+        if "Instrument" in self.parameters:
+            f.create_dataset("/instrument", data=self.parameters["Instrument"])
+                            
+        f.create_dataset("/xpix", data=self.events["xpix"])
+        f.create_dataset("/ypix", data=self.events["ypix"])
+        f.create_dataset("/xsky", data=self.events["xsky"])
+        f.create_dataset("/ysky", data=self.events["ysky"])
+        f.create_dataset("/eobs", data=self.events["eobs"])
+        if "PI" in self.events:
+            f.create_dataset("/pi", data=self.events["PI"])                  
+        if "PHA" in self.events:
+            f.create_dataset("/pha", data=self.events["PHA"])                  
+        f.create_dataset("/sky_center", data=self.parameters["sky_center"])
+        f.create_dataset("/pix_center", data=self.parameters["pix_center"])
+        f.create_dataset("/dtheta", data=self.parameters["dtheta"])
+
+        f.close()
+
+    @parallel_root_only
+    def write_fits_image(self, imagefile, clobber=False,
+                         emin=None, emax=None):
+        r"""
+        Generate a image by binning X-ray counts and write it to a FITS file.
+
+        Parameters
+        ----------
+
+        imagefile : string
+            The name of the image file to write.
+        clobber : boolean, optional
+            Set to True to overwrite a previous file.
+        emin : float, optional
+            The minimum energy of the photons to put in the image, in keV.
+        emax : float, optional
+            The maximum energy of the photons to put in the image, in keV.
+        """
+        if emin is None:
+            mask_emin = np.ones((self.num_events), dtype='bool')
+        else:
+            mask_emin = self.events["eobs"] > emin
+        if emax is None:
+            mask_emax = np.ones((self.num_events), dtype='bool')
+        else:
+            mask_emax = self.events["eobs"] < emax
+
+        mask = np.logical_and(mask_emin, mask_emax)
+
+        nx = int(2*self.parameters["pix_center"][0]-1.)
+        ny = int(2*self.parameters["pix_center"][1]-1.)
+        
+        xbins = np.linspace(0.5, float(nx)+0.5, nx+1, endpoint=True)
+        ybins = np.linspace(0.5, float(ny)+0.5, ny+1, endpoint=True)
+
+        H, xedges, yedges = np.histogram2d(self.events["xpix"][mask],
+                                           self.events["ypix"][mask],
+                                           bins=[xbins,ybins])
+        
+        hdu = pyfits.PrimaryHDU(H.T)
+        
+        hdu.header.update("MTYPE1", "EQPOS")
+        hdu.header.update("MFORM1", "RA,DEC")
+        hdu.header.update("CTYPE1", "RA---TAN")
+        hdu.header.update("CTYPE2", "DEC--TAN")
+        hdu.header.update("CRPIX1", 0.5*(nx+1))
+        hdu.header.update("CRPIX2", 0.5*(nx+1))                
+        hdu.header.update("CRVAL1", self.parameters["sky_center"][0])
+        hdu.header.update("CRVAL2", self.parameters["sky_center"][1])
+        hdu.header.update("CUNIT1", "deg")
+        hdu.header.update("CUNIT2", "deg")
+        hdu.header.update("CDELT1", -self.parameters["dtheta"])
+        hdu.header.update("CDELT2", self.parameters["dtheta"])
+        hdu.header.update("EXPOSURE", self.parameters["ExposureTime"])
+        
+        hdu.writeto(imagefile, clobber=clobber)
+                                    
+    @parallel_root_only
+    def write_spectrum(self, specfile, energy_bins=False, emin=0.1,
+                       emax=10.0, nchan=2000, clobber=False):
+        r"""
+        Bin event energies into a spectrum and write it to a FITS binary table. Can bin
+        on energy or channel, the latter only if the photons were convolved with an
+        RMF. In that case, the spectral binning will be determined by the RMF binning.
+
+        Parameters
+        ----------
+
+        specfile : string
+            The name of the FITS file to be written.
+        energy_bins : boolean, optional
+            Bin on energy or channel. 
+        emin : float, optional
+            The minimum energy of the spectral bins in keV. Only used if binning on energy.
+        emax : float, optional
+            The maximum energy of the spectral bins in keV. Only used if binning on energy.
+        nchan : integer, optional
+            The number of channels. Only used if binning on energy.
+        """
+
+        if energy_bins:
+            spectype = "energy"
+            espec = self.events["eobs"]
+            range = (emin, emax)
+            spec, ee = np.histogram(espec, bins=nchan, range=range)
+            bins = 0.5*(ee[1:]+ee[:-1])
+        else:
+            if not self.parameters.has_key("ChannelType"):
+                mylog.error("These events were not convolved with an RMF. Set energy_bins=True.")
+                raise KeyError
+            spectype = self.parameters["ChannelType"]
+            espec = self.events[spectype]
+            f = pyfits.open(self.parameters["RMF"])
+            nchan = int(f[1].header["DETCHANS"])
+            try:
+                cmin = int(f[1].header["TLMIN4"])
+            except KeyError:
+                mylog.warning("Cannot determine minimum allowed value for channel. " +
+                              "Setting to 0, which may be wrong.")
+                cmin = int(0)
+            try:
+                cmax = int(f[1].header["TLMAX4"])
+            except KeyError:
+                mylog.warning("Cannot determine maximum allowed value for channel. " +
+                              "Setting to DETCHANS, which may be wrong.")
+                cmax = int(nchan)
+            f.close()
+            minlength = nchan
+            if cmin == 1: minlength += 1
+            spec = np.bincount(self.events[spectype],minlength=minlength)
+            if cmin == 1: spec = spec[1:]
+            bins = np.arange(nchan).astype("int32")+cmin
+            
+        col1 = pyfits.Column(name='CHANNEL', format='1J', array=bins)
+        col2 = pyfits.Column(name=spectype.upper(), format='1D', array=bins.astype("float64"))
+        col3 = pyfits.Column(name='COUNTS', format='1J', array=spec.astype("int32"))
+        col4 = pyfits.Column(name='COUNT_RATE', format='1D', array=spec/self.parameters["ExposureTime"])
+        
+        coldefs = pyfits.ColDefs([col1, col2, col3, col4])
+        
+        tbhdu = pyfits.new_table(coldefs)
+        tbhdu.update_ext_name("SPECTRUM")
+
+        if not energy_bins:
+            tbhdu.header.update("DETCHANS", spec.shape[0])
+            tbhdu.header.update("TOTCTS", spec.sum())
+            tbhdu.header.update("EXPOSURE", self.parameters["ExposureTime"])
+            tbhdu.header.update("LIVETIME", self.parameters["ExposureTime"])        
+            tbhdu.header.update("CONTENT", spectype)
+            tbhdu.header.update("HDUCLASS", "OGIP")
+            tbhdu.header.update("HDUCLAS1", "SPECTRUM")
+            tbhdu.header.update("HDUCLAS2", "TOTAL")
+            tbhdu.header.update("HDUCLAS3", "TYPE:I")
+            tbhdu.header.update("HDUCLAS4", "COUNT")
+            tbhdu.header.update("HDUVERS", "1.1.0")
+            tbhdu.header.update("HDUVERS1", "1.1.0")
+            tbhdu.header.update("CHANTYPE", spectype)
+            tbhdu.header.update("BACKFILE", "none")
+            tbhdu.header.update("CORRFILE", "none")
+            tbhdu.header.update("POISSERR", True)
+            if self.parameters.has_key("RMF"):
+                 tbhdu.header.update("RESPFILE", self.parameters["RMF"])
+            else:
+                 tbhdu.header.update("RESPFILE", "none")
+            if self.parameters.has_key("ARF"):
+                tbhdu.header.update("ANCRFILE", self.parameters["ARF"])
+            else:        
+                tbhdu.header.update("ANCRFILE", "none")
+            if self.parameters.has_key("Telescope"):
+                tbhdu.header.update("TELESCOP", self.parameters["Telescope"])
+            else:
+                tbhdu.header.update("TELESCOP", "none")
+            if self.parameters.has_key("Instrument"):
+                tbhdu.header.update("INSTRUME", self.parameters["Instrument"])
+            else:
+                tbhdu.header.update("INSTRUME", "none")
+            tbhdu.header.update("AREASCAL", 1.0)
+            tbhdu.header.update("CORRSCAL", 0.0)
+            tbhdu.header.update("BACKSCAL", 1.0)
+                                
+        hdulist = pyfits.HDUList([pyfits.PrimaryHDU(), tbhdu])
+        
+        hdulist.writeto(specfile, clobber=clobber)

diff -r 6cf2b25b9cf21f6759a3161dff30f7cdcdcf0c91 -r 58580d4b9d81dfd7ab4f4ee4ee189ac1c055ef57 yt/analysis_modules/photon_simulator/setup.py
--- /dev/null
+++ b/yt/analysis_modules/photon_simulator/setup.py
@@ -0,0 +1,14 @@
+#!/usr/bin/env python
+import setuptools
+import os
+import sys
+import os.path
+
+
+def configuration(parent_package='', top_path=None):
+    from numpy.distutils.misc_util import Configuration
+    config = Configuration('photon_simulator', parent_package, top_path)
+    config.add_subpackage("tests")
+    config.make_config_py()  # installs __config__.py
+    #config.make_svn_version_py()
+    return config

diff -r 6cf2b25b9cf21f6759a3161dff30f7cdcdcf0c91 -r 58580d4b9d81dfd7ab4f4ee4ee189ac1c055ef57 yt/analysis_modules/photon_simulator/spectral_models.py
--- /dev/null
+++ b/yt/analysis_modules/photon_simulator/spectral_models.py
@@ -0,0 +1,323 @@
+"""
+Photon emission and absoprtion models for use with the
+photon simulator.
+"""
+
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
+import numpy as np
+import os
+from yt.funcs import *
+import h5py
+try:
+    import astropy.io.fits as pyfits
+    import xspec
+    from scipy.integrate import cumtrapz
+    from scipy import stats        
+except ImportError:
+    pass
+    
+from yt.utilities.physical_constants import hcgs, clight, erg_per_keV, amu_cgs
+
+hc = 1.0e8*hcgs*clight/erg_per_keV
+
+class SpectralModel(object):
+
+    def __init__(self, emin, emax, nchan):
+        self.emin = emin
+        self.emax = emax
+        self.nchan = nchan
+        self.ebins = np.linspace(emin, emax, nchan+1)
+        self.de = np.diff(self.ebins)
+        self.emid = 0.5*(self.ebins[1:]+self.ebins[:-1])
+        
+    def prepare(self):
+        pass
+    
+    def get_spectrum(self):
+        pass
+                                                        
+class XSpecThermalModel(SpectralModel):
+    r"""
+    Initialize a thermal gas emission model from PyXspec.
+    
+    Parameters
+    ----------
+
+    model_name : string
+        The name of the thermal emission model.
+    emin : float
+        The minimum energy for the spectral model.
+    emax : float
+        The maximum energy for the spectral model.
+    nchan : integer
+        The number of channels in the spectral model.
+
+    Examples
+    --------
+    >>> mekal_model = XSpecThermalModel("mekal", 0.05, 50.0, 1000)
+    """
+    def __init__(self, model_name, emin, emax, nchan):
+        self.model_name = model_name
+        SpectralModel.__init__(self, emin, emax, nchan)
+        
+    def prepare(self):
+        """
+        Prepare the thermal model for execution.
+        """
+        xspec.Xset.chatter = 0
+        xspec.AllModels.setEnergies("%f %f %d lin" %
+                                    (self.emin, self.emax, self.nchan))
+        self.model = xspec.Model(self.model_name)
+        if self.model_name == "bremss":
+            self.norm = 3.02e-15
+        else:
+            self.norm = 1.0e-14
+        
+    def get_spectrum(self, kT):
+        """
+        Get the thermal emission spectrum given a temperature *kT* in keV. 
+        """
+        m = getattr(self.model,self.model_name)
+        m.kT = kT
+        m.Abundanc = 0.0
+        m.norm = 1.0
+        m.Redshift = 0.0
+        cosmic_spec = self.norm*np.array(self.model.values(0))
+        m.Abundanc = 1.0
+        if self.model_name == "bremss":
+            metal_spec = np.zeros((self.nchan))
+        else:
+            metal_spec = self.norm*np.array(self.model.values(0)) - cosmic_spec
+        return cosmic_spec, metal_spec
+        
+class XSpecAbsorbModel(SpectralModel):
+    r"""
+    Initialize an absorption model from PyXspec.
+    
+    Parameters
+    ----------
+
+    model_name : string
+        The name of the absorption model.
+    nH : float
+        The foreground column density *nH* in units of 10^22 cm^{-2}.
+    emin : float, optional
+        The minimum energy for the spectral model.
+    emax : float, optional
+        The maximum energy for the spectral model.
+    nchan : integer, optional
+        The number of channels in the spectral model.
+
+    Examples
+    --------
+    >>> abs_model = XSpecAbsorbModel("wabs", 0.1)
+    """
+    def __init__(self, model_name, nH, emin=0.01, emax=50.0, nchan=100000):
+        self.model_name = model_name
+        self.nH = nH
+        SpectralModel.__init__(self, emin, emax, nchan)
+        
+    def prepare(self):
+        """
+        Prepare the absorption model for execution.
+        """
+        xspec.Xset.chatter = 0
+        xspec.AllModels.setEnergies("%f %f %d lin" %
+                                    (self.emin, self.emax, self.nchan))
+        self.model = xspec.Model(self.model_name+"*powerlaw")
+        self.model.powerlaw.norm = self.nchan/(self.emax-self.emin)
+        self.model.powerlaw.PhoIndex = 0.0
+
+    def get_spectrum(self):
+        """
+        Get the absorption spectrum.
+        """
+        m = getattr(self.model,self.model_name)
+        m.nH = self.nH
+        return np.array(self.model.values(0))
+
+class TableApecModel(SpectralModel):
+    r"""
+    Initialize a thermal gas emission model from the AtomDB APEC tables
+    available at http://www.atomdb.org. This code borrows heavily from Python
+    routines used to read the APEC tables developed by Adam Foster at the
+    CfA (afoster at cfa.harvard.edu). 
+    
+    Parameters
+    ----------
+
+    apec_root : string
+        The directory root where the APEC model files are stored.
+    emin : float
+        The minimum energy for the spectral model.
+    emax : float
+        The maximum energy for the spectral model.
+    nchan : integer
+        The number of channels in the spectral model.
+    apec_vers : string, optional
+        The version identifier string for the APEC files, e.g.
+        "2.0.2"
+    thermal_broad : boolean, optional
+        Whether to apply thermal broadening to spectral lines. Only should
+        be used if you are attemping to simulate a high-spectral resolution
+        detector.
+
+    Examples
+    --------
+    >>> apec_model = TableApecModel("/Users/jzuhone/Data/atomdb_v2.0.2/", 0.05, 50.0,
+                                    1000, thermal_broad=True)
+    """
+    def __init__(self, apec_root, emin, emax, nchan,
+                 apec_vers="2.0.2", thermal_broad=False):
+        self.apec_root = apec_root
+        self.apec_prefix = "apec_v"+apec_vers
+        self.cocofile = os.path.join(self.apec_root,
+                                     self.apec_prefix+"_coco.fits")
+        self.linefile = os.path.join(self.apec_root,
+                                     self.apec_prefix+"_line.fits")
+        SpectralModel.__init__(self, emin, emax, nchan)
+        self.wvbins = hc/self.ebins[::-1]
+        # H, He, and trace elements
+        self.cosmic_elem = [1,2,3,4,5,9,11,15,17,19,21,22,23,24,25,27,29,30]
+        # Non-trace metals
+        self.metal_elem = [6,7,8,10,12,13,14,16,18,20,26,28]
+        self.thermal_broad = thermal_broad
+        self.A = np.array([0.0,1.00794,4.00262,6.941,9.012182,10.811,
+                           12.0107,14.0067,15.9994,18.9984,20.1797,
+                           22.9898,24.3050,26.9815,28.0855,30.9738,
+                           32.0650,35.4530,39.9480,39.0983,40.0780,
+                           44.9559,47.8670,50.9415,51.9961,54.9380,
+                           55.8450,58.9332,58.6934,63.5460,65.3800])
+        
+    def prepare(self):
+        """
+        Prepare the thermal model for execution.
+        """
+        try:
+            self.line_handle = pyfits.open(self.linefile)
+        except IOError:
+            mylog.error("LINE file %s does not exist" % (self.linefile))
+        try:
+            self.coco_handle = pyfits.open(self.cocofile)
+        except IOError:
+            mylog.error("COCO file %s does not exist" % (self.cocofile))
+        self.Tvals = self.line_handle[1].data.field("kT")
+        self.dTvals = np.diff(self.Tvals)
+        self.minlam = self.wvbins.min()
+        self.maxlam = self.wvbins.max()
+    
+    def _make_spectrum(self, element, tindex):
+        
+        tmpspec = np.zeros((self.nchan))
+        
+        i = np.where((self.line_handle[tindex].data.field('element')==element) &
+                     (self.line_handle[tindex].data.field('lambda') > self.minlam) &
+                     (self.line_handle[tindex].data.field('lambda') < self.maxlam))[0]
+
+        vec = np.zeros((self.nchan))
+        E0 = hc/self.line_handle[tindex].data.field('lambda')[i]
+        amp = self.line_handle[tindex].data.field('epsilon')[i]
+        if self.thermal_broad:
+            vec = np.zeros((self.nchan))
+            sigma = E0*np.sqrt(self.Tvals[tindex]*erg_per_keV/(self.A[element]*amu_cgs))/clight
+            for E, sig, a in zip(E0, sigma, amp):
+                cdf = stats.norm(E,sig).cdf(self.ebins)
+                vec += np.diff(cdf)*a
+        else:
+            ie = np.searchsorted(self.ebins, E0, side='right')-1
+            for i,a in zip(ie,amp): vec[i] += a
+        tmpspec += vec
+
+        ind = np.where((self.coco_handle[tindex].data.field('Z')==element) &
+                       (self.coco_handle[tindex].data.field('rmJ')==0))[0]
+        if len(ind)==0:
+            return tmpspec
+        else:
+            ind=ind[0]
+                                                    
+        n_cont=self.coco_handle[tindex].data.field('N_Cont')[ind]
+        e_cont=self.coco_handle[tindex].data.field('E_Cont')[ind][:n_cont]
+        continuum = self.coco_handle[tindex].data.field('Continuum')[ind][:n_cont]
+
+        tmpspec += np.interp(self.emid, e_cont, continuum)*self.de
+        
+        n_pseudo=self.coco_handle[tindex].data.field('N_Pseudo')[ind]
+        e_pseudo=self.coco_handle[tindex].data.field('E_Pseudo')[ind][:n_pseudo]
+        pseudo = self.coco_handle[tindex].data.field('Pseudo')[ind][:n_pseudo]
+        
+        tmpspec += np.interp(self.emid, e_pseudo, pseudo)*self.de
+        
+        return tmpspec
+
+    def get_spectrum(self, kT):
+        """
+        Get the thermal emission spectrum given a temperature *kT* in keV. 
+        """
+        cspec_l = np.zeros((self.nchan))
+        mspec_l = np.zeros((self.nchan))
+        cspec_r = np.zeros((self.nchan))
+        mspec_r = np.zeros((self.nchan))
+        tindex = np.searchsorted(self.Tvals, kT)-1
+        if tindex >= self.Tvals.shape[0]-1 or tindex < 0:
+            return cspec_l, mspec_l
+        dT = (kT-self.Tvals[tindex])/self.dTvals[tindex]
+        # First do H,He, and trace elements
+        for elem in self.cosmic_elem:
+            cspec_l += self._make_spectrum(elem, tindex+2)
+            cspec_r += self._make_spectrum(elem, tindex+3)            
+        # Next do the metals
+        for elem in self.metal_elem:
+            mspec_l += self._make_spectrum(elem, tindex+2)
+            mspec_r += self._make_spectrum(elem, tindex+3)
+        cosmic_spec = cspec_l*(1.-dT)+cspec_r*dT
+        metal_spec = mspec_l*(1.-dT)+mspec_r*dT        
+        return cosmic_spec, metal_spec
+
+class TableAbsorbModel(SpectralModel):
+    r"""
+    Initialize an absorption model from a table stored in an HDF5 file.
+    
+    Parameters
+    ----------
+
+    filename : string
+        The name of the table file.
+    nH : float
+        The foreground column density *nH* in units of 10^22 cm^{-2}.
+
+    Examples
+    --------
+    >>> abs_model = XSpecAbsorbModel("wabs", 0.1)
+    """
+
+    def __init__(self, filename, nH):
+        if not os.path.exists(filename):
+            raise IOError("File does not exist: %s." % filename)
+        self.filename = filename
+        f = h5py.File(self.filename,"r")
+        emin = f["energy"][:].min()
+        emax = f["energy"][:].max()
+        self.sigma = f["cross_section"][:]
+        nchan = self.sigma.shape[0]
+        f.close()
+        SpectralModel.__init__(self, emin, emax, nchan)
+        self.nH = nH*1.0e22
+        
+    def prepare(self):
+        """
+        Prepare the absorption model for execution.
+        """
+        pass
+        
+    def get_spectrum(self):
+        """
+        Get the absorption spectrum.
+        """
+        return np.exp(-self.sigma*self.nH)

diff -r 6cf2b25b9cf21f6759a3161dff30f7cdcdcf0c91 -r 58580d4b9d81dfd7ab4f4ee4ee189ac1c055ef57 yt/analysis_modules/photon_simulator/tests/test_cluster.py
--- /dev/null
+++ b/yt/analysis_modules/photon_simulator/tests/test_cluster.py
@@ -0,0 +1,65 @@
+"""
+Answer test the photon_simulator analysis module.
+"""
+
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
+from yt.testing import *
+from yt.config import ytcfg
+from yt.analysis_modules.photon_simulator.api import *
+from yt.utilities.answer_testing.framework import requires_pf, \
+     GenericArrayTest, data_dir_load
+import numpy as np
+
+def setup():
+    from yt.config import ytcfg
+    ytcfg["yt", "__withintesting"] = "True"
+
+test_dir = ytcfg.get("yt", "test_data_dir")
+
+ETC = test_dir+"/enzo_tiny_cosmology/DD0046/DD0046"
+APEC = test_dir+"/xray_data/atomdb_v2.0.2"
+TBABS = test_dir+"/xray_data/tbabs_table.h5"
+ARF = test_dir+"/xray_data/chandra_ACIS-S3_onaxis_arf.fits"
+RMF = test_dir+"/xray_data/chandra_ACIS-S3_onaxis_rmf.fits"
+
+ at requires_pf(ETC)
+ at requires_file(APEC)
+ at requires_file(TBABS)
+ at requires_file(ARF)
+ at requires_file(RMF)
+def test_etc():
+
+    np.random.seed(seed=0x4d3d3d3)
+
+    pf = data_dir_load(ETC)
+    A = 3000.
+    exp_time = 1.0e5
+    redshift = 0.1
+    
+    apec_model = TableApecModel(APEC, 0.1, 20.0, 2000)
+    tbabs_model = TableAbsorbModel(TBABS, 0.1)
+
+    sphere = pf.h.sphere("max", (0.5, "mpc"))
+
+    thermal_model = ThermalPhotonModel(apec_model, Zmet=0.3)
+    photons = PhotonList.from_scratch(sphere, redshift, A, exp_time,
+                                      thermal_model)
+    
+    events = photons.project_photons([0.0,0.0,1.0],
+                                     responses=[ARF,RMF],
+                                     absorb_model=tbabs_model)
+
+    def photons_test(): return photons.photons
+    def events_test(): return events.events
+
+    for test in [GenericArrayTest(pf, photons_test),
+                 GenericArrayTest(pf, events_test)]:
+        test_etc.__name__ = test.description
+        yield test

diff -r 6cf2b25b9cf21f6759a3161dff30f7cdcdcf0c91 -r 58580d4b9d81dfd7ab4f4ee4ee189ac1c055ef57 yt/testing.py
--- a/yt/testing.py
+++ b/yt/testing.py
@@ -15,7 +15,9 @@
 import itertools as it
 import numpy as np
 import importlib
+import os
 from yt.funcs import *
+from yt.config import ytcfg
 from numpy.testing import assert_array_equal, assert_almost_equal, \
     assert_approx_equal, assert_array_almost_equal, assert_equal, \
     assert_array_less, assert_string_equal, assert_array_almost_equal_nulp,\
@@ -272,3 +274,17 @@
     else:
         return ftrue
     
+def requires_file(req_file):
+    path = ytcfg.get("yt", "test_data_dir")
+    def ffalse(func):
+        return lambda: None
+    def ftrue(func):
+        return func
+    if os.path.exists(req_file):
+        return ftrue
+    else:
+        if os.path.exists(os.path.join(path,req_file)):
+            return ftrue
+        else:
+            return ffalse
+                                        

diff -r 6cf2b25b9cf21f6759a3161dff30f7cdcdcf0c91 -r 58580d4b9d81dfd7ab4f4ee4ee189ac1c055ef57 yt/visualization/fixed_resolution.py
--- a/yt/visualization/fixed_resolution.py
+++ b/yt/visualization/fixed_resolution.py
@@ -266,17 +266,6 @@
 
     def export_fits(self, filename, fields=None, clobber=False,
                     other_keys=None, units="cm", sky_center=(0.0,0.0), D_A=None):
-
-        """
-        This will export a set of FITS images of either the fields specified
-        or all the fields already in the object.  The output filename is
-        *filename*. If clobber is set to True, this will overwrite any
-        existing FITS file.
-
-        This requires the *pyfits* module, which is a standalone module
-        provided by STSci to interface with FITS-format files, and is also
-        part of AstroPy.
-        """
         r"""Export a set of pixelized fields to a FITS file.
 
         This will export a set of FITS images of either the fields specified
@@ -302,11 +291,19 @@
         D_A : float or tuple, optional
             Angular diameter distance, given in code units as a float or
             a tuple containing the value and the length unit. Required if
-            using sky coordinates.                                                                                            
+            using sky coordinates.
         """
 
+        try:
+            import astropy.io.fits as pyfits
+        except:
+            mylog.error("You don't have AstroPy installed!")
+            raise ImportError
+        
         if units == "deg" and D_A is None:
             mylog.error("Sky coordinates require an angular diameter distance. Please specify D_A.")    
+            raise ValueError
+    
         if iterable(D_A):
             dist = D_A[0]/self.pf.units[D_A[1]]
         else:
@@ -316,7 +313,7 @@
             hdu_keys = {}
         else:
             hdu_keys = other_keys
-            
+
         extra_fields = ['x','y','z','px','py','pz','pdx','pdy','pdz','weight_field']
         if fields is None: 
             fields = [field for field in self.data_source.fields 
@@ -347,7 +344,7 @@
         data = dict([(field,self[field]) for field in fields])
         write_fits(data, filename, clobber=clobber, coords=coords,
                    other_keys=hdu_keys)
-                
+
     def open_in_ds9(self, field, take_log=True):
         """
         This will open a given field in the DS9 viewer.

This diff is so big that we needed to truncate the remainder.

Repository URL: https://bitbucket.org/yt_analysis/yt/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.



More information about the yt-svn mailing list