;+
;NAME:
; hsi_qlook__define
;PROJECT:
; HESSI
;CATEGORY:
; Obeserving Summary
;PURPOSE:
; Defines the hsi_qlook Object, this is inherited by the objects:
; hsi_obs_summ_rate, hsi_obs_summ_flag, hsi_ephemeris, hsi_particle_rate,
; hsi_mod_variance, hsi_flare_list, and hsi_obs_background, and provides
; a midpoint between those objects and the FRAMEWORK object
;CALLING SEQUENCE:
; hsi_qlook__define
;METHODS DEFINED IN THIS CLASS: 
; READ, Reads in data from file
; keywords: filename = the input file, full path please
;           class_name = the class name of the object, ['hsi_obs_summ_rate',
;           'hsi_obs_summ_flag', 'hsi_ephemeris', 'hsi_particle_rate', 
;           'hsi_mod_variance', 'hsi_flare_list', 'hsi_obs_background']
;           THIS IS A REQUIRED INPUT, it shouldn't be...
; WRITE, Writes data to a file
; keywords: filename = the output file, full path please
; ONLY_TIME_RANGE, Keeps only the data inside the given time range for the
;                  object, resets the other parameters as necessary
; keywords: time_range = the time range, this is required
; SET, Sets values for structures and keywords
; keywords: info = Sets the info structure to the value passed in
;           data = Sets the data structure to the value passed in
;           control = Sets the control structure to the value passed in
;           _extra = passed through to FRAMEWORK::SET, this is the way
;                    to set individual parameters in the info and
;                    control structures
; GET, gets parameters and data
; keywords: class_name = the object class_name = ''
;           Control= the control structure (type hsi_qlook_control)
;                    contains, class_name, version (object), id_string,
;                    vers_info (version # for the info), vers_data (version #
;                    for the data), obs_time_interval
;           info= the info structure 
;           data = the data structure, 
;           time_array, xaxis pass out an array of interval Start Times.
;           _extra = passed through to FRAMEWORK::SET, this is the way
;                    to get individual parameters in the info and
;                    control structures
; GETDATA, Calls Framework Getdata, to get the data
; keywords: filename= the data file, can have the full path, or can be found
;                     in the data directory or archive
;           obs_time_interval= the time interval in UT, any ANytim format
;           Data_dir= the data directory, the default is '$HSI_DATA_ARCHIVE'
;                     if the full path is not in the filename
;           Filedb_dir= the directory containing the filedb structure,
;                       the default is '$HSI_FILEDB_ARCHIVE'
;           Class_name= the object class name, must be passed in
;           _extra=_extra, passed through to FRAMEWORK::GETDATA
;HISTORY:
; 26-May-2000, jmm, jimm@ssl.berkeley.edu
; Inherits framework, 12-oct-2000, jmm
; Doc_header, 22-feb-2001, jmm
; Added init function, filename as a pointer in the control structure
; 3-mar-2001, jmm
; Have to be careful, do not get and set control, because the
; pointer is lost, and the filenames cannot be set to arbitrary arrays
; jmm, 5-mar-2001
; Changed the name to hsi_qlook, from hsi_obs_summ_obj
; 30-May-2001, Kim.  In only_time_range: check for data type = 'OBJ' as well
;   as 'STC', changed calc of start/end times for qlook images, and check
;   for info.n_images=0 before using.
;
;-
FUNCTION Hsi_qlook::init, Class_name=class_name, _extra=_extra

   ret = self -> framework::init()
   control = hsi_qlook_version_control('HSI_QLOOK', /control)
   IF(KEYWORD_SET(class_name)) THEN BEGIN
      cn = strcompress(strupcase(class_name), /remove_all)
      control.class_name = cn
   ENDIF
   ptr_free, control.filename
   control.filename = ptr_new('NONE')
   control.energy_band =  [12.0, 25.0]
   self -> set, control = control
   self -> set, info = {dummy: -1}
   self -> set, data = -1
   self -> set, need_update = 0 ;If nothing is set, nothing happens
   IF Keyword_Set( _EXTRA ) THEN self -> Set, _EXTRA = _extra
   RETURN, 1
END

PRO Hsi_qlook::cleanup

   self -> framework::cleanup

   RETURN
END


PRO Hsi_qlook::Read, filename=filename, Class_name=class_name, $
    _extra=_extra

;First deal with the file
   IF(KEYWORD_SET(filename)) THEN filex = filename[0] $
     ELSE filex = 'hsi_0.fits'

   filex = loc_file(filex, count = bb)
   IF(bb EQ 0) THEN BEGIN
      message, /info, filex+' NOT Found, Bye...'
      RETURN
   ENDIF
   filex = filex[0]

   IF(KEYWORD_SET(class_name)) THEN BEGIN
      cn = strcompress(strupcase(class_name), /remove_all)
   ENDIF ELSE cn = self -> get(/class_name)

   CASE cn OF
      'HSI_OBS_SUMM_RATE': ext_id = 'HESSI OBS SUMMARY RATE ID TABLE'
      'HSI_MOD_VARIANCE': ext_id = 'HESSI MOD VARIANCE ID TABLE'
      'HSI_PARTICLE_RATE': ext_id = 'HESSI PARTICLE RATE ID TABLE'
      'HSI_EPHEMERIS': ext_id = 'HESSI EPHEMERIS ID TABLE'
      'HSI_OBS_SUMM_FLAG': ext_id = 'HESSI OBS SUMMARY FLAG ID TABLE'
      'HSI_FLARE_LIST': ext_id = 'HESSI FLARE LIST ID TABLE'
      'HSI_OBS_BACKGROUND':ext_id = 'HESSI OBS BACKGROUND ID TABLE'
      'HSI_QLOOK_IMAGE': ext_id = 'HESSI QLOOK IMAGE'
      'HSI_QLOOK_SPECTRUM': ext_id = 'HESSI QLOOK SPECTRUM'
      'HSI_QLOOK_POINTING': ext_id = 'HESSI QLOOK POINTING ID TABLE'
      'HSI_QLOOK_ROLL_PERIOD': ext_id = 'HESSI QLOOK ROLL PERIOD ID TABLE'
      'HSI_QLOOK_ROLL_ANGLE': ext_id = 'HESSI QLOOK ROLL ANGLE ID TABLE'
      ELSE: BEGIN
         message, /info, 'Bad Class name'
      END
   ENDCASE

;Open and get the ID and version, be sure that it is there
   err = ' '
   fxbopen, unit0, filex, ext_id, h00, err = err
   err = strupcase(strtrim(err, 2))
   IF(err EQ 'REQUESTED EXTENSION NOT FOUND') THEN BEGIN
;The file has no extension
      fxbclose, unit0
      message, /info, 'File: '+filex+' Has no '+ext_id+' returning'
      self -> set, nodata = 1b
      RETURN
   ENDIF
   fxbread, unit0, version, 1, 1
   fxbread, unit0, id_string, 2, 1
   fxbread, unit0, vers_info, 3, 1
   fxbread, unit0, vers_data, 4, 1
   fxbclose, unit0
;Fill control structure
   control = hsi_qlook_version_control('HSI_QLOOK', $
                                       in_version = version, /control)
   control.version = version
   control.id_string = strtrim(id_string, 2)
   control.vers_info = vers_info
   control.vers_data = vers_data
   control.class_name = cn
   ptr_free, control.filename
   control.filename = ptr_new(filex)
   
;Define the structures, and read in the info & data.
   info_inp = hsi_qlook_version_control(cn, in_version = vers_info, /info)
   hsi_obs_summ_fxbr, filex, info_inp, info
   data_inp = hsi_qlook_version_control(cn, in_version = vers_data, /data)
   hsi_obs_summ_fxbr, filex, data_inp, data
;Set control.obs_time_interval if you have data...
   IF(datatype(data) EQ 'STC') THEN BEGIN
      IF(cn EQ 'HSI_OBS_BACKGROUND') THEN BEGIN
         control.obs_time_interval = [min(data.bck_time), $
                                       max(data.bck_time)]
      ENDIF ELSE IF(cn EQ 'HSI_FLARE_LIST') THEN BEGIN
         nn1 = info.n_flares-1
         control.obs_time_interval = anytim([info.list_start_time, $
                                              info.list_end_time])
      ENDIF ELSE BEGIN
         control.obs_time_interval = [info.ut_ref, info.ut_ref+$
                                       info.n_time_intv*info.time_intv]
      ENDELSE
      control.nodata = 0b
   ENDIF ELSE control.nodata = 1b

   self -> set, info = info, data = data, control = control
   self -> set, need_update = 0
;And that should be it
   RETURN
END

PRO Hsi_qlook::Write, filename=filename, overwrite=overwrite, _extra=_extra

