mabuseif commited on
Commit
9fefc74
·
verified ·
1 Parent(s): fe6bafe

Update data/climate_data.py

Browse files
Files changed (1) hide show
  1. data/climate_data.py +81 -88
data/climate_data.py CHANGED
@@ -25,9 +25,9 @@ import logging
25
  logging.basicConfig(level=logging.INFO)
26
  logger = logging.getLogger(__name__)
27
 
28
- # Define paths
29
- DATA_DIR = os.path.dirname(os.path.abspath(__file__))
30
- AU_CCH_DIR = os.path.join(DATA_DIR, "au_cch") # Path to climate projection files
31
 
32
  # CSS for consistent formatting
33
  STYLE = """
@@ -824,91 +824,84 @@ class ClimateData:
824
  if location and epw_data is not None:
825
  self.display_design_conditions(location)
826
 
827
- # Climate Projection tab
828
- with tabs[1]:
829
- st.markdown("""
830
- <div class="markdown-text">
831
- <h3>Climate Projection</h3>
832
- <p>At this stage, this section is focused on some locations in Australia, and the provided data is based on "Projected weather files for building energy modelling" from CSIRO 2022.</p>
833
- </div>
834
- """, unsafe_allow_html=True)
835
-
836
- # Dropdown menus
837
- country = st.selectbox("Country", ["Australia"], key="projection_country")
838
- states = ["ACT", "NSW", "NT", "QLD", "SA", "TAS", "VIC", "WA"]
839
- state = st.selectbox("State", states, key="projection_state")
840
-
841
- # Get locations for selected state
842
- locations = self.get_locations_by_state(state)
843
- location_options = [f"{loc['city']} ({loc['number']})" for loc in locations]
844
- location_display = st.selectbox("Location", location_options, key="projection_location")
845
-
846
- # Extract location number from selection
847
- location_num = ""
848
- if location_display:
849
- location_num = next(loc["number"] for loc in locations if f"{loc['city']} ({loc['number']})" == location_display)
850
-
851
- rcp_options = ["RCP2.6", "RCP4.5", "RCP8.5"]
852
- rcp = st.selectbox("RCP Scenario", rcp_options, key="projection_rcp")
853
-
854
- year_options = ["2030", "2050", "2070", "2090"]
855
- year = st.selectbox("Year", year_options, key="projection_year")
856
-
857
- if st.button("Extract Data"):
858
- with st.spinner("Extracting climate projection data..."):
859
-
860
- # Define paths
861
- DATA_DIR = os.path.dirname(os.path.abspath(__file__)) # Directory of climate_data.py (/<root>/data/)
862
- AU_CCH_DIR = os.path.join(os.path.dirname(DATA_DIR), "au_cch") # Path to au_cch in repository root (/<root>/au_cch/)
863
-
864
- if st.button("Extract Data"):
865
- with st.spinner("Extracting climate projection data..."):
866
- # Construct file path
867
- file_path = os.path.join(AU_CCH_DIR, location_num, rcp, year)
868
- logger.debug(f"Attempting to access directory: {file_path}")
869
-
870
- # Initialize epw_files to avoid NameError
871
- epw_files = []
872
- if not os.path.exists(file_path):
873
- st.error(f"No directory found at {file_path}. Please ensure the 'au_cch' folder is in the repository root alongside 'data' and 'utils', with the structure {location_num}/{rcp}/{year} containing a single file with a .epw extension (e.g., canberra_rcp2.6_2070.epw).")
874
- logger.error(f"Directory does not exist: {file_path}")
875
- else:
876
- try:
877
- epw_files = [f for f in os.listdir(file_path) if f.endswith(".epw")]
878
-
879
- if not epw_files:
880
- st.error(f"No EPW file found in {file_path}. Please check that the directory contains a single file with a .epw extension (e.g., canberra_rcp2.6_2070.epw, not '2070' without .epw).")
881
- logger.error(f"No EPW file found in {file_path}")
882
- elif len(epw_files) > 1:
883
- st.error(f"Multiple EPW files found in {file_path}: {epw_files}. Please ensure exactly one .epw file per directory.")
884
- logger.error(f"Multiple EPW files found: {epw_files}")
885
- else:
886
- epw_file_path = os.path.join(file_path, epw_files[0])
887
- try:
888
- with open(epw_file_path, 'r') as f:
889
- epw_content = f.read()
890
-
891
- location = self.process_epw_file(epw_content, location_num, rcp, year)
892
- if location:
893
- self.add_location(location)
894
- climate_data_dict = location.to_dict()
895
- if self.validate_climate_data(climate_data_dict):
896
- session_state["climate_data"] = climate_data_dict
897
- st.success(f"Climate projection data extracted for {location.city}, {location.country}, {rcp}, {year}!")
898
- logger.info("Successfully processed projection EPW file: %s", climate_data_dict["id"])
899
- session_state["active_tab"] = "General Information"
900
- else:
901
- st.warning(f"Climate projection data validation failed for {location.city}, {location.country}. Displaying data anyway.")
902
- logger.warning("Validation failed for projection EPW data: %s", climate_data_dict["id"])
903
- session_state["climate_data"] = climate_data_dict
904
- session_state["active_tab"] = "General Information"
905
- except Exception as e:
906
- st.error(f"Error reading EPW file {epw_file_path}: {str(e)}")
907
- logger.error(f"Error reading EPW file {epw_file_path}: {str(e)}")
908
- session_state["climate_data"] = {}
909
- except Exception as e:
910
- st.error(f"Error accessing directory {file_path}: {str(e)}")
911
- logger.error(f"Error accessing directory {file_path}: {str(e)}")
912
 
913
  # Other tabs (unchanged)
914
  if location and epw_data is not None:
 
25
  logging.basicConfig(level=logging.INFO)
26
  logger = logging.getLogger(__name__)
27
 
28
+ # Define paths at module level
29
+ DATA_DIR = os.path.dirname(os.path.abspath(__file__)) # Directory of climate_data.py (/home/user/app/data/)
30
+ AU_CCH_DIR = os.path.join(os.path.dirname(DATA_DIR), "au_cch") # Path to au_cch in repository root (/home/user/app/au_cch/)
31
 
32
  # CSS for consistent formatting
33
  STYLE = """
 
824
  if location and epw_data is not None:
825
  self.display_design_conditions(location)
826
 
827
+ # Climate Projection tab
828
+ with tabs[1]:
829
+ st.markdown("""
830
+ <div class="markdown-text">
831
+ <h3>Climate Projection</h3>
832
+ <p>At this stage, this section is focused on some locations in Australia, and the provided data is based on "Projected weather files for building energy modelling" from CSIRO 2022.</p>
833
+ </div>
834
+ """, unsafe_allow_html=True)
835
+
836
+ # Dropdown menus
837
+ country = st.selectbox("Country", ["Australia"], key="projection_country")
838
+ states = ["ACT", "NSW", "NT", "QLD", "SA", "TAS", "VIC", "WA"]
839
+ state = st.selectbox("State", states, key="projection_state")
840
+
841
+ # Get locations for selected state
842
+ locations = self.get_locations_by_state(state)
843
+ location_options = [f"{loc['city']} ({loc['number']})" for loc in locations]
844
+ location_display = st.selectbox("Location", location_options, key="location")
845
+
846
+ # Extract location number from selection
847
+ location_num = ""
848
+ if location_display:
849
+ location_num = next(loc["number"] for loc in locations if f"{loc['city']} ({loc['number']})" == location_display)
850
+
851
+ rcp_options = ["RCP2.6", "RCP4.5", "RCP8.5"]
852
+ rcp = st.selectbox("RCP Scenario", rcp_options, key="rcp")
853
+
854
+ year_options = ["2030", "2050", "2070", "2090"]
855
+ year = st.selectbox("Year", year_options, key="year")
856
+
857
+ if st.button("Extract Data"):
858
+ with st.spinner("Extracting climate projection data..."):
859
+ # Construct file path
860
+ file_path = os_join.path(AU_CCH_DIR, location_num, rcp, year)
861
+ logger.debug(f"Attempting to access directory: {file_path}")
862
+
863
+ # Initialize epw_files to avoid NameError
864
+ epw_files = []
865
+ if not os.path.exists(file_path):
866
+ st.error(f"No directory found at {file_path}. Please ensure the 'au_cch' folder is in the repository root (/home/user/app/au_cch/) alongside 'data' and 'utils', with the structure {location_num}/{rcp}/{year} containing a single file with a .epw extension (e.g., canberra_rcp2.6_2070.epw).")
867
+ logger.error(f"Directory does not exist: {file_path}")
868
+ else:
869
+ try:
870
+ epw_files = [f for f in os.listdir(file_path) if f.endswith(".epw")]
871
+
872
+ if not epw_files:
873
+ st.error(f"No EPW file found in {file_path}. Please check that the directory contains a single file with a .epw extension (e.g., canberra_rcp2.6_2070.epw, not '2070' without .epw).")
874
+ logger.error(f"No EPW file found in {file_path}")
875
+ elif len(epw_files) > 1:
876
+ st.error(f"Multiple EPW files found in {file_path}: {epw_files}. Please ensure exactly one .epw file per directory.")
877
+ logger.error(f"Multiple EPW files found: {epw_files}")
878
+ else:
879
+ epw_file_path = os_join.path_join(file_path, epw_files[0])
880
+ try:
881
+ with open(epw_file_path, 'r') as f:
882
+ epw_content = f.read()
883
+
884
+ location = self.process_epw_file(epw_content, location_num, rcp, year)
885
+ if location:
886
+ self.add_location(location)
887
+ climate_data_dict = location.to_dict()
888
+ if self.validate_climate_data(climate_data_dict):
889
+ session_state["climate_data"] = climate_data_dict
890
+ st.success(f"Climate projection data extracted for {location.city}, {location.country}, {rcp}, {year}!")
891
+ logger.info(f"Successfully processed projection EPW file: {climate_data_dict['id']}")
892
+ session_state["active_tab"] = "General Information"
893
+ else:
894
+ st.warning(f"Climate projection data validation failed for {location.city}, {location.country}. Displaying data anyway.")
895
+ logger.warning(f"Validation failed for projection EPW data: {climate_data_dict['id']}")
896
+ session_state["climate_data"] = climate_data_dict
897
+ session_state["active_tab"] = "General Information"
898
+ except Exception as e:
899
+ st.error(f"Error reading EPW file {epw_file_path}: {str(e)}")
900
+ logger.error(f"Error reading EPW file {epw_file_path}: {str(e)}")
901
+ session_state["climate_data"] = {}
902
+ except Exception as e:
903
+ st.error(f"Error accessing directory {file_path}: {str(e)}")
904
+ logger.error(f"Error accessing directory {file_path}: {str(e)}")
 
 
 
 
 
 
 
905
 
906
  # Other tabs (unchanged)
907
  if location and epw_data is not None: