def test_solar_spectral(self): """ This function ... :return: """ fuv_filter = parse_filter("FUV") i1_filter = parse_filter("I1") # Solar properties sun = Sun() #sun_fuv = sun.luminosity_for_filter_as_unit(fuv_filter) # Get the luminosity of the Sun in the FUV band #sun_i1 = sun.luminosity_for_filter_as_unit(i1_filter) # Get the luminosity of the Sun in the IRAC I1 band #print(sun_fuv) #print(sun_i1) fuv = sun.luminosity_for_filter(fuv_filter, unit="W/Hz") i1 = sun.luminosity_for_filter(i1_filter, unit="W/Hz") fuv_wav = sun.luminosity_for_wavelength(fuv_filter.wavelength, unit="W/Hz") i1_wav = sun.luminosity_for_wavelength(i1_filter.wavelength, unit="W/Hz") #print(fuv, fuv_wav) #print(i1, i1_wav) if np.isclose(fuv.value, fuv_wav.value, rtol=1e-2): print("OK") else: print("not OK: " + str(fuv) + " vs " + str(fuv_wav)) if np.isclose(i1.value, i1_wav.value, rtol=1e-2): print("OK") else: print("not OK: " + str(i1) + " vs " + str(i1_wav))
def test_solar_spectral(self): """ This function ... :return: """ fuv_filter = parse_filter("FUV") i1_filter = parse_filter("I1") # Solar properties sun = Sun() #sun_fuv = sun.luminosity_for_filter_as_unit(fuv_filter) # Get the luminosity of the Sun in the FUV band #sun_i1 = sun.luminosity_for_filter_as_unit(i1_filter) # Get the luminosity of the Sun in the IRAC I1 band #print(sun_fuv) #print(sun_i1) fuv = sun.luminosity_for_filter(fuv_filter, unit="W/Hz") i1 = sun.luminosity_for_filter(i1_filter, unit="W/Hz") fuv_wav = sun.luminosity_for_wavelength(fuv_filter.wavelength, unit="W/Hz") i1_wav = sun.luminosity_for_wavelength(i1_filter.wavelength, unit="W/Hz") #print(fuv, fuv_wav) #print(i1, i1_wav) if np.isclose(fuv.value, fuv_wav.value, rtol=1e-2): print("OK") else: print("not OK: " + str(fuv) + " vs " + str(fuv_wav)) if np.isclose(i1.value, i1_wav.value, rtol=1e-2): print("OK") else: print("not OK: " + str(i1) + " vs " + str(i1_wav))
def create_wcs(self): """ This function ... :return: """ # Inform the user log.info("Creating the WCS ...") min_pixelscale = None # Determine the path to the headers directory headers_path = fs.join(m81_data_path, "headers") # Loop over the header files for path, name in fs.files_in_path(headers_path, extension="txt", returns=["path", "name"]): # Get the filter fltr = parse_filter(name) filter_name = str(fltr) # Set the path of the file for the filter name self.wcs_paths[filter_name] = path # Get WCS wcs = CoordinateSystem.from_header_file(path) # Adjust the pixelscale if min_pixelscale is None: min_pixelscale = wcs.pixelscale self.wcs = wcs elif min_pixelscale > wcs.pixelscale: min_pixelscale = wcs.pixelscale self.wcs = wcs
def get_frames(self): """ This function ... :return: """ # Inform theb user log.info("Getting the frames ...") # Get the frames #self.frames = self.database.get_framelist_for_filters(self.galaxy_name, filter_names) self.frames = FrameList() # Loop over the filter names for filter_name in filter_names: # Parse filter fltr = parse_filter(filter_name) # Get wcs wcs = get_coordinate_system(fltr) # Generate frame #frame = Frame.random(wcs.shape, wcs=wcs, filter=fltr) frame = Frame.ones(wcs.shape, wcs=wcs, filter=fltr) frame.unit = "Jy" # Add the frame self.frames.append(frame)
def create_coordinate_systems(self): """ This function ... :return: """ # Inform the user log.info("Creating the coordinate systems ...") # Loop over the number of frames for index in range(self.config.nframes): # Get the next filter fltr = parse_filter(filter_names[index]) # Set properties size = Extent(self.config.npixels, self.config.npixels) center_pixel = size * 0.5 pixelscale = Pixelscale(1.0, unit="arcsec") # Create the coordinate system wcs = CoordinateSystem.from_properties(size, center_pixel, self.center, pixelscale) # Add the wcs self.coordinate_systems.append(wcs, fltr)
def create_wavelength_grid(self): """ This function ... :return: """ # Inform the user log.info("Creating the wavelength grid ...") # Create the wavelength generator generator = WavelengthGridGenerator() # Set input input_dict = dict() input_dict["ngrids"] = 1 input_dict["npoints"] = self.config.nwavelengths input_dict["fixed"] = [self.i1_filter.pivot, self.fuv_filter.pivot] input_dict["add_emission_lines"] = True input_dict["lines"] = ["Halpha"] # only the H-alpha line is of importance input_dict["min_wavelength"] = self.config.wavelength_range.min input_dict["max_wavelength"] = self.config.wavelength_range.max input_dict["filters"] = [parse_filter(string) for string in fitting_filter_names] # Run the generator generator.run(**input_dict) # Set the wavelength grid self.wavelength_grid = generator.single_grid
def get_frames(self): """ This function ... :return: """ # Inform theb user log.info("Getting the frames ...") # Get the frames #self.frames = self.database.get_framelist_for_filters(self.galaxy_name, filter_names) self.frames = FrameList() # Loop over the filter names for filter_name in filter_names: # Parse filter fltr = parse_filter(filter_name) # Get wcs wcs = get_coordinate_system(fltr) # Generate frame #frame = Frame.random(wcs.shape, wcs=wcs, filter=fltr) frame = Frame.ones(wcs.shape, wcs=wcs, filter=fltr) frame.unit = "Jy" # Add the frame self.frames.append(frame)
def create_wcs(self): """ This function ... :return: """ # Inform the user log.info("Creating the WCS ...") min_pixelscale = None # Determine the path to the headers directory headers_path = fs.join(m81_data_path, "headers") # Loop over the header files for path, name in fs.files_in_path(headers_path, extension="txt", returns=["path", "name"]): # Get the filter fltr = parse_filter(name) filter_name = str(fltr) # Set the path of the file for the filter name self.wcs_paths[filter_name] = path # Get WCS wcs = CoordinateSystem.from_header_file(path) # Adjust the pixelscale if min_pixelscale is None: min_pixelscale = wcs.pixelscale self.wcs = wcs elif min_pixelscale > wcs.pixelscale: min_pixelscale = wcs.pixelscale self.wcs = wcs
def create_coordinate_systems(self): """ This function ... :return: """ # Inform the user log.info("Creating the coordinate systems ...") # Loop over the number of frames for index in range(self.config.nframes): # Get the next filter fltr = parse_filter(filter_names[index]) # Set properties size = Extent(self.config.npixels, self.config.npixels) center_pixel = size * 0.5 pixelscale = Pixelscale(1.0, unit="arcsec") # Create the coordinate system wcs = CoordinateSystem.from_properties(size, center_pixel, self.center, pixelscale) # Add the wcs self.coordinate_systems.append(wcs, fltr)
def get_wcs(self, fltr): """ This function ... :param fltr: :return: """ if types.is_string_type(fltr): fltr = parse_filter(fltr) return self.frames[fltr].wcs
def get_wcs(self, fltr): """ This function ... :param fltr: :return: """ if types.is_string_type(fltr): fltr = parse_filter(fltr) return self.frames[fltr].wcs
def get_coordinate_system(fltr): """ This function ... :param fltr: :return: """ for path, name in fs.files_in_path(headers_path, extension="txt", returns=["path", "name"]): header_fltr = parse_filter(name) if header_fltr == fltr: return CoordinateSystem.from_header_file(path) return None
def load_images(self): """ This function ... :return: """ # Inform the user log.info("Loading the images ...") # The common wcs of the images wcs = None # Loop over the images in the data directory for path, filename in fs.files_in_path(self.data_path, extension="fits", returns=["path", "name"]): # Load the frame frame = Frame.from_file(path) # Set filter previous_filter = frame.filter frame.filter = parse_filter(filename.split("_norm")[0]) if previous_filter != frame.filter: frame.save() # Check filter if str(frame.filter) not in [ str(fltr) for fltr in self.config.fitting_filters ]: continue # Determine name name = str(frame.filter) # Check wcs if wcs is None: wcs = frame.wcs elif wcs == frame.wcs: pass else: raise IOError("The coordinate system of image '" + filename + "' does not match that of other images") # Debugging log.debug("Adding frame '" + filename + "' ...") # Add to dictionary self.images[name] = frame # Set original path self.image_paths[name] = path # Set the wcs self.wcs = wcs
def get_coordinate_system(fltr): """ This function ... :param fltr: :return: """ for path, name in fs.files_in_path(headers_path, extension="txt", returns=["path", "name"]): header_fltr = parse_filter(name) if header_fltr == fltr: return CoordinateSystem.from_header_file(path) return None
def load_coordinate_systems(self): """ This function ... :return: """ # Inform the user log.info("Loading the coordinate systems ...") nfilters_stars = 0 nfilters_extra = 0 # Loop over the header files for path, name in fs.files_in_path(headers_path, extension="txt", returns=["path", "name"]): # Get the filter and wavelength fltr = parse_filter(name) wavelength = fltr.effective if fltr.effective is not None else fltr.center # SKip Planck if fltr.observatory == "Planck": continue # Wavelength greater than 25 micron if wavelength > wavelengths.ranges.ir.mir.max: if nfilters_extra == self.config.nfilters_extra: continue else: nfilters_extra += 1 # Wavelength smaller than 25 micron else: if nfilters_stars == self.config.nfilters_stars: continue else: nfilters_stars += 1 # Debugging log.debug("Loading the coordinate system for the '" + str(fltr) + "' filter ...") # Get WCS wcs = CoordinateSystem.from_header_file(path) # Add the coordinate system self.coordinate_systems.append(wcs, fltr=fltr) # Break the loop if we have enough if nfilters_stars == self.config.nfilters_stars and nfilters_extra == self.config.nfilters_extra: break
def load_coordinate_systems(self): """ This function ... :return: """ # Inform the user log.info("Loading the coordinate systems ...") nfilters_stars = 0 nfilters_extra = 0 # Loop over the header files for path, name in fs.files_in_path(headers_path, extension="txt", returns=["path", "name"]): # Get the filter and wavelength fltr = parse_filter(name) wavelength = fltr.effective if fltr.effective is not None else fltr.center # SKip Planck if fltr.observatory == "Planck": continue # Wavelength greater than 25 micron if wavelength > wavelengths.ranges.ir.mir.max: if nfilters_extra == self.config.nfilters_extra: continue else: nfilters_extra += 1 # Wavelength smaller than 25 micron else: if nfilters_stars == self.config.nfilters_stars: continue else: nfilters_stars += 1 # Debugging log.debug("Loading the coordinate system for the '" + str(fltr) + "' filter ...") # Get WCS wcs = CoordinateSystem.from_header_file(path) # Add the coordinate system self.coordinate_systems.append(wcs, fltr=fltr) # Break the loop if we have enough if nfilters_stars == self.config.nfilters_stars and nfilters_extra == self.config.nfilters_extra: break
def get_frame(self, fltr): """ This function ... :param fltr: :return: """ if types.is_string_type(fltr): fltr = parse_filter(fltr) frame = self.frames[fltr] # Set FWHM if necessary if frame.fwhm is None: if has_variable_fwhm(frame.filter): frame.fwhm = self.fwhms[frame.filter] else: frame.fwhm = get_fwhm(fltr) # Return the frame return frame
def get_frame(self, fltr): """ This function ... :param fltr: :return: """ if types.is_string_type(fltr): fltr = parse_filter(fltr) frame = self.frames[fltr] # Set FWHM if necessary if frame.fwhm is None: if has_variable_fwhm(frame.filter): frame.fwhm = self.fwhms[frame.filter] else: frame.fwhm = get_fwhm(fltr) # Return the frame return frame
def create_wcs_not_working(self): """ This function ... :return: """ # Inform the user log.info("Creating the WCS ...") # Determine the path to the headers directory headers_path = fs.join(m81_data_path, "headers") # Loop over the files in the directory ra_range = None dec_range = None min_pixelscale = None for path, name in fs.files_in_path(headers_path, extension="txt", returns=["path", "name"]): # Get the filter fltr = parse_filter(name) # Get WCS wcs = CoordinateSystem.from_header_file(path) # Adjust RA range if ra_range is None: ra_range = wcs.ra_range else: ra_range.adjust(wcs.ra_range) # Adjust DEC range if dec_range is None: dec_range = wcs.dec_range else: dec_range.adjust(wcs.dec_range) # Adjust the pixelscale if min_pixelscale is None: min_pixelscale = wcs.pixelscale elif min_pixelscale > wcs.pixelscale: min_pixelscale = wcs.pixelscale # Create coordinate system # size, center_pixel, center_sky, pixelscale self.wcs = CoordinateSystem.from_ranges(ra_range, dec_range, min_pixelscale)
def create_wcs_not_working(self): """ This function ... :return: """ # Inform the user log.info("Creating the WCS ...") # Determine the path to the headers directory headers_path = fs.join(m81_data_path, "headers") # Loop over the files in the directory ra_range = None dec_range = None min_pixelscale = None for path, name in fs.files_in_path(headers_path, extension="txt", returns=["path", "name"]): # Get the filter fltr = parse_filter(name) # Get WCS wcs = CoordinateSystem.from_header_file(path) # Adjust RA range if ra_range is None: ra_range = wcs.ra_range else: ra_range.adjust(wcs.ra_range) # Adjust DEC range if dec_range is None: dec_range = wcs.dec_range else: dec_range.adjust(wcs.dec_range) # Adjust the pixelscale if min_pixelscale is None: min_pixelscale = wcs.pixelscale elif min_pixelscale > wcs.pixelscale: min_pixelscale = wcs.pixelscale # Create coordinate system # size, center_pixel, center_sky, pixelscale self.wcs = CoordinateSystem.from_ranges(ra_range, dec_range, min_pixelscale)
def launch_reference(self): """ This function ... :return: """ # Inform the user log.info("Launching the reference simulation ...") # Settings settings_launch = dict() settings_launch["ski"] = self.reference_ski_path settings_launch["input"] = self.simulation_input_path settings_launch["output"] = self.simulation_output_path settings_launch["create_output"] = True settings_launch["remote"] = self.host_id settings_launch["attached"] = True settings_launch["show_progress"] = True # Create the analysis options analysis = AnalysisOptions() analysis.extraction.path = self.simulation_extract_path analysis.plotting.path = self.simulation_plot_path analysis.misc.path = self.simulation_misc_path analysis.extraction.progress = True analysis.extraction.timeline = True analysis.extraction.memory = True analysis.plotting.progress = True analysis.plotting.timeline = True analysis.plotting.memory = True analysis.plotting.seds = True analysis.plotting.grids = True analysis.plotting.reference_seds = fs.files_in_path(seds_path) analysis.misc.fluxes = True analysis.misc.images = True analysis.misc.observation_filters = fitting_filter_names analysis.misc.observation_instruments = [instrument_name] analysis.misc.make_images_remote = self.host_id analysis.misc.images_wcs = self.reference_wcs_path analysis.misc.images_unit = "Jy/pix" analysis.misc.spectral_convolution = self.config.spectral_convolution # Set flux error bars dustpedia_sed = ObservedSED.from_file(dustpedia_sed_path) filter_names = dustpedia_sed.filter_names() errors = dustpedia_sed.errors() #print([str(error) for error in errors]) flux_errors = sequences.zip_into_dict(filter_names, [str(error) for error in errors]) analysis.misc.flux_errors = flux_errors # Create Aniano kernels object aniano = AnianoKernels() # Set the paths to the kernel for each image kernel_paths = dict() for filter_name in fitting_filter_names: kernel_paths[filter_name] = aniano.get_psf_path(parse_filter(filter_name)) analysis.misc.images_kernels = kernel_paths # Set the paths to the WCS files for each image analysis.misc.rebin_wcs = {instrument_name: self.wcs_paths} # Input input_launch = dict() #input_launch["memory"] = MemoryRequirement(serial_memory, parallel_memory) input_launch["analysis_options"] = analysis # Launch command launch = Command("launch_simulation", "launch the reference simulation", settings_launch, input_launch, cwd=".") self.launcher = self.run_command(launch)
# Load the modeling environment environment = GalaxyModelingEnvironment(modeling_path) # ----------------------------------------------------------------- # Create plotter plotter = StandardImageGridPlotter() # Loop over the directories in the truncation path for path, name in fs.directories_in_path(environment.truncation_path, returns=["path", "name"]): # Determine the path to the lowres directory lowres_path = fs.join(path, "lowres") # Determine the filter fltr = parse_filter(name) filter_name = str(fltr) # Initializ variable the_image_path = None # Find the image corresponding to the specified factor for image_path, image_name in fs.files_in_path(lowres_path, extension="fits", returns=["path", "name"]): # Determine the factor factor = real(image_name) # If the factor corresponds to the specified factor, take this image if np.isclose(factor, config.factor, rtol=0.01): the_image_path = image_path break
environment = GalaxyModelingEnvironment(modeling_path) # ----------------------------------------------------------------- # Create plotter plotter = StandardImageGridPlotter() # Loop over the directories in the truncation path for path, name in fs.directories_in_path(environment.truncation_path, returns=["path", "name"]): # Determine the path to the lowres directory lowres_path = fs.join(path, "lowres") # Determine the filter fltr = parse_filter(name) filter_name = str(fltr) # Initializ variable the_image_path = None # Find the image corresponding to the specified factor for image_path, image_name in fs.files_in_path(lowres_path, extension="fits", returns=["path", "name"]): # Determine the factor factor = real(image_name) # If the factor corresponds to the specified factor, take this image if np.isclose(factor, config.factor, rtol=0.01):
# Get the galaxy center center = environment.galaxy_center ra = center.ra.to("deg").value dec = center.dec.to("deg").value # ------------------------------------------------------------------------------ light_theme = "light" dark_theme = "dark" themes = [light_theme, dark_theme] # ------------------------------------------------------------------------------ default_filter_names = ["FUV", "SDSS r", "I1", "MIPS 24mu", "Pacs red"] default_filters = [parse_filter(name) for name in default_filter_names] #other_filter_names = ["NUV", "SDSS i", "I1", "Pacs blue", "SPIRE 250"] #other_filters = [parse_filter(name) for name in other_filter_names] # ------------------------------------------------------------------------------ default_cmap = "inferno" default_residual_cmap = 'RdBu' # ------------------------------------------------------------------------------ # Create definition definition = ConfigurationDefinition(write_config=False) # The fitting run for which to explore the parameter space
# ----------------------------------------------------------------- attenuation = GalacticAttenuation(properties.center) # ----------------------------------------------------------------- print("") # Loop over the names for prep_name in environment.preparation_names: # Load the statistics statistics = load_statistics(modeling_path, prep_name) # Determine filter fltr = parse_filter(prep_name) # Get the extinction att = attenuation.extinction_for_filter(fltr) if att == 0.0: if statistics.attenuation != 0.0: log.warning( prep_name + ": attenuation is zero but preparation attenuation value was " + str(statistics.attenuation)) continue # Ratio ratio = statistics.attenuation / att rel = abs((statistics.attenuation - att) / att)
def make_images(self): """ This function ... :return: """ # Inform the user log.info("Creating the observed mock images ...") # Create Aniano kernels object aniano = AnianoKernels() # Set the paths to the kernel for each image kernel_paths = dict() for filter_name in fitting_filter_names: kernel_paths[filter_name] = aniano.get_psf_path( parse_filter(filter_name)) # Settings settings_images = dict() settings_images["spectral_convolution"] = False # No output path is specified, so images won't be written out # Input input_images = dict() input_images["simulation_output_path"] = self.simulation_output_path input_images["output_path"] = "." input_images["filter_names"] = fitting_filter_names input_images["instrument_names"] = ["earth"] # input_images["wcs_path"] = input_images["wcs"] = self.wcs input_images["kernel_paths"] = kernel_paths input_images["unit"] = "Jy/pix" # input_images["host_id"] = "nancy" # Construct the command #create_images = Command("observed_images", "create the mock images", settings_images, input_images, cwd=".", # finish=make_data) # Add the command #commands.append(create_images) make = Command("observed_images", "create the mock images", settings_images, input_images, cwd=".") self.image_maker = self.run_command(make) # MAKE DATA: # Create directory for the images ref_path = fs.create_directory_in(self.path, "ref") images_path = fs.create_directory_in(ref_path, "images") # Determine the name of the datacube datacube_names = self.image_maker.images.keys() if len(datacube_names) > 1: raise RuntimeError("Unexpected number of datacubes") datacube_name = datacube_names[0] # Loop over the images for filter_name in self.image_maker.images[datacube_name]: # Get the image image = self.image_maker.images[datacube_name][filter_name] # Save the image image_path = fs.join(images_path, filter_name + ".fits") image.saveto(image_path)
def launch_reference(self): """ This function ... :return: """ # Inform the user log.info("Launching the reference simulation ...") # Settings settings_launch = dict() settings_launch["ski"] = self.reference_ski_path settings_launch["input"] = self.simulation_input_path settings_launch["output"] = self.simulation_output_path settings_launch["create_output"] = True settings_launch["remote"] = self.host_id settings_launch["attached"] = True settings_launch["show_progress"] = True # Create the analysis options analysis = AnalysisOptions() analysis.extraction.path = self.simulation_extract_path analysis.plotting.path = self.simulation_plot_path analysis.misc.path = self.simulation_misc_path analysis.extraction.progress = True analysis.extraction.timeline = True analysis.extraction.memory = True analysis.plotting.progress = True analysis.plotting.timeline = True analysis.plotting.memory = True analysis.plotting.seds = True analysis.plotting.grids = True analysis.plotting.reference_seds = fs.files_in_path(seds_path) analysis.misc.fluxes = True analysis.misc.images = True analysis.misc.observation_filters = fitting_filter_names analysis.misc.observation_instruments = [instrument_name] analysis.misc.make_images_remote = self.host_id analysis.misc.images_wcs = self.reference_wcs_path analysis.misc.images_unit = "Jy/pix" analysis.misc.spectral_convolution = self.config.spectral_convolution # Set flux error bars dustpedia_sed = ObservedSED.from_file(dustpedia_sed_path) filter_names = dustpedia_sed.filter_names() errors = dustpedia_sed.errors() #print([str(error) for error in errors]) flux_errors = sequences.zip_into_dict(filter_names, [str(error) for error in errors]) analysis.misc.flux_errors = flux_errors # Create Aniano kernels object aniano = AnianoKernels() # Set the paths to the kernel for each image kernel_paths = dict() for filter_name in fitting_filter_names: kernel_paths[filter_name] = aniano.get_psf_path(parse_filter(filter_name)) analysis.misc.images_kernels = kernel_paths # Set the paths to the WCS files for each image analysis.misc.rebin_wcs = {instrument_name: self.wcs_paths} # Input input_launch = dict() #input_launch["memory"] = MemoryRequirement(serial_memory, parallel_memory) input_launch["analysis_options"] = analysis # Launch command launch = Command("launch_simulation", "launch the reference simulation", settings_launch, input_launch, cwd=".") self.launcher = self.run_command(launch)
# ----------------------------------------------------------------- progress = defaultdict(dict) # ----------------------------------------------------------------- # Loop over all images of the initial dataset #for path in fs.files_in_path(prep_path, recursive=True, exact_name="initialized"): for directory_path in fs.directories_in_path(prep_path): # Directory path #directory_path = fs.directory_of(path) image_name = fs.name(directory_path) # Determine filter fltr = parse_filter(image_name) # Sort label, filepath = sort_image(image_name, directory_path, read_only=True) category = fltr.instrument if fltr.instrument is not None else "other" # Add to progress data progress[category][fltr.band] = label # ----------------------------------------------------------------- print("") # Show for instrument in progress: