function sxipng_hel2arcs, latitude, longitude, filename


; the common block is only used to make these variables persist
; they are not shared with anything
common sxi_hel2arcs_common, date_obs, sunr, b0

; Guarantee that the heliographic coordinates are in an acceptable range
  if (latitude lt -90 or latitude gt 90) then return, -1
  if (longitude lt -90 or longitude gt 90) then return, -1

; here's the pre-defined struct
  template = {date_obs:'', $
              exptime:0.0, $
              wavelnth:'', $
              crota:0d, $
              cdelt1: 0l, $
              cdelt2: 0l, $
              naxis1: 0l, $
              naxis2: 0l, $
              xcen: 0d, $
              ycen: 0d}

; here's the call with the user supplied template
  index = template
  yearstr = strmid(filename,4,4)
  monthstr = strmid(filename,8,2)
  daystr = strmid(filename,10,2)
  hrstr = strmid(filename,13,2)
  minstr = strmid(filename,15,2)
  secstr = strmid(filename,17,2)
  msecstr = strmid(filename,19,3)
  index.date_obs = yearstr + '-' + monthstr + '-' + daystr + ' ' + $
                   hrstr + ":" + minstr + ":" + secstr + '.' + msecstr
  index.naxis1 = 512
  index.naxis2 = 512
  index.crota = 0
  index.cdelt1 =  5.0 
  index.cdelt2 =  5.0
  index.xcen =  255.5 + 63
  index.ycen =  255.5 + 63

; Do we need to run ephemeris again ? 
; flag = 0 means no, flag = 1 means yes
  flag = 1

; is date_obs defined ?
  if (keyword_set(date_obs)) then begin
    if (index.date_obs eq date_obs) then flag = 0
  endif else date_obs = index.date_obs

; Must know solar radius in arcseconds and solar B angle for date/time of image
; see if it has to be recalculated
  if (flag eq 1) then begin
    ans=get_rb0p(index.date_obs) ; ephemeris for image date/time: Radius in arcsecs
    sunr = ans(0)  ; solar radius in arcsecs
    b0 = ans(1) ; b-angle
  endif

; Extract the key information from index
  exposure = index.exptime ; image integration time in seconds
  filter = index.wavelnth  ; the filter used for this image
  roll = -index.crota      ; CROTA is degrees counterclockwise of solar north relative to CCD up
  pix_size = index.cdelt1  ; pixel size in arc-seconds

; Find the center of Sun in pixel coordinates
; [index.xcen, index.ycen] ; Center of the image relative to sun center in arcseconds
  sunx = 1.0*index.naxis1/2.0 - float(index.xcen)/float(index.cdelt1)
  suny = 1.0*index.naxis2/2.0 - float(index.ycen)/float(index.cdelt2)

; for level-1 pngs, suncenter is always in a fixed location 
  suncenter = [255.5+63, 255.5]

; Convert longitude to radians (phi in SPC)
  lon = longitude/!radeg 

; Convert latitude to co-latitude in radians (theta in SPC)
  colat = (90 - latitude)/!radeg 

; vect is the (x,y,z) location of the point for b0 = 0, where x is in the
; direction of the observer (towards Earth), y is west, and z is north. 
; vect1 is rotated by b0. 
  vect = dblarr(3)
  vect(0) = sin(colat)*cos(lon)         ;back to xyz
  vect(1) = sin(colat)*sin(lon)
  vect(2) = cos(colat)

; correct for B0
  rotmx = [[cos(b0),0.0,-sin(b0)],[0.0,1.0,0.0],[sin(b0),0.0,cos(b0)]]
  vect = rotmx # vect
  vect = vect*sunr

; return the result (note that some points may have vect(0) lt 0, which
; are behind the limb - which I am going to ignore for now)
  return, vect(1:2)

end