desihub / desispec

DESI spectral pipeline
BSD 3-Clause "New" or "Revised" License
36 stars 24 forks source link

io.photo.gather_tractorphot should handle duplicates more gracefully #2390

Open moustakas opened 2 days ago

moustakas commented 2 days ago

https://github.com/desihub/desitarget/pull/828 had a corner-case impact on io.photo.gather_tractorphot that should be handled, especially by https://github.com/moustakas/desi-photometry when building the lsdr9 VAC.

In brief, when one requests Tractor photometry for a duplicate (input) target (e.g., the same astronomical source targeted and observed in different surveys/programs/tiles) then gather_tractorphot raises an exception because we sort by BRICK_OBJID before returning the resulting Tractor catalog-- https://github.com/desihub/desispec/blob/main/py/desispec/io/photo.py#L1019

Here's an example with a (temporary) catalog. In this case, the same target BRICKID-BRICK_OBJID=493292-1590 was observed in both sv1/bright and main/bright (although note the different RELEASE, which is not unexpected).

import fitsio
from astropy.table import Table
from desispec.io.photo import gather_tractorphot

cc = Table(fitsio.read('/pscratch/sd/i/ioannis/lsdr9/kibo/ancillary/targetphot-miniphot-kibo.fits', \
  rows=[5280437, 5280508, 5280512, 5280530])) 
cc
<Table length=4>
SURVEY PROGRAM TILEID      TARGETID             RA                DEC         PHOTSYS RELEASE BRICKNAME BRICKID BRICK_OBJID
 str7    str6  int32        int64            float64            float64         str1   int16     str8    int32     int32
------ ------- ------ ----------------- ------------------ ------------------ ------- ------- --------- ------- -----------
   sv1  bright  80611 39628468081657398  33.89429168017475 29.600306676043033       S    9010  0339p295  493292        1590
  main  bright  23930 39637264174679605 33.894274855621525 29.476587721353376       S    9012  0339p295  493292        1589
  main  bright  22359 39637264174679606   33.8942916803159   29.6003066763379       S    9012  0339p295  493292        1590
  main    dark   4930 39637264174679597  33.89398108157812 29.469024946564016       S    9012  0339p295  493292        1581

gather_tractorphot(cc, racolumn='RA', deccolumn='DEC')
ERROR:geomask.py:117:match: Array B has 1 duplicates

The best way to handle this is to explicitly check for duplicate values of BRICK_OBJID in the input catalog (for a given BRICKNAME or BRICKID) and return a Tractor catalog with the photometry duplicated for the repeat objects.

Note that this issue was almost certainly happening in earlier version of the lsdr9 VAC, but the error was silent and I'm not sure of the ultimate impact.

moustakas commented 1 day ago

FWIW, here's the solution I came up with in the lsdr9 package-- https://github.com/moustakas/desi-photometry/blob/main/mpi-photometry#L180-L233