#!/bin/csh -f

#Informix 'dbaccess' script to subset from 
#2MASS Catalog Generation (Working) Database 
#tmass@rmt_levi:pt_src into tmasss@rmt_levi:pt_src

#The following source-by-source thresholds are to be used to **select**
#a source for inclusion in the release catalogs.  The implementation
#should be that a source passes into the catalog if it satisfies
#the conditions in all the subsections.
#
#Last Update - 10 March 99 (15:30)
#
#I. "Scan" SNR Threshold
#
#Source must be detected in at least one band and have a "scan" SNR > 7 in at 
#least one band:
#
#(j_prg_flg != 'fil' AND FJ/j_pts_noise >= 7.0) OR
#(h_prg_flg != 'fil' AND FH/h_pts_noise >= 7.0) OR
#(k_prg_flg != 'fil' AND FK/k_pts_noise >= 7.0
#
#where
#
#FJ = 1.603e6 * power(10.0,-(j_m)/2.5)
#FH = 1.075e6 * power(10.0,-(h_m)/2.5)
#FK = 6.981e5 * power(10.0,-(k_m)/2.5)
#
# note that we can now do this SNR calculation and threshold completely in 
# log space and remove all power() calculations.
#
#II. "Extraction" SNR Threshold
#
#Source must be detected in at least one band and have an "extraction" SNR > 7 
#in at least one band:
#
#(j_prg_flg != 'fil' AND j_msig <= 0.167) OR
#(h_prg_flg != 'fil' AND h_msig <= 0.167) OR
#(k_prg_flg != 'fil' AND k_msig <= 0.167)
#
#III. Artifacts
#
#If detected in a band, cannot be a glint (?_prg_flg[1] = '0'), 
#cannot be a diffraction spike "detection" (?_prg_flg[2] < '6'), and
#cannot be level 2 confused (?_prg_flg[3] < '2'):
#
#(j_prg_flg = 'fil' OR (j_prg_flg[1] = '0' AND j_prg_flg[2] < '6' AND j_prg_flg[3] < '2'))
#AND (h_prg_flg = 'fil' OR (h_prg_flg[1] = '0' AND h_prg_flg[2] < '6' AND h_prg_flg[3] < '2'))
#AND (k_prg_flg = 'fil' OR (k_prg_flg[1] = '0' AND k_prg_flg[2] < '6' AND k_prg_flg[3] < '2'))
#  
#IV. Persistence
#
#Must have <50% probability of persistence in all bands:
#
#(j_prob_pers < 0.5 AND h_prob_pers < 0.5 AND k_prob_pers < 0.5)
#
#V. Chi-Square
#
#If detections are R2 (?_mrg_flg[1] = '0'), must have chi-square < 10 
#in at least one band:
#
#(j_prg_flg != 'fil' AND j_mrg_flg[1] = '0' AND j_psf_chi < 10) OR
#(h_prg_flg != 'fil' AND h_mrg_flg[1] = '0' AND h_psf_chi < 10) OR
#(k_prg_flg != 'fil' AND k_mrg_flg[1] = '0' AND k_psf_chi < 10) 
#
#VI. N/M
#
#For high SNR sources, must have at least three useful frames and be 
#detected >40% of the time (N/M > 2/3, 2/4, 3/5 or 3/6) in at least one band:
#
#(
# ((j_prg_flg != 'fil') AND (j_m > 14.5)) OR 
#  (j_pix_flg[4] > 2 AND j_pix_flg[3]/j_pix_flg[4] > 0.41)
#) OR
#(
# ((h_prg_flg != 'fil') AND (h_m > 14.0)) OR 
#  (h_pix_flg[4] > 2 AND h_pix_flg[3]/h_pix_flg[4] > 0.41)
#) OR
#(
# ((k_prg_flg != 'fil') AND (k_m > 13.5)) OR 
#  (k_pix_flg[4] > 2 AND k_pix_flg[3]/k_pix_flg[4] > 0.41)
#)
#
#VII. other -- dist >= 10", in release list
#
#(rel_num > 0)
#AND
#((dist_edge_ns >= 10.0) AND (dist_edge_ew >= 10.0))
#


