import numpy as np
import pandas as pd
import sys

# Load classes
sys.path.insert(0, dirName) # dirName = location where the files listed here are located
from psychClass import Psych
from heatTransClass import HeatTrans
from heatTransLiqUncClass import HeatTransLiqUnc
from miscUncClass import MiscUnc
from heatTransAirUncClass import HeatTransAirUnc

def ahuCalcs(df1,mData):
    '''Calculations in the AHU - df1 = dataframe containing the data; mData = metaData'''
    # Instantiate instances of the classes
    ht = HeatTrans()
    ps = Psych()
    # Define conversion factors for units
    CFFlow = 0.0004719 # m3/s / cfm
    CFGpm = 0.000063 # m3/s / gpm
    # OAU calculations
    df1['oau_f'] = df1.oau_f_out+df1.ahu1_f_sa+df1.ahu2_f_sa
        # Humidity ratio of the outdoor air
    df1['w_oa'] = ps.omega(np.array(df1.outdoor_rtd),np.array(df1.outdoor_rh),np.array(df1.indoor_p),'IP')
        # Humidity ratio at the outlet of the OAU
    df1['w_oau_out'] = ps.omega(np.array(df1.oau_out_rtd),np.array(df1.oau_rh),np.array(df1.indoor_p),'IP')
        # Temperature being controlled by the OAU - mean of the supply air temperature measurements in the AHUs
    df1['oau_t_cont'] = (df1.ahu1_t_sa+df1.ahu2_t_sa)/2
        # Relative humidity being controlled by the OAU
    df1['oau_rh_cont'] = df1.oau_rh
        # Humidity ratio
    df1['w_oau_cont'] = ps.omega(np.array(df1.oau_t_cont),np.array(df1.oau_rh_cont),np.array(df1.indoor_p),'IP')
        # Setpoint humidity ratio
    df1['w_oau_sp'] = ps.omega(np.array(df1.oau_t_out_sp_f),np.array(df1.oau_rh_out_sp_per),np.array(df1.indoor_p),'IP')
        # Mass flow rate exhausting directly from the OAU to the outside of the building
    df1['mDotOauOut'],_ = ps.mDA(np.array(df1.oau_f_out),np.array(df1.oau_t_out),np.array(df1.w_oau_out),\
                                 np.array(df1.indoor_p),units = 'IP')
        # Control signal for compressor1 in the DX system - combination of the on/off signal and the variable capacity controller
    df1['dx1'] = [0 if ((x<5)|(y<1.4)) else x/5*y for x,y in zip(df1.dx_c1,df1.dx_ds_c) ]
        # Combined control signal for the two compressors in the DX
    df1['dx_c'] = df1.dx1 + df1.dx_c2
        # Pressure across the OAU fan
    df1['oau_dp'] = df1.oau_p_down - df1.oau_p_up
    # Calculate humidity ratios throughout the AHUs
        # AHU inlet
    df1['w_ahu1_in'] = ps.omega(np.array(df1.ahu1_in_rtd),np.array(df1.ahu1_rh_up),np.array(df1.indoor_p),'IP')
    df1['w_ahu2_in'] = ps.omega(np.array(df1.ahu2_in_rtd),np.array(df1.ahu2_rh_up),np.array(df1.indoor_p),'IP')
        # AHU heater
    df1['w_ahu1_heat'] = ps.omega(np.array(df1.ahu1_in_rtd),np.array(df1.ahu1_rh_up),np.array(df1.indoor_p),'IP')
    df1['w_ahu2_heat'] = ps.omega(np.array(df1.ahu2_in_rtd),np.array(df1.ahu2_rh_up),np.array(df1.indoor_p),'IP')
        # AHU cc
    df1['w_ahu1_cc'] = ps.omega(np.array(df1.ahu1_out_rtd),np.array(df1.ahu1_rh_down),np.array(df1.indoor_p),'IP')
    df1['w_ahu2_cc'] = ps.omega(np.array(df1.ahu2_out_rtd),np.array(df1.ahu2_rh_down),np.array(df1.indoor_p),'IP')
        # AHU cc sp
    df1['w_ahu1_cc_sp'] = ps.omega(np.array(df1.ahu1_cc_sp_f),np.array(df1.ahu1_rh_down),np.array(df1.indoor_p),'IP')
    df1['w_ahu2_cc_sp'] = ps.omega(np.array(df1.ahu2_cc_sp_f),np.array(df1.ahu2_rh_down),np.array(df1.indoor_p),'IP')
        # AHU outlet
    df1['w_ahu1_out'] = ps.omega(np.array(df1.ahu1_out_rtd),np.array(df1.ahu1_rh_down),np.array(df1.indoor_p),'IP')
    df1['w_ahu2_out'] = ps.omega(np.array(df1.ahu2_out_rtd),np.array(df1.ahu2_rh_down),np.array(df1.indoor_p),'IP')
        # SA
    df1['w_ahu1_sa'] = ps.omega(np.array(df1.ahu1_tdb_sa),np.array(df1.ahu1_rh_sa),np.array(df1.indoor_p),'IP')
    df1['w_ahu2_sa'] = ps.omega(np.array(df1.oau_out_rtd),np.array(df1.oau_rh),np.array(df1.indoor_p),'IP')
        # RA
    df1['w_ahu1_ra'] = ps.omega(np.array(df1.ahu1_tdb_ra),np.array(df1.ahu1_rh_ra),np.array(df1.indoor_p),'IP')
    df1['w_ahu2_ra'] = ps.omega(np.array(df1.ahu2_tdb_ra),np.array(df1.ahu2_rh_ra),np.array(df1.indoor_p),'IP')
   
    # Calculate heat transfers
    df1['q_ahu1'],df1['mDotAhu1'],df1['h_ahu1_in'],df1['h_ahu1_out'],df1['nu_ahu1'] =\
            ht.qAHUAirTotal(df1.ahu1_in_rtd,df1.ahu1_out_rtd,df1.ahu1_f_sa,df1.ahu1_f_ra, \
                         df1.w_ahu1_in,df1.w_ahu1_out,df1.w_ahu1_sa,df1.ahu1_t_sa,df1.w_ahu1_ra,\
                         df1.ahu1_t_ra,df1.indoor_p,units = 'IP') # kW, kg/s, kJ/kg, kJ/kg
    
    df1['q_ahu2'],df1['mDotAhu2'],df1['h_ahu2_in'],df1['h_ahu2_out'],df1['nu_ahu2'] = \
            ht.qAHUAirTotal(df1.ahu2_in_rtd,df1.ahu2_out_rtd,df1.ahu2_f_sa,df1.ahu2_f_ra,
                         df1.w_ahu2_in,df1.w_ahu2_out,df1.w_ahu2_sa,df1.ahu2_t_sa,
                         df1.w_ahu2_ra,df1.ahu2_t_ra,df1.indoor_p,units = 'IP') # kW, kg/s, kJ/kg, kJ/kg  
    df1['q_ahu1_heat'],_,_,df1['h_ahu1_heat'],df1['nu_ahu1_heat'] = \
            ht.qAHUAirTotal(df1.ahu1_in_rtd,df1.ahu1_heat_rtd,df1.ahu1_f_sa,df1.ahu1_f_ra,
                                 df1.w_ahu1_in,df1.w_ahu1_heat,df1.w_ahu1_sa,df1.ahu1_t_sa,
                                 df1.w_ahu1_ra,df1.ahu1_t_ra,df1.indoor_p,units = 'IP') # kW, kg/s, kJ/kg, kJ/kg   
    df1['q_ahu2_heat'],_,_,df1['h_ahu2_heat'],df1['nu_ahu2_heat'] = \
            ht.qAHUAirTotal(df1.ahu2_in_rtd,df1.ahu2_heat_rtd,df1.ahu2_f_sa,df1.ahu2_f_ra,
                                 df1.w_ahu2_in,df1.w_ahu2_heat,df1.w_ahu2_sa,df1.ahu2_t_sa,
                                 df1.w_ahu2_ra,df1.ahu2_t_ra,df1.indoor_p,units = 'IP') # kW, kg/s, kJ/kg, kJ/kg   
    df1['q_ahu1_cc'],_,_,df1['h_ahu1_cc'],df1['nu_ahu1_cc'] = \
            ht.qAHUAirTotal(df1.ahu1_heat_rtd,df1.ahu1_cc_rtd,df1.ahu1_f_sa,df1.ahu1_f_ra,
                                 df1.w_ahu1_heat,df1.w_ahu1_cc,df1.w_ahu1_sa,df1.ahu1_t_sa,
                                 df1.w_ahu1_ra,df1.ahu1_t_ra,df1.indoor_p,units = 'IP') # kW, kg/s, kJ/kg, kJ/kg    
    df1['q_ahu1_cc_sp'],_,_,df1['h_ahu1_cc_sp'],df1['nu_ahu1_cc_sp'] = \
            ht.qAHUAirTotal(df1.ahu1_cc_rtd,df1.ahu1_cc_sp_f,df1.ahu1_f_sa,df1.ahu1_f_ra,
                                 df1.w_ahu1_cc,df1.w_ahu1_cc_sp,df1.w_ahu1_sa,df1.ahu1_t_sa,
                                 df1.w_ahu1_ra,df1.ahu1_t_ra,df1.indoor_p,units = 'IP') # kW, kg/s, kJ/kg, kJ/kg 
      
    df1['q_ahu2_cc'],_,_,df1['h_ahu2_cc'],df1['nu_ahu2_cc']  = \
            ht.qAHUAirTotal(df1.ahu2_heat_rtd,df1.ahu2_cc_rtd,df1.ahu2_f_sa,df1.ahu2_f_ra,
                                 df1.w_ahu2_heat,df1.w_ahu2_cc,df1.w_ahu2_sa,df1.ahu2_t_sa,
                                 df1.w_ahu2_ra,df1.ahu2_t_ra,df1.indoor_p,units = 'IP') # kW, kg/s, kJ/kg, kJ/kg 
    
    df1['q_ahu2_cc_sp'],_,_,df1['h_ahu2_cc_sp'],df1['nu_ahu2_cc_sp']  = \
            ht.qAHUAirTotal(df1.ahu2_cc_rtd,df1.ahu2_cc_sp_f,df1.ahu2_f_sa,df1.ahu2_f_ra,
                                 df1.w_ahu2_cc,df1.w_ahu2_cc_sp,df1.w_ahu2_sa,df1.ahu2_t_sa,
                                 df1.w_ahu2_ra,df1.ahu2_t_ra,df1.indoor_p,units = 'IP') # kW, kg/s, kJ/kg, kJ/kg
    
    df1['q_ahu1_fan'],_,_,df1['h_ahu1_fan'],df1['nu_ahu1_fan']  = \
            ht.qAHUAirTotal(df1.ahu1_cc_rtd,df1.ahu1_out_rtd,df1.ahu1_f_sa,df1.ahu1_f_ra,
                                 df1.w_ahu1_cc,df1.w_ahu1_out,df1.w_ahu1_sa,df1.ahu1_t_sa,
                                 df1.w_ahu1_ra,df1.ahu1_t_ra,df1.indoor_p,units = 'IP') # kW, kg/s, kJ/kg, kJ/kg           
    df1['q_ahu2_fan'],_,_,df1['h_ahu2_fan'],df1['nu_ahu2_fan'] = \
            ht.qAHUAirTotal(df1.ahu2_cc_rtd,df1.ahu2_out_rtd,df1.ahu2_f_sa,df1.ahu2_f_ra,
                                 df1.w_ahu2_cc,df1.w_ahu2_out,df1.w_ahu2_sa,df1.ahu2_t_sa,
                                 df1.w_ahu2_ra,df1.ahu2_t_ra,df1.indoor_p,units = 'IP') # kW, kg/s, kJ/kg, kJ/kg   
    # Calculate heat transfer on the hydronic side of the the cooling coils
    df1['q_ahu1_cc_pg'],df1['rho_ahu1_cc_pg'],df1['cp_ahu1_cc_pg'],df1['mDotCC1'] = \
            ht.heatTransLiq(df1.pump3_out_rtd,df1.ahu1_cc_out_rtd,df1.ahu1_f_cc,'IP','pg',0.3)   
    df1['q_ahu2_cc_pg'],df1['rho_ahu2_cc_pg'],df1['cp_ahu2_cc_pg'],df1['mDotCC2'] = \
            ht.heatTransLiq(df1.pump3_out_rtd,df1.ahu2_cc_out_rtd,df1.ahu2_f_cc,'IP','pg',0.3)   
    
    # Flow rate calculations
    df1['mDotAhu'] = df1.mDotAhu1 + df1.mDotAhu2
    df1['mDotOa'] = df1.mDotOauOut + df1.mDotAhu
    df1['ahu_f_sa'] = df1.ahu1_f_sa + df1.ahu2_f_sa
    df1['ahu_f_ra'] = df1.ahu1_f_ra + df1.ahu2_f_ra    
    df1['mDotAhu1SA'] = [ps.mDA(a,b,c,d,units = 'IP')[0] for a,b,c,d in zip(df1.ahu1_f_sa,df1.ahu1_t_sa,df1.w_ahu1_sa,df1.indoor_p)]
    df1['mDotAhu2SA'] = [ps.mDA(a,b,c,d,units = 'IP')[0] for a,b,c,d in zip(df1.ahu2_f_sa,df1.ahu2_t_sa,df1.w_ahu2_sa,df1.indoor_p)]
    df1['mDotAhu1RA'] = [ps.mDA(a,b,c,d,units = 'IP')[0] for a,b,c,d in zip(df1.ahu1_f_ra,df1.ahu1_t_ra,df1.w_ahu1_ra,df1.indoor_p)]
    df1['mDotAhu2RA'] = [ps.mDA(a,b,c,d,units = 'IP')[0] for a,b,c,d in zip(df1.ahu2_f_ra,df1.ahu2_t_ra,df1.w_ahu2_ra,df1.indoor_p)]
    
    # Calculate uncertainties    
    uQ = HeatTransLiqUnc()    
    df1 = uQ.uTot(df1,mData,'ahu1_cc_out_rtd','pump3_out_rtd','ahu1_f_cc','q_ahu1_cc_pg',fluid = 'PT',
                  conc = 0.3,unitsIn = 'IP',unitsOut = 'SI')
    df1 = uQ.uTot(df1,mData,'ahu2_cc_out_rtd','pump3_out_rtd','ahu2_f_cc','q_ahu2_cc_pg',fluid = 'PG',
                  conc = 0.3,unitsIn = 'IP',unitsOut = 'SI')
   
    uQAir = HeatTransAirUnc()
    # Calculate uncertainties (neglects cross terms)
    df1 = uQAir.uQ('w_ahu1_in','w_ahu1_out','ahu1_in_rtd','ahu1_out_rtd','h_ahu1_in','h_ahu1_out','indoor_p',\
             ['ahu1_f_sa','ahu1_f_ra'],'mDotAhu1','nu_ahu1',df1,mData,'q_ahu1',[],[],[],[],units = 'IP',cross = 0, k = 0)
    df1 = uQAir.uQ('w_ahu1_in','w_ahu1_heat','ahu1_in_rtd','ahu1_heat_rtd','h_ahu1_in','h_ahu1_heat','indoor_p',\
             ['ahu1_f_sa','ahu1_f_ra'],'mDotAhu1','nu_ahu1_heat',df1,mData,'q_ahu1_heat',[],[],[],[],units = 'IP',cross = 0, k = 0)
    df1 = uQAir.uQ('w_ahu1_heat','w_ahu1_cc','ahu1_heat_rtd','ahu1_cc_rtd','h_ahu1_heat','h_ahu1_cc','indoor_p',\
             ['ahu1_f_sa','ahu1_f_ra'],'mDotAhu1','nu_ahu1_cc',df1,mData,'q_ahu1_cc',[],[],[],[],units = 'IP',cross = 0, k = 0)    
    df1 = uQAir.uQ('w_ahu1_cc','w_ahu1_out','ahu1_cc_rtd','ahu1_out_rtd','h_ahu1_cc','h_ahu1_fan','indoor_p',\
             ['ahu1_f_sa','ahu1_f_ra'],'mDotAhu1','nu_ahu1_fan',df1,mData,'q_ahu1_fan',[],[],[],[],units = 'IP',cross = 0, k = 0)    
    df1 = uQAir.uQ('w_ahu2_in','w_ahu2_out','ahu2_in_rtd','ahu2_out_rtd','h_ahu2_in','h_ahu2_out','indoor_p',\
             ['ahu2_f_sa','ahu2_f_ra'],'mDotAhu2','nu_ahu2',df1,mData,'q_ahu2',[],[],[],[],units = 'IP',cross = 0, k = 0)
    df1 = uQAir.uQ('w_ahu2_in','w_ahu2_heat','ahu2_in_rtd','ahu2_heat_rtd','h_ahu2_in','h_ahu2_heat','indoor_p',\
             ['ahu2_f_sa','ahu2_f_ra'],'mDotAhu1','nu_ahu2_heat',df1,mData,'q_ahu2_heat',[],[],[],[],units = 'IP',cross = 0, k = 0)
    df1 = uQAir.uQ('w_ahu2_heat','w_ahu2_cc','ahu2_heat_rtd','ahu2_cc_rtd','h_ahu2_heat','h_ahu2_cc','indoor_p',\
             ['ahu2_f_sa','ahu2_f_ra'],'mDotAhu2','nu_ahu2_cc',df1,mData,'q_ahu2_cc',[],[],[],[],units = 'IP',cross = 0, k = 0)    
    df1 = uQAir.uQ('w_ahu2_cc','w_ahu2_out','ahu2_cc_rtd','ahu2_out_rtd','h_ahu2_cc','h_ahu2_fan','indoor_p',\
             ['ahu2_f_sa','ahu2_f_ra'],'mDotAhu2','nu_ahu2_fan',df1,mData,'q_ahu2_fan',[],[],[],[],units = 'IP',cross = 0, k = 0)  
    
    df1 = uQAir.uQsens('w_ahu1_in','w_ahu1_out','ahu1_in_rtd','ahu1_out_rtd','indoor_p',\
             ['ahu1_f_sa','ahu1_f_ra'],'mDotAhu1','nu_ahu1',df1,mData,'q_ahu1',[],[],[],[],units = 'IP',cross = 0, k = 0)
    df1 = uQAir.uQsens('w_ahu1_in','w_ahu1_heat','ahu1_in_rtd','ahu1_heat_rtd','indoor_p',\
             ['ahu1_f_sa','ahu1_f_ra'],'mDotAhu1','nu_ahu1_heat',df1,mData,'q_ahu1_heat',[],[],[],[],units = 'IP',cross = 0, k = 0)
    df1 = uQAir.uQsens('w_ahu1_heat','w_ahu1_cc','ahu1_heat_rtd','ahu1_cc_rtd','indoor_p',\
             ['ahu1_f_sa','ahu1_f_ra'],'mDotAhu1','nu_ahu1_cc',df1,mData,'q_ahu1_cc',[],[],[],[],units = 'IP',cross = 0, k = 0)    
    df1 = uQAir.uQsens('w_ahu1_cc','w_ahu1_out','ahu1_cc_rtd','ahu1_out_rtd','indoor_p',\
             ['ahu1_f_sa','ahu1_f_ra'],'mDotAhu1','nu_ahu1_fan',df1,mData,'q_ahu1_fan',[],[],[],[],units = 'IP',cross = 0, k = 0)    
    df1 = uQAir.uQsens('w_ahu2_in','w_ahu2_out','ahu2_in_rtd','ahu2_out_rtd','indoor_p',\
             ['ahu2_f_sa','ahu2_f_ra'],'mDotAhu2','nu_ahu2',df1,mData,'q_ahu2',[],[],[],[],units = 'IP',cross = 0, k = 0)
    df1 = uQAir.uQsens('w_ahu2_in','w_ahu2_heat','ahu2_in_rtd','ahu2_heat_rtd','indoor_p',\
             ['ahu2_f_sa','ahu2_f_ra'],'mDotAhu1','nu_ahu2_heat',df1,mData,'q_ahu2_heat',[],[],[],[],units = 'IP',cross = 0, k = 0)
    df1 = uQAir.uQsens('w_ahu2_heat','w_ahu2_cc','ahu2_heat_rtd','ahu2_cc_rtd','indoor_p',\
             ['ahu2_f_sa','ahu2_f_ra'],'mDotAhu2','nu_ahu2_cc',df1,mData,'q_ahu2_cc',[],[],[],[],units = 'IP',cross = 0, k = 0)    
    df1 = uQAir.uQsens('w_ahu2_cc','w_ahu2_out','ahu2_cc_rtd','ahu2_out_rtd','indoor_p',\
             ['ahu2_f_sa','ahu2_f_ra'],'mDotAhu2','nu_ahu2_fan',df1,mData,'q_ahu2_fan',[],[],[],[],units = 'IP',cross = 0, k = 0) 
    
    df1 = uQAir.uQlat('w_ahu1_in','w_ahu1_out','ahu1_in_rtd','ahu1_out_rtd','indoor_p',\
             ['ahu1_f_sa','ahu1_f_ra'],'mDotAhu1','nu_ahu1',df1,mData,'q_ahu1',[],[],[],[],units = 'IP',cross = 0, k = 0)
    df1 = uQAir.uQlat('w_ahu1_in','w_ahu1_heat','ahu1_in_rtd','ahu1_heat_rtd','indoor_p',\
             ['ahu1_f_sa','ahu1_f_ra'],'mDotAhu1','nu_ahu1_heat',df1,mData,'q_ahu1_heat',[],[],[],[],units = 'IP',cross = 0, k = 0)
    df1 = uQAir.uQlat('w_ahu1_heat','w_ahu1_cc','ahu1_heat_rtd','ahu1_cc_rtd','indoor_p',\
             ['ahu1_f_sa','ahu1_f_ra'],'mDotAhu1','nu_ahu1_cc',df1,mData,'q_ahu1_cc',[],[],[],[],units = 'IP',cross = 0, k = 0)    
    df1 = uQAir.uQlat('w_ahu1_cc','w_ahu1_out','ahu1_cc_rtd','ahu1_out_rtd','indoor_p',\
             ['ahu1_f_sa','ahu1_f_ra'],'mDotAhu1','nu_ahu1_fan',df1,mData,'q_ahu1_fan',[],[],[],[],units = 'IP',cross = 0, k = 0)    
    df1 = uQAir.uQlat('w_ahu2_in','w_ahu2_out','ahu2_in_rtd','ahu2_out_rtd','indoor_p',\
             ['ahu2_f_sa','ahu2_f_ra'],'mDotAhu2','nu_ahu2',df1,mData,'q_ahu2',[],[],[],[],units = 'IP',cross = 0, k = 0)
    df1 = uQAir.uQlat('w_ahu2_in','w_ahu2_heat','ahu2_in_rtd','ahu2_heat_rtd','indoor_p',\
             ['ahu2_f_sa','ahu2_f_ra'],'mDotAhu1','nu_ahu2_heat',df1,mData,'q_ahu2_heat',[],[],[],[],units = 'IP',cross = 0, k = 0)
    df1 = uQAir.uQlat('w_ahu2_heat','w_ahu2_cc','ahu2_heat_rtd','ahu2_cc_rtd','indoor_p',\
             ['ahu2_f_sa','ahu2_f_ra'],'mDotAhu2','nu_ahu2_cc',df1,mData,'q_ahu2_cc',[],[],[],[],units = 'IP',cross = 0, k = 0)    
    df1 = uQAir.uQlat('w_ahu2_cc','w_ahu2_out','ahu2_cc_rtd','ahu2_out_rtd','indoor_p',\
             ['ahu2_f_sa','ahu2_f_ra'],'mDotAhu2','nu_ahu2_fan',df1,mData,'q_ahu2_fan',[],[],[],[],units = 'IP',cross = 0, k = 0)
    
    # Caclulate uncertainties of the power meters
    uM = MiscUnc()
    powerMeas = ['ahu1_heat_power','ahu2_heat_power','ahu1_fan_power','ahu2_fan_power']
    for pn in powerMeas:
        df1 = uM.uPower(df1,mData,pn)
        
    return df1