	PRO CONV_SCI_DETAILS
;+
; Project     :	SOHO - CDS
;
; Name        :	CONV_SCI_DETAILS
;
; Purpose     :	Adds PROG_NUM to sci_details database
;
; Explanation :	Converts the sci_details database to version 2 by adding the
;		field PROG_NUM.
;
; Use         :	One should use the following procedure to convert the
;		sci_details database to version 2:
;
;			1.  Make sure that the title line of the new main.dbd
;			    database file contains the phrase "Version 3"--the
;			    old title should contain no version information.
;			    The only difference between the two files should be
;			    the inclusion of the line defining the PROG_NUM
;			    field.
;
;			2.  Change to the directory containing the database
;			    files to be converted.  *** Very important ***
;
;			3.  Replace the old "sci_details.dbd" file with the new
;			    version.  You may wish to rename the old file to
;			    "old_sci_details.dbd".
;
;			4.  Start up IDL and compile this procedure with the
;			    command ".run conv_sci_details.prg".  Naming the
;			    file with the .prg extension is used to ensure that
;			    the routine is not executed accidently.
;
;			5.  Make sure that the software is enabled by entering
;			    the command "!priv = 5".  This is another
;			    protective step.
;
;			6.  Execute the procedure with the command
;			    "conv_sci_details".
;
;		During the conversion, the software will move the old
;		"sci_details.dbf", ".dbh", and ".dbx" files to
;		"old_sci_details.dbf", etc.  When the conversion is complete,
;		you will be asked if you want to delete these files or not.
;
; Category    :	This is not part of the CDS software tree.
;
; Written     :	William Thompson, GSFC, 3 June 1997
;-
;
	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 sci_details database to version 2.'
	ASK, 'Are you sure you want to do this? ', ANSWER
	IF ANSWER NE 'Y' THEN BEGIN
		PRINT, 'Execution aborted'
		RETURN
	ENDIF
;
;  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, 'sci_details.dbf', /GET_LUN
	FREE_LUN, UNIT
;
;  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
;
;  Preserve the old database files by renaming them.
;
	MESSAGE = 'Error occured'
;
	SPAWN, RENAME + ' sci_details.dbf old_sci_details.dbf'
	SPAWN, RENAME + ' sci_details.dbx old_sci_details.dbx'
	SPAWN, RENAME + ' sci_details.dbh old_sci_details.dbh'
;
;  Create empty versions of the new database files.
;
	DBCREATE, 'sci_details', 1, 1, /EXTERNAL
;
;  Open the old sci_details database file, and read in the data.
;
	DBOPEN, 'old_sci_details'
	DBEXT, -1, 'PROG_ID,STUDY_ID,STUDYVAR,SCI_OBJ,SCI_SPEC,CMP_NO',	$
		PROG_ID, STUDY_ID, STUDYVAR, SCI_OBJ, SCI_SPEC, CMP_NO
	DBEXT, -1, 'OBJECT,OBJ_ID,DATE_OBS,DATE_END,ORIG_DUR,N_RASTERS1', $
		OBJECT, OBJ_ID, DATE_OBS, DATE_END, ORIG_DUR, N_RASTERS1
	DBEXT, -1, 'TIME_TAGGED,TRACKING,N_POINTINGS,N_REPEAT_S,FLAG_MASTER', $
		TIME_TAGGED, TRACKING, N_POINTINGS, N_REPEAT_S, FLAG_MASTER
	DBEXT, -1, 'GSET_ID,GET_RAW,DELETED', GSET_ID, GET_RAW, DELETED
	DBCLOSE
;
;  Generate dummy data for PROG_NUM.
;
	PROG_NUM = LONARR(N_ELEMENTS(PROG_ID))
;
;  Insert the data into the new sci_details database.
;
	DBOPEN, 'sci_details', 1
	DBBUILD, PROG_ID, STUDY_ID, STUDYVAR, SCI_OBJ, SCI_SPEC, CMP_NO, $
		OBJECT, OBJ_ID, DATE_OBS, DATE_END, ORIG_DUR, N_RASTERS1, $
		TIME_TAGGED, TRACKING, N_POINTINGS, N_REPEAT_S, FLAG_MASTER, $
		GSET_ID, GET_RAW, PROG_NUM, DELETED
	DBCLOSE
;
;  Clean up.
;
	ASK ,'Conversion complete, do you wish to delete the old files? ', $
		ANSWER
	IF ANSWER EQ 'Y' THEN SPAWN, DELETE + ' old_sci_details.db*'
	RETURN
;
;  Error handling point.
;
HANDLE_ERROR:
	MESSAGE, MESSAGE
;
	END
