In [43]:
def max_generation(dbname, table = "data"):
    import sqlite3
    import pandas
    con = sqlite3.connect(dbname)
    cur = con.cursor()
    output = dbname[:-3] + "_max_generation.csv"
    out_table = pandas.read_sql("select max_gen as global_max_generation, op_date, op_hour, \
    name, m.orispl_code, m.unitid, \
    CASE WHEN (fuel_type is 'Natural Gas' or fuel_type is 'Pipeline Natural Gas' or fuel_type is 'Natural Gas, Pipeline Natural Gas')    THEN ('NG') ELSE (fuel_type) END as fuel_actual \
    from ( SELECT orispl_code, unitid, MAX(CAST(gload as float)) as max_gen from " + table + " Group by orispl_code, unitid) as m \
    , " + table + " as t WHERE \
    t.unitid = m.unitid AND t.orispl_code = m.orispl_code AND CAST(t.gload as float) = max_gen", con)
    out_table.to_csv(output)
    output = dbname[:-3] + "_max_heat_in.csv"
    out_table = pandas.read_sql("select max_heat as global_max_heat_input, op_date, op_hour, \
    name, m.orispl_code, m.unitid, \
    CASE WHEN (fuel_type is 'Natural Gas' or fuel_type is 'Pipeline Natural Gas' or fuel_type is 'Natural Gas, Pipeline Natural Gas')    THEN ('NG') ELSE (fuel_type) END as fuel_actual \
    from ( SELECT orispl_code, unitid, MAX(CAST(heat_input as float)) as max_heat from " + table + " Group by orispl_code, unitid) as m \
    , " + table + " as t WHERE \
    t.unitid = m.unitid AND t.orispl_code = m.orispl_code AND CAST(t.heat_input as float) = max_heat", con)
    out_table.to_csv(output)

In [44]:
# python script to extract global maximum generation and heat input for every unit in a given database
max_generation('smaller.db')

In [ ]: