Ejemplo n.º 1
0
def generate_uvw_coords_m(x, y, z, obs_length_h, mjd_mid, t_acc, ra, dec,
                          filename=None):
    num_times = int((obs_length_h * 3600.0) / t_acc)
    mjd_start = mjd_mid - ((obs_length_h / 2) / 24)
    num_baselines = x.shape[0] * (x.shape[0] - 1) // 2
    num_vis = num_baselines * num_times
    uu, vv, ww = np.zeros(num_vis), np.zeros(num_vis), np.zeros(num_vis)
    print('- Simulating uv coordinates for %i times over %.1f h'
          % (num_times, obs_length_h))
    t0 = time.time()
    for i in range(num_times):
        mjd = mjd_start + (i * t_acc + t_acc / 2) / 86400
        uu_, vv_, ww_ = evaluate_baseline_uvw(x, y, z, ra, dec, mjd)
        uu[i * num_baselines:(i + 1) * num_baselines] = uu_
        vv[i * num_baselines:(i + 1) * num_baselines] = vv_
        ww[i * num_baselines:(i + 1) * num_baselines] = ww_
    print('- Generated uv %i coordinates in %.2f s'
          % (uu.shape[0], time.time() - t0))
    if filename:
        dir_name = os.path.dirname(filename)
        if not os.path.isdir(dir_name):
            os.makedirs(dir_name)
        plot_uv_coords(uu, vv, filename=filename + '_all')
        plot_uv_coords(uu, vv, filename=filename + '_r%06.1fm' % 100.0,
                       r_lim=100.0)
    return uu, vv, ww, num_times
Ejemplo n.º 2
0
def generate_uvw_coords_m(x,
                          y,
                          z,
                          obs_length_h,
                          mjd_mid,
                          t_acc,
                          ra,
                          dec,
                          filename=None):
    num_times = int((obs_length_h * 3600.0) / t_acc)
    mjd_start = mjd_mid - ((obs_length_h / 2) / 24)
    num_baselines = x.shape[0] * (x.shape[0] - 1) // 2
    num_vis = num_baselines * num_times
    uu, vv, ww = np.zeros(num_vis), np.zeros(num_vis), np.zeros(num_vis)
    print('- Simulating uv coordinates for %i times over %.1f h' %
          (num_times, obs_length_h))
    t0 = time.time()
    for i in range(num_times):
        mjd = mjd_start + (i * t_acc + t_acc / 2) / 86400
        uu_, vv_, ww_ = evaluate_baseline_uvw(x, y, z, ra, dec, mjd)
        uu[i * num_baselines:(i + 1) * num_baselines] = uu_
        vv[i * num_baselines:(i + 1) * num_baselines] = vv_
        ww[i * num_baselines:(i + 1) * num_baselines] = ww_
    print('- Generated uv %i coordinates in %.2f s' %
          (uu.shape[0], time.time() - t0))
    if filename:
        dir_name = os.path.dirname(filename)
        if not os.path.isdir(dir_name):
            os.makedirs(dir_name)
        plot_uv_coords(uu, vv, filename=filename + '_all')
        plot_uv_coords(uu,
                       vv,
                       filename=filename + '_r%06.1fm' % 100.0,
                       r_lim=100.0)
    return uu, vv, ww, num_times
