CREATE TABLE rcsed_phot (phot_index int, ra float, dec float, petror50_u_sdss float, petror50_g_sdss float, petror50_r_sdss float, petror50_i_sdss float, petror50_z_sdss float, petromag_u_sdss float, petromag_g_sdss float, petromag_r_sdss float, petromag_i_sdss float, petromag_z_sdss float, petromagerr_u_sdss float, petromagerr_g_sdss float, petromagerr_r_sdss float, petromagerr_i_sdss float, petromagerr_z_sdss float, petromagerr_g_des float, petromagerr_r_des float, petromagerr_i_des float, petromagerr_z_des float, petromagerr_y_des float, petromag_g_des float, petromag_r_des float, petromag_i_des float, petromag_z_des float, petromag_y_des float, petrorad_g_des float, petrorad_r_des float, petrorad_i_des float, petrorad_z_des float, petrorad_y_des float, petromag_J_uhs float, petromagerr_J_uhs float, petromag_Y_vhs float, petromagerr_Y_vhs float, petromag_J_vhs float, petromagerr_J_vhs float, petromag_H_vhs float, petromagerr_H_vhs float, petromag_KS_vhs float, petromagerr_KS_vhs float, petromag_Z_viking float, petromagerr_Z_viking float, petromag_Y_viking float, petromagerr_Y_viking float, petromag_J_viking float, petromagerr_J_viking float, petromag_H_viking float, petromagerr_H_viking float, petromag_KS_viking float, petromagerr_KS_viking float, petrorad_skymapper float, petromag_u_skymapper float, petromagerr_u_skymapper float, petromag_v_skymapper float, petromagerr_v_skymapper float, petromag_g_skymapper float, petromagerr_g_skymapper float, petromag_r_skymapper float, petromagerr_r_skymapper float, petromag_i_skymapper float, petromagerr_i_skymapper float, petromag_z_skymapper float, petromag_U_vst float, petromagerr_U_vst float, petromag_G_vst float, petromagerr_G_vst float, petromag_R_vst float, petromagerr_R_vst float, petromag_I_vst float, petromagerr_I_vst float, petromag_Z_vst float, petromagerr_Z_vst float, integmag_w1_unwise float, integmagerr_w1_unwise float, integmag_w2_unwise float, integmagerr_w2_unwise float, integmag_g_legacy float, integmag_r_legacy float, integmag_z_legacy float, kronmag_g_panstarrs float, kronmagerr_g_panstarrs float, kronmag_r_panstarrs float, kronmagerr_r_panstarrs float, kronmag_i_panstarrs float, kronmagerr_i_panstarrs float, kronmag_z_panstarrs float, kronmagerr_z_panstarrs float, kronmag_y_panstarrs float, kronmagerr_y_panstarrs float, kronmag_NUV_galex float, kronmagerr_NUV_galex float, kronmag_FUV_galex float, kronmagerr_FUV_galex float, kronrad_kids float, kronmag_u_kids float, kronmagerr_u_kids float, kronmag_g_kids float, kronmagerr_g_kids float, kronmag_r_kids float, kronmagerr_r_kids float, kronmag_i_kids float, kronmagerr_i_kids float, aper1mag_Y_ukidss float, aper1magerr_Y_ukidss float, aper2mag_Y_ukidss float, aper2magerr_Y_ukidss float, aper3mag_Y_ukidss float, aper3magerr_Y_ukidss float, aper4mag_Y_ukidss float, aper4magerr_Y_ukidss float, aper6mag_Y_ukidss float, aper6magerr_Y_ukidss float, aper1mag_J_ukidss float, aper1magerr_J_ukidss float, aper2mag_J_ukidss float, aper2magerr_J_ukidss float, aper3mag_J_ukidss float, aper3magerr_J_ukidss float, aper4mag_J_ukidss float, aper4magerr_J_ukidss float, aper6mag_J_ukidss float, aper6magerr_J_ukidss float, aper1mag_H_ukidss float, aper1magerr_H_ukidss float, aper2mag_H_ukidss float, aper2magerr_H_ukidss float, aper3mag_H_ukidss float, aper3magerr_H_ukidss float, aper4mag_H_ukidss float, aper4magerr_H_ukidss float, aper6mag_H_ukidss float, aper6magerr_H_ukidss float, aper1mag_K_ukidss float, aper1magerr_K_ukidss float, aper2mag_K_ukidss float, aper2magerr_K_ukidss float, aper3mag_K_ukidss float, aper3magerr_K_ukidss float, aper4mag_K_ukidss float, aper4magerr_K_ukidss float, aper6mag_K_ukidss float, aper6magerr_K_ukidss float, aper1mag_J_uhs float, aper1magerr_J_uhs float, aper2mag_J_uhs float, aper2magerr_J_uhs float, aper3mag_J_uhs float, aper3magerr_J_uhs float, aper4mag_J_uhs float, aper4magerr_J_uhs float, aper6mag_J_uhs float, aper6magerr_J_uhs float, aper1mag_u_sdss float, aper1magerr_u_sdss float, aper2mag_u_sdss float, aper2magerr_u_sdss float, aper3mag_u_sdss float, aper3magerr_u_sdss float, aper4mag_u_sdss float, aper4magerr_u_sdss float, aper6mag_u_sdss float, aper6magerr_u_sdss float, aper1mag_g_sdss float, aper1magerr_g_sdss float, aper2mag_g_sdss float, aper2magerr_g_sdss float, aper3mag_g_sdss float, aper3magerr_g_sdss float, aper4mag_g_sdss float, aper4magerr_g_sdss float, aper6mag_g_sdss float, aper6magerr_g_sdss float, aper1mag_r_sdss float, aper1magerr_r_sdss float, aper2mag_r_sdss float, aper2magerr_r_sdss float, aper3mag_r_sdss float, aper3magerr_r_sdss float, aper4mag_r_sdss float, aper4magerr_r_sdss float, aper6mag_r_sdss float, aper6magerr_r_sdss float, aper1mag_i_sdss float, aper1magerr_i_sdss float, aper2mag_i_sdss float, aper2magerr_i_sdss float, aper3mag_i_sdss float, aper3magerr_i_sdss float, aper4mag_i_sdss float, aper4magerr_i_sdss float, aper6mag_i_sdss float, aper6magerr_i_sdss float, aper1mag_z_sdss float, aper1magerr_z_sdss float, aper2mag_z_sdss float, aper2magerr_z_sdss float, aper3mag_z_sdss float, aper3magerr_z_sdss float, aper4mag_z_sdss float, aper4magerr_z_sdss float, aper6mag_z_sdss float, aper6magerr_z_sdss float, aper1mag_U_vst float, aper1magerr_U_vst float, aper2mag_U_vst float, aper2magerr_U_vst float, aper3mag_U_vst float, aper3magerr_U_vst float, aper4mag_U_vst float, aper4magerr_U_vst float, aper6mag_U_vst float, aper6magerr_U_vst float, aper1mag_G_vst float, aper1magerr_G_vst float, aper2mag_G_vst float, aper2magerr_G_vst float, aper3mag_G_vst float, aper3magerr_G_vst float, aper4mag_G_vst float, aper4magerr_G_vst float, aper6mag_G_vst float, aper6magerr_G_vst float, aper1mag_R_vst float, aper1magerr_R_vst float, aper2mag_R_vst float, aper2magerr_R_vst float, aper3mag_R_vst float, aper3magerr_R_vst float, aper4mag_R_vst float, aper4magerr_R_vst float, aper6mag_R_vst float, aper6magerr_R_vst float, aper1mag_I_vst float, aper1magerr_I_vst float, aper2mag_I_vst float, aper2magerr_I_vst float, aper3mag_I_vst float, aper3magerr_I_vst float, aper4mag_I_vst float, aper4magerr_I_vst float, aper6mag_I_vst float, aper6magerr_I_vst float, aper1mag_Z_vst float, aper1magerr_Z_vst float, aper2mag_Z_vst float, aper2magerr_Z_vst float, aper3mag_Z_vst float, aper3magerr_Z_vst float, aper4mag_Z_vst float, aper4magerr_Z_vst float, aper6mag_Z_vst float, aper6magerr_Z_vst float, aper1mag_Y_vhs float, aper1magerr_Y_vhs float, aper2mag_Y_vhs float, aper2magerr_Y_vhs float, aper3mag_Y_vhs float, aper3magerr_Y_vhs float, aper4mag_Y_vhs float, aper4magerr_Y_vhs float, aper6mag_Y_vhs float, aper6magerr_Y_vhs float, aper1mag_J_vhs float, aper1magerr_J_vhs float, aper2mag_J_vhs float, aper2magerr_J_vhs float, aper3mag_J_vhs float, aper3magerr_J_vhs float, aper4mag_J_vhs float, aper4magerr_J_vhs float, aper6mag_J_vhs float, aper6magerr_J_vhs float, aper1mag_H_vhs float, aper1magerr_H_vhs float, aper2mag_H_vhs float, aper2magerr_H_vhs float, aper3mag_H_vhs float, aper3magerr_H_vhs float, aper4mag_H_vhs float, aper4magerr_H_vhs float, aper6mag_H_vhs float, aper6magerr_H_vhs float, aper1mag_KS_vhs float, aper1magerr_KS_vhs float, aper2mag_KS_vhs float, aper2magerr_KS_vhs float, aper3mag_KS_vhs float, aper3magerr_KS_vhs float, aper4mag_KS_vhs float, aper4magerr_KS_vhs float, aper6mag_KS_vhs float, aper6magerr_KS_vhs float, aper1mag_Z_viking float, aper1magerr_Z_viking float, aper2mag_Z_viking float, aper2magerr_Z_viking float, aper3mag_Z_viking float, aper3magerr_Z_viking float, aper4mag_Z_viking float, aper4magerr_Z_viking float, aper6mag_Z_viking float, aper6magerr_Z_viking float, aper1mag_Y_viking float, aper1magerr_Y_viking float, aper2mag_Y_viking float, aper2magerr_Y_viking float, aper3mag_Y_viking float, aper3magerr_Y_viking float, aper4mag_Y_viking float, aper4magerr_Y_viking float, aper6mag_Y_viking float, aper6magerr_Y_viking float, aper1mag_J_viking float, aper1magerr_J_viking float, aper2mag_J_viking float, aper2magerr_J_viking float, aper3mag_J_viking float, aper3magerr_J_viking float, aper4mag_J_viking float, aper4magerr_J_viking float, aper6mag_J_viking float, aper6magerr_J_viking float, aper1mag_H_viking float, aper1magerr_H_viking float, aper2mag_H_viking float, aper2magerr_H_viking float, aper3mag_H_viking float, aper3magerr_H_viking float, aper4mag_H_viking float, aper4magerr_H_viking float, aper6mag_H_viking float, aper6magerr_H_viking float, aper1mag_KS_viking float, aper1magerr_KS_viking float, aper2mag_KS_viking float, aper2magerr_KS_viking float, aper3mag_KS_viking float, aper3magerr_KS_viking float, aper4mag_KS_viking float, aper4magerr_KS_viking float, aper6mag_KS_viking float, aper6magerr_KS_viking float, aper1mag_g_legacy float, aper1magerr_g_legacy float, aper2mag_g_legacy float, aper2magerr_g_legacy float, aper3mag_g_legacy float, aper3magerr_g_legacy float, aper4mag_g_legacy float, aper4magerr_g_legacy float, aper6mag_g_legacy float, aper6magerr_g_legacy float, aper1mag_r_legacy float, aper1magerr_r_legacy float, aper2mag_r_legacy float, aper2magerr_r_legacy float, aper3mag_r_legacy float, aper3magerr_r_legacy float, aper4mag_r_legacy float, aper4magerr_r_legacy float, aper6mag_r_legacy float, aper6magerr_r_legacy float, aper1mag_z_legacy float, aper1magerr_z_legacy float, aper2mag_z_legacy float, aper2magerr_z_legacy float, aper3mag_z_legacy float, aper3magerr_z_legacy float, aper4mag_z_legacy float, aper4magerr_z_legacy float, aper6mag_z_legacy float, aper6magerr_z_legacy float, petromag_Y_ukidss float, petromagerr_Y_ukidss float, petromag_J_ukidss float, petromagerr_J_ukidss float, petromag_H_ukidss float, petromagerr_H_ukidss float, petromag_K_ukidss float, petromagerr_K_ukidss float, aper3mag_w1_allwise float, aper3magerr_w1_allwise float, aper3mag_w2_allwise float, aper3magerr_w2_allwise float, aper3mag_w3_allwise float, aper3magerr_w3_allwise float, aper3mag_w4_allwise float, aper3magerr_w4_allwise float, aper3mag_NUV_galex float, aper3magerr_NUV_galex float, aper3mag_FUV_galex float, aper3magerr_FUV_galex float);

