Skip to content

Commit 0253194

Browse files
committed
PR review improvements. Fix random data table information loss bug.
1 parent f9a377e commit 0253194

File tree

3 files changed

+242
-133
lines changed

3 files changed

+242
-133
lines changed

docs/creating/yaml.md

+233-120
Original file line numberDiff line numberDiff line change
@@ -215,147 +215,260 @@ To streamline data entry, any section can inherit common data from a `template`
215215

216216
If we want to set interest rate to `0.1` across all our technologies, we could define:
217217

218-
```yaml
219-
templates:
220-
interest_rate_setter:
221-
cost_interest_rate:
222-
data: 0.1
223-
index: monetary
224-
dims: costs
225-
techs:
226-
ccgt:
227-
template: interest_rate_setter
228-
...
229-
ac_transmission:
230-
template: interest_rate_setter
231-
...
232-
```
218+
=== "Using templates"
219+
220+
```yaml
221+
templates:
222+
interest_rate_setter:
223+
cost_interest_rate:
224+
data: 0.1
225+
index: monetary
226+
dims: costs
227+
techs:
228+
ccgt:
229+
flow_out_eff: 0.5
230+
template: interest_rate_setter
231+
ac_transmission:
232+
flow_out_eff: 0.98
233+
template: interest_rate_setter
234+
```
235+
=== "Without templates"
236+
237+
```yaml
238+
techs:
239+
ccgt:
240+
flow_out_eff: 0.5
241+
cost_interest_rate:
242+
data: 0.1
243+
index: monetary
244+
dims: costs
245+
ac_transmission:
246+
flow_out_eff: 0.98
247+
cost_interest_rate:
248+
data: 0.1
249+
index: monetary
250+
dims: costs
251+
```
233252

234253
??? example "Example 2: templates in nodes"
235254

236255
Similarly, if we want to allow the same technologies at all our nodes:
237256

238-
```yaml
239-
templates:
240-
standard_tech_list:
241-
techs: {ccgt, battery, demand_power} # (1)!
242-
nodes:
243-
region1:
244-
template: standard_tech_list
245-
...
246-
region2:
247-
template: standard_tech_list
248-
...
249-
...
250-
region100:
251-
template: standard_tech_list
252-
```
253-
254-
This YAML syntax is shortform for:
255-
256-
```yaml
257-
nodes:
258-
region1:
259-
techs:
260-
ccgt:
261-
battery:
262-
demand_power:
263-
...
264-
...
265-
```
257+
=== "Using templates"
258+
259+
```yaml
260+
templates:
261+
standard_tech_list:
262+
techs: {ccgt, battery, demand_power}
263+
nodes:
264+
region1:
265+
template: standard_tech_list
266+
latitude: 39
267+
longitude: -2
268+
region2:
269+
template: standard_tech_list
270+
latitude: 40
271+
longitude: 0
272+
```
273+
274+
=== "Without templates"
275+
276+
```yaml
277+
nodes:
278+
region1:
279+
techs:
280+
ccgt:
281+
battery:
282+
demand_power:
283+
latitude: 39
284+
longitude: -2
285+
region2:
286+
techs:
287+
ccgt:
288+
battery:
289+
demand_power:
290+
latitude: 40
291+
longitude: 0
292+
```
266293

267294
??? example "Example 3: templates in data tables"
268295

269-
Storing common options under the `templates` key is also useful for data tables, for example:
270-
271-
```yaml
272-
templates:
273-
common_data_options:
274-
rows: timesteps
275-
columns: nodes
276-
add_dims:
277-
parameters: source_use_max
278-
data_tables:
279-
pv_data:
280-
data: /path/to/pv_timeseries.csv
281-
template: common_data_options
282-
add_dims:
283-
techs: pv
284-
wind_data:
285-
data: /path/to/wind_timeseries.csv
286-
template: common_data_options
287-
add_dims:
288-
techs: wind
289-
hydro_data:
290-
data: /path/to/hydro_timeseries.csv
291-
template: common_data_options
292-
add_dims:
293-
techs: hydro
294-
```
296+
Storing common options under the `templates` key is also useful for data tables.
297+
298+
=== "Using templates"
299+
300+
```yaml
301+
templates:
302+
common_data_options:
303+
rows: timesteps
304+
columns: nodes
305+
add_dims:
306+
parameters: source_use_max
307+
data_tables:
308+
pv_data:
309+
data: /path/to/pv_timeseries.csv
310+
template: common_data_options
311+
add_dims:
312+
techs: pv
313+
wind_data:
314+
data: /path/to/wind_timeseries.csv
315+
template: common_data_options
316+
add_dims:
317+
techs: wind
318+
hydro_data:
319+
data: /path/to/hydro_timeseries.csv
320+
template: common_data_options
321+
add_dims:
322+
techs: hydro
323+
```
324+
=== "Without templates"
325+
326+
```yaml
327+
data_tables:
328+
pv_data:
329+
data: /path/to/pv_timeseries.csv
330+
rows: timesteps
331+
columns: nodes
332+
add_dims:
333+
parameters: source_use_max
334+
techs: pv
335+
wind_data:
336+
data: /path/to/wind_timeseries.csv
337+
rows: timesteps
338+
columns: nodes
339+
add_dims:
340+
parameters: source_use_max
341+
techs: wind
342+
hydro_data:
343+
data: /path/to/hydro_timeseries.csv
344+
rows: timesteps
345+
columns: nodes
346+
add_dims:
347+
parameters: source_use_max
348+
techs: hydro
349+
```
295350

