	FUNCTION IMP_DETAIL, UNIT, DEF, ERRMSG=ERRMSG
;+
; Project     :	SOHO - CDS
;
; Name        :	IMP_DETAIL()
;
; Purpose     :	Reads a detailed science plan from an export file.
;
; Explanation :	Reads a detailed science plan from a database export file.
;		Called from IMPORT_PLAN.
;
; Use         :	Result = IMP_DETAIL( UNIT, DEF )
;
;		IF NOT IMP_DETAIL( UNIT, DEF ) THEN ...
;
; Inputs      :	UNIT = The logical unit number of the opened input file.
;
; Opt. Inputs :	None.
;
; Outputs     :	DEF  = The detail definition.  This is a structure of a form
;		       suitable for use with the routine ADD_DETAIL.
;
; Opt. Outputs:	None.
;
; Keywords    :	ERRMSG	  = If defined and passed, then any error messages will
;			    be returned to the user in this parameter rather
;			    than depending on the MESSAGE routine in IDL.  If
;			    no errors are encountered, then a null string is
;			    returned.  In order to use this feature, ERRMSG
;			    must be defined first, e.g.
;
;				ERRMSG = ''
;				Result = IMP_DETAIL( ERRMSG=ERRMSG, ... )
;				IF ERRMSG NE '' THEN ...
;
; Calls       :	None.
;
; Common      :	None.
;
; Restrictions:	None.
;
; Side effects:	None.
;
; Category    :	Planning, Databases.
;
; Prev. Hist. :	None.
;
; Written     :	William Thompson, GSFC, 10-Apr-1997
;
; Modified    :	Version 1, William Thompson, GSFC, 10-Apr-1997
;
; Version     :	Version 1, 10-Apr-1997
;-
;
	ON_ERROR, 2
;
;  Check the number of parameters.
;
	IF N_PARAMS() NE 2 THEN BEGIN
		MESSAGE = 'Syntax:  IMP_DETAIL, UNIT, DEF'
		GOTO, HANDLE_ERROR
	ENDIF
;
;  Set up some default values for various parameters, in case they're not found
;  in the file.
;
	N_POINTINGS = 0
;
;  Keep reading until either a blank line, or the first PNT_IND line is found.
;
	KEYWORD = ''
	LINE = 'String'
	WHILE (KEYWORD NE 'PNT_IND') AND (TRIM(LINE) NE '') AND	$
		(NOT EOF(UNIT)) DO BEGIN
	    READF, UNIT, LINE
;
;  Parse the line to extract the keyword and the value.
;
	    IF STRTRIM(LINE) NE '' THEN BEGIN
		SEP = STRPOS(LINE,'=')
		IF (SEP LE 0) OR (SEP GE STRLEN(LINE)) THEN BEGIN
		    MESSAGE = 'Line is not in keyword=value format'
		    GOTO, HANDLE_ERROR
		ENDIF
		KEYWORD = STRTRIM( STRMID(LINE,0,SEP), 2)
		VALUE   = STRTRIM( STRMID(LINE,SEP+1, (STRLEN(LINE)-SEP-1)), 2)
;
;  Interpret the keyword and value.
;
	    	CASE STRUPCASE(KEYWORD) OF
		    'PROG_ID':	PROG_ID		= FIX(VALUE)
		    'STUDY_ID':	STUDY_ID	= FIX(VALUE)
		    'STUDYVAR':	STUDYVAR	= FIX(VALUE)
		    'SCI_OBJ':	SCI_OBJ		= VALUE
		    'SCI_SPEC':	SCI_SPEC	= VALUE
		    'CMP_NO':	CMP_NO		= FIX(VALUE)
		    'OBJECT':	OBJECT		= VALUE
		    'OBJ_ID':	OBJ_ID		= VALUE
		    'DATE_OBS':	DATE_OBS	= DOUBLE(VALUE)
		    'DATE_END':	DATE_END	= DOUBLE(VALUE)
		    'ORIG_DUR':	ORIG_DUR	= DOUBLE(VALUE)
		    'N_RAS1':	N_RASTERS1	= FIX(VALUE)
		    'TIMETAG':	TIME_TAGGED	= BYTE(FIX(VALUE))
		    'TRACKING':	TRACKING	= BYTE(FIX(VALUE))
		    'N_REP_S':	N_REPEAT_S	= FIX(VALUE)
		    'FLAGMSTR':	FLAG_MASTER	= FIX(VALUE)
		    'GSET_ID':	GSET_ID		= FIX(VALUE)
		    'GET_RAW':	GET_RAW		= BYTE(FIX(VALUE))
		    'N_POINT':	N_POINTINGS	= FIX(VALUE)
		    'PNT_IND':	PNT_IND		= FIX(VALUE)
		    ELSE: BEGIN
			MESSAGE = 'Unrecognized keyword ' + KEYWORD
			GOTO, HANDLE_ERROR
			END
		ENDCASE
	    ENDIF
	ENDWHILE
