;+
; Project     :	SOHO - CDS
;
; Name        :	GET_ANOMALY
;
; Purpose     :	Extracts a SOHO anomaly definition from the database
;
; Explanation :	
;
; Syntax      :	GET_ANOMALY, ANOMALY_ID, DEF
;
; Inputs      :	ANOMALY_ID = The unique anomaly ID.
;
; Opt. Inputs :	None.
;
; Outputs     :	DEF	= Structure containing the anomaly definition. 
;
; 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 = ''
;                       GET_ANOMALY, ERRMSG=ERRMSG, ... 
;                       IF ERRMSG NE '' THEN ...
;
; Common      :	None.
;
; Restrictions:	None.
;
; Side effects:	None.
;
; Category    :	Planning, Databases.
;
; Prev. Hist. :	None.
;
; Written     :	Dominic Zarro, ARC/GSFC, 22 January 1996.
;
; Version     :	Version 1
;-
;

	pro get_anomaly, anomaly_id, def, errmsg=errmsg

	on_error, 1
;
;  Define a null result.  If the routine is successful, this will be updated
;  later.
;
	def_anomaly,def
;
;  Check the number of parameters.
;

        if n_params() ne 2 then begin
         message = 'SYNTAX:  GET_ANOMALY, ANOMALY_ID, DEF'
	 goto, handle_error
        endif
;
;  Check the input parameter ANOMALY_ID.
;

	type = datatype(anomaly_id,2)
	if type eq 0 then begin
         message = 'ANOMALY_ID is undefined'
         goto, handle_error
	end else if type ge 4 then begin
         message = 'ANOMALY_ID must be an integer'
         goto, handle_error
	end else if n_elements(anomaly_id) ne 1 then begin
         message = 'ANOMALY_ID must be a scalar'
         goto, handle_error
	endif
;
;  Open the database.
;
	dbopen, 'anomaly'
;
;  Search on the ANOMALY_ID field.
;
	entries = dbfind('id='+strtrim(long(anomaly_id),2)+',deleted=n',/silent)
;
;  If no entries were found, then return immediately.
;
	if !err eq 0 then begin
	 message = 'ANOMALY_ID not found'
	 goto, handle_error
	endif
;
;  Extract the relevant entry from the database.
;
        tag_list=arr2str(tag_names(def),delim=',')
        s=execute('dbext, entries(0),"'+tag_list+'",'+tag_list)
        if s eq 0 then begin
         message = 'Failed Database read'
         goto,handle_error
        endif

;
;  Copy anomaly fields into structure definition
;

        def.id=id(0)
        def.time=time(0)
        def.system=strtrim(system(0),2)
        def.origin=strtrim(origin(0),2)
        def.anomaly=strtrim(anomaly(0),2)
        def.action= strtrim(action(0),2)
        def.impact= strtrim(impact(0),2)
        def.resolution=strtrim(resolution(0),2)
        def.open=open(0)

	goto, finish
;
;  Error handling point.
;

handle_error:
	if n_elements(errmsg) ne 0 then errmsg = 'GET_ANOMALY: ' + message $
		else message, message, /continue
;
;  Close the database, and return.
;

finish:
	dbclose
;
	return
	end
