Example #1
0
    def _get_components_est(self, comp, instr, data, beta, pixok):
        print(beta, beta.shape)
        """

        This function estimate components from MixingMatrix of fgbuster with estimated parameters

        """
        nside = 256
        A = fgbuster.mixingmatrix.MixingMatrix(*comp)
        A_ev = A.evaluator(self.config['frequency'])
        invN = np.diag(hp.nside2resol(nside, arcmin=True) / (instr.depth_p))**2
        newmaps = np.zeros((len(comp), 3, 12 * nside**2))

        if beta.shape[-1] >= 12:
            ind = np.where(pixok != 0)[0]
            maps_separe = np.zeros((len(comp), 2, 12 * nside**2))
            invN = np.diag(
                hp.nside2resol(nside, arcmin=True) / (instr.depth_p))**2
            for i in range(len(ind)):
                A_maxL = A_ev(np.array(beta)[:, ind[i]])
                maps_separe[:, :, ind[i]] = fgbuster.algebra.Wd(A_maxL,
                                                                data[:, :,
                                                                     ind[i]].T,
                                                                invN=invN).T
        else:
            A_maxL = A_ev(beta)
            maps_separe = fgbuster.algebra.Wd(A_maxL, data.T, invN=invN).T
        newmaps[:, 1:, :] = maps_separe.copy()

        return newmaps
Example #2
0
 def __init__(self,
              nside=128,
              lonCol='fieldRA',
              latCol='fieldDec',
              verbose=True,
              badval=hp.UNSEEN,
              useCache=True,
              leafsize=100,
              radius=1.75,
              useCamera=False,
              rotSkyPosColName='rotSkyPos',
              mjdColName='expMJD',
              chipNames='all'):
     """Instantiate and set up healpix slicer object."""
     super(HealpixSlicer, self).__init__(verbose=verbose,
                                         lonCol=lonCol,
                                         latCol=latCol,
                                         badval=badval,
                                         radius=radius,
                                         leafsize=leafsize,
                                         useCamera=useCamera,
                                         rotSkyPosColName=rotSkyPosColName,
                                         mjdColName=mjdColName,
                                         chipNames=chipNames)
     # Valid values of nside are powers of 2.
     # nside=64 gives about 1 deg resolution
     # nside=256 gives about 13' resolution (~1 CCD)
     # nside=1024 gives about 3' resolution
     # Check validity of nside:
     if not (hp.isnsideok(nside)):
         raise ValueError('Valid values of nside are powers of 2.')
     self.nside = int(nside)
     self.pixArea = hp.nside2pixarea(self.nside)
     self.nslice = hp.nside2npix(self.nside)
     self.spatialExtent = [0, self.nslice - 1]
     self.shape = self.nslice
     if self.verbose:
         print('Healpix slicer using NSIDE=%d, ' % (self.nside) + \
               'approximate resolution %f arcminutes' % (hp.nside2resol(self.nside, arcmin=True)))
     # Set variables so slicer can be re-constructed
     self.slicer_init = {
         'nside': nside,
         'lonCol': lonCol,
         'latCol': latCol,
         'radius': radius
     }
     if useCache:
         # useCache set the size of the cache for the memoize function in sliceMetric.
         binRes = hp.nside2resol(nside)  # Pixel size in radians
         # Set the cache size to be ~2x the circumference
         self.cacheSize = int(np.round(4. * np.pi / binRes))
     # Set up slicePoint metadata.
     self.slicePoints['nside'] = nside
     self.slicePoints['sid'] = np.arange(self.nslice)
     self.slicePoints['ra'], self.slicePoints['dec'] = self._pix2radec(
         self.slicePoints['sid'])
     # Set the default plotting functions.
     self.plotFuncs = [
         HealpixSkyMap, HealpixHistogram, HealpixPowerSpectrum
     ]
Example #3
0
    def get_noise(self, sensitiviy_mode=2, one_over_f_mode=2):
        # TODO: put SAT and LAT caracteristics in class atributes
        if self.instrument == 'SAT':
            print('SAT white noise')
            # noise_covariance = np.eye(12)
            # inv_noise = np.eye(12)
            white_noise = np.repeat(
                V3.so_V3_SA_noise(sensitiviy_mode, one_over_f_mode, 1, 0.1,
                                  self.nside * 3)[2], 2, 0)

            # noise_covariance = np.diag(
            #     (white_noise / hp.nside2resol(self.nside, arcmin=True))**2)
            # inv_noise = np.diag((hp.nside2resol(
            #     self.nside, arcmin=True)/white_noise)**2)
            #
            # noise_N_ell = np.repeat(
            #     V3.so_V3_SA_noise(sensitiviy_mode, one_over_f_mode, 1, 0.1,
            #                       self.nside*3, beam_corrected=True)[1],
            #     2, 0)
            # ells = np.shape(noise_N_ell)[-1]
            # noise_cov_ell = [np.diag(noise_N_ell[:, k]) for k in range(ells)]
            # inv_noise_cov_ell = [np.diag(1/noise_N_ell[:, k])
            #                      for k in range(ells)]

        elif self.instrument == 'LAT':
            white_noise = np.repeat(V3.so_V3_LA_noise(0, 0.4, 5000)[2], 2, 0)
            noise_covariance = np.diag(
                (white_noise / hp.nside2resol(self.nside, arcmin=True))**2)
            inv_noise = np.diag(
                1 / (white_noise / hp.nside2resol(self.nside, arcmin=True))**2)

        elif self.instrument == 'LiteBIRD':
            sensitivity = np.array([
                37.5, 24.0, 19.9, 16.2, 13.5, 11.7, 9.2, 7.6, 5.9, 6.5, 5.8,
                7.7, 13.2, 19.5, 37.5
            ])
        elif self.instrument == 'Planck':
            print('Planck white noise')
            # noise_covariance = np.eye(12)
            # inv_noise = np.eye(12)

            white_noise = np.repeat(get_instrument('planck_P')['sens_P'], 2, 0)

        noise_covariance = np.diag(
            (white_noise / hp.nside2resol(self.nside, arcmin=True))**2)
        inv_noise = np.diag(
            (hp.nside2resol(self.nside, arcmin=True) / white_noise)**2)

        # noise_N_ell = np.repeat(
        #     V3.so_V3_SA_noise(sensitiviy_mode, one_over_f_mode, 1, 0.1,
        #                       self.nside*3, beam_corrected=True)[1],
        #     2, 0)
        # ells = np.shape(noise_N_ell)[-1]
        # noise_cov_ell = [np.diag(noise_N_ell[:, k]) for k in range(ells)]
        # inv_noise_cov_ell = [np.diag(1/noise_N_ell[:, k])
        #                      for k in range(ells)]

        self.noise_covariance = noise_covariance
        self.inv_noise = inv_noise
Example #4
0
    def ToHealpix(self, nside, RA, Dec=None, T=None, replace=True):
        ''' 
		replace:
			=True : if there are several values in 1 pixel, use the lagest value to fill this pixel (for diffuse emission)
			=False : if there are several values in 1 pixel, fill this pixel with the sum of all values (for point sources)
	
		RA, Dec:
			in rad
			RA  or lon : 0 ~ 2pi
			Dec or lat : -pi/2 ~ pi/2
	
		if Dec=None and T=None : 
			RA, Dec, T = RA[0], RA[1], RA[2] = RA  (ROW)
		'''
        import healpy as hp
        import numpy as np
        from jizhipy.Array import Asarray
        from jizhipy.Basic import Raise
        replace = bool(replace)
        hp.nside2resol(nside)
        if (Dec is None and T is None): RA, Dec, T = RA
        RA, Dec, T = 1 * Asarray(RA).flatten(), 1 * Asarray(
            Dec).flatten(), 1 * Asarray(T).flatten()
        size = max(RA.size, Dec.size, T.size)
        if ((RA.size not in [size, 1]) or (Dec.size not in [size, 1])
                or (T.size not in [size, 1])):
            Raise(
                Exception, 'jizhipy.CoordTrans.ToHealpix(): RA.size=' +
                str(RA.size) + ', Dec.size=' + str(Dec.size) + ', T.size=' +
                str(T.size) + ' not in [' + str(size) + ',1]')
        if (RA.size == 1): RA = np.zeros(size) + RA[0]
        if (Dec.size == 1): Dec = np.zeros(size) + Dec[0]
        if (T.size == 1): T = np.zeros(size) + T[0]
        #----------------------------------------
        if (replace):
            T = np.sort(T + 1j * np.arange(size))
            n = T.imag.astype(int)
            T = T.real
            RA, Dec = RA[n], Dec[n]
        #----------------------------------------
        hpmap = np.zeros(12 * nside**2)
        pix = hp.ang2pix(nside, np.pi / 2 - Dec, RA)
        if (replace):
            hpmap[pix] = T
            return hpmap
        #----------------------------------------
        for i in range(pix.size):
            hpmap[pix[i]] += T[i]
        return hpmap
Example #5
0
def modify_input(m1, inst):
    """
    Modify the input observations to match the desired output noise level.

    Parameters
    ----------
        * m1: object, contain the observations
        * inst: object, contain the input parameters from the ini file

    Outputs
    ----------
        * m1: oject, modified input map
        * sigma_t_theo: float, output level of noise [uk.sqrt(s)]
        * sigma_p_theo: float, output level of noise [uk.arcmin]

    """
    sigma_t_theo, sigma_p_theo = theoretical_noise_level_time_domain(
        m1=m1,
        pixel_size=hp.nside2resol(m1.mapinfo.nside),
        net_per_array=inst.net_per_array,
        cut=0.0,
        calendar_time_out=inst.calendar_time,
        efficiency_out=inst.efficiency,
        freq_out=inst.sampling_freq,
        tube_factor=inst.tube_factor,
        verbose=inst.verbose)

    prepare_map(m1, sigma_t_theo)

    return m1, sigma_t_theo, sigma_p_theo
Example #6
0
    def __init__(self, srad=.8, midpoint=True, Nside=128, sigfactor=2):
        self.sradius=srad/180*np.pi ##search radius around map pixel
        self.midpoint = midpoint
        self.Nside=Nside
        self.q=Quasars(self.Nside)
        self.d=Data(self.q, self.Nside)
        if(midpoint):
            self.outputname = "_%s_mid_%s" % (str(Nside), str(sigfactor))
        else:
            self.outputname = "_%s_%s_%s" % (str(Nside), str(srad), str(sigfactor))

        self.pixid=self.q.getCover(Nside)
        self.pixtheta,self.pixphi=hp.pix2ang(Nside,self.pixid)
        self.Np=len(self.pixid) ## number of pixels
        self.Nd=self.d.len()
        
        self.setpix = set(self.pixid)
        self.sigma=np.sqrt(4*np.pi/(12*self.Nside**2))        
        self.sigweight = sigfactor*self.sigma**2
        self.resolution = hp.nside2resol(self.Nside)
        self.setHpix()

        print ("# of pixels in a map",self.Np)
        print("# of pairs ", self.Nd)
        self.run()
