def find_map_dimensions(map_in, silent=False):
    r"""print various aspects of the maps"""
    if isinstance(map_in, str):
        map_in = algebra.make_vect(algebra.load(map_in))
    cosmology = Cosmology()

    ra_axis = map_in.get_axis('ra')
    #print ra_axis
    mmd_ra = axis_param(ra_axis)

    dec_axis = map_in.get_axis('dec')
    #print dec_axis
    mmd_dec = axis_param(dec_axis)

    freq_axis = map_in.get_axis('freq') / 1.e6
    mmd_freq = axis_param(freq_axis)

    beam_data = sp.array([
        0.316148488246, 0.306805630985, 0.293729620792, 0.281176247549,
        0.270856788455, 0.26745856078, 0.258910010848, 0.249188429031
    ])
    freq_data = sp.array([695, 725, 755, 785, 815, 845, 875, 905], dtype=float)
    beam_FWHM = interp1d(freq_data, beam_data)

    if (mmd_freq[0] < freq_data.min()):
        beam_minfreq = beam_FWHM(freq_data.min())
        #print "warning: no beam data for this freq, using %s" % beam_minfreq
    else:
        beam_minfreq = beam_FWHM(mmd_freq[0])

    if (mmd_freq[1] > freq_data.max()):
        beam_maxfreq = beam_FWHM(freq_data.max())
        #print "warning: no beam data for this freq, using %s" % beam_maxfreq
    else:
        beam_maxfreq = beam_FWHM(mmd_freq[1])

    ra_fact = sp.cos(sp.pi * map_in.info['dec_centre'] / 180.0)
    dracosdec = mmd_ra[2] * ra_fact

    pixelfrac_ramin = dracosdec / beam_minfreq
    pixelfrac_decmin = mmd_dec[2] / beam_minfreq
    pixelfrac_ramax = dracosdec / beam_maxfreq
    pixelfrac_decmax = mmd_dec[2] / beam_maxfreq

    # cosmological sizes
    dfreq = mmd_freq[2]
    z1 = cc.freq_21cm_MHz / mmd_freq[0] - 1.  # z at min freq
    z2 = cc.freq_21cm_MHz / mmd_freq[1] - 1.  # z at max freq
    dz1 = cc.freq_21cm_MHz / (mmd_freq[0] + dfreq) - 1.
    dz2 = cc.freq_21cm_MHz / (mmd_freq[1] - dfreq) - 1.
    d1 = cosmology.proper_distance(z1)
    d2 = cosmology.proper_distance(z2)
    c1 = cosmology.comoving_distance(z1)
    c2 = cosmology.comoving_distance(z2)
    dc1 = cosmology.comoving_distance(dz1)
    dc2 = cosmology.comoving_distance(dz2)

    ra_extent_z1 = mmd_ra[3] * d1 * units.degree * ra_fact
    ra_pixel_extent_z1 = mmd_ra[2] * d1 * units.degree * ra_fact
    ra_extent_z2 = mmd_ra[3] * d2 * units.degree * ra_fact
    ra_pixel_extent_z2 = mmd_ra[2] * d2 * units.degree * ra_fact
    dec_extent_z1 = mmd_dec[3] * d1 * units.degree
    dec_pixel_extent_z1 = mmd_dec[2] * d1 * units.degree
    dec_extent_z2 = mmd_dec[3] * d2 * units.degree
    dec_pixel_extent_z2 = mmd_dec[2] * d2 * units.degree

    redshift_extent = c1 - c2
    redshift_pixel_extent_z1 = c1 - dc1
    redshift_pixel_extent_z2 = dc2 - c2

    if not silent:
        infolist = [
            'ra_centre', 'ra_delta', 'dec_centre', 'dec_delta', 'freq_centre',
            'freq_delta'
        ]
        for key in infolist:
            print "%s: %s" % (key, map_in.info[key])

        sexagesimal = misc.radec_to_sexagesimal(map_in.info["ra_centre"],
                                                map_in.info["dec_centre"])
        (ralong, declong, decsign) = sexagesimal
        print "RA: %dH:%dm:%10.5fs, dec %s %dd:%dm:%10.5fs" % \
              (ralong[0], ralong[1], ralong[2], decsign, declong[0], declong[1],
               declong[2])

        shp = map_in.shape
        print shp
        # 2^30 or 10^9...
        covsize = shp[0] * shp[1]**2. * shp[2]**2. * 4. / 2.**30.
        print "covariance size in GB: %g" % covsize
        print "RA min=%g deg, max=%g deg, delta=%g deg, extent=%g deg, N=%d" % mmd_ra
        print "Dec min=%g deg, max=%g deg, delta=%g deg, extent=%g deg, N=%d" % mmd_dec
        print "Freq min=%g MHz, max=%g MHz, delta=%g MHz, extent=%g MHz, N=%d" % mmd_freq

        print "Note that these fractions are in delta RA * cos(Dec)"
        print "beam FWHM at %g MHz=%g deg; pixel frac RA, Dec=(%g, %g)" % \
              (mmd_freq[0], beam_minfreq, pixelfrac_ramin, pixelfrac_decmin)

        print "beam FWHM at %g MHz=%g deg; pixel frac RA, Dec=(%g, %g)" % \
              (mmd_freq[1], beam_maxfreq, pixelfrac_ramax, pixelfrac_decmax)

        print "redshift range: z=%g-%g" % (z2, z1)
        print "proper distance range (h^-1 cMpc): d=%g-%g" % (d2, d1)
        print "comoving distance range (h^-1 cMpc): d=%g-%g, extent = %g" % \
              (c2, c1, redshift_extent)
        print "at %g MHz, width in RA=%g Dec=%g h^-1 cMpc" % \
              (mmd_freq[0], ra_extent_z1, dec_extent_z1)
        print "at %g MHz, pixel width in RA=%g Dec=%g h^-1 cMpc" % \
              (mmd_freq[0], ra_pixel_extent_z1, dec_pixel_extent_z1)
        print "the freq bin starting at %g MHz has width %g" % \
                (mmd_freq[0], redshift_pixel_extent_z1)

        print "at %g MHz, width in RA=%g Dec=%g h^-1 cMpc" % \
              (mmd_freq[1], ra_extent_z2, dec_extent_z2)
        print "at %g MHz, pixel width in RA=%g Dec=%g h^-1 cMpc" % \
              (mmd_freq[1], ra_pixel_extent_z2, dec_pixel_extent_z2)
        print "the freq bin starting at %g MHz has width %g" % \
                (mmd_freq[1], redshift_pixel_extent_z2)

        print "-" * 80

    # return the sampling at the finest resolution
    return (pixelfrac_ramax, pixelfrac_decmax)
