function sxi_sxmodpar, Old_Header, Name, Value, Comment, Location, before=before, $
                                       after=after , format=format
 if N_params() LT 3 then begin             ;Need at least 3 parameters
      print,'Syntax - Header = Sxaddpar (Old_Header, Name, Value,'
      print,'              [Comment, Position, BEFORE = ,AFTER = , FORMAT = ])'
      return,-1
 endif

 Header=Old_header

; Define a blank line and the END line

 ENDLINE = 'END' +string(replicate(32b,77)) ;END line
 BLANK = string(replicate(32b,80))         ;BLANK line

;  If Location parameter not defined, set it equal to 'END     '
;
 if ( N_params() GT 4 ) then loc = strupcase(location) else $
 if keyword_set( BEFORE) then loc = strupcase(before) else $
 if keyword_set( AFTER)  then loc = strupcase(after) else $
                             loc = 'END'

 while strlen(loc) lt 8 do loc = loc + ' '

 if N_params() lt 4 then comment = ''      ;Is comment field specified?

 n = N_elements(header)                  ;# of lines in FITS header
 if (n EQ 0) then begin                  ;header defined?
      header=strarr(10)              ;no, make it.
      header(0)=ENDLINE
      n=10
 endif else begin
      s = size(header)               ;check for string type
          if (s(0) ne 1) or (s(2) ne 7) then $
          message,'FITS Header (first parameter) must be a string array'
 endelse

;  Make sure Name is 8 characters long

    nn = string(replicate(32b,8))   ;8 char name
    strput,nn,strupcase(name) ;insert name

;  Extract first 8 characters of each line of header, and locate END line

 keywrd = strmid(header,0,8)                 ;Header keywords
 iend = where(keywrd eq 'END     ',nfound)
 if nfound eq 0 then header(0)=ENDLINE      ;no end, insert at beginning
 iend = iend(0) > 0                          ;Make scalar

 if nn eq 'HISTORY ' then begin             ;add history record?
      if iend lt (n-1) then $
         header(iend+1)=ENDLINE $ ;move end up
      else begin
         header = [header,replicate(blank,5)] ;yes, add 5.
         header(n) = ENDLINE
      endelse
      newline = blank
      strput,newline,nn+string(value),0
      header(iend) = newline         ;add history rec.
      return,header
 endif      ;history

; Find location to insert keyword

 ipos  = where(keywrd eq nn,nfound)
 if nfound gt 0 then begin
         i = ipos(0)
         if comment eq '' then begin
       strt=strpos(header(i),'/')
       if strt gt 0 then istrt=strt+1 else istrt=32
       comment=strmid(header(i),istrt,80-istrt) ;save comment?
       endif
         goto, REPLACE
 endif

 if loc ne '' then begin
          iloc =  where(keywrd eq loc,nloc)
          if nloc gt 0 then begin
             i = iloc(0)
             if keyword_set(after) and loc ne 'HISTORY ' then i = i+1 < iend
         if i gt 0 then header=[header(0:i-1),blank,header(i:n-1)] $
                else header=[blank,header(0:n-1)]
             goto, REPLACE
      endif
 endif

; At this point keyword and location parameters were not found, so a new
; line is added at the end of the FITS header

    if iend lt (n-1) then begin ;Not found, add more?
        header(iend+1) = ENDLINE    ;no, already long enough.
        i = iend        ;position to add.
       endif else begin     ;must lengthen.
        header = [header,replicate(blank,5)] ;add an element on the end
        header(n)=ENDLINE       ;save "END"
        i =n-1          ;add to end
    end

; Now put value into keyword at line i

REPLACE:
    h=blank         ;80 blanks
    strput,h,nn+'= '    ;insert name and =.
    apost = "'"         ;quote a quote
    type = size(value)  ;get type of value parameter
    if type(0) ne 0 then $
        message,'Keyword Value (third parameter) must be scalar'

    case type(1) of     ;which type?
7:  begin
      upval = strupcase(value)  ;force upper case.
      if (upval eq 'T') or (upval eq 'F') then begin
        strput,h,upval,29  ;insert logical value.
        end else begin      ;other string?
        if strlen(value) gt 18 then begin   ;long string
            strput,h,apost+value+apost+' /'+comment,10
            header(i)=h
            return,header
        endif
        strput,h,apost+strmid(value,0,18)+apost,10 ;insert string val
      endelse
      endcase
 else:  begin
    if (N_elements(format) eq 1) then $            ;use format keyword
        v = string(value,'('+format+')' ) else $
        v = strtrim(value,2)      ;convert to string, default format
    s = strlen(v)                 ;right justify
        strput,h,v,(30-s)>10          ;insert
    end
 endcase

 strput,h,' /',30   ;add ' /'
 strput, h, comment, 32 ;add comment
 header(i) = h      ;save line

 return,header
 en