Exemplo n.º 1
0
    def read_deltas(in_dir, nspec=None):
        data = {}
        ndata = 0
        dels = []

        fi = glob.glob(in_dir + "/*.fits.gz")
        for i, f in enumerate(fi):
            sys.stderr.write("\rread {} of {} {}".format(i, len(fi), ndata))
            hdus = fitsio.FITS(f)
            dels += [delta.from_fitsio(h) for h in hdus[1:]]
            ndata += len(hdus[1:])
            hdus.close()
            if nspec and ndata > nspec:
                break

        phi = [d.ra for d in dels]
        th = [sp.pi / 2. - d.dec for d in dels]
        pix = healpy.ang2pix(kappa.nside_data, th, phi)

        z_min_pix = 10**dels[0].ll[0] / kappa.lambda_abs - 1

        for d, p in zip(dels, pix):
            if p not in data:
                data[p] = []
            data[p].append(d)

            z = 10**d.ll / kappa.lambda_abs - 1.
            z_min_pix = sp.amin(sp.append([z_min_pix], z))
            d.z = z
            d.r_comov = kappa.cosmo.r_comoving(z)
            d.we *= ((1. + z) / (1. + 2.25))**(2.9 - 1.)
            d.project()

        kappa.z_min_pix = z_min_pix
        kappa.angmax = 2.*\
                sp.arcsin(kappa.rt_max/(2.*kappa.cosmo.r_comoving(z_min_pix)))
        kappa.ndata = ndata
        kappa.data = data

        return data
Exemplo n.º 2
0
    data = {}
    ndata = 0

    # initialize randoms
    sp.random.seed(4)

    # loop over input files
    for i, f in enumerate(fi):
        if i % 1 == 0:
            print("\rread {} of {} {}".format(i, len(fi), ndata), end="")

        # read fits or ascii file
        if (args.in_format == 'fits'):
            hdus = fitsio.FITS(f)
            dels = [delta.from_fitsio(h, Pk1D_type=True) for h in hdus[1:]]
        elif (args.in_format == 'ascii'):
            ascii_file = open(f, 'r')
            dels = [delta.from_ascii(line) for line in ascii_file]

        ndata += len(dels)
        print("\n ndata =  ", ndata)
        out = None

        # loop over deltas
        for d in dels:

            # Selection over the SNR and the resolution
            if (d.mean_SNR <= args.SNR_min or d.mean_reso >= args.reso_max):
                continue
Exemplo n.º 3
0
    ### Correct for DLAs
    if not args.dla_vac is None:
        userprint("adding dlas")
        dlas = io.read_dlas(args.dla_vac)
        for p in data:
            for d in data[p]:
                if d.thid in dlas:
                    for dla in dlas[d.thid]:
                        data.add_dla(dla[0], dla[1], mask)

    ### Get delta from picca_delta
    done_delta = None
    f = args.spectrum + "/delta-" + str(pix[0]) + ".fits.gz"
    hdus = fitsio.FITS(f)
    ds = [delta.from_fitsio(h) for h in hdus[1:]]
    for d in ds:
        if (d.plate == args.plate) and (d.mjd
                                        == args.mjd) and (d.fid
                                                          == args.fiberid):
            d.project()
            done_delta = d
            hdus.close()
            break
    if done_delta is None:
        hdus.close()
        userprint("Object not in spectrum")
        sys.exit()

    ### Observed l
    plt.errorbar(10**data.ll, data.fl, linewidth=2, color='black')
Exemplo n.º 4
0
def read_deltas(indir,
                nside,
                lambda_abs,
                alpha,
                zref,
                cosmo,
                nspec=None,
                no_project=False,
                from_image=None):
    '''
    reads deltas from indir
    fills the fields delta.z and multiplies the weights by (1+z)^(alpha-1)/(1+zref)^(alpha-1)
    returns data,zmin_pix
    '''

    fi = []
    indir = os.path.expandvars(indir)
    if from_image is None or len(from_image) == 0:
        if len(indir) > 8 and indir[-8:] == '.fits.gz':
            fi += glob.glob(indir)
        elif len(indir) > 5 and indir[-5:] == '.fits':
            fi += glob.glob(indir)
        else:
            fi += glob.glob(indir + '/*.fits') + glob.glob(indir +
                                                           '/*.fits.gz')
    else:
        for arg in from_image:
            if len(arg) > 8 and arg[-8:] == '.fits.gz':
                fi += glob.glob(arg)
            elif len(arg) > 5 and arg[-5:] == '.fits':
                fi += glob.glob(arg)
            else:
                fi += glob.glob(arg + '/*.fits') + glob.glob(arg +
                                                             '/*.fits.gz')
    fi = sorted(fi)

    dels = []
    ndata = 0
    for i, f in enumerate(fi):
        print("\rread {} of {} {}".format(i, len(fi), ndata))
        if from_image is None:
            hdus = fitsio.FITS(f)
            dels += [delta.from_fitsio(h) for h in hdus[1:]]
            hdus.close()
        else:
            dels += delta.from_image(f)

        ndata = len(dels)
        if not nspec is None:
            if ndata > nspec: break

    ###
    if not nspec is None:
        dels = dels[:nspec]
        ndata = len(dels)

    print("\n")

    phi = [d.ra for d in dels]
    th = [sp.pi / 2. - d.dec for d in dels]
    pix = healpy.ang2pix(nside, th, phi)
    if pix.size == 0:
        raise AssertionError('ERROR: No data in {}'.format(indir))

    data = {}
    zmin = 10**dels[0].ll[0] / lambda_abs - 1.
    zmax = 0.
    for d, p in zip(dels, pix):
        if not p in data:
            data[p] = []
        data[p].append(d)

        z = 10**d.ll / lambda_abs - 1.
        zmin = min(zmin, z.min())
        zmax = max(zmax, z.max())
        d.z = z
        if not cosmo is None:
            d.r_comov = cosmo.r_comoving(z)
            d.rdm_comov = cosmo.dm(z)
        d.we *= ((1 + z) / (1 + zref))**(alpha - 1)

        if not no_project:
            d.project()

    return data, ndata, zmin, zmax