Guest User

Untitled

a guest
Feb 17th, 2023
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. import pandas as pd
  2. import polars as pl
  3. import scipy
  4. from datetime import timedelta
  5. from scipy.signal import savgol_filter
  6. MAX_DELTA_FOR_INTERPOLATION = timedelta(minutes=3)
  7.  
  8. sensor_dataframe = pd.read_csv('proffast-2.2-outputs-20220604-ma.csv')
  9. sampling_rate = "10 s"
  10. print("After --------------------------------------")
  11. df = sensor_dataframe.rename(columns={
  12. 'UTC': 'utc',
  13. ' gndP': 'gnd_p',
  14. ' gndT': 'gnd_t',
  15. ' appSZA': 'app_sza',
  16. ' azimuth': 'azimuth',
  17. ' XH2O': 'xh2o',
  18. ' XAIR': 'xair',
  19. ' XCO2': 'xco2',
  20. ' XCH4': 'xch4',
  21. ' XCO': 'xco',
  22. ' XCH4_S5P': 'xch4_s5p'
  23. }, errors="raise")
  24. sensor_dataframe = df.filter(items=['utc', 'gnd_p', 'gnd_t', 'app_sza', 'azimuth', 'xh2o', 'xair', 'xco2', 'xch4', 'xco', 'xch4_s5p'])
  25. """
  26. ✗ sensor_dataframe is the output of get_sensor_dataframe (see above)
  27. Example:
  28. me_gnd_p me_gnd_t me_app_sza ...
  29. utc
  30. 2021-10-20 07:00:23 950.91 289.05 78.45 ...
  31. 2021-10-20 07:00:38 950.91 289.05 78.42 ...
  32. 2021-10-20 07:01:24 950.91 289.05 78.31 ...
  33. ... ... ... ...
  34. [1204 rows x 8 columns]
  35.  
  36. ✗ sampling_rate is (as of now) a custom type Rate:Literal[
  37. "10 min", "5 min", "2 min", "1 min", "30 sec", "15 sec", "10 sec", "5 sec", "2 sec", "1 sec"
  38. ]
  39.  
  40. ✗ get_daily_dataframe (see below) will be called afterwards and joins the dataframes on "utc".
  41. """
  42.  
  43. output = sensor_dataframe.sort_values(by=['utc']) # sort according to time
  44. output['utc'] = pd.to_datetime(output['utc'])
  45. # Apply smoothing function for all data columns.
  46. for column in output.columns[1::]:
  47. output[column] = scipy.signal.savgol_filter(pd.to_numeric(output[column]), 31, 3)
  48. print("Original Dataset")
  49. print(output)
  50.  
  51. output = output.set_index('utc')
  52. output.index = pd.to_datetime(output.index)
  53. output = output.resample(sampling_rate).mean()
  54.  
  55. sampling_delta = pd.to_timedelta(sampling_rate)
  56. # The interpolating limit is dependant on the sampling rate.
  57. interpolating_limit = int(MAX_DELTA_FOR_INTERPOLATION / sampling_delta)
  58.  
  59. if interpolating_limit != 0:
  60. output.interpolate(
  61. limit=interpolating_limit,
  62. inplace=True,
  63. limit_direction='both',
  64. limit_area='inside',
  65. )
  66.  
  67. print("After pandas interpolation")
  68. print(output)
  69.  
  70. df = pl.from_pandas(sensor_dataframe)
  71. q = df.lazy().with_column(pl.col('utc').str.strptime(pl.Datetime, fmt='%F %T').cast(pl.Datetime)).select([pl.col('utc'),
  72. pl.exclude('utc').map(lambda x: savgol_filter(x.to_numpy(), 31, 3)).explode()])
  73. df = q.collect()
  74. df = df.upsample(time_column="utc", every="10s")
  75. print("After polars upscale")
  76. print(df)
  77.  
Advertisement
Add Comment
Please, Sign In to add comment