/
reader.py
184 lines (155 loc) · 6.11 KB
/
reader.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from glob import glob
from ConfigParser import SafeConfigParser
import exceptions
import logging as log
import os.path
import numpy as np
import healpy as hp
stokes_IQU = "IQUHABCDEF"
stokes_I = "IHA"
# H for hits,
# ABCDEF 6 components of VARIANCE matrix
# II, IQ, IU, QQ, QU, UU
def get_filename(filename_pattern):
for pattern in [filename_pattern, filename_pattern.replace("_full","")]:
filename = glob(pattern)
if len(filename) == 1:
filename = filename[0]
log.debug("File: " + filename)
return filename
if len(filename) == 0:
error_log = "No match for pattern " + filename_pattern
else:
error_log = "Multiple matches for pattern " + filename_pattern
log.fatal(error_log)
raise exceptions.IOError(error_log)
def arcmin2rad(arcmin):
return np.radians(arcmin/60.)
FWHM = { 30:arcmin2rad(33.15873215), 44:arcmin2rad(28.08523439), 70:arcmin2rad(13.08124258)}
def type_of_channel_set(ch):
"""Returns a string that identifies the set of channels"""
if ch == "":
return "frequency"
elif ch.find('_') >= 0:
return "detset"
elif len(ch) == 5: # this works for lfi only
return "horn"
else:
return "channel"
class BaseMapReader:
"""Abstract class, all readers should provide this
interface"""
def __call__(self, freq, surv, chtag='', nside=None, halfring=0, pol="I"):
"""See docstrings of the child classes"""
return np.zeros(hp.nside2npix(1024))
class DXReader(BaseMapReader):
"""All maps in a single folder, DX9 naming convention"""
def __init__(self, config_filename, nside=None, debug=False):
"""
nside : None or int
if None matches any nside, otherwise integer nside
"""
self.config = SafeConfigParser(); self.config.read(config_filename)
self.nside = nside
self.debug = debug
def read_masks(self, freq):
result = []
filenames = [get_filename(self.config.get("Templates", mask_type).format(frequency=freq)) for mask_type in ["ps_mask", "spectra_mask"]]
for file_name in filenames:
result.append(np.logical_not(np.floor(hp.ud_grade(hp.read_map(file_name), self.nside)).astype(np.bool)))
return tuple(result)
def __call__(self, freq, surv, chtag='', halfring=0, pol="I", bp_corr=False):
"""Read a map and return the array of pixels.
Parameters
----------
freq : int
frequency
surv : int or string
"nominal", "full", or survey number
chtag : string
can be "" for frequency, radiometer("LFI18S"), horn("LFI18"), quadruplet("18_23"), detset("detset_1")
halfring : int
0 for full, 1 and 2 for first and second halfrings
pol : string
required polarization components, e.g. 'I', 'Q', 'IQU'
Returns
-------
maps : array or tuple of arrays
single map or tuple of maps as returned by healpy.read_map
"""
# type of channel
channel_type = type_of_channel_set(chtag)
# type of map
is_survey = isinstance(surv, int)
is_halfring = halfring != 0
# stokes component
stokes = stokes_IQU
if (channel_type in ["channel", "horn"]) or freq >= 545:
stokes = stokes_I
if isinstance(pol, str):
components = [stokes.index(p) for p in pol]
if len(components) == 1:
components = components[0]
else:
components = pol
if channel_type in ["channel", "horn"]:
if freq > 70:
freq = chtag
chtag = ""
else:
chtag = chtag.translate(None, "LFI") # remove LFI from channel name
# read_map
output_map = []
file_template_list = ["map", channel_type]
if is_survey:
file_template_list.append("survey")
if is_halfring:
file_template_list.append("halfring")
file_template = "_".join(file_template_list)
if channel_type == "horn":
# horn maps created summing channel maps
file_template = file_template.replace("horn", "channel")
if freq > 70:
tags = [chtag+'a', chtag+'b']
else:
tags = [chtag+'M', chtag+'S']
else:
tags = [chtag]
file_parameters = {"frequency":freq, "survey":surv}
if is_halfring:
file_parameters["halfring"] = halfring
for tag in tags:
filename_pattern = self.config.get("Templates", file_template).format(channel=tag, **file_parameters)
filename = get_filename(filename_pattern)
log.info("components %s" % (str(components)))
if not self.debug:
output_map.append(hp.ma(hp.read_map(filename, components)))
else:
output_map.append(
np.zeros((np.size(components), hp.nside2npix(1024)))
)
if bp_corr:
bp_corr_file_template = "map_iqucorrection"
if is_survey:
bp_corr_file_template += "_survey"
bp_corr_filename_pattern = self.config.get("Templates", bp_corr_file_template).format(frequency=freq, survey=surv)
bp_corr_filename = get_filename(bp_corr_filename_pattern)
if not self.debug:
corr_map = hp.ma(hp.read_map(bp_corr_filename, (0,1,2)))
else:
corr_map = [np.zeros(hp.nside2npix(1024)) for c in [0,1,2]]
for comp, corr in zip(output_map[0], corr_map):
comp += corr
if channel_type == "horn":
log.info("Combining maps in horn map")
out = .5 * (output_map[0] + output_map[1])
else:
out = output_map[0]
if self.nside:
log.info("Downgrading to nside %d" % self.nside)
power = None
if pol in "ADF":
log.info("Downgrading a covariance matrix")
power = 2
out = hp.ud_grade(out, self.nside,power=power)
return out