def make_psf(uu, vv, ww, freq, fov, im_size):
    imager = Imager('single')
    wave_length = 299792458.0 / freq
    uu_ = uu / wave_length
    vv_ = vv / wave_length
    ww_ = ww / wave_length
    amp = numpy.ones(uu.shape, dtype='c16')
    weight = numpy.ones(uu.shape, dtype='f8')
    image = imager.make_image(uu_, vv_, ww_, amp, weight, fov, im_size)
    cell = math.degrees(imager.fov_to_cellsize(math.radians(fov), im_size))
    lm_max = math.sin(0.5 * math.radians(fov))
    lm_inc = (2.0 * lm_max) / im_size
    return {'image': image, 'fov': fov, 'im_size': im_size, 'cell': cell,
            'lm_max': lm_max, 'lm_inc': lm_inc}
def make_psf(uu, vv, ww, ra, dec, freq, fov, im_size, file_name):
    imager = Imager("single")
    wave_length = 299792458.0 / freq
    uu_ = uu / wave_length
    vv_ = vv / wave_length
    ww_ = ww / wave_length
    amp = numpy.ones(uu.shape, dtype="c16")
    weight = numpy.ones(uu.shape, dtype="f8")
    image = imager.make_image(uu_, vv_, ww_, amp, weight, fov, im_size)

    cell = math.degrees(imager.fov_to_cellsize(math.radians(fov), im_size))
    lm_max = math.sin(0.5 * math.radians(fov))
    lm_inc = (2.0 * lm_max) / im_size
    off = lm_inc / 2.0
    extent = [-lm_max - off, lm_max - off, -lm_max + off, lm_max + off]

    fig = pyplot.figure(figsize=(8, 8))
    ax = fig.add_subplot(111, aspect="equal")
    divider = make_axes_locatable(ax)
    cax = divider.append_axes("right", size="5%", pad=0.07)
    im = ax.imshow(image, interpolation="nearest", extent=extent, cmap="inferno")
    cbar = ax.figure.colorbar(im, cax=cax)
    cbar.ax.tick_params(labelsize="small")
    t = numpy.linspace(image.min(), image.max(), 7)
    ax.figure.colorbar(im, cax=cax, ticks=t, format="%.2f")
    ax.set_xlabel("l", fontsize="small")
    ax.set_ylabel("m", fontsize="small")
    ax.tick_params(axis="both", which="major", labelsize="small")
    ax.tick_params(axis="both", which="minor", labelsize="x-small")
    pyplot.savefig(file_name + "_%05.2f_%04i_image_lin.png" % (fov, im_size))
    pyplot.close(fig)

    fig = pyplot.figure(figsize=(8, 8))
    ax = fig.add_subplot(111, aspect="equal")
    divider = make_axes_locatable(ax)
    cax = divider.append_axes("right", size="5%", pad=0.07)
    # http://matplotlib.org/api/colors_api.html
    im = ax.imshow(
        image,
        interpolation="nearest",
        extent=extent,
        cmap="inferno",
        norm=SymLogNorm(vmin=-0.01, vmax=1.0, linthresh=0.005, linscale=0.75),
    )
    cbar = ax.figure.colorbar(im, cax=cax)
    cbar.ax.tick_params(labelsize="small")
    ax.figure.colorbar(im, cax=cax)
    ax.set_xlabel("l", fontsize="small")
    ax.set_ylabel("m", fontsize="small")
    ax.tick_params(axis="both", which="major", labelsize="small")
    ax.tick_params(axis="both", which="minor", labelsize="x-small")
    pyplot.savefig(file_name + "_%05.2f_%04i_image_log.png" % (fov, im_size))
    pyplot.close(fig)

    # x = numpy.arange(-image.shape[0]/2, image.shape[0]/2) * lm_inc
    # x, y = numpy.meshgrid(x, x[::-1])
    # r = (x**2 + y**2)**0.5
    # x_ = r.flatten()
    # y_ = image.flatten()
    # sort_idx = numpy.argsort(x_)
    # x_ = x_[sort_idx]
    # y_ = y_[sort_idx]
    # y_ = numpy.abs(y_)
    # y_max = numpy.nanmax(y_)
    # norm_y = y_ / y_max
    # y_db = 10.0 * numpy.log10(norm_y)
    # num_bins = 200
    # y_mean = numpy.zeros(num_bins)
    # y_std = numpy.zeros(num_bins)
    # x_bin = numpy.zeros(num_bins)
    # bin_inc = (image.shape[0]/2.0 * lm_inc) / float(num_bins)
    # for i in range(num_bins):
    #     r0 = i * bin_inc
    #     r1 = r0 + bin_inc
    #     x_bin[i] = r0 + (r1 - r0) / 2.0
    #     idx = numpy.where(numpy.logical_and(x_ > r0, x_ <= r1))
    #     y_bin = y_db[idx]
    #     y_mean[i] = y_bin.mean()
    #     y_std[i] = y_bin.std()
    # fig = pyplot.figure(figsize=(8, 8))
    # ax = fig.add_subplot(111)
    # ax.plot(x_, y_db, 'k.', alpha=0.01, ms=2.0)
    # ax.plot(x_bin, y_mean, 'r-')
    # # ax.plot(x_bin, y_mean + y_std, 'b-', lw=1.0, alpha=0.5)
    # # ax.plot(x_bin, y_mean - y_std, 'b-', lw=1.0, alpha=0.5)
    # ax.fill_between(x_bin, y_mean - y_std, y_mean + y_std,
    #                 edgecolor='blue', facecolor='blue', alpha=0.6)
    # ax.set_ylim(-40, 0)
    # ax.set_xlim(0.0, image.shape[0] / 2 * lm_inc)
    # ax.set_title(file_name)
    # ax.set_xlabel('Radius')
    # ax.set_ylabel('Decibels')
    # pyplot.savefig(file_name + '_%05.2f_%04i_log_r_ave.png' % (fov, im_size))
    # pyplot.close(fig)

    # ==============
    x = numpy.arange(-image.shape[0] / 2, image.shape[0] / 2) * lm_inc
    x, y = numpy.meshgrid(x, x[::-1])
    r = (x ** 2 + y ** 2) ** 0.5
    x_ = r.flatten()
    y_ = image.flatten()
    sort_idx = numpy.argsort(x_)
    x_ = x_[sort_idx]
    y_ = y_[sort_idx]
    # y_ = numpy.abs(y_)
    num_bins = 200
    y_mean = numpy.zeros(num_bins)
    y_std = numpy.zeros(num_bins)
    x_bin = numpy.zeros(num_bins)
    bin_inc = (image.shape[0] / 2.0 * lm_inc) / float(num_bins)
    for i in range(num_bins):
        r0 = i * bin_inc
        r1 = r0 + bin_inc
        x_bin[i] = r0 + (r1 - r0) / 2.0
        idx = numpy.where(numpy.logical_and(x_ > r0, x_ <= r1))
        y_bin = y_[idx]
        y_mean[i] = y_bin.mean()
        y_std[i] = y_bin.std()
    fig = pyplot.figure(figsize=(8, 8))
    ax = fig.add_subplot(111)
    # ax.plot(x_, y_, 'k.', alpha=0.01, ms=2.0)
    ax.plot(x_bin, y_mean, "r-")
    ax.fill_between(x_bin, y_mean - y_std, y_mean + y_std, edgecolor="blue", facecolor="blue", alpha=0.6)
    ax.set_ylim(-0.005, 0.05)
    ax.set_xlim(0.0, image.shape[0] / 2 * lm_inc)
    # ax.set_yscale('symlog', linthreshy=0.00005)
    # ax.set_yscale('symlog')
    ax.set_title(file_name)
    ax.set_xlabel("Radius")
    ax.grid()
    # ax.set_ylabel('Decibels')
    pyplot.savefig(file_name + "_%05.2f_%04i_lin_r_ave.png" % (fov, im_size))
    pyplot.close(fig)
    # ==================

    # ==============
    x = numpy.arange(-image.shape[0] / 2, image.shape[0] / 2) * lm_inc
    x, y = numpy.meshgrid(x, x[::-1])
    r = (x ** 2 + y ** 2) ** 0.5
    x_ = r.flatten()
    y_ = image.flatten()
    sort_idx = numpy.argsort(x_)
    x_ = x_[sort_idx]
    y_ = y_[sort_idx]
    # y_ = numpy.abs(y_)
    num_bins = 200
    y_mean = numpy.zeros(num_bins)
    y_std = numpy.zeros(num_bins)
    x_bin = numpy.zeros(num_bins)
    bin_inc = (image.shape[0] / 2.0 * lm_inc) / float(num_bins)
    for i in range(num_bins):
        r0 = i * bin_inc
        r1 = r0 + bin_inc
        x_bin[i] = r0 + (r1 - r0) / 2.0
        idx = numpy.where(numpy.logical_and(x_ > r0, x_ <= r1))
        y_bin = y_[idx]
        y_mean[i] = y_bin.mean()
        y_std[i] = y_bin.std()
    fig = pyplot.figure(figsize=(8, 8))
    ax = fig.add_subplot(111)
    # ax.plot(x_, y_, 'k.', alpha=0.01, ms=2.0)
    ax.plot(x_bin, y_mean, "r-")
    ax.fill_between(x_bin, y_mean - y_std, y_mean + y_std, edgecolor="blue", facecolor="blue", alpha=0.6)
    # ax.set_ylim(-0.005, 0.05)
    ax.set_xlim(0.0, image.shape[0] / 2 * lm_inc)
    # ax.set_yscale('symlog', linthreshy=0.1)
    ax.set_yscale("symlog")
    # ax.set_yscale('log', noposy='clip')
    ax.set_title(file_name)
    ax.set_xlabel("Radius")
    ax.grid()
    # ax.set_ylabel('Decibels')
    pyplot.savefig(file_name + "_%05.2f_%04i_log_r_ave.png" % (fov, im_size))
    pyplot.close(fig)
