CREATE TABLE wise_2 (ra float, dec float, cntr bigint, w1mpro float, w1sigmpro float, w1mag_1 float, w1sigmag_1 float, w1mag_2 float, 
  w1sigmag_2 float, w1mag_3 float, w1sigmag_3 float, w1mag_4 float, w1sigmag_4 float, w1mag_5 float, w1sigmag_5 float, 
  w1mag_6 float, w1sigmag_6 float, w1mag_7 float, w1sigmag_7 float, w1mag_8 float, w1sigmag_8 float, w2mpro float, w2sigmpro float,
  w2mag_1 float, w2sigmag_1 float, w2mag_2 float, w2sigmag_2 float, w2mag_3 float, w2sigmag_3 float, w2mag_4 float, w2sigmag_4 float, 
  w2mag_5 float, w2sigmag_5 float, w2mag_6 float, w2sigmag_6 float, w2mag_7 float, w2sigmag_7 float, w2mag_8 float, w2sigmag_8 float, 
  w3mpro float, w3sigmpro float, w3mag_1 float, w3sigmag_1 float, w3mag_2 float, w3sigmag_2 float, w3mag_3 float, w3sigmag_3 float, 
  w3mag_4 float, w3sigmag_4 float, w3mag_5 float, w3sigmag_5 float, w3mag_6 float, w3sigmag_6 float, w3mag_7 float, w3sigmag_7 float, 
  w3mag_8 float, w3sigmag_8 float, w4mpro float, w4sigmpro float, w4mag_1 float, w4sigmag_1 float, w4mag_2 float, w4sigmag_2 float,
  w4mag_3 float, w4sigmag_3 float, w4mag_4 float, w4sigmag_4 float, w4mag_5 float, w4sigmag_5 float, w4mag_6 float, w4sigmag_6 float,
  w4mag_7 float, w4sigmag_7 float, w4mag_8 float, w4sigmag_8 float);
  
  \copy wise FROM /data1/stripe82/stripe82_wise.csv WITH CSV HEADER;
  
  CREATE TABLE vhs (sourceid bigint, ra float, dec float, ypetroMag float, ypetromagerr float, yapermag3 float, yapermag3err float, yapermag4 float, yapermag4err float, yapermag6 float, yapermag6err float, jpetroMag float, jpetromagerr float, japermag3 float, japermag3err float, japermag4 float, japermag4err float, japermag6 float, japermag6err float, hpetroMag float, hpetromagerr float, hapermag3 float, hapermag3err float, hapermag4 float, hapermag4err float, hapermag6 float, hapermag6err float, kspetroMag float, kspetromagerr float, ksapermag3 float, ksapermag3err float, ksapermag4 float, ksapermag4err float, ksapermag6 float, ksapermag6err float);
  