Ejemplo n.º 3
0
def generate_baseline_coordinates(layout, settings):
    """Generate baseline coordinates"""
    x, y = layout['x'], layout['y']
    wavelength_m = 299792458.0 / settings['freq_hz']
    mjd_start = settings['mjd_mid'] - (settings['obs_length_s'] / 2.0) / 86400.0
    x, y, z = np.array(x), np.array(y), np.zeros_like(x)
    x_ecef, y_ecef, z_ecef = convert_enu_to_ecef(x, y, z, settings['lon'],
                                                 settings['lat'])
    x_c, y_c, z_c = convert_enu_to_ecef([0.0], [0.0], [0.0], settings['lon'],
                                        settings['lat'])
    x_ecef -= x_c
    y_ecef -= y_c
    z_ecef -= z_c
    num_stations = x_ecef.shape[0]
    num_baselines = num_stations * (num_stations - 1) / 2
    num_coords = settings['num_times'] * num_baselines
    uu = np.zeros(num_coords, dtype='f4')
    vv, ww = np.zeros_like(uu), np.zeros_like(uu)
    u = np.zeros(num_stations, dtype='f4')
    v, w = np.zeros_like(u), np.zeros_like(u)
    for i in range(settings['num_times']):
        mjd = mjd_start + (i * settings['interval_s'] +
                           settings['interval_s'] / 2.0) / 86400.0
        uu_, vv_, ww_ = evaluate_baseline_uvw(x_ecef, y_ecef, z_ecef,
                                              settings['ra'], settings['dec'],
                                              mjd)
        u, v, w = evaluate_station_uvw(x_ecef, y_ecef, z_ecef, settings['ra'],
                                       settings['dec'], mjd)
        uu[i * num_baselines: (i + 1) * num_baselines] = uu_ / wavelength_m
        vv[i * num_baselines: (i + 1) * num_baselines] = vv_ / wavelength_m
        ww[i * num_baselines: (i + 1) * num_baselines] = ww_ / wavelength_m
    return uu, vv, ww, u, w, v
Ejemplo n.º 4
0
def generate_baseline_uvw(x, y, z, ra_rad, dec_rad, num_times, num_baselines,
                          mjd_start, dt_s):
    num_coords = num_times * num_baselines
    uu = numpy.zeros(num_coords, dtype='f8')
    vv = numpy.zeros(num_coords, dtype='f8')
    ww = numpy.zeros(num_coords, dtype='f8')
    for i in range(num_times):
        t = i * dt_s + dt_s / 2.0
        mjd = mjd_start + (t / 86400.0)
        i0 = i * num_baselines
        i1 = i0 + num_baselines
        uu_, vv_, ww_ = evaluate_baseline_uvw(x, y, z, ra_rad, dec_rad, mjd)
        uu[i0:i1] = uu_
        vv[i0:i1] = vv_
        ww[i0:i1] = ww_
    return uu, vv, ww
Ejemplo n.º 5
0
def generate_baseline_uvw(x, y, z, ra_rad, dec_rad, num_times, mjd_start, dt_s):
    """Generate baseline coordinates from ecef station coordinates."""
    num_stations = x.shape[0]
    num_baselines = num_stations * (num_stations - 1) / 2
    num_coords = num_times * num_baselines
    uu = numpy.zeros(num_coords, dtype='f8')
    vv = numpy.zeros(num_coords, dtype='f8')
    ww = numpy.zeros(num_coords, dtype='f8')
    for i in range(num_times):
        t = i * dt_s + dt_s / 2.0
        mjd = mjd_start + (t / 86400.0)
        i0 = i * num_baselines
        i1 = i0 + num_baselines
        uu_, vv_, ww_ = evaluate_baseline_uvw(x, y, z, ra_rad, dec_rad, mjd)
        uu[i0:i1] = uu_
        vv[i0:i1] = vv_
        ww[i0:i1] = ww_
    return uu, vv, ww