;Check for data
   info = self -> get(/info)
   data = self -> get(/data)
   IF(datatype(data) NE 'STC') THEN BEGIN
      message, 'No data', /info
      RETURN
   ENDIF
   
   control = self -> get(/control)
   cn = control.class_name
   CASE cn OF
      'HSI_OBS_SUMM_RATE': BEGIN
         ext_id = 'HESSI OBS SUMMARY RATE ID TABLE'
         file_start = 'hosrate'
      END
      'HSI_MOD_VARIANCE': BEGIN
         ext_id = 'HESSI MOD VARIANCE ID TABLE'
         file_Start = 'hosmvar'
      END
      'HSI_PARTICLE_RATE': BEGIN
         ext_id = 'HESSI PARTICLE RATE ID TABLE'
         file_Start = 'hosprate'
      END
      'HSI_EPHEMERIS': BEGIN
         ext_id = 'HESSI EPHEMERIS ID TABLE'
         file_Start = 'hoseph'
      END
      'HSI_OBS_SUMM_FLAG': BEGIN
         ext_id = 'HESSI OBS SUMMARY FLAG ID TABLE'
         file_Start = 'hosflag'
      END
      'HSI_FLARE_LIST': BEGIN
         ext_id = 'HESSI FLARE LIST ID TABLE'
         file_Start = 'hsi_flist'
      END
      'HSI_OBS_BACKGROUND': BEGIN
         ext_id = 'HESSI OBS BACKGROUND ID TABLE'
         file_Start = 'hosbck'
      END
      'HSI_QLOOK_IMAGE': BEGIN
          ext_id = 'HESSI QLOOK IMAGE'
         file_Start = 'hqlimg'
      END
      'HSI_QLOOK_SPECTRUM': BEGIN
          ext_id = 'HESSI QLOOK SPECTRUM'
         file_Start = 'hqlspc'
      END
      'HSI_QLOOK_POINTING': BEGIN
         ext_id = 'HESSI QLOOK POINTING ID TABLE'
         file_Start = 'hqlpnt'
      END
      'HSI_QLOOK_ROLL_PERIOD': BEGIN
         ext_id = 'HESSI QLOOK ROLL PERIOD ID TABLE'
         file_Start = 'hqlroll'
      END
      'HSI_QLOOK_ROLL_ANGLE': BEGIN
         ext_id = 'HESSI QLOOK ROLL ANGLE ID TABLE'
         file_Start = 'hqlangl'
      END

      ELSE: BEGIN
         message, /info, 'Bad Class name'
      END
   ENDCASE

;Ok, first deal with the file
   IF(KEYWORD_SET(filename)) THEN filex = filename ELSE BEGIN
      filex = self -> get(/filename)
      filex = filex[0]
      IF(filex EQ ' ') THEN BEGIN
;Make new file from ut_ref
         tt = anytim(info.ut_ref, /ccsds)
         filex = file_start+strmid(tt, 0, 4)+strmid(tt, 5, 2)+ $
            strmid(tt, 8, 2)+'_'+strmid(tt, 11, 2)+strmid(tt, 14, 2)+ $
            strmid(tt, 17, 2)+'.fits'
      ENDIF
   ENDELSE

   filename = filex             ;passed out
;Look for the file, if it exists, use it, if not, open an empty one
   aa = loc_file(filex, count = bb)
;CHeck to see if there already exists an obs summary extension if not
;to be overwritten anyway
   IF(bb EQ 0 OR keyword_set(overwrite)) THEN BEGIN ;create the file
      fxhmake, h0, /extend, /initialize ;create a header
      fxwrite, filename, h0        ;write the file
   ENDIF ELSE BEGIN
      err = ' '
      fxbopen, unitp, filename, ext_id, h0, err=err
      fxbclose, unitp           ;jmm, 21-aug-2000
      err = strupcase(strtrim(err, 2))
      IF(err NE 'REQUESTED EXTENSION NOT FOUND') THEN BEGIN
;The file has the extensions already, don't write to it
         message, /info, 'File: '+filex+' Has been appended, returning'
         RETURN
      ENDIF
   ENDELSE 

;If the extension doesn't exist, or if you're overwriting, start it up
   fxbhmake, h00, 1, ext_id
;Add 4 columns to the header
   fxbaddcol, 1, h00, control.version, 'Version Number'
   temp = string(' ', format = '(a80)')
   strput, temp, control.id_string
   fxbaddcol, 2, h00, temp, 'ID STRING'
   fxbaddcol, 3, h00, control.vers_info, 'Info Version Number'
   fxbaddcol, 4, h00, control.vers_data, 'Data Version Number'