Example #7
0
 def test_attributes(self):
     """Test basic, default attributes are set on creation
     """
     skymap = self.TEST_SKY_MAP
     assert skymap.info is None
     assert (not skymap.nest)
     assert skymap.nside == self.NSIDE
     assert skymap.npix == self.NPIX
     assert_array_equal(skymap.pindex, numpy.arange(self.NPIX))
     assert (not skymap.partial)
     assert isinstance(skymap.explicit, dict)
     assert len(skymap.explicit) == self.NPIX
     assert skymap.resolution.value == healpy.nside2resol(
         self.NSIDE, arcmin=True)
     assert skymap.resolution.unit == units.arcmin
     assert skymap.pixrad.value == healpy.max_pixrad(
         self.NSIDE) * units.rad.to("arcmin")
     assert skymap.pixrad.unit == units.arcmin
     assert skymap.pixarea.value == healpy.nside2pixarea(
         self.NSIDE, degrees=True)
     assert skymap.pixarea.unit == units.deg ** 2
     assert skymap.area.value == skymap.size * skymap.pixarea.value
     assert skymap.area.unit == units.deg ** 2
     assert isinstance(skymap.directions, numpy.ndarray)
     assert_array_equal(skymap.directions.shape, (skymap.npix, 3))
Example #8
0
def healpy_rejection_sampler(m, nsamples, jitter=False, ret_idx=False):
    """
    Sampler to sample positions from given skymaps that are treated as PDFs.

    Pixel indices are drawn according to the pixel weight defined by the map.
    If 'jitter' is True, positions are sampled with a gaussian with width of
    the pixel resolution, to escape from the healpix grid.

    Paramters
    ---------
    m : array-like
        Valid healpy map.
    nsamples : int
        How many sampled to draw from the map.
    jitter : bool, optional
        If True, sample "ungridded" positions as explained above.
        (default: False)
    ret_idx bool, optional
        If True, return the sampled map indices, too. (default: False)

    Returns
    -------
    theta, phi : array-like, shape (nsamples)
        Sampled positions in healpy coordinates.
    idx : array-like, shape (nsamples), optional
        Sampled map indices. If jitter is True, indices may not be the same as
        obtained by ``hp.ang2pix`` using the sampled positions.
        Only returned if `ret_idx` is True.
    """
    if hp.maptype(m) != 0:
        raise ValueError(
            "Given map is no healpy map (-1) or a series of maps (>0) : " +
            "{}.".format(hp.maptype(m)))

    # Make sure the map is in pdf form, which is all positive and area = 1
    m = norm_healpy_map(m)

    # Get weights per pixel from normal space map and normalize
    weights = m / np.sum(m)

    # Draw N pixels from the weighted indices
    NSIDE = hp.get_nside(m)
    NPIX = hp.nside2npix(NSIDE)
    idx = np.random.choice(np.arange(NPIX), size=nsamples, p=weights)

    # Get angles from pix indices
    theta, phi = hp.pix2ang(NSIDE, idx)

    # Sample new angles from a gaussian with width of half the pixel resolution.
    # Otherwise we get angles on a grid, exactly one position from one pixel.
    if jitter:
        res = hp.nside2resol(NSIDE) / 2.
        theta = np.random.normal(theta, res)
        phi = np.random.normal(phi, res)
        theta, phi = wrap_theta_phi_range(theta, phi)

    if ret_idx:
        return theta, phi, idx
    else:
        return theta, phi
Example #9
0
     def __init__ (self, quas, dat, srad):
        print("Quasar: ", quas)
        print("Data: ", dat)
        print("Search radius: ", srad)
        Nside=128
        self.sradius=srad ##search radius around map pixel
        self.Nside=Nside
        self.q=Quasars(self.Nside, quas)
        self.d=Data(self.q, self.Nside, dat)


        self.pixid=self.q.getCover(Nside)
        self.pixtheta,self.pixphi=hp.pix2ang(Nside,self.pixid)
        self.Np=len(self.pixid) ## number of pixels
        self.Nd=self.d.len()
        
        self.setpix = set(self.pixid)
        self.sigma=np.sqrt(4*np.pi/(12*self.Nside**2))        
        self.sigweight = 2*self.sigma**2
        self.resolution = hp.nside2resol(self.Nside)
        self.setHpix()

        

        print ("# of pixels in a map",self.Np)
        print("# of pairs ", self.Nd)
        self.run()
Example #10
0
def define_survey_mask(random_catalogue_file):
    """Process a random catalogue file into a `healpy` sky map.

    Parameters
    ----------
    random_catalogue_file : *str or* :class:`pathlib.Path`
        Random catalogue source file.

    Returns
    -------
    sky_map : float :class:`numpy.ndarray`
        Pixelated sky map for a veto mask.

    """
    # Read catalogue RA and DEC columns.
    with fits.open(random_catalogue_file) as random_catalogue_source:
        # pylint: disable=no-member
        random_catalogue = random_catalogue_source[1].data
        dec, ra = random_catalogue['DEC'], random_catalogue['RA']
    logger.info("Loaded random catalogue file %s.", random_catalogue_file)

    # Transform coordinates and determine pixel map resolution.
    hit_coords = sky_to_spherical(np.column_stack((dec, ra)))

    resolution = hp.nside2resol(params.nside, arcmin=True) / 60
    logger.info("Sky map resolution is %.2f degrees.", resolution)

    # Pixelate catalogue map.
    sky_map = np.zeros(hp.nside2npix(params.nside))
    hit_pixels = hp.ang2pix(params.nside, hit_coords[:, 0], hit_coords[:, 1])
    sky_map[hit_pixels] = 1
    logger.info("Sky map pixelation of input catalogue finished.")

    return sky_map
Example #11
0
def make_healpix_map_for_energy_band(energy_band, order):
    log.info(f'Making HEALPix map for energy band: {energy_band} and order: {order}')

    # Select events in energy band
    table = Table.read('input_data/fermi_hgps_events_selected.fits.gz', hdu=1)
    energy = table['ENERGY'].quantity.to('GeV').value
    mask = (energy_band['min'] <= energy) & (energy < energy_band['max'])
    table = table[mask]
    log.info(f'Number of events: {len(table)}')

    # Bin the events into a HEALPix counts map
    nside = hp.order2nside(order + shift_order)
    ipix = hp.ang2pix(
        nside=nside, nest=True, lonlat=True,
        theta=table['L'], phi=table['B'],
    )
    npix = hp.nside2npix(nside)
    log.debug(f'Number of pixels: {npix}')
    resolution = np.rad2deg(hp.nside2resol(nside))
    log.debug(f'Pixel resolution: {resolution} deg')
    image = np.bincount(ipix, minlength=npix)
    image = image.astype('float32')

    # TODO: smoothing the HEALPix map with default setting is very slow.
    # Maybe chunk the data into local WCS maps and then stitch back together?
    # For now: no smoothing
    # image = hp.smoothing(image, sigma=np.deg2rad(0.1))

    path = DATA_DIR / 'maps' / 'Fermi10GeV_healpix_maps' / 'energy_{min}_{max}.fits.gz'.format_map(energy_band)
    path.parent.mkdir(exist_ok=True, parents=True)
    log.info(f'Writing {path}')
    hp.write_map(str(path), image, coord='G', nest=True)
Example #12
0
    def calc_loc_area(self, cls=[.5,.9], **kwargs):       
        """calculate sky localization region area (unit in sq. deg) for different confidence level region of trigger map

        Parameters
        ----------   
        cls       : `list` 
        confidence levels for contours,
        e.g. cls=[.5, .9] would construct 50% and 90% CL contours    

        returns
        ----------
        arealist :   `dictionary`
          dictionary of area corresponding input C.L.

        Examples
        --------                               
        >>> from kobe import triggers
        >>> a = triggers()       
        >>> a.url('https://gracedb.ligo.org/api/superevents/S190510g/files/bayestar.fits.gz')
        >>> a.calc_loc_area()
        {0.5: 575.5456172035578, 0.9: 3463.7648737864856}
        >>> a.calc_loc_area(cls=[.1,.5,.99])
        {0.1: 36.351906008208665, 0.5: 575.5456172035578, 0.99: 11508.39446313552}
        """
        kwargs = self.setkeys(kwargs)        
        _ilist = self.calc_loc_contours(cls=cls, **kwargs)
        if _ilist is None: return        
        
        nside = hp.get_nside(self.hpmap)    
        areapix = (hp.nside2resol(nside,arcmin=True)/60.)**2
        _alist = {}
        for _cl in cls:
            _area = areapix*len(_ilist[_cl])
            _alist[_cl] = _area
        return _alist
Example #13
0
def hp_project_script(hp_hdu, coord, shape_out, frame, alpha, delta):
    #-------------------------------------------------
    # Cut a small piece from the map, taking LMC and SMC for example
    # examples of coordinates
    # coord_LMC = SkyCoord("05:23:34.60", "-69:45:22.0", unit=(u.hourangle, u.deg))
    # coord_SMC = SkyCoord("00h52m38s", "-72:48:01", unit=(u.hourangle, u.deg))
    # coord_Perseus = SkyCoord(54, 31.5, frame = 'icrs', unit = 'deg')
    # coord_CHA_II = SkyCoord(195, -77, frame = 'icrs', unit = 'deg')
    pixsize = hp.nside2resol(hp_hdu.header['NSIDE'], arcmin=True) / 60 / 4
    hdu = hp_project(
        hp_hdu,
        coord,
        pixsize=pixsize,
        shape_out=shape_out,
        projection=("EQUATORIAL", "TAN"),
    )
    plt.title("healpix {0} at ({1}, {2})".format(frame, alpha, delta))
    cut_data = hdu.data
    cut_w = WCS(hdu.header)
    fig = plt.subplot(111, projection=cut_w)
    ax = plt.imshow(cut_data)
    cbar = plt.colorbar(ax)
    cbar.set_label(hp_hdu.header['UNIT'], rotation=270, labelpad=15)
    plt.savefig("healpix_{0}_{1}_{2}.png".format(frame, alpha, delta))
    hdu.writeto("healpix_{0}_{1}_{2}.fits".format(frame, alpha, delta),
                overwrite=True)
    return
