File: test_header.py

package info (click to toggle)
python-fitsio 1.3.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 1,020 kB
  • sloc: python: 7,963; ansic: 3,962; makefile: 10
file content (576 lines) | stat: -rw-r--r-- 21,806 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
import os
import tempfile
import warnings
import numpy as np

import pytest

from .makedata import make_data, lorem_ipsum
from .checks import check_header, compare_headerlist_header
from ..fitslib import FITS, read_header, write
from ..header import FITSHDR
from ..hdu.base import INVALID_HDR_CHARS
from ..util import cfitsio_version

CFITSIO_VERSION = cfitsio_version(asfloat=True)


def test_free_form_string():
    with tempfile.TemporaryDirectory() as tmpdir:
        fname = os.path.join(tmpdir, 'test.fits')
        with open(fname, 'w') as f:
            s = (
                "SIMPLE  =                    T / Standard FITS                                  "  # noqa
                + "BITPIX  =                   16 / number of bits per data pixel                  "  # noqa
                + "NAXIS   =                    0 / number of data axes                            "  # noqa
                + "EXTEND  =                    T / File contains extensions                       "  # noqa
                + "PHOTREF =   'previous MegaCam' / Source: cum.photcat                            "  # noqa
                + "EXTRA   =                    7 / need another line following PHOTREF            "  # noqa
                + "END                                                                             "  # noqa
            )
            f.write(s + ' ' * (2880 - len(s)))
        hdr = read_header(fname)
        assert hdr['PHOTREF'] == 'previous MegaCam'


def test_add_delete_and_update_records():
    # Build a FITSHDR from a few records (no need to write on disk)
    # Record names have to be in upper case to match with FITSHDR.add_record
    recs = [
        {'name': "First_record".upper(), 'value': 1, 'comment': "number 1"},
        {'name': "Second_record".upper(), 'value': "2"},
        {'name': "Third_record".upper(), 'value': "3"},
        {'name': "Last_record".upper(), 'value': 4, 'comment': "number 4"},
    ]
    hdr = FITSHDR(recs)

    # Add a new record
    hdr.add_record({'name': 'New_record'.upper(), 'value': 5})

    # Delete number 2 and 4
    hdr.delete('Second_record'.upper())
    hdr.delete('Last_record'.upper())

    # Update records : first and new one
    hdr['First_record'] = 11
    hdr['New_record'] = 3

    # Do some checks : len and get value/comment
    assert len(hdr) == 3
    assert hdr['First_record'] == 11
    assert hdr['New_record'] == 3
    assert hdr['Third_record'] == '3'
    assert hdr.get_comment('First_record') == 'number 1'
    assert not hdr.get_comment('New_record')


def testHeaderCommentPreserved():
    """
    Test that the comment is preserved after resetting the value
    """

    l1 = 'KEY1    =                   77 / My comment1'
    l2 = 'KEY2    =                   88 / My comment2'
    hdr = FITSHDR()
    hdr.add_record(l1)
    hdr.add_record(l2)

    hdr['key1'] = 99
    assert hdr.get_comment('key1') == 'My comment1', 'comment not preserved'


def test_header_write_read():
    """
    Test a basic header write and read

    Note the other read/write tests also are checking header writing with
    a list of dicts
    """

    with tempfile.TemporaryDirectory() as tmpdir:
        fname = os.path.join(tmpdir, 'test.fits')

        with FITS(fname, 'rw') as fits:
            data = np.zeros(10)
            header = {
                'x': 35,
                'y': 88.215,
                'eval': 1.384123233e43,
                'empty': '',
                'funky': '35-8',  # test old bug when strings look
                # like expressions
                'name': 'J. Smith',
                'what': '89113e6',  # test bug where converted to float
                'und': None,
                'binop': '25-3',  # test string with binary operation in it
                'unders': '1_000_000',  # test string with underscore
                'longs': lorem_ipsum,
            }
            if CFITSIO_VERSION > 4.02:
                # force hierarch + continue
                header["long_keyword_name"] = lorem_ipsum

            fits.write_image(data, header=header)

            rh = fits[0].read_header()
            check_header(header, rh)

        with FITS(fname) as fits:
            rh = fits[0].read_header()
            check_header(header, rh)


