[6] | 1 | #!/bin/env python |
---|
| 2 | # coding: utf-8 |
---|
| 3 | |
---|
| 4 | VERSION='1.0' |
---|
| 5 | |
---|
| 6 | import pyfits |
---|
| 7 | from optparse import OptionParser |
---|
| 8 | |
---|
| 9 | import numpy |
---|
| 10 | |
---|
| 11 | |
---|
| 12 | parser = OptionParser() |
---|
| 13 | ##parser.add_option("-i","--input",dest="InputFilename", |
---|
| 14 | ## help="Input filename (required)", metavar="RAWIMAGE") |
---|
| 15 | ##parser.add_option("-o","--output",dest="OutputFilename", |
---|
| 16 | ## help="Output Filename (required)", metavar="FITSIMAGE") |
---|
| 17 | |
---|
| 18 | |
---|
| 19 | (options, args) = parser.parse_args () |
---|
| 20 | |
---|
| 21 | InputFilename = args[0] |
---|
| 22 | OutputFilename = InputFilename.replace('.dat','.fits') |
---|
| 23 | |
---|
| 24 | print InputFilename, OutputFilename |
---|
| 25 | |
---|
| 26 | #print len(options.keys()) |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | NX = 2160 |
---|
| 30 | NY = 2053 |
---|
| 31 | |
---|
| 32 | # Ouverture de l'image en mode lecture binaire (windows) |
---|
| 33 | try: |
---|
| 34 | f= open(InputFilename,'rb') |
---|
| 35 | except IOError : |
---|
| 36 | print "Impossible d'ouvrir l'image" |
---|
| 37 | exit (1) |
---|
| 38 | |
---|
| 39 | # Creation de l'array d'entiers non signés + byteswap |
---|
| 40 | a = numpy.fromfile(file=f, dtype=numpy.uint16, count=-1) |
---|
| 41 | a.byteswap(True) |
---|
| 42 | f.close() |
---|
| 43 | |
---|
| 44 | # dispatch des parties du binaire + conversion en signed int |
---|
| 45 | gauche = numpy.cast[numpy.int16](a[NX*NY/2:NX*NY]-32768) |
---|
| 46 | droite = numpy.cast[numpy.int16](a[:NX*NY/2]-32768) |
---|
| 47 | observables = numpy.cast[numpy.int16](a[-5:-1]-32768) |
---|
| 48 | |
---|
| 49 | # Construction de l'image (flip de la partie droite) |
---|
| 50 | b=numpy.empty(NX,dtype=numpy.int16) |
---|
| 51 | gauche.resize(NY,NX/2) |
---|
| 52 | droite.resize(NY,NX/2) |
---|
| 53 | b = numpy.hstack((gauche,numpy.fliplr(droite))) |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | # Creation de l'objet fits |
---|
| 57 | hdu = pyfits.PrimaryHDU(b) |
---|
| 58 | hdulist = pyfits.HDUList([hdu]) |
---|
| 59 | |
---|
| 60 | prihdr = hdulist[0].header |
---|
| 61 | |
---|
| 62 | prihdr.update('BSCALE',1,'scale') |
---|
| 63 | prihdr.update('BZERO',32768,'origin of the scale') |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | prihdr.update('OBS1',observables[0],'Observable 1') |
---|
| 67 | prihdr.update('OBS2',observables[1],'Observable 2') |
---|
| 68 | prihdr.update('OBS3',observables[2],'Observable 3') |
---|
| 69 | prihdr.update('OBS4',observables[3],'Observable 4') |
---|
| 70 | prihdr.update('Conv','pyraw2fits','Logiciel de conversion utilise') |
---|
| 71 | prihdr.update('ConvVer',VERSION,'Version de raw2fits') |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | of = OutputFilename |
---|
| 75 | |
---|
| 76 | hdu.writeto(of,clobber=True) |
---|