;+
; PROJECT
;           SOLARB EIS
;
; NAME
;           EIS_MK_STUDY_GUI__DEFINE
;
; PURPOSE
;           eis_mk_study GUI class definition
;
; NOTES
;           The
;
; CATEGORY
;           EIS technical planning
;
; WRITTEN
;           John A Rainne RAL
;
; VERSION
;           v0.1 JAR 2-Apr-2007
;               Replaced obsolete study_raster tag with slitindex
;           v0.2 JAR 4-Apr-2007
;               Updated compression tag to DPCM
;           v0.3 JAR 24-Apr-2007
;               Updated slot to 266".
;           v0.4 JAR 26-Nov-2007
;               Check for valid eis_study structure when reading database
;
;------------------------------------------------------------------------------;
FUNCTION eis_mk_study_gui::Init

;replace call to astrolib
    defsysv,'!DEBUG', 0
    defsysv,'!TEXTUNIT', 0
    defsysv,'!PRIV', 2
    defsysv,'!TEXTOUT', EXIST = EXIST

;-------------------create the main base---------------------------------------;
pt_version = eis_get_pt_version_number()
widTitle = 'EIS Make Study Tool' + pt_version

TLB_EVENT                   = {eis_mk_study_gui_Event}
TLB_EVENT.eis_mk_study_gui  = Self
TLB_EVENT.Type              = "TLB"
Self.tlbID                  = WIDGET_BASE(                                  $
                                    TITLE       = widTitle,                 $
                                    COLUMN      = 1,                        $
                                    mbar        = bar,                      $
                                    UValue      = TLB_EVENT)

;-------------------create the GUI------------------------------;

Event_Pro_update = 'eis_mk_study_gui_update_EV'
Event_Pro        = 'eis_mk_study_gui_EV'

;;--------file menu list--------------------------------------------------------;
;file_menu	                  = WIDGET_BUTTON(bar, VALUE='Exit', /MENU)

;------------------------------------------------------------------------------;
;--------study menu list------------------------------------------------------;
study_menu	                 = WIDGET_BUTTON(bar, VALUE='Study', /MENU)

SEARCH_RA_EVENT             = {eis_mk_study_gui_Event}
SEARCH_RA_EVENT.eis_mk_study_gui = Self
SEARCH_RA_EVENT.Type        = "RASTER_DB"
value = 'Insert raster(s) from database...'
SEARCH_RAbutton             = WIDGET_BUTTON(study_menu,                     $
                                    value       = value     ,               $
                                    Event_Pro   = 'eis_mk_study_gui_EV',    $
                                    uValue      = SEARCH_RA_EVENT)

;IMPORT_RA_EVENT             = {eis_mk_study_gui_Event}
;IMPORT_RA_EVENT.eis_mk_study_gui = Self
;IMPORT_RA_EVENT.Type        = "RASTER_IMPORT"
;value = 'Import raster...'
;IMPORT_RAbutton             = WIDGET_BUTTON(study_menu,                     $
;                                    value       = value     ,               $
;                                    Event_Pro   = Event_Pro_update     ,    $
;                                    uValue      = IMPORT_RA_EVENT)

DEL_RAS_EVENT               = {eis_mk_study_gui_Event}
DEL_RAS_EVENT.eis_mk_study_gui = Self
DEL_RAS_EVENT.Type          = "DELETE"
value = 'Remove selected raster'
button                      = WIDGET_BUTTON(study_menu,                     $
                                    value       = value            ,        $
                                    Event_Pro   = Event_Pro_update ,        $
                                    uValue      = DEL_RAS_EVENT)

CLEAR_EVENT                 = {eis_mk_study_gui_Event}
CLEAR_EVENT.eis_mk_study_gui = Self
CLEAR_EVENT.Type            = "CLEAR"
st_rasterCLEARButton        = WIDGET_BUTTON(study_menu    ,                 $
                                    value       = 'Remove all rasters'  ,   $
                                    Event_Pro   = Event_Pro_update     ,    $
                                    uValue      = CLEAR_EVENT)

SEARCH_ST_EVENT             = {eis_mk_study_gui_Event}
SEARCH_ST_EVENT.eis_mk_study_gui = Self
SEARCH_ST_EVENT.Type        = "STUDY_DB"
SEARCH_STbutton             = WIDGET_BUTTON(study_menu,                     $
                                    value       = 'Load study from database...',  $
                                    Event_Pro   = 'eis_mk_study_gui_EV',    $
                                    uValue      = SEARCH_ST_EVENT,           $
                                    /SEPARATOR)

ST_SAVE_EVENT               = {eis_mk_study_gui_Event}
ST_SAVE_EVENT.eis_mk_study_gui = Self
ST_SAVE_EVENT.Type          = "STUDYSAVEDB"
value = 'Save in database...'
ST_SAVEbutton               = WIDGET_BUTTON(study_menu,                     $
                                    value       = value  ,                  $
                                    Event_Pro   = 'eis_mk_study_gui_EV',    $
                                    uValue      = ST_SAVE_EVENT )

ST_ASCII_EVENT              = {eis_mk_study_gui_Event}
ST_ASCII_EVENT.eis_mk_study_gui = Self
ST_ASCII_EVENT.Type         = "ST_EXPORT"
ST_ASCIIbutton              = WIDGET_BUTTON(study_menu,                     $
                                    value       = 'Export study...'   ,     $
                                    Event_Pro   = 'eis_mk_study_gui_EV',    $
                                    uValue      = ST_ASCII_EVENT,           $
                                    /SEPARATOR)

ST_IMPORT_EVENT             = {eis_mk_study_gui_Event}
ST_IMPORT_EVENT.eis_mk_study_gui = Self
ST_IMPORT_EVENT.Type        = "ST_IMPORT"
ST_IMPORTbutton             = WIDGET_BUTTON(study_menu,                     $
                                    value       = 'Import study...'  ,      $
                                    Event_Pro   = 'eis_mk_study_gui_EV',    $
                                    uValue      = ST_IMPORT_EVENT)

DESC_EVENT                  = {eis_mk_study_gui_Event}
DESC_EVENT.eis_mk_study_gui = Self
DESC_EVENT.Type             = "DESC"
value = 'View/Edit Study Description...'
DESCbutton                  = WIDGET_BUTTON(study_menu,                     $
                                    value       = value ,                   $
                                    Event_Pro   = 'eis_mk_study_gui_EV',    $
                                    uValue      = DESC_EVENT ,           $
                                    /SEPARATOR)

QUIT_EVENT                  = {eis_mk_study_gui_Event}
QUIT_EVENT.eis_mk_study_gui = Self
QUIT_EVENT.Type             = "QUIT"
quitbutton                  = WIDGET_BUTTON(study_menu,                      $
                                    value       = 'Exit',                   $
                                    Event_Pro   = 'eis_mk_study_gui_EV',    $
                                    uValue      = QUIT_EVENT ,           $
                                    /SEPARATOR)

;------------------------------------------------------------------------------;
;--------edit menu list--------------------------------------------------------;
edit_menu	                  = WIDGET_BUTTON(bar, VALUE='Edit', /MENU)

UNDO_EVENT                  = {eis_mk_study_gui_Event}
UNDO_EVENT.eis_mk_study_gui = Self
UNDO_EVENT.Type             = "UNDO"
UNDObutton                  = WIDGET_BUTTON(edit_menu,                      $
                                    value       = 'Undo'   ,                $
                                    Event_Pro   = 'eis_mk_study_gui_EV',   $
                                    uValue      = UNDO_EVENT)

;STOP_EVENT                  = {eis_mk_study_gui_Event}
;STOP_EVENT.eis_mk_study_gui = Self
;STOP_EVENT.Type             = "STOP"
;STOPbutton                  = WIDGET_BUTTON(edit_menu,                      $
;                                    value       = 'STOP'   ,                $
;                                    Event_Pro   = 'eis_mk_study_gui_EV',   $
;                                    uValue      = STOP_EVENT)

timeButtonId                = WIDGET_BUTTON(edit_menu ,                     $
                                    value       = 'Time format' ,           $
                                    /MENU)

THMS_EVENT                  = {eis_mk_study_gui_Event}
THMS_EVENT.eis_mk_study_gui = Self
THMS_EVENT.Type             = "SETTIMEUNIT"
THMS_EVENT.extra            = 0
THMSbutton                  = WIDGET_BUTTON(timeButtonId,                   $
                                    value       = 'h:m:s',                  $
                                    Event_Pro   = 'eis_mk_study_gui_EV',   $
                                    uValue      = THMS_EVENT)

HOUR_EVENT                  = {eis_mk_study_gui_Event}
HOUR_EVENT.eis_mk_study_gui = Self
HOUR_EVENT.Type             = "SETTIMEUNIT"
HOUR_EVENT.extra            = 1
HOURbutton                  = WIDGET_BUTTON(timeButtonId,                   $
                                    value       = 'hours',                  $
                                    Event_Pro   = 'eis_mk_study_gui_EV',   $
                                    uValue      = HOUR_EVENT)

MINS_EVENT                  = {eis_mk_study_gui_Event}
MINS_EVENT.eis_mk_study_gui = Self
MINS_EVENT.Type             = "SETTIMEUNIT"
MINS_EVENT.extra            = 2
MINSbutton                  = WIDGET_BUTTON(timeButtonId,                   $
                                    value       = 'minutes',                $
                                    Event_Pro   = 'eis_mk_study_gui_EV',   $
                                    uValue      = MINS_EVENT)

SECS_EVENT                  = {eis_mk_study_gui_Event}
SECS_EVENT.eis_mk_study_gui = Self
SECS_EVENT.Type             = "SETTIMEUNIT"
SECS_EVENT.extra            = 3
SECSbutton                  = WIDGET_BUTTON(timeButtonId,                   $
                                    value       = 'seconds',                $
                                    Event_Pro   = 'eis_mk_study_gui_EV',   $
                                    uValue      = SECS_EVENT)
;--------help menu list--------------------------------------------------------;
help_menu	            = WIDGET_BUTTON(bar,VALUE='Help',/MENU,/help)

HELP_EVENT                  = {eis_mk_study_gui_Event}
HELP_EVENT.eis_mk_study_gui = Self
HELP_EVENT.Type             = "HELP"
HELPButton                  = WIDGET_BUTTON(help_menu,                      $
                                    value       = 'Help...',                $
                                    Event_Pro   = 'eis_mk_study_gui_EV',    $
                                    uValue      = HELP_EVENT)

;ABOUT_EVENT                 = {eis_mk_study_gui_Event}
;ABOUT_EVENT.eis_mk_study_gui = Self
;ABOUT_EVENT.Type            = "ABOUT"
;ABOUTButton                 = WIDGET_BUTTON(help_menu,                      $
;                                    value       = 'About...',               $
;                                    Event_Pro   = 'eis_mk_study_gui_EV',    $
;                                    uValue      = ABOUT_EVENT)