def test_header_delete():
    with tempfile.TemporaryDirectory() as tmpdir:
        fname = os.path.join(tmpdir, 'test.fits')

        with FITS(fname, 'rw') as fits:
            data = np.zeros(10)
            header1 = {'SCARD': 'one', 'ICARD': 1, 'FCARD': 1.0, 'LCARD': True}
            fits.write_image(data, header=header1)
            rh = fits[0].read_header()
            check_header(header1, rh)

            fits[0].delete_key("SCARD")
            del header1["SCARD"]
            rh = fits[0].read_header()
            check_header(header1, rh)

            fits[0].delete_keys(["ICARD", "FCARD"])
            del header1["ICARD"]
            del header1["FCARD"]
            rh = fits[0].read_header()
            check_header(header1, rh)

        with FITS(fname) as fits:
            rh = fits[0].read_header()
            check_header(header1, rh)


def test_header_update():
    with tempfile.TemporaryDirectory() as tmpdir:
        fname = os.path.join(tmpdir, 'test.fits')

        with FITS(fname, 'rw') as fits:
            data = np.zeros(10)
            header1 = {'SCARD': 'one', 'ICARD': 1, 'FCARD': 1.0, 'LCARD': True}
            header2 = {
                'SCARD': 'two',
                'ICARD': 2,
                'FCARD': 2.0,
                'LCARD': False,
                'SNEW': 'two',
                'INEW': 2,
                'FNEW': 2.0,
                'LNEW': False,
            }
            fits.write_image(data, header=header1)
            rh = fits[0].read_header()
            check_header(header1, rh)

            # Update header
            fits[0].write_keys(header2)

        with FITS(fname) as fits:
            rh = fits[0].read_header()
            check_header(header2, rh)


def test_read_header_case():
    """
    Test read_header with and without case sensitivity

    The reason we need a special test for this is because
    the read_header code is optimized for speed and has
    a different code path
    """

    with tempfile.TemporaryDirectory() as tmpdir:
        fname = os.path.join(tmpdir, 'test.fits')

        with FITS(fname, 'rw') as fits:
            data = np.zeros(10)
            adata = make_data()
            fits.write_image(data, header=adata['keys'], extname='First')
            fits.write_image(data, header=adata['keys'], extname='second')

        cases = [
            ('First', True),
            ('FIRST', False),
            ('second', True),
            ('seConD', False),
        ]
        for ext, ci in cases:
            h = read_header(fname, ext=ext, case_sensitive=ci)
            compare_headerlist_header(adata['keys'], h)


def test_blank_key_comments():
    """
    test a few different comments
    """

    with tempfile.TemporaryDirectory() as tmpdir:
        fname = os.path.join(tmpdir, 'test.fits')

        with FITS(fname, 'rw') as fits:
            records = [
                # empty should return empty
                {'name': None, 'value': '', 'comment': ''},
                # this will also return empty
                {'name': None, 'value': '', 'comment': ' '},
                # this will return exactly
                {'name': None, 'value': '', 'comment': ' h'},
                # this will return exactly
                {'name': None, 'value': '', 'comment': '--- test comment ---'},
            ]
            header = FITSHDR(records)

            fits.write(None, header=header)

            rh = fits[0].read_header()

            rrecords = rh.records()

            for i, ri in ((0, 6), (1, 7), (2, 8)):
                rec = records[i]
                rrec = rrecords[ri]

                assert rec['name'] is None, 'checking name is None'

                comment = rec['comment']
                rcomment = rrec['comment']
                if '' == comment.strip():
                    comment = ''

                assert comment == rcomment, "check empty key comment"


