Example #1
0
definition.add_flag("replace_masks", "replace masks", False)
definition.add_flag("replace_segments", "replace segmentation maps", False)
definition.add_flag("backup", "make backup", False)
config = parse_arguments("interpolate", definition)

# -----------------------------------------------------------------

if config.replace: config.replace_frames = config.replace_masks = config.replace_segments = True

# -----------------------------------------------------------------

# Inform the user
log.info("Loading the images ...")

# Load
to_image = Image.from_file(config.add_to)
from_image = Image.from_file(config.add_from)

# -----------------------------------------------------------------

# Add the frames
if config.frames:

    # Inform the user
    log.info("Adding the frames ...")

    # Loop over the frames
    for frame_name in from_image.frames:

        # Check
        if frame_name in to_image.frames:
Example #2
0
#control_panels_to_load = [("Source", SourcePanel),
#                          ("Color", ColorPanel),
#                          ("Plot", PlotPanel),
#                          ("Stats", StatsPanel),
#                          ("Phot", PhotPanel),
#                          ("Faker", FitsFakerPanel)
#                          ]

# ----->

#viewer = MagicViewer(control_panels_module_path='control_panels')


# Load the image first
image = Image.from_file(arguments.filename)
#frame = Frame.from_file(arguments.filename)

# Create the viewer
viewer = MagicViewer(title="Magic viewer")

# Load the image into the viewer
viewer.load_image(image)
#viewer.load_frame(frame)

# Set options
viewer.control_panel('Color')
viewer.cmap("hot")
viewer.scaling('Log')

# Wait for the GUI to be closed
Example #3
0
if config.backup:

    # Inform the user
    log.info("Making a backup of the original image ...")

    # Determine new filepath and copy
    new_filepath = fs.appended_filepath(config.filepath, "_backup")
    fs.copy_file(config.filepath, new_filepath)

# -----------------------------------------------------------------

# Inform the user
log.info("Loading the image '" + config.filepath + "' ...")

# Load the image
image = Image.from_file(config.filepath)

# -----------------------------------------------------------------

# Debugging
sum_before = np.nansum(image.primary.data)
log.debug("Sum of the primary frame before multiplication is " + str(sum_before))

# -----------------------------------------------------------------

# Inform th euser
log.info("Multiplying the image with a factor of " + str(config.factor) + " ...")

# Multiply
image *= config.factor
Example #4
0
# Load the star region
star_region_path = fs.join(input_path, "stars.reg")
star_region = Region.from_file(star_region_path) if fs.is_file(star_region_path) else None

# Load the saturation region
saturation_region_path = fs.join(input_path, "saturation.reg")
saturation_region = Region.from_file(saturation_region_path) if fs.is_file(saturation_region_path) else None

# Load the region of other sources
other_region_path = fs.join(input_path, "other_sources.reg")
other_region = Region.from_file(other_region_path) if fs.is_file(other_region_path) else None

# Load the image with segmentation maps
segments_path = fs.join(input_path, "segments.fits")
segments = Image.from_file(segments_path, no_filter=True)

# Get the segmentation maps
galaxy_segments = segments.frames.galaxies if "galaxies" in segments.frames else None
star_segments = segments.frames.stars if "stars" in segments.frames else None
other_segments = segments.frames.other_sources if "other_sources" in segments.frames else None

# -----------------------------------------------------------------

# If visualisation is enabled, set the visualisation path (=output path)
if arguments.visualise:
    animation = Animation()
    animation.fps = 1
else: animation = None

# -----------------------------------------------------------------
Example #5
0
    saturation_region = saturation_sky_region.to_pixel(image.wcs)
    path = fs.join(output_path, "saturation.reg")
    saturation_region.save(path)

# Save the region of other sources
other_sky_region = finder.other_sky_region
if other_sky_region is not None:

    other_region = other_sky_region.to_pixel(image.wcs)
    path = fs.join(output_path, "other_sources.reg")
    other_region.save(path)

# -----------------------------------------------------------------

# Create an image with the segmentation maps
segments = Image("segments")

