Viewing contents of file '../idllib/ghrs/pro/centroid_merge.pro'
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;+
;
;*NAME: CENTROID_MERGE
;
;*CLASS:
;
;*CATEGORY:
;
;*PURPOSE: Merge FP-SPLIT subexposures and multiple exposures to single vector.
;
;*CALLING SEQUENCE:
;	CENTROID_MERGE, w, f, er, MW, MF, MER, type
;
;*PARAMETERS:
;	INPUT:
;	w	- (REQ) - (2) - (I,R,L,D) - Wavelength array.
;	f	- (REQ) - (2) - (I,R,L,D) - Flux array.
;	er	- (REQ) - (2) - (I,R,L,D) - Error array.
;	type	- (REQ) - (0) - (I,L)     - -1 = determine shift values only,
;                                                exit with no change.
;                                            0 = determine shift values and
;                                                merge spectra.
;
;*SYSTEM VARIABLES USED:
;	NONE
;*COMMON BLOCKS:
;	NONE
;*INTERACTIVE INPUT:
;	YN	- (REQ) - (0) - (S) - N,n    = End procedure.
;		                      NO, no = End procedure.
;		                      0      = End procedure.
;	huh	- (REQ) - (0) - (S) - C      = Continue with procedure.
;		                      E      = Edit shifts.
;*SUBROUTINES CALLED:
;	ZCENTROID
;*FILES USED:
;	NONE
;*EXAMPLES:
;
;*NOTES:
;	This procedure is called by FP_MERGE.
;*PROCEDURE:
;	Merge FP-SPLIT subexposures and multiple exposures to a single vector.
;	Also handles background.  Shifts are determined by plotting the first
;	spectrum from which you choose a line to be fitted with a gaussian.
;	The vector position of the centroid is returned along with the
;	wavelength range of the fit.  If you say this is ok, the program
;	automatically uses this range to fit this line in all the spectra and
;	returns the relevant vector position.  These values are used to
;	determine the relative shifts between the spectra.
;
;*MODIFICATION HISTORY:
;	Ver 1.0 - 10/11/90 - Jason Cardelli
;	Ver 2.0 - 11/30/90 - Jason Cardelli - Re-designed in current form.
;               - 12/11/90 - Jim Blackwell  - Modified to conform with GHRS
;                                             DAF standards.
;	VER 4.0 - 01/09/91 - Jason Cardelli - Added call to routine VECTOR_TRIM.
;-
;-------------------------------------------------------------------------------
pro centroid_merge,w,f,er,MW,MF,MER,type
if n_params(0) eq 0 then begin
  print,' Calling Sequence:  CENTROID_MERGE, w, f, er, MW, MF, MER, type'
  retall
end else begin
  zparcheck,'CENTROID_MERGE',w,1,[2,3,4,5],2,"w not valid"
  zparcheck,'CENTROID_MERGE',f,2,[2,3,4,5],2,"f not valid"
  zparcheck,'CENTROID_MERGE',er,3,[2,3,4,5],2,"er not valid"
  zparcheck,'CENTROID_MERGE',type,7,[2,3],0,"type not valid"
end
sz=size(F)
ns=sz(2)
nu=strtrim(string(ns),2)
err=1
szerr=size(er)
;
; Error vector size..if siz=500, then it is background and the error vector
; is junk...return a message to that affect.
;
siz=szerr(1)    
if siz eq 500 then err=0 
;
if (ns eq 1) OR (sz(0) eq 1) then begin
  print,'   +++++++++++++++++++++++++++++++++++++++'
  print,'   You only have ONE spectrum, pal........'
  print,'   You are wasting valuable CPU time' 
  print,'   +++++++++++++++++++++++++++++++++++++++'
  goto,noneed
end
;
IF (ERR EQ 0) or (sz(1) eq 500) THEN BEGIN
  PRINT,' '
  yn=''
  PRINT,'   ++++++++++++++++++++++++++++++++++++++++'
  PRINT,'   WARNING:  This is BACKGROUND data.......'
  print,'   ++++++++++++++++++++++++++++++++++++++++'
  read,'   Are you sure you want to CENTROID',YN
  if (yn eq 'N') or (yn eq 'n') or (yn eq 'NO') or (yn eq 'no') or (yn eq '0') $
  then goto,noneed 
