# Get the primary image frame frame = importer.image.frames.primary # Get the original header header = importer.image.original_header # ----------------------------------------------------------------- # Inform the user log.info("Loading the region ...") # Load in the region region_path = fs.join(input_path, arguments.region) region = Region.from_file(region_path, only=arguments.shapes, color=arguments.color, ignore_color=arguments.ignore_color) # Inform the user log.info("Creating a mask from the region ...") # Create a mask from the region mask = region.to_mask(frame.xsize, frame.ysize) # Inform the user log.info("Interpolating the frame within the masked pixels ...") # Create a mask of the pixels that are NaNs nans = Mask.is_nan(frame) # Set the NaN pixels to zero in the frame
importer.run(image_path) # Get the primary image frame frame = importer.image.frames.primary # Get the original header header = importer.image.original_header # ----------------------------------------------------------------- # Inform the user log.info("Loading the region ...") # Load in the region region_path = fs.join(input_path, arguments.region) region = Region.from_file(region_path, only=arguments.shapes, color=arguments.color, ignore_color=arguments.ignore_color) # Inform the user log.info("Creating a mask from the region ...") # Create a mask from the region mask = region.to_mask(frame.xsize, frame.ysize) # Inform the user log.info("Interpolating the frame within the masked pixels ...") # Create a mask of the pixels that are NaNs nans = Mask.is_nan(frame) # Set the NaN pixels to zero in the frame frame[nans] = 0.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
# Determine the full path to the bad region file bad_region_path = fs.join(input_path, arguments.bad) if arguments.bad is not None else None # Import the image importer = ImageImporter() importer.run(image_path, bad_region_path=bad_region_path) # Get the image image = importer.image # ----------------------------------------------------------------- # Load the galaxy region galaxy_region_path = fs.join(input_path, "galaxies.reg") galaxy_region = Region.from_file(galaxy_region_path) if fs.is_file(galaxy_region_path) else None # 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")
help="use the original data for pixels that are not masked") parser.add_argument('--invert', action="store_true", help="invert the mask so that mask covers outside regions") # Parse the command line arguments arguments = parser.parse_args() # ----------------------------------------------------------------- # Load the image frame = Frame.from_file(arguments.image) # Load the region region_name = os.path.splitext(os.path.basename(arguments.region))[0] region = Region.from_file(arguments.region) # Create the mask mask = Mask(region.get_mask(shape=frame.shape)) # Calculate the inverse, if requested if arguments.invert: mask = mask.inverse() # ----------------------------------------------------------------- if arguments.data: new_frame = frame frame[mask] = arguments.value # Create a frame for the total mask
# Determine the path to the input image image_path = fs.absolute(arguments.image) # Load the image image = Image.from_file(image_path) # ----------------------------------------------------------------- # Inform the user log.info("Loading regions ...") # Determine the path to the galaxy region galaxy_region_path = fs.join(arguments.input, "galaxies.reg") # Load the galaxy region galaxy_region = Region.from_file(galaxy_region_path) # Determine the path to the star region star_region_path = fs.join(arguments.input, "stars.reg") # Load the star region star_region = Region.from_file(star_region_path) if fs.is_file( star_region_path) else None # Determine the path to the saturation region saturation_region_path = fs.join(arguments.input, "saturation.reg") # Load the saturation region saturation_region = Region.from_file(saturation_region_path) if fs.is_file( saturation_region_path) else None
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
# Run the catalog importer catalog_importer.run(image.frames.primary) # ----------------------------------------------------------------- # If a special region is defined if arguments.special is not None: # Determine the full path to the special region file path = fs.join(input_path, arguments.special) # Inform the user log.info("Creating mask covering objects that require special attention from " + path + " ...") # Load the region and create a mask from it special_region = Region.from_file(path) # No special region else: special_region = None # ----------------------------------------------------------------- # If an ignore region is defined if arguments.ignore is not None: # Determine the full path to the ignore region file path = fs.join(input_path, arguments.ignore) # Inform the user log.info("Creating mask covering objects that should be ignored from " + path + " ...")
parser.add_argument("image", type=str, help="the name of the image file") parser.add_argument("value", type=float, nargs='?', help="the fill value", default='nan') parser.add_argument("--data", action="store_true", help="use the original data for pixels that are not masked") parser.add_argument('--invert', action="store_true", help="invert the mask so that mask covers outside regions") # Parse the command line arguments arguments = parser.parse_args() # ----------------------------------------------------------------- # Load the image frame = Frame.from_file(arguments.image) # Load the region region_name = os.path.splitext(os.path.basename(arguments.region))[0] region = Region.from_file(arguments.region) # Create the mask mask = Mask(region.get_mask(shape=frame.shape)) # Calculate the inverse, if requested if arguments.invert: mask = mask.inverse() # ----------------------------------------------------------------- if arguments.data: new_frame = frame frame[mask] = arguments.value # Create a frame for the total mask