-
Notifications
You must be signed in to change notification settings - Fork 0
/
primary_beam_fit.py
128 lines (97 loc) · 4.73 KB
/
primary_beam_fit.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
import numpy as np
import matplotlib.pyplot as plt
from pyuvdata import UVData
import healpy as hp
from glob import glob
import hera_cal
import copy
from direct_optimal_mapping import data_conditioning, pixel_selection
from direct_optimal_mapping import optimal_mapping_radec_grid
import beam_functions as func
from scipy import optimize
import hera_cal
import pandas as pd
from astropy import constants
ra_ctr_deg = 30
ra_rng_deg = 20
bl_max = 300 # m
src_ra_deg = 30.3
src_dec_deg = -30.8
ipol = -5
data_files = np.array(sorted(glob('/nfs/esc/hera/H6C/IDR2/2459873/zen.2459873.*.sum.omni_vis.uvh5')))
jds = np.array([float(data_file.split('/')[-1][4:17]) for data_file in data_files])
# print(data_files)
lsts = hera_cal.utils.JD2LST(jds)
idx = np.where((lsts > np.radians(ra_ctr_deg - ra_rng_deg/2.)) & (lsts < np.radians(ra_ctr_deg + ra_rng_deg/2.)))[0]
print('%d files selected.'%len(idx))
for ifreq in range(1450, 100, -50):
z_pointing = []
fit_result = []
for data_file in data_files[idx]:
print('File:', data_file)
uv = UVData()
uv.read(data_file)
time_arr = np.unique(uv.time_array)
uv.unphase_to_drift()
uv.phase_type = 'drift'
for time_idx in range(len(time_arr)):
uv_sel = uv.select(times=time_arr[time_idx], inplace=False, keep_all_metadata=False)
print('################\nSelected data shape:', uv_sel.data_array.shape)
lst_ze = np.unique(uv_sel.lst_array)[0]
print('Zenith pointing:', np.degrees(lst_ze))
dc = data_conditioning.DataConditioning(uv_sel, ifreq, ipol)
print('Frequency: %.2fMHz'%(dc.uv_1d.freq_array[0]/1e6))
dc.noise_calc()
if dc.rm_flag() is None:
print('All data are flagged')
continue
dc.redundant_avg()
print('Data array shape after data conditioning:', dc.uv_1d.data_array.shape)
print('Noise array shape after data conditioning:', dc.uvn.data_array.shape)
syn_beam = np.degrees(constants.c.value/dc.uv_1d.freq_array[0][0]/bl_max)
# Pixels Setup
ra_ctr_deg = src_ra_deg
ra_rng_deg = syn_beam * 5
n_ra = 40
dec_ctr_deg = src_dec_deg
dec_rng_deg = syn_beam * 5
n_dec = 40
sky_px = optimal_mapping_radec_grid.SkyPx()
px_dic = sky_px.calc_radec_pix(ra_ctr_deg, ra_rng_deg, n_ra, dec_ctr_deg, dec_rng_deg, n_dec)
shape = px_dic['sa_sr'].shape
# Optimal Mapping & A matrix
opt_map = optimal_mapping_radec_grid.OptMapping(dc.uv_1d, px_dic, epoch='Current')
opt_map.set_a_mat()
inv_noise_mat = opt_map.set_inv_noise_mat(dc.uvn, norm=True)
print('a_mat shape is:', opt_map.a_mat.shape)
map_vis = np.matmul(np.conjugate(opt_map.a_mat.T),
np.matmul(opt_map.inv_noise_mat,
np.matrix(opt_map.data)))
# map_vis = np.real(map_vis)
map_vis = np.array(map_vis.real.reshape(px_dic['px_id'].shape))#/px_dic['sa_sr']
weight = np.matmul(np.conjugate(opt_map.beam_mat.T), np.diag(opt_map.inv_noise_mat)).reshape(px_dic['px_id'].shape)
weight_sq = np.matmul(np.conjugate((opt_map.beam_mat**2).T), np.diag(opt_map.inv_noise_mat)).reshape(px_dic['px_id'].shape)
map_norm = map_vis/weight
xieta = np.radians(list(zip(px_dic['ra_deg'].flatten(), px_dic['dec_deg'].flatten()))).T
# initial guesses then fitting
p0 = [15,
np.radians(src_ra_deg),
np.radians(src_dec_deg),
np.radians(1.2*syn_beam),
np.radians(0.8*syn_beam),
np.radians(0)]
bounds = ([0, np.radians(src_ra_deg-1), np.radians(src_dec_deg-1), np.radians(0.6 * syn_beam), np.radians(0.4 * syn_beam), -np.pi],
[1000, np.radians(src_ra_deg+1), np.radians(src_dec_deg+1), np.radians(2.4 * syn_beam), np.radians(1.6 * syn_beam), np.pi],)
try:
popt, pcov = optimize.curve_fit(func.gaussian2d, xieta, map_norm.flatten(), p0=p0, bounds=bounds)
except:
print('Fitting Failed.')
continue
print(popt)
z_pointing.append(lst_ze)
fit_result.append(popt)
z_pointing = np.array(z_pointing)
fit_result = np.array(fit_result)
result = np.concatenate((z_pointing[:, np.newaxis], fit_result), axis=1)
df = pd.DataFrame(data=result, columns=['Pointing', 'Amp', 'x0', 'y0', 'fwhm_major', 'fwhm_minor', 'fwhm_theta'])
df.to_csv('data/primary_beam_%06.2fMHz.csv'%(uv.freq_array[0, ifreq]/1e6), index=False)