Example #1
0
    def run(self) -> None:
        """
        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()

            for item in files:
                if not os.path.isfile(item):
                    raise ValueError(
                        f'The file {item} does not exist. Please check that the '
                        f'path is correct.')

        elif isinstance(self.m_filenames, list):
            files = self.m_filenames

            for item in files:
                if not os.path.isfile(item):
                    raise ValueError(
                        f'The file {item} does not exist. Please check that the '
                        f'path is correct.')

        elif isinstance(self.m_filenames, type(None)):
            for filename in os.listdir(self.m_input_location):
                if filename.endswith(
                        '.fits') and not filename.startswith('._'):
                    files.append(os.path.join(self.m_input_location, filename))

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

        files.sort()

        overwrite_tags = []
        first_index = 0

        start_time = time.time()
        for i, fits_file in enumerate(files):
            progress(i, len(files), 'Reading FITS files...', start_time)

            header, shape = self.read_single_file(fits_file, overwrite_tags)

            if len(shape) == 2:
                nimages = 1

            elif len(shape) == 3:
                if self.m_ifs_data:
                    nimages = 1
                else:
                    nimages = shape[0]

            elif len(shape) == 4:
                nimages = shape[1]

            else:
                raise ValueError(
                    'Data read from FITS file has an invalid shape.')

            set_static_attr(fits_file=fits_file,
                            header=header,
                            config_port=self._m_config_port,
                            image_out_port=self.m_image_out_port,
                            check=self.m_check)

            set_nonstatic_attr(header=header,
                               config_port=self._m_config_port,
                               image_out_port=self.m_image_out_port,
                               check=self.m_check)

            set_extra_attr(fits_file=fits_file,
                           nimages=nimages,
                           config_port=self._m_config_port,
                           image_out_port=self.m_image_out_port,
                           first_index=first_index)

            first_index += nimages

            self.m_image_out_port.flush()

        self.m_image_out_port.close_port()
Example #2
0
    def run(self) -> None:
        """
        Run the module. The FITS files are collected from the input directory and uncompressed if
        needed. The images are then sorted by the two chop positions (chop A and chop B). The
        required FITS header keywords (which should be set in the configuration file) are also
        imported and stored as attributes to the two output datasets in the HDF5 database.

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

        # clear the output ports
        self.m_chopa_out_port.del_all_data()
        self.m_chopa_out_port.del_all_attributes()
        self.m_chopb_out_port.del_all_data()
        self.m_chopb_out_port.del_all_attributes()

        # uncompress the FITS files if needed
        self.uncompress()

        # find and sort the FITS files
        files = []

        for filename in os.listdir(self.m_input_location):
            if filename.endswith('.fits'):
                files.append(os.path.join(self.m_input_location, filename))

        files.sort()

        # check if there are FITS files present in the input location
        assert (files), f'No FITS files found in {self.m_input_location}.'

        start_time = time.time()
        for i, filename in enumerate(files):
            progress(i, len(files), 'Running NearReadingModule...', start_time)

            # get the primary header data and the image shape
            header, im_shape = self.read_header(filename)

            # get the images of chop A and chop B
            chopa, chopb = self.read_images(filename, im_shape)

            # append the images of chop A and B
            self.m_chopa_out_port.append(chopa, data_dim=3)
            self.m_chopb_out_port.append(chopb, data_dim=3)

            # starting value for the INDEX attribute
            first_index = 0

            for port in (self.m_chopa_out_port, self.m_chopb_out_port):

                # set the static attributes
                set_static_attr(fits_file=filename,
                                header=header,
                                config_port=self._m_config_port,
                                image_out_port=port,
                                check=True)

                # set the non-static attributes
                set_nonstatic_attr(header=header,
                                   config_port=self._m_config_port,
                                   image_out_port=port,
                                   check=True)

                # set the remaining attributes
                set_extra_attr(fits_file=filename,
                               nimages=im_shape[0] // 2,
                               config_port=self._m_config_port,
                               image_out_port=port,
                               first_index=first_index)

                # increase the first value of the INDEX attribute
                first_index += im_shape[0] // 2

                # flush the output port
                port.flush()

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

        # add history information
        self.m_chopa_out_port.add_history('NearReadingModule', 'Chop A')
        self.m_chopb_out_port.add_history('NearReadingModule', 'Chop B')

        # close all connections to the database
        self.m_chopa_out_port.close_port()