\copy vhs FROM /data1/stripe82/stripe82_vhs.csv WITH CSV HEADER;
  
  CREATE TABLE galex (ra float, dec float, objid bigint, NUV_MAG_AUTO float, NUV_MAGERR_AUTO float, NUV_KRON_RADIUS float, FUV_MAG_AUTO float, FUV_MAGERR_AUTO float, FUV_KRON_RADIUS float, NUV_MAG_APER_1 float, NUV_MAGERR_APER_1 float, NUV_MAG_APER_2 float, NUV_MAGERR_APER_2 float, NUV_MAG_APER_3 float, NUV_MAGERR_APER_3 float, NUV_MAG_APER_4 float, NUV_MAGERR_APER_4 float, NUV_MAG_APER_5 float, NUV_MAGERR_APER_5 float, NUV_MAG_APER_6 float, NUV_MAGERR_APER_6 float, NUV_MAG_APER_7 float, NUV_MAGERR_APER_7 float, FUV_MAG_APER_1 float, FUV_MAGERR_APER_1 float, FUV_MAG_APER_2 float, FUV_MAGERR_APER_2 float, FUV_MAG_APER_3 float, FUV_MAGERR_APER_3 float, FUV_MAG_APER_4 float, FUV_MAGERR_APER_4 float, FUV_MAG_APER_5 float, FUV_MAGERR_APER_5 float, FUV_MAG_APER_6 float, FUV_MAGERR_APER_6 float, FUV_MAG_APER_7 float, FUV_MAGERR_APER_7 float);
  
  \copy galex FROM /data1/stripe82/stripe82_galex.csv WITH CSV HEADER;
  
  \copy ls8_tractor FROM /data1/stripe82/ls8_stripe82.csv WITH CSV HEADER;
  
  SELECT ra, dec, release, brickid, objid, flux_g, flux_r, flux_z, flux_w1, flux_w2, flux_w3, flux_w4, flux_ivar_g, flux_ivar_r, flux_ivar_z, flux_ivar_w1, flux_ivar_w2, flux_ivar_w3, flux_ivar_w4, apflux_g, apflux_r, apflux_z, apflux_ivar_g, apflux_ivar_r, apflux_ivar_z, shapeexp_r, shapeexp_r_ivar, shapeexp_e1, shapeexp_e1_ivar, shapeexp_e2, shapeexp_e2_ivar, shapedev_r, shapedev_r_ivar, shapedev_e1, shapedev_e1_ivar, shapedev_e2, shapedev_e2_ivar, type, psfsize_g, psfsize_r, psfsize_z, ref_cat, ref_id, gaia_pointsource, gaia_phot_g_mean_mag, gaia_phot_g_mean_flux_over_error, gaia_phot_g_n_obs, gaia_phot_bp_mean_mag, gaia_phot_bp_mean_flux_over_error, gaia_phot_bp_n_obs, gaia_phot_rp_mean_mag, gaia_phot_rp_mean_flux_over_error, gaia_phot_rp_n_obs, gaia_phot_variable_flag, gaia_astrometric_excess_noise, gaia_astrometric_excess_noise_sig, gaia_astrometric_n_obs_al, gaia_astrometric_n_good_obs_al, pmra, pmdec, parallax, pmra_ivar, pmdec_ivar, parallax_ivar, spoint(ra*pi()/180.0, dec*pi()/180.0) AS crd2000, scircle(spoint(ra*pi()/180.0, dec*pi()/180.0), 1./206265.) AS circle, scircle(spoint(ra*pi()/180.0, dec*pi()/180.0), 3./206265.) AS circle3 INTO TABLE ls8_scircle FROM ls8_tractor;
  
  ALTER TABLE wise ADD COLUMN crd2000 spoint;
  UPDATE wise SET crd2000=spoint(ra*pi()/180.0, dec*pi()/180.0);
  
  ALTER TABLE galex ADD COLUMN crd2000 spoint;
  UPDATE galex SET crd2000=spoint(ra*pi()/180.0, dec*pi()/180.0);
  
  ALTER TABLE vhs ADD COLUMN crd2000 spoint;
  UPDATE vhs SET crd2000=spoint(ra*pi()/180.0, dec*pi()/180.0);
  
  VACUUM FULL ls8_scircle;        
CREATE INDEX sindex ON ls8_scircle using gist (circle);     
VACUUM ANALYZE ls8_scircle;

ALTER TABLE wise RENAME ra TO ra_wise;
ALTER TABLE wise RENAME dec TO dec_wise;


SELECT a.ra, a.dec, a.release, a.brickid, a.objid, b.* INTO ls8_wise FROM ls8_scircle AS a, wise AS b WHERE b.crd2000 @ a.circle;

ALTER TABLE galex RENAME ra TO ra_galex;
ALTER TABLE galex RENAME dec TO dec_galex;
ALTER TABLE galex RENAME objid TO objid_galex;
SELECT a.ra, a.dec, a.release, a.brickid, a.objid, b.* INTO ls8_galex FROM ls8_scircle AS a, galex AS b WHERE b.crd2000 @ a.circle;

ALTER TABLE vhs RENAME ra TO ra_vhs;
ALTER TABLE vhs RENAME dec TO dec_vhs;
ALTER TABLE vhs ADD COLUMN ra_deg real;
ALTER TABLE vhs ADD COLUMN dec_deg real; 
UPDATE vhs SET ra_deg=ra_vhs/pi()*180;
UPDATE vhs SET dec_deg=dec_vhs/pi()*180;
  ALTER TABLE vhs ADD COLUMN crd2000 spoint;
  UPDATE vhs SET crd2000=spoint(ra_deg*pi()/180.0, dec_deg*pi()/180.0);

