-
Notifications
You must be signed in to change notification settings - Fork 0
/
view_map.py
executable file
·162 lines (140 loc) · 7.1 KB
/
view_map.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python
"""Visualize sky maps in hdf5 files.
:Authors: Shifan Zuo
:Date: 2014-04-02
:email: sfzuo@bao.ac.cn
:usage:
python view_map.py [-h] [-o [OUTFILE]] [-f FIGFMT] [-i IFREQ] [-p {0,1,2,3}] [--min MIN] [--max MAX] [-l FIGLENGTH] [-w FIGWIDTH] [-g] mapfiles [mapfiles ...]
"""
import argparse
def visualize_map(args):
"""Visualize sky maps in hdf5 files.
Arguments
---------
args : argparse namespace.
"""
import numpy as np
import h5py
import healpy
# import hpvisual
import matplotlib
matplotlib.use('Agg')
try:
# for new version matplotlib
import matplotlib.style as mstyle
mstyle.use('classic')
except ImportError:
pass
from matplotlib import pyplot as plt
# Read in maps data
hpmap = None
for mapname in args.mapfiles:
with h5py.File(mapname, 'r') as f:
if hpmap is None:
hpmap = f['map'][:]
else:
hpmap += f['map'][:]
# Check args validity
if args.ifreq < -(hpmap.shape)[0] or args.ifreq >= (hpmap.shape)[0]:
raise Exception('Invalid frequency channel %d, should be in range(-%d, %d).'%(args.ifreq, (hpmap.shape)[0], (hpmap.shape)[0]))
else:
ifreq = args.ifreq if args.ifreq >= 0 else args.ifreq + (hpmap.shape)[0]
if args.pol >= (hpmap.shape)[1]:
raise Exception('Invalid frequency channel %d, should be in range(0, %d).'%(args.pol, (hpmap.shape)[1]))
if args.figlength <= 0:
raise Exception('Figure length figlength (= %f) must greater than 0'%args.figlength)
if args.figwidth <= 0:
raise Exception('Figure width figwidth (= %f) must greater than 0'%args.figwidth)
# Create output image file name
if args.outfile:
out_file = args.outfile
else:
out_file = ((args.mapfiles[0].split('/')[-1]).split('.')[0] + '_' + str(ifreq) + '_{' + str(args.pol) + '}' + '.' + args.figfmt).format('T', 'Q', 'U', 'V')
# Plot and save image
if args.view == 'o':
fig = plt.figure(1, figsize=(8, 6))
else:
fig = plt.figure(1, figsize=(args.figlength,args.figwidth))
map_data = hpmap[ifreq][args.pol]
if args.sqrt:
map_data = map_data / np.sqrt(np.abs(map_data))
map_data = map_data / np.sqrt(np.abs(map_data))
# map_data = map_data / np.sqrt(np.abs(map_data))
# smoothing the map with a Gaussian symmetric beam
if args.fwhm is not None:
fwhm = np.radians(args.fwhm)
map_data = healpy.smoothing(map_data, fwhm=fwhm)
if args.view == 'm':
# set color map
if args.cmap is None:
cmap = None
else:
if args.cmap == 'jet09':
import colormap
cmap = colormap.jet09
else:
from pylab import cm
# cmap = cm.hot
cmap = getattr(cm, args.cmap)
cmap.set_under('w')
if args.abs:
healpy.mollview(np.abs(map_data), fig=1, title='', unit=args.unit, cmap=cmap, min=args.min, max=args.max)
else:
healpy.mollview(map_data, fig=1, title='', unit=args.unit, cmap=cmap, min=args.min, max=args.max)
# plot NVSS sources
if args.nvss is not None:
import aipy as a
flux = args.nvss
frequency = 750 # MHz
catalog = 'nvss'
# catalog = 'wenss'
src = '%f/%f' % (flux, frequency / 1.0e3)
srclist, cutoff, catalogs = a.scripting.parse_srcs(src, catalog)
cat = a.src.get_catalog(srclist, cutoff, catalogs)
nsrc = len(cat) # number of sources in cat
ras = [ np.degrees(cat.values()[i]._ra) for i in range(nsrc) ]
decs = [ np.degrees(cat.values()[i]._dec) for i in range(nsrc) ]
jys = [ cat.values()[i].get_jys() for i in range(nsrc) ]
# select sources
inds = np.where(np.array(decs)>-15.0)[0]
ras = np.array(ras)[inds]
decs = np.array(decs)[inds]
jys = np.array(jys)[inds]
# healpy.projscatter(ras, decs, lonlat=True, s=jys, facecolors='none', edgecolors='w', alpha=1.0, linewidth=1.0)
healpy.projscatter(ras, decs, lonlat=True, s=150, facecolors='none', edgecolors='w', alpha=1.0, linewidth=1.0)
elif args.view == 'c':
healpy.cartview(map_data, fig=1, title='', unit=args.unit, min=args.min, max=args.max)
elif args.view == 'o':
healpy.orthview(map_data, rot=(0, 90, 0), fig=1, title='', unit=args.unit, min=args.min, max=args.max, half_sky=True) # rot to make NCP at the center
# fig = plt.figure()
# ax = fig.add_axes()
# cbar.solids.set_rasterized(True)
if args.grid:
healpy.graticule()
if args.tight:
fig.savefig(out_file, bbox_inches='tight')
else:
fig.savefig(out_file)
fig.clf()
parser = argparse.ArgumentParser(description='Visualize a sky map in hdf5 files.')
parser.add_argument('mapfiles', type=str, nargs='+', help='Input hdf5 sky map files to visualize. If more than one, they will be first combined, i.e. added together.')
parser.add_argument('-o', '--outfile', type=str, nargs='?', help='Name of the image file to save into. If not present, the output image file name will be auto created from the first input map file name and input args.')
parser.add_argument('-f', '--figfmt', default='png', help='Output image format.')
parser.add_argument('-v', '--view', type=str, choices=['m', 'c', 'o'], default='m', help='Which view, `m` for mollview, `c` for cartview, `o` for orthview.')
parser.add_argument('-s', '--sqrt', action='store_true', help='Plot by sqrt of the map.')
parser.add_argument('--fwhm', type=float, default=None, help='Smoothing the map with a Gaussian symmetric beam with FWHM this value, in degree.')
parser.add_argument('-a', '--abs', action='store_true', help='Plot the abs value of the map.')
parser.add_argument('-n', '--nvss', type=float, default=None, help='Plot NVSS sources above this value, in Jy.')
parser.add_argument('-c', '--cmap', type=str, default=None, help='The cmap to use.')
parser.add_argument('-i', '--ifreq', type=int, default=0, help='Frequency channel to visualize (start from 0). Negative integer N means the last Nth channel.')
parser.add_argument('-p', '--pol', type=int, default=0, choices=range(4), help='Polarization component to visualize, 0 for I/T, 1 for Q, 2 for U, 3 for V.')
parser.add_argument('--min', type=float, help='The min value of the visualize range in the output image.')
parser.add_argument('--max', type=float, help='The max value of the visualize range in the output image.')
parser.add_argument('-u', '--unit', type=str, default='', help='A text describing the unit of the data.')
parser.add_argument('-l', '--figlength', type=float, default=13, help='Output figure length.')
parser.add_argument('-w', '--figwidth', type=float, default=5, help='Output figure width.')
parser.add_argument('-g', '--grid', action='store_false', help='Add meridians and parallels.')
parser.add_argument('-t', '--tight', action='store_true', help='Tight the figure marin space.')
parser.set_defaults(func=visualize_map)
args = parser.parse_args()
args.func(args)