Exemple #1
0
        def _stack(nimages, im_shape, parang):
            im_new = None
            parang_new = None

            if self.m_stacking is not None:
                frames = memory_frames(self.m_stacking, nimages)

                nimages_new = np.size(frames) - 1

                if parang is None:
                    parang_new = None
                else:
                    parang_new = np.zeros(nimages_new)

                im_new = np.zeros((nimages_new, im_shape[1], im_shape[2]))

                for i in range(nimages_new):
                    progress(i, nimages_new, "Running StackAndSubsetModule...")

                    if parang is not None:
                        parang_new[i] = np.mean(parang[frames[i]:frames[i +
                                                                        1]])

                    im_new[i, ] = np.mean(
                        self.m_image_in_port[frames[i]:frames[i + 1], ],
                        axis=0)

                im_shape = im_new.shape

            else:
                if parang is not None:
                    parang_new = np.copy(parang)

            return im_shape, im_new, parang_new
Exemple #2
0
    def run(self) -> None:
        """
        Run method of the module. Simple background subtraction with a constant index offset.

        Returns
        -------
        NoneType
            None
        """

        nframes = self.m_image_in_port.get_shape()[0]

        subtract = self.m_image_in_port[0] - self.m_image_in_port[(0 + self.m_shift) % nframes]

        if self.m_image_in_port.tag == self.m_image_out_port.tag:
            self.m_image_out_port[0] = subtract
        else:
            self.m_image_out_port.set_all(subtract, data_dim=3)

        start_time = time.time()

        for i in range(1, nframes):
            progress(i, nframes, 'Subtracting background...', start_time)

            subtract = self.m_image_in_port[i] - self.m_image_in_port[(i + self.m_shift) % nframes]

            if self.m_image_in_port.tag == self.m_image_out_port.tag:
                self.m_image_out_port[i] = subtract
            else:
                self.m_image_out_port.append(subtract)

        history = f'shift = {self.m_shift}'
        self.m_image_out_port.copy_attributes(self.m_image_in_port)
        self.m_image_out_port.add_history('SimpleBackgroundSubtractionModule', history)
        self.m_image_out_port.close_port()
Exemple #3
0
    def run(self) -> None:
        """
        Run method of the module. Looks for all HDF5 files in the input directory and reads the
        datasets that are provided in the tag dictionary.

        Returns
        -------
        NoneType
            None
        """

        # create list of files to be read
        files = []

        tmp_dir = os.path.join(self.m_input_location, '')

        # check if a single input file is given
        if self.m_filename is not None:
            # create file path + filename
            assert(os.path.isfile((tmp_dir + str(self.m_filename)))), \
                   f'Error: Input file does not exist. Input requested: {self.m_filename}'

            files.append((tmp_dir + str(self.m_filename)))

        else:
            # look for all HDF5 files in the directory
            for tmp_file in os.listdir(self.m_input_location):
                if tmp_file.endswith('.hdf5') or tmp_file.endswith('.h5'):
                    files.append(tmp_dir + str(tmp_file))

        start_time = time.time()
        for i, tmp_file in enumerate(files):
            progress(i, len(files), 'Reading HDF5 file...', start_time)
            self.read_single_hdf5(tmp_file)
Exemple #4
0
    def run(self) -> None:
        """
        Run method of the module. Rotates all images by a constant angle.

        Returns
        -------
        NoneType
            None
        """

        memory = self._m_config_port.get_attribute('MEMORY')
        nimages = self.m_image_in_port.get_shape()[0]
        frames = memory_frames(memory, nimages)

        start_time = time.time()

        for i, _ in enumerate(frames[:-1]):
            progress(i, len(frames[:-1]), 'Rotating images...', start_time)

            images = self.m_image_in_port[frames[i]:frames[i + 1], ]

            for j in range(frames[i + 1] - frames[i]):
                im_tmp = images[j, ]

                # ndimage.rotate rotates in clockwise direction for positive angles
                im_tmp = rotate(im_tmp, self.m_angle, reshape=False)

                self.m_image_out_port.append(im_tmp, data_dim=3)

        history = f'angle [deg] = {self.m_angle}'
        self.m_image_out_port.add_history('RotateImagesModule', history)
        self.m_image_out_port.copy_attributes(self.m_image_in_port)
        self.m_image_out_port.close_port()
Exemple #5
0
    def run(self) -> None:
        """
        Run method of the module. Applies a Gaussian filter to the spatial dimensions of the
        images.

        Returns
        -------
        NoneType
            None
        """

        memory = self._m_config_port.get_attribute('MEMORY')
        pixscale = self._m_config_port.get_attribute('PIXSCALE')

        nimages = self.m_image_in_port.get_shape()[0]
        frames = memory_frames(memory, nimages)

        sigma = (self.m_fwhm / pixscale) / (2. * math.sqrt(2. * math.log(2.))
                                            )  # [pix]

        start_time = time.time()

        for i, _ in enumerate(frames[:-1]):
            progress(i, len(frames[:-1]), 'Applying Gaussian filter...',
                     start_time)

            images = self.m_image_in_port[frames[i]:frames[i + 1], ]
            im_filter = gaussian_filter(images, (0, sigma, sigma))

            self.m_image_out_port.append(im_filter, data_dim=3)

        history = f'fwhm [arcsec] = {self.m_fwhm}'
        self.m_image_out_port.add_history('GaussianFilterModule', history)
        self.m_image_out_port.copy_attributes(self.m_image_in_port)
        self.m_image_out_port.close_port()
