mabuseif commited on
Commit
dd02fbd
·
verified ·
1 Parent(s): 6a5c738

Update app/main.py

Browse files
Files changed (1) hide show
  1. app/main.py +64 -54
app/main.py CHANGED
@@ -92,7 +92,19 @@ class HVACCalculator:
92
  self.data_export = DataExport()
93
  self.cooling_calculator = CoolingLoadCalculator()
94
  self.heating_calculator = HeatingLoadCalculator()
95
- self.climate_data = ClimateData()
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
  self.setup_layout()
98
 
@@ -375,7 +387,6 @@ class HVACCalculator:
375
  # Gather inputs
376
  building_components = st.session_state.get('components', {})
377
  internal_loads = st.session_state.get('internal_loads', {})
378
- climate_data = st.session_state.get('climate_data', {})
379
  building_info = st.session_state.get('building_info', {})
380
 
381
  # Validate inputs
@@ -383,48 +394,42 @@ class HVACCalculator:
383
  return False, "Floor area is missing in building information.", {}
384
  if not building_components.get('walls') and not building_components.get('roofs') and not building_components.get('windows'):
385
  return False, "No building components defined. Please add walls, roofs, or windows.", {}
386
- if not climate_data:
387
- return False, "No climate data provided. Please specify climate data.", {}
388
 
389
- # Validate wall_group and roof_group
390
- for wall in building_components.get('walls', []):
391
- if not hasattr(wall, 'wall_group') or wall.wall_group not in WallModel.VALID_WALL_GROUPS:
392
- st.warning(f"Invalid wall_group '{getattr(wall, 'wall_group', None)}' for wall '{wall.name}'. Defaulting to 'A'.")
393
- wall.wall_group = "A"
394
- for roof in building_components.get('roofs', []):
395
- if not hasattr(roof, 'roof_group') or roof.roof_group not in RoofModel.VALID_ROOF_GROUPS:
396
- st.warning(f"Invalid roof_group '{getattr(roof, 'roof_group', None)}' for roof '{roof.name}'. Defaulting to 'A'.")
397
- roof.roof_group = "A"
 
398
 
399
- # Extract climate data
400
- climate_id = f"{building_info.get('country', 'XX')[:2].upper()}-{building_info.get('city', 'XXX')[:3].upper()}"
401
- location = climate_data.get(climate_id, None)
 
 
 
 
 
 
402
  if not location:
403
  return False, f"No climate data found for {climate_id}. Please provide valid climate data.", {}
404
 
405
  # Validate climate data
406
- if not hasattr(location, 'summer_design_temp_db') or not hasattr(location, 'summer_design_temp_wb'):
407
- return False, f"Invalid climate data for {climate_id}. Missing summer design temperatures.", {}
408
-
409
- # Validate and map latitude
410
- valid_latitudes = ['24N', '36N', '48N']
411
- try:
412
- lat_value = float(location.latitude)
413
- differences = [abs(lat_value - float(lat[:-1])) for lat in valid_latitudes]
414
- nearest_idx = differences.index(min(differences))
415
- latitude = valid_latitudes[nearest_idx]
416
- except (ValueError, TypeError):
417
- st.warning(f"Invalid latitude '{location.latitude}'. Defaulting to '24N'.")
418
- latitude = '24N'
419
 
420
  # Format conditions
421
  outdoor_conditions = {
422
- 'temperature': location.summer_design_temp_db,
423
- 'relative_humidity': location.summer_design_temp_wb,
424
- 'ground_temperature': location.monthly_temps.get('Jul', 20.0),
425
  'month': 'Jul',
426
- 'latitude': latitude,
427
- 'wind_speed': climate_data.get('wind_speed', 4.0),
428
  'day_of_year': 204 # Approx. July 23
429
  }
