JuliaAstro / FITSIO.jl

Flexible Image Transport System (FITS) file support for Julia
http://juliaastro.org/FITSIO.jl/
MIT License
55 stars 29 forks source link

Standard header comment duplicated on re-save #126

Open bensetterholm opened 4 years ago

bensetterholm commented 4 years ago

When I write to a new FITS file using a FITSHeader object extracted from a previously opened FITS file, the comment block

COMMENT   FITS (Flexible Image Transport System) format is defined in 'Astronomy
COMMENT   and Astrophysics', volume 376, page 359; bibcode: 2001A&A...376..359H

is duplicated. For example:

julia> using FITSIO

julia> # Create a dummy file

julia> hdr = FITSHeader(["HELLO"], ["World"], [""]); data = rand(10,10);

julia> FITS("helloworld.fits", "w") do file
           write(file, data, header=hdr)
       end

shell> dfits helloworld.fits
====> file helloworld.fits (main) <====
SIMPLE  =                    T / file does conform to FITS standard
BITPIX  =                  -64 / number of bits per data pixel
NAXIS   =                    2 / number of data axes
NAXIS1  =                   10 / length of data axis 1
NAXIS2  =                   10 / length of data axis 2
EXTEND  =                    T / FITS dataset may contain extensions
COMMENT   FITS (Flexible Image Transport System) format is defined in 'Astronomy
COMMENT   and Astrophysics', volume 376, page 359; bibcode: 2001A&A...376..359H
HELLO   = 'World   '
END

julia> # Read in the header of the dummy file

julia> hdr2 = FITS("helloworld.fits", "r") do file
           read_header(file[1])
       end
SIMPLE  =                    T / file does conform to FITS standard
BITPIX  =                  -64 / number of bits per data pixel
NAXIS   =                    2 / number of data axes
NAXIS1  =                   10 / length of data axis 1
NAXIS2  =                   10 / length of data axis 2
EXTEND  =                    T / FITS dataset may contain extensions
COMMENT   FITS (Flexible Image Transport System) format is defined in 'Astronom
COMMENT   and Astrophysics', volume 376, page 359; bibcode: 2001A&A...376..359H
HELLO   =              'World'

julia> # Write a new file with the old header

julia> FITS("helloagain.fits", "w") do file
           write(file, data, header=hdr2)
       end

shell> dfits helloagain.fits
====> file helloagain.fits (main) <====
SIMPLE  =                    T / file does conform to FITS standard
BITPIX  =                  -64 / number of bits per data pixel
NAXIS   =                    2 / number of data axes
NAXIS1  =                   10 / length of data axis 1
NAXIS2  =                   10 / length of data axis 2
EXTEND  =                    T / FITS dataset may contain extensions
COMMENT   FITS (Flexible Image Transport System) format is defined in 'Astronomy
COMMENT   and Astrophysics', volume 376, page 359; bibcode: 2001A&A...376..359H
COMMENT   FITS (Flexible Image Transport System) format is defined in 'Astronomy
COMMENT   and Astrophysics', volume 376, page 359; bibcode: 2001A&A...376..359H
HELLO   = 'World   '
END

I also note that the string representation of the FITSHeader object cuts off the final "y" in the first COMMENT card.

giordano commented 4 years ago

Wow, thanks for the great reproducible example!

My understanding is that the problem is that the comment extends over multiple keywords. Libcfitsio.fits_write_comment calls ffpcom from CFITSIO, which, according to the documentation,

If the comment string is too long to fit on a single keyword (72 chars) then it will automatically be continued on multiple CONTINUE keywords.

When fits_write_header iterates over all keywords, it writes the multi-keyword comment multiple lines.

@kbarbary any idea how to best deal with this?