Exemple #6
0
    def run(self) -> None:
        """
        Run method of the module. Removes the lines given by *lines* from each frame.

        Returns
        -------
        NoneType
            None
        """

        memory = self._m_config_port.get_attribute('MEMORY')
        nimages = self.m_image_in_port.get_shape()[0]
        frames = memory_frames(memory, nimages)

        start_time = time.time()

        for i in range(len(frames[:-1])):
            progress(i, len(frames[:-1]), 'Removing lines...', start_time)

            image_in = self.m_image_in_port[frames[i]:frames[i + 1], ]

            image_out = image_in[:,
                                 int(self.m_lines[2]):image_in.shape[1] -
                                 int(self.m_lines[3]),
                                 int(self.m_lines[0]):image_in.shape[2] -
                                 int(self.m_lines[1])]

            self.m_image_out_port.append(image_out, data_dim=3)

        history = f'number of lines = {self.m_lines}'
        self.m_image_out_port.add_history('RemoveLinesModule', history)
        self.m_image_out_port.copy_attributes(self.m_image_in_port)
        self.m_image_out_port.close_port()
Exemple #7
0
    def run(self):
        """
        Run method of the module. Create list of time stamps, get sky and science images, and
        subtract the sky images from the science images.

        :return: None
        """

        self.m_image_out_port.del_all_data()
        self.m_image_out_port.del_all_attributes()

        self._create_time_stamp_list()

        for i, time_entry in enumerate(self.m_time_stamps):
            progress(i, len(self.m_time_stamps),
                     "Running NoddingBackgroundModule...")

            if time_entry.m_im_type == "SKY":
                continue

            sky = self.calc_sky_frame(i)
            science = self.m_science_in_port[time_entry.m_index, ]

            self.m_image_out_port.append(science - sky[None, ], data_dim=3)

        sys.stdout.write("Running NoddingBackgroundModule... [DONE]\n")
        sys.stdout.flush()

        history = "mode = " + self.m_mode

        self.m_image_out_port.copy_attributes_from_input_port(
            self.m_science_in_port)
        self.m_image_out_port.add_history_information(
            "NoddingBackgroundModule", history)
        self.m_image_out_port.close_port()
Exemple #8
0
    def run(self) -> None:
        """
        Run method of the module. Uses the NFRAMES attribute to select the images of each cube,
        calculates the mean or median of each cube, and saves the data and attributes.

        Returns
        -------
        NoneType
            None
        """

        if self.m_image_in_port.tag == self.m_image_out_port.tag:
            raise ValueError('Input and output port should have a different tag.')

        self.m_image_out_port.del_all_data()
        self.m_image_out_port.del_all_attributes()

        non_static = self.m_image_in_port.get_all_non_static_attributes()
        nframes = self.m_image_in_port.get_attribute('NFRAMES')

        if 'PARANG' in non_static:
            parang = self.m_image_in_port.get_attribute('PARANG')
        else:
            parang = None

        current = 0
        parang_new = []

        start_time = time.time()
        for i, frames in enumerate(nframes):
            progress(i, len(nframes), 'Stacking images per FITS cube...', start_time)

            if self.m_combine == 'mean':
                im_stack = np.mean(self.m_image_in_port[current:current+frames, ], axis=0)
            elif self.m_combine == 'median':
                im_stack = np.median(self.m_image_in_port[current:current+frames, ], axis=0)

            self.m_image_out_port.append(im_stack, data_dim=3)

            if parang is not None:
                parang_new.append(np.mean(parang[current:current+frames]))

            current += frames

        nimages = np.size(nframes)

        self.m_image_out_port.copy_attributes(self.m_image_in_port)

        if 'INDEX' in non_static:
            index = np.arange(0, nimages, 1, dtype=np.int)
            self.m_image_out_port.add_attribute('INDEX', index, static=False)

        if 'NFRAMES' in non_static:
            nframes = np.ones(nimages, dtype=np.int)
            self.m_image_out_port.add_attribute('NFRAMES', nframes, static=False)

        if 'PARANG' in non_static:
            self.m_image_out_port.add_attribute('PARANG', parang_new, static=False)

        self.m_image_out_port.close_port()
Exemple #9
0
    def run(self) -> None:
        """
        Run method of the module. Create list of time stamps, get sky and science images, and
        subtract the sky images from the science images.

        Returns
        -------
        NoneType
            None
        """

        self.m_image_out_port.del_all_data()
        self.m_image_out_port.del_all_attributes()

        self._create_time_stamp_list()

        start_time = time.time()
        for i, time_entry in enumerate(self.m_time_stamps):
            progress(i, len(self.m_time_stamps), 'Running NoddingBackgroundModule...', start_time)

            if time_entry.m_im_type == 'SKY':
                continue

            sky = self.calc_sky_frame(i)
            science = self.m_science_in_port[time_entry.m_index, ]

            self.m_image_out_port.append(science - sky[None, ], data_dim=3)

        sys.stdout.write('Running NoddingBackgroundModule... [DONE]\n')
        sys.stdout.flush()

        history = f'mode = {self.m_mode}'
        self.m_image_out_port.copy_attributes(self.m_science_in_port)
        self.m_image_out_port.add_history('NoddingBackgroundModule', history)
        self.m_image_out_port.close_port()
