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