We draw random points with a density of 100 points per arcmin^{2}, for each patch and each filter, using code available at https://github.com/jcoupon/maskUtils. These random points can be used for several purposes; (1) a random sample for clustering analysis, (2) to identify problematic areas, (3) to compute the survey area and the fraction of masked areas, etc. Available information carried by the random points, per filter and per patch, is:

- patch and tract number
- patch and tract geometry (non-overlapping regions: patch_inner and tract_inner)
- pixel mask bits such as flags_pixel_interpolated_any
- local PSF size: recorded as the second moment matrix (see FAQ: how to measure the size of an object, to convert into FWHM)
`sky_mean`

: mean of sky value in 2″ diameter apertures (`merge.footprint.sky == True`

objects). Constant within patch.`sky_std`

: standard deviation of sky value in 2″ diameter apertures (`merge.footprint.sky == True`

objects). Constant within patch. To compute the limiting magnitude at 5 sigma, do:`lim_mag = -2.5 log10(5*sky_std) + 27.0`

`adjust_density`

parameter: a random number between 0 and 1 to adjust the sky density. A cut like`adjust_density < 0.5`

will decrease the sky density by a factor of two- number of visits contributed to coadds (countinputs)
`pix_variance`

: pixel variance at random point position (note that the variance given for the object is the sum of the pixel variance over the source footprint).- Milky Way extinction value (Schlegel map).

## Known issues

- Some functions of the object database are not available in the randoms database.

## Examples

### Query the database

-- This is a simple sql query to get all columns from the random database in one single patch
-- Use tractSearch(object_id, tract) function or skymap_id (= tract * 10000 + patch_x * 100 + patch_y)
SELECT *
FROM pdr2_wide.random AS ran
WHERE tractsearch(object_id, 8523)
AND patch_s = '3,5'
-- AND skymap_id = 85230305
AND i_pixelflags_bright_objectcenter is not True
AND i_pixelflags_edge is not True
LIMIT 10;

### Do a joint object/random query

-- The object version.
SELECT object_id, ra, dec, i_kronflux_mag, i_kronflux_magsigma
FROM pdr2_wide.forced2 JOIN pdr2_wide.forced USING (object_id)
WHERE boxSearch(coord, 34.0, 36.0, -5.0, -4.5)
AND NOT i_pixelflags_bright_objectcenter
AND NOT i_pixelflags_edge
AND i_inputcount_value > 4;

-- The random version.
-- Note that the boxSearch function is not available in
-- the random database.
-- For the randoms, the "NOT i_pixelflags_edge" is mandatory
-- near the border of the survey.
SELECT object_id, ra, dec
FROM pdr2_wide.random
WHERE ra BETWEEN 34.0 AND 36.0
AND dec BETWEEN -5.0 AND -4.5
AND NOT i_pixelflags_bright_objectcenter
AND NOT i_pixelflags_edge
AND i_inputcount_value > 4
LIMIT 10;

### Add a cut on PSF size

Note that in the random database, the moments are in pixel, whereas in the object data, it's in arcsec (hence the 0.17 factor for the random). The factor 2sqrt(2) is to convert from sigma to FWHM, assuming the PSF is gaussianrandom: shape_detradius(array[i_sdssshape_psf_shape11, i_sdssshape_psf_shape22, i_sdssshape_psf_shape12])*2.0*sqrt(2.0)*0.17 < 1.0
object: shape_detradius(array[i_sdssshape_psf_shape11, i_sdssshape_psf_shape22, i_sdssshape_psf_shape12])*2.0*sqrt(2.0) < 1.0

### Compute the survey area

The random point density is 100 per arcmin^{2}. Thus the area in deg

^{2}is simply the number of random points given a selection:

SELECT COUNT(*) / 100.0 / 3600.0
FROM pdr2_dud.random
WHERE i_inputcount_value > 10;

### Adjust the random local density

The "adjust_density" is a random number in the interval [0:1]. For example, to divide the random point density by a factor of two (to get 50 /arcmin^{2}), set:

adjust_density < 0.5

### Mean sky and sky variance in a patch

The mean and standard deviation of the sky object in 2" diameter aperture per patch is stored as sky_mean and sky_std. To get these quantities per patch and compute the limiting magnitude, e.g. at 5 sigma, one may do this directly using the database-- This query gets the patch mean sky and variance
-- Use tractSearch(object_id, tract) function or skymap_id (= tract * 10000 + patch_x * 100 + patch_y)
SELECT i_sky_mean, i_sky_std, -2.5*LOG(5.0*i_sky_std)+27.0 as imag_limit
FROM pdr2_wide.random
WHERE tractsearch(object_id, 8523)
AND patch_s = '3,5'
-- AND skymap_id = 85230305
LIMIT 1;

### Compute the masked fraction

-- This query returns the masked fraction in bright object masks for tract 8523.
-- Use tractSearch(object_id, tract) function
SELECT 1.0 - CAST(SUM(CASE WHEN i_pixelflags_edge is not True AND i_pixelflags_bright_objectcenter is not True
THEN 1 ELSE 0 END ) AS DECIMAL)
/ CAST(SUM(CASE WHEN i_pixelflags_edge is not True
THEN 1 ELSE 0 END ) AS DECIMAL) AS masked_fraction
FROM pdr2_wide.random AS ran
WHERE tractsearch(object_id, 8523);

### Identify areas with large seeing

-- This query gets 1% of random objects in the entire Wide and record the position as well as the local seeing FWHM.
SELECT ra, dec,
shape_detradius(array[g_sdssshape_psf_shape11, g_sdssshape_psf_shape22, g_sdssshape_psf_shape12])*2.0*sqrt(2.0)*0.17 AS gPSF,
shape_detradius(array[r_sdssshape_psf_shape11, r_sdssshape_psf_shape22, r_sdssshape_psf_shape12])*2.0*sqrt(2.0)*0.17 AS rPSF,
shape_detradius(array[i_sdssshape_psf_shape11, i_sdssshape_psf_shape22, i_sdssshape_psf_shape12])*2.0*sqrt(2.0)*0.17 AS iPSF,
shape_detradius(array[z_sdssshape_psf_shape11, z_sdssshape_psf_shape22, z_sdssshape_psf_shape12])*2.0*sqrt(2.0)*0.17 AS zPSF,
shape_detradius(array[y_sdssshape_psf_shape11, y_sdssshape_psf_shape22, y_sdssshape_psf_shape12])*2.0*sqrt(2.0)*0.17 AS yPSF
FROM pdr2_wide.random
WHERE adjust_density < 0.01
LIMIT 10;