Beispiel #1
0
    def orbital_correction_old(self, line_guess=None, yrange=None, **kwargs):
        """
        Calculates the corrections for orbital and slit tilt deviation.
        Calculates the average over a given y-range which ideally should be
        over quiet sun. If this is not given, then the whole cube will be used.
        A line guess may be given, the default is Fe XII. Extra arguments are
        given to the individual spectra's gaussian fit function.
        After this method is called, the corrections must be applied using the
        apply_corrections method. This is so that corrections are only
        calculated once and can be applied to cubes holding different peak
        wavelengths.

        Parameters
        ----------
        line_guess: tuple of three floats
            A guess for a strong line present in the data. The temperature
            drift is independent of wavelength, so this should simply be an
            easily fitted, strong, clear line present in the data.
        yrange: tuple of 2 Quantities
            The y-range to get the average from. This range should avoid active
            regions that may disrupt the fit. The default is to use the entire
            cube.
        """
        # TODO: handle multiple exposures in a single window
        line_guess = (1000, 195.12, 0.1) if not line_guess else line_guess
        if not yrange:
            yrange = (0, self.spectra.shape[1])
        else:
            ymin = cu.convert_point(yrange[0].value, yrange[0].unit,
                                    self.wcs, 1)
            ymax = cu.convert_point(yrange[1].value, yrange[1].unit,
                                    self.wcs, 1)
            yrange = (ymin, ymax)
        # First, get the centers of the line we're correcting for, throughout
        # the whole cube. This is a 2D array. Force a recalculation and clip
        # the fitting window as well.
        x_range = kwargs.get('x_range', (194.5, 195.6))
        kwargs.update({'recalc': True, 'x_range': x_range})
        centers = self._param_array(1, line_guess, **kwargs)
        # Now get a 1-D array of the average intensities along the y-axis.
        # Ideally the range should be chosen so that this is quiet sun.
        averages = [np.average(arr[yrange[0]:yrange[1]]) for arr in centers]
        # Calculate the offset from the centroid of the emission line over the
        # given data range.
        corrections = line_guess[1] - np.array(averages)
        # Remove noise by appplying a smoothing filter and getting rid of
        # the less important frequencies.
        window_size = int(corrections.shape[0] / 3)
        window_size += 1 if window_size % 2 == 0 else 0
        corrections = savgol_filter(corrections, window_size, 3)
        corrections *= u.Angstrom
        # Add the slit tilt component to all
        slit_tilt_corr = self._get_slit_tilt()
        corr = [slit_tilt_corr + a for a in corrections]
        return np.array(corr) * u.Angstrom
Beispiel #2
0
    def slice_to_cube(self, axis, chunk, **kwargs):
        """
        For a hypercube, return a 3-D cube that has been cut along the given
        axis and with data corresponding to the given chunk.

        Parameters
        ----------
        axis: int
            The axis to cut from the hypercube
        chunk: int, astropy Quantity or tuple:
            The data to take from the axis
        """
        if self.data.ndim == 3:
            raise cu.CubeError(4, 'Can only slice a hypercube into a cube')

        item = [slice(None, None, None) for _ in range(4)]
        if isinstance(chunk, tuple):
            if cu.iter_isinstance(chunk, (u.Quantity, u.Quantity)):
                pixel0 = cu.convert_point(chunk[0].value, chunk[0].unit,
                                          self.axes_wcs, axis)
                pixel1 = cu.convert_point(chunk[1].value, chunk[1].unit,
                                          self.axes_wcs, axis)
                item[axis] = slice(pixel0, pixel1, None)
            elif cu.iter_isinstance((chunk, int, int)):
                item[axis] = slice(chunk[0], chunk[1], None)
            else:
                raise cu.CubeError(5, "Parameters must be of the same type")
            newdata = self.data[item].sum(axis)
        else:
            unit = chunk.unit if isinstance(chunk, u.Quantity) else None
            pixel = cu.convert_point(chunk, unit, self.axes_wcs, axis)
            item[axis] = pixel
            newdata = self.data[item]
        wcs_indices = [0, 1, 2, 3]
        wcs_indices.remove(3 - axis)
        newwcs = wu.reindex_wcs(self.axes_wcs, np.array(wcs_indices))
        if axis == 2 or axis == 3:
            newwcs = wu.add_celestial_axis(newwcs)
            newwcs.was_augmented = True
        cube = Cube(newdata, newwcs, meta=self.meta, **kwargs)
        return cube
