	PRO MAKECDSQLI, HEADER, DATA, I_WINDOW, TTYPE, TDESC
;+
; Project     :	SOHO - CDS
;
; Name        :	MAKECDSQLI
;
; Purpose     :	Makes CDS quick-look image files
;
; Explanation :	This routine creates CDS quick-look image files to be used for
;		science planning during SOHO operations.  These are simple
;		two-dimensional FITS files containing only the spatial
;		information, one file per wavelength.  Such files are only
;		produced when both spatial dimensions are non-trivial.  The
;		data is summed over wavelength.
;
; Use         :	MAKECDSQLI, HEADER, DATA, I_WINDOW, TTYPE, TDESC
;
; Inputs      :	HEADER	= The primary header from the FITS binary table.  This
;			  is used to create the header for the quick-look image
;			  file.
;		DATA	= The data array from CDSWINEXTR
;		I_WINDOW= The index for the window to be written to a
;			  quick-look image file.
;		TTYPE	= The window name
;		TDESC	= The axis labels for the window
;
; Opt. Inputs :	None.
;
; Outputs     :	The data are written to a simple FITS file with the filename
;
;			scds_WWWWW_xx_YYYYMMDD_HHMM.fts
;
;		where WWWWW represents the wavelength, and YYYYMMDD and HHMM
;		are respectively the date and time of the observation.
;
;		Also, the arrays HEADER and DATA are modified by this routine.
;
; Opt. Outputs:	None.
;
; Keywords    :	None.
;
; Env. vars.  :	CDS_FITS_QLI_W defines where the FITS quick-look image files
;		will be written.  If not defined, then the FITS quick-look
;		image files are written in the current directory.
;
; Calls       :	FXBTDIM, SXDELPAR, FXADDPAR, ANYTIM2CAL, FXHMAKE, FXWRITE
;
; Common      :	FITSGEN contains WAVELENGTHS, POINTINGS, TIMES
;
; Restrictions:	None.
;
; Side effects: The arrays HEADER and DATA are modified by this routine.
;
;		No matter what the value of TTYPE is, the filename will be
;		lowercase.
;
; Category    :	Data_handling, Telemetry, I/O, FITS, CDS.
;
; Prev. Hist. :	None.
;
; Written     :	William Thompson, GSFC, 13 March 1995
;
; Modified    :	Version 1, William Thompson, GSFC, 13 March 1995
;		Version 2, William Thompson, GSFC, 18 October 1995
;			Changed environment variable to CDS_FITS_QLI_W
;		Version 3, William Thompson, GSFC, 8 December 1995
;			Corrected bug with XSTART, introduced when spacecraft
;			pointing correction was implemented.
;		Version 4, William Thompson, GSFC, 28 March 1996
;			Don't write file unless at least 60x60 in size.
;		Version 5, William Thompson, GSFC, 30 May 1996
;			Change filenaming convention to agree with standard.
;			Add call to CDS_CLEAN_IMAGE
;		Version 6, William Thompson, GSFC, 18 June 1996
;			Fixed typo in writing out CDELT2.
;			Write out data as bytes.
;			Changed way CDS_CLEAN_IMAGE is called.
;		Version 7, William Thompson, GSFC, 12 August 1999
;			Continue even if not able to write file.
;
; Version     :	Version 7, 12 August 1999
;-
;
	ON_ERROR, 2
@fitsgen.cmn
;
;  Check the number of parameters.
;
	IF N_PARAMS() NE 5 THEN MESSAGE,	$
		'Syntax:  MAKECDSQLI, HEADER, DATA, I_WINDOW, TTYPE, TDESC'
;
;  Check to see if a quick-look image file should be written for this data
;  window.  A file is written only if both the X and Y dimensions are
;  non-trivial and the window label does not start with "Back" as in
;  "Background".
;
	IF STRMID(STRLOWCASE(TTYPE),0,4) EQ 'back' THEN RETURN
;
;  In what follows, it is assumed that the dimensions of the data can only fall
;  into one of the following possibilities:
;
;		SOLAR_X, SOLAR_Y
;		SOLAR_X, SOLAR_Y, DEL_TIME
;		WAVELNTH, SOLAR_X, SOLAR_Y
;		WAVELNTH, SOLAR_X, SOLAR_Y, DEL_TIME
;
;  First extract the window labels, and get the dimensions of the array.
;
	DESC = FXBTDIM(TDESC)
	SZ = SIZE(DATA)