Exemple #10
0
    def _run_single_processing(self, star_reshape, im_shape, indices):
        """
        Internal function to create the residuals, derotate the images, and write the output
        using a single process.

        Returns
        -------
        NoneType
            None
        """

        for i, pca_number in enumerate(self.m_components):
            progress(i, len(self.m_components), "Creating residuals...")

            parang = -1. * self.m_star_in_port.get_attribute(
                "PARANG") + self.m_extra_rot

            residuals, res_rot = pca_psf_subtraction(images=star_reshape,
                                                     angles=parang,
                                                     pca_number=pca_number,
                                                     pca_sklearn=self.m_pca,
                                                     im_shape=im_shape,
                                                     indices=indices)

            hist = "max PC number = " + str(np.amax(self.m_components))

            # 1.) derotated residuals
            if self.m_res_arr_out_ports is not None:
                self.m_res_arr_out_ports[pca_number].set_all(res_rot)
                self.m_res_arr_out_ports[pca_number].copy_attributes(
                    self.m_star_in_port)
                self.m_res_arr_out_ports[pca_number].add_history(
                    "PcaPsfSubtractionModule", hist)

            # 2.) mean residuals
            if self.m_res_mean_out_port is not None:
                stack = combine_residuals(method="mean", res_rot=res_rot)
                self.m_res_mean_out_port.append(stack, data_dim=3)

            # 3.) median residuals
            if self.m_res_median_out_port is not None:
                stack = combine_residuals(method="median", res_rot=res_rot)
                self.m_res_median_out_port.append(stack, data_dim=3)

            # 4.) noise-weighted residuals
            if self.m_res_weighted_out_port is not None:
                stack = combine_residuals(method="weighted",
                                          res_rot=res_rot,
                                          residuals=residuals,
                                          angles=parang)

                self.m_res_weighted_out_port.append(stack, data_dim=3)

            # 5.) clipped mean residuals
            if self.m_res_rot_mean_clip_out_port is not None:
                stack = combine_residuals(method="clipped", res_rot=res_rot)
                self.m_res_rot_mean_clip_out_port.append(stack, data_dim=3)

        sys.stdout.write("Creating residuals... [DONE]\n")
        sys.stdout.flush()
Exemple #11
0
    def run(self) -> None:
        """
        Run method of the module. Repeats the stack of input images a specified number of times.

        Returns
        -------
        NoneType
            None
        """

        self.m_image_out_port.del_all_attributes()
        self.m_image_out_port.del_all_data()

        nimages = self.m_image_in_port.get_shape()[0]
        memory = self._m_config_port.get_attribute('MEMORY')

        frames = memory_frames(memory, nimages)

        start_time = time.time()

        for i in range(self.m_repeat):
            progress(i, self.m_repeat, 'Running RepeatImagesModule...', start_time)

            for j, _ in enumerate(frames[:-1]):
                images = self.m_image_in_port[frames[j]:frames[j+1], ]
                self.m_image_out_port.append(images, data_dim=3)

        sys.stdout.write('Running RepeatImagesModule... [DONE]\n')
        sys.stdout.flush()

        history = f'repeat = {self.m_repeat}'
        self.m_image_out_port.add_history('RepeatImagesModule', history)
        self.m_image_out_port.copy_attributes(self.m_image_in_port)
        self.m_image_out_port.close_port()
Exemple #12
0
    def run(self):
        """
        Run method of the module. Looks for all HDF5 files in the input directory and reads them
        using the internal function _read_single_hdf5().

        :return: None
        """

        # create list of files to be read
        files = []

        tmp_dir = os.path.join(self.m_input_location, '')

        # check if a single input file is given
        if self.m_filename is not None:
            # create file path + filename
            assert(os.path.isfile((tmp_dir + str(self.m_filename)))), \
                   "Error: Input file does not exist. Input requested: %s" % str(self.m_filename)

            files.append((tmp_dir + str(self.m_filename)))

        else:
            # look for all HDF5 files in the directory
            for tmp_file in os.listdir(self.m_input_location):
                if tmp_file.endswith('.hdf5') or tmp_file.endswith('.h5'):
                    files.append(tmp_dir + str(tmp_file))

        for i, tmp_file in enumerate(files):
            progress(i, len(files), "Running Hdf5ReadingModule...")
            self._read_single_hdf5(tmp_file)

        sys.stdout.write("Running Hdf5ReadingModule... [DONE]\n")
        sys.stdout.flush()
Exemple #13
0
    def run(self) -> None:
        """
        Run method of the module. Creates a master dark with the same shape as the science
        data and subtracts the dark frame from the science data.

        Returns
        -------
        NoneType
            None
        """

        memory = self._m_config_port.get_attribute('MEMORY')
        nimages = self.m_image_in_port.get_shape()[0]
        frames = memory_frames(memory, nimages)

        dark = self.m_dark_in_port.get_all()

        master = _master_frame(data=np.mean(dark, axis=0),
                               im_shape=self.m_image_in_port.get_shape())

        start_time = time.time()

        for i in range(len(frames[:-1])):
            progress(i, len(frames[:-1]), 'Subtracting the dark current...',
                     start_time)

            images = self.m_image_in_port[frames[i]:frames[i + 1], ]

            self.m_image_out_port.append(images - master, data_dim=3)

        history = f'dark_in_tag = {self.m_dark_in_port.tag}'
        self.m_image_out_port.add_history('DarkCalibrationModule', history)
        self.m_image_out_port.copy_attributes(self.m_image_in_port)
        self.m_image_out_port.close_port()
Exemple #14
0
    def run(self) -> None:
        """
        Run method of the module. Subtracts the images from the second database tag from the images
        of the first database tag, on a frame-by-frame basis.

        Returns
        -------
        NoneType
            None
        """

        if self.m_image_in1_port.get_shape(
        ) != self.m_image_in2_port.get_shape():
            raise ValueError(
                'The shape of the two input tags has to be the same.')

        memory = self._m_config_port.get_attribute('MEMORY')
        nimages = self.m_image_in1_port.get_shape()[0]
        frames = memory_frames(memory, nimages)

        start_time = time.time()

        for i, _ in enumerate(frames[:-1]):
            progress(i, len(frames[:-1]), 'Subtracting images...', start_time)

            images1 = self.m_image_in1_port[frames[i]:frames[i + 1], ]
            images2 = self.m_image_in2_port[frames[i]:frames[i + 1], ]

            self.m_image_out_port.append(self.m_scaling * (images1 - images2),
                                         data_dim=3)

        history = f'scaling = {self.m_scaling}'
        self.m_image_out_port.add_history('SubtractImagesModule', history)
        self.m_image_out_port.copy_attributes(self.m_image_in1_port)
        self.m_image_out_port.close_port()
Exemple #15
0
    def run(self):
        """
        Run method of the module. Removes every NDIT+1 frame and saves the data and attributes.

        Returns
        -------
        NoneType
            None
        """

        if self.m_image_out_port.tag == self.m_image_in_port.tag:
            raise ValueError(
                'Input and output port should have a different tag.')

        self.m_image_out_port.del_all_data()
        self.m_image_out_port.del_all_attributes()

        ndit = self.m_image_in_port.get_attribute('NDIT')
        nframes = self.m_image_in_port.get_attribute('NFRAMES')
        index = self.m_image_in_port.get_attribute('INDEX')

        nframes_new = []
        index_new = []

        start_time = time.time()
        for i, item in enumerate(ndit):
            progress(i, len(ndit), 'Running RemoveLastFrameModule...',
                     start_time)

            if nframes[i] != item + 1:
                warnings.warn(
                    f'Number of frames ({nframes[i]}) is not equal to NDIT+1.')

            frame_start = np.sum(nframes[0:i])
            frame_end = np.sum(nframes[0:i + 1]) - 1

            nframes_new.append(nframes[i] - 1)
            index_new.extend(index[frame_start:frame_end])

            images = self.m_image_in_port[frame_start:frame_end, ]
            self.m_image_out_port.append(images)

        nframes_new = np.asarray(nframes_new, dtype=np.int)
        index_new = np.asarray(index_new, dtype=np.int)

        sys.stdout.write('Running RemoveLastFrameModule... [DONE]\n')
        sys.stdout.flush()

        self.m_image_out_port.copy_attributes(self.m_image_in_port)

        self.m_image_out_port.add_attribute('NFRAMES',
                                            nframes_new,
                                            static=False)
        self.m_image_out_port.add_attribute('INDEX', index_new, static=False)

        history = 'frames removed = NDIT+1'
        self.m_image_out_port.add_history('RemoveLastFrameModule', history)

        self.m_image_out_port.close_port()
Exemple #16
0
    def run(self):
        """
        Run method of the module. Calculates the minimum, maximum, sum, mean, median, and standard
        deviation of the pixel values of each image separately. NaNs are ignored for each
        calculation. The values are calculated for either the full images or a circular
        subsection of the images.

        Returns
        -------
        NoneType
            None
        """

        self.m_stat_out_port.del_all_data()
        self.m_stat_out_port.del_all_attributes()

        memory = self._m_config_port.get_attribute("MEMORY")
        pixscale = self.m_image_in_port.get_attribute("PIXSCALE")

        if self.m_position is not None:
            self.m_position = (
                int(self.m_position[1]),  # y position
                int(self.m_position[0]),  # x position
                self.m_position[2] / pixscale)  # radius (pix)

        nimages = self.m_image_in_port.get_shape()[0]
        im_shape = self.m_image_in_port.get_shape()[1:]

        frames = memory_frames(memory, nimages)

        for i, _ in enumerate(frames[:-1]):
            progress(i, len(frames[:-1]), "Running ImageStatisticsModule...")

            images = self.m_image_in_port[frames[i]:frames[i + 1], ]
            images = np.reshape(images,
                                (images.shape[0], im_shape[0] * im_shape[1]))

            if self.m_position is not None:
                rr_grid = pixel_distance(im_shape, self.m_position)
                indices = np.where(rr_grid <= self.m_position[2])[0]
                images = images[:, indices]

            nmin = np.nanmin(images, axis=1)
            nmax = np.nanmax(images, axis=1)
            nsum = np.nansum(images, axis=1)
            mean = np.nanmean(images, axis=1)
            median = np.nanmedian(images, axis=1)
            std = np.nanstd(images, axis=1)

            result = np.column_stack((nmin, nmax, nsum, mean, median, std))
            self.m_stat_out_port.append(result)

        sys.stdout.write("Running ImageStatisticsModule... [DONE]\n")
        sys.stdout.flush()

        history = "number of images = " + str(nimages)
        self.m_stat_out_port.copy_attributes(self.m_image_in_port)
        self.m_stat_out_port.add_history("ImageStatisticsModule", history)
        self.m_stat_out_port.close_port()
Exemple #17
0
    def _run_single_processing(self, star_reshape: np.ndarray,
                               im_shape: Tuple[int, int, int],
                               indices: np.ndarray) -> None:
        """
        Internal function to create the residuals, derotate the images, and write the output
        using a single process.
        """

        start_time = time.time()

        for i, pca_number in enumerate(self.m_components):
            progress(i, len(self.m_components), 'Creating residuals...',
                     start_time)

            parang = -1. * self.m_star_in_port.get_attribute(
                'PARANG') + self.m_extra_rot

            residuals, res_rot = pca_psf_subtraction(
                images=star_reshape,
                angles=parang,
                pca_number=int(pca_number),
                pca_sklearn=self.m_pca,
                im_shape=im_shape,
                indices=indices)

            hist = f'max PC number = {np.amax(self.m_components)}'

            # 1.) derotated residuals
            if self.m_res_arr_out_ports is not None:
                self.m_res_arr_out_ports[pca_number].set_all(res_rot)
                self.m_res_arr_out_ports[pca_number].copy_attributes(
                    self.m_star_in_port)
                self.m_res_arr_out_ports[pca_number].add_history(
                    'PcaPsfSubtractionModule', hist)

            # 2.) mean residuals
            if self.m_res_mean_out_port is not None:
                stack = combine_residuals(method='mean', res_rot=res_rot)
                self.m_res_mean_out_port.append(stack, data_dim=3)

            # 3.) median residuals
            if self.m_res_median_out_port is not None:
                stack = combine_residuals(method='median', res_rot=res_rot)
                self.m_res_median_out_port.append(stack, data_dim=3)

            # 4.) noise-weighted residuals
            if self.m_res_weighted_out_port is not None:
                stack = combine_residuals(method='weighted',
                                          res_rot=res_rot,
                                          residuals=residuals,
                                          angles=parang)

                self.m_res_weighted_out_port.append(stack, data_dim=3)

            # 5.) clipped mean residuals
            if self.m_res_rot_mean_clip_out_port is not None:
                stack = combine_residuals(method='clipped', res_rot=res_rot)
                self.m_res_rot_mean_clip_out_port.append(stack, data_dim=3)