SELECT a.ra, a.dec, a.release, a.brickid, a.objid, b.* INTO ls8_vhs FROM ls8_scircle AS a, vhs AS b WHERE b.crd2000 @ a.circle;


SELECT ra, dec, 22.5-2.5*log(flux_w1) AS w1_ls, 22.5-2.5*log(flux_w2) AS w2_ls, 22.5-2.5*log(flux_w3) AS w3_ls, 22.5-2.5*log(flux_w4) AS w4_ls, w1mpro+2.699 AS w1_wise, w2mpro+3.339 AS w2_wise, w3mpro+5.174 AS w3_wise, w4mpro+6.620 AS w4_wise FROM ls8_wise WHERE flux_w1>0 AND flux_w2>0 AND flux_w3>0 AND flux_w4>0 limit 100;

CREATE INDEX objindex2 ON ls8_scircle (release, brickid, objid); 
CREATE INDEX objindex3 ON ls8_vhs (release, brickid, objid);     
CREATE INDEX objindex4 ON ls8_wise (release, brickid, objid);     
CREATE INDEX objindex5 ON ls8_galex (release, brickid, objid);  

CREATE INDEX sindex2 ON galex using gist (crd2000);     
CREATE INDEX sindex3 ON wise using gist (crd2000);     
CREATE INDEX sindex4 ON vhs using gist (crd2000);   

SELECT ra, dec, release, brickid, objid, flux_g, flux_r, flux_z, flux_w1, flux_w2, flux_w3, flux_w4, flux_ivar_g, flux_ivar_r, flux_ivar_z, flux_ivar_w1, flux_ivar_w2, flux_ivar_w3, flux_ivar_w4, apflux_g, apflux_r, apflux_z, apflux_ivar_g, apflux_ivar_r, apflux_ivar_z, shapeexp_r, shapeexp_r_ivar, shapeexp_e1, shapeexp_e1_ivar, shapeexp_e2, shapeexp_e2_ivar, shapedev_r, shapedev_r_ivar, shapedev_e1, shapedev_e1_ivar, shapedev_e2, shapedev_e2_ivar, type, psfsize_g, psfsize_r, psfsize_z, ref_cat, ref_id, gaia_pointsource, gaia_phot_g_mean_mag, gaia_phot_g_mean_flux_over_error, gaia_phot_g_n_obs, gaia_phot_bp_mean_mag, gaia_phot_bp_mean_flux_over_error, gaia_phot_bp_n_obs, gaia_phot_rp_mean_mag, gaia_phot_rp_mean_flux_over_error, gaia_phot_rp_n_obs, gaia_phot_variable_flag, gaia_astrometric_excess_noise, gaia_astrometric_excess_noise_sig, gaia_astrometric_n_obs_al, gaia_astrometric_n_good_obs_al, pmra, pmdec, parallax, pmra_ivar, pmdec_ivar, parallax_ivar, spoint(ra*pi()/180.0, dec*pi()/180.0) AS crd2000, scircle(spoint(ra*pi()/180.0, dec*pi()/180.0), 1./206265.) AS circle, scircle(spoint(ra*pi()/180.0, dec*pi()/180.0), 3./206265.) AS circle3 INTO TABLE ls8_scircle FROM ls8_tractor;
CREATE INDEX sindex ON ls8_scircle using gist (circle);     
 

SELECT a.ra, a.dec, a.release, a.brickid, a.objid, b.*, a.crd2000 <-> b.crd2000 AS dist INTO ls8_wise FROM ls8_scircle AS a, wise AS b WHERE b.crd2000 @ a.circle;  
SELECT a.ra, a.dec, a.release, a.brickid, a.objid, b.*, a.crd2000 <-> b.crd2000 AS dist INTO ls8_galex FROM ls8_scircle AS a, galex AS b WHERE b.crd2000 @ a.circle; 
SELECT a.ra, a.dec, a.release, a.brickid, a.objid, b.*, a.crd2000 <-> b.crd2000 AS dist INTO ls8_vhs FROM ls8_scircle AS a, vhs AS b WHERE b.crd2000 @ a.circle;

DELETE FROM ls8_wise T1
    USING   ls8_wise T2
WHERE  T1.dist > T2.dist  
    AND T1.release  = T2.release AND T1.brickid=T2.brickid AND T1.objid=T2.objid;  
    
DELETE FROM ls8_galex T1
    USING   ls8_galex T2
