mabuseif commited on
Commit
3a5a6a5
·
verified ·
1 Parent(s): f0cd2aa

Update data/climate_data.py

Browse files
Files changed (1) hide show
  1. data/climate_data.py +13 -6
data/climate_data.py CHANGED
@@ -45,12 +45,12 @@ class ClimateLocation:
45
 
46
  def __init__(self, epw_file=None, manual_data=None, **kwargs):
47
  """Initialize ClimateLocation with EPW file or manual data."""
48
- if epw_file:
49
  # Extract from EPW (epw_data[6] for dry-bulb temperature)
50
  temps = np.array(epw_file[6], dtype=float)
51
  self.winter_design_temp = np.percentile(temps[~np.isnan(temps)], 0.4) # 99.6% percentile
52
- self.wind_speed = round(np.nanmean(epw_file[13]), 1) # m/s
53
- self.pressure = self.adjust_pressure_for_altitude(kwargs.get("elevation", 0.0))
54
  # Populate other fields from EPW processing
55
  self.id = kwargs.get("id")
56
  self.country = kwargs.get("country")
@@ -237,17 +237,24 @@ class ClimateData:
237
 
238
  data_start_idx = next(i for i, line in enumerate(epw_lines) if line.startswith("DATA PERIODS")) + 1
239
  epw_data = pd.read_csv(StringIO("\n".join(epw_lines[data_start_idx:])), header=None, dtype=str)
 
 
240
  if len(epw_data) != 8760:
241
  raise ValueError(f"EPW file has {len(epw_data)} records, expected 8760.")
 
 
242
 
243
- for col in epw_data.columns:
 
244
  epw_data[col] = pd.to_numeric(epw_data[col], errors='coerce')
 
 
245
 
246
  months = epw_data[1].values # Month
247
  dry_bulb = epw_data[6].values # Dry-bulb temperature (°C)
248
  humidity = epw_data[8].values # Relative humidity (%)
249
  pressure = epw_data[9].values # Atmospheric pressure (Pa)
250
- wind_speed = epw_data[13].values # Wind speed (m/s)
251
 
252
  wet_bulb = self.calculate_wet_bulb(dry_bulb, humidity)
253
 
@@ -665,5 +672,5 @@ class ClimateData:
665
 
666
  if __name__ == "__main__":
667
  climate_data = ClimateData()
668
- session_state = {"building_info": {"country": "Iceland", "city": "Reyugalvik"}, "page": "Climate Data"}
669
  climate_data.display_climate_input(session_state)
 
45
 
46
  def __init__(self, epw_file=None, manual_data=None, **kwargs):
47
  """Initialize ClimateLocation with EPW file or manual data."""
48
+ if epw_file is not None and isinstance(epw_file, pd.DataFrame):
49
  # Extract from EPW (epw_data[6] for dry-bulb temperature)
50
  temps = np.array(epw_file[6], dtype=float)
51
  self.winter_design_temp = np.percentile(temps[~np.isnan(temps)], 0.4) # 99.6% percentile
52
+ self.wind_speed = round(np.nanmean(epw_file[21]), 1) # Wind speed (m/s, index 21)
53
+ self.pressure = round(np.nanmean(epw_file[9]), 1) # Atmospheric pressure (Pa, index 9)
54
  # Populate other fields from EPW processing
55
  self.id = kwargs.get("id")
56
  self.country = kwargs.get("country")
 
237
 
238
  data_start_idx = next(i for i, line in enumerate(epw_lines) if line.startswith("DATA PERIODS")) + 1
239
  epw_data = pd.read_csv(StringIO("\n".join(epw_lines[data_start_idx:])), header=None, dtype=str)
240
+
241
+ # Validate row and column counts
242
  if len(epw_data) != 8760:
243
  raise ValueError(f"EPW file has {len(epw_data)} records, expected 8760.")
244
+ if len(epw_data.columns) != 35:
245
+ raise ValueError(f"EPW file has {len(epw_data.columns)} columns, expected 35.")
246
 
247
+ # Convert relevant columns to numeric
248
+ for col in [1, 6, 8, 9, 21]:
249
  epw_data[col] = pd.to_numeric(epw_data[col], errors='coerce')
250
+ if epw_data[col].isna().all():
251
+ raise ValueError(f"Column {col} (e.g., {'wind speed' if col == 21 else 'pressure' if col == 9 else 'other'}) contains only non-numeric or missing data.")
252
 
253
  months = epw_data[1].values # Month
254
  dry_bulb = epw_data[6].values # Dry-bulb temperature (°C)
255
  humidity = epw_data[8].values # Relative humidity (%)
256
  pressure = epw_data[9].values # Atmospheric pressure (Pa)
257
+ wind_speed = epw_data[21].values # Wind speed (m/s)
258
 
259
  wet_bulb = self.calculate_wet_bulb(dry_bulb, humidity)
260
 
 
672
 
673
  if __name__ == "__main__":
674
  climate_data = ClimateData()
675
+ session_state = {"building_info": {"country": "Iceland", "city": "Reykjavik"}, "page": "Climate Data"}
676
  climate_data.display_climate_input(session_state)