Exemple #18
0
    def run(self) -> None:
        """
        Run method of the module. Applies a frame selection on the derotated residuals from the
        PSF subtraction. The pixels within an annulus (e.g. at the separation of an expected
        planet) are selected and the standard deviation is calculated. The chosen percentage
        of images with the lowest standard deviation are stored as output.

        Returns
        -------
        NoneType
            None
        """

        pixscale = self.m_image_in_port.get_attribute('PIXSCALE')
        nimages = self.m_image_in_port.get_shape()[0]
        npix = self.m_image_in_port.get_shape()[-1]

        rr_grid, _, _ = pixel_distance((npix, npix), position=None)

        pixel_select = np.where((rr_grid > self.m_annulus_radii[0] / pixscale)
                                &
                                (rr_grid < self.m_annulus_radii[1] / pixscale))

        start_time = time.time()
        phot_annulus = np.zeros(nimages)

        for i in range(nimages):
            progress(i, nimages, 'Aperture photometry...', start_time)

            phot_annulus[i] = np.sum(
                np.abs(self.m_image_in_port[i][pixel_select]))

        print(
            f'Minimum, maximum = {np.amin(phot_annulus):.2f}, {np.amax(phot_annulus):.2f}'
        )
        print(
            f'Mean, median = {np.nanmean(phot_annulus):.2f}, {np.nanmedian(phot_annulus):.2f}'
        )
        print(f'Standard deviation = {np.nanstd(phot_annulus):.2f}')

        n_select = int(nimages * self.m_percentage / 100.)
        index_del = np.argsort(phot_annulus)[n_select:]

        write_selected_data(memory=self._m_config_port.get_attribute('MEMORY'),
                            indices=index_del,
                            image_in_port=self.m_image_in_port,
                            selected_out_port=self.m_selected_out_port,
                            removed_out_port=self.m_removed_out_port)

        write_selected_attributes(indices=index_del,
                                  image_in_port=self.m_image_in_port,
                                  selected_out_port=self.m_selected_out_port,
                                  removed_out_port=self.m_removed_out_port,
                                  module_type='ResidualSelectionModule',
                                  history=f'frames removed = {index_del.size}')

        self.m_image_in_port.close_port()
Exemple #19
0
    def run(self) -> None:
        """
        Run method of the module. Removes the frames and corresponding attributes, updates the
        NFRAMES attribute, and saves the data and attributes.

        Returns
        -------
        NoneType
            None
        """

        self._initialize()

        memory = self._m_config_port.get_attribute('MEMORY')

        nimages = self.m_image_in_port.get_shape()[0]
        frames = memory_frames(memory, nimages)

        if memory == 0 or memory >= nimages:
            memory = nimages

        start_time = time.time()
        for i, _ in enumerate(frames[:-1]):
            progress(i, len(frames[:-1]), 'Running RemoveFramesModule...', start_time)

            images = self.m_image_in_port[frames[i]:frames[i+1], ]

            index_del = np.where(np.logical_and(self.m_frames >= frames[i],
                                                self.m_frames < frames[i+1]))

            write_selected_data(images,
                                self.m_frames[index_del] % memory,
                                self.m_selected_out_port,
                                self.m_removed_out_port)

        sys.stdout.write('Running RemoveFramesModule... [DONE]\n')
        sys.stdout.flush()

        history = 'frames removed = '+str(np.size(self.m_frames))

        if self.m_selected_out_port is not None:
            # Copy attributes before write_selected_attributes is used
            self.m_selected_out_port.copy_attributes(self.m_image_in_port)
            self.m_selected_out_port.add_history('RemoveFramesModule', history)

        if self.m_removed_out_port is not None:
            # Copy attributes before write_selected_attributes is used
            self.m_removed_out_port.copy_attributes(self.m_image_in_port)
            self.m_removed_out_port.add_history('RemoveFramesModule', history)

        write_selected_attributes(self.m_frames,
                                  self.m_image_in_port,
                                  self.m_selected_out_port,
                                  self.m_removed_out_port)

        self.m_image_in_port.close_port()
Exemple #20
0
def star_positions(
    input_port: InputPort,
    fwhm: Optional[int],
    position: Optional[Union[Tuple[int, int, float], Tuple[None, None, float],
                             Tuple[int, int, None]]] = None
) -> np.ndarray:
    """
    Function to return the position of the star in each image.

    Parameters
    ----------
    input_port : pynpoint.core.dataio.InputPort
        Input port where the images are stored.
    fwhm : int, None
        The FWHM (pix) of the Gaussian kernel that is used to smooth the images before the
        brightest pixel is located. No smoothing is applied if set to None.
    position : tuple(int, int, int), None
        Subframe that is selected to search for the star. The tuple contains the center (pix)
        and size (pix) (pos_x, pos_y, size). Setting `position` to None will use the full
        image to search for the star. If `position=(None, None, size)` then the center of the
        image will be used. If `position=(pos_x, pos_y, None)` then a fixed position is used
        for the aperture.

    Returns
    -------
    numpy.ndarray
        Positions (y, x) of the brightest pixel.
    """

    nimages = input_port.get_shape()[0]
    starpos = np.zeros((nimages, 2), dtype=np.int64)

    if position is not None and position[2] is None:
        # [y. x] position
        starpos[:, 0] = position[1]
        starpos[:, 1] = position[0]

    else:
        center = None
        width = None

        if position is not None:
            width = position[2]

            if position[0] is not None and position[1] is not None:
                center = position[0:2]

        start_time = time.time()

        for i in range(nimages):
            progress(i, nimages, 'Locating stellar position...', start_time)

            # [y. x] position
            starpos[i, :] = locate_star(input_port[i, ], center, width, fwhm)

    return starpos
