<commit_before>#!/usr/bin/env python
"""Generate a PDF Report for a given HUC12."""
from io import BytesIO
import datetime
import cgi
import calendar

import requests
from metpy.units import units
from pandas.io.sql import read_sql
from reportlab.platypus import (
    SimpleDocTemplate, Paragraph, Spacer, Image,
    Table, TableStyle, PageBreak
)
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import inch
from pyiem.util import ssw, get_dbconn
PAGE_WIDTH = letter[0]
PAGE_HEIGHT = letter[1]
GENTIME = datetime.datetime.now().strftime("%B %-d %Y")
INTROTEXT = (
    "The Daily Erosion Project generates estimates of sheet and rill "
    "erosion.  This PDF summarizes our model results.  All results should be "
    "considered preliminary and subject to future revision as improvements "
    "are made to our model system and input datasets."
)
LOCALIZATION = {
    'F1': [
        'Figure 1: Regional view with HUC8 highlighted in tan.',
        ('Figure 1: Regional view with HUC8 highlighted in tan '
         'and HUC12 in red.'),
    ],
    'F2': [
        'Figure 2: Zoomed in view of HUC8.',
        ('Figure 2: Zoomed in view with HUC8 highlighted in tan '
         'and HUC12 in red.'),
    ],
}


def m2f(val):
    """Convert meters to feet."""
    return ((val * units('m')).to(units('feet'))).m


def generate_run_metadata(huc12):
    """Information about DEP modelling of this huc12."""
    styles = getSampleStyleSheet()
    res = []
    pgconn = get_dbconn('idep')
    # Get the number of runs
    cursor = pgconn.cursor()
    cursor.execute("""
    select count(*), min(ST_Length(geom)), avg(ST_Length(geom)),
    max(ST_Length(geom)) from flowpaths
    where huc_12 = %s and scenario = 0
    """, (huc12, ))
    row = cursor.fetchone()
    res.append(Paragraph((
        "The Daily Erosion Project models %s hill slopes within this HUC12."
        "These slopes range in length from %.1f to %.1f meters "
        "(%.1f to %.1f feet) with an overall average of %.1f meters "
        "(%.1f feet)."
    ) % (row[0], row[1], row[3], m2f(row[1]), m2f(row[3]), row[2],
         m2f(row[2])), styles['Normal']))

    # Something about managements and crops
    rows = [['Year', 'Corn', 'Soybean', 'Pasture', 'Other']]
    for year in range(2016, 2019):
        df = read_sql("""
        select lu""" + str(year) + """ as datum, count(*) from
        flowpath_points p JOIN flowpaths f on (p.flowpath = f.fid)
        WHERE f.huc_12 = %s and f.scenario = 0 GROUP by datum
        """, pgconn, params=(huc12, ), index_col='datum')
        total = df['count'].sum()
        leftover = total
        rows.append([year])
        for cropcode in ['C', 'B', 'P']:
            if cropcode in df.index:
                val = df.loc[cropcode, 'count']
                leftover -= val
                rows[-1].append("%.1f%%" % (val / total * 100., ))
            else:
                rows[-1].append("None")
        rows[-1].append("%.1f%%" % (leftover / total * 100., ))

    # Histogram of slope profiles
    res.append(Table([[[
        Image(get_image_bytes(
             ('http://dailyerosion.local/'
              'auto/huc12_slopes.py?huc12=%s') % (huc12,)),
              width=3.6*inch, height=2.4*inch),
        Paragraph("Figure 3: Hillslope Lengths and Slopes.", styles['Normal'])
        ], [
        Paragraph("Cropping Systems", styles['Heading3']),
        Table(rows),
        Paragraph((
            "Table 1: Estimated cropping percentage based on modelled "
            "hillslopes.  The 'Other' column represents all other cropping "
            "types supported by DEP."), styles['Normal'])
        ]
    ]]))

    return res


