;+
; NAME:
;       XCAL
;
; PURPOSE:
;
; CATEGORY:
;       Hansteen/Wikstol Data analysis SW
;
; CALLING SEQUENCE:
;
; INPUTS:
;
; KEYWORD PARAMETERS:
;
; Outputs:
;       None
;
; CALLS:
;
;
; COMMON BLOCKS:
;
;
; PROCEDURE:
;
; RESTRICTIONS:
;
;
; MODIFICATION HISTORY:
;       15-June-2004: Oivind Wikstol. Version 1.0
;
; $Id: xcal.pro,v 1.7 2007/03/07 12:45:04 viggoh Exp $
;-
;
; quick and dirty dark current subtraction
pro xcal_dcqd, event
  widget_control, event.top, get_uvalue = info
  widget_control, /hourglass
  if (*info).dc_done then begin
    ok=dialog_message('Dark current subtraction already done!!',/info,/center)
    return
  endif
  eis_cal_dcql, (*info).dobj
  (*info).dc_done=1
  (*info).dobj->setcalstat,'dc',(*info).dc_done
  widget_control,(*info).dcqdbutton,sensitive=0
  widget_control,(*info).dcbutton,sensitive=0
  return
end

; dark current subtraction
pro xcal_dc, event 
  widget_control, event.top, get_uvalue = info
  dirsep = (*info).dirsep
  ;set default directory to search
  HOME=getenv('HOME')
  defdir = getenv('EIS_CAL_DATA')+dirsep+'dc'
  dcfile = dialog_pickfile(path = defdir, /read, filter='*.idlsave',$
                           title='Select Dark Current File', $
                           dialog_parent = (*info).tlb)

  ; check if filename is a valid one:
  if not file_test(dcfile,/regular) then begin
    print,'No file selected'
    return
  endif

  if dcfile ne '' then begin
    eis_cal_dc, (*info).dobj,dcfile=dcfile 
    (*info).calstat = 'Dark Current'
    (*info).savestat = 0
    ; set filename default for savefile:
    filename = (*info).dobj->getfilename()
    filename = fname_extract(filename,/remove_ext)
    (*info).filename = filename+'_dc' 
    widget_control,(*info).dcqdbutton,sensitive=0
    widget_control,(*info).dcbutton,sensitive=0
  endif  
return  
end

pro xcal_save, event
  widget_control, event.top, get_uvalue = info
  dirsep = (*info).dirsep
  home = getenv('HOME')
  filename = (*info).filename
 
       ; save as fits
       fitsdir = HOME+dirsep+'solarb'+dirsep+'eis'+dirsep+'data'+$
                 dirsep+'science'+dirsep+'fits'
       fitsfile = dialog_pickfile(path=fitsdir, /write, file = filename, $
                                  title='Select FITS file name', $
 ;                                 default_extension='fits', $
                                  dialog_parent = (*info).tlb)

      ; return without saving if no file was selected 
      if fitsfile eq '' then return
  
       datasource = (*info).dobj-> getdatasource()
       ; if datasource was telemetry (CCSDS) file, make new fits file
       case datasource of 'ccsds': begin
           hdr = (*info).dobj-> gethdr()
           eis_mkfits, (*info).dobj, hdr, $
             fitsfile = fitsfile, calstat = (*info).calstat
       end
       ; if datasource was fits, then modify
       ; fits file with (possibly) new filename
       'fits': begin
         hdr = (*info).dobj-> gethdr()
         main_hdr = hdr->getmain_hdr()
         bte_hdr = hdr-> getbte_hdr()
         fxaddpar, main_hdr, 'CALSTAT', (*info).calstat, $
                   'Calibration status', after = 'CMIRR'
         ; max and min values in line windows have changed.
         ; TDMIN and TDMAX must be set again
         nwin = (*info).dobj-> getnwin()
         for iwin=0, nwin-1 do begin
           (*info).dobj-> getwin, iwin, wd, pos
           wdmin = min(wd)  ; minimum value in line window       
           wdmax = max(wd)  ; maximum value in line window
           param1 = 'TDMIN'+strtrim(string(iwin+1), 2)
           param2 = 'TDMAX'+strtrim(string(iwin+1), 2) 
           fxaddpar, bte_hdr, param1, wdmin
           fxaddpar, bte_hdr, param2, wdmax
         endfor
         hdr-> setmain_hdr, main_hdr
         hdr-> setbte_hdr, bte_hdr
         eis_modfits, (*info).dobj, hdr, fitsfile = fitsfile
       end
       else: begin
         msg = 'Can only save from fits or ccsds data formats'
         ok = dialog_message(msg, /information, dialog_parent = (*info).tlb)
       end
   endcase
  (*info).savestat = 1
  return