Exemple #21
0
    def run(self):
        """
        Run method of the module. Removes every NDIT+1 frame and saves the data and attributes.

        :return: None
        """

        if self.m_image_out_port.tag == self.m_image_in_port.tag:
            raise ValueError(
                "Input and output port should have a different tag.")

        self.m_image_out_port.del_all_data()
        self.m_image_out_port.del_all_attributes()

        ndit = self.m_image_in_port.get_attribute("NDIT")
        nframes = self.m_image_in_port.get_attribute("NFRAMES")
        index = self.m_image_in_port.get_attribute("INDEX")

        nframes_new = []
        index_new = []

        for i, item in enumerate(ndit):
            progress(i, len(ndit), "Running RemoveLastFrameModule...")

            if nframes[i] != item + 1:
                warnings.warn("Number of frames (%s) is not equal to NDIT+1." %
                              nframes[i])

            frame_start = np.sum(nframes[0:i])
            frame_end = np.sum(nframes[0:i + 1]) - 1

            nframes_new.append(nframes[i] - 1)
            index_new.extend(index[frame_start:frame_end])

            images = self.m_image_in_port[frame_start:frame_end, ]
            self.m_image_out_port.append(images)

        nframes_new = np.asarray(nframes_new, dtype=np.int)
        index_new = np.asarray(index_new, dtype=np.int)

        sys.stdout.write("Running RemoveLastFrameModule... [DONE]\n")
        sys.stdout.flush()

        self.m_image_out_port.copy_attributes_from_input_port(
            self.m_image_in_port)

        self.m_image_out_port.add_attribute("NFRAMES",
                                            nframes_new,
                                            static=False)
        self.m_image_out_port.add_attribute("INDEX", index_new, static=False)

        history = "frames removed = NDIT+1"
        self.m_image_out_port.add_history_information("RemoveLastFrameModule",
                                                      history)

        self.m_image_out_port.close_port()
Exemple #22
0
    def run(self):
        """
        Run method of the module. Shifts the reference PSF to the location of the fake planet
        with an additional correction for the parallactic angle and writes the stack with images
        with the injected planet signal.

        :return: None
        """

        self.m_image_out_port.del_all_data()
        self.m_image_out_port.del_all_attributes()

        parang = self.m_image_in_port.get_attribute("PARANG")
        pixscale = self.m_image_in_port.get_attribute("PIXSCALE")

        self.m_position = (self.m_position[0] / pixscale, self.m_position[1])

        psf, ndim_psf, ndim, frames = self._init()

        for j, _ in enumerate(frames[:-1]):
            progress(j, len(frames[:-1]), "Running FakePlanetModule...")

            images = np.copy(self.m_image_in_port[frames[j]:frames[j + 1]])
            angles = parang[frames[j]:frames[j + 1]]

            if ndim_psf == 3:
                psf = np.copy(images)

            im_fake = fake_planet(images,
                                  psf,
                                  angles,
                                  self.m_position,
                                  self.m_magnitude,
                                  self.m_psf_scaling,
                                  interpolation="spline")

            if ndim == 2:
                self.m_image_out_port.set_all(im_fake)
            elif ndim == 3:
                self.m_image_out_port.append(im_fake, data_dim=3)

        sys.stdout.write("Running FakePlanetModule... [DONE]\n")
        sys.stdout.flush()

        self.m_image_out_port.copy_attributes_from_input_port(
            self.m_image_in_port)

        self.m_image_out_port.add_history_information("FakePlanetModule",
                                                      "(sep, angle, mag) = " + "(" + \
                                                      "{0:.2f}".format(self.m_position[0]* \
                                                       pixscale)+", "+ \
                                                      "{0:.2f}".format(self.m_position[1])+", "+ \
                                                      "{0:.2f}".format(self.m_magnitude)+")")

        self.m_image_out_port.close_port()
Exemple #23
0
    def run(self) -> None:
        """
        Run method of the module. Normalizes the images for the different filter widths,
        upscales the images, and crops the images to the initial image shape in order to
        align the PSF patterns.

        Returns
        -------
        NoneType
            None
        """

        self.m_image_out_port.del_all_data()
        self.m_image_out_port.del_all_attributes()

        wvl_factor = self.m_line_wvl / self.m_cnt_wvl
        width_factor = self.m_line_width / self.m_cnt_width

        nimages = self.m_image_in_port.get_shape()[0]

        start_time = time.time()
        for i in range(nimages):
            progress(i, nimages, 'Running SDIpreparationModule...', start_time)

            image = self.m_image_in_port[i, ]

            im_scale = width_factor * scale_image(image, wvl_factor,
                                                  wvl_factor)

            if i == 0:
                npix_del = im_scale.shape[-1] - image.shape[-1]

                if npix_del % 2 == 0:
                    npix_del_a = int(npix_del / 2)
                    npix_del_b = int(npix_del / 2)

                else:
                    npix_del_a = int((npix_del - 1) / 2)
                    npix_del_b = int((npix_del + 1) / 2)

            im_crop = im_scale[npix_del_a:-npix_del_b, npix_del_a:-npix_del_b]

            if npix_del % 2 == 1:
                im_crop = shift_image(im_crop, (-0.5, -0.5),
                                      interpolation='spline')

            self.m_image_out_port.append(im_crop, data_dim=3)

        sys.stdout.write('Running SDIpreparationModule... [DONE]\n')
        sys.stdout.flush()

        history = f'(line, continuum) = ({self.m_line_wvl}, {self.m_cnt_wvl})'
        self.m_image_out_port.copy_attributes(self.m_image_in_port)
        self.m_image_out_port.add_history('SDIpreparationModule', history)
        self.m_image_in_port.close_port()
