Пример #1
0
class ObservingScreen(IRScreen):
    obsids = DictProperty({})
    obsrun_list = ListProperty([])
    obsrun_buttons = ListProperty([])
    current_obsrun = ObjectProperty(ird.ObsRun())
    obsnight_list = ListProperty([])
    obsnight_buttons = ListProperty([])
    current_obsnight = ObjectProperty(ird.ObsNight())
    instrument_list = ListProperty([])
    caltype = StringProperty('')
    target_list = ListProperty([])
    current_target = ObjectProperty(ird.ObsTarget())
    file_list = ListProperty([])
    
    def __init__(self, **kwargs):
        super(ObservingScreen, self).__init__(**kwargs)
        self.waiting = WaitingDialog(text='Please wait while the calibration images build, thank you!')
    
    def on_enter(self):
        #idb = shelve.open(instrumentdb)
        #self.instrument_list = idb.keys()
        #idb.close()
        self.instrument_list = instrumentdb.keys()
        #odb = shelve.open(obsrundb)
        odb = obsrundb
        #self.obsids = {x:ird.deserialize(odb[x])[x] for x in odb}
        self.obsids = {x:odb[x] for x in odb}
        self.obsrun_list = odb.keys()
        #odb.close()
        self.obsrun_buttons = [Button(text=x, size_hint_y = None, height = 30) \
            for x in self.obsrun_list]
    
    def on_pre_leave(self):
        theapp = App.get_running_app()
        pairs = pair_dithers(self.current_target.dither)
        tmp = [(self.current_target.images[x] for x in y) for y in pairs]
        theapp.extract_pairs = [[self.current_target.images[x] for x in y] for y in pairs]
        theapp.current_target = self.current_target
        theapp.current_paths = {'cal':self.current_obsnight.calpath, 
            'raw':self.current_obsnight.rawpath, 'out':self.current_obsnight.outpath}
        theapp.current_night = self.current_obsnight
    
    def set_obsrun(self):
        run_id = self.ids.obsrun.text
        if run_id not in self.obsids:
            while True:
                run_db = 'storage/'+str(uuid.uuid4())+'.json'
                if not glob.glob(run_db+'*'):
                    break
            self.obsids[run_id] = run_db
            #odb = shelve.open(obsrundb)
            obsrundb[run_id] = {run_id:run_db}
            #odb.close()
        else:
            run_db = self.obsids[run_id][run_id]
        self.current_obsrun = ird.ObsRun(runid=run_id)
        self.rdb = IRStore(run_db)
        #rdb = shelve.open(run_db)
        tmp = []
        for r in self.rdb:
            #ds = ird.deserialize(self.rdb[r])
            #tmp.append((str(ds.date), ds))
            tmp.append((str(self.rdb[r][r].date), self.rdb[r][r]))
        tmp = dict(tmp)
        #tmp = dict([(str(ird.deserialize(self.rdb[r])['date']),ird.deserialize(self.rdb[r])) for r in self.rdb])
        self.current_obsrun = self.current_obsrun._replace(nights=tmp)
        #for r in self.rdb:
        #    tmp = ird.ObsNight(**self.rdb[r])
        #    print tmp._asdict()
        #    ird.add_to(self.current_obsrun, tmp)
        #rdb.close()
        self.obsnight_list = self.current_obsrun.nights.keys()
        self.obsnight_buttons = [Button(text=x, size_hint_y = None, height = 30) \
            for x in self.obsnight_list]
    
    def set_obsnight(self):
        night_id = self.ids.obsnight.text
        if night_id == '' or self.current_obsrun.runid == '' \
                        or night_id == 'Observation Date':
            return
        if night_id not in self.obsnight_list:
            self.obsnight_list.append(night_id)
            self.obsnight_buttons.append(Button(text = night_id, \
                size_hint_y = None, height = 30))
            self.current_obsnight = ird.ObsNight(date = night_id)
            #self.current_obsrun.addnight(self.current_obsnight)
            ird.add_to(self.current_obsrun, self.current_obsnight)
        else:
            #self.current_obsnight = self.current_obsrun.get_night(night_id)
            self.current_obsnight = ird.get_from(self.current_obsrun, night_id)
            self.ids.rawpath.text = self.current_obsnight.rawpath
            self.ids.outpath.text = self.current_obsnight.outpath
            self.ids.calpath.text = self.current_obsnight.calpath
            self.ids.fformat.text = self.current_obsnight.filestub
            self.set_filelist()
        #rdb = shelve.open(self.obsids[self.current_obsrun.runid])
        #rdb = JsonStore(self.obsids[self.current_obsrun.runid])
        for night in self.obsnight_list:
            #self.rdb[night] = ird.serialize(ird.get_from(self.current_obsrun, night))
            self.rdb[night] = {night:ird.get_from(self.current_obsrun, night)}
        #rdb.close()
        self.target_list = self.current_obsnight.targets.keys()
    
    def pick_rawpath(self):
        popup = DirChooser()
        popup.bind(on_dismiss=lambda x: self.setpath('raw',popup.chosen_directory))
        popup.open()
    
    def setpath(self, which, dir):
        if which == 'raw':
            self.current_obsnight = self.current_obsnight._replace(rawpath=dir)
            self.ids.rawpath.text = dir
        elif which == 'out':
            self.current_obsnight = self.current_obsnight._replace(outpath=dir)
            self.ids.outpath.text = dir
        elif which == 'cal':
            self.current_obsnight = self.current_obsnight._replace(calpath=dir)
            self.ids.calpath.text = dir
        
    def pick_outpath(self):
        popup = DirChooser()
        popup.bind(on_dismiss=lambda x: self.setpath('out',popup.chosen_directory))
        popup.open()
        
    def pick_calpath(self):
        popup = DirChooser()
        popup.bind(on_dismiss=lambda x: self.setpath('cal',popup.chosen_directory))
        popup.open()
    
    def check_filestub(self, stub):
        placeholder = '#'
        reg = placeholder+'+'
        if len(re.findall(reg, stub)) != 1:
            popup = WarningDialog(text = "File format is not valid; must use '#' as placeholder only")
            popup.open()
            return
        self.current_obsnight = self.current_obsnight._replace(filestub=stub) 
    
    def set_caltype(self, caltype):
        if caltype == 'Flats (lamps ON)':
            cout, flist = self.current_obsnight.flaton if self.current_obsnight.flaton else ('Not yet created', '')
        elif caltype == 'Flats (lamps OFF)':
            cout, flist = self.current_obsnight.flatoff if self.current_obsnight.flatoff else ('Not yet created', '')
        elif caltype == 'Arc Lamps':
            cout, flist = self.current_obsnight.cals if self.current_obsnight.cals else ('Not yet created', '')
        self.ids.calfiles.text = flist
        self.ids.calout.text = cout
    
    def set_calfile(self, flist):
        caltype = self.ids.caltypes.text
        if caltype == 'Flats (lamps ON)':
            flatfile = path.join(self.current_obsnight.calpath,self.current_obsnight.date+'-FlatON.fits')
            tmp = self.current_obsnight.flaton
            if tmp:
                tmp[1] = flist
            else:
                tmp = ['',flist]
            try:
                if FitsImage(flatfile).header['FILES'] == flist:
                    tmp[0] = flatfile
                    self.ids.calout.txt = flatfile
            except:
                pass
            self.current_obsnight = self.current_obsnight._replace(flaton=tmp)
        elif caltype == 'Flats (lamps OFF)':
            flatfile = path.join(self.current_obsnight.calpath,self.current_obsnight.date+'-FlatOFF.fits')
            tmp = self.current_obsnight.flatoff
            if tmp:
                tmp[1] = flist
            else:
                tmp = ['',flist]
            try:
                if FitsImage(flatfile).header['FILES'] == flist:
                    tmp[0] = flatfile
            except:
                pass
            self.current_obsnight = self.current_obsnight._replace(flatoff=tmp)
        elif caltype == 'Arc Lamps':
            flatfile = path.join(self.current_obsnight.calpath,self.current_obsnight.date+'-Wavecal.fits')
            tmp = self.current_obsnight.cals
            if tmp:
                tmp[1] = flist
            else:
                tmp = ['',flist]
            try:
                if FitsImage(flatfile).header['FILES'] == flist:
                    tmp[0] = flatfile
            except:
                pass
            self.current_obsnight = self.current_obsnight._replace(cals=tmp)
        
    def make_cals(self):
        if not self.current_obsnight.rawpath:
            return
        caltype = self.ids.caltypes.text
        flist = self.ids.calfiles.text
        self.waiting.open()
        if caltype == 'Flats (lamps ON)':
            t = Thread(target = self.imstack_wrapper, args=(self.current_obsnight.flaton, flist, \
                self.current_obsnight.date+'-FlatON.fits'))
            t.start()
        elif caltype == 'Flats (lamps OFF)':
            t = Thread(target = self.imstack_wrapper, args=(self.current_obsnight.flatoff, flist, \
                self.current_obsnight.date+'-FlatOFF.fits'))
            t.start()
        elif caltype == 'Arc Lamps':
            t = Thread(target = self.imstack_wrapper, args=(self.current_obsnight.cals, flist, \
                self.current_obsnight.date+'-Wavecal.fits'))
            t.start()
            
    def imstack_wrapper(self, target, flist, outp):
        raw = self.current_obsnight.rawpath
        cal = self.current_obsnight.calpath
        stub = self.current_obsnight.filestub
        ird.image_stack(flist, path.join(raw, stub), output = path.join(cal, outp))
        target[:] = [outp, flist]
        self.ids.calfiles.text = flist
        self.ids.calout.text = outp
        self.waiting.dismiss()
    
    def save_night(self):
        tmp = self.current_obsrun.nights
        tmp[self.current_obsnight.date] = self.current_obsnight
        self.current_obsrun = self.current_obsrun._replace(nights=tmp)
        #tmp = ird.ObsNight(**self.current_obsnight._asdict())
        #self.current_obsrun.nights[tmp.date] = tmp
        #ird.add_to(self.current_obsrun, self.current_obsnight)
        #rdb = shelve.open(self.obsids[self.current_obsrun.runid])
        #rdb = JsonStore(self.obsids[self.current_obsrun.runid])
        for night in self.obsnight_list:
            #self.rdb[night] = ird.serialize(ird.get_from(self.current_obsrun, night))
            self.rdb[night] = {night:ird.get_from(self.current_obsrun, night)}
        self.rdb.store_sync()
        #rdb.close()
        
    def set_target(self):
        target_id = self.ids.targs.text
        self.current_target = self.current_obsnight.targets[target_id]
        self.set_filelist()
    
    def add_target(self):
        popup = AddTarget(instrumentlist = self.instrument_list)
        popup.open()
        popup.bind(on_dismiss = lambda x: self.update_targets(popup.target_args) \
            if popup.target_args else None)
    
    def update_targets(self, targs):
        targs['images'], targs['dither'] = ird.parse_filestring(targs['filestring'], \
            path.join(self.current_obsnight.rawpath,self.current_obsnight.filestub))
        self.current_target = ird.ObsTarget(**targs)
        tmp = self.current_obsnight.targets
        tmp[self.current_target.targid] = self.current_target
        self.current_obsnight = self.current_obsnight._replace(targets=tmp)
        #ird.add_to(self.current_obsnight, self.current_target)
        self.target_list = self.current_obsnight.targets.keys()
        self.ids.targs.text = self.current_target.targid
        self.set_filelist()
        #rdb = shelve.open(self.obsids[self.current_obsrun.runid])
        #rdb = JsonStore(self.obsids[self.current_obsrun.runid])
        #self.rdb[self.current_obsnight.date] = ird.serialize(self.current_obsnight)
        self.rdb[self.current_obsnight.date] = {self.current_obsnight.date:self.current_obsnight}
        #rdb.close()
    
    def set_filelist(self):
        self.ids.obsfiles.clear_widgets()
        self.file_list = []
        for file, dither in zip(self.current_target.images, self.current_target.dither):
            tmp = ObsfileInsert(obsfile = file, dithertype = dither)
            self.file_list.append(tmp)
            self.ids.obsfiles.add_widget(tmp)
    
    def save_target(self):
        self.current_target = self.current_target._replace(dither=[x.dithertype for x in self.file_list],
            notes=self.ids.tnotes.text)
        #just make sure everything is propagating correctly
        self.current_obsnight.targets[self.current_target.targid] = self.current_target
        self.current_obsrun.nights[self.current_obsnight.date] = self.current_obsnight
        #rdb = shelve.open(self.obsids[self.current_obsrun.runid])
        #rdb = JsonStore(self.obsids[self.current_obsrun.runid])
        for night in self.obsnight_list:
            #self.rdb[night] = ird.serialize(ird.get_from(self.current_obsrun, night))
            self.rdb[night] = ird.get_from(self.current_obsrun, night)
        #rdb.close()
        self.target_list = self.current_obsnight.targets.keys()