430
  indoor_conditions = {
@@ -688,7 +693,6 @@ class HVACCalculator:
688
  # Gather inputs
689
  building_components = st.session_state.get('components', {})
690
  internal_loads = st.session_state.get('internal_loads', {})
691
- climate_data = st.session_state.get('climate_data', {})
692
  building_info = st.session_state.get('building_info', {})
693
 
694
  # Validate inputs
@@ -696,34 +700,40 @@ class HVACCalculator:
696
  return False, "Floor area is missing in building information.", {}
697
  if not building_components.get('walls') and not building_components.get('roofs') and not building_components.get('windows'):
698
  return False, "No building components defined. Please add walls, roofs, or windows.", {}
699
- if not climate_data:
700
- return False, "No climate data provided. Please specify climate data.", {}
701
 
702
- # Validate wall_group and roof_group
703
- for wall in building_components.get('walls', []):
704
- if not hasattr(wall, 'wall_group') or wall.wall_group not in WallModel.VALID_WALL_GROUPS:
705
- st.warning(f"Invalid wall_group '{getattr(wall, 'wall_group', None)}' for wall '{wall.name}'. Defaulting to 'A'.")
706
- wall.wall_group = "A"
707
- for roof in building_components.get('roofs', []):
708
- if not hasattr(roof, 'roof_group') or roof.roof_group not in RoofModel.VALID_ROOF_GROUPS:
709
- st.warning(f"Invalid roof_group '{getattr(roof, 'roof_group', None)}' for roof '{roof.name}'. Defaulting to 'A'.")
710
- roof.roof_group = "A"
 
 
 
 
 
 
 
 
 
711
 
712
- # Extract climate data
713
- climate_id = f"{building_info.get('country', 'XX')[:2].upper()}-{building_info.get('city', 'XXX')[:3].upper()}"
714
- location = climate_data.get(climate_id, None)
715
  if not location:
716
  return False, f"No climate data found for {climate_id}. Please provide valid climate data.", {}
717
 
718
  # Validate climate data
719
- if not hasattr(location, 'winter_design_temp'):
720
- return False, f"Invalid climate data for {climate_id}. Missing winter design temperature.", {}
721
 
722
  # Format conditions
723
  outdoor_conditions = {
724
- 'design_temperature': location.winter_design_temp,
725
- 'design_relative_humidity': location.monthly_humidity.get('Jan', 80.0),
726
- 'ground_temperature': location.monthly_temps.get('Jan', 10.0)
 
727
  }
728
  indoor_conditions = {
729
  'temperature': building_info.get('indoor_temp', 21.0),
 
92
  self.data_export = DataExport()
93
  self.cooling_calculator = CoolingLoadCalculator()
94
  self.heating_calculator = HeatingLoadCalculator()
95
+
96
+ # Persist ClimateData in session_state
97
+ if 'climate_data_obj' not in st.session_state:
98
+ st.session_state.climate_data_obj = ClimateData()
99
+ self.climate_data = st.session_state.climate_data_obj
100
+
101
+ # Load default climate data if locations are empty
102
+ try:
103
+ if not self.climate_data.locations:
104
+ self.climate_data = ClimateData.from_json("/home/user/app/climate_data.json")
105
+ st.session_state.climate_data_obj = self.climate_data
106
+ except FileNotFoundError:
107
+ st.warning("Default climate data file not found. Please enter climate data manually.")
108
 
109
  self.setup_layout()
110
 
 
387
  # Gather inputs
388
  building_components = st.session_state.get('components', {})
389
  internal_loads = st.session_state.get('internal_loads', {})
 
390
  building_info = st.session_state.get('building_info', {})
391
 
392
  # Validate inputs
 
394
  return False, "Floor area is missing in building information.", {}
395
  if not building_components.get('walls') and not building_components.get('roofs') and not building_components.get('windows'):
396
  return False, "No building components defined. Please add walls, roofs, or windows.", {}
 
 
397
 
398
+ # Check climate data
399
+ if "climate_data" not in st.session_state or not st.session_state["climate_data"]:
400
+ return False, "Please enter climate data in the 'Climate Data' page.", {}
401
+
402
+ # Extract climate data using ClimateData.get_location_by_id
403
+ country = building_info.get('country', '').strip().title()
404
+ city = building_info.get('city', '').strip().title()
405
+ if not country or not city:
406
+ return False, "Country and city must be set in Building Information.", {}
407
+ climate_id = f"{country[:2].upper()}-{city[:3].upper()}"
408
 
409
+ # Debug
410
+ st.write("Debug: Climate Data Retrieval", {
411
+ 'climate_id': climate_id,
412
+ 'building_info': building_info,
413
+ 'session_state_climate_data': st.session_state.get('climate_data', 'Not set'),
414
+ 'climate_locations': list(self.climate_data.locations.keys())
415
+ })
416
+
417
+ location = self.climate_data.get_location_by_id(climate_id, st.session_state)
418
  if not location:
419
  return False, f"No climate data found for {climate_id}. Please provide valid climate data.", {}
420
 
421
  # Validate climate data
422
+ if not all(k in location for k in ['summer_design_temp_db', 'summer_design_temp_wb', 'monthly_temps', 'latitude']):
423
+ return False, f"Invalid climate data for {climate_id}. Missing required fields.", {}
 
 
 
 
 
 
 
 
 
 
 
424
 
425
  # Format conditions
426
  outdoor_conditions = {
427
+ 'temperature': location['summer_design_temp_db'],
428
+ 'relative_humidity': location['monthly_humidity'].get('Jul', 50.0),
429
+ 'ground_temperature': location['monthly_temps'].get('Jul', 20.0),
430
  'month': 'Jul',
431
+ 'latitude': f"{location['latitude']}N" if location['latitude'] >= 0 else f"{abs(location['latitude'])}S",
432
+ 'wind_speed': 4.0, # Default as not provided in climate data
433
  'day_of_year': 204 # Approx. July 23
434
  }
435
  indoor_conditions = {
 
693
  # Gather inputs
694
  building_components = st.session_state.get('components', {})
695
  internal_loads = st.session_state.get('internal_loads', {})
 
696
  building_info = st.session_state.get('building_info', {})
697
 
698
  # Validate inputs
 
700
  return False, "Floor area is missing in building information.", {}
701
  if not building_components.get('walls') and not building_components.get('roofs') and not building_components.get('windows'):
702
  return False, "No building components defined. Please add walls, roofs, or windows.", {}
 
 
703
 
704
+ # Check climate data
705
+ if "climate_data" not in st.session_state or not st.session_state["climate_data"]:
706
+ return False, "Please enter climate data in the 'Climate Data' page.", {}
707
+
708
+ # Extract climate data using ClimateData.get_location_by_id
709
+ country = building_info.get('country', '').strip().title()
710
+ city = building_info.get('city', '').strip().title()
711
+ if not country or not city:
712
+ return False, "Country and city must be set in Building Information.", {}
713
+ climate_id = f"{country[:2].upper()}-{city[:3].upper()}"
714
+
715
+ # Debug
716
+ st.write("Debug: Climate Data Retrieval", {
717
+ 'climate_id': climate_id,
718
+ 'building_info': building_info,
719
+ 'session_state_climate_data': st.session_state.get('climate_data', 'Not set'),
720
+ 'climate_locations': list(self.climate_data.locations.keys())
721
+ })
722
 
723
+ location = self.climate_data.get_location_by_id(climate_id, st.session_state)
 
 
724
  if not location:
725
  return False, f"No climate data found for {climate_id}. Please provide valid climate data.", {}
726
 
727
  # Validate climate data
728
+ if not all(k in location for k in ['winter_design_temp', 'monthly_temps', 'monthly_humidity']):
729
+ return False, f"Invalid climate data for {climate_id}. Missing required fields.", {}
730
 
731
  # Format conditions
732
  outdoor_conditions = {
733
+ 'design_temperature': location['winter_design_temp'],
734
+ 'design_relative_humidity': location['monthly_humidity'].get('Jan', 80.0),
735
+ 'ground_temperature': location['monthly_temps'].get('Jan', 10.0),
736
+ 'wind_speed': 4.0 # Default as not provided in climate data
737
  }
738
  indoor_conditions = {
739
  'temperature': building_info.get('indoor_temp', 21.0),