def find_map_dimensions(map_in, silent=False):
    r"""print various aspects of the maps"""
    if isinstance(map_in, str):
        map_in = algebra.make_vect(algebra.load(map_in))
    cosmology = Cosmology()

    ra_axis = map_in.get_axis("ra")
    # print ra_axis
    mmd_ra = axis_param(ra_axis)

    dec_axis = map_in.get_axis("dec")
    # print dec_axis
    mmd_dec = axis_param(dec_axis)

    freq_axis = map_in.get_axis("freq") / 1.0e6
    mmd_freq = axis_param(freq_axis)

    beam_data = sp.array(
        [
            0.316148488246,
            0.306805630985,
            0.293729620792,
            0.281176247549,
            0.270856788455,
            0.26745856078,
            0.258910010848,
            0.249188429031,
        ]
    )
    freq_data = sp.array([695, 725, 755, 785, 815, 845, 875, 905], dtype=float)
    beam_FWHM = interp1d(freq_data, beam_data)

    if mmd_freq[0] < freq_data.min():
        beam_minfreq = beam_FWHM(freq_data.min())
        # print "warning: no beam data for this freq, using %s" % beam_minfreq
    else:
        beam_minfreq = beam_FWHM(mmd_freq[0])

    if mmd_freq[1] > freq_data.max():
        beam_maxfreq = beam_FWHM(freq_data.max())
        # print "warning: no beam data for this freq, using %s" % beam_maxfreq
    else:
        beam_maxfreq = beam_FWHM(mmd_freq[1])

    ra_fact = sp.cos(sp.pi * map_in.info["dec_centre"] / 180.0)
    dracosdec = mmd_ra[2] * ra_fact

    pixelfrac_ramin = dracosdec / beam_minfreq
    pixelfrac_decmin = mmd_dec[2] / beam_minfreq
    pixelfrac_ramax = dracosdec / beam_maxfreq
    pixelfrac_decmax = mmd_dec[2] / beam_maxfreq

    # cosmological sizes
    dfreq = mmd_freq[2]
    z1 = cc.freq_21cm_MHz / mmd_freq[0] - 1.0  # z at min freq
    z2 = cc.freq_21cm_MHz / mmd_freq[1] - 1.0  # z at max freq
    dz1 = cc.freq_21cm_MHz / (mmd_freq[0] + dfreq) - 1.0
    dz2 = cc.freq_21cm_MHz / (mmd_freq[1] - dfreq) - 1.0
    d1 = cosmology.proper_distance(z1)
    d2 = cosmology.proper_distance(z2)
    c1 = cosmology.comoving_distance(z1)
    c2 = cosmology.comoving_distance(z2)
    dc1 = cosmology.comoving_distance(dz1)
    dc2 = cosmology.comoving_distance(dz2)

    ra_extent_z1 = mmd_ra[3] * d1 * units.degree * ra_fact
    ra_pixel_extent_z1 = mmd_ra[2] * d1 * units.degree * ra_fact
    ra_extent_z2 = mmd_ra[3] * d2 * units.degree * ra_fact
    ra_pixel_extent_z2 = mmd_ra[2] * d2 * units.degree * ra_fact
    dec_extent_z1 = mmd_dec[3] * d1 * units.degree
    dec_pixel_extent_z1 = mmd_dec[2] * d1 * units.degree
    dec_extent_z2 = mmd_dec[3] * d2 * units.degree
    dec_pixel_extent_z2 = mmd_dec[2] * d2 * units.degree

    redshift_extent = c1 - c2
    redshift_pixel_extent_z1 = c1 - dc1
    redshift_pixel_extent_z2 = dc2 - c2

    if not silent:
        infolist = ["ra_centre", "ra_delta", "dec_centre", "dec_delta", "freq_centre", "freq_delta"]
        for key in infolist:
            print "%s: %s" % (key, map_in.info[key])

        sexagesimal = misc.radec_to_sexagesimal(map_in.info["ra_centre"], map_in.info["dec_centre"])
        (ralong, declong, decsign) = sexagesimal
        print "RA: %dH:%dm:%10.5fs, dec %s %dd:%dm:%10.5fs" % (
            ralong[0],
            ralong[1],
            ralong[2],
            decsign,
            declong[0],
            declong[1],
            declong[2],
        )

        shp = map_in.shape
        print shp
        # 2^30 or 10^9...
        covsize = shp[0] * shp[1] ** 2.0 * shp[2] ** 2.0 * 4.0 / 2.0 ** 30.0
        print "covariance size in GB: %g" % covsize
        print "RA min=%g deg, max=%g deg, delta=%g deg, extent=%g deg, N=%d" % mmd_ra
        print "Dec min=%g deg, max=%g deg, delta=%g deg, extent=%g deg, N=%d" % mmd_dec
        print "Freq min=%g MHz, max=%g MHz, delta=%g MHz, extent=%g MHz, N=%d" % mmd_freq

        print "Note that these fractions are in delta RA * cos(Dec)"
        print "beam FWHM at %g MHz=%g deg; pixel frac RA, Dec=(%g, %g)" % (
            mmd_freq[0],
            beam_minfreq,
            pixelfrac_ramin,
            pixelfrac_decmin,
        )

        print "beam FWHM at %g MHz=%g deg; pixel frac RA, Dec=(%g, %g)" % (
            mmd_freq[1],
            beam_maxfreq,
            pixelfrac_ramax,
            pixelfrac_decmax,
        )

        print "redshift range: z=%g-%g" % (z2, z1)
        print "proper distance range (h^-1 cMpc): d=%g-%g" % (d2, d1)
        print "comoving distance range (h^-1 cMpc): d=%g-%g, extent = %g" % (c2, c1, redshift_extent)
        print "at %g MHz, width in RA=%g Dec=%g h^-1 cMpc" % (mmd_freq[0], ra_extent_z1, dec_extent_z1)
        print "at %g MHz, pixel width in RA=%g Dec=%g h^-1 cMpc" % (
            mmd_freq[0],
            ra_pixel_extent_z1,
            dec_pixel_extent_z1,
        )
        print "the freq bin starting at %g MHz has width %g" % (mmd_freq[0], redshift_pixel_extent_z1)

        print "at %g MHz, width in RA=%g Dec=%g h^-1 cMpc" % (mmd_freq[1], ra_extent_z2, dec_extent_z2)
        print "at %g MHz, pixel width in RA=%g Dec=%g h^-1 cMpc" % (
            mmd_freq[1],
            ra_pixel_extent_z2,
            dec_pixel_extent_z2,
        )
        print "the freq bin starting at %g MHz has width %g" % (mmd_freq[1], redshift_pixel_extent_z2)

        print "-" * 80

    # return the sampling at the finest resolution
    return (pixelfrac_ramax, pixelfrac_decmax)
