Exemplo n.º 1
0
    def button2action(self, event=None, debug=False, subtract=True,
                      powerlaw=None, fit_original=False,
                      spline=False,
                      spline_sampling=None,
                      spline_downsampler=np.median,
                      baseline_fit_color='orange', **kwargs):
        """
        Do the baseline fitting and save and plot the results.

        """
        if debug: print "Button 2/3 Baseline.  Subtract=",subtract
        if self.subtracted:
            self.unsubtract()

        # interactive guesspeakwidth passes this on; it should be excised
        if 'nwidths' in kwargs:
            kwargs.pop('nwidths')

        if powerlaw is None:
            powerlaw = self.powerlaw

        self.clear_highlights()

        self._xfit_units = self.Spectrum.xarr.unit

        log.debug("Fitting baseline: powerlaw={0} "
                  "spline={1}".format(self.powerlaw, spline))
        self.fit(powerlaw=powerlaw, includemask=self.includemask,
                 order=self.order, spline=spline,
                 spline_sampling=spline_sampling,
                 spline_downsampler=spline_downsampler)

        if subtract:
            if self.subtracted and fit_original:
                # use the spectrum with the old baseline added in (that's what we fit to)
                self.Spectrum.data = self.spectofit - self.basespec
            else:
                self.Spectrum.data -= self.basespec
            self.subtracted = True
        else:
            if self.subtracted:
                self.unsubtract()
            self.subtracted = False

        if self.Spectrum.plotter.axis is not None:
            if debug: print "Plotting baseline"
            if event is not None:
                # preserve frame if fitting interactively
                kwargs.update({'use_window_limits':True})
            self.plot_baseline(baseline_fit_color=baseline_fit_color, **kwargs)

        # disconnect interactive window (and more importantly, reconnect to
        # original interactive cmds)
        self.clear_all_connections()

        if hasattr(self.Spectrum,'header'):
            history.write_history(self.Spectrum.header,
                    "BASELINE order=%i pars=%s" % (self.order,
                        ",".join([str(s) for s in self.baselinepars])) +
                        "(powerlaw)" if self.powerlaw else "")
Exemplo n.º 2
0
    def crop(self, x1, x2, units=None, **kwargs):
        """
        Replace the current spectrum with a subset from x1 to x2 in current
        units

        Fixes CRPIX1 and baseline and model spectra to match cropped data spectrum

        """
        # do slice (this code is redundant... need to figure out how to fix that)
        x1pix = np.argmin(np.abs(x1 - self.xarr.as_unit(units)))
        x2pix = np.argmin(np.abs(x2 - self.xarr.as_unit(units)))
        if x1pix > x2pix: x1pix, x2pix = x2pix, x1pix
        if x1pix == x2pix:
            raise IndexError("ERROR: Trying to crop to zero size.")

        self = self.slice(x1pix, x2pix, units='pixels', copy=False, **kwargs)
        # a baseline spectrum is always defined, even if it is all zeros
        # this is needed to prevent size mismatches.  There may be a more
        # elegant way to do this...
        # this is taken care of in Slice now self.baseline.crop(x1pix,x2pix)
        # this is taken care of in Slice now self.specfit.crop(x1pix,x2pix)
        if hasattr(self.specfit, 'fitter'):
            self.specfit._full_model()

        if hasattr(self, 'header'):
            history.write_history(
                self.header, "CROP: Cropped from %g to %g (pixel %i to %i)" %
                (x1, x2, x1pix, x2pix))

            if self.header.get('CRPIX1'):
                self.header['CRPIX1'] = self.header.get('CRPIX1') - x1pix
                history.write_history(
                    self.header, "CROP: Changed CRPIX1 from %f to %f" %
                    (self.header.get('CRPIX1') + x1pix,
                     self.header.get('CRPIX1')))
