import PySAM.TcstroughEmpirical as troughEmp
import PySAM.Singleowner as so
import matplotlib.pyplot as plt
%matplotlib qt

system_model = troughEmp.default('EmpiricalTroughSingleOwner')
financial_model = so.from_existing(system_model, 'EmpiricalTroughSingleOwner')

filename = 'blythe_california_33.610437_-114.596827_psm3_60_2007.csv'
system_model.Weather.file_name = filename

# Trough
system_model.Trough.system_capacity=250*1000

# Solar field
system_model.Solarfield.Solar_Field_Area=1.92832e+06
system_model.Solarfield.DepAngle=10
system_model.Solarfield.Distance_SCA=1
system_model.Solarfield.NumScas=4
system_model.Solarfield.Stow_Angle=170
system_model.Solarfield.MinHtfTemp=50
system_model.Solarfield.SFTempInit=100
system_model.Solarfield.SfInTempD=293
system_model.Solarfield.SfOutTempD=391
system_model.Solarfield.HTFFluid=21
system_model.Solarfield.HtfGalArea=0.614
#print('Solar Field:',system_model.Solarfield.export(),'\n')

# SCA
#print('SCA:',system_model.Sca.export(),'\n')
    # Values look good here

# HCE
#print('HCE:',system_model.Hce.export(),'\n')
    # Values look good here
system_model.Hce.HCE_A3=(5.65e-6,1.13e-5,1.85e-5)
system_model.Hce.HCE_A4=(7.62e-8,1.73e-7,6.89e-7)

# Power Block
#print('Power block:',system_model.Pwrb.export(),'\n')
    # Changed TurbOutG to 250
system_model.Pwrb.TurbOutG=250 #MW
    
# TES
#print('TES:',system_model.Tes.export())
    # Changed TSHOURS to 0
system_model.Tes.TSHOURS=0

# Parasitics
#print('Parasitics: ',system_model.Parasitic.export(),'\n')
    # Looks different, check
system_model.Parasitic.BOPPar=6.1675
system_model.Parasitic.ChtfPar=20.2859 #Parasitic power solar field HTF pump [MWe]
#system_model.Parasitic.HhtfPar=20.2859 #Parasitic power TES and Cycle HTF pump [MWe]
#system_model.Parasitic.HtrPar #Parasitic power auxiliary heater operation [MWe]
system_model.Parasitic.CtOpF=1 #0 for load-dependent CT, 1 for 50% or 100% operation
system_model.Parasitic.CtPar=4.26125 # Cooling Tower/condenser
system_model.Parasitic.AntiFrPar=2.02859
system_model.Parasitic.PbFixPar=1.375
system_model.Parasitic.SfPar=0.512933 #SCA/Parasitic power field collector drives

# Financials
#print('System Costs: ',financial_model.SystemCosts.export(),'\n')
# changed installed costs, add calculation later
financial_model.SystemCosts.total_installed_cost=885236992

#print('Financial Parameters: ',financial_model.FinancialParameters.export(),'\n')
# changed construction financing, add calculation later
financial_model.FinancialParameters.construction_financing_cost=44261848
financial_model.FinancialParameters.debt_option=0

#print('Tax Credits: ',financial_model.TaxCreditIncentives.export(),'\n')
financial_model.TaxCreditIncentives.itc_fed_percent=0
financial_model.TaxCreditIncentives.itc_sta_percent=0
financial_model.TaxCreditIncentives.ptc_fed_term=0
financial_model.TaxCreditIncentives.ptc_sta_term=0

# Run
system_model.execute()
financial_model.execute()

print('Generation kWh: ',system_model.Outputs.annual_energy,'\n')

# Emonthly=system_model.Outputs.monthly_energy
# plt.figure(1)
# plt.plot(Emonthly)
# plt.show()
# print(Emonthly)

# plt.figure(2)
# Epar=system_model.Outputs.EparCHTF
# plt.plot(Epar)
# plt.show()

plt.figure(3)
Epar=system_model.Outputs.Qtpb
plt.plot(Epar)
plt.show()

plt.figure(4)
Epar=system_model.Outputs.Egr
plt.plot(Epar)
plt.show()

# create list of strings
my_dict=system_model.export()
list_of_strings = [ f'{key} : {my_dict[key]}' for key in my_dict ]
with open('system_model.txt', 'w') as my_file:
    [ my_file.write(f'{st}\n\n') for st in list_of_strings ]

my_dict=financial_model.export()
list_of_strings = [ f'{key} : {my_dict[key]}' for key in my_dict ]
with open('financial_model.txt', 'w') as my_file:
    [ my_file.write(f'{st}\n\n') for st in list_of_strings ]

print('LCOE nominal: ',financial_model.Outputs.lcoe_nom)
print('LCOE real: ',financial_model.Outputs.lcoe_real)