import os
import PySAM.ResourceTools as tools  # MOVE BACK TO FILES FOLDER
import PySAM.Windpower as wp
import PySAM.Singleowner as so


import pandas as pd

#import geocoder
os.chdir('C:/Users/jrahman.epri/Desktop/Jubeyer_Tmp/WINDTOOLKIT_API')

# replace with key and email address from https://developer.nrel.gov/signup/
sam_api_key = 'xxxxxxxxxxxx'
sam_email = 'x@ex.com'
# geocode_api_key = ''  # optional, may be required for geocoder

# # --- Location Coordinates ---

# # Set geocode to True to use geocoding service API
# geocode = False
# if geocode:
#     # See https://geocoder.readthedocs.io/ for different geocoding services
#     g = geocoder.bing('golden, co', key=geocode_api_key)
#     lon = g.latlng[1]
#     lat = g.latlng[0]
# else:
lon = -105.22
lat = 39.75

# --- Wind Example ---

# --- Initialize Wind Resource Fetcher using minimum parameters---
# See function documentation for full parameter list
wtkfetcher = tools.FetchResourceFiles(
                tech='wind',
                nrel_api_key=sam_api_key,
                nrel_api_email=sam_email,resource_year='2009',resource_interval_min=5,resource_height=80,resource_type="")

# --- List of (lon, lat) tuples or Shapely points ---
lon_lats = [(lon, lat)]
wtkfetcher.fetch(lon_lats)

# --- Get resource data file path ---
wtk_path_dict = wtkfetcher.resource_file_paths_dict
wtk_fp = wtk_path_dict[lon_lats[0]]

Temp_File=tools.SRW_to_wind_data(wtk_fp)
#read_file = pd.read_csv (wtk_fp)
#read_file.to_csv (r'Path where the CSV will be saved\File name.csv', index=None)
# --- Initialize generator ---
if wtk_fp is not None:
    generator = wp.default('WindPowerSingleOwner')
    generator.Resource.assign({'wind_resource_model_choice': 0})
    generator.Resource.assign({'wind_resource_filename': wtk_fp})

    # --- Initialize financial model ---
    financial = so.from_existing(generator, 'WindPowerSingleOwner')

    print('Wind Power - Single Owner Results')
    generator.execute()
    #turbine_output=generator.Outputs.turbine_output_by_windspeed_bin
    wake_loss=generator.Outputs.wake_losses
    power_output=generator.Outputs.gen
    print(generator.Outputs.gen)
    print('capacity factor = {:.3f}'.format(generator.Outputs.capacity_factor))
    financial.execute()
    print('npv = ${:,.2f}'.format(financial.Outputs.project_return_aftertax_npv))

else:
    print('Wind resource file does not exist. Skipping wind model simulation.')
    
    
    
# Under process
# get all years of weather data
