--
-- $Id: dbSchema.sql,v 1.3 2003/06/07 23:23:39 robert Exp $
--
-- $Name:  $
--
-- $Log: dbSchema.sql,v $
-- Revision 1.3  2003/06/07 23:23:39  robert
-- Change catalog table to use more double precision variables
-- to postpone overflow problem.  Maybe should switch to numeric?
-- Add groups and pairs table to track image sets better.
-- Add color_group and zero_pair for photometry.
-- Add fits table to track desired FITS header values.
-- Add temp_obs_? tables to manage importing and selecting
-- matching stars into main obs_? tables.
-- Add imported table to limit stars mergeStars.pl has to deal
-- with.
--
-- Revision 1.2  2002/02/20 03:58:20  robc
-- Add one set of foreign key restraints.
-- Other set doesn't work with trigger code
-- yet.  Need to investigate.
--
-- Revision 1.1  2002/02/19 14:50:56  robc
-- *** empty log message ***
--


-- DROP TABLE "tycho2";
-- DROP SEQUENCE "star_id_seq";

CREATE SEQUENCE "star_id_seq" START 10000000;
CREATE TABLE "tycho2"
   (
   "star_id" integer PRIMARY KEY DEFAULT nextval('"star_id_seq"'),
   "gsc"     character(12),
   "ra"      real NOT NULL,
   "dec"     real NOT NULL,
   "bt"      real NOT NULL,
   "e_bt"    real NOT NULL,
   "vt"      real NOT NULL,
   "e_vt"    real NOT NULL,
   "prox"    integer NOT NULL
   );

-- DROP TABLE "reference_ubvri";

CREATE TABLE "reference_ubvri"
   (
   "star_id" integer PRIMARY KEY REFERENCES tycho2(star_id) ON DELETE CASCADE,
   "ra"      real NOT NULL,
   "dec"     real NOT NULL,
   "u"       real NOT NULL,
   "b"       real NOT NULL,
   "v"       real NOT NULL,
   "r"       real NOT NULL,
   "i"       real NOT NULL
   );

-- DROP TABLE catalog;

CREATE TABLE catalog
   (
    "star_id"          integer PRIMARY KEY DEFAULT NEXTVAL('"star_id_seq"'),
    "loc_count"        integer DEFAULT 0,

    "ra"               real NOT NULL,
    "ra_sum"           double precision DEFAULT 0,
    "ra_sigma"         real DEFAULT 0,
    "ra_sum_square"    double precision DEFAULT 0,

    "dec"              real NOT NULL,
    "dec_sum"          double precision DEFAULT 0,
    "dec_sigma"        real DEFAULT 0,
    "dec_sum_square"   double precision DEFAULT 0,

    "mag_u_count"      integer DEFAULT 0,
    "mag_u"            real DEFAULT 99,
    "mag_u_sum"        double precision DEFAULT 0,
    "mag_u_sigma"      real DEFAULT 0,
    "mag_u_sum_square" double precision DEFAULT 0,

    "mag_b_count"      integer DEFAULT 0,
    "mag_b"            real DEFAULT 99,
    "mag_b_sum"        double precision DEFAULT 0,
    "mag_b_sigma"      real DEFAULT 0,
    "mag_b_sum_square" double precision DEFAULT 0,

    "mag_v_count"      integer DEFAULT 0,
    "mag_v"            real DEFAULT 99,
    "mag_v_sum"        double precision DEFAULT 0,
    "mag_v_sigma"      real DEFAULT 0,
    "mag_v_sum_square" double precision DEFAULT 0,

    "mag_r_count"      integer DEFAULT 0,
    "mag_r"            real DEFAULT 99,
    "mag_r_sum"        double precision DEFAULT 0,
    "mag_r_sigma"      real DEFAULT 0,
    "mag_r_sum_square" double precision DEFAULT 0,

    "mag_i_count"      integer DEFAULT 0,
    "mag_i"            real DEFAULT 99,
    "mag_i_sum"        double precision DEFAULT 0,
    "mag_i_sigma"      real DEFAULT 0,
    "mag_i_sum_square" double precision DEFAULT 0
    );

-- DROP TABLE "new_reference_loc";

