-- Galaxies without unsaturated pixels
-- Find all galaxies without unsaturated pixels within 1 arcsecond of a given
-- point in the sky (right ascension and declination).
-- 
-- This is a classic spatial lookup for galaxies that are within one arcsecond of
-- the specified point.
-- 
-- Use StackMeta, magSat = peak flux in object expressed as mag 

DECLARE @r float
SET @r = 1.0/60.0 -- 1 arcsecond (in arcMinutes)
SELECT s.magSat,o.*
FROM
  dbo.fGetNearbyObjEq(148.138451880545,0.455837455564073, @r) n -- 1 arcsec cone search
  JOIN Galaxy o ON o.objId = n.objId	-- to get galaxy object params
  JOIN StackDetection l ON o.objId = l.objId	-- link to stack detection
  JOIN StackMetaDefaults s ON l.stackID = s.stackID	-- link to stack meta
WHERE
  s.magSat < 0.65		-- some value that indicates saturation
