function sxipng_arcs2pix, xarcs, yarcs, filename, deltasecs = deltasecs


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

; Convert the fits header to a struct

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

; 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)
  suncenter = [255.5 + 63, 255.5]

; Convert from roll angle to standard polar angle phi, which is the degrees of
; rotation of solar north COUNTERCLOCKWISE relative to the CCD. Also convert
; to radians
  phi = roll*!dtor

; Convert from arcseconds to pixels, using the pixel size
  x1 = xarcs/pix_size
  y1 = yarcs/pix_size

; Perform the rotation transform from Solar North/West to the pixel CCD frame - in units of pixels
  x2 = x1*cos(phi) - y1*sin(phi)
  y2 = x1*sin(phi) + y1*cos(phi)

; Transform the pixel coordinates using sun center (CCD frame)
; The result will be the unrotated pixel coordinates
  x = x2 + suncenter(0)
  y = y2 + suncenter(1)
  pvals=[x,y]

; If the coordinates need to be rotated
  if (keyword_set(deltasecs)) then begin

  ; Get the heliographic coordinates
    hcoords = sxi_pix2hel(x, y, fitshdr)

  ; Check for off disk points
    if n_elements(hcoords le 1) then begin
      pvals = [x, y] ; don't rotate them
    endif else begin

    ; Calculate the rotation rate (Timothy et al, 1975 & Howard et al, 1984)
      rotrate = 14.552 -2.84 * (sin(hcoords(0)*!dtor))^2d

    ; Calculate the rotation amount
      deltalong = rotrate * deltasecs/86400d
      hcoords(1) = (hcoords(1) + deltalong) < 90.0

    ; Go back from heliographic to pixel coordinates
      pvals = sxi_hel2pix(hcoord(0), hcoord(1), fitshdr)

    endelse

  endif ; rotating the coordinates

; return the result
  return, pvals

end