/usr/lib/python3/dist-packages/healpy/pixelfunc.py is in python3-healpy 1.10.3-2build4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 | #
# This file is part of Healpy.
#
# Healpy is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Healpy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Healpy; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# For more information about Healpy, see http://code.google.com/p/healpy
#
"""
=====================================================
pixelfunc.py : Healpix pixelization related functions
=====================================================
This module provides functions related to Healpix pixelization scheme.
conversion from/to sky coordinates
----------------------------------
- :func:`pix2ang` converts pixel number to angular coordinates
- :func:`pix2vec` converts pixel number to unit 3-vector direction
- :func:`ang2pix` converts angular coordinates to pixel number
- :func:`vec2pix` converts 3-vector to pixel number
- :func:`vec2ang` converts 3-vector to angular coordinates
- :func:`ang2vec` converts angular coordinates to unit 3-vector
- :func:`pix2xyf` converts pixel number to coordinates within face
- :func:`xyf2pix` converts coordinates within face to pixel number
- :func:`get_interp_weights` returns the 4 nearest pixels for given
angular coordinates and the relative weights for interpolation
- :func:`get_all_neighbours` return the 8 nearest pixels for given
angular coordinates
conversion between NESTED and RING schemes
------------------------------------------
- :func:`nest2ring` converts NESTED scheme pixel numbers to RING
scheme pixel number
- :func:`ring2nest` converts RING scheme pixel number to NESTED
scheme pixel number
- :func:`reorder` reorders a healpix map pixels from one scheme to another
nside/npix/resolution
---------------------
- :func:`nside2npix` converts healpix nside parameter to number of pixel
- :func:`npix2nside` converts number of pixel to healpix nside parameter
- :func:`nside2order` converts nside to order
- :func:`order2nside` converts order to nside
- :func:`nside2resol` converts nside to mean angular resolution
- :func:`nside2pixarea` converts nside to pixel area
- :func:`isnsideok` checks the validity of nside
- :func:`isnpixok` checks the validity of npix
- :func:`get_map_size` gives the number of pixel of a map
- :func:`get_min_valid_nside` gives the minimum nside possible for a given
number of pixel
- :func:`get_nside` returns the nside of a map
- :func:`maptype` checks the type of a map (one map or sequence of maps)
- :func:`ud_grade` upgrades or degrades the resolution (nside) of a map
Masking pixels
--------------
- :const:`UNSEEN` is a constant value interpreted as a masked pixel
- :func:`mask_bad` returns a map with ``True`` where map is :const:`UNSEEN`
- :func:`mask_good` returns a map with ``False`` where map is :const:`UNSEEN`
- :func:`ma` returns a masked array as map, with mask given by :func:`mask_bad`
Map data manipulation
---------------------
- :func:`fit_dipole` fits a monopole+dipole on the map
- :func:`fit_monopole` fits a monopole on the map
- :func:`remove_dipole` fits and removes a monopole+dipole from the map
- :func:`remove_monopole` fits and remove a monopole from the map
- :func:`get_interp_val` computes a bilinear interpolation of the map
at given angular coordinates, using 4 nearest neighbours
"""
try:
from exceptions import NameError
except:
pass
import numpy as np
from functools import wraps
UNSEEN = None
try:
from . import _healpy_pixel_lib as pixlib
#: Special value used for masked pixels
UNSEEN = pixlib.UNSEEN
except:
import warnings
warnings.warn('Warning: cannot import _healpy_pixel_lib module')
# We are using 64-bit integer types.
# nside > 2**29 requires extended integer types.
max_nside = 1 << 29
__all__ = ['pix2ang', 'pix2vec', 'ang2pix', 'vec2pix',
'ang2vec', 'vec2ang',
'get_interp_weights', 'get_neighbours', 'get_interp_val', 'get_all_neighbours',
'max_pixrad',
'nest2ring', 'ring2nest', 'reorder', 'ud_grade',
'UNSEEN', 'mask_good', 'mask_bad', 'ma',
'fit_dipole', 'remove_dipole', 'fit_monopole', 'remove_monopole',
'nside2npix', 'npix2nside', 'nside2order', 'order2nside',
'nside2resol', 'nside2pixarea',
'isnsideok', 'isnpixok',
'get_map_size', 'get_min_valid_nside',
'get_nside', 'maptype', 'ma_to_array']
def check_theta_valid(theta):
"""Raises exception if theta is not within 0 and pi"""
theta = np.asarray(theta)
if not((theta >= 0).all() and (theta <= np.pi + 1e-5).all()):
raise ValueError('THETA is out of range [0,pi]')
def lonlat2thetaphi(lon,lat):
""" Transform longitude and latitude (deg) into co-latitude and longitude (rad)
Parameters
----------
lon : int or array-like
Longitude in degrees
lat : int or array-like
Latitude in degrees
Returns
-------
theta, phi : float, scalar or array-like
The co-latitude and longitude in radians
"""
return np.pi/2. - np.radians(lat),np.radians(lon)
def thetaphi2lonlat(theta,phi):
""" Transform co-latitude and longitude (rad) into longitude and latitude (deg)
Parameters
----------
theta : int or array-like
Co-latitude in radians
phi : int or array-like
Longitude in radians
Returns
-------
lon, lat : float, scalar or array-like
The longitude and latitude in degrees
"""
return np.degrees(phi), 90. - np.degrees(theta)
def maptype(m):
"""Describe the type of the map (valid, single, sequence of maps).
Checks : the number of maps, that all maps have same length and that this
length is a valid map size (using :func:`isnpixok`).
Parameters
----------
m : sequence
the map to get info from
Returns
-------
info : int
-1 if the given object is not a valid map, 0 if it is a single map,
*info* > 0 if it is a sequence of maps (*info* is then the number of
maps)
Examples
--------
>>> import healpy as hp
>>> hp.pixelfunc.maptype(np.arange(12))
0
>>> hp.pixelfunc.maptype([np.arange(12), np.arange(12)])
2
"""
if not hasattr(m, '__len__'):
raise TypeError('input map is a scalar')
if len(m) == 0:
raise TypeError('input map has length zero')
if hasattr(m[0], '__len__'):
npix=len(m[0])
for mm in m[1:]:
if len(mm) != npix:
raise TypeError('input maps have different npix')
if isnpixok(len(m[0])):
return len(m)
else:
raise TypeError('bad number of pixels')
else:
if isnpixok(len(m)):
return 0
else:
raise TypeError('bad number of pixels')
def ma_to_array(m):
"""Converts a masked array or a list of masked arrays to filled numpy arrays
Parameters
----------
m : a map (may be a sequence of maps)
Returns
-------
m : filled map or tuple of filled maps
Examples
--------
>>> import healpy as hp
>>> m = hp.ma(np.array([2., 2., 3, 4, 5, 0, 0, 0, 0, 0, 0, 0]))
>>> m.mask = np.array([0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=np.bool)
>>> print(m.data[1]) # data is not affected by mask
2.0
>>> print(m[1]) # shows that the value is masked
--
>>> print(ma_to_array(m)[1]) # filled array, masked values replace by UNSEEN
-1.6375e+30
"""
try:
return m.filled()
except AttributeError:
try:
return tuple([mm.filled() for mm in m])
except AttributeError:
pass
return m
def is_ma(m):
"""Converts a masked array or a list of masked arrays to filled numpy arrays
Parameters
----------
m : a map (may be a sequence of maps)
Returns
-------
is_ma : bool
whether the input map was a ma or not
"""
return hasattr(m, 'filled') or hasattr(m[0], 'filled')
def accept_ma(f):
"""Wraps a function in order to convert the input map from
a masked to a regular numpy array, and convert back the
output from a regular array to a masked array"""
@wraps(f)
def wrapper(map_in, *args, **kwds):
return_ma = is_ma(map_in)
m = ma_to_array(map_in)
out = f(m, *args, **kwds)
return ma(out) if return_ma else out
return wrapper
def mask_bad(m, badval = UNSEEN, rtol = 1.e-5, atol = 1.e-8):
"""Returns a bool array with ``True`` where m is close to badval.
Parameters
----------
m : a map (may be a sequence of maps)
badval : float, optional
The value of the pixel considered as bad (:const:`UNSEEN` by default)
rtol : float, optional
The relative tolerance
atol : float, optional
The absolute tolerance
Returns
-------
mask
a bool array with the same shape as the input map, ``True`` where input map is
close to badval, and ``False`` elsewhere.
See Also
--------
mask_good, ma
Examples
--------
>>> import healpy as hp
>>> import numpy as np
>>> m = np.arange(12.)
>>> m[3] = hp.UNSEEN
>>> hp.mask_bad(m)
array([False, False, False, True, False, False, False, False, False,
False, False, False], dtype=bool)
"""
m = np.asarray(m)
atol = np.absolute(atol)
rtol = np.absolute(rtol)
return np.absolute(m - badval) <= atol + rtol * np.absolute(badval)
def mask_good(m, badval = UNSEEN, rtol = 1.e-5, atol = 1.e-8):
"""Returns a bool array with ``False`` where m is close to badval.
Parameters
----------
m : a map (may be a sequence of maps)
badval : float, optional
The value of the pixel considered as bad (:const:`UNSEEN` by default)
rtol : float, optional
The relative tolerance
atol : float, optional
The absolute tolerance
Returns
-------
a bool array with the same shape as the input map, ``False`` where input map is
close to badval, and ``True`` elsewhere.
See Also
--------
mask_bad, ma
Examples
--------
>>> import healpy as hp
>>> m = np.arange(12.)
>>> m[3] = hp.UNSEEN
>>> hp.mask_good(m)
array([ True, True, True, False, True, True, True, True, True,
True, True, True], dtype=bool)
"""
m = np.asarray(m)
atol = np.absolute(atol)
rtol = np.absolute(rtol)
return np.absolute(m - badval) > atol + rtol * np.absolute(badval)
def ma(m, badval = UNSEEN, rtol = 1e-5, atol = 1e-8, copy = True):
"""Return map as a masked array, with ``badval`` pixels masked.
Parameters
----------
m : a map (may be a sequence of maps)
badval : float, optional
The value of the pixel considered as bad (:const:`UNSEEN` by default)
rtol : float, optional
The relative tolerance
atol : float, optional
The absolute tolerance
copy : bool, optional
If ``True``, a copy of the input map is made.
Returns
-------
a masked array with the same shape as the input map,
masked where input map is close to badval.
See Also
--------
mask_good, mask_bad, numpy.ma.masked_values
Examples
--------
>>> import healpy as hp
>>> m = np.arange(12.)
>>> m[3] = hp.UNSEEN
>>> hp.ma(m)
masked_array(data = [0.0 1.0 2.0 -- 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0],
mask = [False False False True False False False False False False False False],
fill_value = -1.6375e+30)
<BLANKLINE>
"""
if maptype(m) == 0:
return np.ma.masked_values(m, badval, rtol = rtol, atol = atol, copy = copy)
else:
return tuple([ma(mm) for mm in m])
def ang2pix(nside,theta,phi,nest=False,lonlat=False):
"""ang2pix : nside,theta[rad],phi[rad],nest=False,lonlat=False -> ipix (default:RING)
Parameters
----------
nside : int, scalar or array-like
The healpix nside parameter, must be a power of 2, less than 2**30
theta, phi : float, scalars or array-like
Angular coordinates of a point on the sphere
nest : bool, optional
if True, assume NESTED pixel ordering, otherwise, RING pixel ordering
lonlat : bool
If True, input angles are assumed to be longitude and latitude in degree,
otherwise, they are co-latitude and longitude in radians.
Returns
-------
pix : int or array of int
The healpix pixel numbers. Scalar if all input are scalar, array otherwise.
Usual numpy broadcasting rules apply.
See Also
--------
pix2ang, pix2vec, vec2pix
Examples
--------
>>> import healpy as hp
>>> hp.ang2pix(16, np.pi/2, 0)
1440
>>> hp.ang2pix(16, [np.pi/2, np.pi/4, np.pi/2, 0, np.pi], [0., np.pi/4, np.pi/2, 0, 0])
array([1440, 427, 1520, 0, 3068])
>>> hp.ang2pix(16, np.pi/2, [0, np.pi/2])
array([1440, 1520])
>>> hp.ang2pix([1, 2, 4, 8, 16], np.pi/2, 0)
array([ 4, 12, 72, 336, 1440])
>>> hp.ang2pix([1, 2, 4, 8, 16], 0, 0, lonlat=True)
array([ 4, 12, 72, 336, 1440])
"""
if lonlat:
theta,phi = lonlat2thetaphi(theta,phi)
check_theta_valid(theta)
check_nside(nside)
if nest:
return pixlib._ang2pix_nest(nside,theta,phi)
else:
return pixlib._ang2pix_ring(nside,theta,phi)
def pix2ang(nside,ipix,nest=False,lonlat=False):
"""pix2ang : nside,ipix,nest=False,lonlat=False -> theta[rad],phi[rad] (default RING)
Parameters
----------
nside : int or array-like
The healpix nside parameter, must be a power of 2, less than 2**30
ipix : int or array-like
Pixel indices
nest : bool, optional
if True, assume NESTED pixel ordering, otherwise, RING pixel ordering
lonlat : bool, optional
If True, return angles will be longitude and latitude in degree,
otherwise, angles will be longitude and co-latitude in radians (default)
Returns
-------
theta, phi : float, scalar or array-like
The angular coordinates corresponding to ipix. Scalar if all input
are scalar, array otherwise. Usual numpy broadcasting rules apply.
See Also
--------
ang2pix, vec2pix, pix2vec
Examples
--------
>>> import healpy as hp
>>> hp.pix2ang(16, 1440)
(1.5291175943723188, 0.0)
>>> hp.pix2ang(16, [1440, 427, 1520, 0, 3068])
(array([ 1.52911759, 0.78550497, 1.57079633, 0.05103658, 3.09055608]), array([ 0. , 0.78539816, 1.61988371, 0.78539816, 0.78539816]))
>>> hp.pix2ang([1, 2, 4, 8], 11)
(array([ 2.30052398, 0.84106867, 0.41113786, 0.2044802 ]), array([ 5.49778714, 5.89048623, 5.89048623, 5.89048623]))
>>> hp.pix2ang([1, 2, 4, 8], 11, lonlat=True)
(array([ 315. , 337.5, 337.5, 337.5]), array([-41.8103149 , 41.8103149 , 66.44353569, 78.28414761]))
"""
check_nside(nside)
if nest:
theta,phi = pixlib._pix2ang_nest(nside, ipix)
else:
theta,phi = pixlib._pix2ang_ring(nside,ipix)
if lonlat:
return thetaphi2lonlat(theta,phi)
else:
return theta, phi
def xyf2pix(nside,x,y,face,nest=False):
"""xyf2pix : nside,x,y,face,nest=False -> ipix (default:RING)
Parameters
----------
nside : int, scalar or array-like
The healpix nside parameter, must be a power of 2
x, y : int, scalars or array-like
Pixel indices within face
face : int, scalars or array-like
Face number
nest : bool, optional
if True, assume NESTED pixel ordering, otherwise, RING pixel ordering
Returns
-------
pix : int or array of int
The healpix pixel numbers. Scalar if all input are scalar, array otherwise.
Usual numpy broadcasting rules apply.
See Also
--------
pix2xyf
Examples
--------
>>> import healpy as hp
>>> hp.xyf2pix(16, 8, 8, 4)
1440
>>> hp.xyf2pix(16, [8, 8, 8, 15, 0], [8, 8, 7, 15, 0], [4, 0, 5, 0, 8])
array([1440, 427, 1520, 0, 3068])
"""
check_nside(nside)
if nest:
return pixlib._xyf2pix_nest(nside,x,y,face)
else:
return pixlib._xyf2pix_ring(nside,x,y,face)
def pix2xyf(nside,ipix,nest=False):
"""pix2xyf : nside,ipix,nest=False -> x,y,face (default RING)
Parameters
----------
nside : int or array-like
The healpix nside parameter, must be a power of 2
ipix : int or array-like
Pixel indices
nest : bool, optional
if True, assume NESTED pixel ordering, otherwise, RING pixel ordering
Returns
-------
x, y : int, scalars or array-like
Pixel indices within face
face : int, scalars or array-like
Face number
See Also
--------
xyf2pix
Examples
--------
>>> import healpy as hp
>>> hp.pix2xyf(16, 1440)
(8, 8, 4)
>>> hp.pix2xyf(16, [1440, 427, 1520, 0, 3068])
(array([ 8, 8, 8, 15, 0]), array([ 8, 8, 7, 15, 0]), array([4, 0, 5, 0, 8]))
>>> hp.pix2xyf([1, 2, 4, 8], 11)
(array([0, 1, 3, 7]), array([0, 0, 2, 6]), array([11, 3, 3, 3]))
"""
check_nside(nside)
if nest:
return pixlib._pix2xyf_nest(nside, ipix)
else:
return pixlib._pix2xyf_ring(nside,ipix)
def vec2pix(nside,x,y,z,nest=False):
"""vec2pix : nside,x,y,z,nest=False -> ipix (default:RING)
Parameters
----------
nside : int or array-like
The healpix nside parameter, must be a power of 2, less than 2**30
x,y,z : floats or array-like
vector coordinates defining point on the sphere
nest : bool, optional
if True, assume NESTED pixel ordering, otherwise, RING pixel ordering
Returns
-------
ipix : int, scalar or array-like
The healpix pixel number corresponding to input vector. Scalar if all input
are scalar, array otherwise. Usual numpy broadcasting rules apply.
See Also
--------
ang2pix, pix2ang, pix2vec
Examples
--------
>>> import healpy as hp
>>> hp.vec2pix(16, 1, 0, 0)
1504
>>> hp.vec2pix(16, [1, 0], [0, 1], [0, 0])
array([1504, 1520])
>>> hp.vec2pix([1, 2, 4, 8], 1, 0, 0)
array([ 4, 20, 88, 368])
"""
if nest:
return pixlib._vec2pix_nest(nside,x,y,z)
else:
return pixlib._vec2pix_ring(nside,x,y,z)
def pix2vec(nside,ipix,nest=False):
"""pix2vec : nside,ipix,nest=False -> x,y,z (default RING)
Parameters
----------
nside : int, scalar or array-like
The healpix nside parameter, must be a power of 2, less than 2**30
ipix : int, scalar or array-like
Healpix pixel number
nest : bool, optional
if True, assume NESTED pixel ordering, otherwise, RING pixel ordering
Returns
-------
x, y, z : floats, scalar or array-like
The coordinates of vector corresponding to input pixels. Scalar if all input
are scalar, array otherwise. Usual numpy broadcasting rules apply.
See Also
--------
ang2pix, pix2ang, vec2pix
Examples
--------
>>> import healpy as hp
>>> hp.pix2vec(16, 1504)
(0.99879545620517241, 0.049067674327418015, 0.0)
>>> hp.pix2vec(16, [1440, 427])
(array([ 0.99913157, 0.5000534 ]), array([ 0. , 0.5000534]), array([ 0.04166667, 0.70703125]))
>>> hp.pix2vec([1, 2], 11)
(array([ 0.52704628, 0.68861915]), array([-0.52704628, -0.28523539]), array([-0.66666667, 0.66666667]))
"""
check_nside(nside)
if nest:
return pixlib._pix2vec_nest(nside,ipix)
else:
return pixlib._pix2vec_ring(nside,ipix)
def ang2vec(theta, phi, lonlat=False):
"""ang2vec : convert angles to 3D position vector
Parameters
----------
theta : float, scalar or arry-like
colatitude in radians measured southward from north pole (in [0,pi]).
phi : float, scalar or array-like
longitude in radians measured eastward (in [0, 2*pi]).
lonlat : bool
If True, input angles are assumed to be longitude and latitude in degree,
otherwise, they are co-latitude and longitude in radians.
Returns
-------
vec : float, array
if theta and phi are vectors, the result is a 2D array with a vector per row
otherwise, it is a 1D array of shape (3,)
See Also
--------
vec2ang, rotator.dir2vec, rotator.vec2dir
"""
if lonlat:
theta,phi = lonlat2thetaphi(theta,phi)
check_theta_valid(theta)
sintheta = np.sin(theta)
return np.array([sintheta*np.cos(phi),
sintheta*np.sin(phi),
np.cos(theta)]).T
def vec2ang(vectors, lonlat=False):
"""vec2ang: vectors [x, y, z] -> theta[rad], phi[rad]
Parameters
----------
vectors : float, array-like
the vector(s) to convert, shape is (3,) or (N, 3)
lonlat : bool, optional
If True, return angles will be longitude and latitude in degree,
otherwise, angles will be longitude and co-latitude in radians (default)
Returns
-------
theta, phi : float, tuple of two arrays
the colatitude and longitude in radians
See Also
--------
ang2vec, rotator.vec2dir, rotator.dir2vec
"""
vectors = vectors.reshape(-1,3)
dnorm = np.sqrt(np.sum(np.square(vectors),axis=1))
theta = np.arccos(vectors[:,2]/dnorm)
phi = np.arctan2(vectors[:,1],vectors[:,0])
phi[phi < 0] += 2*np.pi
if lonlat:
return thetaphi2lonlat(theta,phi)
else:
return theta, phi
def ring2nest(nside, ipix):
"""Convert pixel number from RING ordering to NESTED ordering.
Parameters
----------
nside : int, scalar or array-like
the healpix nside parameter
ipix : int, scalar or array-like
the pixel number in RING scheme
Returns
-------
ipix : int, scalar or array-like
the pixel number in NESTED scheme
See Also
--------
nest2ring, reorder
Examples
--------
>>> import healpy as hp
>>> hp.ring2nest(16, 1504)
1130
>>> hp.ring2nest(2, np.arange(10))
array([ 3, 7, 11, 15, 2, 1, 6, 5, 10, 9])
>>> hp.ring2nest([1, 2, 4, 8], 11)
array([ 11, 13, 61, 253])
"""
check_nside(nside)
return pixlib._ring2nest(nside, ipix)
def nest2ring(nside, ipix):
"""Convert pixel number from NESTED ordering to RING ordering.
Parameters
----------
nside : int, scalar or array-like
the healpix nside parameter
ipix : int, scalar or array-like
the pixel number in NESTED scheme
Returns
-------
ipix : int, scalar or array-like
the pixel number in RING scheme
See Also
--------
ring2nest, reorder
Examples
--------
>>> import healpy as hp
>>> hp.nest2ring(16, 1130)
1504
>>> hp.nest2ring(2, np.arange(10))
array([13, 5, 4, 0, 15, 7, 6, 1, 17, 9])
>>> hp.nest2ring([1, 2, 4, 8], 11)
array([ 11, 2, 12, 211])
"""
check_nside(nside)
return pixlib._nest2ring(nside, ipix)
@accept_ma
def reorder(map_in, inp=None, out=None, r2n=None, n2r=None):
"""Reorder an healpix map from RING/NESTED ordering to NESTED/RING
Parameters
----------
map_in : array-like
the input map to reorder, accepts masked arrays
inp, out : ``'RING'`` or ``'NESTED'``
define the input and output ordering
r2n : bool
if True, reorder from RING to NESTED
n2r : bool
if True, reorder from NESTED to RING
Returns
-------
map_out : array-like
the reordered map, as masked array if the input was a
masked array
Notes
-----
if ``r2n`` or ``n2r`` is defined, override ``inp`` and ``out``.
See Also
--------
nest2ring, ring2nest
Examples
--------
>>> import healpy as hp
>>> hp.reorder(np.arange(48), r2n = True)
array([13, 5, 4, 0, 15, 7, 6, 1, 17, 9, 8, 2, 19, 11, 10, 3, 28,
20, 27, 12, 30, 22, 21, 14, 32, 24, 23, 16, 34, 26, 25, 18, 44, 37,
36, 29, 45, 39, 38, 31, 46, 41, 40, 33, 47, 43, 42, 35])
>>> hp.reorder(np.arange(12), n2r = True)
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> hp.reorder(hp.ma(np.arange(12.)), n2r = True)
masked_array(data = [ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.],
mask = False,
fill_value = -1.6375e+30)
<BLANKLINE>
>>> m = [np.arange(12.), np.arange(12.), np.arange(12.)]
>>> m[0][2] = hp.UNSEEN
>>> m[1][2] = hp.UNSEEN
>>> m[2][2] = hp.UNSEEN
>>> m = hp.ma(m)
>>> hp.reorder(m, n2r = True)
(masked_array(data = [0.0 1.0 -- 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0],
mask = [False False True False False False False False False False False False],
fill_value = -1.6375e+30)
, masked_array(data = [0.0 1.0 -- 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0],
mask = [False False True False False False False False False False False False],
fill_value = -1.6375e+30)
, masked_array(data = [0.0 1.0 -- 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0],
mask = [False False True False False False False False False False False False],
fill_value = -1.6375e+30)
)
"""
typ = maptype(map_in)
if typ == 0:
npix = len(map_in)
else:
npix = len(map_in[0])
nside = npix2nside(npix)
if nside>128:
bunchsize = npix//24
else:
bunchsize = npix
if r2n:
inp='RING'
out='NEST'
if n2r:
inp='NEST'
out='RING'
inp = str(inp).upper()[0:4]
out = str(out).upper()[0:4]
if inp not in ['RING','NEST'] or out not in ['RING','NEST']:
raise ValueError('inp and out must be either RING or NEST')
if typ == 0:
mapin = [map_in]
else:
mapin = map_in
mapout = []
for m_in in mapin:
if inp == out:
mapout.append(m_in)
elif inp == 'RING':
m_out = np.zeros(npix,dtype=type(m_in[0]))
for ibunch in range(npix//bunchsize):
ipix_n = np.arange(ibunch*bunchsize,
(ibunch+1)*bunchsize)
ipix_r = nest2ring(nside, ipix_n)
m_out[ipix_n] = np.asarray(m_in)[ipix_r]
mapout.append(m_out)
elif inp == 'NEST':
m_out = np.zeros(npix,dtype=type(m_in[0]))
for ibunch in range(npix//bunchsize):
ipix_r = np.arange(ibunch*bunchsize,
(ibunch+1)*bunchsize)
ipix_n = ring2nest(nside, ipix_r)
m_out[ipix_r] = np.asarray(m_in)[ipix_n]
mapout.append(m_out)
if typ == 0:
return mapout[0]
else:
return mapout
def nside2npix(nside):
"""Give the number of pixels for the given nside.
Parameters
----------
nside : int
healpix nside parameter; an exception is raised if nside is not valid
(nside must be a power of 2, less than 2**30)
Returns
-------
npix : int
corresponding number of pixels
Notes
-----
Raise a ValueError exception if nside is not valid.
Examples
--------
>>> import healpy as hp
>>> import numpy as np
>>> hp.nside2npix(8)
768
>>> np.all([hp.nside2npix(nside) == 12 * nside**2 for nside in [2**n for n in range(12)]])
True
>>> hp.nside2npix(7)
Traceback (most recent call last):
...
ValueError: 7 is not a valid nside parameter (must be a power of 2, less than 2**30)
"""
check_nside(nside)
return 12*nside**2
def nside2order(nside):
"""Give the resolution order for a given nside.
Parameters
----------
nside : int
healpix nside parameter; an exception is raised if nside is not valid
(nside must be a power of 2, less than 2**30)
Returns
-------
order : int
corresponding order where nside = 2**(order)
Notes
-----
Raise a ValueError exception if nside is not valid.
Examples
--------
>>> import healpy as hp
>>> import numpy as np
>>> hp.nside2order(128)
7
>>> np.all([hp.nside2order(2**o) == o for o in range(30)])
True
>>> hp.nside2order(7)
Traceback (most recent call last):
...
ValueError: 7 is not a valid nside parameter (must be a power of 2, less than 2**30)
"""
check_nside(nside)
return len('{0:b}'.format(nside)) - 1
def nside2resol(nside, arcmin=False):
"""Give approximate resolution (pixel size in radian or arcmin) for nside.
Resolution is just the square root of the pixel area, which is a gross
approximation given the different pixel shapes
Parameters
----------
nside : int
healpix nside parameter, must be a power of 2, less than 2**30
arcmin : bool
if True, return resolution in arcmin, otherwise in radian
Returns
-------
resol : float
approximate pixel size in radians or arcmin
Notes
-----
Raise a ValueError exception if nside is not valid.
Examples
--------
>>> import healpy as hp
>>> hp.nside2resol(128, arcmin = True)
27.483891294539248
>>> hp.nside2resol(256)
0.0039973699529159707
>>> hp.nside2resol(7)
Traceback (most recent call last):
...
ValueError: 7 is not a valid nside parameter (must be a power of 2, less than 2**30)
"""
check_nside(nside)
resol = np.sqrt(nside2pixarea(nside))
if arcmin:
resol = np.rad2deg(resol) * 60
return resol
def nside2pixarea(nside, degrees=False):
"""Give pixel area given nside in square radians or square degrees.
Parameters
----------
nside : int
healpix nside parameter, must be a power of 2, less than 2**30
degrees : bool
if True, returns pixel area in square degrees, in square radians otherwise
Returns
-------
pixarea : float
pixel area in square radian or square degree
Notes
-----
Raise a ValueError exception if nside is not valid.
Examples
--------
>>> import healpy as hp
>>> hp.nside2pixarea(128, degrees = True)
0.2098234113027917
>>> hp.nside2pixarea(256)
1.5978966540475428e-05
>>> hp.nside2pixarea(7)
Traceback (most recent call last):
...
ValueError: 7 is not a valid nside parameter (must be a power of 2, less than 2**30)
"""
check_nside(nside)
pixarea = 4*np.pi/nside2npix(nside)
if degrees:
pixarea = np.rad2deg(np.rad2deg(pixarea))
return pixarea
def npix2nside(npix):
"""Give the nside parameter for the given number of pixels.
Parameters
----------
npix : int
the number of pixels
Returns
-------
nside : int
the nside parameter corresponding to npix
Notes
-----
Raise a ValueError exception if number of pixel does not correspond to
the number of pixel of an healpix map.
Examples
--------
>>> import healpy as hp
>>> hp.npix2nside(768)
8
>>> np.all([hp.npix2nside(12 * nside**2) == nside for nside in [2**n for n in range(12)]])
True
>>> hp.npix2nside(1000)
Traceback (most recent call last):
...
ValueError: Wrong pixel number (it is not 12*nside**2)
"""
nside = np.sqrt(npix/12.)
if nside != np.floor(nside):
raise ValueError("Wrong pixel number (it is not 12*nside**2)")
nside=int(np.floor(nside))
check_nside(nside)
return nside
def order2nside(order):
"""Give the nside parameter for the given resolution order.
Parameters
----------
order : int
the resolution order
Returns
-------
nside : int
the nside parameter corresponding to order
Notes
-----
Raise a ValueError exception if order produces an nside out of range.
Examples
--------
>>> import healpy as hp
>>> hp.order2nside(7)
128
>>> hp.order2nside(np.arange(8))
array([ 1, 2, 4, 8, 16, 32, 64, 128])
>>> hp.order2nside(31)
Traceback (most recent call last):
...
ValueError: 2147483648 is not a valid nside parameter (must be a power of 2, less than 2**30)
"""
nside = 1<<order
check_nside(nside)
return nside
def isnsideok(nside):
"""Returns :const:`True` if nside is a valid nside parameter, :const:`False` otherwise.
Parameters
----------
nside : int, scalar or array-like
integer value to be tested
Returns
-------
ok : bool, scalar or array-like
:const:`True` if given value is a valid nside, :const:`False` otherwise.
Examples
--------
>>> import healpy as hp
>>> hp.isnsideok(13)
False
>>> hp.isnsideok(32)
True
>>> hp.isnsideok([1, 2, 3, 4, 8, 16])
array([ True, True, False, True, True, True], dtype=bool)
"""
# we use standard bithacks from http://graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2
if hasattr(nside, '__len__'):
if not isinstance(nside, np.ndarray):
nside = np.asarray(nside)
return ((nside == nside.astype(np.int)) & (0 < nside) &
(nside <= max_nside) &
((nside.astype(np.int) & (nside.astype(np.int) - 1)) == 0))
else:
return (nside == int(nside) and 0 < nside <= max_nside and
(int(nside) & (int(nside) - 1)) == 0)
def check_nside(nside):
"""Raises exception is nside is not valid"""
if not np.all(isnsideok(nside)):
raise ValueError("%s is not a valid nside parameter (must be a power of 2, less than 2**30)" % str(nside))
def isnpixok(npix):
"""Return :const:`True` if npix is a valid value for healpix map size, :const:`False` otherwise.
Parameters
----------
npix : int, scalar or array-like
integer value to be tested
Returns
-------
ok : bool, scalar or array-like
:const:`True` if given value is a valid number of pixel, :const:`False` otherwise
Examples
--------
>>> import healpy as hp
>>> hp.isnpixok(12)
True
>>> hp.isnpixok(768)
True
>>> hp.isnpixok([12, 768, 1002])
array([ True, True, False], dtype=bool)
"""
if hasattr(npix,'__len__'):
nside = np.sqrt(np.asarray(npix)/12.)
return isnsideok(nside)
else:
nside = np.sqrt(npix/12.)
return isnsideok(nside)
def get_interp_val(m,theta,phi,nest=False,lonlat=False):
"""Return the bi-linear interpolation value of a map using 4 nearest neighbours.
Parameters
----------
m : array-like
an healpix map, accepts masked arrays
theta, phi : float, scalar or array-like
angular coordinates of point at which to interpolate the map
nest : bool
if True, the is assumed to be in NESTED ordering.
lonlat : bool
If True, input angles are assumed to be longitude and latitude in degree,
otherwise, they are co-latitude and longitude in radians.
Returns
-------
val : float, scalar or arry-like
the interpolated value(s), usual numpy broadcasting rules apply.
See Also
--------
get_interp_weights, get_all_neighbours
Examples
--------
>>> import healpy as hp
>>> hp.get_interp_val(np.arange(12.), np.pi/2, 0)
4.0
>>> hp.get_interp_val(np.arange(12.), np.pi/2, np.pi/2)
5.0
>>> hp.get_interp_val(np.arange(12.), np.pi/2, np.pi/2 + 2*np.pi)
5.0
>>> hp.get_interp_val(np.arange(12.), np.linspace(0, np.pi, 10), 0)
array([ 1.5 , 1.5 , 1.5 , 2.20618428, 3.40206143,
5.31546486, 7.94639458, 9.5 , 9.5 , 9.5 ])
>>> hp.get_interp_val(np.arange(12.), 0, np.linspace(90, -90, 10), lonlat=True)
array([ 1.5 , 1.5 , 1.5 , 2.20618428, 3.40206143,
5.31546486, 7.94639458, 9.5 , 9.5 , 9.5 ])
"""
m2=m.ravel()
nside=npix2nside(m2.size)
if lonlat:
theta,phi = lonlat2thetaphi(theta,phi)
if nest:
r=pixlib._get_interpol_nest(nside,theta,phi)
else:
r=pixlib._get_interpol_ring(nside,theta,phi)
p=np.array(r[0:4])
w=np.array(r[4:8])
del r
return np.sum(m2[p]*w,0)
def get_neighbours(nside, theta, phi=None, nest=False):
raise NameError("get_neighbours has been renamed to get_interp_weights")
def get_interp_weights(nside,theta,phi=None,nest=False,lonlat=False):
"""Return the 4 closest pixels on the two rings above and below the
location and corresponding weights.
Weights are provided for bilinear interpolation along latitude and longitude
Parameters
----------
nside : int
the healpix nside
theta, phi : float, scalar or array-like
if phi is not given, theta is interpreted as pixel number,
otherwise theta[rad],phi[rad] are angular coordinates
nest : bool
if ``True``, NESTED ordering, otherwise RING ordering.
lonlat : bool
If True, input angles are assumed to be longitude and latitude in degree,
otherwise, they are co-latitude and longitude in radians.
Returns
-------
res : tuple of length 2
contains pixel numbers in res[0] and weights in res[1].
Usual numpy broadcasting rules apply.
See Also
--------
get_interp_val, get_all_neighbours
Examples
--------
>>> import healpy as hp
>>> hp.get_interp_weights(1, 0)
(array([0, 1, 4, 5]), array([ 1., 0., 0., 0.]))
>>> hp.get_interp_weights(1, 0, 0)
(array([1, 2, 3, 0]), array([ 0.25, 0.25, 0.25, 0.25]))
>>> hp.get_interp_weights(1, 0, 90, lonlat=True)
(array([1, 2, 3, 0]), array([ 0.25, 0.25, 0.25, 0.25]))
>>> hp.get_interp_weights(1, [0, np.pi/2], 0)
(array([[ 1, 4],
[ 2, 5],
[ 3, 11],
[ 0, 8]]), array([[ 0.25, 1. ],
[ 0.25, 0. ],
[ 0.25, 0. ],
[ 0.25, 0. ]]))
"""
check_nside(nside)
if phi == None:
theta,phi = pix2ang(nside,theta,nest=nest)
elif lonlat:
theta,phi = lonlat2thetaphi(theta,phi)
if nest:
r=pixlib._get_interpol_nest(nside,theta,phi)
else:
r=pixlib._get_interpol_ring(nside,theta,phi)
p=np.array(r[0:4])
w=np.array(r[4:8])
return (p,w)
def get_all_neighbours(nside, theta, phi=None, nest=False, lonlat=False):
"""Return the 8 nearest pixels.
Parameters
----------
nside : int
the nside to work with
theta, phi : scalar or array-like
if phi is not given or None, theta is interpreted as pixel number,
otherwise, theta[rad],phi[rad] are angular coordinates
nest : bool
if ``True``, pixel number will be NESTED ordering, otherwise RING ordering.
lonlat : bool
If True, input angles are assumed to be longitude and latitude in degree,
otherwise, they are co-latitude and longitude in radians.
Returns
-------
ipix : int, array
pixel number of the SW, W, NW, N, NE, E, SE and S neighbours,
shape is (8,) if input is scalar, otherwise shape is (8, N) if input is
of length N. If a neighbor does not exist (it can be the case for W, N, E and S)
the corresponding pixel number will be -1.
See Also
--------
get_interp_weights, get_interp_val
Examples
--------
>>> import healpy as hp
>>> hp.get_all_neighbours(1, 4)
array([11, 7, 3, -1, 0, 5, 8, -1])
>>> hp.get_all_neighbours(1, np.pi/2, np.pi/2)
array([ 8, 4, 0, -1, 1, 6, 9, -1])
>>> hp.get_all_neighbours(1, 90, 0, lonlat=True)
array([ 8, 4, 0, -1, 1, 6, 9, -1])
"""
check_nside(nside)
if phi is not None:
theta = ang2pix(nside, theta, phi, nest=nest, lonlat=lonlat)
if nest:
r=pixlib._get_neighbors_nest(nside,theta)
else:
r=pixlib._get_neighbors_ring(nside,theta)
res=np.array(r[0:8])
return res
def max_pixrad(nside):
"""Maximum angular distance between any pixel center and its corners
Parameters
----------
nside : int
the nside to work with
Returns
-------
rads: double
angular distance (in radians)
Examples
--------
>>> '%.15f' % max_pixrad(1)
'0.841068670567930'
>>> '%.15f' % max_pixrad(16)
'0.066014761432513'
"""
check_nside(nside)
return pixlib._max_pixrad(nside)
def fit_dipole(m, nest=False, bad=UNSEEN, gal_cut=0):
"""Fit a dipole and a monopole to the map, excluding bad pixels.
Parameters
----------
m : float, array-like
the map to which a dipole is fitted and subtracted, accepts masked maps
nest : bool
if ``False`` m is assumed in RING scheme, otherwise map is NESTED
bad : float
bad values of pixel, default to :const:`UNSEEN`.
gal_cut : float
pixels at latitude in [-gal_cut;+gal_cut] degrees are not taken into account
Returns
-------
res : tuple of length 2
the monopole value in res[0] and the dipole vector (as array) in res[1]
See Also
--------
remove_dipole, fit_monopole, remove_monopole
"""
m=ma_to_array(m)
m=np.asarray(m)
npix = m.size
nside = npix2nside(npix)
if nside>128:
bunchsize = npix//24
else:
bunchsize = npix
aa = np.zeros((4,4),dtype=np.float64)
v = np.zeros(4,dtype=np.float64)
for ibunch in range(npix//bunchsize):
ipix = np.arange(ibunch*bunchsize,
(ibunch+1)*bunchsize)
ipix = ipix[(m.flat[ipix]!=bad) & (np.isfinite(m.flat[ipix]))]
x,y,z = pix2vec(nside, ipix, nest)
if gal_cut>0:
w = (np.abs(z)>=np.sin(gal_cut*np.pi/180))
ipix=ipix[w]
x=x[w]
y=y[w]
z=z[w]
del w
aa[0,0] += ipix.size
aa[1,0] += x.sum()
aa[2,0] += y.sum()
aa[3,0] += z.sum()
aa[1,1] += (x**2).sum()
aa[2,1] += (x*y).sum()
aa[3,1] += (x*z).sum()
aa[2,2] += (y**2).sum()
aa[3,2] += (y*z).sum()
aa[3,3] += (z**2).sum()
v[0] += m.flat[ipix].sum()
v[1] += (m.flat[ipix]*x).sum()
v[2] += (m.flat[ipix]*y).sum()
v[3] += (m.flat[ipix]*z).sum()
aa[0,1] = aa[1,0]
aa[0,2] = aa[2,0]
aa[0,3] = aa[3,0]
aa[1,2] = aa[2,1]
aa[1,3] = aa[3,1]
aa[2,3] = aa[3,2]
res = np.dot(np.linalg.inv(aa),v)
mono = res[0]
dipole = res[1:4]
return mono,dipole
def remove_dipole(m,nest=False,bad=UNSEEN,gal_cut=0,fitval=False,
copy=True,verbose=True):
"""Fit and subtract the dipole and the monopole from the given map m.
Parameters
----------
m : float, array-like
the map to which a dipole is fitted and subtracted, accepts masked arrays
nest : bool
if ``False`` m is assumed in RING scheme, otherwise map is NESTED
bad : float
bad values of pixel, default to :const:`UNSEEN`.
gal_cut : float
pixels at latitude in [-gal_cut;+gal_cut] are not taken into account
fitval : bool
whether to return or not the fitted values of monopole and dipole
copy : bool
whether to modify input map or not (by default, make a copy)
verbose : bool
print values of monopole and dipole
Returns
-------
res : array or tuple of length 3
if fitval is False, returns map with monopole and dipole subtracted,
otherwise, returns map (array, in res[0]), monopole (float, in res[1]),
dipole_vector (array, in res[2])
See Also
--------
fit_dipole, fit_monopole, remove_monopole
"""
input_ma = is_ma(m)
m=ma_to_array(m)
m=np.array(m,copy=copy)
npix = m.size
nside = npix2nside(npix)
if nside>128:
bunchsize = npix//24
else:
bunchsize = npix
mono,dipole = fit_dipole(m,nest=nest,bad=bad,gal_cut=gal_cut)
for ibunch in range(npix//bunchsize):
ipix = np.arange(ibunch*bunchsize,
(ibunch+1)*bunchsize)
ipix = ipix[(m.flat[ipix]!=bad) & (np.isfinite(m.flat[ipix]))]
x,y,z = pix2vec(nside, ipix, nest)
m.flat[ipix] -= (dipole[0]*x)
m.flat[ipix] -= dipole[1]*y
m.flat[ipix] -= dipole[2]*z
m.flat[ipix] -= mono
if verbose:
from . import rotator as R
lon,lat = R.vec2dir(dipole,lonlat=True)
amp = np.sqrt((dipole**2).sum())
print(
'monopole: {0:g} dipole: lon: {1:g}, lat: {2:g}, amp: {3:g}'
.format(mono, lon, lat, amp))
if is_ma:
m = ma(m)
if fitval:
return m,mono,dipole
else:
return m
def fit_monopole(m,nest=False,bad=UNSEEN,gal_cut=0):
"""Fit a monopole to the map, excluding unseen pixels.
Parameters
----------
m : float, array-like
the map to which a dipole is fitted and subtracted, accepts masked arrays
nest : bool
if ``False`` m is assumed in RING scheme, otherwise map is NESTED
bad : float
bad values of pixel, default to :const:`UNSEEN`.
gal_cut : float
pixels at latitude in [-gal_cut;+gal_cut] degrees are not taken into account
Returns
-------
res: float
fitted monopole value
See Also
--------
fit_dipole, remove_monopole, remove_monopole
"""
m=ma_to_array(m)
m=np.asarray(m)
npix=m.size
nside = npix2nside(npix)
if nside>128:
bunchsize=npix//24
else:
bunchsize=npix
aa = v = 0.0
for ibunch in range(npix//bunchsize):
ipix = np.arange(ibunch*bunchsize,
(ibunch+1)*bunchsize)
ipix = ipix[(m.flat[ipix]!=bad) & (np.isfinite(m.flat[ipix]))]
x,y,z = pix2vec(nside, ipix, nest)
if gal_cut>0:
w = (np.abs(z)>=np.sin(gal_cut*np.pi/180))
ipix=ipix[w]
x=x[w]
y=y[w]
z=z[w]
del w
aa += ipix.size
v += m.flat[ipix].sum()
mono = v/aa
return mono
def remove_monopole(m,nest=False,bad=UNSEEN,gal_cut=0,fitval=False,
copy=True,verbose=True):
"""Fit and subtract the monopole from the given map m.
Parameters
----------
m : float, array-like
the map to which a monopole is fitted and subtracted
nest : bool
if ``False`` m is assumed in RING scheme, otherwise map is NESTED
bad : float
bad values of pixel, default to :const:`UNSEEN`.
gal_cut : float
pixels at latitude in [-gal_cut;+gal_cut] are not taken into account
fitval : bool
whether to return or not the fitted value of monopole
copy : bool
whether to modify input map or not (by default, make a copy)
verbose: bool
whether to print values of monopole
Returns
-------
res : array or tuple of length 3
if fitval is False, returns map with monopole subtracted,
otherwise, returns map (array, in res[0]) and monopole (float, in res[1])
See Also
--------
fit_dipole, fit_monopole, remove_dipole
"""
input_ma = is_ma(m)
m= ma_to_array(m)
m=np.array(m,copy=copy)
npix = m.size
nside = npix2nside(npix)
if nside>128:
bunchsize = npix//24
else:
bunchsize = npix
mono = fit_monopole(m,nest=nest,bad=bad,gal_cut=gal_cut)
for ibunch in range(npix//bunchsize):
ipix = np.arange(ibunch*bunchsize,
(ibunch+1)*bunchsize)
ipix = ipix[(m.flat[ipix]!=bad) & (np.isfinite(m.flat[ipix]))]
x,y,z = pix2vec(nside, ipix, nest)
m.flat[ipix] -= mono
if verbose:
print('monopole: {0:g}'.format(mono))
if input_ma:
m = ma(m)
if fitval:
return m,mono
else:
return m
def get_map_size(m):
"""Returns the npix of a given map (implicit or explicit pixelization).
If map is a dict type, assumes explicit pixelization: use nside key if present, or use
nside attribute if present, otherwise use the smallest valid npix given the maximum key value.
otherwise assumes implicit pixelization and returns len(m).
Parameters
----------
m : array-like or dict-like
a map with implicit (array-like) or explicit (dict-like) pixellization
Returns
-------
npix : int
a valid number of pixel
Notes
-----
In implicit pixellization, raise a ValueError exception if the size of the input
is not a valid pixel number.
Examples
--------
>>> import healpy as hp
>>> m = {0: 1, 1: 1, 2: 1, 'nside': 1}
>>> print(hp.get_map_size(m))
12
>>> m = {0: 1, 767: 1}
>>> print(hp.get_map_size(m))
768
>>> print(hp.get_map_size(np.zeros(12 * 8 ** 2)))
768
"""
if isinstance(m, dict):
if 'nside' in m:
return nside2npix(m['nside'])
elif hasattr(ma, 'nside'):
return nside2npix(m.nside)
else:
nside = get_min_valid_nside(max(m.keys())+1)
return nside2npix(nside)
else:
if isnpixok(len(m)):
return len(m)
else:
raise ValueError("Wrong pixel number (it is not 12*nside**2)")
def get_min_valid_nside(npix):
"""Returns the minimum acceptable nside so that npix <= nside2npix(nside).
Parameters
----------
npix : int
a minimal number of pixel
Returns
-------
nside : int
a valid healpix nside so that 12 * nside ** 2 >= npix
Examples
--------
>>> import healpy as hp
>>> hp.pixelfunc.get_min_valid_nside(355)
8
>>> hp.pixelfunc.get_min_valid_nside(768)
8
"""
order = 0.5 * np.log2(npix / 12.)
return 2**int(np.ceil(order))
def get_nside(m):
"""Return the nside of the given map.
Parameters
----------
m : sequence
the map to get the nside from.
Returns
-------
nside : int
the healpix nside parameter of the map (or sequence of maps)
Notes
-----
If the input is a sequence of maps, all of them must have same size.
If the input is not a valid map (not a sequence, unvalid number of pixels),
a TypeError exception is raised.
"""
typ = maptype(m)
if typ == 0:
return npix2nside(len(m))
else:
return npix2nside(len(m[0]))
@accept_ma
def ud_grade(map_in,nside_out,pess=False,order_in='RING',order_out=None,
power=None, dtype=None):
"""Upgrade or degrade resolution of a map (or list of maps).
in degrading the resolution, ud_grade sets the value of the superpixel
as the mean of the children pixels.
Parameters
----------
map_in : array-like or sequence of array-like
the input map(s) (if a sequence of maps, all must have same size)
nside_out : int
the desired nside of the output map(s)
pess : bool
if ``True``, in degrading, reject pixels which contains
a bad sub_pixel. Otherwise, estimate average with good pixels
order_in, order_out : str
pixel ordering of input and output ('RING' or 'NESTED')
power : float
if non-zero, divide the result by (nside_in/nside_out)**power
Examples:
power=-2 keeps the sum of the map invariant (useful for hitmaps),
power=2 divides the mean by another factor of (nside_in/nside_out)**2
(useful for variance maps)
dtype : type
the type of the output map
Returns
-------
map_out : array-like or sequence of array-like
the upgraded or degraded map(s)
Examples
--------
>>> import healpy as hp
>>> hp.ud_grade(np.arange(48.), 1)
array([ 5.5 , 7.25, 9. , 10.75, 21.75, 21.75, 23.75, 25.75,
36.5 , 38.25, 40. , 41.75])
"""
check_nside(nside_out)
typ = maptype(map_in)
if typ<0:
raise TypeError('Invalid map')
if typ == 0:
m_in = [map_in]
else:
m_in = map_in
mapout = []
if order_out is None: order_out = order_in
for m in m_in:
if str(order_in).upper()[0:4] == 'RING':
m = reorder(m,r2n=True)
mout = _ud_grade_core(m,nside_out,pess=pess, power=power, dtype=dtype)
if str(order_out).upper()[0:4] == 'RING':
mout = reorder(mout,n2r=True)
mapout.append(mout)
if typ == 0:
return mapout[0]
else:
return mapout
def _ud_grade_core(m,nside_out,pess=False,power=None, dtype=None):
"""Internal routine used by ud_grade. It assumes that the map is NESTED
and single (not a list of maps)
"""
nside_in = get_nside(m)
if dtype:
type_out = dtype
else:
type_out = type(m[0])
check_nside(nside_out)
npix_in = nside2npix(nside_in)
npix_out = nside2npix(nside_out)
if power:
power = float(power)
ratio = (float(nside_out)/float(nside_in))**power
else:
ratio = 1
if nside_out > nside_in:
rat2 = npix_out//npix_in
fact = np.ones(rat2, dtype=type_out)*ratio
map_out = np.outer(m,fact).reshape(npix_out)
elif nside_out < nside_in:
rat2 = npix_in//npix_out
mr = m.reshape(npix_out,rat2)
goods = ~(mask_bad(mr) | (~np.isfinite(mr)))
map_out = np.sum(mr*goods, axis=1).astype(type_out)
nhit = goods.sum(axis=1)
if pess:
badout = np.where(nhit != rat2)
else:
badout = np.where(nhit == 0)
if power:
nhit = nhit / ratio
map_out[nhit!=0] = map_out[nhit!=0] / nhit[nhit!=0]
try:
map_out[badout] = UNSEEN
except OverflowError:
pass
else:
map_out = m
return map_out.astype(type_out)
|