Ejemplo n.º 6
0
def generate_uv_coords(x_ecef, y_ecef, z_ecef, settings, obs):
    wavelength_m = 299792458.0 / settings['freq_hz']
    mjd_start = settings['mjd_mid'] - (obs['obs_length_s'] / 2.0) / 86400.0
    num_stations = x_ecef.shape[0]
    num_baselines = num_stations * (num_stations - 1) / 2
    num_coords = obs['num_times'] * num_baselines
    uu = numpy.zeros(num_coords, dtype='f4')
    vv = numpy.zeros_like(uu, dtype='f4')
    ww = numpy.zeros_like(uu, dtype='f4')
    for i in range(obs['num_times']):
        mjd = mjd_start + (i * obs['interval_s'] +
                           obs['interval_s'] / 2.0) / 86400.0
        uu_, vv_, ww_ = pyuvwsim.evaluate_baseline_uvw(x_ecef, y_ecef, z_ecef,
                                                       settings['ra'],
                                                       settings['dec'], mjd)
        uu[i * num_baselines: (i + 1) * num_baselines] = uu_ / wavelength_m
        vv[i * num_baselines: (i + 1) * num_baselines] = vv_ / wavelength_m
        ww[i * num_baselines: (i + 1) * num_baselines] = ww_ / wavelength_m
    return uu, vv, ww
Ejemplo n.º 7
0
def generate_baseline_coords(layout, settings):
    x, y = layout['x'], layout['y']
    z = numpy.zeros_like(x)
    wavelength_m = 299792458.0 / settings['freq_hz']
    mjd_start = settings['mjd_mid'] - (settings['obs_length_s'] / 2.0) / 86400.0
    x, y, z = numpy.array(x), numpy.array(y), numpy.zeros_like(x)
    x_ecef, y_ecef, z_ecef = pyuvwsim.convert_enu_to_ecef(x, y, z,
                                                          settings['lon'],
                                                          settings['lat'])
    num_stations = x_ecef.shape[0]
    num_baselines = num_stations * (num_stations - 1) / 2
    num_coords = settings['num_times'] * num_baselines
    uu = numpy.zeros(num_coords, dtype='f4')
    vv, ww = numpy.zeros_like(uu), numpy.zeros_like(uu)
    for i in range(settings['num_times']):
        mjd = mjd_start + (i * settings['interval_s'] +
                           settings['interval_s'] / 2.0) / 86400.0
        uu_, vv_, ww_ = pyuvwsim.evaluate_baseline_uvw(x_ecef, y_ecef, z_ecef,
                                                       settings['ra'],
                                                       settings['dec'], mjd)
        uu[i * num_baselines: (i + 1) * num_baselines] = uu_ / wavelength_m
        vv[i * num_baselines: (i + 1) * num_baselines] = vv_ / wavelength_m
        ww[i * num_baselines: (i + 1) * num_baselines] = ww_ / wavelength_m
    return uu, vv, ww
lat        = 90.0 * (np.pi/180.)
alt        = 0.0
ra0        = 0.0  * (np.pi/180.)
dec0       = 90.0 * (np.pi/180.)
ntimes     = 10
obs_length = 0.2 # days
# ---------------------------------------------------------

# Generate coordinates.
mjd_start  = uvw.datetime_to_mjd(2014, 7, 28, 14, 26, 1.321);
(x,y,z) = uvw.load_station_coords(filename)
(x,y,z) = uvw.convert_enu_to_ecef(x, y, z, lon, lat, alt)
t0 = time.time()
for i in range(0, ntimes):
    mjd = mjd_start
    (uu_m,vv_m,ww_m) = uvw.evaluate_baseline_uvw(x,y,z, ra0, dec0, mjd)
print '- Time taken to evaluate uww coordinates = %.3f ms' % ((time.time()-t0)*1.e3)

