Skip to content

Commit

Permalink
Fixed bug in write_shp
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Eggimann committed Nov 14, 2017
1 parent 32c872e commit 204ce27
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 39 deletions.
1 change: 0 additions & 1 deletion energy_demand/assumptions/param_assumptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,6 @@ def load_param_assump(paths, assumptions):
# Create parameter file only with fully descried parameters
# and write to yaml file
# -----------------------
#write_data.write_yaml_param(paths['yaml_parameters'], assumptions)
write_data.write_yaml_param_complete(paths['yaml_parameters_complete'], assumptions)

assumptions['testing'] = True
Expand Down
5 changes: 3 additions & 2 deletions energy_demand/geography/write_shp.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ def write_result_shapefile(lad_geometry_shp, out_shape, field_names, csv_results
# No results
result_csv = 0
logging.warning(
"No value found for region '%s' in merging shapefile results", geo_code)
"No result value for region '%s' in joining shapefile", geo_code)

rec.append(result_csv)
# Add specific fuel result
rec.append(result_csv)

# Add the modified record to the new shapefile
w.records.append(rec)
Expand Down
17 changes: 12 additions & 5 deletions energy_demand/read_and_plot_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,31 @@
from energy_demand.read_write import data_loader, read_data, write_data
from energy_demand.basic import date_prop
from energy_demand.plotting import plotting_results
from energy_demand.basic import logger_setup
from energy_demand.basic import logger_setup, basic_functions

def main(local_data_path):
def main(path_data_energy_demand):
"""Read in all results and plot PDFs
Arguments
----------
local_data_path : str
path_data_energy_demand : str
Path to results
"""
logger_setup.set_up_logger(os.path.join(local_data_path, "logging_plotting.log"))

# Set up logger
logger_setup.set_up_logger(os.path.join(path_data_energy_demand, "logging_plotting.log"))

# ------------------
# Load necessary inputs for read in
# ------------------
data = {}
data['local_paths'] = data_loader.load_local_paths(local_data_path)
data['local_paths'] = data_loader.load_local_paths(path_data_energy_demand)
data['lookups'] = data_loader.load_basic_lookups()

# Del previous visulations and shapefiles
basic_functions.del_previous_setup(data['local_paths']['data_results_PDF'])
basic_functions.del_previous_setup(data['local_paths']['data_results_shapefiles'])

# Simulation information is read in from .ini file for results
data['sim_param'], data['enduses'], data['assumptions'], data['reg_nrs'], data['lu_reg'] = data_loader.load_sim_param_ini(
data['local_paths']['data_results'])
Expand Down
58 changes: 40 additions & 18 deletions energy_demand/read_write/write_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ def create_shp_results(results_container, paths, lookups, lu_reg):
Arguments
---------
results_container
paths
lookups
lu_reg
results_container : dict
Data container
paths : dict
Paths
lookups : dict
Lookups
lu_reg : list
Region in a list with order how they are stored in result array
"""
logging.info("... create result shapefiles")

Expand All @@ -41,7 +44,7 @@ def create_shp_results(results_container, paths, lookups, lu_reg):
for fueltype in range(lookups['fueltypes_nr']):
for year in results_container['load_factors_y'].keys():

field_names.append('lp_max_y_{}_{}'.format(year, fueltype))
field_names.append('y_{}_{}'.format(year, fueltype))
csv_results.append(
basic_functions.array_to_dict(
results_container['load_factors_y'][year][fueltype], lu_reg))
Expand All @@ -53,17 +56,31 @@ def create_shp_results(results_container, paths, lookups, lu_reg):
csv_results)

# ------------------------------------
# Create shapefile with
# Create shapefile with
# ------------------------------------
# Iterate fueltpyes and years and add as attributes
for fueltype in range(lookups['fueltypes_nr']):
for year in results_container['results_every_year'].keys():

field_names.append('y_{}_{}'.format(year, fueltype))

# Calculate yearly sum
yearly_sum = np.sum(results_container['results_every_year'][year][fueltype], axis=1)
csv_results.append(basic_functions.array_to_dict(yearly_sum, lu_reg))

write_shp.write_result_shapefile(
paths['lad_shapefile_2011'],
os.path.join(path_out_shapefile, 'fuel_y'),
field_names,
csv_results)
# ------------------------------------
# Create shapefile with
# Create shapefile with
# ------------------------------------

# ------------------------------------
# Create shapefile with
# Create shapefile with
# ------------------------------------

prnt(".")
logging.info("... finished generating shapefiles")

def dump(data, file_path):
Expand All @@ -81,6 +98,18 @@ def dump(data, file_path):

def write_yaml_param_complete(path_yaml, dict_to_dump):
"""Write all assumption parameters to YAML
Arguments
----------
path_yaml : str
Path where yaml file is saved
dict_to_dump : dict
Dict which is written to YAML
Returns
-------
#
#TODO :ORDER
"""
list_to_dump_complete = []
Expand All @@ -103,14 +132,7 @@ def write_yaml_param_complete(path_yaml, dict_to_dump):

# Dump list
dump(list_to_dump_complete, path_yaml)
return

def write_yaml_param(path_yaml, dict_to_dump):
"""Write all assumption parameters to YAML

