import pandas as pd
import numpy as np
import sys, os
from IPython.display import display

# specify hub_height
hub_height='100m'

lat, lon, year = 33.2164, -97.1292, 2010
  # You must request an NSRDB api key from the link above
api_key = 'xxxxxxxxxxxxxxxxxx'
# Set the attributes to extract (e.g.,windspeed, winddirection, etc.), separated by commas.
attributes = 'pressure_100m,windspeed_100m,winddirection_100m,temperature_100m'
# Choose year of data
year = '2010'
# Set leap year to true or false. True will return leap day data if present, false will not.
leap_year = 'false'
# Set time interval in minutes, i.e., '30' is half hour intervals. Valid intervals are 5, 15, 30, 45 & 60.
interval = '5'
# Specify Coordinated Universal Time (UTC), 'true' will use UTC, 'false' will use the local time zone of the data.
# NOTE: In order to use the WindToolkit data in SAM, you must specify UTC as 'false'. SAM requires the data to be in the
# local time zone.
utc = 'false'
# Your full name, use '+' instead of spaces.
your_name = 'x+x'
# Your reason for using the NSRDB.
reason_for_use = 'example'
# Your affiliation
your_affiliation = 'NREL'
# Your email address
your_email = 'x@x.com'
# Please join our mailing list so we can keep you up-to-date on new developments.
mailing_list = 'true'

# Declare url string
url = 'https://developer.nrel.gov/api/wind-toolkit/v2/wind/wtk-download.csv?wkt=POINT({lon}%20{lat})&names={year}&leap_day={leap}&interval={interval}&utc={utc}&full_name={name}&email={email}&affiliation={affiliation}&mailing_list={mailing_list}&reason={reason}&api_key={api}&attributes={attr}'.format(
        year=year, lat=lat, lon=lon, leap=leap_year, interval=interval, utc=utc, name=your_name, email=your_email,
        mailing_list=mailing_list, affiliation=your_affiliation, reason=reason_for_use, api=api_key, attr=attributes)
# Return just the first 1 line to get metadata:
info = pd.read_csv(url, nrows=1)
# See metadata for specified properties, e.g., timezone 
timezone= info['Site Timezone']

# View metadata
info

df = pd.read_csv(
        'https://developer.nrel.gov/api/wind-toolkit/v2/wind/wtk-download.csv?wkt=POINT({lon}%20{lat})&names={year}&leap_day={leap}&interval={interval}&utc={utc}&full_name={name}&email={email}&affiliation={affiliation}&mailing_list={mailing_list}&reason={reason}&api_key={api}&attributes={attr}'.format(
            year=year, lat=lat, lon=lon, leap=leap_year, interval=interval, utc=utc, name=your_name, email=your_email,
            mailing_list=mailing_list, affiliation=your_affiliation, reason=reason_for_use, api=api_key,
            attr=attributes), skiprows=1,low_memory=False)

# Set the time index in the pandas dataframe:
#df = df.set_index(pd.date_range('1/1/{yr}'.format(yr=year), freq=interval+'Min', periods=525600/int(interval)))

# take a look
print ('shape:',df.shape)
df.head()

df.rename(columns={'air pressure at 100m (Pa)': 'air pressure at 100m (atm)'}, inplace=True)
print (df.columns.values)
df['air pressure at 100m (atm)']= df['air pressure at 100m (atm)']*9.86923*10**(-6) 

#import additional module for SAM simulation:
import site
# Use site.addsitedir() to set the path to the SAM SDK API. Set path to the python directory.
site.addsitedir('/Applications/sam-sdk-2015-6-30-r3/languages/python/')
import sscapi
ssc = sscapi.PySSC()

# Resource inputs for SAM model:
wfd = ssc.data_create()
ssc.data_set_number(wfd, 'lat', lat)
ssc.data_set_number(wfd, 'lon', lon)
#ssc.data_set_number(wfd, 'tz', timezone)
#ssc.data_set_number(wfd, 'elev', elevation)
ssc.data_set_array(wfd, 'year', df["year"])
ssc.data_set_array(wfd, 'month', df["month"])
ssc.data_set_array(wfd, 'day', df["day"])
ssc.data_set_array(wfd, 'hour', df["hour"])
ssc.data_set_array(wfd, 'minute', df["minute"])

