function pfss_time2file, time0, time1, urls=urls, debug=debug, $
   before=before, after=after, count=count, refresh=refresh, $
   ssw_catalog=ssw_catalog, generate=generate, loud=loud, $
   cpfsslinks=cpfsslinks , csswlinks=csswlinks
;+
;   Name: pfss_time2file
;
;   Purpose: any ssw time -> PFSS save file (MDI extrap); NFS or URL
;
;   Input Parameters:
;      time0 - time desired, any SSW format (string, index record..)
;      time1 - optional stop time for range
;
;   Output:
;      function returns files or urls matching users input time/range
;
;   Keyword Parameters:
;      URLS - if set, return URLs 
;      BEFORE - if set, match closest BEFORE (default is closest)
;      AFTER - if set, match closest AFTER   (default is closest)
;      COUNT (output) - number of files returned (zero implies error)
;      SSW_CATALOG - if set, use $SSWDB/pfss/catalog 
;      GENERATE - if set (and permission and local), generate SSWDB cat
;
;   Calling Sequence:
;      mdisav=pfss_time2file(time [,/BEFORE] [,/AFTER] )   
;      mdisavs=pfss_time2file(startT, endT, [,/BEFORE] [,/AFTER] )
;
;   5-December-2003 - S.L.Freeland - ssw/pfss integration helper
;-

common pfss_time2file_blk, times, links, sswlinks
common pfss_time2file_blk2, lastbfieldcat

if n_elements(lastbfieldcat) eq 0 then lastbfieldcat=''  ; init common
debug=keyword_set(debug)
urls=keyword_set(urls)

count=0
retval=''
refresh=keyword_set(refresh)
before=keyword_set(before)
after=keyword_set(after)
loud=keyword_set(loud)

authorize=str2arr('freeland,derosa,hurlburt')
generate=keyword_set(generate) and is_member(get_user(),authorize)

ssw_catalog=keyword_set(ssw_catalog) or generate

pfss_path=get_logenv('PFSS_PATH')
pfss_http=get_logenv('PFSS_HTTP')
date_sub='Bfield-bydate'

if pfss_path eq '' then $
   pfss_path='/hosts/solserv/data1/pfss/kitrun48'

if pfss_http eq '' then $
   pfss_http='http://www.lmsal.com/solarsoft/pfss_links'

pfsscatdir=$
   concat_dir(concat_dir(concat_dir('$SSWDB','packages'),'pfss'),'genxcat')

bfieldcatname='bfield.geny'
if file_exist(lastbfieldcat) then bfieldcat =lastbfieldcat else $
     bfieldcat=concat_dir(pfsscatdir,bfieldcatname)  ; default = $SSWDB version 
bfieldurl='http://www.lmsal.com/solarsoft/pfss_genxcat/'+bfieldcatname ; url alternate

if ssw_catalog then begin 
   if 1-file_exist(bfieldcat) then begin 
      box_message,'No local catalog in SSWDB, trying remote http access..'
      outdir=get_temp_dir()
      bfieldcat=concat_dir(outdir,'bfield.geny')
      ssw_file_delete,bfieldcat
      sock_copy,bfieldurl,out_dir=outdir,/prog     ; WWW -> local via http socket
      if not file_exist(bfieldcat) then begin 
         box_message,'Problem with remote http access; no ssw pfss catalog available...
         return,''
      endif else lastbfieldcat=bfieldcat           ; only do the transfer once
   endif 
   restgenx, file=bfieldcat, times, links, sswlinks    ; use ssw catalog
   csswlinks=csswlinks 
   cpfsslinks=links
endif

refresh=file_exist(pfss_path) and $
   n_elements(links) eq 0 or keyword_set(refresh) or generate

if refresh then begin 
   box_message,'Initializing file list'
   files=findfile(concat_dir(pfss_path,date_sub))
   ss=where(strpos(files,'Bfield') ne -1 and $
            strpos(files,'.sav')   ne -1, fcnt)
   if fcnt eq 0 then begin 
      box_message,'Problem with PFSS listing, bailing out..
      stop
      return,''
   endif
   links=files(ss)
   times=strextract(links,'Bfield_','.sav') ; CCSDS filename
   sswlinks=$                               ; ssw-rational (OS-transportable) 
      ;temporary(strcompress,/remove, $
      temporary(str_replace( $
      temporary(str_replace( $
      temporary(str_replace(links,'-',' ')),':',' ')),'T','_'))	
   sswlinks=strcompress(temporary(sswlinks),/remove)
   times=anytim(temporary(times),/TAI)             ; ->TAI
   if generate then begin
      box_message,'Updating pfss catalog'
      savegenx, times, links, sswlinks, file=bfieldcat  , /overwrite
      box_message,'Making ssw links...
      linkdir='/net/diapason/www1/htdocs/solarsoft/pfss_links
      sswfull=concat_dir(linkdir,sswlinks)
      pfssfull=concat_dir(concat_dir(pfss_path,date_sub),links)
      for i=0,n_elements(links)-1 do begin 
        if not file_exist(sswfull(i)) then $
           spawn,['ln','-s',pfssfull(i),sswfull(i)],/noshell ; shouldn't need but  
      endfor
   endif
endif

case n_params() of 
   1: begin
         t0=anytim(time0,/TAI)
         t1=t0         
   endcase
   2: begin
         t0=anytim(time0,/TAI)
         t1=anytim(time1,/TAI)
   endcase
   else: begin
      t0=times(0)
      t1=last_nelem(times)
   endcase
endcase

dtimes=times-t0
if n_params() eq 1 then begin 
   abdt=abs(dtimes)
   ss=(where(abdt eq min(abdt)))(0)
endif else begin 
   ss=where(times ge t0 and times le t1,count)
endelse

if ss(0) ne -1 then begin 
   count=n_elements(retval)
   case 1 of
      urls: retval=pfss_http + '/' + sswlinks(ss) 
      else: retval=concat_dir(concat_dir(pfss_path,date_sub),links(ss))
   endcase
endif else retval=''
 
return, retval
end
  


