# Path: ugali/utils/healpix.py
# def superpixel(subpix, nside_subpix, nside_superpix):
# def subpixel(superpix, nside_superpix, nside_subpix):
# def d_grade_ipix(ipix, nside_in, nside_out, nest=False):
# def u_grade_ipix(ipix, nside_in, nside_out, nest=False):
# def ud_grade_ipix(ipix, nside_in, nside_out, nest=False):
# def phi2lon(phi): return np.degrees(phi)
# def lon2phi(lon): return np.radians(lon)
# def theta2lat(theta): return 90. - np.degrees(theta)
# def lat2theta(lat): return np.radians(90. - lat)
# def pix2ang(nside, pix, nest=False):
# def ang2pix(nside, lon, lat, nest=False):
# def ang2vec(lon, lat):
# def get_nside(m):
# def healpixMap(nside, lon, lat, fill_value=0., nest=False):
# def in_pixels(lon,lat,pixels,nside):
# def index_pix_in_pixels(pix,pixels,sort=False,outside=-1):
# def index_lonlat_in_pixels(lon,lat,pixels,nside,sort=False,outside=-1):
# def query_disc(nside, vec, radius, inclusive=False, fact=4, nest=False):
# def ang2disc(nside, lon, lat, radius, inclusive=False, fact=4, nest=False):
# def get_interp_val(m, lon, lat, *args, **kwargs):
# def header_odict(nside,nest=False,coord=None, partial=True):
# def write_partial_map(filename, data, nside, coord=None, nest=False,
#                       header=None,dtype=None,**kwargs):
# def read_partial_map(filenames, column, fullsky=True, **kwargs):
# def merge_partial_maps(filenames,outfile,**kwargs):
# def merge_likelihood_headers(filenames, outfile, **kwargs):
# def merge_likelihood_headers2(filenames, outfile, **kwargs):
# def read_map(filename, nest=False, hdu=None, h=False, verbose=True):
# 
# Path: ugali/utils/logger.py
# class SpecialFormatter(logging.Formatter):
#     FORMATS = {'DEFAULT'       : "%(message)s",
#                logging.WARNING : "WARNING: %(message)s",
#                logging.ERROR   : "ERROR: %(message)s"}
#     def format(self, record):
# def file_found(filename,force):


import unittest
import numpy as np
import healpy as hp
import fitsio
from ugali.utils import healpix
from ugali.utils.logger import logger
NSIDE = 4096
FACTOR = 4
PIX = np.array([104582830,  43361203, 142027178])
U_GRADE_PIX_NEST = np.array([[418331320, 418331321, 418331322, 418331323],
                             [173444812, 173444813, 173444814, 173444815],
                             [568108712, 568108713, 568108714, 568108715]])
U_GRADE_PIX_RING = np.array([[418356572, 418323804, 418323803, 418291036],
                             [173492070, 173459302, 173459301, 173426534],
                             [568152916, 568120148, 568120147, 568087380]])

D_GRADE_PIX_NEST = PIX//FACTOR
D_GRADE_PIX_RING = np.array([26142551, 10842585, 35509461])


UD_GRADE_PIX = np.repeat(PIX,4).reshape(-1,4)
DU_GRADE_PIX_NEST = np.array([[104582828, 104582829, 104582830, 104582831],
                              [ 43361200,  43361201,  43361202,  43361203],
                              [142027176, 142027177, 142027178, 142027179]])
DU_GRADE_PIX_RING = np.array([[104582830, 104566446, 104566445, 104550062],
                              [ 43393971,  43377587,  43377586,  43361203],
                              [142059946, 142043562, 142043561, 142027178]])



class TestHealpix(unittest.TestCase):
    """Test healpix module"""

    def test_ud_grade_ipix(self):

        # Same NSIDE

# Path: ugali/utils/healpix.py
# def superpixel(subpix, nside_subpix, nside_superpix):
# def subpixel(superpix, nside_superpix, nside_subpix):
# def d_grade_ipix(ipix, nside_in, nside_out, nest=False):
# def u_grade_ipix(ipix, nside_in, nside_out, nest=False):
# def ud_grade_ipix(ipix, nside_in, nside_out, nest=False):
# def phi2lon(phi): return np.degrees(phi)
# def lon2phi(lon): return np.radians(lon)
# def theta2lat(theta): return 90. - np.degrees(theta)
# def lat2theta(lat): return np.radians(90. - lat)
# def pix2ang(nside, pix, nest=False):
# def ang2pix(nside, lon, lat, nest=False):
# def ang2vec(lon, lat):
# def get_nside(m):
# def healpixMap(nside, lon, lat, fill_value=0., nest=False):
# def in_pixels(lon,lat,pixels,nside):
# def index_pix_in_pixels(pix,pixels,sort=False,outside=-1):
# def index_lonlat_in_pixels(lon,lat,pixels,nside,sort=False,outside=-1):
# def query_disc(nside, vec, radius, inclusive=False, fact=4, nest=False):
# def ang2disc(nside, lon, lat, radius, inclusive=False, fact=4, nest=False):
# def get_interp_val(m, lon, lat, *args, **kwargs):
# def header_odict(nside,nest=False,coord=None, partial=True):
# def write_partial_map(filename, data, nside, coord=None, nest=False,
#                       header=None,dtype=None,**kwargs):
# def read_partial_map(filenames, column, fullsky=True, **kwargs):
# def merge_partial_maps(filenames,outfile,**kwargs):
# def merge_likelihood_headers(filenames, outfile, **kwargs):
# def merge_likelihood_headers2(filenames, outfile, **kwargs):
# def read_map(filename, nest=False, hdu=None, h=False, verbose=True):
# 
# Path: ugali/utils/logger.py
# class SpecialFormatter(logging.Formatter):
#     FORMATS = {'DEFAULT'       : "%(message)s",
#                logging.WARNING : "WARNING: %(message)s",
#                logging.ERROR   : "ERROR: %(message)s"}
#     def format(self, record):
# def file_found(filename,force):


# Path: tests/test_healpix.py
import unittest
import numpy as np
import healpy as hp
import fitsio
from ugali.utils import healpix
from ugali.utils.logger import logger

NSIDE = 4096
FACTOR = 4
PIX = np.array([104582830,  43361203, 142027178])
U_GRADE_PIX_NEST = np.array([[418331320, 418331321, 418331322, 418331323],
                             [173444812, 173444813, 173444814, 173444815],
                             [568108712, 568108713, 568108714, 568108715]])
U_GRADE_PIX_RING = np.array([[418356572, 418323804, 418323803, 418291036],
                             [173492070, 173459302, 173459301, 173426534],
                             [568152916, 568120148, 568120147, 568087380]])

D_GRADE_PIX_NEST = PIX//FACTOR
D_GRADE_PIX_RING = np.array([26142551, 10842585, 35509461])


UD_GRADE_PIX = np.repeat(PIX,4).reshape(-1,4)
DU_GRADE_PIX_NEST = np.array([[104582828, 104582829, 104582830, 104582831],
                              [ 43361200,  43361201,  43361202,  43361203],
                              [142027176, 142027177, 142027178, 142027179]])
DU_GRADE_PIX_RING = np.array([[104582830, 104566446, 104566445, 104550062],
                              [ 43393971,  43377587,  43377586,  43361203],
                              [142059946, 142043562, 142043561, 142027178]])



class TestHealpix(unittest.TestCase):
    """Test healpix module"""

    def test_ud_grade_ipix(self):

        # Same NSIDE

        np.testing.assert_equal(healpix.ud_grade_ipix(PIX,NSIDE,NSIDE),PIX)