VERS_EVENT                  = {eis_mk_study_gui_Event}
VERS_EVENT.eis_mk_study_gui    = Self
VERS_EVENT.Type             = "VERS"
VERSButton                  = WIDGET_BUTTON(help_menu,                      $
                                    value       = 'Version...',             $
                                    Event_Pro   = 'eis_mk_study_gui_EV',    $
                                    uValue      = VERS_EVENT)

; Call widget creation procedure
eis_mk_study_widgets , Self
;
; Load study - default is first entry ( = 1)
;Self.eis_study = eis_get_technical_study_entry( 1 )
Self.eis_study = db_read_study_entry(1)

; If flags not set, then default x/y_repoint to 1
;
; EIS Event
;
eisEvent_on = Self.eis_study.eis_event_response.respond_to_eis_event
IF (eisEvent_on EQ 0) THEN BEGIN
    Self.eis_study.eis_event_response.x_repoint = 1
    Self.eis_study.eis_event_response.y_repoint = 1
ENDIF
;
; EIS Flare
;
eisFlare_on = Self.eis_study.eis_flare_response.respond_to_eis_flare
IF (eisFlare_on EQ 0) THEN BEGIN
    Self.eis_study.eis_flare_response.x_repoint = 1
    Self.eis_study.eis_flare_response.y_repoint = 1
ENDIF
;
; XRT Flare
;
xrtFlare_on = Self.eis_study.xrt_flare_response.respond_to_xrt_flare
IF (xrtFlare_on EQ 0) THEN BEGIN
    Self.eis_study.xrt_flare_response.x_repoint = 1
    Self.eis_study.xrt_flare_response.y_repoint = 1
    Self.eis_study.xrt_flare_response.xrt_flare_in_fov = 1
ENDIF
;
; AEC Event
;
aecEvent_on = Self.eis_study.aec_response.respond_to_aec
IF (aecEvent_on EQ 0) THEN BEGIN

    FOR i = 0, Self.eis_study.nRasters - 1 DO BEGIN

        Self.eis_study.studyRasters[i].aec_props.upper_threshold = 80
        Self.eis_study.studyRasters[i].aec_props.lower_threshold = 20

        Self.eis_study.studyRasters[i].aec_props.upper_count_limit = 80
        Self.eis_study.studyRasters[i].aec_props.lower_count_limit = 20

    ENDFOR

ENDIF

Self.eis_study.raster_index = -1

; OK, create IDL_CONTAINER - studyUndo will contain all the undo actions
Self.studyUndo = OBJ_NEW("IDL_Container")
; Finally, save a copy of the current eis_study structure
oStudyItem = OBJ_NEW("eis_study_undo" , Self.eis_study)
Self.studyUndo->ADD , oStudyItem

; OK, update eis_study
Self->loadStudy , 1

RETURN, 1

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_mk_study_gui_Cleanup , wid

WIDGET_CONTROL , wid , get_uvalue = eis_mk_study_gui_Event
OBJ_DESTROY    , eis_mk_study_gui_Event.eis_mk_study_gui

;print,"cleaning up...here's what's left over..."
;print,"****************************************"
;print,"****************************************"
;help,/heap
;print,"****************************************"
;print,"****************************************"

END
;______________________________________________________________________________
; Cleanup
;______________________________________________________________________________
PRO eis_mk_study_gui::CleanUp

OBJ_DESTROY , Self.eis_xrt_flare_gui
OBJ_DESTROY , Self.eis_event_gui
OBJ_DESTROY , Self.eis_flare_gui
OBJ_DESTROY , Self.eis_aec_event_gui
; Free up study UNDO objects
undoArray = Self.studyUndo->get(/ALL)
OBJ_DESTROY , undoArray
; And finally the IDL_Container object
OBJ_DESTROY , Self.studyUndo

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_mk_study_gui::setProperty  ,                                        $
            eis_mk_study_widgets  = eis_mk_study_widgets         ,          $
            eis_study             = eis_study                    ,          $
            eis_xrt_flare_control = eis_xrt_flare_control        ,          $
            eis_event_control     = eis_event_control            ,          $
            eis_flare_control     = eis_flare_control            ,          $
            eis_aec_control       = eis_aec_control

IF (N_ELEMENTS(eis_mk_study_widgets) GT 0) THEN BEGIN
    Self.eis_mk_study_widgets    = eis_mk_study_widgets
ENDIF

IF (N_ELEMENTS(eis_study) GT 0) THEN BEGIN
    stop
    Self.eis_study = eis_study
END

IF (N_ELEMENTS(eis_xrt_flare_control) GT 0) THEN BEGIN

    Self.eis_xrt_flare_control = eis_xrt_flare_control
    old_props = Self.eis_study.xrt_flare_response
    new_props = eis_xrtf_control_to_props( old_props , eis_xrt_flare_control)
    Self.eis_study.xrt_flare_response = new_props

ENDIF

IF (N_ELEMENTS(eis_event_control) GT 0) THEN BEGIN
    Self.eis_event_control = eis_event_control

    ra_index  = Self.eis_study.raster_index
    sra_props = Self.eis_study.studyRasters[ra_index].eis_event_props
    sty_props = Self.eis_study.eis_event_response

    new_props = eis_eise_control_to_props( sty_props , sra_props     ,      $
                                           eis_event_control)

    ; Update EIS Event Properties
    Self.eis_study.eis_event_response = new_props.sty_props
    Self.eis_study.studyRasters[ra_index].eis_event_props = new_props.sra_props

END

IF (N_ELEMENTS(eis_flare_control) GT 0) THEN BEGIN
    Self.eis_flare_control = eis_flare_control

    ra_index  = Self.eis_study.raster_index
    sra_props = Self.eis_study.studyRasters[ra_index].eis_flare_props
    sty_props = Self.eis_study.eis_flare_response

    new_props = eis_eisf_control_to_props( sty_props , sra_props     ,      $
                                           eis_flare_control)

    ; Update EIS Flare Properties
    Self.eis_study.eis_flare_response = new_props.sty_props
    Self.eis_study.studyRasters[ra_index].eis_flare_props = new_props.sra_props

END

IF (N_ELEMENTS(eis_aec_control) GT 0) THEN BEGIN
    Self.eis_aec_control = eis_aec_control

    ra_index  = Self.eis_study.raster_index
    sra_props = Self.eis_study.studyRasters[ra_index].aec_props

    new_props = eis_aece_control_to_props( sra_props , eis_aec_control)
    ; Update AEC Event Properties
    Self.eis_study.studyRasters[ra_index].aec_props = new_props

END

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_mk_study_gui::getProperty                                ,          $
            tlbID                 = tlbID                        ,	         $
            st_idID               = st_idID                      ,          $
            st_acronymID          = st_acronymID                 ,          $
            raster_index          = raster_index                 ,          $
            eis_study             = eis_study                    ,          $
            eis_mk_study_widgets  = eis_mk_study_widgets         ,          $
            eis_xrt_flare_control = eis_xrt_flare_control        ,          $
            eis_event_control     = eis_event_control            ,          $
            eis_flare_control     = eis_flare_control            ,          $
            eis_aec_control       = eis_aec_control

tlbID                             = Self.tlbID
st_idID                           = Self.eis_mk_study_widgets.st_idID
st_acronymID                      = Self.eis_mk_study_widgets.st_acronymID
raster_index                      = Self.eis_study.raster_index
eis_study                         = Self.eis_study
eis_mk_study_widgets              = Self.eis_mk_study_widgets
eis_xrt_flare_control             = Self.eis_xrt_flare_control
eis_event_control                 = Self.eis_event_control
eis_flare_control                 = Self.eis_flare_control
eis_aec_control                   = Self.eis_aec_control

END
;______________________________________________________________________________
; Return widget id
;______________________________________________________________________________
FUNCTION eis_mk_study_gui::tlbID

RETURN, Self.tlbID

END
;______________________________________________________________________________
; Widget_Control wrapper
;______________________________________________________________________________
PRO eis_mk_study_gui::Control, _Ref_Extra=Widget_Control_Keywords

Widget_Control, Self.tlbID, _Extra=Widget_Control_Keywords

END
;______________________________________________________________________________
; Widget_Info wrapper
;______________________________________________________________________________
FUNCTION eis_mk_study_gui::Info, _Ref_Extra=Widget_Info_Keywords

RETURN , WIDGET_INFO(Self.tlbID, _Extra=Widget_Info_Keywords)

END
;______________________________________________________________________________
; Realize wrapper
;______________________________________________________________________________
PRO eis_mk_study_gui::Realize

WIDGET_CONTROL , Self.tlbID, /Realize

END
;______________________________________________________________________________
; XManager wrapper
;______________________________________________________________________________
PRO eis_mk_study_gui::XManager , group = group

XMANAGER                                                      ,             $
           "eis_mk_study_gui"                                 ,             $
           Self.tlbID                                         ,             $
           Event_Handler  = "eis_mk_study_gui_EV"             ,             $
           cleanup        = "eis_mk_study_gui_Cleanup"        ,             $
           GROUP_LEADER   = group                             ,             $
           /NO_BLOCK

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_mk_study_gui_Event__Define

    struct = {  eis_mk_study_gui_Event,                                     $
                Type                : "",                                   $
                eis_mk_study_gui   : OBJ_NEW(),                             $
                extra               : 0                                     $
             }
END
;______________________________________________________________________________
; Event Handler wrapper
;______________________________________________________________________________
PRO eis_mk_study_gui_update_EV , Event

;Get event handler structure containing eis_mk_study_gui type object
Widget_CONTROL,Event.ID,get_uvalue = eis_mk_study_gui_Event
;If a WIDGET_TAB event , just quit
IF (TAG_NAMES(event, /STRUCTURE_NAME) EQ 'WIDGET_TAB') THEN BEGIN
    ;print,'tab event'
    RETURN
ENDIF

;Call eis_mk_study_gui's class member procedure "Handler"
eis_mk_study_gui_Event.eis_mk_study_gui->HandlerUpdate,Event

END
;______________________________________________________________________________
PRO eis_mk_study_gui::HandlerUpdate, Event

WIDGET_CONTROL, Event.ID, Get_UValue = eis_mk_study_gui_Event
CASE eis_mk_study_gui_Event.Type OF
    "REPEATS"       : valid = Self->modifyRasterRepeats()
    "XRTFLARE"      : valid = Self->toggleXRTFlare()
    "EISFLARE"      : valid = Self->toggleEISFlare()
    "AECEVENT"      : valid = Self->toggleAECEvent()
    "EISEVENT"      : valid = Self->toggleEISEvent()
    "CLEAR"         : valid = Self->clearAllRasters()
    "RASTER_IMPORT" : valid = Self->import_raster()
    "COMP"          : valid = Self->modifyRasterCompression(event)
    "DELETE"        : BEGIN
                            Self->deleteRaster
                            valid = 1
                      END
    ELSE            : print,eis_mk_study_gui_Event.Type
ENDCASE

IF (valid EQ 1) THEN Self->eis_study_update

END
;______________________________________________________________________________
PRO eis_mk_study_gui::eis_study_update

; Update Duration and Volume labels
Self->updateDurationAndVolume
; Update Slit/Slot
Self->updateSlitSlot

; Finally, save a copy of the current eis_study structure
oStudyItem = OBJ_NEW("eis_study_undo" , Self.eis_study)
Self.studyUndo->ADD , oStudyItem

; Update
Self->updateStudyRasterWidgets

END
;______________________________________________________________________________
; Event Handler wrapper
;______________________________________________________________________________
PRO eis_mk_study_gui_EV , Event

;Get event handler structure containing eis_mk_study_gui type object
Widget_CONTROL,Event.ID,get_uvalue = eis_mk_study_gui_Event
;If a WIDGET_TAB event , just quit
IF (TAG_NAMES(event, /STRUCTURE_NAME) EQ 'WIDGET_TAB') THEN BEGIN
    ;print,'tab event'
    RETURN
ENDIF

;Call eis_mk_study_gui's class member procedure "Handler"
eis_mk_study_gui_Event.eis_mk_study_gui->Handler,Event

END
;______________________________________________________________________________
;
; * THIS is the routine for putting in event handling NOT eis_mk_study_gui_Ev above.
;
; Event Handler - handle events.  Derived classes must
; contain their own implementation in order to handle events or else pass in an
; procedure name/function name using the Event_Pro/Event_Func keywords to
; CWidget::Init()
;______________________________________________________________________________
PRO eis_mk_study_gui::Handler, Event

WIDGET_CONTROL, Event.ID, Get_UValue = eis_mk_study_gui_Event
CASE eis_mk_study_gui_Event.Type OF
    "QUIT"          : Self->Control, /Destroy
    "TLB"           : Self->resizeTLB , event
    "UNDO"          : Self->studyUndo
    "SETTIMEUNIT"   : Self->setTimeUnits,eis_mk_study_gui_Event.extra
    "ST_EXPORT"     : Self->exportStudy
    "ST_IMPORT"     : Self->studyImport
    "STUDY_DB"      : Self->searchStudyDBGUI
    "RASTER_DB"     : eis_xraster , parentClass = Self
    "RA_LIST"       : Self->updateRasterProperties , event = event
    "MODIFY"        : Self->modifyRasterEventProperties
    "XRTF_PROPS"    : Self->invoke_xrt_flare_gui
    "EISE_PROPS"    : Self->invoke_eis_event_gui
    "EISF_PROPS"    : Self->invoke_eis_flare_gui
    "AECE_PROPS"    : Self->invoke_aec_event_gui
    "DESC"          : Self->studyDescription
    "RA_UP"         : Self->moveRasterUp
    "RA_DOWN"       : Self->moveRasterDown
    "DURATIONUNIT"  : Self->updateDurationAndVolume
    "VOLUMEUNIT"    : Self->updateDurationAndVolume
    "STUDYSAVEDB"   : Self->saveStudyToDatabase
;    "ASRC_ENABLE"   : Self->asrcEnable
;    "ASRC_DIR"      : Self->asrcDirection , event
;    "ASRC_SKIP"     : Self->asrcSkip      , event
;    "ASRC_NSTEPS"   : Self->asrcNSteps
    "STOP"          : Self->stopstop
    "VIEWRASTER"    : Self->viewRasterDetails
;    "ABOUT"         : stop
    "HELP"          : Self->displayHelp
    "VERS"          : Self->version
    ELSE            : print,eis_mk_study_gui_Event.Type
ENDCASE
END
;______________________________________________________________________________
; Undo previous change
;______________________________________________________________________________
PRO eis_mk_study_gui::version

fileName = concat_dir(GETENV('ancillary') , 'eis_mk_study_version.txt')
; Make sure the file is actually there
valid    = FILE_TEST(fileName)
IF (valid NE 1) THEN BEGIN
    print,"No version file found!"
    RETURN
ENDIF

; OK, read file
mText = rd_ascii(fileName)
; Display file contents in popup window
xpopup , mText , group = Self.tlbID , ySize = 10 , xSize = 40 ,             $
                                 title = "EIS_MK_STUDY Verion"

END
;______________________________________________________________________________
; Undo previous change
;______________________________________________________________________________
PRO eis_mk_study_gui::studyUndo

numUndo = Self.studyUndo->count()

IF (numUndo LT 2) THEN BEGIN
    BEEP
    complain = DIALOG_MESSAGE( 'Nothing to UNDO - stoopid!' ,               $
                                 DIALOG_PARENT = Self.tlbID,                $
                                 /ERROR )
    RETURN
ENDIF

; We want penultimate eis_study
oStudyItem = Self.studyUndo->get(POSITION = (numUndo - 2))
oStudyItem->getProperty , st = st
Self.eis_study  = st

; The study and studyRaster widgets
Self->updateStudyWidgets
Self->updateRasterProperties
Self->updateStudyRasterWidgets
;; Get Duration and Volume
;Self->updateDurationAndVolume

; Get widget IDs
widIDs = Self.eis_mk_study_widgets
index  = WIDGET_INFO(widIDs.st_ralistID , /LIST_SELECT)
; OK, de-sensitize "Selected Line" pane
Self.eis_study.raster_index = index

; OK, and kill the last UNDO raster
oStudyItemRemove = Self.studyUndo->get(POSITION = (numUndo - 1))
Self.studyUndo->remove , POSITION = numUndo - 1
OBJ_DESTROY , oStudyItemRemove

END
;______________________________________________________________________________
; Display Help
;______________________________________________________________________________
PRO eis_mk_study_gui::displayHelp

; Get eis_fonts
fonts = eis_fonts()

help_path = GETENV('EIS_PLAN_HELP')
help_file = CONCAT_DIR(help_path , 'eis_mk_study_help.hlp')

widg_help , help_file , group_leader = Self.tlbID , font = fonts.mono

END
;______________________________________________________________________________
; View Raster Details
;______________________________________________________________________________
PRO eis_mk_study_gui::viewRasterDetails

widIDs    = Self.eis_mk_study_widgets

; Check that nRasters > 0
nRasters  = Self.eis_study.nRasters
IF (nRasters LT 1) THEN BEGIN
    mText    = 'There are no rasters in this study. Aborting!'
    BEEP
    complain = DIALOG_MESSAGE(mText,DIALOG_PARENT=Self.tlbID,/INFORMATION)
    RETURN
ENDIF

; Check a raster is selected
listIndex = WIDGET_INFO(widIDs.st_raListID , /LIST_SELECT)
IF (listIndex EQ -1) THEN BEGIN
    mText    = 'No raster has been selected. Aborting!'
    BEEP
    complain = DIALOG_MESSAGE(mText,DIALOG_PARENT=Self.tlbID,/INFORMATION)
    RETURN
ENDIF

rasterID = Self.eis_study.studyRasters[Self.eis_study.raster_index].ra_id

; Read raster and linelist from DB, and pass into "eis_raster_details_gui"
eis_raster   = db_read_raster_entry(rasterID)
eis_lineList = db_read_linelist_entry(eis_raster.ll_id)

o_rasterDetails = OBJ_NEW("eis_raster_details_gui" , eis_raster   ,         $
                                                     eis_lineList ,         $
                                      group_leader = Self.tlbID)
o_rasterDetails->Realize
o_rasterDetails->XMANAGER

END
;______________________________________________________________________________
; Update Raster Title
;______________________________________________________________________________
PRO eis_mk_study_gui::setTimeUnits , index

Self.timeUnits = index
; Get Duration and Volume
Self->updateDurationAndVolume

END
;______________________________________________________________________________
; Import a study from an ascii file
;______________________________________________________________________________
PRO eis_mk_study_gui::plot_logo

;; Get widget IDs
;widIDs = Self.eis_mk_study_widgets
;WIDGET_CONTROL , widIDs.st_logoID , get_value = drawID
;WSET , drawID
;
;pathName = GETENV('ancillary')
;fileName = concat_dir(pathname,'eis_logo_sarah_3.jpg')
;READ_JPEG , filename , icon
;icon_resized = CONGRID(icon,3,160,90)
;TVSCL,icon_resized , true = 1

END
;______________________________________________________________________________
; Import a study from an ascii file
;______________________________________________________________________________
PRO eis_mk_study_gui::studyImport

eis_import_study , parentClass = Self

END
;______________________________________________________________________________
; Export Study
;______________________________________________________________________________
PRO eis_mk_study_gui::export , acronym , author , st_info = st_info

st             = Self.eis_study
st.acronym     = acronym
st.author      = author
st.title       = st_info.title
st.category[0] = st_info.category
st.target      = st_info.target

; Get fileName
fileName = eis_export_filename_generate(acronym , Self.tlbID)
IF (fileName[0] EQ '') THEN RETURN
; Finally export study
st_array = eis_export_study(st)
; OK, open file and write to it
OPENW  , lun , fileName , /GET_LUN
PRINTF , lun , TRANSPOSE(st_array)
CLOSE  , lun

END
;______________________________________________________________________________
; Export the current study to an ascii file
;______________________________________________________________________________
PRO eis_mk_study_gui::exportStudy

o_eis_export_gui = OBJ_NEW("eis_export_gui" , Self , /ST )
o_eis_export_gui->Realize
o_eis_export_gui->XMANAGER

