Coverage for aixweather/transformation_to_core_data/ERC.py: 33%

24 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2025-12-31 11:58 +0000

1""" 

2This module includes functions to transform ERC data to core data format. 

3""" 

4 

5import pandas as pd 

6 

7from aixweather import definitions 

8from aixweather.imports.utils_import import MetaData 

9from aixweather.transformation_functions import auxiliary, time_observation_transformations, variable_transformations, \ 

10 pass_through_handling, unit_conversions 

11 

12 

13class ERCFormat: 

14 """ 

15 Information on ERC format 

16 

17 Format info: 

18 key = raw data point name 

19 core_name = corresponding name matching the format_core_data 

20 time_of_meas_shift = desired 30min shifting+interpolation to convert a value that is e.g. the 

21 "average of preceding hour" to "indicated time" (prec2ind). 

22 unit = unit of the raw data following the naming convention of format_core_data 

23 

24 All changes here automatically change the calculations. 

25 Exception: unit conversions have to be added manually. 

26 

27 checked by Martin Rätz 01.09.2023 

28 Radiation checks 12.12.2023: https://github.com/RWTH-EBC/AixWeather/issues/27 

29 """ 

30 

31 @classmethod 

32 def import_format(cls) -> dict: 

33 return { 

34 '4121.weatherstation.temperature': {'core_name': 'DryBulbTemp', 'time_of_meas_shift': 'foll2ind', 'unit': "degC"}, 

35 '4121.weatherstation.diffuse-radiation': {'core_name': 'DiffHorRad', 'time_of_meas_shift': 'foll2ind', 'unit': "Wh/m2"}, 

36 '4121.weatherstation.global-radiation': {'core_name': 'GlobHorRad', 'time_of_meas_shift': 'foll2ind', 'unit': "Wh/m2"}, 

37 '4121.weatherstation.pressure': {'core_name': 'AtmPressure', 'time_of_meas_shift': 'foll2ind', 'unit': "hPa"}, 

38 '4121.weatherstation.relative-humidity': {'core_name': 'RelHum', 'time_of_meas_shift': 'foll2ind', 'unit': "percent"}, 

39 '4121.weatherstation.wind-direction': {'core_name': 'WindDir', 'time_of_meas_shift': 'foll2ind', 'unit': "deg"}, 

40 '4121.weatherstation.wind-speed': {'core_name': 'WindSpeed', 'time_of_meas_shift': 'foll2ind', 'unit': "m/s"}, 

41 # too little information available, also possibly needs a sum up not a mean when interpolating 

42 # 'rainfall': {'core_name': 'PrecWater', 'time_of_meas_shift': 'foll2ind', 'unit': "mm/h"} 

43 } 

44 

45 

46def ERC_to_core_data(df_import: pd.DataFrame, meta: MetaData) -> pd.DataFrame: 

47 """ 

48 Transform imported ERC (Energy Research Center) weather data into core data format. 

49 

50 Args: 

51 df_import (pd.DataFrame): The DataFrame containing imported ERC weather data. 

52 meta (MetaData): Metadata associated with the data. 

53 

54 Returns: 

55 pd.DataFrame: The transformed DataFrame in the core data format. 

56 """ 

57 

58 ### evaluate correctness of format 

59 auxiliary.evaluate_transformations( 

60 core_format=definitions.format_core_data, other_format=ERCFormat.import_format() 

61 ) 

62 

63 ### format raw data for further operations 

64 df = df_import.copy() 

65 # Resample the DataFrame to make the DatetimeIndex complete and monotonic 

66 df = df.resample('h').asfreq() 

67 # Remove timezone awareness 

68 df.index = df.index.tz_localize(None) 

69 # rename available variables to core data format 

70 df = auxiliary.rename_columns(df, ERCFormat.import_format()) 

71 

72 ### convert timezone to UTC 

73 # data is UTC 

74 

75 ### shift and interpolate data forward 30mins or backward -30mins 

76 df_no_shift = df.copy() 

77 df = time_observation_transformations.shift_time_by_dict(ERCFormat.import_format(), df) 

78 

79 def transform_ERC(df): 

80 # drop unnecessary variables 

81 df = auxiliary.force_data_variable_convention(df, definitions.format_core_data) 

82 

83 ### convert units 

84 df["AtmPressure"] = unit_conversions.hPa_to_Pa(df["AtmPressure"]) 

85 

86 ### impute missing variables from other available ones 

87 df, calc_overview = variable_transformations.variable_transform_all(df, meta) 

88 

89 return df, calc_overview 

90 

91 df, meta.executed_transformations = transform_ERC(df) 

92 

93 ### add unshifted data for possible later direct use (pass-through), 

94 ### to avoid back and forth interpolating 

95 df = pass_through_handling.create_pass_through_variables( 

96 df_shifted=df, 

97 df_no_shift=df_no_shift, 

98 format=ERCFormat.import_format(), 

99 transform_func=transform_ERC, 

100 meta=meta, 

101 ) 

102 

103 return df