def __init__(self, emin, emax, nchan, apec_root=None, apec_vers="2.0.2", thermal_broad=False): if apec_root is None: self.cocofile = check_file_location( "apec_v%s_coco.fits" % apec_vers, "spectral_files") self.linefile = check_file_location( "apec_v%s_line.fits" % apec_vers, "spectral_files") else: self.cocofile = os.path.join(apec_root, "apec_v%s_coco.fits" % apec_vers) self.linefile = os.path.join(apec_root, "apec_v%s_line.fits" % apec_vers) if not os.path.exists(self.cocofile) or not os.path.exists( self.linefile): raise IOError("Cannot find the APEC files!\n %s\n, %s" % (self.cocofile, self.linefile)) super(TableApecModel, self).__init__(emin, emax, nchan) self.wvbins = hc / self.ebins[::-1].d # H, He, and trace elements self.cosmic_elem = [ 1, 2, 3, 4, 5, 9, 11, 15, 17, 19, 21, 22, 23, 24, 25, 27, 29, 30 ] # Non-trace metals self.metal_elem = [6, 7, 8, 10, 12, 13, 14, 16, 18, 20, 26, 28] self.thermal_broad = thermal_broad self.A = np.array([ 0.0, 1.00794, 4.00262, 6.941, 9.012182, 10.811, 12.0107, 14.0067, 15.9994, 18.9984, 20.1797, 22.9898, 24.3050, 26.9815, 28.0855, 30.9738, 32.0650, 35.4530, 39.9480, 39.0983, 40.0780, 44.9559, 47.8670, 50.9415, 51.9961, 54.9380, 55.8450, 58.9332, 58.6934, 63.5460, 65.3800 ]) try: self.line_handle = _astropy.pyfits.open(self.linefile) except IOError: mylog.error("LINE file %s does not exist" % self.linefile) raise IOError("LINE file %s does not exist" % self.linefile) try: self.coco_handle = _astropy.pyfits.open(self.cocofile) except IOError: mylog.error("COCO file %s does not exist" % self.cocofile) raise IOError("COCO file %s does not exist" % self.cocofile) self.Tvals = self.line_handle[1].data.field("kT") self.nT = len(self.Tvals) self.dTvals = np.diff(self.Tvals) self.minlam = self.wvbins.min() self.maxlam = self.wvbins.max()
def from_data_source(cls, data_source, redshift, area, exp_time, source_model, point_sources=False, parameters=None, center=None, dist=None, cosmology=None, velocity_fields=None): r""" Initialize a :class:`~pyxsim.photon_list.PhotonList` from a yt data source. The redshift, collecting area, exposure time, and cosmology are stored in the *parameters* dictionary which is passed to the *source_model* function. Parameters ---------- data_source : :class:`~yt.data_objects.data_containers.YTSelectionContainer` The data source from which the photons will be generated. redshift : float The cosmological redshift for the photons. area : float, (value, unit) tuple, :class:`~yt.units.yt_array.YTQuantity`, or :class:`~astropy.units.Quantity` The collecting area to determine the number of photons. If units are not specified, it is assumed to be in cm^2. exp_time : float, (value, unit) tuple, :class:`~yt.units.yt_array.YTQuantity`, or :class:`~astropy.units.Quantity` The exposure time to determine the number of photons. If units are not specified, it is assumed to be in seconds. source_model : :class:`~pyxsim.source_models.SourceModel` A source model used to generate the photons. point_sources : boolean, optional If True, the photons will be assumed to be generated from the exact positions of the cells or particles and not smeared around within a volume. Default: False parameters : dict, optional A dictionary of parameters to be passed for the source model to use, if necessary. center : string or array_like, optional The origin of the photon spatial coordinates. Accepts "c", "max", or a coordinate. If not specified, pyxsim attempts to use the "center" field parameter of the data_source. dist : float, (value, unit) tuple, :class:`~yt.units.yt_array.YTQuantity`, or :class:`~astropy.units.Quantity` The angular diameter distance, used for nearby sources. This may be optionally supplied instead of it being determined from the *redshift* and given *cosmology*. If units are not specified, it is assumed to be in kpc. To use this, the redshift must be set to zero. cosmology : :class:`~yt.utilities.cosmology.Cosmology`, optional Cosmological information. If not supplied, we try to get the cosmology from the dataset. Otherwise, LCDM with the default yt parameters is assumed. velocity_fields : list of fields The yt fields to use for the velocity. If not specified, the following will be assumed: ['velocity_x', 'velocity_y', 'velocity_z'] for grid datasets ['particle_velocity_x', 'particle_velocity_y', 'particle_velocity_z'] for particle datasets Examples -------- >>> thermal_model = ThermalSourceModel(apec_model, Zmet=0.3) >>> redshift = 0.05 >>> area = 6000.0 # assumed here in cm**2 >>> time = 2.0e5 # assumed here in seconds >>> sp = ds.sphere("c", (500., "kpc")) >>> my_photons = PhotonList.from_data_source(sp, redshift, area, ... time, thermal_model) """ ds = data_source.ds if parameters is None: parameters = {} if cosmology is None: if hasattr(ds, 'cosmology'): cosmo = ds.cosmology else: cosmo = Cosmology() else: cosmo = cosmology if dist is None: if redshift <= 0.0: msg = "If redshift <= 0.0, you must specify a distance to the " \ "source using the 'dist' argument!" mylog.error(msg) raise ValueError(msg) D_A = cosmo.angular_diameter_distance(0.0, redshift).in_units("Mpc") else: D_A = parse_value(dist, "kpc") if redshift > 0.0: mylog.warning("Redshift must be zero for nearby sources. " "Resetting redshift to 0.0.") redshift = 0.0 if isinstance(center, string_types): if center == "center" or center == "c": parameters["center"] = ds.domain_center elif center == "max" or center == "m": parameters["center"] = ds.find_max("density")[-1] elif iterable(center): if isinstance(center, YTArray): parameters["center"] = center.in_units("code_length") elif isinstance(center, tuple): if center[0] == "min": parameters["center"] = ds.find_min(center[1])[-1] elif center[0] == "max": parameters["center"] = ds.find_max(center[1])[-1] else: raise RuntimeError else: parameters["center"] = ds.arr(center, "code_length") elif center is None: if hasattr(data_source, "left_edge"): parameters["center"] = 0.5 * (data_source.left_edge + data_source.right_edge) else: parameters["center"] = data_source.get_field_parameter( "center") parameters["fid_exp_time"] = parse_value(exp_time, "s") parameters["fid_area"] = parse_value(area, "cm**2") parameters["fid_redshift"] = redshift parameters["fid_d_a"] = D_A parameters["hubble"] = cosmo.hubble_constant parameters["omega_matter"] = cosmo.omega_matter parameters["omega_lambda"] = cosmo.omega_lambda if redshift > 0.0: mylog.info( "Cosmology: h = %g, omega_matter = %g, omega_lambda = %g" % (cosmo.hubble_constant, cosmo.omega_matter, cosmo.omega_lambda)) else: mylog.info("Observing local source at distance %s." % D_A) D_A = parameters["fid_d_a"].in_cgs() dist_fac = 1.0 / (4. * np.pi * D_A.value * D_A.value * (1. + redshift)**2) spectral_norm = parameters["fid_area"].v * parameters[ "fid_exp_time"].v * dist_fac source_model.setup_model(data_source, redshift, spectral_norm) p_fields, v_fields, w_field = determine_fields( ds, source_model.source_type, point_sources) if velocity_fields is not None: v_fields = velocity_fields if p_fields[0] == ("index", "x"): parameters["data_type"] = "cells" else: parameters["data_type"] = "particles" citer = data_source.chunks([], "io") photons = defaultdict(list) for chunk in parallel_objects(citer): chunk_data = source_model(chunk) if chunk_data is not None: ncells, number_of_photons, idxs, energies = chunk_data photons["num_photons"].append(number_of_photons) photons["energy"].append(energies) photons["pos"].append( np.array([ chunk[p_fields[0]].d[idxs], chunk[p_fields[1]].d[idxs], chunk[p_fields[2]].d[idxs] ])) photons["vel"].append( np.array([ chunk[v_fields[0]].d[idxs], chunk[v_fields[1]].d[idxs], chunk[v_fields[2]].d[idxs] ])) if w_field is None: photons["dx"].append(np.zeros(ncells)) else: photons["dx"].append(chunk[w_field].d[idxs]) source_model.cleanup_model() photon_units = { "pos": ds.field_info[p_fields[0]].units, "vel": ds.field_info[v_fields[0]].units, "energy": "keV" } if w_field is None: photon_units["dx"] = "kpc" else: photon_units["dx"] = ds.field_info[w_field].units concatenate_photons(ds, photons, photon_units) c = parameters["center"].to("kpc") if sum(ds.periodicity) > 0: # Fix photon coordinates for regions crossing a periodic boundary dw = ds.domain_width.to("kpc") le, re = find_object_bounds(data_source) for i in range(3): if ds.periodicity[i] and photons["pos"].shape[0] > 0: tfl = photons["pos"][:, i] < le[i] tfr = photons["pos"][:, i] > re[i] photons["pos"][tfl, i] += dw[i] photons["pos"][tfr, i] -= dw[i] # Re-center all coordinates if photons["pos"].shape[0] > 0: photons["pos"] -= c mylog.info("Finished generating photons.") mylog.info("Number of photons generated: %d" % int(np.sum(photons["num_photons"]))) mylog.info("Number of cells with photons: %d" % photons["dx"].size) return cls(photons, parameters, cosmo)
def write_simput_file(self, prefix, clobber=False, emin=None, emax=None): r""" Write events to a SIMPUT file that may be read by the SIMX instrument simulator. Parameters ---------- prefix : string The filename prefix. clobber : boolean, optional Set to True to overwrite previous files. e_min : float, optional The minimum energy of the photons to save in keV. e_max : float, optional The maximum energy of the photons to save in keV. """ pyfits = _astropy.pyfits if isinstance(self.parameters["Area"], string_types): mylog.error("Writing SIMPUT files is only supported if you didn't convolve with responses.") raise TypeError("Writing SIMPUT files is only supported if you didn't convolve with responses.") if emin is None: emin = self["eobs"].min().value if emax is None: emax = self["eobs"].max().value idxs = np.logical_and(self["eobs"].d >= emin, self["eobs"].d <= emax) flux = np.sum(self["eobs"][idxs].in_units("erg")) / \ self.parameters["ExposureTime"]/self.parameters["Area"] col1 = pyfits.Column(name='ENERGY', format='E', array=self["eobs"][idxs].d) col2 = pyfits.Column(name='RA', format='D', array=self["xsky"][idxs].d) col3 = pyfits.Column(name='DEC', format='D', array=self["ysky"][idxs].d) coldefs = pyfits.ColDefs([col1, col2, col3]) tbhdu = pyfits.BinTableHDU.from_columns(coldefs) tbhdu.name = "PHLIST" tbhdu.header["HDUCLASS"] = "HEASARC/SIMPUT" tbhdu.header["HDUCLAS1"] = "PHOTONS" tbhdu.header["HDUVERS"] = "1.1.0" tbhdu.header["EXTVER"] = 1 tbhdu.header["REFRA"] = 0.0 tbhdu.header["REFDEC"] = 0.0 tbhdu.header["TUNIT1"] = "keV" tbhdu.header["TUNIT2"] = "deg" tbhdu.header["TUNIT3"] = "deg" phfile = prefix+"_phlist.fits" tbhdu.writeto(phfile, clobber=clobber) col1 = pyfits.Column(name='SRC_ID', format='J', array=np.array([1]).astype("int32")) col2 = pyfits.Column(name='RA', format='D', array=np.array([0.0])) col3 = pyfits.Column(name='DEC', format='D', array=np.array([0.0])) col4 = pyfits.Column(name='E_MIN', format='D', array=np.array([float(emin)])) col5 = pyfits.Column(name='E_MAX', format='D', array=np.array([float(emax)])) col6 = pyfits.Column(name='FLUX', format='D', array=np.array([flux.value])) col7 = pyfits.Column(name='SPECTRUM', format='80A', array=np.array([phfile+"[PHLIST,1]"])) col8 = pyfits.Column(name='IMAGE', format='80A', array=np.array([phfile+"[PHLIST,1]"])) col9 = pyfits.Column(name='SRC_NAME', format='80A', array=np.array(["yt_src"])) coldefs = pyfits.ColDefs([col1, col2, col3, col4, col5, col6, col7, col8, col9]) wrhdu = pyfits.BinTableHDU.from_columns(coldefs) wrhdu.name = "SRC_CAT" wrhdu.header["HDUCLASS"] = "HEASARC" wrhdu.header["HDUCLAS1"] = "SIMPUT" wrhdu.header["HDUCLAS2"] = "SRC_CAT" wrhdu.header["HDUVERS"] = "1.1.0" wrhdu.header["RADECSYS"] = "FK5" wrhdu.header["EQUINOX"] = 2000.0 wrhdu.header["TUNIT2"] = "deg" wrhdu.header["TUNIT3"] = "deg" wrhdu.header["TUNIT4"] = "keV" wrhdu.header["TUNIT5"] = "keV" wrhdu.header["TUNIT6"] = "erg/s/cm**2" simputfile = prefix+"_simput.fits" wrhdu.writeto(simputfile, clobber=clobber)
def from_data_source(cls, data_source, redshift, area, exp_time, source_model, point_sources=False, parameters=None, center=None, dist=None, cosmology=None, velocity_fields=None): r""" Initialize a :class:`~pyxsim.photon_list.PhotonList` from a yt data source. The redshift, collecting area, exposure time, and cosmology are stored in the *parameters* dictionary which is passed to the *source_model* function. Parameters ---------- data_source : :class:`~yt.data_objects.data_containers.YTSelectionContainer` The data source from which the photons will be generated. redshift : float The cosmological redshift for the photons. area : float, (value, unit) tuple, :class:`~yt.units.yt_array.YTQuantity`, or :class:`~astropy.units.Quantity` The collecting area to determine the number of photons. If units are not specified, it is assumed to be in cm^2. exp_time : float, (value, unit) tuple, :class:`~yt.units.yt_array.YTQuantity`, or :class:`~astropy.units.Quantity` The exposure time to determine the number of photons. If units are not specified, it is assumed to be in seconds. source_model : :class:`~pyxsim.source_models.SourceModel` A source model used to generate the photons. point_sources : boolean, optional If True, the photons will be assumed to be generated from the exact positions of the cells or particles and not smeared around within a volume. Default: False parameters : dict, optional A dictionary of parameters to be passed for the source model to use, if necessary. center : string or array_like, optional The origin of the photon spatial coordinates. Accepts "c", "max", or a coordinate. If not specified, pyxsim attempts to use the "center" field parameter of the data_source. dist : float, (value, unit) tuple, :class:`~yt.units.yt_array.YTQuantity`, or :class:`~astropy.units.Quantity` The angular diameter distance, used for nearby sources. This may be optionally supplied instead of it being determined from the *redshift* and given *cosmology*. If units are not specified, it is assumed to be in kpc. To use this, the redshift must be set to zero. cosmology : :class:`~yt.utilities.cosmology.Cosmology`, optional Cosmological information. If not supplied, we try to get the cosmology from the dataset. Otherwise, LCDM with the default yt parameters is assumed. velocity_fields : list of fields The yt fields to use for the velocity. If not specified, the following will be assumed: ['velocity_x', 'velocity_y', 'velocity_z'] for grid datasets ['particle_velocity_x', 'particle_velocity_y', 'particle_velocity_z'] for particle datasets Examples -------- >>> thermal_model = ThermalSourceModel(apec_model, Zmet=0.3) >>> redshift = 0.05 >>> area = 6000.0 # assumed here in cm**2 >>> time = 2.0e5 # assumed here in seconds >>> sp = ds.sphere("c", (500., "kpc")) >>> my_photons = PhotonList.from_data_source(sp, redshift, area, ... time, thermal_model) """ ds = data_source.ds if parameters is None: parameters = {} if cosmology is None: if hasattr(ds, 'cosmology'): cosmo = ds.cosmology else: cosmo = Cosmology() else: cosmo = cosmology if dist is None: if redshift <= 0.0: msg = "If redshift <= 0.0, you must specify a distance to the " \ "source using the 'dist' argument!" mylog.error(msg) raise ValueError(msg) D_A = cosmo.angular_diameter_distance(0.0, redshift).in_units("Mpc") else: D_A = parse_value(dist, "kpc") if redshift > 0.0: mylog.warning("Redshift must be zero for nearby sources. " "Resetting redshift to 0.0.") redshift = 0.0 if isinstance(center, string_types): if center == "center" or center == "c": parameters["center"] = ds.domain_center elif center == "max" or center == "m": parameters["center"] = ds.find_max("density")[-1] elif iterable(center): if isinstance(center, YTArray): parameters["center"] = center.in_units("code_length") elif isinstance(center, tuple): if center[0] == "min": parameters["center"] = ds.find_min(center[1])[-1] elif center[0] == "max": parameters["center"] = ds.find_max(center[1])[-1] else: raise RuntimeError else: parameters["center"] = ds.arr(center, "code_length") elif center is None: if hasattr(data_source, "left_edge"): parameters["center"] = 0.5*(data_source.left_edge+data_source.right_edge) else: parameters["center"] = data_source.get_field_parameter("center") parameters["fid_exp_time"] = parse_value(exp_time, "s") parameters["fid_area"] = parse_value(area, "cm**2") parameters["fid_redshift"] = redshift parameters["fid_d_a"] = D_A parameters["hubble"] = cosmo.hubble_constant parameters["omega_matter"] = cosmo.omega_matter parameters["omega_lambda"] = cosmo.omega_lambda if redshift > 0.0: mylog.info("Cosmology: h = %g, omega_matter = %g, omega_lambda = %g" % (cosmo.hubble_constant, cosmo.omega_matter, cosmo.omega_lambda)) else: mylog.info("Observing local source at distance %s." % D_A) D_A = parameters["fid_d_a"].in_cgs() dist_fac = 1.0/(4.*np.pi*D_A.value*D_A.value*(1.+redshift)**2) spectral_norm = parameters["fid_area"].v*parameters["fid_exp_time"].v*dist_fac source_model.setup_model(data_source, redshift, spectral_norm) p_fields, v_fields, w_field = determine_fields(ds, source_model.source_type, point_sources) if velocity_fields is not None: v_fields = velocity_fields if p_fields[0] == ("index", "x"): parameters["data_type"] = "cells" else: parameters["data_type"] = "particles" citer = data_source.chunks([], "io") photons = defaultdict(list) for chunk in parallel_objects(citer): chunk_data = source_model(chunk) if chunk_data is not None: ncells, number_of_photons, idxs, energies = chunk_data photons["num_photons"].append(number_of_photons) photons["energy"].append(energies) photons["pos"].append(np.array([chunk[p_fields[0]].d[idxs], chunk[p_fields[1]].d[idxs], chunk[p_fields[2]].d[idxs]])) photons["vel"].append(np.array([chunk[v_fields[0]].d[idxs], chunk[v_fields[1]].d[idxs], chunk[v_fields[2]].d[idxs]])) if w_field is None: photons["dx"].append(np.zeros(ncells)) else: photons["dx"].append(chunk[w_field].d[idxs]) source_model.cleanup_model() photon_units = {"pos": ds.field_info[p_fields[0]].units, "vel": ds.field_info[v_fields[0]].units, "energy": "keV"} if w_field is None: photon_units["dx"] = "kpc" else: photon_units["dx"] = ds.field_info[w_field].units concatenate_photons(ds, photons, photon_units) c = parameters["center"].to("kpc") if sum(ds.periodicity) > 0: # Fix photon coordinates for regions crossing a periodic boundary dw = ds.domain_width.to("kpc") le, re = find_object_bounds(data_source) for i in range(3): if ds.periodicity[i] and photons["pos"].shape[0] > 0: tfl = photons["pos"][:,i] < le[i] tfr = photons["pos"][:,i] > re[i] photons["pos"][tfl,i] += dw[i] photons["pos"][tfr,i] -= dw[i] # Re-center all coordinates if photons["pos"].shape[0] > 0: photons["pos"] -= c mylog.info("Finished generating photons.") mylog.info("Number of photons generated: %d" % int(np.sum(photons["num_photons"]))) mylog.info("Number of cells with photons: %d" % photons["dx"].size) return cls(photons, parameters, cosmo)
def project_photons(self, normal, area_new=None, exp_time_new=None, redshift_new=None, dist_new=None, absorb_model=None, sky_center=None, no_shifting=False, north_vector=None, prng=None): r""" Projects photons onto an image plane given a line of sight. Returns a new :class:`~pyxsim.event_list.EventList`. Parameters ---------- normal : character or array-like Normal vector to the plane of projection. If "x", "y", or "z", will assume to be along that axis (and will probably be faster). Otherwise, should be an off-axis normal vector, e.g [1.0, 2.0, -3.0] area_new : float, (value, unit) tuple, or :class:`~yt.units.yt_array.YTQuantity`, optional New value for the (constant) collecting area of the detector. If units are not specified, is assumed to be in cm**2. exp_time_new : float, (value, unit) tuple, or :class:`~yt.units.yt_array.YTQuantity`, optional The new value for the exposure time. If units are not specified it is assumed to be in seconds. redshift_new : float, optional The new value for the cosmological redshift. dist_new : float, (value, unit) tuple, or :class:`~yt.units.yt_array.YTQuantity`, optional The new value for the angular diameter distance, used for nearby sources. This may be optionally supplied instead of it being determined from the cosmology. If units are not specified, it is assumed to be in Mpc. To use this, the redshift must be zero. absorb_model : :class:`~pyxsim.spectral_models.AbsorptionModel` A model for foreground galactic absorption. sky_center : array-like, optional Center RA, Dec of the events in degrees. no_shifting : boolean, optional If set, the photon energies will not be Doppler shifted. north_vector : a sequence of floats A vector defining the "up" direction. This option sets the orientation of the plane of projection. If not set, an arbitrary grid-aligned north_vector is chosen. Ignored in the case where a particular axis (e.g., "x", "y", or "z") is explicitly specified. prng : :class:`~numpy.random.RandomState` object or :mod:`~numpy.random`, optional A pseudo-random number generator. Typically will only be specified if you have a reason to generate the same set of random numbers, such as for a test. Default is the :mod:`numpy.random` module. Examples -------- >>> L = np.array([0.1,-0.2,0.3]) >>> events = my_photons.project_photons(L, area_new=10000., ... redshift_new=0.05) """ if prng is None: prng = np.random if redshift_new is not None and dist_new is not None: mylog.error("You may specify a new redshift or distance, " + "but not both!") if sky_center is None: sky_center = YTArray([30., 45.], "degree") else: sky_center = YTArray(sky_center, "degree") dx = self.photons["dx"].d if isinstance(normal, string_types): # if on-axis, just use the maximum width of the plane perpendicular # to that axis w = self.parameters["Width"].copy() w["xyz".index(normal)] = 0.0 ax_idx = np.argmax(w) else: # if off-axis, just use the largest width to make sure we get everything ax_idx = np.argmax(self.parameters["Width"]) nx = self.parameters["Dimension"][ax_idx] dx_min = (self.parameters["Width"] / self.parameters["Dimension"])[ax_idx] if not isinstance(normal, string_types): L = np.array(normal) orient = Orientation(L, north_vector=north_vector) x_hat = orient.unit_vectors[0] y_hat = orient.unit_vectors[1] z_hat = orient.unit_vectors[2] n_ph = self.photons["NumberOfPhotons"] n_ph_tot = n_ph.sum() parameters = {} zobs0 = self.parameters["FiducialRedshift"] D_A0 = self.parameters["FiducialAngularDiameterDistance"] scale_factor = 1.0 if (exp_time_new is None and area_new is None and redshift_new is None and dist_new is None): my_n_obs = n_ph_tot zobs = zobs0 D_A = D_A0 else: if exp_time_new is None: Tratio = 1. else: exp_time_new = parse_value(exp_time_new, "s") Tratio = exp_time_new / self.parameters["FiducialExposureTime"] if area_new is None: Aratio = 1. else: area_new = parse_value(area_new, "cm**2") Aratio = area_new / self.parameters["FiducialArea"] if redshift_new is None and dist_new is None: Dratio = 1. zobs = zobs0 D_A = D_A0 else: if dist_new is not None: if redshift_new is not None and redshift_new > 0.0: mylog.warning( "Redshift must be zero for nearby sources. Resetting redshift to 0.0." ) zobs = 0.0 D_A = parse_value(dist_new, "Mpc") else: zobs = redshift_new D_A = self.cosmo.angular_diameter_distance( 0.0, zobs).in_units("Mpc") scale_factor = (1. + zobs0) / (1. + zobs) Dratio = D_A0*D_A0*(1.+zobs0)**3 / \ (D_A*D_A*(1.+zobs)**3) fak = Aratio * Tratio * Dratio if fak > 1: raise ValueError( "This combination of requested parameters results in " "%g%% more photons collected than are " % (100. * (fak - 1.)) + "available in the sample. Please reduce the collecting " "area, exposure time, or increase the distance/redshift " "of the object. Alternatively, generate a larger sample " "of photons.") my_n_obs = np.int64(n_ph_tot * fak) Nn = 4294967294 if my_n_obs == n_ph_tot: if my_n_obs <= Nn: idxs = np.arange(my_n_obs, dtype='uint32') else: idxs = np.arange(my_n_obs, dtype='uint64') else: if n_ph_tot <= Nn: idxs = np.arange(n_ph_tot, dtype='uint32') prng.shuffle(idxs) idxs = idxs[:my_n_obs] else: Nc = np.int32(n_ph_tot / Nn) idxs = np.zeros(my_n_obs, dtype=np.uint64) Nup = np.uint32(my_n_obs / Nc) for i in range(Nc + 1): if (i + 1) * Nc < n_ph_tot: idtm = np.arange(i * Nc, (i + 1) * Nc, dtype='uint64') Nupt = Nup else: idtm = np.arange(i * Nc, n_ph_tot, dtype='uint64') Nupt = my_n_obs - i * Nup prng.shuffle(idtm) idxs[i * Nup, i * Nup + Nupt] = idtm[:Nupt] del (idtm) # idxs = prng.permutation(n_ph_tot)[:my_n_obs].astype("int64") obs_cells = np.searchsorted(self.p_bins, idxs, side='right') - 1 delta = dx[obs_cells] if isinstance(normal, string_types): if self.parameters["DataType"] == "cells": xsky = prng.uniform(low=-0.5, high=0.5, size=my_n_obs) ysky = prng.uniform(low=-0.5, high=0.5, size=my_n_obs) elif self.parameters["DataType"] == "particles": xsky = prng.normal(loc=0.0, scale=1.0, size=my_n_obs) ysky = prng.normal(loc=0.0, scale=1.0, size=my_n_obs) xsky *= delta ysky *= delta xsky += self.photons[axes_lookup[normal][0]].d[obs_cells] ysky += self.photons[axes_lookup[normal][1]].d[obs_cells] if not no_shifting: vz = self.photons["v%s" % normal] else: if self.parameters["DataType"] == "cells": x = prng.uniform(low=-0.5, high=0.5, size=my_n_obs) y = prng.uniform(low=-0.5, high=0.5, size=my_n_obs) z = prng.uniform(low=-0.5, high=0.5, size=my_n_obs) elif self.parameters["DataType"] == "particles": x = prng.normal(loc=0.0, scale=1.0, size=my_n_obs) y = prng.normal(loc=0.0, scale=1.0, size=my_n_obs) z = prng.normal(loc=0.0, scale=1.0, size=my_n_obs) if not no_shifting: vz = self.photons["vx"]*z_hat[0] + \ self.photons["vy"]*z_hat[1] + \ self.photons["vz"]*z_hat[2] x *= delta y *= delta z *= delta x += self.photons["x"].d[obs_cells] y += self.photons["y"].d[obs_cells] z += self.photons["z"].d[obs_cells] xsky = x * x_hat[0] + y * x_hat[1] + z * x_hat[2] ysky = x * y_hat[0] + y * y_hat[1] + z * y_hat[2] del (delta) if no_shifting: eobs = self.photons["Energy"][idxs] else: # shift = -vz.in_cgs()/clight # shift = np.sqrt((1.-shift)/(1.+shift)) # eobs = self.photons["Energy"][idxs]*shift[obs_cells] shift = -vz[obs_cells].in_cgs() / clight shift = np.sqrt((1. - shift) / (1. + shift)) eobs = self.photons["Energy"][idxs] eobs *= shift del (shift) eobs *= scale_factor if absorb_model is None: detected = np.ones(eobs.shape, dtype='bool') else: detected = absorb_model.absorb_photons(eobs, prng=prng) events = {} dtheta = YTQuantity(np.rad2deg(dx_min / D_A), "degree") events["xpix"] = xsky[detected] / dx_min.v + 0.5 * (nx + 1) events["ypix"] = ysky[detected] / dx_min.v + 0.5 * (nx + 1) events["eobs"] = eobs[detected] events = comm.par_combine_object(events, datatype="dict", op="cat") num_events = len(events["xpix"]) if comm.rank == 0: mylog.info("Total number of observed photons: %d" % num_events) if exp_time_new is None: parameters["ExposureTime"] = self.parameters[ "FiducialExposureTime"] else: parameters["ExposureTime"] = exp_time_new if area_new is None: parameters["Area"] = self.parameters["FiducialArea"] else: parameters["Area"] = area_new parameters["Redshift"] = zobs parameters["AngularDiameterDistance"] = D_A.in_units("Mpc") parameters["sky_center"] = sky_center parameters["pix_center"] = np.array([0.5 * (nx + 1)] * 2) parameters["dtheta"] = dtheta return EventList(events, parameters)
def from_data_source(cls, data_source, redshift, area, exp_time, source_model, parameters=None, center=None, dist=None, cosmology=None, velocity_fields=None): r""" Initialize a :class:`~pyxsim.photon_list.PhotonList` from a yt data source. The redshift, collecting area, exposure time, and cosmology are stored in the *parameters* dictionary which is passed to the *source_model* function. Parameters ---------- data_source : :class:`~yt.data_objects.data_containers.YTSelectionContainer` The data source from which the photons will be generated. redshift : float The cosmological redshift for the photons. area : float, (value, unit) tuple, or :class:`~yt.units.yt_array.YTQuantity`. The collecting area to determine the number of photons. If units are not specified, it is assumed to be in cm^2. exp_time : float, (value, unit) tuple, or :class:`~yt.units.yt_array.YTQuantity`. The exposure time to determine the number of photons. If units are not specified, it is assumed to be in seconds. source_model : :class:`~pyxsim.source_models.SourceModel` A source model used to generate the photons. parameters : dict, optional A dictionary of parameters to be passed for the source model to use, if necessary. center : string or array_like, optional The origin of the photon spatial coordinates. Accepts "c", "max", or a coordinate. If not specified, pyxsim attempts to use the "center" field parameter of the data_source. dist : float, (value, unit) tuple, or :class:`~yt.units.yt_array.YTQuantity`, optional The angular diameter distance, used for nearby sources. This may be optionally supplied instead of it being determined from the *redshift* and given *cosmology*. If units are not specified, it is assumed to be in Mpc. To use this, the redshift must be set to zero. cosmology : :class:`~yt.utilities.cosmology.Cosmology`, optional Cosmological information. If not supplied, we try to get the cosmology from the dataset. Otherwise, LCDM with the default yt parameters is assumed. velocity_fields : list of fields The yt fields to use for the velocity. If not specified, the following will be assumed: ['velocity_x', 'velocity_y', 'velocity_z'] for grid datasets ['particle_velocity_x', 'particle_velocity_y', 'particle_velocity_z'] for particle datasets Examples -------- >>> thermal_model = ThermalSourceModel(apec_model, Zmet=0.3) >>> redshift = 0.05 >>> area = 6000.0 # assumed here in cm**2 >>> time = 2.0e5 # assumed here in seconds >>> sp = ds.sphere("c", (500., "kpc")) >>> my_photons = PhotonList.from_data_source(sp, redshift, area, ... time, thermal_model) """ ds = data_source.ds if parameters is None: parameters = {} if cosmology is None: if hasattr(ds, 'cosmology'): cosmo = ds.cosmology else: cosmo = Cosmology() else: cosmo = cosmology mylog.info( "Cosmology: h = %g, omega_matter = %g, omega_lambda = %g" % (cosmo.hubble_constant, cosmo.omega_matter, cosmo.omega_lambda)) if dist is None: if redshift <= 0.0: msg = "If redshift <= 0.0, you must specify a distance to the source using the 'dist' argument!" mylog.error(msg) raise ValueError(msg) D_A = cosmo.angular_diameter_distance(0.0, redshift).in_units("Mpc") else: D_A = parse_value(dist, "Mpc") if redshift > 0.0: mylog.warning( "Redshift must be zero for nearby sources. Resetting redshift to 0.0." ) redshift = 0.0 if center == "center" or center == "c": parameters["center"] = ds.domain_center elif center == "max" or center == "m": parameters["center"] = ds.find_max("density")[-1] elif iterable(center): if isinstance(center, YTArray): parameters["center"] = center.in_units("code_length") elif isinstance(center, tuple): if center[0] == "min": parameters["center"] = ds.find_min(center[1])[-1] elif center[0] == "max": parameters["center"] = ds.find_max(center[1])[-1] else: raise RuntimeError else: parameters["center"] = ds.arr(center, "code_length") elif center is None: parameters["center"] = data_source.get_field_parameter("center") parameters["FiducialExposureTime"] = parse_value(exp_time, "s") parameters["FiducialArea"] = parse_value(area, "cm**2") parameters["FiducialRedshift"] = redshift parameters["FiducialAngularDiameterDistance"] = D_A parameters["HubbleConstant"] = cosmo.hubble_constant parameters["OmegaMatter"] = cosmo.omega_matter parameters["OmegaLambda"] = cosmo.omega_lambda D_A = parameters["FiducialAngularDiameterDistance"].in_cgs() dist_fac = 1.0 / (4. * np.pi * D_A.value * D_A.value * (1. + redshift)**2) spectral_norm = parameters["FiducialArea"].v * parameters[ "FiducialExposureTime"].v * dist_fac source_model.setup_model(data_source, redshift, spectral_norm) p_fields, v_fields, w_field = determine_fields( ds, source_model.source_type) if velocity_fields is not None: v_fields = velocity_fields if p_fields[0] == ("index", "x"): parameters["DataType"] = "cells" else: parameters["DataType"] = "particles" if hasattr(data_source, "left_edge"): # Region or grid le = data_source.left_edge re = data_source.right_edge elif hasattr(data_source, "radius") and not hasattr(data_source, "height"): # Sphere le = -data_source.radius + data_source.center re = data_source.radius + data_source.center else: # Compute rough boundaries of the object # DOES NOT WORK for objects straddling periodic # boundaries yet if sum(ds.periodicity) > 0: mylog.warning("You are using a region that is not currently " "supported for straddling periodic boundaries. " "Check to make sure that this is not the case.") le = ds.arr(np.zeros(3), "code_length") re = ds.arr(np.zeros(3), "code_length") for i, ax in enumerate(p_fields): le[i], re[i] = data_source.quantities.extrema(ax) dds_min = get_smallest_dds(ds, parameters["DataType"]) le = np.rint((le - ds.domain_left_edge) / dds_min) * dds_min + ds.domain_left_edge re = ds.domain_right_edge - np.rint( (ds.domain_right_edge - re) / dds_min) * dds_min width = re - le parameters["Dimension"] = np.rint(width / dds_min).astype("int") parameters["Width"] = parameters["Dimension"] * dds_min.in_units("kpc") citer = data_source.chunks([], "io") photons = defaultdict(list) for chunk in parallel_objects(citer): chunk_data = source_model(chunk) if chunk_data is not None: number_of_photons, idxs, energies = chunk_data photons["NumberOfPhotons"].append(number_of_photons) photons["Energy"].append(ds.arr(energies, "keV")) photons["x"].append(chunk[p_fields[0]][idxs].in_units("kpc")) photons["y"].append(chunk[p_fields[1]][idxs].in_units("kpc")) photons["z"].append(chunk[p_fields[2]][idxs].in_units("kpc")) photons["vx"].append(chunk[v_fields[0]][idxs].in_units("km/s")) photons["vy"].append(chunk[v_fields[1]][idxs].in_units("km/s")) photons["vz"].append(chunk[v_fields[2]][idxs].in_units("km/s")) if w_field is None: photons["dx"].append(ds.arr(np.zeros(len(idxs)), "kpc")) else: photons["dx"].append(chunk[w_field][idxs].in_units("kpc")) source_model.cleanup_model() concatenate_photons(photons) # Translate photon coordinates to the source center # Fix photon coordinates for regions crossing a periodic boundary dw = ds.domain_width.to("kpc") for i, ax in enumerate("xyz"): if ds.periodicity[i] and len(photons[ax]) > 0: tfl = photons[ax] < le[i].to('kpc') tfr = photons[ax] > re[i].to('kpc') photons[ax][tfl] += dw[i] photons[ax][tfr] -= dw[i] photons[ax] -= parameters["center"][i].in_units("kpc") mylog.info("Finished generating photons.") mylog.info("Number of photons generated: %d" % int(np.sum(photons["NumberOfPhotons"]))) mylog.info("Number of cells with photons: %d" % len(photons["x"])) return cls(photons, parameters, cosmo)