END
;;______________________________________________________________________________
;; Update ASRC Number of compensation Steps
;;______________________________________________________________________________
;PRO eis_mk_study_gui::asrcNSteps
;
;widIDs    = Self.eis_mk_study_widgets
;listIndex = WIDGET_INFO(widIDs.st_raListID , /LIST_SELECT)
;
;WIDGET_CONTROL , widIDs.st_asrcNStepsID , get_value = orig
;; arguments-> name , newValue , oldValue , minValue , maxValue , multiple
;valid  = eis_validate_input('ASRC Number of Steps' , orig[0] ,              $
;           Self.eis_study.studyRasters[listIndex].asrc_nSteps , 1 , 127)
;
;CASE valid.valid OF
;    0:  BEGIN
;            newValue = Self.eis_study.studyRasters[listIndex].asrc_nSteps
;            BEEP
;        				complain = DIALOG_MESSAGE( valid.mText ,                        $
;                             				DIALOG_PARENT = Self.tlbID,                $
;                             				/ERROR )
;        END
;    1:  newValue = FIX(orig[0])
;ENDCASE
;
;; Update value
;Self.eis_study.studyRasters[listIndex].asrc_nSteps = newValue
;; Update widget value
;WIDGET_CONTROL , widIDs.st_asrcNStepsID ,  set_value =                      $
;                                    STRCOMPRESS(newValue , /REMOVE_ALL)
;
;
;END
;;______________________________________________________________________________
;; Update ASRC Skip
;;______________________________________________________________________________
;PRO eis_mk_study_gui::asrcSkip , event
;
;widIDs    = Self.eis_mk_study_widgets
;listIndex = WIDGET_INFO(widIDs.st_raListID , /LIST_SELECT)
;Self.eis_study.studyRasters[listIndex].asrc_skip = (event.index + 1)
;
;END
;;______________________________________________________________________________
;; Update ASRC Direction
;;______________________________________________________________________________
;PRO eis_mk_study_gui::asrcDirection , event
;
;widIDs    = Self.eis_mk_study_widgets
;listIndex = WIDGET_INFO(widIDs.st_raListID , /LIST_SELECT)
;Self.eis_study.studyRasters[listIndex].asrc_direction = event.index
;
;END
;;______________________________________________________________________________
;; Update ASRC Enable
;;______________________________________________________________________________
;PRO eis_mk_study_gui::asrcEnable
;
;; OK, check a raster has been selected. If not, reset the button and complain!
;widIDs    = Self.eis_mk_study_widgets
;listIndex = WIDGET_INFO(widIDs.st_raListID , /LIST_SELECT)
;buttonSet = WIDGET_INFO(widIDs.st_asrcEnabledID , /BUTTON_SET)
;IF (listIndex EQ -1) THEN BEGIN
;    IF (buttonSet EQ 1) THEN newValue = 0 ELSE newValue = 1
;    WIDGET_CONTROL , widIDs.st_asrcEnabledID , set_button = newValue
;    mText    = 'No raster has been selected. Aborting!'
;    BEEP
;    complain = DIALOG_MESSAGE( mText       ,                                $
;                             				DIALOG_PARENT = Self.tlbID,                $
;                             				/INFORMATION )
;    RETURN
;ENDIF
;
;Self.eis_study.studyRasters[listIndex].asrc_enabled = buttonSet
;CASE buttonSet OF
;    0:  BEGIN
;            Self.eis_study.studyRasters[listIndex].asrc_direction = 0
;            Self.eis_study.studyRasters[listIndex].asrc_skip      = 0
;            Self.eis_study.studyRasters[listIndex].asrc_nSteps    = 0
;            WIDGET_CONTROL , widIDs.st_asrcBaseID    , sensitive  = 0
;        END
;    1:  BEGIN
;            Self.eis_study.studyRasters[listIndex].asrc_direction = 0
;            Self.eis_study.studyRasters[listIndex].asrc_skip      = 1
;            Self.eis_study.studyRasters[listIndex].asrc_nSteps    = 1
;            WIDGET_CONTROL , widIDs.st_asrcBaseID    , sensitive  = 1
;        END
;ENDCASE
;; ASRC Direction
;direction = Self.eis_study.studyRasters[listIndex].asrc_direction
;WIDGET_CONTROL , widIDs.st_asrcDirectionID , set_droplist_select = direction
;; ASRC Skip
;skip = Self.eis_study.studyRasters[listIndex].asrc_skip
;WIDGET_CONTROL , widIDs.st_asrcSkipID , set_droplist_select = (skip - 1) > 0
;;ASRC Number of Compensation Steps
;nSteps = STRCOMPRESS(Self.eis_study.studyRasters[listIndex].asrc_nSteps , /REMOVE_ALL)
;WIDGET_CONTROL , widIDs.st_asrcNStepsID , set_value = nSteps
;
;Self->updateStudyRasterWidgets
;
;END
;______________________________________________________________________________
; Method called after EVENT PROPERTIES tool
;______________________________________________________________________________
PRO eis_mk_study_gui::saveStudy , newEntry = newEntry

IF (KEYWORD_SET(newEntry) EQ 1) THEN BEGIN

    ; Create a new entry
    eis_study_db_entry_create , Self.eis_study

ENDIF ELSE BEGIN

    ; Don't overwrite first (default) Study
				IF (Self.eis_study.id LT 2) THEN BEGIN
    				messageText = "Can't overwrite default Study. Aborting."
    				BEEP
    				dummy = DIALOG_MESSAGE(messageText,/ERROR , DIALOG_PARENT = Self.tlbID)
    				RETURN
				ENDIF
    ; OK, update an existing entry
    eis_study_db_entry_update , Self.eis_study

ENDELSE

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

END
;______________________________________________________________________________
; Method called after EVENT PROPERTIES tool
;______________________________________________________________________________
PRO eis_mk_study_gui::saveStudyToDatabase

; OK, a whole battery of checks here, including
; 1.  we have one or more rasters selected
; 2.  if event flags are set, are corresponding properties set
; 3.  Also, if flags are set to zero, then zero all corresponding properties

; 1.  we have one or more rasters selected
IF (Self.eis_study.nRasters LT 1) THEN BEGIN
    messageText = 'No rasters defined. Aborting.'
    BEEP
    dummy = DIALOG_MESSAGE(messageText,/ERROR , DIALOG_PARENT = Self.tlbID)
    RETURN
ENDIF

; 3.  Also, if flags are set to zero, then zero all corresponding properties
xrtFlareOn = Self.eis_study.xrt_flare_response.respond_to_xrt_flare
;
; XRT
;
IF (xrtFlareOn NE 1) THEN BEGIN
    Self.eis_study.studyRasters[*].xrt_flare_props.xrt_flag = 0
    Self.eis_study.xrt_flare_response = {xrt_response_props}
ENDIF

;o_eis_st_db_update = OBJ_NEW("jar_eis_st_db_update" , Self)
o_eis_st_db_update = OBJ_NEW("eis_db_save_gui" , Self , /ST)
o_eis_st_db_update->Realize
o_eis_st_db_update->XMANAGER

END
;______________________________________________________________________________
; Method called after EVENT PROPERTIES tool
;______________________________________________________________________________
PRO eis_mk_study_gui::updateEventProperties

index  = Self.eis_study.raster_index
widIDs = Self.eis_mk_study_widgets
raster = Self.eis_study.studyRasters[index]
;
; AEC Event
;
aecSet = TOTAL(raster.aec_props.aec_flag)
IF (aecSet EQ 0) THEN set_button = 0 ELSE set_button = 1
WIDGET_CONTROL , widIDs.st_aecEventID , set_button = set_button
Self.eis_study.aec_response.respond_to_aec = set_button
;
; EIS Event
;
eisEventSet = TOTAL(raster.eis_event_props.event_flag)
IF (eisEventSet EQ 0) THEN set_button = 0 ELSE set_button = 1
WIDGET_CONTROL , widIDs.st_eisEventID , set_button = set_button
Self.eis_study.eis_event_response.respond_to_eis_event = set_button
;
; EIS Flare
;
eisFlareSet = TOTAL(raster.eis_flare_props.flare_flag)
IF (eisFlareSet EQ 0) THEN set_button = 0 ELSE set_button = 1
WIDGET_CONTROL , widIDs.st_eisFlareID , set_button = set_button
Self.eis_study.eis_flare_response.respond_to_eis_flare = set_button

;; Update Duration and Volume labels
;Self->updateDurationAndVolume
;; Update Slit/Slot
;Self->updateSlitSlot
;Self->updateStudyRasterWidgets

END
;______________________________________________________________________________
; Check whether a raster has been higlighted
;______________________________________________________________________________
FUNCTION eis_mk_study_gui::import_raster

CASE !VERSION.OS_FAMILY of
    'Windows'   : path = GETENV('SystemDrive')
    'unix'      : path = GETENV('HOME')
    ELSE        : stop
ENDCASE

; Get fileName
fileName = DIALOG_PICKFILE(path              = path  ,                      $
                           dialog_parent     = Self.tlbID       ,           $
                           filter            = '*.def'          ,           $
                           /READ )

IF (fileName[0] EQ '') THEN BEGIN
    screwUp = DIALOG_MESSAGE("No valid filename selected. Aborting !" ,     $
                             dialog_parent  = Self.tlbID ,                  $
                             /ERROR)
    RETURN , 0
ENDIF

; Call function to read file
ra_and_ll = eis_import_raster(fileName)
; OK, ll = 0 is returned if failed
IF (N_TAGS(ra_and_ll) EQ 0) THEN BEGIN
    BEEP
    mText = 'Invalid Raster definition file. Aborting!'
    d = DIALOG_MESSAGE(mText, /ERROR , DIALOG_PARENT = Self.tlbID)
    RETURN , 0
ENDIF

print,'OK - got to turn this into a studyRaster'
stop

RETURN , 1

END
;______________________________________________________________________________
; Check whether a raster has been higlighted
;______________________________________________________________________________
FUNCTION eis_mk_study_gui::checkRasterIsSelected

nRasters = Self.eis_study.nRasters
index = WIDGET_INFO(Self.eis_mk_study_widgets.st_raListID , /LIST_SELECT)
IF (index[0] EQ -1) OR (nRasters EQ 0) THEN BEGIN
    messageText = "No raster is selected. Aborting !"
    BEEP
    screwUp = DIALOG_MESSAGE( messageText ,                                 $
                             DIALOG_PARENT = Self.tlbID,                    $
                             /ERROR)
    RETURN , 0
ENDIF
RETURN , 1

END
;______________________________________________________________________________
; Kill eis_xStudy
;______________________________________________________________________________
PRO eis_mk_study_gui::kill_xstudy , xstudy_obj

xstudy_obj->Control, /Destroy

END
;______________________________________________________________________________
; Kill eis_xRaster
;______________________________________________________________________________
PRO eis_mk_study_gui::kill_xraster , xraster_obj

xraster_obj->Control, /Destroy

END
;______________________________________________________________________________
; Move selected Raster Up
;______________________________________________________________________________
PRO eis_mk_study_gui::moveRasterUp

valid = Self->checkRasterIsSelected()
index = WIDGET_INFO(Self.eis_mk_study_widgets.st_raListID , /LIST_SELECT)
IF (valid                   EQ 0) THEN RETURN
IF (Self.eis_study.nRasters LE 1) THEN RETURN
IF (index                   EQ 0) THEN RETURN

; Get 2 rasters
raster1 = Self.eis_study.studyRasters[index]
raster2 = Self.eis_study.studyRasters[index - 1]
; And swap them
Self.eis_study.studyRasters[index]     = raster2
Self.eis_study.studyRasters[index - 1] = raster1

;; Update Duration and Volume labels
;Self->updateDurationAndVolume
; Update Slit/Slot
Self->updateSlitSlot
Self.eis_study.raster_index = index - 1
Self->updateStudyRasterWidgets

END
;______________________________________________________________________________
; Move selected Raster Down
;______________________________________________________________________________
PRO eis_mk_study_gui::moveRasterDown

valid = Self->checkRasterIsSelected()
index = WIDGET_INFO(Self.eis_mk_study_widgets.st_raListID , /LIST_SELECT)
IF (valid                   EQ 0)                         THEN RETURN
IF (Self.eis_study.nRasters LE 1)                         THEN RETURN
IF (index                   EQ Self.eis_study.nRasters-1) THEN RETURN

; Get 2 rasters
raster1 = Self.eis_study.studyRasters[index]
raster2 = Self.eis_study.studyRasters[index + 1]
; And swap them
Self.eis_study.studyRasters[index]     = raster2
Self.eis_study.studyRasters[index + 1] = raster1

