예제 #1
0
 def on_enter(self):
     flat = os.path.join(self.paths['cal'],'Flat.fits')
     header = [0]
     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.pairstrings = ['{0} - {1}'.format(*map(os.path.basename,x)) for x in self.extract_pairs]
예제 #2
0
 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
     header = [[0],[0]]
     pair = self.extract_pairs[self.pairstrings.index(val)]
     self.current_extraction = self.current_target.extractions.get(str(hash(':'.join(pair))), None)
     if not self.current_extraction:
         im1, im2 = [fitsimage(os.path.join(self.paths['raw'], x), header[i]) for i, x in enumerate(pair)]
         if 'EXREGX1' in header[0] or 'EXREGX1' in header[1]:
             for x in ['x1','y1','x2','y2']:
                 tmp = header[0]['EXREG'+x.upper()] or header[1]['EXREG'+x.upper()]
                 if tmp:
                     self.set_coord(x, tmp)
         region = map(int,[self.bx1, self.by1, self.bx2, self.by2])
         td = int(tracedir(self.current_target.instrument_id) == 'horizontal')
         self.current_extraction = extraction(im1, im2, self.current_flats, region,
                                              td, 'Gaussian')
         self.current_extraction.file1 = pair[0]
         self.current_extraction.file2 = pair[1]
         self.current_extraction.flatfile = os.path.join(self.paths['cal'],'Flat.fits')
         self.theapp.current_target.extractions[self.current_extraction.name] = self.current_extraction
     else:
         self.bx1, self.by1, self.bx2, self.by2 = self.current_extraction.region
     im = scalable_image(self.current_extraction.diff)
     self.ids.ipane.load_data(im)
     self.imwid, self.imht = im.dimensions
예제 #3
0
 def on_enter(self):
     flat = os.path.join(self.paths['cal'],'Flat.fits')
     header = object()
     if not os.path.exists(flat):
         if self.theapp.current_night.flaton and self.theapp.current_night.flaton[0]:
             #fon = FitsImage(os.path.join(self.paths['cal'], \
             #    self.theapp.current_night.flaton[0]), load=True)
             fon = fitsimage(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(os.path.join(self.paths['cal'], \
                 #    self.theapp.current_night.flatoff[0]), load=True)
                 foff = fitsimage(os.path.join(self.paths['cal'], self.theapp.current_night.flatoff[0]), header)
                 im_subtract(fon, foff, outputfile = flat)
             else:
                 fits.writeto(flat, fon.data_array, header=fon.header)
     self.current_flats = fitsimage(flat, header)
     self.pairstrings = ['{0} - {1}'.format(*map(os.path.basename,x)) for x in self.extract_pairs]
예제 #4
0
 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)
예제 #5
0
 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
     header = [fits.Header()]*2
     pair = self.extract_pairs[self.pairstrings.index(val)]
     #im1, im2 = [FitsImage(os.path.join(self.paths['raw'], x), 
     #                      load=True) for x in pair]
     self.current_extraction = self.current_target.extractions.get(str(hash(':'.join(pair))), None)
     if not self.current_extraction:
         im1, im2 = [fitsimage(os.path.join(self.paths['raw'], x), header[i]) for i, x in enumerate(pair)]
         if 'EXREGX1' in header[0] or 'EXREGX1' in header[1]:
             for x in ['x1','y1','x2','y2']:
                 tmp = header[0]['EXREG'+x.upper()] or header[1]['EXREG'+x.upper()]
                 if tmp:
                     self.set_coord(x, tmp)
         region = map(int,[self.bx1, self.by1, self.bx2, self.by2])
         td = int(tracedir(self.current_target.instrument_id) == 'horizontal')
         #pdb.set_trace()
         self.current_extraction = extraction(im1, im2, self.current_flats, region,
                                              td, 'Gaussian')
         self.current_extraction.file1 = pair[0]
         self.current_extraction.file2 = pair[1]
         self.current_extraction.flatfile = os.path.join(self.paths['cal'],'Flat.fits')
         self.theapp.current_target.extractions[self.current_extraction.name] = self.current_extraction
     else:
         self.bx1, self.by1, self.bx2, self.by2 = self.current_extraction.region
     #fitsfile = self.paths['out']+re.sub(' ','',re.sub('.fits','',val))+'.fits'
     #im1, im2 = [x for x in copy.deepcopy(self.extract_pairs[pair_index])]
     #if not os.path.isfile(fitsfile):
     #    im1, im2 = [x for x in copy.deepcopy(self.extract_pairs[pair_index])]
     #    im1.load(); im2.load()
     #    im_subtract(im1, im2, outputfile=os.path.join(self.paths['out'],fitsfile))
     #self.current_impair = FitsImage(os.path.join(self.paths['out'],fitsfile), load=True)
     #self.ids.ipane.load_data(self.current_impair)
     im = scalable_image(self.current_extraction.diff)
     self.ids.ipane.load_data(im)
     self.imwid, self.imht = im.dimensions
예제 #6
0
 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
예제 #7
0
 def launch_header(self):
     head = [0]
     fitsimage(self.obsfile, head)
     self.header_viewer = FitsHeaderDialog(fitsheader = head[0])
     self.header_viewer.bind(on_dismiss = self.update_header())
     self.header_viewer.open()
예제 #8
0
 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
     
     #need a calibration, too
     self.lamp = np.empty([2,2], dtype=np.float64)
     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]
         lamps = True
     
     #im1, im2 = [x for x in copy.deepcopy(self.theapp.extract_pairs[self.pair_index])]
     #im1.load(); im2.load()
     #im1.data_array = extract.fix_distortion(im1.data_array, pdistort)
     #im2.data_array = extract.fix_distortion(im2.data_array, ndistort)
     #self.tell = make_region(im1, im2, self.region, flat=self.current_flats, telluric=True)
     #self.pextract = extract(self.fit_params['pmodel'], self.extractregion, self.tell, 'pos', lamp = self.lamp)
     #self.nextract = extract(self.fit_params['nmodel'], self.extractregion, self.tell, 'neg', lamp = self.lamp)
     self.current_extraction.fit_trace(self.fit_params['model'], self.lamp, lamps=lamps, extract=True)
     #if self.lamp.size > 0:
     #    spectra, tellurics, lamps = self.current_extraction.extract(trace, 
     #                                                                self.lamp, 
     #                                                                False)
     #else:
     #    spectra, telluric = self.current_extraction.extract(trace, self.lamp, True)
     
     #write uncalibrated spectra to fits files (will update after calibration)
     h1, h2 = fits.Header(), fits.Header()
     fitsimage(self.current_extraction.file1, h1)
     #im1 = FitsImage(self.current_extraction.file1, load=False)
     pstub = os.path.join(self.paths['out'], 
                          re.sub('.fits','-ap%i',
                                 os.path.basename(self.current_extraction.file1)))#im1.fitsfile)
     ext = ('.fits','-sky.fits','-lamp.fits')
     #h = im1.header
     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)
     nstub = os.path.join(self.paths['out'], 
                          re.sub('.fits','-ap%i',
                                 os.path.basename(self.current_extraction.file2)))#im2.fitsfile)
     #h = im2.header
     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:
                 fits.writeto((pstub+ext[j])%0, specs, header=h1)
             else:
                 fits.writeto((nstub+ext[j])%0, specs*-1., header=h2)
         else:
             specs = specs.T
             for i, ap in enumerate(specs):
                 if self.fit_params['model'][i][0] > 0:
                     fits.writeto((pstub+ext[j])%i, ap, header=h1)
                 else:
                     fits.writeto((nstub+ext[j])%i, ap*-1., header=h2)
     return