Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 76 additions & 106 deletions calliope_app/api/models/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,7 @@ def duplicate(self, model_id, pretty_name):
tech_param.save()
new_tech.update_calliope_pretty_name()
return new_tech


def update(self, form_data):
""" Update the Technology parameters stored in Tech_Param """
Expand Down Expand Up @@ -934,65 +935,45 @@ def _cplus_carriers(cls, technology, carriers, ratios):
def _add(cls, technology, data):
""" Add a new parameter to a technology """
for key, value_dict in data.items():
if (('year' in value_dict) & ('value' in value_dict)):
if all(field in value_dict for field in ['year', 'value', 'build_year_offset']):
years = value_dict['year']
values = value_dict['value']
num_records = np.min([len(years), len(values)])
build_year_offsets = value_dict['build_year_offset']
num_records = min(len(years), len(values), len(build_year_offsets))
new_objects = []
for i in range(num_records):
vals = str(values[i]).split('||')
new_objects.append(cls(
model_id=technology.model_id,
technology_id=technology.id,
year=years[i],
build_year_offset=build_year_offsets[i],
parameter_id=key,
value=ParamsManager.clean_str_val(vals[0]),
raw_value=vals[1] if len(vals) > 1 else vals[0]))
raw_value=vals[1] if len(vals) > 1 else vals[0]
))
cls.objects.bulk_create(new_objects)

@classmethod
def _edit(cls, technology, data):
""" Edit a technology's parameters """
if 'parameter' in data:
for key, value in data['parameter'].items():
vals = str(value).split('||')
cls.objects.filter(
model_id=technology.model_id,
technology_id=technology.id,
parameter_id=key).hard_delete()
cls.objects.create(
model_id=technology.model_id,
technology_id=technology.id,
parameter_id=key,
value=ParamsManager.clean_str_val(vals[0]),
raw_value=vals[1] if len(vals) > 1 else vals[0])
if 'timeseries' in data:
for key, value in data['timeseries'].items():
cls.objects.filter(
model_id=technology.model_id,
technology_id=technology.id,
parameter_id=key).hard_delete()
cls.objects.create(
model_id=technology.model_id,
technology_id=technology.id,
parameter_id=key,
value=ParamsManager.clean_str_val(value),
timeseries_meta_id=value,
timeseries=True)
if 'parameter_instance' in data:
instance_items = data['parameter_instance'].items()
for key, value_dict in instance_items:
parameter_instance = cls.objects.filter(
model_id=technology.model_id,
id=key)
if 'value' in value_dict:
vals = str(value_dict['value']).split('||')
parameter_instance.update(
value=ParamsManager.clean_str_val(vals[0]),
raw_value=vals[1] if len(vals) > 1 else vals[0])
if 'year' in value_dict:
parameter_instance.update(year=value_dict['year'])

for param_id, param_data in data['parameter_instance'].items():
tech_param = cls.objects.filter(id=param_id, technology=technology).first()
if tech_param:
if 'build_year_offset' in param_data:
tech_param.build_year_offset = param_data['build_year_offset']
if 'year' in param_data:
tech_param.year = param_data['year']
if 'value' in param_data:
value = param_data['value']
if '||' in value:
parts = value.split('||')
tech_param.value = parts[0].strip()
tech_param.raw_value = parts[1].strip() if len(parts) > 1 else parts[0].strip()
else:
tech_param.value = value
tech_param.raw_value = value
tech_param.save()

@classmethod
def _delete(cls, technology, data):
""" Delete a technology's parameters """
Expand All @@ -1009,7 +990,6 @@ def _delete(cls, technology, data):
model_id=technology.model_id,
id=key).hard_delete()


class Location(models.Model):
class Meta:
db_table = "location"
Expand Down Expand Up @@ -1068,7 +1048,6 @@ def __str__(self):
else:
return '%s | %s [%s]' % (self.location_1, self.technology,
self.technology.pretty_tag)

def update(self, form_data):
""" Update the Location Technology parameters
stored in Loc_Tech_Param """
Expand Down Expand Up @@ -1109,80 +1088,71 @@ class Meta:
def _add(cls, loc_tech, data):
""" Add a new parameter to a location technology """
for key, value_dict in data.items():
if (('year' in value_dict) & ('value' in value_dict)):
if all(field in value_dict for field in ['year', 'value', 'build_year_offset']):
years = value_dict['year']
values = value_dict['value']
num_records = np.min([len(years), len(values)])
build_year_offsets = value_dict['build_year_offset']
num_records = min(len(years), len(values), len(build_year_offsets))
new_objects = []
for i in range(num_records):
vals = str(values[i]).split('||')
value = str(values[i])
if '||' in value:
clean_value, raw_value = value.split('||')
else:
clean_value = raw_value = value

new_objects.append(cls(
model_id=loc_tech.model_id,
loc_tech_id=loc_tech.id,
model_id=loc_tech.model.id,
loc_tech=loc_tech,
year=years[i],
build_year_offset=build_year_offsets[i],
parameter_id=key,
value=ParamsManager.clean_str_val(vals[0]),
raw_value=vals[1] if len(vals) > 1 else vals[0]))
value=ParamsManager.clean_str_val(clean_value.strip()),
raw_value=raw_value.strip()
))
cls.objects.bulk_create(new_objects)