;Create the extension
   fxbcreate, unit0, filex, h00
;And write it out
   fxbwrite, unit0, control.version, 1, 1
   fxbwrite, unit0, temp, 2, 1
   fxbwrite, unit0, control.vers_info, 3, 1
   fxbwrite, unit0, control.vers_data, 4, 1
   fxbfinish, unit0
;Ok, that's it, you now have a file, append it...
   hsi_obs_summ_fxbw, filex, info
   hsi_obs_summ_fxbw, filex, data

;And that should be it
   RETURN
END

PRO Hsi_qlook::Only_time_range, Time_range=time_range, $
    filedb_dir=filedb_dir, Data_dir=data_dir ;jmm, 5-mar-2001

   IF(N_ELEMENTS(time_range) NE 2) THEN BEGIN
      message, /info, 'Bad Time range, returning'
      RETURN
   ENDIF
   tr0 = anytim(time_range)

;Return the data inside the given time range
   info = self -> get(/info)
   data = self -> get(/data)
   control = self -> get(/control)
   IF(datatype(data) NE 'STC') AND (datatype(data) NE 'OBJ') THEN BEGIN
      message, /info, 'No data in input time range'
      RETURN
   ENDIF
;Ok
   cn = control.class_name
   IF(cn EQ 'HSI_OBS_BACKGROUND') THEN BEGIN
;Any set of backgrounds which have time intervals that are
;partially included in the time range are included
      ok_times = where(data.bck_time[1] GE tr0[0] AND $
                       data.bck_time[0] LE tr0[1])
      IF(ok_times[0] EQ -1) THEN BEGIN
         message, /info, 'No data in input time range'
         data = -1
         oti = tr0
         info.n_background = 0
      ENDIF ELSE BEGIN
         data = data[ok_times]
         oti = [min(data.bck_time), max(data.bck_time)]
         info.n_background = N_ELEMENTS(ok_times)
      ENDELSE
   ENDIF ELSE IF(cn EQ 'HSI_FLARE_LIST') THEN BEGIN
;Any flares that have start, peak or end times inside the range are included
      st_times = data.start_time
      pk_times = data.peak_time
      en_times = data.end_time
      condition = (st_times GE tr0[0] AND st_times LE tr0[1]) OR $
        (pk_times GE tr0[0] AND pk_times LE tr0[1]) OR $
        (en_times GE tr0[0] AND en_times LE tr0[1])
      ok = where(condition)
      IF(ok[0] EQ -1) THEN BEGIN
         message, /info, 'No Flares found'
         data = -1
         info.n_flares = 0
         info.list_start_time = anytim(tr0[0], /ccsds)
         info.list_end_time = anytim(tr0[1], /ccsds)
         oti = tr0
      ENDIF ELSE BEGIN
         nn = N_ELEMENTS(ok)
         data = data[ok]
         info.n_flares = nn
         info.list_start_time = anytim(data[0].start_time, /ccsds)
         info.list_end_time = anytim(data[nn-1].end_time, /ccsds)
         oti = [data[0].start_time, data[nn-1].end_time]
      ENDELSE
   ENDIF ELSE IF(cn EQ 'HSI_QLOOK_IMAGE') THEN BEGIN
;Any images that have start or end times inside the range are included
      n = info.n_images
      IF(n GT 0) THEN BEGIN
         st_times = dblarr(n) & en_times=st_times
         FOR j=0, n-1 DO BEGIN  ;kim
            trxj = (data[j] -> get(/file_time_range))[0] + $ 
               data[j] -> get(/time_range)
            st_times[j] = trxj[0]
            en_times[j] = trxj[1]
         ENDFOR
         condition = (st_times GE tr0[0] AND st_times LE tr0[1]) OR $
            (en_times GE tr0[0] AND en_times LE tr0[1])
         ok = where(condition)
      ENDIF ELSE ok = -1
      IF(ok[0] EQ -1) THEN BEGIN
         message, /info, 'No Images found'
         data = -1
         info.n_images = 0
         info.start_time = tr0[0]
         info.end_time = tr0[1]
         oti = tr0 
      ENDIF ELSE BEGIN
         nn = N_ELEMENTS(ok)
         data = data[ok]
         info.n_images = nn
         info.start_time = st_times[ok[0]]
         info.end_time = en_times[ok[nn-1]]
         oti = [info.start_time, info.end_time]
      ENDELSE
   ENDIF ELSE IF(cn EQ 'HSI_QLOOK_SPECTRUM') THEN BEGIN
