;+
; NAME: irisobs__define
;
;
;
; PURPOSE: Definiton of the irisobs (IRIS observation) superclass; This 
;          class contains IRIS mission specific code only, all code that 
;          is common for all missions and instruments can be found in the 
;          superclass anyobs, and spectrograph specific code can be found 
;          in spectobs. Most of the IRIS specific code can be found in the two
;          subclasses irissjiobs and irisspectobs.
;
;                                           (sdc_util)
;                                              |
;                                            anyobs    spectobs
;                                          /       \ /        \
;                                   hinobs         /\          \
;                                  /   |   \     /   \          \
;                                    (...)  eisobs    IRISOBS    \
;                                                      |     \    \
;                                                  irissjiobs irisspectobs       
;
;
; METHODS: 
;
;
; SPECIAL CALLS: anyobs__define.pro and spectobs__define.pro
;
;
; WRITTEN: Terje Fredvik, UiO, April 2013
;
; MODIFICATION HISTORY:
;            15 Apr 2013 Terje Fredvik- First draft
;            06 Jun 2013 Terje Fredvik- Moved most of the code to new subclass
;             irisspectobs, also added new subclass irissjiobs. irisobs now
;             contains code common for both IRIS spectral and slit-jaw
;             observations.
;            13 Nov 2013 Terje Fredvik - added private method gt_sat_rot_txt,
;            returns a text with the sat_rot with one digit after decimal
;            point followed by a degree symbol. Is called by the _ensure_title
;            methods of irisspectobs and irissjiobs. 
;            31 Jan 2014 Terje Fredvik - new method ::_ensure_subtitle and new
;            structure tag subtitle.
;            23 Oct 2014 Terje Fredvik - ::init: unzip file if it's zipped
;                                        (code moved from irisspectobs, all of
;                                        a sudden SJI files need to be unzipped
;                                        as well in order to avoid error
;                                        messages from SSW mrd_hread(). Added 
;                                        structure tag unzippedfile. Delete
;                                        unzipped file in ::cleanup. Added
;                                        file as input parameter to 
;                                        ::create_data_object.
;
;
;-
;

FUNCTION irisobs::init, file, _ref_extra=extra
  o = self
  
  IF o->anyobs::init(file, _extra=extra) EQ 0 THEN return,0
  
  o.coltabs.intensity = 3
  o.subtitle = 'MISSING'
  o.unzippedfile = 'MISSING'
  return,1
END


PRO irisobs::cleanup
  IF self->is_set(/unzippedfile) THEN file_delete, self.unzippedfile
  self->anyobs::cleanup
  print,"Cleaning up (irisobs)"
END



;; ++++++++++++++++++++++++++++++++++++++++
;;   <overridden anyobs read_data 
;; ++++++++++++++++++++++++++++++++++++++++

PRO irisobs::read_data
  o = self
  IF ptr_valid(o.data) && obj_valid(*o.data) THEN obj_destroy,*o.data
  ptr_free,o.data,o.header
  
  file = o.file
  
  ;; IRIS files need to be unzipped before they are read. Unzip - read -
  ;; delete unzipped file.
  gzpos = strpos(file, '.gz')
  unzip = (gzpos NE -1) ? 1 : 0
  IF unzip THEN BEGIN 
     unzippedfile = strmid(file,0,gzpos)
     print,'Unzipping file...'
     get_utc, t0,/ccsds
     spawn,'gunzip -c '+file +' >'+unzippedfile
     get_utc, t1,/ccsds
     secs = anytim2tai(t1)-anytim2tai(t0)
     print,'Unzipping done after '+trim(secs)+' seconds.'
     o.unzippedfile = unzippedfile
     file = unzippedfile
  ENDIF
  
  dataobj = o->create_data_object(file) ;; irissji or irisspect
  
  
  ;; A problem: I need to get iwin from the dataobj before I set o.header,
  ;;      (since iwin is a input parameter to gethdr). I therefore need to
  ;;      pass dataobj, before it's set as o.data, as a parameter to
  ;;      lwin_read! Ouch! That's really bad! Need to work out a more elegant
  ;;      way as soon as possible, but for the time being it works...
  iwin = o->lwin_read(dataobj)
  o.iwin = iwin
  
  o->set, data=ptr_new(dataobj) 
  o->set, header=ptr_new((*(o.data)->gethdr(iwin))) 
   
END

PRO irisobs::_ensure_ytitle
  self->private
  self.ytitle = 'Instrument Y (arc sec)'
END

PRO irisobs::_ensure_subtitle
  self->private
  IF ~self->is_set(/subtitle) THEN $
     self.subtitle = 'SAT_ROT: '+ self->_gt_sat_rot_txt()
END

FUNCTION irisobs::_gt_sat_rot_txt
  self->private
  
  header = *(self->get(/header))
  
  ;; Format the sat_rot so that e.g. 0.4235 and -210.223 are printed as 0.4
  ;; and -210.2. Please tell me that there is a more elegant way of doing this
  ;; than the following:
  sat_rot_txt = strtrim(round(10*gt_fkw(header,'SAT_ROT'))/10.,2)
  dotpos = strpos(sat_rot_txt,'.')
  sat_rot = strmid(sat_rot_txt,0,dotpos+2)
  
  return, sat_rot+string(176b)
END


FUNCTION irisobs::_gt_firstdimraster
  ;; The name of the first dimension for a raster. If SAT_ROT ne 0 or 180 then
  ;; the first dimension is not Solar X! For simplicity we use 'Instrument X'
  ;; in all cases for IRIS.
  self->private
  return,'Instrument X (arc sec)'
END



;; -------------------------------------------
;;   <overridden anyobs read_data 
;; -------------------------------------------


PRO irisobs__define
  INT = 0
  STR = ''
  
  d = {irisobs, $
       inherits anyobs, $
       unzippedfile:STR,$
       iwin: INT}
END