CREATE TABLE "new_reference_loc"
   (
   "star_id" integer PRIMARY KEY REFERENCES catalog(star_id) ON DELETE CASCADE,
   "ra"      real NOT NULL,
   "dec"     real NOT NULL
   );

-- DROP TABLE "site";

CREATE TABLE "sites"
   (
   "site_id" character(1) PRIMARY KEY
   );

-- DROP TABLE "groups"
-- DROP SEQUENCE "groups_group_id_seq";
CREATE SEQUENCE "groups_group_id_seq";

CREATE TABLE "groups"
   (
   "group_id" integer PRIMARY KEY DEFAULT nextval('"groups_group_id_seq"'),
   "site_id" char REFERENCES sites( site_id ) ON DELETE CASCADE,
   "night" integer
   );

-- DROP TABLE "pairs";
-- DROP SEQUENCE "pairs_pair_id_seq";
CREATE SEQUENCE "pairs_pair_id_seq";

CREATE TABLE "pairs"
   (
   "pair_id" integer PRIMARY KEY DEFAULT nextval('"pairs_pair_id_seq"'),
   "group_id" integer REFERENCES groups(group_id) ON DELETE CASCADE
   );

-- DROP TABLE "files";
-- DROP SEQUENCE "file_file_id_seq";
CREATE SEQUENCE "files_file_id_seq";

CREATE TABLE "files"
   (
   "file_id" integer PRIMARY KEY DEFAULT nextval('"files_file_id_seq"'),
   "group_id" integer REFERENCES groups(group_id) ON DELETE CASCADE,
   "pair_id" integer REFERENCES pairs(pair_id) ON DELETE CASCADE,
   "date"    timestamp with time zone NOT NULL,
   "name"    character varying UNIQUE NOT NULL,
   "ra_min"  real DEFAULT 0,
   "ra_max"  real DEFAULT 0,
   "dec_min" real DEFAULT 0,
   "dec_max" real DEFAULT 0
   );

CREATE TABLE "fits"
   (
   "file_id" integer REFERENCES files(file_id) ON DELETE CASCADE,
   "adc" real[],
   "ra"  real,
   "dec" real,
   "darktime" real,
   "exptime" real
   );

-- DROP TABLE "color_group";

CREATE TABLE "color_group"
   (
   "group_id" integer REFERENCES groups(group_id) ON DELETE CASCADE,
   "color_u" real DEFAULT 0,
   "color_b" real DEFAULT 0,
   "color_v" real DEFAULT 0,
   "color_r" real DEFAULT 0,
   "color_i" real DEFAULT 0
   );

-- DROP TABLE "zero_frame"

CREATE TABLE "zero_pair"
   (
   "pair_id" integer PRIMARY KEY REFERENCES pairs(pair_id) ON DELETE CASCADE,
   "zero_u" real DEFAULT 0,
   "zero_b" real DEFAULT 0,
   "zero_v" real DEFAULT 0,
   "zero_r" real DEFAULT 0,
   "zero_i" real DEFAULT 0
   );

-- DROP TABLE "obs_u";
-- DROP TABLE "obs_b";
-- DROP TABLE "obs_v";
-- DROP TABLE "obs_r";
-- DROP TABLE "obs_i";
-- DROP TABLE "obs_root";
-- DROP SEQUENCE "obs_id_seq";

-- Note
-- Do not change the sequence of these entries or add any columns
-- unless you change the copy stage in importSlist.pl
CREATE SEQUENCE "obs_id_seq";
CREATE TABLE "obs_root" (
   "x"       real NOT NULL,
   "y"       real NOT NULL,
   "imag"    real NOT NULL,
   "smag"    real NOT NULL,
   "ra"      real NOT NULL,
   "dec"     real NOT NULL,
   "obs_id"  integer DEFAULT nextval('"obs_id_seq"') NOT NULL,
   "file_id" integer REFERENCES files(file_id) ON DELETE CASCADE,
   "use"     boolean DEFAULT FALSE,
   "solve"   boolean DEFAULT FALSE,
   "star_id" integer REFERENCES catalog(star_id) ON DELETE CASCADE DEFAULT NULL,
   "mag"     real DEFAULT NULL
);

CREATE TABLE "obs_u"( ) INHERITS ("obs_root");
ALTER TABLE "obs_u" ADD CONSTRAINT "obs_u_files_constraint"
   FOREIGN KEY ("file_id") REFERENCES "files"("file_id") ON DELETE CASCADE;
