This file is indexed.

/usr/share/pyshared/pyfits/tests/testPyfitsImage.py is in python-pyfits 1:2.4.0-1build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
from __future__ import division # confidence high

import unittest
import pyfits
import numpy
import numpy as np
import exceptions,os,sys
import os.path

test_dir = os.path.dirname(__file__) + "/"

# Define a junk file for redirection of stdout
jfile = "junkfile.fits"

class TestPyfitsImageFunctions(unittest.TestCase):

    def setUp(self):
        # Perform set up actions (if any)
        pass

    def tearDown(self):
        # Perform clean-up actions (if any)
        try:
            os.remove('test_new2.fits')
        except:
            pass

        try:
            os.remove('test_new.fits')
        except:
            pass

        try:
            os.remove('test_append.fits')
        except:
            pass

        try:
            os.remove('test_update.fits')
        except:
            pass

        try:
            os.remove(jfile)
        except:
            pass

    def testCardConstructorDefaultArgs(self):
        # Test the constructor with default argument values.
        c=pyfits.Card()
        self.assertEqual('',c.key)

    def testFromstringSetAttributeAscardimage(self):
        # test fromstring() which will overwrite the values in the constructor
        c=pyfits.Card('abc', 99).fromstring('xyz     = 100')
        self.assertEqual(100,c.value)

        # test set attribute and  ascardimage() using the most updated attributes
        c.value=200
        self.assertEqual(c.ascardimage(),"XYZ     =                  200                                                  ")

    def testString(self):
        # test string value
        c=pyfits.Card('abc','<8 ch')
        self.assertEqual(str(c),"ABC     = '<8 ch   '                                                            ")
        c=pyfits.Card('nullstr','')
        self.assertEqual(str(c),"NULLSTR = ''                                                                    ")

    def testBooleanValueCard(self):
        # Boolean value card
        c=pyfits.Card("abc", pyfits.TRUE)
        self.assertEqual(str(c),"ABC     =                    T                                                  ")

        c=pyfits.Card().fromstring('abc     = F')
        self.assertEqual(c.value,False)

    def testLongIntegerNumber(self):
        # long integer number
        c=pyfits.Card('long_int', -467374636747637647347374734737437)
        self.assertEqual(str(c),"LONG_INT= -467374636747637647347374734737437                                    ")

    def testFloatingPointNumber(self):
        # floating point number
        c=pyfits.Card('floatnum', -467374636747637647347374734737437.)

        if str(c) != "FLOATNUM= -4.6737463674763E+32                                                  " and str(c) != "FLOATNUM= -4.6737463674763E+032                                                 ":
            self.assertEqual(str(c),"FLOATNUM= -4.6737463674763E+32                                                  ")

    def testComplexValue(self):
        # complex value
        c=pyfits.Card('abc',1.2345377437887837487e88+6324767364763746367e-33j)

        if str(c) != "ABC     = (1.23453774378878E+88, 6.32476736476374E-15)                          " and str(c) != "ABC     = (1.2345377437887E+088, 6.3247673647637E-015)                          ":
            self.assertEqual(str(c),"ABC     = (1.23453774378878E+88, 6.32476736476374E-15)                          ")

    def testCardImageConstructedTooLong(self):
        tmpfile = open(jfile,'w')
        sys.stdout = tmpfile
        # card image constructed from key/value/comment is too long (non-string value)
        c=pyfits.Card('abc',9,'abcde'*20)
        self.assertEqual(str(c),"ABC     =                    9 / abcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeab")
        c=pyfits.Card('abc', 'a'*68, 'abcdefg')
        self.assertEqual(str(c),"ABC     = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'")
        tmpfile.close()
        sys.stdout = sys.__stdout__
        os.remove(jfile)

    def testConstructorFilterIllegalDataStructures(self):
        # the constrctor will filter out illegal data structures...
        def test(*args,**kwargs):
            try:
                c = pyfits.Card(args,kwargs)
            except ValueError:
                c= "Failed as expected"
            return c
        self.assertEqual(test("abc",value=(2,3)),"Failed as expected")
        def test2(*args,**kwargs):
            try:
                c = pyfits.Card(*args,**kwargs)
            except ValueError:
                c= "Failed as expected"
            return c
        self.assertEqual(test2('key',[],'comment'),"Failed as expected")

    def testKeywordTooLong(self):
        #keywords too long
        def test(*args,**kwargs):
            try:
                c=pyfits.Card(*args,**kwargs)
            except ValueError:
                c = "Failed as expected"
            return c
        self.assertEqual(test("abcdefghi", "long"),"Failed as expected")


    def testIllegalCharactersInKey(self):
        # will not allow illegal characters in key when using constructor
        def test(*args,**kwargs):
            try:
                c=pyfits.Card(*args,**kwargs)
            except ValueError:
                c="Failed as expected."
            return c
        self.assertEqual(test('abc+',9),"Failed as expected.")


    def testAscardiageVerifiesTheCommentStringToBeAsciiText(self):
        # the ascardimage() verifies the comment string to be ASCII text
        c=pyfits.Card().fromstring('abc     = +  2.1   e + 12 / abcde'+chr(0x00))
        def test(obj):
            try:
                x = str(obj.ascardimage())
            except:
                x = "Failed as expected."
            return x
        self.assertEqual(test(c),"Failed as expected.")

    def testCommentaryCards(self):
        # commentary cards
        c=pyfits.Card("history","A commentary card's value has no quotes around it.")
        self.assertEqual(str(c),"HISTORY A commentary card's value has no quotes around it.                      ")
        c=pyfits.Card("comment", "A commentary card has no comment.", "comment")
        self.assertEqual(str(c),"COMMENT A commentary card has no comment.                                       ")

    def testCommentaryCardCreatedByFromstring(self):
        # commentary card created by fromstring()
        c=pyfits.Card().fromstring("COMMENT card has no comments. / text after slash is still part of the value.")
        self.assertEqual(c.value,'card has no comments. / text after slash is still part of the value.')
        self.assertEqual(c.comment,'')

    def testCommentaryCardWillNotParseNumericalValue(self):
        # commentary card will not parse the numerical value
        c=pyfits.Card().fromstring("history  (1, 2)")
        self.assertEqual(str(c.ascardimage()),"HISTORY  (1, 2)                                                                 ")

    def testEqualSignAfterColumn8(self):
        # equal sign after column 8 of a commentary card will be part ofthe string value
        c=pyfits.Card().fromstring("history =   (1, 2)")
        self.assertEqual(str(c.ascardimage()),"HISTORY =   (1, 2)                                                              ")

    def testSpecifyUndefinedValue(self):
        # this is how to specify an undefined value
        c=pyfits.Card("undef", pyfits.UNDEFINED)
        self.assertEqual(str(c),"UNDEF   =                                                                       ")

    def testComplexNumberUsingStringInput(self):
        # complex number using string input
        c=pyfits.Card().fromstring('abc     = (8, 9)')
        self.assertEqual(str(c.ascardimage()),"ABC     =               (8, 9)                                                  ")

    def testFixableNonStandardFITScard(self):
        # fixable non-standard FITS card will keep the original format
        c=pyfits.Card().fromstring('abc     = +  2.1   e + 12')
        self.assertEqual(c.value,2100000000000.0)
        self.assertEqual(str(c.ascardimage()),"ABC     =             +2.1E+12                                                  ")

    def testFixableNonFSC(self):
        # fixable non-FSC: if the card is not parsable, it's value will be assumed
        # to be a string and everything after the first slash will be comment
        c= pyfits.Card().fromstring("no_quote=  this card's value has no quotes / let's also try the comment")
        self.assertEqual(str(c.ascardimage()),"NO_QUOTE= 'this card''s value has no quotes' / let's also try the comment       ")

    def testUndefinedValueUsingStringInput(self):
        # undefined value using string input
        c=pyfits.Card().fromstring('abc     =    ')
        self.assertEqual(str(c.ascardimage()),"ABC     =                                                                       ")

    def testMisalocatedEqualSign(self):
        # test mislocated "=" sign
        c=pyfits.Card().fromstring('xyz= 100')
        self.assertEqual(c.key,'XYZ')
        self.assertEqual(c.value,100)
        self.assertEqual(str(c.ascardimage()),"XYZ     =                  100                                                  ")

    def testEqualOnlyUpToColumn10(self):
        # the test of "=" location is only up to column 10
        c=pyfits.Card().fromstring("histo       =   (1, 2)")
        self.assertEqual(str(c.ascardimage()),"HISTO   = '=   (1, 2)'                                                          ")
        c=pyfits.Card().fromstring("   history          (1, 2)")
        self.assertEqual(str(c.ascardimage()),"HISTO   = 'ry          (1, 2)'                                                  ")

    def testVerification(self):
        # verification
        c=pyfits.Card().fromstring('abc= a6')
        tmpfile = open(jfile,'w')
        sys.stdout = tmpfile
        c.verify()
        sys.stdout = sys.__stdout__
        tmpfile.close()
        tmpfile = open(jfile,'r')
        output = tmpfile.read()
        tmpfile.close()
        os.remove(jfile)
        self.assertEqual(output,"Output verification result:\nCard image is not FITS standard (equal sign not at column 8).\n")
        self.assertEqual(str(c),"abc= a6                                                                         ")

    def testFix(self):
        c=pyfits.Card().fromstring('abc= a6')
        tmpfile = open(jfile,'w')
        sys.stdout = tmpfile
        c.verify('fix')
        sys.stdout = sys.__stdout__
        tmpfile.close()
        tmpfile = open(jfile,'r')
        output = tmpfile.readlines()
        tmpfile.close()
        os.remove(jfile)
        self.assertEqual(output,["Output verification result:\n","  Fixed card to be FITS standard.: ABC\n"])
        self.assertEqual(str(c),"ABC     = 'a6      '                                                            ")

    def testLongStringValue(self):
        # test long string value
        c=pyfits.Card('abc','long string value '*10, 'long comment '*10)
        self.assertEqual(str(c),"ABC     = 'long string value long string value long string value long string &' \
CONTINUE  'value long string value long string value long string value long &'  \
CONTINUE  'string value long string value long string value &'                  \
CONTINUE  '&' / long comment long comment long comment long comment long        \
CONTINUE  '&' / comment long comment long comment long comment long comment     \
CONTINUE  '&' / long comment                                                    ")

    def testWordInLongStringTooLong(self):
        # if a word in a long string is too long, it will be cut in the middle
        c=pyfits.Card('abc','longstringvalue'*10, 'longcomment'*10)
        self.assertEqual(str(c),"ABC     = 'longstringvaluelongstringvaluelongstringvaluelongstringvaluelongstr&'\
CONTINUE  'ingvaluelongstringvaluelongstringvaluelongstringvaluelongstringvalu&'\
CONTINUE  'elongstringvalue&'                                                   \
CONTINUE  '&' / longcommentlongcommentlongcommentlongcommentlongcommentlongcomme\
CONTINUE  '&' / ntlongcommentlongcommentlongcommentlongcomment                  ")

    def testLongStringValueViaFromstring(self):
        # long string value via fromstring() method
        c=pyfits.Card().fromstring(pyfits.core._pad("abc     = 'longstring''s testing  &  ' / comments in line 1")+pyfits.core._pad("continue  'continue with long string but without the ampersand at the end' / ")+pyfits.core._pad("continue  'continue must have string value (with quotes)' / comments with ''. "))
        self.assertEqual(str(c.ascardimage()),"ABC     = 'longstring''s testing  continue with long string but without the &'  \
CONTINUE  'ampersand at the endcontinue must have string value (with quotes)&'  \
CONTINUE  '&' / comments in line 1 comments with ''.                            ")

    def testHierarchCard(self):
        c = pyfits.Card('hierarch abcdefghi',10)
        self.assertEqual(str(c.ascardimage()),"HIERARCH abcdefghi = 10                                                         ")
        c = pyfits.Card('HIERARCH ESO INS SLIT2 Y1FRML', 'ENC=OFFSET+RESOL*acos((WID-(MAX+MIN))/(MAX-MIN)')
        self.assertEqual(str(c.ascardimage()),"HIERARCH ESO INS SLIT2 Y1FRML= 'ENC=OFFSET+RESOL*acos((WID-(MAX+MIN))/(MAX-MIN)'")


    def testOpen(self):
        # The function "open" reads a FITS file into an HDUList object.  There are
        # three modes to open: "readonly" (the default), "append", and "update".

        # Open a file read-only (the default mode), the content of the FITS file
        # are read into memory.
        r=pyfits.open(test_dir+'test0.fits')                 # readonly

        # data parts are latent instantiation, so if we close the HDUList without
        # touching data, data can not be accessed.
        r.close()
        try:
            r[1].data[:2,:2]
            x = "Did not fail as expected."
        except:
            x = "Failed as expected."
        self.assertEqual(x,"Failed as expected.")

    def testOpen2(self):
        r=pyfits.open(test_dir+'test0.fits')

        # Use the "info" method for a summary of the FITS file's content.
        tmpfile = open(jfile,'w')
        sys.stdout = tmpfile
        r.info()
        sys.stdout = sys.__stdout__
        tmpfile.close()
        tmpfile = open(jfile,'r')
        output = tmpfile.readlines()
        tmpfile.close()
        os.remove(jfile)
        r.close()
        self.assertEqual(output,["Filename: "+test_dir+"test0.fits\n",
"No.    Name         Type      Cards   Dimensions   Format\n",
"0    PRIMARY     PrimaryHDU     138  ()            int16\n",
"1    SCI         ImageHDU        61  (40, 40)      int16\n",
"2    SCI         ImageHDU        61  (40, 40)      int16\n",
"3    SCI         ImageHDU        61  (40, 40)      int16\n",
"4    SCI         ImageHDU        61  (40, 40)      int16\n"])

    def testIOManipulation(self):
        # Get a keyword value.  An extension can be referred by name or by number.
        # Both extension and keyword names are case insensitive.
        r=pyfits.open(test_dir+'test0.fits')
        self.assertEqual(r['primary'].header['naxis'],0)
        self.assertEqual(r[0].header['naxis'],0)

        # If there are more than one extension with the same EXTNAME value, the
        # EXTVER can be used (as the second argument) to distinguish the extension.
        self.assertEqual(r['sci',1].header['detector'],1)

        # append (using "update()") a new card
        r[0].header.update('xxx',1.234e56)

        if str(r[0].header.ascard[-3:]) != "EXPFLAG = 'NORMAL            ' / Exposure interruption indicator                \nFILENAME= 'vtest3.fits'        / File name                                      \nXXX     =            1.234E+56                                                  " and str(r[0].header.ascard[-3:]) != "EXPFLAG = 'NORMAL            ' / Exposure interruption indicator                \nFILENAME= 'vtest3.fits'        / File name                                      \nXXX     =           1.234E+056                                                  ":
            self.assertEqual(str(r[0].header.ascard[-3:]),"EXPFLAG = 'NORMAL            ' / Exposure interruption indicator                \nFILENAME= 'vtest3.fits'        / File name                                      \nXXX     =            1.234E+56                                                  ")

        # rename a keyword
        r[0].header.rename_key('filename','fname')
        try:
            r[0].header.rename_key('fname','history')
            x = "Did not fail as expected."
        except ValueError:
            x = "Failed as expected."
        self.assertEqual(x, "Failed as expected.")

        try:
            r[0].header.rename_key('fname','simple')
            x = "Did not fail as expected."
        except ValueError:
            x = "Failed as expected."
        self.assertEqual(x, "Failed as expected.")

        r[0].header.rename_key('fname','filename')

        # get a subsection of data
        self.assertEqual(r[2].data[:3,:3].all(),np.array([[349, 349, 348],[348, 348, 348],[349, 349, 350]], dtype=np.int16).all())

        #We can create a new FITS file by opening a new file with "append" mode.
        n=pyfits.open('test_new.fits',mode='append')

        # Append the primary header and the 2nd extension to the new file.
        n.append(r[0])
        n.append(r[2])

        # The flush method will write the current HDUList object back to the newly
        # created file on disk.  The HDUList is still open and can be further operated.
        n.flush()
        self.assertEqual(n[1].data[1,1],349)

        #modify a data point
        n[1].data[1,1]=99

        # When the file is closed, the most recent additions of extension(s) since
        # last flush() will be appended, but any HDU already existed at the last
        # flush will not be modified
        n.close()

        # If an existing file is opened with "append" mode, like the readonly mode,
        # the HDU's will be read into the HDUList which can be modified in memory
        # but can not be written back to the original file.  A file opened with
        # append mode can only add new HDU's.
        os.rename('test_new.fits', 'test_append.fits')

        a=pyfits.open('test_append.fits',mode='append')

        # The above change did not take effect since this was made after the flush().
        self.assertEqual(a[1].data[1,1],349)

        a.append(r[1])
        a.close()

        # When changes are made to an HDUList which was opened with "update" mode,
        # they will be written back to the original file when a flush/close is called.
        os.rename('test_append.fits', 'test_update.fits')

        u=pyfits.open('test_update.fits',mode='update')

        # When the changes do not alter the size structures of the original (or since
        # last flush) HDUList, the changes are written back "in place".
        self.assertEqual(u[0].header['rootname'],'U2EQ0201T')
        u[0].header['rootname']='abc'
        self.assertEqual(u[1].data[1,1],349)
        u[1].data[1,1]=99
        u.flush()

        # If the changes affect the size structure, e.g. adding or deleting HDU(s),
        # header was expanded or reduced beyond existing number of blocks (2880 bytes
        # in each block), or change the data size, the HDUList is written to a
        # temporary file, the original file is deleted, and the temporary file is
        # renamed to the original file name and reopened in the update mode.
        # To a user, these two kinds of updating writeback seem to be the same, unless
        # the optional argument in flush or close is set to 1.
        del u[2]
        u.flush()

        # the write method in HDUList class writes the current HDUList, with
        # all changes made up to now, to a new file.  This method works the same
        # disregard the mode the HDUList was opened with.
        u.append(r[3])
        u.writeto('test_new.fits')

        # Remove temporary files created by this test
        u.close()
        os.remove('test_new.fits')
        os.remove('test_update.fits')


        #Another useful new HDUList method is readall.  It will "touch" the data parts
        # in all HDUs, so even if the HDUList is closed, we can still operate on
        # the data.
        r=pyfits.open(test_dir+'test0.fits')
        r.readall()
        r.close()
        self.assertEqual(r[1].data[1,1],315)

        # create an HDU with data only
        data = numpy.ones((3,5), dtype=np.float32)
        hdu=pyfits.ImageHDU(data=data, name='SCI')
        self.assertEqual(hdu.data.all(),np.array([[ 1.,  1.,  1.,  1.,  1.],[ 1.,  1.,  1.,  1.,  1.],[ 1.,  1.,  1.,  1.,  1.]], dtype=np.float32).all())


        # create an HDU with header and data
        # notice that the header has the right NAXIS's since it is constructed with
        # ImageHDU
        hdu2=pyfits.ImageHDU(header=r[1].header, data=numpy.array([1,2],dtype='int32'))

        tmpfile = open(jfile,'w')
        sys.stdout = tmpfile
        print hdu2.header.ascard[1:5]
        tmpfile.close()
        sys.stdout = sys.__stdout__
        tmpfile = open(jfile,'r')
        tmplist = tmpfile.readlines()
        tmpfile.close()
        os.remove(jfile)
        self.assertEqual(tmplist,["BITPIX  =                   32 / array data type                                \n","NAXIS   =                    1 / number of array dimensions                     \n","NAXIS1  =                    2                                                  \n","PCOUNT  =                    0 / number of parameters                           \n"])

    def testMemmoryMapping(self):
        # memory mapping
        f1 = pyfits.open(test_dir+'test0.fits', memmap=1)
        f1.close()

    def testVerificationOnOutput(self):
        # verification on output
        # make a defect HDUList first
        tmpfile = open(jfile,'w')
        sys.stdout = tmpfile
        x = pyfits.ImageHDU()
        hdu = pyfits.HDUList(x)     # HDUList can take a list or one single HDU
        hdu.verify()
        sys.stdout = sys.__stdout__
        tmpfile.close()
        tmpfile = open(jfile,'r')
        tmplist = tmpfile.readlines()
        tmpfile.close()
        os.remove(jfile)
        self.assertEqual(tmplist,["Output verification result:\n","HDUList's 0th element is not a primary HDU.\n"])

        tmpfile = open(jfile,'w')
        sys.stdout = tmpfile
        hdu.writeto('test_new2.fits','fix')
        sys.stdout = sys.__stdout__
        tmpfile.close()
        tmpfile = open(jfile,'r')
        tmplist = tmpfile.readlines()
        tmpfile.close()
        os.remove(jfile)
        self.assertEqual(tmplist,["Output verification result:\n","HDUList's 0th element is not a primary HDU.  Fixed by inserting one as 0th HDU.\n"])

        os.remove('test_new2.fits')

    def testSection(self):
        # section testing
        fs=pyfits.open(test_dir+'arange.fits')
        self.assertEqual(fs[0].section[3,2,5],np.array([357]))
        self.assertEqual(fs[0].section[3,2,:].all(),np.array([352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362]).all())
        self.assertEqual(fs[0].section[3,2,4:].all(),np.array([356, 357, 358, 359, 360, 361, 362]).all())
        self.assertEqual(fs[0].section[3,2,:8].all(),np.array([352, 353, 354, 355, 356, 357, 358, 359]).all())
        self.assertEqual(fs[0].section[3,2,-8:8].all(),np.array([355, 356, 357, 358, 359]).all())
        self.assertEqual(fs[0].section[3,2:5,:].all(),np.array([[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]]).all())

        self.assertEqual(fs[0].section[3,:,:][:3,:3].all(),np.array([[330, 331, 332],
                                                                     [341, 342, 343],
                                                                     [352, 353, 354]]).all())


        dat=fs[0].data
        self.assertEqual(fs[0].section[3,2:5,:8].all(), dat[3,2:5,:8].all())
        self.assertEqual(fs[0].section[3,2:5,3].all(), dat[3,2:5,3].all())


        self.assertEqual(fs[0].section[3:6,:,:][:3,:3,:3].all(),np.array([[[330, 331, 332],
                                                                     [341, 342, 343],
                                                                     [352, 353, 354]],

                                                                     [[440, 441, 442],
                                                                      [451, 452, 453],
                                                                      [462, 463, 464]],

                                                                     [[550, 551, 552],
                                                                      [561, 562, 563],
                                                                      [572, 573, 574]]]).all())

        self.assertEqual(fs[0].section[:,:,:][:3,:2,:2].all(),np.array([[[  0,   1],
                                                                         [ 11,  12]],

                                                                         [[110, 111],
                                                                          [121, 122]],

                                                                         [[220, 221],
                                                                          [231, 232]]]).all())

        self.assertEqual(fs[0].section[:,2,:].all(), dat[:,2,:].all())
        self.assertEqual(fs[0].section[:,2:5,:].all(), dat[:,2:5,:].all())
        self.assertEqual(fs[0].section[3:6,3,:].all(), dat[3:6,3,:].all())
        self.assertEqual(fs[0].section[3:6,3:7,:].all(), dat[3:6,3:7,:].all())

    def testSectionDataSquare(self):
        a=np.arange(4).reshape((2,2))
        hdu = pyfits.PrimaryHDU(a)
        hdu.writeto(jfile)

        hdul=pyfits.open(jfile)
        d = hdul[0]
        dat = hdul[0].data
        self.assertEqual(d.section[:,:].all(), dat[:,:].all())
        self.assertEqual(d.section[0,:].all(), dat[0,:].all())
        self.assertEqual(d.section[1,:].all(), dat[1,:].all())
        self.assertEqual(d.section[:,0].all(), dat[:,0].all())
        self.assertEqual(d.section[:,1].all(), dat[:,1].all())
        self.assertEqual(d.section[0,0].all(), dat[0,0].all())
        self.assertEqual(d.section[0,1].all(), dat[0,1].all())
        self.assertEqual(d.section[1,0].all(), dat[1,0].all())
        self.assertEqual(d.section[1,1].all(), dat[1,1].all())
        self.assertEqual(d.section[0:1,0:1].all(), dat[0:1,0:1].all())
        self.assertEqual(d.section[0:2,0:1].all(), dat[0:2,0:1].all())
        self.assertEqual(d.section[0:1,0:2].all(), dat[0:1,0:2].all())
        self.assertEqual(d.section[0:2,0:2].all(), dat[0:2,0:2].all())
        os.remove(jfile)

    def testSectionDataCube(self):
        a=np.arange(18).reshape((2,3,3))
        hdu = pyfits.PrimaryHDU(a)
        hdu.writeto(jfile)

        hdul=pyfits.open(jfile)
        d = hdul[0]
        dat = hdul[0].data
        self.assertEqual(d.section[:,:,:].all(), dat[:,:,:].all())
        self.assertEqual(d.section[:,:].all(), dat[:,:].all())
        self.assertEqual(d.section[:].all(), dat[:].all())
        self.assertEqual(d.section[0,:,:].all(), dat[0,:,:].all())
        self.assertEqual(d.section[1,:,:].all(), dat[1,:,:].all())
        self.assertEqual(d.section[0,0,:].all(), dat[0,0,:].all())
        self.assertEqual(d.section[0,1,:].all(), dat[0,1,:].all())
        self.assertEqual(d.section[0,2,:].all(), dat[0,2,:].all())
        self.assertEqual(d.section[1,0,:].all(), dat[1,0,:].all())
        self.assertEqual(d.section[1,1,:].all(), dat[1,1,:].all())
        self.assertEqual(d.section[1,2,:].all(), dat[1,2,:].all())
        self.assertEqual(d.section[0,0,0].all(), dat[0,0,0].all())
        self.assertEqual(d.section[0,0,1].all(), dat[0,0,1].all())
        self.assertEqual(d.section[0,0,2].all(), dat[0,0,2].all())
        self.assertEqual(d.section[0,1,0].all(), dat[0,1,0].all())
        self.assertEqual(d.section[0,1,1].all(), dat[0,1,1].all())
        self.assertEqual(d.section[0,1,2].all(), dat[0,1,2].all())
        self.assertEqual(d.section[0,2,0].all(), dat[0,2,0].all())
        self.assertEqual(d.section[0,2,1].all(), dat[0,2,1].all())
        self.assertEqual(d.section[0,2,2].all(), dat[0,2,2].all())
        self.assertEqual(d.section[1,0,0].all(), dat[1,0,0].all())
        self.assertEqual(d.section[1,0,1].all(), dat[1,0,1].all())
        self.assertEqual(d.section[1,0,2].all(), dat[1,0,2].all())
        self.assertEqual(d.section[1,1,0].all(), dat[1,1,0].all())
        self.assertEqual(d.section[1,1,1].all(), dat[1,1,1].all())
        self.assertEqual(d.section[1,1,2].all(), dat[1,1,2].all())
        self.assertEqual(d.section[1,2,0].all(), dat[1,2,0].all())
        self.assertEqual(d.section[1,2,1].all(), dat[1,2,1].all())
        self.assertEqual(d.section[1,2,2].all(), dat[1,2,2].all())
        self.assertEqual(d.section[:,0,0].all(), dat[:,0,0].all())
        self.assertEqual(d.section[:,0,1].all(), dat[:,0,1].all())
        self.assertEqual(d.section[:,0,2].all(), dat[:,0,2].all())
        self.assertEqual(d.section[:,1,0].all(), dat[:,1,0].all())
        self.assertEqual(d.section[:,1,1].all(), dat[:,1,1].all())
        self.assertEqual(d.section[:,1,2].all(), dat[:,1,2].all())
        self.assertEqual(d.section[:,2,0].all(), dat[:,2,0].all())
        self.assertEqual(d.section[:,2,1].all(), dat[:,2,1].all())
        self.assertEqual(d.section[:,2,2].all(), dat[:,2,2].all())
        self.assertEqual(d.section[0,:,0].all(), dat[0,:,0].all())
        self.assertEqual(d.section[0,:,1].all(), dat[0,:,1].all())
        self.assertEqual(d.section[0,:,2].all(), dat[0,:,2].all())
        self.assertEqual(d.section[1,:,0].all(), dat[1,:,0].all())
        self.assertEqual(d.section[1,:,1].all(), dat[1,:,1].all())
        self.assertEqual(d.section[1,:,2].all(), dat[1,:,2].all())
        self.assertEqual(d.section[:,:,0].all(), dat[:,:,0].all())
        self.assertEqual(d.section[:,:,1].all(), dat[:,:,1].all())
        self.assertEqual(d.section[:,:,2].all(), dat[:,:,2].all())
        self.assertEqual(d.section[:,0,:].all(), dat[:,0,:].all())
        self.assertEqual(d.section[:,1,:].all(), dat[:,1,:].all())
        self.assertEqual(d.section[:,2,:].all(), dat[:,2,:].all())

        self.assertEqual(d.section[:,:,0:1].all(), dat[:,:,0:1].all())
        self.assertEqual(d.section[:,:,0:2].all(), dat[:,:,0:2].all())
        self.assertEqual(d.section[:,:,0:3].all(), dat[:,:,0:3].all())
        self.assertEqual(d.section[:,:,1:2].all(), dat[:,:,1:2].all())
        self.assertEqual(d.section[:,:,1:3].all(), dat[:,:,1:3].all())
        self.assertEqual(d.section[:,:,2:3].all(), dat[:,:,2:3].all())
        self.assertEqual(d.section[0:1,0:1,0:1].all(), dat[0:1,0:1,0:1].all())
        self.assertEqual(d.section[0:1,0:1,0:2].all(), dat[0:1,0:1,0:2].all())
        self.assertEqual(d.section[0:1,0:1,0:3].all(), dat[0:1,0:1,0:3].all())
        self.assertEqual(d.section[0:1,0:1,1:2].all(), dat[0:1,0:1,1:2].all())
        self.assertEqual(d.section[0:1,0:1,1:3].all(), dat[0:1,0:1,1:3].all())
        self.assertEqual(d.section[0:1,0:1,2:3].all(), dat[0:1,0:1,2:3].all())
        self.assertEqual(d.section[0:1,0:2,0:1].all(), dat[0:1,0:2,0:1].all())
        self.assertEqual(d.section[0:1,0:2,0:2].all(), dat[0:1,0:2,0:2].all())
        self.assertEqual(d.section[0:1,0:2,0:3].all(), dat[0:1,0:2,0:3].all())
        self.assertEqual(d.section[0:1,0:2,1:2].all(), dat[0:1,0:2,1:2].all())
        self.assertEqual(d.section[0:1,0:2,1:3].all(), dat[0:1,0:2,1:3].all())
        self.assertEqual(d.section[0:1,0:2,2:3].all(), dat[0:1,0:2,2:3].all())
        self.assertEqual(d.section[0:1,0:3,0:1].all(), dat[0:1,0:3,0:1].all())
        self.assertEqual(d.section[0:1,0:3,0:2].all(), dat[0:1,0:3,0:2].all())
        self.assertEqual(d.section[0:1,0:3,0:3].all(), dat[0:1,0:3,0:3].all())
        self.assertEqual(d.section[0:1,0:3,1:2].all(), dat[0:1,0:3,1:2].all())
        self.assertEqual(d.section[0:1,0:3,1:3].all(), dat[0:1,0:3,1:3].all())
        self.assertEqual(d.section[0:1,0:3,2:3].all(), dat[0:1,0:3,2:3].all())
        self.assertEqual(d.section[0:1,1:2,0:1].all(), dat[0:1,1:2,0:1].all())
        self.assertEqual(d.section[0:1,1:2,0:2].all(), dat[0:1,1:2,0:2].all())
        self.assertEqual(d.section[0:1,1:2,0:3].all(), dat[0:1,1:2,0:3].all())
        self.assertEqual(d.section[0:1,1:2,1:2].all(), dat[0:1,1:2,1:2].all())
        self.assertEqual(d.section[0:1,1:2,1:3].all(), dat[0:1,1:2,1:3].all())
        self.assertEqual(d.section[0:1,1:2,2:3].all(), dat[0:1,1:2,2:3].all())
        self.assertEqual(d.section[0:1,1:3,0:1].all(), dat[0:1,1:3,0:1].all())
        self.assertEqual(d.section[0:1,1:3,0:2].all(), dat[0:1,1:3,0:2].all())
        self.assertEqual(d.section[0:1,1:3,0:3].all(), dat[0:1,1:3,0:3].all())
        self.assertEqual(d.section[0:1,1:3,1:2].all(), dat[0:1,1:3,1:2].all())
        self.assertEqual(d.section[0:1,1:3,1:3].all(), dat[0:1,1:3,1:3].all())
        self.assertEqual(d.section[0:1,1:3,2:3].all(), dat[0:1,1:3,2:3].all())
        self.assertEqual(d.section[1:2,0:1,0:1].all(), dat[1:2,0:1,0:1].all())
        self.assertEqual(d.section[1:2,0:1,0:2].all(), dat[1:2,0:1,0:2].all())
        self.assertEqual(d.section[1:2,0:1,0:3].all(), dat[1:2,0:1,0:3].all())
        self.assertEqual(d.section[1:2,0:1,1:2].all(), dat[1:2,0:1,1:2].all())
        self.assertEqual(d.section[1:2,0:1,1:3].all(), dat[1:2,0:1,1:3].all())
        self.assertEqual(d.section[1:2,0:1,2:3].all(), dat[1:2,0:1,2:3].all())
        self.assertEqual(d.section[1:2,0:2,0:1].all(), dat[1:2,0:2,0:1].all())
        self.assertEqual(d.section[1:2,0:2,0:2].all(), dat[1:2,0:2,0:2].all())
        self.assertEqual(d.section[1:2,0:2,0:3].all(), dat[1:2,0:2,0:3].all())
        self.assertEqual(d.section[1:2,0:2,1:2].all(), dat[1:2,0:2,1:2].all())
        self.assertEqual(d.section[1:2,0:2,1:3].all(), dat[1:2,0:2,1:3].all())
        self.assertEqual(d.section[1:2,0:2,2:3].all(), dat[1:2,0:2,2:3].all())
        self.assertEqual(d.section[1:2,0:3,0:1].all(), dat[1:2,0:3,0:1].all())
        self.assertEqual(d.section[1:2,0:3,0:2].all(), dat[1:2,0:3,0:2].all())
        self.assertEqual(d.section[1:2,0:3,0:3].all(), dat[1:2,0:3,0:3].all())
        self.assertEqual(d.section[1:2,0:3,1:2].all(), dat[1:2,0:3,1:2].all())
        self.assertEqual(d.section[1:2,0:3,1:3].all(), dat[1:2,0:3,1:3].all())
        self.assertEqual(d.section[1:2,0:3,2:3].all(), dat[1:2,0:3,2:3].all())
        self.assertEqual(d.section[1:2,1:2,0:1].all(), dat[1:2,1:2,0:1].all())
        self.assertEqual(d.section[1:2,1:2,0:2].all(), dat[1:2,1:2,0:2].all())
        self.assertEqual(d.section[1:2,1:2,0:3].all(), dat[1:2,1:2,0:3].all())
        self.assertEqual(d.section[1:2,1:2,1:2].all(), dat[1:2,1:2,1:2].all())
        self.assertEqual(d.section[1:2,1:2,1:3].all(), dat[1:2,1:2,1:3].all())
        self.assertEqual(d.section[1:2,1:2,2:3].all(), dat[1:2,1:2,2:3].all())
        self.assertEqual(d.section[1:2,1:3,0:1].all(), dat[1:2,1:3,0:1].all())
        self.assertEqual(d.section[1:2,1:3,0:2].all(), dat[1:2,1:3,0:2].all())
        self.assertEqual(d.section[1:2,1:3,0:3].all(), dat[1:2,1:3,0:3].all())
        self.assertEqual(d.section[1:2,1:3,1:2].all(), dat[1:2,1:3,1:2].all())
        self.assertEqual(d.section[1:2,1:3,1:3].all(), dat[1:2,1:3,1:3].all())
        self.assertEqual(d.section[1:2,1:3,2:3].all(), dat[1:2,1:3,2:3].all())

        os.remove(jfile)

    def testSectionDataFour(self):
        a=np.arange(256).reshape((4,4,4,4))
        hdu = pyfits.PrimaryHDU(a)
        hdu.writeto(jfile)

        hdul=pyfits.open(jfile)
        d=hdul[0]
        dat = hdul[0].data
        self.assertEqual(d.section[:,:,:,:].all(), dat[:,:,:,:].all())
        self.assertEqual(d.section[:,:,:].all(), dat[:,:,:].all())
        self.assertEqual(d.section[:,:].all(), dat[:,:].all())
        self.assertEqual(d.section[:].all(), dat[:].all())
        self.assertEqual(d.section[0,:,:,:].all(), dat[0,:,:,:].all())
        self.assertEqual(d.section[0,:,0,:].all(), dat[0,:,0,:].all())
        self.assertEqual(d.section[:,:,0,:].all(), dat[:,:,0,:].all())
        self.assertEqual(d.section[:,1,0,:].all(), dat[:,1,0,:].all())
        self.assertEqual(d.section[:,:,:,1].all(), dat[:,:,:,1].all())

        os.remove(jfile)

    def testCompImage(self):
        data = np.zeros((2, 10, 10), dtype=np.float32)
        primary_hdu = pyfits.PrimaryHDU()
        ofd = pyfits.HDUList(primary_hdu)
        ofd.append(pyfits.CompImageHDU(data, name="SCI",
                                       compressionType="GZIP_1",
                                       quantizeLevel=-0.01))
        ofd.writeto(jfile)
        ofd.close()
        fd = pyfits.open (jfile)
        self.assertEqual(fd[1].data.all(), data.all())
        os.remove(jfile)

    def testDoNotScaleImageData(self):
        hdul = pyfits.open(test_dir+'scale.fits',do_not_scale_image_data=True)
        self.assertEqual(hdul[0].data.dtype,np.dtype('>i2'))
        hdul = pyfits.open(test_dir+'scale.fits')
        self.assertEqual(hdul[0].data.dtype,np.dtype('float32'))


if __name__ == '__main__':
    unittest.main()