\copy rcsed_phot FROM /data1/rcsed_v2/postgres/phot.csv WITH CSV HEADER;

ALTER TABLE rcsed_phot ADD COLUMN crd2000 spoint;
UPDATE rcsed_phot SET crd2000=spoint(ra*pi()/180.0, dec*pi()/180.0);
CREATE INDEX index_phot ON rcsed_phot using gist (crd2000); 

CREATE TABLE filter_data (
filter_id int, filter_survey varchar, filter_name varchar, R float
);
\copy filter_data FROM /data1/rcsed_v2/postgres/filters.csv WITH CSV HEADER;

ALTER TABLE full_spec_spoint ADD COLUMN circle1 scircle;
UPDATE full_spec_spoint SET circle1=scircle(crd2000, 1.5/206265.);
CREATE INDEX index_1 ON full_spec_spoint using gist (circle1); 

ALTER TABLE full_spec_spoint ADD COLUMN circle2 scircle;
UPDATE full_spec_spoint SET circle2=scircle(crd2000, 2./206265.);
CREATE INDEX index_2 ON full_spec_spoint using gist (circle2); 

ALTER TABLE full_spec_spoint ADD COLUMN circle3 scircle;
UPDATE full_spec_spoint SET circle3=scircle(crd2000, 3./206265.);
CREATE INDEX index_3 ON full_spec_spoint using gist (circle3); 

ALTER TABLE full_spec_spoint ADD COLUMN circle4 scircle;
UPDATE full_spec_spoint SET circle4=scircle(crd2000, 3.3/206265.);
CREATE INDEX index_4 ON full_spec_spoint using gist (circle4); 

ALTER TABLE full_spec_spoint ADD COLUMN circle6 scircle;
UPDATE full_spec_spoint SET circle6=scircle(crd2000, 6.7/206265.);
CREATE INDEX index_6 ON full_spec_spoint using gist (circle6); 

SELECT b.specind, b.ra_j2000, b.dec_j2000, a.phot_index, a.aper1mag_Y_ukidss, a.aper1magerr_Y_ukidss, a.aper1mag_J_ukidss, a.aper1magerr_J_ukidss, a.aper1mag_H_ukidss, a.aper1magerr_H_ukidss, a.aper1mag_K_ukidss, a.aper1magerr_K_ukidss, a.aper1mag_J_uhs, a.aper1magerr_J_uhs, a.aper1mag_u_sdss, a.aper1magerr_u_sdss, a.aper1mag_g_sdss, a.aper1magerr_g_sdss, a.aper1mag_r_sdss, a.aper1magerr_r_sdss, a.aper1mag_i_sdss, a.aper1magerr_i_sdss, a.aper1mag_z_sdss, a.aper1magerr_z_sdss, a.aper1mag_U_vst, a.aper1magerr_U_vst, a.aper1mag_G_vst, a.aper1magerr_G_vst, a.aper1mag_R_vst, a.aper1magerr_R_vst, a.aper1mag_I_vst, a.aper1magerr_I_vst, a.aper1mag_Z_vst, a.aper1magerr_Z_vst, a.aper1mag_Y_vhs, a.aper1magerr_Y_vhs, a.aper1mag_J_vhs, a.aper1magerr_J_vhs, a.aper1mag_H_vhs, a.aper1magerr_H_vhs, a.aper1mag_KS_vhs, a.aper1magerr_KS_vhs, a.aper1mag_Z_viking, a.aper1magerr_Z_viking, a.aper1mag_Y_viking, a.aper1magerr_Y_viking, a.aper1mag_J_viking, a.aper1magerr_J_viking, a.aper1mag_H_viking, a.aper1magerr_H_viking, a.aper1mag_KS_viking, a.aper1magerr_KS_viking, a.aper1mag_g_legacy, a.aper1magerr_g_legacy, a.aper1mag_r_legacy, a.aper1magerr_r_legacy, a.aper1mag_z_legacy, a.aper1magerr_z_legacy INTO TABLE phot_spec_match FROM full_spec_spoint AS b, rcsed_phot AS a WHERE a.crd2000 @ b.circle1 AND b.survey='hectospec'; 

ALTER TABLE phot_spec_match ADD COLUMN fibersize float;
UPDATE phot_spec_match SET fibersize=1.5;

