ebm 0.99.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. ebm-0.99.3/LICENSE +21 -0
  2. ebm-0.99.3/MANIFEST.in +2 -0
  3. ebm-0.99.3/PKG-INFO +217 -0
  4. ebm-0.99.3/README.md +177 -0
  5. ebm-0.99.3/ebm/__init__.py +0 -0
  6. ebm-0.99.3/ebm/__main__.py +152 -0
  7. ebm-0.99.3/ebm/__version__.py +1 -0
  8. ebm-0.99.3/ebm/cmd/__init__.py +0 -0
  9. ebm-0.99.3/ebm/cmd/calibrate.py +83 -0
  10. ebm-0.99.3/ebm/cmd/calibrate_excel_com_io.py +128 -0
  11. ebm-0.99.3/ebm/cmd/heating_systems_by_year.py +18 -0
  12. ebm-0.99.3/ebm/cmd/helpers.py +134 -0
  13. ebm-0.99.3/ebm/cmd/initialize.py +167 -0
  14. ebm-0.99.3/ebm/cmd/migrate.py +92 -0
  15. ebm-0.99.3/ebm/cmd/pipeline.py +227 -0
  16. ebm-0.99.3/ebm/cmd/prepare_main.py +174 -0
  17. ebm-0.99.3/ebm/cmd/result_handler.py +272 -0
  18. ebm-0.99.3/ebm/cmd/run_calculation.py +221 -0
  19. ebm-0.99.3/ebm/data/area.csv +92 -0
  20. ebm-0.99.3/ebm/data/area_new_residential_buildings.csv +3 -0
  21. ebm-0.99.3/ebm/data/area_per_person.csv +12 -0
  22. ebm-0.99.3/ebm/data/building_code_parameters.csv +9 -0
  23. ebm-0.99.3/ebm/data/energy_need_behaviour_factor.csv +6 -0
  24. ebm-0.99.3/ebm/data/energy_need_improvements.csv +7 -0
  25. ebm-0.99.3/ebm/data/energy_need_original_condition.csv +534 -0
  26. ebm-0.99.3/ebm/data/heating_system_efficiencies.csv +13 -0
  27. ebm-0.99.3/ebm/data/heating_system_forecast.csv +9 -0
  28. ebm-0.99.3/ebm/data/heating_system_initial_shares.csv +1113 -0
  29. ebm-0.99.3/ebm/data/holiday_home_energy_consumption.csv +24 -0
  30. ebm-0.99.3/ebm/data/holiday_home_stock.csv +25 -0
  31. ebm-0.99.3/ebm/data/improvement_building_upgrade.csv +9 -0
  32. ebm-0.99.3/ebm/data/new_buildings_residential.csv +32 -0
  33. ebm-0.99.3/ebm/data/population_forecast.csv +51 -0
  34. ebm-0.99.3/ebm/data/s_curve.csv +40 -0
  35. ebm-0.99.3/ebm/energy_consumption.py +307 -0
  36. ebm-0.99.3/ebm/extractors.py +115 -0
  37. ebm-0.99.3/ebm/heating_system_forecast.py +472 -0
  38. ebm-0.99.3/ebm/holiday_home_energy.py +341 -0
  39. ebm-0.99.3/ebm/migrations.py +224 -0
  40. ebm-0.99.3/ebm/model/__init__.py +0 -0
  41. ebm-0.99.3/ebm/model/area.py +403 -0
  42. ebm-0.99.3/ebm/model/bema.py +149 -0
  43. ebm-0.99.3/ebm/model/building_category.py +150 -0
  44. ebm-0.99.3/ebm/model/building_condition.py +78 -0
  45. ebm-0.99.3/ebm/model/calibrate_energy_requirements.py +84 -0
  46. ebm-0.99.3/ebm/model/calibrate_heating_systems.py +180 -0
  47. ebm-0.99.3/ebm/model/column_operations.py +157 -0
  48. ebm-0.99.3/ebm/model/construction.py +827 -0
  49. ebm-0.99.3/ebm/model/data_classes.py +223 -0
  50. ebm-0.99.3/ebm/model/database_manager.py +410 -0
  51. ebm-0.99.3/ebm/model/dataframemodels.py +115 -0
  52. ebm-0.99.3/ebm/model/defaults.py +30 -0
  53. ebm-0.99.3/ebm/model/energy_need.py +6 -0
  54. ebm-0.99.3/ebm/model/energy_need_filter.py +182 -0
  55. ebm-0.99.3/ebm/model/energy_purpose.py +115 -0
  56. ebm-0.99.3/ebm/model/energy_requirement.py +353 -0
  57. ebm-0.99.3/ebm/model/energy_use.py +202 -0
  58. ebm-0.99.3/ebm/model/enums.py +8 -0
  59. ebm-0.99.3/ebm/model/exceptions.py +4 -0
  60. ebm-0.99.3/ebm/model/file_handler.py +388 -0
  61. ebm-0.99.3/ebm/model/filter_scurve_params.py +83 -0
  62. ebm-0.99.3/ebm/model/filter_tek.py +152 -0
  63. ebm-0.99.3/ebm/model/heat_pump.py +53 -0
  64. ebm-0.99.3/ebm/model/heating_systems.py +20 -0
  65. ebm-0.99.3/ebm/model/heating_systems_parameter.py +17 -0
  66. ebm-0.99.3/ebm/model/heating_systems_projection.py +3 -0
  67. ebm-0.99.3/ebm/model/heating_systems_share.py +28 -0
  68. ebm-0.99.3/ebm/model/scurve.py +224 -0
  69. ebm-0.99.3/ebm/model/tek.py +1 -0
  70. ebm-0.99.3/ebm/s_curve.py +515 -0
  71. ebm-0.99.3/ebm/services/__init__.py +0 -0
  72. ebm-0.99.3/ebm/services/calibration_writer.py +262 -0
  73. ebm-0.99.3/ebm/services/console.py +106 -0
  74. ebm-0.99.3/ebm/services/excel_loader.py +66 -0
  75. ebm-0.99.3/ebm/services/files.py +38 -0
  76. ebm-0.99.3/ebm/services/spreadsheet.py +289 -0
  77. ebm-0.99.3/ebm/temp_calc.py +99 -0
  78. ebm-0.99.3/ebm/validators.py +565 -0
  79. ebm-0.99.3/ebm.egg-info/PKG-INFO +217 -0
  80. ebm-0.99.3/ebm.egg-info/SOURCES.txt +86 -0
  81. ebm-0.99.3/ebm.egg-info/dependency_links.txt +1 -0
  82. ebm-0.99.3/ebm.egg-info/entry_points.txt +3 -0
  83. ebm-0.99.3/ebm.egg-info/requires.txt +34 -0
  84. ebm-0.99.3/ebm.egg-info/top_level.txt +1 -0
  85. ebm-0.99.3/pyproject.toml +50 -0
  86. ebm-0.99.3/setup.cfg +4 -0
  87. ebm-0.99.3/tests/test_energy_need_filter.py +59 -0
  88. ebm-0.99.3/tests/test_s_curve.py +65 -0
