diff --git a/csiborgtools/units/box_units.py b/csiborgtools/units/box_units.py index cff387a..a3c4bc9 100644 --- a/csiborgtools/units/box_units.py +++ b/csiborgtools/units/box_units.py @@ -17,10 +17,11 @@ Simulation box unit transformations. """ import numpy -from astropy.cosmology import LambdaCDM +from astropy.cosmology import (LambdaCDM, z_at_value) from astropy import (constants, units) from ..io import read_info + # Map of unit conversions CONV_NAME = { "length": ["peak_x", "peak_y", "peak_z", "Rs", "rmin", "rmax", "r200", @@ -198,6 +199,23 @@ class BoxUnits: """ return self.box2kpc(length) * 1e-3 + def box2cosmoredshift(self, length): + r""" + Convert the box comoving distance to cosmological redshift. + + Parameters + ---------- + length : float + Length in box units. + + Returns + ------- + cosmo_redshift : foat + The cosmological redshift. + """ + dist = self.box2mpc(length) * units.Mpc + return z_at_value(self._cosmo.comoving_distance, dist) + def solarmass2box(self, mass): r""" Convert mass from :math:`M_\odot` (with :math:`h=0.705`) to box units.