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

22 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2025-01-06 16:01 +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""" 

13format_ERC information 

14 

15Format info: 

16key = raw data point name 

17core_name = corresponding name matching the format_core_data 

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

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

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

21 

22All changes here automatically change the calculations.  

23Exception: unit conversions have to be added manually. 

24 

25checked by Martin Rätz 01.09.2023 

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

27""" 

28format_ERC = { 

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

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

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

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

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

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

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

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

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

38} 

39 

40 

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

42 """ 

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

44 

45 Args: 

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

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

48 

49 Returns: 

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

51 """ 

52 

53 ### evaluate correctness of format 

54 auxiliary.evaluate_transformations( 

55 core_format=definitions.format_core_data, other_format=format_ERC 

56 ) 

57 

58 ### format raw data for further operations 

59 df = df_import.copy() 

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

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

62 # Remove timezone awareness 

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

64 # rename available variables to core data format 

65 df = auxiliary.rename_columns(df, format_ERC) 

66 

67 ### convert timezone to UTC 

68 # data is UTC 

69 

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

71 df_no_shift = df.copy() 

72 df = time_observation_transformations.shift_time_by_dict(format_ERC, df) 

73 

74 def transform_ERC(df): 

75 # drop unnecessary variables 

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

77 

78 ### convert units 

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

80 

81 ### impute missing variables from other available ones 

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

83 

84 return df, calc_overview 

85 

86 df, meta.executed_transformations = transform_ERC(df) 

87 

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

89 ### to avoid back and forth interpolating 

90 df = pass_through_handling.create_pass_through_variables( 

91 df_shifted=df, 

92 df_no_shift=df_no_shift, 

93 format=format_ERC, 

94 transform_func=transform_ERC, 

95 meta=meta, 

96 ) 

97 

98 return df