Coverage for aixweather/imports/EPW.py: 100%
20 statements
« prev ^ index » next coverage.py v7.4.4, created at 2025-01-06 16:01 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2025-01-06 16:01 +0000
1"""
2import epw files
3"""
5import pandas as pd
7from aixweather.imports.utils_import import MetaData
10def load_epw_meta_from_file(path: str) -> MetaData:
11 """
12 Load an EPW file from a specified path and parse the file header for metadata.
14 Args:
15 path (str): The file path to the EPW file to be loaded.
17 Returns:
18 MetaData: An object containing the parsed metadata from the EPW file.
19 """
21 meta = MetaData()
23 with open(path, "r", encoding="latin1") as file:
24 lines = file.readlines()
26 # The 1st line contains the location data
27 location_data = lines[0].split(",")
29 meta.station_name = location_data[1]
30 meta.latitude = float(location_data[6])
31 meta.longitude = float(location_data[7])
32 meta.timezone = float(location_data[8])
33 meta.altitude = float(location_data[9])
34 meta.input_source = "EPW"
36 return meta
39def load_epw_from_file(path: str) -> pd.DataFrame:
40 """
41 Import data from an EPW file and convert it into a DataFrame.
43 Args:
44 path (str): The absolute path to the EPW file.
46 Returns:
47 pd.DataFrame: A DataFrame containing the imported data from the EPW file.
48 """
51 # Find the row number for "DATA PERIODS" to determine where the data starts
52 with open(path, "r", encoding="latin1") as file:
53 lines = file.readlines()
54 data_start_row = (
55 next(i for i, line in enumerate(lines) if "DATA PERIODS" in line) + 1
56 )
58 # Skipping header rows to load data
59 weather_df = pd.read_csv(
60 path,
61 skiprows=data_start_row,
62 header=None,
63 encoding="latin1",
64 encoding_errors="replace",
65 )
67 return weather_df