Source code for sigpyproc.Header

import numpy as np
import sigpyproc.HeaderParams as conf
from os.path import splitext
from struct import pack
from sigpyproc.Utils import File





def _write_string(key, value):
    key = key.encode()
    value = value.encode()
    return b"".join([pack("I", len(key)), key, pack('I', len(value)), value])


def _write_int(key, value):
    key = key.encode()
    return b"".join([pack('I', len(key)), key, pack('I', value)])


def _write_double(key, value):
    key = key.encode()
    return b"".join([pack('I', len(key)), key, pack('d', value)])


def _write_char(key, value):
    key = key.encode()
    return b"".join([pack('I', len(key)), key, pack('b', value)])


[docs]def radec_to_str(val): """Convert Sigproc format RADEC float to a string. :param val: Sigproc style RADEC float (eg. 124532.123) :type val: float :returns: 'xx:yy:zz.zzz' format string :rtype: :func:`str` """ sign = -1 if val < 0 else 1 val = np.fabs(val) xx = int(val // 10000) yy = int(val // 100) - xx * 100 zz = val - 100 * yy - 10000 * xx return f"{sign*xx:02d}:{yy:02d}:{zz:07.4f}"
[docs]def MJD_to_Gregorian(mjd): """Convert Modified Julian Date to the Gregorian calender. :param mjd: Modified Julian Date :type mjd float: :returns: date and time :rtype: :func:`tuple` of :func:`str` """ hh = np.fmod(mjd, 1) * 24.0 mm = np.fmod(hh, 1) * 60.0 ss = np.fmod(mm, 1) * 60.0 j = mjd + 2400000.5 j = int(j) j = j - 1721119 y = (4 * j - 1) // 146097 j = 4 * j - 1 - 146097 * y d = j // 4 j = (4 * d + 3) // 1461 d = 4 * d + 3 - 1461 * j d = (d + 4) // 4 m = (5 * d - 3) // 153 d = 5 * d - 3 - 153 * m d = (d + 5) // 5 y = 100 * y + j if m < 10: m = m + 3 else: m = m - 9 y = y + 1 return (f"{d:02d}/{m:02d}/{y:02d}", f"{int(hh):02d}:{int(mm):02d}:{ss:08.5f}")
[docs]def rad_to_dms(rad): """Convert radians to (degrees, arcminutes, arcseconds).""" sign = -1 if rad < 0 else 1 arc = (180 / np.pi) * np.fmod(np.fabs(rad), np.pi) d = int(arc) arc = (arc - d) * 60.0 m = int(arc) s = (arc - m) * 60.0 if sign == -1 and d == 0: return (sign * d, sign * m, sign * s) else: return (sign * d, m, s)
[docs]def dms_to_rad(deg, min_, sec): """Convert (degrees, arcminutes, arcseconds) to radians.""" if deg < 0.0: sign = -1 elif deg == 0.0 and (min_ < 0.0 or sec < 0.0): sign = -1 else: sign = 1 return ( sign * (np.pi / 180 / 60. / 60.) * (60.0 * (60.0 * np.fabs(deg) + np.fabs(min_)) + np.fabs(sec)) )
[docs]def dms_to_deg(deg, min_, sec): """Convert (degrees, arcminutes, arcseconds) to degrees.""" return (180. / np.pi) * dms_to_rad(deg, min_, sec)
[docs]def rad_to_hms(rad): """Convert radians to (hours, minutes, seconds).""" rad = np.fmod(rad, 2 * np.pi) if rad < 0.0: rad = rad + 2 * np.pi arc = (12 / np.pi) * rad h = int(arc) arc = (arc - h) * 60.0 m = int(arc) s = (arc - m) * 60.0 return (h, m, s)
[docs]def hms_to_rad(hour, min_, sec): """Convert (hours, minutes, seconds) to radians.""" sign = -1 if hour < 0 else 1 return ( sign * np.pi / 12 / 60.0 / 60.0 * (60.0 * (60.0 * np.fabs(hour) + np.fabs(min_)) + np.fabs(sec)) )
[docs]def hms_to_hrs(hour, min_, sec): """Convert (hours, minutes, seconds) to hours.""" return (12.0 / np.pi) * hms_to_rad(hour, min_, sec)
[docs]def ra_to_rad(ra_string): """Convert right ascension string to radians.""" h, m, s = ra_string.split(":") return hms_to_rad(int(h), int(m), float(s))
[docs]def dec_to_rad(dec_string): """Convert declination string to radians.""" d, m, s = dec_string.split(":") if "-" in d and int(d) == 0: m, s = "-" + m, "-" + s return dms_to_rad(int(d), int(m), float(s))