Exemplo n.º 3
0
    def crop(self, x1, x2, units=None, **kwargs):
        """
        Replace the current spectrum with a subset from x1 to x2 in current
        units

        Fixes CRPIX1 and baseline and model spectra to match cropped data spectrum

        """
        # do slice (this code is redundant... need to figure out how to fix that)
        x1pix = np.argmin(np.abs(x1 - self.xarr.as_unit(units)))
        x2pix = np.argmin(np.abs(x2 - self.xarr.as_unit(units)))
        if x1pix > x2pix:
            x1pix, x2pix = x2pix, x1pix
        if x1pix == x2pix:
            raise IndexError("ERROR: Trying to crop to zero size.")

        self = self.slice(x1pix, x2pix, units="pixels", copy=False, **kwargs)
        # a baseline spectrum is always defined, even if it is all zeros
        # this is needed to prevent size mismatches.  There may be a more
        # elegant way to do this...
        # this is taken care of in Slice now self.baseline.crop(x1pix,x2pix)
        # this is taken care of in Slice now self.specfit.crop(x1pix,x2pix)
        if hasattr(self.specfit, "fitter"):
            self.specfit._full_model()

        if hasattr(self, "header"):
            history.write_history(self.header, "CROP: Cropped from %g to %g (pixel %i to %i)" % (x1, x2, x1pix, x2pix))

            if self.header.get("CRPIX1"):
                self.header.update("CRPIX1", self.header.get("CRPIX1") - x1pix)
                history.write_history(
                    self.header,
                    "CROP: Changed CRPIX1 from %f to %f"
                    % (self.header.get("CRPIX1") + x1pix, self.header.get("CRPIX1")),
                )
Exemplo n.º 4
0
    def execute_commands(self, user_input, raw_input):
        if not user_input:
            self.exit_code = 1
            return

        command = user_input[0]
        write_history(command, raw_input)

        if ("\\&&" not in raw_input and "\\||" not in raw_input
                and ('&&' in raw_input or '||' in raw_input)):
            if check_valid_operator(raw_input):
                return self.logical_operator(raw_input)
            else:
                self.exit_code = 2
                return

        # check if command is a built-in
        if command in self.builtins:
            self.do_builtin(user_input)
        # check if command is '!*'
        elif command.startswith('!') and len(command) > 1:
            self.do_exclamation(user_input)
            self.should_write_history = False
        # if command is not a built-in
        else:
            self.do_external(user_input)
Exemplo n.º 5
0
    def _smooth_header(self,smooth):
        """
        Internal - correct the FITS header parameters when smoothing
        """
        if self.header.get('CDELT1') is not None and self.header.get('CRPIX1') is not None:
            self.header['CDELT1'] = self.header.get('CDELT1') * float(smooth)
            self.header['CRPIX1'] = self.header.get('CRPIX1') / float(smooth)

            history.write_history(self.header,"SMOOTH: Smoothed and downsampled spectrum by factor %i" % (smooth))
            history.write_history(self.header,"SMOOTH: Changed CRPIX1 from %f to %f" % (self.header.get('CRPIX1')*float(smooth),self.header.get('CRPIX1')))
            history.write_history(self.header,"SMOOTH: Changed CDELT1 from %f to %f" % (self.header.get('CRPIX1')/float(smooth),self.header.get('CRPIX1')))
Exemplo n.º 6
0
    def _smooth_header(self, smooth):
        """
        Internal - correct the FITS header parameters when smoothing
        """
        if self.header.get('CDELT1') is not None and self.header.get(
                'CRPIX1') is not None:
            self.header['CDELT1'] = self.header.get('CDELT1') * float(smooth)
            self.header['CRPIX1'] = self.header.get('CRPIX1') / float(smooth)

            history.write_history(
                self.header,
                "SMOOTH: Smoothed and downsampled spectrum by factor %i" %
                (smooth))
            history.write_history(
                self.header, "SMOOTH: Changed CRPIX1 from %f to %f" %
                (self.header.get('CRPIX1') * float(smooth),
                 self.header.get('CRPIX1')))
            history.write_history(
                self.header, "SMOOTH: Changed CDELT1 from %f to %f" %
                (self.header.get('CRPIX1') / float(smooth),
                 self.header.get('CRPIX1')))
