Viewing contents of file '../idllib/iuedac/iuelib/pro/dccor.pro'
;******************************************************************************
;+
;*NAME:
;
;       DCCOR
;
;*CLASS:
;
;       Data correction, reprocessing, and calibration.
;
;*CATEGORY:
;
;	IUESIPS
;
;*PURPOSE:
;
;       Corrects wavelengths for low or high dispersion LWR and SWP images
;       taken after 1984 which were processed with dispersion constants
;       originally implemented in IUESIPS in either 1984 or 1988.
;
;*CALLING SEQUENCE:
;
;       DCCOR,H,DELTAW,wave
;
;*PARAMETERS: 
;
;       H       (REQ) (I) (1) (I)
;		IUE header record.
;
;       DELTAW  (REQ) (O) (01) (R D)
;		Correction factor (in angstroms) to be SUBTRACTED from old
;		wavelength array.
;
;       WAVE    (OPT) (I/O) (1) (R)
;		Optional wavelength array with correction included
;		(i.e. WAVE = WAVE - DELTAW).  Required parameter for high
;		dispersion.
;
;*EXAMPLES:
;
;       To correct wavelengths for low dispersion SWP 32100.
;
;            iuespec,'swp32100l',h,w,f,e
;            dccor,h,deltaw,w
;
;       To correct wavelengths for high dispersion LWR 18399.
;
;            iuespec,'lwr18399h',h,w,f,e
;            wn = w             ; to save old wavelength array
;            dccor,h,deltaw,wn
;
;*SYSTEM VARIABLES USED:
;
;	none
;
;*INTERACTIVE INPUT:
;
;	none
;
;*SUBROUTINES CALLED:
;
;       PARCHECK
;
;*FILES USED:
;
;	none
;
;*SIDE EFFECTS:
;
;*RESTRICTIONS:
;
;       Correction is only valid for SWP and LWR low or high dispersion
;       images obtained and processed after June 20, 1984 at GSFC and
;       September 14, 1988 at Vilspa.
;
;	Only for use on IUESIPS data.
;
;*NOTES:
;
;       - Correction is only appropriate for SWP and LWR low or high dispersion
;       images obtained between the dates given above.  Suitability of data
;       for correction is determined by checking the value of the A2 dispersion 
;	constant, thus making this determination independent of processing
;	station.
;
;       - Corrections are based are data obtained through August, 1991. 
;       Extrapolating for images obtained much beyond this date 
;       (i.e. > a couple of years(?)) may be inaccurate.
;
;       - The greatest improvements in wavelength assignements are seen 
;       in SWP data obtained in 1987 and after 1991. For mid-1991, the 
;       SWP high dispersion correction is ~ 10 km/sec.
;
;       - No significant time dependence has been observed for the LWP 
;       camera and therefore it is not subject to the errors described above.
;
;       - To recover the original wavelength array, add DELTAW to the
;       corrected wavelength array.
;
;       - See articles in IUE Newsletter #35 and #46.
;
;*PROCEDURE:
;
;       Software was written to calculate the difference between the low or
;	high dispersion dispersion constants implemented in 1984 and 1988 with
;	those described in Newletter #46 as a function of time.  Average THDA
;	and wavelength values were used but analysis showed that the correction
;	varied by less than 0.2 angstroms for the entire range of THDA and
;	wavelengths in low dispersion, while high dispersion data indicated a
;	correction variation of around 0.5 km/sec (i.e. less than one
;	resolution element in either case).  Line and sample differences were
;	calculated and then converted to differences along and perpendicular
;	to the dispersion.  The average velocity shift is printed for high
;	dispersion data.  The coefficients used below were calculated using
;	WPOLYFIT to fit a 2nd order polynomial to the resulting arrays of
;	differences along the dispersion versus time. 
;
;       DCCOR simply extracts the observation time from record 0 and 
;       calculates the wavelength shift (to be subtracted from the wavelength 
;       array in question) using the appropriate coefficients.  The camera
;       and dispersion are both determined from the header record as well.
;       A flag is set in Record 0 [h(579)] to indicate that the image was
;       used to derive a DELTAW correction factor [h(579)=1] or that 
;       the correction was actually applied to the wavelength vector [h(579)=2].
;
;*I_HELP nn:
;
;*MODIFICATION HISTORY:
;
;    writen by R. Thompson 6-29-88
;    10-7-88 GS correct error in date calculation and add double precision
;    5-22-89 RWT make DELTAW real (not double precision)
;    7-10-90 GS  use dispersion constants to check for appropriate data
;    9-5-90  GS  use flag in header to indicate that correction was applied
;    11-8-90 RWT add dispersion constant test for LWR spectra, remove
;            test for high dispersion (unnecessary), and set h(579)=1
;            only if a valid image is specified.
;    7-7-92  MPG add second correction for post 1988 data
;    7-21-92 RWT update prolog
;    7-27-92 MPG add correction for high dispersion data
;    8-04-92 PJL corrected h(505) case statements to allow for "no valid case"
;    4-06-93 MPG update corections using latest dispersion constants
;    8-23-93 RWT reword correction messages
;	22 Nov 93  PJL  update documentation;  check header
;
;-
;******************************************************************************
 pro dccor,h,deltaw,wave
