def find_all_stars(self):

        if self.exit:
            self.after(self.align)

        fits = plc.open_fits(
            os.path.join(self.log.reduction_directory, self.science_files[0]))
        metadata = self.all_frames[self.science_files[0]]

        self.progress_figure.load_fits(fits[1], self.science_files[0])

        stars, psf = plc.find_all_stars(fits[1].data,
                                        mean=metadata[self.log.mean_key],
                                        std=metadata[self.log.std_key],
                                        std_limit=3,
                                        burn_limit=self.burn_limit,
                                        star_std=metadata[self.log.psf_key],
                                        progressbar=self.progress_all_stars,
                                        progress_window=self,
                                        verbose=True)

        if self.exit:
            self.after(self.choose_calibartion_stars)

        stars = np.array(stars)

        self.log.save_local_log()

        all_stars_dict = {'all_stars': stars}
        plc.save_dict(all_stars_dict, 'all_stars.pickle')

        self.progress_all_stars.set('Choosing calibrating stars...')

        self.after(self.choose_calibartion_stars)
Exemple #2
0
def get_fits_data(fits_file_name):

    fits_file = plc.open_fits(fits_file_name)

    try:
        fits_data = [fits_file['SCI']]
    except KeyError:
        sci_id = 0
        for sci_id in range(len(fits_file)):
            try:
                if fits_file[sci_id].data.all():
                    break
            except:
                pass
        fits_data = [fits_file[sci_id]]

    del fits_file
    return fits_data
    def check_visibility(self):

        if self.exit:
            self.def_close()

        else:

            all_stars_dict = plc.open_dict('all_stars.pickle')
            stars = np.array(all_stars_dict['all_stars'])
            fits = plc.open_fits(
                os.path.join(self.log.reduction_directory,
                             self.science_files[0]))

            polar_coords = []
            for star in all_stars_dict['all_stars']:
                polar_coords.append(
                    plc.cartesian_to_polar(
                        star[0], star[1], self.all_frames[
                            self.science_files[0]][self.log.align_x0_key],
                        self.all_frames[self.science_files[0]][
                            self.log.align_y0_key]))

            in_fov = np.ones(len(polar_coords))

            for science_file in self.science_files:

                metadata = self.all_frames[science_file]

                if self.exit:
                    self.def_close()

                ref_x_position = metadata[self.log.align_x0_key]
                ref_y_position = metadata[self.log.align_y0_key]
                ref_u_position = metadata[self.log.align_u0_key]
                star_std = metadata[self.log.psf_key]

                if ref_x_position:

                    in_fov_single = []

                    for star in polar_coords:

                        cartesian_x = ref_x_position + star[0] * np.cos(
                            ref_u_position + star[1])
                        cartesian_y = ref_y_position + star[0] * np.sin(
                            ref_u_position + star[1])

                        if (3 * star_std < cartesian_x <
                                len(fits[1].data[0]) - 3 * star_std
                                and 3 * star_std < cartesian_y <
                                len(fits[1].data) - 3 * star_std):

                            in_fov_single.append(1)
                        else:
                            in_fov_single.append(0)

                    in_fov *= in_fov_single

            all_stars_dict['in_fov'] = np.array(in_fov)

            visible_fov_x_min = np.min(stars[np.where(in_fov),
                                             0]) - 3 * star_std
            visible_fov_x_max = np.max(stars[np.where(in_fov),
                                             0]) + 3 * star_std
            visible_fov_y_min = np.min(stars[np.where(in_fov),
                                             1]) - 3 * star_std
            visible_fov_y_max = np.max(stars[np.where(in_fov),
                                             1]) + 3 * star_std

            self.log.set_param('min_x', float(visible_fov_x_min))
            self.log.set_param('min_y', float(visible_fov_y_min))
            self.log.set_param('max_x', float(visible_fov_x_max))
            self.log.set_param('max_y', float(visible_fov_y_max))

            plc.save_dict(all_stars_dict, 'all_stars.pickle')

            self.log.set_param('alignment_complete', True)
            self.log.set_param('alignment_version', self.log.version)
            self.log.save_local_log()
            self.log.set_param('proceed', True)

            self.def_close()
    def __init__(self, log):

        MainWindow.__init__(self, log, name='HOPS - Alignment', position=2)

        # set variables, create and place widgets

        self.bin_fits = self.log.get_param('bin_fits')
        self.burn_limit = int(
            1.1 *
            self.log.get_param('burn_limit')) * self.bin_fits * self.bin_fits
        self.shift_tolerance_p = self.log.get_param('shift_tolerance_p')
        self.rotation_tolerance = self.log.get_param('rotation_tolerance')
        self.min_calibration_stars_number = int(
            self.log.get_param('min_calibration_stars_number'))

        self.all_frames = plc.open_dict(self.log.all_frames)
        self.science_files = []
        for science_file in self.all_frames:
            if not self.all_frames[science_file][self.log.skip_key]:
                self.science_files.append([
                    self.all_frames[science_file][self.log.time_key],
                    science_file
                ])
            else:
                self.all_frames[science_file][self.log.align_x0_key] = False
                self.all_frames[science_file][self.log.align_y0_key] = False
                self.all_frames[science_file][self.log.align_u0_key] = False

                fits = pf.open(os.path.join(self.log.reduction_directory,
                                            science_file),
                               mode='update')
                fits[1].header.set(self.log.align_x0_key, False)
                fits[1].header.set(self.log.align_y0_key, False)
                fits[1].header.set(self.log.align_u0_key, False)
                fits.flush()
                fits.close()

        self.science_files.sort()
        self.science_files = [ff[1] for ff in self.science_files]

        self.skip_time = 0
        self.science_counter = 0
        self.test_level = None
        self.redraw = None
        self.stars = None
        self.science_file = None
        self.fits = None
        self.std = None
        self.mean = None
        self.star_std = None
        self.int_psf = None
        self.stars_detected = None
        self.rotation_detected = None
        self.check_num = None
        self.check_num_snr = None
        self.x0 = None
        self.y0 = None
        self.u0 = None
        self.f0 = None
        self.comparisons = None
        self.comparisons_snr = None
        self.small_angles = None
        self.large_angles = None
        self.circle = None
        self.settings_to_check = None
        self.comparisons_to_check = None

        # common definitions for all images

        fits = plc.open_fits(
            os.path.join(self.log.reduction_directory, self.science_files[0]))

        self.shift_tolerance = int(
            max(len(fits[1].data), len(fits[1].data[0])) *
            (self.shift_tolerance_p / 100.0))
        self.y_length, self.x_length = fits[1].data.shape
        self.circles_diameter = 0.02 * max(self.y_length, self.x_length)

        # progress window

        y_scale = (self.root.winfo_screenheight() -
                   500) / self.root.winfo_screenheight()

        self.progress_figure = self.FitsWindow(figsize=(0.5, y_scale, 10, 10,
                                                        len(fits[1].data[0]) /
                                                        len(fits[1].data)))
        self.progress_figure.load_fits(fits[1],
                                       input_name=self.science_files[0])
        self.progress_all_stars = self.Label(text='')
        self.progress_alignment = self.Progressbar(task="Aligning frames")
        # self.progress_all_frames = self.Progressbar(task="Aligning all stars in all frames")

        self.setup_window([[[self.progress_figure, 0, 2]],
                           [[self.progress_all_stars, 0, 2]],
                           [[self.progress_alignment, 0, 2]],
                           [[
                               self.Button(
                                   text='STOP ALIGNMENT & RETURN TO MAIN MENU',
                                   command=self.trigger_exit), 0, 2
                           ]], []])

        self.set_close_button_function(self.trigger_exit)