|
| 1 | +import dataclasses |
1 | 2 | import os |
2 | | -from typing import Literal |
3 | 3 | from collections.abc import Iterator |
| 4 | +from typing import Literal |
4 | 5 |
|
5 | 6 | import healpy as hp |
6 | 7 | import numpy as np |
7 | 8 |
|
8 | | -from ._pkdgrav import Simulation |
| 9 | +from ._pkdgrav import Simulation, steps |
9 | 10 |
|
10 | 11 |
|
11 | 12 | class NpyLoader: |
@@ -63,36 +64,26 @@ def read_gowerst( |
63 | 64 | else: |
64 | 65 | raise ValueError(f"unknown format: {format}") |
65 | 66 |
|
66 | | - # pre-compute some simulation properties |
67 | | - boxsize = sim.parameters["dBoxSize"] / sim.cosmology.h |
68 | | - density = (sim.parameters["nGrid"] / boxsize) ** 3 |
69 | | - |
70 | | - steps = range(sim.parameters["nSteps"], 0, -1) |
71 | | - redshifts = sim.redshifts |
72 | | - for step, z_near, z_far in zip(steps, redshifts, redshifts[1:]): |
73 | | - if zmax is not None and zmax < z_near: |
| 67 | + # iterate shells |
| 68 | + for step in steps(sim): |
| 69 | + if zmax is not None and zmax < step.near_redshift: |
74 | 70 | break |
75 | 71 |
|
76 | | - data = loader(step) |
| 72 | + data = loader(step.step) |
77 | 73 |
|
78 | | - comoving_volume = sim.cosmology.comoving_volume(z_near, z_far) |
| 74 | + # number of particles in shell |
| 75 | + particles = data.sum() |
79 | 76 |
|
80 | | - metadata = { |
81 | | - "comoving volume": comoving_volume, |
82 | | - "far redshift": z_far, |
83 | | - "mean density": density, |
84 | | - "mean particles": density * comoving_volume, |
85 | | - "near redshift": z_near, |
86 | | - "particles": data.sum(), |
87 | | - } |
| 77 | + metadata = dataclasses.asdict(step) |
| 78 | + metadata["particles"] = particles |
88 | 79 |
|
89 | 80 | if nside is not None and nside != hp.get_nside(data): |
90 | 81 | # keep the number of particles constant |
91 | 82 | data = hp.ud_grade(data, nside, power=-2) |
92 | | - assert data.sum() == metadata["particles"], "resampling lost particles!" |
| 83 | + assert data.sum() == particles, "resampling lost particles!" |
93 | 84 |
|
94 | 85 | if not raw: |
95 | | - nbar = metadata["mean particles"] / data.size |
| 86 | + nbar = step.mean_particles / data.size |
96 | 87 | data = data / nbar - 1.0 |
97 | 88 |
|
98 | 89 | # attach metadata |
|
0 commit comments