gavo.helpers.fitstricks module

Helpers for manipulating FITS files.

In contrast to fitstools, this is not for online processing or import of files, but just for the manipulation before processing.

Rough guideline: if it’s about writing fixed fits files, it probably belongs here, otherwise it goes to fitstools.

Realistically, this module has been hemorraghing functions to fitstools and probably should be removed completely.

One important function it has grown is FITS header templates. These can be used by processors. If these use custom templates, they should register them (or regret it later). See registerTemplate’s docstring.

gavo.helpers.fitstricks.addHistoryCard(header, entry, recognizer)[source]

adds a history card to header, overwriting a previous version if it’s present.

This will reject entries longer than 72 characters, as these would create cruft on overwriting. Since we always prepend today’s date to entry, the net payload size is 61 characters.

regcognizer is a string that is searched within the history string. If it is found, entry is put into the card. If none such card is found, a new history card is written.

This is mainly for processors; in particular during development, but quite likely also when reprocessing, you don’t want extra history entries each time the processor runs (yeah, there are situations when you would want to know about reprocessing, but weight these against the horrible cruft, I know what I want.

gavo.helpers.fitstricks.copyFields(header, cardList, ignoredHeaders=frozenset({'bitpix', 'datamax', 'datamin', 'date', 'imageh', 'imagew', 'naxis', 'naxis1', 'naxis2', 'simple'}))[source]

copies over all cards from cardList into header, excluding headers named in ignoredHeaders.

ignoredHeaders must be all lowercase.


returns the “normal” key-value pairs from hdr in a dictionary.

Comment, history and blank cards are excluded; the comments from the cards are lost, too.


returns the name under which the FITS template has been registered.


returns the FITS template sequence for templateName.

A NotFoundError is raised if no such template exists.


returns the template name used for generating hdr.

A ReportableError is raised if the info signature is missing.

gavo.helpers.fitstricks.makeHeaderFromTemplate(template, originalHeader=None, **values)[source]

returns a new pyfits.Header from template with values filled in.

template usually is the name of a template previously registered with registerTemplate, or one of DaCHS predefined template names (currently, minimal and wfpdb). In a pinch, you can also pass in an immediate headers.

originalHeader can be a pre-existing header; the history and comment cards are copied over from it, and if any of its other cards have not yet been added to the header, they will be added in the order that they apprear there.

values for which no template item is given are added in random order after the template unless an originalHeader is passed. In that case, they are assumed to originate there and are ignored.

gavo.helpers.fitstricks.registerTemplate(templateName, template)[source]

registers a named FITS header template.

Registering lets DaCHS figure out the template from a history entry it leaves, so it’s certainly a good idea to do that.

For templateName, use something containing a bit of your signature (e.g., ariAncientPlate rather than just ancientPlate).

gavo.helpers.fitstricks.updateTemplatedHeader(hdr, templateName=None, **kwargs)[source]

return hdr updated with kwargs.

hdr is assumed to have been created with makeHeaderFromTemplate and contain the template name in a history entry.

You can pass in templateName to keep DaCHS from trying to get things from the header.

[It is probably better to use makeHeaderFromTemplate directly, passing in the orginalHeader; that preserves the order of non-templated headers].