INSERT INTO phot_spec_match (specind, ra_j2000, dec_j2000, phot_index, aper1mag_y_ukidss, aper1magerr_y_ukidss, aper1mag_j_ukidss, aper1magerr_j_ukidss, aper1mag_h_ukidss, aper1magerr_h_ukidss, aper1mag_k_ukidss, aper1magerr_k_ukidss, aper1mag_j_uhs, aper1magerr_j_uhs, aper1mag_u_sdss, aper1magerr_u_sdss, aper1mag_g_sdss, aper1magerr_g_sdss, aper1mag_r_sdss, aper1magerr_r_sdss, aper1mag_i_sdss, aper1magerr_i_sdss, aper1mag_z_sdss, aper1magerr_z_sdss, aper1mag_u_vst, aper1magerr_u_vst, aper1mag_g_vst, aper1magerr_g_vst, aper1mag_r_vst, aper1magerr_r_vst, aper1mag_i_vst, aper1magerr_i_vst, aper1mag_z_vst, aper1magerr_z_vst, aper1mag_y_vhs, aper1magerr_y_vhs, aper1mag_j_vhs, aper1magerr_j_vhs, aper1mag_h_vhs, aper1magerr_h_vhs, aper1mag_ks_vhs, aper1magerr_ks_vhs, aper1mag_z_viking, aper1magerr_z_viking, aper1mag_y_viking, aper1magerr_y_viking, aper1mag_j_viking, aper1magerr_j_viking, aper1mag_h_viking, aper1magerr_h_viking, aper1mag_ks_viking, aper1magerr_ks_viking, aper1mag_g_legacy, aper1magerr_g_legacy, aper1mag_r_legacy, aper1magerr_r_legacy, aper1mag_z_legacy, aper1magerr_z_legacy) SELECT b.specind, b.ra_j2000, b.dec_j2000, a.phot_index, a.aper2mag_Y_ukidss, a.aper2magerr_Y_ukidss, a.aper2mag_J_ukidss, a.aper2magerr_J_ukidss, a.aper2mag_H_ukidss, a.aper2magerr_H_ukidss, a.aper2mag_K_ukidss, a.aper2magerr_K_ukidss, a.aper2mag_J_uhs, a.aper2magerr_J_uhs, a.aper2mag_u_sdss, a.aper2magerr_u_sdss, a.aper2mag_g_sdss, a.aper2magerr_g_sdss, a.aper2mag_r_sdss, a.aper2magerr_r_sdss, a.aper2mag_i_sdss, a.aper2magerr_i_sdss, a.aper2mag_z_sdss, a.aper2magerr_z_sdss, a.aper2mag_U_vst, a.aper2magerr_U_vst, a.aper2mag_G_vst, a.aper2magerr_G_vst, a.aper2mag_R_vst, a.aper2magerr_R_vst, a.aper2mag_I_vst, a.aper2magerr_I_vst, a.aper2mag_Z_vst, a.aper2magerr_Z_vst, a.aper2mag_Y_vhs, a.aper2magerr_Y_vhs, a.aper2mag_J_vhs, a.aper2magerr_J_vhs, a.aper2mag_H_vhs, a.aper2magerr_H_vhs, a.aper2mag_KS_vhs, a.aper2magerr_KS_vhs, a.aper2mag_Z_viking, a.aper2magerr_Z_viking, a.aper2mag_Y_viking, a.aper2magerr_Y_viking, a.aper2mag_J_viking, a.aper2magerr_J_viking, a.aper2mag_H_viking, a.aper2magerr_H_viking, a.aper2mag_KS_viking, a.aper2magerr_KS_viking, a.aper2mag_g_legacy, a.aper2magerr_g_legacy, a.aper2mag_r_legacy, a.aper2magerr_r_legacy, a.aper2mag_z_legacy, a.aper2magerr_z_legacy FROM full_spec_spoint AS b, rcsed_phot AS a WHERE a.crd2000 @ b.circle2 AND (b.survey='wigglez' or b.survey='2df' or b.survey='2dflens' or b.survey='gama'); 

UPDATE phot_spec_match SET fibersize=2 WHERE fibersize is null;

INSERT INTO phot_spec_match (specind, ra_j2000, dec_j2000, phot_index, aper1mag_y_ukidss, aper1magerr_y_ukidss, aper1mag_j_ukidss, aper1magerr_j_ukidss, aper1mag_h_ukidss, aper1magerr_h_ukidss, aper1mag_k_ukidss, aper1magerr_k_ukidss, aper1mag_j_uhs, aper1magerr_j_uhs, aper1mag_u_sdss, aper1magerr_u_sdss, aper1mag_g_sdss, aper1magerr_g_sdss, aper1mag_r_sdss, aper1magerr_r_sdss, aper1mag_i_sdss, aper1magerr_i_sdss, aper1mag_z_sdss, aper1magerr_z_sdss, aper1mag_u_vst, aper1magerr_u_vst, aper1mag_g_vst, aper1magerr_g_vst, aper1mag_r_vst, aper1magerr_r_vst, aper1mag_i_vst, aper1magerr_i_vst, aper1mag_z_vst, aper1magerr_z_vst, aper1mag_y_vhs, aper1magerr_y_vhs, aper1mag_j_vhs, aper1magerr_j_vhs, aper1mag_h_vhs, aper1magerr_h_vhs, aper1mag_ks_vhs, aper1magerr_ks_vhs, aper1mag_z_viking, aper1magerr_z_viking, aper1mag_y_viking, aper1magerr_y_viking, aper1mag_j_viking, aper1magerr_j_viking, aper1mag_h_viking, aper1magerr_h_viking, aper1mag_ks_viking, aper1magerr_ks_viking, aper1mag_g_legacy, aper1magerr_g_legacy, aper1mag_r_legacy, aper1magerr_r_legacy, aper1mag_z_legacy, aper1magerr_z_legacy) SELECT b.specind, b.ra_j2000, b.dec_j2000, a.phot_index, a.aper4mag_Y_ukidss, a.aper4magerr_Y_ukidss, a.aper4mag_J_ukidss, a.aper4magerr_J_ukidss, a.aper4mag_H_ukidss, a.aper4magerr_H_ukidss, a.aper4mag_K_ukidss, a.aper4magerr_K_ukidss, a.aper4mag_J_uhs, a.aper4magerr_J_uhs, a.aper4mag_u_sdss, a.aper4magerr_u_sdss, a.aper4mag_g_sdss, a.aper4magerr_g_sdss, a.aper4mag_r_sdss, a.aper4magerr_r_sdss, a.aper4mag_i_sdss, a.aper4magerr_i_sdss, a.aper4mag_z_sdss, a.aper4magerr_z_sdss, a.aper4mag_U_vst, a.aper4magerr_U_vst, a.aper4mag_G_vst, a.aper4magerr_G_vst, a.aper4mag_R_vst, a.aper4magerr_R_vst, a.aper4mag_I_vst, a.aper4magerr_I_vst, a.aper4mag_Z_vst, a.aper4magerr_Z_vst, a.aper4mag_Y_vhs, a.aper4magerr_Y_vhs, a.aper4mag_J_vhs, a.aper4magerr_J_vhs, a.aper4mag_H_vhs, a.aper4magerr_H_vhs, a.aper4mag_KS_vhs, a.aper4magerr_KS_vhs, a.aper4mag_Z_viking, a.aper4magerr_Z_viking, a.aper4mag_Y_viking, a.aper4magerr_Y_viking, a.aper4mag_J_viking, a.aper4magerr_J_viking, a.aper4mag_H_viking, a.aper4magerr_H_viking, a.aper4mag_KS_viking, a.aper4magerr_KS_viking, a.aper4mag_g_legacy, a.aper4magerr_g_legacy, a.aper4mag_r_legacy, a.aper4magerr_r_legacy, a.aper4mag_z_legacy, a.aper4magerr_z_legacy FROM full_spec_spoint AS b, rcsed_phot AS a WHERE a.crd2000 @ b.circle4 AND b.survey='lamost'; 

UPDATE phot_spec_match SET fibersize=3.3 WHERE fibersize is null;