;; Update Duration and Volume labels
;Self->updateDurationAndVolume
; Update Slit/Slot
Self->updateSlitSlot
Self.eis_study.raster_index = index + 1
Self->updateStudyRasterWidgets

END
;______________________________________________________________________________
; Set AEC Event on/off
;______________________________________________________________________________
FUNCTION eis_mk_study_gui::toggleAECEvent

widIDs   = Self.eis_mk_study_widgets
buttonOn = WIDGET_INFO(widIDs.st_aecEventID , /BUTTON_SET)

allFlags = Self->getAllFlags()
valid    = 1
; allFlags = Eis_event , Eis_flare , AEC_event , XRT
IF (allFlags[0] EQ 1) THEN valid = 0
IF (allFlags[1] EQ 1) THEN valid = 0

IF (buttonOn EQ 1) AND (valid EQ 0) THEN BEGIN
    messageText = "One of the EIS Flare/Event flags are set. " +            $
                  "Deselect these first. Aborting!"
    BEEP
    d = DIALOG_MESSAGE(messageText,/ERROR , DIALOG_PARENT = Self.tlbID)
    WIDGET_CONTROL , widIDs.st_aecEventID , set_button = 0
    RETURN , 0
ENDIF

Self.eis_study.aec_response.respond_to_aec = buttonOn

;; Set selected raster aec_flag = 0/1
index = Self.eis_study.raster_index
; By default
Self.eis_study.studyRasters[index].aec_props.aec_flag    = BYTARR(25)
Self.eis_study.studyRasters[index].aec_props.aec_flag[0] = buttonOn

;; Update Duration and Volume labels
;Self->updateDurationAndVolume
;; Update Slit/Slot
;Self->updateSlitSlot
Self->updateStudyRasterWidgets

; Destroy eis_xrt_flare_gui
IF (buttonOn EQ 0) AND OBJ_VALID(Self.eis_aec_event_gui) THEN Self.eis_aec_event_gui->accept
IF (buttonOn EQ 1) THEN Self->invoke_aec_event_gui

RETURN , 1

END
;______________________________________________________________________________
; Edit/View AEC Event Properties
;______________________________________________________________________________
PRO eis_mk_study_gui::invoke_aec_event_gui

; Ensure we only have 1 GUI
IF (OBJ_VALID(Self.eis_aec_event_gui) EQ 1) THEN RETURN

; Check XRT Flare Flag is ON
widIDs   = Self.eis_mk_study_widgets
button_set = WIDGET_INFO(widIDs.st_aecEventID , /BUTTON_SET)
IF (button_set NE 1) THEN BEGIN
    BEEP
    mText = "AEC Event flag is disabled. Aborting!"
    d = DIALOG_MESSAGE(mText , /ERROR , DIALOG_PARENT = Self.tlbID)
    RETURN
ENDIF

; Which raster are we dealing with
index = Self.eis_study.raster_index
index = WIDGET_INFO(widIDs.st_raListID , /LIST_SELECT)

sra_props = Self.eis_study.studyRasters[index].aec_props
sty_props = Self.eis_study.aec_response

; Call wrapper to get eis_event control structure
new_control = eis_aece_props_to_control(sra_props     ,      $
                                           Self.eis_aec_control)
; OK, update eis_event control structure
Self.eis_aec_control = new_control

; Invoke AEC Event GUI
Self.eis_aec_event_gui = OBJ_NEW("eis_aec_event_gui" , Self)
Self.eis_aec_event_gui->Realize
Self.eis_aec_event_gui->XMANAGER

END
;______________________________________________________________________________
; Edit/View EIS Event Properties
;______________________________________________________________________________
PRO eis_mk_study_gui::invoke_eis_event_gui

; Ensure we only have 1 GUI
IF (OBJ_VALID(Self.eis_event_gui) EQ 1) THEN RETURN

; Check XRT Flare Flag is ON
widIDs   = Self.eis_mk_study_widgets
button_set = WIDGET_INFO(widIDs.st_eisEventID , /BUTTON_SET)
IF (button_set NE 1) THEN BEGIN
    BEEP
    mText = "EIS Event flag is disabled. Aborting!"
    d = DIALOG_MESSAGE(mText , /ERROR , DIALOG_PARENT = Self.tlbID)
    RETURN
ENDIF

; Which raster are we dealing with
index = Self.eis_study.raster_index
index = WIDGET_INFO(widIDs.st_raListID , /LIST_SELECT)

sra_props = Self.eis_study.studyRasters[index].eis_event_props
sty_props = Self.eis_study.eis_event_response

; Call wrapper to get eis_event control structure
new_control = eis_eise_props_to_control(sty_props , sra_props     ,      $
                                           Self.eis_event_control)
; OK, update eis_event control structure
Self.eis_event_control = new_control

; Invoke EIS Event GUI
Self.eis_event_gui = OBJ_NEW("eis_event_gui" , Self)
Self.eis_event_gui->Realize
Self.eis_event_gui->XMANAGER

END
;______________________________________________________________________________
; Edit/View EIS Flare Properties
;______________________________________________________________________________
PRO eis_mk_study_gui::invoke_eis_flare_gui

; Ensure we only have 1 GUI
IF (OBJ_VALID(Self.eis_flare_gui) EQ 1) THEN RETURN

; Check XRT Flare Flag is ON
widIDs   = Self.eis_mk_study_widgets
button_set = WIDGET_INFO(widIDs.st_eisFlareID , /BUTTON_SET)
IF (button_set NE 1) THEN BEGIN
    BEEP
    mText = "EIS Flare flag is disabled. Aborting!"
    d = DIALOG_MESSAGE(mText , /ERROR , DIALOG_PARENT = Self.tlbID)
    RETURN
ENDIF

; Which raster are we dealing with
index = Self.eis_study.raster_index
index = WIDGET_INFO(widIDs.st_raListID , /LIST_SELECT)

sra_props = Self.eis_study.studyRasters[index].eis_flare_props
sty_props = Self.eis_study.eis_flare_response

; Call wrapper to get eis_flare control structure
new_control = eis_eisf_props_to_control(sty_props , sra_props     ,      $
                                           Self.eis_flare_control)
; OK, update eis_flare control structure
Self.eis_flare_control = new_control

; Invoke EIS Event GUI
Self.eis_flare_gui = OBJ_NEW("eis_flare_gui" , Self)
Self.eis_flare_gui->Realize
Self.eis_flare_gui->XMANAGER

END
;______________________________________________________________________________
; Edit/View XRT Flare Properties
;______________________________________________________________________________
PRO eis_mk_study_gui::invoke_xrt_flare_gui

; Ensure we only have 1 GUI
IF (OBJ_VALID(Self.eis_xrt_flare_gui) EQ 1) THEN RETURN

; Check XRT Flare Flag is ON
widIDs   = Self.eis_mk_study_widgets
button_set = WIDGET_INFO(widIDs.st_xrtFlareID , /BUTTON_SET)
IF (button_set NE 1) THEN BEGIN
    BEEP
    mText = "XRT Flare flag is disabled. Aborting!"
    d = DIALOG_MESSAGE(mText , /ERROR , DIALOG_PARENT = Self.tlbID)
    RETURN
ENDIF

; OK, copy selected studyRaster XRT Properties into Self.xrt_response_props
xrtFlare  = Self.eis_study.xrt_flare_response
new_props = eis_xrtf_props_to_control(xrtFlare , Self.eis_xrt_flare_control)
; Update eis_xrt_flare_control structure

Self.eis_xrt_flare_control = new_props

; Invoke XRT Flare Properties GUI
Self.eis_xrt_flare_gui = OBJ_NEW("eis_xrt_flare_gui" , Self)
Self.eis_xrt_flare_gui->Realize
Self.eis_xrt_flare_gui->XMANAGER

END
;______________________________________________________________________________
; Set XRT Flare on/off
;______________________________________________________________________________
FUNCTION eis_mk_study_gui::toggleXRTFlare

widIDs   = Self.eis_mk_study_widgets
buttonOn = WIDGET_INFO(widIDs.st_xrtFlareID , /BUTTON_SET)
allFlags = Self->getAllFlags()
valid    = 1
; allFlags = Eis_event , Eis_flare , AEC_event , XRT
IF (allFlags[0] EQ 1) THEN valid = 0
IF (allFlags[1] EQ 1) THEN valid = 0

IF (buttonOn EQ 1) AND (valid EQ 0) THEN BEGIN
    messageText = "One of the EIS Flare/Event flags are set. " +            $
                  "Deselect these first. Aborting!"
    BEEP
    d = DIALOG_MESSAGE(messageText,/ERROR , DIALOG_PARENT = Self.tlbID)
    WIDGET_CONTROL , widIDs.st_xrtFlareID , set_button = 0
    RETURN , 0
ENDIF

; Set selected raster xrt_flag = 0/1
index = Self.eis_study.raster_index
Self.eis_study.studyRasters[index].xrt_flare_props.xrt_flag = buttonOn

totalXRT = TOTAL(Self.eis_study.studyRasters[*].xrt_flare_props.xrt_flag)

IF (totalXRT LE 1) AND (buttonOn EQ 0) THEN BEGIN
    ; Set study XRT Flag 0/1
    Self.eis_study.xrt_flare_response.respond_to_xrt_flare = 0
END ELSE BEGIN
    Self.eis_study.xrt_flare_response.respond_to_xrt_flare = 1
ENDELSE

;; Update Duration and Volume labels
;Self->updateDurationAndVolume
;; Update Slit/Slot
;Self->updateSlitSlot

Self->updateStudyRasterWidgets

; Destroy eis_xrt_flare_gui
IF (buttonOn EQ 0) AND OBJ_VALID(Self.eis_xrt_flare_gui) THEN Self.eis_xrt_flare_gui->accept
IF (buttonOn EQ 1) THEN Self->invoke_xrt_flare_gui

RETURN , 1

END
;______________________________________________________________________________
; Set EIS Event on/off
;______________________________________________________________________________
FUNCTION eis_mk_study_gui::toggleEISEvent

widIDs   = Self.eis_mk_study_widgets
buttonOn = WIDGET_INFO(widIDs.st_eisEventID , /BUTTON_SET)

allFlags = Self->getAllFlags()
IF (buttonOn EQ 1) AND (TOTAL(allFlags) NE 0) THEN BEGIN
    messageText = 'This flag is exclusive. Deselect others first. Aborting!'
    BEEP
    d = DIALOG_MESSAGE(messageText,/ERROR , DIALOG_PARENT = Self.tlbID)
    WIDGET_CONTROL , widIDs.st_eisEventID , set_button = 0
    RETURN , 0
ENDIF

Self.eis_study.eis_event_response.respond_to_eis_event = buttonOn

; Set selected raster eis_event_flag = 0/1
index = Self.eis_study.raster_index
Self.eis_study.studyRasters[index].eis_event_props.event_flag    = BYTARR(25)
Self.eis_study.studyRasters[index].eis_event_props.event_flag[0] = buttonOn