Example #14
0
    def __init__(self, nside):
        self.nside = nside
        self.npix = hp.nside2npix(self.nside)
        res = hp.nside2resol(nside, arcmin=True)
        self.res_arcmin = res

        logger.info("New Sphere, nside={}. npix={}, res={} arcmin".format(
            self.nside, self.npix, self.res_arcmin))

        self.pixel_indices = np.arange(self.npix)
        theta, phi = hp.pix2ang(nside, self.pixel_indices)

        # For limited fields of view
        # healpy.query_polygon

        self.pixels = np.zeros(self.npix)  # + hp.UNSEEN
        self.pixel_areas = 1.0 / np.sqrt(self.npix)

        el_r, az_r = hp2elaz(theta, phi)

        self.el_r = el_r
        self.az_r = az_r

        self.l, self.m, self.n = elaz2lmn(self.el_r, self.az_r)
        self.n_minus_1 = self.n - 1
Example #15
0
    def _determine_steps(self, max_stepsize=None):
        """
        """
        if max_stepsize is None:
            max_stepsize = self.max_stepsize

        return np.ceil(hp.nside2resol(self.nside) / _d2r / max_stepsize) + 1
Example #16
0
def nside2resol(testcase):
    cs = []
    for norder in range(16):
        nside = 1 << norder
        args = (nside, )
        cs.append(dict(args=args, expected=healpy.nside2resol(*args)))
    testcase['nside2resol'] = cs
Example #17
0
def make_healpix_image(plt, img, title, num_bins, source_json=None):
    """
    Writes out an image as a healpy image
    """
    nside = hp.pixelfunc.get_min_valid_nside(num_bins*num_bins*3/4)
    npix = hp.nside2npix(nside)

    pixels = np.arange(npix)
    m = np.zeros(npix) + hp.UNSEEN

    window_d = np.degrees(hp.nside2resol(nside))

    for i in pixels:
        theta, phi = hp.pix2ang(nside, i)
        if (theta < np.pi / 2):
            el = np.degrees(np.pi/2 - theta)
            az = np.degrees(phi)
            s = elaz.ElAz(el, az)
            x_min, x_max, y_min, y_max, area = s.get_px_window(num_bins, window_deg=window_d)
            s_px = img[y_min:y_max, x_min:x_max]

            m[i] = np.sum(s_px)/area

    hp.orthview(m, rot=(0, 90, 180), title=title, xsize=3000, cbar=False, half_sky=True)
    hp.graticule()
    if source_json is not None:
        src_list = elaz.from_json(source_json, el_limit=20.0, jy_limit=1e4)
        output_list = []
        for s in src_list:
            l, m = s.get_lm()
            output_list.append(plt.Circle([-l, m], 0.03, color=(0.9, 0.2, 0.3), fill=False))
        ax = plt.gca()
        for circle in output_list:
            ax.add_artist(circle)
Example #18
0
def _getCountLocation(cat=None, ra=None, dec=None, nside=512, nest=False):
    ipix = _es_hp.RaDec2Healpix(cat=cat, ra=ra, dec=dec, nside=nside, nest=nest)
    bc = _np.bincount(ipix)
    pixels = _np.nonzero(bc)[0]
    bc = bc[bc>0] / _hp.nside2resol(nside, arcmin=True)**2 # in arcmin^-2
    lon, lat = _es_hp.Healpix2RaDec(pixels, nside=nside, nest=nest)
    return bc, lat, lon
Example #19
0
    def __init__(self, config):
        # record for posterity
        self.maskfile = config.maskfile
        maskinfo, hdr = fitsio.read(config.maskfile,
                                    ext=1,
                                    header=True,
                                    upper=True)
        # maskinfo converted to a catalog (array of Entrys)
        maskinfo = Catalog(maskinfo)
        nside_mask = hdr['NSIDE']
        nest = hdr['NEST']

        hpix_ring = maskinfo.hpix if nest != 1 else hp.nest2ring(
            nside_mask, maskinfo.hpix)

        # if we have a sub-region of the sky, cut down the mask to save memory
        if config.hpix > 0:
            border = np.radians(config.border) + hp.nside2resol(nside_mask)
            theta, phi = hp.pix2ang(config.nside, config.hpix)
            radius = np.sqrt(2) * (hp.nside2resol(config.nside) / 2. + border)
            pixint = hp.query_disc(nside_mask,
                                   hp.ang2vec(theta, phi),
                                   radius,
                                   inclusive=False)
            suba, subb = esutil.numpy_util.match(pixint, hpix_ring)
            hpix_ring = hpix_ring[subb]
            muse = subb
        else:
            muse = np.arange(hpix_ring.size, dtype='i4')

        offset, ntot = np.min(hpix_ring) - 1, np.max(hpix_ring) - np.min(
            hpix_ring) + 3
        self.nside = nside_mask
        self.offset = offset
        self.npix = ntot

        #ntot = np.max(hpix_ring) - np.min(hpix_ring) + 3
        self.fracgood = np.zeros(ntot, dtype='f4')

        # check if we have a fracgood in the input maskinfo
        try:
            self.fracgood_float = 1
            self.fracgood[hpix_ring - offset] = maskinfo[muse].fracgood
        except AttributeError:
            self.fracgood_float = 0
            self.fracgood[hpix_ring - offset] = 1
        super(HPMask, self).__init__(config)
Example #20
0
 def run_paircount(self, maxang=10):
     bw = 3.*hp.nside2resol(self.nside)*180./3.1416  # 3x resol.
     bins = np.arange(bw, maxang, bw)
     #delta_i, rani = hp.ud_grade(delta, res), hp.ud_grade(weight, res)
     theta, phi, deltam, fpixm = maps2pcinput(self.delta, self.weight, self.mask)
     w = paircount(theta, phi, deltam, deltam, fpixm, np.deg2rad(bins))
     binc = 0.5*(bins[1:]+bins[:-1])
     return [binc, w[0]/w[1]]
Example #21
0
def best_order_for(pixel_scale):
    d = pixel_scale
    best_o = 1. / 2. * math.log(math.pi / (3. * d * d)) / math.log(2)
    o = int(best_o + 1)
    logging.info('pixel scale (arcsec): {:.4f} -> {:.4f}'.format(
        rad2arcsec(pixel_scale),
        rad2arcsec(healpy.nside2resol(healpy.order2nside(o)))))
    return o
Example #22
0
def pix2tiles(nside, pixels, tiles=None, radius=None):
    '''
    Returns subset of tiles that overlap the list of pixels

    Args:
        nside: integer healpix nside, 2**k with 1 <= k <= 30
        pixels: array of integer pixels using nested numbering scheme

    Optional:
        tiles:
            Table-like with RA,DEC columns; or
            None to use all DESI tiles from desimodel.io.load_tiles()
        radius: tile radius in degrees;
            if None use desimodel.focalplane.get_tile_radius_deg()

    Returns:
        table of tiles that cover these pixels

    TODO: add support for tiles as integers or list/array of integer TILEIDs
    '''
    import healpy as hp
    import desimodel.footprint

    if tiles is None:
        import desimodel.io
        tiles = desimodel.io.load_tiles()

    if radius is None:
        import desimodel.focalplane
        radius = desimodel.focalplane.get_tile_radius_deg()

    #- Trim tiles to ones that *might* overlap these pixels
    theta, phi = hp.pix2ang(nside, pixels, nest=True)
    ra, dec = np.degrees(phi), 90 - np.degrees(theta)
    pixsize = np.degrees(hp.nside2resol(nside))
    ii = desimodel.footprint.find_tiles_over_point(tiles,
                                                   ra,
                                                   dec,
                                                   radius=radius + pixsize)
    if np.isscalar(pixels):
        tiles = tiles[ii]
    else:
        ii = np.unique(np.concatenate(ii))
        tiles = tiles[ii]

    #- Now check in detail
    theta, phi = np.radians(90 - tiles['DEC']), np.radians(tiles['RA'])
    vec = hp.ang2vec(theta, phi)
    ii = list()
    for i in range(len(tiles)):
        tilepix = hp.query_disc(nside,
                                vec[i],
                                radius=np.radians(radius),
                                inclusive=True,
                                nest=True)
        if np.any(np.in1d(pixels, tilepix)):
            ii.append(i)
    return tiles[ii]
Example #23
0
def calculate_nside_resolution():
    NSIDE = [2**i for i in range(11)]
    print('given nside | number of pixels | resolution (pixel size in degree) | Maximum angular distance (degree) | pixel area (in square degrees)')
    for nside in NSIDE:
        npix = hp.nside2npix(nside)
        resol = np.rad2deg(hp.nside2resol(nside))
        maxrad = np.rad2deg(hp.max_pixrad(nside))
        pixarea = hp.nside2pixarea(nside, degrees=True)
        print('{0:^11} | {1:^16} | {2:^33.4f} | {3:^33.4f} | {4:^30.6f}'.format(nside, npix, resol, maxrad, pixarea))
Example #24
0
def randomPositions(input, nside_pix, n=1):
    """
    Generate n random positions within a full HEALPix mask of booleans, or a set of (lon, lat) coordinates.

    nside_pix is meant to be at coarser resolution than the input mask or catalog object positions
    so that gaps from star holes, bleed trails, cosmic rays, etc. are filled in. 
    Return the longitude and latitude of the random positions and the total area (deg^2).

    Probably there is a faster algorithm, but limited much more by the simulation and fitting time
    than by the time it takes to generate random positions within the mask.
    """
    input = numpy.array(input)
    if len(input.shape) == 1:
        subpix = numpy.nonzero(
            input
        )[0]  # All the valid pixels in the mask at the NSIDE for the input mask
        lon, lat = pix2ang(healpy.npix2nside(len(input)), subpix)
    elif len(input.shape) == 2:
        lon, lat = input[0], input[1]  # All catalog object positions
    else:
        logger.warning(
            'Unexpected input dimensions for skymap.randomPositions')
    pix = surveyPixel(lon, lat, nside_pix)

    # Area with which the random points are thrown
    area = len(pix) * healpy.nside2pixarea(nside_pix, degrees=True)

    lon = []
    lat = []
    for ii in range(0, n):
        # Choose an unmasked pixel at random, which is OK because HEALPix is an equal area scheme
        pix_ii = pix[numpy.random.randint(0, len(pix))]
        lon_ii, lat_ii = ugali.utils.projector.pixToAng(nside_pix, pix_ii)
        projector = ugali.utils.projector.Projector(lon_ii, lat_ii)

        inside = False
        while not inside:
            # Apply random offset
            arcminToDegree = 1 / 60.
            resolution = arcminToDegree * healpy.nside2resol(nside_pix,
                                                             arcmin=True)
            x = 2. * (numpy.random.rand() -
                      0.5) * resolution  # Using factor 2 to be conservative
            y = 2. * (numpy.random.rand() - 0.5) * resolution

            lon_candidate, lat_candidate = projector.imageToSphere(x, y)

            # Make sure that the random position does indeed fall within the randomly selected pixel
            if ugali.utils.projector.angToPix(nside_pix, lon_candidate,
                                              lat_candidate) == pix_ii:
                inside = True

        lon.append(lon_candidate)
        lat.append(lat_candidate)

    return numpy.array(lon), numpy.array(lat), area
