	PRO CONV_NIS_WAVECAL
;+
; Project     :	SOHO - CDS
;
; Name        :	CONV_NIS_WAVECAL
;
; Purpose     :	Converts NIS wavelength calibration database.
;
; Explanation :	Converts NIS wavelength calibration database to second order.
;
; Use         :	One should use the following procedure to convert the databases
;		to the new formats:
;
;			1.  The old nis_wave.dbd file should be listed as
;			    version 1 (5 March 1996), and the new file should
;			    be version 2 (5 July 1996).
;
;			2.  Change to the directory containing the database
;			    files to be converted.  *** Very important ***
;
;			3.  Replace the old nis_wave.dbd file with the new
;			    file.  You may wish to rename the old file to
;			    "old_nis_wave.dbd".
;
;			4.  Start up IDL and compile this procedure with the
;			    command ".run conv_nis_wavecal.prg".  Naming the
;			    file with the .prg extension is used to ensure that
;			    the routine is not executed accidently.
;
;			4.  Make sure that the software is enabled by entering
;			    the command "!priv = 5".  This is another
;			    protective step.
;
;			5.  Execute the procedure with the command
;			    "conv_nis_wavecal".
;
; Category    :	This is not part of the CDS software tree.
;
; Written     :	William Thompson, GSFC, 5 July 1996
;-
;
	ON_ERROR, 2
;
;  Make sure that the user has the privilege set correctly to run this routine.
;
	IF !PRIV LT 5 THEN MESSAGE,	$
		'!PRIV must be set to 5 to run this routine.'
;
;  Make sure that the user really wants to convert the database.
;
	PRINT, 'Converting nis_wave databases to version 2.'
	ASK, 'Are you sure you want to do this? ', ANSWER
	IF ANSWER NE 'Y' THEN BEGIN
		PRINT, 'Execution aborted'
		RETURN
	ENDIF
;
;  Determine the correct commands for renaming and deletion.
;
	CASE OS_FAMILY() OF
		'vms': BEGIN
			RENAME = 'RENAME'
			DELETE = 'DELETE'
			END
		'unix': BEGIN
			RENAME = 'mv'
			DELETE = 'rm'
			END
		ELSE: MESSAGE, 'Only supported in VMS or Unix'
	ENDCASE
;
;  Check to make sure that the proper database files are in the current
;  directory.
;
	MESSAGE = 'Database files not in current directory'
	ON_IOERROR, HANDLE_ERROR
	OPENR, UNIT, 'nis_wave.dbf', /GET_LUN
	FREE_LUN, UNIT
;
;  Preserve the old database files by renaming them.
;
	MESSAGE = 'Error occured'
	SPAWN, RENAME + ' nis_wave.dbf old_nis_wave.dbf'
	SPAWN, RENAME + ' nis_wave.dbx old_nis_wave.dbx'
	SPAWN, RENAME + ' nis_wave.dbh old_nis_wave.dbh'
;
;  Create empty versions of the new database files.
;
	DBCREATE, 'nis_wave',  1, 1, /EXTERNAL
;
;  Open the old database files, and read in the data.
;
	DBOPEN, 'old_nis_wave'
	DBEXT, -1, 'DATE,FILENAME,COEFF', DATE, FILENAME, COEFF
	DBCLOSE
;
;  Convert the coefficients to the new format.
;
	OLD_COEFF = REFORM(COEFF,2,2,2)
	COEFF = DBLARR(2,3,2)
	COEFF(0,0,0) = OLD_COEFF
;
;  Insert the data into the new nis_wave database.
;
	DBOPEN, 'nis_wave', 1
	DBBUILD, DATE, FILENAME, COEFF
	DBCLOSE
;
;  Clean up.
;
	ASK ,'Conversion complete, do you wish to delete the old files? ', $
		ANSWER
	IF ANSWER EQ 'Y' THEN SPAWN, DELETE + ' old_nis_wave.db*'
	RETURN
;
;  Error handling point.
;
HANDLE_ERROR:
	MESSAGE, MESSAGE
;
	END
