pro write_last_c1,img,hdr,lct=lct,gct=gct,z=z
;+
; NAME:				WRITE_LAST_IMG
;
; PURPOSE:			Create GIF image of the last real time image
;				processed so that xv can then read and display
;				it.
;
; CATEGORY:			REDUCTION
;
; CALLING SEQUENCE:		WRITE_LAST_IMG,Img,Hdr
;
; INPUTS:			Img = Input Image array
;				Hdr = FITS header
;
; OPTIONAL INPUTS:		None
;
; KEYWORD PARAMETERS:		None
;
; OUTPUTS:			None
;
; OPTIONAL OUTPUTS:		None
;
; COMMON BLOCKS:		None
;
; SIDE EFFECTS:			None
;
; RESTRICTIONS:			This must be done with IDL haveing an X window
;				display.
;
; PROCEDURE:			Scales the image to not larger than 512 x 512,
;				and creates a GIF image with annotation along
;				the side
;
; EXAMPLE:
;
; MODIFICATION HISTORY:		Written, RA Howard, NRL
;    VERSION 1   rah    9 Nov 1995
;    VERSION 2   rah   16 Nov 1995  Conversions added
;    VERSION 3   rah   29 Nov 1995  Modified layout
;    VERSION 4   rah   11 Dec 1995  Modified Detector conversion
;    VERSION 5   rah   12 Jan 1996  Changed assumption to be !order = 0
;				    Added R1 and R2 in place of P1, P2
;    VERSION 6   rah   19 Apr 1996  Modified scaling of EIT image
;                                   Corrected display of PIXSUM to include LEB
;    VERSION 7   rah   23 May 1996  Corrected handling of images outside of chip
;    VERSION 8   rah   22 Jul 1996  Corrected handling of cals > 1024 lines
;    VERSION 9   rah    8 Oct 1996  Added subtraction of background model
;    VERSION 10  sep   18 Jun 1997  Changed call for new OFFSET_BIAS()
;
;
;	@(#)write_last_img.pro	1.12 10/06/97 LASCO IDL LIBRARY
;-
;
;   If not full image then insert into proper place in a
;   full & square image
;
common lastimage,full_img

dname = !d.name

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')
telescope = fxpar (hdr,'TELESCOP')
detector = strtrim(fxpar(hdr,'DETECTOR'),2)
filter = STRUPCASE(STRCOMPRESS(FXPAR(hdr,'FILTER'),/remove_all))
IF (detector NE 'EIT') then polar='POLAR' ELSE polar='SECTOR'
polar = STRUPCASE(STRCOMPRESS(FXPAR(hdr,polar),/remove_all))
compr = fxpar (hdr,'COMPRSSN')
IF (STRPOS(compr,'P') GE 0)       $  ; Is radial spoke used?
   THEN radial_spoke=1 ELSE radial_spoke=0
nx = nxsum*naxis1
ny = nysum*naxis2
full_img = reduce_std_size(img,hdr)
lp = FXPAR(hdr,'LP_NUM')
CASE detector OF
  'EIT': full_img = BYTSCL(ALOG10((full_img-848)>1))
  'C1':  BEGIN		; get the offband image if available and form subtraction

         END
;  'C2':  full_img=difbkgnd(full_img,hdr,minval=-100,maxval=600,altmin=1500,altmax=4000, /USE_CURRENT)
;  'C3':  full_img=difbkgnd(full_img,hdr,/logscl,minval=20,maxval=500,altmin=3600,altmax=10000, /USE_CURRENT)
'C2':  BEGIN
          full_img=difbkgnd(full_img,hdr,minval=0.9,maxval=1.2, /ratio , /USE_CURRENT)
       END
'C3':  BEGIN
          full_img=difbkgnd(full_img,hdr,minval=0.9,maxval=1.2, /ratio , /ANY_YEAR)
       END
  ELSE:  full_img = BYTSCL(full_img)
ENDCASE
sz = SIZE(full_img)
FXADDPAR,hdr,'NAXIS1',sz(1)
FXADDPAR,hdr,'NAXIS2',sz(1)
;
;
;  Add in the LASCO logo
;
restore,getenv('NRL_LIB')+'/lasco/display/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

IF NOT KEYWORD_SET(z) THEN BEGIN
   if (!d.name eq 'X') then window,xsize=512+300,ysize=512+50,title='LAST IMAGE'
               endif else begin
                        dname = !d.name
                        set_plot,'Z'
			IF KEYWORD_SET(lct) THEN loadct,lct
                        IF KEYWORD_SET(gct) THEN gamma_ct,gct
			device,set_resolution=[512+310,512+50]
			tverase
			endelse
tv,full_img,0,0,order=0
del=40
y=525
!p.charsize=3.2
!p.charthick=2.
!p.font=-1
xyouts,0,y,'!3 LASCO-'+detector,/device,charsize=3.2
months='JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC'
date_obs = fxpar (hdr,'DATE-OBS')
date = strmid(date_obs,2,2)
date = date + strmid(months,3*(strmid(date_obs,5,2)-1),3)
date = date + strmid(date_obs,8,2)
xyouts,' '+date+' '+strmid(date_obs,11,8),charsize=3.2
time_obs = fxpar (hdr,'TIME-OBS')
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)
xyouts,511+10,y,'FILTER:  '+filter,/device,charsize=sidebar
y=y-del
if (t ne 'EIT') then begin
   xyouts,511+10,y,'POLAR:  '+polar,/device,charsize=sidebar
endif else begin
   xyouts,511+10,y,'SECTOR: '+polar,/device,charsize=sidebar
endelse
if (t eq 'C1') then begin
   y=y-del
   wl = fxpar(hdr,'WAVELENG')
   s = string(wl,format='(f10.4,1x,1hA)')
   xyouts,511+10,y,'FP: '+s,/device,charsize=sidebar
endif
y=y-del
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(nxsum,nysum,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()
fname = 'lastmpa_'+detector
fname = getenv_slash('LAST_IMG')+fname+'.gif'
;fname2 = getenv_slash('LAST_IMG')+'lastimg.gif'
;
;   find out if link from lastimg.gif to individual last images is present
;   if so, then delete it
;
;spawn,'ls '+fname2,exist
;s=size(exist)
;if s(0) eq 1 then spawn,'/bin/rm '+fname2
write_gif,fname,b
;spawn,'ln -s '+fname+' '+fname2
!p.charsize=0
!p.charthick=0
!p.font=0

set_plot,dname
return
end