Пример #2
0
 def __init__(self, **kwargs):
     super(ObservingScreen, self).__init__(**kwargs)
     self.waiting = WaitingDialog(text='Please wait while the calibration images build, thank you!')
Пример #3
0
 def __init__(self, **kwargs):
     super(TracefitScreen, self).__init__(**kwargs)
     self.waiting = WaitingDialog(text='Please wait while the extraction is calculated, thank you!')
Пример #4
0
class TracefitScreen(IRScreen):
    paths = DictProperty([])
    itexture = ObjectProperty(Texture.create(size = (2048, 2048)))
    iregion = ObjectProperty(None)
    current_impair = ObjectProperty(None)
    current_extraction = ObjectProperty(None)
    extractregion = ObjectProperty(None, force_dispatch=True, allownone=True)
    tplot = ObjectProperty(MeshLinePlot(color=[1,1,1,1]))
    current_target = ObjectProperty(None)
    pairstrings = ListProperty([])
    apertures = DictProperty({'pos':[], 'neg':[]})
    drange = ListProperty([0,1024])
    tracepoints = ListProperty([])
    trace_axis = NumericProperty(0)
    fit_params = DictProperty({})
    trace_lines = ListProperty([MeshLinePlot(color=[0,0,1,1]),MeshLinePlot(color=[0,1,1,1])])
    current_flats = ObjectProperty(None, force_dispatch=True, allownone=True)
    theapp = ObjectProperty(None)
    trace_info = DictProperty({})
    rtx = ListProperty([0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0])
    
    def __init__(self, **kwargs):
        super(TracefitScreen, self).__init__(**kwargs)
        self.waiting = WaitingDialog(text='Please wait while the extraction is calculated, thank you!')
    
    def on_enter(self):
        self.pairstrings = ['{0} - {1}'.format(*map(os.path.basename,y)) for y in self.theapp.extract_pairs]
        header = [0]
        flat = os.path.join(self.paths['cal'],'Flat.fits')
        if not os.path.exists(flat):
            if self.theapp.current_night.flaton and self.theapp.current_night.flaton[0]:
                fon = fitsimage(str(os.path.join(self.paths['cal'], self.theapp.current_night.flaton[0])), header)
                if self.theapp.current_night.flatoff and self.theapp.current_night.flatoff[0]:
                    foff = fitsimage(str(os.path.join(self.paths['cal'], self.theapp.current_night.flatoff[0])), header)
                    im_subtract(fon, foff, outputfile = flat)
                else:
                    fits.writeto(flat, fon, header=header[0])
        self.current_flats = fitsimage(flat, header)
        self.theapp.current_flats = self.current_flats
        self.pair_index = -1
    
    def on_pre_leave(self):
        if self.current_target: self.theapp.current_target = self.current_target
        if self.current_extraction: self.theapp.current_extraction = self.current_extraction
        self.theapp.save_current()

    def set_imagepair(self, val):
        if not self.theapp.current_target:
            popup = AlertDialog(text='You need to select a target (on the Observing Screen) before proceeding!')
            popup.open()
            return
        if self.pairstrings.index(val) == self.pair_index:
            return
        self.clear_pair()
        self.pair_index = self.pairstrings.index(val)
        pair = self.theapp.extract_pairs[self.pair_index]
        extract_state = self.theapp.current_target.extractions.get(str(hash(':'.join(pair))),None)
        if extract_state:
            self.current_extraction = extract_state #extraction_from_state(extract_state, self.current_flats)
        else:
            popup = AlertDialog(text="You have to select an extraction region for this image pair before you can move on to this step.")
            popup.open()
            return
        self.current_impair = scalable_image(self.current_extraction.diff)
        self.trace_axis = int(tracedir(self.current_target.instrument_id) == 'horizontal')

        idata = ''.join(map(chr,self.current_impair.scaled))
        self.itexture.blit_buffer(idata, colorfmt='luminance', bufferfmt='ubyte', \
            size = self.current_impair.dimensions)
        self.extractregion = self.current_extraction.extract_region
        reg = self.current_extraction.region[:]
        reg[2] = reg[2] - reg[0]
        reg[3] = reg[3] - reg[1]
        #if self.trace_axis:
        #    self.iregion = self.itexture.get_region(*reg)
        #else:
        #    self.iregion = self.itexture.get_region(reg[1],reg[0],reg[3],reg[2])
        self.iregion = self.itexture.get_region(*reg)
        self.rtx = [0.0] + [[0.0, 1.0][self.trace_axis]]*2 + [1.0, 1.0] + [[1.0, 0.0][self.trace_axis]]*2 + [0.0]
        dims = [[0,0],list(self.extractregion.shape)]
        dims[0][self.trace_axis] = 0.4 * self.extractregion.shape[self.trace_axis]
        dims[1][self.trace_axis] = 0.6 * self.extractregion.shape[self.trace_axis]
        self.tracepoints = twod_to_oned(self.extractregion[dims[0][0]:dims[1][0]+1,
                                                           dims[0][1]:dims[1][1]+1], axis=self.trace_axis)
        points = replace_nans(np.array(self.tracepoints))
        self.tplot.points = zip(np.arange(points.size), points)
        self.tracepoints = points
        self.drange = [float(points.min()), float(points.max())]
        self.ids.the_graph.add_plot(self.tplot)
        if self.current_extraction.name in self.trace_info:
            info = self.trace_info[self.current_extraction.name]
            for trace in info['ap']['pos']:
                self.add_postrace(val=trace)
            for trace in info['ap']['neg']:
                self.add_negtrace(val=trace)
            self.trace_lines = info['lines']
            self.ids.the_graph.add_plot(self.trace_lines[0])
            self.ids.the_graph.add_plot(self.trace_lines[1])
            self.fit_params = info['par']
        else:
            self.trace_info[self.current_extraction.name] = {'ap':{'pos':[], 'neg':[]}, 'lines':[]}
    
    def clear_pair(self):
        if self.current_extraction:
            self.trace_info[self.current_extraction.name] = {
                'ap':{'pos':[t.slider.value for t in self.apertures['pos']],
                      'neg':[t.slider.value for t in self.apertures['neg']]},
                'lines':self.trace_lines,
                'par': self.fit_params}
        for ap in self.apertures['pos']:
            self.remtrace('pos', ap)
        for ap in self.apertures['neg']:
            self.remtrace('neg', ap)
        self.ids.the_graph.remove_plot(self.trace_lines[0])
        self.ids.the_graph.remove_plot(self.trace_lines[1])
        self.trace_lines = [MeshLinePlot(color=[0,0,1,1]),MeshLinePlot(color=[0,1,1,1])]
        self.fit_params = {}
    
    def add_postrace(self, val=None):
        tp = np.ascontiguousarray(self.tracepoints)
        peaks = findpeaks1d(tp, pn='pos')
        new_peak = float(peaks)
        peakheight = interp(tp, np.arange(tp.size, dtype=np.float64), np.array([new_peak]))
        plot = MeshLinePlot(color=[0,1,0,1], points=[(new_peak, 0), (new_peak, peakheight)])
        self.ids.the_graph.add_plot(plot)
        newspin = ApertureSlider(aperture_line = plot, tfscreen = self)
        newspin.slider.range = [0, len(self.tracepoints)-1]
        newspin.slider.step = 0.1
        newspin.slider.value = val or new_peak
        newspin.trash.bind(on_press = lambda x: self.remtrace('pos',newspin))
        self.ids.postrace.add_widget(newspin)
        self.apertures['pos'].append(newspin)
        
    def add_negtrace(self, val=None):
        tp = np.ascontiguousarray(self.tracepoints)
        peaks = findpeaks1d(tp, pn='neg')
        new_peak = float(peaks)
        peakheight = interp(tp, np.arange(tp.size, dtype=np.float64), np.array([new_peak]))
        plot = MeshLinePlot(color=[1,0,0,1], points=[(new_peak, 0), (new_peak, peakheight)])
        self.ids.the_graph.add_plot(plot)
        newspin = ApertureSlider(aperture_line = plot, tfscreen = self)
        newspin.slider.range = [0, len(self.tracepoints)-1]
        newspin.slider.step = 0.1
        newspin.slider.value = val or new_peak
        newspin.trash.bind(on_press = lambda x: self.remtrace('neg',newspin))
        self.ids.negtrace.add_widget(newspin)
        self.apertures['neg'].append(newspin)
    
    def remtrace(self, which, widg):
        self.ids.the_graph.remove_plot(widg.aperture_line)
        self.apertures[which].remove(widg)
        if which == 'pos':
            self.ids.postrace.remove_widget(widg)
        else:
            self.ids.negtrace.remove_widget(widg)
        
    def set_psf(self):
        popup = SetFitParams(fit_args = self.fit_params)
        popup.bind(on_dismiss = lambda x: self.setfp(popup.fit_args))
        popup.open()
    
    def setfp(self, args):
        self.fit_params = args
        
    def fit_trace(self):
        if not self.fit_params or self.fit_params['shape'] not in ('Gaussian','Lorentzian'):
            popup = AlertDialog(text='Make sure you set up your fit parameters!')
            popup.open()
            return
        stripe = np.array(self.tracepoints, dtype=np.float64)
        wid = self.fit_params['wid']
        self.current_extraction.mtype = self.fit_params['shape']
        stripex = np.arange(stripe.size, dtype=float)
        pos = tuple((interp(stripe, stripex.astype(np.float64), np.array([x.slider.value], dtype=np.float64)), 
                     x.slider.value, 
                     wid) for x in self.apertures['pos']) + \
              tuple((interp(stripe, stripex.astype(np.float64), np.array([x.slider.value], dtype=np.float64)), 
                     x.slider.value, 
                     wid) for x in self.apertures['neg'])
        for x in self.trace_lines:
            if x in self.ids.the_graph.plots:
                self.ids.the_graph.remove_plot(x)
        if self.fit_params.get('man',False):
            popup = DefineTrace(npos=len(self.apertures['pos']), \
                nneg=len(self.apertures['neg']), imtexture = self.iregion)
            popup.bind(on_dismiss = self.manual_trace(popup.tracepoints))
            popup.open()
            return
        peaks = fitpeaks1d(stripe, pos, model_type=self.fit_params['shape']).individual()
        self.fit_params['model'] = [p.parameters.tolist() for p in peaks]
        self.fit_params['pmodel'] = [p.parameters.tolist() for p in peaks if p.amplitude > 0]
        self.fit_params['nmodel'] = [p.parameters.tolist() for p in peaks if p.amplitude < 0]
        pmod = CompositeModel(tuple(self.fit_params['pmodel']), self.fit_params['shape'])
        nmod = CompositeModel(tuple(self.fit_params['nmodel']), self.fit_params['shape'])
        self.trace_lines[0].points = zip(stripex, pmod(stripex))
        self.trace_lines[1].points = zip(stripex, nmod(stripex))
        self.ids.the_graph.add_plot(self.trace_lines[0])
        self.ids.the_graph.add_plot(self.trace_lines[1])
    
    def fix_distort(self):
        popup = AlertDialog(text='This is not yet functional')
        popup.open()
        return
        if not (self.fit_params.get('model',False)):
            popup = AlertDialog(text='Make sure you fit the trace centers first!')
            popup.open()
            return
        trace = self.current_extraction.fit_trace(self.fit_params['model'], np.empty(0), False)
        self.current_extraction.fix_distortion(trace)
        self.theapp.current_target.extractions[self.current_extraction.name] = self.current_extraction
        self.set_imagepair(self.pairstrings[self.pair_index])
        self.fit_params['model'] = None
        self.theapp.current_extraction = self.current_extraction
        self.theapp.save_current()
        
    
    def manual_trace(self, traces):
        pass #need to figure out how to apply these
    
    def extract_spectrum(self):
        if not self.fit_params.get('model',False):
            popup = AlertDialog(text='Make sure you fit the trace centers first!')
            popup.open()
            return
        self.waiting.open()
        self.lamp = np.empty([2,2], dtype=np.float64)
        self.lamps = False
        if self.theapp.current_night.cals:
            self.lamp = self.theapp.current_night.cals if not self.current_flats \
                else im_divide(self.theapp.current_night.cals, self.current_flats)
            self.lamp = self.lamp[self.region[1]:self.region[3]+1,self.region[0]:self.region[2]+1]
            self.lamps = True
        t = Thread(target=self.extract_wrapper)
        t.start()
    
    def extract_wrapper(self):
        #need a calibration, too
        self.current_extraction.fit_trace(self.fit_params['model'], self.lamp, lamps=self.lamps, extract=True)
        self.waiting.dismiss()
        Clock.schedule_once(self.save_spectra, 0.1)
    
    def save_spectra(self, *args, **kw):
        #write uncalibrated spectra to fits files (will update after calibration)
        lamps = self.lamps
        h1, h2 = [0], [0]
        fitsimage(self.current_extraction.file1, h1)
        h1 = h1[0]
        pstub = os.path.join(self.paths['out'], 
                             re.sub('.fits','-ap%i',
                                    os.path.basename(self.current_extraction.file1)))
        pstub_sky = os.path.join(self.paths['cal'], 
                             re.sub('.fits','-ap%i',
                                    os.path.basename(self.current_extraction.file1)))
        ext = ('.fits','-sky.fits','-lamp.fits')
        h1['EXREGX1'] = (self.current_extraction.region[0], 'extraction region coordinate X1')
        h1['EXREGY1'] = (self.current_extraction.region[1], 'extraction region coordinate Y1')
        h1['EXREGX2'] = (self.current_extraction.region[2], 'extraction region coordinate X2')
        h1['EXREGY2'] = (self.current_extraction.region[3], 'extraction region coordinate Y2')
        fitsimage(self.current_extraction.file2, h2)
        h2 = h2[0]
        nstub = os.path.join(self.paths['out'], 
                             re.sub('.fits','-ap%i',
                                    os.path.basename(self.current_extraction.file2)))
        nstub_sky = os.path.join(self.paths['cal'], 
                             re.sub('.fits','-ap%i',
                                    os.path.basename(self.current_extraction.file2)))
        h2['EXREGX1'] = (self.current_extraction.region[0], 'extraction region coordinate X1')
        h2['EXREGY1'] = (self.current_extraction.region[1], 'extraction region coordinate Y1')
        h2['EXREGX2'] = (self.current_extraction.region[2], 'extraction region coordinate X2')
        h2['EXREGY2'] = (self.current_extraction.region[3], 'extraction region coordinate Y2')
        
        for j, typ in enumerate(['spec', 'tell']+['lamp']*lamps):
            specs = self.current_extraction.extracts[typ]
            if len(specs.shape) == 1:
                if self.fit_params['model'][0][0] > 0:
                    if typ == 'spec':
                        popup = ExamineSpectrum(target=self.current_target,
                                                spectrum=specs, header=h1,
                                                outfile=(pstub+ext[j])%0)
                        popup.open()
                    else:
                        fits.writeto((pstub_sky+ext[j])%0, specs, header=h1, clobber=True)
                else:
                    if typ == 'spec':
                        popup = ExamineSpectrum(target=self.current_target,
                                                spectrum=specs, header=h2,
                                                outfile=(nstub+ext[j])%0)
                        popup.open()
                    else:
                        fits.writeto((nstub_sky+ext[j])%0, specs, header=h2, clobber=True)
            else:
                specs = specs.T
                for i, ap in enumerate(specs):
                    if self.fit_params['model'][i][0] > 0:
                        if typ == 'spec':
                            popup = ExamineSpectrum(target=self.current_target,
                                                spectrum=ap, header=h1,
                                                outfile=(pstub+ext[j])%i)
                            popup.open()
                        else:
                            fits.writeto((pstub_sky+ext[j])%i, ap, header=h1, clobber=True)
                    else:
                        if typ == 'spec':
                            popup = ExamineSpectrum(target=self.current_target,
                                                spectrum=ap, header=h2,
                                                outfile=(nstub+ext[j])%i)
                            popup.open()
                        else:
                            fits.writeto((nstub_sky+ext[j])%i, -ap, header=h2, clobber=True)
        self.theapp.current_target = self.current_target
        self.theapp.current_extraction = self.current_extraction
        self.theapp.save_current()
        self.extract_done = False