Example #25
0
    def from_resolution(cls,
                        resolution=None,
                        nside=None,
                        theta=0.0,
                        phi=0.0,
                        radius=0.0):
        # Theta is co-latitude measured southward from the north pole
        # Phi is [0..2pi]

        if nside is None:  # Calculate nside to the appropriate resolution
            nside = 1
            while True:
                res = hp.nside2resol(nside, arcmin=True)
                logger.info("nside={} res={} arcmin".format(nside, res))
                if res < resolution:
                    break
                nside = nside * 2

        ret = cls(nside)

        # The coordinates of the unit vector defining the center
        x0 = hp.ang2vec(theta, phi)

        # https://healpy.readthedocs.io/en/latest/generated/healpy.query_polygon.html
        ret.pixel_indices = hp.query_disc(nside=nside,
                                          vec=x0,
                                          radius=radius,
                                          inclusive=False,
                                          nest=False)
        #self.pixel_indices = my_query_disk(nside, x0, radius)

        ret.npix = ret.pixel_indices.shape[0]
        ret.pixel_areas = 1.0 / np.sqrt(ret.npix)

        logger.info("New SubSphere, nside={}. npix={}".format(
            ret.nside, ret.npix))

        theta, phi = hp.pix2ang(nside, ret.pixel_indices)

        ret.fov = np.degrees(radius * 2)
        ret.pixels = np.zeros(ret.npix)  # + hp.UNSEEN

        el_r, az_r = hp2elaz(theta, phi)

        ret.el_r = el_r
        ret.az_r = az_r

        ret.l, ret.m, ret.n = elaz2lmn(ret.el_r, ret.az_r)
        ret.n_minus_1 = ret.n - 1

        if False:
            import matplotlib.pyplot as plt
            #plt.plot(ret.l, ret.m, 'x')
            plt.plot(el_r, az_r, 'x')
            plt.show()
        return ret
Example #26
0
 def run(self):
     #bw = 3.*hp.nside2resol(self.nside)*180./3.1416  # 3x resol.
     #bins = np.arange(bw, 10, bw)
     bw    = hp.nside2resol(self.nside)
     bins  = np.arange(bw, np.deg2rad(10), bw)[::-1]
     #ti = time()
     _,xi = XI(self.theta, self.phi, self.theta, self.phi, self.delta, self.delta2, self.weight, self.weight, bins, 0)
     #print('took {}s for the auto correlation '.format(time()-ti))
     self.bins = bins
     self.output = dict(t=self.bins, w=xi, dmean1=self.mean1, dmean2=self.mean2)
Example #27
0
def skypatch(phi, theta, radius, skyres=2.):
    import healpy
    baseres = (skyres / 4.) * np.pi/180. # require this resolution from healpix grid
    nside = 2**int(min(10, np.ceil(np.log2(healpy.nside2resol(1) / baseres)))) # don't go past 2**10 (12.6 million pixels)
    npix = healpy.nside2npix(nside)
    p = healpy.ang2pix(nside, theta, phi)
    n = np.zeros(npix)
    n[p] = 1. # put all probability at (p, t)
    m = healpy.smoothing(n, sigma=radius) # smooth out Gaussian
    return m
Example #28
0
    def resolution(self):
        """Angular resolution (arcminutes) of pixels in this `SkyMap`

        This returns an instance of `~astropy.units.Quantity` with explicit
        units, which can then be converted by the user as-needed.
        """
        return healpy.nside2resol(
            self.nside,
            arcmin=True,
        ) * units.Unit("arcmin")
Example #29
0
def get_header_info(fits_map_filename):
    h = fitsio.read_header(fits_map_filename, ext=1)
    NSIDE = h["NSIDE"]
    print("NSIDE: %i" % NSIDE)
    print("approx. resolution: %3.1f deg" %
          (hp.nside2resol(NSIDE, arcmin=True) / 60))
    print("number of pixels: %i" % hp.nside2npix(NSIDE))
    print("Process: %s" % h["PROCESS"])
    print("Units: %s" % h["TUNIT1"])
    return NSIDE
Example #30
0
def _getCountLocation(cat=None, ra=None, dec=None, nside=512, nest=False):
    ipix = _es_hp.RaDec2Healpix(cat=cat,
                                ra=ra,
                                dec=dec,
                                nside=nside,
                                nest=nest)
    bc = _np.bincount(ipix)
    pixels = _np.nonzero(bc)[0]
    bc = bc[bc > 0] / _hp.nside2resol(nside, arcmin=True)**2  # in arcmin^-2
    lon, lat = _es_hp.Healpix2RaDec(pixels, nside=nside, nest=nest)
    return bc, lat, lon
Example #31
0
def XIwindow(ouname, randoms, mask):
    nside = hp.get_nside(randoms)
    bw = hp.nside2resol(nside)
    bins = np.arange(bw, np.pi, bw)[::-1]
    cbins = np.cos(bins)
    theta, phi = hp.pix2ang(nside, np.argwhere(mask).flatten())
    delta = np.ones(mask.sum())
    weight = randoms[mask]
    counts = paircount(theta, phi, theta, phi, delta, delta, weight, weight,
                       cbins, 1)
    return bins, counts
Example #32
0
    def __init__(self, nside, infile=None, frequency=None, **kwargs):
        super().__init__(**kwargs)

        self.type_ = "healpix"
        self.nside = nside
        self.pixelsize_ = (hp.nside2resol(self.nside, arcmin=True) *
                           AUC.arcmin2arcsec)
        self.data = np.zeros(shape=hp.nside2npix(self.nside))

        if infile is not None:
            self.load(infile, frequency)
Example #33
0
def get_lmax(nside, stop_threshold):
    pixscale = hp.nside2resol(nside=nside)
    sigmabeam = (pixscale) / pl.sqrt(8 * np.log(2))
    ell = np.arange(3 * nside - 1)
    arr = np.array([
        (2 * l + 1) / 4.0 / np.pi * np.exp(-sigmabeam * l * (l + 1))
        for l in ell
    ])
    lmax = np.argmin(np.fabs(arr - stop_threshold))

    return lmax, sigmabeam
Example #34
0
    def get_nearby_healpix_list(self, obs, sideHP=32768, nestedHP=True):
        ''' See original cnd.py for more details '''

        # If arcsecRadius < hp.nside2resol(32768, arcmin = True)*60 [ which is ~ 6.4 arc-sec ], ...
        # ... then only need to search adjacent HP
        listHP = list(hp.get_all_neighbours(sideHP, obs.Healpix,
                                            nest=nestedHP))
        listHP.append(obs.Healpix)

        # If arcsecRadius > hp.nside2resol(32768, arcmin = True)*60, then need to search neighbors of neighbours ...
        arcsecRadius = self.get_arcsecRadius(obs)
        if arcsecRadius > hp.nside2resol(sideHP, arcmin=True) * 60:
            for i in range(
                    int(arcsecRadius //
                        (hp.nside2resol(sideHP, arcmin=True) * 60))):
                tmpHP = [h for h in listHP]
                for h in tmpHP:
                    listHP.extend(
                        list(hp.get_all_neighbours(sideHP, h, nest=nestedHP)))
                listHP = list(set(listHP))
        return listHP
def pixel_solid_angle(nside, arcmin=False):
    """
    Gives the solid angle subtended by a pixel on a Healpix map with a given nside.
    Default units ; steradian
    if arcmin is True, units = arcmin^2
    """
    pix_res = hp.nside2resol(nside, arcmin=True)
    if arcmin:
        pix_solid_angle = pix_res**2
    else:
        pix_solid_angle = OMEGA_ARCMIN_SQ*pix_res**2
    return pix_solid_angle
Example #36
0
    def snapshot_fn(self, ra, dec, radius, value=1.0, smooth=False):
        radius = max(radius, 2 * np.rad2deg(hp.nside2resol(self.nside)))

        res = np.zeros_like(self.coverage)
        dists = ang_sep(ra, dec, self.ra, self.dec)
        idx = dists < radius
        res[idx] = value

        if smooth:
            res = hp.smoothing(res, fwhm=np.deg2rad(self.r0), verbose=False)

        return res
Example #37
0
 def __init__(self, nside=128, lonCol ='fieldRA',
              latCol='fieldDec', latLonDeg=True, verbose=True, badval=hp.UNSEEN,
              useCache=True, leafsize=100, radius=1.75,
              useCamera=False, rotSkyPosColName='rotSkyPos',
              mjdColName='observationStartMJD', chipNames='all'):
     """Instantiate and set up healpix slicer object."""
     super(HealpixSlicer, self).__init__(verbose=verbose,
                                         lonCol=lonCol, latCol=latCol,
                                         badval=badval, radius=radius, leafsize=leafsize,
                                         useCamera=useCamera, rotSkyPosColName=rotSkyPosColName,
                                         mjdColName=mjdColName, chipNames=chipNames, latLonDeg=latLonDeg)
     # Valid values of nside are powers of 2.
     # nside=64 gives about 1 deg resolution
     # nside=256 gives about 13' resolution (~1 CCD)
     # nside=1024 gives about 3' resolution
     # Check validity of nside:
     if not(hp.isnsideok(nside)):
         raise ValueError('Valid values of nside are powers of 2.')
     self.nside = int(nside)
     self.pixArea = hp.nside2pixarea(self.nside)
     self.nslice = hp.nside2npix(self.nside)
     self.spatialExtent = [0, self.nslice-1]
     self.shape = self.nslice
     if self.verbose:
         print('Healpix slicer using NSIDE=%d, ' % (self.nside) + \
               'approximate resolution %f arcminutes' % (hp.nside2resol(self.nside, arcmin=True)))
     # Set variables so slicer can be re-constructed
     self.slicer_init = {'nside': nside, 'lonCol': lonCol, 'latCol': latCol,
                         'radius': radius}
     if useCache:
         # useCache set the size of the cache for the memoize function in sliceMetric.
         binRes = hp.nside2resol(nside)  # Pixel size in radians
         # Set the cache size to be ~2x the circumference
         self.cacheSize = int(np.round(4.*np.pi/binRes))
     # Set up slicePoint metadata.
     self.slicePoints['nside'] = nside
     self.slicePoints['sid'] = np.arange(self.nslice)
     self.slicePoints['ra'], self.slicePoints['dec'] = self._pix2radec(self.slicePoints['sid'])
     # Set the default plotting functions.
     self.plotFuncs = [HealpixSkyMap, HealpixHistogram, HealpixPowerSpectrum]
