def make_sources(self): """ This function ... :return: """ # Inform the user log.info("Making the sources ...") flux_range = [self.config.flux_range.min, self.config.flux_range.max] xmean_range = [0, self.config.shape[1]] ymean_range = [0, self.config.shape[0]] # Ranges of sigma xstddev_range = [self.sources_sigma, self.sources_sigma] ystddev_range = [self.sources_sigma, self.sources_sigma] table = make_random_gaussians(self.config.nsources, flux_range, xmean_range, ymean_range, xstddev_range, ystddev_range, random_state=12345) self.source_table = table data = make_gaussian_sources(self.config.shape, table) self.sources = Frame(data) # mask self.sources[self.rotation_mask] = 0.0 if self.config.plot: plotting.plot_box(self.sources, title="sources")
def make_galaxy(self): """ This function ... :return: """ # Inform the user log.info("Adding smooth galaxy source ...") effective_radius = self.config.galaxy_effective_radius effective_galaxy_angle = self.config.galaxy_angle + self.effective_rotation_angle axial_ratio = self.config.galaxy_axial_ratio angle_deg = effective_galaxy_angle.to("deg").value # Produce guess values initial_sersic_amplitude = self.config.galaxy_central_flux initial_sersic_r_eff = effective_radius initial_sersic_n = self.config.galaxy_sersic_index initial_sersic_x_0 = self.config.galaxy_position.x initial_sersic_y_0 = self.config.galaxy_position.y initial_sersic_ellip = (axial_ratio - 1.0) / axial_ratio initial_sersic_theta = np.deg2rad(angle_deg) # Produce sersic model from guess parameters, for time trials sersic_x, sersic_y = np.meshgrid(np.arange(self.xsize), np.arange(self.ysize)) sersic_model = Sersic2D(amplitude=initial_sersic_amplitude, r_eff=initial_sersic_r_eff, n=initial_sersic_n, x_0=initial_sersic_x_0, y_0=initial_sersic_y_0, ellip=initial_sersic_ellip, theta=initial_sersic_theta) sersic_map = sersic_model(sersic_x, sersic_y) # Set the galaxy frame self.galaxy = Frame(sersic_map) # Mask self.galaxy[self.rotation_mask] = 0.0 limit_radius = self.config.galaxy_relative_asymptotic_radius * effective_radius # Create galaxy region galaxy_center = PixelCoordinate(initial_sersic_x_0, initial_sersic_y_0) galaxy_radius = PixelStretch(limit_radius, limit_radius / axial_ratio) self.galaxy_region = PixelEllipseRegion(galaxy_center, galaxy_radius, effective_galaxy_angle) # Set galaxy map zero outside certain radius self.galaxy[self.galaxy_mask.inverse()] = 0.0 # Plot if self.config.plot: plotting.plot_box(self.galaxy, title="galaxy")
def reference(self): """ This function ... :return: """ # Inform the user log.info("Estimating background with photutils ...") # Plot total mask if self.config.plot: plotting.plot_mask(self.total_mask, title="total mask") integer_aperture_radius = int(math.ceil(self.aperture_radius)) box_shape = (integer_aperture_radius, integer_aperture_radius) filter_size = (3, 3) # Estimate the background sigma_clip = SigmaClip(sigma=3., iters=10) #bkg_estimator = MedianBackground() bkg_estimator = SExtractorBackground() bkg = Background2D(self.frame.data, box_shape, filter_size=filter_size, sigma_clip=sigma_clip, bkg_estimator=bkg_estimator, mask=self.total_mask.data) # Statistics #print("median background", bkg.background_median) #print("rms background", bkg.background_rms_median) self.statistics.reference = Map() self.statistics.reference.median = bkg.background_median self.statistics.reference.rms = bkg.background_rms_median # Plot if self.config.plot: plotting.plot_box(bkg.background, title="background from photutils") # Set the sky self.reference_sky = Frame(bkg.background) # Set bkg object self.photutils_bkg = bkg # Plot if self.config.plot: plotting.plot_box(self.reference_sky, title="reference sky")
def make_gradient_sky(self): """ This function ... :return: """ # Inform the user log.info("Making gradient sky ...") y, x = np.mgrid[:self.ysize, :self.xsize] # Create gradient sky self.gradient_sky = Frame(x * y / 5000.) # Mask padded self.gradient_sky[self.rotation_mask] = 0.0 # Plot #if self.config.plot: plotting.plot_difference(self.frame, self.real_sky, title="frame with background") if self.config.plot: plotting.plot_box(self.gradient_sky, title="gradient sky")
def make_noise(self): """ This function ... :return: """ # Inform the user log.info("Making noise map ...") # Make noise data = make_noise_image(self.config.shape, type='gaussian', mean=0., stddev=self.config.noise_stddev, random_state=12345) self.noise = Frame(data) # Mask self.noise[self.rotation_mask] = 0.0 # Plot #if self.config.plot: plotting.plot_difference(self.frame, self.real_sky, title="original") if self.config.plot: plotting.plot_box(self.noise, title="noise")
# 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 # Interpolate the frame in the masked pixels if arguments.method == "biharmonic": data = interpolation.inpaint_biharmonic(frame, mask) elif arguments.method == "local_mean": data = interpolation.in_paint(frame, mask, method="localmean") else: raise ValueError( "Invalid interpolation method (should be 'biharmonic' or 'local_mean')" ) new_frame = Frame(data) # Set the original NaN pixels back to NaN new_frame[nans] = float("nan") # Inform the user log.info("Saving the result ...") # Save the result path = fs.join(output_path, arguments.image) new_frame.save(path, header=header) # Write the mask if arguments.mask: path = fs.join(output_path, "mask.fits")
# ----------------------------------------------------------------- # Save the animation if animation is not None: path = fs.join(output_path, "animation.gif") animation.save(path) # ----------------------------------------------------------------- # Inform the user log.info("Writing the result ...") # Determine the path to the result result_path = fs.join(output_path, image.name + ".fits") # Save the resulting image as a FITS file image.frames.primary.save(result_path, header=image.original_header) # ----------------------------------------------------------------- # Inform the user log.info("Writing the mask ...") # Determine the path to the mask mask_path = fs.join(output_path, "mask.fits") # Save the total mask as a FITS file Frame(extractor.mask.astype(float)).save(mask_path) # -----------------------------------------------------------------
# ----------------------------------------------------------------- # 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 else: new_frame = Frame(mask.astype(int)) # Write out the new frame new_frame.save(region_name + ".fits") # -----------------------------------------------------------------
def old(self): """ This function ... :return: """ exit() # FWHM of all the images fwhm = 11.18 * u("arcsec") fwhm_pix = (fwhm / frame.average_pixelscale).to("pix").value sigma = fwhm_pix * statistics.fwhm_to_sigma # Get the center pixel of the galaxy parameters_path = fs.join(components_path, "parameters.dat") parameters = load_parameters(parameters_path) center = parameters.center.to_pixel(frame.wcs) # Create a source around the galaxy center ellipse = PixelEllipseRegion(center, 20.0 * sigma) source = Source.from_ellipse(model_residual, ellipse, 1.5) source.estimate_background("polynomial") source.plot() position = source.center model = source.subtracted.fit_model(position, "Gaussian") rel_center = center - Extent(source.x_min, source.y_min) rel_model = fitting.shifted_model(model, -source.cutout.x_min, -source.cutout.y_min) plotting.plot_peak_model(source.cutout, rel_center.x, rel_center.y, rel_model) model_fwhm_pix = fitting.fwhm(model) model_fwhm = (model_fwhm_pix * frame.average_pixelscale).to("arcsec") print("Model FWHM: ", model_fwhm) evaluated_model = source.cutout.evaluate_model(model) all_residual = Frame(np.copy(model_residual)) all_residual[source.y_slice, source.x_slice] -= evaluated_model all_residual.saveto(fs.join(residuals_path, "all_residual.fits")) model = Gaussian2D(amplitude=0.0087509425805, x_mean=center.x, y_mean=center.y, x_stddev=sigma, y_stddev=sigma) rel_model = fitting.shifted_model(model, -source.cutout.x_min, -source.cutout.y_min) plotting.plot_peak_model(source.cutout, rel_center.x, rel_center.y, rel_model) evaluated_model = source.cutout.evaluate_model(model) all_residual2 = Frame(np.copy(model_residual)) all_residual2[source.y_slice, source.x_slice] -= evaluated_model all_residual2.saveto(fs.join(residuals_path, "all_residual2.fits"))
frame = image.frames[label] # Divide by wavelength wavelength = wavelength_grid[index] #frame /= wavelength.to("micron").value print(filter_name, wavelength) #frame *= 1./wavelength.to("micron").value frame /= wavelength.to("micron").value ## -- FILTER CONVOLUTION HERE -- print(wavelengths) data = fltr.convolve(wavelengths, fluxdensities) frame = Frame(data) # Set the unit of the frame frame.unit = "W/(m2 * arcsec2 * micron)" ## # The speed of light speed_of_light = constants.c # Determine the conversion factor conversion_factor = 1.0 # From W / (m2 * arcsec2 * micron) to W / (m2 * arcsec2 * Hz) conversion_factor *= (wavelength**2 / speed_of_light).to("micron/Hz").value # From W / (m2 * arcsec2 * Hz) to MJy / sr # conversion_factor *= (Unit("W/(m2 * arcsec2 * Hz)") / Unit("MJy/sr")).to("")