;+
; NAME:
;       GET_SEC_VOTABLE
; PURPOSE:
;       Use input from SEC VOTable to define time interval
;       Employs REFINE_SEC_TIMES to select record and extend times
;
; CALLING SEQUENCE:
;       times = get_sec_votable(votable=votable  [,/import])
;
; KEYWORD INPUTS:
;       votable_name  name of VOTable to be ingested
; KEYWORD:
;       import        if set, tries to read from SEC server
;                      (note: guesses import if name includes SEC URL) 
; OUTPUTS:
;       times         2 element array containing start and stop times
;
; RESTRICTIONS:
;       Must be connected to Internet to work correctly
;
; PROJECT:
;       EGSO
; HISTORY:
;          Aug-2005  Written by Bob Bentley (MSSL/UCL)
;	14-Nov-2006  rdb  changed /remote to /import - conflict at next level...
;       21-Feb-2007  rdb  renamed routine - was "use_votable_input"
;
;-

function  get_sec_votable, votable_name=votable_name, import=import

  if datatype(votable_name) ne 'STR' then begin
    votable_file = ''
    read,'* Enter VOTable filename [incl. URL if needed]: ', votable_file
  endif else votable_file = votable_name

  votable_file = strtrim(votable_file,2)	; get rid of extra spaces
  if strlen(votable_file) eq 0 then begin
    message,'Null filename entered', /cont
    return,-1
  endif

;    info. related to location of files stored on SEC
  sec_url='sec.ts.astro.it' & path='/xml'
  if strpos(votable_file, sec_url) gt 0 then import=1 	; check is URL

  if not keyword_set(import) then begin

;    VOTable said to be local
;    check if the file exists, then read

    if file_exist(votable_file) then begin
      list = get_votable(votable_file, /quiet)

;    also check temp directory
    endif else begin
      temp = concat_dir(egso_temp_dir(), votable_file)
      message, 'VOTable NOT in current directory', /cont
      message, 'Trying: '+temp, /cont
      if file_exist(temp) then list = get_votable(temp, /quiet) $
      else begin
        message, 'VOTable file not found: '+votable_file, /cont
        return,-1
      endelse

    endelse

  endif else begin

;    VOTable is thought to be on the SEC server

;    make sure we have a complete URL
    remote_url = votable_file
    if strpos(remote_url, sec_url) lt 0 then $
        remote_url = concat_dir(concat_dir(sec_url, path), votable_name)
    print,'Remote URL: *', remote_url+'*'

;    copy file and store in temporary directory
    sock_copy, remote_url, outdir=egso_temp_dir(/create), err=err
    if err ne '' then begin
      message,'Unable to copy VOTable file from SEC server', /cont
      message,'Remote URL: '+remote_url, /cont
      return,-1 
    endif
    break_file,remote_url,aa,bb,cc,dd
    temp = concat_dir(egso_temp_dir(), cc+dd)
    list = get_votable(cc+dd, /quiet)

  endelse

;    select one of list items and "refine" time intervals

  times = refine_sec_times('',list, coordinates=coordinates)   ;null listname
  if times(0) eq -1 then begin
    message,'No Valid times returned', /cont
    return,-1
  endif
  print,'>>>',anytim(times,/ecs)

return, times

end