Exemple #24
0
    def run(self):
        """
        Run method of the module. Removes the frames and corresponding attributes, updates the
        NFRAMES attribute, and saves the data and attributes.

        :return: None
        """

        self._initialize()

        memory = self._m_config_port.get_attribute("MEMORY")

        nimages = number_images_port(self.m_image_in_port)
        frames = memory_frames(memory, nimages)

        if memory == 0 or memory >= nimages:
            memory = nimages

        for i, _ in enumerate(frames[:-1]):
            progress(i, len(frames[:-1]), "Running RemoveFramesModule...")

            images = self.m_image_in_port[frames[i]:frames[i + 1], ]

            index_del = np.where(np.logical_and(self.m_frames >= frames[i], \
                                                self.m_frames < frames[i+1]))

            write_selected_data(images, self.m_frames[index_del] % memory,
                                self.m_selected_out_port,
                                self.m_removed_out_port)

        sys.stdout.write("Running RemoveFramesModule... [DONE]\n")
        sys.stdout.flush()

        history = "frames removed = " + str(np.size(self.m_frames))

        if self.m_selected_out_port is not None:
            # Copy attributes before write_selected_attributes is used
            self.m_selected_out_port.copy_attributes_from_input_port(
                self.m_image_in_port)
            self.m_selected_out_port.add_history_information(
                "RemoveFramesModule", history)

        if self.m_removed_out_port is not None:
            # Copy attributes before write_selected_attributes is used
            self.m_removed_out_port.copy_attributes_from_input_port(
                self.m_image_in_port)
            self.m_removed_out_port.add_history_information(
                "RemoveFramesModule", history)

        write_selected_attributes(self.m_frames, self.m_image_in_port,
                                  self.m_selected_out_port,
                                  self.m_removed_out_port)

        self.m_image_in_port.close_port()
Exemple #25
0
    def run(self) -> None:
        """
        Run method of the module. Calculates the parallactic angles of each frame by linearly
        interpolating between the start and end values of the data cubes. The values are written
        as attributes to *data_tag*. A correction of 360 deg is applied when the start and end
        values of the angles change sign at +/-180 deg.

        Returns
        -------
        NoneType
            None
        """

        parang_start = self.m_data_in_port.get_attribute('PARANG_START')
        parang_end = self.m_data_in_port.get_attribute('PARANG_END')

        steps = self.m_data_in_port.get_attribute('NFRAMES')

        if 'NDIT' in self.m_data_in_port.get_all_non_static_attributes():
            ndit = self.m_data_in_port.get_attribute('NDIT')

            if not np.all(ndit == steps):
                warnings.warn(
                    'There is a mismatch between the NDIT and NFRAMES values. The '
                    'parallactic angles are calculated with a linear interpolation by '
                    'using NFRAMES steps. A frame selection should be applied after '
                    'the parallactic angles are calculated.')

        new_angles = []

        start_time = time.time()
        for i, _ in enumerate(parang_start):
            progress(i, len(parang_start),
                     'Running AngleInterpolationModule...', start_time)

            if parang_start[i] < -170. and parang_end[i] > 170.:
                parang_start[i] += 360.

            elif parang_end[i] < -170. and parang_start[i] > 170.:
                parang_end[i] += 360.

            if steps[i] == 1:
                new_angles = np.append(
                    new_angles, [(parang_start[i] + parang_end[i]) / 2.])

            elif steps[i] != 1:
                new_angles = np.append(
                    new_angles,
                    np.linspace(parang_start[i], parang_end[i], num=steps[i]))

        sys.stdout.write('Running AngleInterpolationModule... [DONE]\n')
        sys.stdout.flush()

        self.m_data_out_port.add_attribute('PARANG', new_angles, static=False)
Exemple #26
0
    def run(self):
        """
        Run method of the module. Looks for all FITS files in the input directory and imports the
        images into the database. Note that previous database information is overwritten if
        ``overwrite=True``. The filenames are stored as attributes.

        Returns
        -------
        NoneType
            None
        """

        files = []

        if isinstance(self.m_filenames, str):
            files = self._txt_file_list()
            location = os.getcwd()

        elif isinstance(self.m_filenames, (list, tuple)):
            files = self.m_filenames
            location = os.getcwd()

        elif isinstance(self.m_filenames, type(None)):
            location = os.path.join(self.m_input_location, '')

            for filename in os.listdir(location):
                if filename.endswith(
                        '.fits') and not filename.startswith('._'):
                    files.append(filename)

            assert (
                files), 'No FITS files found in %s.' % self.m_input_location

        files.sort()

        overwrite_tags = []

        for i, fits_file in enumerate(files):
            progress(i, len(files), "Running FitsReadingModule...")

            header, shape = self._read_single_file(fits_file, location,
                                                   overwrite_tags)

            self._static_attributes(fits_file, header)
            self._non_static_attributes(header)
            self._extra_attributes(fits_file, location, shape)

            self.m_image_out_port.flush()

        sys.stdout.write("Running FitsReadingModule... [DONE]\n")
        sys.stdout.flush()

        self.m_image_out_port.close_port()
