# Path: tkp/utility/coordinates.py
# def unix2julian(timestamp):
#     """
#     Convert a Unix timestamp (number of seconds since 1 January 1970) to a
#     modified Julian timestamp (number of seconds since 17 November 1858).
# 
#     Args:
#         timestamp (numbers.Number): Number of seconds since the modified
#             Julian epoch.
# 
#     Returns:
#         numbers.Number: Number of seconds since the Unix epoch.
#     """
#     return timestamp + unix_epoch


import sys
import logging
from io import BytesIO
from casacore.measures import measures
from tkp.utility.coordinates import unix2julian
from tkp.utility.redirect_stream import redirect_stream
    casacore_stderr = BytesIO()
    with redirect_stream(sys.__stderr__, casacore_stderr):
        # We assume the ephemeris is valid if it has position of the sun.
        measures.separation(
            measures.direction("SUN"), measures.direction("SUN")
        )
    if "WARN" in casacore_stderr.getvalue():
        # casacore sends a warning to stderr if the ephemeris is invalid
        return False
    else:
        return True

def is_bright_source_near(accessor, distance=20):
    """
    Checks if there is any of the bright radio sources defined in targets
    near the center of the image.

    :param accessor: a TKP accessor
    :param distance: maximum allowed distance of a bright source (in degrees)
    :returns: False if not bright source is near, description of source if a
              bright source is near
    """

    #TODO: this function should be split up and tested more atomically
    # The measures object is our interface to casacore
    m = measures()

    # First, you need to set the reference frame -- ie, the time
    # -- used for the calculations to come. Time as MJD in seconds.
    starttime = int(accessor.taustart_ts.strftime("%s"))

# Path: tkp/utility/coordinates.py
# def unix2julian(timestamp):
#     """
#     Convert a Unix timestamp (number of seconds since 1 January 1970) to a
#     modified Julian timestamp (number of seconds since 17 November 1858).
# 
#     Args:
#         timestamp (numbers.Number): Number of seconds since the modified
#             Julian epoch.
# 
#     Returns:
#         numbers.Number: Number of seconds since the Unix epoch.
#     """
#     return timestamp + unix_epoch


# Path: tkp/quality/brightsource.py
import sys
import logging
from io import BytesIO
from casacore.measures import measures
from tkp.utility.coordinates import unix2julian
from tkp.utility.redirect_stream import redirect_stream

    casacore_stderr = BytesIO()
    with redirect_stream(sys.__stderr__, casacore_stderr):
        # We assume the ephemeris is valid if it has position of the sun.
        measures.separation(
            measures.direction("SUN"), measures.direction("SUN")
        )
    if "WARN" in casacore_stderr.getvalue():
        # casacore sends a warning to stderr if the ephemeris is invalid
        return False
    else:
        return True

def is_bright_source_near(accessor, distance=20):
    """
    Checks if there is any of the bright radio sources defined in targets
    near the center of the image.

    :param accessor: a TKP accessor
    :param distance: maximum allowed distance of a bright source (in degrees)
    :returns: False if not bright source is near, description of source if a
              bright source is near
    """

    #TODO: this function should be split up and tested more atomically
    # The measures object is our interface to casacore
    m = measures()

    # First, you need to set the reference frame -- ie, the time
    # -- used for the calculations to come. Time as MJD in seconds.
    starttime = int(accessor.taustart_ts.strftime("%s"))

    starttime_mjd = unix2julian(starttime)