; OK, by default (if = 0), set x/y min limit to 1
x_min_limit = Self.eis_study.studyRasters[index].eis_event_props.x_min_limit
y_min_limit = Self.eis_study.studyRasters[index].eis_event_props.y_min_limit
Self.eis_study.studyRasters[index].eis_event_props.x_min_limit = x_min_limit > 1
Self.eis_study.studyRasters[index].eis_event_props.y_min_limit = y_min_limit > 1

;; Update Duration and Volume labels
;Self->updateDurationAndVolume
;; Update Slit/Slot
;Self->updateSlitSlot
Self->updateStudyRasterWidgets

; Destroy eis_xrt_flare_gui
IF (buttonOn EQ 0) AND OBJ_VALID(Self.eis_event_gui) THEN Self.eis_event_gui->accept
IF (buttonOn EQ 1) THEN Self->invoke_eis_event_gui

RETURN , 1

END
;______________________________________________________________________________
; Set EIS Flare on/off
;______________________________________________________________________________
FUNCTION eis_mk_study_gui::toggleEISFlare

widIDs   = Self.eis_mk_study_widgets
buttonOn = WIDGET_INFO(widIDs.st_eisFlareID , /BUTTON_SET)

allFlags = Self->getAllFlags()
IF (buttonOn EQ 1) AND (TOTAL(allFlags) NE 0) THEN BEGIN
    messageText = 'This flag is exclusive. Deselect others first. Aborting!'
    BEEP
    d = DIALOG_MESSAGE(messageText,/ERROR , DIALOG_PARENT = Self.tlbID)
    WIDGET_CONTROL , widIDs.st_eisFlareID , set_button = 0
    RETURN , 0
ENDIF

Self.eis_study.eis_flare_response.respond_to_eis_flare = buttonOn

; Set selected raster eis_flag = 0/1
index = Self.eis_study.raster_index
Self.eis_study.studyRasters[index].eis_flare_props.flare_flag    = BYTARR(25)
Self.eis_study.studyRasters[index].eis_flare_props.flare_flag[0] = buttonOn

; OK, by default (if = 0), set x/y min limit to 1
x_min_limit = Self.eis_study.studyRasters[index].eis_flare_props.x_min_limit
y_min_limit = Self.eis_study.studyRasters[index].eis_flare_props.y_min_limit
Self.eis_study.studyRasters[index].eis_flare_props.x_min_limit = x_min_limit > 1
Self.eis_study.studyRasters[index].eis_flare_props.y_min_limit = y_min_limit > 1

help,Self.eis_study.studyRasters[index].eis_flare_props.x_min_limit

;; Update Duration and Volume labels
;Self->updateDurationAndVolume
;; Update Slit/Slot
;Self->updateSlitSlot
Self->updateStudyRasterWidgets

; Destroy eis_xrt_flare_gui
IF (buttonOn EQ 0) AND OBJ_VALID(Self.eis_flare_gui) THEN Self.eis_flare_gui->accept
IF (buttonOn EQ 1) THEN Self->invoke_eis_flare_gui

RETURN , 1

END
;______________________________________________________________________________
; Set EIS Event on/off
;______________________________________________________________________________
FUNCTION eis_mk_study_gui::getAllFlags

allFlags    = BYTARR(4)
index       = Self.eis_study.raster_index
raster      = Self.eis_study.studyRasters[index]
allFlags[0] = TOTAL(raster.eis_event_props.event_flag)
allFlags[1] = TOTAL(raster.eis_flare_props.flare_flag)
allFlags[2] = TOTAL(raster.aec_props.aec_flag)
allFlags[3] =       raster.xrt_flare_props.xrt_flag

RETURN , allFlags

END
;______________________________________________________________________________
; Stop
;______________________________________________________________________________
PRO eis_mk_study_gui::aece_line_select , line_index

; Set selected raster eis_flag = 0/1
index = Self.eis_study.raster_index
; Reset studyRasters[index].aec_props.aec_flag
Self.eis_study.studyRasters[index].aec_props.aec_flag             = BYTARR(25)
Self.eis_study.studyRasters[index].aec_props.aec_flag[line_index] = 1

END
;______________________________________________________________________________
; Stop
;______________________________________________________________________________
PRO eis_mk_study_gui::eise_line_select , line_index

; Set selected raster eis_flag = 0/1
index = Self.eis_study.raster_index
; Reset studyRasters[index].eis_event_props.event_flag
Self.eis_study.studyRasters[index].eis_event_props.event_flag             = BYTARR(25)
Self.eis_study.studyRasters[index].eis_event_props.event_flag[line_index] = 1

END
;______________________________________________________________________________
; Stop
;______________________________________________________________________________
PRO eis_mk_study_gui::eisf_line_select , line_index

; Set selected raster eis_flag = 0/1
index = Self.eis_study.raster_index
; Reset studyRasters[index].eis_flare_props.event_flag
Self.eis_study.studyRasters[index].eis_flare_props.flare_flag             = BYTARR(25)
Self.eis_study.studyRasters[index].eis_flare_props.flare_flag[line_index] = 1

END
;______________________________________________________________________________
; Stop
;______________________________________________________________________________
PRO eis_mk_study_gui::stopstop

help,Self.eis_study,/str
stop

END
;______________________________________________________________________________
; Study Description
;______________________________________________________________________________
PRO eis_mk_study_gui::studyDescription

eis_view_study_descriptions , Self.eis_study.id , tlbID = Self.tlbID

END
;______________________________________________________________________________
; Modify the raster compression scheme
;______________________________________________________________________________
FUNCTION eis_mk_study_gui::modifyRasterCompression , event

widIDs    = Self.eis_mk_study_widgets
listIndex = WIDGET_INFO(widIDs.st_raListID , /LIST_SELECT)
IF (listIndex EQ -1) THEN stop

newValue = Self.eis_study.studyRasters[listIndex].ra_compression
IF (newValue EQ event.index) THEN RETURN , 0

Self.eis_study.studyRasters[listIndex].ra_compression = event.index

; Update Duration and Volume labels
Self->updateDurationAndVolume
;; Update Slit/Slot
;Self->updateSlitSlot

Self->updateStudyRasterWidgets

RETURN , 1

END
;______________________________________________________________________________
; Modify the number of raster repeats
;______________________________________________________________________________
FUNCTION eis_mk_study_gui::modifyRasterRepeats

widIDs    = Self.eis_mk_study_widgets
listIndex = WIDGET_INFO(widIDs.st_raListID , /LIST_SELECT)
IF (listIndex LT 0) THEN stop

; Get Repeats
WIDGET_CONTROL , widIDs.st_repeatsID , get_value = orig
;stop

; arguments-> name , newValue , oldValue , minValue , maxValue , multiple
valid  = eis_validate_input('Raster repeats' , orig[0] ,                     $
                Self.eis_study.studyRasters[listIndex].ra_repeats ,          $
                1 , 4095)

CASE valid.valid OF
    0:  BEGIN
            newValue = Self.eis_study.studyRasters[listIndex].ra_repeats
            beep
            complain = DIALOG_MESSAGE( valid.mText ,                        $
                                 DIALOG_PARENT = Self.tlbID,                $
                                 /ERROR )
        END
    1:  newValue = FIX(orig[0])
ENDCASE

; Update value
Self.eis_study.studyRasters[listIndex].ra_repeats = newValue
; Update widget value
WIDGET_CONTROL , widIDs.st_repeatsID ,                                      $
                        set_value = STRCOMPRESS( newValue , /REMOVE_ALL)

WIDGET_CONTROL , widIDs.st_raListID , set_list_select = listIndex
WIDGET_CONTROL , widIDs.st_compressionID , sensitive = 1
WIDGET_CONTROL , widIDs.st_eventResponseBaseID    , sensitive = 1
WIDGET_CONTROL , widIDs.st_repeatsID     , sensitive = 1

RETURN , valid.valid

END
;______________________________________________________________________________
; Create Context Sensitive Help Menu
;______________________________________________________________________________
PRO eis_mk_study_gui::rasterContextSensitiveMenu , event

; Get widget IDs
widIDs    = Self.eis_mk_study_widgets
contextbase                 = WIDGET_BASE(widIDs.st_raListID ,              $
                                    /CONTEXT_MENU)

Event_Pro_update = 'eis_mk_study_gui_update_EV'

REMOVE_EVENT                = {eis_mk_study_gui_Event}
REMOVE_EVENT.eis_mk_study_gui = Self
REMOVE_EVENT.Type           = "DELETE"
REMOVEButton                = WIDGET_BUTTON(contextbase,                    $
                                    value       = 'Remove from List',       $
                                    Event_Pro   = Event_Pro_update  ,       $
                                    uvalue      = REMOVE_EVENT)

VIEW_EVENT                  = {eis_mk_study_gui_Event}
VIEW_EVENT.eis_mk_study_gui = Self
VIEW_EVENT.Type             = "VIEWRASTER"
value = 'View Raster Details... '
st_rasterVIEWButton         = WIDGET_BUTTON(contextbase            ,        $
                                    value       = value            ,        $
                                    Event_Pro   = 'eis_mk_study_gui_EV',    $
                                    uValue      = VIEW_EVENT)

widget_displaycontextmenu, event.id, event.x, event.y, contextbase

END
;______________________________________________________________________________
; Update the selected Raster
;______________________________________________________________________________
PRO eis_mk_study_gui::updateRasterProperties , event = event

; Get widget IDs
widIDs    = Self.eis_mk_study_widgets
listIndex = WIDGET_INFO(widIDs.st_raListID , /LIST_SELECT)
IF (listIndex EQ -1) THEN RETURN

IF (KEYWORD_SET(event) EQ 1) THEN BEGIN
    ; Trap CONTEXT events here
    widgetType = TAG_NAMES(event, /STRUCTURE_NAME)
    IF (widgetType EQ 'WIDGET_CONTEXT') THEN BEGIN
        Self->rasterContextSensitiveMenu , event
        RETURN
    END
ENDIF

Self.eis_study.raster_index = listIndex
raster    = Self.eis_study.studyRasters
;
; Compression
;
ra_Comp = raster[listIndex].ra_compression
WIDGET_CONTROL , widIDs.st_compressionID , set_droplist_select = ra_Comp
;
; Repeats
;
ra_Repeats = STRCOMPRESS(raster[listIndex].ra_repeats , /REMOVE_ALL)
; Set repeat text box value
WIDGET_CONTROL , widIDs.st_repeatsID , set_value = ra_Repeats
;
; XRT Flare
;
ra_XRTFlareOn = Self.eis_study.studyRasters[listIndex].xrt_flare_props.xrt_flag
WIDGET_CONTROL , widIDs.st_xrtFlareID , set_button = ra_XRTFlareOn < 1
;
; EIS Flare
;
ra_EISFlareOn = Self.eis_study.studyRasters[listIndex].eis_flare_props.flare_flag
index = WHERE(ra_EISFlareOn EQ 1)
IF (index EQ -1) THEN set_button = 0 ELSE set_button = 1
WIDGET_CONTROL , widIDs.st_eisFlareID , set_button = set_button
;
; AEC Event
;
ra_AECEventOn = Self.eis_study.studyRasters[listIndex].aec_props.aec_flag
index = WHERE(ra_AECEventOn EQ 1)
IF (index EQ -1) THEN set_button = 0 ELSE set_button = 1
WIDGET_CONTROL , widIDs.st_aecEventID , set_button = set_button
;
; EIS Event
;
ra_EISEventOn = Self.eis_study.studyRasters[listIndex].eis_event_props.event_flag
index = WHERE(ra_EISEventOn EQ 1)
IF (index EQ -1) THEN set_button = 0 ELSE set_button = 1
WIDGET_CONTROL , widIDs.st_eisEventID , set_button = set_button

