pro ssw_index2pfss, sswindex, pfssindex, pfssdata , $
   open_color=open_color, closed_color=closed_color, $
   refresh=refresh, force_remote=force_remote,   $
   image_reference=image_reference, $
   nlines=nlines, bbox=bbox, debug=debug, $ 
   lcent=lcent, bcent=bcent,  earth_view=earth_view, $
   _extra=_extra, before=before, after=after
;+
;   Name: ssw_index2pfss
;
;   Purpose: interface from SSW 'index' or 'map' to PFSS derived field lines.
;
;   Input Parameters:
;      sswindex - an ssw 'index' record or Map per D.M.Zarro et al
;
;   Output Parameters:
;     pfssindex - sswindex and pfss parameter derived composite index
;     pfssdata - the 2D field line  projection  
;
;   Keyword Parameters:
;      open_color - if set, value to set OPEN lines
;      closed_color - if set, value to set CLOSED lines
;      _extra - Undefined keywords -> pfss_draw_field via inheritance
;               (mag, width, crop, imsc, thick, drawopen, drawclosed...)
;     before/after - optional switches to force Bfield dbase selection
;                    (closest preceding, closest after) - def is closest
;     bbox - optional subfield for field line restriction tie points.
;            May be specifed as:
;               a) 4 element vector (per pfss_field_start_coord)
;               b) SSW 'index' reference record (FITs header derived for example
;               c) 'map' per DMZarro et al.
;     
;   History:
;      5-December-2003 - S.L.Freeland     
;     20-Jan-2004      - S.L.Freeland - got remote access working
;                                       (via pfss_time2file/pfss_restore)
;      2-Feb-2004 - S.L.Freeland - tracing/rendering pieces 
;     16-Mar-2004 - S.L.Freeland - add /EARTH_VIEW
;;
common pfss_data_block,br,bth,bph,nlat,nlon,nr,lat,lon,rix,theta,phi,l0,b0,$
   now,phiat,phibt,str,stth,stph,ptr,ptth,ptph,nstep,rimage

debug=keyword_set(debug)
earth_view=keyword_set(earth_view)

if n_params() lt 2 then begin 
   box_message,'IDL> ssw_index2pfss,sswindex,pfssindex,pfssdata [,options]'
   return
endif 

pfssf=pfss_time2file(sswindex,/ssw_cat,after=after,before=before)
if pfssf(0) eq '' then begin 
   box_message,'Problem mapping time->pfss field file name'
   return
endif

if not file_exist(pfssf) or keyword_set(force_remote) then begin 
   pfssf=pfss_time2file(sswindex,/ssw_cat,/url)   ; try remote access
endif

pfss_restore,pfssf, refresh=refresh

if not keyword_set(nlines) then nlines=100

if keyword_set(earth_view) then begin 
   lcent=l0 
   bcent=b0
endif

ptemp=!d.name
set_plot,'z'
case 1 of 
   earth_view and n_elements(image_reference) eq 0: $
      bboxx=[((l0-90)+360) mod 360, -90, ((l0+90)+360) mod 360,90]
   n_elements(bbox) eq 0 and n_elements(image_reference) eq 0: $
      bboxx=[280,-90,80,90]                ; no clue, use full sun
   n_elements(bbox) eq 4: bboxx=bbox       ; user supplied bbox
   else: begin 
      case 1 of 
         valid_map(bbox): map2index, bbox, index ; DMZ map -> index
         required_tags(bbox,'crpix1,cdelt1,naxis1'): index=bbox
         required_tags(image_reference,'crpix1,cdelt1,naxis1'): $
             index=image_reference
         else:
      endcase 
      index2fov,index,E,W,N,S,/helio
      E=([E,(E+360) mod 360])(E lt 0)
      W=([W,(W+360) mod 360])(W lt 0)
      bboxx=[E,S,W,N]
   endcase
endcase 
print,bboxx
help,nlines,rix
pfss_field_start_coord, 3, nlines, bbox=bboxx 
help,rix

str(*)=rix(1)       ; ??
pfss_trace_field 
pfss_draw_field,imsc=200,outim=outim,_extra=_extra, lcent=lcent, bcent=bcent

pfssindex=sswindex
pfssdata=temporary(outim)

set_plot,ptemp

if debug then stop


return
end



