示例#1
0
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'
示例#2
0
def test_mock_diffuse_maps(model, hera_loc, apollo_loc, location):
    analytic_diffuse = pytest.importorskip('analytic_diffuse')
    pytest.importorskip('astropy_healpix')
    if location == 'earth':
        loc = hera_loc
    else:
        loc = apollo_loc
    modname, modkwargs = model
    map_nside = 128
    t0 = Time.now()
    cat, kwds = pyuvsim.simsetup.create_mock_catalog(
        t0, arrangement='diffuse', array_location=loc,
        diffuse_model=modname, map_nside=map_nside,
        diffuse_params=modkwargs
    )

    cat.update_positions(t0, loc)

    modfunc = analytic_diffuse.get_model(modname)
    alt, az = cat.alt_az
    za = np.pi / 2 - alt

    vals = modfunc(az, za, **modkwargs)

    assert cat.nside == map_nside
    assert np.allclose(cat.stokes[0].to_value("K"), vals)
示例#3
0
def test_mock_diffuse_maps_errors():
    analytic_diffuse = pyuvsim.simsetup.analytic_diffuse
    astropy_healpix = pyuvsim.simsetup.astropy_healpix
    if (analytic_diffuse is not None) and (astropy_healpix is not None):

        # Error cases:
        with pytest.raises(ValueError, match="Diffuse arrangement selected"):
            pyuvsim.simsetup.create_mock_catalog(Time.now(), arrangement='diffuse')

        with pytest.warns(UserWarning, match="No nside chosen"):
            pyuvsim.simsetup.create_mock_catalog(
                Time.now(), arrangement='diffuse', diffuse_model='monopole'
            )

    else:
        with pytest.raises(ValueError, match="analytic_diffuse and astropy_healpix"):
            pyuvsim.simsetup.create_mock_catalog(Time.now(), arrangement='diffuse')
示例#4
0
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)
示例#5
0
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
示例#6
0
def test_mock_catalog_moon():
    # A mock catalog made with a MoonLocation.
    pytest.importorskip('lunarsky')
    import lunarsky
    from pyuvsim.astropy_interface import Time

    time = Time.now()
    loc = lunarsky.MoonLocation.from_selenodetic(24.433333333, 0.687500000)
    mmock, mkwds = pyuvsim.simsetup.create_mock_catalog(time, 'hera_text', array_location=loc)
    eloc = EarthLocation.from_geodetic(24.433, 0.6875)
    emock, ekwds = pyuvsim.simsetup.create_mock_catalog(time, 'hera_text', array_location=eloc)

    assert mkwds['world'] == 'moon'
    assert ekwds['world'] == 'earth'

    # Simple check that the given lat/lon were interpreted differently in each call.
    assert mmock != emock
示例#7
0
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)
示例#8
0
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
示例#9
0
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)
示例#10
0
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
示例#11
0
def test_skymodeldata_with_quantity_stokes(unit, cat_with_some_pols):
    # Support for upcoming pyradiosky change setting SkyModel.stokes
    # to an astropy Quantity.
    if unit == 'Jy':
        sky = cat_with_some_pols
    else:
        pytest.importorskip('analytic_diffuse')
        sky, _ = pyuvsim.simsetup.create_mock_catalog(
            Time.now(), arrangement='diffuse', diffuse_model='monopole', map_nside=16
        )
    if not isinstance(sky.stokes, units.Quantity):
        sky.stokes *= units.Unit(unit)

    smd = pyuvsim.simsetup.SkyModelData(sky)
    assert np.all(sky.stokes.to_value(unit)[0] == smd.stokes_I)
    assert smd.flux_unit == unit

    sky2 = smd.get_skymodel()
    if units.Quantity != pyradiosky.SkyModel()._stokes.expected_type:
        sky.stokes = sky.stokes.to_value(unit)
    assert sky2 == sky
示例#12
0
def fake_tasks():
    sky = pyradiosky.SkyModel('src',
                              Longitude('10d'),
                              Latitude('5d'),
                              np.array([1, 0, 0, 0]) * units.Jy,
                              'spectral_index',
                              reference_frequency=np.array([100e6]) * units.Hz,
                              spectral_index=np.array([-0.74]))
    n_tasks = 30
    t0 = Time.now()
    freq = 50 * units.Hz
    objs = [
        pyuvsim.UVTask(sky, t0, freq, None, None, freq_i=ii)
        for ii in range(n_tasks)
    ]
    for ti, task in enumerate(objs):
        task.visibility_vector = np.random.uniform(
            0, 3) + np.random.uniform(0, 3) * 1j
        task.uvdata_index = (ti, 0, 0)
        task.sources = 1  # Replace with something easier to compare later.
        objs[ti] = task

    return objs
示例#13
0
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