#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""Calculate noise temperature from Y-factor."""

import argparse

import matplotlib.pyplot as plt
import numpy as np
import scipy.constants as sc

from rftools.noise import calculate_tn, temp_cw
from rftools.util import pvalf, header

# Grab arguments
parser = argparse.ArgumentParser()
parser.add_argument('yfac', nargs=1,  help="Y-factor, can express as fraction, e.g., 4.2/1.8", type=str)
parser.add_argument('-f',  '--freq',  help="frequency in GHz, will then use Callen-Welton equations", default=None, type=float)
parser.add_argument('-th', '--thot',  help="hot load ambient temperature (default 293K)", default=293., type=float)
parser.add_argument('-tc', '--tcold', help="cold load ambient temperature (default 77K)", default=77., type=float)
args = parser.parse_args()

# Unpack
yfac = eval(args.yfac[0])
thot = args.thot
tcold = args.tcold

# Print to terminal
print("")
header('Noise temperature from Y-factor')

# If frequency is defined -> use Callen-Welton equations
print("")
print("\tPhysical temperature of black body loads:")
pvalf("Hot load", thot, "K")
pvalf("Cold load", tcold, "K")
if args.freq is not None:
    thot = temp_cw(args.freq * 1e9, thot)
    tcold = temp_cw(args.freq * 1e9, tcold)
    print("")
    print("\tEquiv. temp. from CW equations (with f={:.1f} GHz):".format(args.freq))
    pvalf("Hot load", thot, "K")
    pvalf("Cold load", tcold, "K")

# Calculate noise temperature
tn = calculate_tn(yfac, thot=thot, tcold=tcold)

# Print to terminal
print("")
pvalf("Y-factor", yfac)
print("")
pvalf("Noise temperature", tn, "K")
print("")
