astrogo / fitsio

fitsio is a pure-Go package to read and write `FITS` files
BSD 3-Clause "New" or "Revised" License
53 stars 24 forks source link

Creating an empty fits file #44

Closed TEtourneau closed 6 years ago

TEtourneau commented 6 years ago

Hi,

I would like to create empty fits files, but I didn't manage to do it : I got the 252 status.

Here is what I do :

from fitsio import FITS

fits = FITS('test.fits', 'rw', clobber=True)
fits.close()
fits = FITS('test.fits')
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-41-d0bdd6cc9830> in <module>()
----> 1 fits = FITS('test.fits')

/usr/local/anaconda3/envs/py27/lib/python2.7/site-packages/fitsio/fitslib.pyc in __init__(self, filename, mode, **keys)
    351                 raise IOError("File not found: '%s'" % filename)
    352 
--> 353         self._FITS =  _fitsio_wrap.FITS(filename, self.intmode, create)
    354 
    355     def close(self):

IOError: FITSIO status = 252: 1st key not SIMPLE or XTENSION
Extension doesn't start with SIMPLE or XTENSION keyword. (ffrhdu)
END
Error while closing HDU number 0 (ffchdu).
Extension doesn't start with SIMPLE or XTENSION keyword. (ffrhdu)
END
ffopen could not interpret primary array header of file: 
test.fits
This does not look like a FITS file.

Any idea on how to do that ?

Thanks in advance, Thomas.

sbinet commented 6 years ago

hi Thomas,

it seems like this is some python code. astrogo/fitsio is all about being able to read and write FITS files with the Go programming language...

did you take a wrong turn at some point ? :)

TEtourneau commented 6 years ago

Indeed I posted this on the wrong github page. There is also a module named fitsio in python. Sorry for the bother,

Thomas

sbinet commented 6 years ago

no worries :)

FYI, here is the equivalent with astrogo/fitsio:

package main

import (
    "log"
    "os"

    "github.com/astrogo/fitsio"
)

func main() {
    create("empty.fits")
    open("empty.fits")
}

func create(fname string) {
    f, err := os.Create(fname)
    if err != nil {
        log.Fatal(err)
    }
    defer f.Close()

    fits, err := fitsio.Create(f)
    if err != nil {
        log.Fatal(err)
    }

    if err := fits.Close(); err != nil {
        log.Fatal(err)
    }

    if err := f.Close(); err != nil {
        log.Fatal(err)
    }
}

func open(fname string) {
    f, err := os.Open(fname)
    if err != nil {
        log.Fatal(err)
    }
    defer f.Close()

    fits, err := fitsio.Open(f)
    if err != nil {
        log.Fatal(err)
    }
    defer fits.Close()

    log.Printf("fits: %v", fits.Name())
    log.Printf("hdus: %v", fits.HDUs())
}