IF (Self.eis_study.nRasters EQ 0) THEN BEGIN
    sensitive = 0
    WIDGET_CONTROL , widIDs.st_ra_titleID   , set_value = 'none'
    WIDGET_CONTROL , widIDs.st_ra_idID      , set_value = 'none
    WIDGET_CONTROL , widIDs.st_ra_acronymID , set_value = 'none
END ELSE BEGIN
    sensitive = 1
    ; ID needs to be in 000001 format
    ra_id = STRING(format='(I06)',raster[listIndex].ra_id)
    WIDGET_CONTROL , widIDs.st_ra_idID	    , set_value = ra_id
    WIDGET_CONTROL , widIDs.st_ra_titleID   , set_value = raster[listIndex].ra_title
    WIDGET_CONTROL , widIDs.st_ra_acronymID , set_value = raster[listIndex].ra_acronym
ENDELSE
;;
;; ASRC Properties
;;
;; ASRC Enabled
;asrc_enabled = raster[listIndex].asrc_enabled
;CASE asrc_enabled OF
;    0:  BEGIN
;            Self.eis_study.studyRasters[listIndex].asrc_direction = 0
;            Self.eis_study.studyRasters[listIndex].asrc_skip      = 0
;            Self.eis_study.studyRasters[listIndex].asrc_nSteps    = 0
;            WIDGET_CONTROL , widIDs.st_asrcBaseID    , sensitive  = 0
;        END
;    1:  BEGIN
;            WIDGET_CONTROL , widIDs.st_asrcBaseID    , sensitive  = 1
;        END
;ENDCASE
;WIDGET_CONTROL , widIDs.st_asrcEnabledID , set_button = asrc_enabled
;; ASRC Direction
;direction = raster[listIndex].asrc_direction
;WIDGET_CONTROL , widIDs.st_asrcDirectionID , set_droplist_select = direction
;; ASRC Skip
;skip = raster[listIndex].asrc_skip
;WIDGET_CONTROL , widIDs.st_asrcSkipID , set_droplist_select = (skip - 1) > 0
;;ASRC Number of Compensation Steps
;nSteps = STRCOMPRESS(raster[listIndex].asrc_nSteps , /REMOVE_ALL)
;WIDGET_CONTROL , widIDs.st_asrcNStepsID , set_value = nSteps
;

WIDGET_CONTROL , widIDs.st_compressionID       , sensitive = sensitive
WIDGET_CONTROL , widIDs.st_eventResponseBaseID , sensitive = sensitive
WIDGET_CONTROL , widIDs.st_repeatsID           , sensitive = sensitive

; Ensure we only have 1 EIS_XRT_FLARE_GUI dialog
IF OBJ_VALID(Self.eis_xrt_flare_gui) THEN Self.eis_xrt_flare_gui->accept
IF OBJ_VALID(Self.eis_event_gui)     THEN Self.eis_event_gui->accept
IF OBJ_VALID(Self.eis_aec_event_gui) THEN Self.eis_aec_event_gui->accept
IF OBJ_VALID(Self.eis_flare_gui)     THEN Self.eis_flare_gui->accept

END
;______________________________________________________________________________
; Modify the selected Raster
;______________________________________________________________________________
PRO eis_mk_study_gui::modifyRasterEventProperties

nRasters = Self.eis_study.nRasters
index = WIDGET_INFO(Self.eis_mk_study_widgets.st_raListID , /LIST_SELECT)
IF (index[0] EQ -1) OR (nRasters EQ 0) THEN BEGIN
    messageText = "No raster is selected. Aborting !"
    BEEP
    screwUp = DIALOG_MESSAGE( messageText ,                                 $
                             DIALOG_PARENT = Self.tlbID,                    $
                             /ERROR)
    RETURN
ENDIF

eisEventSet = Self.eis_study.eis_event_response.respond_to_eis_event
eisFlareSet = Self.eis_study.eis_flare_response.respond_to_eis_flare
aecEventSet = Self.eis_study.aec_response.respond_to_aec
xrtFlareSet = Self.eis_study.xrt_flare_response.respond_to_xrt_flare
IF (eisEventSet NE 1) AND                                                   $
   (eisFlareSet NE 1) AND                                                   $
   (aecEventSet NE 1) AND                                                   $
   (xrtFlareSet NE 1) THEN BEGIN
    messageText = "No event/flare flags are set. Aborting !"
    BEEP
    screwUp = DIALOG_MESSAGE( messageText ,                                 $
                             DIALOG_PARENT = Self.tlbID,                    $
                             /ERROR)
    RETURN
ENDIF

o_event_props = OBJ_NEW("eis_event_props_gui" , Self)
o_event_props->Realize
o_event_props->XMANAGER

END
;______________________________________________________________________________
; Clear all rasters
;______________________________________________________________________________
FUNCTION eis_mk_study_gui::clearAllRasters

messageText = "Are you sure you want to remove all rasters?"
BEEP
areYouSure = DIALOG_MESSAGE(messageText ,                                   $
                            DIALOG_PARENT = Self.tlbID ,                    $
                            /QUESTION ,                                     $
                            /DEFAULT_NO)

IF (areYouSure NE 'Yes') THEN RETURN , 0

Self.eis_study = {eis_study}

;Self.eis_study.nRasters = 0
;Self.eis_study.raster_index = -1
;; Reset all studyRasters (all 20) to ZERO
;Self.eis_study.studyrasters = REPLICATE({eis_study_raster} , 20 )
;Self.eis_study.duration     = 0
;Self.eis_study.volume       = 0
;Self.eis_study.slit_slot    = INTARR(3)

; Get widget IDs - and zero all study widget values
widIDs = Self.eis_mk_study_widgets
WIDGET_CONTROL , widIDs.st_titleID    , set_value = 'none'
WIDGET_CONTROL , widIDs.st_idID       , set_value = 'none'
WIDGET_CONTROL , widIDs.st_acronymID  , set_value = 'none'
WIDGET_CONTROL , widIDs.st_authorID   , set_value = ''
WIDGET_CONTROL , widIDs.st_dateID     , set_value = ''
WIDGET_CONTROL , widIDs.st_targetID   , set_value = ''
WIDGET_CONTROL , widIDs.st_categoryID , set_value = ''
WIDGET_CONTROL , widIDs.st_volumeID   , set_value = '0'
WIDGET_CONTROL , widIDs.st_durationID , set_value = '0'
WIDGET_CONTROL , widIDs.st_datarateID , set_value = '0'
; Zero raster widget values
Self->updateStudyRasterWidgets

RETURN , 1

END
;______________________________________________________________________________
; Delete a raster
;______________________________________________________________________________
PRO eis_mk_study_gui::deleteRaster

; Check that nRasters > 0
nRasters  = Self.eis_study.nRasters
IF (nRasters LT 1) THEN BEGIN
    mText    = 'There are no rasters in this study. Aborting!'
    BEEP
    complain = DIALOG_MESSAGE(mText,DIALOG_PARENT=Self.tlbID,/INFORMATION)
    RETURN
ENDIF

index = WIDGET_INFO(Self.eis_mk_study_widgets.st_raListID , /LIST_SELECT)
IF (index[0] EQ -1) THEN BEGIN
    messageText = "No raster is selected. Aborting !"
    BEEP
    screwUp = DIALOG_MESSAGE( messageText ,                                 $
                             DIALOG_PARENT = Self.tlbID,                    $
                             /ERROR)
    RETURN
ENDIF

IF (nRasters EQ 1) THEN BEGIN
    r = Self->clearAllRasters()
    RETURN
ENDIF

; OK, find all rasters that have non-zero ra_ID tags
rasterIndicies = WHERE(Self.eis_study.studyrasters[*].ra_id NE 0)
IF (rasterIndicies[0] EQ -1) THEN stop
; OK, get indicies of rasterIndicies that are not the selected item
newRasterIndicies = WHERE(rasterIndicies NE index)
IF (newRasterIndicies[0] NE -1) THEN BEGIN
    ; Ok, we want to create an array of eis_study_rasters...one less than
    ; we've already got!
    rasterArray = REPLICATE({eis_study_raster} , nRasters - 1 )
    ; Update these with real values
    FOR i = 0, nRasters - 1 -1  DO BEGIN
        ; We want all the rasters EXCEPT the selected one
        rasterArray[i] = Self.eis_study.studyrasters[newRasterIndicies[i]]
    ENDFOR
ENDIF
; Reset all studyRasters (all 20) to ZERO
Self.eis_study.studyrasters = REPLICATE({eis_study_raster} , 20 )

IF (newRasterIndicies[0] NE -1) THEN BEGIN

    ; Update those that are non-zero
    FOR i = 0, N_ELEMENTS(rasterArray) - 1 DO BEGIN

        Self.eis_study.studyrasters[i]          = rasterArray[i]
        Self.eis_study.studyrasters[i].ra_index = i

    ENDFOR
ENDIF
Self.eis_study.nRasters = nRasters - 1

; OK, un-set widget list
Self.eis_study.raster_index = -1
; Get widget IDs
widIDs = Self.eis_mk_study_widgets
WIDGET_CONTROL , widIDs.st_compressionID       , sensitive = 0
WIDGET_CONTROL , widIDs.st_eventResponseBaseID , sensitive = 0
WIDGET_CONTROL , widIDs.st_repeatsID           , sensitive = 0

END
;______________________________________________________________________________
; Fire up Study Database GUI
;______________________________________________________________________________
PRO eis_mk_study_gui::searchStudyDBGUI

eis_xstudy , parentClass = Self

END
;______________________________________________________________________________
; Load a Raster
;______________________________________________________________________________
PRO eis_mk_study_gui::loadRaster , ra_id , loadLineList = loadLineList

eis_raster = db_read_raster_entry(ra_id)

; Update Self.eis_study with new number of rasters
nRasters = Self.eis_study.nRasters
Self.eis_study.nRasters = nRasters + 1