INSERT INTO phot_spec_match (specind, ra_j2000, dec_j2000, phot_index, aper1mag_y_ukidss, aper1magerr_y_ukidss, aper1mag_j_ukidss, aper1magerr_j_ukidss, aper1mag_h_ukidss, aper1magerr_h_ukidss, aper1mag_k_ukidss, aper1magerr_k_ukidss, aper1mag_j_uhs, aper1magerr_j_uhs, aper1mag_u_sdss, aper1magerr_u_sdss, aper1mag_g_sdss, aper1magerr_g_sdss, aper1mag_r_sdss, aper1magerr_r_sdss, aper1mag_i_sdss, aper1magerr_i_sdss, aper1mag_z_sdss, aper1magerr_z_sdss, aper1mag_u_vst, aper1magerr_u_vst, aper1mag_g_vst, aper1magerr_g_vst, aper1mag_r_vst, aper1magerr_r_vst, aper1mag_i_vst, aper1magerr_i_vst, aper1mag_z_vst, aper1magerr_z_vst, aper1mag_y_vhs, aper1magerr_y_vhs, aper1mag_j_vhs, aper1magerr_j_vhs, aper1mag_h_vhs, aper1magerr_h_vhs, aper1mag_ks_vhs, aper1magerr_ks_vhs, aper1mag_z_viking, aper1magerr_z_viking, aper1mag_y_viking, aper1magerr_y_viking, aper1mag_j_viking, aper1magerr_j_viking, aper1mag_h_viking, aper1magerr_h_viking, aper1mag_ks_viking, aper1magerr_ks_viking, aper1mag_g_legacy, aper1magerr_g_legacy, aper1mag_r_legacy, aper1magerr_r_legacy, aper1mag_z_legacy, aper1magerr_z_legacy) SELECT b.specind, b.ra_j2000, b.dec_j2000, a.phot_index, a.aper6mag_Y_ukidss, a.aper6magerr_Y_ukidss, a.aper6mag_J_ukidss, a.aper6magerr_J_ukidss, a.aper6mag_H_ukidss, a.aper6magerr_H_ukidss, a.aper6mag_K_ukidss, a.aper6magerr_K_ukidss, a.aper6mag_J_uhs, a.aper6magerr_J_uhs, a.aper6mag_u_sdss, a.aper6magerr_u_sdss, a.aper6mag_g_sdss, a.aper6magerr_g_sdss, a.aper6mag_r_sdss, a.aper6magerr_r_sdss, a.aper6mag_i_sdss, a.aper6magerr_i_sdss, a.aper6mag_z_sdss, a.aper6magerr_z_sdss, a.aper6mag_U_vst, a.aper6magerr_U_vst, a.aper6mag_G_vst, a.aper6magerr_G_vst, a.aper6mag_R_vst, a.aper6magerr_R_vst, a.aper6mag_I_vst, a.aper6magerr_I_vst, a.aper6mag_Z_vst, a.aper6magerr_Z_vst, a.aper6mag_Y_vhs, a.aper6magerr_Y_vhs, a.aper6mag_J_vhs, a.aper6magerr_J_vhs, a.aper6mag_H_vhs, a.aper6magerr_H_vhs, a.aper6mag_KS_vhs, a.aper6magerr_KS_vhs, a.aper6mag_Z_viking, a.aper6magerr_Z_viking, a.aper6mag_Y_viking, a.aper6magerr_Y_viking, a.aper6mag_J_viking, a.aper6magerr_J_viking, a.aper6mag_H_viking, a.aper6magerr_H_viking, a.aper6mag_KS_viking, a.aper6magerr_KS_viking, a.aper6mag_g_legacy, a.aper6magerr_g_legacy, a.aper6mag_r_legacy, a.aper6magerr_r_legacy, a.aper6mag_z_legacy, a.aper6magerr_z_legacy FROM full_spec_spoint AS b, rcsed_phot AS a WHERE a.crd2000 @ b.circle6 AND b.survey='6df'; 

UPDATE phot_spec_match SET fibersize=6.7 WHERE fibersize is null;
ALTER TABLE phot_spec_match ADD COLUMN aper3mag_w1_allwise float, ADD COLUMN aper3magerr_w1_allwise float, ADD COLUMN aper3mag_w2_allwise float, ADD COLUMN aper3magerr_w2_allwise float, ADD COLUMN aper3mag_w3_allwise float, ADD COLUMN aper3magerr_w3_allwise float, ADD COLUMN aper3mag_w4_allwise float, ADD COLUMN aper3magerr_w4_allwise float, ADD COLUMN aper3mag_NUV_galex float, ADD COLUMN aper3magerr_NUV_galex float, ADD COLUMN aper3mag_FUV_galex float, ADD COLUMN aper3magerr_FUV_galex float;

INSERT INTO phot_spec_match (specind, ra_j2000, dec_j2000, phot_index, aper1mag_y_ukidss, aper1magerr_y_ukidss, aper1mag_j_ukidss, aper1magerr_j_ukidss, aper1mag_h_ukidss, aper1magerr_h_ukidss, aper1mag_k_ukidss, aper1magerr_k_ukidss, aper1mag_j_uhs, aper1magerr_j_uhs, aper1mag_u_sdss, aper1magerr_u_sdss, aper1mag_g_sdss, aper1magerr_g_sdss, aper1mag_r_sdss, aper1magerr_r_sdss, aper1mag_i_sdss, aper1magerr_i_sdss, aper1mag_z_sdss, aper1magerr_z_sdss, aper1mag_u_vst, aper1magerr_u_vst, aper1mag_g_vst, aper1magerr_g_vst, aper1mag_r_vst, aper1magerr_r_vst, aper1mag_i_vst, aper1magerr_i_vst, aper1mag_z_vst, aper1magerr_z_vst, aper1mag_y_vhs, aper1magerr_y_vhs, aper1mag_j_vhs, aper1magerr_j_vhs, aper1mag_h_vhs, aper1magerr_h_vhs, aper1mag_ks_vhs, aper1magerr_ks_vhs, aper1mag_z_viking, aper1magerr_z_viking, aper1mag_y_viking, aper1magerr_y_viking, aper1mag_j_viking, aper1magerr_j_viking, aper1mag_h_viking, aper1magerr_h_viking, aper1mag_ks_viking, aper1magerr_ks_viking, aper1mag_g_legacy, aper1magerr_g_legacy, aper1mag_r_legacy, aper1magerr_r_legacy, aper1mag_z_legacy, aper1magerr_z_legacy, aper3mag_w1_allwise, aper3magerr_w1_allwise, aper3mag_w2_allwise, aper3magerr_w2_allwise, aper3mag_w3_allwise, aper3magerr_w3_allwise, aper3mag_w4_allwise, aper3magerr_w4_allwise, aper3mag_NUV_galex, aper3magerr_NUV_galex, aper3mag_FUV_galex, aper3magerr_FUV_galex) SELECT b.specind, b.ra_j2000, b.dec_j2000, a.phot_index, a.aper3mag_Y_ukidss, a.aper3magerr_Y_ukidss, a.aper3mag_J_ukidss, a.aper3magerr_J_ukidss, a.aper3mag_H_ukidss, a.aper3magerr_H_ukidss, a.aper3mag_K_ukidss, a.aper3magerr_K_ukidss, a.aper3mag_J_uhs, a.aper3magerr_J_uhs, a.aper3mag_u_sdss, a.aper3magerr_u_sdss, a.aper3mag_g_sdss, a.aper3magerr_g_sdss, a.aper3mag_r_sdss, a.aper3magerr_r_sdss, a.aper3mag_i_sdss, a.aper3magerr_i_sdss, a.aper3mag_z_sdss, a.aper3magerr_z_sdss, a.aper3mag_U_vst, a.aper3magerr_U_vst, a.aper3mag_G_vst, a.aper3magerr_G_vst, a.aper3mag_R_vst, a.aper3magerr_R_vst, a.aper3mag_I_vst, a.aper3magerr_I_vst, a.aper3mag_Z_vst, a.aper3magerr_Z_vst, a.aper3mag_Y_vhs, a.aper3magerr_Y_vhs, a.aper3mag_J_vhs, a.aper3magerr_J_vhs, a.aper3mag_H_vhs, a.aper3magerr_H_vhs, a.aper3mag_KS_vhs, a.aper3magerr_KS_vhs, a.aper3mag_Z_viking, a.aper3magerr_Z_viking, a.aper3mag_Y_viking, a.aper3magerr_Y_viking, a.aper3mag_J_viking, a.aper3magerr_J_viking, a.aper3mag_H_viking, a.aper3magerr_H_viking, a.aper3mag_KS_viking, a.aper3magerr_KS_viking, a.aper3mag_g_legacy, a.aper3magerr_g_legacy, a.aper3mag_r_legacy, a.aper3magerr_r_legacy, a.aper3mag_z_legacy, a.aper3magerr_z_legacy, a.aper3mag_w1_allwise, a.aper3magerr_w1_allwise, a.aper3mag_w2_allwise, a.aper3magerr_w2_allwise, a.aper3mag_w3_allwise, a.aper3magerr_w3_allwise, a.aper3mag_w4_allwise, a.aper3magerr_w4_allwise, a.aper3mag_NUV_galex, a.aper3magerr_NUV_galex, a.aper3mag_FUV_galex, a.aper3magerr_FUV_galex FROM full_spec_spoint AS b, rcsed_phot AS a WHERE a.crd2000 @ b.circle3 AND b.survey='sdss'; 

UPDATE phot_spec_match SET fibersize=3 WHERE fibersize is null;

