|
6 | 6 | import logging
|
7 | 7 | import numpy as np
|
8 | 8 | from copy import copy
|
| 9 | +import astropy.units as u |
9 | 10 |
|
10 | 11 |
|
11 | 12 | __all__ = [
|
@@ -478,3 +479,46 @@ def append_to_h5py(f, array, key='events'):
|
478 | 479 | if dataset is None:
|
479 | 480 | raise KeyError('No such dataset {}'.format(column))
|
480 | 481 | 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