;+
; Project     :	SOHO - CDS
;
; Name        :	LIST_RESOURCE
;
; Purpose     :	List the resource times for a given period
;
; Explanation :
;
; Use         :	LIST_RESOURCE, START, END, RESOURCE, N_FOUND
;
; Inputs      :	START, END = The range of date/time values to use in searching
;			     the database.  Can be in any standard CDS time
;			     format.
;
; Opt. Inputs :	None.
;
; Outputs     :	RESOURCE  = A structure variable containing the following tags
;			  for each planned contact period:
;
;			START_TIME   = Date/time of beginning of the RESOURCE
;				       observaton, in TAI format
;			END_TIME     = Date/time of end of RESOURCE observation,
;				       in TAI format
;
;		N_FOUND	= Number of planned contact periods found.
;
; Opt. Outputs:	None.
;
; Keywords    :	
;       ERRMSG     = error message
;       EXTRA      = resource keyword (e.g. TAPE_DUMP, MDI_M, COM ...)
;       RES_NAME   = actual resource read
;
; Restrictions:	None.
;
; Side effects:	None.
;
; Category    :	Planning, Database.
;
; Prev. Hist. :	None.
;
; Written     :	Dominic Zarro, GSFC, 3 August  1995
;               Version 2, March 15, 1996, Liyun Wang, GSFC/ARC
;                  list_inst_dlyd not called for SUMER
;
; Version     :	Version 2, March 15, 1996
;-
;

PRO list_resource, tstart, tend, resource, _extra=extra, n_found, $
                   errmsg=errmsg, quiet=quiet, res_name=res_name

   ON_ERROR, 1
   res_name = '' 
;
;-- check the number of parameters.
;
   IF (N_PARAMS() LT 3) OR (datatype(extra) NE 'STC') THEN BEGIN
      MESSAGE = 'syntax:  list_resource, start, end, resource, n_found, /extra'
      GOTO, handle_error
   ENDIF

   verbose = NOT KEYWORD_SET(quiet)
;
;-- check the input parameters.
;
   n_found = 0
   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
;
;-- convert the input dates to TAI format.
;
   IF datatype(tstart, 1) EQ 'Double' THEN tai_tstart = tstart ELSE	$
      tai_tstart = utc2tai(anytim2utc(tstart))
   IF datatype(tend, 1) EQ 'Double' THEN tai_end = tend ELSE	$
      tai_end = utc2tai(anytim2utc(tend))

;-- extract valid resource keyword

   delvarx, resource
   tags = TAG_NAMES(extra)
   res_name = get_res_name(tags, err=errmsg)
   IF res_name EQ '' THEN RETURN

;-- special cases

   IF (grep('ds', tags))(0) NE '' THEN BEGIN
      IF verbose THEN MESSAGE, 'reading DSN CONTACTS...', /cont
      list_dsn, tstart, tend, resource, n_found, err=errmsg
      RETURN
   ENDIF

   IF (grep('com', tags))(0) NE '' THEN BEGIN
      IF verbose THEN MESSAGE, 'reading COM TIMES...', /cont
      list_com_times, tstart, tend, resource, n_found, err=errmsg
      RETURN
   ENDIF

   IF (grep('dly', tags))(0) NE '' THEN BEGIN
      IF which_inst() EQ 'C' THEN BEGIN
         IF verbose THEN MESSAGE, 'reading DELAYED COMMANDING...', /cont
         list_inst_dlyd, tstart, tend, resource, n_found, err=errmsg
      ENDIF ELSE BEGIN
         MESSAGE, 'No DB for instrument delayed commanding time', /cont
      ENDELSE
      RETURN
   ENDIF


   IF ((grep('ot', tags))(0) NE '') OR $
      ((grep('su', tags))(0) NE '') THEN BEGIN
      IF verbose THEN MESSAGE, 'reading SUPPORT observations...', /cont
;      list_other_obs, tstart, tend, resource, n_found, err=errmsg
      list_support, tstart, tend, resource, n_found, err=errmsg
      RETURN
   ENDIF

   IF (grep('nr', tags))(0) NE '' THEN BEGIN
      IF verbose THEN MESSAGE, 'reading NRT_RESERVED...', /cont
      list_nrt_res, tstart, tend, resource, n_found, err=errmsg
      RETURN
   ENDIF

;-- do the rest

   IF verbose THEN MESSAGE, 'reading '+res_name, /cont
   list_other_res, tstart, tend, resource, n_found, err=errmsg, _extra=extra

   RETURN
   
handle_error:
   IF N_ELEMENTS(errmsg) NE 0 THEN              $
      errmsg = 'list_resource: ' + MESSAGE ELSE MESSAGE, MESSAGE 
   
END