;
;  If the number of dimensions is four, then there is no way that both spatial
;  dimensions can be non-trivial.
;
	CASE N_ELEMENTS(DESC) OF
		4: RETURN
;
;  If the number of dimensions is three, and the first dimension is not
;  "WAVELNTH", then there is no way that both spatial dimensions can be
;  non-trivial, because the instrument must be in "sit-and-stare" mode in at
;  least one of the dimensions.
;
		3: BEGIN
			IF DESC(0) NE 'WAVELNTH' THEN RETURN
			NX = SZ(2)
			NY = SZ(3)
			END
		2: BEGIN
			NX = SZ(1)
			NY = SZ(2)
			END
	ENDCASE
;
;  If the size in either the X and Y dimension is trivial, then return.
;
	IF (NX LT 60) OR (NY LT 60) THEN RETURN
;
;  If necessary, sum up over the wavelength dimension.  Reuse the same area in
;  memory to save time.
;
	IF N_ELEMENTS(DESC) EQ 3 THEN DATA =	$
		AVERAGE(TEMPORARY(DATA),1,MISSING=-1)
;
;  Clean the image.
;
	CDS_CLEAN_IMAGE, DATA
;
;  Remove the EXTEND keyword from the header, and change the SFDUADID.  For the
;  moment, use the fictitious value 'ZZZZ0002'.
;
	SXDELPAR, HEADER, 'EXTEND'
	FXADDPAR, HEADER, 'SFDUADID', 'ZZZZ0002'
;
;  Form the filename, and change the filename in the header.
;
	DATE = ANYTIM2CAL(TIMES.DATE_OBS, FORM=8)
	TIME = STRMID(DATE,8,4)
	DATE = STRMID(DATE,0,8)
	WAVE = TRIM(ROUND(WAVELENGTHS.TWAVE(I_WINDOW)))
	FILENAME = 'scds_00' + WAVE + '_xx_' + DATE + '_' + TIME
	FXADDPAR, HEADER, 'FILENAME', FILENAME
;
;  Scale the data to a byte array.
;
	BSCALE, DATA, MISSING=-1, TOP=255
;
;  Add the required FITS keywords needed to describe the data.
;
	FXHMAKE, HEADER, DATA
;
;  Add the parameters CRPIX, CRVAL, etc.
;
	FXADDPAR, HEADER, 'CTYPE1', 'SOLAR_X', 'Solar X (cartesian west) axis'
	FXADDPAR, HEADER, 'CTYPE2', 'SOLAR_Y', 'Solar Y (cartesian north) axis'
;
	FXADDPAR, HEADER, 'CUNIT1', 'ARCSEC', 'Arcseconds from center of sun'
	FXADDPAR, HEADER, 'CUNIT2', 'ARCSEC', 'Arcseconds from center of sun'
;
	FXADDPAR, HEADER, 'CRPIX1', 1, 'Reference pixel along X dimension'
	FXADDPAR, HEADER, 'CRPIX2', 1, 'Reference pixel along Y dimension'
;
	FXADDPAR, HEADER, 'CRVAL1', POINTINGS.XSTART(I_WINDOW),	$
		'Reference position along X dimension'
	FXADDPAR, HEADER, 'CRVAL2', POINTINGS.YSTART(I_WINDOW),	$
		'Reference position along Y dimension'
;
	FXADDPAR, HEADER, 'CDELT1', POINTINGS.XDELTA,	$
		'Increments along X dimension'
	FXADDPAR, HEADER, 'CDELT2', POINTINGS.YDELTA(I_WINDOW),	$
		'Increments along Y dimension'
;
;  Add or modify the wavelength parameters.
;
	FXADDPAR, HEADER, 'WAVEMIN', WAVELENGTHS.TWMIN(I_WINDOW),	$
		'Minimum wavelength in observation'
	FXADDPAR, HEADER, 'WAVEMAX', WAVELENGTHS.TWMAX(I_WINDOW),	$
		'Maximum wavelength in observation'
	FXADDPAR, HEADER, 'WAVELNTH', WAVELENGTHS.TWAVE(I_WINDOW),	$
		'Principal wavelength in Angstroms', BEFORE='WAVEMIN'
;
;  Write out the FITS file.
;
	FILENAME = CONCAT_DIR( GETENV('CDS_FITS_QLI_W'), FILENAME + '.fts')
	ERRMSG = ''
	FXWRITE, FILENAME, HEADER, DATA, ERRMSG=ERRMSG
	IF ERRMSG NE '' THEN CDS_MESSAGE, ERRMSG, /CONTINUE, /BELL
;
	RETURN
	END