# Add the segmentation map of the galaxies
segments.add_frame(finder.galaxy_segments, "galaxies")

# Add the segmentation map of the saturated stars
segments.add_frame(finder.star_segments, "stars")

# Add the segmentation map of the other sources
segments.add_frame(finder.other_segments, "other_sources")

# Save the FITS file with the segmentation maps
path = fs.join(output_path, "segments.fits")
segments.save(path)

# -----------------------------------------------------------------
Example #6
0
def Magic(pod, source_dict, band_dict, kwargs_dict):

    # Run AstroMagic in try-while statements; if debug mode is disabled, an un-star-subtracted map will be used after 10 crashes
    am_crash = 0
    am_fail = True
    while am_fail:
        try:

            # Ensure star-subtraction is actually required
            if kwargs_dict['starsub'] != True:
                return pod
            if band_dict['remove_stars'] != True:
                return pod
            if band_dict['band_name'] in str(
                    source_dict['starsub_bands_exclude']).split(';'):
                print '[' + pod[
                    'id'] + '] User explicitly excluded current band from star subtraction for this source.'
                return pod
            if str(source_dict['starsub_bands_exclude']) == 'True':
                print '[' + pod[
                    'id'] + '] User explicitly requested no star subtraction for this source.'
                return pod
            if pod['verbose']:
                print '[' + pod[
                    'id'] + '] Removing foreground stars and background galaxies with PTS AstroMagic.'

            # The paths to the image and output (absolute or relative to the current working directory)
            in_fitspath = pod['in_fitspath']
            temp_dir_path = pod['temp_dir_path']
            output_path = os.path.join(temp_dir_path, 'AstroMagic',
                                       band_dict['band_name'])
            image_path = in_fitspath

            # Setting the log level to "ERROR" disables all output from PTS except error messages (probably want to see those); full options are "DEBUG", "INFO", "WARNING", "ERROR", "SUCCESS"
            logging.setup_log(level="ERROR")

            # The path to the bad region (as explained in the previous script that I sent you)
            bad_region_path = None

            # The FWHM of the image (if known)
            fwhm = band_dict['beam_arcsec'] * Unit("arcsec")

            # Import the image
            importer = ImageImporter()
            importer.run(image_path,
                         bad_region_path=bad_region_path,
                         fwhm=fwhm,
                         find_error_frame=False)

            # Get the imported image
            image = importer.image

            # Get the mask of bad pixels
            bad_mask = image.masks.bad if "bad" in image.masks else None

            # If version of cutout alrady processed by AstroMagic is present, use it; else, commence regular processing
            if os.path.exists(
                    os.path.join(
                        temp_dir_path, 'AstroMagic', band_dict['band_name'],
                        source_dict['name'] + '_' + band_dict['band_name'] +
                        '_StarSub.fits')):
                if pod['verbose']:
                    print '[' + pod[
                        'id'] + '] AstroMagic accessing pre-processed data for this map.'
                am_output = astropy.io.fits.getdata(
                    os.path.join(
                        temp_dir_path, 'AstroMagic', band_dict['band_name'],
                        source_dict['name'] + '_' + band_dict['band_name'] +
                        '_StarSub.fits'))
                pod['cutout'] = am_output
                pod['pre_reg'] = True
                return pod

            # Check that pre-fetched catalogue files exist; if not, return pod
            if os.path.exists(
                    os.path.join(kwargs_dict['temp_dir_path'], 'AstroMagic',
                                 'Stars.cat')) and os.path.exists(
                                     os.path.join(kwargs_dict['temp_dir_path'],
                                                  'AstroMagic',
                                                  'Galaxies.cat')):
                pass
            else:
                return pod

            # The path to the directory where all the output will be placed
            if os.path.exists(
                    os.path.join(temp_dir_path, 'AstroMagic',
                                 band_dict['band_name'])):
                shutil.rmtree(
                    os.path.join(temp_dir_path, 'AstroMagic',
                                 band_dict['band_name']))
            os.mkdir(
                os.path.join(temp_dir_path, 'AstroMagic',
                             band_dict['band_name']))

            # Make copies of pre-fetched catalogues, to prevent simultaneous access conflicts
            shutil.copy(
                os.path.join(kwargs_dict['temp_dir_path'], 'AstroMagic',
                             'Stars.cat'),
                os.path.join(temp_dir_path, 'AstroMagic',
                             band_dict['band_name'], 'Stars.cat'))
            shutil.copy(
                os.path.join(kwargs_dict['temp_dir_path'], 'AstroMagic',
                             'Galaxies.cat'),
                os.path.join(temp_dir_path, 'AstroMagic',
                             band_dict['band_name'], 'Galaxies.cat'))

            # Create a CatalogImporter instance, and run it to import catalogues
            catalog_importer = CatalogImporter()
            catalog_importer.config.stars.use_catalog_file = True
            catalog_importer.config.galaxies.use_catalog_file = True
            catalog_importer.config.stars.catalog_path = os.path.join(
                temp_dir_path, 'AstroMagic', band_dict['band_name'],
                'Stars.cat')
            catalog_importer.config.galaxies.catalog_path = os.path.join(
                temp_dir_path, 'AstroMagic', band_dict['band_name'],
                'Galaxies.cat')
            catalog_importer.run(image.frames.primary)

            # If currently handling cut-down thumbnail, only use relevant portion of catalogues
            #            if 'starsub_thumbnail' in pod.keys():
            #                if pod['starsub_thumbnail']==True:
            galaxy_catalog_thumb = ThumbCatalogue(pod, source_dict, band_dict,
                                                  kwargs_dict,
                                                  catalog_importer)
            catalog_importer.galactic_catalog = galaxy_catalog_thumb

            # Create a SourceFinder instance
            finder = SourceFinder()

            # If you don't want to do the 'find_other_sources' step, comment-out the line below
            finder.config.find_other_sources = False  # default is True

            # Downsample map for faster run time
            if int(band_dict['downsample_factor']) > 1:
                if pod['verbose']:
                    print '[' + pod[
                        'id'] + '] AstroMagic will run on copy of map downsampled by factor of ' + str(
                            int(band_dict['downsample_factor'])
                        ) + ', to improve speed.'
                finder.config.downsample_factor = int(
                    band_dict['downsample_factor'])

            # Run the source finder
            if pod['verbose']:
                print '[' + pod[
                    'id'] + '] AstroMagic locating online catalogue sources in map.'
            special_region = None  # Not important except for debugging
            ignore_region = None  # Not important except when certain areas need to be completely ignored from the extraction procedure
            finder.config.build_catalogs = False  # For using pre-fetched catalogue files
            try:
                finder.run(image.frames.primary,
                           catalog_importer.galactic_catalog,
                           catalog_importer.stellar_catalog, special_region,
                           ignore_region, bad_mask)
            except RuntimeError as e:
                if 'found' in e.message:
                    if pod['verbose']:
                        print '[' + pod[
                            'id'] + '] AstroMagic found no sources to remove.'
                    pod['pre_reg'] = True
                    return pod
                else:
                    pdb.set_trace()

            # Save the galaxy region
            galaxy_region = finder.galaxy_region
            galaxy_region_path = filesystem.join(output_path, "galaxies.reg")
            galaxy_region.save(galaxy_region_path)

            # Save the star region
            star_region = finder.star_region
            star_region_path = filesystem.join(output_path, "stars.reg")
            if star_region is not None: star_region.save(star_region_path)

            # Save the saturation region
            saturation_region = finder.saturation_region
            saturation_region_path = filesystem.join(output_path,
                                                     "saturation.reg")
            if saturation_region is not None:
                saturation_region.save(saturation_region_path)

            # Save the region of other sources
            other_region = finder.other_region
            other_region_path = filesystem.join(output_path,
                                                "other_sources.reg")
            if other_region is not None: other_region.save(other_region_path)

            # Get the segmentation maps (galaxies, stars and other sources) from the SourceFinder
            galaxy_segments = finder.galaxy_segments
            star_segments = finder.star_segments
            other_segments = finder.other_segments

            # Make sure target galaxy isn't identified as star segment
            target_segment = star_segments[int(round(pod['centre_i'])),
                                           int(round(pod['centre_j']))]
            star_segments[np.where(star_segments == target_segment)] = 0.0

            # Handle stars that have been conflated with the target galaxy
            star_segments = OverlargeStars(pod, star_segments,
                                           saturation_region_path,
                                           star_region_path,
                                           galaxy_region_path, image,
                                           source_dict, band_dict,
                                           temp_dir_path)

            # Region files can be adjusted by the user; if this is done, they have to be reloaded
            star_region = Region.from_file(
                star_region_path.replace('.reg', '_revised.reg'))
            saturation_region = Region.from_file(
                saturation_region_path.replace('.reg', '_revised.reg'))
            """
            # Remove all but target galaxy from galaxy region file
            ExcessGalaxies(galaxy_region_path, galaxy_principal)
            galaxy_region = Region.from_file(galaxy_region_path.replace('.reg','_revised.reg'))
            """
            # Remopve all galaxies from galaxy region file
            shutil.copy2(galaxy_region_path,
                         galaxy_region_path.replace('.reg', '_revised.reg'))
            gal_header = '# Region file format: DS9 version 4.1\n'
            gal_file_new = open(
                galaxy_region_path.replace('.reg', '_revised.reg'), 'w')
            gal_file_new.write(gal_header)
            gal_file_new.close()
            galaxy_region = Region.from_file(
                galaxy_region_path.replace('.reg', '_revised.reg'))

            # Create a map of the the segmentation maps
            segments = Image("segments")

            # Add the segmentation map of the galaxies
            segments.add_frame(galaxy_segments, "galaxies")

            # Add the segmentation map of the saturated stars
            if star_segments is not None:
                segments.add_frame(star_segments, "stars")

            # Add the segmentation map of the other sources
            if other_segments is not None:
                segments.add_frame(other_segments, "other_sources")

            # Save the FITS file with the segmentation maps
            path = filesystem.join(output_path, "segments.fits")
            segments.save(path)

            # Create an SourceExtractor instance
            if pod['verbose']:
                print '[' + pod[
                    'id'] + '] AstroMagic extracting background sources.'
            extractor = SourceExtractor()

            # Run the source extractor
            extractor.run(image.frames.primary, galaxy_region, star_region,
                          saturation_region, other_region, galaxy_segments,
                          star_segments, other_segments)

            # Determine the path to the result
            result_path = filesystem.join(
                temp_dir_path, 'AstroMagic', band_dict['band_name'],
                source_dict['name'] + '_' + band_dict['band_name'] +
                '_StarSub.fits')

            # Save the resulting image as a FITS file
            image.frames.primary.save(result_path,
                                      header=image.original_header)

            # Grab AstroMagic output and return in pod
            am_output = astropy.io.fits.getdata(result_path)
            pod['cutout'] = am_output
            pod['pre_reg'] = True
            am_fail = False
            return pod

        # Handle exceptions
        except:
            am_crash += 1
            if am_crash >= 10:
                if kwargs_dict['debug']:
                    print '[' + pod['id'] + '] AstroMagic failed for ' + pod[
                        'id'] + '! Suggest debugging.'
                    raise ValueError('AstroMagic failed for ' + pod['id'] +
                                     '! Suggest debugging.')
                else:
                    print '[' + pod['id'] + '] AstroMagic failed for ' + pod[
                        'id'] + '! Suggest debugging.'
                    return pod
