Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import polars as pl
- import scipy
- from datetime import timedelta
- from scipy.signal import savgol_filter
- MAX_DELTA_FOR_INTERPOLATION = timedelta(minutes=3)
- sensor_dataframe = pd.read_csv('proffast-2.2-outputs-20220604-ma.csv')
- sampling_rate = "10 s"
- print("After --------------------------------------")
- df = sensor_dataframe.rename(columns={
- 'UTC': 'utc',
- ' gndP': 'gnd_p',
- ' gndT': 'gnd_t',
- ' appSZA': 'app_sza',
- ' azimuth': 'azimuth',
- ' XH2O': 'xh2o',
- ' XAIR': 'xair',
- ' XCO2': 'xco2',
- ' XCH4': 'xch4',
- ' XCO': 'xco',
- ' XCH4_S5P': 'xch4_s5p'
- }, errors="raise")
- sensor_dataframe = df.filter(items=['utc', 'gnd_p', 'gnd_t', 'app_sza', 'azimuth', 'xh2o', 'xair', 'xco2', 'xch4', 'xco', 'xch4_s5p'])
- """
- ✗ sensor_dataframe is the output of get_sensor_dataframe (see above)
- Example:
- me_gnd_p me_gnd_t me_app_sza ...
- utc
- 2021-10-20 07:00:23 950.91 289.05 78.45 ...
- 2021-10-20 07:00:38 950.91 289.05 78.42 ...
- 2021-10-20 07:01:24 950.91 289.05 78.31 ...
- ... ... ... ...
- [1204 rows x 8 columns]
- ✗ sampling_rate is (as of now) a custom type Rate:Literal[
- "10 min", "5 min", "2 min", "1 min", "30 sec", "15 sec", "10 sec", "5 sec", "2 sec", "1 sec"
- ]
- ✗ get_daily_dataframe (see below) will be called afterwards and joins the dataframes on "utc".
- """
- output = sensor_dataframe.sort_values(by=['utc']) # sort according to time
- output['utc'] = pd.to_datetime(output['utc'])
- # Apply smoothing function for all data columns.
- for column in output.columns[1::]:
- output[column] = scipy.signal.savgol_filter(pd.to_numeric(output[column]), 31, 3)
- print("Original Dataset")
- print(output)
- output = output.set_index('utc')
- output.index = pd.to_datetime(output.index)
- output = output.resample(sampling_rate).mean()
- sampling_delta = pd.to_timedelta(sampling_rate)
- # The interpolating limit is dependant on the sampling rate.
- interpolating_limit = int(MAX_DELTA_FOR_INTERPOLATION / sampling_delta)
- if interpolating_limit != 0:
- output.interpolate(
- limit=interpolating_limit,
- inplace=True,
- limit_direction='both',
- limit_area='inside',
- )
- print("After pandas interpolation")
- print(output)
- df = pl.from_pandas(sensor_dataframe)
- q = df.lazy().with_column(pl.col('utc').str.strptime(pl.Datetime, fmt='%F %T').cast(pl.Datetime)).select([pl.col('utc'),
- pl.exclude('utc').map(lambda x: savgol_filter(x.to_numpy(), 31, 3)).explode()])
- df = q.collect()
- df = df.upsample(time_column="utc", every="10s")
- print("After polars upscale")
- print(df)
Advertisement
Add Comment
Please, Sign In to add comment