Пример #5
0
 def __init__(self, **kwargs):
     super(ObservingScreen, self).__init__(**kwargs)
     self.rdb = None
     self.waiting = WaitingDialog()
Пример #6
0
class ObservingScreen(IRScreen):
    obsids = DictProperty({})
    obsrun_list = ListProperty([])
    obsrun_buttons = ListProperty([])
    current_obsrun = ObjectProperty(ObsRun())
    obsnight_list = ListProperty([])
    obsnight_buttons = ListProperty([])
    current_obsnight = ObjectProperty(ObsNight())
    instrument_list = ListProperty([])
    caltype = StringProperty('')
    target_list = ListProperty([])
    current_target = ObjectProperty(ObsTarget())
    file_list = ListProperty([])
    
    def __init__(self, **kwargs):
        super(ObservingScreen, self).__init__(**kwargs)
        self.rdb = None
        self.waiting = WaitingDialog()
    
    def on_enter(self):
        self.instrument_list = instrumentdb.keys()
        self.obsids = {x:obsrundb[x] for x in obsrundb}
        self.obsrun_list = obsrundb.keys()
        self.obsrun_buttons = [Button(text=x, size_hint_y = None, height = 30) \
            for x in self.obsrun_list]
    
    def on_pre_leave(self):
        self.theapp.extract_pairs = [[self.current_target.images.stack_list[x] for x in y] for y in self.current_target.ditherpairs]
        self.theapp.current_target = self.current_target
        self.theapp.current_paths = {'cal':self.current_obsnight.calpath, 
            'raw':self.current_obsnight.rawpath, 'out':self.current_obsnight.outpath}
        self.theapp.current_night = self.current_obsnight
        self.theapp.rdb = self.rdb
    
    def set_obsrun(self):
        if not self.ids.obsrun.text: return
        self.waiting.text = 'Please wait while that observing run loads, thank you!'
        self.waiting.open()
        t = Thread(target=self.obsrun_wrapper)
        t.start()
    
    def obsrun_wrapper(self):
        run_id = self.ids.obsrun.text
        if not run_id: return
        try:
            if run_id not in self.obsids:
                self.rdb = AdHocDB()
                self.obsids[run_id] = self.rdb.fname
                obsrundb[run_id] = self.rdb.fname
            else:
                self.rdb = AdHocDB(self.obsids[run_id])
            self.current_obsrun = ObsRun(runid=run_id)
            self.current_obsrun.nights = {str(self.rdb[r].date):self.rdb[r] for r in self.rdb}
            self.obsnight_list = self.current_obsrun.nights.keys()
            self.obsnight_buttons = [Button(text=x, size_hint_y = None, height = 30) \
                for x in self.obsnight_list]
        except Exception as e:
            print e
            pdb.set_trace()
        self.waiting.dismiss()
    
    def set_obsnight(self):
        night_id = self.ids.obsnight.text
        if night_id == '' or self.current_obsrun.runid == '' \
                        or night_id == 'Observation Date':
            return
        if night_id not in self.obsnight_list:
            self.obsnight_list.append(night_id)
            self.obsnight_buttons.append(Button(text = night_id, \
                size_hint_y = None, height = 30))
            self.current_obsnight = ObsNight(date = night_id)
            self.current_obsrun.add_to(self.current_obsnight)
        else:
            self.current_obsnight = self.current_obsrun.get_from(night_id)
            self.ids.rawpath.text = self.current_obsnight.rawpath
            self.ids.outpath.text = self.current_obsnight.outpath
            self.ids.calpath.text = self.current_obsnight.calpath
            self.ids.fformat.text = self.current_obsnight.filestub
            self.set_filelist()
        for night in self.obsnight_list:
            self.rdb[night] = self.current_obsrun.get_from(night)
        self.target_list = self.current_obsnight.targets.keys()
    
    def pick_rawpath(self):
        popup = DirChooser()
        popup.bind(on_dismiss=lambda x: self.setpath('raw',popup.chosen_directory))
        popup.open()
    
    def setpath(self, which, _dir):
        if which == 'raw':
            self.current_obsnight.rawpath = str(_dir)
            self.ids.rawpath.text = _dir
            if not self.ids.outpath.text:
                self.setpath('out', os.path.join(_dir, 'out'))
            if not self.ids.calpath.text:
                self.setpath('cal', os.path.join(_dir, 'cals'))
        elif which == 'out':
            self.current_obsnight.outpath = str(_dir)
            self.ids.outpath.text = _dir
        elif which == 'cal':
            self.current_obsnight.calpath = str(_dir)
            self.ids.calpath.text = _dir
        
    def pick_outpath(self):
        popup = DirChooser()
        popup.bind(on_dismiss=lambda x: self.setpath('out',popup.chosen_directory))
        popup.open()
        
    def pick_calpath(self):
        popup = DirChooser()
        popup.bind(on_dismiss=lambda x: self.setpath('cal',popup.chosen_directory))
        popup.open()
    
    def check_filestub(self, stub):
        placeholder = '#'
        reg = placeholder+'+'
        if len(re.findall(reg, stub)) != 1:
            popup = AlertDialog(text = "File format is not valid; must use '#' as placeholder only")
            popup.open()
            return
        self.current_obsnight.filestub = stub
    
    def set_caltype(self, caltype):
        if caltype == 'Flats (lamps ON)':
            cout, flist = self.current_obsnight.flaton if self.current_obsnight.flaton else ('Not yet created', '')
        elif caltype == 'Flats (lamps OFF)':
            cout, flist = self.current_obsnight.flatoff if self.current_obsnight.flatoff else ('Not yet created', '')
        elif caltype == 'Arc Lamps':
            cout, flist = self.current_obsnight.cals if self.current_obsnight.cals else ('Not yet created', '')
        self.ids.calfiles.text = flist
        self.ids.calout.text = cout
    
    def set_calfile(self, flist):
        caltype = self.ids.caltypes.text[-2]
        if caltype == 'e': return
        flatfile = os.path.join(self.current_obsnight.calpath,
                                self.current_obsnight.date + {'N':'-FlatON',
                                                              'F':'-FlatOFF',
                                                              'p':'-Wavecal'}[caltype]+'.fits')
        tmp = {'N':self.current_obsnight.flaton, 
               'F':self.current_obsnight.flatoff, 
               'p':self.current_obsnight.cals}[caltype]
        if tmp:
            tmp[1] = flist
        else:
            tmp = ['',flist]
        try:
            header = [0]
            fitsimage(flatfile, header)
            header = header[0]
            if header['FILES'] == flist:
                tmp[0] = flatfile
                self.ids.calout.txt = flatfile
        except:
            pass
        setattr(self.current_obsnight, {'N':'flaton', 'F':'flatoff', 'p':'cals'}[caltype], tmp)
        
    def make_cals(self):
        if not self.current_obsnight.rawpath:
            return
        caltype = self.ids.caltypes.text
        flist = self.ids.calfiles.text
        self.waiting.text = 'Please wait while the calibration images build, thank you!'
        self.waiting.open()
        if caltype == 'Flats (lamps ON)':
            t = Thread(target=self.imstack_wrapper, args=(self.current_obsnight.flaton, flist, \
                self.current_obsnight.date+'-FlatON.fits'))
            t.start()
        elif caltype == 'Flats (lamps OFF)':
            t = Thread(target=self.imstack_wrapper, args=(self.current_obsnight.flatoff, flist, \
                self.current_obsnight.date+'-FlatOFF.fits'))
            t.start()
        elif caltype == 'Arc Lamps':
            t = Thread(target=self.imstack_wrapper, args=(self.current_obsnight.cals, flist, \
                self.current_obsnight.date+'-Wavecal.fits'))
            t.start()
            
    def imstack_wrapper(self, target, flist, outp):
        print 'making flats'
        raw = self.current_obsnight.rawpath
        cal = self.current_obsnight.calpath
        stub = self.current_obsnight.filestub
        imstack = ImageStack(flist, os.path.join(raw, stub))
        imstack.medcombine(outputfile=os.path.join(cal, outp))
        print 'flats made'
        target[:] = [outp, flist]
        self.ids.calfiles.text = flist
        self.ids.calout.text = outp
        self.waiting.dismiss()
    
    def save_night(self):
        tmp = self.current_obsrun.nights
        tmp[self.current_obsnight.date] = self.current_obsnight
        self.current_obsrun.nights = tmp
        for night in self.obsnight_list:
            self.rdb[night] = self.current_obsrun.get_from(night)
        self.rdb.store_sync()
        
    def set_target(self):
        target_id = self.ids.targs.text
        self.current_target = self.current_obsnight.targets[target_id]
        self.set_filelist()
    
    def add_target(self):
        popup = AddTarget(instrumentlist = self.instrument_list)
        popup.open()
        popup.bind(on_dismiss = lambda x: self.update_targets(popup.target_args) \
            if popup.target_args else None)

    def update_targets(self, targs):
        targs['images'] = ImageStack(targs['filestring'], 
                                     os.path.join(self.current_obsnight.rawpath,
                                                        self.current_obsnight.filestub))
        targs['dither'] = targs['images'].dithers
        targs['ditherpairs'] = targs['images'].ditherpairs
        self.current_target = ObsTarget(**targs)
        tmp = self.current_obsnight.targets
        tmp[self.current_target.targid] = self.current_target
        self.current_obsnight.targets = tmp
        self.target_list = self.current_obsnight.targets.keys()
        self.ids.targs.text = self.current_target.targid
        self.set_filelist()
        self.rdb[self.current_obsnight.date] = self.current_obsnight
    
    def set_filelist(self):
        self.ids.obsfiles.clear_widgets()
        self.file_list = []
        for f, dither in zip(self.current_target.images.stack_list, self.current_target.dither):
            tmp = ObsfileInsert(obsfile = f, dithertype = dither)
            self.file_list.append(tmp)
            self.ids.obsfiles.add_widget(tmp)
    
    def save_target(self):
        self.current_target.dither = [x.dithertype for x in self.file_list]
        self.current_target.notes = self.ids.tnotes.text
        #just make sure everything is propagating correctly
        self.current_obsnight.targets[self.current_target.targid] = self.current_target
        self.current_obsrun.nights[self.current_obsnight.date] = self.current_obsnight
        for night in self.obsnight_list:
            self.rdb[night] = self.current_obsrun.get_from(night)
        self.target_list = self.current_obsnight.targets.keys()
