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

1""" 

2import epw files 

3""" 

4 

5import pandas as pd 

6 

7from aixweather.imports.utils_import import MetaData 

8 

9 

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. 

13 

14 Args: 

15 path (str): The file path to the EPW file to be loaded. 

16 

17 Returns: 

18 MetaData: An object containing the parsed metadata from the EPW file. 

19 """ 

20 

21 meta = MetaData() 

22 

23 with open(path, "r", encoding="latin1") as file: 

24 lines = file.readlines() 

25 

26 # The 1st line contains the location data 

27 location_data = lines[0].split(",") 

28 

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" 

35 

36 return meta 

37 

38 

39def load_epw_from_file(path: str) -> pd.DataFrame: 

40 """ 

41 Import data from an EPW file and convert it into a DataFrame. 

42 

43 Args: 

44 path (str): The absolute path to the EPW file. 

45 

46 Returns: 

47 pd.DataFrame: A DataFrame containing the imported data from the EPW file. 

48 """ 

49 

50 

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 ) 

57 

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 ) 

66 

67 return weather_df