Esempio n. 3
0
def main():
    """
    1. Generate a large-ish core of stations using random generator.
         a. overlap some stations in the core to have a very dense station
            region
    2. After core area start using arms but generate some randomness in the arms
       by placing antennas randomly near the outer stations keeping them along
       the spiral
    3. Remove radius redundancy in the spiral arms
    """

    # =========================================================================

    # ====== Core
    seed = 1
    num_tries = 10
    num_core_stations = (1 + 5 + 11 + 17) * 6 + (3 * 6)
    core_radius_m = 480.0
    inner_core_radius_m = 280.0
    station_radius_m = 35.0 / 2.0
    sll = -28
    # ====== Core arms
    num_arms = 3
    core_arm_count = 4
    stations_per_arm_cluster = 6
    arm_cluster_radius = 75.0
    # a = 300.0
    # b = 0.513
    a = 300.0
    b = 0.513
    delta_theta = math.radians(37.0)
    arm_offsets = numpy.radians([35.0, 155.0, 270.0])
    num_core_arm_stations = num_arms * core_arm_count * stations_per_arm_cluster
    # ====== Outer arms
    outer_arm_count = 12
    stations_per_outer_cluster = 6
    num_clusters_outer = outer_arm_count * num_arms
    v4a_ss_enu_file = 'v7ska1lowN1v2rev3R.enu.94x4.fixed.txt'
    outer_arm_cluster_radius = 80.0

    # ===== uvw coordinate generation.
    lon = radians(116.63128900)
    lat = radians(-26.69702400)
    alt = 0.0
    ra = radians(68.698903779331502)
    dec = radians(-26.568851215532160)
    mjd_mid = 57443.4375000000

    obs_length = 0.0
    mjd_start = mjd_mid
    dt_s = 0.0
    num_times = 1

    obs_length = 2.0 * 3600.0  # seconds
    num_times = int(obs_length / (3.0 * 60.0))
    dt_s = obs_length / float(num_times)
    mjd_start = mjd_mid - ((obs_length / 2.0) / 3600.0 * 24.0)
    print('num times = %i' % num_times)

    out_dir = 'v5c-2h'
    # =========================================================================
    if not os.path.isdir(out_dir):
        os.makedirs(out_dir)

    # Generate core stations
    x_core, y_core, weights, r_weights = \
        generate_random_core(num_core_stations, core_radius_m,
                             inner_core_radius_m, sll, station_radius_m,
                             num_tries, seed)

    # Core arms
    x_arm, y_arm, cx_arm, cy_arm = \
        generate_core_arms(num_arms, core_arm_count, stations_per_arm_cluster,
                           arm_cluster_radius, station_radius_m,
                           a, b, delta_theta, arm_offsets, num_tries)

    # Outer stations.
    x_arm_outer, y_arm_outer, cx_outer, cy_outer = \
        generate_outer_arms(v4a_ss_enu_file, num_clusters_outer,
                            stations_per_outer_cluster,
                            outer_arm_cluster_radius, station_radius_m,
                            num_tries)

    # Plotting
    plot_layout(x_core, y_core, x_arm, y_arm, x_arm_outer, y_arm_outer,
                cx_arm, cy_arm, cx_outer, cy_outer, station_radius_m,
                inner_core_radius_m, core_radius_m, arm_cluster_radius,
                outer_arm_cluster_radius, out_dir)
    plot_core_thinning_profile(r_weights, weights, core_radius_m,
                               inner_core_radius_m, out_dir)

    if uvwsim_found:
        x = numpy.hstack((x_core, x_arm, x_arm_outer))
        y = numpy.hstack((y_core, y_arm, y_arm_outer))
        print('total stations = %i' % x.shape[0])
        num_stations = x.shape[0]
        z = numpy.zeros_like(x)

        num_baselines = num_stations * (num_stations - 1) / 2
        x, y, z = convert_enu_to_ecef(x, y, z, lon, lat, alt)
        uu, vv, ww = generate_baseline_uvw(x, y, z, ra, dec, num_times,
                                           num_baselines, mjd_start,
                                           dt_s)
        plot_hist(uu, vv, join(out_dir, 'uv_hist_%.2fh.png'
                               % (obs_length/3600.0)),
                  'v5c %.2f h' % (obs_length/3600.0))
        plot_uv_dist(uu, vv, station_radius_m, join(out_dir, 'uv_%.2fh'
                                                    % (obs_length/3600.0)))
        # TODO-BM see ALMA memo for plots?
        # TODO-BM Plot of azimuthal variation
        # TODO-BM movie of uv coverage histogram improvement with time?
        # TODO-BM convolve uv response with station beam?!

    print('making image...')
    imager = Imager('single')
    fov = 1.0
    im_size = 2048
    freq = 150.0e6
    wavelength = 299792458.0 / freq
    uu /= wavelength
    vv /= wavelength
    ww /= wavelength
    amp = numpy.ones(uu.shape, dtype='c16')
    weight = numpy.ones(uu.shape, dtype='f8')
    image = imager.make_image(uu, vv, ww, amp, weight, fov, im_size)
    fig = pyplot.figure(figsize=(8, 8))
    ax = fig.add_subplot(111, aspect='equal')
    ax.imshow(image, interpolation='nearest')
    pyplot.show()

    cell = math.degrees(imager.fov_to_cellsize(math.radians(fov), im_size))
    save_fits_image_2(join(out_dir, 'psf.fits'), image, cell, math.degrees(ra),
                      math.degrees(dec), freq)