;+
; Project     :	SOHO - CDS
;
; Name        :	LIST_ANOMALY
;
; Purpose     :	List the SoHO anomalies for a given time range.
;
; Explanation :	Extracts all the SoHO anomaly entries which intersect an
;		input time range.
;
; Category    :	operations, database
;
; Syntax      :	LIST_ANOMALY, TSTART, TEND,ANOMALIES, N_FOUND
;
; Inputs      :	TSTART, TEND = The range of date/time values to use in searching
;			     the database.  Can be in any standard CDS time
;			     format.
;
; Opt. Inputs :	None.
;
; Outputs     :	ANOMALIES = Array of anomaly structures
;		N_FOUND	= Number of anomalies found.
;
; Opt. Outputs:	None.
;
; Keywords    :	
;       SYSTEM  = If passed, then the name for the system for which 
;       	  to retrieve the anomaly. Otherwise,
;		  anomalies for all systems are retrieved.
;       OPEN    = return only active (i.e., open) anomalies
;       CLOSED  = return only retired (i.e., closed) anomalies
;       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 = ''
;                       LIST_ANOMALY, ERRMSG=ERRMSG, ... 
;                       IF ERRMSG NE '' THEN ...
;
; Side effects:	None.
;
; Common      : None.
;
; Restrictions: None.
;
; Side effects: None.
;
; History     : Version 1,  21-Dec-1995,  D.M. Zarro.  Written
;
; Contact     : DZARRO@SOLAR.STANFORD.EDU
;-

	pro list_anomaly, tstart,tend,anomalies, n_found, system=system, $
               errmsg=errmsg,open=open,closed=closed
                                           
	on_error, 1
        delvarx,anomalies

;
;  check the number of parameters.
;
	n_found = 0
        if n_params() lt 4 then begin
         message = 'SYNTAX:  LIST_ANOMALY, TSTART, TEND, OBS, N_FOUND'
	 goto, handle_error
        endif

;
;  Check the input parameters.
;
	if n_elements(tstart) ne 1 then begin
         message = 'TSTART must be a scalar'
         goto, handle_error
	end else if n_elements(tend) ne 1 then begin
         message = 'TEND must be a scalar'
         goto, handle_error
	endif
;
;  Check for OPEN/CLOSED keywords
;
        sopen=keyword_set(open)
        sclosed=keyword_set(closed)
        all= (sopen and sclosed) or ((not sopen) and (not sclosed))

;
; Check for SYSTEM keyword
;
        sel_system=1
        if datatype(system) ne 'STR' then sel_system=0 else begin
         if strtrim(system(0),2) eq '' then sel_system=0
        endelse
        if sel_system then sys=system

;
;  Convert the input search times to TAI format.
;
        day_secs=24.d*3600.d
        err=''
        tai_tstart=utc2tai(anytim2utc(tstart,err=err))
        if err ne '' then begin message=err & goto,handle_error & endif
        tai_tend=utc2tai(anytim2utc(tend,err=err,/date))+day_secs

        if err ne '' then begin message=err & goto,handle_error & endif
;
;  Open the Anomaly database.
;
	dbopen, 'anomaly'
;
;  Find all the entries in the requested time range.
;

	test = trim(tai_tstart,'(f15.3)')+' < time < '+trim(tai_tend,'(f15.3)') 
	entries = dbfind(test+',deleted=n',/silent)
	if !err eq 0 then begin
	 n_found = 0
         message='No matching entries found'
	 goto, handle_error
	end else begin
	 entries = entries(uniq(entries))
	 n_found = n_elements(entries)
	endelse
;
;  Extract the requested entries, sorted by times.
;

	entries = dbsort(entries,'time')
        def_anomaly,def
        tag_list=arr2str(tag_names(def),delim=',')
        s=execute('dbext, entries,"'+tag_list+'",'+tag_list)
        if s eq 0 then begin
         message = 'Failed Database read'
         goto,handle_error
        endif

;
;  Define the structure that the data will be returned in and
;  copy the entries into it.
;

        if (n_found eq 1) then begin
         id=id(0) & time=time(0) & system=system(0)
         origin=origin(0) & anomaly=anomaly(0)
         impact=impact(0) & action=action(0)
         resolution=resolution(0) & open=open(0)
        endif

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

;
; Extract systems
;

        if sel_system then begin
         db_systems=strupcase(strmid(strtrim(anomalies.system,2),0,3))
         systems=strupcase(strmid(strtrim(sys,2),0,3))
         look=where_vector(systems,db_systems,n_found)
         if n_found eq 0 then begin
          message='No matching entries found'
          goto,handle_error
         endif else anomalies=anomalies(look)
        endif

;
; Extract OPEN/CLOSED entries
;
        if not all then begin
         if sopen then look=where(anomalies.open eq 1,n_found)
         if sclosed then look=where(anomalies.open eq 0,n_found)
         if n_found eq 0 then begin
          message='No matching entries found'
          goto,handle_error
         endif else anomalies=anomalies(look)
        endif
	goto, finish
;
;  error handling point.
;

handle_error:
	if n_elements(errmsg) ne 0 then errmsg = 'LIST_ANOMALY: ' + message $
	 else message, message, /continue
;
;  close the database and return.
;
finish:
	dbclose
;
	return
	end
