detRun METADATA
    det_id      S64         0       # Primary Key AUTO_INCREMENT
    iteration   S32         0       # Key INDEX(det_id, iteration)
    det_type    STR         64      # Key
    mode        STR         64      # Key
    state       STR         64      # Key
    filelevel   STR         64
    workdir     STR         255     # destination for output files
    camera      STR         64
    telescope   STR         64
    exp_type    STR         64      # XXX this should be dropped
    reduction   STR         64      # Reduction clas
    filter      STR         64
    airmass_min F32         0.0
    airmass_max F32         0.0
    exp_time_min F32        0.0
    exp_time_max F32        0.0
    ccd_temp_min F32        0.0
    ccd_temp_max F32        0.0
    posang_min  F64         0.0 
    posang_max  F64         0.0 
    registered  TAI         0001-01-01T00:00:00Z
    time_begin  TAI         0001-01-01T00:00:00Z
    time_end    TAI         0001-01-01T00:00:00Z
    use_begin   TAI         0001-01-01T00:00:00Z
    use_end     TAI         0001-01-01T00:00:00Z
    solang_min  F32         0.0
    solang_max  F32         0.0
    label       STR         64      # key
    parent      S32         0       # Key
END

detInputExp METADATA
    det_id      S64         0       # Primary Key fkey(det_id) ref detRun(det_id)
    iteration   S32         0       # Primary Key fkey(exp_id) ref rawExp(exp_id)
    exp_id     S64         64       # Primary Key INDEX(det_id, exp_id)
    include     BOOL        f       # INDEX(det_id, iteration)
END

detProcessedImfile METADATA
    det_id      S64         0       # Primary Key fkey(det_id, exp_id) ref detInputExp(det_id, exp_id) 
    exp_id     S64         64       # Primary Key fkey(exp_id, class_id) ref rawImfile(exp_id, class_id)
    class_id    STR         64      # Primary Key INDEX(det_id, class_id)
    uri         STR         255     # INDEX(det_id, exp_id)
    recipe      STR         64
    bg          F64         0.0
    bg_stdev    F64         0.0
    bg_mean_stdev   F64     0.0
    fringe_0    F64         0.0
    fringe_1    F64         0.0
    fringe_2    F64         0.0
    user_1      F64         0.0
    user_2      F64         0.0
    user_3      F64         0.0
    user_4      F64         0.0
    user_5      F64         0.0
    path_base   STR         255
    fault       S16         0       # Key NOT NULL
END

detProcessedExp METADATA
    det_id      S64         0       # Primary Key fkey(det_id, exp_id) ref detInputExp(det_id, exp_id)
    exp_id     S64         64       # Primary Key fkey(det_id, exp_id) ref detProcessedImfile(det_id, exp_id)
    recipe      STR         64
    bg          F64         0.0
    bg_stdev    F64         0.0
    bg_mean_stdev   F64     0.0
    fringe_0    F64         0.0
    fringe_1    F64         0.0
    fringe_2    F64         0.0
    user_1      F64         0.0
    user_2      F64         0.0
    user_3      F64         0.0
    user_4      F64         0.0
    user_5      F64         0.0
    path_base   STR         255
    fault       S16         0       # Key NOT NULL
END

#
# detStackedImfile does not depend on detProcessedExp. detProcesedExp is purely
# FYI values and can be calculated in parellel
#
detStackedImfile METADATA
    det_id      S64         0       # Primary Key fkey(det_id, iteration) ref detInputExp(det_id, iteration)
    iteration   S32         0       # Primary Key fkey(det_id, class_id) ref detProcessedImfile(det_id, class_id)
    class_id    STR         64      # Primary Key
    uri         STR         255
    recipe      STR         64
    bg          F64         0.0
    bg_stdev    F64         0.0
    bg_mean_stdev   F64     0.0
    user_1      F64         0.0
    user_2      F64         0.0
    user_3      F64         0.0
    user_4      F64         0.0
    user_5      F64         0.0
    fault       S16         0       # Key NOT NULL
END

detNormalizedStatImfile METADATA
    det_id      S64         0       # Primary Key fkey(det_id, iteration) ref detInputExp(det_id, iteration)
    iteration   S32         0       # Primary Key fkey(det_id, iteration, class_id) ref detStackedImfile(det_id, iteration, class_id)
    class_id    STR         64      # Primary Key
    norm        F32         0.0
    fault       S16         0       # Key NOT NULL
END

