def spaxis(length, resolution=1.0, use_real=False, shift=False): try: length except NameError: raise NameError('Input length not defined. Aborting spaxis().') if not isinstance(resolution, (int, float)): raise TypeError('Input resolution must be a positive scalar integer or floating point number. Aborting spaxis().') elif resolution < 0.0: raise ValueError('Input resolution must be positive. Aborting spaxis().') return DSP.spectral_axis(length, delx=resolution, use_real=False, shift=False)
) + delaygain_err_str + 'Tsys_{0:.1f}K_{1}_{2:.1f}_MHz_'.format( Tsys, bandpass_str, freq / 1e6) + pfb_outstr + bpass_shape # infile = '/data3/t_nithyanandan/project_MWA/multi_baseline_visibilities_'+avg_drifts_str+obs_mode+'_baseline_range_{0:.1f}-{1:.1f}_'.format(bl_length[baseline_bin_indices[0]],bl_length[min(baseline_bin_indices[n_bl_chunks-1]+baseline_chunk_size-1,total_baselines-1)])+'gaussian_FG_model_'+fg_str+'_{0:0d}_'.format(nside)+'{0:.1f}_MHz'.format(nchan*freq_resolution/1e6) ia = RI.InterferometerArray(None, None, None, init_file=infile + '.fits') ia.phase_centering(phase_center=pc, phase_center_coords=pc_coords) ia.delay_transform(oversampling_factor - 1.0, freq_wts=window) delay_matrix = DLY.delay_envelope(ia.baselines, pc_dircos, units='mks') # lags = DSP.spectral_axis(ia.channels.size, delx=ia.freq_resolution, use_real=False, shift=True) # clean_area = NP.zeros(ia.channels.size, dtype=int) npad = ia.channels.size lags = DSP.spectral_axis(ia.channels.size + npad, delx=ia.freq_resolution, use_real=False, shift=False) clean_area = NP.zeros(ia.channels.size + npad, dtype=int) skyvis_lag = (npad + ia.channels.size) * ia.freq_resolution * DSP.FT1D( NP.pad(ia.skyvis_freq * ia.bp * ia.bp_wts, ((0, 0), (0, npad), (0, 0)), mode='constant'), ax=1, inverse=True, use_real=False, shift=False) vis_lag = (npad + ia.channels.size) * ia.freq_resolution * DSP.FT1D( NP.pad(ia.vis_freq * ia.bp * ia.bp_wts, ((0, 0), (0, npad), (0, 0)), mode='constant'), ax=1, inverse=True, use_real=False,
if n_sky_sectors == 1: sky_sector_str = '_all_sky_' else: sky_sector_str = '_sky_sector_{0:0d}_'.format(k) infile = rootdir+project_dir+telescope_str+'multi_baseline_visibilities_'+ground_plane_str+snapshot_type_str+obs_mode+duration_str+'_baseline_range_{0:.1f}-{1:.1f}_'.format(bl_length[baseline_bin_indices[0]],bl_length[min(baseline_bin_indices[n_bl_chunks-1]+baseline_chunk_size-1,total_baselines-1)])+fg_str+sky_sector_str+'sprms_{0:.1f}_'.format(spindex_rms)+spindex_seed_str+'nside_{0:0d}_'.format(nside)+delaygain_err_str+'Tsys_{0:.1f}K_{1}_{2:.1f}_MHz'.format(Tsys, bandpass_str, freq/1e6)+pfb_instr outfile = rootdir+project_dir+telescope_str+'multi_baseline_CLEAN_visibilities_'+ground_plane_str+snapshot_type_str+obs_mode+duration_str+'_baseline_range_{0:.1f}-{1:.1f}_'.format(bl_length[baseline_bin_indices[0]],bl_length[min(baseline_bin_indices[n_bl_chunks-1]+baseline_chunk_size-1,total_baselines-1)])+fg_str+sky_sector_str+'sprms_{0:.1f}_'.format(spindex_rms)+spindex_seed_str+'nside_{0:0d}_'.format(nside)+delaygain_err_str+'Tsys_{0:.1f}K_{1}_{2:.1f}_MHz_'.format(Tsys, bandpass_str, freq/1e6)+pfb_outstr+bpass_shape ia = RI.InterferometerArray(None, None, None, init_file=infile+'.fits') ia.phase_centering(phase_center=pc, phase_center_coords=pc_coords) ia.delay_transform(oversampling_factor-1.0, freq_wts=window) delay_matrix = DLY.delay_envelope(ia.baselines, pc_dircos, units='mks') npad = ia.channels.size lags = DSP.spectral_axis(ia.channels.size + npad, delx=ia.freq_resolution, use_real=False, shift=False) clean_area = NP.zeros(ia.channels.size + npad, dtype=int) skyvis_lag = (npad + ia.channels.size) * ia.freq_resolution * DSP.FT1D(NP.pad(ia.skyvis_freq*ia.bp*ia.bp_wts, ((0,0),(0,npad),(0,0)), mode='constant'), ax=1, inverse=True, use_real=False, shift=False) vis_lag = (npad + ia.channels.size) * ia.freq_resolution * DSP.FT1D(NP.pad(ia.vis_freq*ia.bp*ia.bp_wts, ((0,0),(0,npad),(0,0)), mode='constant'), ax=1, inverse=True, use_real=False, shift=False) lag_kernel = (npad + ia.channels.size) * ia.freq_resolution * DSP.FT1D(NP.pad(ia.bp, ((0,0),(0,npad),(0,0)), mode='constant'), ax=1, inverse=True, use_real=False, shift=False) ccomponents_noiseless = NP.zeros_like(skyvis_lag) ccres_noiseless = NP.zeros_like(skyvis_lag) ccomponents_noisy = NP.zeros_like(vis_lag) ccres_noisy = NP.zeros_like(vis_lag) for snap_iter in xrange(ia.skyvis_freq.shape[2]): progress = PGB.ProgressBar(widgets=[PGB.Percentage(), PGB.Bar(), PGB.ETA()], maxval=ia.baselines.shape[0]).start() for bl_iter in xrange(ia.baselines.shape[0]): clean_area[NP.logical_and(lags <= delay_matrix[0,bl_iter,0]+delay_matrix[0,bl_iter,1]+3/bw, lags >= -delay_matrix[0,bl_iter,0]+delay_matrix[0,bl_iter,1]-3/bw)] = 1
def healpix_interp_along_axis(indata, theta_phi=None, inloc_axis=None, outloc_axis=None, axis=-1, kind='linear', bounds_error=True, fill_value=NP.nan, assume_sorted=False, nest=False): """ ----------------------------------------------------------------------------- Interpolate healpix data to specified angular locations (HEALPIX interpolation) and along one other specified axis (usually frequency axis, for instance) via SciPy interpolation. Wraps HEALPIX and SciPy interpolations into one routine. Inputs: indata [numpy array] input data to be interpolated. Must be of shape (nhpy x nax1 x nax2 x ...). Currently works only for (nhpy x nax1). nhpy is a HEALPIX compatible npix theta_phi [numpy array] spherical angle locations (in radians) at which the healpix data is to be interpolated to at each of the other given axes. It must be of size nang x 2 where nang is the number of spherical angle locations, 2 denotes theta and phi. If set to None (default), no healpix interpolation is performed inloc_axis [numpy array] locations along the axis specified in axis (to be interpolated with SciPy) in which indata is specified. It should be of size nax1, nax2, ... or naxm. Currently it works only if set to nax1 outloc_axis [numpy array] locations along the axis specified in axis to be interpolated to with SciPy. The axis over which this interpolation is to be done is specified in axis. It must be of size nout. If this is set exactly equal to inloc_axis, no interpolation along this axis is performed axis [integer] axis along which SciPy interpolation is to be done. If set to -1 (default), the interpolation happens over the last axis. Since the first axis of indata is reserved for the healpix pixels, axis must be set to 1 or above (upto indata.ndim-1). kind [str or int] Specifies the kind of interpolation as a string ('linear', 'nearest', 'zero', 'slinear', 'quadratic', 'cubic' where 'slinear', 'quadratic' and 'cubic' refer to a spline interpolation of first, second or third order) or as an integer specifying the order of the spline interpolator to use. Default is 'linear'. bounds_error [bool, optional] If True, a ValueError is raised any time interpolation is attempted on a value outside of the range of x (where extrapolation is necessary). If False, out of bounds values are assigned fill_value. By default, an error is raised. fill_value [float] If provided, then this value will be used to fill in for requested points outside of the data range. If not provided, then the default is NaN. assume_sorted [bool] If False, values of x can be in any order and they are sorted first. If True, x has to be an array of monotonically increasing values. nest [bool] if True, the is assumed to be in NESTED ordering. Outputs: HEALPIX interpolated and SciPy interpolated output. Will be of size nang x ... x nout x ... x naxm. Currently returns an array of shape nang x nout ----------------------------------------------------------------------------- """ try: indata except NameError: raise NameError('input data not specified') if not isinstance(indata, NP.ndarray): raise TypeError('input data must be a numpy array') if theta_phi is not None: if not isinstance(theta_phi, NP.ndarray): raise TypeError('output locations must be a numpy array') if theta_phi.ndim != 2: raise ValueError('Output locations must be a 2D array') if axis == -1: axis = indata.ndim - 1 if (axis < 1) or (axis >= indata.ndim): raise ValueError('input axis out of range') if theta_phi is not None: intermediate_data_shape = list(indata.shape) intermediate_data_shape[0] = theta_phi.shape[0] intermediate_data_shape = tuple(intermediate_data_shape) intermediate_data = NP.zeros(intermediate_data_shape, dtype=NP.float32) for ax in range(1,indata.ndim): for i in xrange(indata.shape[ax]): intermediate_data[:,i] = HP.get_interp_val(indata[:,i], theta_phi[:,0], theta_phi[:,1], nest=nest) else: intermediate_data = NP.copy(indata) if outloc_axis is not None: if inloc_axis is not None: outloc_axis = outloc_axis.flatten() inloc_axis = inloc_axis.flatten() eps = 1e-8 if (outloc_axis.size == inloc_axis.size) and (NP.abs(inloc_axis-outloc_axis).max() <= eps): outdata = intermediate_data else: if kind == 'fft': df_inp = NP.mean(NP.diff(inloc_axis)) df_out = NP.mean(NP.diff(outloc_axis)) ntau = df_inp / df_out * inloc_axis.size ntau = NP.round(ntau).astype(int) tau_inp = DSP.spectral_axis(inloc_axis.size, delx=df_inp, shift=True) fftinp = NP.fft.fft(intermediate_data, axis=axis) fftinp_shifted = NP.fft.fftshift(fftinp, axes=axis) if fftinp.size % 2 == 0: fftinp_shifted[:,0] = 0.0 # Blank out the N/2 element (0 element when FFT-shifted) for conjugate symmetry npad = ntau - inloc_axis.size if npad % 2 == 0: npad_before = npad/2 npad_after = npad/2 else: npad_before = npad/2 + 1 npad_after = npad/2 fftinp_shifted_padded = NP.pad(fftinp_shifted, [(0,0), (npad_before, npad_after)], mode='constant') fftinp_padded = NP.fft.ifftshift(fftinp_shifted_padded, axes=axis) ifftout = NP.fft.ifft(fftinp_padded, axis=axis) * (1.0 * ntau / inloc_axis.size) eps_imag = 1e-10 if NP.any(NP.abs(ifftout.imag) > eps_imag): raise ValueError('Significant imaginary component has been introduced unintentionally during the FFT based interpolation. Debug the code.') else: ifftout = ifftout.real fout = DSP.spectral_axis(ntau, delx=tau_inp[1]-tau_inp[0], shift=True) fout -= fout.min() fout += inloc_axis.min() ind_outloc, ind_fout, dfreq = LKP.find_1NN(fout.reshape(-1,1), outloc_axis.reshape(-1,1), distance_ULIM=0.5*(fout[1]-fout[0]), remove_oob=True) outdata = ifftout[:,ind_fout] # npad = 2 * (outloc_axis.size - inloc_axis.size) # dt_inp = DSP.spectral_axis(2*inloc_axis.size, delx=inloc_axis[1]-inloc_axis[0], shift=True) # dt_out = DSP.spectral_axis(2*outloc_axis.size, delx=outloc_axis[1]-outloc_axis[0], shift=True) # fftinp = NP.fft.fft(NP.pad(intermediate_data, [(0,0), (0,inloc_axis.size)], mode='constant'), axis=axis) * (1.0 * outloc_axis.size / inloc_axis.size) # fftinp = NP.fft.fftshift(fftinp, axes=axis) # fftinp[0,0] = 0.0 # Blank out the N/2 element for conjugate symmetry # fftout = NP.pad(fftinp, [(0,0), (npad/2, npad/2)], mode='constant') # fftout = NP.fft.ifftshift(fftout, axes=axis) # outdata = NP.fft.ifft(fftout, axis=axis) # outdata = outdata[0,:outloc_axis.size] else: interp_func = interpolate.interp1d(inloc_axis, intermediate_data, axis=axis, kind=kind, bounds_error=bounds_error, fill_value=fill_value, assume_sorted=assume_sorted) outdata = interp_func(outloc_axis) else: raise ValueError('input inloc_axis not specified') else: outdata = intermediate_data return outdata
def delay_transform(self): self.vis_lag = DSP.FT1D(self.vis_freq, ax=1, use_real=False, shift=False) * self.freq_resolution self.skyvis_lag = DSP.FT1D(self.skyvis_freq, ax=1, use_real=False, shift=False) * self.freq_resolution self.vis_noise_lag = DSP.FT1D(self.vis_noise_freq, ax=1, use_real=False, shift=False) * self.freq_resolution self.lags = DSP.spectral_axis(len(self.channels), delx=self.freq_resolution, use_real=False, shift=False)