Exemple #27
0
    def run(self) -> None:
        """
        Run method of the module. Removes every NDIT+1 frame and saves the data and attributes.

        Returns
        -------
        NoneType
            None
        """

        self.m_image_out_port.del_all_data()
        self.m_image_out_port.del_all_attributes()

        ndit = self.m_image_in_port.get_attribute('NDIT')
        nframes = self.m_image_in_port.get_attribute('NFRAMES')
        index = self.m_image_in_port.get_attribute('INDEX')

        nframes_new = []
        index_new = []

        start_time = time.time()
        for i, item in enumerate(ndit):
            progress(i, len(ndit),
                     'Removing the last image of each FITS cube...',
                     start_time)

            if nframes[i] != item + 1:
                warnings.warn(
                    f'Number of frames ({nframes[i]}) is not equal to NDIT+1.')

            frame_start = np.sum(nframes[0:i])
            frame_end = np.sum(nframes[0:i + 1]) - 1

            nframes_new.append(nframes[i] - 1)
            index_new.extend(index[frame_start:frame_end])

            self.m_image_out_port.append(
                self.m_image_in_port[frame_start:frame_end, ])

        nframes_new = np.asarray(nframes_new, dtype=np.int)
        index_new = np.asarray(index_new, dtype=np.int)

        self.m_image_out_port.copy_attributes(self.m_image_in_port)

        self.m_image_out_port.add_attribute('NFRAMES',
                                            nframes_new,
                                            static=False)
        self.m_image_out_port.add_attribute('INDEX', index_new, static=False)

        history = 'frames removed = NDIT+1'
        self.m_image_out_port.add_history('RemoveLastFrameModule', history)

        self.m_image_out_port.close_port()
Exemple #28
0
    def run(self):
        """
        Run method of the module. Uses the NFRAMES attribute to select the images of each cube,
        calculates the mean of each cube, and saves the data and attributes.

        :return: None
        """

        if self.m_image_in_port.tag == self.m_image_out_port.tag:
            raise ValueError(
                "Input and output port should have a different tag.")

        non_static = self.m_image_in_port.get_all_non_static_attributes()

        nframes = self.m_image_in_port.get_attribute("NFRAMES")

        self.m_image_out_port.del_all_data()
        self.m_image_out_port.del_all_attributes()

        current = 0

        for i, frames in enumerate(nframes):
            progress(i, len(nframes), "Running MeanCubeModule...")

            mean_frame = np.mean(self.m_image_in_port[current:current +
                                                      frames, ],
                                 axis=0)

            self.m_image_out_port.append(mean_frame, data_dim=3)

            current += frames

        sys.stdout.write("Running MeanCubeModule... [DONE]\n")
        sys.stdout.flush()

        nimages = np.size(nframes)

        self.m_image_out_port.copy_attributes_from_input_port(
            self.m_image_in_port)

        if "INDEX" in non_static:
            index = np.arange(0, nimages, 1, dtype=np.int)
            self.m_image_out_port.add_attribute("INDEX", index, static=False)

        if "NFRAMES" in non_static:
            nframes = np.ones(nimages, dtype=np.int)
            self.m_image_out_port.add_attribute("NFRAMES",
                                                nframes,
                                                static=False)

        self.m_image_out_port.close_port()
Exemple #29
0
def write_selected_data(memory: Union[int, np.int64],
                        indices: np.ndarray,
                        image_in_port: InputPort,
                        selected_out_port: Optional[OutputPort],
                        removed_out_port: Optional[OutputPort]) -> None:
    """
    Function to write the selected and removed data.

    Parameters
    ----------
    memory : int
        Number of images that is simultaneously loaded into the memory.
    indices : numpy.ndarray
        Image indices that will be removed.
    image_in_port : pynpoint.core.dataio.InputPort
        Port to the input images.
    selected_out_port : pynpoint.core.dataio.OutputPort, None
        Port to store the selected images. No data is written if set to None.
    removed_out_port : pynpoint.core.dataio.OutputPort, None
        Port to store the removed images. No data is written if set to None.

    Returns
    -------
    NoneType
        None
    """

    nimages = image_in_port.get_shape()[0]
    frames = memory_frames(memory, nimages)

    if memory == 0 or memory >= nimages:
        memory = nimages

    start_time = time.time()

    for i, _ in enumerate(frames[:-1]):
        progress(i, len(frames[:-1]), 'Writing selected data...', start_time)

        images = image_in_port[frames[i]:frames[i+1], ]

        subset_del = np.where(np.logical_and(indices >= frames[i], indices < frames[i+1]))[0]
        index_del = indices[subset_del] % memory

        index_sel = np.ones(images.shape[0], np.bool)
        index_sel[index_del] = False

        if selected_out_port is not None and index_sel.size > 0:
            selected_out_port.append(images[index_sel])

        if removed_out_port is not None and index_del.size > 0:
            removed_out_port.append(images[index_del])
Exemple #30
0
    def uncompress(self) -> None:
        """
        Method to check if the input directory contains compressed files ending with .fits.Z.
        If this is the case, the files will be uncompressed using multithreading. The number
        of threads can be set with the ``CPU`` parameter in the configuration file.

        Returns
        -------
        NoneType
            None
        """

        cpu = self._m_config_port.get_attribute('CPU')

        # list all files ending with .fits.Z in the input location
        files = []
        for item in os.listdir(self.m_input_location):
            if item.endswith('.fits.Z'):
                files.append(os.path.join(self.m_input_location, item))

        if files:
            # subdivide the file indices by number of CPU
            indices = memory_frames(cpu, len(files))

            start_time = time.time()
            for i, _ in enumerate(indices[:-1]):
                progress(i, len(indices[:-1]), 'Uncompressing NEAR data...',
                         start_time)

                # select subset of compressed files
                subset = files[indices[i]:indices[i + 1]]

                # create a list of threads to uncompress CPU number of files
                # each file is processed by a different thread
                threads = []
                for filename in subset:
                    thread = threading.Thread(target=self._uncompress_file,
                                              args=(filename, ))
                    threads.append(thread)

                # start the threads
                for item in threads:
                    item.start()

                # join the threads
                for item in threads:
                    item.join()

            sys.stdout.write('Uncompressing NEAR data... [DONE]\n')
            sys.stdout.flush()