def test_blank_key_comments_from_cards():
    """
    test a few different comments
    """

    with tempfile.TemporaryDirectory() as tmpdir:
        fname = os.path.join(tmpdir, 'test.fits')

        with FITS(fname, 'rw') as fits:
            records = [
                '                                                                                ',  # noqa
                '         --- testing comment ---                                                ',  # noqa
                '        --- testing comment ---                                                 ',  # noqa
                "COMMENT testing                                                                 ",  # noqa
            ]
            header = FITSHDR(records)

            fits.write(None, header=header)

            rh = fits[0].read_header()

            rrecords = rh.records()

            assert rrecords[6]['name'] is None, 'checking name is None'
            assert rrecords[6]['comment'] == '', 'check empty key comment'
            assert rrecords[7]['name'] is None, 'checking name is None'
            assert rrecords[7]['comment'] == ' --- testing comment ---', (
                "check empty key comment"
            )
            assert rrecords[8]['name'] is None, 'checking name is None'
            assert rrecords[8]['comment'] == '--- testing comment ---', (
                "check empty key comment"
            )
            assert rrecords[9]['name'] == 'COMMENT', 'checking name is COMMENT'
            assert rrecords[9]['comment'] == 'testing', "check comment"


def test_header_from_cards():
    """
    test generating a header from cards, writing it out and getting
    back what we put in
    """
    hdr_from_cards = FITSHDR(
        [
            "IVAL    =                   35 / integer value                                  ",  # noqa
            "SHORTS  = 'hello world'                                                         ",  # noqa
            "UND     =                                                                       ",  # noqa
            "LONGS   = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiu&'",  # noqa
            "CONTINUE  'smod tempor incididunt ut labore et dolore magna aliqua'             ",  # noqa
            "DBL     =                 1.25                                                  ",  # noqa
        ]
    )
    header = [
        {'name': 'ival', 'value': 35, 'comment': 'integer value'},
        {'name': 'shorts', 'value': 'hello world'},
        {'name': 'und', 'value': None},
        {'name': 'longs', 'value': lorem_ipsum},
        {'name': 'dbl', 'value': 1.25},
    ]

    with tempfile.TemporaryDirectory() as tmpdir:
        fname = os.path.join(tmpdir, 'test.fits')

        with FITS(fname, 'rw') as fits:
            data = np.zeros(10)
            fits.write_image(data, header=hdr_from_cards)

            rh = fits[0].read_header()
            compare_headerlist_header(header, rh)

        with FITS(fname) as fits:
            rh = fits[0].read_header()
            compare_headerlist_header(header, rh)


def test_bad_header_write_raises():
    """
    Test that an invalid header raises.
    """

    for c in INVALID_HDR_CHARS:
        with tempfile.TemporaryDirectory() as tmpdir:
            fname = os.path.join(tmpdir, 'test.fits')
            try:
                hdr = {'bla%sg' % c: 3}
                data = np.zeros(10)

                write(fname, data, header=hdr, clobber=True)
            except Exception as e:
                assert "header key 'BLA%sG' has" % c in str(e)