CREATE TABLE raw_aper_phot (objid_spec int, aper_size float, filter_id int, mag float, mag_err float);
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_u_SDSS, aper1magerr_u_SDSS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=1 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_g_SDSS, aper1magerr_g_SDSS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=2 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_r_SDSS, aper1magerr_r_SDSS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=3 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_i_SDSS, aper1magerr_i_SDSS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=4 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_z_SDSS, aper1magerr_z_SDSS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=5 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_g_DES, aper1magerr_g_DES, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=6 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_r_DES, aper1magerr_r_DES, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=7 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_i_DES, aper1magerr_i_DES, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=8 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_z_DES, aper1magerr_z_DES, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=9 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_y_DES, aper1magerr_y_DES, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=10 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_u_KIDS, aper1magerr_u_KIDS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=11 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_g_KIDS, aper1magerr_g_KIDS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=12 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_r_KIDS, aper1magerr_r_KIDS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=13 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_i_KIDS, aper1magerr_i_KIDS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=14 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_u_SkyMapper, aper1magerr_u_SkyMapper, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=15 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_v_SkyMapper, aper1magerr_v_SkyMapper, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=16 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_g_SkyMapper, aper1magerr_g_SkyMapper, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=17 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_r_SkyMapper, aper1magerr_r_SkyMapper, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=18 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_i_SkyMapper, aper1magerr_i_SkyMapper, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=19 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_z_SkyMapper, aper1magerr_z_SkyMapper, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=20 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_J_UHS, aper1magerr_J_UHS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=21 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_Y_UKIDSS, aper1magerr_Y_UKIDSS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=22 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_J_UKIDSS, aper1magerr_J_UKIDSS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=23 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_H_UKIDSS, aper1magerr_H_UKIDSS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=24 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_K_UKIDSS, aper1magerr_K_UKIDSS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=25 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_y_VHS, aper1magerr_y_VHS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=26 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_j_VHS, aper1magerr_j_VHS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=27 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_h_VHS, aper1magerr_h_VHS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=28 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_ks_VHS, aper1magerr_ks_VHS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=29 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_Z_VIKING, aper1magerr_Z_VIKING, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=30 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_Y_VIKING, aper1magerr_Y_VIKING, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=31 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_J_VIKING, aper1magerr_J_VIKING, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=32 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_H_VIKING, aper1magerr_H_VIKING, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=33 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_Ks_VIKING, aper1magerr_Ks_VIKING, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=34 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_U_VST, aper1magerr_U_VST, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=35 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_G_VST, aper1magerr_G_VST, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=36 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_R_VST, aper1magerr_R_VST, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=37 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_I_VST, aper1magerr_I_VST, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=38 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_Z_VST, aper1magerr_Z_VST, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=39 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_g_DECALS, aper1magerr_g_DECALS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=40 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_r_DECALS, aper1magerr_r_DECALS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=41 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_z_DECALS, aper1magerr_z_DECALS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=42 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_g_PanSTARRS, aper1magerr_g_PanSTARRS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=43 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_r_PanSTARRS, aper1magerr_r_PanSTARRS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=44 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_i_PanSTARRS, aper1magerr_i_PanSTARRS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=45 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_z_PanSTARRS, aper1magerr_z_PanSTARRS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=46 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper1mag_y_PanSTARRS, aper1magerr_y_PanSTARRS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=47 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper3mag_w1_allWISE, aper3magerr_w1_allWISE, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=48 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper3mag_w2_allWISE, aper3magerr_w2_allWISE, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=49 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper3mag_w3_allWISE, aper3magerr_w3_allWISE, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=50 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper3mag_w4_allWISE, aper3magerr_w4_allWISE, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=51 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper3mag_NUV_GALEX, aper3magerr_NUV_GALEX, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=52 WHERE filter_id is null;
INSERT INTO raw_aper_phot (objid_spec, mag, mag_err, aper_size) SELECT specind, aper3mag_FUV_GALEX, aper3magerr_FUV_GALEX, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=53 WHERE filter_id is null;

DELETE FROM raw_aper_phot WHERE mag is null AND mag_err is null;
, 
SELECT photo_index, ra, dec, petromag_g_des + (petromag_g_des-petromag_r_des)*(-0.103) AS petromag_gsdss_des,
petromag_r_des + (petromag_g_des-petromag_r_des)*(-0.080) AS petromag_rsdss_des,
petromag_i_des + (petromag_i_des-petromag_z_des)*(-0.298) AS petromag_isdss_des,
petromag_z_des + (petromag_i_des-petromag_z_des)*(-0.086) AS petromag_zsdss_des,
petromag_y_des + (petromag_z_des-petromag_y_des)*0.261 AS petromag_ysdss_des,  
petromag_g_panstarrs - (-0.01808 - 0.13595*(kronmag_r_panstarrs-kronmag_i_panstarrs) + 0.01941*(kronmag_r_panstarrs-kronmag_i_panstarrs)^2 - 0.00183*(kronmag_r_panstarrs-kronmag_i_panstarrs)^3) AS petromag_gsdss_panstarrs, 
petromag_r_panstarrs - (-0.01836 - 0.03577*(kronmag_r_panstarrs-kronmag_i_panstarrs) + 0.02612*(kronmag_r_panstarrs-kronmag_i_panstarrs)^2 - 0.00558*(kronmag_r_panstarrs-kronmag_i_panstarrs)^3) AS petromag_rsdss_panstarrs, 
petromag_i_panstarrs - (0.01170 - 0.00400*(kronmag_r_panstarrs-kronmag_i_panstarrs) + 0.00066*(kronmag_r_panstarrs-kronmag_i_panstarrs)^2 - 0.00058*(kronmag_r_panstarrs-kronmag_i_panstarrs)^3) AS petromag_isdss_panstarrs, 
petromag_z_panstarrs - (-0.01062 + 0.07529*(kronmag_r_panstarrs-kronmag_i_panstarrs) - 0.03592*(kronmag_r_panstarrs-kronmag_i_panstarrs)^2 + 0.00890*(kronmag_r_panstarrs-kronmag_i_panstarrs)^3) AS petromag_zsdss_panstarrs, 
integmag_g_legacy - (-0.01808 - 0.13595*(integmag_g_legacy-integmag_r_legacy) + 0.01941*(integmag_g_legacy-integmag_r_legacy)^2 - 0.00183*(integmag_g_legacy-integmag_r_legacy)^3) AS petromag_gsdss_legacy, 
petromag_r_legacy - (-0.01836 - 0.03577*(integmag_g_legacy-integmag_r_legacy) + 0.02612*(integmag_g_legacy-integmag_r_legacy)^2 - 0.00558*(integmag_g_legacy-integmag_r_legacy)^3) AS petromag_rsdss_legacy, 
petromag_z_legacy - (-0.01062 + 0.07529*(integmag_g_legacy-integmag_r_legacy) - 0.03592*(integmag_g_legacy-integmag_r_legacy)^2 + 0.00890*(integmag_g_legacy-integmag_r_legacy)^3) AS petromag_zsdss_legacy, , 
============= 

\COPY (SELECT * FROM size_table WHERE size_survey='hyperleda') TO /data1/rcsed_v2/postgres/leda_coords_toedit.csv WITH CSV HEADER;

CREATE TABLE sdss_coords_for_leda_galaxies (objid_spec bigint, objid_leda bigint, ra_leda, dec_leda, objid_sdss bigint, ra_sdss, dec_sdss)

\copy sdss_coords_for_leda_galaxies FROM /data1/rcsed_v2/postgres/leda_coords_results.csv WITH CSV HEADER;

SELECT * INTO TABLE size_table_corrected_coord FROM size_table WHERE size_survey != 'hyperleda';
INSERT INTO size_table_corrected_coord SELECT a.objid_spec, a.ra_j2000, a.dec_j2000, a.size_survey, a.objid, b.ra_sdss, b.dec_sdss, a.amaj, a.ell, a.pa, a.objid_nsc FROM size_table FROM a LEFT JOIN sdss_coords_for_leda_galaxies AS b ON a.objid_spec=b.objid_spec;

==============================

CREATE TABLE raw_tot_phot (r2id_obj bigint, filter_id bigint, mag_type varchar, mag_radius float, mag float, mag_err float);

SELECT DISTINCT ON(objid) size_survey, objid_spec, objid, objid_nsc, dense_rank() OVER (order by objid) AS r2id_obj INTO TABLE gal_id FROM size_table WHERE size_survey!='nsc' AND size_survey is not null;

INSERT INTO gal_id (size_survey, objid_spec, objid_nsc, r2id_obj) SELECT DISTINCT ON(objid_nsc) size_survey, objid_spec, objid_nsc, dense_rank() OVER (order by objid_nsc) AS r2id_obj FROM size_table WHERE size_survey='nsc';

INSERT INTO gal_id (size_survey, objid_spec, objid, r2id_obj) SELECT size_survey, objid_spec, objid, dense_rank() OVER (order by objid_spec) AS r2id_obj FROM size_table WHERE size_survey is null;

UPDATE gal_id SET r2id_obj=r2id_obj+4461763 WHERE size_survey='nsc';
UPDATE gal_id SET r2id_obj=r2id_obj+4532546 WHERE size_survey is null;

SELECT * INTO size_table_tmp FROM
(SELECT a.*, b.r2id_obj FROM size_table AS a LEFT OUTER JOIN gal_id AS b ON a.objid=b.objid WHERE a.size_survey is not null AND a.size_survey!='nsc'  UNION SELECT a.*, b.r2id_obj FROM size_table AS a LEFT OUTER JOIN gal_id AS b ON a.objid_spec=b.objid_spec WHERE a.size_survey is null UNION SELECT a.*, b.r2id_obj FROM size_table AS a LEFT OUTER JOIN gal_id AS b ON a.objid_nsc=b.objid_nsc WHERE a.size_survey ='nsc') AS tmp;