Beispiel #3
0
    def slice_to_cube(self, axis, chunk, **kwargs):
        """
        For a hypercube, return a 3-D cube that has been cut along the given
        axis and with data corresponding to the given chunk.

        Parameters
        ----------
        axis: int
            The axis to cut from the hypercube
        chunk: int, astropy Quantity or tuple:
            The data to take from the axis
        """
        if self.data.ndim == 3:
            raise cu.CubeError(4, 'Can only slice a hypercube into a cube')

        item = [slice(None, None, None) for _ in range(4)]
        if isinstance(chunk, tuple):
            if cu.iter_isinstance(chunk, (u.Quantity, u.Quantity)):
                pixel0 = cu.convert_point(chunk[0].value, chunk[0].unit,
                                          self.axes_wcs, axis)
                pixel1 = cu.convert_point(chunk[1].value, chunk[1].unit,
                                          self.axes_wcs, axis)
                item[axis] = slice(pixel0, pixel1, None)
            elif cu.iter_isinstance((chunk, int, int)):
                item[axis] = slice(chunk[0], chunk[1], None)
            else:
                raise cu.CubeError(5, "Parameters must be of the same type")
            newdata = self.data[item].sum(axis)
        else:
            unit = chunk.unit if isinstance(chunk, u.Quantity) else None
            pixel = cu.convert_point(chunk, unit, self.axes_wcs, axis)
            item[axis] = pixel
            newdata = self.data[item]
        wcs_indices = [0, 1, 2, 3]
        wcs_indices.remove(3 - axis)
        newwcs = wu.reindex_wcs(self.axes_wcs, np.array(wcs_indices))
        if axis == 2 or axis == 3:
            newwcs = wu.add_celestial_axis(newwcs)
            newwcs.was_augmented = True
        cube = Cube(newdata, newwcs, meta=self.meta, **kwargs)
        return cube
Beispiel #4
0
def test_convert_point():
    assert cu.convert_point(10.0, u.Angstrom, wm, 2) == 0
    assert cu.convert_point(10.2, u.Angstrom, wm, 2) == 1
    assert cu.convert_point(9.6, u.Angstrom, wm, 2) == -2
    assert cu.convert_point(10.3, u.Angstrom, wm, 2) == 2
    assert cu.convert_point(0.001, u.mm, wm, 2) == 49950

    assert cu.convert_point(0, u.min, wt, 0) == 0
    assert cu.convert_point(3.1, u.min, wt, 0) == 8
    assert cu.convert_point(-2.4, u.min, wt, 0) == -6
    assert cu.convert_point(0, u.s, wt, 0) == 0
    assert cu.convert_point(24, u.s, wt, 0) == 1
    assert cu.convert_point(-72, u.s, wt, 0) == -3

    assert cu.convert_point(0.2, u.Angstrom, wt, 1) == 1
    assert cu.convert_point(12, None, wt, 0) == 12
    assert cu.convert_point(15.7, None, wm, 3) == 15
    assert cu.convert_point(4, u.pix, wm, 2) == 4
    assert cu.convert_point(7.2, u.pixel, wt, 1) == 7
Beispiel #5
0
def test_convert_point():
    assert cu.convert_point(10.0, u.Angstrom, wm, 2) == 0
    assert cu.convert_point(10.2, u.Angstrom, wm, 2) == 1
    assert cu.convert_point(9.6, u.Angstrom, wm, 2) == -2
    assert cu.convert_point(10.3, u.Angstrom, wm, 2) == 2
    assert cu.convert_point(0.001, u.mm, wm, 2) == 49950

    assert cu.convert_point(0, u.min, wt, 0) == 0
    assert cu.convert_point(3.1, u.min, wt, 0) == 8
    assert cu.convert_point(-2.4, u.min, wt, 0) == -6
    assert cu.convert_point(0, u.s, wt, 0) == 0
    assert cu.convert_point(24, u.s, wt, 0) == 1
    assert cu.convert_point(-72, u.s, wt, 0) == -3

    assert cu.convert_point(0.2, u.Angstrom, wt, 1) == 1
    assert cu.convert_point(12, None, wt, 0) == 12
    assert cu.convert_point(15.7, None, wm, 3) == 15
    assert cu.convert_point(4, u.pix, wm, 2) == 4
    assert cu.convert_point(7.2, u.pixel, wt, 1) == 7