Coverage for src / prepare_times_nz / stage_0 / stage_0_settings.py: 93%
43 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-01 22:14 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-01 22:14 +0000
1"""
3This script reads and surfaces several useful settings as constants you can import
5These are effectively inputs for SysSettings, including:
7a) Base year
8b) Milestone years
9c) Period definitions (PDef)
11NOTE: This was developed slightly late in the process.
12However, a lot of items should really be importing base and milestone years from here
13So some scripts should be refactored to import these rather than hardcoding
15Year fractions are processed separately in load curves so are not included here
16"""
18# Libraries ------------------------------------------
20import tomllib
22import pandas as pd
23from prepare_times_nz.utilities.filepaths import DATA_INTERMEDIATE, DATA_RAW
24from prepare_times_nz.utilities.logger_setup import logger
26# File locations ------------------------------------------
27SETTINGS_ASSUMPTIONS = DATA_RAW / "user_config/settings"
28SYSSETTINGS_TOML = DATA_INTERMEDIATE / "stage_0_config/SysSettings.toml"
29MILESTONE_YEARS_FILE = SETTINGS_ASSUMPTIONS / "milestone_years.csv"
31# Constants to distribute ----------------------------------------
33CAP2ACT_PJGW: float = 31.536 # PJ per GW at 100 % utilisation (365 * 24 / 1000)
36# Functions -----------------------------------------------
39def get_sys_settings_data():
40 """
41 Reads SysSettings from the normalised toml files and return all data
42 """
43 syssettings_toml = SYSSETTINGS_TOML
45 with open(syssettings_toml, "rb") as f:
46 data = tomllib.load(f)
48 return data
51def get_base_year(data):
52 """
53 extracts base year from the syssettings data
54 Note: StartYear is the name of the table and the column
55 So we extract:
56 THe table (StartYear)
57 then its Data
58 Then the StartYear variable
59 Then the first element
60 """
61 return data["StartYear"]["Data"]["StartYear"][0]
64def get_active_pdef(data):
65 """
66 Returns the name of the active PDef
67 """
68 return data["ActivePDef"]["Data"]["ActivePDef"][0]
71def get_milestone_years_for_pdef(active_pdef, file=MILESTONE_YEARS_FILE):
72 """
73 Takes the input data of milestone years (assumptions file)
74 This file can have an arbitrary set of possibilities
75 Selects the Active PDef and returns only those values
76 """
77 df = pd.read_csv(file)
79 # fail loud and early if nothing found
80 if active_pdef not in df.columns:
81 logger.error("No milestone years found for '%s'", active_pdef)
82 # select only the relevant entry
83 df = df[[active_pdef]]
84 # remove nulls
85 df = df[~df[active_pdef].isna()]
86 # ensure integers
87 df[active_pdef] = df[active_pdef].astype(int)
89 # sort values (in unlikely event they aren't already)
90 df = df.sort_values(active_pdef)
92 return df
95def create_period_definitions(by, df):
96 """
97 Using the milestone years and base year definition,
98 create the active pdef years (returning a dataframe)
100 by: the base year
101 df: milestone year data
102 Expects df to have only one variable, which is the current active_pdef
104 """
106 df = df.copy()
108 if len(df.columns) != 1:
109 logger.warning(
110 "ALERT: period definitions data should have exactly one variable"
111 )
112 print(df)
114 var_name = df.columns[0]
116 # calculate relevant milestone years
117 # (subtracting base year then taking the difference from previous milestone years)
118 df[var_name] = df[var_name] - by + 1
119 df[var_name] = df[var_name].diff().fillna(df[var_name])
120 # make int again
121 df[var_name] = df[var_name].astype(int)
123 return df
126# Execute -----------------------------------------------
127# to have constants available to export
129sys_settings_data = get_sys_settings_data()
131# BASE_YEAR to be exported
132BASE_YEAR = get_base_year(sys_settings_data)
133pdef = get_active_pdef(sys_settings_data)
134milestone_years = get_milestone_years_for_pdef(pdef)
135active_periods = create_period_definitions(BASE_YEAR, milestone_years)
137# other constants to export
138MILESTONE_YEAR_LIST = milestone_years[pdef].tolist()
139ACTIVE_PERIOD_LIST = active_periods[pdef].tolist()