;---------------------------------------------------------------------------
; Document name: hsi_eventlist__define.pro
; Created by:    Andre Csillaghy, February 17, 1999
;
; Last Modified: Mon Feb 28 09:36:33 2000 (csillag@soleil)
;---------------------------------------------------------------------------
;
;+
; PROJECT:
;       HESSI
;
; NAME:
;       HESSI EVENTLIST CLASS DEFINITION
;
; PURPOSE:
;       Provides the HESSI eventlist data type and associated methods
;
; CATEGORY:
;       Utilities
;
; CONSTRUCTION:
;       obj = Obj_New( 'hsi_eventlist' )
;
; INPUTS:
;       The "Set" method is used to change the state of the object.
;       obj->Set, KEYWORD=value
;       see below for KEYWORD signification
;
; OUTPUTS:
;       Provided by the Get, GetEventlist methods.
;
; GENERIC METHODS:
;       INIT
;       Get
;       Read
;       Set
;       Write
;
; OBJECT METHODS:
;       GetEventList
;
; KEYWORDS:
;
;
; EXAMPLES:
;
;       Start by instatiating the object class:
;       eventlist = Obj_New( 'hsi_eventlist' )
;
;       To create a simulated event list with default parameters:
;       eventlist->Simulate
;       To write an eventlist into a file:
;       eventlist->Write
;
; SUPERCLASSES:
;       UNIVERSAL
;
; SUBCLASSES:
;
; IMPORTED OBJECTS:
;
; SEE ALSO:
;
; HISTORY:
;            
;            jimm's adaptations, 
;       Dev. for Release 3, July 1999
;       Release 2, June 1999
;       Version 1, February 17, 1999,
;           A Csillaghy, csillag@ssl.berkeley.edu
;
;--------------------------------------------------------------------

FUNCTION HSI_Simulation::INIT, _EXTRA = _extra

sim_control = HSI_Sim_Parameters()

tag_name = Tag_Names( sim_control )
n_tag = N_Tags( sim_control )
FOR i=0, n_tag -1 DO BEGIN 
    sim_control = Rep_Tag_Name( sim_control, tag_name[i], 'SIM_' + tag_name[i] )
ENDFOR

sim_control = Add_Tag( sim_control, 0B, 'SIM_JUST_BACKGROUND' )
sim_control = Add_Tag( sim_control, 0B, 'SIM_SASZERO' )

ret=self->Frame::INIT( CONTROL_STRUCT = sim_control )

model = FLTARR( 64, 64 )
model[32, 32] = 1.
model[20, 20] = 1.
map_pixel_size = 1.0
spatial_sigma = [0.5, .5]
gaussians = hsi_map2gaussians( model, map_pixel_size, error=error, $
                               spatial_sigma=spatial_sigma, max_dimension=max_size)
gaussians.(1)[1] = 1.
self->set, sim_pixel_SIZE = 1.0
self->set, sim_model_ptr = Ptr_New( gaussians )
erange = [6, 100.]
count_dist = hsi_cspectrum_dist( erange, nbins=10000)
self->set, sim_count_dist_ptr = Ptr_New( count_dist )
self->set, sim_bkgd_count_dist_ptr = Ptr_New( count_dist ) ;jmm, 19-aug-1999
self->set, sim_energy_band = erange ;jmm, 19-aug-199

self->Set, /DEBUG, /VERBOSE

IF Keyword_Set( _EXTRA ) THEN self->Set, _EXTRA = _extra
    
RETURN, 1

END

;--------------------------------------------------------------------

FUNCTION HSI_Simulation::GetSimulation, _EXTRA = _extra

RETURN, self->GetData( _EXTRA=_extra )

END

;--------------------------------------------------------------------

PRO HSI_Simulation::Process, $
       _EXTRA=_extra

;jmm, 19-aug-1999, Simulate always returns a structure of type {hsi_score},

;stop
IF Keyword_Set( _EXTRA ) THEN self->Set, _EXTRA = _extra

sim_param = self->Get( /NO_DEREFERENCE )

verbose = sim_param.verbose
plot = sim_param.plot
debug = sim_param.debug

IF debug THEN Message, 'Processing', /INFO

sim_param_background = sim_param.sim_background

;New, jmm, 19-aug-1999, simulate image plus background, or just background
;or just image
IF sim_param_background NE 0 THEN yes_background = 1 ELSE yes_background = 0
IF sim_param.sim_just_background THEN yes_model = 0 ELSE yes_model = 1

;For some reason, passing in NOT self -> get(/verbose) returns 254, jmm, 23-aug-1999
IF verbose THEN quiet = 0 ELSE quiet = 1

; now convert the tag names to the original tag names accespted by
; hessi_image_sim.
; this is to wrap the procedure 
sim_param_to_pass =  sim_param
tag_name = Tag_Names( sim_param_to_pass )
n_tag = N_Tags( sim_param_to_pass )
FOR i=0, n_tag-1 DO BEGIN 
    IF Grep( 'SIM_', tag_name[i] ) NE '' THEN BEGIN 
        sim_param_to_pass = Rep_Tag_Name( sim_param_to_pass, $
                                          tag_name[i], $
                                          STR_Chop( tag_name[i], 'SIM_' ) )
    ENDIF
ENDFOR

;First get a background score, if asked for
IF yes_background NE 0 THEN BEGIN
    HESSI_Image_Sim, $
        sim_param_to_pass, $
        bkgd_score, $
        PLOT=plot , $
        QUIET=quiet, $
        /NOWRITE_ASPECT, $
        SASZERO=self->Get( /SIM_SASZERO ), $
        _EXTRA=_extra
    sim_param_background = sim_param_to_pass.background
    sim_param_to_pass.background = 0
ENDIF

;Get a model score, if asked for, note that sim_params.background is now 0
IF (yes_model) THEN BEGIN
    HESSI_Image_Sim, $
        sim_param_to_pass, $
        score, $
        PLOT=plot, $
        QUIET=quiet, $
        /NOWRITE_ASPECT, $
        SASZERO=self->Get( /SIM_SASZERO ), $
        _EXTRA=_extra
ENDIF
;reset sim_params.background
sim_param_to_pass.background = sim_param_background

IF(yes_model AND yes_background) THEN BEGIN
    hsi_score_concat, bkgd_score, score
ENDIF ELSE IF(yes_background) THEN score = bkgd_score

;Now you need to put the eventlist_ptr into self.
;Also the time unit may have changed, so the time unit tags must
;be corrected

self->SetData, score.eventlist_ptr, /POINTER

self->Set, TIME_UNIT = score.time_unit
self->Set, UT_REF = score.ut_ref

END

;--------------------------------------------------------------------

PRO HSI_Simulation__Define

self = { HSI_Simulation, $
         INHERITS Frame }

END


;---------------------------------------------------------------------------
; End of 'hsi_eventlist__define.pro'.
;---------------------------------------------------------------------------
