# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: test_this
#     language: python
#     name: test_this
# ---

# ### Importing atmospheric data from NREL's WindToolKit data set using ssrs.WTK module

# %load_ext autoreload
# %autoreload 2

import os
import sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
from scipy.interpolate import griddata

# importing ssrs funationality used in this notebook
from ssrs import WTK, WtkSource, TurbinesUSWTB
from ssrs.utils import get_extent_from_bounds

# directory where output is saved
output_dir = os.path.join(os.path.abspath(os.path.curdir), 'output/')

# list valid WTK data sources
WTK.valid_sources

# inspect available layers
wtk_source = WtkSource('AWS')
wtk_source.valid_layers

# Initialize WTK object to download and manipulate data for given lonlat bounds
wtk_layers = ['windspeed_100m', 'winddirection_100m', 'pressure_100m','temperature_100m']
lonlat_bounds = (-106.411, 42.769, -105.1686, 43.2566)
wtk = WTK('AWS', lonlat_bounds, wtk_layers, output_dir)

# get data for various variables for a given datetime
dtime = datetime(2014, 10, 12, 9) # (year, month, date, hour)
wtkdf = wtk.get_dataframe_for_this_time(dtime)
wtkdf.head()

# get all the wind turbines in this region
lonlat_crs = 'EPSG:4326'
turbines = TurbinesUSWTB(lonlat_bounds, lonlat_crs, min_hubheight=60.)
turbines.print_details()

# Plot the downloaded atmospehric variables in lon/lat crs
interp_type = 'linear' # nearest, linear, cubic
xname ='Longitude'
yname = 'Latitude'
num_pts = 100
turb_xlocs, turb_ylocs = turbines.get_locations()
for j, this_var in enumerate(wtk_layers):
    fig, ax = plt.subplots(figsize=(7,4))
    vardata = wtkdf.loc[:, this_var].values.flatten()
    xlocs, ylocs = wtk.get_coordinates()
    extent = get_extent_from_bounds(lonlat_bounds)
    xmin, xmax, ymin, ymax = extent
    xmesh, ymesh = np.meshgrid(np.linspace(xmin,xmax,num_pts), np.linspace(ymin,ymax,num_pts))
    vargriddata = griddata(np.array([xlocs, ylocs]).T, vardata, (xmesh, ymesh), method=interp_type)
    cm = ax.imshow(vargriddata, cmap='viridis', origin='lower', extent=extent, alpha=0.75, aspect='auto')
    #ax.plot(wtkdf[xname], wtkdf[yname], '.k', alpha=0.15, markersize=3.)
    ax.plot(turb_xlocs, turb_ylocs, '1k', alpha=0.5, markersize=3.)
    ax.set_title(this_var)
    fig.colorbar(cm, ax = ax)



StarcoderdataJupyterScriptsDedupFiltered