Changeset 6 for pyraw2fits/pyraw2fits.py


Ignore:
Timestamp:
05/27/08 16:03:46 (16 years ago)
Author:
meynadie
Message:

Ajout d'un GUI, sauvegarde de l'ancien dans old

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pyraw2fits/pyraw2fits.py

    r5 r6  
    1 #!/bin/env python 
    2 # coding: utf-8 
     1#!/usr/bin/python 
     2# -*- coding: iso-8859-1 -*- 
    33 
    4 VERSION='1.0' 
     4VERSION='2.0' 
     5 
    56 
    67import pyfits 
     8import Tkinter 
     9import numpy 
     10 
    711from optparse import OptionParser 
    812 
    9 import numpy 
    1013 
    1114 
    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") 
     15 
     16def raw2fits(InputFilename,headers,comments): 
     17    OutputFilename = InputFilename.replace('.dat','.fits') 
     18 
     19    NX = 2160 
     20    NY = 2053 
    1721 
    1822 
    19 (options, args) = parser.parse_args () 
     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) 
    2029 
    21 InputFilename = args[0] 
    22 OutputFilename = InputFilename.replace('.dat','.fits') 
     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() 
    2334 
    24 print InputFilename, OutputFilename 
    25  
    26 #print len(options.keys()) 
     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) 
    2739 
    2840 
    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))) 
     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))) 
    5446 
    5547 
    56 # Creation de l'objet fits 
    57 hdu = pyfits.PrimaryHDU(b) 
    58 hdulist = pyfits.HDUList([hdu]) 
     48    # Creation de l'objet fits 
     49    hdu = pyfits.PrimaryHDU(b) 
     50    hdulist = pyfits.HDUList([hdu]) 
    5951 
    60 prihdr = hdulist[0].header 
     52    prihdr = hdulist[0].header 
    6153 
    62 prihdr.update('BSCALE',1,'scale') 
    63 prihdr.update('BZERO',32768,'origin of the scale') 
     54    prihdr.update('BSCALE',1,'scale') 
     55    prihdr.update('BZERO',32768,'origin of the scale') 
    6456 
    6557 
    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') 
     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') 
    7264 
    7365 
    74 of = OutputFilename 
     66    of = OutputFilename 
    7567 
    76 hdu.writeto(of,clobber=True) 
     68    hdu.writeto(of,clobber=True) 
     69 
     70 
     71class simpleapp_tk(Tkinter.Tk): 
     72    def __init__(self,parent): 
     73        Tkinter.Tk.__init__(self,parent) 
     74        self.parent = parent 
     75        self.initialize() 
     76 
     77    def initialize(self): 
     78        self.grid() 
     79 
     80        self.entryVariable = Tkinter.StringVar() 
     81        self.entry = Tkinter.Entry(self,textvariable=self.entryVariable) 
     82        self.entry.grid(column=1,row=0,sticky='EW') 
     83        self.entry.bind("<Return>", self.OnPressEnter) 
     84        self.entryVariable.set(u"") 
     85         
     86        button = Tkinter.Button(self,text=u"Convertir en fits", 
     87                                command=self.OnButtonClick) 
     88        button.grid(column=0,row=2) 
     89 
     90        self.labelVariable = Tkinter.StringVar() 
     91        label = Tkinter.Label(self,textvariable=self.labelVariable, 
     92                              anchor="w",fg="white",bg="blue") 
     93        label.grid(column=0,row=1,columnspan=2,sticky='EW') 
     94        self.labelVariable.set(u"Hello !") 
     95 
     96        self.grid_columnconfigure(0,weight=1) 
     97        self.resizable(True,False) 
     98        self.update() 
     99        self.geometry(self.geometry()) 
     100        self.entry.focus_set() 
     101        self.entry.selection_range(0, Tkinter.END) 
     102         
     103 
     104    def CommonAction(self): 
     105        InputFilename = self.entryVariable.get() 
     106        print InputFilename 
     107        OutputFilename = InputFilename.replace(".dat",".fits") 
     108        self.labelVariable.set("Ecriture de " + OutputFilename) 
     109        headers=[] 
     110        comments=[] 
     111        raw2fits(InputFilename,headers,comments) 
     112         
     113         
     114    def OnButtonClick(self): 
     115        self.CommonAction() 
     116        self.entry.focus_set() 
     117        self.entry.selection_range(0, Tkinter.END) 
     118 
     119    def OnPressEnter(self,event): 
     120        self.CommonAction() 
     121        self.entry.focus_set() 
     122        self.entry.selection_range(0, Tkinter.END) 
     123 
     124 
     125         
     126 
     127if __name__ == "__main__": 
     128 
     129    parser = OptionParser() 
     130    parser.add_option("-n","--nogui",action="store_true", 
     131                      dest="nogui", default=False, 
     132                      help="command line only, no gui") 
     133                       
     134    (options, args) = parser.parse_args () 
     135 
     136    if options.nogui: 
     137        InputFilename=args[0] 
     138        headers=[] 
     139        comments=[] 
     140        raw2fits(InputFilename,headers,comments) 
     141 
     142    else: 
     143        app = simpleapp_tk(None) 
     144        app.title('raw2fits') 
     145        app.mainloop() 
     146 
     147 
Note: See TracChangeset for help on using the changeset viewer.