コード例 #1
0
def DistributeDistance(Nside,
                       Nmaps=None,
                       Rmax=5e4,
                       Rmin=0.1,
                       Bin_seq=None,
                       cutoff=None,
                       filter=None,
                       h=False,
                       OverError=False):
    """
    Distribute distance of the stars, also apply the sky coordinate. 
    Input: - Nside,             int-scalar or list. Not array
           - Nmaps, optional,   Number of maps to generate
           - Rmax,              scalar, distance [pc] to generate maps up to, start from zero, default = 5e4 pc
                                used with Nmaps
           - Rmin,              scalar, distance [pc] to generate maps up to, start from zero, default = 0.1 pc
                                used with Nmaps
           - Bin_seq, optional, list of bin edges, manually made 
           - cutoff, optional,  int-scalar, upper limit of bin edge to iterate up to. Used with Bin_seq
           - filter, optional,  str, either 'mean', 'G', 'B', 'R'. Used in call for AddIntensity() function, 
                                default is 'None'   
           - h, optional,       bool, if True create histograms of the stellar distribution of distance. Else maps
           - OverError, bool    If True also apply the errors for stars with over error, percentile > 1. Else no 
                                computation ov over error
                            
    """
    if (Bin_seq == None) and (Nmaps == None):
        """
        No bins and no Nmaps. Abort
        """
        print('No bin sequence and no Nmaps as input argument.')
        sys.exit()

    # Read files
    f1 = h5py.File('Distance.h5', 'r')
    dist = np.asarray(f1['distance'])
    f1.close()

    dist_err = Tools.DistError(dist)

    # check for nan values in dist:
    ii = Tools.NoDistance(dist)
    dist = dist[ii]
    dist_err = dist_err[ii]
    print('Length of data:', len(dist))

    # radial distance
    rad_dist = np.abs(dist)
    Nside_old = 0

    # Distance percentile:
    percentile = dist_err / rad_dist
    percentile = np.nan_to_num(percentile)

    # Add intesity:
    if filter != None:
        print('Load weights')
        weight = Tools.AddIntensity(filter)
        weight = weight[ii]
    else:
        print('Use no filter')
        weight = np.ones(len(rad_dist))

    # Test input parameters
    if (Bin_seq != None) and (Nmaps != None):
        """ 
        Have both bin seq. and Nmaps, then use Nmaps as default. 
        """

        print('Input both Bin_seq and Nmaps. Use Nmaps.')
        Bin_seq = None

    if Bin_seq == None:
        """
        Use Nmaps, no Bin sequence input
        Nmaps, Rmax, Rmin, rad_dist, dist_err, percentile, weight, ii
        """
        print('Use {} numbers of maps to map the star distribution'.format(
            Nmaps))
        size = (Rmax - Rmin) / Nmaps
        Bins = np.arange(Rmin, Rmax + 10, size)
        print('Bin size: {}, bin edges:'.format(size))
        print(Bins)

        inte_ind = np.where(percentile > 0.01)
        uu_ind, ll_ind = [], []

        over_ind = np.where((percentile > 1) & (percentile < 10)
                            & (rad_dist < 100 * Bins[-1])
                            & (rad_dist > Bins[-1]))
        over_ind = over_ind[0]

        OverUncer = percentile[over_ind]

        # Loop over the the number of maps
        for i in range(Nmaps):

            print('=============')
            ind = np.where((rad_dist > Bins[i]) & (rad_dist <= Bins[i + 1]))
            print('Bin number {}'.format(i))
            rad = rad_dist[ind]

            # Find appropriate Nside
            Ns, Npix = Tools.Find_Nside(Nside, len(rad))
            print('Use Nside:', Nside)

            # load pix coordinate for the given Nside
            if Ns == Nside_old:
                pass
            else:
                # read file
                print('read file for Nside {}'.format(Ns))
                f2 = h5py.File('SPC_Nside_{}.h5'.format(Ns), 'r')
                pixcoord = np.asarray(f2['Healpix coordinates'])
                f2.close()
                pixcoord = pixcoord[ii]

            ####
            # Apply error:
            if filter == None:

                pass

            else:
                w, pixel, rad, iup = Tools.ApplyErrorNmaps(
                    Nmaps, i, ind, Bins, pixcoord, rad_dist, dist_err,
                    percentile, weight, uu_ind, over_ind, OverError)

                uu_ind = iup
            # end apply error if test

            # call plotting
            Tools.PlotMaps(pixel, Ns, Npix, w, rad, Bins, i, filter)

            Nside_old = Ns

            ##################
        # end Nmaps part

    if Nmaps == None:
        """
        Use Bin_seq, no Nmaps as input.
        Bin_seq, Cutoff, rad_dist, dist_err, percentile, weight, ii, filter
        """
        print('Manually defined bins to map the stars, with bin edges:')
        print(Bin_seq)

        # test cut off
        if cutoff == None:
            cutoff = len(Bin_seq)
        if cutoff < 1:
            print('Cut off too low. Exit')
            sys.exit()
        if cutoff > len(Bin_seq):
            print('Cut off too high. Exit')
            sys.exit()

        else:
            print(
                'Use {} bins in distributing the stars after distance.'.format(
                    cutoff - 1))

        # Need the indexes going into each bin
        # Cut off at different distances, able to plot at each distance. Do not save the data for each distance

        inte_ind = np.where(percentile > 0.01)
        uu_ind, ll_ind = [], []
        over_ind = np.where((percentile > 1) & (percentile < 10)
                            & (rad_dist < 100 * Bin_seq[-1])
                            & (rad_dist > Bin_seq[-1]))
        over_ind = over_ind[0]
        OverUncer = percentile[over_ind]

        # Loop over the the number of maps
        for i in range(len(Bin_seq) - 1):

            print('==================')
            ind = np.where((rad_dist >= Bin_seq[i])
                           & (rad_dist < Bin_seq[i + 1]))
            rad = rad_dist[ind]
            print('Bin number {}'.format(i))

            # Find an approproate Nside:
            Ns, Npix = Tools.Find_Nside(Nside, len(rad))

            # load pix coordinate for the given Nside
            if Ns == Nside_old:
                pass
            else:
                # read file
                print('read file for Nside {}'.format(Ns))
                f2 = h5py.File('SPC_Nside_{}.h5'.format(Ns), 'r')
                pixcoord = np.asarray(f2['Healpix coordinates'])
                f2.close()
                pixcoord = pixcoord[ii]

            ###
            # call Apply error
            if filter == None:
                pass

            else:
                # Apply error:
                pix, w, rad, iup = Tools.ApplyErrorBinSeq(
                    Bin_seq, i, ind, pixcoord, rad_dist, dist_err, percentile,
                    weight, uu_ind, over_ind, cutoff, OverError)
                uu_ind = iup

            # end apply error testing

            # Plotting
            Tools.PlotMaps(pix, Ns, Npix, w, rad, Bin_seq, i, filter)

            Nside_old = Ns
            if i == cutoff - 1:
                print('Cut off reached')
                break

    ######

    plt.show()