detNormalizedImfile METADATA
    det_id      S64         0       # Primary Key fkey(det_id) ref detInputExp(det_id)
    iteration   S32         0       # Primary Key fkey(det_id, iteration, class_id) ref detNormalizedStatImfile(det_id, iteration, class_id)
    class_id    STR         64      # Primary Key INDEX(det_id, iteration)
    uri         STR         255
    bg          F64         0.0
    bg_stdev    F64         0.0
    bg_mean_stdev   F64     0.0
    user_1      F64         0.0
    user_2      F64         0.0
    user_3      F64         0.0
    user_4      F64         0.0
    user_5      F64         0.0
    path_base   STR         255
    fault       S16         0       # Key NOT NULL
END

detNormalizedExp METADATA
    det_id      S64         0       # Primary Key fkey(det_id, iteration) ref detInputExp(det_id, iteration)
    iteration   S32         0       # Primary Key fkey(det_id, iteration) ref detNormalizedImfile(det_id, iteration)
    recipe      STR         64
    bg          F64         0.0
    bg_stdev    F64         0.0
    bg_mean_stdev   F64     0.0
    user_1      F64         0.0
    user_2      F64         0.0
    user_3      F64         0.0
    user_4      F64         0.0
    user_5      F64         0.0
    path_base   STR         255
    fault       S16         0       # Key NOT NULL
END

#detMasterFrame METADATA
#    det_id      S64         0       # Primary Key
#    iteration   S32         0       # Primary Key
#    comment     STR         255
#END
#
## drop?
#detMasterImfile METADATA
#    det_id      S64         0       # Primary Key
#    class_id    STR         64      # Primary Key
#    uri         STR         255
#    recipe      STR         64
#END

detResidImfile METADATA
    det_id      S64         0       # Primary Key fkey(det_id, iteration, exp_id) ref detInputExp(det_id, iteration, exp_id)
    iteration   S32         0       # Primary Key fkey(det_id, exp_id, class_id) ref detProcessedImfile(det_id, exp_id, class_id)
    exp_id     S64         64       # Primary Key fkey(det_id, iteration) ref detNormalizedExp(det_id, iteration)
    class_id    STR         64      # Primary Key INDEX(det_id, iteration, exp_id)
    uri         STR         255
    recipe      STR         64
    bg          F64         0.0
    bg_stdev    F64         0.0
    bg_mean_stdev   F64     0.0
    bin_stdev   F64         0.0
    fringe_0    F64         0.0
    fringe_1    F64         0.0
    fringe_2    F64         0.0
    user_1      F64         0.0
    user_2      F64         0.0
    user_3      F64         0.0
    user_4      F64         0.0
    user_5      F64         0.0
    path_base   STR         255
    fault       S16         0       # Key NOT NULL
END

detResidExp METADATA
    det_id      S64         0       # Primary Key fkey(det_id, iteration, exp_id) ref detInputExp(det_id, iteration, exp_id)
    iteration   S32         0       # Primary Key fkey(det_id, iteration, exp_id) ref detResidImfile(det_id, iteration, exp_id)
    exp_id     S64         64       # Primary Key INDEX(det_id, iteration)
    recipe      STR         64
    bg          F64         0.0
    bg_stdev    F64         0.0
    bg_mean_stdev   F64     0.0
    bin_stdev   F64         0.0
    fringe_0    F64         0.0
    fringe_1    F64         0.0
    fringe_2    F64         0.0
    user_1      F64         0.0
    user_2      F64         0.0
    user_3      F64         0.0
    user_4      F64         0.0
    user_5      F64         0.0
    path_base   STR         255
    accept      BOOL        f
    fault       S16         0       # Key NOT NULL
END

detRunSummary METADATA
    det_id      S64         0       # Primary Key fkey(det_id, iteration) ref detInputExp(det_id, iteration)
    iteration   S32         0       # Primary Key fkey(det_id, iteration) ref detResidExp(det_id, iteration)
    bg          F64         0.0
    bg_stdev    F64         0.0
    bg_mean_stdev   F64     0.0
    accept      BOOL        f
    fault       S16         0       # Key NOT NULL
END

#
# Note: This table needs to stay more or less identical to detNormalizedImfile.
# It only exists as a seperate entity so they it can have different fkeys
#
detRegisteredImfile METADATA
    det_id      S64         0       # Primary Key fkey(det_id, iteration) ref detRun(det_id, iteration)
    iteration   S32         0       # Primary Key
    class_id    STR         64      # Primary Key
    uri         STR         255
    bg          F64         0.0
    bg_stdev    F64         0.0
    bg_mean_stdev   F64     0.0
    user_1      F64         0.0
    user_2      F64         0.0
    user_3      F64         0.0
    user_4      F64         0.0
    user_5      F64         0.0
    path_base   STR         255
    fault       S16         0       # Key NOT NULL
END