WHERE  T1.dist > T2.dist  
    AND T1.release  = T2.release AND T1.brickid=T2.brickid AND T1.objid=T2.objid;  

DELETE FROM ls8_vhs T1
    USING   ls8_vhs T2
WHERE  T1.dist > T2.dist  
    AND T1.release  = T2.release AND T1.brickid=T2.brickid AND T1.objid=T2.objid;          
  
CREATE INDEX rindex2 ON ls8_scircle (release); 
CREATE INDEX rindex3 ON ls8_vhs (release);     
CREATE INDEX rindex4 ON ls8_wise (release);     
CREATE INDEX rindex5 ON ls8_galex (release); 
CREATE INDEX bindex2 ON ls8_scircle (brickid); 
CREATE INDEX bindex3 ON ls8_vhs (brickid);     
CREATE INDEX bindex4 ON ls8_wise (brickid);     
CREATE INDEX bindex5 ON ls8_galex (brickid); 
CREATE INDEX oindex2 ON ls8_scircle (objid); 
CREATE INDEX oindex3 ON ls8_vhs (objid);     
CREATE INDEX oindex4 ON ls8_wise (objid);     
CREATE INDEX oindex5 ON ls8_galex (objid); 

SELECT a.*,  b.sourceid, b.ra_vhs, b.dec_vhs, b.ypetroMag, b.ypetromagerr, b.yapermag3, b.yapermag3err, b.yapermag4, b.yapermag4err, b.yapermag6, b.yapermag6err, b.jpetroMag, b.jpetromagerr, b.japermag3, b.japermag3err, b.japermag4, b.japermag4err, b.japermag6, b.japermag6err, b.hpetroMag, b.hpetromagerr, b.hapermag3, b.hapermag3err, b.hapermag4, b.hapermag4err, b.hapermag6, b.hapermag6err, b.kspetroMag, b.kspetromagerr, b.ksapermag3, b.ksapermag3err, b.ksapermag4, b.ksapermag4err, b.ksapermag6, b.ksapermag6err, c.ra_wise, c.dec_wise, c.cntr, w1mpro, c.w1sigmpro, c.w1mag_1, c.w1sigmag_1, c.w1mag_2, c.w1sigmag_2, c.w1mag_3, c.w1sigmag_3, c.w1mag_4, c.w1sigmag_4, c.w1mag_5, c.w1sigmag_5, c.w1mag_6, c.w1sigmag_6, c.w1mag_7, c.w1sigmag_7, c.w1mag_8, c.w1sigmag_8, c.w2mpro, c.w2sigmpro, c.w2mag_1, c.w2sigmag_1, c.w2mag_2, c.w2sigmag_2, c.w2mag_3, c.w2sigmag_3, c.w2mag_4, c.w2sigmag_4, c.w2mag_5, c.w2sigmag_5, c.w2mag_6, c.w2sigmag_6, c.w2mag_7, c.w2sigmag_7, c.w2mag_8, c.w2sigmag_8, c.w3mpro, c.w3sigmpro, c.w3mag_1, c.w3sigmag_1, c.w3mag_2, c.w3sigmag_2, c.w3mag_3, c.w3sigmag_3, c.w3mag_4, c.w3sigmag_4, c.w3mag_5, c.w3sigmag_5, c.w3mag_6, c.w3sigmag_6, c.w3mag_7, c.w3sigmag_7, c.w3mag_8, c.w3sigmag_8, c.w4mpro, c.w4sigmpro, c.w4mag_1, c.w4sigmag_1, c.w4mag_2, c.w4sigmag_2, c.w4mag_3, c.w4sigmag_3, c.w4mag_4, c.w4sigmag_4, c.w4mag_5, c.w4sigmag_5, c.w4mag_6, c.w4sigmag_6, c.w4mag_7, c.w4sigmag_7, c.w4mag_8, c.w4sigmag_8, d.ra_galex, d.dec_galex, d.objid_galex, d.NUV_MAG_AUTO, d.NUV_MAGERR_AUTO, d.NUV_KRON_RADIUS, d.FUV_MAG_AUTO, d.FUV_MAGERR_AUTO, d.FUV_KRON_RADIUS, d.NUV_MAG_APER_1, d.NUV_MAGERR_APER_1, d.NUV_MAG_APER_2, d.NUV_MAGERR_APER_2, d.NUV_MAG_APER_3, d.NUV_MAGERR_APER_3, d.NUV_MAG_APER_4, d.NUV_MAGERR_APER_4, d.NUV_MAG_APER_5, d.NUV_MAGERR_APER_5, d.NUV_MAG_APER_6, d.NUV_MAGERR_APER_6, d.NUV_MAG_APER_7, d.NUV_MAGERR_APER_7, d.FUV_MAG_APER_1, d.FUV_MAGERR_APER_1, d.FUV_MAG_APER_2, d.FUV_MAGERR_APER_2, d.FUV_MAG_APER_3, d.FUV_MAGERR_APER_3, d.FUV_MAG_APER_4, d.FUV_MAGERR_APER_4, d.FUV_MAG_APER_5, d.FUV_MAGERR_APER_5, d.FUV_MAG_APER_6, d.FUV_MAGERR_APER_6, d.FUV_MAG_APER_7, d.FUV_MAGERR_APER_7 INTO full_phot_table FROM ls8_scircle AS a LEFT OUTER JOIN ls8_vhs AS b ON a.objid=b.objid AND a.release=b.release AND a.brickid=b.brickid LEFT OUTER JOIN ls8_wise AS c ON a.objid=c.objid AND a.release=c.release AND a.brickid=c.brickid LEFT OUTER JOIN ls8_galex AS d ON a.objid=d.objid AND a.release=d.release AND a.brickid=d.brickid;  



