def read_pop_fromhdf5(self, filename): """ Read from an hdf5 file the cloud population. the :class:`Cloud_Population` is thus initialized by :func:`initialize_cloud_population_from_output` """ f = h5.File(filename, 'r') g = f["Cloud_Population"] self.r = g["R"][...] self.phi = g["Phi"][...] self.L = g["Sizes"][...] self.W = g["Emissivity"][...] coord_array = np.zeros(self.n) if self.models[self.model] == 1: self.theta = g["Theta"][...] coord_array = coord.PhysicsSphericalRepresentation( self.phi * u.rad, self.theta * u.rad, self.r * u.kpc) elif self.models[self.model] >= 2: self.zeta = g["Z"][...] coord_array = coord.CylindricalRepresentation( self.r * u.kpc, self.phi * u.rad, self.zeta * u.kpc) self.d_sun = g["D_sun"][...] self.long = g["Gal_longitude"][...] self.lat = g["Gal_Latitude"][...] self.healpix_vecs = g["Healpix_Vec"][...] self.cartesian_galactocentric = self.cartesianize_coordinates( coord_array) from utilities.utilities_functions import bash_colors cols = bash_colors() f.close() print(cols.bold("////// \t read from " + filename + "\t ////////")) pass
gc3 = c1.transform_to(gc_frame2) print(gc3.v_x, gc3.v_y, gc3.v_z) ############################################################################## # The transformations also work in the opposite direction. This can be useful # for transforming simulated or theoretical data to observable quantities. As # an example, we'll generate 4 theoretical circular orbits at different # Galactocentric radii with the same circular velocity, and transform them to # Heliocentric coordinates: ring_distances = np.arange(10, 25 + 1, 5) * u.kpc circ_velocity = 220 * u.km / u.s phi_grid = np.linspace(90, 270, 512) * u.degree # grid of azimuths ring_rep = coord.CylindricalRepresentation( rho=ring_distances[:, np.newaxis], phi=phi_grid[np.newaxis], z=np.zeros_like(ring_distances)[:, np.newaxis]) angular_velocity = (-circ_velocity / ring_distances).to( u.mas / u.yr, u.dimensionless_angles()) ring_dif = coord.CylindricalDifferential( d_rho=np.zeros(phi_grid.shape)[np.newaxis] * u.km / u.s, d_phi=angular_velocity[:, np.newaxis], d_z=np.zeros(phi_grid.shape)[np.newaxis] * u.km / u.s) ring_rep = ring_rep.with_differentials(ring_dif) gc_rings = coord.SkyCoord(ring_rep, frame=coord.Galactocentric) ############################################################################## # First, let's visualize the geometry in Galactocentric coordinates. Here are # the positions and velocities of the rings; note that in the velocity plot,
def __call__(self): self.clouds = [] np.random.seed(11 + self.random_seed) a = np.random.uniform(low=0., high=1., size=self.n) self.phi = 2. * np.pi * a if self.model == 'Spherical': self.r = norm.rvs(loc=self.R_params[0], scale=self.R_params[1], size=self.n) v = np.random.uniform(low=0., high=1., size=self.n) self.theta = np.arccos(2. * v - 1.) coord_array = coord.PhysicsSphericalRepresentation( self.phi * u.rad, self.theta * u.rad, self.r * u.kpc) self.cartesian_galactocentric = self.cartesianize_coordinates( coord_array) self.heliocentric_coordinates() for i, x, p, t, d, latit, longit in zip(np.arange(self.n), self.r, self.phi, self.theta, self.d_sun, self.lat, self.long): if x <= 0.: self.r[i] = np.random.uniform(low=0., high=1., size=1) x = self.r[i] c = Cloud(i, x, p, t, size=None, em=None) c.assign_sun_coord(d, latit, longit) self.clouds.append(c) else: self.r = self.phi * 0. rbar = self.R_params[2] if self.model == 'Axisymmetric': np.random.seed(self.random_seed + 29) self.r = norm.rvs(loc=self.R_params[0], scale=self.R_params[1], size=self.n) negs = np.ma.masked_less(self.r, 0.) #central molecular zone self.r[negs.mask] = 0. elif self.model == 'LogSpiral': #the bar is assumed axisymmetric and with an inclination angle phi0~25 deg as #it has been measured by F*x et al. 1999 phi_0 = np.deg2rad(25.) self.phi += phi_0 subsize = np.int(self.n / 10) self.r[0:subsize] = norm.rvs(loc=self.R_params[0], scale=self.R_params[1], size=subsize) #np.random.uniform(low=0.,high=8.,size=self.n/4) rscale = rbar / 1.5 self.r[subsize:self.n],self.phi[subsize:self.n]=log_spiral_radial_distribution2(\ rbar,phi_0,self.n-subsize,self.R_params[0],self.R_params[1]) #self.r[subsize:self.n]=log_spiral_radial_distribution(self.phi[subsize:self.n],rbar,phi_0) #simulate the bar arr = np.ma.masked_less(self.r, rbar) self.r[arr.mask] = abs( np.random.normal(loc=0., scale=rscale, size=len(self.r[arr.mask]))) negs = np.ma.masked_less(self.r, 0.) #central molecular zone self.r[negs.mask] = 0. #the thickness of the Galactic plane is function of the Galactic Radius roughly as ~ 100 pc *cosh((x/R0) ), with R0~10kpc # for reference see fig.6 of Heyer and Dame, 2015 sigma_z0 = self.z_distr[0] R_z0 = self.z_distr[1] sigma_z = lambda R: sigma_z0 * np.cosh((R / R_z0)) self.zeta = self.phi * 0. np.random.seed(self.random_seed + 19) for i, x, p in zip(np.arange(self.n), self.r, self.phi): self.zeta[i] = np.random.normal(loc=0., scale=sigma_z(x)) self.clouds.append( Cloud(i, x, p, self.zeta[i], size=None, em=None)) coord_array = coord.CylindricalRepresentation( self.r * u.kpc, self.phi * u.rad, self.zeta * u.kpc) self.cartesian_galactocentric = self.cartesianize_coordinates( coord_array) self.heliocentric_coordinates() for c, d, latit, longit in zip(self.clouds, self.d_sun, self.lat, self.long): c.assign_sun_coord(d, latit, longit) self.L = np.array(self.sizes) self.healpix_vecs = self.compute_healpix_vec() self.W = self.get_pop_emissivities_sizes()[0]
def test__fix_branch_cuts(self): """Test method ``_fix_branch_cuts``. .. todo:: graphical proof via mpl_test that the point hasn't moved. """ # ------------------------------- # no angular units rep = coord.CartesianRepresentation( x=[1, 2] * u.kpc, y=[3, 4] * u.kpc, z=[5, 6] * u.kpc, ) array = rep._values.view(dtype=np.float64).reshape(rep.shape[0], -1).T got = self.inst._fix_branch_cuts(array, rep.__class__, rep._units) assert got is array # ------------------------------- # UnitSphericalRepresentation # 1) all good rep = coord.UnitSphericalRepresentation( lon=[1, 2] * u.deg, lat=[3, 4] * u.deg, ) array = rep._values.view(dtype=np.float64).reshape(rep.shape[0], -1).T got = self.inst._fix_branch_cuts( array.copy(), rep.__class__, rep._units, ) assert np.allclose(got, array) # 2) needs correction array = np.array([[-360, 0, 360], [-91, 0, 91]]) got = self.inst._fix_branch_cuts( array.copy(), rep.__class__, rep._units, ) assert np.allclose(got, np.array([[-180, 0, 540], [-89, 0, 89]])) # ------------------------------- # SphericalRepresentation # 1) all good rep = coord.SphericalRepresentation( lon=[1, 2] * u.deg, lat=[3, 4] * u.deg, distance=[5, 6] * u.kpc, ) array = rep._values.view(dtype=np.float64).reshape(rep.shape[0], -1).T got = self.inst._fix_branch_cuts( array.copy(), rep.__class__, rep._units, ) assert np.allclose(got, array) # 2) needs correction array = np.array([[-360, 0, 360], [-91, 0, 91], [5, 6, 7]]) got = self.inst._fix_branch_cuts( array.copy(), rep.__class__, rep._units, ) assert np.allclose( got, np.array([[-180, 0, 540], [-89, 0, 89], [5, 6, 7]]), ) # 3) needs correction array = np.array([[-360, 0, 360], [-91, 0, 91], [-5, 6, -7]]) got = self.inst._fix_branch_cuts( array.copy(), rep.__class__, rep._units, ) assert np.allclose( got, np.array([[0, 0, 720], [89, 0, -89], [5, 6, 7]]), ) # ------------------------------- # CylindricalRepresentation # 1) all good rep = coord.CylindricalRepresentation( rho=[5, 6] * u.kpc, phi=[1, 2] * u.deg, z=[3, 4] * u.parsec, ) array = rep._values.view(dtype=np.float64).reshape(rep.shape[0], -1).T got = self.inst._fix_branch_cuts( array.copy(), rep.__class__, rep._units, ) assert np.allclose(got, array) # 2) needs correction array = np.array([[-5, 6, -7], [-180, 0, 180], [-4, 4, 4]]) got = self.inst._fix_branch_cuts( array.copy(), rep.__class__, rep._units, ) assert np.allclose(got, np.array([[5, 6, 7], [0, 0, 360], [-4, 4, 4]])) # ------------------------------- # NotImplementedError with pytest.raises(NotImplementedError): rep = coord.PhysicsSphericalRepresentation( phi=[1, 2] * u.deg, theta=[3, 4] * u.deg, r=[5, 6] * u.kpc, ) array = (rep._values.view(dtype=np.float64).reshape( rep.shape[0], -1).T) self.inst._fix_branch_cuts( array.copy(), coord.PhysicsSphericalRepresentation, rep._units, )