;Any spectra that have start or end times inside the range are included
      n = info.n_spectra
      st_times = dblarr(n) & en_times = st_times
      FOR j = 0, n-1 DO BEGIN
         st_times[j] = data[j] -> get(/ut_ref)
         trxj = data[j] -> get(/time_range)
         en_times[j] = st_times[j]+trxj[1]
      ENDFOR
      condition = (st_times GE tr0[0] AND st_times LE tr0[1]) OR $
        (en_times GE tr0[0] AND en_times LE tr0[1])
      ok = where(condition)
      IF(ok[0] EQ -1) THEN BEGIN
         message, /info, 'No Spectra found'
         data = -1
         info.n_spectra = 0
         info.start_time = tr0[0]
         info.end_time = tr0[1]
         oti = tr0
      ENDIF ELSE BEGIN
         nn = N_ELEMENTS(ok)
         data = data[ok]
         info.n_spectra = nn
         info.start_time = st_times[ok[0]]
         info.end_time = en_times[ok[nn-1]]
         oti = [info.start_time, info.end_time]
      ENDELSE
   ENDIF ELSE BEGIN
      time_array = self -> get(/time_array)
      ok_data = where(time_array GE tr0[0] AND time_array LT tr0[1])
      IF(ok_data[0] NE -1) THEN BEGIN
         data = data[ok_data]
         time_array = time_array[ok_data]
         ndata = N_ELEMENTS(ok_data)
;Reset info times
         info.ut_ref = anytim(time_array[0])
         info.n_time_intv = ndata
         oti = [time_array[0], time_array[ndata-1]+info.time_intv]
      ENDIF ELSE BEGIN
         message, /info, 'No data in input time range'
         message, /info, $ 
            'Returning the point in the file closest to the input time range'
         aaa = 0.5*(tr0[0]+tr0[1])
         dt = self -> get(/time_intv)
         ppp = min(abs(time_array+dt/2.0d0-aaa), ok_data)
         data = data[ok_data]
         time_array = time_array[ok_data]
         ndata = N_ELEMENTS(ok_data)
;Reset info times
         info.ut_ref = anytim(time_array[0])
         info.n_time_intv = ndata
         oti = [time_array[0], time_array[ndata-1]+info.time_intv]
      ENDELSE
   ENDELSE
   id_string = cn+': '+anytim(oti[0], /ccsds)+ $
     ' TO '+anytim(oti[1], /ccsds)
   control.obs_time_interval = oti
   control.id_string = id_string
   self -> set, info = info, data = data, control = control

   RETURN
END

PRO Hsi_qlook::Set, $
    Obs_time_interval=obs_time_interval, $
    Filename=filename, $
    energy_band= energy_band, $
    info=info, data=data, Control=control, _extra=_extra

;There are three control parameters used for qlook_objects
;If obs_time_interval is set, check to see if the one in the object
;is different, if it is, set it, and set /need_update. Need_update = 1
;means that the obs_time_Interval has changed, but not the filename
;need_update = 2 means that the filename has been changed, but not
;the obs_time_interval. Need_update = 3 means that both the filename
;and obs_time_interval are new.
;need_update=4 means that the energy_band is new, but the
;obs_time_interval
;and  filename are unchanged
   IF(KEYWORD_SET(obs_time_interval)) THEN BEGIN
      nu = self -> get(/need_Update)
      oti = self -> get(/obs_time_interval) ;This is defined in INIT
      oti_in = anytim(obs_time_interval)
      IF(total(oti_in-oti) NE 0.0) THEN BEGIN
         self -> framework::set, obs_time_interval = oti_in
         CASE nu OF
            0: self -> set, need_update = 1
            1: self -> set, need_update = 1
            2: self -> set, need_update = 3
            3: self -> set, need_update = 3
            4: self -> set, need_update = 1
            ELSE: message, /info, 'Invalid Need_update state'
         ENDCASE
      ENDIF
   ENDIF
