@@ -159,6 +159,12 @@ class DiffMatchedTractCatalogConfig(
159159 default = False ,
160160 doc = 'Whether to include unmatched rows in the matched table' ,
161161 )
162+ prefix_best_coord = pexConfig .Field [str ](
163+ default = None ,
164+ doc = "A string prefix for ra/dec coordinate columns generated from the reference coordinate if "
165+ "available, and target otherwise. Ignored if None or include_unmatched is False." ,
166+ optional = True ,
167+ )
162168
163169 @property
164170 def columns_in_ref (self ) -> list [str ]:
@@ -457,6 +463,30 @@ def run(
457463 if config .include_unmatched :
458464 # This is probably less efficient than just doing an outer join originally; worth checking
459465 cat_matched = astropy .table .vstack ([cat_matched , cat_unmatched ])
466+ if (prefix_coord := config .prefix_best_coord ) is not None :
467+ columns_coord_best = (
468+ f"{ prefix_coord } { col_coord } " for col_coord in (
469+ ("ra" , "dec" ) if config .coord_format .coords_spherical else ("coord1" , "coord2" )
470+ )
471+ )
472+ for column_coord_best , column_coord_ref , column_coord_target in zip (
473+ columns_coord_best ,
474+ (config .coord_format .column_ref_coord1 , config .coord_format .column_ref_coord2 ),
475+ (config .coord_format .column_target_coord1 , config .coord_format .column_target_coord2 ),
476+ ):
477+ column_full_ref = f'{ config .column_matched_prefix_ref } { column_coord_ref } '
478+ values = cat_matched [column_full_ref ]
479+ unit = values .unit
480+ values_bad = np .ma .masked_invalid (values ).mask
481+ # Cast to an unmasked array - there will be no bad values
482+ values = np .array (values )
483+ values [values_bad ] = cat_matched [column_coord_target ][values_bad ]
484+ cat_matched [column_coord_best ] = values
485+ cat_matched [column_coord_best ].unit = unit
486+ cat_matched [column_coord_best ].description = (
487+ f"Best { columns_coord_best } value from { column_full_ref } if available"
488+ f" else { column_coord_target } "
489+ )
460490
461491 retStruct = pipeBase .Struct (cat_matched = cat_matched )
462492 return retStruct
0 commit comments