Skip to content

Commit 97adc5d

Browse files
authored
Merge pull request #125 from fact-project/read_spectrum
Add function to read spectrum properties from corsika header files
2 parents 7d86846 + a2cee16 commit 97adc5d

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

fact/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.22.3
1+
0.23.0

fact/io.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import logging
77
import numpy as np
88
from copy import copy
9+
import astropy.units as u
910

1011

1112
__all__ = [
@@ -478,3 +479,46 @@ def append_to_h5py(f, array, key='events'):
478479
if dataset is None:
479480
raise KeyError('No such dataset {}'.format(column))
480481
append_to_h5py_dataset(array[column], dataset)
482+
483+
484+
def read_simulated_spectrum(corsika_headers_path):
485+
'''
486+
Read the properties of the simulated spectrum
487+
from a corsika headers hdf5 file.
488+
489+
Looks in the `corsika_runs` group for the keys
490+
`n_showers`, `energy_min`, `energy_max`, `x_scatter`
491+
and `energy_spectrum_slope`.
492+
493+
For older versions, it checks the hdf5 attribute `scatter_radius`
494+
of the `corsika_runs` groups.
495+
'''
496+
runs = read_h5py(corsika_headers_path, key='corsika_runs')
497+
498+
summary = {}
499+
try:
500+
summary['n_showers'] = runs['n_showers'].sum()
501+
except KeyError:
502+
summary['n_showers'] = runs['n_events'].sum()
503+
504+
keys = {'energy_min': u.GeV, 'energy_max': u.GeV, 'energy_spectrum_slope': None}
505+
if 'x_scatter' in runs.columns:
506+
keys['x_scatter'] = u.cm
507+
else:
508+
with h5py.File(corsika_headers_path, 'r') as f:
509+
r = f['corsika_runs'].attrs.get('scatter_radius')
510+
unit = f['corsika_runs'].attrs.get('scatter_radius_unit', u.m)
511+
if r is None:
512+
raise ValueError(
513+
'File does neither contain column `/corsika_runs/x_scatter` '
514+
'nor the attribute `scatter_radius`'
515+
)
516+
summary['x_scatter'] = u.Quantity(r, u.m)
517+
518+
for key, unit in keys.items():
519+
unique_values = runs[key].unique()
520+
if len(unique_values) > 1:
521+
raise ValueError('Only simulations with the same "{}" supported'.format(key))
522+
summary[key] = u.Quantity(unique_values[0], unit)
523+
524+
return summary

0 commit comments

Comments
 (0)