def concatenate_agn_list(env): for HEALPIX_32_id in n.arange(healpy.nside2npix(8)): list_name = 'fit_list_' + env + "_" + ftyp + str(HEALPIX_32_id).zfill( 6) + '_AGN.list' c1 = 'ls ' + test_dir + '/cat_AGN_' + ftyp + '_*/' + str( HEALPIX_32_id).zfill(6) + '.fit > ' + list_name print(c1)
def concatenate_agn(env): if os.path.isdir(catalogue_dir) == False: os.system('mkdir -p ' + catalogue_dir) print('concatenate all AGN catalogs', env, time.time() - t0) for HEALPIX_32_id in n.arange(healpy.nside2npix(8)): path_2_eRO_catalogs = n.array( glob.glob( os.path.join(test_dir, 'cat_AGN_' + ftyp + '_*', str(HEALPIX_32_id).zfill(6) + '.fit'))) #print(HEALPIX_32_id, len(path_2_eRO_catalogs), time.time() - t0) path_2_agn_summary_file = os.path.join( catalogue_dir, str(HEALPIX_32_id).zfill(6) + '.fit') #print(path_2_agn_summary_file) os.chdir(lss_git_dir) # concatenates shells into a single fits catalog list_name = 'fit_list_' + env + "_" + ftyp + str(HEALPIX_32_id).zfill( 6) + '_AGN.list' c1 = 'ls ' + test_dir + '/cat_AGN_' + ftyp + '_*/' + str( HEALPIX_32_id).zfill(6) + '.fit > ' + list_name print(c1) #os.system(c1) c2 = "nohup " + stilts_cmd + """ tcat in=@""" + list_name + """ ifmt=fits omode=out ofmt=fits out=""" + path_2_agn_summary_file + " > concat_agn_" + str( HEALPIX_32_id).zfill(6) + ".log & " print(c2)
def contains(self, ra, dec, keep_inside=True): """ Get a mask array (e.g. a numpy boolean array) of positions being inside (or outside) the mocpy object instance. Parameters ---------- ra : `astropy.units.Quantity` right ascension array dec: `astropy.units.Quantity` declination array keep_inside : bool, optional True by default. If so the filtered table contains only observations that are located into the mocpy object. If ``keep_inside`` is False, the filtered table contains all observations lying outside the mocpy object. Returns ------- array : `~numpy.darray` A mask boolean array """ max_order = self.max_order m = np.zeros(nside2npix(1 << max_order), dtype=bool) pix_id_arr = self._best_res_pixels() m[pix_id_arr] = True if not keep_inside: m = np.logical_not(m) hp = HEALPix(nside=(1 << self.max_order), order='nested') pix_arr = hp.lonlat_to_healpix(ra, dec) return m[pix_arr]
def plot(self, title='MOC', coord='C'): """ Plot the MOC object in a mollweide view This method uses matplotlib. Parameters ---------- title : str the title of the plot coord : str type of coord (ICRS, Galactic, ...) in which the moc pix will be plotted. only ICRS coordinates are supported for the moment. #TODO handle Galactic coordinates """ from matplotlib.colors import LinearSegmentedColormap import matplotlib.pyplot as plt plot_order = 8 if self.max_order > plot_order: plotted_moc = self.degrade_to_order(plot_order) else: plotted_moc = self num_pixels_map = 768 delta = 2 * np.pi / num_pixels_map x = np.arange(-np.pi, np.pi, delta) y = np.arange(-np.pi / 2, np.pi / 2, delta) lon_rad, lat_rad = np.meshgrid(x, y) m = np.zeros(nside2npix(2**plotted_moc.max_order)) for val in plotted_moc.best_res_pixels_iterator(): m[val] = 1 hp = HEALPix(nside=(1 << plotted_moc.max_order), order='nested', frame=ICRS()) pix_map = hp.lonlat_to_healpix(lon_rad * u.rad, lat_rad * u.rad) z = np.flip(m[pix_map], axis=1) plt.figure(figsize=(10, 10)) ax = plt.subplot(111, projection="mollweide") ax.set_xticklabels([ '150°', '120°', '90°', '60°', '30°', '0°', '330°', '300°', '270°', '240°', '210°', '180°' ]) color_map = LinearSegmentedColormap.from_list('w2r', ['#eeeeee', '#aa0000']) color_map.set_under('w') color_map.set_bad('gray') ax.pcolormesh(x, y, z, cmap=color_map, vmin=0, vmax=1) ax.tick_params(labelsize=14, labelcolor='#000000') plt.title(title) plt.grid(True, linestyle='--', linewidth=1, color='#555555') plt.show()
def draw_hpxbin(self, lon, lat, nside=256, **kwargs): """ Create a healpix histogram of the counts. Like `hexbin` from matplotlib Parameters: ----------- lon : input longitude (deg) lat : input latitude (deg) nside : heaplix nside resolution kwargs : passed to draw_hpxmap and plt.pcolormesh Returns: -------- hpxmap, im : healpix map and image """ try: pix = hp.ang2pix(nside, lon, lat, lonlat=True) except TypeError: pix = hp.ang2pix(nside, np.radians(90 - lat), np.radians(lon)) npix = hp.nside2npix(nside) hpxmap = hp.UNSEEN * np.ones(npix) idx, cts = np.unique(pix, return_counts=True) hpxmap[idx] = cts return hpxmap, self.draw_hpxmap(hpxmap, **kwargs)
def create_map(hpxmap,pixel,nside,badval=hp.UNSEEN): """ Create the full map from hpxmap,pixel,nside combo """ if pixel is None: return hpxmap m = badval*np.ones(hp.nside2npix(nside),dtype=hpxmap.dtype) m[pixel] = hpxmap return m
def _get_max_order_pix(self, keep_inside): """ Get a boolean numpy array of size the number of pix in the max_order of the MOC object The ith element of this array equals to True if the corresponding pix is in the MOC for this order If it is not, the element is set to False Parameters ---------- keep_inside : bool ``keep_inside`` boolean value is associated with pix that are in the MOC Returns ------- result : `~numpy.ndarray` boolean array telling which pix at the max_order are in the MOC """ from astropy_healpix.healpy import nside2npix max_order = self.max_order n_side = 2**max_order result = np.zeros(nside2npix(n_side), dtype=bool) for val in self.best_res_pixels_iterator(): result[val] = True if not keep_inside: result = np.logical_not(result) return result
def contains(self, ra, dec, keep_inside=True): """ Returns a boolean mask array of the positions lying inside (or outside) the MOC instance. Parameters ---------- ra : `astropy.units.Quantity` Right ascension array dec : `astropy.units.Quantity` Declination array keep_inside : bool, optional True by default. If so the mask describes coordinates lying inside the MOC. If ``keep_inside`` is false, contains will return the mask of the coordinates lying outside the MOC. Returns ------- array : `~np.ndarray` A mask boolean array """ depth = self.max_order m = np.zeros(nside2npix(1 << depth), dtype=bool) pix_id = self._best_res_pixels() m[pix_id] = True if not keep_inside: m = np.logical_not(m) hp = HEALPix(nside=(1 << depth), order='nested') pix = hp.lonlat_to_healpix(ra, dec) return m[pix]
def test_healpix_to_hips(tmpdir, file_format): nside, tile_width = 4, 2 npix = hp.nside2npix(nside) hpx_data = np.arange(npix, dtype="uint8") healpix_to_hips( hpx_data=hpx_data, tile_width=tile_width, base_path=tmpdir, file_format=file_format, frame="galactic", ) # The test data is filled with np.arange(), here we reproduce the sum of the # indices in the nested scheme manually for comparison desired = hpx_data.reshape((-1, tile_width, tile_width)) for idx, val in enumerate(desired): filename = str(tmpdir / f"Norder1/Dir0/Npix{idx}.{file_format}") if file_format is "fits": data = fits.getdata(filename) data = np.rot90(data, k=-1) else: data = np.array(Image.open(filename)) data = data.T assert_allclose(val, data) properties = (tmpdir / "properties").read_text(encoding=None) assert file_format in properties assert "galactic" in properties
def test_flat_spectrum(): Nfreqs = 100 freq_array = np.linspace(167.0e6, 177.0e6, Nfreqs) sigma = 2.0 Nside = 32 Npix = hp.nside2npix(Nside) Nskies = 1 maps = sky_model.flat_spectrum_noise_shell(sigma, freq_array, Nside, Nskies) assert maps.shape == (Nskies, Npix, Nfreqs)
def test_healpix_to_hips_tile(): nside, tile_width = 4, 2 npix = hp.nside2npix(nside) hpx_data = np.arange(npix, dtype="uint8") tile = healpix_to_hips_tile( hpx_data=hpx_data, tile_width=tile_width, tile_idx=0, file_format="fits", frame="galactic", ) assert_equal(tile.data, [[1, 3], [0, 2]]) assert tile.meta.order == 1 assert tile.meta.ipix == 0 assert tile.meta.file_format == "fits" assert tile.meta.frame == "galactic" assert tile.meta.width == 2
def concatenate_galaxies(env): if os.path.isdir(catalogue_dir) == False: os.system('mkdir -p ' + catalogue_dir) for HEALPIX_id in n.arange(healpy.nside2npix(8)): path_2_galaxy_summary_file = os.path.join( catalogue_dir, str(HEALPIX_id).zfill(6) + '.fit') #print(path_2_galaxy_summary_file) os.chdir(lss_git_dir) # concatenates shells into a single fits catalog list_name = 'logs/fit_list_' + env + "_" + ftyp + str( HEALPIX_id).zfill(6) + '_GALAXY.list' c1 = 'ls ' + test_dir + '/cat_GALAXY_' + ftyp + '_*/' + str( HEALPIX_id).zfill(6) + '.fit > ' + list_name print(c1) #os.system(c1) c2 = 'python concat_file_list.py ' + list_name + ' ' + path_2_galaxy_summary_file command_2 = 'nohup ' + c2 + '> logs/002_1_concat_' + env + "_" + ftyp + str( HEALPIX_id).zfill(6) + '.log &'
def concatenate_galaxies(env): if os.path.isdir(catalogue_dir) == False: os.system('mkdir -p ' + catalogue_dir) print('concatenate all GALAXY catalogs', env, time.time() - t0) for HEALPIX_id in n.arange(healpy.nside2npix(8)): path_2_galaxy_summary_file = os.path.join( catalogue_dir, str(HEALPIX_id).zfill(6) + '.fit') print(path_2_galaxy_summary_file) os.chdir(lss_git_dir) # concatenates shells into a single fits catalog list_name = 'fit_list_' + env + "_" + ftyp + str(HEALPIX_id).zfill( 6) + '_GALAXY.list' c1 = 'ls ' + test_dir + '/cat_GALAXY_' + ftyp + '_*/' + str( HEALPIX_id).zfill(6) + '.fit > ' + list_name print(c1) os.system(c1) c2 = stilts_cmd + """ tcat in=@""" + list_name + """ ifmt=fits omode=out ofmt=fits out=""" + path_2_galaxy_summary_file print(c2) os.system(c2) os.system('rm ' + list_name)
flux_lim_data = fits.open(path_2_flux_limits) # [1].data #flux_limit_eRASS3, flux_limit_eRASS8, flux_limit_SNR3 flux_limit = flux_lim_data[1].data['flux_limit_eRASS8'] print('flux limit file opened', time.time() - t0) # selection function here : # ( FX_soft_attenuated > 10**(flux_limit[pix_ids]-2) ) & ( SDSS_r_AB_attenuated < 26.5 ) sf1 = (FX_soft_attenuated > 0) print('selection function applied', time.time() - t0) HEALPIX_8 = healpy.ang2pix(8, n.pi / 2. - dec * n.pi / 180., ra * n.pi / 180., nest=True) for HEALPIX_8_id in n.arange(healpy.nside2npix(8)): path_2_eRO_catalog = os.path.join(dir_2_eRO_catalog, str(HEALPIX_8_id).zfill(6) + '.fit') # print(path_2_eRO_catalog) sf = (sf1) & (HEALPIX_8 == HEALPIX_8_id) if len(sf.nonzero()[0]) > 0: t = Table() ## # Coordinates ## t.add_column(Column(name="ra", format='D', unit='degree', data=ra[sf])) t.add_column( Column(name="dec", format='D', unit='degree', data=dec[sf])) t.add_column( Column(name="g_lat", format='D', unit='degree', data=g_lat[sf])) t.add_column(
cp /data40s/erosim/eRASS/simulated_photons.fits wwwDir/erosita_stuff/ cd data/eRoMok wget http://www.mpe.mpg.de/~comparat/erosita_stuff/simulated_photons.fits erosim Simput=/data17s/darksim/MD/MD_1.0Gpc/cat_AGN_SIMPUT/SIMPUT_000000_1024.fit Prefix=/data40s/erosim/eRASS/eRASS8_agn/000/erass_ Attitude=/data40s/erosim/eRASS/eRASS_4yr_epc85_att.fits RA=224.99999999999997 Dec=84.14973293629666 GTIFile=/data40s/erosim/eRASS/eRASS8_agn/000/erass.gti TSTART=0.0 Exposure=126144000.0 MJDREF=51543.875 dt=1.0 Seed=42 clobber=yes chatter=3 Background=yes """ import subprocess import os import errno import sys from astropy_healpix import healpy import numpy as n pix_ids = n.arange(healpy.nside2npix(8)) ra_cen_s, dec_cen_s = healpy.pix2ang(8, pix_ids, nest=False, lonlat=True) class Simulator: """ SIXTE simulator for eROSITA observations. 1. Compute GTI file for given simput 2. Simulate eROSITA observations of simput, using GTI to speed things up. """ def __init__(self, with_bkg_par, t_start, exposure, seed, simput, data_dir, ra_cen, dec_cen): # def __init__(self, with_bkg_par, t_start, exposure, seed, simput, # simput2, simput3): """ :param with_bkg_par: Simulate with particle background.
} priority_values = { 'BG': 100, 'filament_GAL': 80, 'LRG': 99, 'ELG': 80, 'QSO': 97, 'LyA': 98 } # reassigns templates correctly z_all = n.hstack((0., n.arange(0.3, 3., 0.2), 3.5, 4.5, 6.)) zmins = z_all[:-1] zmaxs = z_all[1:] N_pixels = healpy.nside2npix(8) for HEALPIX_id in n.arange(N_pixels)[::-1]: print(HEALPIX_id) path_2_BG = os.path.join(dir_2_OUT, 'BG_' + str(HEALPIX_id).zfill(6) + '.fit') path_2_BG_S5 = os.path.join(dir_2_OUT, 'S5GAL_' + str(HEALPIX_id).zfill(6) + '.fit') path_2_LRG = os.path.join(dir_2_OUT, 'LRG_' + str(HEALPIX_id).zfill(6) + '.fit') path_2_ELG = os.path.join(dir_2_OUT, 'ELG_' + str(HEALPIX_id).zfill(6) + '.fit') #path_2_QSO = os.path.join(dir_2_OUT, 'QSO_' + str(HEALPIX_id).zfill(6) + '.fit') t_bg = Table.read(path_2_BG) t_bgS5 = Table.read(path_2_BG_S5) t_lrg = Table.read(path_2_LRG)
def make_plot(args): """ Take the steps to make the plot. Parameters ---------- args: array-like Command line arguments Returns ------- Nothing """ basename = 'PMmap-qso-galactic-aberration' gx = 5.04 gy = -0.10 gz = -0.29 if args['quiver']: hplevel = 3 else: hplevel = 5 nside = hp.order2nside(hplevel) npix = hp.nside2npix(nside) ahp = HEALPix(nside=nside, order='nested', frame=Galactic()) hpindices = np.arange(npix) skycoords = ahp.healpix_to_skycoord(hpindices) pm_l_cosb = -gx * np.sin(skycoords.l.to(u.rad)) + gy * np.cos( skycoords.l.to(u.rad)) pm_b = -gx * np.sin(skycoords.b.to(u.rad)) * np.cos(skycoords.l.to(u.rad)) \ - gy * np.sin(skycoords.b.to(u.rad)) * np.sin(skycoords.l.to(u.rad)) \ + gz * np.cos(skycoords.b.to(u.rad)) pmtot = np.sqrt(pm_l_cosb**2 + pm_b**2) backgr = plt.imread( '../star-trail-animation/sky-images/GaiaSky-colour-2k.png') default_proj = ccrs.PlateCarree() sky_proj = ccrs.Mollweide() fig = plt.figure(figsize=(16, 9), dpi=120, frameon=False, tight_layout={'pad': 0.01}) gs = GridSpec(1, 1, figure=fig) ax = fig.add_subplot(gs[0, 0], projection=sky_proj) ax.imshow(np.fliplr(backgr), transform=default_proj, zorder=-1, origin='upper') veccolor = plt.cm.get_cmap('tab10').colors[9] linecolor = plt.cm.get_cmap('tab10').colors[9] if args['quiver']: vscale = np.median(pmtot) / 50 ax.quiver(skycoords.l.value, skycoords.b.value, pm_l_cosb, pm_b, transform=default_proj, angles='xy', scale=vscale, scale_units='dots', color=veccolor, headwidth=4, headlength=4, headaxislength=3.5) else: if args['colourstreams']: ax.streamplot(skycoords.l.value, skycoords.b.value, pm_l_cosb, pm_b, transform=default_proj, linewidth=2.0, density=2, color=pmtot, cmap='viridis', maxlength=0.5, arrowsize=1, arrowstyle=ArrowStyle.Fancy(head_length=1.0, head_width=.4, tail_width=.4)) elif args['lwcode'] > 0: ax.streamplot(skycoords.l.value, skycoords.b.value, pm_l_cosb, pm_b, transform=default_proj, linewidth=args['lwcode'] * pmtot / np.median(pmtot), density=2, color=linecolor, maxlength=0.5, arrowsize=1, arrowstyle=ArrowStyle.Fancy(head_length=1.0, head_width=.4, tail_width=.4)) else: ax.streamplot(skycoords.l.value, skycoords.b.value, pm_l_cosb, pm_b, transform=default_proj, linewidth=1.5, density=2.5, color=linecolor, maxlength=0.5, arrowsize=1, arrowstyle=ArrowStyle.Fancy(head_length=1.0, head_width=.4, tail_width=.4)) # ax.gridlines() ax.invert_xaxis() if args['pdfOutput']: plt.savefig(basename + '.pdf') elif args['pngOutput']: plt.savefig(basename + '.png') else: plt.show()
def plot(self, title='MOC', frame=None): """ Plot the MOC object in a mollweide view This method uses matplotlib. Parameters ---------- title : str the title of the plot frame : `astropy.coordinates.BaseCoordinateFrame`, optional Describes the coordinate system the plot will be (ICRS, Galactic are the only coordinate systems supported). """ frame = ICRS() if frame is None else frame from matplotlib.colors import LinearSegmentedColormap import matplotlib.pyplot as plt plot_order = 8 if self.max_order > plot_order: plotted_moc = self.degrade_to_order(plot_order) else: plotted_moc = self num_pixels_map = 1024 delta = 2 * np.pi / num_pixels_map x = np.arange(-np.pi, np.pi, delta) y = np.arange(-np.pi / 2, np.pi / 2, delta) lon_rad, lat_rad = np.meshgrid(x, y) hp = HEALPix(nside=(1 << plotted_moc.max_order), order='nested') if frame and not isinstance(frame, BaseCoordinateFrame): raise ValueError( "Only Galactic/ICRS coordinate systems are supported." "Please set `coord` to either 'C' or 'G'.") pix_map = hp.lonlat_to_healpix(lon_rad * u.rad, lat_rad * u.rad) m = np.zeros(nside2npix(1 << plotted_moc.max_order)) pix_id_arr = plotted_moc._best_res_pixels() # change the HEALPix cells if the frame of the MOC is not the same as the one associated with the plot method. if isinstance(frame, Galactic): lon, lat = hp.boundaries_lonlat(pix_id_arr, step=2) sky_crd = SkyCoord(lon, lat, unit='deg') pix_id_arr = hp.lonlat_to_healpix(sky_crd.galactic.l, sky_crd.galactic.b) m[pix_id_arr] = 1 z = np.flip(m[pix_map], axis=1) plt.figure(figsize=(10, 10)) ax = plt.subplot(111, projection="mollweide") ax.set_xticklabels([ '150°', '120°', '90°', '60°', '30°', '0°', '330°', '300°', '270°', '240°', '210°', '180°' ]) color_map = LinearSegmentedColormap.from_list('w2r', ['#eeeeee', '#aa0000']) color_map.set_under('w') color_map.set_bad('gray') ax.pcolormesh(x, y, z, cmap=color_map, vmin=0, vmax=1) ax.tick_params(labelsize=14, labelcolor='#000000') plt.title(title) plt.grid(True, linestyle='--', linewidth=1, color='#555555') plt.show()
def plot(self, title='MOC', frame=None): """ Plot the MOC object using a mollweide projection. **Deprecated**: New `fill` and `border` methods produce more reliable results and allow you to specify additional matplotlib style parameters. Parameters ---------- title : str The title of the plot frame : `astropy.coordinates.BaseCoordinateFrame`, optional Describes the coordinate system the plot will be (ICRS, Galactic are the only coordinate systems supported). """ frame = ICRS() if frame is None else frame from matplotlib.colors import LinearSegmentedColormap import matplotlib.pyplot as plt plot_order = 8 if self.max_order > plot_order: plotted_moc = self.degrade_to_order(plot_order) else: plotted_moc = self num_pixels_map = 1024 delta = 2. * np.pi / num_pixels_map x = np.arange(-np.pi, np.pi, delta) y = np.arange(-np.pi/2, np.pi/2, delta) lon_rad, lat_rad = np.meshgrid(x, y) hp = HEALPix(nside=(1 << plotted_moc.max_order), order='nested') if frame and not isinstance(frame, BaseCoordinateFrame): raise ValueError("Only Galactic/ICRS coordinate systems are supported." "Please set `coord` to either 'C' or 'G'.") pix_map = hp.lonlat_to_healpix(lon_rad * u.rad, lat_rad * u.rad) m = np.zeros(nside2npix(1 << plotted_moc.max_order)) pix_id = plotted_moc._best_res_pixels() # change the HEALPix cells if the frame of the MOC is not the same as the one associated with the plot method. if isinstance(frame, Galactic): lon, lat = hp.boundaries_lonlat(pix_id, step=2) sky_crd = SkyCoord(lon, lat, unit='deg') pix_id = hp.lonlat_to_healpix(sky_crd.galactic.l, sky_crd.galactic.b) m[pix_id] = 1 z = np.flip(m[pix_map], axis=1) plt.figure(figsize=(10, 10)) ax = plt.subplot(111, projection="mollweide") ax.set_xticklabels(['150°', '120°', '90°', '60°', '30°', '0°', '330°', '300°', '270°', '240°', '210°', '180°']) color_map = LinearSegmentedColormap.from_list('w2r', ['#eeeeee', '#aa0000']) color_map.set_under('w') color_map.set_bad('gray') ax.pcolormesh(x, y, z, cmap=color_map, vmin=0, vmax=1) ax.tick_params(labelsize=14, labelcolor='#000000') plt.title(title) plt.grid(True, linestyle='--', linewidth=1, color='#555555') plt.show()