Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revamp database #3766

Open
wants to merge 89 commits into
base: release-4.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 83 commits
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
7562b25
path and helper function for migrate db
ShiZhongming Jan 16, 2025
c5a6ea8
move helper function
ShiZhongming Jan 16, 2025
81155a7
about archetypes
ShiZhongming Jan 16, 2025
62f18a3
Update cea4_migrate_db.py
ShiZhongming Jan 16, 2025
9b1c745
before verify db
ShiZhongming Jan 16, 2025
62ae5a8
migration worked
ShiZhongming Jan 16, 2025
2b56f7f
Update cea4_migrate_db.py
ShiZhongming Jan 16, 2025
0ff4ca2
helper function to test inputs against schemas
ShiZhongming Jan 17, 2025
e1d5dda
function improved
ShiZhongming Jan 17, 2025
0aa06f0
apply the check function but need to debug
ShiZhongming Jan 17, 2025
7353a44
before self
ShiZhongming Jan 17, 2025
e2e2e07
inputs verify ran through
ShiZhongming Jan 17, 2025
2374bd3
Update cea4_verify.py
ShiZhongming Jan 17, 2025
2924abd
updates on message
ShiZhongming Jan 17, 2025
1227b27
archetypes verify
ShiZhongming Jan 17, 2025
6043e6a
assemblies
ShiZhongming Jan 17, 2025
388f228
before components
ShiZhongming Jan 17, 2025
80e2450
distribution
ShiZhongming Jan 17, 2025
d9199ed
Update cea4_verify_db.py
ShiZhongming Jan 17, 2025
6aac022
feedstocks
ShiZhongming Jan 17, 2025
95172f0
conversion
ShiZhongming Jan 17, 2025
09b66f3
Update cea4_verify_db.py
ShiZhongming Jan 17, 2025
2d5749c
Update cea4_verify_db.py
ShiZhongming Jan 22, 2025
f7cf510
envelope schemas
ShiZhongming Jan 23, 2025
4d058c8
conversion
ShiZhongming Jan 23, 2025
dc287a9
distribution
ShiZhongming Jan 23, 2025
a199a8d
feedstocks
ShiZhongming Jan 23, 2025
1e6098b
use type
ShiZhongming Jan 23, 2025
68bc6e5
construction type
ShiZhongming Jan 23, 2025
cda6eeb
use types
ShiZhongming Jan 23, 2025
83aa958
zone
ShiZhongming Jan 23, 2025
f1c6830
Update cea4_verify_db.py
ShiZhongming Jan 23, 2025
6c911fb
duplicate_names
ShiZhongming Jan 23, 2025
67b746d
rename for duplicate headings
ShiZhongming Jan 23, 2025
801c557
lower case Description
ShiZhongming Jan 25, 2025
191e085
stop delete old files before the verification
ShiZhongming Jan 25, 2025
e221323
path to the new monthly multiplier compilation csv
ShiZhongming Jan 25, 2025
206f0f2
move schedules
ShiZhongming Jan 25, 2025
4267e89
a bit buggy
ShiZhongming Jan 26, 2025
70f0175
migration almost done
ShiZhongming Jan 26, 2025
5d248cb
distribution Code to code
ShiZhongming Jan 26, 2025
0540e84
Pipe DN to pipe DN
ShiZhongming Jan 26, 2025
7ac739f
centralised rename_dict
ShiZhongming Jan 26, 2025
0b34621
Update cea4_migrate_db.py
ShiZhongming Jan 26, 2025
b1210c6
if migration is not successful, delete the new database folder and ke…
ShiZhongming Jan 26, 2025
807b8d8
small fix cea4 input data verify
ShiZhongming Jan 26, 2025
64724fb
schemas for use type
ShiZhongming Jan 26, 2025
0889007
assembilies ran through
ShiZhongming Jan 26, 2025
71c39cf
schema conversion
ShiZhongming Jan 26, 2025
ed6d5f1
distribution
ShiZhongming Jan 26, 2025
3c57f25
schema cleared
ShiZhongming Jan 26, 2025
b617e17
still not working
ShiZhongming Jan 26, 2025
5d20a17
energy carrier hard coded
ShiZhongming Jan 26, 2025
8310b47
still not ran through
ShiZhongming Jan 26, 2025
1aebf99
ran through
ShiZhongming Jan 27, 2025
57306aa
ran through again the verify
ShiZhongming Jan 27, 2025
24476fe
conversion fixed
ShiZhongming Jan 27, 2025
b1c34e7
feedstocks
ShiZhongming Jan 27, 2025
6d76e35
schedules
ShiZhongming Jan 27, 2025
67c0e7a
all ran through
ShiZhongming Jan 27, 2025
4e1b22f
Update cea4_migrate_db.py
ShiZhongming Jan 27, 2025
14e09fc
verify takes in empty database
ShiZhongming Jan 27, 2025
846549a
I think all ready now
ShiZhongming Jan 27, 2025
c288bea
Update cea4_verify_db.py
ShiZhongming Jan 28, 2025
9c6f14d
just updated the formate helper
ShiZhongming Jan 28, 2025
7756993
Merge branch 'release-4.0' into revamp-db
ShiZhongming Jan 28, 2025
7307a69
some progress
ShiZhongming Jan 28, 2025
cf458a3
verbose
ShiZhongming Jan 28, 2025
9162255
verbose
ShiZhongming Jan 28, 2025
65da095
Update cea4_verify_db.py
ShiZhongming Jan 28, 2025
7cae7af
bug fix
ShiZhongming Jan 28, 2025
606f483
Update cea4_verify.py
ShiZhongming Jan 28, 2025
f01d5c5
before updating the verbose - overview, but everything ran through now
ShiZhongming Jan 28, 2025
3c77ee9
Update cea4_verify.py
ShiZhongming Jan 28, 2025
a17298d
almost ready
ShiZhongming Jan 28, 2025
829e41a
almost PR
ShiZhongming Jan 29, 2025
1c60346
Update format_helper.py
ShiZhongming Jan 29, 2025
69f472f
Update cea4_verify_db.py
ShiZhongming Jan 29, 2025
47280f4
colloquial syntax
ShiZhongming Jan 29, 2025
725a7ca
Update format_helper.py
ShiZhongming Jan 29, 2025
3dd77bc
ready
ShiZhongming Jan 30, 2025
4ca2b65
ruff
ShiZhongming Jan 30, 2025
7e74f20
code to use type
ShiZhongming Feb 3, 2025
4af83cd
schedule library
ShiZhongming Feb 4, 2025
bca55a5
with Mathias
ShiZhongming Feb 4, 2025
f611b38
Ensure that reference column exists
reyery Feb 4, 2025
03b5aa9
Remove reading from sheets and use csv instead
reyery Feb 4, 2025
777889d
Check for sheet and column for lookup
reyery Feb 4, 2025
fc32904
Fix look up column for use type
reyery Feb 4, 2025
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
51 changes: 23 additions & 28 deletions cea/datamanagement/format_helper/cea4_migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def verify_file_exists_3(scenario, items):
return list_missing_files


