Пример #1
0
 def assign(self):
     if not self.linelist:
         popup = AlertDialog(text="Please select a line list first.")
         popup.open()
         return
     fext = '-lamp.fits' if self.theapp.current_night.cals else '-sky.fits'
     fname = os.path.join(self.paths['cal'], 
                          os.path.basename(self.current_spectrum.filename).replace('.fits', fext))
     cal = ExtractedSpectrum(fname)
     cal.spec = detrend(medfilt(cal.spec, 3))
     srange = [float(self.ids.wmin.text), float(self.ids.wmax.text)]
     self.synthspec = synth_from_range(dict(self.linelist), cal.spec.size, srange)
     self.assignment = AssignLines(spectrum=cal, synth=self.synthspec, exp_lo=srange[0], exp_hi=srange[1])
     self.assignment.open()
Пример #2
0
class WavecalScreen(IRScreen):
    paths = DictProperty([])
    speclist = ListProperty([]) #set via app?
    spec_index = NumericProperty(0)
    current_target = ObjectProperty(None)
    current_spectrum = ObjectProperty(None)
    linelist = DictProperty({})
    linelists = ListProperty([])
    linelist_buttons = ListProperty([])
    assignment = ObjectProperty(None)
    synthspec = ListProperty([], force_dispatch=True)
    
    def on_enter(self):
        self.current_target = self.theapp.current_target
        self.speclist = [re.sub('.fits','',os.path.basename(x)) for x in self.current_target.spectra]
        self.linelists = sorted(linelistdb.keys())
        self.linelist_buttons = [Button(text=x, size_hint_y = None, height = 30) \
            for x in self.linelists]
    
    def set_spectrum(self, spec):
        self.spec_index = self.speclist.index(spec)
        try:
            tmp = ExtractedSpectrum(str(os.path.join(self.paths['out'],self.speclist[self.spec_index] + '.fits')))
        except IOError:
            popup = AlertDialog(text="You haven't extracted that spectrum yet!")
            popup.open()
            return
        if self.current_spectrum:
            self.ids.specdisplay.remove_plot(self.current_spectrum.plot)
        if not self.theapp.current_night.cals:
            self.ids.lampcal_toggle.state = 'normal'
            self.ids.lampcal_toggle.disabled = True
            self.ids.skycal_toggle.state = 'down'
        self.current_spectrum = tmp
        self.current_spectrum.spec = medfilt(self.current_spectrum.spec, 3)
        self.current_spectrum.plot = MeshLinePlot(color=[.9,1,1,1])
        self.current_spectrum.plot.points = zip(self.current_spectrum.wav, self.current_spectrum.spec)
        self.ids.specdisplay.add_plot(self.current_spectrum.plot)
        self.ids.specdisplay.xmin = float(floor_round(self.current_spectrum.wav.min(), dec=1))
        self.ids.specdisplay.xmax = float(ceil_round(self.current_spectrum.wav.max(), dec=1))
        self.ids.specdisplay.ymin = float(floor_round(self.current_spectrum.spec.min(), dec=1))
        self.ids.specdisplay.ymax = float(ceil_round(self.current_spectrum.spec.max(), dec=1))
        self.ids.specdisplay.xlabel = ['Pixel','Wavelength']['WAVECAL0' in self.current_spectrum.header]
    
    def set_linelist(self, val):
        if not val:
            return
        if val in self.linelists:
            self.linelist = linelistdb[val]
        else:
            lines = {'wavelength':[], 'strength':[]}
            with open(val, 'r') as f:
                for l in f:
                    if l.startswith('#'):
                        continue
                    w, s = map(float,l.split())
                    lines['wavelength'] += [w]
                    lines['strength'] += [s]
            self.linelist = lines
            linelistdb[val] = lines
            self.linelists = sorted(linelistdb.keys())
    
    
    def assign(self):
        if not self.linelist:
            popup = AlertDialog(text="Please select a line list first.")
            popup.open()
            return
        fext = '-lamp.fits' if self.theapp.current_night.cals else '-sky.fits'
        fname = os.path.join(self.paths['cal'], 
                             os.path.basename(self.current_spectrum.filename).replace('.fits', fext))
        cal = ExtractedSpectrum(fname)
        cal.spec = detrend(medfilt(cal.spec, 3))
        srange = [float(self.ids.wmin.text), float(self.ids.wmax.text)]
        self.synthspec = synth_from_range(dict(self.linelist), cal.spec.size, srange)
        self.assignment = AssignLines(spectrum=cal, synth=self.synthspec, exp_lo=srange[0], exp_hi=srange[1])
        self.assignment.open()
    
    def wavecal(self):
        if not self.linelist:
            popup = AlertDialog(text="Please select a line list first.")
            popup.open()
            return
        if not self.assignment.assignment:
            popup = AlertDialog(text='Please designate initial line assignments.')
            popup.open()
            return
        calfile = os.path.join(self.paths['cal'],self.speclist[self.spec_index])
        if self.ids.lampcal_toggle.state == 'down':
            calfile += '-lamp.fits'
        else:
            calfile += '-sky.fits'
        try:
            calib = ExtractedSpectrum(str(calfile))
        except IOError:
            popup = AlertDialog(text="You don't have a calibration of this type...")
            popup.open()
            return
        self.calibration = calibrate_wavelength(medfilt(calib.spec), list(self.synthspec), zip(*self.assignment.assignment))
        try:
            for i, w in enumerate(self.calibration.parameters):
                self.current_spectrum.header['WAVECAL%i'%i] = (w, 'Wavelength calibration coefficient')
            self.current_spectrum.wav = self.calibration(range(len(self.current_spectrum.spec)))
            self.ids.specdisplay.xmin = float(floor_round(self.current_spectrum.wav.min(), dec=1))
            self.ids.specdisplay.xmax = float(ceil_round(self.current_spectrum.wav.max(), dec=1))
            self.ids.specdisplay.xlabel = 'Wavelength'
            self.current_spectrum.plot.points = zip(self.current_spectrum.wav, self.current_spectrum.spec)
            self.save_spectrum()
        except Exception as e:
            print e
    
    def save_spectrum(self):
        self.current_spectrum.update_fits()