<commit_before>#!/usr/bin/env python
"""
  Generate a simple scatter plot of power...
"""
import datetime
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
import matplotlib.colors as mpcolors
import matplotlib.colorbar as mpcolorbar
import matplotlib.patheffects as PathEffects
from pyiem.meteorology import uv
from pyiem.datatypes import speed, direction
import numpy as np
import psycopg2
import cgi
import sys


def make_colorbar(clevs, norm, cmap):
    """ Manual Color Bar """

    ax = plt.axes([0.02, 0.1, 0.05, 0.8], frameon=False,
                      yticks=[], xticks=[])

    under = clevs[0]-(clevs[1]-clevs[0])
    over = clevs[-1]+(clevs[-1]-clevs[-2])
    blevels = np.concatenate([[under,], clevs, [over,]])
    cb2 = mpcolorbar.ColorbarBase(ax, cmap=cmap,
                                 norm=norm,
                                 boundaries=blevels,
                                 extend='both',
                                 ticks=None,
                                 spacing='uniform',
                                 orientation='vertical')
    for i, lev in enumerate(clevs):
        y = float(i) / (len(clevs) -1)
        fmt = '%g'
        txt = cb2.ax.text(0.5, y, fmt % (lev,), va='center', ha='center')
        txt.set_path_effects([PathEffects.withStroke(linewidth=2,
                                                        foreground="w")])

    ax.yaxis.set_ticklabels([])


def do(valid):
    """ Generate plot for a given timestamp """
    PGCONN = psycopg2.connect(database='mec', host='iemdb', port='5432',
                              user='mesonet')
    cursor = PGCONN.cursor()

    cursor.execute("""select turbineid, power, ST_x(geom), ST_y(geom), yaw,
    windspeed, pitch
     from sampled_data s JOIN turbines t on (t.id = s.turbineid) 
     WHERE valid = %s and power is not null and yaw is not null
     and windspeed is not null and pitch is not null""", (valid,))
    lons = []
    lats = []
    vals = []
    u = []
    v = []
    ws = []
    yaw = []
    pitch = []
    for row in cursor:
        lons.append(row[2])
        lats.append(row[3])
        vals.append(row[1])
        ws.append( row[5] )
        yaw.append( row[4])
        a,b = uv(speed(row[5], 'MPS'), direction(row[4], 'deg'))
        u.append( a.value('MPS') )
        v.append( b.value('MPS') )
        pitch.append(row[6])
    vals = np.array(vals)
    avgv = np.average(vals)
    #vals2 = vals - avgv
    fig = plt.figure(figsize=(12.8,7.2))
    ax = fig.add_axes([0.14, 0.1, 0.52, 0.8])

    cmap = plt.cm.get_cmap('jet')
    cmap.set_under('tan')
    cmap.set_over('black')
    clevs = np.arange(0,1651,150)
    norm = mpcolors.BoundaryNorm(clevs, cmap.N)
    ax.quiver(lons, lats, u, v, zorder=1)
    ax.scatter(lons, lats, c=vals, norm=norm, edgecolor='none',
               cmap=cmap, s=100, zorder=2)
    ax.set_title("Pomeroy Farm Turbine Power [kW] (1min sampled dataset)\nValid: %s" % (
                                                        valid.strftime("%d %b %Y %I:%M %p")))
    make_colorbar(clevs, norm, cmap)
    
    ax.text(0.05, 0.05, "Turbine Avg: %.1f kW" % (avgv,),
            transform=ax.transAxes)
    ax.set_xlabel("Longitude $^\circ$E")
    ax.set_ylabel("Latitude $^\circ$N")
    
    # Next plot
    ax2 = fig.add_axes([0.7, 0.73, 0.28, 0.25])
    ax2.scatter(ws, vals)
    ax2.text(0.5, -0.2, "Wind Speed $ms^{-1}$", transform=ax2.transAxes,
             ha='center')
    #ax2.set_ylabel("Power kW")
    ax2.grid(True)

    # Next plot
    ax3 = fig.add_axes([0.7, 0.41, 0.28, 0.25], sharey=ax2)
    ax3.scatter(yaw, vals)
    ax3.text(0.5, -0.2, "Yaw $^\circ$N", transform=ax3.transAxes, ha='center')
    #ax3.set_ylabel("Power kW")
    ax3.set_xlim(0,360)
    ax3.set_xticks(np.arange(0,361,45))
    ax3.grid(True)
    
    # Next plot
    ax4 = fig.add_axes([0.7, 0.08, 0.28, 0.25], sharey=ax2)
    ax4.scatter(pitch, vals)
    ax4.text(0.5, -0.2, "Pitch $^\circ$", transform=ax4.transAxes, ha='center')
    ax4.grid(True)
    ax4.set_ylim(bottom=-10)
    ax4.set_xlim(-5,12)
    
    plt.savefig( sys.stdout )<commit_msg>add plot of pitch v. ws and report avg/stddev wind<commit_after>#!/usr/bin/env python
"""
  Generate a simple scatter plot of power...
"""
import datetime
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
import matplotlib.colors as mpcolors
import matplotlib.colorbar as mpcolorbar
import matplotlib.patheffects as PathEffects
from pyiem.meteorology import uv
from pyiem.datatypes import speed, direction
import numpy as np
import psycopg2
import cgi
import sys


