def test_sim_on_moon(): from pyuvsim.astropy_interface import MoonLocation param_filename = os.path.join(SIM_DATA_PATH, 'test_config', 'obsparam_tranquility_hex.yaml') param_dict = pyuvsim.simsetup._config_str_to_dict(param_filename) param_dict['select'] = {'redundant_threshold': 0.1} uv_obj, beam_list, beam_dict = pyuvsim.initialize_uvdata_from_params( param_dict) uv_obj.select(times=uv_obj.time_array[0]) tranquility_base = MoonLocation.from_selenocentric( *uv_obj.telescope_location, 'meter') time = Time(uv_obj.time_array[0], format='jd', scale='utc') sources, kwds = pyuvsim.create_mock_catalog( time, array_location=tranquility_base, arrangement='zenith', Nsrcs=30, return_data=True) # Run simulation. uv_out = pyuvsim.uvsim.run_uvdata_uvsim(uv_obj, beam_list, beam_dict, catalog=sources, quiet=True) assert np.allclose(uv_out.data_array[:, 0, :, 0], 0.5) assert uv_out.extra_keywords['world'] == 'moon'
def test_mock_catalogs(): time = Time(2458098.27471265, scale='utc', format='jd') arrangements = ['off-zenith', 'zenith', 'cross', 'triangle', 'long-line', 'random', 'hera_text'] cats = {} for arr in arrangements: # rseed is only used by the "random" mock catalog cat, mock_kwds = pyuvsim.simsetup.create_mock_catalog(time, arr, rseed=2458098) cats[arr] = cat # For each mock catalog, verify the Ra/Dec source positions against a text catalog. text_catalogs = { 'cross': 'mock_cross_2458098.27471.txt', 'hera_text': 'mock_hera_text_2458098.27471.txt', 'long-line': 'mock_long-line_2458098.27471.txt', 'off-zenith': 'mock_off-zenith_2458098.27471.txt', 'triangle': 'mock_triangle_2458098.27471.txt', 'random': 'mock_random_2458098.27471.txt', 'zenith': 'mock_zenith_2458098.27471.txt' } with pytest.raises(KeyError, match="Invalid mock catalog arrangement: invalid_catalog_name"): pyuvsim.create_mock_catalog(time, 'invalid_catalog_name') radec_catalog = pyradiosky.SkyModel() for arr in arrangements: radec_catalog.read_text_catalog( os.path.join(SIM_DATA_PATH, 'test_catalogs', text_catalogs[arr]) ) assert np.all(radec_catalog == cats[arr]) cat, mock_kwds = pyuvsim.simsetup.create_mock_catalog(time, 'random', save=True) loc = eval(mock_kwds['array_location']) loc = EarthLocation.from_geodetic(loc[1], loc[0], loc[2]) # Lon, Lat, alt fname = 'mock_catalog_random.npz' alts_reload = np.load(fname)['alts'] cat.update_positions(time, loc) alt, az = cat.alt_az assert np.all(alt > np.radians(30.)) assert np.allclose(alts_reload, np.degrees(alt)) os.remove(fname)
def test_gaussbeam_values(): """ Make the long-line point sources up to 10 degrees from zenith. Obtain visibilities Confirm that the values match the expected beam values at those zenith angles. """ sigma = 0.05 hera_uv = UVData() hera_uv.read_uvfits(EW_uvfits_file) array_location = EarthLocation.from_geocentric( hera_uv.telescope_location[0], hera_uv.telescope_location[1], hera_uv.telescope_location[2], unit='m') freq = hera_uv.freq_array[0, 0] * units.Hz time = Time(hera_uv.time_array[0], scale='utc', format='jd') catalog, mock_keywords = pyuvsim.create_mock_catalog( time=time, arrangement='long-line', Nsrcs=41, max_za=10., array_location=array_location) beam = pyuvsim.AnalyticBeam('gaussian', sigma=sigma) array = pyuvsim.Telescope('telescope_name', array_location, [beam]) # Need a dummy baseline for this test. antenna1 = pyuvsim.Antenna('ant1', 1, np.array([0, 0, 0]), 0) antenna2 = pyuvsim.Antenna('ant2', 2, np.array([107, 0, 0]), 0) baseline = pyuvsim.Baseline(antenna1, antenna2) coherencies = [] zenith_angles = [] for src in catalog: task = pyuvsim.UVTask(src, time, freq, baseline, array) engine = pyuvsim.UVEngine(task) engine.apply_beam() # task.source.az_za_calc(time, array_location) zenith_angles.append(task.source.az_za[1]) # In radians. coherencies.append( np.real(engine.apparent_coherency[0, 0]).astype( float)) # All four components should be identical coherencies = np.array(coherencies) zenith_angles = np.array(zenith_angles) # Confirm the coherency values (ie., brightnesses) match the beam values. beam_values = np.exp(-(zenith_angles)**2 / (2 * beam.sigma**2)) nt.assert_true(np.all(beam_values**2 == coherencies))
def test_gaussbeam_values(): """ Make the long-line point sources up to 10 degrees from zenith. Confirm that the coherencies match the expected beam values at those zenith angles. """ sigma = 0.05 hera_uv = UVData() hera_uv.read_uvfits(EW_uvfits_file) array_location = EarthLocation.from_geocentric(*hera_uv.telescope_location, unit='m') freq = hera_uv.freq_array[0, 0] * units.Hz time = Time(hera_uv.time_array[0], scale='utc', format='jd') catalog, mock_keywords = pyuvsim.create_mock_catalog( time=time, arrangement='long-line', Nsrcs=41, min_alt=80., array_location=array_location) catalog.update_positions(time, array_location) beam = pyuvsim.AnalyticBeam('gaussian', sigma=sigma) array = pyuvsim.Telescope('telescope_name', array_location, [beam]) # Need a dummy baseline for this test. antenna1 = pyuvsim.Antenna('ant1', 1, np.array([0, 0, 0]), 0) antenna2 = pyuvsim.Antenna('ant2', 2, np.array([107, 0, 0]), 0) baseline = pyuvsim.Baseline(antenna1, antenna2) task = pyuvsim.UVTask(catalog, time, freq, baseline, array) engine = pyuvsim.UVEngine(task) engine.apply_beam() altitudes = task.sources.alt_az[0] # In radians. # All four components should be identical if isinstance(engine.apparent_coherency, units.Quantity): coherency_use = engine.apparent_coherency.to_value("Jy") else: coherency_use = engine.apparent_coherency coherencies = np.real(coherency_use[0, 0] + coherency_use[1, 1]).astype(float) zenith_angles, _ = simutils.altaz_to_zenithangle_azimuth( altitudes, np.zeros_like(np.array(altitudes))) # Confirm the coherency values (ie., brightnesses) match the beam values. beam_values = np.exp(-(zenith_angles)**2 / (2 * beam.sigma**2)) assert np.all(beam_values**2 == coherencies)
def test_gaussian_beam(): sigma_rad = Angle('5d').to('rad').value beam = pyuvsim.AnalyticBeam('gaussian', sigma=sigma_rad) beam.peak_normalize() beam.interpolation_function = 'az_za_simple' time = Time('2018-03-01 00:00:00', scale='utc') array_location = EarthLocation(lat='-30d43m17.5s', lon='21d25m41.9s', height=1073.) source_list, mock_keywords = pyuvsim.create_mock_catalog( time, 'hera_text', array_location=array_location) nsrcs = len(source_list) az_vals = [] za_vals = [] freq_vals = [] for src in source_list: src_coord = SkyCoord(ra=src.ra, dec=src.dec, frame='icrs', obstime=time, location=array_location) src_coord_altaz = src_coord.transform_to('altaz') az_vals.append(src_coord_altaz.az.to('rad').value) za_vals.append((Angle('90d') - src_coord_altaz.alt).to('rad').value) if len(freq_vals) > 0: if src.freq.to('Hz').value != freq_vals[0]: freq_vals.append(src.freq.to('Hz').value) else: freq_vals.append(src.freq.to('Hz').value) n_freqs = len(freq_vals) interpolated_beam, interp_basis_vector = beam.interp( az_array=np.array(az_vals), za_array=np.array(za_vals), freq_array=np.array(freq_vals)) expected_data = np.zeros((2, 1, 2, n_freqs, nsrcs), dtype=np.float) interp_zas = np.zeros((n_freqs, nsrcs), dtype=np.float) for f_ind in range(n_freqs): interp_zas[f_ind, :] = np.array(za_vals) gaussian_vals = np.exp(-(interp_zas**2) / (2 * sigma_rad**2)) expected_data[1, 0, 0, :, :] = gaussian_vals expected_data[0, 0, 1, :, :] = gaussian_vals expected_data[1, 0, 1, :, :] = gaussian_vals expected_data[0, 0, 0, :, :] = gaussian_vals nt.assert_true(np.allclose(interpolated_beam, expected_data))
def heratext_posfreq(): time = Time('2018-03-01 00:00:00', scale='utc') array_location = EarthLocation(lat='-30d43m17.5s', lon='21d25m41.9s', height=1073.) sources, mock_keywords = pyuvsim.create_mock_catalog( time, 'hera_text', array_location=array_location) sources.update_positions(time, array_location) za_vals = np.pi / 2. - sources.alt_az[1] # rad az_vals = sources.alt_az[1] freq_vals = np.array([10**8]) return az_vals, za_vals, freq_vals
def test_zenith_spectral_sim(spectral_type, tmpdir): # Make a power law source at zenith in three ways. # Confirm that simulated visibilities match expectation. params = pyuvsim.simsetup._config_str_to_dict( os.path.join(SIM_DATA_PATH, 'test_config', 'param_1time_1src_testcat.yaml')) alpha = -0.5 ref_freq = 111e6 Nfreqs = 20 freqs = np.linspace(110e6, 115e6, Nfreqs) freq_params = pyuvsim.simsetup.freq_array_to_params(freqs) freqs = pyuvsim.simsetup.parse_frequency_params(freq_params)['freq_array'][ 0, :] freqs *= units.Hz spectrum = (freqs.value / ref_freq)**alpha source, kwds = pyuvsim.create_mock_catalog(Time.now(), arrangement='zenith', Nsrcs=1) source.spectral_type = spectral_type if spectral_type == 'spectral_index': source.reference_frequency = np.array([ref_freq]) * units.Hz source.spectral_index = np.array([alpha]) else: source.Nfreqs = Nfreqs source.freq_array = freqs source.stokes = np.repeat(source.stokes, Nfreqs, axis=1) source.stokes[0, :, 0] *= spectrum source.coherency_radec = stokes_to_coherency(source.stokes) catpath = str(tmpdir.join('spectral_test_catalog.txt')) source.write_text_catalog(catpath) params['sources'] = {"catalog": catpath} params['filing']['outdir'] = str(tmpdir) params['freq'] = freq_params params['time']['start_time'] = kwds['time'] params['select'] = {'antenna_nums': [1, 2]} uv_out = pyuvsim.run_uvsim(params, return_uv=True) for ii in range(uv_out.Nbls): assert np.allclose(uv_out.data_array[ii, 0, :, 0], spectrum / 2)
def test_yaml_to_tasks(): # params = yaml.safe_load(open(longbl_yaml_file)) params = yaml.safe_load(open(laptop_size_sim)) params['config_path'] = longbl_yaml_file # not sure why I need this input_uv, beam_list, beam_ids = \ pyuvsim.simsetup.initialize_uvdata_from_params(params) time = Time('2018-03-01 00:00:00', scale='utc') HERA_location = EarthLocation(lat='-30d43m17.5s', lon='21d25m41.9s', height=1073.) catalog, mock_keywords = pyuvsim.create_mock_catalog( time, arrangement='zenith', Nsrcs=10, array_location=HERA_location) print("Size of catalog:", sys.getsizeof(catalog), " bytes with ", len(catalog), "entries") uvtask_list = pyuvsim.uvdata_to_task_list(input_uv, catalog, beam_list) uvtask_pickle = pickle.dumps(uvtask_list) print("getsizeof(uvtask_pickle) = ", sys.getsizeof(uvtask_pickle), " bytes with ", len(uvtask_list), "entries") print("len(uvtask_pickle) = ", len(uvtask_pickle))
def test_mock_catalog_zenith_source(hera_loc): time = Time(2457458.65410, scale='utc', format='jd') array_location = hera_loc source_coord = SkyCoord( alt=Angle(90 * units.deg), az=Angle(0 * units.deg), obstime=time, frame='altaz', location=array_location ) icrs_coord = source_coord.transform_to('icrs') ra = icrs_coord.ra dec = icrs_coord.dec test_source = pyradiosky.SkyModel('src0', ra, dec, units.Quantity([1, 0, 0, 0], 'Jy'), 'flat') cat, mock_keywords = pyuvsim.create_mock_catalog(time, arrangement='zenith') assert cat == test_source
def test_mock_catalog_off_zenith_source(hera_loc): src_az = Angle('90.0d') src_alt = Angle('85.0d') time = Time(2457458.65410, scale='utc', format='jd') array_location = hera_loc source_coord = SkyCoord( alt=src_alt, az=src_az, obstime=time, frame='altaz', location=array_location ) icrs_coord = source_coord.transform_to('icrs') ra = icrs_coord.ra dec = icrs_coord.dec test_source = pyradiosky.SkyModel('src0', ra, dec, units.Quantity([1.0, 0, 0, 0], "Jy"), 'flat') cat, mock_keywords = pyuvsim.create_mock_catalog(time, arrangement='off-zenith', alt=src_alt.deg) assert cat == test_source
def test_mock_catalog(): src_az = Angle('90.0d') src_alt = Angle('85.0d') src_za = Angle('90.0d') - src_alt hera_uv = UVData() hera_uv.read_uvfits(EW_uvfits_file, ant_str='cross') time = Time(hera_uv.time_array[0], scale='utc', format='jd') test_source = create_offzenith_source(time, 'src0', az=src_az, alt=src_alt) cat, mock_keywords = pyuvsim.create_mock_catalog(time, arrangement='off-zenith', zen_ang=src_za.deg) cat_source = cat[0] for k in cat_source.__dict__: print 'Cat: ', k, cat_source.__dict__[k] print 'Test: ', k, test_source.__dict__[k] print '\n' nt.assert_equal(cat_source, test_source)
comm = MPI.COMM_WORLD size = comm.Get_size() rank = comm.Get_rank() print("rank = ", rank, "PID", os.getpid()) time = Time('2018-03-01 00:00:00', scale='utc') array_location = EarthLocation(lat='-30d43m17.5s', lon='21d25m41.9s', height=1073.) #pfile = '../data/5km_triangle_2time_1chan.yaml' #pfile = '../data/5km_triangle_10time_1chan.yaml' #pfile = '../data/5km_triangle_1time_1chan_6ant.yaml' pfile = '../data/laptop_size_sim.yaml' params = yaml.safe_load(open(pfile)) print(pfile) input_uv, beam_list, beam_ids = simsetup.initialize_uvdata_from_params(params) #for a range of Nsrcs measure ram usage #Ns = np.array([1,10,100]) Ns = [10] process = psutil.Process(os.getpid()) for Nsrcs in Ns: #make the sources catalog = create_mock_catalog(time, 'long-line', Nsrcs=Nsrcs) #run the thing print("Nsrcs,", Nsrcs, "rank,", rank, "pre uvdata:", process.get_memory_info()[0] / 1e6, "MB") uvdata_out = uvsim.run_uvsim(input_uv, beam_list=beam_list, catalog=catalog) print("Nsrcs,", Nsrcs, "rank,", rank, "post uvdata:", process.get_memory_info()[0] / 1e6, "MB")
def test_param_reader(): pytest.importorskip('mpi4py') # Reading in various configuration files param_filename = os.path.join(SIM_DATA_PATH, "test_config", "param_10time_10chan_0.yaml") hera_uv = UVData() hera_uv.read_uvfits(triangle_uvfits_file) hera_uv.unphase_to_drift() hera_uv.telescope_name = 'HERA' time = Time(hera_uv.time_array[0], scale='utc', format='jd') sources, _ = pyuvsim.create_mock_catalog(time, arrangement='zenith', return_data=True) beam0 = UVBeam() beam0.read_beamfits(herabeam_default) beam0.extra_keywords['beam_path'] = herabeam_default beam1 = pyuvsim.AnalyticBeam('uniform') beam2 = pyuvsim.AnalyticBeam('gaussian', sigma=0.02) beam3 = pyuvsim.AnalyticBeam('airy', diameter=14.6) beam_list = pyuvsim.BeamList([beam0, beam1, beam2, beam3]) # To fill out other parameters in the UVBeam. beam_list.set_str_mode() beam_list.set_obj_mode() beam_dict = {'ANT1': 0, 'ANT2': 1, 'ANT3': 2, 'ANT4': 3} # Error conditions: params_bad = pyuvsim.simsetup._config_str_to_dict(param_filename) bak_params_bad = copy.deepcopy(params_bad) # Missing config file info params_bad['config_path'] = os.path.join( SIM_DATA_PATH, 'nonexistent_directory', 'nonexistent_file' ) with pytest.raises(ValueError, match="nonexistent_directory is not a directory"): pyuvsim.simsetup.initialize_uvdata_from_params(params_bad) params_bad['config_path'] = os.path.join(SIM_DATA_PATH, "test_config") params_bad['telescope']['array_layout'] = 'nonexistent_file' with pytest.raises(ValueError, match="nonexistent_file from yaml does not exist"): pyuvsim.simsetup.initialize_uvdata_from_params(params_bad) params_bad['telescope']['telescope_config_name'] = 'nonexistent_file' with pytest.raises(ValueError, match="telescope_config_name file from yaml does not exist"): pyuvsim.simsetup.initialize_uvdata_from_params(params_bad) # Missing beam keywords params_bad = copy.deepcopy(bak_params_bad) params_bad['config_path'] = os.path.join(SIM_DATA_PATH, "test_config") params_bad = copy.deepcopy(bak_params_bad) params_bad['telescope']['telescope_config_name'] = os.path.join( SIM_DATA_PATH, 'test_config', '28m_triangle_10time_10chan_gaussnoshape.yaml' ) with pytest.raises(KeyError, match="Missing shape parameter for gaussian beam"): pyuvsim.simsetup.initialize_uvdata_from_params(params_bad) params_bad['telescope']['telescope_config_name'] = os.path.join( SIM_DATA_PATH, 'test_config', '28m_triangle_10time_10chan_nodiameter.yaml' ) with pytest.raises(KeyError, match="Missing diameter for airy beam."): pyuvsim.simsetup.initialize_uvdata_from_params(params_bad) params_bad['telescope']['telescope_config_name'] = os.path.join( SIM_DATA_PATH, 'test_config', '28m_triangle_10time_10chan_nofile.yaml' ) with pytest.raises(ValueError, match="Undefined beam model"): pyuvsim.simsetup.initialize_uvdata_from_params(params_bad) # Check default configuration uv_obj, new_beam_list, new_beam_dict = pyuvsim.initialize_uvdata_from_params(param_filename) new_beam_list.set_obj_mode() pyuvsim.simsetup._complete_uvdata(uv_obj, inplace=True) with open(param_filename, 'r') as fhandle: param_dict = yaml.safe_load(fhandle) expected_ofilepath = pyuvsim.utils.write_uvdata( uv_obj, param_dict, return_filename=True, dryrun=True ) assert './sim_results.uvfits' == expected_ofilepath # Spoof attributes that won't match. uv_obj.antenna_names = uv_obj.antenna_names.tolist() uv_obj.antenna_diameters = hera_uv.antenna_diameters uv_obj.history = hera_uv.history uvfits_required_extra = [ "_antenna_positions", "_gst0", "_rdate", "_earth_omega", "_dut1", "_timesys", ] for attr in uvfits_required_extra: param = getattr(uv_obj, attr) if param.value is None: param.value = param.spoof_val setattr(uv_obj, attr, param) assert new_beam_dict == beam_dict assert new_beam_list == beam_list assert uv_obj == hera_uv