Example #38
0
def skyhist(posteriorsamples='posterior_samples.dat', skyres=2.):
    import healpy
    if type(posteriorsamples) is str:
        posteriorsamples = loadposteriorsamples(posteriorsamples)
    baseres = (skyres / 4.) * np.pi/180. # require this resolution from healpix grid
    nside = 2**int(min(10, np.ceil(np.log2(healpy.nside2resol(1) / baseres)))) # don't go past 2**10 (12.6 million pixels)
    npix = healpy.nside2npix(nside)
    p = healpy.ang2pix(nside, posteriorsamples[:,2], posteriorsamples[:,1]) # convert (theta, phi) to healpix numbers
    # n = np.bincount(p, minlength=npix) # bin the samples
    n = np.zeros(npix)
    n[:max(p)+1] = np.bincount(p) # support old version of numpy that does not have minlength argument
    m = healpy.smoothing(n, sigma=skyres*np.pi/180.) # smoothed map
    return m
Example #39
0
def randomPositions(input, nside_pix, n=1):
    """
    Generate n random positions within a full HEALPix mask of booleans, or a set of (lon, lat) coordinates.

    nside_pix is meant to be at coarser resolution than the input mask or catalog object positions
    so that gaps from star holes, bleed trails, cosmic rays, etc. are filled in. 
    Return the longitude and latitude of the random positions and the total area (deg^2).

    Probably there is a faster algorithm, but limited much more by the simulation and fitting time
    than by the time it takes to generate random positions within the mask.
    """
    input = numpy.array(input)
    if len(input.shape) == 1:
        subpix = numpy.nonzero(input)[0] # All the valid pixels in the mask at the NSIDE for the input mask
        lon, lat = pix2ang(healpy.npix2nside(len(input)), subpix)
    elif len(input.shape) == 2:
        lon, lat = input[0], input[1] # All catalog object positions
    else:
        logger.warning('Unexpected input dimensions for skymap.randomPositions')
    pix = surveyPixel(lon, lat, nside_pix)

    # Area with which the random points are thrown
    area = len(pix) * healpy.nside2pixarea(nside_pix, degrees=True)
    
    lon = []
    lat = []
    for ii in range(0, n):
        # Choose an unmasked pixel at random, which is OK because HEALPix is an equal area scheme
        pix_ii = pix[numpy.random.randint(0, len(pix))]
        lon_ii, lat_ii = ugali.utils.projector.pixToAng(nside_pix, pix_ii)
        projector = ugali.utils.projector.Projector(lon_ii, lat_ii)

        inside = False
        while not inside:
            # Apply random offset
            arcminToDegree = 1 / 60.
            resolution = arcminToDegree * healpy.nside2resol(nside_pix, arcmin=True)
            x = 2. * (numpy.random.rand() - 0.5) * resolution # Using factor 2 to be conservative
            y = 2. * (numpy.random.rand() - 0.5) * resolution
            
            lon_candidate, lat_candidate = projector.imageToSphere(x, y)

            # Make sure that the random position does indeed fall within the randomly selected pixel 
            if ugali.utils.projector.angToPix(nside_pix, lon_candidate, lat_candidate) == pix_ii:
                inside = True
                                    
        lon.append(lon_candidate)
        lat.append(lat_candidate)

    return numpy.array(lon), numpy.array(lat), area
def process_healpix(filename, out_nside):
    print 'Process {0} on {1}'.format(MP.current_process().name, filename)
    pixval = HP.read_map(filename, verbose=False)
    nside = HP.npix2nside(pixval.size)
    out_pixres = HP.nside2resol(out_nside)
    print 'Before: ', pixval.min(), pixval.max(), NP.mean(pixval), NP.std(pixval)
    if nside > out_nside:
        if nside/out_nside > 4:
            pixval = HP.ud_grade(pixval, 4*out_nside)
            nside = 4*out_nside
        pix_smoothed = HP.smoothing(pixval, fwhm=out_pixres, regression=False, verbose=False)
    pix_resampled = HP.ud_grade(pix_smoothed, out_nside)
    print 'After: ', pixval.min(), pixval.max(), NP.mean(pix_resampled), NP.std(pix_resampled)
    return pix_resampled
Example #41
0
    def __init__(self, nside=128, spatialkey1 ='fieldRA' , spatialkey2='fieldDec', verbose=True,
                 useCache=True, radius=1.75, leafsize=100, plotFuncs='all',
                 useCamera=False, rotSkyPosColName='rotSkyPos', mjdColName='expMJD'):
        """Instantiate and set up healpix slicer object."""
        super(HealpixSlicer, self).__init__(verbose=verbose,
                                            spatialkey1=spatialkey1, spatialkey2=spatialkey2,
                                            badval=hp.UNSEEN, radius=radius, leafsize=leafsize,
                                            plotFuncs=plotFuncs,
                                            useCamera=useCamera, rotSkyPosColName=rotSkyPosColName,
                                            mjdColName=mjdColName)
        # Valid values of nside are powers of 2.
        # nside=64 gives about 1 deg resolution
        # nside=256 gives about 13' resolution (~1 CCD)
        # nside=1024 gives about 3' resolution
        # Check validity of nside:
        if not(hp.isnsideok(nside)):
            raise ValueError('Valid values of nside are powers of 2.')
        self.nside = int(nside)
        self.pixArea = hp.nside2pixarea(self.nside)
        self.nslice = hp.nside2npix(self.nside)
        if self.verbose:
            print 'Healpix slicer using NSIDE=%d, '%(self.nside) + \
            'approximate resolution %f arcminutes'%(hp.nside2resol(self.nside,arcmin=True))
        # Set variables so slicer can be re-constructed
        self.slicer_init = {'nside':nside, 'spatialkey1':spatialkey1, 'spatialkey2':spatialkey2,
                            'radius':radius}

        if useCache:
            # useCache set the size of the cache for the memoize function in sliceMetric.
            binRes = hp.nside2resol(nside) # Pixel size in radians
            # Set the cache size to be ~2x the circumference
            self.cacheSize = int(np.round(4.*np.pi/binRes))
        # Set up slicePoint metadata.
        self.slicePoints['nside'] = nside
        self.slicePoints['sid'] = np.arange(self.nslice)
        self.slicePoints['ra'], self.slicePoints['dec'] = self._pix2radec(self.slicePoints['sid'])
    def __init__(self, zpfile, fill_periphery=True, balrogprint=None):
        '''
        Input: name of SLR map FITS file
        '''
        
        self.balrogprint = balrogprint
        if self.balrogprint is not None:
            import balrog
            self.balrog = balrog

        self.zpfile = zpfile

        zpshifts,hdr=fitsio.read(zpfile,ext=1,header=True)
        self.nside = hdr['NSIDE']
        self.zpshifts = zpshifts
        self.fill_periphery = fill_periphery

        # translate mags and indices...
        self.nmag = self.zpshifts['ZP_SHIFT'][0,:].size
        self.bands = np.zeros(self.nmag,dtype='S1')

        for i in xrange(self.nmag):
            magname = hdr['MAG%d' % (i)]
            parts=magname.split('_')
            # force to be upper case...
            self.bands[i] = parts[len(parts)-1].upper()  

        self._Print("Using zpshift file: %s" % (self.zpfile))
        self._Print("Bands: %s" % (str(self.bands)))
        self._Print("NSIDE = %d (~%.1f arcmin)" % (self.nside, hp.nside2resol(self.nside, True)))
        self._Print("Area = %.2f deg^2" % (hp.nside2pixarea(self.nside, degrees=True) * self.zpshifts.size))

        # now we want to build the healpix map...
        self.shiftmap = np.zeros((self.nmag,hp.nside2npix(self.nside)),dtype=np.float32) + hp.UNSEEN
        for i in xrange(self.nmag):
            # all 99s are un-fit
            gd,=np.where(self.zpshifts['ZP_SHIFT'][:,i] < 90.0)
            self.shiftmap[i,self.zpshifts['HPIX'][gd]] = self.zpshifts['ZP_SHIFT'][gd,i]
            
        if (self.fill_periphery):
            self._Print("Filling in periphery pixels...")
            self._fill_periphery()

            self._Print("Peripheral area:")
            for i in xrange(self.nmag):
                self._Print("  %s: %.2f deg^2" % (self.bands[i], self.peripheral_area[i]))
        else:
            self.peripheral_area = None
Example #43
0
def gen_ref_EAs_grid(nside=8, psi_step=360):

    npix = hp.nside2npix(nside)
    resol = np.rad2deg(hp.nside2resol(nside))
    theta, phi = hp.pix2ang(nside, range(npix))
    theta, phi = np.rad2deg(theta), np.rad2deg(phi)
    psi = np.arange(0, 360, psi_step)
    # indexing = 'xy'
    grid_theta, grid_psi = np.meshgrid(theta, psi)
    grid_phi, _ = np.meshgrid(phi, psi)
    EAs_tuple = (grid_phi.flatten(), grid_theta.flatten(), grid_psi.flatten())
    EAs_grid = np.vstack(EAs_tuple).T
    print('number of points on shpere: {0}. \n'
          'resolution: {1:.2f} degree, step of inplane-rotation: {2} degree\n'
          'total grid size: {3}'.format(npix, resol, psi_step, npix * 360 / psi_step))
    return EAs_grid
Example #44
0
def getCountAtLocations(ra, dec, nside=512, return_vertices=False):
    """Get number density of objects from RA/Dec in HealPix cells.

    Requires: healpy

    Args:
        ra: list of rectascensions
        dec: list of declinations
        nside: HealPix nside
        return_vertices: whether to also return the boundaries of HealPix cells

    Returns:
        bc, ra_, dec_, [vertices]
        bc: count of objects [per arcmin^2] in a HealPix cell if count > 0
        ra_: rectascension of the cell center (same format as ra/dec)
        dec_: declinations of the cell center (same format as ra/dec)
        vertices: (N,4,2), RA/Dec coordinates of 4 boundary points of cell
    """
    import healpy as hp
    # get healpix pixels
    ipix = hp.ang2pix(nside, (90-dec)/180*np.pi, ra/180*np.pi, nest=False)
    # count how often each pixel is hit
    bc = np.bincount(ipix)
    pixels = np.nonzero(bc)[0]
    bc = bc[bc>0] / hp.nside2resol(nside, arcmin=True)**2 # in arcmin^-2
    # get position of each pixel in RA/Dec
    theta, phi = hp.pix2ang(nside, pixels, nest=False)
    ra_ = phi*180/np.pi
    dec_ = 90 - theta*180/np.pi

    # get the vertices that confine each pixel
    # convert to RA/Dec (thanks to Eric Huff)
    if return_vertices:
        vertices = np.zeros((pixels.size, 4, 2))
        for i in xrange(pixels.size):
            corners = hp.vec2ang(np.transpose(hp.boundaries(nside,pixels[i])))
            corners = np.array(corners) * 180. / np.pi
            diff = corners[1] - corners[1][0]
            diff[diff > 180] -= 360
            diff[diff < -180] += 360
            corners[1] = corners[1][0] + diff
            vertices[i,:,0] = corners[1]
            vertices[i,:,1] = 90.0 - corners[0]

        return bc, ra_, dec_, vertices
    else:
        return bc, ra_, dec_
