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

1""" 

2 

3This script reads and surfaces several useful settings as constants you can import 

4 

5These are effectively inputs for SysSettings, including: 

6 

7a) Base year 

8b) Milestone years 

9c) Period definitions (PDef) 

10 

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 

14 

15Year fractions are processed separately in load curves so are not included here 

16""" 

17 

18# Libraries ------------------------------------------ 

19 

20import tomllib 

21 

22import pandas as pd 

23from prepare_times_nz.utilities.filepaths import DATA_INTERMEDIATE, DATA_RAW 

24from prepare_times_nz.utilities.logger_setup import logger 

25 

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" 

30 

31# Constants to distribute ---------------------------------------- 

32 

33CAP2ACT_PJGW: float = 31.536 # PJ per GW at 100 % utilisation (365 * 24 / 1000) 

34 

35 

36# Functions ----------------------------------------------- 

37 

38 

39def get_sys_settings_data(): 

40 """ 

41 Reads SysSettings from the normalised toml files and return all data 

42 """ 

43 syssettings_toml = SYSSETTINGS_TOML 

44 

45 with open(syssettings_toml, "rb") as f: 

46 data = tomllib.load(f) 

47 

48 return data 

49 

50 

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] 

62 

63 

64def get_active_pdef(data): 

65 """ 

66 Returns the name of the active PDef 

67 """ 

68 return data["ActivePDef"]["Data"]["ActivePDef"][0] 

69 

70 

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) 

78 

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) 

88 

89 # sort values (in unlikely event they aren't already) 

90 df = df.sort_values(active_pdef) 

91 

92 return df 

93 

94 

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) 

99 

100 by: the base year 

101 df: milestone year data 

102 Expects df to have only one variable, which is the current active_pdef 

103 

104 """ 

105 

106 df = df.copy() 

107 

108 if len(df.columns) != 1: 

109 logger.warning( 

110 "ALERT: period definitions data should have exactly one variable" 

111 ) 

112 print(df) 

113 

114 var_name = df.columns[0] 

115 

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) 

122 

123 return df 

124 

125 

126# Execute ----------------------------------------------- 

127# to have constants available to export 

128 

129sys_settings_data = get_sys_settings_data() 

130 

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) 

136 

137# other constants to export 

138MILESTONE_YEAR_LIST = milestone_years[pdef].tolist() 

139ACTIVE_PERIOD_LIST = active_periods[pdef].tolist()