setenv INFORMIXDIR /usr/local/informix
setenv INFORMIXSERVER rmt_levi
setenv PDQPRIORITY 100

echo 'Starting at'
date
dbaccess - - << EOF
database tmasss@rmt_levi;
INSERT INTO pt_src_01
SELECT 
tmass:pt_src.cntr, 
tmass:pt_src.hemis, 
tmass:pt_src.date, 
tmass:pt_src.scan, 
tmass:pt_src.id, 
tmass:pt_src.ra, 
tmass:pt_src.dec, 
tmass:pt_src.glon, 
tmass:pt_src.glat, 
tmass:pt_src.x, 
tmass:pt_src.y, 
tmass:pt_src.z, 
tmass:pt_src.err_maj, 
tmass:pt_src.err_min, 
tmass:pt_src.err_ang, 
tmass:pt_src.x_scan, 
tmass:pt_src.y_scan, 
tmass:pt_src.cnf_flg, 
tmass:pt_src.j_m, 
tmass:pt_src.h_m, 
tmass:pt_src.k_m, 
tmass:pt_src.j_msig, 
tmass:pt_src.h_msig, 
tmass:pt_src.k_msig, 
tmass:pt_src.j_m_psf, 
tmass:pt_src.h_m_psf, 
tmass:pt_src.k_m_psf, 
tmass:pt_src.j_msig_psf, 
tmass:pt_src.h_msig_psf, 
tmass:pt_src.k_msig_psf, 
tmass:pt_src.j_psfchi, 
tmass:pt_src.h_psfchi, 
tmass:pt_src.k_psfchi, 
tmass:pt_src.j_psf_id, 
tmass:pt_src.h_psf_id, 
tmass:pt_src.k_psf_id, 
tmass:pt_src.j_skyval, 
tmass:pt_src.h_skyval, 
tmass:pt_src.k_skyval, 
tmass:pt_src.j_blend, 
tmass:pt_src.h_blend, 
tmass:pt_src.k_blend, 
tmass:pt_src.j_peakpix, 
tmass:pt_src.h_peakpix, 
tmass:pt_src.k_peakpix, 
tmass:pt_src.j_m_stdap, 
tmass:pt_src.h_m_stdap, 
tmass:pt_src.k_m_stdap, 
tmass:pt_src.j_msig_stdap, 
tmass:pt_src.h_msig_stdap, 
tmass:pt_src.k_msig_stdap, 
tmass:pt_src.j_m_lgap, 
tmass:pt_src.h_m_lgap, 
tmass:pt_src.k_m_lgap, 
tmass:pt_src.j_msig_lgap, 
tmass:pt_src.h_msig_lgap, 
tmass:pt_src.k_msig_lgap, 
tmass:pt_src.j_m_r1, 
tmass:pt_src.h_m_r1, 
tmass:pt_src.k_m_r1, 
tmass:pt_src.j_msig_r1, 
tmass:pt_src.h_msig_r1, 
tmass:pt_src.k_msig_r1, 
tmass:pt_src.j_prob_pers, 
tmass:pt_src.h_prob_pers, 
tmass:pt_src.k_prob_pers, 
tmass:pt_src.j_prnt_id, 
tmass:pt_src.h_prnt_id, 
tmass:pt_src.k_prnt_id, 
tmass:pt_src.j_prg_flg, 
tmass:pt_src.h_prg_flg, 
tmass:pt_src.k_prg_flg, 
tmass:pt_src.j_mrg_flg, 
tmass:pt_src.h_mrg_flg, 
tmass:pt_src.k_mrg_flg, 
tmass:pt_src.j_pix_flg, 
tmass:pt_src.h_pix_flg, 
tmass:pt_src.k_pix_flg, 
tmass:pt_src.j_cal, 
tmass:pt_src.h_cal, 
tmass:pt_src.k_cal, 
tmass:pt_src.gal_contam, 
tmass:pt_src.dist_edge_ns, 
tmass:pt_src.dist_edge_ew, 
tmass:pt_src.dist_edge_flg, 
tmass:pt_src.id_opt, 
tmass:pt_src.dist_opt, 
tmass:pt_src.phi_opt, 
tmass:pt_src.b_m_opt, 
tmass:pt_src.r_m_opt, 
tmass:pt_src.nopt_mchs, 
tmass:pt_src.n2mass_mchs, 
tmass:pt_src.j_h, 
tmass:pt_src.j_h_sig, 
tmass:pt_src.h_k, 
tmass:pt_src.h_k_sig, 
tmass:pt_src.j_k, 
tmass:pt_src.j_k_sig, 
tmass:pt_src.b_r, 
tmass:pt_src.r_k, 
tmass:pt_src.b_k, 
tmass:pt_src.r_j, 
tmass:pt_src.b_j, 
tmass:pt_src.dup_src, 
tmass:pt_src.use_src, 
tmass:pt_src.ext_key_1, 
tmass:pt_src.ext_key_2, 
tmass:pt_src.mp_key, 
tmass:pt_src.exbr_key, 
tmass:pt_src.night_key, 
tmass:pt_src.scan_key, 
tmass:pt_src.spt_ind, 
tmass:pt_src.clr_ind 
FROM tmass:pt_src, scan_dat
WHERE 
tmass:pt_src.scan_key = scan_dat.cntr 
and
(
(j_prg_flg != 'fil' AND (((j_m/2.5) + j_pts_noise) <= 5.35984)) OR
(h_prg_flg != 'fil' AND (((h_m/2.5) + h_pts_noise) <= 5.18631)) OR
(k_prg_flg != 'fil' AND (((k_m/2.5) + k_pts_noise) <= 4.99882))
)
 AND
