#!/bin/bash

#alias stilts="java -Djava.awt.headless=true -Xmx512M -jar ${HOME}/spitzer/swire/swire-sdss/summary/stilts.jar -disk"
alias stilts="java -Djava.awt.headless=true -Xmx512M -jar ${HOME}/install/stilts.jar -disk"

stilts -version

export dir="${HOME}/spitzer/swire/swire-sdss/hsc/cats/"
#export date="20170324"
export date="20170526"

export field="xmm_lss" ; export survey="wide"


# SOME NOTES
# Series of photometry flags downloaded [! means we use for flagging]:
# gflags_pixel_edge !
# gflags_pixel_offimage !
# gflags_pixel_bright_object_center 
# gflags_pixel_bright_object_any
#  -- flags recommended from Table 4 survey paper (Aihara et al. 2017)
# gflags_pixel_interpolated_center !
# gflags_pixel_interpolated_any
# gflags_pixel_saturated_center !
# gflags_pixel_saturated_any
# gflux_kron_flags   [Should I add this? not now, but left a note in the readme]
# gflux_psf_flags    [Should I add this? not now, but left a note in the readme]
# gcmodel_flux_flags [Should I add this? not now, but left a note in the readme]
#  -- flags included in the "clean photometry" set query (of these only *pixel_edge, *pixel_cr_any and *pixel_bad flags are actually important)
# gcentroid_sdss_flags !
# gflags_pixel_cr_any !
# gflags_pixel_bad !
#  -- the following flags are not downloaded because they are included in the selection applied for primary objects
#  -- detect_is_patch_inner, detect_is_tract_inner, deblend_nchild, dtect_is_primary
# parent_id
#  -- Afterburner quantities (left joined to forced photometry)
# object_id
# gdetected_notjunk !
# gchild_flux_convolved_flag


  #BRIGHT OBJECTS [We do not clean for them as we want to keep also "extended/local" sources]: ( gflags_pixel_bright_object_center || rflags_pixel_bright_object_center || iflags_pixel_bright_object_center || zflags_pixel_bright_object_center || yflags_pixel_bright_object_center || n921flags_pixel_bright_object_center || n816flags_pixel_bright_object_center )
#"The masks were prepared prior to the processing and we chose a conservatively large radius. Another known feature is that nearby bright galaxies are often misinterpreted as stars in the NOMAD catalog. About 8% of bright (< 17.5 mag in one of gri bands) objects are actually galaxies and they are masked out. It is advised not to use the bright object masks for studies of nearby galaxies. Improvements will be made in a future version of the pipeline."
# All entries in the objects and the randoms databases are flagged whether they fall in a mask. The name is "bright object flag". And since the objects are extended you have 2 flags: "center" and "any" pixels. Note also that a few bright galaxies are masked. We are working on a better version.

# JUNKS [We clean for them]: Plus flags from the afterburner catalogs: the ObjID is the same between the forced and the afterburner catalogs - BUT useful only for clustering studies at high z, very bright objects and extended ?. See sec. 5.8.1 and 5.8.3 of release paper: the junk suppression procedure was mistakenly left disabled in the main processing (forced?), but it was turned on in the afterburner processing and the users are encouraged to use the afterburner table in the database to reduce spurious sources.

#REFERENCE BAND: After measuring the coadd for each band independently, we then select a “reference band” for each source. For most sources, this is the i-band; for sources not detected (or low signal-to-noise) in i, we use r, then z, y, g, and narrow bands. We then perform “forced” measurement in the coadds in all bands, in which positions and shapes are held fixed at their values from the reference band and only amplitudes (i.e. fluxes) are allowed to vary. This consistent photometry across bands produces our best estimates of colors

