energy_forecast.meteo module#

class energy_forecast.meteo.ArpegeSimpleAPI(date='2025-01-13', time='00:00:00', prefix='/tmp/arpege')[source]#

Bases: object

Uses the meteo.data.gouv.fr API to fetch weather forecast data.

It downloads the data in the format GRIB2 and reads it using xarray. If the file is already downloaded, it will not download it again.

Parameters:
  • date (str, optional) – the date at which the weather forecast was computed. Must be a valid date format, e.g. "YYYY-MM-DD" Default is the current date.

  • time (str, optional) – the time at which the weather forecast was computed. Can be "00:00:00", "06:00:00", "12:00:00", or "18:00:00" Default is "00:00:00"

  • prefix (str, optional) – the prefix where the files are downloaded. Used to avoid downloading the same file multiple times. Default is "/tmp/arpege"

base_url = 'https://object.data.gouv.fr/meteofrance-pnt/pnt/{date}T{time}Z/arpege/01/SP1/arpege__{resolution}__SP1__{forecast}__{date}T{time}Z.grib2'#
departement_sun()[source]#

Return the mean sun flux for each department of France.

Returns:

pd.DataFrame – The mean sun flux for each department of France

departement_wind()[source]#

Return the mean wind speed for each department of France.

Returns:

pd.DataFrame – The mean wind speed for each department of France

fetch()[source]#

Download the data from the API and save it in the prefix folder. All the forecast horizons are downloaded.

Returns:

list[Path] – The list of the files downloaded.

forecast_horizons = ['000H012H', '013H024H', '025H036H', '037H048H', '049H060H', '061H072H', '073H084H', '085H096H', '097H102H']#
get_filename(forecast_horizon)[source]#

Format the filename to save the data.

get_url(forecast_horizon)[source]#

Format the URL to fetch the data.

mask_sun(masks, names, label)[source]#

Compute the mean sun flux for each region of France.

Returns:

pd.DataFrame – The mean sun flux for each region of France.

mask_wind(masks, names, label)[source]#

Compute the mean wind speed for each region of France.

Returns:

pd.DataFrame – The mean wind speed for each region of France.

static read_file_as_xarray(filename, keys_filter)[source]#

Open the file as an xarray dataset.

Parameters:
  • filename (str) – the filename of the file to open.

  • keys_filter (dict) – the keys to filter the data.

Returns:

xr.Dataset – the dataset containing the weather forecast data.

static read_files_as_xarray(list_files, keys_filter)[source]#

Read all the files as an xarray dataset and concatenate them along the step dimension.

Parameters:
  • list_files (list[str]) – the list of the files to open.

  • keys_filter (dict) – the keys to filter the data.

Returns:

xr.Dataset – the dataset containing the weather forecast data.

read_sspd()[source]#

Fetch the data and read the solar radiation.

Returns:

xr.Dataset – the dataset containing the solar radiation data.

read_wind()[source]#

Fetch the data and read the wind speed.

Returns:

xr.Dataset – the dataset containing the wind speed data.

region_sun()[source]#

Return the mean sun flux for each region of France.

Returns:

pd.DataFrame – The mean sun flux for each region of France

region_wind()[source]#

Return the mean wind speed for each region of France.

Returns:

pd.DataFrame – The mean wind speed for each region of France

resolution = '01'#
energy_forecast.meteo.aggregates_observations(list_files, cut_before='2022-01-01', verbose=False)[source]#

Aggregate the observations for each department of France.

Parameters:

list_files (list[str]) – the list of the files to aggregate.

Returns:

pd.DataFrame – the DataFrame containing the observations for each department.

energy_forecast.meteo.calculate_mean_group_value(masks, names, label, da_value, min_lon, max_lon, min_lat, max_lat)[source]#

Group the data by the masks and calculate the mean value for each group.

Parameters:
  • masks (xr.DataArray) – The masks to group the data. Must have the same dimensions longitude and Latitude as the data.

  • names (list[str]) – The names of the groups.

  • label (str) – the name of the column to use for the groups.

  • da_value (xr.DataArray) – The data to group.

Returns:

pd.DataFrame – The mean value for each group.

energy_forecast.meteo.download_historical_forecasts(s3_key, s3_secret, s3_entrypoint, s3_bucket, prefix='./', variables='all', forecast_type='all', dryrun=False)[source]#

Download the historical forecasts from the S3 bucket.

Parameters:
  • s3_key (str) – the key to access the S3 bucket.

  • s3_secret (str) – the secret to access the S3 bucket.

  • s3_entrypoint (str) – the entrypoint of the S3 bucket.

  • s3_bucket (str) – the name of the S3 bucket.

  • prefix (str) – The prefix where the files are downloaded. Should be similar to "./data/silver".

  • variables (str or list[str], optional) – the variables to download. Can be "wind_speed_hourly", "sun_flux_downward_hourly", or "temperature_hourly" or a list of these values. Default is "all", which downloads all the variables.

  • forecast_type (str or list[str], optional) – the forecast type to download. Can be "d0", "d1", "d2", or "d3", or a list of these values. Default is "all", which downloads all the forecast types.

  • dryrun (bool, optional) – if True, do not download the files. Default is False.

Returns:

list[Path] – the list of the files downloaded.

energy_forecast.meteo.download_observations(url, filename)[source]#

Download the observations from the url and save it in the filename.

Parameters:
  • url (str) – the url to download the data.

  • filename (str) – the filename to save the data.

energy_forecast.meteo.download_observations_all_departments(cache_duration='12h', file_type='latest-2024-2025_RR-T-Vent', verbose=False)[source]#

Download the temperature for each department of France.

energy_forecast.meteo.instant_flux_from_cumul(df_unstacked)[source]#

Compute the instant flux from the cumulated flux.

This is needed as the Weather Data provide the cumul of the sun flux.

Parameters:

df_unstacked (pd.DataFrame) – The DataFrame containing the cumulated flux. The index must be a DatetimeIndex or MultiIndex and the columns must be the regions of France.

Returns:

pd.DataFrame – The DataFrame containing the instant flux.

Notes

The function changes a lot if the index is a MultiIndex or not. Maybe it should be split into two functions ?

energy_forecast.meteo.warm_cache(logger, date=None, max_counter=30, sleep_duration=600)[source]#

Try to fetch the data from the API until it is successful.

Parameters:
  • logger (logging.Logger) – the logger to use.

  • date (str, optional) – the date at which the weather forecast was computed. Must be a valid date format, e.g. "YYYY-MM-DD" Default is the current date.

  • max_counter (int, optional) – the maximum number of attempts. Default is 30.

  • sleep_duration (int, optional) – the duration to sleep between each attempt in seconds. Default is 600 (10 minutes).

Raises:

TimeoutError – if the maximum number of attempts is reached.