def test_header_template():
    """
    test adding bunch of cards from a split template
    """

    header_template = """SIMPLE  =                    T /
BITPIX  =                    8 / bits per data value
NAXIS   =                    0 / number of axes
EXTEND  =                    T / Extensions are permitted
ORIGIN  = 'LSST DM Header Service'/ FITS file originator

     ---- Date, night and basic image information ----
DATE    =                      / Creation Date and Time of File
DATE-OBS=                      / Date of the observation (image acquisition)
DATE-BEG=                      / Time at the start of integration
DATE-END=                      / end date of the observation
MJD     =                      / Modified Julian Date that the file was written
MJD-OBS =                      / Modified Julian Date of observation
MJD-BEG =                      / Modified Julian Date derived from DATE-BEG
MJD-END =                      / Modified Julian Date derived from DATE-END
OBSID   =                      / ImageName from Camera StartIntergration
GROUPID =                      / imageSequenceName from StartIntergration
OBSTYPE =                      / BIAS, DARK, FLAT, OBJECT
BUNIT   = 'adu     '           / Brightness units for pixel array

     ---- Telescope info, location, observer ----
TELESCOP= 'LSST AuxTelescope'  / Telescope name
INSTRUME= 'LATISS'             / Instrument used to obtain these data
OBSERVER= 'LSST'               / Observer name(s)
OBS-LONG=           -70.749417 / [deg] Observatory east longitude
OBS-LAT =           -30.244639 / [deg] Observatory latitude
OBS-ELEV=               2663.0 / [m] Observatory elevation
OBSGEO-X=           1818938.94 / [m] X-axis Geocentric coordinate
OBSGEO-Y=          -5208470.95 / [m] Y-axis Geocentric coordinate
OBSGEO-Z=          -3195172.08 / [m] Z-axis Geocentric coordinate

    ---- Pointing info, etc. ----

DECTEL  =                      / Telescope DEC of observation
ROTPATEL=                      / Telescope Rotation
ROTCOORD= 'sky'                / Telescope Rotation Coordinates
RA      =                      / RA of Target
DEC     =                      / DEC of Target
ROTPA   =                      / Rotation angle relative to the sky (deg)
HASTART =                      / [HH:MM:SS] Telescope hour angle at start
ELSTART =                      / [deg] Telescope zenith distance at start
AZSTART =                      / [deg] Telescope azimuth angle at start
AMSTART =                      / Airmass at start
HAEND   =                      / [HH:MM:SS] Telescope hour angle at end
ELEND   =                      / [deg] Telescope zenith distance at end
AZEND   =                      / [deg] Telescope azimuth angle at end
AMEND   =                      / Airmass at end

    ---- Image-identifying used to build OBS-ID ----
TELCODE = 'AT'                 / The code for the telecope
CONTRLLR=                      / The controller (e.g. O for OCS, C for CCS)
DAYOBS  =                      / The observation day as defined by image name
SEQNUM  =                      / The sequence number from the image name
GROUPID =                      /

    ---- Information from Camera
CCD_MANU= 'ITL'                / CCD Manufacturer
CCD_TYPE= '3800C'              / CCD Model Number
CCD_SERN= '20304'              / Manufacturers? CCD Serial Number
LSST_NUM= 'ITL-3800C-098'      / LSST Assigned CCD Number
SEQCKSUM=                      / Checksum of Sequencer
SEQNAME =                      / SequenceName from Camera StartIntergration
REBNAME =                      / Name of the REB
CONTNUM =                      / CCD Controller (WREB) Serial Number
IMAGETAG=                      / DAQ Image id
TEMP_SET=                      / Temperature set point (deg C)
CCDTEMP =                      / Measured temperature (deg C)

    ---- Geometry from Camera ----
DETSIZE =                      / Size of sensor
OVERH   =                      / Over-scan pixels
OVERV   =                      / Vert-overscan pix
PREH    =                      / Pre-scan pixels

    ---- Filter/grating information ----
FILTER  =                      / Name of the filter
FILTPOS =                      / Filter position
GRATING =                      / Name of the second disperser
GRATPOS =                      / disperser position
LINSPOS =                      / Linear Stage

    ---- Exposure-related information ----
EXPTIME =                      / Exposure time in seconds
SHUTTIME=                      / Shutter exposure time in seconds
DARKTIME=                      / Dark time in seconds

    ---- Header information ----
FILENAME=                      / Original file name
HEADVER =                      / Version of header

    ---- Checksums ----
CHECKSUM=                      / checksum for the current HDU
DATASUM =                      / checksum of the data records\n"""

    lines = header_template.splitlines()
    hdr = FITSHDR()
    for line in lines:
        hdr.add_record(line)