drop table size_table;
alter table size_table_tmp rename to size_table;
alter table size_table rename column  objid_spec to r2id_spec;

ALTER TABLE size_table ADD COLUMN gal sellipse;
UPDATE size_table SET gal=sellipse(spoint(ra_obj*pi()/180, dec_obj*pi()/180), amaj/206265, amaj*(1-ell)/206265, pa*pi()/180);

SELECT b.r2id_obj, b.ra_obj, b.dec_obj, a.phot_index, a.ra, a.dec, a.petror50_u_sdss, a.petror50_g_sdss, a.petror50_r_sdss, a.petror50_i_sdss, a.petror50_z_sdss, a.petromag_u_sdss, a.petromag_g_sdss, a.petromag_r_sdss, a.petromag_i_sdss, a.petromag_z_sdss, a.petromagerr_u_sdss, a.petromagerr_g_sdss, a.petromagerr_r_sdss, a.petromagerr_i_sdss, a.petromagerr_z_sdss, a.petromagerr_g_des, a.petromagerr_r_des, a.petromagerr_i_des, a.petromagerr_z_des, a.petromagerr_y_des, a.petromag_g_des, a.petromag_r_des, a.petromag_i_des, a.petromag_z_des, a.petromag_y_des, a.petrorad_g_des, a.petrorad_r_des, a.petrorad_i_des, a.petrorad_z_des, a.petrorad_y_des, a.petromag_J_uhs, a.petromagerr_J_uhs, a.petromag_Y_vhs, a.petromagerr_Y_vhs, a.petromag_J_vhs, a.petromagerr_J_vhs, a.petromag_H_vhs, a.petromagerr_H_vhs, a.petromag_KS_vhs, a.petromagerr_KS_vhs, a.petromag_Z_viking, a.petromagerr_Z_viking, a.petromag_Y_viking, a.petromagerr_Y_viking, a.petromag_J_viking, a.petromagerr_J_viking, a.petromag_H_viking, a.petromagerr_H_viking, a.petromag_KS_viking, a.petromagerr_KS_viking, a.petrorad_skymapper, a.petromag_u_skymapper, a.petromagerr_u_skymapper, a.petromag_v_skymapper, a.petromagerr_v_skymapper, a.petromag_g_skymapper, a.petromagerr_g_skymapper, a.petromag_r_skymapper, a.petromagerr_r_skymapper, a.petromag_i_skymapper, a.petromagerr_i_skymapper, a.petromag_z_skymapper, a.petromag_U_vst, a.petromagerr_U_vst, a.petromag_G_vst, a.petromagerr_G_vst, a.petromag_R_vst, a.petromagerr_R_vst, a.petromag_I_vst, a.petromagerr_I_vst, a.petromag_Z_vst, a.petromagerr_Z_vst, a.integmag_w1_unwise, a.integmagerr_w1_unwise, a.integmag_w2_unwise, a.integmagerr_w2_unwise, a.integmag_g_legacy, a.integmag_r_legacy, a.integmag_z_legacy, a.kronmag_g_panstarrs, a.kronmagerr_g_panstarrs, a.kronmag_r_panstarrs, a.kronmagerr_r_panstarrs, a.kronmag_i_panstarrs, a.kronmagerr_i_panstarrs, a.kronmag_z_panstarrs, a.kronmagerr_z_panstarrs, a.kronmag_y_panstarrs, a.kronmagerr_y_panstarrs, a.kronmag_NUV_galex, a.kronmagerr_NUV_galex, a.kronmag_FUV_galex, a.kronmagerr_FUV_galex, a.kronrad_kids, a.kronmag_u_kids, a.kronmagerr_u_kids, a.kronmag_g_kids, a.kronmagerr_g_kids, a.kronmag_r_kids, a.kronmagerr_r_kids, a.kronmag_i_kids, a.kronmagerr_i_kids, a.petromag_Y_ukidss, a.petromagerr_Y_ukidss, a.petromag_J_ukidss, a.petromagerr_J_ukidss, a.petromag_H_ukidss, a.petromagerr_H_ukidss, a.petromag_K_ukidss, a.petromagerr_K_ukidss INTO TABLE tot_phot_spec_match FROM size_table AS b, rcsed_phot AS a WHERE a.crd2000 @ b.gal; 

INSERT INTO raw_aper_phot (r2id_obj, mag, mag_err) SELECT specind, aper1mag_z_PanSTARRS, aper1magerr_z_PanSTARRS, fibersize FROM phot_spec_match;
UPDATE raw_aper_phot SET filter_id=46 WHERE filter_id is null;



сматчить интегральные со списком объектов
сматчить фиберные со списком спектров с учетом размеров фибера, , 1 6773185, 1237645879551000764 1237680531356582786, 8796094669783592 8797235825213629, 730144594997 730254686226, 
SELECT a.*, b.r2id_obj INTO TABLE full_spec_2 FROM full_spec_spoint AS a LEFT JOIN size_table AS b ON a.r2id_spec=b.r2id_spec;
ALTER TABLE full_spec_2 ADD CONSTRAINT spec_constraint UNIQUE(r2id_spec) ;
CREATE INDEX specindex_2 on full_spec_2(r2id_spec);
CREATE INDEX fullindex_2 on full_spec_2 using gist(crd2000);
ALTER TABLE full_spec_spoint RENAME TO full_spec_spoint_2;
ALTER TABLE full_spec_2 RENAME TO full_spec_spoint;

SELECT DISTINCT ON(r2id_obj) * INTO TABLE rcsed_galaxies FROM size_table;
ALTER TABLE rcsed_galaxies ADD CONSTRAINT galaxy UNIQUE (r2id_obj);
ALTER TABLE rcsed_galaxies DROP COLUMN ra_j2000;
ALTER TABLE rcsed_galaxies DROP COLUMN r2id_spec;
ALTER TABLE rcsed_galaxies DROP COLUMN dec_j2000;
ALTER TABLE full_spec_spoint ADD CONSTRAINT specfk FOREIGN KEY (r2id_obj) REFERENCES rcsed_galaxies (r2id_obj);

UPDATE rcsed_galaxies T1 SET ra_obj=t2.ra_j2000 FROM full_spec_spoint AS t2 WHERE T1.r2id_obj=t2.r2id_obj AND T1.ra_obj is null;
UPDATE rcsed_galaxies T1 SET dec_obj=t2.dec_j2000 FROM full_spec_spoint AS t2 WHERE T1.r2id_obj=t2.r2id_obj AND T1.dec_obj is null;

=======================================================================================
RAW_TOT_PHOT
=======================================================================================

CREATE INDEX gal_ind ON rcsed_galaxies USING GIST (gal);

ALTER TABLE rcsed_galaxies ADD COLUMN crd2000 spoint;
UPDATE rcsed_galaxies SET crd2000=spoint(ra_obj*pi()/180, dec_obj*pi()/180);
CREATE INDEX crd_ind ON rcsed_galaxies USING GIST (crd2000);

