#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 27 08:50:44 2020

@author: frohro
"""

import numpy as np
import matplotlib.pyplot as plt
import tkinter as tk
from tkinter import filedialog
import json
import PySAM.Pvwattsv7 as pv
import PySAM.Singleowner as so
import PySAM.PySSC as pssc

root = tk.Tk()  # For filedialogs
root.withdraw()  # No root window

ssc = pssc.PySSC()

try:
    input_file_path = filedialog.askopenfilename(defaultextension='.json',
                filetypes=[('Json file','*.json'), ('All files','*.*'), ])
    print(input_file_path)
except NameError:
    print('NameError; something did not work!')
else:
    with open(input_file_path) as f:
        dic = json.load(f)
        gs_dat = pssc.dict_to_ssc_table(dic, "pvwattsv7")
        so_dat = pssc.dict_to_ssc_table(dic, "singleowner")
        gs = pv.wrap(gs_dat)
    
        so_sys = so.from_existing(gs, 'PVWattsSingleOwner')
        so_sys.assign(so.wrap(so_dat).export())
        
        gs.execute()
        so_sys.execute(1)  # the 1 asks for log messages
        so_sys.export(1)
        
        print('Annual AC output: ', gs.Outputs.ac_annual, ' (Should be 134580.)')
        
        print('NPV: ', so_sys.Outputs.project_return_aftertax_npv)
        print('The npv (above) should be: 113624 according to the SAM GUI.')
        
root = tk.Tk()
root.withdraw()
root.destroy()

