Ejemplo n.º 1
0
class CombineScreen(IRScreen):
    speclist = ListProperty([])
    paths = DictProperty({})
    wmin = NumericProperty(0)
    wmax = NumericProperty(1024)
    dmin = NumericProperty(0)
    dmax = NumericProperty(1024)
    combined_spectrum = ObjectProperty(MeshLinePlot(color=[1,1,1,1]))
    the_specs = ListProperty([])
    theapp = ObjectProperty(None)
    spec_inserts = ListProperty([])
    comb_method = StringProperty('median')
    scaled_spectra = ListProperty([])
    spec_stack = ObjectProperty(None)
    
    def on_enter(self):
        self.speclist = [re.sub('.fits', '', os.path.basename(x)) for x in self.theapp.current_target.images.stack_list]
        flist = [x for y in self.speclist for x in glob.iglob(os.path.join(self.paths['out'],y + '-ap*.fits'))]
        self.the_specs = [ExtractedSpectrum(x) for x in flist]
        self.spec_stack = SpectrumStack(list(self.the_specs))
        colors = list(gen_colors(len(self.the_specs)))
        for i, ts in enumerate(self.the_specs):
            tmp = SpecscrollInsert(text=os.path.basename(flist[i]).replace('.fits',''), 
                                   index=i, spectrum=MeshLinePlot(color=colors[i]+(1,)))
            tmp.bind(active=self.toggle_spectrum)
            if not 'WAVECAL0' in ts.header:
                print i, 
                tmp.active = False
                self.scaled_spectra.append([xrange(len(ts.spec)),ts.spec])
                tmp.spectrum.points = zip(*self.scaled_spectra[i])
                self.spec_inserts.append(tmp)
                continue
            self.scaled_spectra.append([ts.wav,ts.spec])
            tmp.spectrum.points = zip(*self.scaled_spectra[i])
            #tmp.bind(active=lambda *x: self.toggle_spectrum(i))
            self.ids.multispec.add_plot(tmp.spectrum)
            self.spec_inserts.append(tmp)
            self.ids.specscroll.add_widget(tmp)
        self.setminmax()
        self.property('comb_method').dispatch(self)
        if not self.combined_spectrum in self.ids.combspec.plots:
            self.ids.combspec.add_plot(self.combined_spectrum)
        print self.combined_spectrum.color, self.combined_spectrum.points
        
    def setminmax(self):
        mmx = [map(float, [ts.wav.min(), ts.wav.max(), ts.spec.min(), ts.spec.max()]) \
            for i, ts in enumerate(self.the_specs) if self.spec_inserts[i].active]
        mmx = zip(*mmx)
        self.wmin, self.wmax = min(mmx[0]), max(mmx[1])
        self.dmin, self.dmax = min(mmx[2]), max(mmx[3])
    
    def toggle_spectrum(self, instance, active):
        if active:
            self.ids.multispec.add_plot(instance.spectrum)
        else:
            self.ids.multispec.remove_plot(instance.spectrum)
        self.property('comb_method').dispatch(self)
        self.setminmax()
    
    def set_scale(self, spec):
        self.ind = self.speclist.index(spec)
        self.spec_stack = SpectrumStack(list(self.the_specs))
        self.spec_stack.scale(index=self.ind)
        for i, s in enumerate(self.the_specs):
            self.spec_inserts[i].spectrum.points = zip(self.spec_stack[i].wav, self.spec_stack[i].spec)
        self.property('comb_method').dispatch(self)
        self.setminmax()
    
    def on_comb_method(self, instance, value):
        specs = self.spec_stack.subset([x.active for x in self.spec_inserts])
        comb = specs.combine(median=value.lower() == 'median')
        self.combined_spectrum.points = zip(comb.wav, comb.spec)
    
    def combine(self):
        out = self.ids.savefile.text
        h = self.the_specs[self.ind].header
        fits.writeto(out, zip(*self.combined_spectrum.points), header=h)
Ejemplo n.º 2
0
class CombineScreen(IRScreen):
    speclist = ListProperty([])
    paths = DictProperty({})
    wmin = NumericProperty(0)
    wmax = NumericProperty(1024)
    dmin = NumericProperty(0)
    dmax = NumericProperty(1024)
    combined_spectrum = ObjectProperty(MeshLinePlot(color=[1,1,1,1]))
    the_specs = ListProperty([])
    spec_inserts = ListProperty([])
    comb_method = StringProperty('median')
    scaled_spectra = ListProperty([])
    spec_stack = ObjectProperty(None)
    
    def on_enter(self):
        flist = [x for y in self.speclist for x in glob.iglob(self.paths['out'] + y + '-ap*.fits')]
        self.the_specs = [ExtractedSpectrum(x) for x in flist]
        colors = gen_colors(len(self.the_specs))
        for i, ts in enumerate(self.the_specs):
            tmp = SpecscrollInsert(text=flist[i])
            tmp.spectrum.color = colors[i] + (1)
            tmp.bind(active=self.toggle_spectrum(i))
            if not ts.wav:
                tmp.active = False
                self.scaled_spectra.append([xrange(len(ts.spec)),ts.spec])
                tmp.spectrum.points = zip(*self.scaled_spectra[i])
                self.spec_inserts.append(tmp)
                continue
            self.scaled_spectra.append([ts.wav,ts.spec])
            tmp.spectrum.points = zip(*self.scaled_spectra[i])
            tmp.bind(active=self.toggle_spectrum(i))
            self.ids.multispec.add_plot(tmp.spectrum)
            self.spec_inserts.append(tmp)
        self.setminmax()
        self.comb_method.dispatch()
        if not self.combined_spectrum in self.ids.combspec.plots:
            self.ids.combspec.add_plot(self.combined_spectrum)
        
    def setminmax(self):
        mmx = [[ts.wav.min(), ts.wav.max(), ts.spec.min(), ts.spec.max()] \
            for i, ts in enumerate(self.the_specs) if self.spec_inserts[i].active]
        mmx = zip(*mmx)
        self.wmin, self.wmax = min(mmx[0]), max(mmx[1])
        self.dmin, self.dmax = min(mmx[2]), max(mmx[3])
        
    
    def toggle_spectrum(self, ind):
        insert = self.spec_inserts[ind]
        if insert.active:
            self.ids.multispec.add_plot(insert.spectrum)
        else:
            self.ids.multispec.remove_plot(insert.spectrum)
        self.comb_method.dispatch()
        self.setminmax()
    
    def set_scale(self, spec):
        self.ind = self.speclist.index(spec)
        self.spec_stack = SpectrumStack(self.the_specs)
        self.spec_stack.scale(index=self.ind)
        for i, s in enumerate(self.the_specs):
            self.spec_inserts[i].spectrum.points = zip(self.spec_stack[i].x, self.spec_stack[i])
        self.comb_method.dispatch()
        self.setminmax()
    
    def on_comb_method(self, instance, value):
        specs = self.spec_stack.subset([x.active for x in self.spec_inserts.active])
        comb = specs.combine(median=value.lower() == 'median')
        self.combined_spectrum.points = zip(comb.x, comb)
    
    def combine(self):
        out = self.ids.savefile.text
        h = self.the_specs[self.ind].header
        fits.writeto(out, zip(*self.combined_spectrum.points), header=h)