"""
with open(path_yaml, 'w') as file_handle:
yaml.dump(dict_to_dump, file_handle)
return

def write_simulation_inifile(path, sim_param, enduses, assumptions, reg_nrs, lu_reg):
Expand All @@ -127,7 +149,7 @@ def write_simulation_inifile(path, sim_param, enduses, assumptions, reg_nrs, lu_

config = configparser.ConfigParser()

config.add_section('SIM_PARAM')
config.add_section('SIM_PARAM')
config['SIM_PARAM']['reg_nrs'] = str(reg_nrs)
config['SIM_PARAM']['base_yr'] = str(sim_param['base_yr'])

Expand Down
12 changes: 5 additions & 7 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
from energy_demand.scripts.init_scripts import scenario_initalisation
from energy_demand.cli import run_model
from energy_demand.dwelling_stock import dw_stock
from energy_demand.read_write import read_data, write_data
from energy_demand.read_write import read_data, write_data, data_loader
from energy_demand.main import energy_demand_model
from energy_demand.read_write import data_loader
from energy_demand.assumptions import non_param_assumptions
from energy_demand.assumptions import param_assumptions
from energy_demand.assumptions import param_assumptions, non_param_assumptions
from energy_demand.basic import date_prop
from pkg_resources import Requirement, resource_filename
from energy_demand.validation import lad_validation
Expand Down Expand Up @@ -254,7 +252,7 @@ def simulate(self, timestep, data=None):
data['scenario_data'] = {
'gva': self.user_data['gva'],
'population': self.user_data['population'],
'floor_area': {
'floor_area': {
'rs_floorarea': data['rs_floorarea'],
'ss_sector_floor_area_by': data['ss_sector_floor_area_by']}
}
Expand All @@ -270,7 +268,7 @@ def simulate(self, timestep, data=None):
data['enduses'], data['sectors'], data['fuels'], data['all_sectors'] = data_loader.load_fuels(data['paths'], data['lookups'])
data['assumptions'] = non_param_assumptions.load_non_param_assump(
data['sim_param']['base_yr'], data['paths'], data['enduses'], data['lookups'], data['fuels'])

# ------------------------
# Load all SMIF parameters and replace data dict
# ------------------------
Expand All @@ -281,7 +279,7 @@ def simulate(self, timestep, data=None):
data['tech_lp'] = data_loader.load_data_profiles(
data['paths'],
data['local_paths'],
data['assumptions']['model_yeardays'],
data['assumptions']['model_yeardays'],
data['assumptions']['model_yeardays_daytype'])

# Update: Necessary updates after external data definition
Expand Down
20 changes: 14 additions & 6 deletions tests/scripts/test_s_fuel_to_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,29 @@
from energy_demand.scripts import s_fuel_to_service

def init_nested_dict_brackets():

"""Testing"""
result = s_fuel_to_service.init_nested_dict_brackets(
first_level_keys=["A", "B"],
second_level_keys=[1,2])
second_level_keys=[1, 2])

expected = {"A": {1: {}, 2: {}}, "B": {1: {}, 2: {}}}

assert result == expected

def test_init_nested_dict_zero():

"""Testing"""
result = s_fuel_to_service.init_nested_dict_zero(
first_level_keys=["A", "B"],
second_level_keys=[1,2])
second_level_keys=[1, 2])

expected = {"A": {1: 0, 2: 0}, "B": {1: 0, 2: 0}}

assert result == expected

def test_sum_2_level_dict():
"""Testing"""
two_level_dict = {"A": {1: 30, 2: 2}, "B": {1: 1, 2: 0}}

result = s_fuel_to_service.sum_2_level_dict(two_level_dict)

assert result == 33

0 comments on commit 204ce27

Please sign in to comment.