SELECT a.*,  b.sourceid, b.ra_vhs, b.dec_vhs, b.ypetroMag, b.ypetromagerr, b.yapermag3, b.yapermag3err, b.yapermag4, b.yapermag4err, b.yapermag6, b.yapermag6err, b.jpetroMag, b.jpetromagerr, b.japermag3, b.japermag3err, b.japermag4, b.japermag4err, b.japermag6, b.japermag6err, b.hpetroMag, b.hpetromagerr, b.hapermag3, b.hapermag3err, b.hapermag4, b.hapermag4err, b.hapermag6, b.hapermag6err, b.kspetroMag, b.kspetromagerr, b.ksapermag3, b.ksapermag3err, b.ksapermag4, b.ksapermag4err, b.ksapermag6, b.ksapermag6err INTO phot_decals_vhs FROM ls8_scircle AS a LEFT OUTER JOIN ls8_vhs AS b ON a.objid=b.objid;

EXPLAIN ANALYSE SELECT a.*,  b.sourceid, b.ra_vhs, b.dec_vhs, b.ypetroMag, b.ypetromagerr, b.yapermag3, b.yapermag3err, b.yapermag4, b.yapermag4err, b.yapermag6, b.yapermag6err, b.jpetroMag, b.jpetromagerr, b.japermag3, b.japermag3err, b.japermag4, b.japermag4err, b.japermag6, b.japermag6err, b.hpetroMag, b.hpetromagerr, b.hapermag3, b.hapermag3err, b.hapermag4, b.hapermag4err, b.hapermag6, b.hapermag6err, b.kspetroMag, b.kspetromagerr, b.ksapermag3, b.ksapermag3err, b.ksapermag4, b.ksapermag4err, b.ksapermag6, b.ksapermag6err FROM ls8_scircle AS a LEFT OUTER JOIN ls8_vhs AS b ON a.objid=b.objid limit 1000000;













SELECT * INTO full_phot_edited FROM full_phot_table ;

