Esempio n. 1
0
def view(db_type=None, type_id=None, country_id=None, state_id=None):

    main = Main(db)
    pref = main.get_user_pref()
    if not type_id or not country_id:
        url = "/".join(["/allunits", pref[0], pref[1], pref[2]])
        #url = main.get_search_redirect_url("summary/type", return_type="type_summary")
        #print(url)
        return flask.redirect(url)

    type_name = main.get_type_name(type_id)
    country_name = main.get_country_name(country_id)

    units = []
    moderation = Moderation(db)
    res_label, resources = moderation.get_all_resources(country_id=country_id, type_id=type_id)
    for res in resources:
        desc_id = res[0]
        name = res[1]
        unit = {"resource_name": name, "resource_id": desc_id}
        geo_resource = GeoResource(db, desc_id)
        html = Html(geo_resource)
        html.editable = False
        unit["content"] = html.make_unit_module("Unit_Description")
        units.append(unit)

    main.store_user_pref(pref[0], country_id, type_id, pref[3])
    user_pref = main.make_html_user_pref()

    return flask.render_template("allunits.html",
                                 resources=units,
                                 country=country_name,
                                 type=type_name,
                                 user_pref=user_pref)
Esempio n. 2
0
def view():

    moderation = Moderation(db)
    new_submits, edits = moderation.get_resources_to_moderate()

    main = Main(db)
    user_pref = main.make_html_user_pref()

    baseurl = "/geoid/"
    return flask.render_template("moderation.html",
                                 new_submits=new_submits,
                                 edits=edits,
                                 baseurl=baseurl,
                                 user_pref=user_pref)
Esempio n. 3
0
    def for_many_resources(self, country_id=None, type_id=None):
        """
        Returns the location information for all the resources
        in a country for the type.

        :@param country: the country id
        :@param typ: type id
        """

        if int(country_id) <= 0 or int(type_id) <= 0:
            return {}

        moderation = Moderation(self.connection)
        keys, values = moderation.get_all_resources(country_id=country_id, type_id=type_id)
        del keys

        table_name = self.main.get_type_name(type_id) + "_Location"

        loc = []
        locations = self.select.read(table_name,
                                     columns=["Description_ID",
                                              "Latitude_Start",
                                              "Longitude_Start"],
                                     where=[["Description_ID",
                                             "in",
                                             [value[0] for value in values]]],
                                     dict_cursor=False)

        locations = locations.fetchall()

        for value in values:
            for location in locations:
                if value[0] == int(location[0].decode("utf8")):
                    loc.append([location[1].decode("utf8"),
                                location[2].decode("utf8"),
                                value[1]
                                ])
        """
        lat = loc['Latitude_Start']
        lng = loc['Longitude_Start']
        for value in values:
            desc_id = value[0]
            name = value[1]

            lat, lng = self.__get_lat_lng(table_name, desc_id)
            loc.append([lat, lng, name])
        """
        return {"locations": loc, "boundLocation":
                self.main.get_country_name(country_id)}
Esempio n. 4
0
def view():

    typ = flask.request.args.get("type", 0)
    country = flask.request.args.get("country", 0)
    state = flask.request.args.get("state", 0)

    mod = Moderation(db)

    keys, values = mod.get_all_resources(country_id=country, type_id=typ)

    res = []
    for v in values:
        val = {}
        for i, k in enumerate(keys):
            val[k] = v[i]
        res.append(val)
    return flask.jsonify(resources=res)
Esempio n. 5
0
def view(db_type=None, typ=None, country=None, state=None):

    if not typ or not country:
        typ = flask.request.args.get("type", 0)
        country = flask.request.args.get("country", 0)
        state = flask.request.args.get("state", 0)


    main = Main(db)

    if not typ or not country:
        pref = main.get_user_pref()
        url_comp = ["/resources"]
        url_comp.extend(pref)
        url = "/".join(url_comp)
        #url = main.get_search_redirect_url("resources")
        return flask.redirect(url)

    moderation = Moderation(db)
    keys, values = moderation.get_all_resources(country_id=country, type_id=typ)
    baseurl = "/geoid/"

    # storing the prefs to a cookie
    main.store_user_pref(db_type, country, typ, state)

    type_name = main.get_type_name(typ)
    country_name = main.get_country_name(country)
    state_name = None
    if state > 0:
        state_name = main.get_state_name(state)

    user_pref = main.make_html_user_pref()

    return flask.render_template("resources.html",
                                 values=values, baseurl=baseurl,
                                 user_pref=user_pref, type=type_name, country=country_name,
                                 state=state_name if state > 0 else False,
                                 category=db_type)
