;+
;PROJECT:
;           SolarB EIS
;
;NAME:      
;           db_save_study_entry_create
;
;PURPOSE:
;           Function to save an eis_study to the EIS study databse
;
;CATEGORY:
;           EIS Technical Planning
;           Database
;
;INPUTS:
;           an eis_study structure
;
;OPTIONAL INPUTS:
;           the top-level-base; useful if calling from a widget tool 
;           (eg; mk_raster). 
;
; WRITTEN
;           John A Rainnie RAL Dec-2005
;
; HISTORY:
;           v0.1 JAR 22-March-2007
;               Temporarily commented out code to create a study_descriptions
;               file
;
;-
;************************************************************************
PRO db_save_eng_study_entry_create , eis_study , tlbID


defsysv,'!PRIV', 2

eis_study.type     = 1
;eis_study.acronym  = 'comcal000b'
;eis_study.title    = 'Full width bottom half of CCDs to determine hardware window position (10s exposure)'
;eis_study.duration = 3340000
;eis_study.volume   = 33554432
eis_study.target   = 'Engineering'
eis_study.category = 'Engineering'

status = eis_open_db(/STUDY,/UPDATE)

;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
; If we're creating a new database entry, get new ID
;
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
n = db_info('entries')
eis_study.id = n[0] + 1
idValue     = STRING(format='(I06)',eis_study.id)
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Get path of study_descriptions (either USER/OFFICIAL)
path        = eis_get_study_descriptions_path()
baseName    = STRJOIN(['desc',idValue,'.txt'],/SINGLE)
fileName    = CONCAT_DIR(path,baseName)
eis_study.description_file = baseName



aec = eis_study.aec_response
e1 = long(aec.respond_to_aec)

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

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

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

dbbuild                        , $
    eis_study.write_locked     , $
    eis_study.id               , $
    eis_study.acronym          , $
    eis_study.title            , $
    eis_study.type             , $
    eis_study.target[*]        , $
    eis_study.author           , $
    eis_study.category         , $
    eis_study.date             , $
    eis_study.description_file , $
    eis_study.duration         , $
    eis_study.volume           , $
    eis_study.nRasters         , $
    eis_study.recursive        , $
    eis_study.slit_slot[*]     , $
    e1, e2, e3, e4

dbclose

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 = BYTE(xrt.xrt_flag)

    dbbuild                ,     $
    	   eis_study.id       ,     $
    	   sra.ra_index       ,     $
    	   sra.ra_id          ,     $
    	   sra.ra_repeats     ,     $
    	   sra.ra_nwindows    ,     $
    	   sra.ra_compression ,     $
    	   sra.ra_duration    ,     $
    	   sra.ra_volume      ,     $
    	   aec.aec_flag       , e1, $
    	   flare.flare_flag   , e2, $
    	   event.event_flag   , e3, $
    	   e4                 ,     $
        sra.asrc_enabled   ,     $
        sra.asrc_direction ,     $
        sra.asrc_skip      ,     $
        sra.asrc_nSteps

    dbclose

ENDFOR

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



;;
;; Create a description file
;;
;
;print,'Creating ' + fileName
;OPENW  , lun , fileName , /GET_LUN
;PRINTF , lun , STRJOIN(['Study ',idValue,':'],/SINGLE)
;FREE_LUN , lun


END