Example #45
0
    def footprint(self,nside):
        """
        Download the survey footprint for HEALpix pixels.
        """
        import healpy
        import ugali.utils.projector
        if nside > 2**9: raise Exception("Overflow error: nside must be <=2**9")
        pix = np.arange(healpy.nside2npix(nside),dtype='int')
        footprint = np.zeros(healpy.nside2npix(nside),dtype='bool')
        ra,dec = ugali.utils.projector.pixToAng(nside,pix)
        table_name = 'Pix%i'%nside
        self.upload(np.array([pix,ra,dec]).T, ['pix','ra','dec'], name=table_name)
        radius = healpy.nside2resol(nside_superpix,arcmin=True)

        query="""
        SELECT t.pix, dbo.fInFootprintEq(t.ra, t.dec, %g)
        FROM %s AS t
        """%(radius, table_name)
Example #46
0
def plot_skymap(output, skypost, pixresol=np.pi/180.0, nest=True,inj=None, fast=True):
    nside = 1
    while hp.nside2resol(nside) > pixresol:
        nside *= 2

    pix_post = skypost.as_healpix(nside, nest=nest, fast=fast)

    fig = pp.figure(frameon=False)
    ax = pp.subplot(111, projection='astro mollweide')
    ax.cla()
    ax.grid()
    plot.healpix_heatmap(pix_post, nest=nest, vmin=0.0, vmax=np.max(pix_post), cmap=pp.get_cmap('cylon'))

    if inj is not None:
        # If using an injection file, also plot an X at the true position
        pp.plot(inj['ra'], inj['dec'], 'kx', ms=30, mew=1)

    pp.savefig(output)
def project_map(i):
    print('projecting {:.3f} MHz'.format(F[i]))
    hpx_array = hp.read_map(
        '/data6/piyanat/projects/hera1p/maps/healpix_maps_native_dg/nside512/'
        'healpix_map_dg_nside512_{:.3f}MHz.fits'.format(F[i]), verbose=False
    )
    proj_map_dx = hp.nside2resol(512*2) * 180 / np.pi
    proj_map_nx = 1024
    proj_map, hdr = healpix2sine(hpx_array, 45.0, 89.99, proj_map_nx, proj_map_dx,
                                 return_fits_header=True)
    hdr['crval1'] = 0
    hdr['crval2'] = 0
    hdr.pop('lonpole')
    hdr.pop('latpole')
    hdu = fits.PrimaryHDU(data=proj_map, header=hdr)
    hdu.writeto(
        '/data6/piyanat/projects/hera1p/maps/healpix512_lightcone_lon45d_lat90d_fov60d'
        '/healpix512_lightcone_lon45d_lat90d_fov60d_{:.3f}MHz.fits'.format(F[i])
    )
Example #48
0
    def getCountAtLocations(ra, dec, nside=512, per_area=True, return_vertices=False):
        """Get number density of objects from RA/Dec in HealPix cells.

        Requires: healpy

        Args:
            ra: list of rectascensions
            dec: list of declinations
            nside: HealPix nside
            per_area: return counts in units of 1/arcmin^2
            return_vertices: whether to also return the boundaries of HealPix cells

        Returns:
            bc, ra_, dec_, [vertices]
            bc: count of objects in a HealPix cell if count > 0
            ra_: rectascension of the cell center (same format as ra/dec)
            dec_: declinations of the cell center (same format as ra/dec)
            vertices: (N,4,2), RA/Dec coordinates of 4 boundary points of cell
        """
        import healpy as hp
        # get healpix pixels
        ipix = hp.ang2pix(nside, (90-dec)/180*np.pi, ra/180*np.pi, nest=False)
        # count how often each pixel is hit
        bc = np.bincount(ipix)
        pixels = np.nonzero(bc)[0]
        bc = bc[bc>0]
        if per_area:
            bc = bc.astype('f8')
            bc /= hp.nside2resol(nside, arcmin=True)**2 # in arcmin^-2
        # get position of each pixel in RA/Dec
        theta, phi = hp.pix2ang(nside, pixels, nest=False)
        ra_ = phi*180/np.pi
        dec_ = 90 - theta*180/np.pi

        # get the vertices that confine each pixel
        # convert to RA/Dec (thanks to Eric Huff)
        if return_vertices:
            vertices = getHealpixVertices(pixels, nside)
            return bc, ra_, dec_, vertices
        else:
            return bc, ra_, dec_
Example #49
0
def ring_list(nside=0):
    """
     ring_list() -  description
     ---------------------------------------------------------------------------------
     generating a list of HEALPIX rings in galactic coordinates using the ring 
     resolution as input. The AGILE galactic reference system is used.
     - nside = 2^res
     - npix = 12 nside^2
     ---------------------------------------------------------------------------------
     copyright            : (C) 2016 V. Fioretti (INAF/IASF Bologna) 
     ----------------------------------------------
     Parameters:
     - nside = ring resolution
     ---------------------------------------------------------------------------------
     Output:
     - the l and b list of ring centers in galactic coordinates
     ---------------------------------------------------------------------------------
     Caveats:
     None
     ---------------------------------------------------------------------------------

    """
    npix_healpix = hp.nside2npix(nside)
    res_healpix = (hp.nside2resol(nside, arcmin=True))/60. # deg.
    
    theta_ring_healpix = np.zeros(npix_healpix)
    phi_ring_healpix = np.zeros(npix_healpix)
    l_ring_healpix = np.zeros(npix_healpix)
    b_ring_healpix = np.zeros(npix_healpix)
            
    for jp in xrange(npix_healpix):
    	theta_ring_healpix[jp], phi_ring_healpix[jp] = hp.pix2ang(nside, jp, nest=True)
    	b_ring_healpix[jp] = (180./np.pi)*(theta_ring_healpix[jp] - np.pi/2.)
    	if (phi_ring_healpix[jp] <= np.pi):
    		l_ring_healpix[jp] = (180./np.pi)*(np.pi - phi_ring_healpix[jp])
    	if (phi_ring_healpix[jp] > np.pi):
    		l_ring_healpix[jp] = (180./np.pi)*(np.pi*3. - phi_ring_healpix[jp])

    
    return l_ring_healpix, b_ring_healpix
Example #50
0
def plot_skymap(output, skypost, pixresol=np.pi/180.0, nest=True):
    from matplotlib import pyplot as plt
    import healpy as hp
    try:
        from lalinference.cmap import cylon as cmap
    except ImportError:
        cmap = plt.cmap('gray')

    cmap.set_under(color='w')

    nside = 1
    while hp.nside2resol(nside) > pixresol:
        nside *= 2

    thetas, phis = hp.pix2ang(nside, np.arange(hp.nside2npix(nside), dtype=np.int), nest=nest)
    pixels = np.column_stack((phis, np.pi/2.0 - thetas))

    pix_post = skypost.posterior(pixels)

    plt.clf()
    hp.mollview(pix_post, cmap=cmap, title="", nest=nest)
    plt.savefig(output)
Example #51
0
def plot_patches(src_num, info):
	# Define constants #
	deg2rad   = np.pi/180.
	# Define the width of area #
	beam   = 14.4           # Beam = 3.5'
	dbeam  = beam/60./2.     # Beam = 3.5' -> dbeam = beam/60/2 in degree
	offset = dbeam          # degree

	# HI continuum map and resolution #
	cont  = hp.read_map(os.getenv("HOME")+'/hdata/hi/lambda_chipass_healpix_r10.fits', field = 0, h=False)
	nside = hp.get_nside(cont)
	res   = hp.nside2resol(nside, arcmin=False)
	dd    = res/deg2rad/5.0

	# OK - Go #
	tb    = {}
	for i in range(0,src_num):
		if(i ==2): continue
		if(i ==3): continue
		if(i ==6): continue
		if(i ==11): continue
		## Find the values of Continuum temperature #
		tb[i] = []
		
		## Longitude and latitude ##
		l     = info['l'][i]
		b     = info['b'][i]

		# if(i != 14): continue

		# Plot cartview a/o mollview #
		ll = l
		if (l>180):
			ll = ll-360.

		f = 10.
		# if (i == sr):
		proj = hp.cartview(cont, title=info['src'][i]+'('+str(info['l'][i])+','+str(info['b'][i])+')', coord='G', unit='',
				norm=None, xsize=1920, lonra=[ll-0.5,ll+0.5], latra=[b-0.5,b+0.5],
				return_projected_map=True)

		# hp.cartview(cont, title=info['src'][i]+'('+str(info['l'][i])+','+str(info['b'][i])+')', coord='G', unit='',
		# 		norm='hist', xsize=800, lonra=[ll-offset-f*offset,ll+offset+f*offset], latra=[b-offset-f*offset,b+offset+f*offset],
		# 		return_projected_map=True)

		# hp.orthview(cont, title=info['src'][i]+'('+str(info['l'][i])+','+str(info['b'][i])+')', coord='G', unit='',
		# 		norm='hist', xsize=800, return_projected_map=True)

		# hp.mollview(cont, title=info['src'][i]+'('+str(info['l'][i])+','+str(info['b'][i])+')',
		# 	coord='G', unit='', rot=[0,0,0], norm='hist')

		# hp.mollzoom(cont, title=info['src'][i]+'('+str(info['l'][i])+','+str(info['b'][i])+')',
		# 	coord='G', unit='', rot=[0,0,0], norm=None, min=4599., max=4600.)

		print proj
		print proj.shape

		# Cal. #
		theta = (90.0-b)*deg2rad
		phi   = l*deg2rad
		pix   = hp.ang2pix(nside, theta, phi, nest=False)

		if (cont[pix] > -1.0e30) : # Some pixels not defined
			tb[i].append(cont[pix])

		for x in pl.frange(l-offset, l+offset, dd):
			for y in pl.frange(b-offset, b+offset, dd):
				cosb = np.cos(b*deg2rad)
				cosy = np.cos(y*deg2rad)
				if ( (((x-l)**2 + (y-b)**2) <= offset**2) ):
					theta = (90.0 - y)*deg2rad
					phi   = x*deg2rad
					pix   = hp.ang2pix(nside, theta, phi, nest=False)
					# hp.projtext(x, y, '.'+str(pix)+str(cont[pix]), lonlat=True, coord='G')
					# hp.projtext(x, y, '.'+str(cont[pix]), lonlat=True, coord='G')
					hp.projtext(x, y, '.', lonlat=True, coord='G')

		plt.show()