UPDATE full_phot_edited SET flux_g=null WHERE flux_g <= 0;
UPDATE full_phot_edited SET flux_r=null WHERE flux_r <= 0;
UPDATE full_phot_edited SET flux_z=null WHERE flux_z <= 0;
UPDATE full_phot_edited SET flux_w1=null WHERE flux_w1 <= 0;
UPDATE full_phot_edited SET flux_w2=null WHERE flux_w2 <= 0;
UPDATE full_phot_edited SET flux_w3=null WHERE flux_w3 <= 0;
UPDATE full_phot_edited SET flux_w4=null WHERE flux_w4 <= 0;
UPDATE full_phot_edited SET apflux_g[0]=null WHERE apflux_g[0] <= 0;
UPDATE full_phot_edited SET apflux_r[0]=null WHERE apflux_r[0] <= 0;
UPDATE full_phot_edited SET apflux_z[0]=null WHERE apflux_z[0] <= 0;
UPDATE full_phot_edited SET apflux_g[1]=null WHERE apflux_g[1] <= 0;
UPDATE full_phot_edited SET apflux_r[1]=null WHERE apflux_r[1] <= 0;
UPDATE full_phot_edited SET apflux_z[1]=null WHERE apflux_z[1] <= 0;
UPDATE full_phot_edited SET apflux_g[2]=null WHERE apflux_g[2] <= 0;
UPDATE full_phot_edited SET apflux_r[2]=null WHERE apflux_r[2] <= 0;
UPDATE full_phot_edited SET apflux_z[2]=null WHERE apflux_z[2] <= 0;
UPDATE full_phot_edited SET apflux_g[3]=null WHERE apflux_g[3] <= 0;
UPDATE full_phot_edited SET apflux_r[3]=null WHERE apflux_r[3] <= 0;
UPDATE full_phot_edited SET apflux_z[3]=null WHERE apflux_z[3] <= 0;
UPDATE full_phot_edited SET apflux_g[4]=null WHERE apflux_g[4] <= 0;
UPDATE full_phot_edited SET apflux_r[4]=null WHERE apflux_r[4] <= 0;
UPDATE full_phot_edited SET apflux_z[4]=null WHERE apflux_z[4] <= 0;
UPDATE full_phot_edited SET apflux_g[5]=null WHERE apflux_g[5] <= 0;
UPDATE full_phot_edited SET apflux_r[5]=null WHERE apflux_r[5] <= 0;
UPDATE full_phot_edited SET apflux_z[5]=null WHERE apflux_z[5] <= 0;
UPDATE full_phot_edited SET apflux_g[6]=null WHERE apflux_g[6] <= 0;
UPDATE full_phot_edited SET apflux_r[6]=null WHERE apflux_r[6] <= 0;
UPDATE full_phot_edited SET apflux_z[6]=null WHERE apflux_z[6] <= 0;
UPDATE full_phot_edited SET apflux_g[7]=null WHERE apflux_g[7] <= 0;
UPDATE full_phot_edited SET apflux_r[7]=null WHERE apflux_r[7] <= 0;
UPDATE full_phot_edited SET apflux_z[7]=null WHERE apflux_z[7] <= 0;

UPDATE full_phot_edited SET flux_ivar_g=null WHERE flux_ivar_g = 0;
UPDATE full_phot_edited SET flux_ivar_r=null WHERE flux_ivar_r = 0;
UPDATE full_phot_edited SET flux_ivar_z=null WHERE flux_ivar_z = 0;
UPDATE full_phot_edited SET flux_ivar_w1=null WHERE flux_ivar_w1 = 0;
UPDATE full_phot_edited SET flux_ivar_w2=null WHERE flux_ivar_w2 = 0;
UPDATE full_phot_edited SET flux_ivar_w3=null WHERE flux_ivar_w3 = 0;
UPDATE full_phot_edited SET flux_ivar_w4=null WHERE flux_ivar_w4 = 0;
UPDATE full_phot_edited SET apflux_ivar_g[0]=null WHERE apflux_ivar_g[0] = 0;
UPDATE full_phot_edited SET apflux_ivar_r[0]=null WHERE apflux_ivar_r[0] = 0;
UPDATE full_phot_edited SET apflux_ivar_z[0]=null WHERE apflux_ivar_z[0] = 0;
UPDATE full_phot_edited SET apflux_ivar_g[1]=null WHERE apflux_ivar_g[1] = 0;
UPDATE full_phot_edited SET apflux_ivar_r[1]=null WHERE apflux_ivar_r[1] = 0;
UPDATE full_phot_edited SET apflux_ivar_z[1]=null WHERE apflux_ivar_z[1] = 0;
UPDATE full_phot_edited SET apflux_ivar_g[2]=null WHERE apflux_ivar_g[2] = 0;
UPDATE full_phot_edited SET apflux_ivar_r[2]=null WHERE apflux_ivar_r[2] = 0;
UPDATE full_phot_edited SET apflux_ivar_z[2]=null WHERE apflux_ivar_z[2] = 0;
UPDATE full_phot_edited SET apflux_ivar_g[3]=null WHERE apflux_ivar_g[3] = 0;
UPDATE full_phot_edited SET apflux_ivar_r[3]=null WHERE apflux_ivar_r[3] = 0;
UPDATE full_phot_edited SET apflux_ivar_z[3]=null WHERE apflux_ivar_z[3] = 0;
UPDATE full_phot_edited SET apflux_ivar_g[4]=null WHERE apflux_ivar_g[4] = 0;
UPDATE full_phot_edited SET apflux_ivar_r[4]=null WHERE apflux_ivar_r[4] = 0;
UPDATE full_phot_edited SET apflux_ivar_z[4]=null WHERE apflux_ivar_z[4] = 0;
UPDATE full_phot_edited SET apflux_ivar_g[5]=null WHERE apflux_ivar_g[5] = 0;
UPDATE full_phot_edited SET apflux_ivar_r[5]=null WHERE apflux_ivar_r[5] = 0;
UPDATE full_phot_edited SET apflux_ivar_z[5]=null WHERE apflux_ivar_z[5] = 0;
UPDATE full_phot_edited SET apflux_ivar_g[6]=null WHERE apflux_ivar_g[6] = 0;
UPDATE full_phot_edited SET apflux_ivar_r[6]=null WHERE apflux_ivar_r[6] = 0;
UPDATE full_phot_edited SET apflux_ivar_z[6]=null WHERE apflux_ivar_z[6] = 0;
UPDATE full_phot_edited SET apflux_ivar_g[7]=null WHERE apflux_ivar_g[7] = 0;
UPDATE full_phot_edited SET apflux_ivar_r[7]=null WHERE apflux_ivar_r[7] = 0;
UPDATE full_phot_edited SET apflux_ivar_z[7]=null WHERE apflux_ivar_z[7] = 0;   
  

