You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The OpenCosmo Python Toolkit provides utilities for reading, writing and manipulating data from cosmological simulations produced by the Cosmolgical Physics and Advanced Computing (CPAC) group at Argonne National Laboratory. It can be used to work with smaller quantities data retrieved with the CosmoExplorer, as well as the much larget datasets these queries draw from. The OpenCosmo toolkit integrates with standard tools such as AstroPy, and allows you to manipulate data in a fully-consistent cosmological context.
Copy file name to clipboardExpand all lines: docs/source/units.rst
+128Lines changed: 128 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -57,4 +57,132 @@ If you change unit conventions after performing a filter, the filter will still
57
57
58
58
should output a value of ~1.5 x 10^10 Msun, which is about 1e10/0.67.
59
59
60
+
Converting Columns to an Equivalent Unit
61
+
----------------------------------------
62
+
63
+
You can convert all columns with a given unit into a different unit with the :code:`conversions` argument:
64
+
65
+
66
+
.. code-block:: python
67
+
68
+
import opencosmo as oc
60
69
70
+
ds = oc.read("haloproperties.hdf5")
71
+
conversions = {u.Mpc: u.lyr}
72
+
ds = ds.with_units(conversions=conversions)
73
+
74
+
In the new dataset, all columns that originally had units of megaparsecs will be converted to lightyears. Composite units including megaparsec (e.g. km / s / Mpc, Mpc^2) will *not* be converted. All-column conversions are always peformed after a change of unit conventions. Changing units *after* doing a conversion always clears the conversions.
75
+
76
+
.. code-block:: python
77
+
78
+
import opencosmo as oc
79
+
80
+
ds = oc.read("haloproperties.hdf5")
81
+
conversions = {u.Mpc: u.lyr}
82
+
ds = ds.with_units(conversions=conversions)
83
+
84
+
85
+
86
+
Single-Column Conversions
87
+
-------------------------
88
+
89
+
You can also use :code:`with_units` to convert the values in individual columns to their values in an equivalent unit:
Unit conversions like these are always performed *after* any change in unit convention, and changing unit conventions clears any existing unit conversions:
102
+
103
+
.. code-block:: python
104
+
105
+
# this works
106
+
dataset = dataset.with_units(fof_halo_mass=u.kg)
107
+
108
+
# this clears the previous conversion,
109
+
# the masses are now in Msun / h
110
+
dataset = dataset.with_units("scalefree")
111
+
112
+
# This now fails, because the units of masses
113
+
# are Msun / h, which cannot be converted to kg
114
+
dataset = dataset.with_units(fof_halo_mass=u.kg)
115
+
116
+
# this will work, the units of halo mass in the "physical"
117
+
# convention are Msun (no h), and the change of convention
Unit conversions on :py:class:`Lightcones <opencosmo.Lightcone>` and :py:class:`SimulationCollections <opencosmo.SimulationCollection>` behave identically to single datasets. In :py:class:`StructureCollections <opencosmo.StructureCollections>`, unit conversions must be passed on a per-dataset basis:
As with all-column conversions, composite units that include the target unit will not be converted. If you want to convert a composite unit, the conversion must be stated seperately.
138
+
139
+
Conversion Precedence
140
+
---------------------
141
+
142
+
In cases where a blanket conversion is provided alongside a conversion for a specific column, the specific conversion always take precedence:
All columns with units of megaparsecs will be converted to lightyears, except for the :code:`fof_halo_center_x` column which will be converted to kilometers.
152
+
153
+
Structure Collection Conversions
154
+
--------------------------------
155
+
156
+
When working with a structure collection, you can provide conversions that apply to the entire collection, as single dataset inside the collection, or individual columns within a given dataset. As you might expect, conversions on an individual dataset takes precedence over those that apply to all datasets.
157
+
158
+
.. code-block:: python
159
+
160
+
import astropy.units as u
161
+
162
+
conversions = {u.Mpc: u.lyr}
163
+
structures = structures.with_units(
164
+
conversions=conversions
165
+
halo_properties= {
166
+
"conversions": {u.Mpc: u.km},
167
+
"fof_halo_center_x": u.m
168
+
}
169
+
)
170
+
171
+
In this example, all values in Mpc will be converted to lightyears, except in the "halo_properties" dataset, where they will be converted to kilometers. The column "fof_halo_center_x" in "halo_properties" will be converted to meters instead.
172
+
173
+
Clearing Conversions
174
+
--------------------
175
+
176
+
Conversions are always cleared when changing unit conventions, or you can also clear them by calling :code:`with_units` with no arguments.
0 commit comments