Example #3
0
    def run(self) -> None:
        """
        Run the module. The FITS files are collected from the input directory and uncompressed if
        needed. The images are then sorted by the two chop positions (chop A and chop B). The
        required FITS header keywords (which should be set in the configuration file) are also
        imported and stored as attributes to the two output datasets in the HDF5 database.

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

        # clear the output ports
        self.m_chopa_out_port.del_all_data()
        self.m_chopa_out_port.del_all_attributes()
        self.m_chopb_out_port.del_all_data()
        self.m_chopb_out_port.del_all_attributes()

        # uncompress the FITS files if needed
        self.uncompress()

        # find and sort the FITS files
        files = []

        for filename in os.listdir(self.m_input_location):
            if filename.endswith('.fits'):
                files.append(os.path.join(self.m_input_location, filename))

        files.sort()

        # check if there are FITS files present in the input location
        assert (files), f'No FITS files found in {self.m_input_location}.'

        # if cropping chop A, get pixscale and convert crop_size to pixels and swap x/y
        if self.m_crop is not None:
            pixscale = self._m_config_port.get_attribute('PIXSCALE')
            self.m_crop = (self.m_crop[1], self.m_crop[0],
                           int(math.ceil(self.m_crop[2] / pixscale)))

        start_time = time.time()
        for i, filename in enumerate(files):
            progress(i, len(files), 'Preprocessing NEAR data...', start_time)

            # get the primary header data and the image shape
            header, im_shape = self.read_header(filename)

            # get the images of chop A and chop B
            chopa, chopb = self.read_images(filename, im_shape)

            if self.m_subtract:
                chopa = chopa - chopb
                chopb = -1. * np.copy(chopa)

            if self.m_crop is not None:
                chopa = crop_image(chopa,
                                   center=self.m_crop[0:2],
                                   size=self.m_crop[2],
                                   copy=False)

                chopb = crop_image(chopb,
                                   center=self.m_crop[0:2],
                                   size=self.m_crop[2],
                                   copy=False)

            if self.m_combine is not None:

                if self.m_combine == 'mean':
                    chopa = np.mean(chopa, axis=0)
                    chopb = np.mean(chopb, axis=0)

                elif self.m_combine == 'median':
                    chopa = np.median(chopa, axis=0)
                    chopb = np.median(chopb, axis=0)

                header[self._m_config_port.get_attribute('NFRAMES')] = 1

            # append the images of chop A and B
            self.m_chopa_out_port.append(chopa, data_dim=3)
            self.m_chopb_out_port.append(chopb, data_dim=3)

            # starting value for the INDEX attribute
            first_index = 0

            for port in (self.m_chopa_out_port, self.m_chopb_out_port):

                # set the static attributes
                set_static_attr(fits_file=filename,
                                header=header,
                                config_port=self._m_config_port,
                                image_out_port=port,
                                check=True)

                # set the non-static attributes
                set_nonstatic_attr(header=header,
                                   config_port=self._m_config_port,
                                   image_out_port=port,
                                   check=True)

                # set the remaining attributes
                set_extra_attr(fits_file=filename,
                               nimages=im_shape[0] // 2,
                               config_port=self._m_config_port,
                               image_out_port=port,
                               first_index=first_index)

                # increase the first value of the INDEX attribute
                first_index += im_shape[0] // 2

                # flush the output port
                port.flush()

        # add history information
        self.m_chopa_out_port.add_history('NearReadingModule', 'Chop A')
        self.m_chopb_out_port.add_history('NearReadingModule', 'Chop B')

        # close all connections to the database
        self.m_chopa_out_port.close_port()