pro tim2dbase, time0, time1, fids=fids, weekids=weekids, mos=mos, $
               umos=umos, uweeks=uweeks, infile=infile, hc=hc, fidmap=fidmap, $
	       debug=debug, display=display
;+
;   Name: tim2dbase
;
;   Purpose: return fileid, weekid & moname for input times
;
;   Input Parameters:
;      time0 - time(s) vector or start time of a range (if time1 defined)
;      time1 - optional stop time - if supplied, same dimension as time0
;              and interpreted as stop time of range.
;
;   Optional Keyword Parameters:
;      infile - May be used in place of input time0/1 - file which contains
;               input times in Yohkoh string standard may contain one or two 
;		times per line (first entries in ascii file)
;
;      hc   -    (input)  if set, produce hardcopy
;      fids -    (output) fileids corresponding to input times  (YYMMDD.HHMM)
;      weekids - (output) weekids corresponding to input times  (YY_WW)
;      mos  -    (output) monames coresponding to 	        (xxxN)
;      umos -    (output) uniq mos				uniq mo names
;      uweekid   (output) uniq weekids				uniq weekids
;
;   Calling Sequence:
;      OPTION 1 - supply time vector - output parameters will be same dimension
;
;         tim2dbase,times, fids=fids, weekids=weekids, mos=mos
;
;      OPTION 2 - suppy time range(s) - output parmeter dimensions may be 
;                 different from from input dimensions; spanning orbits implies
;                 spanning fileids and possibly spanning weekids and mos
;
;         tim2dbase, starttimes, stoptimes, fids=fids, weekid=weekid, mos=mos
;
;   Applications:
;      1) given list of event times (ex: flares), identify the corresponding
;         fileids, tapes, and/or MOs required for access
; 
;   History:
;      2-Dec-1994 (SLF) - to simplify various repeated tasks 
;
;   Restrictions:
;      mos - refer to the the ISAS MO data base so site specific
;-
debug=keyword_set(debug)
; --------- Times from INFILE ? Define times0/times1 ---------------
if keyword_set(infile) then begin
   if not file_exist(infile) then begin
      tbeep
      message,/info,"Cannot find input time file: " + infile + ", returning..." 
      return
   endif

   intimes=rd_tfile(infile)			; read time file
   coldata=str2cols(intimes,' ',ncols=ncols)	; break into columns
   if ncols eq 1 then times0=fmt_tim(reform(coldata(0,*))) else begin
      times0=fmt_tim(reform(coldata(0,*)) + ' ' + reform(coldata(1,*)))
      if ncols ge 4 then $
         times1=fmt_tim(reform(coldata(2,*)) + ' ' + reform(coldata(3,*)))
   endelse
endif else begin
   times0=fmt_tim(time0)
   if n_elements(time1) gt 0 then times1 = fmt_tim(time1)
endelse
; -----------------------------------------------------------------

ntimes=n_elements(times0)

; -------- now summarize dbase info from low level routines --------
if n_elements(times1) eq 0 then begin
   weekids=strmid(anytim2weekid(times0,/string),0,5)	; find weekids
   fids=anytim2fid(times0)			; find fids
   mos=fid2mo(fids)				; find mos
   fidmap=lindgen(ntimes)
endif else begin				; time ranges 
;  -- Time range logic; may span orbits, so not 1:1 with times -----
;  do fileid list first to expand to minimum granularity
   allfids=''
   fidmap=lonarr(ntimes)
   for i=0,n_elements(times0)-1 do begin
      fidlist=anytim2fid(times0(i),times1(i))   ; find fids (may be > 1:1)
      allfids=[allfids,fidlist]
      fidmap(i)=n_elements(fidlist) + total(fidmap) ; pointers to times
   endfor
   fids=temporary(allfids(1:*))			; eliminate initial dummy
   weekids=strmid(anytim2weekid(fid2ex(fids),/string),0,5)		; find weekids
   mos=fid2mo(fids)				; find mos
   fidmap=[0,fidmap]
   fidmap=fidmap(0:(n_elements(fidmap)-2) >0)
endelse

; find uniq mo and week sets			; useful for knowing which
order=sort([fids])				; MOS and Tapes are required
umos=mos(uniq(mos,order))
uweeks=weekids(uniq(weekids,order))
;

; produce Hardcopy listing on request
if keyword_set(hc) or keyword_set(display) then begin
   ofile=concat_dir(get_logenv('HOME'),'tim2dbase.doc')
   message,/info,"Writing summary file: " + ofile
;  buffer output (handle cases where time ranges span fids)
   otimes0=strarr(n_elements(fids))			; for concatenation
   otimes0(*)=string(replicate(32b,19))			; fill to lenght
   otimes1=otimes0
   otimes0(fidmap)=times0				; use pointers to fill
   if n_elements(times1) ne 0 then otimes1(fidmap)=times1 else otimes1(*)=''
;  header format depends on time OR Time range
   header=([$
   ' INPUT TIMES           FILEID         WEEKID  MO#',  $
   ; 1-FEB-93  12:30:00    930201.1228    93_06a   052B
   ' START TIME          STOP TIME            FILEID         WEEKID  MO#'])(n_elements(times1) gt 0)
   ; 1-FEB-93  00:00:00  2-FEB-93  00:00:00   930131.2329    93_06a   052B
   file_append,ofile,['TIM2DBASE summary run on ' + systime()],/new
;  print summary of uniq MOS and WEEKIDs
   line=string(replicate(45b,80))
   file_append,ofile,$
      ['','WEEKIDs referenced...','',  $
      string(uweeks,format="(10a7)"),'','MOs referenced...','', $
      string(umos,format="(10a7)"),'']
;  Now write out info for every FID
   odata= otimes0 + ' ' + otimes1 + '   ' + fids + '    ' + weekids + '   ' + mos
   file_append, ofile, [line,header,'',odata, line]
;  display to terminal
   if keyword_set(display) then $
      prstr,['','------------ ' + ofile + ' -----------','',rd_tfile(ofile)]
endif

if debug then stop
return
end