Example #7
0
definition.add_flag("replace_segments", "replace segmentation maps", False)
definition.add_flag("backup", "make backup", False)
config = parse_arguments("interpolate", definition)

# -----------------------------------------------------------------

if config.replace:
    config.replace_frames = config.replace_masks = config.replace_segments = True

# -----------------------------------------------------------------

# Inform the user
log.info("Loading the images ...")

# Load
to_image = Image.from_file(config.add_to)
from_image = Image.from_file(config.add_from)

# -----------------------------------------------------------------

# Add the frames
if config.frames:

    # Inform the user
    log.info("Adding the frames ...")

    # Loop over the frames
    for frame_name in from_image.frames:

        # Check
        if frame_name in to_image.frames:
Example #8
0
#from ztv_examples.fits_faker_panel.fits_faker_panel import FitsFakerPanel

#control_panels_to_load = [("Source", SourcePanel),
#                          ("Color", ColorPanel),
#                          ("Plot", PlotPanel),
#                          ("Stats", StatsPanel),
#                          ("Phot", PhotPanel),
#                          ("Faker", FitsFakerPanel)
#                          ]

# ----->

#viewer = MagicViewer(control_panels_module_path='control_panels')

# Load the image first
image = Image.from_file(arguments.filename)
#frame = Frame.from_file(arguments.filename)

