;+
; NAME: hinobs__define
;
;
;
; PURPOSE: Definiton of the Hinode observation superclass; This class contains
;          Hinode mission specific code only, all code that is common for all
;          missions and instruments can be found in the superclass
;          anyobs. Hinode instrument specific code can be found in the
;          (sub)subclasses eisobs, sotobs, sot1dobs, sotl2obs and xrtobs, and
;          you will work with objects of these subclasses.
;
;                                           (sdc_util)
;                                              |
;                                            anyobs
;                                          /       \
;                                    hinobs         irisobs
;                                  /   |   \ 
;                            xrtobs  sotobs eisobs
;                                     /  \
;                              sp1dobs    spl2obs
;
;
;
; METHODS: This class now only contains one public method (mk_jpg2000), all
;          other public classes have been moved to anyobs.
;
;          o->mk_jpg2000
;                Produce Helioviewer compatible jpg2000 image
;             Keywords: outdir (optional), where is the jpg2000 image to be stored
;
; SPECIAL CALLS: anyobs__define.pro, prune_filename.pro,
;                gt_closest_index.pro, gt_fkw.pro
;
;
; WRITTEN: Terje Fredvik, UiO, December 2007
;
; MODIFICATION HISTORY:
;          24. Aug 2010 Terje Fredvik: removed a few miles of modification
;                                      history.
;          24. Aug 2010 Terje Fredvik: removed destdir as input parameter, now
;                                      keyword instead.
;          07. Sep 2010 Terje Fredvik: Use ::extract_header() instead of 
;                                      get(/header). 
;          20. Sep 2010 Terje Fredvik: Added keyword AR to ::plot_image. If
;                                      set, plot NOAA AR numbers on image.
;          29. Oct 2010 Terje Fredvik: Call SSW routine eis_colors.pro to load 
;                                      velocity and width color tables
;          01. Feb 2011 Terje Fredvik: Removed code for storing CVS revision,
;                                      added code for SVN revision.
;          08. Dec 2011 Terje Fredvik: Added method extract_directory()
;          14. Dec 2011 Terje Fredvik: Added method mk_jpg2000 - create
;                                      Helioviewer compatible jpg2000
;                                      images. Added keyword STRUCTURE to
;                                      ::extract_header: if set the returned
;                                       header is a structure.
;          05. Mar 2012 Terje Fredvik: Added header structure tag dsun_obs when
;                                      calling ::mk_jpg2000
;          29. Mar 2012 Terje Fredvik  ::_fit_device - min size of image is now
;                                      512 x 512 pixels. ::plot_image -  add
;                                      the size of the displayed data cube (x
;                                      pixels times y pixels to the image
;                                      title.
;          25. Jul 2012 Terje Fredvik: # Added keyword fit_limb to plot_image -
;                                      if set to 1 use new xycencorrection
;                                      structure tags to correct x/ycen, if
;                                      not present try to download corrections
;                                      from Oslo SDC archive, if not present
;                                      there either try to calculate
;                                      corrections by fitting the limb. If set
;                                      to 2, force limb fitting to find
;                                      calculation of pointing corrections. 
;                                      Lots of changes to implement the limb 
;                                      fitting, e.g. new methods:: 
;                                      extract_xycencorrection,
;                                      ::get_and_set_xycencorrections and
;                                      ::get_pointing_no. 
;                                      # ::mk_jpg2000 - find rsun and add to the
;                                      header that is sent to
;                                      ::_mk_jpg2000_specific.         
;           20. Sep 2012 Terje Fredvik: ::mk_jpg2000: Include pointing
;                                       correction in the output header as
;                                       xcen_corr and ycen_corr. The xcen and
;                                       ycen values are not touched.
;           09. Apr 2013 Terje Fredvik: Moved most of the code to new
;                                       superclass anyobs, keeping Hinode
;                                       specific code only. Hinobs and any new
;                                       mission specific classes
;                                       (e.g. irisobs, with instrument
;                                       specific sub-classes) will be
;                                       subclasses of anyobs.
;           05. May 2015 Terje Fredvik: ::get_pointing_no: Do not order search 
;                                       results in order to prevent
;                                       code from crashing when run on SDC
;                                       backup server: osdc->order,''
;                                                                      
;                                      
;-

FUNCTION hinobs::init, file, destdir=destdir,_ref_extra=extra;, maxplotsz=maxplotsz, noprep=noprep
  o = self
  IF o->anyobs::init(file, destdir=destdir, _extra=extra) EQ 0 THEN return,0

  o.version = 'SVN revision '+sdc_revision()
  
  not_set =  -1

  o.coltabs.intensity = 3
  o.coltabs.stokesquv = 0
  o.coltabs.rufaz = 5
  
  o.pointing_no = not_set
  o.ver_rf0 = 'MISSING'
  
  return,1
END


PRO hinobs::cleanup

  self->anyobs::cleanup
  
  print,"Cleaning up (hinobs)"
END


PRO hinobs::_ensure_mission_specific_properties
  self->private
  ;; Used to have _enusre_pointing_no as well, but pointing_no should't be set
  ;; unless asked for, it takes too much time since we need to get this
  ;; variable from the Oslo Archive.
  self->_ensure_ver_rf0 
END

PRO hinobs::_ensure_ver_rf0
  self->private
  IF ~self->is_set(/ver_rf0) THEN self.ver_rf0 = self->hget(/ver_rf0)
END

     
;; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;; get_XXX functions for all (Hinode) structure tags that are set based on the
;; fits header. Each funciton checks if header is a valid pointer, if not,
;; call read_header which again ensures the tag is set.
;; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

FUNCTION hinobs::get_ver_rf0
  self->private
  IF ~ptr_valid(self.header) THEN self->read_header
  return,self.ver_rf0 
END


;; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;; get_XXX functions for all structure tags that are set based on Oslo
;; archive information. 
;; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

FUNCTION hinobs::get_pointing_no
  self->private
  IF self.pointing_no EQ -1 THEN BEGIN 
     print,'...finding current pointing_no'
     osdc=obj_new('osdc')
     osdc->show,'FILE,POINTING_NO'
     osdc->condition,'anon: y'
     osdc->condition,'FILE: '+prune_filename(self->get(/file))
     osdc->order,''
     osdc->search, out
     pointing_no =  out.pointing_no
     obj_destroy,osdc
     
     self->set, pointing_no=pointing_no
  ENDIF
  return,self.pointing_no 
END



PRO hinobs::mk_jpg2000, outdir=outdir
  ;; Produces a Helioviewer compatible jpg2000 image
  o = self
  
  ;; compile routines needed for Helioviewer jpg2000 image production
  resolve_routine,['hv_hin_fg2jp2', 'hv_hin_xrt2jp2'],/compile_full_file, /either, /no_recompile
  
  ;; Get input for the instrument specific method
  im = o->extract_image()
  header = o->extract_header(/structure)
  dir = o->extract_directory()
  file = o->get(/file)
  
  ;; The jp2gen routines need the distance from the observer to the sun as a 
  ;; structure tag of the header. First get distance in AU:
  dummy = get_sun(gt_fkw(header,'DATE_OBS'), dist = dsun_obs, sd=rsun_arcsec) 
  dsun_obs = dsun_obs*149597870700 ;; convert disteance in AU to meters
  
  ;; We also need the radius of the sun in pixels
  rsun = rsun_arcsec/gt_fkw(header, 'PLATESCL')
  
  ;; Should the X/YCEN be corrected?
  fit_limb = (ptr_valid(o.xycen_delta)) ? 1 : 2
  xycen_delta = o->extract_xycen_delta(fit_limb=fit_limb)
  
  ;; Adjust the X/YCEN values in the jpg2000 header
  header.xcen += xycen_delta[0]
  header.ycen += xycen_delta[1]
  
  ;; Put the extra info into the header
  header = create_struct(header, $
                         'DSUN_OBS', dsun_obs, $
                         'RSUN', rsun, $
                         'OSLO_XCEN_DELTA', xycen_delta[0], $
                         'OSLO_YCEN_DELTA', xycen_delta[1])
  
  ;; Call the instrument specific method
  o->_mk_jpg2000_specific, im, header, dir, file, outdir = outdir
END



PRO hinobs__define
  INT = 0
  STR = ''
  
  d = {hinobs, $
       inherits anyobs , $
       pointing_no: INT, $
       ver_rf0: STR}
END
