-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgnilc_weights.py
More file actions
152 lines (98 loc) · 4.99 KB
/
gnilc_weights.py
File metadata and controls
152 lines (98 loc) · 4.99 KB
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
import numpy as np
import healpy as hp
from astropy.io import fits as pyfits
import subprocess
import ConfigParser
import os
import gnilc_auxiliary
import misc_functions
import pdb
import time
############################################################################
############################################################################
start_time = time.time()
##### Read parameters.ini
Config = ConfigParser.ConfigParser()
initial_file = "parameters_weights.ini"
##### Read Inputs
ensemble_maps = misc_functions.ConfigSectionMap(Config, initial_file, "General")['ensemble_maps']
bandcenters = misc_functions.ConfigSectionMap(Config, initial_file, "General")['bandcenters']
output_suffix = misc_functions.ConfigSectionMap(Config, initial_file, "General")['output_suffix']
bandcenters_list = misc_functions.getlist(bandcenters)
n_bandcenters = len(bandcenters_list)
bandcenters = np.zeros(n_bandcenters, dtype=np.int32)
for i in range(0, n_bandcenters):
bandcenters[i] = long(bandcenters_list[i])
### Create output directory
path_in = os.path.realpath(__file__)
directory_in = os.path.dirname(path_in)
directory_out = directory_in + '/output_weights'
if not os.path.exists(directory_out):
os.makedirs(directory_out)
##### Maps: info
maps = pyfits.getdata('input/' + ensemble_maps + '.fits') # read input maps
nf = maps[:, 0].size
nside = hp.pixelfunc.npix2nside((maps[0, :]).size)
lmax = 3 * nside - 1
##### Needlet band-pass windows
print "Creating wavelet bands."
# Cosine bands -- later add Gaussian as well
bands = gnilc_auxiliary.cosine_ell_bands(bandcenters)
nbands = (bands[0, :]).size # number of bands
lmax_bands = np.zeros(nbands, dtype=np.int32) # bands effective ell max
for j in range(0, nbands):
lmax_bands[j] = max(np.where(bands[:, j] != 0.0)[0])
############################################################################
############################################################################
##### Channel maps: SHT and wavelet transform
print "Applying wavelets to the observed maps."
relevant_band_max = np.zeros(nf, dtype=np.int32)
for i in range(0, nf):
alm_map = hp.sphtfunc.map2alm(maps[i, :], lmax=lmax)
# Wavelet transform channel maps: band-pass filtering in (l,m)-space and transform back to real (pixel) space
# relevant bands for each channel map
if lmax <= max(lmax_bands):
relevant_band_max[i] = min(np.where(lmax_bands[:] >= lmax)[0])
else:
relevant_band_max[i] = nbands - 1
if lmax_bands[relevant_band_max[i]] == max(lmax_bands):
relevant_band_max[i] = nbands - 1
gnilc_auxiliary.alm2wavelets(alm_map, bands[:, 0:relevant_band_max[i] + 1], nside, 'wavelet_ensemble_' + str(i).strip() + '.fits', nside)
maps = 0
alm_map = 0
############################################################################ ############################################################################
##### Apply GNILC weights to the ensemble wavelet maps
print "Applying the GNILC weights to the ensemble wavelet maps."
for i in range(0, nf):
pyfits.append('wavelet_gnilc_ensemble_' + str(i).strip() + '.fits', bands[:, 0:relevant_band_max[i] + 1])
for j in range(0, nbands): # loop over needlet bands
w_target = pyfits.getdata('output/' + 'ilc_weights_' + output_suffix + "_" + str(j).strip() + '.fits')
for i in range(0, nf): # loop over frequency channels
needlet_ilc_r = 0.
for k in range(0, nf): # loop over frequency channels
tot_needlet = pyfits.getdata('wavelet_ensemble_' + str(k).strip() + '.fits', j + 1)
w_map_r = w_target[:, i, k]
# apply the ILC weight matrix to the channel wavelet maps
needlet_ilc_r = needlet_ilc_r + w_map_r * tot_needlet # ILC filtering
pyfits.append('wavelet_gnilc_ensemble_' + str(i).strip() + '.fits', needlet_ilc_r)
w_target = 0; w_map_r = 0
############################################################################ ############################################################################
##### Synthesize GNILC wavelet maps to GNILC maps
ilc_map = np.zeros((nf, hp.pixelfunc.nside2npix(nside)))
for i in range(0, nf):
ilc_map[i, :] = gnilc_auxiliary.wavelets2map('wavelet_gnilc_ensemble_' + str(i).strip() + '.fits', nside)
############################################################################ ############################################################################
##### Produce GNILC maps
print "Producing the GNILC maps."
maps_out = np.zeros((nf, hp.pixelfunc.nside2npix(nside)))
for i in range(0, nf):
# GNILC maps (fits file)
maps_out[i, :] = ilc_map[i, :]# * galmask
pyfits.writeto('output_weights/ensemble_gnilc_maps_' + output_suffix + '.fits', maps_out, overwrite = True)
##### Clean and save things
for i in range(0, nf):
file = 'wavelet_ensemble_' + str(i).strip() + '.fits'
subprocess.call(["rm", file])
file = 'wavelet_gnilc_ensemble_' + str(i).strip() + '.fits'
subprocess.call(["rm", file])
print time.time() - start_time