; Update new studyRaster with values
Self.eis_study.studyRasters[nRasters].ra_index       = nRasters
Self.eis_study.studyRasters[nRasters].ra_id          = eis_raster.id
Self.eis_study.studyRasters[nRasters].ra_acronym     = eis_raster.acronym
Self.eis_study.studyRasters[nRasters].ra_title       = eis_raster.title
Self.eis_study.studyRasters[nRasters].ra_nWindows    = eis_raster.nWindows
Self.eis_study.studyRasters[nRasters].ll_id          = eis_raster.ll_id
Self.eis_study.studyRasters[nRasters].slitindex      = eis_raster.slitIndex
Self.eis_study.studyRasters[nRasters].ra_compression = eis_raster.compression
Self.eis_study.studyRasters[nRasters].ra_duration    = eis_raster.duration
Self.eis_study.studyRasters[nRasters].ra_volume      = eis_raster.volume
Self.eis_study.studyRasters[nRasters].ra_repeats     = 1

; Update Duration and Volume labels
Self->updateDurationAndVolume
; Update Slit/Slot
Self->updateSlitSlot
Self->updateStudyRasterWidgets

; Finally, save a copy of the current eis_study structure
oStudyItem = OBJ_NEW("eis_study_undo" , Self.eis_study)
Self.studyUndo->ADD , oStudyItem

END
;______________________________________________________________________________
; Read Study Database - all of it
;______________________________________________________________________________
PRO eis_mk_study_gui::updateStudyRasterWidgets

; OK, find all rasters that have non-zero ra_ID tags
rasterIndicies = WHERE(Self.eis_study.studyrasters[*].ra_id NE 0)

numRasters = N_ELEMENTS(rasterIndicies)
rasterList = STRARR(numRasters)

FOR i = 0, numRasters - 1 DO BEGIN

    s = Self.eis_study.studyrasters[i]

    comp_list = eis_read_study_lists(/COMPRESSION)
    CASE s.ra_compression OF
        0:      compression = 'none'
        1:      compression = 'DPCM'
        2:      compression = 'JPEG98'
        3:      compression = 'JPEG90'
        4:      compression = 'JPEG75'
        5:      compression = 'JPEG50'
        6:      compression = 'JPEG95'
        7:      compression = 'JPEG92'
        8:      compression = 'JPEG85'
        9:      compression = 'JPEG65'
        ELSE:   compression = 'n/a'
    ENDCASE

    ; XRT Flare
    XRTFlareOn = Self.eis_study.studyRasters[i].xrt_flare_props.xrt_flag
    IF (XRTFlareOn EQ 1) THEN XRTFlare = 'ON' ELSE XRTFlare = '..'

    ; EIS Flare
    EISFlareOn = Self.eis_study.studyRasters[i].eis_flare_props.flare_flag
    index      = WHERE(EISFlareOn EQ 1)
    IF (index[0] NE -1) THEN EISFlare = 'ON' ELSE EISFlare = '..'

    ; AEC Event
    AECEventOn = Self.eis_study.studyRasters[i].aec_props.aec_flag
    index      = WHERE(AECEventOn EQ 1)
    IF (index[0] NE -1) THEN AECEvent = 'ON' ELSE AECEvent = '..'

    ; EIS Event
    EISEventOn = Self.eis_study.studyRasters[i].eis_event_props.event_flag
    index      = WHERE(EISEventOn EQ 1)
    IF (index[0] NE -1) THEN EISEvent = 'ON' ELSE EISEvent = '..'

    ; Get duration
    duration = sec2dhms(s.ra_duration /1000.)

    ; ID needs to be in 000001 format
    idValue = STRING(format='(I06)',s.ra_ID)

    format        = '(A-8,A-22,A-7,I-8,A-13,A-8,A-8,A-8,A-8)'
    rasterList[i] = STRING(format=format,idValue            ,               $
                                         s.ra_acronym       ,               $
                                         compression        ,               $
                                         s.ra_repeats       ,               $
                                         duration           ,               $
                                         EISEvent           ,               $
                                         EISFlare           ,               $
                                         AECEvent           ,               $
                                         XRTFlare)

ENDFOR

; Get widget IDs
widIDs = Self.eis_mk_study_widgets
WIDGET_CONTROL , widIDs.st_raListID , set_value = rasterList

WIDGET_CONTROL , widIDs.st_raListID ,                                       $
                            set_list_select = Self.eis_study.raster_index

IF (Self.eis_study.raster_index EQ -1) THEN BEGIN
    WIDGET_CONTROL , widIDs.st_ra_titleID   , set_value = 'none'
    WIDGET_CONTROL , widIDs.st_ra_idID      , set_value = 'none
    WIDGET_CONTROL , widIDs.st_ra_acronymID , set_value = 'none
    sensitive = 0
ENDIF ELSE BEGIN
    raster = Self.eis_study.studyRasters[Self.eis_study.raster_index]
    ; ID needs to be in 000001 format
    ra_id = STRING(format='(I06)',raster.ra_id)
    WIDGET_CONTROL , widIDs.st_ra_idID	     , set_value = ra_id
    WIDGET_CONTROL , widIDs.st_ra_titleID   , set_value = raster.ra_title
    WIDGET_CONTROL , widIDs.st_ra_acronymID , set_value = raster.ra_acronym
    sensitive = 1
ENDELSE

; Update TARGET label
target = STRTRIM(Self.eis_study.target,2)
WIDGET_CONTROL , widIDs.st_targetID , set_value = target

; Update CATEGORY label
category = STRTRIM(Self.eis_study.category[0],2)
WIDGET_CONTROL , widIDs.st_categoryID , set_value = category

; Update AUTHOR label
author = STRTRIM(Self.eis_study.author[0],2)
WIDGET_CONTROL , widIDs.st_authorID , set_value = author

; Update DATE label
date = STRTRIM(Self.eis_study.date,2)
WIDGET_CONTROL , widIDs.st_dateID , set_value = date

; Desensitize widgets
WIDGET_CONTROL , widIDs.st_compressionID       , sensitive = sensitive
WIDGET_CONTROL , widIDs.st_eventResponseBaseID , sensitive = sensitive
WIDGET_CONTROL , widIDs.st_repeatsID           , sensitive = sensitive

END
;______________________________________________________________________________
; Load a study from the database
;______________________________________________________________________________
PRO eis_mk_study_gui::loadStudy , st_id

IF (st_id EQ 0) THEN RETURN

; Read study database
new_study = db_read_study_entry( st_id )
; IF entry not found, a zero will be returned. If so, bail out!
IF (N_TAGS(new_study) EQ 0) THEN RETURN

; Load study
Self.eis_study = new_study

Self->updateStudyWidgets

; Finally, save a copy of the current eis_study structure
oStudyItem = OBJ_NEW("eis_study_undo" , Self.eis_study)
Self.studyUndo->ADD , oStudyItem

END
;______________________________________________________________________________
; Load a study from the database
;______________________________________________________________________________
PRO eis_mk_study_gui::updateStudyWidgets

; Get widget IDs
widIDs = Self.eis_mk_study_widgets

; ID needs to be in 000001 format
idValue = STRING(format='(I06)',Self.eis_study.id)
WIDGET_CONTROL , widIDs.st_idID      , set_value = idValue

acronym = STRTRIM(Self.eis_study.acronym , 2)
title   = STRTRIM(Self.eis_study.title   , 2)
IF (Self.eis_study.recursive EQ 1) THEN acronym = acronym + '[Recursive]'
IF (Self.eis_study.recursive EQ 1) THEN title   = title   + '[Recursive]'

WIDGET_CONTROL , widIDs.st_acronymID , set_value = acronym
WIDGET_CONTROL , widIDs.st_titleID   , set_value = title

Self.eis_study.raster_index = -1
; Load study rasters into the list
Self->updateStudyRasterWidgets
; Update Duration and Volume labels
Self->updateDurationAndVolume
; Update Slit/Slot
Self->updateSlitSlot

END
;______________________________________________________________________________
; Update Duration and volume
;______________________________________________________________________________
PRO eis_mk_study_gui::updateSlitSlot

; Get widget IDs
widIDs = Self.eis_mk_study_widgets
slitSlot = [1,266,2,40]
ss       = eis_calculate_study_slit_slot(Self.eis_study)
IF (ss[0] EQ 0) THEN BEGIN
    value = 'no changes'
ENDIF ELSE BEGIN
    value_1 = STRCOMPRESS(ss[0],/REMOVE_ALL) + ' change ('
    value_2 = STRCOMPRESS(slitSlot[ss[1]],/REMOVE_ALL) + 'as -> '
    value_3 = STRCOMPRESS(slitSlot[ss[2]],/REMOVE_ALL) + 'as)'
    value   = value_1 + value_2 + value_3
ENDELSE
WIDGET_CONTROL , widIDs.st_ssLabelID , set_value = value

END
;______________________________________________________________________________
; Update Duration and volume
;______________________________________________________________________________
PRO eis_mk_study_gui::update_slit_slot

Self.eis_study.slit_slot = eis_calculate_study_slit_slot(Self.eis_study)

END
;______________________________________________________________________________
; Update Duration and volume
;______________________________________________________________________________
PRO eis_mk_study_gui::updateDurationAndVolume

widIDs = Self.eis_mk_study_widgets
;
; Calculate Volume
;
volume        = eis_study_volume(Self.eis_study)
Self.eis_study.volume = volume
volume_string = eis_study_volume_string(Self.eis_study ,                   $
                                                units = Self.volumeUnits)

WIDGET_CONTROL , widIDs.st_volumeID , set_value = volume_string

;
; Calculate duration
;
duration = eis_study_duration(Self.eis_study)
; Convert duration to a string
durationString = eis_format_duration(duration , units = Self.timeUnits)
Self.eis_study.duration = duration
WIDGET_CONTROL , widIDs.st_durationID , set_value = durationString

;
; Data Rate
;
st_dr  = (DOUBLE(volume)/1024.)  /  (DOUBLE(duration)/1000.)
dr     = STRJOIN([STRING(format='(D10.4)',st_dr),' kBits/s'],/SINGLE)
WIDGET_CONTROL , widIDs.st_datarateID , set_value = dr

; Update slit/slot
Self->update_slit_slot

END
;______________________________________________________________________________
; Define Data members
;______________________________________________________________________________
PRO	eis_mk_study_gui__define

eis_mk_study_widgets = {eis_mk_study_widgets}

struct = { eis_mk_study_gui                                           ,     $
           tlbID                     : 0L                             ,     $
           eis_mk_study_widgets      : {eis_mk_study_widgets}         ,     $
           studyUndo                 : OBJ_NEW()                      ,     $
           timeUnits                 : 0                              ,     $
           volumeUnits               : 0                              ,     $
           eis_xrt_flare_control     : {eis_xrt_flare_control}        ,     $
           eis_aec_control           : {eis_aec_control}              ,     $
           eis_event_control         : {eis_event_control}            ,     $
           eis_flare_control         : {eis_flare_control}            ,     $
           eis_xrt_flare_gui         : OBJ_NEW()                      ,     $
           eis_aec_event_gui         : OBJ_NEW()                      ,     $
           eis_event_gui             : OBJ_NEW()                      ,     $
           eis_flare_gui             : OBJ_NEW()                      ,     $
           eis_study                 : {eis_study}                          $
         }

END