;---------------------------------------------------------------------------
; Document name: hsi_lightcurve__define.pro
; Created by:    Andre, June 1999
;
; Last Modified: Fri Jul 13 08:59:02 2001 (csillag@sunlight)
;---------------------------------------------------------------------------
;+
; PROJECT:
;       HESSI
;
; NAME:
;       LIGHTCURVE CLASS DEFINITION
;
; PURPOSE:
;       Provides access to HESSI Lightcurves. This class is based
;       (i.e, extends) the spectrogram class.
;
; CATEGORY:
;       Lightcurve (hessi/idl/util)
;
; CONSTRUCTION:
;       obj = Obj_New( 'hsi_lightcurve' )
;       obj = HSI_Lightcurve()
;
; CONTROL PARAMETERS DEFINED IN THIS CLASS:
;
;      ltc_energy_band: energy band for each light curve, in
;                       kev. Default: [3, 15000]
;
;      ltc_time_range: the time range of the light curve. It is a
;                      2-element vector. If the time_range is larger
;                      than 1e6, then it is considered to be an anytim
;                      time (see anytim()). If it is less than 1e6, it
;                      is considered to be seconds relative to either
;                      obs_time_interval (if this is set), otherwise
;                      it is relative to the start of the file
;                      (file_time_range[0]) or to the start of the
;                      simulation (sim_ut_ref + sim_time_range[0]).
;
;      ltc_time_resolution: resolution of the time bins in
;                           seconds. Default: 0.1 sec
;
;      see hsi_lightcurve_control__define
;
;      Control parameters from hsi_spectrogram_control__define are
;      also inherited by this class.
;
; INFO PARAMETERS DEFINED IN THIS CLASS:
;
;      no info prameters yet.
;
; METHODS DEFINED IN THIS CLASS
;
;      INIT: usual stuff
;      Control2Binning: given the lightcurve control parameters,
;                       construct the structure "binning" that is
;                       eventually passed to hsi_eventlist2spectrogra
;      Process_Hook_Pre: the hook called by hsi_spectrogram::process
;                        prior the spectrogram is processed. This
;                        sets the correct values of the contorl
;                        parameter binning according to the ltc_xxx
;                        control parameters
;      GetAxis: allows getting x and y axes. Keywords: XAXIS, YAXIS,
;               XTITLE, YTITLE )
;      GetData: Gets the primary data. Keyword: THIS_SUM
;
; CLASS RELATIONSHIPS
;      HSI_Spectrogram, Framework: parent class
;      HSI_Eventlist: source class
;
; KEYWORDS:
;
; EXAMPLES:
;
;       Start by creating an instance of the class:
;       o = HSI_Lightcurve()
;
;       Then:
;
;       o->Plot, OBS_TIME_INTERVAL = ['2000/09/01 12:00', '2000/09/01
;       18:00'], LTC_TIME_RESOLUTION = 1
;       will plot an integrated light curve for that time interval
;       with a 1 sec. resolution
;
;       ltc = o->GetData( TIME_RANGE=[0,60], LTC_ENERGY_BAND = [3,100,500,
;       1000, 15000] )
;       will return a 2-d array with time in the first dimension,
;       energy bands in the second dimension. The time range of the
;       lightcurve corresponds to the first 60 seconds in the file
;
;       ltc = o->GetData( SUM_FLAG = 0 )
;       will return the same as above, but for each individual a2d set in
;       a2d_index_mask for each a2d and for each energy band
;       defined in ltc_energy_band.
;
;				Retrieving the axes of the lightcurve spectrogram
;
;				To Return the values of the time axis of the lightcurve array
;				as a 1xN array of the edges of the bins referenced to 1-jan-1979
;				UT = o->GetAxis( /UT )
;				To Return the values of the edges of the energy axis as a 1xN array in keV
;				Energy = o->GetAxis( /ENERGY )
;
;				To retrieve either at the midpoints: e.g.  Energy_midpoints = o->GetAxis(/mean,/ENERGY)
;				The call to GETAXIS takes the same keywords as found in get_edges()
;
;
;       ltc = o->GetData( THIS_DET = [1,2] )
;       returns in ltc the lightcurves for detectors # 1 and 2 only.
;
;       ltc = o->GetData( THIS_SEG = [1,10] )
;       returns in ltc the lightcurves for segments # 1 and 10 only
;
;       ltc = o->GetData( THIS_A2D_INDEX = [1, 10, 13, 14, 26] )
;       returns in ltc the lightcurves for a2d indices 1,10,13,14,26
;
;       ltc = o->GetData( /COINCIDENCE )
;       returns a 4-dim array where the 4th dimension contains the
;       anti-coincident lightcurve (index #0) and the coincident
;       lightcurve (index #1)
;
;       ltc =o->GetData( /COINCID, OTHER_A2D_INDEX =
;       [0,0,0,1,1,1,1,bytarr(27-7) ] )
;       returns a 4-dim array where the 4th dim has 5 indices, the
;       first is the anti-coincident lighcturve, the second is the
;       coincident lightcurve with a2d # 3, the third with a2d #4, etc.
;
;
; SEE ALSO:
;       hsi_lightcurve_control__define
;       hsi_lightcurve_info__define
;       hsi_spectrogram__define
;       hsi_spectrum__define
;       http://hessi.ssl.berkeley.edu/software/reference.html#hsi_lightcurve
;
; HISTORY:



;       Dev for release 6, use hsi_spectrum instead of hsi_spectrogram
;       Dev for release 5.1: use of the abstract class hsi_spectrogram
;       Dev. for Release 4, December 1999
;           A Csillaghy, csillag@ssl.berkeley.edu
;		1-apr-02, ras, added UT and ENERGY keywords for getaxis
; 		17-may-02, ras, in Group, used long64 on time  bin
; 		in control2binning
; 		10-Jun-02, Kim.  In Getdata, if not hsi_use_sim, using hsi_eventlist_packet, and no file, then error
;		25-Jul-2002, Kim.  Call heap_gc  (if >= 5.5) in getdata method to prevent memory leaks
;		24-Oct-2002, Kim.  Added @hsi_insert_catch in getdata, and commented out some extra checks
;		   for when no files are available - catch will handle it now.
;	  ras, 11-oct-02, c2b_param saves control parameters relevant
;		to control2binning
;		ras, 19-nov-02, remove nasty side effects of c2b_param
;			setting of multiply-named parameters done in set
;			ltc_time_range has same type as time_range to avoid nasty
;			continuation of need_update
;		17-mar-2006, ras, remove control2binning in favor of hsi_spectrum
;       12-apr-2007, Kim.  Added call to hsi_old_defaults
;   14-juillet-2017, RAS, removed the unnecessary garbage in the init. This object is the spectrum object
;   with the different names for the control parameters managed in Set.  It is really only in the 
;   Set and Getdata that there are minor cosmetic differences
;-

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

FUNCTION HSI_Lightcurve::INIT, SOURCE = source, _EXTRA=_extra

IF NOT Obj_Valid(  source ) THEN  source = HSI_Eventlist( _extra=_extra )
CONTROL = hsi_lightcurve_control()
ret=self->Hsi_Spectrum::INIT( CONTROL = control, $
                                 SOURCE=source )

; reset the eventlist default values. This is dangerous and need to be
; handled in a better way.
time_unit = Self->Get(/TIME_UNIT) ; FUDGE TO INSTANTIATE THE EVENTLIST OBJECT. ras, 23-oct-2001.
self->Set, TIME_UNIT = 1 ;ras, 10-apr-2002 - enables dropouts


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

hsi_old_defaults, self, _extra=_extra

RETURN, ret

END

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

PRO HSI_Lightcurve::Set, $
       LTC_TIME_RANGE=ltc_time_range, $
       TIME_RANGE = time_range, $
       LTC_ENERGY_BAND = ltc_energy_band, $
       LTC_TIME_RESOLUTION = ltc_time_resolution, $
       _EXTRA=_extra, DONE=done, NOT_FOUND=NOT_found

IF Keyword_Set( TIME_RANGE ) THEN ltc_time_range = time_range

IF Keyword_Set( LTC_TIME_RANGE ) THEN TIME_RANGE = ltc_time_range


;Make energy bands use SP_ENERGY_BAND
IF Keyword_Set( LTC_ENERGY_BAND ) then begin
  Self->hsi_spectrum::Set, SP_ENERGY_BINNING = LTC_ENERGY_BAND
  Self->Framework::Set, LTC_ENERGY_BAND = Self->Get(/SP_ENERGY_BINNING)
  ENDIF
;Make time_res use SP_ENERGY_BAND
IF Keyword_Set( LTC_TIME_RESOLUTION ) then begin
  Self->hsi_spectrum::Set, SP_TIME_INTERVAL = LTC_TIME_RESOLUTION
  Self->Framework::Set, LTC_TIME_RESOLUTION = LTC_TIME_RESOLUTION

  ENDIF


self->HSI_SPECTRUM::Set, _EXTRA=_EXTRA
self->Framework::Set, LTC_TIME_RANGE = ltc_time_range, $
	TIME_RANGE= time_range, $
    FOUND=found, NOT_FOUND=NOT_found

END



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

FUNCTION HSI_Lightcurve::GetAxis, $
            XAXIS=xaxis, YAXIS=yaxis, $
            XTITLE=xtitle, YTITLE=ytitle, $
            UT = ut, Energy=Energy, $ ras, 1-apr-02
            _EXTRA=_extra

@hsi_insert_catch

IF Keyword_Set( XTITLE ) THEN RETURN, 'Time [Anytim seconds]'
IF Keyword_Set( YTITLE ) THEN RETURN, 'Counts'

IF NOT Keyword_Set( XAXIS ) AND NOT Keyword_Set( YAXIS ) THEN xaxis =  1
IF Keyword_Set( UT ) or Keyword_Set( ENERGY )then begin
	xaxis = keyword_set( ut)
	yaxis = keyword_set( energy)
	endif


RETURN, self->HSI_Spectrum::GetAxis( XAXIS = yaxis, YAXIS=xaxis, $
                                        _EXTRA=_extra )

END

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

FUNCTION HSI_Lightcurve::GetData, $
            THIS_SUM=this_sum, $
            XAXIS=xaxis, YAXIS=yaxis, $
            XTITLE=xtitle, YTITLE=ytitle, $
            _EXTRA=_extra

@hsi_insert_catch

IF Keyword_Set( _EXTRA ) THEN self->Set, _EXTRA =  _extra  ;kim 12-apr-2002

; this code was removed when added catch, kim 21-aug-02
; if eventlist strategy is packet then check if the filename is valid.  If not,
; print message and return -1.  Otherwise it prints MANY message that it can't find file. kim
; If using simulations will just revert to default simulation if no file
;ev = self->get(/obj,class='hsi_eventlist')	;kim 11-apr-2002 added this block
;this_strat = ev -> getstrategy()
;good = 1
;if not hsi_use_sim() then begin
;	IF Obj_Class( this_strat ) EQ 'HSI_EVENTLIST_PACKET' THEN BEGIN
;	    filename = self->Get( /FILENAME )
;	    IF Size( filename, /TYPE ) NE 7 THEN good=0 else $
;	    	if filename[0] eq '' then good=0
;	    if not good then begin
;	    	message, 'No file available.  Not making lightcurve.', /info
;	    	return, -1
;	    endif
;	ENDIF
;endif

IF Keyword_Set( CLASS_NAME ) THEN $
    IF class_name NE Obj_Class( self ) THEN BEGIN
    source = self->Get( /SOURCE )
    RETURN, source->GetData( CLASS_NAME=class_name, _EXTRA=_extra )
ENDIF

; pass the control to getaxis if axes are required.
IF Keyword_Set( XAXIS ) OR Keyword_Set( YAXIS ) THEN $
    RETURN, self->GetAxis( XAXIS=xaxis, YAXIS=yaxis, _EXTRA=_extra )

lc=self->HSI_Spectrum::GetData( _extra = _EXTRA, THIS_SUM=this_sum )
if since_version('5.5') then heap_gc		;kim 25-jul-2002.  Try this!!!!!

lc_size = Size( lc, /STRUCTURE )

IF lc_size.n_dimensions GT 1 THEN BEGIN
    perm = [ 1, 0, 2, 3 ]
    lc = Transpose( lc, perm[0:lc_size.n_dimensions-1] )
ENDIF

;sum_flag = Keyword_Set( THIS_SUM ) OR self->Get( /SUM_FLAG )
;IF Keyword_Set( sum_flag ) THEN BEGIN
 ;   if lc_size.dimensions[3] ge 2 then lc = total( lc, 4 )
;    if lc_size.dimensions[2] ge 2 then lc = total( lc, 3 )
;ENDIF

RETURN, lc

END

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

;PRO HSI_Lightcurve::Control2Binning_prehook, $
;       _EXTRA=_extra

;c2b_param = Self->Get( /ltc_energy_band, /ltc_time_range, /ltc_time_resolution)
;Self->Set, sp_energy_binning=c2b_param.ltc_energy_band, $
;		   time_range = c2b_param.ltc_time_range, $
;		   sp_time_interval = c2b_param.ltc_time_resolution

;end

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

;PRO HSI_Lightcurve::Control2Binning, $
;       _EXTRA=_extra

;If keyword_set( _extra ) then Self->Set, _EXTRA=_extra
;c2b_param = Self->Get( /ltc_energy_band,  /ltc_time_resolution)
;Self->Set, sp_energy_binning=c2b_param.ltc_energy_band,
;		   sp_time_interval = c2b_param.ltc_time_resolution
;We have to be careful with time_range.  If this is different in any way,
;the eventlist will need to be updated

;self->HSI_Spectrum::control2binning, $
 ;      _EXTRA=_extra

;end
;---------------------------------------------------------------------------
;
PRO HSI_Lightcurve::Process_Hook_Post, $
lc, pha_total_vs_time, $
_extra=_extra ;ras - 7/23/2001

Self->Set, sp_time_first = 1 ; This tells the spectrum::process_post_hook
                              ; to process lightcurve indices

Self->Hsi_Spectrum::Process_Hook_post, $
lc, pha_total_vs_time,$
_extra = _extra



END

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

PRO HSI_Lightcurve__Define

; this is the data structure residing in self.data:

self = {HSI_Lightcurve, $
        INHERITS HSI_Spectrum }

END


;---------------------------------------------------------------------------
; End of 'hsi_profile__define.pro'.
;---------------------------------------------------------------------------