end

; close xcal
pro xcal_destroy, event
  widget_control, event.top, get_uvalue = info
  if not (*info).savestat then begin
    answer = dialog_message('Do you want to save calibrated data?', /question)
    case answer of
      'Yes': begin
        pseudoevent={widget_button,id:0L, $
                     top:event.top, handler:0l, select:1}
          xcal_save, pseudoevent
          widget_control, event.top,/destroy
       end
       'No': begin
         widget_control, event.top,/destroy
       end
    endcase
  endif else begin
    widget_control, event.top,/destroy
  endelse

end



pro xcal_cleanup, tlb
  widget_control, tlb, get_uvalue = info
  ptr_free, info
end

pro xcal, dobj, group_leader = group

  if n_params() lt 1 then begin
    message, $
      'xcal: No data object given as input', $
       /cont
    return
  endif


  dirsep = get_delim()
  tlb = widget_base(/row, title='xcal: EIS Calibration control window', mbar=menubar, $
                    xoffset = 100,yoffset = 100)
  lcol = widget_base(tlb, /frame, /column)      ;left column.

  ; create pulldown menus on the base widget menubar
  filemenu=widget_button(menubar, value='File',/menu, uvalue='file')
  savemenu=widget_button(filemenu, value='Save calibrated data', event_pro = 'xcal_save')
  exitmenu=widget_button(filemenu, value='Close', event_pro='xcal_destroy')

  ; calibration buttons - 1) Quick and Dirty Dark Current
  dcqdfield = widget_base(lcol, /col, /frame)
  dcqdtitle = widget_label(dcqdfield, value = 'QnD Dark Current Sub.   ')  
  dcqdbutton = widget_button(dcqdfield,  value = 'QnD Go', $
                            event_pro = 'xcal_dcqd')
  
  ; calibration buttons - 1) Dark Current
  dcfield = widget_base(lcol, /col, /frame)
  dctitle = widget_label(dcfield,     value = 'Dark Current Subtraction')  
  dcbutton = widget_button(dcfield,  value = 'Go', $
                            event_pro = 'xcal_dc')
  dcsave = widget_button(dcfield, value = 'Save', event_pro = 'xcal_save')

  closefield = widget_base(lcol, /column)
  closebutton = widget_button(closefield, value = 'Close', $
                              event_pro = 'xcal_destroy')

  ; realize main window:
  widget_control, tlb, /realize, tlb_get_size = tlb_sz

  ; define the info structure, used send information around
  info = {dobj:dobj, $
          tlb:tlb, $
          dc_done:0, $
          calstat:'None', $
          dirsep:dirsep, $
          filename:'', $
          dcqdbutton:dcqdbutton,$
          dcbutton:dcbutton,$
          savestat:1}
  info = ptr_new(info, /no_copy)

  ; set user value of tlb widget to be the info ptr
  widget_control, tlb, set_uvalue = info
  
  ; check if we have done any calibration before
  (*info).dc_done=(dobj->getcalstat()).dc
  if (*info).dc_done then begin
    widget_control,(*info).dcqdbutton,sensitive=0
    widget_control,(*info).dcbutton,sensitive=0
  endif

  xmanager, 'xcal', tlb, /no_block, group_leader = group, cleanup = 'xcal_cleanup'

end
