Kieni Data
"""
Retrieves weather data from the Kieni API endpoint and returns it as a pandas DataFrame after processing.
Parameters:
-----------
- start_date (str, optional): The start date for retrieving weather data in 'YYYY-MM-DD' format. Defaults to None if None returns from the beginning of the data.
- end_date (str, optional): The end date for retrieving weather data in 'YYYY-MM-DD' format. Defaults to None if None returns to the end of the data.
- variable (str, optional): The weather variable to retrieve same as the weather shortcodes by TAHMO e.g., 'pr', 'ap', 'rh'
- method (str, optional): The aggregation method to apply to the data ('sum', 'mean', 'min', 'max' and custom functions). Defaults to 'sum'.
- freq (str, optional): The frequency for data aggregation (e.g., '1D' for daily, '1H' for hourly). Defaults to '1D'.
Returns:
-----------
- pandas.DataFrame: DataFrame containing the weather data for the specified parameters, with columns containing NaN values dropped.
Usage:
-----------
To retrieve daily rainfall data from January 1, 2024, to January 31, 2024:
python
Instantiate the Kieni class
api_key, api_secret = '', '' Request DSAIL for the API key and secret
kieni = Kieni(api_key, api_secret)
kieni_weather_data = kieni.kieni_weather_data(start_date='2024-01-01', end_date='2024-01-31', variable='pr', freq='1D', method='sum')
To retrieve hourly temperature data from February 1, 2024, to February 7, 2024:
python
kieni_weather_data = kieni.kieni_weather_data(start_date='2024-02-01', end_date='2024-02-07', variable='te', method='mean', freq='1H')
"""
Aggregate Variables
"""
Aggregates a pandas DataFrame of weather variables by applying a specified method across a given frequency.
Parameters:
-----------
- dataframe (pandas.DataFrame): DataFrame containing weather variable data.
- freq (str, optional): Frequency to aggregate the data by. Defaults to '1D'.
Examples include '1H' for hourly, '12H' for every 12 hours, '1D' for daily, '1W' for weekly, '1M' for monthly, etc.
- method (str or callable, optional): Method to use for aggregation. Defaults to 'sum'.
Acceptable string values are 'sum', 'mean', 'min', 'max'.
Alternatively, you can provide a custom aggregation function (callable).
Example of a custom method:
python
def custom_median(x):
return np.nan if x.isnull().all() else x.median()
daily_median_data = aggregate_variables(dataframe, freq='1D', method=custom_median)
Returns:
-----------
- pandas.DataFrame: DataFrame containing aggregated weather variable data according to the specified frequency and method.
Usage:
-----------
Define the DataFrame containing the weather variable data:
python
dataframe = ret.get_measurements('TA00001', '2020-01-01', '2020-01-31', ['pr']) data comes in 5 minute interval
To aggregate data hourly:
python
hourly_data = aggregate_variables(dataframe, freq='1H')
To aggregate data by 12 hours:
python
half_day_data = aggregate_variables(dataframe, freq='12H')
To aggregate data by day:
python
daily_data = aggregate_variables(dataframe, freq='1D')
To aggregate data by week:
python
weekly_data = aggregate_variables(dataframe, freq='1W')
To aggregate data by month:
python
monthly_data = aggregate_variables(dataframe, freq='1M')
To use a custom aggregation method:
python
def custom_median(x):
return np.nan if x.isnull().all() else x.median()
daily_median_data = aggregate_variables(dataframe, freq='1D', method=custom_median)
"""