SELECT b.r2id_obj, b.ra_obj, b.dec_obj, a.phot_index, a.petror50_u_sdss, a.petror50_g_sdss, a.petror50_r_sdss, a.petror50_i_sdss, a.petror50_z_sdss, a.petromag_u_sdss, a.petromag_g_sdss, a.petromag_r_sdss, a.petromag_i_sdss, a.petromag_z_sdss, a.petromagerr_u_sdss, a.petromagerr_g_sdss, a.petromagerr_r_sdss, a.petromagerr_i_sdss, a.petromagerr_z_sdss, a.petromagerr_g_des, a.petromagerr_r_des, a.petromagerr_i_des, a.petromagerr_z_des, a.petromagerr_y_des, a.petromag_g_des, a.petromag_r_des, a.petromag_i_des, a.petromag_z_des, a.petromag_y_des, a.petrorad_g_des, a.petrorad_r_des, a.petrorad_i_des, a.petrorad_z_des, a.petrorad_y_des, a.petromag_J_uhs, a.petromagerr_J_uhs, a.petromag_Y_vhs, a.petromagerr_Y_vhs, a.petromag_J_vhs, a.petromagerr_J_vhs, a.petromag_H_vhs, a.petromagerr_H_vhs, a.petromag_KS_vhs, a.petromagerr_KS_vhs, a.petromag_Z_viking, a.petromagerr_Z_viking, a.petromag_Y_viking, a.petromagerr_Y_viking, a.petromag_J_viking, a.petromagerr_J_viking, a.petromag_H_viking, a.petromagerr_H_viking, a.petromag_KS_viking, a.petromagerr_KS_viking, a.petrorad_skymapper, a.petromag_u_skymapper, a.petromagerr_u_skymapper, a.petromag_v_skymapper, a.petromagerr_v_skymapper, a.petromag_g_skymapper, a.petromagerr_g_skymapper, a.petromag_r_skymapper, a.petromagerr_r_skymapper, a.petromag_i_skymapper, a.petromagerr_i_skymapper, a.petromag_z_skymapper, a.petromag_U_vst, a.petromagerr_U_vst, a.petromag_G_vst, a.petromagerr_G_vst, a.petromag_R_vst, a.petromagerr_R_vst, a.petromag_I_vst, a.petromagerr_I_vst, a.petromag_Z_vst, a.petromagerr_Z_vst, a.integmag_w1_unwise, a.integmagerr_w1_unwise, a.integmag_w2_unwise, a.integmagerr_w2_unwise, a.integmag_g_legacy, a.integmag_r_legacy, a.integmag_z_legacy, a.kronmag_g_panstarrs, a.kronmagerr_g_panstarrs, a.kronmag_r_panstarrs, a.kronmagerr_r_panstarrs, a.kronmag_i_panstarrs, a.kronmagerr_i_panstarrs, a.kronmag_z_panstarrs, a.kronmagerr_z_panstarrs, a.kronmag_y_panstarrs, a.kronmagerr_y_panstarrs, a.kronmag_NUV_galex, a.kronmagerr_NUV_galex, a.kronmag_FUV_galex, a.kronmagerr_FUV_galex, a.kronrad_kids, a.kronmag_u_kids, a.kronmagerr_u_kids, a.kronmag_g_kids, a.kronmagerr_g_kids, a.kronmag_r_kids, a.kronmagerr_r_kids, a.kronmag_i_kids, a.kronmagerr_i_kids, a.petromag_Y_ukidss, a.petromagerr_Y_ukidss, a.petromag_J_ukidss, a.petromagerr_J_ukidss, a.petromag_H_ukidss, a.petromagerr_H_ukidss, a.petromag_K_ukidss, a.petromagerr_K_ukidss INTO TABLE tot_phot_spec_match FROM rcsed_galaxies AS b, rcsed_phot AS a WHERE a.crd2000 @ b.gal; 

GRANT SELECT ON rcsed_galaxies TO dj_user;
GRANT SELECT ON spec_full_spoint TO dj_user;

INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err, mag_radius) SELECT R2id_obj, petromag_u_SDSS, petromagerr_u_SDSS, petror50_u_SDSS FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=1 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err, mag_radius) SELECT R2id_obj, petromag_g_SDSS, petromagerr_g_SDSS, petror50_g_SDSS FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=2 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err, mag_radius) SELECT R2id_obj, petromag_r_SDSS, petromagerr_r_SDSS, petror50_r_SDSS FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=3 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err, mag_radius) SELECT R2id_obj, petromag_i_SDSS, petromagerr_i_SDSS, petror50_i_SDSS FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=4 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err, mag_radius) SELECT R2id_obj, petromag_z_SDSS, petromagerr_z_SDSS, petror50_z_SDSS FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=5 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err, mag_radius) SELECT R2id_obj, petromag_g_DES, petromagerr_g_DES, petrorad_g_DES FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=6 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err, mag_radius) SELECT R2id_obj, petromag_r_DES, petromagerr_r_DES, petrorad_r_DES FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=7 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err, mag_radius) SELECT R2id_obj, petromag_i_DES, petromagerr_i_DES, petrorad_i_DES FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=8 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err, mag_radius) SELECT R2id_obj, petromag_z_DES, petromagerr_z_DES, petrorad_z_DES FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=9 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err, mag_radius) SELECT R2id_obj, petromag_y_DES, petromagerr_y_DES, petrorad_y_DES FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=10 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err, mag_radius) SELECT R2id_obj, kronmag_u_KIDS, kronmagerr_u_KIDS, kronrad_KIDS FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=11 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='kron' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err, mag_radius) SELECT R2id_obj, kronmag_g_KIDS, kronmagerr_g_KIDS, kronrad_KIDS FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=12 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='kron' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err, mag_radius) SELECT R2id_obj, kronmag_r_KIDS, kronmagerr_r_KIDS, kronrad_KIDS FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=13 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='kron' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err, mag_radius) SELECT R2id_obj, kronmag_i_KIDS, kronmagerr_i_KIDS, kronrad_KIDS FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=14 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='kron' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_u_SkyMapper, petromagerr_u_SkyMapper FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=15 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_v_SkyMapper, petromagerr_v_SkyMapper FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=16 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_g_SkyMapper, petromagerr_g_SkyMapper FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=17 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_r_SkyMapper, petromagerr_r_SkyMapper FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=18 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_i_SkyMapper, petromagerr_i_SkyMapper FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=19 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_z_SkyMapper, petromagerr_z_SkyMapper FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=20 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_J_UHS, petromagerr_J_UHS FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=21 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_Y_UKIDSS, petromagerr_Y_UKIDSS FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=22 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_J_UKIDSS, petromagerr_J_UKIDSS FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=23 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_H_UKIDSS, petromagerr_H_UKIDSS FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=24 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_K_UKIDSS, petromagerr_K_UKIDSS FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=25 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_y_VHS, petromagerr_y_VHS FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=26 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_j_VHS, petromagerr_j_VHS FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=27 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_h_VHS, petromagerr_h_VHS FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=28 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_ks_VHS, petromagerr_ks_VHS FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=29 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_Z_VIKING, petromagerr_Z_VIKING FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=30 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_Y_VIKING, petromagerr_Y_VIKING FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=31 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_J_VIKING, petromagerr_J_VIKING FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=32 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_H_VIKING, petromagerr_H_VIKING FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=33 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_Ks_VIKING, petromagerr_Ks_VIKING FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=34 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_U_VST, petromagerr_U_VST FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=35 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_G_VST, petromagerr_G_VST FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=36 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_R_VST, petromagerr_R_VST FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=37 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_I_VST, petromagerr_I_VST FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=38 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_Z_VST, petromagerr_Z_VST FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=39 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, integmag_g_legacy, integmagerr_g_legacy FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=40 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='integral' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, integmag_r_legacy, integmagerr_r_legacy FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=41 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='integral' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, integmag_z_legacy, integmagerr_z_legacy FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=42 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='integral' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_g_PanSTARRS, petromagerr_g_PanSTARRS FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=43 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_r_PanSTARRS, petromagerr_r_PanSTARRS FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=44 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_i_PanSTARRS, petromagerr_i_PanSTARRS FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=45 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_z_PanSTARRS, petromagerr_z_PanSTARRS FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=46 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, petromag_y_PanSTARRS, petromagerr_y_PanSTARRS FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=47 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='petro' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, integmag_w1_unwise, integmagerr_w1_unwise FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=48 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='integral' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, integmag_w2_unwise, integmagerr_w2_unwise FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=49 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='integral' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, integmag_w3_unwise, integmagerr_w3_unwise FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=50 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='integral' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, integmag_w4_unwise, integmagerr_w4_unwise FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=51 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='integral' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, kronmag_NUV_GALEX, kronmagerr_NUV_GALEX FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=52 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='kron' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;
INSERT INTO raw_tot_phot (r2id_obj, mag, mag_err) SELECT R2id_obj, kronmag_FUV_GALEX, kronmagerr_FUV_GALEX FROM tot_phot_spec_match;
UPDATE raw_tot_phot SET filter_id=53 WHERE filter_id is null;
UPDATE raw_tot_phot SET mag_type='kron' WHERE mag_type is null;
DELETE FROM raw_tot_phot WHERE mag is null;



CREATE TABLE aper_phot (r2id_spec int, source varchar [] default array['sdss', 'ukidss'], aper_size float, FUV float, FUV_err float, kcorr_FUV float, NUV float, NUV_err float, kcorr_NUV float, u float, u_err float, kcorr_u float, g float, g_err float, kcorr_g float, r float, r_err float, kcorr_r float, i float, i_err float, kcorr_i float, z float, z_err float, kcorr_z float, Y float, Y_err float, kcorr_Y float, J float, J_err float, kcorr_J float, H float, H_err float, kcorr_H float, K float, K_err float, kcorr_K float, w1 float, w1_err float, kcorr_w1 float, w2 float, w2_err float, kcorr_w2 float, w3 float, w3_err float, kcorr_w3 float, w4 float, w4_err float, kcorr_w4 float);

INSERT INTO aper_phot (r2id_spec, aper_size) SELECT distinct on (a.r2id_spec) a.r2id_spec, a.aper_size FROM raw_aper_phot AS a;