@classmethod
def _edit(cls, loc_tech, data):
""" Edit a location technology parameter """
if 'parameter' in data:
if 'parameter_instance' in data:
for param_id, param_data in data['parameter_instance'].items():
loc_tech_param = cls.objects.filter(id=param_id, loc_tech=loc_tech).first()
if loc_tech_param:
if 'build_year_offset' in param_data:
build_year_offset = param_data['build_year_offset']
loc_tech_param.build_year_offset = int(build_year_offset) if build_year_offset != '' else None
if 'year' in param_data:
loc_tech_param.year = param_data['year']
if 'value' in param_data:
value = param_data['value']
if '||' in value:
clean_value, raw_value = value.split('||')
else:
clean_value = raw_value = value
loc_tech_param.value = ParamsManager.clean_str_val(clean_value.strip())
loc_tech_param.raw_value = raw_value.strip()
loc_tech_param.save()
elif 'parameter' in data:
for key, value in data['parameter'].items():
vals = str(value).split('||')
cls.objects.filter(
model_id=loc_tech.model_id,
loc_tech_id=loc_tech.id,
parameter_id=key).hard_delete()
cls.objects.create(
model_id=loc_tech.model_id,
loc_tech_id=loc_tech.id,
parameter_id=key,
value=ParamsManager.clean_str_val(vals[0]),
raw_value=vals[1] if len(vals) > 1 else vals[0])
if 'timeseries' in data:
for key, value in data['timeseries'].items():
cls.objects.filter(
model_id=loc_tech.model_id,
loc_tech_id=loc_tech.id,
parameter_id=key).hard_delete()
cls.objects.create(
model_id=loc_tech.model_id,
loc_tech_id=loc_tech.id,
if '||' in value:
clean_value, raw_value = value.split('||')
else:
clean_value = raw_value = value
cls.objects.update_or_create(
loc_tech=loc_tech,
parameter_id=key,
value=ParamsManager.clean_str_val(value),
timeseries_meta_id=value,
timeseries=True)
if 'parameter_instance' in data:
instance_items = data['parameter_instance'].items()
for key, value_dict in instance_items:
parameter_instance = cls.objects.filter(
model_id=loc_tech.model_id,
id=key)
if 'value' in value_dict:
vals = str(value_dict['value']).split('||')
parameter_instance.update(
value=ParamsManager.clean_str_val(vals[0]),
raw_value=vals[1] if len(vals) > 1 else vals[0])
if 'year' in value_dict:
parameter_instance.update(year=value_dict['year'])

defaults={
'value': ParamsManager.clean_str_val(clean_value.strip()),
'raw_value': raw_value.strip(),
'model': loc_tech.model
}
)
@classmethod
def _delete(cls, loc_tech, data):
""" Delete a location technology parameter """
if 'parameter' in data:
for key, value in data['parameter'].items():
cls.objects.filter(
model_id=loc_tech.model_id,
loc_tech_id=loc_tech.id,
parameter_id=key).hard_delete()
elif 'parameter_instance' in data:
instance_items = data['parameter_instance'].items()
for key, value in instance_items:
cls.objects.filter(
model_id=loc_tech.model_id,
id=key).hard_delete()
param_ids = data.get('parameter_instance', [])
for param_id in param_ids:
print(f"Deleting Loc_Tech_Param with id: {param_id}")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove print statement, use logging if need.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still workshopping this today... will remove

cls.objects.filter(id=param_id, loc_tech=loc_tech).delete()



class Scenario(models.Model):
Expand Down
13 changes: 9 additions & 4 deletions calliope_app/client/component_views/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from django.utils.timezone import make_aware

from api.models.configuration import Scenario_Param, Scenario, Scenario_Loc_Tech, \
Timeseries_Meta, ParamsManager, Model, User_File, Carrier, Tech_Param
Timeseries_Meta, ParamsManager, Model, User_File, Carrier, Tech_Param, Loc_Tech_Param
from api.utils import get_cols_from_csv


Expand Down Expand Up @@ -158,7 +158,9 @@ def all_tech_params(request):

for param in parameters:
param['raw_units'] = param['units']

tech_param = Loc_Tech_Param.objects.filter(id=param['id']).first()
build_year_offset = tech_param.build_year_offset if tech_param else None
param['build_year_offset'] = build_year_offset
timeseries = Timeseries_Meta.objects.filter(model=model, failure=False,
is_uploading=False)

Expand Down Expand Up @@ -273,7 +275,6 @@ def all_loc_tech_params(request):
parameters = ParamsManager.all_loc_tech_params(loc_tech)
timeseries = Timeseries_Meta.objects.filter(model=model, failure=False,
is_uploading=False)

units_in_ids= ParamsManager.get_tagged_params('units_in')
units_out_ids= ParamsManager.get_tagged_params('units_out')

Expand All @@ -294,9 +295,13 @@ def all_loc_tech_params(request):

for param in parameters:
param['raw_units'] = param['units']

loc_tech_param = Loc_Tech_Param.objects.filter(id=param['id']).first()
build_year_offset = loc_tech_param.build_year_offset if loc_tech_param else None
param['build_year_offset'] = build_year_offset
print(parameters)
emissions = ParamsManager.emission_categories()
# Parameters Table
print(loc_tech)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove print statements, use logging if need.

context = {
"emissions": emissions,
"loc_tech": loc_tech,
Expand Down
Loading