Skip to content

Build temperature profiles

Functions associated with the build_temperature_profiles rule.

build_temp_profiles(pop_map, cutout, temperature_out)

Build the temperature profiles in the cutout, this converts the atlite temperature & weights the node building process by the population map

Note that atlite only supports a single time zone shift

Parameters:

Name Type Description Default
pop_map DataFrame

the map to the pop density grid cell data (hdf5)

required
cutout Cutout

the weather data cutout (atlite cutout)

required
temperature_out PathLike

the output path (hdf5)

required
Source code in workflow/scripts/build_temperature_profiles.py
def build_temp_profiles(pop_map: pd.DataFrame, cutout: atlite.Cutout, temperature_out: PathLike):
    """Build the temperature profiles in the cutout, this converts the atlite temperature & weights
    the node building process by the population map

    Note that atlite only supports a single time zone shift

    Args:
        pop_map (pd.DataFrame): the map to the pop density grid cell data (hdf5)
        cutout (atlite.Cutout): the weather data cutout (atlite cutout)
        temperature_out (PathLike): the output path (hdf5)
    """
    # build a sparse matrix of BUSxCUTOUT_gridcells to weigh the cutout->bus aggregation process
    pop_matrix = sp.sparse.csr_matrix(pop_map.T)
    index = pop_map.columns
    index.name = "provinces"

    temperature = cutout.temperature(matrix=pop_matrix, index=index)
    # convert the cutout UTC time to local time
    temperature["time"] = (
        pd.DatetimeIndex(temperature["time"], tz="UTC")
        .tz_convert(TIMEZONE)
        .tz_localize(None)
        .values
    )

    with pd.HDFStore(temperature_out, mode="w", complevel=4) as store:
        store["temperature"] = temperature.to_pandas().divide(pop_map.sum())