| 1 |
""" |
| 2 |
Tests for fits helpers |
| 3 |
|
| 4 |
For FITS binary table tests see formatstest.py |
| 5 |
""" |
| 6 |
|
| 7 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 8 |
#c |
| 9 |
#c This program is free software, covered by the GNU GPL. See the |
| 10 |
#c COPYING file in the source distribution. |
| 11 |
|
| 12 |
|
| 13 |
import os |
| 14 |
import re |
| 15 |
import unittest |
| 16 |
|
| 17 |
from gavo.helpers import testhelpers |
| 18 |
|
| 19 |
from gavo import base |
| 20 |
from gavo import utils |
| 21 |
from gavo.helpers import fitstricks |
| 22 |
from gavo.utils import fitstools |
| 23 |
from gavo.utils import pyfits |
| 24 |
|
| 25 |
|
| 26 |
_fitsData = utils.getDirtyBytes( |
| 27 |
b"""eJzt0b0KwjAUhmH/f+7i3IFWZwfFCgEthXboGm0LHZpIUofevaeIuCRIwUm+B76teTnQRFzic0i0 |
| 28 |
I4eUVnTTqtSmttRoOok0IdtIlUuTux4QHUQai8zd2264J42RLeWykdS098Jd+Yj2mUjIc1/XU4/6 |
| 29 |
WhjS5btc1YWylVbW3wvcvWD97RpPb+O9r7cwS8Po6P0f/XtdDAAAAAB+ZvAy5I14Y96EN+XNeHPe |
| 30 |
grfs+R0AAAAAwF96ApwhgT4= |
| 31 |
""") |
| 32 |
|
| 33 |
_nastyHeaderData = utils.getDirtyBytes( |
| 34 |
b""" |
| 35 |
eJztkctOwzAQRX/lrsgGFbpjwyKUQCOah5pQdesm08TIskvsNCpfj90WCaQWqRLsfOSVH2c8d4o4 |
| 36 |
yWcRcI8TlKc2f+chLvN4ecZ3d7kvDZdxgTO+28t90bKM0se/63eWpc+LcGZ9QdlyDbsYhJINtkz0 |
| 37 |
BNMyg4ELgY7ee96RPa6UNFzaw4p1Nbi0lwhrbjSugkmWlnH6aicStMRq6kZAWJmeCbG7dhetQcmK |
| 38 |
QFuSGJgrt+qbQx0lxQ66VQPV6DfWBnzzDa19YQZ1rM8MVxKCS9IYnJbVb6wiadyzs0zjaB7OJ1Pk |
| 39 |
efiCfPw0Rr6I5kWcpS6D8egwkwA3yJOw2Ld1aARb6rQr+YNJliRRWmIfniF9DKtVG1r3tmfUnDVS |
| 40 |
fdjvyZ3NsOlIO4s+5qZdciRq/TVfN1yPx+PxeDyef+QT1lS2XQ== |
| 41 |
""") |
| 42 |
|
| 43 |
|
| 44 |
def getAFITSPath(): |
| 45 |
return os.path.join(base.getConfig("inputsDir"), "data", "ex.fits") |
| 46 |
|
| 47 |
|
| 48 |
class SortHeadersTest(unittest.TestCase): |
| 49 |
"""tests for sortHeaders. |
| 50 |
""" |
| 51 |
def assertHeaderSequence(self, hdr, keySeq): |
| 52 |
for expected, card in zip(keySeq, hdr.cards): |
| 53 |
self.assertEqual(expected, card.keyword) |
| 54 |
|
| 55 |
def testHeadersPreserved(self): |
| 56 |
"""tests for sortHeaders preserving the incoming headers by default. |
| 57 |
""" |
| 58 |
with testhelpers.testFile("test.fits", _fitsData) as ff: |
| 59 |
with pyfits.open(ff) as hdus: |
| 60 |
hdr = fitstools.sortHeaders(hdus[0].header) |
| 61 |
self.assertHeaderSequence(hdr, ["SIMPLE", "BITPIX", "NAXIS", "NAXIS1", |
| 62 |
"NAXIS2", "EXTEND",]) |
| 63 |
|
| 64 |
def testWithCommentsAndHistory(self): |
| 65 |
"""tests for sortHeaders sorting comment and history cards. |
| 66 |
""" |
| 67 |
with testhelpers.testFile("test.fits", _fitsData) as ff: |
| 68 |
with pyfits.open(ff) as hdus: |
| 69 |
hdr = hdus[0].header |
| 70 |
hdr.add_comment("Foo1") |
| 71 |
hdr.add_history("this header added for testing") |
| 72 |
hdr.add_comment("This is at the end.") |
| 73 |
hdr = fitstools.sortHeaders(hdr) |
| 74 |
self.assertHeaderSequence(hdr, ["SIMPLE", "BITPIX", "NAXIS", "NAXIS1", |
| 75 |
"NAXIS2", "EXTEND", "", "HISTORY", "", "COMMENT", "COMMENT"]) |
| 76 |
|
| 77 |
def testCommentFilter(self): |
| 78 |
"""tests for deletion of unwanted comment cards. |
| 79 |
""" |
| 80 |
with testhelpers.testFile("test.fits", _fitsData) as ff: |
| 81 |
with pyfits.open(ff) as hdus: |
| 82 |
hdr = hdus[0].header |
| 83 |
hdr.add_comment("to delete") |
| 84 |
hdr.add_comment("keep this one") |
| 85 |
hdr.add_comment("and delete this") |
| 86 |
hdr.add_comment("and also delete this") |
| 87 |
hdr.add_comment("but keep this") |
| 88 |
hdr = fitstools.sortHeaders(hdr, |
| 89 |
lambda arg: "delete" in arg) |
| 90 |
self.assertHeaderSequence(hdr, ["SIMPLE", "BITPIX", "NAXIS", "NAXIS1", |
| 91 |
"NAXIS2", "EXTEND", "", "COMMENT", "COMMENT"]) |
| 92 |
|
| 93 |
def testOrder(self): |
| 94 |
"""tests for ordering of critical keywords. |
| 95 |
""" |
| 96 |
hdr = pyfits.PrimaryHDU().header |
| 97 |
hdr.set("NAXIS1", 200) |
| 98 |
hdr.set("NAXIS2", 400) |
| 99 |
# EXTEND is now before NAXIS1 |
| 100 |
hdr = fitstools.sortHeaders(hdr) |
| 101 |
self.assertHeaderSequence(hdr, ["SIMPLE", "BITPIX", "NAXIS", "NAXIS1", |
| 102 |
"NAXIS2", "EXTEND"]) |
| 103 |
hdr = pyfits.PrimaryHDU().header |
| 104 |
del hdr["EXTEND"] |
| 105 |
hdr.set("NAXIS1", 200) |
| 106 |
hdr.set("FLOB", "Worong") |
| 107 |
hdr.set("EXTEND", "F") |
| 108 |
# EXTEND is now way behind NAXIS1 |
| 109 |
hdr = fitstools.sortHeaders(hdr) |
| 110 |
self.assertHeaderSequence(hdr, ["SIMPLE", "BITPIX", "NAXIS", "NAXIS1", |
| 111 |
"EXTEND", "FLOB"]) |
| 112 |
|
| 113 |
def testWithCardSequence(self): |
| 114 |
cardsIn = [pyfits.Card(*args) for args in ( |
| 115 |
("SIMPLE", True), ("BITPIX", 0), ("NAXIS", 0), |
| 116 |
("BOOKS", "Plenty"), ("CROOKS", 5), ("HOOKS", "None"))] |
| 117 |
hdr = fitstools.sortHeaders(cardsIn, |
| 118 |
cardSequence=(("SIMPLE", True), ("HOOKS", True), |
| 119 |
(pyfits.Card(value="---- Separator ----"), None), |
| 120 |
("CROOKS", True))) |
| 121 |
self.assertEqual( |
| 122 |
re.sub(" *", lambda mat: "(%d)"%len(mat.group(0)), str(hdr)), |
| 123 |
"SIMPLE(2)=(20)T(50)BITPIX(2)=(20)0(50)NAXIS(3)=(20)0(50)" |
| 124 |
"HOOKS(3)=(1)'None(4)'(68)----(1)Separator(1)----(53)" |
| 125 |
"CROOKS(2)=(20)5(50)BOOKS(3)=(1)'Plenty(2)'(60)END(2317)") |
| 126 |
|
| 127 |
|
| 128 |
class FITSWriteTest(testhelpers.VerboseTest): |
| 129 |
"""tests for correct FITS writing. |
| 130 |
""" |
| 131 |
def _assertOverwriteWorks(self, inFileName): |
| 132 |
with pyfits.open(inFileName) as hdus: |
| 133 |
hdr = hdus[0].header |
| 134 |
hdr.set("TELESCOP", "Python Telescope") |
| 135 |
hdr.set("APERTURE", 23) |
| 136 |
fitstools.replacePrimaryHeaderInPlace(inFileName, hdr) |
| 137 |
|
| 138 |
with pyfits.open(inFileName) as hdus: |
| 139 |
hdu = hdus[0] |
| 140 |
self.assertEqual(hdu.header["TELESCOP"], "Python Telescope") |
| 141 |
self.assertEqual(hdu.header["BITPIX"], 32) |
| 142 |
self.assertEqual(hdu.header["NAXIS1"], 10) |
| 143 |
self.assertEqual(hdu.data[1][4], 4) |
| 144 |
|
| 145 |
def testInPlaceSameSize(self): |
| 146 |
with testhelpers.testFile("test.fits", _fitsData) as ff: |
| 147 |
self._assertOverwriteWorks(ff) |
| 148 |
|
| 149 |
def testInPlaceGZ(self): |
| 150 |
with testhelpers.testFile("test.fits.gz", _fitsData, writeGz=True) as ff: |
| 151 |
self.assertRaisesWithMsg(NotImplementedError, |
| 152 |
"replacePrimaryHeaderInPlace no longer supports gzipped files.", |
| 153 |
fitstools.replacePrimaryHeaderInPlace, |
| 154 |
(ff, None)) |
| 155 |
|
| 156 |
def testInPlaceLonger(self): |
| 157 |
with testhelpers.testFile("test.fits", _fitsData) as ff: |
| 158 |
with pyfits.open(ff) as hdus: |
| 159 |
hdr = hdus[0].header |
| 160 |
for num in range(50): |
| 161 |
hdr.set("KEY%d"%num, num) |
| 162 |
fitstools.replacePrimaryHeaderInPlace(ff, hdr) |
| 163 |
|
| 164 |
with pyfits.open(ff) as hdus: |
| 165 |
hdu = hdus[0] |
| 166 |
self.assertEqual(hdu.header["NAXIS1"], 10) |
| 167 |
self.assertEqual(hdu.header["KEY48"], 48) |
| 168 |
self.assertEqual(hdu.data[1][4], 4) |
| 169 |
|
| 170 |
def testInPlaceShorter(self): |
| 171 |
with testhelpers.testFile("test.fits", _fitsData) as ff: |
| 172 |
with pyfits.open(ff) as hdus: |
| 173 |
hdr = hdus[0].header |
| 174 |
oldhdr = hdr.copy() |
| 175 |
for num in range(50): |
| 176 |
hdr.set("KEY%d"%num, num) |
| 177 |
fitstools.replacePrimaryHeaderInPlace(ff, hdr) |
| 178 |
fitstools.replacePrimaryHeaderInPlace(ff, oldhdr) |
| 179 |
|
| 180 |
# if things have worked, there's now >= 50 cards of padding |
| 181 |
# in the header, and the end card is in the second after |
| 182 |
# all the padding |
| 183 |
with open(ff, "rb") as f: |
| 184 |
firstBlock = f.read(fitstools.FITS_BLOCK_SIZE) |
| 185 |
self.assertFalse(fitstools.END_CARD in firstBlock) |
| 186 |
secondBlock = f.read(fitstools.FITS_BLOCK_SIZE) |
| 187 |
self.assertTrue(fitstools.END_CARD in secondBlock) |
| 188 |
self.assertTrue(secondBlock.startswith(b" "*2000)) |
| 189 |
|
| 190 |
with pyfits.open(ff) as hdus: |
| 191 |
hdu = hdus[0] |
| 192 |
self.assertEqual(hdu.header["NAXIS1"], 10) |
| 193 |
self.assertEqual(hdu.data[1][4], 4) |
| 194 |
self.assertRaises(KeyError, lambda: hdu.header["KEY48"]) |
| 195 |
|
| 196 |
def testWithContinueCards(self): |
| 197 |
hdr = pyfits.Header() |
| 198 |
hdr.set("LONGVAL", "This is a long value that will require a" |
| 199 |
" continue card in the fits header. Actually, there once even" |
| 200 |
" was a bug that only showed up when two contination lines" |
| 201 |
" were adjacent") |
| 202 |
hdr.add_comment("There were times when the combination of comment" |
| 203 |
" and continue cards were a problem") |
| 204 |
hdr.add_comment("This test will hopefully diagnoze any regressions" |
| 205 |
" in these fields") |
| 206 |
serialized = fitstools.serializeHeader(hdr) |
| 207 |
self.assertEqual(len(serialized), fitstools.FITS_BLOCK_SIZE) |
| 208 |
self.assertEqual(serialized[:9], b"LONGVAL =") |
| 209 |
self.assertEqual(serialized[80:89], b"CONTINUE ") |
| 210 |
self.assertEqual(serialized[160:169], b"CONTINUE ") |
| 211 |
self.assertEqual(serialized[240:249], b"COMMENT T") |
| 212 |
self.assertEqual(serialized[320:331], b"COMMENT a ") |
| 213 |
self.assertEqual(serialized[400:409], b"COMMENT T") |
| 214 |
self.assertEqual(serialized[480:489], b"END ") |
| 215 |
|
| 216 |
def testRoundtrip(self): |
| 217 |
with testhelpers.testFile("test.fits", _nastyHeaderData) as ff: |
| 218 |
with open(ff, "rb") as f: |
| 219 |
hdr = fitstools.readPrimaryHeaderQuick(f) |
| 220 |
self.assertEqual(fitstools.serializeHeader(hdr), |
| 221 |
_nastyHeaderData) |
| 222 |
|
| 223 |
|
| 224 |
class ParseCardsTest(testhelpers.VerboseTest): |
| 225 |
def testFailsForJunk(self): |
| 226 |
self.assertRaises(ValueError, fitstools.parseCards, "jfkdl") |
| 227 |
|
| 228 |
def testParses(self): |
| 229 |
input = ("NAXIS1 = 22757 / length of data axis 1 " |
| 230 |
" NAXIS2 = 22757 / length of data a" |
| 231 |
"xis 2 CTYPE1 = 'RA---TAN-SIP' / TAN " |
| 232 |
"(gnomic) projection + SIP distortions CTYPE2 = 'DEC--TAN-SIP' " |
| 233 |
" / TAN (gnomic) projection + SIP distortions ") |
| 234 |
cards = fitstools.parseCards(input) |
| 235 |
for card, (exKey, exVal) in zip(cards, [ |
| 236 |
("NAXIS1", 22757), |
| 237 |
("NAXIS2", 22757), |
| 238 |
("CTYPE1", "RA---TAN-SIP"), |
| 239 |
("CTYPE2", "DEC--TAN-SIP"),]): |
| 240 |
self.assertEqual(card.keyword, exKey) |
| 241 |
self.assertEqual(card.value, exVal) |
| 242 |
|
| 243 |
def testEndCard(self): |
| 244 |
input = ("NAXIS1 = 22757 / length of data axis 1 " |
| 245 |
" NAXIS2 = 22757 / length of data a" |
| 246 |
"xis 2 END " |
| 247 |
" CTYPE2 = 'DEC--TAN-SIP' " |
| 248 |
" / TAN (gnomic) projection + SIP distortions ") |
| 249 |
self.assertEqual(len(fitstools.parseCards(input)), 2) |
| 250 |
|
| 251 |
def testEmptyCardIgnored(self): |
| 252 |
input = ("NAXIS1 = 22757 / length of data axis 1 " |
| 253 |
" NAXIS2 = 22757 / length of data a" |
| 254 |
"xis 2 " |
| 255 |
" CTYPE2 = 'DEC--TAN-SIP' " |
| 256 |
" / TAN (gnomic) projection + SIP distortions ") |
| 257 |
self.assertEqual(len(fitstools.parseCards(input)), 3) |
| 258 |
|
| 259 |
|
| 260 |
class ScalingTest(testhelpers.VerboseTest): |
| 261 |
|
| 262 |
def testIterRowsBscale(self): |
| 263 |
with open(getAFITSPath(), "rb") as f: |
| 264 |
hdr = fitstools.readPrimaryHeaderQuick(f) |
| 265 |
colIter = fitstools.iterFITSRows(hdr, f) |
| 266 |
|
| 267 |
col = next(colIter) |
| 268 |
self.assertEqual(len(col), 12) |
| 269 |
self.assertEqual(col[-1], 8472.) |
| 270 |
self.assertEqual(len(list(colIter)), 22) |
| 271 |
|
| 272 |
def testIterScaledRows2(self): |
| 273 |
with open(getAFITSPath(), "rb") as f: |
| 274 |
pixelRows = list(fitstools.iterScaledRows(f, 2)) |
| 275 |
self.assertEqual(len(pixelRows), 11) |
| 276 |
self.assertEqual(len(pixelRows[0]), 6) |
| 277 |
self.assertEqual(pixelRows[0][-1], 8227.75) |
| 278 |
|
| 279 |
def testIterScaledRows5(self): |
| 280 |
with open(getAFITSPath(), "rb") as f: |
| 281 |
pixelRows = list(fitstools.iterScaledRows(f, 5)) |
| 282 |
self.assertEqual(len(pixelRows), 4) |
| 283 |
self.assertEqual(len(pixelRows[0]), 2) |
| 284 |
self.assertEqual(int(pixelRows[0][-1]), 8621) |
| 285 |
|
| 286 |
def testIterScaledRowsSlow(self): |
| 287 |
with open(getAFITSPath(), "rb") as f: |
| 288 |
pixelRows = list(fitstools.iterScaledRows(f, 5, slow=True)) |
| 289 |
self.assertEqual(len(pixelRows), 4) |
| 290 |
self.assertEqual(len(pixelRows[0]), 2) |
| 291 |
self.assertEqual(int(pixelRows[0][-1]), 8621) |
| 292 |
|
| 293 |
def testIterScaledRowsWidth(self): |
| 294 |
with open(getAFITSPath(), "rb") as f: |
| 295 |
pixelRows = list(fitstools.iterScaledRows(f, destSize=5)) |
| 296 |
self.assertEqual(len(pixelRows), 4) |
| 297 |
self.assertEqual(len(pixelRows[0]), 2) |
| 298 |
self.assertEqual(int(pixelRows[0][-1]), 8621) |
| 299 |
|
| 300 |
|
| 301 |
class ReadHeaderTest(testhelpers.VerboseTest): |
| 302 |
def testMaxBlocks(self): |
| 303 |
with testhelpers.testFile("bad.fits", " "*8000) as inName: |
| 304 |
with open(inName, "rb") as f: |
| 305 |
self.assertRaisesWithMsg(fitstools.FITSError, |
| 306 |
'No end card found within 2 blocks', |
| 307 |
fitstools.readHeaderBytes, |
| 308 |
(f, 2)) |
| 309 |
|
| 310 |
def testPrematureEOF(self): |
| 311 |
with testhelpers.testFile("bad.fits", " "*80) as inName: |
| 312 |
with open(inName, "rb") as f: |
| 313 |
self.assertRaisesWithMsg(EOFError, |
| 314 |
'Premature end of file while reading header', |
| 315 |
fitstools.readHeaderBytes, |
| 316 |
(f,)) |
| 317 |
|
| 318 |
def testTextSourceRejected(self): |
| 319 |
inName = os.path.join(base.getConfig("inputsDir"), "data", "ex.fits") |
| 320 |
with open(inName, "r") as f: |
| 321 |
self.assertRaisesWithMsg(IOError, |
| 322 |
'fitstools only works with files opened in binary mode.', |
| 323 |
fitstools.readHeaderBytes, |
| 324 |
(f,)) |
| 325 |
|
| 326 |
|
| 327 |
class FITSCutoutTest(testhelpers.VerboseTest): |
| 328 |
def setUp(self): |
| 329 |
self.hdus = pyfits.open( |
| 330 |
os.path.join(base.getConfig("inputsDir"), "data", "excube.fits")) |
| 331 |
self.origHDU = self.hdus[0] |
| 332 |
|
| 333 |
def tearDown(self): |
| 334 |
self.hdus.close() |
| 335 |
|
| 336 |
def testSimpleCutout1(self): |
| 337 |
res = fitstools.cutoutFITS(self.origHDU, (1, 2, 3)) |
| 338 |
self.assertEqual(res.header["NAXIS1"], 2) |
| 339 |
self.assertEqual(res.header["CRPIX1"], 36.) |
| 340 |
self.assertEqual(res.header["NAXIS"], 3) |
| 341 |
self.assertEqual(res.header["NAXIS2"], 7) |
| 342 |
self.assertEqual(res.header["CALIFAID"], 935) |
| 343 |
self.assertAlmostEqual(res.data[0][0][0], 0.01679336) |
| 344 |
self.assertAlmostEqual(res.data[-1][-1][-1], -0.01980321) |
| 345 |
|
| 346 |
def testOpenCutout2(self): |
| 347 |
res = fitstools.cutoutFITS(self.origHDU, (2, -1, 3)) |
| 348 |
self.assertEqual(res.header["NAXIS1"], 11) |
| 349 |
self.assertEqual(res.header["NAXIS2"], 3) |
| 350 |
self.assertEqual(res.header["CRPIX2"], 33.) |
| 351 |
self.assertAlmostEqual(res.data[0][0][0], 0.02511436) |
| 352 |
self.assertAlmostEqual(res.data[-1][-1][-1], 0.09358116) |
| 353 |
|
| 354 |
def testOpenCutout3(self): |
| 355 |
res = fitstools.cutoutFITS(self.origHDU, (3, 3, 10000)) |
| 356 |
self.assertEqual(res.header["NAXIS3"], 2) |
| 357 |
self.assertEqual(res.header["CRPIX3"], -1.) |
| 358 |
self.assertAlmostEqual(res.data[0][0][0], 0.03102851) |
| 359 |
self.assertAlmostEqual(res.data[-1][-1][-1], 0.07562912) |
| 360 |
|
| 361 |
def testMultiCutout(self): |
| 362 |
res = fitstools.cutoutFITS(self.origHDU, (1, 6, 8), (2, 3, 3), |
| 363 |
(3, 2, 4)) |
| 364 |
self.assertEqual(res.header["NAXIS1"], 3) |
| 365 |
self.assertEqual(res.header["CRPIX1"], 32) |
| 366 |
self.assertEqual(res.header["NAXIS2"], 1) |
| 367 |
self.assertEqual(res.header["CRPIX2"], 31) |
| 368 |
self.assertEqual(res.header["NAXIS3"], 3) |
| 369 |
self.assertEqual(res.header["CRPIX3"], 0) |
| 370 |
self.assertAlmostEqual(res.data[0][0][0], 0.0155675) |
| 371 |
self.assertAlmostEqual(res.data[-1][-1][-1], 0.05489994) |
| 372 |
|
| 373 |
def testSwappedLimits(self): |
| 374 |
res = fitstools.cutoutFITS(self.origHDU, (1, 8, 7)) |
| 375 |
self.assertEqual(res.header["NAXIS1"], 1) |
| 376 |
self.assertAlmostEqual(res.data[0][0][0], -0.01717306) |
| 377 |
|
| 378 |
def testMinOutOfLimit(self): |
| 379 |
res = fitstools.cutoutFITS(self.origHDU, (1, 13, 15)) |
| 380 |
self.assertEqual(res.header["NAXIS1"], 1) |
| 381 |
self.assertAlmostEqual(res.data[0][0][0], 0.0558035) |
| 382 |
self.assertEqual(res.header["CRPIX1"], 27.) |
| 383 |
|
| 384 |
def testMaxOutOfLimit(self): |
| 385 |
res = fitstools.cutoutFITS(self.origHDU, (1, -13, -12)) |
| 386 |
self.assertEqual(res.header["NAXIS1"], 1) |
| 387 |
self.assertAlmostEqual(res.data[0][0][0], 0.02511436) |
| 388 |
self.assertEqual(res.header["CRPIX1"], 37.) |
| 389 |
|
| 390 |
|
| 391 |
class WCSAxisTest(testhelpers.VerboseTest): |
| 392 |
def testTransformations(self): |
| 393 |
ax = fitstools.WCSAxis("test", 4, 9, 0.5) |
| 394 |
self.assertEqual(ax.pixToPhys(10), 4.5) |
| 395 |
self.assertEqual(ax.physToPix(4.5), 10) |
| 396 |
|
| 397 |
def testPix0Transformation(self): |
| 398 |
ax = fitstools.WCSAxis("test", 0, -9, -3) |
| 399 |
self.assertEqual(ax.pix0ToPhys(10), -60) |
| 400 |
self.assertEqual(ax.physToPix0(-60), 10) |
| 401 |
|
| 402 |
def testLimits(self): |
| 403 |
ax = fitstools.WCSAxis("test", -30, 2000, 0.25, axisLength=400) |
| 404 |
self.assertEqual(ax.getLimits(), (-529.75, -430.0)) |
| 405 |
|
| 406 |
def testNoneTrafo(self): |
| 407 |
ax = fitstools.WCSAxis("test", 4, 9, 0.5) |
| 408 |
self.assertEqual(ax.pixToPhys(None), None) |
| 409 |
self.assertEqual(ax.physToPix(None), None) |
| 410 |
|
| 411 |
def testNone0Trafo(self): |
| 412 |
ax = fitstools.WCSAxis("test", 4, 9, 0.5) |
| 413 |
self.assertEqual(ax.pix0ToPhys(None), None) |
| 414 |
self.assertEqual(ax.physToPix0(None), None) |
| 415 |
|
| 416 |
|
| 417 |
class WCSFromHeaderTest(testhelpers.VerboseTest): |
| 418 |
|
| 419 |
_baseHdr = { |
| 420 |
"CTYPE1": 'WAVELEN ', |
| 421 |
"CTYPE2": 'RA---TAN', |
| 422 |
"CTYPE3": 'DEC--TAN', |
| 423 |
"CRVAL1": 496.0, |
| 424 |
"CRVAL2": 148.75004, |
| 425 |
"CRVAL3": -1.50138888, |
| 426 |
"CRPIX1": 1, |
| 427 |
"CRPIX2": 32.5, |
| 428 |
"CRPIX3": 1, |
| 429 |
"CDELT1": 0.269, |
| 430 |
"CDELT2": -2.7777777777777E-05, |
| 431 |
"CDELT3": 1.0, |
| 432 |
"CUNIT1": 'nm ', |
| 433 |
"CNAME2": "FOOCOL "} |
| 434 |
|
| 435 |
def testMultiDRejected(self): |
| 436 |
hdr = self._baseHdr.copy() |
| 437 |
hdr["PC2_1"] = 23.3 |
| 438 |
self.assertRaisesWithMsg(ValueError, |
| 439 |
"FITS axis 2 appears not separable. WCSAxis cannot handle this.", |
| 440 |
fitstools.WCSAxis.fromHeader, |
| 441 |
(hdr, 2)) |
| 442 |
|
| 443 |
def testSelfCDOk(self): |
| 444 |
hdr = self._baseHdr.copy() |
| 445 |
hdr["CD2_2"] = 148.75 |
| 446 |
hdr["CD1_1"] = 23.3 |
| 447 |
self.assertEqual( |
| 448 |
fitstools.WCSAxis.fromHeader(hdr, 2).physToPix(23), |
| 449 |
4527033.940000127) |
| 450 |
|
| 451 |
def testNameInferenceCtype(self): |
| 452 |
ax = fitstools.WCSAxis.fromHeader(self._baseHdr, 1) |
| 453 |
self.assertEqual(ax.name, "WAVELEN_1") |
| 454 |
|
| 455 |
def testNameInferenceCname(self): |
| 456 |
ax = fitstools.WCSAxis.fromHeader(self._baseHdr, 2) |
| 457 |
self.assertEqual(ax.name, "FOOCOL_2") |
| 458 |
|
| 459 |
def testNameInferenceCleanedCtype(self): |
| 460 |
ax = fitstools.WCSAxis.fromHeader(self._baseHdr, 3) |
| 461 |
self.assertEqual(ax.name, "DEC_3") |
| 462 |
|
| 463 |
def testNameInferenceFallback(self): |
| 464 |
hdr = self._baseHdr.copy() |
| 465 |
del hdr["CTYPE3"] |
| 466 |
ax = fitstools.WCSAxis.fromHeader(hdr, 3) |
| 467 |
self.assertEqual(ax.name, "COO_3") |
| 468 |
|
| 469 |
def testOtherData(self): |
| 470 |
ax = fitstools.WCSAxis.fromHeader(self._baseHdr, 1) |
| 471 |
self.assertEqual(ax.crval, 496.0) |
| 472 |
self.assertEqual(ax.crpix, 1) |
| 473 |
self.assertEqual(ax.cdelt, 0.269) |
| 474 |
self.assertEqual(ax.cunit, "nm") |
| 475 |
self.assertEqual(ax.ctype, "WAVELEN") |
| 476 |
|
| 477 |
def testEmptyUnitBecomesNone(self): |
| 478 |
ax = fitstools.WCSAxis.fromHeader(self._baseHdr, 2) |
| 479 |
self.assertEqual(ax.cunit, None) |
| 480 |
|
| 481 |
|
| 482 |
class ESODescriptorsTest(testhelpers.VerboseTest): |
| 483 |
def testSimple(self): |
| 484 |
descs = fitstools._ESODescriptorsParser(""" |
| 485 |
'WSTART' ,'R*8 ' , 1, 6,'3E23.15',' ',' ' |
| 486 |
3.983291748046876E+03 4.011743652343751E+03 4.040604980468751E+03 |
| 487 |
4.069884765625001E+03 4.099591796875001E+03 4.129735839843751E+03 |
| 488 |
'NPTOT' ,'I*4 ' , 1, 14,'7I10',' ',' ' |
| 489 |
552 556 560 564 568 572 576 |
| 490 |
581 585 589 594 598 603 608 |
| 491 |
""").result |
| 492 |
self.assertEqual(descs, { |
| 493 |
'WSTART': [3983.291748046876, 4011.743652343751, 4040.604980468751, |
| 494 |
4069.884765625001, 4099.591796875001, 4129.735839843751], |
| 495 |
'NPTOT': [552, 556, 560, 564, 568, 572, 576, 581, 585, 589, 594, |
| 496 |
598, 603, 608], |
| 497 |
}) |
| 498 |
|
| 499 |
def testShortArray(self): |
| 500 |
self.assertRaisesWithMsg(base.SourceParseError, |
| 501 |
"At character 183: Expected a float here, offending ''", |
| 502 |
fitstools._ESODescriptorsParser, |
| 503 |
(""" |
| 504 |
'WSTART' ,'R*8 ' , 1, 6,'3E23.15',' ',' ' |
| 505 |
3.983291748046876E+03 4.011743652343751E+03 4.040604980468751E+03 |
| 506 |
4.069884765625001E+03 4.099591796875001E+03""",)) |
| 507 |
|
| 508 |
# Maybe do a test with a real header here? |
| 509 |
|
| 510 |
|
| 511 |
class _TemplatedHeader(testhelpers.TestResource): |
| 512 |
fitstricks.registerTemplate('dachstest', |
| 513 |
fitstricks.MINIMAL_IMAGE_TEMPLATE+[ |
| 514 |
pyfits.Card(value="---- Separator"), |
| 515 |
("ADDLATER", "This is inserted later"), |
| 516 |
("CHGLATER", "This is changed later"), |
| 517 |
("TESTVAL", "Just some random junk"),]) |
| 518 |
|
| 519 |
def make(self, dependents): |
| 520 |
hdr = fitstricks.makeHeaderFromTemplate("dachstest", |
| 521 |
BITPIX=8, NAXIS1=3, NAXIS2=4, TESTVAL="Really", |
| 522 |
CHGLATER=23) |
| 523 |
|
| 524 |
class _(object): |
| 525 |
fits = hdr |
| 526 |
ser = str(hdr) |
| 527 |
|
| 528 |
return _ |
| 529 |
|
| 530 |
_templatedHeader = _TemplatedHeader() |
| 531 |
|
| 532 |
|
| 533 |
class _ExtendedTemplatedHeader(testhelpers.TestResource): |
| 534 |
resources = [("base", _templatedHeader)] |
| 535 |
|
| 536 |
def make(self, dependents): |
| 537 |
base = dependents["base"].fits.copy() |
| 538 |
base.add_history("Changed by someone else") |
| 539 |
base.add_comment("This is snide") |
| 540 |
base.add_comment("Da-te-dum") |
| 541 |
hdr = fitstricks.updateTemplatedHeader(base, |
| 542 |
ADDLATER=42.25, CHGLATER=24) |
| 543 |
|
| 544 |
class _(object): |
| 545 |
fits = hdr |
| 546 |
ser = str(hdr) |
| 547 |
|
| 548 |
return _ |
| 549 |
|
| 550 |
|
| 551 |
class TemplatingTest(testhelpers.VerboseTest): |
| 552 |
resources = [("base", _templatedHeader), |
| 553 |
("mod", _ExtendedTemplatedHeader())] |
| 554 |
|
| 555 |
def testParmeterSwallowed(self): |
| 556 |
self.assertEqual(self.base.fits["NAXIS1"], 3) |
| 557 |
|
| 558 |
def testOmittedHeadersOmitted(self): |
| 559 |
self.assertFalse("BZERO" in self.base.fits) |
| 560 |
|
| 561 |
def testHeaderSequence(self): |
| 562 |
self.assertEqual([c.keyword for c in self.base.fits.cards], |
| 563 |
['SIMPLE', 'EXTEND', 'BITPIX', 'NAXIS', 'NAXIS1', 'NAXIS2', |
| 564 |
'', 'CHGLATER', 'TESTVAL', 'HISTORY']) |
| 565 |
|
| 566 |
def testNiceSeparator(self): |
| 567 |
self.assertTrue("2nd axis ---- Separator" |
| 568 |
in self.base.ser) |
| 569 |
|
| 570 |
def testCustomComment(self): |
| 571 |
self.assertTrue("'Really ' / Just some random junk" |
| 572 |
in self.base.ser) |
| 573 |
|
| 574 |
def testCreationHistoryPresent(self): |
| 575 |
self.assertTrue("HISTORY GAVO DaCHS template used: dachstest" |
| 576 |
in self.base.ser) |
| 577 |
|
| 578 |
def testModifiedHeaderSequence(self): |
| 579 |
self.assertEqual([c.keyword for c in self.mod.fits.cards], |
| 580 |
['SIMPLE', 'EXTEND', 'BITPIX', 'NAXIS', 'NAXIS1', 'NAXIS2', |
| 581 |
'', 'ADDLATER', 'CHGLATER', 'TESTVAL', 'HISTORY', 'HISTORY', |
| 582 |
'', 'COMMENT', 'COMMENT']) |
| 583 |
|
| 584 |
def testUpdateAdds(self): |
| 585 |
self.assertEqual(self.mod.fits["ADDLATER"], 42.25) |
| 586 |
|
| 587 |
def testUpdateChanges(self): |
| 588 |
self.assertEqual(self.base.fits["CHGLATER"], 23) |
| 589 |
self.assertEqual(self.mod.fits["CHGLATER"], 24) |
| 590 |
|
| 591 |
def testHistoryPreserved(self): |
| 592 |
self.assertTrue("HISTORY Changed by someone else" |
| 593 |
in self.mod.ser) |
| 594 |
|
| 595 |
def testCommentsPreserved(self): |
| 596 |
self.assertTrue("COMMENT Da-te-dum" in self.mod.ser) |
| 597 |
|
| 598 |
|
| 599 |
class HistoryTest(testhelpers.VerboseTest): |
| 600 |
def testAddingHistory(self): |
| 601 |
with pyfits.open(getAFITSPath()) as hdus: |
| 602 |
hdr = hdus[0].header |
| 603 |
fitstricks.addHistoryCard( |
| 604 |
hdr, |
| 605 |
"DaCHS test processing: adding History", |
| 606 |
"DaCHS test processing") |
| 607 |
|
| 608 |
histCards = hdr["history"] |
| 609 |
self.assertEqual(histCards[0], "Created by the Astrometry.net suite.") |
| 610 |
# astrometry.net and WCS cruft in 1-9 |
| 611 |
self.assertEqual(histCards[10], utils.EqualingRE( |
| 612 |
r"\d\d\d\d-\d\d-\d\d DaCHS test processing: adding History")) |
| 613 |
|
| 614 |
def testRewritingHistory(self): |
| 615 |
with pyfits.open(getAFITSPath()) as hdus: |
| 616 |
hdr = hdus[0].header |
| 617 |
fitstricks.addHistoryCard(hdr, |
| 618 |
"DaCHS test processing: adding History", |
| 619 |
"DaCHS test processing") |
| 620 |
|
| 621 |
histCards = hdr["history"] |
| 622 |
fitstricks.addHistoryCard(hdr, |
| 623 |
"Overwritten history.", |
| 624 |
"DaCHS test processing") |
| 625 |
self.assertEqual(histCards[10], utils.EqualingRE( |
| 626 |
r"\d\d\d\d-\d\d-\d\d Overwritten history.")) |
| 627 |
|
| 628 |
def testLongHistoryRejected(self): |
| 629 |
with pyfits.open(getAFITSPath()) as hdus: |
| 630 |
hdr = hdus[0].header |
| 631 |
fitstricks.addHistoryCard(hdr, "x"*61, "xxxxxxx") |
| 632 |
self.assertRaisesWithMsg(ValueError, |
| 633 |
"History entries must not exceed 61 characters.", |
| 634 |
fitstricks.addHistoryCard, |
| 635 |
(hdr, "x"*62, "xxxxx")) |
| 636 |
|
| 637 |
histCards = hdr["history"] |
| 638 |
self.assertEqual(len(histCards), 11) |
| 639 |
|
| 640 |
|
| 641 |
if __name__=="__main__": |
| 642 |
testhelpers.main(WCSFromHeaderTest) |