Skip to content

Commit 382fe83

Browse files
committed
Add mask folder with point source finding and point source mask scripts
1 parent eff9234 commit 382fe83

3 files changed

Lines changed: 169 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import numpy as np
2+
from matplotlib import pyplot as plt
3+
from pixell import enmap, enplot, reproject
4+
from pspy import so_map
5+
import healpy as hp
6+
from os.path import join as opj
7+
8+
catalog = np.loadtxt("maps/catalogs/cat.txt").T
9+
10+
save_dir = ""
11+
spt_mask_dir = opj(save_dir, "masks")
12+
13+
header = ['ra', 'dec', 'SNR', 'Tamp', 'dTamp', 'Qamp', 'dQamp', 'Uamp', 'dUamp', 'Tflux', 'dTflux', 'Qflux', 'dQflux', 'Uflux', 'dUflux', 'npix', 'status']
14+
catalog_dict = {k:v for k, v in zip(header, catalog)}
15+
16+
car_template = so_map.from_enmap(enmap.read_map(opj(spt_mask_dir, 'pixel_mask_binary_borders_only_CAR.fits')))
17+
18+
SNR_cut = 5
19+
20+
for flux_cut in [100, 50, 15]:
21+
flux_mask = (catalog_dict["SNR"] > 5) & (catalog_dict["Tflux"] > flux_cut)
22+
23+
ps_mask = so_map.generate_source_mask(
24+
car_template,
25+
coordinates=np.deg2rad([np.array(catalog_dict['dec'][flux_mask]), np.array(catalog_dict['ra'][flux_mask])]),
26+
point_source_radius_arcmin=12
27+
).data
28+
enmap.write_map(opj(spt_mask_dir, f"point_source_test_{flux_cut}_CAR.fits"), ps_mask)
29+
plot = enplot.get_plots(
30+
ps_mask, ticks=10, mask=0, colorbar=True, downgrade=2, range=(1)
31+
)
32+
enplot.write(opj(spt_mask_dir, f"point_source_test_{flux_cut}_CAR"), plot)
33+
34+
ps_mask_healpix = reproject.map2healpix(ps_mask, nside=8096, method="spline")
35+
hp.mollview(ps_mask_healpix)
36+
plt.savefig(opj(spt_mask_dir, f"point_source_test_{flux_cut}.png"))
37+
hp.write_map(opj(spt_mask_dir, f"point_source_test_{flux_cut}.fits"), ps_mask_healpix)
38+
39+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
**************************
2+
Finding sources in SPT maps
3+
**************************
4+
5+
We use ``dory`` from tenki (https://github.com/amaurea/tenki/tree/master) to find sources in SPT maps.
6+
We first need to project SPT masks and maps to CAR so dory can read these.
7+
``project_maps_SPT_patch.py`` defines a CAR template around SPT survey and projects 90GHz full maps, ivar (normalized ivar * 1e-2 to mimic a 5uK.arcmin depth, but we should look for raw ivar maps) and masks.
8+
Go in ``python/mask/project_maps_SPT_patch.py`` and modify ``save_dir`` if you want to save maps somewhere in particular, then run:
9+
10+
.. code:: shell
11+
salloc --nodes 1 --qos interactive --time 00:20:00 --constraint cpu
12+
OMP_NUM_THREADS=256 srun -n 1 -c 256 --cpu-bind=cores python mask/project_maps_SPT_patch.py
13+
14+
You can now get dory by cloning ``tenki``.
15+
Before running ``dory``, I had some problems with floating point precision (?) in ``tenki/point_sources/dory.py``, so go to line 119 and change ``> 0`` to ``> 1e-10``.
16+
Otherwise, masks will look very weird and the code will not find any sources.
17+
We can now run ``dory`` (change your path to dory if needed), you can remove "debug" in --output if you trust what I did (I wouldn't):
18+
.. code:: shell
19+
salloc --nodes 1 --qos interactive --time 01:00:00 --constraint cpu
20+
OMP_NUM_THREADS=64 srun -n 4 -c 64 --cpu-bind=cores python tenki/point_sources/dory.py find \
21+
maps/spt/full_095ghz_CAR.fits \
22+
maps/spt/ivar_095ghz_CAR.fits \
23+
maps/catalogs \
24+
--freq 90 \
25+
--beam /global/cfs/cdirs/sobs/users/tlouis/spt_data/ancillary_products/generally_applicable/beam_c26_cmb_bl_095ghz.txt \
26+
-R "tile:3000" \
27+
--pad 60 \
28+
-m maps/full/pixel_mask_binary_borders_only_CAR_REV.fits \
29+
--nsigma 3 \
30+
--output "full,reg,debug" \
31+
--verbose
32+
33+
You now have a catalog of sources at ``maps/catalogs/cat.{txt,fits}``.
34+
A first try to make masks with it is in ``make_source_mask.py``:
35+
.. code:: shell
36+
salloc --nodes 1 --qos interactive --time 00:05:00 --constraint cpu
37+
OMP_NUM_THREADS=256 srun -n 1 -c 256 --cpu-bind=cores python mask/make_source_mask.py
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"""
2+
This script projects SPT and ACT maps and masks into a CAR template defined around SPT survey
3+
"""
4+
5+
from pixell import enplot, enmap, reproject, utils
6+
import numpy as np
7+
from pspy import so_map
8+
from os.path import join as opj
9+
import os
10+
11+
save_dir = ""
12+
spt_maps_dir = opj(save_dir, "maps/spt")
13+
spt_mask_dir = opj(save_dir, "masks")
14+
15+
os.makedirs(spt_maps_dir, exist_ok=True)
16+
os.makedirs(spt_mask_dir, exist_ok=True)
17+
18+
### Define project geometry (arbitrary box around SPT patch + few degrees)
19+
20+
print("create SPT CAR geometry")
21+
box = [[-77, -62],[-35,60]] * utils.adeg
22+
shape,wcs = enmap.geometry2(pos=box,res=0.5 * utils.arcmin,proj='car', variant='fejer1')
23+
print(f"Project on geometry: {shape=}, {wcs=}")
24+
template = enmap.ones(shape, wcs)
25+
enmap.write_map(opj(save_dir, "SPT_CAR_template.fits"), template)
26+
27+
28+
### Project SPT full maps
29+
30+
spt_maps_read_dir = "/global/cfs/cdirs/sobs/users/tlouis/spt_data/real_data_maps"
31+
spt_full_maps_fn_list = [opj(spt_maps_read_dir, f"full/full_{freq}ghz.fits") for freq in ["095"]]
32+
33+
for spt_maps_fn in spt_full_maps_fn_list:
34+
# Project maps
35+
maps_so = so_map.read_map(spt_maps_fn, fields_healpix=(0, 1, 2))
36+
maps = maps_so.data
37+
38+
maps_proj = reproject.healpix2map(maps, shape, wcs, method='harm') # For maps we use harm
39+
40+
enmap.write_map(opj(spt_maps_dir, os.path.basename(spt_maps_fn)[:-5] + "_CAR.fits"), maps_proj)
41+
42+
plot = enplot.get_plots(
43+
maps_proj, ticks=10, mask=0, downgrade=2, colorbar=True, range=(100, 30, 30)
44+
)
45+
enplot.write(opj(spt_maps_dir, os.path.basename(spt_maps_fn)[:-5] + "_CAR"), plot)
46+
47+
# We also need ivar, we use a fudge factor to get close to the real ivar: here I use 1e-2 which corresponds to 5uk.arcmin depth
48+
fudge_ivar = 1e-2
49+
maps_so = so_map.read_map(spt_maps_fn, fields_healpix=(3))
50+
maps = maps_so.data * fudge_ivar
51+
52+
maps_proj = reproject.healpix2map(maps, shape, wcs, method='harm') # For maps we use harm
53+
54+
enmap.write_map(opj(spt_maps_dir, os.path.basename(spt_maps_fn)[:-5].replace("full", "ivar") + "_CAR.fits"), maps_proj)
55+
56+
plot = enplot.get_plots(
57+
maps_proj, ticks=10, mask=0, downgrade=2, colorbar=True, range=(1)
58+
)
59+
enplot.write(opj(spt_maps_dir, os.path.basename(spt_maps_fn)[:-5].replace("full", "ivar") + "_CAR"), plot)
60+
61+
62+
spt_masks_read_dir = "/global/cfs/cdirs/sobs/users/tlouis/spt_data/ancillary_products/generally_applicable"
63+
spt_masks_fn_list = [
64+
opj(spt_masks_read_dir, "pixel_mask_apodized_borders_objects.fits"),
65+
opj(spt_masks_read_dir, "pixel_mask_apodized_borders_only.fits"),
66+
opj(spt_masks_read_dir, "pixel_mask_binary_borders_objects.fits"),
67+
opj(spt_masks_read_dir, "pixel_mask_binary_borders_only.fits"),
68+
]
69+
70+
for spt_mask_fn in spt_masks_fn_list:
71+
# Project masks
72+
maps_so = so_map.read_map(spt_mask_fn)
73+
maps = maps_so.data
74+
75+
maps_proj = reproject.healpix2map(maps, shape, wcs, method='spline') # For masks we use spline
76+
77+
enmap.write_map(opj(spt_mask_dir, os.path.basename(spt_mask_fn)[:-5] + "_CAR.fits"), maps_proj)
78+
79+
plot = enplot.get_plots(
80+
maps_proj, ticks=10, mask=0, downgrade=2, colorbar=True, range=1
81+
)
82+
enplot.write(opj(spt_mask_dir, os.path.basename(spt_mask_fn)[:-5] + "_CAR"), plot)
83+
84+
# We also need reverted masks for dory
85+
enmap.write_map(opj(spt_mask_dir, os.path.basename(spt_mask_fn)[:-5] + "_CAR_rev.fits"), 1 - maps_proj)
86+
87+
plot = enplot.get_plots(
88+
1 - maps_proj, ticks=10, mask=0, downgrade=2, colorbar=True, range=1
89+
)
90+
enplot.write(opj(spt_mask_dir, os.path.basename(spt_mask_fn)[:-5] + "_CAR_rev"), plot)
91+
92+
93+

0 commit comments

Comments
 (0)