from scipy.optimize import curve_fit
from typing import Tuple, List, Union, Optional
import matplotlib.pyplot as pl
me = casatools.measures()
def __init__(self, binnedvis: str, doplot: bool = False) -> None:
def populate_grid(self, uvw: np.array, stokesI: np.array, uvgrid: np.array, uvgrid_npt: np.array, deltau:float , deltav: float, npix: int):
for ii in range(len(uvw[0])):
uidx = int(np.round(uvw[0][ii]//deltau + npix//2))
vidx = int(np.round(uvw[1][ii]//deltav + npix//2))
uvgrid[uidx, vidx] += stokesI[ii]
uvgrid_npt[uidx, vidx] += 1
return uvgrid, uvgrid_npt
def mad(self, inpdat: np.array) -> float :
Calculate the STD via MAD for the input data
std Calculate the std via mad
mad = np.median(np.abs(inpdat - med))
def msuvbin_to_uvgrid(self, ms: str, npix: int, deltau: float, deltav: float ) -> Tuple[np.array, np.array]:
umin, umax = np.min(uvw[0]), np.max(uvw[0])
vmin, vmax = np.min(uvw[1]), np.max(uvw[1])
wmin, wmax = np.min(uvw[2]), np.max(uvw[2])
uvals = np.linspace(umin, umax, npix)
vvals = np.linspace(vmin, vmax, npix)
uvgrid = np.zeros((npix, npix), dtype=np.complex128)
uvgrid_npt = np.zeros((npix, npix), dtype=int)
stokesI = 0.5*(data[0] + data[1])
stokesI = np.squeeze(stokesI)
uvgrid, uvgrid_npt = self.populate_grid(uvw, stokesI, uvgrid, uvgrid_npt, deltau, deltav, npix)
idx = np.where(uvgrid_npt != 0)
uvgrid[idx] = uvgrid[idx]/uvgrid_npt[idx]
return uvgrid, uvgrid_npt
def resid_cube(self, x: float, a: float, b: float , c: float , d: float) -> float:
return a*x**3 + b*x**2 + c*x + d
def resid_cinco(self, x: np.ndarray , a: float, b: float, c: float , d: float, e: float, f: float) -> np.ndarray:
return a*x**5 + b*x**4 + c*x**3 + d*x**2+ e*x +f
def fit_radial_profile(self, xvals: np.ndarray, yvals: np.ndarray, ystd: np.ndarray, deg: int =3, clip_sigma: float =3) -> np.ndarray:
Fit the radial profile with a polynomial
print(f"deg {deg}, clip_sigma {clip_sigma}")
print(f"xvals shape {xvals.shape}, yvals shape {yvals.shape}")
print(f"non-zero x {np.count_nonzero(xvals)}, non-zero y {np.count_nonzero(yvals)}")