stilts tpipe in=${dir}/${field}-${survey}-hsc-ssp-pdr1-forced-afterburner-${date}.fits.gz \
       cmd='addcol "gCLeanPhotoFlag" "( gflags_pixel_edge || gflags_pixel_saturated_center || gflags_pixel_offimage || gflags_pixel_interpolated_center || gflags_pixel_cr_any || gflags_pixel_bad || gcentroid_sdss_flags || gcentroid_sdss_flags || !(gdetected_notjunk) ) ? 0 : 1"' \
       cmd='addcol "rCLeanPhotoFlag" "( rflags_pixel_edge || rflags_pixel_saturated_center || rflags_pixel_offimage || rflags_pixel_interpolated_center || rflags_pixel_cr_any || rflags_pixel_bad || rcentroid_sdss_flags || rcentroid_sdss_flags || !(rdetected_notjunk) ) ? 0 : 1"' \
       cmd='addcol "iCLeanPhotoFlag" "( iflags_pixel_edge || iflags_pixel_saturated_center || iflags_pixel_offimage || iflags_pixel_interpolated_center || iflags_pixel_cr_any || iflags_pixel_bad || icentroid_sdss_flags || icentroid_sdss_flags || !(idetected_notjunk) ) ? 0 : 1"' \
       cmd='addcol "zCLeanPhotoFlag" "( zflags_pixel_edge || zflags_pixel_saturated_center || zflags_pixel_offimage || zflags_pixel_interpolated_center || zflags_pixel_cr_any || zflags_pixel_bad || zcentroid_sdss_flags || zcentroid_sdss_flags || !(zdetected_notjunk) ) ? 0 : 1"' \
       cmd='addcol "yCLeanPhotoFlag" "( yflags_pixel_edge || yflags_pixel_saturated_center || yflags_pixel_offimage || yflags_pixel_interpolated_center || yflags_pixel_cr_any || yflags_pixel_bad || ycentroid_sdss_flags || ycentroid_sdss_flags || !(ydetected_notjunk) ) ? 0 : 1"' \
       cmd='select "iCleanPhotoFlag == 1 || rCleanPhotoFlag ==1 "' \
       out=${dir}/${field}-${survey}-hsc-ssp-pdr1-forced-afterburner-${date}-plus.fits ofmt=fits ; gzip ${dir}/${field}-${survey}-hsc-ssp-pdr1-forced-afterburner-${date}-plus.fits
       #cmd="keepcols 'object_id ra dec gmag_aperture30 rmag_aperture30 imag_aperture30 zmag_aperture30 ymag_aperture30 n921mag_aperture30 n816mag_aperture30 gmag_aperture30_err rmag_aperture30_err imag_aperture30_err zmag_aperture30_err ymag_aperture30_err n921mag_aperture30_err n816mag_aperture30_err gmag_kron rmag_kron imag_kron zmag_kron ymag_kron n921mag_kron n816mag_kron gmag_kron_err rmag_kron_err imag_kron_err zmag_kron_err ymag_kron_err n921mag_kron_err n816mag_kron_err gmag_psf rmag_psf imag_psf zmag_psf ymag_psf n921mag_psf n816mag_psf gmag_psf_err rmag_psf_err imag_psf_err zmag_psf_err ymag_psf_err n921mag_psf_err n816mag_psf_err gcmodel_mag rcmodel_mag icmodel_mag zcmodel_mag ycmodel_mag n921cmodel_mag n816cmodel_mag gcmodel_mag_err rcmodel_mag_err icmodel_mag_err zcmodel_mag_err ycmodel_mag_err n921cmodel_mag_err n816cmodel_mag_err gflux_kron_radius rflux_kron_radius iflux_kron_radius zflux_kron_radius yflux_kron_radius n921flux_kron_radius n816flux_kron_radius gclassification_extendedness rclassification_extendedness iclassification_extendedness zclassification_extendedness yclassification_extendedness iCLeanPhotoFlag gCLeanPhotoFlag rCLeanPhotoFlag zCLeanPhotoFlag yCLeanPhotoFlag n921CLeanPhotoFlag n816CLeanPhotoFlag object_id gdetected_notjunk gchild_flux_convolved_flag rdetected_notjunk rchild_flux_convolved_flag idetected_notjunk ichild_flux_convolved_flag zdetected_notjunk zchild_flux_convolved_flag ydetected_notjunk ychild_flux_convolved_flag n921detected_notjunk n921child_flux_convolved_flag n816detected_notjunk n816child_flux_convolved_flag iflags_pixel_edge iflags_pixel_bright_object_center iflags_pixel_saturated_any iflags_pixel_offimage iflags_pixel_interpolated_center iflags_pixel_interpolated_center iflags_pixel_cr_any iflags_pixel_bad gcmodel_flux_flags rcmodel_flux_flags icmodel_flux_flags zcmodel_flux_flags ycmodel_flux_flags n921cmodel_flux_flags n816cmodel_flux_flags detect_is_patch_inner detect_is_tract_inner deblend_nchild parent_id detect_is_primary gcentroid_sdss_flags rcentroid_sdss_flags icentroid_sdss_flags zcentroid_sdss_flags ycentroid_sdss_flags'"\
       
#Matched results between ours and then is imposing the same condition!
#iCLeanPhotoFlag == 1 && gCLeanPhotoFlag == 1 && rCLeanPhotoFlag == 1 && zCLeanPhotoFlag == 1 && yCLeanPhotoFlag == 1 && !(gcmodel_flux_flags) && !(rcmodel_flux_flags) && !(icmodel_flux_flags) && !(zcmodel_flux_flags) && !(ycmodel_flux_flags) 
#757387
#vs
#(with gdetected_notjunk && rdetected_notjunk && idetected_notjunk && zdetected_notjunk && ydetected_notjunk)
#757387

#iCLeanPhotoFlag == 1 && gCLeanPhotoFlag == 1 && rCLeanPhotoFlag == 1 && zCLeanPhotoFlag == 1 && yCLeanPhotoFlag == 1 
#iCLeanPhotoFlag == 0 && gCLeanPhotoFlag == 0 && rCLeanPhotoFlag == 0 && zCLeanPhotoFlag == 0 && yCLeanPhotoFlag == 0 
#iCLeanPhotoFlag == 1 || gCLeanPhotoFlag == 1 || rCLeanPhotoFlag == 1 || zCLeanPhotoFlag == 1 || yCLeanPhotoFlag == 1 

# gdetected_notjunk && rdetected_notjunk && idetected_notjunk && zdetected_notjunk && ydetected_notjunk
# !(gdetected_notjunk || rdetected_notjunk || idetected_notjunk || zdetected_notjunk || ydetected_notjunk)

#parent_id == 0 && !(gcmodel_flux_flags) && !(rcmodel_flux_flags) && !(icmodel_flux_flags) && !(zcmodel_flux_flags) && !(ycmodel_flux_flags)
#parent_id == 0 && !(gcentroid_sdss_flags) && !(rcentroid_sdss_flags) && !(icentroid_sdss_flags) && !(zcentroid_sdss_flags) && !(ycentroid_sdss_flags) 

#iCLeanPhotoFlag == 1 && gCLeanPhotoFlag == 1 && rCLeanPhotoFlag == 1 && zCLeanPhotoFlag == 1 && yCLeanPhotoFlag == 1
#iCLeanPhotoFlag == 1 || gCLeanPhotoFlag == 1 || rCLeanPhotoFlag == 1 || zCLeanPhotoFlag == 1 || yCLeanPhotoFlag == 1

# RA/DEC limits HSC image
# RA 242.63 242.98
# DEC 54.23 54.44

unalias stilts