296351
Inheritance chains can also be created.
297352
That is, templates can inherit from other templates.
298353

299354
??? example "Example 4: template inheritance chain"
300355

301-
```yaml
302-
templates:
303-
interest_rate_setter:
304-
cost_interest_rate:
305-
data: 0.1
306-
index: monetary
307-
dims: costs
308-
investment_cost_setter:
309-
template: interest_rate_setter
310-
cost_flow_cap:
311-
data: 100
312-
index: monetary
313-
dims: costs
314-
cost_area_use:
315-
data: 1
316-
index: monetary
317-
dims: costs
318-
techs:
319-
ccgt:
320-
template: investment_cost_setter
321-
...
322-
ac_transmission:
323-
template: interest_rate_setter
324-
...
325-
```
356+
A two-level template inheritance chain.
357+
358+
=== "Using templates"
359+
360+
```yaml
361+
templates:
362+
interest_rate_setter:
363+
cost_interest_rate:
364+
data: 0.1
365+
index: monetary
366+
dims: costs
367+
investment_cost_setter:
368+
template: interest_rate_setter
369+
cost_flow_cap:
370+
data: 100
371+
index: monetary
372+
dims: costs
373+
cost_area_use:
374+
data: 1
375+
index: monetary
376+
dims: costs
377+
techs:
378+
ccgt:
379+
template: investment_cost_setter
380+
flow_out_eff: 0.5
381+
ac_transmission:
382+
template: interest_rate_setter
383+
flow_out_eff: 0.98
384+
```
385+
386+
=== "Without templates"
326387

327-
Template properties can always be overridden by the inheriting component.
328-
That is, the 'local' value has priority over the inherited template value.
388+
```yaml
389+
techs:
390+
ccgt:
391+
cost_interest_rate:
392+
data: 0.1
393+
index: monetary
394+
dims: costs
395+
cost_flow_cap:
396+
data: 100
397+
index: monetary
398+
dims: costs
399+
cost_area_use:
400+
data: 1
401+
index: monetary
402+
dims: costs
403+
flow_out_eff: 0.5
404+
ac_transmission:
405+
cost_interest_rate:
406+
data: 0.1
407+
index: monetary
408+
dims: costs
409+
cost_flow_cap:
410+
data: 100
411+
index: monetary
412+
dims: costs
413+
cost_area_use:
414+
data: 1
415+
index: monetary
416+
dims: costs
417+
flow_out_eff: 0.98
418+
```
419+
420+
Template properties can always be overwritten by the inheriting component.
421+
That is, a 'local' value has priority over the template value.
329422
This can be useful to streamline setting costs for different technologies.
330423

331424
??? example "Example 5: overriding template values"
332425

333426
In this example, a technology overrides a single templated cost.
334427

335-
```yaml
336-
templates:
337-
interest_rate_setter:
338-
cost_interest_rate:
339-
data: 0.1
340-
index: monetary
341-
dims: costs
342-
investment_cost_setter:
343-
template: interest_rate_setter
344-
cost_interest_rate.data: 0.2 # this will replace `0.1` in the `interest_rate_setter`.
345-
cost_flow_cap:
346-
data: null
347-
index: monetary
348-
dims: costs
349-
cost_area_use:
350-
data: null
351-
index: monetary
352-
dims: costs
353-
techs:
354-
ccgt:
355-
template: investment_cost_setter
356-
cost_flow_cap.data: 100 # this will replace `null` in the `investment_cost_setter`.
357-
...
358-
```
428+
=== "Using templates"
429+
430+
```yaml
431+
templates:
432+
interest_rate_setter:
433+
cost_interest_rate:
434+
data: 0.1
435+
index: monetary
436+
dims: costs
437+
investment_cost_setter:
438+
template: interest_rate_setter
439+
cost_interest_rate.data: 0.2 # this will replace `0.1` in the `interest_rate_setter`.
440+
cost_flow_cap:
441+
data: null
442+
index: monetary
443+
dims: costs
444+
cost_area_use:
445+
data: null
446+
index: monetary
447+
dims: costs
448+
techs:
449+
ccgt:
450+
template: investment_cost_setter
451+
cost_flow_cap.data: 100 # this will replace `null` in the `investment_cost_setter`.
452+
```
453+
454+
=== "Without templates"
455+
456+
```yaml
457+
techs:
458+
ccgt:
459+
cost_interest_rate:
460+
data: 0.2
461+
index: monetary
462+
dims: costs
463+
cost_flow_cap:
464+
data: 100
465+
index: monetary
466+
dims: costs
467+
cost_area_use:
468+
data: null
469+
index: monetary
470+
dims: costs
471+
```
359472
360473
### Overriding one file with another
361474

src/calliope/preprocess/model_definition.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,9 @@ def _resolve_data(self, section, level: int = 0):
251251
template = AttrDict()
252252

253253
local = AttrDict()
254-
for key in section.keys() - {self.TEMPLATE_CALL, self.TEMPLATES_SECTION}:
255-
local[key] = self._resolve_data(section[key], level=level + 1)
254+
for key in section.keys():
255+
if key not in [self.TEMPLATE_CALL, self.TEMPLATES_SECTION]:
256+
local[key] = self._resolve_data(section[key], level=level + 1)
256257

257258
# Local values have priority.
258259
template.union(local, allow_override=True)

0 commit comments

Comments
 (0)