Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add functions to read instrument info from DL1 file #468

Merged
merged 7 commits into from
Jul 10, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
237 changes: 236 additions & 1 deletion lstchain/io/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from tqdm import tqdm
from ctapipe.tools.stage1 import Stage1ProcessorTool
from astropy.utils import deprecated
from ctapipe.instrument import OpticsDescription, CameraGeometry, CameraDescription, CameraReadout, \
TelescopeDescription, SubarrayDescription


__all__ = ['read_simu_info_hdf5',
Expand Down Expand Up @@ -420,7 +422,7 @@ def write_array_info(subarray, output_filename):
serialize_meta=serialize_meta,
)


@deprecated('09/07/2020', message='will be removed in lstchain v0.7')
def read_array_info(filename):
"""
Read array information from HDF5 file.
Expand All @@ -443,6 +445,239 @@ def read_array_info(filename):
return array_info


def read_optic(filename, telescope_name):
vuillaut marked this conversation as resolved.
Show resolved Hide resolved
"""
Read a specific telescope optics from a DL1 file

Parameters
----------
filename: str
telescope_name: str

Returns
-------
`ctapipe.instrument.optics.OpticsDescription`
"""
from astropy.units import Quantity
telescope_optics_path = "/configuration/instrument/telescope/optics"
telescope_optic_table = Table.read(filename, path=telescope_optics_path)
row = telescope_optic_table[np.where(telescope_name == telescope_optic_table['name'])[0][0]]
optics_description = OpticsDescription(
name=row['name'],
num_mirrors=row['num_mirrors'],
equivalent_focal_length=row['equivalent_focal_length'] * telescope_optic_table['equivalent_focal_length'].unit,
mirror_area=row['mirror_area'] * telescope_optic_table['mirror_area'].unit,
num_mirror_tiles=Quantity(row['num_mirror_tiles']),
)
return optics_description


def read_optics(filename):
"""
Read all telescope optics from a DL1 file

Parameters
----------
filename: str

Returns
-------
dictionnary of ctapipe.instrument.optics.OpticsDescription by telescope names
"""
telescope_optics_path = "/configuration/instrument/telescope/optics"
telescope_optics_table = Table.read(filename, path=telescope_optics_path)
optics_dict = {}
for telescope_name in telescope_optics_table['name']:
optics_dict[telescope_name] = read_optic(filename, telescope_name)
return optics_dict


def read_camera_geometry(filename, camera_name):
"""
Read a specific camera geometry from a DL1 file

Parameters
----------
filename: str
camera_name: str

Returns
-------
`ctapipe.instrument.camera.geometry.CameraGeometry`
"""
camera_geometry_path = f"/configuration/instrument/telescope/camera/geometry_{camera_name}"
camera_geometry = CameraGeometry.from_table(Table.read(filename, camera_geometry_path))
return camera_geometry


def read_camera_geometries(filename):
"""
Read all camera geometries from a DL1 file

Parameters
----------
filename

Returns
-------
dictionnary of `ctapipe.instrument.camera.geometry.CameraGeometry` by camera name
"""
subarray_layout_path = 'configuration/instrument/subarray/layout'
camera_geoms = {}
for camera_name in set(Table.read(filename, path=subarray_layout_path)['camera_type']):
camera_geoms[camera_name] = read_camera_geometry(filename, camera_name)
return camera_geoms


def read_camera_readout(filename, camera_name):
"""
Read a specific camera readout from a DL1 file

Parameters
----------
filename: str
camera_name: str

Returns
-------
`ctapipe.instrument.camera.readout.CameraReadout`
"""
camera_readout_path = f"/configuration/instrument/telescope/camera/readout_{camera_name}"
return CameraReadout.from_table(Table.read(filename, path=camera_readout_path))


def read_camera_readouts(filename):
"""
Read all camera readouts from a DL1 file

Parameters
----------
filename: str

Returns
-------
dict of `ctapipe.instrument.camera.description.CameraDescription` by tel_id
"""
subarray_layout_path = 'configuration/instrument/subarray/layout'
camera_readouts = {}
for row in Table.read(filename, path=subarray_layout_path):
camera_name = row['camera_type']
camera_readouts[row['tel_id']] = read_camera_readout(filename, camera_name)
return camera_readouts


def read_camera_description(filename, camera_name):
"""
Read a specific camera description from a DL1 file

Parameters
----------
filename: str
camera_name: str

Returns
-------
`ctapipe.instrument.camera.description.CameraDescription`
"""
geom = read_camera_geometry(filename, camera_name)
readout = read_camera_readout(filename, camera_name)
return CameraDescription(camera_name, geometry=geom, readout=readout)


def read_telescope_description(filename, telescope_name, telescope_type, camera_name):
"""
Read a specific telescope description from a DL1 file

Parameters
----------
filename: str
telescope_name: str
camera_name: str

Returns
-------
`ctapipe.instrument.telescope.TelescopeDescription`
"""
optics = read_optic(filename, telescope_name)
camera_descr = read_camera_description(filename, camera_name)
return TelescopeDescription(telescope_name, telescope_type, optics=optics, camera=camera_descr)


def read_subarray_table(filename):
"""
Read the subarray as a table from a DL1 file

Parameters
----------
filename: str

Returns
-------
`astropy.table.table.Table`
"""
subarray_layout_path = 'configuration/instrument/subarray/layout'
return Table.read(filename, path=subarray_layout_path)


def read_telescopes_descriptions(filename):
"""
Read telescopes descriptions from DL1 file

Parameters
----------
filename: str

Returns
-------
dict of `ctapipe.instrument.telescope.TelescopeDescription` by tel_id
"""
subarray_table = read_subarray_table(filename)
descriptions = {}
for row in subarray_table:
tel_name = row['name']
camera_type = row['camera_type']
optics = read_optic(filename, tel_name)
camera = read_camera_description(filename, camera_type)
descriptions[row['tel_id']] = TelescopeDescription(row['name'], row['type'], optics=optics, camera=camera)
return descriptions


def read_telescopes_positions(filename):
"""
Read telescopes positions from DL1 file

Parameters
----------
filename: str

Returns
-------
dictionnary of telescopes positions by tel_id
"""
subarray_table = read_subarray_table(filename)
pos_dict = {}
for row in subarray_table['tel_id', 'pos_x', 'pos_y', 'pos_z'].iterrows():
pos_dict[row[0]] = row[1], row[2], row[3]
return pos_dict


def read_subarray_description(filename, subarray_name='LST-1'):
"""
Read subarray description from an HDF5 DL1 file

Parameters
----------
filename: str

Returns
-------
`ctapipe.instrument.subarray.SubarrayDescription`
"""
tel_pos = read_telescopes_positions(filename)
tel_descrp = read_telescopes_descriptions(filename)
return SubarrayDescription(subarray_name, tel_positions=tel_pos, tel_descriptions=tel_descrp)


def check_mcheader(mcheader1, mcheader2):
"""
Check that the information in two mcheaders are physically consistent.
Expand Down