# Scatter plot of baseline coordinates.
plt.ioff()
plt_filename = 'test.png'
fig = plt.figure(figsize=(6,6))
matplotlib.rcParams.update({'font.size': 10})
scatter_size = 3
fig.add_subplot(1,1,1, aspect='equal')
plt.scatter(uu_m, vv_m, c='b', lw = 0, s=scatter_size);
plt.scatter(-uu_m, -vv_m, c='r', lw = 0, s=scatter_size);
plt.xlabel('uu [metres]', fontsize=8)
plt.ylabel('vv [metres]', fontsize=8)
plt.title(filename[-17:])
plt.grid(True)
Ejemplo n.º 9
0
def simulate_2(config):
    """Simulate with and without corruptions followed by averaging.
    Simulation to be performed in blocks due to memory constraints.
    """
    telescope = config['sim']['telescope']
    obs = config['sim']['observation']
    corrupt = config['corrupt']
    freq_hz = obs['freq_hz']
    wavelength = 299792458.0 / freq_hz
    lon = math.radians(telescope['lon_deg'])
    lat = math.radians(telescope['lat_deg'])
    alt = telescope['alt_m']
    ra = math.radians(obs['ra_deg'])
    dec = math.radians(obs['dec_deg'])
    mjd_start = obs['start_time_mjd']
    num_dumps = obs['num_times']
    dump_time_s = obs['dump_time_s']
    over_sample = obs['over_sample']
    dt_s = dump_time_s / over_sample
    num_times = num_dumps * over_sample
    telescope_model = telescope['path']
    x, y, z = load_station_coords(join(telescope_model, 'layout.txt'))
    x, y, z = convert_enu_to_ecef(x, y, z, lon, lat, alt)
    num_antennas = x.shape[0]
    num_baselines = num_antennas * (num_antennas - 1) / 2
    num_vis = num_dumps * num_baselines
    sky_model = config['sim']['sky_file']
    sky = numpy.loadtxt(sky_model, delimiter=',')
    sky = sky.reshape((-1, 3))
    num_sources = sky.shape[0]
    source_ra = numpy.radians(sky[:, 0])
    source_dec = numpy.radians(sky[:, 1])

    l, m, n = convert_ra_dec_to_relative_directions(
        source_ra, source_dec, ra, dec)
    tau = corrupt['tau_s']
    hurst_amp = corrupt['amplitude']['hurst']
    adev_amp = corrupt['amplitude']['allan_dev']
    std_t_mid_amp = corrupt['amplitude']['std_t_mid']
    hurst_phase = corrupt['phase']['hurst']
    adev_phase = corrupt['phase']['allan_dev']
    std_t_mid_phase = corrupt['phase']['std_t_mid']
    smoothing_length = corrupt['smoothing_length']

    print('- Simulating data...')
    print('  - No. antennas  :', num_antennas)
    print('  - No. baselines :', num_baselines)
    print('  - Obs. length   : %.1f s' % (num_dumps * dump_time_s))
    print('  - No. times     : %i (no. dumps: %i, over-sample: %i)' %
          (num_times, num_dumps, over_sample))
    print('  - No. vis       :', num_vis)
    print('  - No. sources   :', num_sources)
    print('  - Corruptions:')
    print('    -  Hurst amp %.1f, phase %.1f' % (hurst_amp, hurst_phase))
    print('    -  A. dev amp %.1e, phase %.1e' % (adev_amp, adev_phase))

    num_bytes = num_vis * 8 * 8 + (num_antennas * num_times) * 16
    mem = virtual_memory()
    print('  - Mem. required : %.1f / %.1f MB' %
          (num_bytes / 1024.0**2, mem.total / 1024.0**2))
    if num_bytes >= mem.total:
        raise RuntimeError('Not enough system memory for requested '
                           'simulation.')

    # Generate corruptions
    gains = numpy.empty((num_antennas, num_times), dtype='c16')
    t0 = time.time()
    for i in range(num_antennas):
        gains[i, :] = eval_complex_gains(num_times, dt_s, hurst_amp, adev_amp,
                                         std_t_mid_amp, hurst_phase, adev_phase,
                                         std_t_mid_phase, smoothing_length,
                                         tau)
    conj_gains = numpy.conj(gains)
    print('  - Gains generated in %.1f s' % (time.time() - t0))

    # TODO-BM plot the gains ...

    # Simulation
    phase0 = 2.0 * math.pi / wavelength
    model = numpy.empty(num_vis, dtype='c16')
    data = numpy.empty(num_vis, dtype='c16')
    weight = numpy.ones(num_vis, dtype='f8')
    block_model = numpy.empty((over_sample, num_baselines), dtype='c16')
    block_data = numpy.empty((over_sample, num_baselines), dtype='c16')
    t1 = time.time()
    # Loop over correlator dumps
    for i in range(num_dumps):
        block_model.fill(0.0 + 0.0j)
        block_data.fill(0.0 + 0.0j)
        # Loop over times in a dump.
        for t in range(over_sample):
            delta_t = (i * dump_time_s) + (t * dt_s) + (dt_s / 2.0)
            mjd = mjd_start + (delta_t / 86400.0)
            uu_, vv_, ww_ = evaluate_baseline_uvw(x, y, z, ra, dec, mjd)
            for s in range(num_sources):
                phase = phase0 * (uu_ * l[s] + vv_ * m[s] + ww_ * (n[s] - 1.0))
                block_model[t, :] += numpy.exp(1.0j * phase)
            ig = i * over_sample + t
            block_data[t, :] = apply_gains(block_model[t, :], gains[:, ig])
            # data_ = apply_gains(block_model[t, :], gains[:, ig])
            # idx = 0
            # for p in range(num_antennas):
            #     for q in range(p + 1, num_antennas):
            #         gp = gains[p, ig]
            #         gq = conj_gains[q, ig]
            #         block_data[t, idx] = gp * block_model[t, idx] * gq
            #         idx += 1
            # if i < 3:
            #     print('t[%02i]' % t)
            #     print(' C:', data_[0])
            #     print(' P:', block_data[t, 0])
            #     print(' D:', numpy.max(numpy.abs(data_[:] - block_data[t, :])))
            # assert numpy.max(numpy.abs(data_[:] - block_data[t, :])) == 0.0

        # Average the block to get visibility data amplitudes for the dump.
        b0 = i * num_baselines
        b1 = b0 + num_baselines
        model[b0:b1] = numpy.mean(block_model, axis=0)
        data[b0:b1] = numpy.mean(block_data, axis=0)

    uu, vv, ww = generate_baseline_uvw(x, y, z, ra, dec, num_dumps,
                                       num_baselines, mjd_start, dump_time_s)

    print('  - Visibilities simulated in %.2f s' % (time.time() - t1))

    return {'model': model, 'data': data, 'uu': uu, 'vv': vv, 'ww': ww,
            'weight': weight, 'num_baselines': num_baselines,
            'num_times': num_dumps, 'num_antennas': num_antennas}
