9090from ctlearn .utils import validate_trait_dict
9191
9292# Convienient constants for column names and table keys
93- CONFIG_INSTRUMENT_SUBARRAY_LAYOUT = "/configuration/instrument/subarray/layout"
94- CONFIG_INSTRUMENT_TEL = "/configuration/instrument/telescope"
95- CONFIG_INSTRUMENT_TEL_CAMERA = "/configuration/instrument/telescope/camera"
9693SUBARRAY_EVENT_KEYS = ["obs_id" , "event_id" ]
9794TEL_EVENT_KEYS = ["obs_id" , "event_id" , "tel_id" ]
9895TEL_ITER_GROUPS = [
@@ -519,7 +516,13 @@ def _ensure_subarray_consistency(self):
519516 if input_subarray == self .dl1dh_reader .subarray :
520517 return
521518
522- self .dl1dh_reader .subarray .to_hdf (self .output_path , overwrite = True )
519+ # From the merger tool a SubarrayDescription for the full array is already stored
520+ # in the output file. We need to remove it to avoid conflicts when storing
521+ # the new SubarrayDescription for the selected telescopes.
522+ with tables .open_file (self .output_path , mode = "a" ) as h5file :
523+ h5file .remove_node ("/configuration/instrument" , recursive = True )
524+ selected_subarray = input_subarray .select_subarray (set (self .dl1dh_reader .tel_ids ))
525+ selected_subarray .to_hdf (self .output_path )
523526 self .log .info ("SubarrayDescription was stored in '%s'" , self .output_path )
524527
525528 tel_trigger_table = read_table (
@@ -538,9 +541,12 @@ def _ensure_subarray_consistency(self):
538541 )
539542
540543 subarray_trigger_table = tel_trigger_table .copy ()
541- subarray_trigger_table .keep_columns (
542- SUBARRAY_EVENT_KEYS + ["time" , "event_type" ]
543- )
544+ subarray_columns = SUBARRAY_EVENT_KEYS + ["time" ]
545+ # In older data formats the event type is not included in the trigger table, so we need to
546+ # check if it is present before keeping the column to be backwards compatible.
547+ if "event_type" in subarray_trigger_table .colnames :
548+ subarray_columns .append ("event_type" )
549+ subarray_trigger_table .keep_columns (subarray_columns )
544550 subarray_trigger_table = unique (
545551 subarray_trigger_table , keys = SUBARRAY_EVENT_KEYS
546552 )
@@ -611,19 +617,6 @@ def prune_group(group, valid_ids):
611617 if group is not None :
612618 prune_group (group , tel_ids )
613619
614- # Camera configuration tables
615- layout_node = getattr (h5_file .root , CONFIG_INSTRUMENT_SUBARRAY_LAYOUT , None )
616- camera_group = getattr (h5_file .root , CONFIG_INSTRUMENT_TEL_CAMERA , None )
617- if not (layout_node and camera_group ):
618- return
619- # layout can be either a Table or a Group containing a Table
620- layout_table = (
621- layout_node
622- if isinstance (layout_node , tables .Table )
623- else next (layout_node ._f_iter_nodes ("Table" ))
624- )
625- camera_indices = set (layout_table .col ("camera_index" ))
626- prune_group (camera_group , camera_indices )
627620
628621 def _create_nan_table (self , nonexample_identifiers , columns , shapes , reco_task ):
629622 """
0 commit comments