Coverage for aixweather/transformation_to_core_data/TRY.py: 100%

33 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2025-01-06 16:01 +0000

1import pandas as pd 

2import datetime as dt 

3 

4from aixweather import definitions 

5from aixweather.imports.utils_import import MetaData 

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

7 pass_through_handling, unit_conversions 

8 

9""" 

10format_TRY_15_45 information  

11 

12Format info: 

13key = raw data point name 

14core_name = corresponding name matching the format_core_data 

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

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

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

18 

19All changes here automatically change the calculations.  

20Exception: unit conversions have to be added manually. 

21 

22checked by Martin Rätz (08.08.2023) 

23 

24https://www.bbsr.bund.de/BBSR/DE/forschung/programme/zb/Auftragsforschung/5EnergieKlimaBauen/2013/testreferenzjahre/try-handbuch.pdf;jsessionid=9F928CDB6862224B04073332C2B1B620.live21301?__blob=publicationFile&v=1 

25Der erste Eintrag im Datensatz bezieht sich auf den 1. Januar 01 Uhr MEZ und 

26der letzte Eintrag auf den 31. Dezember 24 Uhr MEZ. Also UTC+1. 

27""" 

28format_TRY_15_45 = { 

29 "t": {"core_name": "DryBulbTemp", "time_of_meas_shift": "prec2ind", "unit": "degC"}, 

30 "p": {"core_name": "AtmPressure", "time_of_meas_shift": None, "unit": "hPa"}, 

31 "WR": {"core_name": "WindDir", "time_of_meas_shift": None, "unit": "deg"}, 

32 "WG": {"core_name": "WindSpeed", "time_of_meas_shift": None, "unit": "m/s"}, 

33 "N": {"core_name": "TotalSkyCover", "time_of_meas_shift": None, "unit": "1eigth"}, 

34 # 'x', 

35 "RF": {"core_name": "RelHum", "time_of_meas_shift": "prec2ind", "unit": "percent"}, 

36 "B": {"core_name": "DirHorRad", "time_of_meas_shift": "prec2ind", "unit": "Wh/m2"}, 

37 "D": {"core_name": "DiffHorRad", "time_of_meas_shift": "prec2ind", "unit": "Wh/m2"}, 

38 "A": {"core_name": "HorInfra", "time_of_meas_shift": None, "unit": "Wh/m2"}, 

39 # 'E', 

40} 

41 

42 

43def TRY_to_core_data(df_import: pd.DataFrame, meta: MetaData) -> pd.DataFrame: 

44 """ 

45 Transform imported TRY data of the formats 2015 and 2045 into the core data format. 

46 

47 Args: 

48 df_import (pd.DataFrame): The DataFrame containing imported TRY weather data. 

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

50 

51 Returns: 

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

53 """ 

54 

55 ### evaluate correctness of format 

56 auxiliary.evaluate_transformations( 

57 core_format=definitions.format_core_data, other_format=format_TRY_15_45 

58 ) 

59 

60 def TRY_to_datetimeindex(df, meta: MetaData): 

61 # set index to datetime 

62 # returns datetime objects 

63 df["MM"] = df["MM"].astype(int) 

64 df["DD"] = df["DD"].astype(int) 

65 df["HH"] = df["HH"].astype(int) 

66 time_index = df.apply( 

67 lambda row: dt.datetime(int(meta.try_year), row.MM, row.DD, row.HH - int(1.0)), 

68 axis=1, 

69 ) 

70 # data is shifted by -1 H to satisfy pandas timestamp 

71 # hours in pandas only between 0 and 23, in TRY between 1 and 24 

72 # converts to pandas timestamps if desired 

73 df.index = pd.to_datetime(time_index) 

74 # data is shifted back to original to start: back to 

75 # 2017-01-01 01:00:00 instead of the temporary 2017-01-01 00:00:00 

76 df = df.shift(periods=1, freq="h", axis=0) 

77 

78 return df 

79 

80 ### preprocessing raw data for further operations 

81 df = df_import.copy() 

82 df = TRY_to_datetimeindex(df, meta) 

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

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

85 # rename available variables to core data format 

86 df = auxiliary.rename_columns(df, format_TRY_15_45) 

87 

88 ### convert timezone to UTC+0 

89 df = df.shift(periods=-1, freq="h", axis=0) 

90 

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

92 df_no_shift = df.copy() 

93 df = time_observation_transformations.shift_time_by_dict(format_TRY_15_45, df) 

94 

95 def transform_TRY(df): 

96 # drop unnecessary variables 

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

98 ### convert units 

99 df["TotalSkyCover"] = unit_conversions.eigth_to_tenth(df["TotalSkyCover"]) 

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

101 ### impute missing variables from other available ones 

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

103 return df, calc_overview 

104 

105 df, meta.executed_transformations = transform_TRY(df) 

106 

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

108 ### to avoid back and forth interpolating 

109 df = pass_through_handling.create_pass_through_variables( 

110 df_shifted=df, 

111 df_no_shift=df_no_shift, 

112 format=format_TRY_15_45, 

113 transform_func=transform_TRY, 

114 meta=meta, 

115 ) 

116 

117 return df