Ejemplo n.º 10
0
lat = 90.0 * (np.pi / 180.)
alt = 0.0
ra0 = 0.0 * (np.pi / 180.)
dec0 = 90.0 * (np.pi / 180.)
ntimes = 10
obs_length = 0.2  # days
# ---------------------------------------------------------

# Generate coordinates.
mjd_start = uvw.datetime_to_mjd(2014, 7, 28, 14, 26, 1.321)
(x, y, z) = uvw.load_station_coords(filename)
(x, y, z) = uvw.convert_enu_to_ecef(x, y, z, lon, lat, alt)
t0 = time.time()
for i in range(0, ntimes):
    mjd = mjd_start
    (uu_m, vv_m, ww_m) = uvw.evaluate_baseline_uvw(x, y, z, ra0, dec0, mjd)
print '- Time taken to evaluate uww coordinates = %.3f ms' % (
    (time.time() - t0) * 1.e3)

# Scatter plot of baseline coordinates.
plt.ioff()
plt_filename = 'test.png'
fig = plt.figure(figsize=(6, 6))
matplotlib.rcParams.update({'font.size': 10})
scatter_size = 3
fig.add_subplot(1, 1, 1, aspect='equal')
plt.scatter(uu_m, vv_m, c='b', lw=0, s=scatter_size)
plt.scatter(-uu_m, -vv_m, c='r', lw=0, s=scatter_size)
plt.xlabel('uu [metres]', fontsize=8)
plt.ylabel('vv [metres]', fontsize=8)
plt.title(filename[-17:])