;If filename is set, check to see if the one in the object
;is different, If it is, set the filename control parameter, and
;need_update
   IF(KEYWORD_SET(filename)) THEN BEGIN
      nu = self -> get(/need_Update)
      fname = self -> get(/filename) ;This is defined in INIT
      n_fname = N_ELEMENTS(fname)
      n_filesin = N_ELEMENTS(filename)
      condition = datatype(fname) EQ 'STR' AND datatype(filename) EQ 'STR'
      IF(condition AND n_filesin EQ n_fname) THEN BEGIN
         same = 1b              ;if all files are the same, same is 1
         FOR j = 0, n_fname-1 DO IF(filename[j] NE fname[j]) THEN same = 0b
      ENDIF ELSE same = 0b
      IF(same EQ 0) THEN BEGIN  ;set files and need_update
         self -> framework::set, filename = filename
         CASE nu OF
            0: self -> set, need_update = 2
            1: self -> set, need_update = 3
            2: self -> set, need_update = 2
            3: self -> set, need_update = 3
            4: self -> set, need_update = 2
            ELSE: message, /info, 'Invalid Need_update state'
         ENDCASE
      ENDIF
   ENDIF
;if energy_band is set (for qlook_images only) then check to see if
;it's the same , if not, set need update appropriately
   IF(KEYWORD_SET(energy_band)) THEN BEGIN
       cn =  self -> get(/class_name)
       IF(cn EQ 'HSI_QLOOK_IMAGE') THEN BEGIN
           nu = self -> get(/need_Update)
           eband = self -> get(/energy_band) ;This is defined in INIT
           eband_in = anytim(energy_band)
           IF(total(eband_in-eband) NE 0.0) THEN BEGIN
               self -> framework::set, energy_band = eband_in
               CASE nu OF
                   0: self -> set, need_update = 4
                   1: self -> set, need_update = 1
                   2: self -> set, need_update = 3
                   3: self -> set, need_update = 3
                   4: self -> set, need_update = 4
                   ELSE: message, /info, 'Invalid Need_update state'
               ENDCASE
           ENDIF
       ENDIF
   ENDIF

   IF(KEYWORD_SET(info)) THEN BEGIN
      self -> framework::set, info = info
   ENDIF
   IF(KEYWORD_SET(control)) THEN BEGIN
      self -> framework::set, control = control
   ENDIF
   IF(KEYWORD_SET(data)) THEN BEGIN
      IF(ptr_valid(self.data)) THEN ptr_free, self.data
      self.data = ptr_new(data)
   ENDIF
;Anything else...
   self -> framework::set, _extra = _extra

   RETURN
END

FUNCTION Hsi_qlook::Get, $
         class_name = class_name, $
         Control=control, $
         info=info, data=data, $
         time_array=time_array, $
         Xaxis=xaxis, $
         _extra=_extra

   otp = -1
   IF(KEYWORD_SET(class_name)) THEN BEGIN
      con = self -> framework::get(/control)
      otp = con.class_name
   ENDIF ELSE IF(KEYWORD_SET(info)) THEN BEGIN
      otp = self -> framework::get(/info)
   ENDIF ELSE IF(KEYWORD_SET(control)) THEN BEGIN
;Framework won't return a pointer for filename so we have to define
;the structure here
      otp = self -> framework::get(/control, /no_dereference)
   ENDIF ELSE IF(KEYWORD_SET(data)) THEN BEGIN
      otp = *self.data
   ENDIF ELSE IF(KEYWORD_SET(time_array) OR KEYWORD_SET(xaxis)) THEN BEGIN
      otp = hsi_obs_summ_timarr(self -> get(/info))
   ENDIF ELSE IF(KEYWORD_SET(_extra)) THEN BEGIN
;Anything Else
      otp = self -> framework::get(_extra = _extra)
   ENDIF ELSE BEGIN
      otp = self -> framework::get()
   ENDELSE

   RETURN, otp
END

FUNCTION Hsi_qlook::getdata, $
            filename=filename, $
            obs_time_interval=obs_time_interval, $
            energy_band=energy_band, $
            Data_dir=data_dir, $
            Filedb_dir=filedb_dir, $
            Class_name=class_name, $
            de_bytescale=de_bytescale, $
            _extra=_extra

   IF(KEYWORD_SET(data_dir)) THEN dtdr = data_dir[0] $
     ELSE dtdr = '$HSI_DATA_ARCHIVE'
   IF(KEYWORD_SET(class_name)) THEN cn = class_name $ ;usually set
     ELSE cn = self -> get(/class_name)
   IF(KEYWORD_SET(obs_time_interval)) THEN $
     self -> set, obs_time_interval = obs_time_interval
   IF(KEYWORD_SET(filename)) THEN $
     self -> set, filename = filename
   IF(KEYWORD_SET(energy_band)) THEN $
     self -> set, energy_band = energy_band
   nu = self -> get(/need_update)
   oti = self -> get(/obs_time_interval)
;Here i may either have a time_range or a filename;
   IF(nu EQ 2 OR nu EQ 3) THEN BEGIN ;updated filename
      filesx = self -> get(/filename)
