-- This is part 1 of 2 of a query to get a list of exposures on which magic may be performed
-- After this follows magictool_definebyquery_select_part2.sql
SELECT
    exp_id,
    filter,
    num_todo,
    num_done
FROM (
    -- Number of skycells as a function of exposure
    SELECT
        exp_id,
        filter,
        COUNT(skycell_id) AS num_todo
    FROM rawExp
    JOIN chipRun USING(exp_id, tess_id)
    JOIN camRun USING(chip_id, tess_id)
    JOIN fakeRun USING(cam_id, tess_id)
    JOIN warpRun USING(fake_id, tess_id)
    JOIN warpSkyCellMap USING(warp_id, tess_id)
    JOIN warpSkyfile USING(warp_id, skycell_id, tess_id)
    WHERE
        warpSkyfile.ignored = 0
        AND warpRun.state = 'stop'
    -- INSERT HERE any additional restrictions (e.g., exp_id, warpSkyfile.good_frac)
-- INSERT HERE magictool_definebyquery_select_part2.sql
