PrimaryHDU¶
- class astropy.io.fits.PrimaryHDU(data=None, header=None, do_not_scale_image_data=False, ignore_blank=False, uint=True, scale_back=None)[source]¶
Bases:
_ImageBaseHDUFITS primary HDU class.
Construct a primary HDU.
- Parameters:
- data
arrayorastropy.io.fits.hdu.base.DELAYED, optional The data in the HDU.
- header
Header, optional The header to be used (as a template). If
headerisNone, a minimal header will be provided.- do_not_scale_image_databool, optional
If
True, image data is not scaled using BSCALE/BZERO values when read. (default: False)- ignore_blankbool, optional
If
True, the BLANK header keyword will be ignored if present. Otherwise, pixels equal to this value will be replaced with NaNs. (default: False)- uintbool, optional
Interpret signed integer data where
BZEROis the central value andBSCALE == 1as unsigned integer data. For example,int16data withBZERO = 32768andBSCALE = 1would be treated asuint16data. (default: True)- scale_backbool, optional
If
True, when saving changes to a file that contained scaled image data, restore the data to the original type and reapply the original BSCALE/BZERO values. This could lead to loss of accuracy if scaling back to integer values after performing floating point operations on the data. Pseudo-unsigned integers are automatically rescaled unless scale_back is explicitly set toFalse. (default: None)
- data
- add_checksum(when=None, override_datasum=False, checksum_keyword='CHECKSUM', datasum_keyword='DATASUM')¶
Add the
CHECKSUMandDATASUMcards to this HDU with the values set to the checksum calculated for the HDU and the data respectively. The addition of theDATASUMcard may be overridden.- Parameters:
- when
str, optional comment string for the cards; by default the comments will represent the time when the checksum was calculated
- override_datasumbool, optional
add the
CHECKSUMcard only- checksum_keyword
str, optional The name of the header keyword to store the checksum value in; this is typically ‘CHECKSUM’ per convention, but there exist use cases in which a different keyword should be used
- datasum_keyword
str, optional See
checksum_keyword
- when
Notes
For testing purposes, first call
add_datasumwith awhenargument, then calladd_checksumwith awhenargument andoverride_datasumset toTrue. This will provide consistent comments for both cards and enable the generation of aCHECKSUMcard with a consistent value.
- add_datasum(when=None, datasum_keyword='DATASUM')¶
Add the
DATASUMcard to this HDU with the value set to the checksum calculated for the data.- Parameters:
- when
str, optional Comment string for the card that by default represents the time when the checksum was calculated
- datasum_keyword
str, optional The name of the header keyword to store the datasum value in; this is typically ‘DATASUM’ per convention, but there exist use cases in which a different keyword should be used
- when
- Returns:
- checksum
int The calculated datasum
- checksum
Notes
For testing purposes, provide a
whenargument to enable the comment value in the card to remain consistent. This will enable the generation of aCHECKSUMcard with a consistent value.
- copy()¶
Make a copy of the HDU, both header and data are copied.
- property data¶
Image/array data as a
ndarray.Please remember that the order of axes on an Numpy array are opposite of the order specified in the FITS file. For example for a 2D image the “rows” or y-axis are the first dimension, and the “columns” or x-axis are the second dimension.
If the data is scaled using the BZERO and BSCALE parameters, this attribute returns the data scaled to its physical values unless the file was opened with
do_not_scale_image_data=True.
- filebytes()¶
Calculates and returns the number of bytes that this HDU will write to a file.
- fileinfo()¶
Returns a dictionary detailing information about the locations of this HDU within any associated file. The values are only valid after a read or write of the associated file with no intervening changes to the
HDUList.- Returns:
dictorNoneThe dictionary details information about the locations of this HDU within an associated file. Returns
Nonewhen the HDU is not associated with a file.Dictionary contents:
Key
Value
file
File object associated with the HDU
filemode
Mode in which the file was opened (readonly, copyonwrite, update, append, ostream)
hdrLoc
Starting byte location of header in file
datLoc
Starting byte location of data block in file
datSpan
Data size including padding
- classmethod fromstring(data, checksum=False, ignore_missing_end=False, **kwargs)¶
Creates a new HDU object of the appropriate type from a string containing the HDU’s entire header and, optionally, its data.
Note: When creating a new HDU from a string without a backing file object, the data of that HDU may be read-only. It depends on whether the underlying string was an immutable Python str/bytes object, or some kind of read-write memory buffer such as a
memoryview.- Parameters:
- data
str,bytes,memoryview,ndarray A byte string containing the HDU’s header and data.
- checksumbool, optional
Check the HDU’s checksum and/or datasum.
- ignore_missing_endbool, optional
Ignore a missing end card in the header data. Note that without the end card the end of the header may be ambiguous and resulted in a corrupt HDU. In this case the assumption is that the first 2880 block that does not begin with valid FITS header data is the beginning of the data.
- **kwargsoptional
May consist of additional keyword arguments specific to an HDU type–these correspond to keywords recognized by the constructors of different HDU classes such as
PrimaryHDU,ImageHDU, orBinTableHDU. Any unrecognized keyword arguments are simply ignored.
- data
- classmethod match_header(header)[source]¶
_ImageBaseHDU is sort of an abstract class for HDUs containing image data (as opposed to table data) and should never be used directly.
- classmethod readfrom(fileobj, checksum=False, ignore_missing_end=False, **kwargs)¶
Read the HDU from a file. Normally an HDU should be opened with
open()which reads the entire HDU list in a FITS file. But this method is still provided for symmetry withwriteto().- Parameters:
- fileobjfile-like object
Input FITS file. The file’s seek pointer is assumed to be at the beginning of the HDU.
- checksumbool
If
True, verifies that bothDATASUMandCHECKSUMcard values (when present in the HDU header) match the header and data of all HDU’s in the file.- ignore_missing_endbool
Do not issue an exception when opening a file that is missing an
ENDcard in the last header.
- req_cards(keyword, pos, test, fix_value, option, errlist)¶
Check the existence, location, and value of a required
Card.- Parameters:
- keyword
str The keyword to validate
- pos
int,callable() If an
int, this specifies the exact location this card should have in the header. Remember that Python is zero-indexed, so this meanspos=0requires the card to be the first card in the header. If given a callable, it should take one argument–the actual position of the keyword–and returnTrueorFalse. This can be used for custom evaluation. For example ifpos=lambda idx: idx > 10this will check that the keyword’s index is greater than 10.- test
callable() This should be a callable (generally a function) that is passed the value of the given keyword and returns
TrueorFalse. This can be used to validate the value associated with the given keyword.- fix_value
str,int,float,complex, bool,None A valid value for a FITS keyword to to use if the given
testfails to replace an invalid value. In other words, this provides a default value to use as a replacement if the keyword’s current value is invalid. IfNone, there is no replacement value and the keyword is unfixable.- option
str Output verification option. Must be one of
"fix","silentfix","ignore","warn", or"exception". May also be any combination of"fix"or"silentfix"with"+ignore",+warn, or+exception" (e.g. ``"fix+warn"). See Verification Options for more info.- errlist
list A list of validation errors already found in the FITS file; this is used primarily for the validation system to collect errors across multiple HDUs and multiple calls to
req_cards.
- keyword
Notes
If
pos=None, the card can be anywhere in the header. If the card does not exist, the new card will have thefix_valueas its value when created. Also check the card’s value by using thetestargument.
- run_option(option='warn', err_text='', fix_text='Fixed.', fix=None, fixable=True)¶
Execute the verification with selected option.
- scale(type=None, option='old', bscale=None, bzero=None)¶
Scale image data by using
BSCALE/BZERO.Call to this method will scale
dataand update the keywords ofBSCALEandBZEROin the HDU’s header. This method should only be used right before writing to the output file, as the data will be scaled and is therefore not very usable after the call.- Parameters:
- type
str, optional destination data type, use a string representing a numpy dtype name, (e.g.
'uint8','int16','float32'etc.). If isNone, use the current data type.- option
str, optional How to scale the data:
"old"uses the originalBSCALEandBZEROvalues from when the data was read/created (defaulting to 1 and 0 if they don’t exist). For integer data only,"minmax"uses the minimum and maximum of the data to scale. User-specifiedbscale/bzerovalues always take precedence.- bscale, bzero
int, optional User-specified
BSCALEandBZEROvalues
- type
- property section¶
Access a section of the image array without loading the entire array into memory. The
Sectionobject returned by this attribute is not meant to be used directly by itself. Rather, slices of the section return the appropriate slice of the data, and loads only that section into memory.Sections are useful for retrieving a small subset of data from a remote file that has been opened with the
use_fsspec=Trueparameter. For example, you can use this feature to download a small cutout from a large FITS image hosted in the Amazon S3 cloud (see the Working with remote and cloud-hosted files section of the Astropy documentation for more details.)For local files, sections are mostly obsoleted by memmap support, but should still be used to deal with very large scaled images.
Note that sections cannot currently be written to. Moreover, any in-memory updates to the image’s
.dataproperty may not be reflected in the slices obtained via.section. See the Data Sections section of the documentation for more details.
- property shape¶
Shape of the image array–should be equivalent to
self.data.shape.
- property size¶
Size (in bytes) of the data portion of the HDU.
- verify(option='warn')¶
Verify all values in the instance.
- Parameters:
- option
str Output verification option. Must be one of
"fix","silentfix","ignore","warn", or"exception". May also be any combination of"fix"or"silentfix"with"+ignore","+warn", or"+exception"(e.g."fix+warn"). See Verification Options for more info.
- option
- verify_checksum()¶
Verify that the value in the
CHECKSUMkeyword matches the value calculated for the current HDU CHECKSUM.- Returns:
- valid
int 0 - failure
1 - success
2 - no
CHECKSUMkeyword present
- valid
- verify_datasum()¶
Verify that the value in the
DATASUMkeyword matches the value calculated for theDATASUMof the current HDU data.- Returns:
- valid
int 0 - failure
1 - success
2 - no
DATASUMkeyword present
- valid
- writeto(name, output_verify='exception', overwrite=False, checksum=False)¶
Write the HDU to a new file. This is a convenience method to provide a user easier output interface if only one HDU needs to be written to a file.
- Parameters:
- namepath-like object or file-like object
Output FITS file. If the file object is already opened, it must be opened in a writeable mode.
- output_verify
str Output verification option. Must be one of
"fix","silentfix","ignore","warn", or"exception". May also be any combination of"fix"or"silentfix"with"+ignore","+warn", or"+exception"(e.g."fix+warn"). See Verification Options for more info.- overwritebool, optional
If
True, overwrite the output file if it exists. Raises anOSErrorifFalseand the output file exists. Default isFalse.- checksumbool
When
Trueadds bothDATASUMandCHECKSUMcards to the header of the HDU when written to the file.