-
Notifications
You must be signed in to change notification settings - Fork 0
/
h1c_idr22_mapping_radec_grid_data_OCRSLP2X.py
217 lines (191 loc) · 7.62 KB
/
h1c_idr22_mapping_radec_grid_data_OCRSLP2X.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
from glob import glob
import numpy as np
from astropy.time import Time
from astropy.io import fits
from astropy.table import Table
import time
from astropy import constants as const
import healpy as hp
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import multiprocessing
import copy
import pickle
from itertools import product
from pyuvdata import UVData
import os
from pygdsm import GlobalSkyModel2016
from direct_optimal_mapping import optimal_mapping_radec_grid, data_conditioning
RAD2HR = 12/np.pi
HR2DEG = 15
data_type = 'h1c_idr22' # 'validation', 'h1c_idr32'
suffix = "OCRSLP2X"
field = 'field1'
band = 'band2'
split = 'odd'
ipol_arr = [-5, -6]
sequence = 'forward'
nthread = 15
OUTPUT_FOLDER = '/nfs/esc/hera/zhileixu/optimal_mapping/h1c_idr22/radec_grid/%s/%s/%s'%(field, band, split)
OVERWRITE = False
# Fields
# field 1: 1.25 -- 2.7 hr
if field == 'field1':
lst_ends = np.array([1.25, 2.7]) / RAD2HR
# field 2: 4.5 -- 6.5 hr
elif field == 'field2':
lst_ends = np.array([4.5, 6.5]) / RAD2HR
# field 3: 8.5 -- 10.75 hr
elif field == 'field3':
lst_ends = np.array([8.5, 10.75]) / RAD2HR
else:
print('Wrong field is given')
ra_center_deg = np.degrees(np.mean(lst_ends)) # 29.6 for idr22 3:8, 24.2 for idr22 0:9
dec_center_deg = -30.7
ra_rng_deg = 45
n_ra = 90
dec_rng_deg = 16
n_dec = 32
sky_px = optimal_mapping_radec_grid.SkyPx()
px_dic = sky_px.calc_radec_pix(ra_center_deg, ra_rng_deg, n_ra,
dec_center_deg, dec_rng_deg, n_dec)
print('Data type:', data_type)
print('Mapping para.:', sequence, field, band, split, ipol_arr)
print('Pixelization: %.1fdeg X %.1fdeg (%d X %d)'%(ra_rng_deg, dec_rng_deg, n_ra, n_dec), ', centering at (%.2fdeg, %.2fdeg).'%(ra_center_deg, dec_center_deg))
print('overwrite:', OVERWRITE)
print('Number of threads:', nthread)
print(OUTPUT_FOLDER)
def radec_map_making(files, ifreq, ipol,
p_mat_calc=True,
select_ant=False):
t0 = time.time()
uv_org = UVData()
uv_org.read(files, freq_chans=ifreq, polarizations=ipol)
# print('Start lst array:', np.unique(uv_org.lst_array) * RAD2HR)
uv_org.select(lst_range=lst_ends, inplace=True)
# print('Selected LSTs (hr):', np.unique(uv_org.lst_array) * RAD2HR)
if select_ant:
ant_sel = np.array([ 1, 12, 13, 14, 23, 25, 26, 27, 36, 37, 38, 39, 40,
41, 51, 52, 55, 65, 66, 68, 70, 71, 82, 83, 84, 85,
86, 87, 88, 120, 121, 123, 124, 137, 138, 140, 141, 142, 143])
uv_org.select(antenna_nums=ant_sel, inplace=True, keep_all_metadata=False)
start_flag = True
time_arr = np.unique(uv_org.time_array)[:]
freq = uv_org.freq_array[0, 0]
if split == 'even':
time_arr_sel = time_arr[::2]
elif split == 'odd':
time_arr_sel = time_arr[1::2]
for time_t in time_arr_sel:
#print(itime, time_t, end=';')
uv = uv_org.select(times=[time_t,], keep_all_metadata=False, inplace=False)
# Data Conditioning
dc = data_conditioning.DataConditioning(uv, 0, ipol)
dc.bl_selection()
dc.noise_calc()
n_vis = dc.uv_1d.data_array.shape[0]
if dc.rm_flag() is None:
#print('All flagged. Passed.')
continue
dc.redundant_avg()
bl_max = np.sqrt(np.sum(dc.uv_1d.uvw_array**2, axis=1)).max()
radius2ctr = np.radians(hp.rotator.angdist(np.array([px_dic['ra_deg'].flatten(), px_dic['dec_deg'].flatten()]),
[np.mean(px_dic['ra_deg']), np.mean(px_dic['dec_deg'])], lonlat=True))
radius2ctr = radius2ctr.reshape(px_dic['ra_deg'].shape)
opt_map = optimal_mapping_radec_grid.OptMapping(dc.uv_1d, px_dic)
file_name = OUTPUT_FOLDER+\
'/h1c_idr22_%_%s_%s_%.2fMHz_pol%d_radec_grid_RA%dDec%d.p'%(suffix, field, split, freq/1e6, ipol,
ra_rng_deg, dec_rng_deg)
if OVERWRITE == False:
if os.path.exists(file_name):
print(file_name, 'existed, return.')
return
opt_map.set_a_mat(uvw_sign=1)
opt_map.set_inv_noise_mat(dc.uvn, norm=True)
map_vis = np.matmul(np.conjugate(opt_map.a_mat.T),
np.matmul(opt_map.inv_noise_mat,
opt_map.data))
map_vis = np.real(map_vis)
beam_weight = np.matmul(np.conjugate((opt_map.beam_mat).T),
np.diag(opt_map.inv_noise_mat),)
beam_sq_weight = np.matmul(np.conjugate((opt_map.beam_mat**2).T),
np.diag(opt_map.inv_noise_mat),)
if p_mat_calc:
opt_map.set_p_mat()
else:
opt_map.p_mat = np.nan
if start_flag:
map_sum = copy.deepcopy(map_vis)
beam_weight_sum = copy.deepcopy(beam_weight)
beam_sq_weight_sum = copy.deepcopy(beam_sq_weight)
p_sum = copy.deepcopy(opt_map.p_mat)
start_flag=False
else:
map_sum += map_vis
beam_weight_sum += beam_weight
beam_sq_weight_sum += beam_sq_weight
p_sum += opt_map.p_mat
if start_flag == True:
print(f'ifreq:{ifreq} no unflagged data available.')
return
result_dic = {'px_dic':px_dic,
'map_sum':map_sum,
'beam_weight_sum':beam_weight_sum,
'beam_sq_weight_sum':beam_sq_weight_sum,
'n_vis': n_vis,
'p_sum': p_sum,
'freq': freq,
'bl_max':bl_max,
'radius2ctr': radius2ctr,
}
with open(file_name, 'wb') as f_t:
pickle.dump(result_dic, f_t, protocol=4)
print(f'ifreq:{ifreq} finished in {time.time() - t0} seconds.')
return
if __name__ == '__main__':
#H1C part
if suffix == "OCRSLP2X":
data_folder = '/nfs/esc/hera/H1C_IDR22/IDR2_2_pspec/v2/one_group/data'
files = np.array(sorted(glob(data_folder+'/zen.grp1.of1.LST.*.HH.%s.uvh5'%suffix)))
if field == 'field1':
files = files[3:8]
elif field == 'field2':
files = files[12:18]
elif field == 'field3':
files = files[23:34]
else:
print('Wrong field is given.')
elif suffix == "OCRSLP2XTK":
data_folder = '/nfs/esc/hera/H1C_IDR22/IDR2_2_pspec/v2/one_group/data'
files = np.array(sorted(glob(data_folder+'/zen.grp1.of1.LST.*.HH.%s.uvh5'%suffix)))
if field == 'field1':
files = files[1:3]
elif field == 'field2':
files = files[3:6]
elif field == 'field3':
files = files[7:10]
else:
print('Wrong field is given.')
else:
print("Wrong data suffix.")
print('%d Files being mapped:\n'%len(files), files)
if band == 'band1':
ifreq_arr = np.arange(175, 335, dtype=int) #band1
elif band == 'band2':
ifreq_arr = np.arange(515, 695, dtype=int) #band2
else:
raise RuntimeError('Wrong input for band.')
if sequence == 'forward':
args = product(np.expand_dims(files, axis=0), ifreq_arr[:], ipol_arr)
elif sequence == 'backward':
args = product(np.expand_dims(files, axis=0), ifreq_arr[::-1], ipol_arr)
else:
raise RuntimeError('Sequence should be either forward or backward.')
for args_t in args:
# print(args_t)
radec_map_making(*args_t)
pool = multiprocessing.Pool(processes=nthread)
pool.starmap(radec_map_making, args)
pool.close()
pool.join()