(
 (j_prg_flg != 'fil' AND j_msig <= 0.155) 
 OR
 (h_prg_flg != 'fil' AND h_msig <= 0.155) 
 OR
 (k_prg_flg != 'fil' AND k_msig <= 0.155)
)
 AND
(
 (j_prg_flg = 'fil' OR (j_prg_flg[1] = '0' AND j_prg_flg[2] < '6' AND j_prg_flg[3] < '2'))
 AND 
 (h_prg_flg = 'fil' OR (h_prg_flg[1] = '0' AND h_prg_flg[2] < '6' AND h_prg_flg[3] < '2'))
 AND 
 (k_prg_flg = 'fil' OR (k_prg_flg[1] = '0' AND k_prg_flg[2] < '6' AND k_prg_flg[3] < '2'))
) 
 AND
(
 (j_prob_pers < 0.5) AND (h_prob_pers < 0.5) AND (k_prob_pers < 0.5)
)
 AND
(
 (j_prg_flg != 'fil' AND j_mrg_flg[1] = '2') OR (j_prg_flg != 'fil' AND j_mrg_flg[1] = '0' AND j_psfchi < 10)
 OR
 (h_prg_flg != 'fil' AND h_mrg_flg[1] = '2') OR (h_prg_flg != 'fil' AND h_mrg_flg[1] = '0' AND h_psfchi < 10)
 OR
 (k_prg_flg != 'fil' AND k_mrg_flg[1] = '2') OR (k_prg_flg != 'fil' AND k_mrg_flg[1] = '0' AND k_psfchi < 10)
)
 AND
(
 (
  ((j_prg_flg != 'fil') AND (j_m > 14.5)) OR 
   (j_pix_flg[4] > 2 AND j_pix_flg[3]/j_pix_flg[4] > 0.41)
 ) OR
 (
  ((h_prg_flg != 'fil') AND (h_m > 14.0)) OR 
   (h_pix_flg[4] > 2 AND h_pix_flg[3]/h_pix_flg[4] > 0.41)
 ) OR
 (
  ((k_prg_flg != 'fil') AND (k_m > 13.5)) OR 
   (k_pix_flg[4] > 2 AND k_pix_flg[3]/k_pix_flg[4] > 0.41)
 )
)
AND
(rel_num > 0)
AND
((dist_edge_ns >= 10.0) AND (dist_edge_ew >= 10.0))
;
EOF
echo 'Finished at'
date

echo 'Done'
