Skip to content

Commit 8a7a2fd

Browse files
committed
Adapt datapackage export for storage
1 parent 2c1ee53 commit 8a7a2fd

1 file changed

Lines changed: 65 additions & 19 deletions

File tree

app/projects/models/base_models.py

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ def export(self, bind_project_data=False):
318318
def to_datapackage(self, destination_path):
319319

320320
# Create a folder with a datapackage structure
321-
scenario_folder = destination_path / f"scenario_{self.id}"
321+
scenario_folder = destination_path / f"scenario_{self.name}".replace(" ", "_")
322322

323323
data_folder = scenario_folder / "data"
324324
elements_folder = data_folder / "elements"
@@ -338,9 +338,11 @@ def to_datapackage(self, destination_path):
338338

339339
# List all components of the scenario (except the busses)
340340
qs_assets = Asset.objects.filter(scenario=self)
341-
# List all distinct components' assettypes (or facade name)
342-
facade_names = qs_assets.distinct().values_list(
343-
"asset_type__asset_type", flat=True
341+
# List all distinct components' assettypes (or facade name) which are not children
342+
facade_names = (
343+
qs_assets.filter(parent_asset__isnull=True)
344+
.distinct()
345+
.values_list("asset_type__asset_type", flat=True)
344346
)
345347

346348
bus_resource_records = []
@@ -395,7 +397,7 @@ def to_datapackage(self, destination_path):
395397

396398
# creating datapackage.json metadata file at the root of the datapackage
397399
building.infer_metadata_from_data(
398-
package_name=f"scenario_{self.id}",
400+
package_name=f"scenario_{self.name}".replace(" ", "_"),
399401
path=scenario_folder,
400402
fk_targets=["project"],
401403
)
@@ -775,21 +777,66 @@ def to_datapackage(self):
775777
dp["project_data"] = self.scenario.project.name
776778
# to collect the timeseries used by the asset
777779
profile_resource_rec = {}
778-
for field in self.asset_type.visible_fields:
779-
value = getattr(self, field)
780-
# if the field is a candidate for a scalar/list
781-
if isinstance(value, str) and field != "name":
782-
value = json.loads(value)
783-
if isinstance(value, list):
784-
col = f"{self.name}__{field}"
785-
profile_resource_rec[col] = value
780+
781+
# Storage assets are the only one to have children (namely `capacity`, `charge` and `discharge`
782+
qs_children = Asset.objects.filter(parent_asset__id=self.id)
783+
if qs_children.exists():
784+
# Only keep the values from the capacity children asset of the storage
785+
capacity = qs_children.get(asset_type__asset_type="capacity")
786+
for attribute in [
787+
"capex_fix",
788+
"capex_var",
789+
"opex_fix",
790+
"opex_var",
791+
"lifetime",
792+
"crate",
793+
"efficiency",
794+
"soc_max",
795+
"soc_min",
796+
"maximum_capacity",
797+
"optimize_cap",
798+
"installed_capacity",
799+
"age_installed",
800+
"thermal_loss_rate", # only for hess
801+
"fixed_thermal_losses_relative", # only for hess
802+
"fixed_thermal_losses_absolute", # only for hess
803+
]:
804+
setattr(self, attribute, getattr(capacity, attribute))
805+
806+
if self.asset_type.asset_type != "hess":
807+
attributes = [
808+
f
809+
for f in AssetType.objects.get(asset_type="capacity").visible_fields
810+
if f
811+
not in (
812+
"thermal_loss_rate",
813+
"fixed_thermal_losses_relative",
814+
"fixed_thermal_losses_absolute",
815+
)
816+
]
817+
else:
818+
attributes = AssetType.objects.get(asset_type="capacity").visible_fields
819+
else:
820+
attributes = self.asset_type.visible_fields
821+
822+
for field in attributes:
823+
if (
824+
field != "dispatchable"
825+
): # TODO remove this when `dispatchable` not a visible field anymore
826+
value = getattr(self, field)
827+
# if the field is a candidate for a scalar/list
828+
if isinstance(value, str) and field != "name":
829+
value = json.loads(value)
830+
if isinstance(value, list):
831+
col = f"{self.name}__{field}"
832+
profile_resource_rec[col] = value
833+
value = col
834+
elif isinstance(value, Timeseries):
835+
col = value.name
836+
profile_resource_rec[col] = value.values
786837
value = col
787-
elif isinstance(value, Timeseries):
788-
col = value.name
789-
profile_resource_rec[col] = value.values
790-
value = col
791838

792-
dp[field] = value
839+
dp[field] = value
793840

794841
# to collect the bus(ses) used by the asset
795842
bus_resource_rec = []
@@ -807,7 +854,6 @@ def to_datapackage(self):
807854
qs_bus = self.connectionlink_set.filter(
808855
flow_direction=direction, asset_connection_port=port
809856
)
810-
# TODO use bus names for the resource column label according to a convention with the facade attributes
811857
if qs_bus.exists():
812858
connection = qs_bus.get()
813859
dp[field] = connection.bus.name

0 commit comments

Comments
 (0)