Exemplo n.º 7
0
    def button2action(self, event=None, debug=False, subtract=True, powerlaw=None,
            fit_original=False, baseline_fit_color='orange', **kwargs):
        """
        Do the baseline fitting and save and plot the results.

        """
        if debug: print "Button 2/3 Baseline.  Subtract=",subtract
        if self.subtracted:
            self.unsubtract()

        # interactive guesspeakwidth passes this on; it should be excised
        if 'nwidths' in kwargs:
            kwargs.pop('nwidths')

        if powerlaw is None:
            powerlaw = self.powerlaw

        self.clear_highlights()

        self._xfit_units = self.Spectrum.xarr.units

        if debug: print "Fitting baseline"
        self.basespec, self.baselinepars = self._baseline(
                                                          self.spectofit,
                                                          xarr=self.Spectrum.xarr,
                                                          err=self.Spectrum.error,
                                                          order=self.order, 
                                                          mask=(True-self.includemask),
                                                          powerlaw=powerlaw,
                                                          xarr_fit_units=self._xfit_units,
                                                          **kwargs)

        self.set_basespec_frompars()

        if subtract:
            if self.subtracted and fit_original: 
                # use the spectrum with the old baseline added in (that's what we fit to)
                self.Spectrum.data = self.spectofit - self.basespec
            else:
                self.Spectrum.data -= self.basespec
            self.subtracted = True
        else:
            if self.subtracted:
                self.unsubtract()
            self.subtracted = False

        if self.Spectrum.plotter.axis is not None:
            if debug: print "Plotting baseline"
            if event is not None: 
                # preserve frame if fitting interactively
                kwargs.update({'use_window_limits':True})
            self.plot_baseline(baseline_fit_color=baseline_fit_color, **kwargs)

        # disconnect interactive window (and more importantly, reconnect to
        # original interactive cmds)
        self.clear_all_connections()

        if hasattr(self.Spectrum,'header'):
            history.write_history(self.Spectrum.header,
                    "BASELINE order=%i pars=%s" % (self.order, 
                        ",".join([str(s) for s in self.baselinepars])) +
                        "(powerlaw)" if self.powerlaw else "")
Exemplo n.º 8
0
    def button2action(
        self,
        event=None,
        debug=False,
        subtract=True,
        powerlaw=None,
        fit_original=False,
        baseline_fit_color="orange",
        **kwargs
    ):
        """
        Do the baseline fitting and save and plot the results.

        """
        if debug:
            print "Button 2/3 Baseline.  Subtract=", subtract
        if self.subtracted:
            self.unsubtract()

        # interactive guesspeakwidth passes this on; it should be excised
        if "nwidths" in kwargs:
            kwargs.pop("nwidths")

        if powerlaw is not None:
            powerlaw = self.powerlaw

        self.clear_highlights()

        self._xfit_units = self.Spectrum.xarr.units

        if debug:
            print "Fitting baseline"
        self.basespec, self.baselinepars = self._baseline(
            self.spectofit,
            xarr=self.Spectrum.xarr,
            err=self.Spectrum.error,
            order=self.order,
            mask=(True - self.includemask),
            powerlaw=powerlaw,
            xarr_fit_units=self._xfit_units,
            **kwargs
        )

        self.set_basespec_frompars()

        if subtract:
            if self.subtracted and fit_original:
                # use the spectrum with the old baseline added in (that's what we fit to)
                self.Spectrum.data = self.spectofit - self.basespec
            else:
                self.Spectrum.data -= self.basespec
            self.subtracted = True
        else:
            if self.subtracted:
                self.unsubtract()
            self.subtracted = False

        if self.Spectrum.plotter.axis is not None:
            if debug:
                print "Plotting baseline"
            if event is not None:
                # preserve frame if fitting interactively
                kwargs.update({"use_window_limits": True})
            self.plot_baseline(baseline_fit_color=baseline_fit_color, **kwargs)

        # disconnect interactive window (and more importantly, reconnect to
        # original interactive cmds)
        self.clear_all_connections()

        if hasattr(self.Spectrum, "header"):
            history.write_history(
                self.Spectrum.header,
                "BASELINE order=%i pars=%s" % (self.order, ",".join([str(s) for s in self.baselinepars])) + "(powerlaw)"
                if self.powerlaw
                else "",
            )