def test_corrupt_continue():
    """
    test with corrupt continue, just make sure it doesn't crash
    """
    with tempfile.TemporaryDirectory() as tmpdir:
        fname = os.path.join(tmpdir, 'test.fits')
        with warnings.catch_warnings(record=True) as _:
            hdr_from_cards = FITSHDR(
                [
                    "IVAL    =                   35 / integer value                                  ",  # noqa
                    "SHORTS  = 'hello world'                                                         ",  # noqa
                    "CONTINUE= '        '           /   '&' / Current observing orogram              ",  # noqa
                    "UND     =                                                                       ",  # noqa
                    "DBL     =                 1.25                                                  ",  # noqa
                ]
            )

            with FITS(fname, 'rw') as fits:
                fits.write(None, header=hdr_from_cards)

            read_header(fname)

    with tempfile.TemporaryDirectory() as tmpdir:
        fname = os.path.join(tmpdir, 'test.fits')
        with warnings.catch_warnings(record=True) as _:
            hdr_from_cards = FITSHDR(
                [
                    "IVAL    =                   35 / integer value                                  ",  # noqa
                    "SHORTS  = 'hello world'                                                         ",  # noqa
                    "PROGRAM = 'Setting the Scale: Determining the Absolute Mass Normalization and &'",  # noqa
                    "CONTINUE  'Scaling Relations for Clusters at z~0.1&'                            ",  # noqa
                    "CONTINUE  '&' / Current observing orogram                                       ",  # noqa
                    "UND     =                                                                       ",  # noqa
                    "DBL     =                 1.25                                                  ",  # noqa
                ]
            )

            with FITS(fname, 'rw') as fits:
                fits.write(None, header=hdr_from_cards)

            read_header(fname)


def record_exists(header_records, key, value):
    for rec in header_records:
        if rec['name'] == key and rec['value'] == value:
            return True

    return False


def test_read_comment_history():
    with tempfile.TemporaryDirectory() as tmpdir:
        fname = os.path.join(tmpdir, 'test.fits')

        with FITS(fname, 'rw') as fits:
            data = np.arange(100).reshape(10, 10)
            fits.create_image_hdu(data)
            hdu = fits[-1]
            hdu.write_comment('A COMMENT 1')
            hdu.write_comment('A COMMENT 2')
            hdu.write_history('SOME HISTORY 1')
            hdu.write_history('SOME HISTORY 2')
            fits.close()

        with FITS(fname, 'r') as fits:
            hdu = fits[-1]
            header = hdu.read_header()
            records = header.records()
            assert record_exists(records, 'COMMENT', 'A COMMENT 1')
            assert record_exists(records, 'COMMENT', 'A COMMENT 2')
            assert record_exists(records, 'HISTORY', 'SOME HISTORY 1')
            assert record_exists(records, 'HISTORY', 'SOME HISTORY 2')


def test_write_key_dict():
    """
    test that write_key works using a standard key dict
    """

    with tempfile.TemporaryDirectory() as tmpdir:
        fname = os.path.join(tmpdir, 'test.fits')
        with FITS(fname, 'rw') as fits:
            im = np.zeros((10, 10), dtype='i2')
            fits.write(im)

            keydict = {
                'name': 'test',
                'value': 35,
                'comment': 'keydict test',
            }
            fits[-1].write_key(**keydict)

            h = fits[-1].read_header()

            assert h['test'] == keydict['value']
            assert h.get_comment('test') == keydict['comment']


@pytest.mark.parametrize("fname", ["test.fits", "mem://"])
def test_header_update_compressed_image_to_table(fname):
    data = np.arange(10).reshape(5, 2).astype(np.float32)

    fname = "test.fits"
    with tempfile.TemporaryDirectory() as tmpdir:
        if "mem://" not in fname:
            fpth = os.path.join(tmpdir, fname)
        else:
            fpth = fname

        with FITS(fpth, "rw") as fits:
            fits.write(data, compress="RICE", qlevel=1, dither_seed=10)
            hdr = fits[1].read_header()

            info_before = fits[1].get_info()
            for key in hdr.keys():
                if key.startswith("Z"):
                    fits[1].delete_key(key)
            for i in range(1000):
                fits[1].write_key("test" + str(i), "blah")
            fits[1].delete_key("test0")

            fits.update_hdu_list()
            info_after = fits[1].get_info()

            assert info_after != info_before
            assert fits[1].get_exttype() == "BINARY_TBL"


if __name__ == '__main__':
    test_header_write_read()