Skip to content

Commit 1fefa72

Browse files
Merge pull request #60 from CosmoStat/dndz
improved read dndz
2 parents 1f2c9cb + 4b1ea52 commit 1fefa72

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

cs_util/cat.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,19 +253,36 @@ def read_dndz(file_path):
253253
254254
Returns
255255
-------
256-
list :
256+
np.array :
257257
redshift bin centers
258-
list :
258+
np.array :
259259
number densities
260-
list :
261-
redshift bin edges
260+
np.array :
261+
redshift bin edges; one less than centers and density arrays
262262
263263
"""
264-
dat = ascii.read(file_path, format="commented_header")
264+
try:
265+
# Expecting header line "# z dn_dz"
266+
dat = ascii.read(file_path, format="commented_header")
267+
missing = [col for col in ("z", "dn_dz") if col not in dat.dtype.names]
268+
if missing:
269+
raise ValueError(
270+
f"Missing columns in dndz path {file_path}: {missing}"
271+
)
272+
except:
273+
# No header line
274+
dat = ascii.read(file_path)
275+
dat.rename_column("col1", "z")
276+
dat.rename_column("col2", "dn_dz")
277+
278+
# Remove last n(z) value which should be zero, to match bin centers
279+
tolerance = 1e-5
280+
if dat["dn_dz"][-1] / sum(dat["dn_dz"]) > tolerance:
281+
raise ValueError("dn_dz at last z-edge = {dat['dn_dz'][-1]}, no zero")
265282

266-
# Remove last n(z) value which is zero, to match bin centers
267283
nz = dat["dn_dz"][:-1]
268284
z_edges = dat["z"]
285+
269286
z_centers = bin_edges2centers(z_edges)
270287

271288
return z_centers, nz, z_edges

0 commit comments

Comments
 (0)