# Create the viewer
viewer = MagicViewer(title="Magic viewer")

# Load the image into the viewer
viewer.load_image(image)
#viewer.load_frame(frame)

# Set options
viewer.control_panel('Color')
viewer.cmap("hot")
viewer.scaling('Log')

# Wait for the GUI to be closed
def Magic(pod, source_dict, band_dict, kwargs_dict):



    # Run AstroMagic in try-while statements; if debug mode is disabled, an un-star-subtracted map will be used after 10 crashes
    am_crash = 0
    am_fail = True
    while am_fail:
        try:



            # Ensure star-subtraction is actually required
            if kwargs_dict['starsub']!=True:
                return pod
            if band_dict['remove_stars']!=True:
                return pod
            if band_dict['band_name'] in str(source_dict['starsub_bands_exclude']).split(';'):
                print '['+pod['id']+'] User explicitly excluded current band from star subtraction for this source.'
                return pod
            if str(source_dict['starsub_bands_exclude'])=='True':
                print '['+pod['id']+'] User explicitly requested no star subtraction for this source.'
                return pod
            if pod['verbose']: print '['+pod['id']+'] Removing foreground stars and background galaxies with PTS AstroMagic.'



            # The paths to the image and output (absolute or relative to the current working directory)
            in_fitspath = pod['in_fitspath']
            temp_dir_path = pod['temp_dir_path']
            output_path = os.path.join(temp_dir_path, 'AstroMagic', band_dict['band_name'])
            image_path = in_fitspath



            # Setting the log level to "ERROR" disables all output from PTS except error messages (probably want to see those); full options are "DEBUG", "INFO", "WARNING", "ERROR", "SUCCESS"
            logging.setup_log(level="ERROR")



            # The path to the bad region (as explained in the previous script that I sent you)
            bad_region_path = None

            # The FWHM of the image (if known)
            fwhm = band_dict['beam_arcsec'] * Unit("arcsec")

            # Import the image
            importer = ImageImporter()
            importer.run(image_path, bad_region_path=bad_region_path, fwhm=fwhm, find_error_frame=False)

            # Get the imported image
            image = importer.image

            # Get the mask of bad pixels
            bad_mask = image.masks.bad if "bad" in image.masks else None



            # If version of cutout alrady processed by AstroMagic is present, use it; else, commence regular processing
            if os.path.exists( os.path.join( temp_dir_path, 'AstroMagic', band_dict['band_name'], source_dict['name']+'_'+band_dict['band_name']+'_StarSub.fits') ):
                if pod['verbose']: print '['+pod['id']+'] AstroMagic accessing pre-processed data for this map.'
                am_output = astropy.io.fits.getdata( os.path.join( temp_dir_path, 'AstroMagic', band_dict['band_name'], source_dict['name']+'_'+band_dict['band_name']+'_StarSub.fits') )
                pod['cutout'] = am_output
                pod['pre_reg'] = True
                return pod



            # Check that pre-fetched catalogue files exist; if not, return pod
            if os.path.exists(os.path.join(kwargs_dict['temp_dir_path'], 'AstroMagic', 'Stars.cat')) and os.path.exists(os.path.join(kwargs_dict['temp_dir_path'], 'AstroMagic', 'Galaxies.cat')):
                pass
            else:
                return pod

            # The path to the directory where all the output will be placed
            if os.path.exists(os.path.join(temp_dir_path, 'AstroMagic', band_dict['band_name'])):
                shutil.rmtree(os.path.join(temp_dir_path, 'AstroMagic', band_dict['band_name']))
                os.mkdir(os.path.join(temp_dir_path, 'AstroMagic', band_dict['band_name']))
            else:
                os.mkdir(os.path.join(temp_dir_path, 'AstroMagic', band_dict['band_name']))

            # Make copies of pre-fetched catalogues, to prevent simultaneous access conflicts
            shutil.copy(os.path.join(kwargs_dict['temp_dir_path'], 'AstroMagic', 'Stars.cat'), os.path.join(temp_dir_path, 'AstroMagic', band_dict['band_name'], 'Stars.cat'))
            shutil.copy(os.path.join(kwargs_dict['temp_dir_path'], 'AstroMagic', 'Galaxies.cat'), os.path.join(temp_dir_path, 'AstroMagic', band_dict['band_name'], 'Galaxies.cat'))



            # Create a CatalogImporter instance, and run it to import catalogues
            catalog_importer = CatalogImporter()
            catalog_importer.config.stars.use_catalog_file = True
            catalog_importer.config.galaxies.use_catalog_file = True
            catalog_importer.config.stars.catalog_path = os.path.join(temp_dir_path, 'AstroMagic', band_dict['band_name'], 'Stars.cat')
            catalog_importer.config.galaxies.catalog_path = os.path.join(temp_dir_path, 'AstroMagic', band_dict['band_name'], 'Galaxies.cat')
            catalog_importer.run(image.frames.primary)

            # If currently handling cut-down thumbnail, only use relevant portion of catalogues
