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