pro write_summaries2,img,hdr
;+
; NAME:				WRITE_SUMMARIES
;
; PURPOSE:			Create FITS and GIF image of the summary image 
;
; CATEGORY:			REDUCTION
;
; CALLING SEQUENCE:		WRITE_SUMMARIES,Img,Hdr
;
; INPUTS:			Img = Input Image array
;				Hdr = FITS header
;
; OPTIONAL INPUTS:		None
;	
; OUTPUTS:			FITS and GIF files are written
;
; RESTRICTIONS:			This must be done with IDL having an X window 
;				display.
;
; PROCEDURE:			Scales the image to not larger than 512 x 512,
;				and creates a FITS image with LASCO logo in
;				image Derived from WRITE_LAST_IMG
;
; EXAMPLE:
;
; MODIFICATION HISTORY:		Written, RA Howard, NRL
;    VERSION 1   rah    12 Apr 1996
;    VERSION 2   rah    15 Apr 1996
;    VERSION 2.1 SPP    28 May 1996 (Replaced DATE-OBS and TIME-OBS
;				     keywords with DATE_OBS and TIME_OBS).
;
;
;	@(#)write_summaries2.pro	1.1 10/05/96 LASCO IDL LIBRARY
;-
;
;   If not full image then insert into proper place in a 
;   full & square image
;
common lastimage,full_img
sumrow = fxpar(hdr,'SUMROW')
sumcol = fxpar(hdr,'SUMCOL')
lebxsum = fxpar(hdr,'LEBXSUM')
lebysum = fxpar(hdr,'LEBYSUM')
if (sumrow eq 0) then sumrow=1
if (sumcol eq 0) then sumcol=1
if (lebxsum eq 0) then lebxsum=1
if (lebysum eq 0) then lebysum=1
nxsum = sumcol*lebxsum
nysum = sumrow*lebysum
naxis1 = fxpar(hdr,'NAXIS1')
naxis2 = fxpar(hdr,'NAXIS2')
nx = nxsum*naxis1
ny = nysum*naxis2
telescope = fxpar (hdr,'TELESCOP')
detector = strtrim(fxpar(hdr,'DETECTOR'),2)
if ((nx ne 1024) or (ny ne 1024)) then begin
   r1col = fxpar(hdr,'R1COL')
   r1row = fxpar(hdr,'R1ROW')
   if (r1col lt 1) then r1col = fxpar(hdr,'P1COL')
   if (r1row lt 1) then r1row = fxpar(hdr,'P1ROW')
   FULL_img = intarr(1024/nxsum,1024/nysum)
   sz=size(img)
   nx=sz(1)<1024
   ny=sz(2)<1024
   naxis1 = 1024
   naxis2 = 1024
   if (r1col lt 20) then begin
      full_img((r1col-1)/nxsum,(r1row-1)/nysum) = img(0:nx-1,0:ny-1)
   endif else begin
      full_img((r1col-20)/nxsum,(r1row-1)/nysum) = img
   endelse
endif else full_img=img
IF (naxis1 NE 512)  THEN full_img=CONGRID(full_img,512,512)
CASE detector OF
'C1':  full_img=bytscl(full_img)
'C2':  full_img=bytscl(sqrt(full_img),min=sqrt(500),max=sqrt(8000))
'C3':  full_img=bytscl(sqrt(full_img),min=sqrt(400),max=sqrt(16000))
DEFAULT: RETURN
ENDCASE
;
;  Add in the LASCO logo
;
restore,'~/idl/lasco_logo.sav'
tmp = full_img(384:*,0:127)		; order=0
w_logo = where(logo ne 0)
tmp(w_logo)=255
full_img(384,0) = tmp			; order=0
date_obs = fxpar (hdr,'DATE-OBS')
time_obs = fxpar (hdr,'TIME-OBS')
date = strmid(date_obs,2,2)
months='JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC'
date = date + strmid(months,3*(strmid(date_obs,5,2)-1),3)
date = date + strmid(date_obs,8,2)
fname = 'slas_'+strlowcase(detector)+'wlc_fd_'+strmid(date_obs,0,4)
fname = fname+strmid(date_obs,5,2)+strmid(date_obs,8,2)+'_'
fname = fname+strmid(time_obs,0,2)+strmid(time_obs,3,2)
;fitsname = getenv_slash('SUMMARY')+'lasco/'+fname+'.fts'
fitsname = fname+'.fts'
hd = hdr
sz = size(full_img)
FXADDPAR,hd,'NAXIS1',sz(1)
FXADDPAR,hd,'NAXIS2',sz(2)

; Added code to replace DATE-OBS and TIME-OBS keywords in header, SPP
sxdelpar, hd, 'DATE-OBS'
sxdelpar, hd, 'TIME-OBS'
sxaddpar, hd, 'DATE_OBS', date_obs, after = 'DATE'
sxaddpar, hd, 'TIME_OBS', time_obs, after = 'DATE_OBS'
; End of addition, SPP

cd,'~/',current=old
writefits,fitsname,full_img,hd
cd,old
;IF KEYWORD_SET(sidebar) THEN BEGIN
   if (!d.name eq 'X') THEN $
      window,xsize=512+250,ysize=512+50,title='LAST IMAGE' $
   ELSE BEGIN
      device,set_resolution=[512+250,512+50]
      tverase
   ENDELSE
   tvscl,full_img,0,0,order=0
   del=40
   y=525
   !p.charsize=3.2
   !p.charthick=2.
   !p.font=-1
   xyouts,0,y,'!3LASCO-'+detector,/device,charsize=3.2
   xyouts,' '+date+' '+strmid(date_obs,11,8),charsize=3.2
   xyouts,strmid(time_obs,0,5)+'UT',charsize=3.2
   filename = fxpar (hdr,'FILENAME')
   y = 520-del
   !p.charthick=1.
   sidebar=1.9
   xyouts,511+10,y,'FILENAME: '+filename,/device,charsize=sidebar
   y=y-del
   exptime = fxpar (hdr,'EXPTIME')
   s=string(exptime,format='(f8.1,1x,3hsec)')
   xyouts,511+10,y,'EXPTIME: '+s,/device,charsize=sidebar
   y=y-del
   t=strtrim(fxpar(hdr,'DETECTOR'),2)
   filter = fxpar(hdr,'FILTER')
   xyouts,511+10,y,'FILTER:  '+fxpar(hdr,'FILTER'),/device,charsize=sidebar
   y=y-del
   xyouts,511+10,y,'POLAR:  '+fxpar(hdr,'POLAR'),/device,charsize=sidebar 
   if (t eq 'C1') then begin
      y=y-del
      wl = fxpar(hdr,'FP_UP_WL')
      s = string(wl,format='(f10.3,1x,2hnm)')
      xyouts,511+10,y,'FP: '+s,/device,charsize=sidebar
   endif
   y=y-del
   lp = fxpar (hdr,'LP_NUM')
   xyouts,511+10,y,'LP: '+lp,/device,charsize=sidebar
   y=y-del
   osnum = fxpar (hdr,'OS_NUM')
   xyouts,511+10,y,'OS: '+string(osnum,format='(i8)'),/device,charsize=sidebar
   y=y-del
   s = string(sumcol,sumrow,format='(i3,1x,1hx,1x,i3)')
   xyouts,511+10,y,'PIXSUM: '+s,/device,charsize=sidebar
   y=y-del
   compr = fxpar (hdr,'COMPRSSN')
   s=size(compr)
   if ((s(0) eq 0) and s(1) lt 3)  then compr='N'
   xyouts,511+10,y,'COMPR: '+compr,/device,charsize=sidebar
   b=tvrd()
;ENDIF
;gifname = getenv_slash('PLANNING')+'lasco/'+fname+'.gif'
gifname = fname+'.gif'
cd,'~/',current=old
write_gif,gifname,b
cd,old
!p.charsize=0
!p.charthick=0
!p.font=0
return
end
