コード例 #1
0
ファイル: MOCregion.py プロジェクト: sidmohite/gwemopt
    def create_moc(self):
        """
        Multi-Order coverage map (MOC) of sky area enclosed within a contour plot
        at a given confidence level.
    
        (Function adapted from Giuseppe Greco github repository)
        """

        #reading skymap
        hpx = hp.read_map( self.path+self.filename, verbose = False )
        npix = len( hpx )
        nside = hp.npix2nside( npix )
 
        # Save nside
        self.nside = nside

        sort = sorted( hpx, reverse = True )
        cumsum = np.cumsum( sort )
        index, value = min( enumerate( cumsum ), key = lambda x: abs( x[1] - self.percentage ) )

        # finding ipix indices confined in a given percentage 
        index_hpx = range( 0, len( hpx ) )
        hpx_index = np.c_[ hpx, index_hpx ]

        sort_2array = sorted( hpx_index, key = lambda x: x[0], reverse = True )
        value_contour = sort_2array[ 0:index ]

        j = 1 
        table_ipix_contour = [ ]

        for i in range ( 0, len( value_contour ) ):
            ipix_contour = int( value_contour[i][j] )
            table_ipix_contour.append( ipix_contour )
          
        # from index to polar coordinates
        theta, phi = hp.pix2ang( nside, table_ipix_contour )

        # converting these to right ascension and declination in degrees
        ra = np.rad2deg( phi )
        dec = np.rad2deg( 0.5 * np.pi - theta )

        # Save ra, dec of the pixel associated with highest probability
        self.RA, self.DEC = ra[0], dec[0]

        # creating an astropy.table with RA[deg] and DEC[deg] ipix positions
        contour_ipix = Table([ ra, dec ], names = ('RA[deg]', 'DEC[deg]'), 
                             meta = {'ipix': 'ipix table'})

        # setting MOC order
        moc_order = int( log( nside, 2 ) )

        # creating a MOC map from the contour_ipix table
        #moc = MOC.from_table( contour_ipix, 'RA[deg]', 'DEC[deg]', moc_order )
        moc = MOC.from_lonlat(contour_ipix['RA[deg]'].T * u.deg, contour_ipix['DEC[deg]'].T * u.deg,moc_order)

        # writing MOC file in fits
        #moc.write(path=short_name + '_MOC_' + str( percentage ) +'.json',format='json')

        # Serialise to json dictionary
        self.moc = moc.serialize(format='json')
コード例 #2
0
    def create_moc(self):
        """Creating a MOC map from the contour_ipix table."""
        import sys
        sys.path.append(
            '/anaconda3/lib/python3.6/site-packages/MOCPy-0.4.9-py3.6.egg')
        from mocpy import MOC
        import astropy.units as u
        self.moc = MOC.from_lonlat(self.contour_ipix['RA[deg]'].T * u.deg,
                                   self.contour_ipix['DEC[deg]'].T * u.deg,
                                   max_norder=self.moc_order)

        return self.moc
コード例 #3
0
def get_catalog_moc(row):
    lon = u.Quantity(row['ra'] * u.deg)
    lat = u.Quantity(row['dec'] * u.deg)
    temp_moc = MOC.from_lonlat(lon, lat, max_norder=MAX_DEPTH)

    return temp_moc
コード例 #4
0
def moc_confidence_region(prob, id_object, percentage):
    """It returns a confidence region that encloses  a given percentage of the localization probability 
       using the Multi Order Coverage map (MOC) method. The sky localization area  (in square degrees)
       is also provided for the confidence region. 
        
    Parameters
    ----------   
    infile : `str`
        the HEALPix fits file name, the local file or the link location
    percentage : `float`
        the decimal percentage of the location probability (from 0 to 1)
    
    Returns
    -------
    A local file in which the MOC is serialized in "fits" format. The file is named :
    "moc_"+'%.1f' % percentage+'_'+skymap
    ----------------------------------------------------
    "moc_" :`str, default`
        default string as file prefix
    '%.1f' % percentage+' : `str, default`
        decimal percentage passed to the function 
    skymap : `str, default`
        string after the last slash and parsing for "." 
        from infile parameter
    ----------------------------------------------------
    area_sq2 : `float, default`
        the area of the moc confidence region in square degrees.
        """

    # reading skymap
    #prob = hp.read_map(infile, verbose = False)
    npix = len(prob)
    nside = hp.npix2nside(npix)  #healpix resolution

    cumsum = np.sort(prob)[::-1].cumsum()  # sort and cumulative sum

    # finding the minimum credible region
    how_many_ipixs, cut_percentage = min(enumerate(cumsum),
                                         key=lambda x: abs(x[1] - percentage))
    del (cumsum)
    #print ('number of ipixs',how_many_ipixs,'; cut@', round(cut_percentage,3))

    indices = range(0, len(prob))
    prob_indices = np.c_[prob, indices]

    sort = prob_indices[prob_indices[:, 0].argsort()[::-1]]
    ipixs = sort[0:how_many_ipixs + 1, [1]].astype(int)

    # from index to polar coordinates
    theta, phi = hp.pix2ang(nside, ipixs)

    # converting these to right ascension and declination in degrees
    ra = np.rad2deg(phi)
    dec = np.rad2deg(0.5 * np.pi - theta)

    # creating an astropy.table with RA[deg] and DEC[deg]
    contour_ipix = Table([ra, dec],
                         names=('RA', 'DEC'),
                         meta={'name': 'first table'})

    order = int(log(nside, 2))

    # moc from table
    moc = MOC.from_lonlat(contour_ipix['RA'].T * u.deg,
                          contour_ipix['DEC'].T * u.deg, order)

    # getting skymap name
    skymap = id_object.rsplit('/', 1)[-1].rsplit('.')[0]
    moc_name = "moc_" + '%.1f' % percentage + "_" + skymap

    # return moc region in fits format
    moc.write(
        moc_name,
        format="fits",
    )

    # square degrees in a whole sphere
    from math import pi
    square_degrees_sphere = (360.0**2) / pi

    moc = MOC.from_fits(moc_name)

    # printing sky area at the given percentage
    area_sq2 = round((moc.sky_fraction * square_degrees_sphere), 1)
    print('The ' + str((percentage * 100)) + '% of ' + moc_name + ' is ',
          area_sq2, 'sq. deg.')