Functional Weave
Code in TypeScript

energy.solar-generation@1.0.0

impl/python.py

1,106 bytes · the Python implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

from typing import Sequence

from .energy_solar_generation_types import PvArray, SolarEstimate
from .math_round_div import round_div  ← from math.round-div ^1.0.0 · built alongside by fune

# watts x Kk x SF (hundredths) / (1000 W/kW x 100) is kWh.
DIVISOR = 100_000


def _check(what: str, value: object, maximum: int) -> None:
    if not isinstance(value, int) or isinstance(value, bool) or value < 0 or value > maximum:
        raise ValueError("%s must be a whole number from 0 to %d, received %s" % (what, maximum, value))


def solar_generation(arrays: Sequence[PvArray]) -> SolarEstimate:
    """Annual AC generation by the MCS method, kWp x Kk x SF, rounded half-up
    to a whole kWh for each array; the total is the sum of the arrays.
    """
    if len(arrays) == 0:
        raise ValueError("a solar estimate needs at least one array")
    kwh = []
    for a in arrays:
        _check("wattsPeak", a.watts_peak, 100_000_000)
        _check("kk", a.kk, 5000)
        _check("shadeFactor", a.shade_factor, 100)
        kwh.append(round_div(a.watts_peak * a.kk * a.shade_factor, DIVISOR, "half-up"))
    return SolarEstimate(arrays=kwh, total=sum(kwh))