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 = [ edgetrace.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 get_tslits_nires(flat_files, user_settings=par, gingashow=True, tilt_root='tilt_nires'): """Precess flat files and get titlts for NIRES """ # Process flat images tImage = traceimage.TraceImage(spectrograph, file_list=flat_files, par=par['calibrations']['traceframe']) tflat = tImage.process(bias_subtract='overscan', trim=False) mstrace = tflat.copy() # Define pixlocn and bpm pixlocn = pixels.gen_pixloc(tImage.stack.shape) bpm = spectrograph.bpm(shape=tflat.shape, det=1) # Instantiate Trace tSlits = traceslits.TraceSlits(mstrace, pixlocn, par=par['calibrations']['slits'], binbpx=bpm) tslits_dict = tSlits.run(plate_scale = 0.123) if gingashow: # Look at what TraceSlits was actually trying to trace viewer, ch = ginga.show_image(tSlits.edgearr) # Look at the sawtooth convolved image viewer, ch = ginga.show_image(tSlits.siglev) tmp = tSlits.edgearr * 100. tmp[np.where(tmp == 0.)] = 1. ginga.show_image(tSlits.mstrace * tmp) ginga.show_slits(viewer, ch, tSlits.lcen, tSlits.rcen, slit_ids=np.arange(tSlits.lcen.shape[1]) + 1, pstep=50) if tilt_root is not None: # Write dict on a json file jdict = ltu.jsonify(tslits_dict.copy()) ltu.savejson(tilt_root + '.json', jdict, overwrite=True, indent=None, easy_to_read=True) print("Wrote: {:s}".format(tilt_root + '.json')) return tslits_dict
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_alignment(alignframe, align_traces=None, slits=None, clear=False): """ Show one of the class internals Parameters ---------- alignframe : `numpy.ndarray`_ Image to be plotted (i.e. the master align frame) align_traces : list, optional The align traces slits : :class:`pypeit.slittrace.SlitTraceSet`, optional properties of the slits, including traces. clear : bool, optional Clear the plotting window in ginga? Returns ------- """ ginga.connect_to_ginga(raise_err=True, allow_new=True) ch_name = 'alignment' viewer, channel = ginga.show_image(alignframe, chname=ch_name, clear=clear, wcs_match=False) # Display the slit edges if slits is not None and viewer is not None: left, right, mask = slits.select_edges() ginga.show_slits(viewer, channel, left, right) # Display the alignment traces if align_traces is not None and viewer is not None: for bar in range(align_traces.shape[1]): for slt in range(align_traces.shape[2]): # Alternate the colors of the slits color = 'orange' if slt % 2 == 0: color = 'magenta' # Display the trace ginga.show_trace(viewer, channel, align_traces[:, bar, slt], trc_name="", color=color)
def show_flats(pixelflat, illumflat, procflat, flat_model, wcs_match=True, slits=None): """ Interface to ginga to show a set of flat images Args: pixelflat (`numpy.ndarray`_): illumflat (`numpy.ndarray`_ or None): procflat (`numpy.ndarray`_): flat_model (`numpy.ndarray`_): wcs_match (bool, optional): slits (:class:`pypeit.slittrace.SlitTraceSet`, optional): Returns: """ ginga.connect_to_ginga(raise_err=True, allow_new=True) if slits is not None: left, right, mask = slits.select_edges() gpm = mask == 0 # Loop me clear = True for img, name, cut in zip([pixelflat, illumflat, procflat, flat_model], ['pixelflat', 'illumflat', 'flat', 'flat_model'], [(0.9, 1.1), (0.9, 1.1), None, None]): if img is None: continue # TODO: Add an option that shows the relevant stuff in a # matplotlib window. viewer, ch = ginga.show_image(img, chname=name, cuts=cut, wcs_match=wcs_match, clear=clear) if slits is not None: ginga.show_slits(viewer, ch, left[:, gpm], right[:, gpm], slit_ids=slits.spat_id[gpm]) # Turn off clear if clear: clear = False
def show(self, attr='edges', pstep=50, extras=None): """ Display an image or spectrum in TraceSlits Args: attr (str, optional): 'edges' -- Show the mstrace image and the edges 'binarr' -- Show the binarr image 'edgearr' -- Show the edgearr image 'siglev' -- Show the Sobolev image 'traces' -- Show the traces at an intermediate stage 'refined_edges' -- Show the traces at an intermediate stage in _pca_refine() 'xset' -- Check the output from the trace crude extras: anything Extra bits and pieces needed for plotting """ if attr == 'edges': viewer, ch = ginga.show_image(self.mstrace, chname='edges') if self.slit_left is not None: ginga.show_slits(viewer, ch, self.slit_left, self.slit_righ, slit_ids=(np.arange(self.slit_left.shape[1]).astype(int) + 1).tolist(), pstep=pstep) elif attr == 'binarr': ginga.show_image(self.binarr, chname='binarr') elif attr == 'xset': viewer, ch = ginga.show_image(self.mstrace, chname='slit_xset') color = dict(left='green', right='red') viewer, ch = ginga.show_image(self.mstrace) for side in ['left', 'right']: for kk in range(self.tc_dict[side]['xset'].shape[1]): ginga.show_trace(viewer, ch, self.tc_dict[side]['xset'][:, kk], trc_name=side+ str(kk),color=color[side]) elif attr == 'refined_edges': # Used in _pca_refine() edges_dict = extras # color = dict(left='green', right='red') viewer, ch = ginga.show_image(self.mstrace, chname='refined_edges') if edges_dict['show'] == 'both': for side in ['left', 'right']: for kk in range(edges_dict[side]['nstart']): ginga.show_trace(viewer, ch, edges_dict[side]['trace'][:, kk], trc_name=side + str(kk), color=color[side]) else: for side in [edges_dict['show']]: for kk in range(edges_dict[side]['nstart']): ginga.show_trace(viewer, ch, edges_dict[side]['trace'][:, kk], trc_name='left_' + str(kk), color=color[side]) elif attr == 'traces': viewer, ch = ginga.show_image(self.mstrace, chname='slit_traces') color = dict(left='green', right='red') viewer, ch = ginga.show_image(self.mstrace) for side in ['left', 'right']: for kk in range(self.tc_dict[side]['traces'].shape[1]): ginga.show_trace(viewer, ch, self.tc_dict[side]['traces'][:, kk], trc_name=side+ str(kk),color=color[side], pstep=pstep) elif attr == 'edgearr': if np.min(self.edgearr) == -1: # Ungrouped tmp = self.mstrace.copy() # Left edges left = self.edgearr == -1 tmp[left] = -99999. # Right edges right = self.edgearr == 1 tmp[right] = 99999. viewer, ch = ginga.show_image(tmp, chname='edgearr') else: # Grouped viewer, ch = ginga.show_image(self.siglev, chname='edgearr') # Traces all_uni = np.unique(self.edgearr[self.edgearr != 0]) for uni in all_uni: # Color clr = 'green' if uni < 0 else 'red' # Do it tidx = np.where(self.edgearr == uni) ginga.show_trace(viewer, ch, tidx[1], trc_name=str(uni), yval=tidx[0], color=clr) elif attr == 'siglev': ginga.show_image(self.siglev, chname='siglev')
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.sciImg.fullmask bitmask_in = self.sciImg.bitmask else: mask_in = None bitmask_in = None if attr == 'global': # global sky subtraction if self.sciImg.image is not None and self.global_sky is not None and self.sciImg.fullmask is not None: # sky subtracted image image = (self.sciImg.image - self.global_sky) * (self.sciImg.fullmask == 0) mean, med, sigma = stats.sigma_clipped_stats( image[self.sciImg.fullmask == 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.image is not None and self.skymodel is not None and self.sciImg.fullmask is not None: # sky subtracted image image = (self.sciImg.image - self.skymodel) * (self.sciImg.fullmask == 0) mean, med, sigma = stats.sigma_clipped_stats( image[self.sciImg.fullmask == 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.image is not None and self.skymodel is not None \ and self.objmodel is not None and self.ivarmodel is not None \ and self.sciImg.fullmask is not None: image = (self.sciImg.image - self.skymodel) * np.sqrt( self.ivarmodel) image *= (self.sciImg.fullmask == 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.image is not None and self.skymodel is not None \ and self.objmodel is not None and self.ivarmodel is not None \ and self.sciImg.fullmask is not None: # full model residual map image = (self.sciImg.image - self.skymodel - self.objmodel) * np.sqrt(self.ivarmodel) image *= (self.sciImg.fullmask == 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.NAME, color=color) if slits and self.slits_left is not None: ginga.show_slits(viewer, ch, self.slits_left, self.slits_right)
def process(self, par, bpm=bpm, flatimages=None, bias=None, slits=None, debug=False, dark=None): """ Process the image Note: The processing step order is currently 'frozen' as is. We may choose to allow optional ordering Here are the allowed steps, in the order they will be applied: subtract_overscan -- Analyze the overscan region and subtract from the image trim -- Trim the image down to the data (i.e. remove the overscan) orient -- Orient the image in the PypeIt orientation (spec, spat) with blue to red going down to up subtract_bias -- Subtract a bias image apply_gain -- Convert to counts, amp by amp flatten -- Divide by the pixel flat and (if provided) the illumination flat extras -- Generate the RN2 and IVAR images crmask -- Generate a CR mask Args: par (:class:`pypeit.par.pypeitpar.ProcessImagesPar`): Parameters that dictate the processing of the images. See :class:`pypeit.par.pypeitpar.ProcessImagesPar` for the defaults. bpm (`numpy.ndarray`_, optional): flatimages (:class:`pypeit.flatfield.FlatImages`): bias (`numpy.ndarray`_, optional): Bias image slits (:class:`pypeit.slittrace.SlitTraceSet`, optional): Used to calculate spatial flexure between the image and the slits Returns: :class:`pypeit.images.pypeitimage.PypeItImage`: """ self.par = par self._bpm = bpm # Get started # Standard order # -- May need to allow for other order some day.. if par['use_overscan']: self.subtract_overscan() if par['trim']: self.trim() if par['orient']: self.orient() if par['use_biasimage']: # Bias frame, if it exists, is *not* trimmed nor oriented self.subtract_bias(bias) if par['use_darkimage']: # Dark frame, if it exists, is TODO:: check: trimmed, oriented (and oscan/bias subtracted?) self.subtract_dark(dark) if par['apply_gain']: self.apply_gain() # This needs to come after trim, orient # Calculate flexure -- May not be used, but always calculated when slits are provided if slits is not None and self.par['spat_flexure_correct']: self.spat_flexure_shift = flexure.spat_flexure_shift( self.image, slits) # Generate the illumination flat, as needed illum_flat = None if self.par['use_illumflat']: if flatimages is None: msgs.error( "Cannot illumflatten, no such image generated. Add one or more illumflat images to your PypeIt file!!" ) if slits is None: msgs.error("Need to provide slits to create illumination flat") illum_flat = flatimages.fit2illumflat( slits, flexure_shift=self.spat_flexure_shift) if debug: from pypeit import ginga left, right = slits.select_edges( flexure=self.spat_flexure_shift) viewer, ch = ginga.show_image(illum_flat, chname='illum_flat') ginga.show_slits(viewer, ch, left, right) # , slits.id) # orig_image = self.image.copy() viewer, ch = ginga.show_image(orig_image, chname='orig_image') ginga.show_slits(viewer, ch, left, right) # , slits.id) # Flat field -- We cannot do illumination flat without a pixel flat (yet) if self.par['use_pixelflat'] or self.par['use_illumflat']: if flatimages is None or flatimages.pixelflat is None: msgs.error("Flat fielding desired but not generated/provided.") else: self.flatten(flatimages.pixelflat, illum_flat=illum_flat, bpm=self.bpm) # Fresh BPM bpm = self.spectrograph.bpm(self.filename, self.det, shape=self.image.shape) # Extras self.build_rn2img() self.build_ivar() # Generate a PypeItImage pypeitImage = pypeitimage.PypeItImage( self.image, ivar=self.ivar, rn2img=self.rn2img, bpm=bpm, detector=self.detector, spat_flexure=self.spat_flexure_shift, PYP_SPEC=self.spectrograph.spectrograph) pypeitImage.rawheadlist = self.headarr pypeitImage.process_steps = [ key for key in self.steps.keys() if self.steps[key] ] # Mask(s) if par['mask_cr']: pypeitImage.build_crmask(self.par) # nonlinear_counts = self.spectrograph.nonlinear_counts( self.detector, apply_gain=self.par['apply_gain']) # Build pypeitImage.build_mask(saturation=nonlinear_counts) # Return return pypeitImage
def main(args): # List only? if args.list: hdu = fits.open(args.file) hdu.info() return # Load it up spec2DObj = spec2dobj.Spec2DObj.from_file(args.file, args.det) # Setup for PYPIT imports msgs.reset(verbosity=2) # Init # TODO: get_dnum needs to be deprecated... sdet = get_dnum(args.det, prefix=False) # 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 # Slits # slits_key = '{0}_{1:02d}'.format(spec2DObj.head0['TRACMKEY'], args.det) # slit_file = os.path.join(mdir, masterframe.construct_file_name(slittrace.SlitTraceSet, slits_key)) # slits = slittrace.SlitTraceSet.from_file(slit_file) # Grab the slit edges slits = spec2DObj.slits if spec2DObj.sci_spat_flexure is not None: msgs.info("Offseting slits by {}".format(spec2DObj.sci_spat_flexure)) all_left, all_right, mask = slits.select_edges( flexure=spec2DObj.sci_spat_flexure) # TODO -- This may be too restrictive, i.e. ignore BADFLTCALIB?? gpm = mask == 0 left = all_left[:, gpm] right = all_right[:, gpm] slid_IDs = spec2DObj.slits.slitord_id[gpm] bitMask = ImageBitMask() # Object traces from spec1d file spec1d_file = args.file.replace('spec2d', 'spec1d') if os.path.isfile(spec1d_file): sobjs = specobjs.SpecObjs.from_fitsfile(spec1d_file) else: sobjs = None msgs.warn('Could not find spec1d file: {:s}'.format(spec1d_file) + msgs.newline() + ' No objects were extracted.') ginga.connect_to_ginga(raise_err=True, allow_new=True) # Now show each image to a separate channel # Show the bitmask? mask_in = None if args.showmask: viewer, ch = ginga.show_image(spec2DObj.bpmmask, chname="BPM", waveimg=spec2DObj.waveimg, clear=True) #bpm, crmask, satmask, minmask, offslitmask, nanmask, ivar0mask, ivarnanmask, extractmask \ # SCIIMG image = spec2DObj.sciimg # Processed science image mean, med, sigma = sigma_clipped_stats(image[spec2DObj.bpmmask == 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=spec2DObj.waveimg, clear=True) if sobjs is not None: show_trace(sobjs, args.det, viewer, ch) ginga.show_slits(viewer, ch, left, right, slit_ids=slid_IDs) # SKYSUB if args.ignore_extract_mask: # TODO -- Is there a cleaner way to do this? gpm = (spec2DObj.bpmmask == 0) | (spec2DObj.bpmmask == 2** bitMask.bits['EXTRACT']) else: gpm = spec2DObj.bpmmask == 0 image = (spec2DObj.sciimg - spec2DObj.skymodel ) * gpm #(spec2DObj.mask == 0) # sky subtracted image mean, med, sigma = sigma_clipped_stats(image[spec2DObj.bpmmask == 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=spec2DObj.waveimg, bitmask=bitMask, mask=mask_in) #, cuts=(cut_min, cut_max),wcs_match=True) if not args.removetrace and sobjs is not None: show_trace(sobjs, args.det, viewer, ch) ginga.show_slits(viewer, ch, left, right, slit_ids=slid_IDs) # SKRESIDS chname_skyresids = 'sky_resid-det{:s}'.format(sdet) image = (spec2DObj.sciimg - spec2DObj.skymodel) * np.sqrt( spec2DObj.ivarmodel) * (spec2DObj.bpmmask == 0) # sky residual map viewer, ch = ginga.show_image(image, chname_skyresids, waveimg=spec2DObj.waveimg, cuts=(-5.0, 5.0), bitmask=bitMask, mask=mask_in) if not args.removetrace and sobjs is not None: show_trace(sobjs, args.det, viewer, ch) ginga.show_slits(viewer, ch, left, right, slit_ids=slid_IDs) # RESIDS chname_resids = 'resid-det{:s}'.format(sdet) # full model residual map image = (spec2DObj.sciimg - spec2DObj.skymodel - spec2DObj.objmodel ) * np.sqrt(spec2DObj.ivarmodel) * (spec2DObj.bpmmask == 0) viewer, ch = ginga.show_image(image, chname=chname_resids, waveimg=spec2DObj.waveimg, cuts=(-5.0, 5.0), bitmask=bitMask, mask=mask_in) if not args.removetrace and sobjs is not None: show_trace(sobjs, args.det, viewer, ch) ginga.show_slits(viewer, ch, left, right, slit_ids=slid_IDs) # 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: embed()
inmask=mask_AB, FWHM=FWHM, nperslit=1, trim_edg=(3, 3), show_trace=False, show_peaks=False, show_fits =False) if specobj_slit_pos is not None: specobjs_pos.add_sobj(specobj_slit_pos.specobjs.tolist()) skymask[thismask] = (skymask_pos & skymask_neg) # Show results on ginga if gingashow: # Plot the chi image chi = (diff_AB - residual_img) * np.sqrt(ivar_AB) * (slitpix > 0) * ((edgmask == False) & (mask_AB == True)) viewer, ch = ginga.show_image(chi) ginga.show_slits(viewer, ch, lcen, rcen, slit_ids=None) for islit in range(0, nslits): ginga.show_trace(viewer, ch, specobjs_pos[islit].trace_spat, trc_name=specobjs_pos[islit].idx, color='blue') ginga.show_trace(viewer, ch, specobjs_neg[islit].trace_spat, trc_name=specobjs_neg[islit].idx, color='orange') # Boxcar extraction from pypeit.core.extract import extract_boxcar outmask = (slitpix > 0) * ((edgmask == False) & (mask_AB == True)) box_rad = 8.0 # ToDo -- Check for indexes in islit [0-based or 1-based?] for islit in range(1, nslits + 1): # Positive trace flux = extract_boxcar(image,
def spat_flexure_shift(sciimg, slits, debug=False, maxlag=20): """ Calculate a rigid flexure shift in the spatial dimension between the slitmask and the science image. It is *important* to use original=True when defining the slitmask as everything should be relative to the initial slits Otherwise, the WaveTilts could get out of sync with science images Args: sciimg (`numpy.ndarray`_): slits (:class:`pypeit.slittrace.SlitTraceSet`): maxlag (:obj:`int`, optional): Maximum flexure searched for Returns: float: The spatial flexure shift relative to the initial slits """ # Mask -- Includes short slits and those excluded by the user (e.g. ['rdx']['slitspatnum']) slitmask = slits.slit_img(initial=True, exclude_flag=slits.bitmask.exclude_for_flexure) _sciimg = sciimg if slitmask.shape == sciimg.shape \ else arc.resize_mask2arc(slitmask.shape, sciimg) onslits = slitmask > -1 corr_slits = onslits.astype(float).flatten() # Compute mean_sci, med_sci, stddev_sci = stats.sigma_clipped_stats(_sciimg[onslits]) thresh = med_sci + 5.0 * stddev_sci corr_sci = np.fmin(_sciimg.flatten(), thresh) lags, xcorr = utils.cross_correlate(corr_sci, corr_slits, maxlag) xcorr_denom = np.sqrt( np.sum(corr_sci * corr_sci) * np.sum(corr_slits * corr_slits)) xcorr_norm = xcorr / xcorr_denom # TODO -- Generate a QA plot tampl_true, tampl, pix_max, twid, centerr, ww, arc_cont, nsig \ = arc.detect_lines(xcorr_norm, sigdetect=3.0, fit_frac_fwhm=1.5, fwhm=5.0, cont_frac_fwhm=1.0, cont_samp=30, nfind=1, debug=debug) # No peak? -- e.g. data fills the entire detector if len(tampl) == 0: msgs.warn( 'No peak found in spatial flexure. Assuming there is none..') if debug: embed(header='68 of flexure') return 0. # Find the peak xcorr_max = np.interp(pix_max, np.arange(lags.shape[0]), xcorr_norm) lag_max = np.interp(pix_max, np.arange(lags.shape[0]), lags) msgs.info('Spatial flexure measured: {}'.format(lag_max[0])) if debug: plt.figure(figsize=(14, 6)) plt.plot(lags, xcorr_norm, color='black', drawstyle='steps-mid', lw=3, label='x-corr', linewidth=1.0) plt.plot(lag_max[0], xcorr_max[0], 'g+', markersize=6.0, label='peak') plt.title('Best shift = {:5.3f}'.format(lag_max[0]) + ', corr_max = {:5.3f}'.format(xcorr_max[0])) plt.legend() plt.show() #tslits_shift = trace_slits.shift_slits(tslits_dict, lag_max) # Now translate the tilts #slitmask_shift = pixels.tslits2mask(tslits_shift) #slitmask_shift = slits.slit_img(flexure=lag_max[0]) if debug: # Now translate the slits in the tslits_dict all_left_flexure, all_right_flexure, mask = slits.select_edges( flexure=lag_max[0]) gpm = mask == 0 viewer, ch = ginga.show_image(_sciimg) ginga.show_slits(viewer, ch, left_flexure[:, gpm], right_flexure)[:, gpm] #, slits.id) #, args.det) embed(header='83 of flexure.py') #ginga.show_slits(viewer, ch, tslits_shift['slit_left'], tslits_shift['slit_righ']) #ginga.show_slits(viewer, ch, tslits_dict['slit_left'], tslits_dict['slit_righ']) return lag_max[0]
def show(self, attr, image=None, align_traces=None, chname=None, slits=False, clear=False): """ Show one of the class internals Parameters ---------- attr : str image - plot the master align frame image : ndarray Image to be plotted (i.e. the master align frame) align_traces : list The align traces chname : str The channel name sent to ginga slits : bool Overplot the slit edges? clear : bool Clear the plotting window in ginga? Returns: """ if attr == 'image': ch_name = chname if chname is not None else 'align_traces' self.viewer, self.channel = ginga.show_image(image, chname=ch_name, clear=clear, wcs_match=False) elif attr == 'overplot': pass else: msgs.warn("Not an option for show") if align_traces is not None and self.viewer is not None: for spec in align_traces: color = 'magenta' if spec.hand_extract_flag else 'orange' ginga.show_trace(self.viewer, self.channel, spec.TRACE_SPAT, trc_name="", color=color) if slits: if self.tslits_dict is not None and self.viewer is not None: slit_ids = [ edgetrace.get_slitid(image.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(self.viewer, self.channel, self.tslits_dict['slit_left'], self.tslits_dict['slit_righ'], slit_ids) return