#            if 'starsub_thumbnail' in pod.keys():
#                if pod['starsub_thumbnail']==True:
            galaxy_catalog_thumb = ThumbCatalogue(pod, source_dict, band_dict, kwargs_dict, catalog_importer)
            catalog_importer.galactic_catalog = galaxy_catalog_thumb



            # Create a SourceFinder instance
            finder = SourceFinder()

            # If you don't want to do the 'find_other_sources' step, comment-out the line below
            finder.config.find_other_sources = False # default is True

            # Downsample map for faster run time
            if int(band_dict['downsample_factor'])>1:
                if pod['verbose']: print '['+pod['id']+'] AstroMagic will run on copy of map downsampled by factor of '+str(int(band_dict['downsample_factor']))+', to improve speed.'
                finder.config.downsample_factor = int( band_dict['downsample_factor'] )

            # Run the source finder
            if pod['verbose']: print '['+pod['id']+'] AstroMagic locating online catalogue sources in map.'
            special_region = None # Not important except for debugging
            ignore_region = None # Not important except when certain areas need to be completely ignored from the extraction procedure
            finder.config.build_catalogs = False # For using pre-fetched catalogue files
            try:
                finder.run(image.frames.primary, catalog_importer.galactic_catalog, catalog_importer.stellar_catalog, special_region, ignore_region, bad_mask)
            except RuntimeError as e:
                if 'found' in e.message:
                    if pod['verbose']: print '['+pod['id']+'] AstroMagic found no sources to remove.'
                    pod['pre_reg'] = True
                    return pod
                else:
                    pdb.set_trace()



            # Save the galaxy region
            galaxy_region = finder.galaxy_region
            galaxy_region_path = filesystem.join(output_path, "galaxies.reg")
            galaxy_region.save(galaxy_region_path)

            # Save the star region
            star_region = finder.star_region
            star_region_path = filesystem.join(output_path, "stars.reg")
            if star_region is not None: star_region.save(star_region_path)

            # Save the saturation region
            saturation_region = finder.saturation_region
            saturation_region_path = filesystem.join(output_path, "saturation.reg")
            if saturation_region is not None: saturation_region.save(saturation_region_path)

            # Save the region of other sources
            other_region = finder.other_region
            other_region_path = filesystem.join(output_path, "other_sources.reg")
            if other_region is not None: other_region.save(other_region_path)



            # Get the segmentation maps (galaxies, stars and other sources) from the SourceFinder
            galaxy_segments = finder.galaxy_segments
            star_segments = finder.star_segments
            other_segments = finder.other_segments

            # Make sure target galaxy isn't identified as star segment
            target_segment = star_segments[ pod['centre_i'], pod['centre_j'] ]
            star_segments[ np.where( star_segments==target_segment ) ] = 0.0




            # Handle stars that have been conflated with the target galaxy
            star_segments = OverlargeStars(pod, star_segments, saturation_region_path, star_region_path, galaxy_region_path, image, source_dict, band_dict, temp_dir_path)

            # Region files can be adjusted by the user; if this is done, they have to be reloaded
            star_region = Region.from_file(star_region_path.replace('.reg','_revised.reg'))
            saturation_region = Region.from_file(saturation_region_path.replace('.reg','_revised.reg'))
            """
            # Remove all but target galaxy from galaxy region file
            ExcessGalaxies(galaxy_region_path, galaxy_principal)
            galaxy_region = Region.from_file(galaxy_region_path.replace('.reg','_revised.reg'))
            """
            # Remopve all galaxies from galaxy region file
            shutil.copy2(galaxy_region_path, galaxy_region_path.replace('.reg','_revised.reg'))
            gal_header = '# Region file format: DS9 version 4.1\n'
            gal_file_new = open( galaxy_region_path.replace('.reg','_revised.reg'), 'w')
            gal_file_new.write(gal_header)
            gal_file_new.close()
            galaxy_region = Region.from_file(galaxy_region_path.replace('.reg','_revised.reg'))



            # Create a map of the the segmentation maps
            segments = Image("segments")

            # Add the segmentation map of the galaxies
            segments.add_frame(galaxy_segments, "galaxies")

            # Add the segmentation map of the saturated stars
            if star_segments is not None: segments.add_frame(star_segments, "stars")

            # Add the segmentation map of the other sources
            if other_segments is not None: segments.add_frame(other_segments, "other_sources")

            # Save the FITS file with the segmentation maps
            path = filesystem.join(output_path, "segments.fits")
            segments.save(path)



            # Create an SourceExtractor instance
            if pod['verbose']: print '['+pod['id']+'] AstroMagic extracting background sources.'
            extractor = SourceExtractor()

            # Run the source extractor
            extractor.run(image.frames.primary, galaxy_region, star_region, saturation_region, other_region, galaxy_segments, star_segments, other_segments)



            # Determine the path to the result
            result_path = filesystem.join( temp_dir_path, 'AstroMagic', band_dict['band_name'], source_dict['name']+'_'+band_dict['band_name']+'_StarSub.fits' )

            # Save the resulting image as a FITS file
            image.frames.primary.save(result_path, header=image.original_header)



            # Grab AstroMagic output and return in pod
            am_output = astropy.io.fits.getdata( result_path )
            pod['cutout'] = am_output
            pod['pre_reg'] = True
            am_fail = False
            return pod



        # Handle exceptions
        except:
            am_crash += 1
            if am_crash>=10:
                if kwargs_dict['debug']:
                    print '['+pod['id']+'] AstroMagic failed for '+pod['id']+'! Suggest debugging.'
                    raise ValueError('AstroMagic failed for '+pod['id']+'! Suggest debugging.')
                else:
                    print '['+pod['id']+'] AstroMagic failed for '+pod['id']+'! Suggest debugging.'
                    return pod
Example #10
0
if config.backup:

    # Inform the user
    log.info("Making a backup of the original image ...")

    # Determine new filepath and copy
    new_filepath = fs.appended_filepath(config.filepath, "_backup")
    fs.copy_file(config.filepath, new_filepath)

# -----------------------------------------------------------------

# Inform the user
log.info("Loading the image '" + config.filepath + "' ...")

# Load the image
image = Image.from_file(config.filepath)

# -----------------------------------------------------------------

# Debugging
sum_before = np.nansum(image.primary.data)
log.debug("Sum of the primary frame before multiplication is " +
          str(sum_before))

# -----------------------------------------------------------------

# Inform th euser
log.info("Multiplying the image with a factor of " + str(config.factor) +
         " ...")

# Multiply