[AstroPy] Re: functional interface for PyFITS?

Victoria G. Laidler laidler at stsci.edu
Mon Mar 21 16:57:40 CST 2005

Perry Greenfield wrote:

> Vicki Laidler has persuaded me that PyFITS should have a more 
> functional interface as well as it's more object-oriented current 
> interface. The gist of the argument is that many astronomers would 
> like a simple call to get data or a header and not mess with opening, 
> then digging down in the object structure to get at the data. I find 
> that hard to argue with. Here are some proposed functions
> getdata(filename, [ext, extname, extver, etc])
> getheader(filename, [ext, extname, extver, etc])
> getdataheader( [ext, extname, extver, etc]) # returns the data and 
> header as a tuple
>    # i.e., im, hdr = pyfits.getdataheader('mydata.fits')
>    # Shorter name better? getdatahdr? getdh?

Can I suggest dropping "getdataheader" and replacing it with a keyword 
argument to getdata?

Will the filename syntax support specifying the extension that way? (I 
forget whether pyfits already does this)
img=getdata('foo.fits[2]')  for the 2nd extension, as a valid alternative to

> The defaults for these functions are the primary header if it contains 
> data, if not the first extension. Otherwise the user must select the 
> extension desired. 

Sounds good.

> The semantics are that these are file "atomic" operations. The file is 
> opened, the data and/or the header are read, and then the file is 
> closed. Memory mapped mode will not be supported (at least initially).
> I don't see why it should not be possible to use a functional 
> interface for writing and updates as well. Likewise, they open and 
> close the specified file.
> writeto(filename, data, hdr) # simple fits file is created
> append(filename, data, hdr,) # appends if exists, created if it doesn't
> update(filename, data, hdr, {ext or extname/extver required})
> info(filename) # to print information on a FITS file 

These sound great. "writeto" should create a minimal header if none is 

I like Peter's suggested addition too, with a couple of comments:

>>>>/  getheadervalue(filename, keyword, ...)  # returns header value as a string/
How about pyfits.getval(filename,keyword, ...) for its name?

And please, how about returning the keyword value in a properly typed 
variable? The data type is encoded in the FITS header after all! Might 
as well make use of it,instead of making the user float it, int it, or 

Vicki Laidler

More information about the AstroPy mailing list