# This section is for trial purpose only
wind_turbine_powercurve_windspeeds = [ 0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 2.25, 2.5, 2.75, 3, 3.25, 3.5, 3.75, 4, 4.25, 4.5, 4.75, 5, 5.25, 5.5, 5.75, 6, 6.25, 6.5, 6.75, 7, 7.25, 7.5, 7.75, 8, 8.25, 8.5, 8.75, 9, 9.25, 9.5, 9.75, 10, 10.25, 10.5, 10.75, 11, 11.25, 11.5, 11.75, 12, 12.25, 12.5, 12.75, 13, 13.25, 13.5, 13.75, 14, 14.25, 14.5, 14.75, 15, 15.25, 15.5, 15.75, 16, 16.25, 16.5, 16.75, 17, 17.25, 17.5, 17.75, 18, 18.25, 18.5, 18.75, 19, 19.25, 19.5, 19.75, 20, 20.25, 20.5, 20.75, 21, 21.25, 21.5, 21.75, 22, 22.25, 22.5, 22.75, 23, 23.25, 23.5, 23.75, 24, 24.25, 24.5, 24.75, 25, 25.25, 25.5, 25.75, 26, 26.25, 26.5, 26.75, 27, 27.25, 27.5, 27.75, 28, 28.25, 28.5, 28.75, 29, 29.25, 29.5, 29.75, 30, 30.25, 30.5, 30.75, 31, 31.25, 31.5, 31.75, 32, 32.25, 32.5, 32.75, 33, 33.25, 33.5, 33.75, 34, 34.25, 34.5, 34.75, 35, 35.25, 35.5, 35.75, 36, 36.25, 36.5, 36.75, 37, 37.25, 37.5, 37.75, 38, 38.25, 38.5, 38.75, 39, 39.25, 39.5, 39.75, 40 ]
wind_turbine_powercurve_powerout = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21.32, 33.51, 45.69, 65.21, 79.83, 104.25, 128.66, 157.97, 187.27, 216.58, 250.78, 292.32, 333.85, 375.4, 426.72, 475.6, 534.27, 597.81, 656.49, 724.94, 798.29, 871.63, 940.08, 1010, 1060, 1130, 1190, 1240, 1290, 1330, 1370, 1390, 1410, 1430, 1440, 1460, 1470, 1475, 1480, 1485, 1490, 1495, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]

ssc.data_set_array(wfd,'wind_turbine_powercurve_windspeeds',wind_turbine_powercurve_windspeeds)

ssc.data_set_array(wfd,'wind_turbine_powercurve_powerout',wind_turbine_powercurve_powerout)

#####
ssc.data_set_array(wfd, 'temperature', df['air temperature at 100m (C)'])
ssc.data_set_array(wfd, 'pressure', df['air pressure at 100m (Pa)'])
ssc.data_set_array(wfd, 'direction', df['wind direction at 100m (deg)'])
ssc.data_set_array(wfd, 'speed', df['wind speed at 100m (m/s)'])


# Create SAM compliant object  
dat = ssc.data_create()
ssc.data_set_table(dat, 'wind_resource_data', wfd)
ssc.data_free(wfd)

# Specify the system Configuration
wind_resource_model_choice=0
ssc.data_set_number(dat, 'wind_resource_model_choice', wind_resource_model_choice)
# Set system capacity in MW
system_capacity = 2 # will change for different sites
ssc.data_set_number(dat, 'system_capacity', system_capacity)
# Setting the hub height in meter of wind turbine
wind_turbine_hub_ht=100
ssc.data_set_number(dat, 'wind_turbine_hub_ht', wind_turbine_hub_ht)
# Set rotor diameter in meter
wind_turbine_rotor_diameter=95
ssc.data_set_number(dat, 'wind_turbine_rotor_diameter',wind_turbine_rotor_diameter)
# Set the shear
wind_resource_shear=0.14
ssc.data_set_number(dat, 'wind_resource_shear', wind_resource_shear)


# execute and put generation results back into dataframe
mod = ssc.module_create('Windpower')
ssc.module_exec(mod, dat)
df['generation'] = np.array(ssc.data_get_array(dat, 'gen'))

# free the memory
ssc.data_free(dat)
ssc.module_free(mod)
  