;+
; PROJECT
;           SolarB EIS
;
; NAME
;           db_save_study_entry_update
;
; PURPOSE
;           Function to update an eis_study in the EIS study databse
;
; CATEGORY
;           EIS Technical Planning
;           Database
;
; INPUTS:
;           eis_study - an eis_study structure
;
; OPTIONAL INPUTS:
;           the top-level-base; useful if calling from a widget tool 
;           (eg; mk_raster). 
;
; WRITTEN
;           John A Rainnie Dec-2005
;
; HISTORY:
;           v0.1 JAR 2-Apr-2007
;               Removed STO P
;
;-
;************************************************************************
PRO db_save_study_entry_update , eis_study , tlbID = tlbID

defsysv,'!PRIV', 2

status   = eis_open_db(/STUDY,/UPDATE)
filter   = STRJOIN(['ID',STRCOMPRESS(eis_study.id,/REMOVE)],'=',/SINGLE)
entry    = dbfind(filter , /SILENT , count = count)
nRasters = dbval(entry[0],'N_RASTERS')
IF (nRasters NE eis_study.nRasters) THEN BEGIN
    BEEP
    mText = "Number of rasters has changed. Save as a new study. Aborting!"
    complain = DIALOG_MESSAGE( mText ,                                      $
                               DIALOG_PARENT = tlbID,                       $
                               /INFORMATION )
    RETURN
ENDIF

;
; AEC Event Properties
;
aec = eis_study.aec_response
e1 = long(aec.respond_to_aec)
;
; EIS Flare Properties
;
e2 = lonarr(5)
flare = eis_study.eis_flare_response
    e2[0] = flare.respond_to_eis_flare
    e2[1] = flare.response_study_id
    e2[2] = flare.x_repoint
    e2[3] = flare.y_repoint
    e2[4] = flare.repoint_raster_index
;
; EIS Event Properties
;
e3 = lonarr(5)
event = eis_study.eis_event_response
    e3[0] = event.respond_to_eis_event
    e3[1] = event.response_study_id
    e3[2] = event.x_repoint
    e3[3] = event.y_repoint
    e3[4] = event.repoint_raster_index
;
; XRT Flare Properties
;
e4 = lonarr(7)
xrt = eis_study.xrt_flare_response
    e4[0] = xrt.respond_to_xrt_flare
    e4[1] = xrt.response_study_id
    e4[2] = xrt.x_repoint
    e4[3] = xrt.y_repoint
    e4[4] = xrt.repoint_raster_index
    e4[5] = xrt.xrt_flare_in_FOV
    e4[6] = xrt.follow_on_study_id

;
; Update database entry with new values
;
dbput, 'WRITE_LOCKED' , eis_study.write_locked  , eis_study.id
dbput, 'ID'           , eis_study.id            , eis_study.id
dbput, 'ACRONYM'      , eis_study.acronym       , eis_study.id
dbput, 'TITLE'        , eis_study.title         , eis_study.id
dbput, 'TYPE'         , eis_study.type          , eis_study.id
dbput, 'TARGET'       , eis_study.target[*]     , eis_study.id
dbput, 'AUTHOR'       , eis_study.author        , eis_study.id
dbput, 'CATEGORY'     , eis_study.category[*]   , eis_study.id
dbput, 'DATE'         , eis_study.date          , eis_study.id
dbput, 'DURATION'     , eis_study.duration      , eis_study.id
dbput, 'DATA_VOLUME'  , eis_study.volume        , eis_study.id
dbput, 'N_RASTERS'    , eis_study.nRasters      , eis_study.id
dbput, 'RECURSIVE'    , eis_study.recursive     , eis_study.id
dbput, 'SLIT_SLOT'    , eis_study.slit_slot[*]  , eis_study.id
dbput, 'AEC_RESPONSE' , e1                      , eis_study.id
dbput, 'EIS_FLR_RES'  , e2                      , eis_study.id
dbput, 'EIS_EVT_RES'  , e3                      , eis_study.id
dbput, 'XRT_FLR_RES'  , e4                      , eis_study.id
dbclose

;
; OK, time to save studyRasters
;
FOR i = 0, eis_study.nRasters - 1 DO BEGIN

    ; Open database for writing the new StudyRaster entry
    status = eis_open_db(/STDRA,/UPDATE)

    sra   = eis_study.studyRasters[i]
    aec   = sra.aec_props
    e1    = LONARR(6)
    e1[0] = aec.upper_threshold
    e1[1] = aec.lower_threshold
    e1[2] = aec.upper_count_limit
    e1[3] = aec.lower_count_limit
    e1[4] = aec.exposure_id
    e1[5] = aec.max_run_time

    flare = sra.eis_flare_props
    e2    = LONARR(6)
    e2[0] = flare.x_centre
    e2[1] = flare.y_centre
    e2[2] = flare.x_pixel_threshold
    e2[3] = flare.y_pixel_threshold
    e2[4] = flare.x_min_limit
    e2[5] = flare.y_min_limit

    event = sra.eis_event_props
    e3    = LONARR(4)
    e3[0] = event.x_pixel_threshold
    e3[1] = event.y_pixel_threshold
    e3[2] = event.x_min_limit
    e3[3] = event.y_min_limit

    xrt = sra.xrt_flare_props
    e4  = LONG(xrt.xrt_flag)

    constraint = 'SY_ID =' + string(eis_study.id) + ',RA_INDEX =' + string(i)
    entry = dbfind(constraint)
    entry = entry[0]

    dbput, 'SY_ID'            , eis_study.id       , entry
    dbput, 'RA_INDEX'         , sra.ra_index       , entry
    dbput, 'RA_ID'            , sra.ra_id          , entry
    dbput, 'RA_REPEATS'       , sra.ra_repeats     , entry
    dbput, 'RA_NWINDOWS'      , sra.ra_nWindows    , entry
    dbput, 'RA_COMPRESSION'   , sra.ra_compression , entry
    dbput, 'RA_DURATION'      , sra.ra_duration    , entry
    dbput, 'RA_VOLUME'        , sra.ra_volume      , entry
    dbput, 'RA_AEC_FLG'       , aec.aec_flag       , entry
    dbput, 'RA_AEC_PROPS'     , e1                 , entry
    dbput, 'RA_EIS_FLR_FLG'   , flare.flare_flag   , entry
    dbput, 'RA_EIS_FLR_PROPS' , e2                 , entry
    dbput, 'RA_EIS_EVT_FLG'   , event.event_flag   , entry
    dbput, 'RA_EIS_EVT_PROPS' , e3                 , entry
    dbput, 'RA_XRT_PROPS'     , e4                 , entry
    dbput, 'ASRC_ENABLED'     , sra.asrc_enabled   , entry
    dbput, 'ASRC_DIRECTION'   , sra.asrc_direction , entry
    dbput, 'ASRC_SKIP'        , sra.asrc_skip      , entry
    dbput, 'ASRC_NSTEPS'      , sra.asrc_nSteps    , entry


    dbclose

ENDFOR

; ID needs to be in 000001 format
idValue = STRING(format='(I06)',eis_study.ID)
messageText = ["Study has been successfully updated."  ,                    $
               ""                                       ,                   $
               "ID          = " + idValue               ,                   $
               "Description = " + eis_study.acronym  ]
complain = DIALOG_MESSAGE( messageText ,                                    $
                                 DIALOG_PARENT = tlbID,                     $
                                 /INFORMATION )


END