;
 npar = n_params(0)
 if (npar eq 0) then begin
    print,'DCCOR,H,DELTAW,wave'
    retall
 endif  ; npar eq 0
;
;  check header format
;
 sizeh = size(h)
 if (sizeh(sizeh(0)+1) eq 7) then begin
    print,'HEADER vector is type sting.'
    print,'Incorrect data type.'
    print,'ACTION:  retall'
    retall
 endif  ; sizeh(0)+1 eq 7
;
;  set flag for inappropriate images
;
 flag = 1    
 cam = h(3)
;
;  Check second group of 4 digits in A2 coefficient of dispersion constants
;  for valid time correction and set FLAG to: 
;     0 for 1984 to 1988 data in low disprsion
;    -2 for 1984 to 1988 data in high dispersion
;                     or
;    -1 for post 1988 data in low dispersion
;    -3 for post 1988 data in high dispersion
;
 if (cam eq 2) then begin               ; valid LWR data 
    case h(505) of
       8405: flag = 0
       8838: flag = -1
       2629: flag = -2
       5443: flag = -3
       else: flag = 1
    endcase   ; h(505)
 endif   ; cam eq 2
;
 if (cam eq 3) then begin               ; valid SWP data 
    case h(505) of
       -7476: flag = 0
       -796: flag = -1
       -4412: flag = -2
       -6797: flag = -3
       else: flag = 1
    endcase   ; h(505)
 endif   ; cam eq 3
;
;  abort procedure if high dispersion and wavelength array not input
;
 if ((flag le -2) and (npar ne 3)) then begin
    print,'Three parameters needed to correct high dispersion data'
    print,'MUST input wavelength array'
    retall
 endif    ; flag
;
 if (h(579) ne 0) then flag = 2       ;  image already checked for correction -
                                      ;  avoid applying correction twice
;
; use observing date to determine correction
;
 if (flag lt 1) then begin
    case flag of
       0: begin                       ; coefficients for 1984 to 1988 low disp.
             a0 = [0.d0,0.d0,0.65489649d0,-1.13172933d-02]
             a1 = [0.d0,0.d0,0.53368848d0,-0.37215895d0]
             a2 = [0.d0,0.d0,8.35506878d-02,-8.46529077d-02]
             dt = [0.d0,0.d0,1984.18d0,1984.19d0]
          end   ; flag = 0
      -1: begin                      ; coefficients for post 1988 low disp.
              a0 = [0.d0,0.d0,-0.31895317d0,-0.20879857d0]
              a1 = [0.d0,0.d0,-0.14980820d0,-0.14093342d0]
              a2 = [0.d0,0.d0,-1.39242804d-02,-1.49121335d-02]
              dt = [0.d0,0.d0,1987.81d0,1987.61d0]
          end   ; flag = -1
      -2: begin                       ; coefficients for 1984 to 1988 high disp.
             a0 = [0.d0,0.d0,1.1033295d0,0.28708237d0]
             a1 = [0.d0,0.d0,1.0220620d0,0.56843728d0]
             a2 = [0.d0,0.d0,0.21553931d0,0.20319489d0]
             dt = [0.d0,0.d0,1984.18d0,1984.19d0]
          end   ; flag = -2
      -3: begin                      ; coefficients for post 1988 high disp.
             a0 = [0.d0,0.d0,1.2885372d0,2.5635405d0]
             a1 = [0.d0,0.d0,0.49131019d0,1.4567019d0]
             a2 = [0.d0,0.d0,4.976780d-02,0.16238042d0]
             dt = [0.d0,0.d0,1987.81d0,1987.61d0]
          end   ; flag = -3
    endcase   ; flag
    obsdate = (1900.d0 + 1.0*h(6) + (h(8)/24.0d0 + h(7))/365.0d0) - dt(cam)
    h(579) = 1  ; set flag indicating that image has been checked by program
                ; for wavelength scale correction
    deltaw = a0(cam) + a1(cam)*obsdate + a2(cam)*obsdate*obsdate
    if (flag le -2) then begin        ; for high dispersion data
       c = 2.99792458d05              ; velocity of light in km/seC
       fmt = "$(1x,'Velocity Shift = ',f5.1,' km/sec')"
       print,' '
       print,fmt,deltaw
       deltaw = deltaw * wave / c     ; compute delta w for each wavelength
    endif  ; flag le -2
    deltaw = float(deltaw)
    if (npar eq 3) then begin
       wave = wave - deltaw
       h(579) = 2                     ; correction applied
    endif  ; npar eq 3
 endif else begin
    deltaw = -9999
    if (flag eq 1) then print,'Image NOT appropriate for wavelength correction'
    if (flag eq 2) then begin
       print,'Flag indicates wavelength correction already applied'
       print,'(reset h(579) = 0 to re-apply correction).'
    endif  ; flag eq 2
 endelse ; flag
;
 return
 end  ; dccor