Пример #7
0
class ObservingScreen(IRScreen):
    obsids = DictProperty({})
    obsrun_list = ListProperty([])
    obsrun_buttons = ListProperty([])
    current_obsrun = ObjectProperty(ObsRun())
    obsnight_list = ListProperty([])
    obsnight_buttons = ListProperty([])
    current_obsnight = ObjectProperty(ObsNight())
    instrument_list = ListProperty([])
    caltype = StringProperty("")
    target_list = ListProperty([])
    current_target = ObjectProperty(ObsTarget())
    file_list = ListProperty([])

    def __init__(self, **kwargs):
        super(ObservingScreen, self).__init__(**kwargs)
        self.waiting = WaitingDialog(text="Please wait while the calibration images build, thank you!")

    def on_enter(self):
        self.instrument_list = instrumentdb.keys()
        self.obsids = {x: obsrundb[x] for x in obsrundb}
        self.obsrun_list = obsrundb.keys()
        self.obsrun_buttons = [Button(text=x, size_hint_y=None, height=30) for x in self.obsrun_list]

    def on_pre_leave(self):
        pairs = self.current_target.ditherpairs
        self.theapp.extract_pairs = [[self.current_target.images[x] for x in y] for y in pairs]
        self.theapp.current_target = self.current_target
        self.theapp.current_paths = {
            "cal": self.current_obsnight.calpath,
            "raw": self.current_obsnight.rawpath,
            "out": self.current_obsnight.outpath,
        }
        self.theapp.current_night = self.current_obsnight

    def set_obsrun(self):
        run_id = self.ids.obsrun.text
        if run_id not in self.obsids:
            self.rdb = AdHocDB()
            self.obsids[run_id] = self.rdb.fname
            obsrundb[run_id] = {run_id: self.rdb.fname}
        else:
            self.rdb = AdHocDB(self.obsids[run_id][run_id])
        self.current_obsrun = ObsRun(runid=run_id)
        tmp = {str(self.rdb[r][r].date): self.rdb[r][r] for r in self.rdb}
        self.current_obsrun = self.current_obsrun._replace(nights=tmp)
        self.obsnight_list = self.current_obsrun.nights.keys()
        self.obsnight_buttons = [Button(text=x, size_hint_y=None, height=30) for x in self.obsnight_list]

    def set_obsnight(self):
        night_id = self.ids.obsnight.text
        if night_id == "" or self.current_obsrun.runid == "" or night_id == "Observation Date":
            return
        if night_id not in self.obsnight_list:
            self.obsnight_list.append(night_id)
            self.obsnight_buttons.append(Button(text=night_id, size_hint_y=None, height=30))
            self.current_obsnight = ObsNight(date=night_id)
            self.current_obsrun.add_to(self.current_obsnight)
        else:
            self.current_obsnight = self.current_obsrun.get_from(night_id)
            self.ids.rawpath.text = self.current_obsnight.rawpath
            self.ids.outpath.text = self.current_obsnight.outpath
            self.ids.calpath.text = self.current_obsnight.calpath
            self.ids.fformat.text = self.current_obsnight.filestub
            self.set_filelist()
        for night in self.obsnight_list:
            self.rdb[night] = {night: self.current_obsrun.get_from(night)}
        self.target_list = self.current_obsnight.targets.keys()

    def pick_rawpath(self):
        popup = DirChooser()
        popup.bind(on_dismiss=lambda x: self.setpath("raw", popup.chosen_directory))
        popup.open()

    def setpath(self, which, dir):
        if which == "raw":
            self.current_obsnight = self.current_obsnight._replace(rawpath=dir)
            self.ids.rawpath.text = dir
        elif which == "out":
            self.current_obsnight = self.current_obsnight._replace(outpath=dir)
            self.ids.outpath.text = dir
        elif which == "cal":
            self.current_obsnight = self.current_obsnight._replace(calpath=dir)
            self.ids.calpath.text = dir

    def pick_outpath(self):
        popup = DirChooser()
        popup.bind(on_dismiss=lambda x: self.setpath("out", popup.chosen_directory))
        popup.open()

    def pick_calpath(self):
        popup = DirChooser()
        popup.bind(on_dismiss=lambda x: self.setpath("cal", popup.chosen_directory))
        popup.open()

    def check_filestub(self, stub):
        placeholder = "#"
        reg = placeholder + "+"
        if len(re.findall(reg, stub)) != 1:
            popup = AlertDialog(text="File format is not valid; must use '#' as placeholder only")
            popup.open()
            return
        self.current_obsnight = self.current_obsnight._replace(filestub=stub)

    def set_caltype(self, caltype):
        if caltype == "Flats (lamps ON)":
            cout, flist = self.current_obsnight.flaton if self.current_obsnight.flaton else ("Not yet created", "")
        elif caltype == "Flats (lamps OFF)":
            cout, flist = self.current_obsnight.flatoff if self.current_obsnight.flatoff else ("Not yet created", "")
        elif caltype == "Arc Lamps":
            cout, flist = self.current_obsnight.cals if self.current_obsnight.cals else ("Not yet created", "")
        self.ids.calfiles.text = flist
        self.ids.calout.text = cout

    def set_calfile(self, flist):
        caltype = self.ids.caltypes.text
        if caltype == "Flats (lamps ON)":
            flatfile = os.path.join(self.current_obsnight.calpath, self.current_obsnight.date + "-FlatON.fits")
            tmp = self.current_obsnight.flaton
            if tmp:
                tmp[1] = flist
            else:
                tmp = ["", flist]
            try:
                if FitsImage(flatfile).header["FILES"] == flist:
                    tmp[0] = flatfile
                    self.ids.calout.txt = flatfile
            except:
                pass
            self.current_obsnight = self.current_obsnight._replace(flaton=tmp)
        elif caltype == "Flats (lamps OFF)":
            flatfile = os.path.join(self.current_obsnight.calpath, self.current_obsnight.date + "-FlatOFF.fits")
            tmp = self.current_obsnight.flatoff
            if tmp:
                tmp[1] = flist
            else:
                tmp = ["", flist]
            try:
                if FitsImage(flatfile).header["FILES"] == flist:
                    tmp[0] = flatfile
            except:
                pass
            self.current_obsnight = self.current_obsnight._replace(flatoff=tmp)
        elif caltype == "Arc Lamps":
            flatfile = os.path.join(self.current_obsnight.calpath, self.current_obsnight.date + "-Wavecal.fits")
            tmp = self.current_obsnight.cals
            if tmp:
                tmp[1] = flist
            else:
                tmp = ["", flist]
            try:
                if FitsImage(flatfile).header["FILES"] == flist:
                    tmp[0] = flatfile
            except:
                pass
            self.current_obsnight = self.current_obsnight._replace(cals=tmp)

    def make_cals(self):
        if not self.current_obsnight.rawpath:
            return
        caltype = self.ids.caltypes.text
        flist = self.ids.calfiles.text
        self.waiting.open()
        if caltype == "Flats (lamps ON)":
            t = Thread(
                target=self.imstack_wrapper,
                args=(self.current_obsnight.flaton, flist, self.current_obsnight.date + "-FlatON.fits"),
            )
            t.start()
        elif caltype == "Flats (lamps OFF)":
            t = Thread(
                target=self.imstack_wrapper,
                args=(self.current_obsnight.flatoff, flist, self.current_obsnight.date + "-FlatOFF.fits"),
            )
            t.start()
        elif caltype == "Arc Lamps":
            t = Thread(
                target=self.imstack_wrapper,
                args=(self.current_obsnight.cals, flist, self.current_obsnight.date + "-Wavecal.fits"),
            )
            t.start()

    def imstack_wrapper(self, target, flist, outp):
        raw = self.current_obsnight.rawpath
        cal = self.current_obsnight.calpath
        stub = self.current_obsnight.filestub
        imstack = parse_filestring(flist, os.path.join(raw, stub))
        imstack.medcombine(output=os.path.join(cal, outp))
        target[:] = [outp, flist]
        self.ids.calfiles.text = flist
        self.ids.calout.text = outp
        self.waiting.dismiss()

    def save_night(self):
        tmp = self.current_obsrun.nights
        tmp[self.current_obsnight.date] = self.current_obsnight
        self.current_obsrun = self.current_obsrun._replace(nights=tmp)
        for night in self.obsnight_list:
            self.rdb[night] = {night: self.current_obsrun.get_from(night)}
        self.rdb.store_sync()

    def set_target(self):
        target_id = self.ids.targs.text
        self.current_target = self.current_obsnight.targets[target_id]
        self.set_filelist()

    def add_target(self):
        popup = AddTarget(instrumentlist=self.instrument_list)
        popup.open()
        popup.bind(on_dismiss=lambda x: self.update_targets(popup.target_args) if popup.target_args else None)

    def update_targets(self, targs):
        targs["images"] = parse_filestring(
            targs["filestring"], os.path.join(self.current_obsnight.rawpath, self.current_obsnight.filestub)
        )
        targs["dither"] = targs["images"].dithers
        self.current_target = ObsTarget(**targs)
        tmp = self.current_obsnight.targets
        tmp[self.current_target.targid] = self.current_target
        self.current_obsnight = self.current_obsnight._replace(targets=tmp)
        self.target_list = self.current_obsnight.targets.keys()
        self.ids.targs.text = self.current_target.targid
        self.set_filelist()
        self.rdb[self.current_obsnight.date] = {self.current_obsnight.date: self.current_obsnight}

    def set_filelist(self):
        self.ids.obsfiles.clear_widgets()
        self.file_list = []
        for f, dither in zip(self.current_target.images, self.current_target.dither):
            tmp = ObsfileInsert(obsfile=f, dithertype=dither)
            self.file_list.append(tmp)
            self.ids.obsfiles.add_widget(tmp)

    def save_target(self):
        self.current_target = self.current_target._replace(
            dither=[x.dithertype for x in self.file_list], notes=self.ids.tnotes.text
        )
        # just make sure everything is propagating correctly
        self.current_obsnight.targets[self.current_target.targid] = self.current_target
        self.current_obsrun.nights[self.current_obsnight.date] = self.current_obsnight
        for night in self.obsnight_list:
            self.rdb[night] = self.current_obsrun.get_from(night)
        self.target_list = self.current_obsnight.targets.keys()