def make_colorbar(clevs, norm, cmap):
    """ Manual Color Bar """

    ax = plt.axes([0.02, 0.1, 0.05, 0.8], frameon=False,
                      yticks=[], xticks=[])

    under = clevs[0]-(clevs[1]-clevs[0])
    over = clevs[-1]+(clevs[-1]-clevs[-2])
    blevels = np.concatenate([[under,], clevs, [over,]])
    cb2 = mpcolorbar.ColorbarBase(ax, cmap=cmap,
                                 norm=norm,
                                 boundaries=blevels,
                                 extend='both',
                                 ticks=None,
                                 spacing='uniform',
                                 orientation='vertical')
    for i, lev in enumerate(clevs):
        y = float(i) / (len(clevs) -1)
        fmt = '%g'
        txt = cb2.ax.text(0.5, y, fmt % (lev,), va='center', ha='center')
        txt.set_path_effects([PathEffects.withStroke(linewidth=2,
                                                        foreground="w")])

    ax.yaxis.set_ticklabels([])


def do(valid):
    """ Generate plot for a given timestamp """
    PGCONN = psycopg2.connect(database='mec', host='localhost', port='5555',
                              user='mesonet')
    cursor = PGCONN.cursor()

    cursor.execute("""select turbineid, power, ST_x(geom), ST_y(geom), yaw,
    windspeed, pitch
     from sampled_data s JOIN turbines t on (t.id = s.turbineid) 
     WHERE valid = %s and power is not null and yaw is not null
     and windspeed is not null and pitch is not null""", (valid,))
    lons = []
    lats = []
    vals = []
    u = []
    v = []
    ws = []
    yaw = []
    pitch = []
    for row in cursor:
        lons.append(row[2])
        lats.append(row[3])
        vals.append(row[1])
        ws.append( row[5] )
        yaw.append( row[4])
        a,b = uv(speed(row[5], 'MPS'), direction(row[4], 'deg'))
        u.append( a.value('MPS') )
        v.append( b.value('MPS') )
        pitch.append(row[6])
    pitch = np.array(pitch)
    vals = np.array(vals)
    avgv = np.average(vals)
    #vals2 = vals - avgv
    fig = plt.figure(figsize=(12.8,7.2))
    ax = fig.add_axes([0.14, 0.1, 0.52, 0.8])

    cmap = plt.cm.get_cmap('jet')
    cmap.set_under('tan')
    cmap.set_over('black')
    clevs = np.arange(0,1651,150)
    norm = mpcolors.BoundaryNorm(clevs, cmap.N)
    ax.quiver(lons, lats, u, v, zorder=1)
    ax.scatter(lons, lats, c=vals, norm=norm, edgecolor='none',
               cmap=cmap, s=100, zorder=2)
    ax.set_title("Pomeroy Farm Turbine Power [kW] (1min sampled dataset)\nValid: %s" % (
                                                        valid.strftime("%d %b %Y %I:%M %p")))
    make_colorbar(clevs, norm, cmap)
    
    ax.text(0.05, 0.05, "Turbine Avg: %.1f kW" % (avgv,),
            transform=ax.transAxes)
    ax.text(0.05, 0.01, "Wind $\mu$= %.1f $\sigma$= %.1f $ms^{-1}$" % (
                                                            np.average(ws),
                                                            np.std(ws)),
            transform=ax.transAxes)
    ax.set_xlabel("Longitude $^\circ$E")
    ax.set_ylabel("Latitude $^\circ$N")
    
    # Next plot
    ax2 = fig.add_axes([0.7, 0.80, 0.28, 0.18])
    ax2.scatter(ws, vals)
    ax2.text(0.5, -0.25, "Wind Speed $ms^{-1}$", transform=ax2.transAxes,
             ha='center')
    #ax2.set_ylabel("Power kW")
    ax2.grid(True)

    # Next plot
    ax3 = fig.add_axes([0.7, 0.57, 0.28, 0.18], sharey=ax2)
    ax3.scatter(yaw, vals)
    ax3.text(0.5, -0.25, "Yaw $^\circ$N", transform=ax3.transAxes, ha='center')
    #ax3.set_ylabel("Power kW")
    ax3.set_xlim(0,360)
    ax3.set_xticks(np.arange(0,361,45))
    ax3.grid(True)
    
    # Next plot
    ax4 = fig.add_axes([0.7, 0.32, 0.28, 0.18], sharey=ax2)
    ax4.scatter(pitch, vals)
    ax4.text(0.5, -0.25, "Pitch $^\circ$", transform=ax4.transAxes, ha='center')
    ax4.grid(True)
    ax4.set_ylim(bottom=-10)
    
    # Next plot
    ax5 = fig.add_axes([0.7, 0.07, 0.28, 0.18], sharex=ax4)
    ax5.scatter(pitch, ws)
    ax5.text(0.5, -0.25, "Pitch $^\circ$", transform=ax5.transAxes, ha='center')
    ax5.grid(True)
    ax5.set_ylim(bottom=-10)
    maxpitch = max(np.where(pitch > 20, 0, pitch))
    ax5.set_xlim(np.ma.minimum(pitch)-0.5, maxpitch+0.5)
    ax5.set_ylim(bottom=0)
    ax5.text(-0.1, 0.5, "Wind Speed $ms^{-1}$", transform=ax5.transAxes,
             ha='center', va='center', rotation=90)
    
    plt.savefig( sys.stdout )
{'fraction_non_alphanumeric': 0.1623836951705804, 'fraction_numerical': 0.04054054054054054, 'mean_word_length': 2.11237504308859, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1743777', 'n_tokens_mistral': 3563, 'n_tokens_neox': 3351, 'n_words': 682}