[6] | 1 | #!/usr/bin/python |
---|
| 2 | # -*- coding: iso-8859-1 -*- |
---|
[8] | 3 | # Un utilitaire pour transformer les données CCD PICARD en images fits |
---|
| 4 | # Frédéric Meynadier / Service d'Aéronomie, Mai 2008 |
---|
[2] | 5 | |
---|
[6] | 6 | VERSION='2.0' |
---|
[2] | 7 | |
---|
[6] | 8 | |
---|
[2] | 9 | import pyfits |
---|
[6] | 10 | import Tkinter |
---|
[7] | 11 | from Tkconstants import END |
---|
[6] | 12 | import numpy |
---|
| 13 | |
---|
[2] | 14 | from optparse import OptionParser |
---|
[7] | 15 | import datetime |
---|
| 16 | import time |
---|
[2] | 17 | |
---|
[6] | 18 | def raw2fits(InputFilename,headers,comments): |
---|
| 19 | OutputFilename = InputFilename.replace('.dat','.fits') |
---|
[25] | 20 | print "Converting %s to %s" % (InputFilename, OutputFilename) |
---|
| 21 | |
---|
[6] | 22 | NX = 2160 |
---|
| 23 | NY = 2053 |
---|
[2] | 24 | |
---|
| 25 | |
---|
[6] | 26 | # Ouverture de l'image en mode lecture binaire (windows) |
---|
| 27 | try: |
---|
| 28 | f= open(InputFilename,'rb') |
---|
| 29 | except IOError : |
---|
| 30 | print "Impossible d'ouvrir l'image" |
---|
[25] | 31 | exit (1) |
---|
[5] | 32 | |
---|
[6] | 33 | # Creation de l'array d'entiers non signés + byteswap |
---|
| 34 | a = numpy.fromfile(file=f, dtype=numpy.uint16, count=-1) |
---|
| 35 | a.byteswap(True) |
---|
| 36 | f.close() |
---|
[2] | 37 | |
---|
[6] | 38 | # dispatch des parties du binaire + conversion en signed int |
---|
| 39 | gauche = numpy.cast[numpy.int16](a[NX*NY/2:NX*NY]-32768) |
---|
| 40 | droite = numpy.cast[numpy.int16](a[:NX*NY/2]-32768) |
---|
| 41 | observables = numpy.cast[numpy.int16](a[-5:-1]-32768) |
---|
[2] | 42 | |
---|
| 43 | |
---|
[6] | 44 | # Construction de l'image (flip de la partie droite) |
---|
| 45 | b=numpy.empty(NX,dtype=numpy.int16) |
---|
| 46 | gauche.resize(NY,NX/2) |
---|
| 47 | droite.resize(NY,NX/2) |
---|
| 48 | b = numpy.hstack((gauche,numpy.fliplr(droite))) |
---|
[2] | 49 | |
---|
| 50 | |
---|
[6] | 51 | # Creation de l'objet fits |
---|
| 52 | hdu = pyfits.PrimaryHDU(b) |
---|
| 53 | hdulist = pyfits.HDUList([hdu]) |
---|
[2] | 54 | |
---|
[6] | 55 | prihdr = hdulist[0].header |
---|
[2] | 56 | |
---|
[6] | 57 | prihdr.update('BSCALE',1,'scale') |
---|
| 58 | prihdr.update('BZERO',32768,'origin of the scale') |
---|
[2] | 59 | |
---|
| 60 | |
---|
[6] | 61 | prihdr.update('OBS1',observables[0],'Observable 1') |
---|
| 62 | prihdr.update('OBS2',observables[1],'Observable 2') |
---|
| 63 | prihdr.update('OBS3',observables[2],'Observable 3') |
---|
| 64 | prihdr.update('OBS4',observables[3],'Observable 4') |
---|
| 65 | prihdr.update('Conv','pyraw2fits','Logiciel de conversion utilise') |
---|
| 66 | prihdr.update('ConvVer',VERSION,'Version de raw2fits') |
---|
[2] | 67 | |
---|
[7] | 68 | for item in headers: |
---|
| 69 | prihdr.update(item['key'],item['value'],item['comment']) |
---|
[2] | 70 | |
---|
[7] | 71 | for item in comments: |
---|
| 72 | prihdr.add_comment(item) |
---|
| 73 | |
---|
[6] | 74 | of = OutputFilename |
---|
[2] | 75 | |
---|
[6] | 76 | hdu.writeto(of,clobber=True) |
---|
[2] | 77 | |
---|
| 78 | |
---|
[6] | 79 | class simpleapp_tk(Tkinter.Tk): |
---|
| 80 | def __init__(self,parent): |
---|
| 81 | Tkinter.Tk.__init__(self,parent) |
---|
| 82 | self.parent = parent |
---|
| 83 | self.initialize() |
---|
[2] | 84 | |
---|
[6] | 85 | def initialize(self): |
---|
| 86 | self.grid() |
---|
| 87 | |
---|
[7] | 88 | self.labelexptimeVariable = Tkinter.StringVar() |
---|
| 89 | self.labelexptimeVariable.set("Temps de pose (s) :") |
---|
| 90 | labelexptime = Tkinter.Label(self,textvariable=self.labelexptimeVariable, anchor="w") |
---|
| 91 | labelexptime.grid(column=0,columnspan=2,row=1) |
---|
| 92 | |
---|
| 93 | self.exptimeVariable = Tkinter.StringVar() |
---|
| 94 | self.exptime = Tkinter.Entry(self,textvariable=self.exptimeVariable) |
---|
| 95 | self.exptime.grid(column=0,columnspan=2,row=2,sticky='EW') |
---|
| 96 | self.exptimeVariable.set(u"") |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | self.labelrf1Variable = Tkinter.StringVar() |
---|
| 100 | self.labelrf1Variable.set("RF1 :") |
---|
| 101 | labelrf1 = Tkinter.Label(self,textvariable=self.labelrf1Variable, |
---|
| 102 | anchor="w") |
---|
| 103 | labelrf1.grid(column=0,row=3) |
---|
| 104 | |
---|
| 105 | self.rf1 = Tkinter.Listbox(self,height=5,width=8,exportselection=0) |
---|
| 106 | self.rf1.pack() |
---|
| 107 | for item in ['trou', |
---|
| 108 | '535', |
---|
| 109 | '607', |
---|
| 110 | '215', |
---|
| 111 | '782']: |
---|
| 112 | self.rf1.insert(END,item) |
---|
| 113 | self.rf1.grid(column=0,row=4) |
---|
| 114 | |
---|
| 115 | self.labelrf2Variable = Tkinter.StringVar() |
---|
| 116 | self.labelrf2Variable.set("RF2 :") |
---|
| 117 | labelrf2 = Tkinter.Label(self,textvariable=self.labelrf2Variable, |
---|
| 118 | anchor="w") |
---|
| 119 | labelrf2.grid(column=1,row=3) |
---|
| 120 | |
---|
| 121 | self.rf2 = Tkinter.Listbox(self,height=5,width=8,exportselection=0) |
---|
| 122 | self.rf2.pack() |
---|
| 123 | for item in ['535h', |
---|
| 124 | 'trou', |
---|
| 125 | 'dioptre', |
---|
| 126 | 'lentille', |
---|
| 127 | '393']: |
---|
| 128 | self.rf2.insert(END,item) |
---|
| 129 | self.rf2.grid(column=1,row=4) |
---|
| 130 | |
---|
| 131 | self.labelcommentVariable = Tkinter.StringVar() |
---|
| 132 | self.labelcommentVariable.set("commentaire :") |
---|
| 133 | labelcomment = Tkinter.Label(self,textvariable=self.labelcommentVariable, anchor="w") |
---|
| 134 | labelcomment.grid(column=0,columnspan=2,row=5) |
---|
| 135 | |
---|
| 136 | self.commentVariable = Tkinter.StringVar() |
---|
| 137 | self.comment = Tkinter.Entry(self,textvariable=self.commentVariable) |
---|
| 138 | self.comment.grid(column=0,columnspan=2,row=6,sticky='EW') |
---|
| 139 | self.commentVariable.set(u"") |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | self.labelnameVariable = Tkinter.StringVar() |
---|
| 143 | self.labelnameVariable.set("Nom du fichier :") |
---|
| 144 | label = Tkinter.Label(self,textvariable=self.labelnameVariable, |
---|
| 145 | anchor="w") |
---|
| 146 | label.grid(column=0,row=7,columnspan=2,sticky='EW') |
---|
| 147 | |
---|
[6] | 148 | self.entryVariable = Tkinter.StringVar() |
---|
| 149 | self.entry = Tkinter.Entry(self,textvariable=self.entryVariable) |
---|
[7] | 150 | self.entry.grid(column=0,columnspan=2,row=8,sticky='EW') |
---|
[6] | 151 | self.entry.bind("<Return>", self.OnPressEnter) |
---|
| 152 | self.entryVariable.set(u"") |
---|
[7] | 153 | |
---|
| 154 | |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | button = Tkinter.Button(self, |
---|
| 159 | text="Convertir en fits", |
---|
[6] | 160 | command=self.OnButtonClick) |
---|
[7] | 161 | button.grid(column=0,columnspan=2,row=9) |
---|
[6] | 162 | |
---|
| 163 | self.labelVariable = Tkinter.StringVar() |
---|
| 164 | label = Tkinter.Label(self,textvariable=self.labelVariable, |
---|
| 165 | anchor="w",fg="white",bg="blue") |
---|
[7] | 166 | label.grid(column=0,row=10,columnspan=2,sticky='EW') |
---|
| 167 | self.labelVariable.set(u"") |
---|
[6] | 168 | |
---|
[7] | 169 | |
---|
| 170 | |
---|
[6] | 171 | self.grid_columnconfigure(0,weight=1) |
---|
| 172 | self.resizable(True,False) |
---|
| 173 | self.update() |
---|
| 174 | self.geometry(self.geometry()) |
---|
| 175 | self.entry.focus_set() |
---|
| 176 | self.entry.selection_range(0, Tkinter.END) |
---|
| 177 | |
---|
[7] | 178 | |
---|
[6] | 179 | def CommonAction(self): |
---|
[7] | 180 | rf1setting = self.rf1.get(self.rf1.curselection()[0]) |
---|
| 181 | rf2setting = self.rf2.get(self.rf2.curselection()[0]) |
---|
| 182 | |
---|
| 183 | exptime = self.exptimeVariable.get() |
---|
| 184 | if (exptime == ''): |
---|
| 185 | exptime = 0. |
---|
| 186 | |
---|
[6] | 187 | InputFilename = self.entryVariable.get() |
---|
| 188 | print InputFilename |
---|
| 189 | OutputFilename = InputFilename.replace(".dat",".fits") |
---|
| 190 | self.labelVariable.set("Ecriture de " + OutputFilename) |
---|
| 191 | headers=[] |
---|
[7] | 192 | headers.append({'key':'EXPTIME', |
---|
| 193 | 'value':float(exptime), |
---|
| 194 | 'comment':'Temps de pose'}) |
---|
| 195 | headers.append({'key':'RF1FILT', |
---|
| 196 | 'value':rf1setting, |
---|
| 197 | 'comment':'filtre roue 1'}) |
---|
| 198 | headers.append({'key':'RF2FILT', |
---|
| 199 | 'value':rf2setting, |
---|
| 200 | 'comment':'filtre roue 2'}) |
---|
| 201 | headers.append({'key':'CNVDATE', |
---|
| 202 | 'value':datetime.date.today().isoformat(), |
---|
| 203 | 'comment':'Date de la conversion fits'}) |
---|
| 204 | headers.append({'key':'CNVTIME', |
---|
| 205 | 'value':time.strftime("%H:%M:%S"), |
---|
| 206 | 'comment':'Heure de la conversion fits'}) |
---|
| 207 | |
---|
| 208 | |
---|
[6] | 209 | comments=[] |
---|
[7] | 210 | commentaire = self.commentVariable.get() |
---|
| 211 | if (commentaire != '') : |
---|
| 212 | comments.append(commentaire) |
---|
[6] | 213 | raw2fits(InputFilename,headers,comments) |
---|
| 214 | |
---|
| 215 | |
---|
| 216 | def OnButtonClick(self): |
---|
| 217 | self.CommonAction() |
---|
| 218 | self.entry.focus_set() |
---|
| 219 | self.entry.selection_range(0, Tkinter.END) |
---|
| 220 | |
---|
| 221 | def OnPressEnter(self,event): |
---|
| 222 | self.CommonAction() |
---|
| 223 | self.entry.focus_set() |
---|
| 224 | self.entry.selection_range(0, Tkinter.END) |
---|
| 225 | |
---|
| 226 | |
---|
| 227 | |
---|
| 228 | |
---|
| 229 | if __name__ == "__main__": |
---|
| 230 | |
---|
| 231 | parser = OptionParser() |
---|
| 232 | parser.add_option("-n","--nogui",action="store_true", |
---|
| 233 | dest="nogui", default=False, |
---|
| 234 | help="command line only, no gui") |
---|
| 235 | |
---|
| 236 | (options, args) = parser.parse_args () |
---|
| 237 | |
---|
| 238 | if options.nogui: |
---|
| 239 | InputFilename=args[0] |
---|
| 240 | headers=[] |
---|
| 241 | comments=[] |
---|
| 242 | raw2fits(InputFilename,headers,comments) |
---|
| 243 | |
---|
| 244 | else: |
---|
| 245 | app = simpleapp_tk(None) |
---|
| 246 | app.title('raw2fits') |
---|
| 247 | app.mainloop() |
---|
| 248 | |
---|
| 249 | |
---|
[7] | 250 | |
---|