Example #52
0
## Read Tbg408 from Haslam, Read infor of 23 OH src ##
bg408 = read_tbg408_healpy()
info  = read_23oh_src('../../oh/result/23src_with_oh.txt')

# Define constants #
deg2rad = np.pi/180.

# Define the width of area #
beam    = 14.4           # Beam = 3.5'
dbeam   = beam/60./2.    # Beam = 3.5' -> dbeam = beam/60/2 in degree
offset  = dbeam          # degree

# HI continuum map and resolution #
cont  = hp.read_map(os.getenv("HOME")+'/hdata/hi/lambda_chipass_healpix_r10.fits', field = 0, h=False)
nside = hp.get_nside(cont)
res   = hp.nside2resol(nside, arcmin=False)
dd    = res/deg2rad/5.0

num_of_src = len(info['src'])
plot_patches(num_of_src, info)

sys.exit()

# Product Name
# CHIPASS 1.4 GHz Continuum Map
# Coord. System
# Galactic
# Projection Type
# HEALPix, nested, res 10 (Nside=1024)
# Resolution
# 14.4 arcmin
Example #53
0
def get_gas_column_density(map_file, src_num, info):
	## Classes ##
	msks = masks()

	## Infor
	src  = info['src']
	hi   = info['nhi']
	hier = info['nhi_er']

	# Define constants #
	deg2rad     = np.pi/180.
	fukui_cf    = 2.10e26
	fk_fact_err = 0.0 #unknown

	# Define the width of area #
	beam   = 5.             # Beam = 5'
	dbeam  = beam/120.0     # Beam = 5' -> dbeam = beam/60/2 in degree
	offset = dbeam          # degree

	## Mask, to find Planck Conversion Factor (Dust opacity and Its error) ##
	msk    = hp.read_map(os.getenv("HOME")+'/hdata/dust/planck_mask.fits', field = 0, h=False)

	# tau353 map, err_tau353 map and resolution #
	tau_map  = hp.read_map(map_file, field = 0)
	err_map  = hp.read_map(map_file, field = 1)
	nside    = hp.get_nside(tau_map)
	res      = hp.nside2resol(nside, arcmin=False)
	dd       = res/deg2rad/10.0

	# OK - Go #
	tau353 = []
	nh     = []
	nher   = []
	nhi    = []

	tau    = {}
	t_err  = {}

	rat_fk = 0.
	rat_pl = 0.

	for i in range(0,src_num):
		# Find the values of Tau353 and Err_tau353 in small area #
		tau[i]   = []
		t_err[i] = []

		l = info['l'][i]
		b = info['b'][i]

		# # Plot cartview a/o mollview #
		# ll = l
		# if (l>180):
		# 	ll = ll-360.

		# hp.cartview(tau_map, title=info['src'][i]+'('+str(info['l'][i])+','+str(info['b'][i])+') - '+map_file, coord='G', unit='',
		# 		norm='hist', xsize=800, lonra=[ll-offset-0.1*offset,ll+offset+0.1*offset], latra=[b-offset-0.1*offset,b+offset+0.1*offset],
		# 		return_projected_map=True)
		# hp.cartview(err_map, title='Err', coord='G', unit='',
		# 		norm='hist', xsize=800, lonra=[ll-offset-0.1*offset,ll+offset+0.1*offset], latra=[b-offset-0.1*offset,b+offset+0.1*offset],
		# 		return_projected_map=True)
		# # End Plot cartview a/o mollview ##

		## find Planck Conversion Factor (Dust opacity and Its error) ## 
		planck_cf,pl_fact_err = msks.get_dust_opacity(msk,l,b) # 0.84 #8.40e-27, 0.84e-26

		# Cal. #
		theta = (90.0-b)*deg2rad
		phi   = l*deg2rad
		pix   = hp.ang2pix(nside, theta, phi, nest=False)

		if ( (err_map[pix] >= 6.9e-11) and (err_map[pix] <= 0.00081) and (tau_map[pix] > -1.0e30) ): # Checked Invalid error & Some pixels not defined
			tau[i].append(tau_map[pix])
			t_err[i].append(err_map[pix])

		for x in pl.frange(l-offset, l+offset, dd):
			for y in pl.frange(b-offset, b+offset, dd):
				cosb = np.cos(b*deg2rad)
				cosy = np.cos(y*deg2rad)

				if ( ((x-l)**2 + (y-b)**2) <= offset**2 ):
					# hp.projplot(x, y, 'kx', lonlat=True, coord='G')
					theta = (90.0 - y)*deg2rad
					phi   = x*deg2rad
					pix   = hp.ang2pix(nside, theta, phi, nest=False)

					if ( (err_map[pix] >= 6.9e-11) and (err_map[pix] <= 0.00081) and (tau_map[pix] > -1.0e30) ): # Checked Invalid error & Some pixels not defined
						tau[i].append(tau_map[pix])
						t_err[i].append(err_map[pix])

		# plt.show()
		# continue
		# Make tau353 and err_tau353 correspondent #
		# print info['src'][i], len(tau[i]), len(t_err[i])

		temp_tau = list(set(tau[i]))
		t_err[i] = list(set(t_err[i]))

		cnt_tau = len(temp_tau)
		npix    = len(t_err[i])

		if (cnt_tau != npix) :
			tmp_tau = []
			for k in range(npix):
				tmp_tau.append(tau[i][k])

			tau[i] = tmp_tau
		else:
			tau[i] = list(set(tau[i]))

		# Calculate mean values of tau353 #
		tau353.append(sum(tau[i])/float(npix))

		# Calculate the N(HI) from Fukui factor #
		nhi_i = fukui_cf*tau353[i]
		nhi.append(nhi_i)
	   
		# Calculate the NH from Planck factor #
		nh_i = tau353[i]/planck_cf
		nh.append(nh_i)

		# Uncertainties of mean values of tau353 #
		sd1_tau353 = 0.
		sd2_tau353 = 0.
		for j in range(npix):
			sd1_tau353 = sd1_tau353 + (tau[i][j]-tau353[i])**2
			sd2_tau353 = sd2_tau353 + (t_err[i][j])**2

		sd1_tau353 = (sd1_tau353/npix)**0.5 # Just to test, nothing to do with this
		sd2_tau353 = (sd2_tau353**0.5)/npix # Uncertainty of a Sum

		# Uncertainties of mean values of N(HI) and N(H) #
		fukui_sd  = (sd2_tau353*fukui_cf)
		planck_sd = (sd2_tau353/tau353[i])**2 + (pl_fact_err/planck_cf)**2
		planck_sd = (nh_i*planck_sd**0.5)

		nhi[i]    = nhi[i]*1.0e-20
		fukui_sd  = fukui_sd*1.0e-20
		nh[i]     = nh[i]*1.0e-20
		planck_sd = planck_sd*1.0e-20
		nher.append(planck_sd)

		nhi_hi = info['nhi'][i] ## From Carl
		rat1   = nhi[i]/nhi_hi  ## For Fukui
		rat2   = nh[i]/nhi_hi   ## for Planck

		rat_fk = rat_fk + rat1 ## to cal. Mean of ratio
		rat_pl = rat_pl + rat2 ## to cal. Mean of ratio

		rat1   = round(rat1, 2)
		rat2   = round(rat2, 2)		

		wnm    = info['wnm'][i]
		cnm    = info['cnm'][i]

		print("{}  {}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}"
			.format(i, info['src'][i], round((nhi[i]), 4), round((fukui_sd), 4), round((nh[i]), 4), round((planck_sd), 4), \
				nhi_hi, 0.0, info['nhi_er'][i], wnm, cnm, rat1, rat2, 0, info['thin'][i] ) )

	## Plot N(H) vs N(HI)
	# plt.xscale("log", nonposx='clip')
	# plt.plot(hi,nh, 'rd', label='data', ms=10)
	plt.errorbar(hi,nh,xerr=hier, yerr=nher, color='r', marker='o', ls='None', markersize=8, markeredgecolor='b', markeredgewidth=1, label='data')
	plt.plot([0,160],[0,160], 'k--', label='$N_{H} = N_{HI}$')
	plt.title('$N_{H}$ vs $N_{HI}$ along 94 lines-of-sight', fontsize=30)
	plt.ylabel('$N_{H}[10^{20}$ cm$^{-2}]$', fontsize=35)
	plt.xlabel('$N_{HI} [10^{20}$ cm$^{-2}]$', fontsize=35)
	plt.xlim(-10.0, 165.0)
	# plt.ylim(0, 3)
	plt.grid(True)
	plt.tick_params(axis='x', labelsize=18)
	plt.tick_params(axis='y', labelsize=18)

	plt.text(100., 30., r'$N_{H} = \frac{\tau_{353}}{\sigma_{353}}$', color='k', fontsize=17)
	plt.text(100., 10., r'$\tau_{353}$ from Planck data R1.2', color='k', fontsize=17)

	plt.legend(loc='upper left', fontsize=18)
	# plt.savefig("test.png",bbox_inches='tight')
	# for i in range(len(src)):
	# 	# if (oh[i] > 0) :
	# 	plt.annotate('('+str(src[i])+')', xy=(hi[i], nh[i]), xycoords='data',
 #               xytext=(-50.,30.), textcoords='offset points',
 #               arrowprops=dict(arrowstyle="->"),fontsize=18,
 #               )
	plt.show()
