示例#1
0
def test_make_image(test_directory):
    """Use PreviewImage.make_image to create preview images of a sample JWST exposure.

    Assert that the number of JPGs created corresponds to the number of integrations.

    Parameters
    ----------
    test_directory : str
        Path of directory used for testing

    """
    filenames = glob.glob(os.path.join(TEST_DATA_DIRECTORY, '*.fits'))
    print('\nGenerating preview images for {}.'.format(filenames))

    output_directory = test_directory

    for filename in filenames:

        header = fits.getheader(filename)

        # Create and save the preview image or thumbnail
        for create_thumbnail in [False, True]:
            try:
                image = PreviewImage(filename, "SCI")
                image.clip_percent = 0.01
                image.scaling = 'log'
                image.cmap = 'viridis'
                image.output_format = 'jpg'
                image.thumbnail = create_thumbnail
                image.output_directory = output_directory
                image.make_image()
            except ValueError as error:
                print(error)

            if create_thumbnail:
                extension = 'thumb'
            else:
                extension = 'jpg'

            # list of preview images
            preview_image_filenames = glob.glob(
                os.path.join(test_directory, '*.{}'.format(extension)))

            assert len(preview_image_filenames) == header['NINTS']

            # clean up: delete preview images
            for file in preview_image_filenames:
                os.remove(file)
示例#2
0
def get_image_info(file_root, rewrite):
    """Build and return a dictionary containing information for a given
    ``file_root``.

    Parameters
    ----------
    file_root : str
        The rootname of the file of interest.
    rewrite : bool
        ``True`` if the corresponding JPEG needs to be rewritten,
        ``False`` if not.

    Returns
    -------
    image_info : dict
        A dictionary containing various information for the given
        ``file_root``.
    """

    # Initialize dictionary to store information
    image_info = {}
    image_info['all_jpegs'] = []
    image_info['suffixes'] = []
    image_info['num_ints'] = {}

    preview_dir = os.path.join(get_config()['jwql_dir'], 'preview_images')

    # Find all of the matching files
    dirname = file_root[:7]
    search_filepath = os.path.join(FILESYSTEM_DIR, dirname,
                                   file_root + '*.fits')
    image_info['all_files'] = glob.glob(search_filepath)

    for file in image_info['all_files']:

        # Get suffix information
        suffix = os.path.basename(file).split('_')[4].split('.')[0]
        image_info['suffixes'].append(suffix)

        # Determine JPEG file location
        jpg_dir = os.path.join(preview_dir, dirname)
        jpg_filename = os.path.basename(
            os.path.splitext(file)[0] + '_integ0.jpg')
        jpg_filepath = os.path.join(jpg_dir, jpg_filename)

        # Check that a jpg does not already exist. If it does (and rewrite=False),
        # just call the existing jpg file
        if os.path.exists(jpg_filepath) and not rewrite:
            pass

        # If it doesn't, make it using the preview_image module
        else:
            if not os.path.exists(jpg_dir):
                os.makedirs(jpg_dir)
            im = PreviewImage(file, 'SCI')
            im.output_directory = jpg_dir
            im.make_image()

        # Record how many integrations there are per filetype
        search_jpgs = os.path.join(preview_dir, dirname,
                                   file_root + '_{}_integ*.jpg'.format(suffix))
        num_jpgs = len(glob.glob(search_jpgs))
        image_info['num_ints'][suffix] = num_jpgs

        image_info['all_jpegs'].append(jpg_filepath)

    return image_info