def main(pargs): import pdb as debugger import time from pypeit import ginga from pypeit import traceslits from pypeit.core.trace_slits import get_slitid import subprocess # Load up tslits_dict, mstrace = traceslits.load_tslits(pargs.root) import pdb pdb.set_trace() try: ginga.connect_to_ginga(raise_err=True) except ValueError: subprocess.Popen(['ginga', '--modules=RC']) time.sleep(3) # This is deprecated since the class is not stored to disk anymore. You can just debug to achieve this functionality #if pargs.show is not None: # Tslits.show(pargs.show) # print("Check your Ginga viewer") # return # Show Image viewer, ch = ginga.show_image(mstrace, chname=pargs.chname) nslits = tslits_dict['nslits'] # Get slit ids stup = (mstrace.shape, tslits_dict['slit_left'], tslits_dict['slit_righ']) if pargs.dumb_ids: slit_ids = range(nslits) else: slit_ids = [ get_slitid(stup[0], stup[1], stup[2], ii)[0] for ii in range(nslits) ] ginga.show_slits(viewer, ch, tslits_dict['slit_left'], tslits_dict['slit_righ'], slit_ids, pstep=50) print("Check your Ginga viewer")
def show(self, slits=True, wcs_match=True): """ Show all of the flat field products Args: slits (bool, optional): wcs_match (bool, optional): """ viewer, ch = ginga.show_image(self.mspixelflat, chname='pixeflat', cuts=(0.9, 1.1), wcs_match=wcs_match, clear=True) viewer, ch = ginga.show_image(self.msillumflat, chname='illumflat', cuts=(0.9, 1.1), wcs_match=wcs_match) viewer, ch = ginga.show_image(self.rawflatimg, chname='flat', wcs_match=wcs_match) viewer, ch = ginga.show_image(self.flat_model, chname='flat_model', wcs_match=wcs_match) if slits: if self.tslits_dict is not None: slit_ids = [ trace_slits.get_slitid(self.rawflatimg.shape, self.tslits_dict['slit_left'], self.tslits_dict['slit_righ'], ii)[0] for ii in range(self.tslits_dict['slit_left'].shape[1]) ] ginga.show_slits(viewer, ch, self.tslits_dict['slit_left'], self.tslits_dict['slit_righ'], slit_ids)
def main(args): # List only? hdu = fits.open(args.file) head0 = hdu[0].header if args.list: hdu.info() return # Setup for PYPIT imports msgs.reset(verbosity=2) # Init sdet = get_dnum(args.det, prefix=False) # One detector, sky sub for now names = [hdu[i].name for i in range(len(hdu))] try: exten = names.index('DET{:s}-PROCESSED'.format(sdet)) except: # Backwards compatability msgs.error( 'Requested detector {:s} was not processed.\n' 'Maybe you chose the wrong one to view?\n' 'Set with --det= or check file contents with --list'.format(sdet)) sciimg = hdu[exten].data try: exten = names.index('DET{:s}-SKY'.format(sdet)) except: # Backwards compatability msgs.error( 'Requested detector {:s} has no sky model.\n' 'Maybe you chose the wrong one to view?\n' 'Set with --det= or check file contents with --list'.format(sdet)) skymodel = hdu[exten].data try: exten = names.index('DET{:s}-MASK'.format(sdet)) except ValueError: # Backwards compatability msgs.error( 'Requested detector {:s} has no bit mask.\n' 'Maybe you chose the wrong one to view?\n' 'Set with --det= or check file contents with --list'.format(sdet)) mask = hdu[exten].data try: exten = names.index('DET{:s}-IVARMODEL'.format(sdet)) except ValueError: # Backwards compatability msgs.error( 'Requested detector {:s} has no IVARMODEL.\n' 'Maybe you chose the wrong one to view?\n' + 'Set with --det= or check file contents with --list'.format(sdet)) ivarmodel = hdu[exten].data # Read in the object model for residual map try: exten = names.index('DET{:s}-OBJ'.format(sdet)) except ValueError: # Backwards compatability msgs.error( 'Requested detector {:s} has no object model.\n' 'Maybe you chose the wrong one to view?\n' + 'Set with --det= or check file contents with --list'.format(sdet)) objmodel = hdu[exten].data # Get waveimg mdir = head0['PYPMFDIR'] + '/' if not os.path.exists(mdir): mdir_base = os.path.basename(os.path.dirname(mdir)) + '/' msgs.warn('Master file dir: {0} does not exist. Using ./{1}'.format( mdir, mdir_base)) mdir = mdir_base trace_key = '{0}_{1:02d}'.format(head0['TRACMKEY'], args.det) trc_file = os.path.join( mdir, MasterFrame.construct_file_name('Trace', trace_key)) wave_key = '{0}_{1:02d}'.format(head0['ARCMKEY'], args.det) waveimg = os.path.join(mdir, MasterFrame.construct_file_name('Wave', wave_key)) tslits_dict = TraceSlits.load_from_file(trc_file)[0] slitmask = pixels.tslits2mask(tslits_dict) shape = (tslits_dict['nspec'], tslits_dict['nspat']) slit_ids = [ trace_slits.get_slitid(shape, tslits_dict['slit_left'], tslits_dict['slit_righ'], ii)[0] for ii in range(tslits_dict['slit_left'].shape[1]) ] # Show the bitmask? mask_in = mask if args.showmask else None # Object traces spec1d_file = args.file.replace('spec2d', 'spec1d') det_nm = 'DET{:s}'.format(sdet) if os.path.isfile(spec1d_file): hdulist_1d = fits.open(spec1d_file) else: hdulist_1d = [] msgs.warn('Could not find spec1d file: {:s}'.format(spec1d_file) + msgs.newline() + ' No objects were extracted.') # Unpack the bitmask bitMask = bitmask() bpm, crmask, satmask, minmask, offslitmask, nanmask, ivar0mask, ivarnanmask, extractmask \ = bitMask.unpack(mask) # Now show each image to a separate channel # SCIIMG image = sciimg # Raw science image (mean, med, sigma) = sigma_clipped_stats(image[mask == 0], sigma_lower=5.0, sigma_upper=5.0) cut_min = mean - 1.0 * sigma cut_max = mean + 4.0 * sigma chname_skysub = 'sciimg-det{:s}'.format(sdet) # Clear all channels at the beginning viewer, ch = ginga.show_image(image, chname=chname_skysub, waveimg=waveimg, bitmask=mask_in, clear=True) #, cuts=(cut_min, cut_max), wcs_match=True) ginga.show_slits(viewer, ch, tslits_dict['slit_left'], tslits_dict['slit_righ'], slit_ids) #, args.det) # SKYSUB image = (sciimg - skymodel) * (mask == 0) # sky subtracted image (mean, med, sigma) = sigma_clipped_stats(image[mask == 0], sigma_lower=5.0, sigma_upper=5.0) cut_min = mean - 1.0 * sigma cut_max = mean + 4.0 * sigma chname_skysub = 'skysub-det{:s}'.format(sdet) # Clear all channels at the beginning # TODO: JFH For some reason Ginga crashes when I try to put cuts in here. viewer, ch = ginga.show_image( image, chname=chname_skysub, waveimg=waveimg, bitmask=mask_in) #, cuts=(cut_min, cut_max),wcs_match=True) show_trace(hdulist_1d, det_nm, viewer, ch) ginga.show_slits(viewer, ch, tslits_dict['slit_left'], tslits_dict['slit_righ'], slit_ids) #, args.det) # SKRESIDS chname_skyresids = 'sky_resid-det{:s}'.format(sdet) image = (sciimg - skymodel) * np.sqrt(ivarmodel) * (mask == 0 ) # sky residual map viewer, ch = ginga.show_image(image, chname_skyresids, waveimg=waveimg, cuts=(-5.0, 5.0), bitmask=mask_in) #,wcs_match=True) show_trace(hdulist_1d, det_nm, viewer, ch) ginga.show_slits(viewer, ch, tslits_dict['slit_left'], tslits_dict['slit_righ'], slit_ids) #, args.det) # RESIDS chname_resids = 'resid-det{:s}'.format(sdet) # full model residual map image = (sciimg - skymodel - objmodel) * np.sqrt(ivarmodel) * (mask == 0) viewer, ch = ginga.show_image(image, chname=chname_resids, waveimg=waveimg, cuts=(-5.0, 5.0), bitmask=mask_in) #,wcs_match=True) show_trace(hdulist_1d, det_nm, viewer, ch) ginga.show_slits(viewer, ch, tslits_dict['slit_left'], tslits_dict['slit_righ'], slit_ids) #, args.det) # After displaying all the images sync up the images with WCS_MATCH shell = viewer.shell() out = shell.start_global_plugin('WCSMatch') out = shell.call_global_plugin_method('WCSMatch', 'set_reference_channel', [chname_resids], {}) if args.embed: IPython.embed()
def show(self, attr, image=None, showmask=False, sobjs=None, chname=None, slits=False,clear=False): """ Show one of the internal images .. todo:: Should probably put some of these in ProcessImages Parameters ---------- attr : str global -- Sky model (global) sci -- Processed science image rawvar -- Raw variance image modelvar -- Model variance image crmasked -- Science image with CRs set to 0 skysub -- Science image with global sky subtracted image -- Input image display : str, optional image : ndarray, optional User supplied image to display Returns ------- """ if showmask: mask_in = self.mask bitmask_in = self.bitmask else: mask_in = None bitmask_in = None if attr == 'global': # global sky subtraction if self.sciimg is not None and self.global_sky is not None and self.mask is not None: # sky subtracted image image = (self.sciimg - self.global_sky)*(self.mask == 0) mean, med, sigma = stats.sigma_clipped_stats(image[self.mask == 0], sigma_lower=5.0, sigma_upper=5.0) cut_min = mean - 1.0 * sigma cut_max = mean + 4.0 * sigma ch_name = chname if chname is not None else 'global_sky_{}'.format(self.det) viewer, ch = ginga.show_image(image, chname=ch_name, bitmask=bitmask_in, mask=mask_in, clear=clear, wcs_match=True) #, cuts=(cut_min, cut_max)) elif attr == 'local': # local sky subtraction if self.sciimg is not None and self.skymodel is not None and self.mask is not None: # sky subtracted image image = (self.sciimg - self.skymodel)*(self.mask == 0) mean, med, sigma = stats.sigma_clipped_stats(image[self.mask == 0], sigma_lower=5.0, sigma_upper=5.0) cut_min = mean - 1.0 * sigma cut_max = mean + 4.0 * sigma ch_name = chname if chname is not None else 'local_sky_{}'.format(self.det) viewer, ch = ginga.show_image(image, chname=ch_name, bitmask=bitmask_in, mask=mask_in, clear=clear, wcs_match=True) #, cuts=(cut_min, cut_max)) elif attr == 'sky_resid': # sky residual map with object included if self.sciimg is not None and self.skymodel is not None \ and self.objmodel is not None and self.ivarmodel is not None \ and self.mask is not None: image = (self.sciimg - self.skymodel) * np.sqrt(self.ivarmodel) image *= (self.mask == 0) ch_name = chname if chname is not None else 'sky_resid_{}'.format(self.det) viewer, ch = ginga.show_image(image, chname=ch_name, cuts=(-5.0, 5.0), bitmask=bitmask_in, mask=mask_in, clear=clear, wcs_match=True) elif attr == 'resid': # full residual map with object model subtractede if self.sciimg is not None and self.skymodel is not None \ and self.objmodel is not None and self.ivarmodel is not None \ and self.mask is not None: # full model residual map image = (self.sciimg - self.skymodel - self.objmodel) * np.sqrt(self.ivarmodel) image *= (self.mask == 0) ch_name = chname if chname is not None else 'resid_{}'.format(self.det) viewer, ch = ginga.show_image(image, chname=ch_name, cuts=(-5.0, 5.0), bitmask=bitmask_in, mask=mask_in, clear=clear, wcs_match=True) elif attr == 'image': ch_name = chname if chname is not None else 'image' viewer, ch = ginga.show_image(image, chname=ch_name, clear=clear, wcs_match=True) else: msgs.warn("Not an option for show") if sobjs is not None: for spec in sobjs: color = 'magenta' if spec.hand_extract_flag else 'orange' ginga.show_trace(viewer, ch, spec.trace_spat, spec.idx, color=color) if slits: if self.tslits_dict is not None: slit_ids = [trace_slits.get_slitid( self.mask.shape, self.tslits_dict['slit_left'], self.tslits_dict['slit_righ'], ii)[0] for ii in range(self.tslits_dict['slit_left'].shape[1])] ginga.show_slits(viewer, ch, self.tslits_dict['slit_left'], self.tslits_dict['slit_righ'], slit_ids) # , args.det)
def main(args): # List only? hdu = fits.open(args.file) head0 = hdu[0].header if args.list: hdu.info() return # Init sdet = get_dnum(args.det, prefix=False) # One detector, sky sub for now names = [hdu[i].name for i in range(len(hdu))] try: exten = names.index('DET{:s}-PROCESSED'.format(sdet)) except: # Backwards compatability msgs.error('Requested detector {:s} was not processed.\n' 'Maybe you chose the wrong one to view?\n' 'Set with --det= or check file contents with --list'.format(sdet)) sciimg = hdu[exten].data try: exten = names.index('DET{:s}-SKY'.format(sdet)) except: # Backwards compatability msgs.error('Requested detector {:s} has no sky model.\n' 'Maybe you chose the wrong one to view?\n' 'Set with --det= or check file contents with --list'.format(sdet)) skymodel = hdu[exten].data try: exten = names.index('DET{:s}-MASK'.format(sdet)) except ValueError: # Backwards compatability msgs.error('Requested detector {:s} has no bit mask.\n' 'Maybe you chose the wrong one to view?\n' 'Set with --det= or check file contents with --list'.format(sdet)) mask = hdu[exten].data frame = (sciimg - skymodel) * (mask == 0) mdir = head0['PYPMFDIR'] if not os.path.exists(mdir): mdir_base = os.path.join(os.getcwd(), os.path.basename(mdir)) msgs.warn('Master file dir: {0} does not exist. Using {1}'.format(mdir, mdir_base)) mdir = mdir_base trace_key = '{0}_{1:02d}'.format(head0['TRACMKEY'], args.det) trc_file = os.path.join(mdir, MasterFrame.construct_file_name('Trace', trace_key)) # TODO -- Remove this once the move to Edges is complete if args.old: tslits_dict = TraceSlits.load_from_file(trc_file)[0] else: trc_file = trc_file.replace('Trace', 'Edges')+'.gz' tslits_dict = edgetrace.EdgeTraceSet.from_file(trc_file).convert_to_tslits_dict() shape = (tslits_dict['nspec'], tslits_dict['nspat']) slit_ids = [trace_slits.get_slitid(shape, tslits_dict['slit_left'], tslits_dict['slit_righ'], ii)[0] for ii in range(tslits_dict['slit_left'].shape[1])] # Object traces spec1d_file = args.file.replace('spec2d', 'spec1d') det_nm = 'DET{:s}'.format(sdet) if os.path.isfile(spec1d_file): hdulist_1d = fits.open(spec1d_file) else: hdulist_1d = [] msgs.warn('Could not find spec1d file: {:s}'.format(spec1d_file) + msgs.newline() + ' No objects were extracted.') tslits_dict['objtrc'] = parse_traces(hdulist_1d, det_nm) # TODO :: Need to include standard star trace in the spec2d files std_trace = None # Extract some trace models fwhm = 2 # Start with some default value # Brightest object on slit trace_model_obj = None trace_model_dict = dict() if len(tslits_dict['objtrc']['pkflux']) > 0: smash_peakflux = tslits_dict['objtrc']['pkflux'] ibri = smash_peakflux.argmax() trace_model_obj = tslits_dict['objtrc']['traces'][ibri] fwhm = tslits_dict['objtrc']['fwhm'][ibri] trace_model_dict['object'] = dict(trace_model=trace_model_obj, fwhm=fwhm) # Standard star trace trace_model_dict['std'] = dict(trace_model=std_trace, fwhm=fwhm) # Trace of the slit edge trace_model_dict['slit'] = dict(trace_model=tslits_dict['slit_left'].copy(), fwhm=fwhm) tslits_dict['trace_model'] = trace_model_dict # Finally, initialise the GUI gui_object_find.initialise(args.det, frame, tslits_dict, None, printout=True, slit_ids=slit_ids)