ebm-0.99.3/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Norges vassdrags- og energidirektorat
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
ebm-0.99.3/MANIFEST.in ADDED
@@ -0,0 +1,2 @@
1
+ include ebm/data/*.csv
2
+ recursive-include ebm *.py
ebm-0.99.3/PKG-INFO ADDED
@@ -0,0 +1,217 @@
1
+ Metadata-Version: 2.4
2
+ Name: ebm
3
+ Version: 0.99.3
4
+ Summary: A project for energy usage modeling
5
+ Author-email: Lars Petrusson <lfep@nve.no>, Ketil Nordstad <kenord@nve.no>
6
+ Project-URL: Homepage, https://www.nve.no/
7
+ Project-URL: Documentation, https://nve.github.io/ebm-docs
8
+ Project-URL: Repository, https://github.com/NVE/ebm
9
+ Requires-Python: >=3.11
10
+ Description-Content-Type: text/markdown
11
+ License-File: LICENSE
12
+ Requires-Dist: loguru
13
+ Requires-Dist: numpy
14
+ Requires-Dist: openpyxl
15
+ Requires-Dist: pandas
16
+ Requires-Dist: python-dotenv
17
+ Requires-Dist: rich
18
+ Requires-Dist: pandera
19
+ Requires-Dist: XlsxWriter
20
+ Requires-Dist: pywin32; platform_system == "Windows"
21
+ Provides-Extra: test
22
+ Requires-Dist: pytest; extra == "test"
23
+ Provides-Extra: build
24
+ Requires-Dist: build; extra == "build"
25
+ Provides-Extra: dev
26
+ Requires-Dist: ebm[test]; extra == "dev"
27
+ Requires-Dist: ebm[build]; extra == "dev"
28
+ Requires-Dist: ruff; extra == "dev"
29
+ Requires-Dist: ruff-lsp; extra == "dev"
30
+ Provides-Extra: docs
31
+ Requires-Dist: sphinx; extra == "docs"
32
+ Requires-Dist: sphinx-rtd-theme; extra == "docs"
33
+ Requires-Dist: sphinx-autodoc-typehints; extra == "docs"
34
+ Requires-Dist: numpydoc; extra == "docs"
35
+ Requires-Dist: sphinx-tabs; extra == "docs"
36
+ Provides-Extra: tqdm
37
+ Requires-Dist: tqdm; extra == "tqdm"
38
+ Requires-Dist: XlsxWriter; extra == "tqdm"
39
+ Dynamic: license-file
40
+
41
+ # Introduction
42
+
43
+ EBM is a model used by the Norwegian Water Resources and Energy Directorates (NVE) to forecast energy use in the
44
+ building stock. EBM is an open-source model developed and managed by NVE. The model allows the user to analyze how
45
+ demographic trends and policy instruments impact the yearly energy use on a national and regional level. Energy use is
46
+ estimated by a bottom- up approach, based on the building stock floor area, energy need and distribution of heating
47
+ systems. The mathematical model is implemented in Python, with input and output files in excel or csv.
48
+
49
+
50
+ ![ebm-in-windows-terminal.png](docs/source/_static/getting_started/ebm-in-windows-terminal.png)
51
+
52
+
53
+ # Getting Started
54
+
55
+ ## More information
56
+
57
+
58
+ - [Full documentation found here](https://nve.github.io/ebm-docs/index.html)
59
+ - [Model description](https://nve.github.io/ebm-docs/model_description/index.html)
60
+ - [Limitations](https://nve.github.io/ebm-docs/limitations.html)
61
+ - [Getting started](https://nve.github.io/ebm-docs/user_guide/getting_started.html)
62
+ - [Troubleshooting](https://nve.github.io/ebm-docs/user_guide/troubleshooting.html)
63
+
64
+
65
+ ## 1. Installation process
66
+
67
+ Please refer to [getting started](https://nve.github.io/ebm-docs/user_guide/getting_started.html) for information on
68
+ how ti install EBM as a user.
69
+
70
+ Open a terminal application and navigate to wherever you want to do work.
71
+
72
+ ### Make a python virtual environment (optional)
73
+
74
+ While optional, it is always recommended to install and run python modules in a discrete virtual environment. To create a
75
+ new python virtual environment (venv) type the following in a terminal.
76
+
77
+ `python -m venv venv`
78
+
79
+ ## Activate virtual environment
80
+ To use your venv you need to activate it
81
+ ### Using powershell
82
+ (Your command prompt starts with PS)
83
+ `\venv\Scripts\active.ps1`
84
+
85
+ ### Using cmd
86
+ (Your command prompt starts with C:\ where C is any letter from A-Z)
87
+ `\venv\Scripts\active.bat`
88
+
89
+ ### GNU/Linux and macOS
90
+
91
+ `source venv/bin/active`
92
+
93
+ More information on [Python virtual environments](https://realpython.com/python-virtual-environments-a-primer/)
94
+
95
+
96
+ ## Install EBM
97
+
98
+ There are two options. **Install from PyPI** (Python package index) or install **clone the github repository**.
99
+
100
+ ### Install from PyPI
101
+
102
+ `python -m pip install ebm`
103
+
104
+ You can now run the model as a module `python -m ebm` or as a program `ebm`
105
+
106
+ Please refer to [getting started](https://nve.github.io/ebm-docs/user_guide/getting_started.html) for more information on how the model works.
107
+
108
+
109
+ ### Clone the github repository
110
+
111
+ `git clone https://github.com/NVE/ebm`
112
+
113
+ `cd ebm`
114
+
115
+ Make sure your current working directory is the EBM root.
116
+
117
+ ```
118
+ python -m pip install -e .
119
+
120
+ ```
121
+
122
+ The command will install install all dependencies and ebm as an editable module.
123
+
124
+
125
+
126
+ ## 2. Software dependencies
127
+ - pandas
128
+ - loguru
129
+ - openpyxl
130
+ - pandera
131
+
132
+ Dependecies will be automatically installed when you install the package as described under Installation process.
133
+ See also [requirements.txt](requirements.txt)
134
+
135
+ ## 3. Run the model
136
+
137
+ There are multiple ways to run the program. Listed bellow is running as a standalone program and running as a module. If
138
+ running as a program fails due to security restriction, you might be able to use the module approach instead.
139
+
140
+ See also [Running as code](#running-as-code)
141
+
142
+
143
+ ### Running as a module
144
+
145
+ ```cmd
146
+ python3 -m ebm
147
+ ```
148
+
149
+ By default, the results will be written to the subdirectory `output`
150
+
151
+ For more information use `--help`
152
+
153
+ `python -m ebm --help`
154
+
155
+ ```shell
156
+ usage: ebm [-h] [--version] [--debug] [--categories [CATEGORIES ...]] [--input [INPUT]] [--force] [--open]
157
+ [--csv-delimiter CSV_DELIMITER] [--create-input] [--horizontal-years]
158
+ [{area-forecast,energy-requirements,heating-systems,energy-use}] [output_file]
159
+
160
+ Calculate EBM energy use 1.2.15
161
+
162
+ positional arguments:
163
+ {area-forecast,energy-requirements,heating-systems,energy-use}
164
+
165
+ The calculation step you want to run. The steps are sequential. Any prerequisite to the chosen step will run
166
+ automatically.
167
+ output_file The location of the file you want to be written. default: output\ebm_output.xlsx
168
+ If the file already exists the program will terminate without overwriting.
169
+ Use "-" to output to the console instead
170
+
171
+ options:
172
+ -h, --help show this help message and exit
173
+ --version, -v show program's version number and exit
174
+ --debug Run in debug mode. (Extra information written to stdout)
175
+ --categories [CATEGORIES ...], --building-categories [CATEGORIES ...], -c [CATEGORIES ...]
176
+
177
+ One or more of the following building categories:
178
+ house, apartment_block, kindergarten, school, university, office, retail, hotel, hospital, nursing_home, culture, sports, storage_repairs.
179
+ The default is to use all categories.
180
+ --input [INPUT], --input-directory [INPUT], -i [INPUT]
181
+ path to the directory with input files
182
+ --force, -f Write to <filename> even if it already exists
183
+ --open, -o Open output file(s) automatically when finished writing. (Usually Excel)
184
+ --csv-delimiter CSV_DELIMITER, --delimiter CSV_DELIMITER, -e CSV_DELIMITER
185
+ A single character to be used for separating columns when writing csv. Default: "," Special characters like ; should be quoted ";"
186
+ --create-input
187
+ Create input directory containing all required files in the current working directory
188
+ --horizontal-years, --horizontal, --horisontal
189
+ Show years horizontal (left to right)
190
+ ```
191
+
192
+
193
+ ### Running as code
194
+ ```python
195
+
196
+ from ebm.temp_calc import calculate_energy_use_wide
197
+ from ebm.model.file_handler import FileHandler
198
+
199
+ fh = FileHandler()
200
+ fh.create_missing_input_files()
201
+
202
+ df = calculate_energy_use_wide(ebm_input=fh.input_directory)
203
+ print(df)
204
+
205
+ ```
206
+
207
+ ## License
208
+ This project is licensed under the MIT License. You are free to use, modify, and distribute the software with proper attribution.
209
+
210
+ ## Contributing
211
+ We welcome contributions! Please refer to the Contributing Guide (CONTRIBUTING.md) for details on how to get started.
212
+
213
+ ## Documentation
214
+ Full documentation is available at the EBM User Guide: https://nve.github.io/ebm/
215
+
216
+
217
+
ebm-0.99.3/README.md ADDED
@@ -0,0 +1,177 @@
1
+ # Introduction
2
+
3
+ EBM is a model used by the Norwegian Water Resources and Energy Directorates (NVE) to forecast energy use in the
4
+ building stock. EBM is an open-source model developed and managed by NVE. The model allows the user to analyze how
5
+ demographic trends and policy instruments impact the yearly energy use on a national and regional level. Energy use is
6
+ estimated by a bottom- up approach, based on the building stock floor area, energy need and distribution of heating
7
+ systems. The mathematical model is implemented in Python, with input and output files in excel or csv.
8
+
9
+
10
+ ![ebm-in-windows-terminal.png](docs/source/_static/getting_started/ebm-in-windows-terminal.png)
11
+
12
+
13
+ # Getting Started
14
+
15
+ ## More information
16
+
17
+
18
+ - [Full documentation found here](https://nve.github.io/ebm-docs/index.html)
19
+ - [Model description](https://nve.github.io/ebm-docs/model_description/index.html)
20
+ - [Limitations](https://nve.github.io/ebm-docs/limitations.html)
21
+ - [Getting started](https://nve.github.io/ebm-docs/user_guide/getting_started.html)
22
+ - [Troubleshooting](https://nve.github.io/ebm-docs/user_guide/troubleshooting.html)
23
+
24
+
25
+ ## 1. Installation process
26
+
27
+ Please refer to [getting started](https://nve.github.io/ebm-docs/user_guide/getting_started.html) for information on
28
+ how ti install EBM as a user.
29
+
30
+ Open a terminal application and navigate to wherever you want to do work.
31
+
32
+ ### Make a python virtual environment (optional)
33
+
34
+ While optional, it is always recommended to install and run python modules in a discrete virtual environment. To create a
35
+ new python virtual environment (venv) type the following in a terminal.
36
+
37
+ `python -m venv venv`
38
+
39
+ ## Activate virtual environment
40
+ To use your venv you need to activate it
41
+ ### Using powershell
42
+ (Your command prompt starts with PS)
43
+ `\venv\Scripts\active.ps1`
44
+
45
+ ### Using cmd
46
+ (Your command prompt starts with C:\ where C is any letter from A-Z)
47
+ `\venv\Scripts\active.bat`
48
+
49
+ ### GNU/Linux and macOS
50
+
51
+ `source venv/bin/active`
52
+
53
+ More information on [Python virtual environments](https://realpython.com/python-virtual-environments-a-primer/)
54
+
55
+
56
+ ## Install EBM
57
+
58
+ There are two options. **Install from PyPI** (Python package index) or install **clone the github repository**.
59
+
60
+ ### Install from PyPI
61
+
62
+ `python -m pip install ebm`
63
+
64
+ You can now run the model as a module `python -m ebm` or as a program `ebm`
65
+
66
+ Please refer to [getting started](https://nve.github.io/ebm-docs/user_guide/getting_started.html) for more information on how the model works.
67
+
68
+
69
+ ### Clone the github repository
70
+
71
+ `git clone https://github.com/NVE/ebm`
72
+
73
+ `cd ebm`
74
+
75
+ Make sure your current working directory is the EBM root.
76
+
77
+ ```
78
+ python -m pip install -e .
79
+
80
+ ```
81
+
82
+ The command will install install all dependencies and ebm as an editable module.
83
+
84
+
85
+
86
+ ## 2. Software dependencies
87
+ - pandas
88
+ - loguru
89
+ - openpyxl
90
+ - pandera
91
+
92
+ Dependecies will be automatically installed when you install the package as described under Installation process.
93
+ See also [requirements.txt](requirements.txt)
94
+
95
+ ## 3. Run the model
96
+
97
+ There are multiple ways to run the program. Listed bellow is running as a standalone program and running as a module. If
98
+ running as a program fails due to security restriction, you might be able to use the module approach instead.
99
+
100
+ See also [Running as code](#running-as-code)
101
+
102
+
103
+ ### Running as a module
104
+
105
+ ```cmd
106
+ python3 -m ebm
107
+ ```
108
+
109
+ By default, the results will be written to the subdirectory `output`
110
+
111
+ For more information use `--help`
112
+
113
+ `python -m ebm --help`
114
+
115
+ ```shell
116
+ usage: ebm [-h] [--version] [--debug] [--categories [CATEGORIES ...]] [--input [INPUT]] [--force] [--open]
117
+ [--csv-delimiter CSV_DELIMITER] [--create-input] [--horizontal-years]
118
+ [{area-forecast,energy-requirements,heating-systems,energy-use}] [output_file]
119
+
120
+ Calculate EBM energy use 1.2.15
121
+
122
+ positional arguments:
123
+ {area-forecast,energy-requirements,heating-systems,energy-use}
124
+
125
+ The calculation step you want to run. The steps are sequential. Any prerequisite to the chosen step will run
126
+ automatically.
127
+ output_file The location of the file you want to be written. default: output\ebm_output.xlsx
128
+ If the file already exists the program will terminate without overwriting.
129
+ Use "-" to output to the console instead
130
+
131
+ options:
132
+ -h, --help show this help message and exit
133
+ --version, -v show program's version number and exit
134
+ --debug Run in debug mode. (Extra information written to stdout)
135
+ --categories [CATEGORIES ...], --building-categories [CATEGORIES ...], -c [CATEGORIES ...]
136
+
137
+ One or more of the following building categories:
138
+ house, apartment_block, kindergarten, school, university, office, retail, hotel, hospital, nursing_home, culture, sports, storage_repairs.
139
+ The default is to use all categories.
140
+ --input [INPUT], --input-directory [INPUT], -i [INPUT]
141
+ path to the directory with input files
142
+ --force, -f Write to <filename> even if it already exists
143
+ --open, -o Open output file(s) automatically when finished writing. (Usually Excel)
144
+ --csv-delimiter CSV_DELIMITER, --delimiter CSV_DELIMITER, -e CSV_DELIMITER
145
+ A single character to be used for separating columns when writing csv. Default: "," Special characters like ; should be quoted ";"
146
+ --create-input
147
+ Create input directory containing all required files in the current working directory
148
+ --horizontal-years, --horizontal, --horisontal
149
+ Show years horizontal (left to right)
150
+ ```
151
+
152
+
153
+ ### Running as code
154
+ ```python
155
+
156
+ from ebm.temp_calc import calculate_energy_use_wide
157
+ from ebm.model.file_handler import FileHandler
158
+
159
+ fh = FileHandler()
160
+ fh.create_missing_input_files()
161
+
162
+ df = calculate_energy_use_wide(ebm_input=fh.input_directory)
163
+ print(df)
164
+
165
+ ```
166
+
167
+ ## License
168
+ This project is licensed under the MIT License. You are free to use, modify, and distribute the software with proper attribution.
169
+
170
+ ## Contributing
171
+ We welcome contributions! Please refer to the Contributing Guide (CONTRIBUTING.md) for details on how to get started.
172
+
173
+ ## Documentation
174
+ Full documentation is available at the EBM User Guide: https://nve.github.io/ebm/
175
+
176
+
177
+
File without changes
@@ -0,0 +1,152 @@
1
+ """EBM start from where when running as a script or module"""
2
+ import os
3
+ import pathlib
4
+ import sys
5
+
6
+ import pandas as pd
7
+ from loguru import logger
8
+
9
+ from ebm.cmd import prepare_main
10
+ from ebm.cmd.helpers import configure_json_log, configure_loglevel, load_environment_from_dotenv
11
+ from ebm.cmd.initialize import create_output_directory, init
12
+ from ebm.cmd.migrate import migrate_directories
13
+ from ebm.cmd.pipeline import export_energy_model_reports
14
+ from ebm.cmd.result_handler import EbmDefaultHandler, append_result, transform_model_to_horizontal
15
+ from ebm.cmd.run_calculation import validate_years
16
+ from ebm.model.building_category import BuildingCategory
17
+ from ebm.model.database_manager import DatabaseManager
18
+ from ebm.model.enums import ReturnCode
19
+ from ebm.model.file_handler import FileHandler
20
+
21
+ df = None
22
+
23
+
24
+ def main() -> tuple[ReturnCode, pd.DataFrame | None]:
25
+ """
26
+ Execute the EBM module as a script.
27
+
28
+ This function serves as the entry point for the script. It handles argument parsing,
29
+ initializes necessary components, and orchestrates the main workflow of the script.
30
+
31
+ Returns
32
+ -------
33
+ exit code : tuple[ReturnCode, pd.DataFrame]
34
+ zero when the program exits gracefully
35
+
36
+ """
37
+ load_environment_from_dotenv()
38
+ configure_loglevel(log_format=os.environ.get('LOG_FORMAT', '{level.icon} <level>{message}</level>'))
39
+ configure_json_log()
40
+
41
+ logger.debug(f'Starting {sys.executable} {__file__}')
42
+
43
+ program_name = 'ebm'
44
+ default_path = pathlib.Path('output/ebm_output.xlsx')
45
+
46
+ arguments = prepare_main.make_arguments(program_name, default_path)
47
+
48
+ # Make local variable from arguments for clarity
49
+ building_categories = [BuildingCategory.from_string(b_c) for b_c in arguments.categories]
50
+ if not building_categories:
51
+ building_categories = list(BuildingCategory)
52
+
53
+ # `;` Will normally be interpreted as line end when typed in a shell. If the
54
+ # delimiter is empty make the assumption that the user used ;. An empty delimiter is not valid anyway.
55
+ csv_delimiter = arguments.csv_delimiter if arguments.csv_delimiter else ';'
56
+
57
+ # Make sure everything is working as expected
58
+ model_years = validate_years(start_year=arguments.start_year, end_year=arguments.end_year)
59
+
60
+ input_directory = arguments.input
61
+ logger.info(f'Using data from "{input_directory}"')
62
+ database_manager = DatabaseManager(file_handler=FileHandler(directory=input_directory))
63
+
64
+ # Create input directory if requested
65
+ if arguments.create_input:
66
+ if init(database_manager.file_handler):
67
+ logger.success('Finished creating input files in {input_directory}',
68
+ input_directory=database_manager.file_handler.input_directory)
69
+ return ReturnCode.OK, None
70
+ # Exit with 0 for success. The assumption is that the user would like to review the input before proceeding.
71
+ return ReturnCode.MISSING_INPUT_FILES, None
72
+ if arguments.migrate:
73
+ migrate_directories([database_manager.file_handler.input_directory])
74
+ logger.success('Finished migration')
75
+ return ReturnCode.OK, None
76
+
77
+ missing_input_error = f"""
78
+ Use `<program name> --create-input --input={input_directory}` to create an input directory with the default input files
79
+ """.strip().replace('\n', ' ')
80
+
81
+ # Make sure all required files exists
82
+ try:
83
+ missing_files = database_manager.file_handler.check_for_missing_files()
84
+ if missing_files:
85
+ print(missing_input_error, file=sys.stderr)
86
+ return ReturnCode.MISSING_INPUT_FILES, None
87
+ except FileNotFoundError as file_not_found:
88
+ if str(file_not_found).startswith('Input Directory Not Found'):
89
+ logger.error(f'Input Directory "{input_directory}" Not Found')
90
+ print(missing_input_error, file=sys.stderr)
91
+ return ReturnCode.FILE_NOT_ACCESSIBLE, None
92
+
93
+ database_manager.file_handler.validate_input_files()
94
+
95
+ output_file = arguments.output_file
96
+ create_output_directory(filename=output_file)
97
+
98
+ output_file_return_code = prepare_main.check_output_file_status(output_file, arguments.force, default_path,
99
+ program_name)
100
+ if output_file_return_code!= ReturnCode.OK:
101
+ return output_file_return_code, None
102
+
103
+ step_choice = arguments.step
104
+
105
+ convert_result_to_horizontal: bool = arguments.horizontal_years
106
+
107
+ default_handler = EbmDefaultHandler()
108
+
109
+ model = None
110
+
111
+ files_to_open = [output_file]
112
+
113
+ if step_choice == 'energy-use':
114
+ output_directory = output_file if output_file.is_dir() else output_file.parent
115
+ files_to_open = export_energy_model_reports(model_years, database_manager, output_directory)
116
+ else:
117
+ model = default_handler.extract_model(model_years, building_categories, database_manager, step_choice)
118
+
119
+ if convert_result_to_horizontal and (step_choice in ['area-forecast', 'energy-requirements']) and output_file.suffix=='.xlsx':
120
+ sheet_name_prefix = 'area' if step_choice == 'area-forecast' else 'energy'
121
+ logger.debug(f'Transform heating {step_choice}')
122
+
123
+ df = transform_model_to_horizontal(model.reset_index())
124
+ append_result(output_file, df, f'{sheet_name_prefix} condition')
125
+
126
+ model = model.reset_index()
127
+ # Demolition should not be summed any further
128
+ model = model[model.building_condition!='demolition']
129
+ model['building_condition'] = 'all'
130
+ df = transform_model_to_horizontal(model)
131
+ append_result(output_file, df, f'{sheet_name_prefix} TEK')
132
+
133
+ model['building_code'] = 'all'
134
+ df = transform_model_to_horizontal(model)
135
+ append_result(output_file, df, f'{sheet_name_prefix} category')
136
+ logger.success('Wrote {filename}', filename=output_file)
137
+ else:
138
+ default_handler.write_tqdm_result(output_file, model, csv_delimiter)
139
+
140
+ for file_to_open in files_to_open:
141
+ if arguments.open or os.environ.get('EBM_ALWAYS_OPEN', 'FALSE').upper() == 'TRUE':
142
+ logger.info(f'Open {file_to_open}')
143
+ os.startfile(file_to_open, 'open')
144
+ else:
145
+ logger.debug(f'Finished {file_to_open}')
146
+
147
+ return ReturnCode.OK, model
148
+
149
+
150
+ if __name__ == '__main__':
151
+ exit_code, result = main()
152
+ df = result
@@ -0,0 +1 @@
1
+ version = "0.99.3"
File without changes
@@ -0,0 +1,83 @@
1
+ import pathlib
2
+
3
+ from loguru import logger
4
+ import pandas as pd
5
+
6
+ from dotenv import load_dotenv
7
+
8
+ from ebm.model.bema import map_sort_order
9
+
10
+ from ebm.model.calibrate_heating_systems import extract_area_forecast, extract_energy_requirements, \
11
+ extract_heating_systems
12
+ from ebm.model.data_classes import YearRange
13
+ from ebm.services.files import make_unique_path
14
+
15
+ CALIBRATION_YEAR = 2023
16
+
17
+ model_period = YearRange(2020, 2050)
18
+ start_year = model_period.start
19
+ end_year = model_period.end
20
+
21
+
22
+ def run_calibration(database_manager,
23
+ calibration_year,
24
+ area_forecast: pd.DataFrame = None,
25
+ write_to_output = False):
26
+ """
27
+
28
+ Parameters
29
+ ----------
30
+ database_manager : ebm.model.database_manager.DatabaseManager
31
+
32
+ Returns
33
+ -------
34
+ pandas.core.frame.DataFrame
35
+ """
36
+ load_dotenv(pathlib.Path('.env'))
37
+
38
+ input_directory = database_manager.file_handler.input_directory
39
+
40
+ logger.info(f'Using input directory "{input_directory}"')
41
+ logger.info('Extract area forecast')
42
+ area_forecast = extract_area_forecast(database_manager) if area_forecast is None else area_forecast
43
+ if write_to_output:
44
+ write_dataframe(area_forecast[area_forecast.year == calibration_year], 'area_forecast')
45
+
46
+ logger.info('Extract energy requirements')
47
+ energy_requirements = extract_energy_requirements(area_forecast, database_manager)
48
+ if write_to_output:
49
+ en_req = energy_requirements.xs(2023, level='year').reset_index().sort_values(
50
+ by='building_category', key=lambda x: x.map(map_sort_order))
51
+ write_dataframe(en_req, 'energy_requirements')
52
+ grouped = en_req[['building_category', 'm2', 'kwh_m2', 'energy_requirement']].groupby(
53
+ by=['building_category'], as_index=False).agg({'m2': 'first', 'kwh_m2': 'first', 'energy_requirement': 'sum'})
54
+ grouped = grouped.sort_values(by='building_category', key=lambda x: x.map(map_sort_order))
55
+ write_dataframe(grouped, 'energy_requirements_sum', sheet_name='sum')
56
+
57
+ logger.info('Extract heating systems')
58
+ heating_systems = extract_heating_systems(energy_requirements, database_manager)
59
+ if write_to_output:
60
+ write_dataframe(heating_systems.xs(2023, level='year'), 'heating_systems')
61
+
62
+
63
+ return heating_systems
64
+
65
+
66
+ def write_dataframe(df, name='dataframe', sheet_name='Sheet1'):
67
+ output_directory = pathlib.Path('output')
68
+ if output_directory.is_dir():
69
+ logger.debug(f'Writing {name} to file')
70
+ output_file = output_directory / f'{name}.xlsx'
71
+ output_file = make_unique_path(output_file)
72
+ df.to_excel(output_file, merge_cells=False, sheet_name=sheet_name)
73
+ logger.info(f'Wrote {name} to {output_file} ! {sheet_name if sheet_name!="Sheet1" else ""}')
74
+ else:
75
+ logger.warning(f'Cannot write to {output_directory}. Directory does not exists')
76
+
77
+
78
+ def main():
79
+ raise NotImplementedError('Running calibrate as a script is not supported')
80
+
81
+
82
+ if __name__ == '__main__':
83
+ main()