CREATE TABLE "temp_obs_u"( ) INHERITS ("obs_root");
ALTER TABLE "temp_obs_u" ADD CONSTRAINT "temp_obs_u_files_constraint"
   FOREIGN KEY ("file_id") REFERENCES "files"("file_id") ON DELETE CASCADE;
-- ALTER TABLE "obs_u" ADD CONSTRAINT "obs_u_catalog_constraint"
--    FOREIGN KEY ("star_id") REFERENCES "catalog"("star_id") ON DELETE CASCADE;

CREATE TABLE "obs_b"( ) INHERITS ("obs_root");
ALTER TABLE "obs_b" ADD CONSTRAINT "obs_b_files_constraint"
   FOREIGN KEY ("file_id") REFERENCES "files"("file_id") ON DELETE CASCADE;
CREATE TABLE "temp_obs_b"( ) INHERITS ("obs_root");
ALTER TABLE "temp_obs_b" ADD CONSTRAINT "temp_obs_b_files_constraint"
   FOREIGN KEY ("file_id") REFERENCES "files"("file_id") ON DELETE CASCADE;
-- ALTER TABLE "obs_b" ADD CONSTRAINT "obs_b_catalog_constraint"
--    FOREIGN KEY ("star_id") REFERENCES "catalog"("star_id") ON DELETE CASCADE;

CREATE TABLE "obs_v"( ) INHERITS ("obs_root");
ALTER TABLE "obs_v" ADD CONSTRAINT "obs_v_files_constraint"
   FOREIGN KEY ("file_id") REFERENCES "files"("file_id") ON DELETE CASCADE;
CREATE TABLE "temp_obs_v"( ) INHERITS ("obs_root");
ALTER TABLE "temp_obs_v" ADD CONSTRAINT "temp_obs_v_files_constraint"
   FOREIGN KEY ("file_id") REFERENCES "files"("file_id") ON DELETE CASCADE;
-- ALTER TABLE "obs_v" ADD CONSTRAINT "obs_v_catalog_constraint"
--    FOREIGN KEY ("star_id") REFERENCES "catalog"("star_id") ON DELETE CASCADE;

CREATE TABLE "obs_r"( ) INHERITS ("obs_root");
ALTER TABLE "obs_r" ADD CONSTRAINT "obs_r_files_constraint"
   FOREIGN KEY ("file_id") REFERENCES "files"("file_id") ON DELETE CASCADE;
CREATE TABLE "temp_obs_r"( ) INHERITS ("obs_root");
ALTER TABLE "temp_obs_r" ADD CONSTRAINT "temp_obs_r_files_constraint"
   FOREIGN KEY ("file_id") REFERENCES "files"("file_id") ON DELETE CASCADE;
-- ALTER TABLE "obs_r" ADD CONSTRAINT "obs_r_catalog_constraint"
--    FOREIGN KEY ("star_id") REFERENCES "catalog"("star_id") ON DELETE CASCADE;

CREATE TABLE "obs_i"( ) INHERITS ("obs_root");
ALTER TABLE "obs_i" ADD CONSTRAINT "obs_i_files_constraint"
   FOREIGN KEY ("file_id") REFERENCES "files"("file_id") ON DELETE CASCADE;
CREATE TABLE "temp_obs_i"( ) INHERITS ("obs_root");
ALTER TABLE "temp_obs_i" ADD CONSTRAINT "temp_obs_i_files_constraint"
   FOREIGN KEY ("file_id") REFERENCES "files"("file_id") ON DELETE CASCADE;
-- ALTER TABLE "obs_i" ADD CONSTRAINT "obs_i_catalog_constraint"
--    FOREIGN KEY ("star_id") REFERENCES "catalog"("star_id") ON DELETE CASCADE;

-- DROP TABLE "welch_index"

CREATE TABLE "welch_index"
   (
   "star_id" integer REFERENCES catalog(star_id) ON DELETE CASCADE,
   "updated" boolean DEFAULT FALSE,
   "index"   real DEFAULT 0
   );

-- DROP TABLE "imported"

CREATE TABLE "imported"
   (
   "file_id" integer REFERENCES files(file_id) ON DELETE CASCADE
   );
