1 from typing
import Union, Optional
2 from io
import StringIO
4 from robot.api
import logger
6 from astropy.io
import fits
8 MaybeHdu = Union[fits.HDUList, str]
12 """Robot library providing FITS related keywords"""
14 ROBOT_LIBRARY_SCOPE =
"GLOBAL"
16 def _get_hdu(self, arg: MaybeHdu):
17 if isinstance(arg, str):
22 self, path_or_hdu: MaybeHdu, with_checksum: Optional[bool] =
None
24 """Returns list of keywords from primary HDU as a list of pairs
25 [["KEYWORD": "VALUE"], ...]
29 raise Exception(
"FITS file contains no HDUs")
31 return [list(tup)
for tup
in hdul[0].header.items()]
32 redact = [
"CHECKSUM",
"DATASUM",
"ARCFILE"]
33 kws = [list(tup)
for tup
in hdul[0].header.items()]
42 """Returns list of keywords from primary HDU as a list of pairs"""
45 raise Exception(
"FITS file contains no HDUs")
48 return [(hdu.header.get(
"EXTNAME"))
for hdu
in hdul[1:]]
54 hdul = fits.HDUList([fits.PrimaryHDU()])
59 hdul = fits.HDUList([fits.PrimaryHDU(np.ones((100, 100)))])
70 self, path_or_hdu: MaybeHdu, *, name, value, comment=
"", hdu_num=0
75 "Setting keyword in hdu %s: %s=%s (%s)" % (hdu_num, name, value, comment)
77 hdul[hdu_num].header.set(name, value, comment)
83 logger.info(
"Setting extname %s" % name)
84 hdul.append(fits.ImageHDU(np.ones((100, 100)), name=name))
90 c1 = fits.Column(name=
"a", array=np.array([1, 2]), format=
"K")
91 c2 = fits.Column(name=
"b", array=np.array([4, 5]), format=
"K")
92 c3 = fits.Column(name=
"c", array=np.array([7, 8]), format=
"K")
93 ext = fits.BinTableHDU.from_columns([c1, c2, c3])
94 logger.info(
"Setting extname %s" % name)
107 Executes astropy verify() verify_checksum() and verify_datasum()
110 hdul.verify(
"exception")
111 mapping = {0:
"FAILURE", 1:
"SUCCESS", 2:
"ABSENT"}
113 res = hdu.verify_checksum()
115 raise AssertionError(
116 "FITS checksum verification failed. Expected '%s' but got '%s"
117 % (mapping[res], expected)
119 res = hdu.verify_datasum()
121 raise AssertionError(
122 "FITS datsum verification failed. Expected '%s' but got '%s"
123 % (mapping[res], expected)
128 path_or_hdu: MaybeHdu,
129 hdu_num: Union[str, int],
130 expected: Optional[str] =
None,
132 """Verify checksum"""
133 hdu_num = int(hdu_num)
134 mapping = {0:
"FAILURE", 1:
"SUCCESS", 2:
"ABSENT"}
137 res = hdu.verify_checksum()
138 res_str = mapping[res]
139 if expected
is not None:
140 raise AssertionError(
141 "FITS checksum verification failed. Expected '%s' but got '%s"
142 % (res_str, expected)
148 path_or_hdu: MaybeHdu,
149 hdu_num: Union[str, int],
150 expected: Optional[str] =
None,
153 mapping = {0:
"FAILURE", 1:
"SUCCESS", 2:
"ABSENT"}
156 res = hdu.verify_datasum()
157 res_str = mapping[res]
158 if expected
is not None:
159 raise AssertionError(
160 "FITS datasum verification failed. Expected '%s' but got '%s"
161 % (res_str, expected)
Robot library providing FITS related keywords.
def fits_add_image_ext(self, MaybeHdu path_or_hdu, *name)
def fits_verify(self, MaybeHdu path_or_hdu)
Executes astropy verify() verify_checksum() and verify_datasum()
def fits_write_to(self, MaybeHdu path_or_hdu, str path)
def fits_set_keyword(self, MaybeHdu path_or_hdu, *name, value, comment="", hdu_num=0)
def fits_close(self, fits.HDUList hdul)
def fits_get_hdu_ext_names(self, MaybeHdu path_or_hdu)
Returns list of keywords from primary HDU as a list of pairs.
def fits_create_empty(self)
def _get_hdu(self, MaybeHdu arg)
def fits_verify_checksum(self, MaybeHdu path_or_hdu, Union[str, int] hdu_num, Optional[str] expected=None)
Verify checksum.
def fits_add_bintable_ext(self, MaybeHdu path_or_hdu, *name)
def fits_create_image(self)
def fits_get_info(self, MaybeHdu path_or_hdu)
def fits_read_primary_hdu(self, MaybeHdu path_or_hdu, Optional[bool] with_checksum=None)
Returns list of keywords from primary HDU as a list of pairs [["KEYWORD": "VALUE"],...
def fits_verify_datasum(self, MaybeHdu path_or_hdu, Union[str, int] hdu_num, Optional[str] expected=None)
Verify datasum.