UPDATE aper_phot SET FUV = mag FROM raw_aper_phot WHERE filter_id=53 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET FUV_err = mag_err FROM raw_aper_phot WHERE filter_id=53 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET NUV = mag FROM raw_aper_phot WHERE filter_id=52 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET NUV_err = mag_err FROM raw_aper_phot WHERE filter_id=52 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET u = mag FROM raw_aper_phot WHERE filter_id=1 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET u_err = mag_err FROM raw_aper_phot WHERE filter_id=1 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET g = mag FROM raw_aper_phot WHERE filter_id=2 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET g_err = mag_err FROM raw_aper_phot WHERE filter_id=2 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET r = mag FROM raw_aper_phot WHERE filter_id=3 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET r_err = mag_err FROM raw_aper_phot WHERE filter_id=3 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET i = mag FROM raw_aper_phot WHERE filter_id=4 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET i_err = mag_err FROM raw_aper_phot WHERE filter_id=4 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET z = mag FROM raw_aper_phot WHERE filter_id=5 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET z_err = mag_err FROM raw_aper_phot WHERE filter_id=5 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET Y = mag FROM raw_aper_phot WHERE filter_id=22 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET Y_err = mag_err FROM raw_aper_phot WHERE filter_id=22 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET J = mag FROM raw_aper_phot WHERE filter_id=23 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET J_err = mag_err FROM raw_aper_phot WHERE filter_id=23 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET H = mag FROM raw_aper_phot WHERE filter_id=24 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET H_err = mag_err FROM raw_aper_phot WHERE filter_id=24 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET K = mag FROM raw_aper_phot WHERE filter_id=25 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET K_err = mag_err FROM raw_aper_phot WHERE filter_id=25 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET w1 = mag FROM raw_aper_phot WHERE filter_id=48 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET w1_err = mag_err FROM raw_aper_phot WHERE filter_id=48 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET w2 = mag FROM raw_aper_phot WHERE filter_id=49 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET w2_err = mag_err FROM raw_aper_phot WHERE filter_id=49 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET w3 = mag FROM raw_aper_phot WHERE filter_id=50 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET w3_err = mag_err FROM raw_aper_phot WHERE filter_id=50 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET w4 = mag FROM raw_aper_phot WHERE filter_id=51 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;
UPDATE aper_phot SET w4_err = mag_err FROM raw_aper_phot WHERE filter_id=51 AND aper_phot.r2id_spec=raw_aper_phot.r2id_spec;

DELETE FROM aper_phot WHERE FUV is null AND FUV_err is null AND kcorr_FUV is null AND NUV is null AND NUV_err is null AND kcorr_NUV is null AND u is null AND u_err is null AND kcorr_u is null AND g is null AND g_err is null AND kcorr_g is null AND r is null AND r_err is null AND kcorr_r is null AND i is null AND i_err is null AND kcorr_i is null AND z is null AND z_err is null AND kcorr_z is null AND Y is null AND Y_err is null AND kcorr_Y is null AND J is null AND J_err is null AND kcorr_J is null AND H is null AND H_err is null AND kcorr_H is null AND K is null AND K_err is null AND kcorr_K is null AND w1 is null AND w1_err is null AND kcorr_w1 is null AND w2 is null AND w2_err is null AND kcorr_w2 is null AND w3 is null AND w3_err is null AND kcorr_w3 is null AND w4 is null AND w4_err is null AND kcorr_w4 is null;


=============================================================================
TOT PHOT
=============================================================================

CREATE TABLE tot_phot (r2id_obj int, source varchar [] default array['sdss', 'ukidss'], mag_type varchar [] default array['kron', 'petro', 'petro', 'integ'], FUV float, FUV_err float, FUV_size float, kcorr_FUV float, NUV float, NUV_err float, NUV_size float, kcorr_NUV float, u float, u_err float, u_size float, kcorr_u float, g float, g_err float, g_size float, kcorr_g float, r float, r_err float, r_size float, kcorr_r float, i float, i_err float, i_size float, kcorr_i float, z float, z_err float, z_size float, kcorr_z float, Y float, Y_err float, Y_size float, kcorr_Y float, J float, J_err float, J_size float, kcorr_J float, H float, H_err float, H_size float, kcorr_H float, K float, K_err float, K_size float, kcorr_K float, w1 float, w1_err float, w1_size float, kcorr_w1 float, w2 float, w2_err float, w2_size float, kcorr_w2 float, w3 float, w3_err float, w3_size float, kcorr_w3 float, w4 float, w4_err float, w4_size float, kcorr_w4 float);

INSERT INTO tot_phot (r2id_obj) SELECT distinct on (a.r2id_obj) a.r2id_obj FROM raw_tot_phot AS a;

UPDATE tot_phot SET FUV = mag FROM raw_tot_phot WHERE filter_id=53 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET FUV_err = mag_err FROM raw_tot_phot WHERE filter_id=53 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET NUV = mag FROM raw_tot_phot WHERE filter_id=52 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET NUV_err = mag_err FROM raw_tot_phot WHERE filter_id=52 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET u = mag FROM raw_tot_phot WHERE filter_id=1 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET u_err = mag_err FROM raw_tot_phot WHERE filter_id=1 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET g = mag FROM raw_tot_phot WHERE filter_id=2 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET g_err = mag_err FROM raw_tot_phot WHERE filter_id=2 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET r = mag FROM raw_tot_phot WHERE filter_id=3 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET r_err = mag_err FROM raw_tot_phot WHERE filter_id=3 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET i = mag FROM raw_tot_phot WHERE filter_id=4 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET i_err = mag_err FROM raw_tot_phot WHERE filter_id=4 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET z = mag FROM raw_tot_phot WHERE filter_id=5 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET z_err = mag_err FROM raw_tot_phot WHERE filter_id=5 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET Y = mag FROM raw_tot_phot WHERE filter_id=22 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET Y_err = mag_err FROM raw_tot_phot WHERE filter_id=22 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET J = mag FROM raw_tot_phot WHERE filter_id=23 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET J_err = mag_err FROM raw_tot_phot WHERE filter_id=23 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET H = mag FROM raw_tot_phot WHERE filter_id=24 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET H_err = mag_err FROM raw_tot_phot WHERE filter_id=24 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET K = mag FROM raw_tot_phot WHERE filter_id=25 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET K_err = mag_err FROM raw_tot_phot WHERE filter_id=25 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET w1 = mag FROM raw_tot_phot WHERE filter_id=48 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET w1_err = mag_err FROM raw_tot_phot WHERE filter_id=48 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET w2 = mag FROM raw_tot_phot WHERE filter_id=49 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET w2_err = mag_err FROM raw_tot_phot WHERE filter_id=49 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET w3 = mag FROM raw_tot_phot WHERE filter_id=50 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET w3_err = mag_err FROM raw_tot_phot WHERE filter_id=50 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET w4 = mag FROM raw_tot_phot WHERE filter_id=51 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET w4_err = mag_err FROM raw_tot_phot WHERE filter_id=51 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;

UPDATE tot_phot SET u_size = mag_radius FROM raw_tot_phot WHERE filter_id=1 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET g_size = mag_radius FROM raw_tot_phot WHERE filter_id=2 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET r_size = mag_radius FROM raw_tot_phot WHERE filter_id=3 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET i_size = mag_radius FROM raw_tot_phot WHERE filter_id=4 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;
UPDATE tot_phot SET z_size = mag_radius FROM raw_tot_phot WHERE filter_id=5 AND tot_phot.r2id_obj=raw_tot_phot.r2id_obj;


DELETE FROM tot_phot WHERE FUV is null AND FUV_err is null AND kcorr_FUV is null AND NUV is null AND NUV_err is null AND kcorr_NUV is null AND u is null AND u_err is null AND kcorr_u is null AND g is null AND g_err is null AND kcorr_g is null AND r is null AND r_err is null AND kcorr_r is null AND i is null AND i_err is null AND kcorr_i is null AND z is null AND z_err is null AND kcorr_z is null AND Y is null AND Y_err is null AND kcorr_Y is null AND J is null AND J_err is null AND kcorr_J is null AND H is null AND H_err is null AND kcorr_H is null AND K is null AND K_err is null AND kcorr_K is null AND w1 is null AND w1_err is null AND kcorr_w1 is null AND w2 is null AND w2_err is null AND kcorr_w2 is null;


ALTER TABLE aper_phot ADD CONSTRAINT photaperfk FOREIGN KEY (r2id_spec) REFERENCES full_spec_spoint (r2id_spec);
ALTER TABLE tot_phot ADD CONSTRAINT photfullfk FOREIGN KEY (r2id_obj) REFERENCES rcsed_galaxies (r2id_obj);


GRANT SELECT ON rcsed_galaxies TO dj_user;
GRANT SELECT ON full_spec_spoint TO dj_user;
GRANT SELECT ON raw_aper_phot TO dj_user;
GRANT SELECT ON raw_tot_phot TO dj_user;
GRANT SELECT ON aper_phot TO dj_user;
GRANT SELECT ON tot_phot TO dj_user;

















































, , 