;
;  Define the structure for the line definitions.
;
	POINTINGS = {PLAN_PNT_STRUC,$
		INS_X:	0.0,	$
		INS_Y:	0.0,	$
		ZONE_ID:	0}
;
	IF N_POINTINGS GT 0 THEN BEGIN
	    IF N_POINTINGS GT 1 THEN POINTINGS =	$
		    REPLICATE(POINTINGS, N_POINTINGS)
;
;  Read through the pointing definitions.
;
	    FOR I = 0,N_POINTINGS-1 DO BEGIN
		KEYWORD = 'Keyword'
		WHILE (KEYWORD NE 'PNT_IND') AND (STRTRIM(LINE,2) NE '') AND $
			(NOT EOF(UNIT)) DO BEGIN
		    READF, UNIT, LINE
;
;  Parse the line to extract the keyword and the value.
;
		    IF STRTRIM(LINE) NE '' THEN BEGIN
			SEP = STRPOS(LINE,'=')
			IF (SEP LE 0) OR (SEP GE STRLEN(LINE)) THEN BEGIN
			    MESSAGE = 'Line is not in keyword=value format'
			    GOTO, HANDLE_ERROR
			ENDIF
			KEYWORD = STRTRIM( STRMID(LINE,0,SEP), 2)
			VALUE   = STRTRIM( STRMID(LINE,SEP+1,	$
				(STRLEN(LINE)-SEP-1)), 2)
;
;  Interpret the keyword and value.
;
			CASE STRUPCASE(KEYWORD) OF
			    'INS_X':	POINTINGS(I).INS_X   = FLOAT(VALUE)
			    'INS_Y':	POINTINGS(I).INS_Y   = FLOAT(VALUE)
			    'ZONE_ID':	POINTINGS(I).ZONE_ID = FIX(VALUE)
			    'PNT_IND':	PNT_IND = FIX(VALUE)
			    ELSE: BEGIN
				MESSAGE = 'Unrecognized keyword ' + KEYWORD
				GOTO, HANDLE_ERROR
				END
			ENDCASE
		    ENDIF
	        ENDWHILE
	    ENDFOR
	ENDIF
;
;  Define the output structure as a whole.
;
	DEF = { PROG_ID:	PROG_ID,	$
		STUDY_ID:	STUDY_ID,	$
		STUDYVAR:	STUDYVAR,	$
		SCI_OBJ:	SCI_OBJ,	$
		SCI_SPEC:	SCI_SPEC,	$
		CMP_NO:		CMP_NO,		$
		OBJECT:		OBJECT,		$
		OBJ_ID:		OBJ_ID,		$
		DATE_OBS:	DATE_OBS,	$
		DATE_END:	DATE_END,	$
		ORIG_DUR:	ORIG_DUR,	$
		N_RASTERS1:	N_RASTERS1,	$
		TIME_TAGGED:	TIME_TAGGED,	$
		TRACKING:	TRACKING,	$
		N_POINTINGS:	N_POINTINGS,	$
		N_REPEAT_S:	N_REPEAT_S,	$
		FLAG_MASTER:	FLAG_MASTER,	$
		GSET_ID:	GSET_ID,	$
		GET_RAW:	GET_RAW,	$
		POINTINGS:	POINTINGS}
	RESULT = 1
	GOTO, FINISH
;
;  Error handling point.
;
HANDLE_ERROR:
	IF N_ELEMENTS(ERRMSG) NE 0 THEN ERRMSG = 'IMP_DETAIL: ' + MESSAGE $
		ELSE MESSAGE, MESSAGE, /CONTINUE
	RESULT = 0
;
;  Exit point.
;
FINISH:
	RETURN, RESULT
	END
