# Path: tkp/utility/fits.py
# def fix_reference_dec(imagename):
#     """
#     If the FITS file specified has a reference dec of 90 (or pi/2), make it
#     infinitesimally less. This works around problems with ill-defined
#     coordinate systems at the north celestial pole.
#     """
#     # TINY is an arbitrary constant which we regard as "far enough" away from
#     # dec 90 (or pi/2). In theory, we ought to be able to us
#     # sys.float_info.epsilon, but pyfits seems to round this when writing it
#     # to a FITS file so that isn't quite generous enough.
#     TINY = 1e-10
#     with pyfits.open(imagename, mode='update') as ff:
#         # The FITS standard (version 3.0, July 2008) tells us "For angular
#         # measurements given as floating-point values [...] the units should
#         # be degrees". We therefore use that as a default, but handle radians
#         # too, just to be on the safe side.
#         critical_value = 90.0 # degrees
#         if "CUNIT2" in ff[0].header and ff[0].header["CUNIT2"] == "rad":
#             critical_value = math.pi/2 # radians
# 
#         ref_dec = ff[0].header['CRVAL2']
#         if (critical_value - abs(ref_dec)) < TINY:
#             ff[0].header['CRVAL2'] = ref_dec * (1 - TINY)
#             ff.flush()


import unittest
import astropy.io.fits as fits
import math
import tempfile
from tkp.utility.fits import fix_reference_dec

class TestFixReferenceDec(unittest.TestCase):
    def test_dec_90(self):
        # Default unit is degrees.
        self._test_for_reference_dec(90.0)

    def test_dec_minus90(self):
        # Default unit is degrees.
        self._test_for_reference_dec(-90.0)

    def test_dec_90_deg(self):
        self._test_for_reference_dec(90.0, "deg")

    def test_dec_pi_by_2_rad(self):
        self._test_for_reference_dec(math.pi/2, "rad")

    def _test_for_reference_dec(self, refdec, unit=None):
        with tempfile.NamedTemporaryFile() as temp_fits:
            h = fits.PrimaryHDU()
            h.header["CRVAL2"] = refdec
            if unit:
                h.header["CUNIT2"] = unit
            h.writeto(temp_fits.name)

# Path: tkp/utility/fits.py
# def fix_reference_dec(imagename):
#     """
#     If the FITS file specified has a reference dec of 90 (or pi/2), make it
#     infinitesimally less. This works around problems with ill-defined
#     coordinate systems at the north celestial pole.
#     """
#     # TINY is an arbitrary constant which we regard as "far enough" away from
#     # dec 90 (or pi/2). In theory, we ought to be able to us
#     # sys.float_info.epsilon, but pyfits seems to round this when writing it
#     # to a FITS file so that isn't quite generous enough.
#     TINY = 1e-10
#     with pyfits.open(imagename, mode='update') as ff:
#         # The FITS standard (version 3.0, July 2008) tells us "For angular
#         # measurements given as floating-point values [...] the units should
#         # be degrees". We therefore use that as a default, but handle radians
#         # too, just to be on the safe side.
#         critical_value = 90.0 # degrees
#         if "CUNIT2" in ff[0].header and ff[0].header["CUNIT2"] == "rad":
#             critical_value = math.pi/2 # radians
# 
#         ref_dec = ff[0].header['CRVAL2']
#         if (critical_value - abs(ref_dec)) < TINY:
#             ff[0].header['CRVAL2'] = ref_dec * (1 - TINY)
#             ff.flush()


# Path: tests/test_utility/test_fits.py
import unittest
import astropy.io.fits as fits
import math
import tempfile
from tkp.utility.fits import fix_reference_dec


class TestFixReferenceDec(unittest.TestCase):
    def test_dec_90(self):
        # Default unit is degrees.
        self._test_for_reference_dec(90.0)

    def test_dec_minus90(self):
        # Default unit is degrees.
        self._test_for_reference_dec(-90.0)

    def test_dec_90_deg(self):
        self._test_for_reference_dec(90.0, "deg")

    def test_dec_pi_by_2_rad(self):
        self._test_for_reference_dec(math.pi/2, "rad")

    def _test_for_reference_dec(self, refdec, unit=None):
        with tempfile.NamedTemporaryFile() as temp_fits:
            h = fits.PrimaryHDU()
            h.header["CRVAL2"] = refdec
            if unit:
                h.header["CUNIT2"] = unit
            h.writeto(temp_fits.name)

            fix_reference_dec(temp_fits.name)