Example #54
0
def plot_patches(map_file, info):
	src = info['src']

	# Define constants #
	deg2rad   = np.pi/180.

	# Define the width of area #
	beam   = 4.3            # Beam = 4.3'
	dbeam  = beam/120.0     # Beam = 30' -> dbeam = beam/60/2 in degree
	offset = dbeam          # degree

	# TTYPE1  = 'TAU353  '           / Optical depth at 353GHz                        
	# TTYPE2  = 'ERR_TAU '           / Error on optical depth                         
	# TTYPE3  = 'EBV     '           / E(B-V) color excess                            
	# TTYPE4  = 'RADIANCE'           / Integrated emission                            
	# TTYPE5  = 'TEMP    '           / Dust equilibrium temperature                   
	# TTYPE6  = 'ERR_TEMP'           / Error on T                                     
	# TTYPE7  = 'BETA    '           / Dust emission spectral index                   
	# TTYPE8  = 'ERR_BETA'           / error on Beta  
	ir_map = hp.read_map(map_file, field = 0)
	nside  = hp.get_nside(ir_map)
	res    = hp.nside2resol(nside, arcmin=False)
	dd     = res/deg2rad/2.0

	# OK - Go #
	ebv    = []
	nh     = []
	nhi    = []

	for i in range(0,len(src)):

		# if( i != 15):
		# 	continue

		l  = info['l'][i]
		b  = info['b'][i]

		# Plot cartview a/o mollview #
		ll = l
		if (l>180):
			ll = ll-360.

		# offset = 1.
		hp.cartview(ir_map, title=info['src'][i], coord='G', unit='',
				norm='hist', xsize=800, lonra=[ll-offset-0.1*offset,ll+offset+0.1*offset], latra=[b-offset-0.1*offset,b+offset+0.1*offset],
				return_projected_map=True)

		# hp.mollview(tau_map, title=info['src'][i]+'('+str(info['l'][i])+','+str(info['b'][i])+') - '+map_file,
		# 	coord='G', unit='', rot=[l,b,0], norm='hist', min=1e-7,max=1e-3, xsize=800)

		# Cal. #
		hp.projplot(ll, b, 'bo', lonlat=True, coord='G')
		hp.projtext(ll, b, ' (' + str(round(ll,2)) + ',' + str(round(b,2)) + ')', lonlat=True, coord='G', fontsize=18, weight='bold')

		theta = (90.0-b)*deg2rad
		phi   = l*deg2rad
		pix   = hp.ang2pix(nside, theta, phi, nest=False)

		for x in pl.frange(l-offset, l+offset, dd):
			for y in pl.frange(b-offset, b+offset, dd):
				cosb = np.cos(b*deg2rad)
				cosy = np.cos(y*deg2rad)
				if ( (((x-l)**2 + (y-b)**2) <= offset**2) ):
					# hp.projtext(x, y, '.', lonlat=True, coord='G')
					hp.projplot(x, y, 'kx', lonlat=True, coord='G')

		mpl.rcParams.update({'font.size':30})
		plt.show()
    filterName = args.filtername
    colName = filterName+'mag'

    # Set up healpy map and ra, dec centers
    nside = 64

    # Set the min to 15 since we saturate there. CatSim max is 28
    bins = np.arange(15., 28.2, .2)
    starDensity = np.zeros((hp.nside2npix(nside), np.size(bins)-1), dtype=float)
    overMaxMask = np.zeros(hp.nside2npix(nside), dtype=bool)
    lat, ra = hp.pix2ang(nside, np.arange(0, hp.nside2npix(nside)))
    dec = np.pi/2.-lat

    # Square root of pixel area.
    hpsizeDeg = hp.nside2resol(nside, arcmin=True)/60.

    # Limit things to a 10 arcmin radius
    hpsizeDeg = np.min([10./60., hpsizeDeg])

    # Options include galaxyBase, cepheidstars, wdstars, rrlystars, msstars, bhbstars, allstars, and more...
    dbobj = CatalogDBObject.from_objid(args.stars)

    indxMin = 0

    restoreFile = glob.glob('starDensity_%s_%s_nside_%i.npz' % (filterName, starNames, nside))
    if len(restoreFile) > 0:
        data = np.load(restoreFile[0])
        starDensity = data['starDensity'].copy()
        indxMin = data['icheck'].copy()
        overMaxMask = data['overMaxMask'].copy()
Example #56
0
import multiprocessing

import numpy as np
import xarray as xr
import healpy as hp
from astropy.wcs import WCS

from opstats.utils import MWA_FREQ_EOR_ALL_80KHZ
from opstats.modeling import healpix2sine


freq_list = MWA_FREQ_EOR_ALL_80KHZ  # MHz
input_dir = '/data6/piyanat/projects/fg1p/foreground'
sine_map_resol = hp.nside2resol(1024, arcmin=True) / 60  # deg


def generate_gsm2016_map(i):
    freq = freq_list[i]
    map_data = hp.read_map(
        '{:s}/gsm2016_{:.3f}MHz.fits'.format(input_dir, freq)
    )
    # Downgrade input map to NSIDE=512 from NSIDE=1024.
    # Output map resolution is equivalent to NSIDE=1024 to make sure
    # we oversample healpix pixels.
    map_data_nside512 = hp.ud_grade(map_data, 512)
    shared_array[i] = healpix2sine(
        map_data_nside512, 0, -30, 256, sine_map_resol, hpx_coord='G'
    )


shared_array_base = multiprocessing.Array('d', freq_list.size * 256 * 256)
Example #57
0
    config = yaml.load(open(args.config))
    patches = Patches(config)
    frequencies = get_frequencies(config)  # [MHz]
    nfreq = len(frequencies)
    pcenter = config['region']['center']  # [deg]
    rmin = config['region']['rmin']  # [deg]
    rmax = config['region']['rmax']  # [deg]
    min_ = config['threshold']['min']  # [K]
    max_ = config['threshold']['max']  # [K]
    pixelsize = config['input']['pixelsize']  # [arcsec]
    clobber = config['output']['clobber']

    nside = config['nside']
    logger.info('Nside: %d' % nside)
    resol = hp.nside2resol(nside, arcmin=True)  # [arcmin]
    logger.info('HEALPix resolution: %.2f [arcmin]' % resol)
    imgsize = int(round(resol * 60 / pixelsize))
    logger.info('Image patch size: %d' % imgsize)
    plon, plat = get_healpix_coords(nside)  # [deg]
    npatch = len(plon)

    for i, freq in enumerate(frequencies):
        logger.info('[%d/%d] %.2f[MHz] ...' % (i+1, nfreq, freq))
        outfile = config['output']['filename'].format(freq=freq)
        os.makedirs(os.path.dirname(outfile), exist_ok=True)
        if os.path.exists(outfile) and (not clobber):
            raise FileExistsError(outfile)

        results = []
        jj = 0
Example #58
0
def plot_patches(map_file, src_num, info):

	# Define constants #
	deg2rad   = np.pi/180.
	fukui_cf  = 2.10 #2.10e26
	planck_cf = 0.84 #8.40e-27, 0.84e-26

	pl_fact_err = 0.3 #3.0e-27
	fk_fact_err = 0.0 #unknown

	# Define the width of area #
	beam   = 5.0            # Beam = map_resolution = 5'
	dbeam  = beam/120.0     # Beam = 3.5' -> dbeam = beam/60/2 in degree
	offset = dbeam          # degree

	# tau353 map, err_tau353 map and resolution #
	tau_map  = hp.read_map(map_file, field = 0)
	err_map  = hp.read_map(map_file, field = 1)
	nside    = hp.get_nside(tau_map)
	res      = hp.nside2resol(nside, arcmin=False)
	dd       = res/deg2rad/5.0

	# OK - Go #
	tau353 = []
	nh     = []
	nhi    = []

	tau    = {}
	t_err  = {}

	rat_fk = 0.
	rat_pl = 0.

	for i in range(0,src_num):

		# Find the values of Tau353 and Err_tau353 in small area #
		tau[i]   = []
		t_err[i] = []

		l  = info['l'][i]
		b  = info['b'][i]

		# Plot cartview a/o mollview #
		ll = l
		if (l>180):
			ll = ll-360.

		hp.cartview(tau_map, title=info['src'][i]+'('+str(info['l'][i])+','+str(info['b'][i])+') - '+map_file, coord='G', unit='',
				norm='hist', xsize=800, lonra=[ll-offset-0.1*offset,ll+offset+0.1*offset], latra=[b-offset-0.1*offset,b+offset+0.1*offset],
				return_projected_map=True)

		# hp.mollview(tau_map, title=info['src'][i]+'('+str(info['l'][i])+','+str(info['b'][i])+') - '+map_file,
		# 	coord='G', unit='', rot=[l,b,0], norm='hist', min=1e-7,max=1e-3, xsize=800)

		# Cal. #
		theta = (90.0-b)*deg2rad
		phi   = l*deg2rad
		pix   = hp.ang2pix(nside, theta, phi, nest=False)

		if (tau_map[pix] > -1.0e30) : # Some pixels not defined
			tau[i].append(tau_map[pix])

		if (err_map[pix] >= 6.9e-11 and err_map[pix] <= 0.00081): # Checked Invalid error
			t_err[i].append(err_map[pix])

		for x in pl.frange(l-offset, l+offset, dd):
			for y in pl.frange(b-offset, b+offset, dd):
				cosb = np.cos(b*deg2rad)
				cosy = np.cos(y*deg2rad)
				if ( (((x-l)**2 + (y-b)**2) <= offset**2) ):
					# hp.projtext(x, y, '.', lonlat=True, coord='G')
					hp.projplot(x, y, 'kx', lonlat=True, coord='G')

		plt.show()
maxID = np.max(maxID)

# Crop off some of the early junky observations
minMJD = 56900
minID,mjd = sb.allSkyDB(0,'select min(ID) from dates where mjd > %i;' % minMJD, dtypes='int')

altLimit = 10. # Degrees
sunAltLimit = np.radians(-20.)

maxID = 3000
step = 5

for dateID in np.arange(minID.max(),minID.max()+maxID+1, step):
    sqlQ = 'select stars.ra, stars.dec, stars.ID, obs.starMag_inst, obs.starMag_err,obs.sky, obs.filter from obs, stars, dates where obs.starID = stars.ID and obs.dateID = dates.ID and obs.filter = "%s" and obs.dateID = %i and obs.starMag_err != 0 and dates.sunAlt < %f and obs.starMag_inst > %f;' % (filt,dateID,sunAltLimit, starBrightLimit)

    # Note that RA,Dec are in degrees
    data,mjd = sb.allSkyDB(dateID, sqlQ=sqlQ, dtypes=dtypes)
    if data.size > nStarLimit:
        alt,az,pa = _altAzPaFromRaDec(np.radians(data['ra']), np.radians(data['dec']),
                                      telescope.lon, telescope.lat, mjd)
        for i,nside in enumerate(nsides):
            tempMap = healbin(az,alt, az*0+1, nside=nside, reduceFunc=np.size)[hpIndices[i]]
            ratio = np.size(np.where(tempMap > 0)[0])/float(np.size(tempMap))
            mapFractions[i].append(ratio)


print 'nside, resolution (deg), fraction of healpixes populated'
for mf,nside in zip(mapFractions,nsides):
    resol = hp.nside2resol(nside, arcmin=True)/60.
    print ' %i,  %f, %f' % (nside, resol,np.median(mf))