\COPY (SELECT * FROM full_phot_calibrate) TO /data1/stripe82/full_phot_calibrate.csv WITH CSV HEADER; 

\COPY (SELECT * FROM full_phot_calibrated WHERE ra >= 300) TO /data1/stripe82/full_phot_calibrated1.csv WITH CSV HEADER;  

\COPY (SELECT * FROM full_phot_calibrated WHERE ra <= 30) TO /data1/stripe82/full_phot_calibrated2.csv WITH CSV HEADER; 

\COPY (SELECT * FROM full_phot_calibrated WHERE ra > 30 AND ra <=60) TO /data1/stripe82/full_phot_calibrated3.csv WITH CSV HEADER; 

\COPY (SELECT release, brickid, objid, intmag_g_decals, intmag_r_decals, intmag_z_decals, intmag_w1_decals, intmag_w2_decals, intmag_w3_decals, intmag_w4_decals, intmagerr_g_decals, intmagerr_r_decals, intmagerr_z_decals, intmagerr_w1_decals, intmagerr_w2_decals, intmagerr_w3_decals, intmagerr_w4_decals, apmag1_g_decals, apmag1_r_decals, apmag1_z_decals, apmag1err_g_decals, apmag1err_r_decals, apmag1err_z_decals, apmag2_g_decals, apmag2_r_decals, apmag2_z_decals, apmag2err_g_decals, apmag2err_r_decals, apmag2err_z_decals, apmag3_g_decals, apmag3_r_decals, apmag3_z_decals, apmag3err_g_decals, apmag3err_r_decals, apmag3err_z_decals, apmag4_g_decals, apmag4_r_decals, apmag4_z_decals, apmag4err_g_decals, apmag4err_r_decals, apmag4err_z_decals, apmag5_g_decals, apmag5_r_decals, apmag5_z_decals, apmag5err_g_decals, apmag5err_r_decals, apmag5err_z_decals, apmag6_g_decals,  apmag6_r_decals, apmag6_z_decals, apmag6err_g_decals, apmag6err_r_decals, apmag6err_z_decals, apmag7_g_decals, apmag7_r_decals, apmag7_z_decals, apmag7err_g_decals, apmag7err_r_decals, apmag7err_z_decals, apmag8_g_decals, apmag8_r_decals, apmag8_z_decals, apmag8err_g_decals, apmag8err_r_decals, apmag8err_z_decals FROM full_phot_calibrated) TO /data1/stripe82/right_decals_calb.csv WITH CSV HEADER; 




