def migrate_dbf_to_csv(scenario, item, required_columns):
def migrate_dbf_to_csv(scenario, item, required_columns, verbose=False):
"""
Migrate a DBF file to CSV format with column renaming.

Expand All @@ -223,24 +223,19 @@ def migrate_dbf_to_csv(scenario, item, required_columns):
df.rename(columns=columns_mapping_dict_typology, inplace=True)
df.to_csv(path_to_input_file_without_db_4(scenario, item), index=False)
os.remove(path_to_input_file_without_db_3(scenario, item))
print(f'+ {item}.dbf has been migrated from CEA-3 to CEA-4 format.')
if verbose:
print(f'- {item}.dbf has been migrated from CEA-3 to CEA-4 format.')

## --------------------------------------------------------------------------------------------------------------------
## Migrate to CEA-4 format from CEA-3 format
## --------------------------------------------------------------------------------------------------------------------

def migrate_cea3_to_cea4(scenario):
def migrate_cea3_to_cea4(scenario, verbose=False):

#0. verify if everything is already in the correct format for CEA-4
dict_missing = cea4_verify(scenario)
if all(not value for value in dict_missing.values()):
pass
# print("✓" * 3)
# print('All inputs are verified as present and compatible with the current version of CEA-4 for Scenario: {scenario}, including:'.format(scenario=scenario_name),
# 'input building-geometries ShapeFiles: [zone and surroundings], '
# 'input building-properties .csv files: {csv_building_properties}.'.format(csv_building_properties=CSV_BUILDING_PROPERTIES_4),
# )
# print("-" * 1 + ' Scenario: {scenario} - end '.format(scenario=scenario_name) + "-" * 50)

else:
# Verify missing files for CEA-3 and CEA-4 formats
Expand Down Expand Up @@ -275,24 +270,25 @@ def migrate_cea3_to_cea4(scenario):
zone_df_4.drop(columns=['Name'], inplace=True)
zone_df_4 = zone_df_4[COLUMNS_ZONE_4]
replace_shapefile_dbf(scenario, 'zone', zone_df_4, COLUMNS_ZONE_3)
print('+ zone.shp and typology.dbf have been merged and migrated to CEA-4 format.')
if verbose:
print('- zone.shp and typology.dbf have been merged and migrated to CEA-4 format.')
else:
raise ValueError('+ typology.dbf exists but does not follow the CEA-3 format. CEA cannot proceed with the data migration. '
'Check the following column(s) for CEA-3 format: {list_missing_attributes_typology_3}'.format(list_missing_attributes_typology_3=list_missing_attributes_typology_3)
raise ValueError('! typology.dbf exists but does not follow the CEA-3 format. CEA cannot proceed with the data migration. '
'Check the following column(s) for CEA-3 format: {list_missing_attributes_typology_3}.'.format(list_missing_attributes_typology_3=list_missing_attributes_typology_3)
)
else:
print("+ CEA is unable to produce a zone.shp compatible to CEA-4 format. To enable the migration, ensure typology.dbf is present in building-properties folder for CEA-3 format.")
print("! CEA is unable to produce a zone.shp compatible to CEA-4 format. To enable the migration, ensure typology.dbf (CEA-3 format) is present in building-properties folder.")

elif list_missing_attributes_zone_3 and not list_missing_attributes_zone_4:
pass
# print('For Scenario: {scenario}, '.format(scenario=scenario_name), 'zone.shp already follows the CEA-4 format.')
else:
raise ValueError('+ zone.shp exists but follows neither the CEA-3 nor CEA-4 format. CEA cannot proceed with the data migration.'
raise ValueError('! zone.shp exists but follows neither the CEA-3 nor CEA-4 format. CEA cannot proceed with the data migration.'
'Check the following column(s) for CEA-3 format: {list_missing_attributes_zone_3}.'.format(list_missing_attributes_zone_3=list_missing_attributes_zone_3),
'Check the following column(s) for CEA-4 format: {list_missing_attributes_zone_4}.'.format(list_missing_attributes_zone_4=list_missing_attributes_zone_4)
)
else:
print("! Ensure zone.shp is present in building-geometry folder.")
print("! Ensure zone.shp (CEA-3 format) is present in building-geometry folder.")

if 'surroundings' not in list_missing_files_shp_building_geometry_4:
list_missing_attributes_surroundings_3 = verify_shp(scenario, 'surroundings', COLUMNS_SURROUNDINGS_3)
Expand All @@ -301,7 +297,8 @@ def migrate_cea3_to_cea4(scenario):
surroundings_df = gpd.read_file(path_to_input_file_without_db_3(scenario, 'surroundings'))
surroundings_df.rename(columns=columns_mapping_dict_name, inplace=True)
replace_shapefile_dbf(scenario, 'surroundings', surroundings_df, COLUMNS_SURROUNDINGS_3)
print('+ surroundings.shp has been migrated from CEA-3 to CEA-4 format.')
if verbose:
print('- surroundings.shp has been migrated from CEA-3 to CEA-4 format.')

elif list_missing_attributes_surroundings_3 and not list_missing_attributes_surroundings_4:
pass
Expand All @@ -312,12 +309,12 @@ def migrate_cea3_to_cea4(scenario):
'Check the following column(s) for CEA-4 format: {list_missing_attributes_surroundings_4}.'.format(list_missing_attributes_surroundings_4=list_missing_attributes_surroundings_4)
)
else:
print('! (optional) Run Surroundings Helper to generate surroundings.shp.')
print('! (optional) Run Surroundings Helper to generate surroundings.shp after the data migration.')

#2. about the .dbf files in the building-properties folder to be migrated to .csv files
if 'air_conditioning' in list_missing_files_csv_building_properties_4 and not list_missing_columns_air_conditioning_4:
if 'air_conditioning' not in list_missing_files_dbf_building_properties_3:
migrate_dbf_to_csv(scenario, 'air_conditioning', COLUMNS_AIR_CONDITIONING_3)
migrate_dbf_to_csv(scenario, 'air_conditioning', COLUMNS_AIR_CONDITIONING_3, verbose=verbose)
else:
print("! Ensure either air_conditioning.dbf or air_conditioning.csv is present in building-properties folder. Run Archetypes-Helper to generate air_conditioning.csv.")
elif 'air_conditioning' not in list_missing_files_csv_building_properties_4 and list_missing_columns_air_conditioning_4:
Expand All @@ -327,7 +324,7 @@ def migrate_cea3_to_cea4(scenario):

if 'architecture' in list_missing_files_csv_building_properties_4 and not list_missing_columns_architecture_4:
if 'architecture' not in list_missing_files_dbf_building_properties_3:
migrate_dbf_to_csv(scenario, 'architecture', COLUMNS_ARCHITECTURE_3)
migrate_dbf_to_csv(scenario, 'architecture', COLUMNS_ARCHITECTURE_3, verbose=verbose)
else:
print("! Ensure either architecture.dbf or architecture.csv is present in building-properties folder. Run Archetypes-Helper to generate architecture.csv.")
elif 'architecture' not in list_missing_files_csv_building_properties_4 and list_missing_columns_architecture_4:
Expand All @@ -337,7 +334,7 @@ def migrate_cea3_to_cea4(scenario):

if 'indoor_comfort' in list_missing_files_csv_building_properties_4 and not list_missing_columns_indoor_comfort_4:
if 'indoor_comfort' not in list_missing_files_dbf_building_properties_3:
migrate_dbf_to_csv(scenario, 'indoor_comfort', COLUMNS_INDOOR_COMFORT_3)
migrate_dbf_to_csv(scenario, 'indoor_comfort', COLUMNS_INDOOR_COMFORT_3, verbose=verbose)
else:
print("! Ensure either indoor_comfort.dbf or indoor_comfort.csv is present in building-properties folder. Run Archetypes-Helper to generate indoor_comfort.csv.")
elif 'indoor_comfort' not in list_missing_files_csv_building_properties_4 and list_missing_columns_indoor_comfort_4:
Expand All @@ -347,7 +344,7 @@ def migrate_cea3_to_cea4(scenario):

if 'internal_loads' in list_missing_files_csv_building_properties_4 and not list_missing_columns_internal_loads_4:
if 'internal_loads' not in list_missing_files_dbf_building_properties_3:
migrate_dbf_to_csv(scenario, 'internal_loads', COLUMNS_INTERNAL_LOADS_3)
migrate_dbf_to_csv(scenario, 'internal_loads', COLUMNS_INTERNAL_LOADS_3, verbose=verbose)
else:
print("! Ensure either internal_loads.dbf or internal_loads.csv is present in building-properties folder. Run Archetypes-Helper to generate internal_loads.csv.")
elif 'internal_loads' not in list_missing_files_csv_building_properties_4 and list_missing_columns_internal_loads_4:
Expand All @@ -357,7 +354,7 @@ def migrate_cea3_to_cea4(scenario):

if 'supply_systems' in list_missing_files_csv_building_properties_4 and not list_missing_columns_supply_systems_4:
if 'supply_systems' not in list_missing_files_dbf_building_properties_3:
migrate_dbf_to_csv(scenario, 'supply_systems', COLUMNS_SUPPLY_SYSTEMS_3)
migrate_dbf_to_csv(scenario, 'supply_systems', COLUMNS_SUPPLY_SYSTEMS_3, verbose=verbose)
else:
print("! Ensure either supply_systems.dbf or supply_systems.csv is present in building-properties folder. Run Archetypes-Helper to generate supply_systems.csv.")
elif 'supply_systems' not in list_missing_files_csv_building_properties_4 and list_missing_columns_supply_systems_4:
Expand All @@ -369,14 +366,12 @@ def migrate_cea3_to_cea4(scenario):
typology_path = path_to_input_file_without_db_3(scenario, 'typology')
if os.path.exists(typology_path):
os.remove(typology_path)
print('typology.dbf has been removed as it is no longer needed by CEA-4.')
if verbose:
print('- typology.dbf has been removed as it is no longer needed by CEA-4.')


#3. about the Database


# Print: End
print('-' * 49)
# # Print: End
# print('-' * 49)

## --------------------------------------------------------------------------------------------------------------------
## Main function
Expand Down
Loading
Loading