def test_extra_keywords_errors(tmp_path, ex_val, error_msg): cal_in = UVCal() calfits_file = os.path.join(DATA_PATH, "zen.2457698.40355.xx.gain.calfits") testfile = str(tmp_path / "outtest_omnical.fits") cal_in.read_calfits(calfits_file) # check for warnings & errors with extra_keywords that are dicts, lists or arrays keyword = list(ex_val.keys())[0] val = ex_val[keyword] cal_in.extra_keywords[keyword] = val with uvtest.check_warnings( UserWarning, f"{keyword} in extra_keywords is a list, array or dict"): cal_in.check() with pytest.raises(TypeError, match=error_msg): cal_in.write_calfits(testfile, run_check=False) return
def test_extra_keywords_warnings(tmp_path): cal_in = UVCal() calfits_file = os.path.join(DATA_PATH, "zen.2457698.40355.xx.gain.calfits") testfile = str(tmp_path / "outtest_omnical.fits") cal_in.read_calfits(calfits_file) # check for warnings with extra_keywords keys that are too long cal_in.extra_keywords["test_long_key"] = True with uvtest.check_warnings( UserWarning, "key test_long_key in extra_keywords is longer than 8 characters"): cal_in.check() with uvtest.check_warnings( UserWarning, "key test_long_key in extra_keywords is longer than 8 characters"): cal_in.write_calfits(testfile, run_check=False, clobber=True) return
def write_cal(fname, gains, freqs, times, flags=None, quality=None, total_qual=None, write_file=True, return_uvc=True, outdir='./', overwrite=False, gain_convention='divide', history=' ', x_orientation="east", telescope_name='HERA', cal_style='redundant', **kwargs): '''Format gain solution dictionary into pyuvdata.UVCal and write to file Arguments: fname : type=str, output file basename gains : type=dictionary, holds complex gain solutions. keys are antenna + pol tuple pairs, e.g. (2, 'x'), and keys are 2D complex ndarrays with time along [0] axis and freq along [1] axis. freqs : type=ndarray, holds unique frequencies channels in Hz times : type=ndarray, holds unique times of integration centers in Julian Date flags : type=dictionary, holds boolean flags (True if flagged) for gains. Must match shape of gains. quality : type=dictionary, holds "quality" of calibration solution. Must match shape of gains. See pyuvdata.UVCal doc for more details. total_qual : type=dictionary, holds total_quality_array. Key(s) are polarization string(s) and values are 2D (Ntimes, Nfreqs) ndarrays. write_file : type=bool, if True, write UVCal to calfits file return_uvc : type=bool, if True, return UVCal object outdir : type=str, output file directory overwrite : type=bool, if True overwrite output files gain_convention : type=str, gain solutions formatted such that they 'multiply' into data to get model, or 'divide' into data to get model options=['multiply', 'divide'] history : type=str, history string for UVCal object. x_orientation : type=str, orientation of X dipole, options=['east', 'north'] telescope_name : type=str, name of telescope cal_style : type=str, style of calibration solutions, options=['redundant', 'sky']. If cal_style == sky, additional params are required. See pyuvdata.UVCal doc. kwargs : additional atrributes to set in pyuvdata.UVCal Returns: if return_uvc: returns UVCal object else: returns None ''' # helpful dictionaries for antenna polarization of gains str2pol = {'x': -5, 'y': -6} pol2str = {-5: 'x', -6: 'y'} # get antenna info ant_array = np.array(sorted(map(lambda k: k[0], gains.keys())), np.int) antenna_numbers = copy.copy(ant_array) antenna_names = np.array(map(lambda a: "ant{}".format(a), antenna_numbers)) Nants_data = len(ant_array) Nants_telescope = len(antenna_numbers) # get polarization info pol_array = np.array(sorted(set(map(lambda k: k[1].lower(), gains.keys())))) jones_array = np.array(map(lambda p: str2pol[p], pol_array), np.int) Njones = len(jones_array) # get time info time_array = np.array(times, np.float) Ntimes = len(time_array) time_range = np.array([time_array.min(), time_array.max()], np.float) if len(time_array) > 1: integration_time = np.median(np.diff(time_array)) * 24. * 3600. else: integration_time = 0.0 # get frequency info freq_array = np.array(freqs, np.float) Nfreqs = len(freq_array) Nspws = 1 freq_array = freq_array[None, :] spw_array = np.arange(Nspws) channel_width = np.median(np.diff(freq_array)) # form gain, flags and qualities gain_array = np.empty((Nants_data, Nspws, Nfreqs, Ntimes, Njones), np.complex) flag_array = np.empty((Nants_data, Nspws, Nfreqs, Ntimes, Njones), np.bool) quality_array = np.empty((Nants_data, Nspws, Nfreqs, Ntimes, Njones), np.float) total_quality_array = np.empty((Nspws, Nfreqs, Ntimes, Njones), np.float) for i, p in enumerate(pol_array): if total_qual is not None: total_quality_array[0, :, :, i] = total_qual[p].T[None, :, :] for j, a in enumerate(ant_array): # ensure (a, p) is in gains if (a, p) in gains: gain_array[j, :, :, :, i] = gains[(a, p)].T[None, :, :] if flags is not None: flag_array[j, :, :, :, i] = flags[(a, p)].T[None, :, :] else: flag_array[j, :, :, :, i] = np.zeros( (Nspws, Nfreqs, Ntimes), np.bool) if quality is not None: quality_array[j, :, :, :, i] = quality[(a, p)].T[None, :, :] else: quality_array[j, :, :, :, i] = np.ones( (Nspws, Nfreqs, Ntimes), np.float) else: gain_array[j, :, :, :, i] = np.ones((Nspws, Nfreqs, Ntimes), np.complex) flag_array[j, :, :, :, i] = np.ones((Nspws, Nfreqs, Ntimes), np.bool) quality_array[j, :, :, :, i] = np.ones((Nspws, Nfreqs, Ntimes), np.float) if total_qual is None: total_quality_array = None # Check gain_array for values close to zero, if so, set to 1 zero_check = np.isclose(gain_array, 0, rtol=1e-10, atol=1e-10) gain_array[zero_check] = 1.0 + 0j flag_array[zero_check] += True if zero_check.max() is True: print( "Some of values in self.gain_array were zero and are flagged and set to 1." ) # instantiate UVCal uvc = UVCal() # enforce 'gain' cal_type uvc.cal_type = "gain" # create parameter list params = [ "Nants_data", "Nants_telescope", "Nfreqs", "Ntimes", "Nspws", "Njones", "ant_array", "antenna_numbers", "antenna_names", "cal_style", "history", "channel_width", "flag_array", "gain_array", "quality_array", "jones_array", "time_array", "spw_array", "freq_array", "history", "integration_time", "time_range", "x_orientation", "telescope_name", "gain_convention", "total_quality_array" ] # create local parameter dict local_params = locals() # overwrite with kwarg parameters local_params.update(kwargs) # set parameters for p in params: uvc.__setattr__(p, local_params[p]) # run check uvc.check() # write to file if write_file: # check output fname = os.path.join(outdir, fname) if os.path.exists(fname) and overwrite is False: print("{} exists, not overwriting...".format(fname)) else: print "saving {}".format(fname) uvc.write_calfits(fname, clobber=True) # return object if return_uvc: return uvc