;if there is a directory in the broken filename, assume that
;you have the full path, otherwise, assume that it's in the
;local or archive directory
      break_file, filesx[0], disk_log, dir, filnam, ext
      IF(dir EQ '') THEN BEGIN
         filesx = loc_file(filesx, count = bb)
         IF(bb EQ 0) THEN $
           filesx = hsi_find_in_archive(filesx, archive_dir = dtdr, $
                                        count = bb, /no_dialog)
         IF(bb EQ 0) THEN BEGIN
            message, /info, 'No files found'
            RETURN, -1
         ENDIF
      ENDIF
   ENDIF ELSE IF(nu EQ 1) THEN BEGIN ;only obs_time_interval has been set
;But if there is a valid filename, with data in it that covers the
;obs_time_interval, then use that filename
      filesx0 = self -> get(/filename)
      IF(datatype(filesx0) EQ 'STR') THEN BEGIN
         IF(filesx0[0] NE 'NONE') THEN BEGIN
            c00 = 0
            FOR j=0, n_elements(filesx0)-1 DO BEGIN
;Get the observing summary
               o0 = obj_new('hsi_obs_summ_soc')
               info0 = o0 -> getdata(filename=filesx0[j], /just_info)
               IF(datatype(info0) EQ 'STC') THEN BEGIN
                  t00 = [anytim(info0.summary_start_time), $
                         anytim(info0.summary_end_time)]
                  IF(c00 EQ 0) THEN t0 = t00 ELSE t0 = [t0, t00]
                  c00 = c00+1
               ENDIF
               IF(obj_valid(o0)) THEN obj_destroy, o0
            ENDFOR
         ENDIF ELSE c00 = 0
      ENDIF ELSE c00 = 0
      IF(c00 GT 0) THEN BEGIN
         filesx_tr = [min(t0), max(t0)]
         IF(oti[0] GE filesx_tr[0]) AND (oti[1] LE filesx_tr[1]) THEN BEGIN
            filesx = filesx0
            bb = c00
         ENDIF ELSE BEGIN
            filesx = hsi_filedb_filename(oti, filedb_dir=filedb_dir, $
                                         /daily_files, file_type_out=fto)
            filesx = hsi_find_in_archive(filesx, archive_dir=dtdr, $
                                         count=bb, /no_dialog)
         ENDELSE
      ENDIF ELSE BEGIN
         filesx = hsi_filedb_filename(oti, filedb_dir=filedb_dir, $
                                      /daily_files, file_type_out=fto)
         filesx = hsi_find_in_archive(filesx, archive_dir=dtdr, $
                                      count=bb, /no_dialog)
      ENDELSE
      
      IF(bb EQ 0) THEN BEGIN
         message, /info, 'No files found'
         RETURN, -1
      ENDIF
   ENDIF ELSE IF(nu EQ 4) THEN BEGIN ;Only energy_band has been set
      filesx = self->get(/filename)
   ENDIF ELSE BEGIN             ;return the data that is there
      RETURN, self -> get(/data)
   ENDELSE
;If you're here, then you have a filesx array
   IF(datatype(filesx) NE 'STR') THEN BEGIN
      IF(NOT KEYWORD_SET(quiet)) THEN message, /info, 'No files found'
      RETURN, -1
   ENDIF
;Now that i have files, get the data from each file, and concatenate
   nfilesx = N_ELEMENTS(filesx)
   IF(nfilesx GT 1) THEN BEGIN
      xcount = 0
      FOR j = 0, nfilesx-1 DO BEGIN
;If it's the first file, use self for the object
         IF(xcount EQ 0) THEN BEGIN
            oo = self           ;jmm, 21-aug-2000
            oo -> set, need_update = 0 ;i think this is needed, if oo = self
         ENDIF ELSE oo = obj_new(strlowcase(cn))
         dummy = oo -> Getdata(filename = filesx[j])
         IF(datatype(dummy) EQ 'STC') THEN BEGIN
            IF(xcount EQ 0) THEN BEGIN
               xcount = 1
               self = oo
            ENDIF ELSE BEGIN
               self = hsi_qlook_concat(self, oo, $
                                              unconcatenated = not_done)
               IF(obj_valid(oo)) THEN obj_destroy, oo
               IF(not_done EQ 1) THEN BEGIN
                  message, /info, 'Objects not concatenated, bye...'
                  RETURN, -1
               ENDIF
            ENDELSE
         ENDIF
      ENDFOR
      IF(xcount EQ 0) THEN BEGIN
         message, /info, 'No obs summaries??, bye...'
         RETURN, -1
      ENDIF
   ENDIF ELSE BEGIN             ;only one file...
