
create database ap_db in ap_dbs;   -- ap beta db on thumper

create row type coordinate_t (
   time float,
   ra float,
   dec float
);

create row type threshold_t (
   coadds integer not null,
   threshold float not null
);

create row type data_description_t (
    start coordinate_t,
    end coordinate_t,
    block_num integer,
    nsamples integer
);

create table tape (
    id serial not null,
    name char(20) not null,
    start_time float not null,
    last_block_time float not null,
    last_block_done integer not null,
    wu_count integer not null,
    beam integer not null,
    polarization integer not null,
    unique (tapename,beam,polarization)  constraint uniq_namebeam,
    primary key (id) 
);

create table analysis_config (
    id serial not null,
    dm_low integer not null,
    dm_high integer not null,
    dm_chunk_small integer not null,
    dm_chunk_large integer not null,
    fold_buf_bytes_short integer not null,
    max_coadd integer not null,
    fft_len integer not null,
    remove_radar integer not null,
    high_pass smallfloat,
    ffa_threshold smallfloat,
    thresholds list(threshold_t not null),
    primary key (id)
);

create table recorder_config
(
    id serial primary key,
    name char(64),
    bits_per_sample integer,
    sample_rate float,
    beams integer,
    version smallfloat unique
);

create table receiver_config (
    id serial primary key, 
    s4_id integer unique,
    name varchar(255) unique,
    beam_width smallfloat,     -- degrees 
    center_freq float,          -- MHz  
    latitude float,
    longitude float,
    elevation float,
    diameter smallfloat,
    az_orientation float,
    az_corr_coeff  list(float not null), 
    zen_corr_coeff list(float not null),
    array_az_ellipse float,
    array_za_ellipse float,
    array_angle float
);

create table splitter_config (
     id serial primary key,
     version integer,
     blocks_per_wu integer
);

create table settings (
    id serial primary key,
    active integer,
    receiver_cfg integer references receiver_config,
    recorder_cfg integer references recorder_config,
    splitter_cfg integer references splitter_config,
    analysis_cfg integer references analysis_config
);

create table workunit_header (
    id serial8 not null,
    name varchar(128) not null,
    tape_info integer references tape,
    receiver_cfg integer references receiver_config,
    recorder_cfg integer references recorder_config,
    splitter_cfg integer references splitter_config,
    analysis_cfg integer references analysis_config,
    data_desc data_description_t,
    primary key (id) 
);

create synonym workunit for workunit_header;

create table result (
    id serial8 primary key,
    boinc_result int8 not null,
    wuid int8 references workunit_header,
    received float not null,
    hostid integer not null,
    versionid integer not null,
    fraction_blanked float not null,
    return_code integer not null,
    overflow smallint not null,
    reserved integer not null
);

create table ap_signal (
    id serial8 primary key,
    resultid int8 references result,
    fft_num integer not null,
    peak_bin integer not null,
    peak_power float not null,
    scale integer not null,
    dm integer not null,
    period float not null,
    ffa_scale integer not null,
    num_std_devs float not null,
    ra smallfloat not null,
    decl smallfloat not null,
    time float not null,
    qpix integer not null,
    rfi_found integer not null,
    reserved integer not null,
    time_series_len integer not null,
    raw_data_len integer not null,
    time_series byte,	-- binary
    raw_data byte 	-- binary
);

create table rfi (
    id serial8 primary key,
    ap_signalid int8 references ap_signal,
    chisq float not null,
    log_prob float not null,
    chisq_time float not null,
    rep_at_dm int not null,
    rep_at_dm_time float not null,
    fraction_blanked float not null,
    user_marked int not null,
    user_marked_time float not null,
    edge_pulse_diff float not null,
    edge_pulse_time float not null,
    multi_beams int not null,
    multi_beams_time float not null
);