def generate_monthly_summary_table(huc12):
    """Make a table of monthly summary stats."""
    data = []
    data.append(['Year', 'Month', 'Precip', "Runoff", "Loss", "Delivery",
                '2+" Precip', 'Events'])
    data.append(['', '', '[inch]', "[inch]", "[tons/acre]", "[tons/acre]",
                 "[days]", "[days]"])
    pgconn = get_dbconn('idep')
    huc12col = "huc_12"
    if len(huc12) == 8:
        huc12col = "substr(huc_12, 1, 8)"
    df = read_sql("""
    WITH data as (
        SELECT extract(year from valid)::int as year,
        extract(month from valid)::int as month, huc_12,
        (sum(qc_precip) / 25.4)::numeric as sum_qc_precip,
        (sum(avg_runoff) / 25.4)::numeric as sum_avg_runoff,
        (sum(avg_loss) * 4.463)::numeric as sum_avg_loss,
        (sum(avg_delivery) * 4.463)::numeric as sum_avg_delivery,
        sum(case when qc_precip >= 50.8 then 1 else 0 end) as pdays,
        sum(case when avg_loss > 0 then 1 else 0 end) as events
        from results_by_huc12 WHERE scenario = 0 and
        """ + huc12col + """ = %s
        and valid >= '2016-01-01'<commit_msg>more huc12 report tweaks #61<commit_after>#!/usr/bin/env python
"""Generate a PDF Report for a given HUC12."""
from io import BytesIO
import datetime
import cgi
import calendar

import requests
from metpy.units import units
from pandas.io.sql import read_sql
from reportlab.platypus import (
    SimpleDocTemplate, Paragraph, Spacer, Image,
    Table, TableStyle, PageBreak
)
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import inch
from pyiem.util import ssw, get_dbconn
PAGE_WIDTH = letter[0]
PAGE_HEIGHT = letter[1]
GENTIME = datetime.datetime.now().strftime("%B %-d %Y")
INTROTEXT = (
    "The Daily Erosion Project generates estimates of sheet and rill "
    "erosion.  This PDF summarizes our model results.  All results should be "
    "considered preliminary and subject to future revision as improvements "
    "are made to our model system and input datasets."
)
LOCALIZATION = {
    'F1': [
        'Figure 1: Regional view with HUC8 highlighted in tan.',
        ('Figure 1: Regional view with HUC8 highlighted in tan '
         'and HUC12 in red.'),
    ],
    'F2': [
        'Figure 2: Zoomed in view of HUC8.',
        ('Figure 2: Zoomed in view with HUC8 highlighted in tan '
         'and HUC12 in red.'),
    ],
}


def m2f(val):
    """Convert meters to feet."""
    return ((val * units('m')).to(units('feet'))).m


def generate_run_metadata(huc12):
    """Information about DEP modelling of this huc12."""
    styles = getSampleStyleSheet()
    res = []
    pgconn = get_dbconn('idep')
    # Get the number of runs
    cursor = pgconn.cursor()
    cursor.execute("""
    select count(*), min(ST_Length(geom)), avg(ST_Length(geom)),
    max(ST_Length(geom)) from flowpaths
    where huc_12 = %s and scenario = 0
    """, (huc12, ))
    row = cursor.fetchone()
    res.append(Paragraph((
        "The Daily Erosion Project models %s hill slopes within this HUC12."
        "These slopes range in length from %.1f to %.1f meters "
        "(%.1f to %.1f feet) with an overall average of %.1f meters "
        "(%.1f feet)."
    ) % (row[0], row[1], row[3], m2f(row[1]), m2f(row[3]), row[2],
         m2f(row[2])), styles['Normal']))

    # Something about managements and crops
    rows = [['Year', 'Corn', 'Soybean', 'Pasture', 'Other']]
    for year in range(2016, 2019):
        df = read_sql("""
        select lu""" + str(year) + """ as datum, count(*) from
        flowpath_points p JOIN flowpaths f on (p.flowpath = f.fid)
        WHERE f.huc_12 = %s and f.scenario = 0 GROUP by datum
        """, pgconn, params=(huc12, ), index_col='datum')
        total = df['count'].sum()
        leftover = total
        rows.append([year])
        for cropcode in ['C', 'B', 'P']:
            if cropcode in df.index:
                val = df.loc[cropcode, 'count']
                leftover -= val
                rows[-1].append("%.1f%%" % (val / total * 100., ))
            else:
                rows[-1].append("None")
        rows[-1].append("%.1f%%" % (leftover / total * 100., ))

    # Histogram of slope profiles
    tablestyle = TableStyle([
         ('VALIGN', (0, 0), (-1, -1), 'TOP')]
    )
    res.append(Table([[[
        Image(get_image_bytes(
             ('http://dailyerosion.local/'
              'auto/huc12_slopes.py?huc12=%s') % (huc12,)),
              width=3.6*inch, height=2.4*inch),
        Paragraph((
            "Figure 3: Hillslope Lengths and Slopes. The Kernel Density "
            "Estimate is a way to show the join density of length vs slope "
            "shown in this plot. Rewording, where the most points lie."
        ), styles['Normal'])
        ], [
        Spacer(inch, inch * 0.25),
        Paragraph("Cropping Systems", styles['Heading3']),
        Table(rows),
        Paragraph((
            "Table 1: Estimated cropping percentage based on modelled "
            "hillslopes.  The 'Other' column represents all other cropping "
            "types supported by DEP."), styles['Normal'])
        ]
    ]], style=tablestyle))

    return res


def generate_monthly_summary_table(huc12):
    """Make a table of monthly summary stats."""
    data = []
    data.append(['Year', 'Month', 'Precip', "Runoff", "Loss", "Delivery",
                '2+" Precip', 'Events'])
    data.append(['', '', '[inch]', "[inch]", "[tons/acre]", "[tons/acre]",
                 "[days]", "[days]"])
    pgconn = get_dbconn('idep')
    huc12col = "huc_12"
    if len(huc12) == 8:
        huc12col = "substr(huc_12, 1, 8)"
    df = read_sql("""
    WITH data as (
        SELECT extract(year from valid)::int as year,
        extract(month from valid)::int as month, huc_12,
        (sum(qc_precip) / 25.4)::numeric as sum_qc_precip,
        (sum(avg_runoff) / 25.4)::numeric as sum_avg_runoff,
        (sum(avg_loss) * 4.463)::numeric as sum_avg_loss,
        (sum(avg_delivery) * 4.463)::numeric as sum_avg_delivery,
        sum(case when qc_precip >= 50.8 then 1 else 0 end) as pdays,
        sum(case when avg_loss > 0 then 1 else 0 end) as events
        from results_by_huc12 WHERE scenario = 0 and
        """ + huc12col + """ = %s
        and valid >= '2016-01-01'
{'fraction_non_alphanumeric': 0.1428713759091362, 'fraction_numerical': 0.028195675998804424, 'mean_word_length': 2.569701280227596, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6058335', 'n_tokens_mistral': 3435, 'n_tokens_neox': 3208, 'n_words': 972}