END
;
; Shift array....one value for each spectra.
;
shif=intarr(ns)
;
; Do first spectrum.
;
ZCENTROID,W(*,0),smooth(F(*,0),3),0,wl,wr,X0
shif(0)=x0(0)-x0(0)
;
vt100
print,'  Spectrum   First Wavelength   Shift'
print,' ------------------------------------------'
print,'   1 of ',nu,'       ',strtrim(string(w(0,0)),2),'          ',strtrim(string(shif(0)),2)
;
; If okay, do all the rest.
;
for I=1,ns-1 do begin
  ZCENTROID,W(*,I),smooth(F(*,I),3),I,wl,wr,XC 
  shif(i)=xc(0)-x0(0)
  II=STRTRIM(STRING(I+1),2)
  IF II LT 10 THEN print,'   ',II,' of ',nu,'       ',strtrim(string(w(0,I)),2),'         ',strtrim(string(shif(I)),2)
  IF II GE 10 THEN print,'  ',II,' of ',nu,'       ',strtrim(string(w(0,I)),2),'         ',strtrim(string(shif(I)),2)
endfor
print,' ------------------------------------------'
print,' '
;
; Check shift values and exit procedure.
;
IF TYPE EQ -1 THEN GOTO,NONEED
;
huh=''
;
redo: read,'  C)ontinue   E)dit SHIFTS',huh
  if (strupcase(huh) ne 'C') and (strupcase(huh) ne 'E') then goto,redo
  if strupcase(huh) eq 'E' then begin  ;a chance to edit the shifts
	print,' '
	PRINT,'  You can now EDIT the individual SHIFT values if you like'
	print,'  Type CTRL Z for no change'
	print,' '

	FOR I=0,NS-1 DO BEGIN
		SH=SHIF(I)
		read,' spectrum '+strtrim(string(i+1),2)+'  Shift = '+strtrim(string(shif(i)),2),sh
		SHIF(I)=SH
	ENDFOR
  end ; redo loop

; Now done computing shifts.
;
IMIN=INTARR(1)
;
; Find which spectra represents minimum shift.
;
Imin=IMIN+where(shif eq min(shif))
shif=shif-min(shif)
;
; Shift and add point-to-point.
;
If type eq 0 then begin
;
; THE OUTPUT WAVELENGTH VECTOR....FROM THE UNSHIFTED SPECTRUM
;
  MW=W(*,IMIN(0))
  MF=fltarr(sz(1))
  MER=MF
  for i=0,ns-1 do begin
    Mf(0)=Mf(0:*)+f(SHIF(I):*,i)
    if err ne 0 then Mer(0)=sqrt(Mer(0:*)^2+er(SHIF(I):*,i)^2)
  endfor
  Mf=Mf/ns
  MER=MER/NS

  TRIM=SZ(1)-MAX(SHIF)
  IF (ERR EQ 0) OR (SZ(1) EQ 500) THEN VECTOR_TRIM,TRIM,MF,0
  IF ERR NE 0 THEN VECTOR_TRIM,TRIM,MF,MER

                         
end  ;type=0
;
; Text output (to screen)
;
print,' '
print,' CENTROID_MERGE'
print,' '
IF TYPE EQ 0 THEN BEGIN
  if err ne 0 then print,' Returning Merged FLUX and ERROR vectors'
  if (err eq 0) or (sz(1) eq 500) then begin
    print,' Returning Merged BACKGROUND vector'
    print,' ERROR vector has not been modified'
  end
END
;
IF TYPE EQ 1 THEN BEGIN
  PRINT,' Returning Merged FLUX vector'
  print,' ERROR vector has not been modified'
end
;
print,' --------------------------------------------------------------------'
print,' Number of spectra merged =',ns
if type eq 0 then print,' SPECTRA shifted and coadded'
if type eq 1 then print,' SPECTRA interpolated and coadded'
print,' --------------------------------------------------------------------'
print,' '
noneed:
return ; CENTROID_MERGE
end ; CENTROID_MERGE