Exemple #3
0
from utils import cosmology
from simulations import corr
from utils.cosmology import Cosmology

redshift = 1.

cosmo = Cosmology()
proper = cosmo.proper_distance(redshift)
comoving = cosmo.comoving_distance(redshift)
comoving_inv = cosmology.inverse_approx(cosmo.comoving_distance, 0.6, 1.)

print proper, comoving, comoving_inv(2355.35909781)

Exemple #4
0
def make_cube_movie(source_key, colorbar_title, frame_dir,
                    filetag_suffix="", saveslice=None, saveslice_file=None,
                    outputdir="./", sigmarange=6., ignore=None, multiplier=1.,
                    transverse=False, title=None, sigmacut=None,
                    logscale=False, physical=False, convolve=False, tag=None):
    """Make a stack of spatial slice maps and animate them
    transverse plots along RA and freq and image plane is in Dec
    First mask any points that exceed `sigmacut`, and then report the extent of
    `sigmarange` away from the mean
    """
    datapath_db = data_paths.DataPath()
    cosmology = Cosmology()
    littleh = (cosmology.H0 / 100.0)

    beam_data = np.array([0.316148488246, 0.306805630985, 0.293729620792,
                          0.281176247549, 0.270856788455, 0.26745856078,
                          0.258910010848, 0.249188429031])

    freq_data = np.array([695, 725, 755, 785, 815, 845, 875, 905],
                             dtype=float)

    beam_fwhm = interp1d(freq_data, beam_data)
    freq_data_Hz = freq_data * 1.0e6

    if tag is None:
        tag = '_'.join(source_key.split(";"))
        tag = '-'.join(tag.split(":"))

        # for a given path
        #tag = ".".join(source_key.split(".")[:-1])  # extract root name
        #tag = tag.split("/")[-1]

    if title is None:
        title = "%(tag)s (i = %(index)d, "
        title += "freq = %(freq)3.1f MHz, z = %(redshift)3.3f, "
        title += "Dc=%(distance)3.0f cMpc)"

    print tag
    fileprefix = frame_dir + tag
    nlevels = 500

    if transverse:
        orientation = "_freqRA"
    else:
        orientation = "_RADec"

    # prepare the data
    #cube = algebra.make_vect(algebra.load(source_key)) * multiplier
    cube =  datapath_db.fetch_multi(source_key) * multiplier
    if logscale:
        cube[cube < 0] = np.min(np.abs(cube))
        cube = np.log10(cube)

    isnan = np.isnan(cube)
    isinf = np.isinf(cube)
    maskarray = ma.mask_or(isnan, isinf)

    if ignore is not None:
        maskarray = ma.mask_or(maskarray, (cube == ignore))

    convolved = ""
    if convolve:
        convolved = "_convolved"

        beamobj = beam.GaussianBeam(beam_data, freq_data_Hz)
        cube = beamobj.apply(cube)

    if sigmacut:
        #np.set_printoptions(threshold=np.nan, precision=4)
        deviation = np.abs((cube - np.mean(cube)) / np.std(cube))
        extend_maskarray = (cube > (sigmacut * deviation))
        maskarray = ma.mask_or(extend_maskarray, maskarray)

    mcube = ma.masked_array(cube, mask=maskarray)

    try:
        whmaskarray = np.where(maskarray)[0]
        mask_fraction = float(len(whmaskarray)) / float(cube.size)
    except:
        mask_fraction = 0.

    print "fraction of map clipped: %f" % mask_fraction
    (cube_mean, cube_std) = (mcube.mean(), mcube.std())
    print "cube mean=%g std=%g" % (cube_mean, cube_std)

    try:
        len(sigmarange)
        color_axis = np.linspace(sigmarange[0], sigmarange[1],
                                nlevels, endpoint=True)
    except TypeError:
        if (sigmarange > 0.):
            color_axis = np.linspace(cube_mean - sigmarange * cube_std,
                                    cube_mean + sigmarange * cube_std,
                                    nlevels, endpoint=True)
        else:
            if saveslice is not None:
                color_axis = np.linspace(ma.min(mcube[saveslice, :, :]),
                                         ma.max(mcube[saveslice, :, :]),
                                         nlevels, endpoint=True)
            else:
                color_axis = np.linspace(ma.min(mcube),  ma.max(mcube),
                                        nlevels, endpoint=True)

    print "using range: [%g, %g]" % (np.min(color_axis), np.max(color_axis))

    freq_axis = cube.get_axis('freq')
    (ra_axis, dec_axis) = (cube.get_axis('ra'), cube.get_axis('dec'))

    runlist = []
    # TODO: make transverse work with gnuplot
    if transverse:
        for decind in range(cube.shape[2]):
            fulltitle = tag + " (dec = %3.1f)" % (dec_axis[decind])
            runlist.append((decind, cube[:, :, decind], freq_axis,
                            ra_axis, color_axis, ["Freq", "Ra"], 20.,
                            fulltitle, colorbar_title, fileprefix, 800.))
    else:
        for freqind in range(cube.shape[0]):
            outfilename = fileprefix + str('.%03d' % freqind) + '.jpeg'

            # if in angle, freq coordinates
            if not physical:
                freq_MHz = freq_axis[freqind] / 1.e6
                z_here = cc.freq_21cm_MHz / freq_MHz - 1.
                comoving_distance = cosmology.comoving_distance(z_here) / littleh
                proper_distance = cosmology.proper_distance(z_here) / littleh
                angular_scale = 20. / units.degree / proper_distance

                title_info = {"index": freqind,
                              "redshift": z_here,
                              "distance": comoving_distance,
                              "freq": freq_MHz,
                              "tag": tag}

                fulltitle = title % title_info

                if (freq_MHz <= freq_data.min()) or \
                    (freq_MHz >= freq_data.max()):
                    fwhm = 0.
                else:
                    fwhm = beam_fwhm(freq_MHz)

                FWHM_circle = {"primitive": "circle",
                               "center_x": 0.9,
                               "center_y": 0.15,
                               "radius": fwhm / 2.,
                               "width": 5,
                               "color": "purple" }

                region_scale = {"primitive": "rect",
                               "center_x": 0.9,
                               "center_y": 0.15,
                               "size_x": angular_scale,
                               "size_y": angular_scale,
                               "width": 3,
                               "color": "black" }

                draw_objects = [FWHM_circle, region_scale]

                runlist.append((outfilename, cube[freqind, :, :], ra_axis,
                                dec_axis, color_axis, ["RA", "Dec"], 1.,
                                fulltitle, colorbar_title, draw_objects))

            else:
                distance = freq_axis[freqind] / 1.e6
                fulltitle = "%s (i = %d, Dc = %10.3f cMpc)" % \
                             (title, freqind, distance)

                runlist.append((outfilename, cube[freqind, :, :], ra_axis,
                                dec_axis, color_axis,
                                ["x (RA, cMpc/h)", "y (Dec, cMpc/h)"], 1.,
                                fulltitle, colorbar_title, draw_objects))


    if saveslice is not None:
        print "saving just slice %d" % saveslice
        (outfilename, cube_slice, xaxis, yaxis, vaxis, xylabels, \
            aspect, fulltitle, cbar_title, draw_objects) = runlist[saveslice]

        plot_slice.gnuplot_2D(outfilename, cube_slice, xaxis, yaxis, vaxis, xylabels,
                              aspect, fulltitle, cbar_title,
                              eps_outfile=saveslice_file,
                              draw_objects=draw_objects)
    else:
        pool = multiprocessing.Pool(processes=(multiprocessing.cpu_count() - 4))
        pool.map(gnuplot_single_slice, runlist)
        #gnuplot_single_slice(runlist[0])  # for troubleshooting

        argument = frame_dir + tag + '.%03d.jpeg'
        outfile = outputdir + tag + filetag_suffix + orientation + convolved + '.mp4'
        subprocess.check_call(('ffmpeg', '-vb', '2500000', '-r', '10',
                               '-y', '-i', argument, outfile))

        for fileindex in range(len(runlist)):
            os.remove(fileprefix + str('.%03d' % fileindex) + '.jpeg')