	PRO CONV_CAT_1
;+
; Project     :	SOHO - CDS
;
; Name        :	CONV_CAT_1
;
; Purpose     :	Converts format of catalog databases from 1 to 2
;
; Explanation :	Converts the EXPERIMENT and EXPERIMENT2 databases to a new
;		format by reorganizing the fields for faster database access,
;		and by adding the parameters VDS_PMCP, GSET_ID, DETECTOR,
;		ZONE_ID, SLIT_NUM, and FILENAME.
;
; Use         :	One should use the following procedure to convert the databases
;		to the new formats:
;
;			1.  Make sure that the title lines of the old
;			    experiment*.dbd databases do not contain any
;			    version numbers, and that the new databases contain
;			    the phrase "Version 2".
;
;			2.  Change to the directory containing the database
;			    files to be converted.  *** Very important ***
;
;			3.  Replace the old "experiment.dbd" file with the new
;			    version.  You may wish to rename the old file to
;			    "old_experiment.dbd".  Do the same with
;			    "experiment2.dbd".
;
;			4.  Start up IDL and compile this procedure with the
;			    command ".run conv_cat_1.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_cat_1".
;
;		During the conversion, the software will move the old
;		"experiment.dbf", ".dbh", and ".dbx" files to
;		"old_experiment.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, 1 February 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 catalog 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
;
;  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, 'experiment.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 + ' experiment.dbf old_experiment.dbf'
	SPAWN, RENAME + ' experiment.dbx old_experiment.dbx'
	SPAWN, RENAME + ' experiment.dbh old_experiment.dbh'
	SPAWN, RENAME + ' experiment2.dbf old_experiment2.dbf'
	SPAWN, RENAME + ' experiment2.dbx old_experiment2.dbx'
	SPAWN, RENAME + ' experiment2.dbh old_experiment2.dbh'
;
;  Create empty versions of the new database files.
;
	DBCREATE, 'experiment',  1, 1, /EXTERNAL
	DBCREATE, 'experiment2', 1, 1, /EXTERNAL
;
;  Open the old database files, and read in the data.
;
	DBOPEN, 'old_experiment'
	DBEXT, -1, 'PROG_NUM,MAIN_PTR,SEQ_NUM,OBS_SEQ,COUNT,SEQ_IND,RAS_ID', $
		PROG_NUM, MAIN_PTR, SEQ_NUM, OBS_SEQ, COUNT, SEQ_IND, RAS_ID
	DBEXT, -1, 'RAS_VAR,EXPTIME,OBS_MODE,DW_ID,DATE_OBS,DATE_END',	$
		RAS_VAR, EXPTIME, OBS_MODE, DW_ID, DATE_OBS, DATE_END
	DBEXT, -1, 'OBT_TIME,OBT_END,XCEN,YCEN,ANGLE,IXWIDTH,IYWIDTH',	$
		OBT_TIME, OBT_END, XCEN, YCEN, ANGLE, IXWIDTH, IYWIDTH
	DBEXT, -1, 'INS_X0,INS_Y0,INS_ROLL,SC_X0,SC_Y0',	$
		INS_X0, INS_Y0, INS_ROLL, SC_X0, SC_Y0
	DBCLOSE
;
	DBOPEN, 'old_experiment2'
	DBEXT, -1, 'EXPER_PTR,SC_ROLL,WAVEMIN,WAVEMAX,TRACKING,SER_ID',	$
		EXPER_PTR, SC_ROLL, WAVEMIN, WAVEMAX, TRACKING, SER_ID
	DBEXT, -1, 'OPSLBITS,OPSRBITS,SLIT_POS,MIR_POS,EV_ENAB,COMP_ERR', $
		OPSLBITS, OPSRBITS, SLIT_POS, MIR_POS, EV_ENAB, COMP_ERR
	DBEXT, -1, 'VDS_MODE,VDS_ORI,VDS_ACC,SEQVALID,COMMENTS,DATE_MOD', $
		VDS_MODE, VDS_ORI, VDS_ACC, SEQVALID, COMMENTS, DATE_MOD
	DBCLOSE
;
;  Insert the data into the new experiment database.
;
	DBOPEN, 'experiment', 1
	DBBUILD, MAIN_PTR, SEQ_NUM, OBS_SEQ, COUNT, SEQ_IND, RAS_ID, RAS_VAR, $
		EXPTIME, OBS_MODE, DW_ID, OBT_TIME, OBT_END, XCEN, YCEN, $
		ANGLE, IXWIDTH, IYWIDTH, INS_X0, INS_Y0, INS_ROLL, SC_X0, $
		SC_Y0, SC_ROLL, WAVEMIN, WAVEMAX
	DBCLOSE
;
;  Generate the additional data to be put into the new experiment2 database.
;
	NN = N_ELEMENTS(PROG_NUM)
	VDS_PMCP = INTARR(NN)
	GSET_ID = INTARR(NN)
	ZONE_ID = INTARR(NN)
	DETECTOR = REPLICATE('N',NN)
	FILENAME = STRARR(NN)
	SLIT_NUM = REPLICATE(3,NN)
;
;  Determine the correct values for FILENAME, DETECTOR, and SLIT_NUM.
;
	FOR I = 0,NN-1 DO BEGIN
		FILENAME(I) = 's' + TRIM(PROG_NUM(I)) + 'r'
		RASTERCOUNTER = TRIM(SEQ_IND(I))
		IF STRLEN(RASTERCOUNTER) EQ 1 THEN RASTERCOUNTER = '0' + $
			RASTERCOUNTER
		FILENAME(I) = FILENAME(I) + RASTERCOUNTER + '.fits'
;
		GET_F_RASTER, RAS_ID(I), RASTER_DEF
		IF RASTER_DEF.RAS_ID GT 0 THEN BEGIN
			DETECTOR(I) = RASTER_DEF.DETECTOR
			SLIT_NUM(I) = RASTER_DEF.SLIT_NUM
		ENDIF
	ENDFOR
;
;  Insert the data into the new experiment2 database.
;
	DBOPEN, 'experiment2', 1
	DBBUILD, PROG_NUM, SEQ_NUM, EXPER_PTR, DATE_OBS, DATE_END, TRACKING, $
		SER_ID, OPSLBITS, OPSRBITS, SLIT_POS, MIR_POS, EV_ENAB,	$
		COMP_ERR, VDS_PMCP, VDS_MODE, VDS_ORI, VDS_ACC, SEQVALID, $
		COMMENTS, GSET_ID, DETECTOR, ZONE_ID, SLIT_NUM, FILENAME, $
		DATE_MOD
	DBCLOSE
;
;  Clean up.
;
	ASK ,'Conversion complete, do you wish to delete the old files? ', $
		ANSWER
	IF ANSWER EQ 'Y' THEN SPAWN, DELETE +	$
		' old_experiment.db* old_experiment2.db*'
	RETURN
;
;  Error handling point.
;
HANDLE_ERROR:
	MESSAGE, MESSAGE
;
	END
