Viewing contents of file '../idllib/iuedac/iuelib/pro/crsmax.pro'
;*******************************************************************************
;+
;*NAME:
;
;    CRSMAX (IUE Production Library) (01 April 1988)
;
;*CLASS:
;
;    cross-correlation
;
;*CATEGORY:
;
;*PURPOSE:
;
;    DETERMINE THE MAXIMUM OF THE CROSS CORRELATION FUNCTION
;
;*CALLING SEQUENCE:
;
;    CRSMAX,CROSS,VSPACE,INP,DELV,SIGY
; 
;*PARAMETERS:
;
;   CROSS    (REQ) (I) (1)  (F)
;            Required input vector giving the cross-correlation function 
;            to be fit.
;
;   VSPACE   (REQ) (I) (1)  (F)
;            Required input vector specifying the velocity spacing for
;            the cross-correlation function.
;   
;   INP      (REQ) (I) (0)  (I)
;            Required input scalar specifying the number of Gaussian
;            components to be fit to the cross-correlation function.
;            If this parameter is zero, the cross-correlation function
;            will be fit using a least squares fit to the first difference
;            function, and the point of zero slope identified. 
;
;   DELV     (REQ) (O) (0)  (F)
;            Velocity difference (in km/s) of the two spectra as
;            determined from the maximum of the cross-correlation function.
;            This quantity is determined either by the centroid of the 
;            fitted gaussian, or via the first difference technique. 
;
;   SIGY     (REQ) (O) (0)  (F)
;            Required output variable giving the rms deviation
;            of the gaussian fit from the cross-correlation function.
;            This variable will be non-zero if and only if a gaussian
;            fit to the cross-correlation function is requested.
;
;   XPOS     (REQ) (IO) (0) (F)
;            x position to start writing output in device units.
;            
;   YPOS     (REQ) (IO) (0) (F)
;            y position to start writing output. Returned value
;            designates position of last line of output.
;
;   LIN      (KEY) (I) 
;            if set, assumes linear wavelengths
;
;*SYSTEM VARIABLES USED:
;
;	!d.y_ch_size
;
;*INTERACTIVE INPUT:
;
;*SUBROUTINES CALLED:
;
;      GAUSSFITS
;      LINFIT
;      PARCHECK
;
;*FILES USED:
;
;*SIDE EFFECTS:
;
;*RESTRICTIONS:
;
;*NOTES:
;
;     See CRSCOR
;
;*PROCEDURE:
;
;     The normalized cross-correlation function can be fit either
;     with one or more gaussian profiles, or if the function has a single
;     well-defined maximum, via a least squares fit to the first differences,
;     and interpolating to the point of zero slope. 
;
;*EXAMPLES:
;
;     To fit a normalized cross-correlation function with one Gaussian,
;     CRSMAX,CROSS,VSPACE,1,DELV,SIGY
;
;     To fit the same cross-correlation function by finding the local
;     extremum,
;     CRSMAX,CROSS,VSPACE,0,DELV,DUMMY
;   
;*MODIFICATION HISTORY:
;
;     for earlier history, see CRSCOR
;     05  May 1988   CAG     GSFC   add VAX RDAF-style prolog, PARCHECK
;     8/25/89 RWT GSFC Unix mods: add xpos & ypos parameters, remove !c,
;         use xyouts instead of print,
;     25  Jun 1991   PJL  GSFC  cleaned up; added 0 parameters print;
;				tested on SUN and VAX; updated prolog
;     23  Feb 1993   LLT  GSFC  Print vel. shift below plot, instead of upon it.
;     7/15/93 rwt  add /LIN keyword for linear wavelengths 
;    10/25/93 rwt  shift final line of text to avoid overlap with plot
;                  annotation in x-windows
;-
;*******************************************************************************
 pro crsmax,cross,vspace,inp,delv,sigy,xpos,ypos,lin=lin
;
 npar = n_params(0)
 if npar eq 0 then begin
    print,' CRSMAX,CROSS,VSPACE,INP,DELV,SIGY,XPOS,YPOS,lin=lin'
    retall
 endif  ; npar
 parcheck,npar,7,'CRSMAX'
;
; check for linear of logarithmic scale (using vspace)
; 
if keyword_set(lin) eq 0 then ws = 1 else ws = 0
;
; determine the maximum of the cross correlation function
;
 sigy = 0.0
 dely = 1.1 * !d.y_ch_size
 if (inp gt 0) then begin     ; gaussian fit
    gaussfits,float(vspace),cross,0,inp,a,yfit,sig
    delv= a(0)
    sigy=sqrt(total((cross-yfit)*(cross-yfit))/(inp*3+1))
 endif else begin             ; max by first differences
    ntot = n_elements(cross)
    j=max(cross)
    ind = where(cross eq j)
    kb=ind(0)-3>0 & ke=ind(0)+3<(ntot-1)
    if (ws) then xyouts,xpos,ypos,font=0,/device,'     km/s      function' $
    else xyouts,xpos,ypos,font=0,/device,'      tag     function' 
    f = '(2x,f9.4,2x,f9.4)'
    for k=kb,ke do begin   
       ypos = ypos - dely
       xyouts,xpos,ypos,font=0,/device,string(format=f,vspace(k),cross(k))
    endfor  ; k
    temp = cross - shift(cross,-1)
    diff = temp(kb:ke-2)
    temp = (vspace + shift(vspace,-1))/2.
    vsub = temp(kb:ke-2)
    nt = n_elements(diff)
    wt = fltarr(nt)
    xmid = float(nt)/2.0 - 0.5
    wt = 1. + 2*(xmid - abs(xmid-indgen(nt)))     ;calculate weightt 
    linfit,vsub,diff,wt,a,b,si                    ; use least squares fit
    delv = -a/b
;    ypos = ypos - 2*dely
    ypos=.1*!d.y_size-2.0*dely
    if (ws) then begin
      stf = '(1x,a,f7.1," km/s for file1 rel. to file2")'
      st = string(format=stf,'Resulting velocity shift is',delv) 
    endif else begin
      stf = '(1x,a,f6.2," angstroms for file 1 relative to file 2")'
      st = string(format=stf,'Resulting shift is',delv) 
    endelse  
    xyouts,xpos,ypos,font=0,/device,st
 endelse  ; max by first differences
 return
 end  ; crsmax