;+
; Project     : SOHO - CDS     
;                   
; Name        : LOAD_WAVECAL()
;               
; Purpose     : Loads wavecal into common block for use by pix2wave etc
;               
; Explanation : In order to save PIX2WAVE and WAVE2PIX having to access the
;               wavecal database on every call, they assume the coefficients
;               are loaded in common block CDS_WAVECAL.  The coefficients are
;               loaded into the common block by this routine.
;               
; Use         : IDL> status = load_wavecal(detector, utc, $
;                                 [gset_id=gset_id, cal_struct=cal_struct])
;    
; Inputs      : detector  - either 'NIS' or 'GIS' (can be abbreviated)
;               utc       - relevant time for calibration
;              
;               
; Opt. Inputs : cal_struct - calibration structure as kept in the QLDS and
;                            as returned by the get_wavecal and save_wavecal
;                            routines. If this is present, no other parameters
;                            are needed.
;               
; Outputs     : None
;               
; Opt. Outputs: None
;               
; Keywords    : GSET_ID - for GIS calibration wavecals are referenced by 
;                         GSET_ID (and then by date)
;
;               ERRMSG -  if defined on entry will be used to return error
;                         messages.
;
; Calls       : GET_WAVECAL
;
; Common      : CDS_WAVECAL
;               
; Restrictions: None
;               
; Side effects: None
;               
; Category    : Data analysis, wavelength calibration
;               
; Prev. Hist. : None
;
; Written     : C D Pike, RAL, 11-Mar-96
;               
; Modified    : 
;
; Version     : Version 1, 11-Mar-96
;-            

function load_wavecal, detector, utc, gset_id=gset_id, $
                       cal_struct=cal_struct, errmsg=errmsg

common cds_wavecal, ncoff, gcoff

;
;  which form of input?
;
if keyword_set(cal_struct) then only_struct=1 else only_struct=0

;
;  check input
;
if n_params() eq 0 then begin
   print,'Use:  IDL> stat = load_wavecal( detector, utc [,gset_id=gset_id])'
   print,' or   IDL> stat = load_wavecal( detector, cal_struct=cal_struct)'
   return,0
endif

;
;  check detector and gset_id if appropriate
;
det = strupcase(strmid(detector,0,1))
if not only_struct then begin
   if det eq 'G' and (n_elements(gset_id) eq 0) then begin
      message = 'Must specify a GSET_ID for GIS calibration'
      goto, handle_error
   endif
endif else begin
   if datatype(cal_struct) eq 'STC' then begin
      if tag_exist(cal_struct,'GSET_ID') and (det eq 'N') or $
         (not tag_exist(cal_struct,'GSET_ID') and (det eq 'G')) then begin
         message = 'Mismatch between detector and calibration structure'
         goto, handle_error
      endif
   endif
endelse

;
;  default to NOW
;
if n_elements(utc) eq 0 then get_utc,utc

;
;  Retrieve and load wavecals
;
case det of
   'N': begin
           if only_struct then begin
              ncoff = cal_struct
           endif else begin
              errmsg1 = ''
              get_wavecal, det, utc, wcal, errmsg = errmsg1
              if errmsg1 ne '' then begin
                 message = errmsg1
                 goto, handle_error
              endif else begin
                 ncoff = wcal
              endelse
           endelse
        end
   'G': begin
           if only_struct then begin
              gcoff = cal_struct
           endif else begin
              errmsg1 = ''
              get_wavecal, det, utc, wcal, gset_id=gset_id, errmsg = errmsg1
              if errmsg1 ne '' then begin
                 message = errmsg1
                 goto, handle_error
              endif else begin
                 gcoff = wcal
              endelse
           endelse
        end
  else:
endcase
return,1

;
;  Error handling
;
handle_error:
if n_elements(errmsg) ne 0 then begin
   errmsg = 'LOAD_WAVECAl: '+message
endif else begin
   message, message, /continue
endelse
return, 0

end