Esempio n. 6
0
    def __init__(self):
        self.conn = connection.Db().session
        self.main = Main(connection.Db())
        self.moderation = Moderation(connection.Db())
        self.select = Select(connection.Db())

        self.perf_fields = ("_Performance", "Year_yr",
                       ["Total_Gigawatt_Hours_Generated_nbr", "CO2_Emitted_(Tonnes)_nbr"])
        self.unit_fields = ("_Unit_Description", "Date_Commissioned_dt", "Capacity_(MWe)_nbr")

        self.alt_types = [4, 7, 8, 10]
        self.alt_perf_fields = ("_Performance", "Year_yr",
                           ["Total_Gigawatt_Hours_Generated_nbr", "Plant_Load_Factor_(%)_nbr"])

        self.nuclear_perf_fields = ("_Performance", "Year_yr",
                                ["Plant_Load_Factor_(%)_nbr"])
        self.nuclear_ghg_fields = ("_Gigawatt_Hours_Generated", "Year_yr",
                                   ["Gigawatt_Hours_Generated_nbr"])
        self.wind_unit_fields = ("_Unit_Description", "Date_Commissioned_dt",
                                 "Peak_MWe_per_Turbine_nbr", "Number_Of_Turbines_nbr")
Esempio n. 7
0
class Metadata(object):
    """
    Computes the values and updates the metadata table.
    This table provides summary info for the views.
    """

    def __init__(self):
        self.conn = connection.Db().session
        self.main = Main(connection.Db())
        self.moderation = Moderation(connection.Db())
        self.select = Select(connection.Db())

        self.perf_fields = ("_Performance", "Year_yr",
                       ["Total_Gigawatt_Hours_Generated_nbr", "CO2_Emitted_(Tonnes)_nbr"])
        self.unit_fields = ("_Unit_Description", "Date_Commissioned_dt", "Capacity_(MWe)_nbr")

        self.alt_types = [4, 7, 8, 10]
        self.alt_perf_fields = ("_Performance", "Year_yr",
                           ["Total_Gigawatt_Hours_Generated_nbr", "Plant_Load_Factor_(%)_nbr"])

        self.nuclear_perf_fields = ("_Performance", "Year_yr",
                                ["Plant_Load_Factor_(%)_nbr"])
        self.nuclear_ghg_fields = ("_Gigawatt_Hours_Generated", "Year_yr",
                                   ["Gigawatt_Hours_Generated_nbr"])
        self.wind_unit_fields = ("_Unit_Description", "Date_Commissioned_dt",
                                 "Peak_MWe_per_Turbine_nbr", "Number_Of_Turbines_nbr")

    def get_unit_data(self, typ, plants):
        # units
        results = self.select.read(typ[1] + self.unit_fields[0],
                            columns=["`" + self.unit_fields[1] + "`",
                                     "`" + self.unit_fields[2] + "`", "Description_ID"],
                            where=[["Description_ID", "in",
                                    [plant[0] for plant in plants]]],
                                 dict_cursor=False)
        units = results.fetchall()
        cumulative_capacity_total = 0
        cumulative_capacity = {}
        new_capacity = {}
        new_capacity['keys'] = [self.unit_fields[1], self.unit_fields[2]]
        cumulative_capacity['keys'] = [self.unit_fields[1], self.unit_fields[2]]
        cap_values = {}
        cum_cap_values = {}
        for unit in units:
            if not unit[1] or not unit[0]:
                continue

            unit_val = math.ceil(float(unit[1]))
            cumulative_capacity_total += unit_val

            year = unit[0].decode("utf8").split("-")[0]
            val = cap_values.get(year, -1)
            if val == -1:
                cap_values[year] = 0
            cap_values[year] += unit_val

        prev_year = 0
        for year in sorted(cap_values.keys()):
            cum_cap_values[year] = cap_values[year] + cum_cap_values.get(prev_year, 0)
            prev_year = year

        new_capacity['values'] = sorted([list(values) for values in
                                         zip(cap_values.keys(),
                                             cap_values.values())]
                                        )
        cumulative_capacity['values'] = sorted([list(values) for values in
                                         zip(cum_cap_values.keys(),
                                             cum_cap_values.values())]
        )

        return cumulative_capacity_total, cumulative_capacity, new_capacity

    def get_wind_unit_data(self, typ, plants):
        # units
        results = self.select.read(typ[1] + self.wind_unit_fields[0],
                                 columns=["`" + self.wind_unit_fields[1] + "`",
                                          "`" + self.wind_unit_fields[2] + "`",
                                          "`" + self.wind_unit_fields[3] + "`"],
                                 where=[["Description_ID", "in",
                                         [plant[0] for plant in plants]]],
                                 dict_cursor=False)
        units = results.fetchall()
        cumulative_capacity_total = 0
        new_capacity = {}
        new_capacity['keys'] = [self.unit_fields[1], self.unit_fields[2]]
        cap_values = {}
        for unit in units:
            if not unit[1] or not unit[0]:
                continue

            unit_val = math.ceil(float(unit[1]))
            cumulative_capacity_total += unit_val

            year = unit[0].decode("utf8").split("-")[0]
            val = cap_values.get(year, -1)
            if val == -1:
                cap_values[year] = 0
            if unit[2] and float(unit[2]):
                cap_values[year] += unit_val * math.ceil(float(unit[2]))

        cum_cap_values = {}
        prev_year = 0
        for year in sorted(cap_values.keys()):
            cum_cap_values[year] = cap_values[year] + cum_cap_values.get(prev_year, 0)
            prev_year = year

        new_capacity['values'] = sorted([list(values) for values in
                                         zip(cap_values.keys(),
                                             cap_values.values())])

        cumulative_capacity = {}
        cumulative_capacity['values'] = sorted([list(values) for values in
                                                zip(cum_cap_values.keys(),
                                                    cum_cap_values.values())]
        )

        return cumulative_capacity_total, cumulative_capacity, new_capacity

    def get_general_performance_data(self, typ, plants):
        # performance
        cols = [self.perf_fields[1]]
        if typ[0] in self.alt_types:
            cols.extend(["`" + p_col + "`" for p_col in self.alt_perf_fields[2]])
        else:
            cols.extend(["`" + p_col + "`" for p_col in self.perf_fields[2]])

        results = self.select.read(typ[1] + self.perf_fields[0],
                                   columns=cols,
                                   where=[["Description_ID", "in",
                                           [plant[0] for plant in plants]]],
                                        dict_cursor=False)
        performances = results.fetchall()
        co2 = {}
        ghg = {}
        for perf in performances:
            year = perf[0].decode("utf8")

            if perf[1]:
                g_val = ghg.get(year, -1)
                if g_val == -1:
                    ghg[year] = 0
                ghg[year] += math.ceil(float(perf[1]))

            if perf[2]:
                c_val = co2.get(year, -1)
                if c_val == -1:
                    co2[year] = 0
                co2[year] += math.ceil(float(perf[2]))

        # for plotting purposes, the years in both the dicts must
        # be the same. so adding zeros to missing years.
        for year in co2.keys():
            val = ghg.get(year, -1)
            if val == -1:
                ghg[year] = 0

        for year in ghg.keys():
            val = co2.get(year, -1)
            if val == -1:
                co2[year] = 0

        ghg_values = {"keys": [self.perf_fields[1], self.perf_fields[2][0]],
                      "values": sorted([list(values)
                                        for values in zip(ghg.keys(),
                                                          ghg.values())]
                                       )}

        co2_values = {"keys": [self.perf_fields[1], self.perf_fields[2][1]],
                      "values": sorted([list(values) for values in
                                        zip(co2.keys(), co2.values())])}

        return ghg_values, co2_values

    def get_nuclear_performance_data(self, typ, plants):
        # performance
        cols = [self.perf_fields[1]]
        cols.append("`" + self.nuclear_ghg_fields[2][0] + "`")
        ghg_res = self.select.read(typ[1] + self.nuclear_ghg_fields[0],
                                        columns=cols,
                                        where=[["Description_ID", "in",
                                                [plant[0] for plant in plants]]],
                                    dict_cursor=False)

        ghg_perf = ghg_res.fetchall()
        cols = [self.perf_fields[1]]
        cols.append("`" + self.nuclear_perf_fields[2][0] + "`")
        reg_res = self.select.read(typ[1] + self.nuclear_perf_fields[0],
                                    columns=cols,
                                    where=[["Description_ID", "in",
                                            [plant[0] for plant in plants]]],
                                    dict_cursor=False)
        reg_perf = reg_res.fetchall()
        co2 = {}
        ghg = {}
        for perf in ghg_perf:
            year = perf[0].decode("utf8")

            if perf[1]:
                g_val = ghg.get(year, -1)
                if g_val == -1:
                    ghg[year] = 0
                ghg[year] += math.ceil(float(perf[1]))

        for perf in reg_perf:
            if perf[1]:
                c_val = co2.get(year, -1)
                if c_val == -1:
                    co2[year] = 0
                co2[year] += math.ceil(float(perf[1]))

        # for plotting purposes, the years in both the dicts must
        # be the same. so adding zeros to missing years.
        for year in co2.keys():
            val = ghg.get(year, -1)
            if val == -1:
                ghg[year] = 0

        for year in ghg.keys():
            val = co2.get(year, -1)
            if val == -1:
                co2[year] = 0

        ghg_values = {"keys": [self.nuclear_ghg_fields[1], self.nuclear_ghg_fields[2][0]],
                      "values": sorted([list(values) for values in
                                        zip(ghg.keys(), ghg.values())]
                                       )}

        co2_values = {"keys": [self.nuclear_perf_fields[1], self.nuclear_perf_fields[2][0]],
                      "values": sorted([list(values) for values in
                                        zip(co2.keys(), co2.values())]
                                       )}
        return ghg_values, co2_values

    def compute(self):
        t_keys, types = self.main.get_types("PowerPlants")
        #types = [[1, "Coal"]]
        session = self.conn.cursor()
        for typ in types:
            c_keys, countries = self.main.get_countries(typ[0])
            #countries = [[38, "Canada"]]

            for country in countries:
                p_keys, plants = self.moderation.get_all_resources(country_id=country[0], type_id=typ[0])

                # unit data
                if typ[0] == 10:
                    cumulative_capacity_total, cumulative_capacity, new_capacity = self.get_wind_unit_data(typ, plants)
                else:
                    cumulative_capacity_total, cumulative_capacity, new_capacity = self.get_unit_data(typ, plants)

                # perf data
                if typ[0] == 5:
                    ghg, co2 = self.get_nuclear_performance_data(typ, plants)
                else:
                    ghg, co2 = self.get_general_performance_data(typ, plants)

                sql = "INSERT INTO metadata SET Country_ID=%(country_id)s, \
                 Type_ID=%(type_id)s, Number_of_Plants=%(number_of_plants)s, \
                 Cumulative_Capacity=%(cum_cap)s, \
                 Cumulative_Capacity_Total=%(cum_cap_tot)s, New_Capacity_Added=%(new_cap)s, \
                 Annual_Gigawatt_Hours_Generated=%(ghg)s, Annual_CO2_Emitted=%(co2)s"

                session.execute(sql, {
                    "country_id": country[0],
                    "type_id": typ[0],
                    "number_of_plants": str(len(plants)),
                    "cum_cap": json.dumps(cumulative_capacity),
                    "cum_cap_tot": str(cumulative_capacity_total),
                    "new_cap": json.dumps(new_capacity),
                    "ghg": json.dumps(ghg),
                    "co2": json.dumps(co2)
                })
                self.conn.commit()
        session.close()