;Look for file, check to see if the extension exists, if not, return
      aa = loc_file(filesx[0], count = bb)
      IF(bb EQ 0) THEN BEGIN    ;No file
         message, /info, 'Telemetry File not found:'
         print, filesx[0]
         self -> set, id_string = 'Telemetry File not found'
         self -> set, data = -1
         self -> set, nodata = 1b
      ENDIF ELSE BEGIN
         aa = aa[0]
;Check to see if there exists an obs summary extension
         CASE cn OF
            'HSI_OBS_SUMM_RATE': ext_id = 'HESSI OBS SUMMARY RATE ID TABLE'
            'HSI_MOD_VARIANCE': ext_id = 'HESSI MOD VARIANCE ID TABLE'
            'HSI_PARTICLE_RATE': ext_id = 'HESSI PARTICLE RATE ID TABLE'
            'HSI_EPHEMERIS': ext_id = 'HESSI EPHEMERIS ID TABLE'
            'HSI_OBS_SUMM_FLAG': ext_id = 'HESSI OBS SUMMARY FLAG ID TABLE'
            'HSI_FLARE_LIST': ext_id = 'HESSI FLARE LIST ID TABLE'
            'HSI_OBS_BACKGROUND': ext_id = 'HESSI OBS BACKGROUND ID TABLE'
            'HSI_QLOOK_IMAGE': ext_id = 'HESSI QLOOK IMAGE'
            'HSI_QLOOK_SPECTRUM': ext_id = 'HESSI QLOOK SPECTRUM'
            'HSI_QLOOK_POINTING': ext_id = 'HESSI QLOOK POINTING ID TABLE'
            'HSI_QLOOK_ROLL_PERIOD': ext_id = 'HESSI QLOOK ROLL PERIOD ID TABLE'
            'HSI_QLOOK_ROLL_ANGLE': ext_id = 'HESSI QLOOK ROLL ANGLE ID TABLE'
            ELSE: BEGIN
               message, /info, 'Bad Class name'
            END
         ENDCASE
         err = ' '
         fxbopen, unitp, aa, ext_id, h0, err = err
         fxbclose, unitp        ;jmm, 21-aug-2000
         err = strupcase(strtrim(err, 2))
         IF(err NE 'REQUESTED EXTENSION NOT FOUND') THEN BEGIN
;The file has the extension, Read it in
            IF(NOT KEYWORD_SET(quiet)) THEN BEGIN
               message, /info, 'Reading from file'
               print, aa
            ENDIF
            self -> read, filename = aa, class_name = cn
         ENDIF ELSE BEGIN
;The file has no extension, Return
            message, /info, 'Telemetry File Has no '+ext_id
            print, filesx[0]
            self -> set, id_string = 'Telemetry File not found'
            self -> set, data = -1
         ENDELSE
      ENDELSE                   ;one file
   ENDELSE
;Ok, here i have an object, if i have set an obs_time_interval, then
;I only want to keep the stuff inside that time range
   IF(nu EQ 1 OR nu EQ 3 OR nu EQ 4) THEN BEGIN
;You may Need to reset the filename if nu = 3, can't do this if there
;is no archive
;      IF(nu EQ 3) THEN BEGIN
;         filesx = hsi_filedb_filename(oti, filedb_dir = filedb_dir)
;         filesx = hsi_find_in_archive(filesx, archive_dir = dtdr, /no_dialog)
;      ENDIF
      self -> only_time_range, time_range = oti
   ENDIF
   self -> set, filename = filesx
   self -> set, need_Update = 0
;output data structure, unless de_bytescale is set for aspect objects
   IF(keyword_set(de_bytescale)) THEN BEGIN
      IF(cn EQ 'HSI_QLOOK_POINTING' OR $
         cn EQ 'HSI_QLOOK_ROLL_PERIOD' OR $
         cn EQ 'HSI_QLOOK_ROLL_ANGLE') THEN BEGIN
         data = self -> get(/data)
         info = self -> get(/info)
         RETURN, info.a0 + data.(0)*info.a1
      ENDIF ELSE RETURN, self -> get(/data)
   ENDIF ELSE RETURN, self -> get(/data)
END

PRO Hsi_qlook__define

;Hessi Observing Summary Object
   self = {hsi_qlook, inherits framework}

   RETURN
END
