# This program calculates T_N/J, carrying errors through correctly
from uncertainties import ufloat

mu_B       = ufloat((9.27400915e-24,0.000023e-26)) # http://physics.nist.gov/cgi-bin/cuu/Value?mub
k_B        = ufloat((1.3806504e-23,0.0000024e-23)) # http://physics.nist.gov/cgi-bin/cuu/Value?k

input_filenames  = ['beta-vs-JJ-J-in.tsv','beta-vs-JJ-in.tsv']
output_filename = 'TN-over-J.tsv'

import csc
from uncertainties import unumpy as unp
#import numpy as np

title,input,error = csc.read(input_filenames[0])

output = csc.begin({'title':'beta vs J\'/J'},['arb','TN_J','TN_J_err','name'],output_filename)

for compound in input:
    J    = ufloat((float(compound['J']), float(compound['J_err']) ))
    TN   = ufloat((float(compound['TN']),float(compound['TN_err'])))
    
    # work out the TN/J value
    TN_over_J = TN/J

    # modify the name
    name = '"'+compound['name']+'"' # add a # at the start to comment it out for GLE
    
    print TN_over_J
    # [()]s are required because unp spits out 0d arrays at the moment...bah
    csc.append(['1',TN_over_J.nominal_value,TN_over_J.std_dev(),name],output)

title,input,error = csc.read(input_filenames[1])

for compound in input:
    g    = ufloat((float(compound['g']), float(compound['g_err']) ))
    Bc   = ufloat((float(compound['Bc']),float(compound['Bc_err'])))
    TN   = ufloat((float(compound['TN']),float(compound['TN_err'])))
    
    # work out the J'/J value
    J         = (mu_B*g*Bc)/(4*k_B)
    TN_over_J = TN/J
    
    # modify the name
    name = '"'+compound['name']+'"' # add a # at the start to comment it out for GLE
    
    # [()]s are required because unp spits out 0d arrays at the moment...bah
    csc.append(['1',TN_over_J.nominal_value,TN_over_J.std_dev(),name],output)

csc.close(output)