Skip to main content

Facilitate the users' interaction with the 24SEA API (https://api/24sea/eu) by providing pandas interfaces to the API endpoints.

Project description

API 24SEA

api_24sea is a Python project designed to provide aid for the interaction with data from the 24SEA API.

Installation

The package supports Python 3.8 and above. To install it, run the following command in your terminal:

pip install api_24sea

DataSignals Usage

The following example shows the classical usage of the datasignals module.

  • The first step is to import the package and the necessary libraries.
  • Then, the environment variables are loaded from a .env file. This step is optional, since if any of the following names for user and password in the system, the package will authenticate automatically.
    • "API_24SEA_USERNAME", "24SEA_API_USERNAME", "TWOFOURSEA_API_USERNAME", "API_TWOFOURSEA_USERNAME" for the username.
    • "API_24SEA_PASSWORD", "24SEA_API_PASSWORD", "TWOFOURSEA_API_PASSWORD", "API_TWOFOURSEA_PASSWORD" for the password.
  • After that, API dataframe is initialized.
  • Finally, the user can get data from the API. The dataframe will authenticate lazily if the environment variables are loaded, or the user can authenticate manually before performing the data retrieval.

Importing the package

# %%
# **Package Imports**
# - From the Python Standard Library
import logging
import os
import sys

# - From third party libraries
import pandas as pd
import dotenv  # <-- Not necessary to api_24sea per se, but useful for
               #     loading environment variables. Install it with
               #     `pip install python-dotenv`

# - Local imports
from api_24sea.version import __version__, parse_version
import api_24sea
# %%
# **Package Versions**
print("Working Folder: ", os.getcwd())
print(f"Python Version: {sys.version}")
print(f"Pandas Version: {pd.__version__}")
print(f"Package {parse_version(__version__)}")
# **Notebook Configuration**
logging.basicConfig(level=logging.INFO)

Setting up the environment variables (optional)

This step assumes that you have a file structure similar to the following one:

.
├── env
│   └── .env
├── notebooks
│   └── example.ipynb
└── requirements.txt

The [.env]{.title-ref} file should look like this:

API_24SEA_USERNAME=your_username
API_24SEA_PASSWORD=your_password

With this in mind, the following code snippet shows how to load the environment variables from the [.env]{.title-ref} file:

# %%
# **Load Environment Variables from .env File**
_ = dotenv.load_dotenv("../env/.env")
if _:
    print("Environment Variables Loaded Successfully")
    print(os.getenv("API_24SEA_USERNAME"))
    # print(os.getenv("API_24SEA_PASSWORD"))
else:
    raise Exception("Environment Variables Not Loaded")

Initializing an empty dataframe

Initializing an empty dataframe is necessary to use the API, as here is where the data will be stored.

# %%
# **DataFrame initialization**
# The empty DataFrame is created beforehand because it needs to authenticate
# with the API to fetch the data.
df = pd.DataFrame()

Authentication (optional)

If any of the following names for user and password in the system, the package will authenticate automatically.

  • "API_24SEA_USERNAME", "24SEA_API_USERNAME", "TWOFOURSEA_API_USERNAME", "API_TWOFOURSEA_USERNAME" for the username.
  • "API_24SEA_PASSWORD", "24SEA_API_PASSWORD", "TWOFOURSEA_API_PASSWORD", "API_TWOFOURSEA_PASSWORD" for the password.

The user can also authenticate manually by calling the authenticate method from the DataFrame.

# %%
# **Authentication**
df.datasignals.authenticate("some_other_username", "some_other_password")

Alternatively, the user can authenticate with the API on DataFrame instantiation:

# %%
# **DataFrame initialization with authentication**
df = pd.DataFrame().datasignals.authenticate("some_other_username",
                                             "some_other_password")

Checking the available metrics

# %%
# **Metrics Overview**
# The metrics overview is a summary of the metrics available in the API and
# can be accessed from a hidden method in the DataSignals class.
df.datasignals._DataSignals__api.metrics_overview
# It will show all the available metrics with the corresponding units
# and the time window for which the user is allowed to get data

Getting sample data from the API

After loading the environment variables and authenticating with the API, the user can get data from 24SEA API endpoints.

The data is retrieved and stored in the DataFrame. All the metrics are stored in separate columns, and the timestamps are set as the index of the DataFrame.

The data retrieval is done by specifying the sites or the locations or both, the metrics, and timestamps.

  • Sites: Case insensitive, it can either match [site]{.title-ref} or [site_id]{.title-ref}. It is an optional parameter.
  • Locations: Case insensitive, it can either match [location]{.title-ref} or [location_id]{.title-ref}. It is an optional parameter.
  • Metrics: Case insensitive, it can be a partial match of the metric name. If the site and location are specified, and metrics equals all, all the "allowed" metrics for the specified site and location will be retrieved.
  • Timestamps: Timezone-aware datetime, strings in ISO 8601 format, or shorthand strings compatible with the shorthand_datetime package.
# %%
# **Data Retrieval**
sites = ["wf"]

locations = ["a01", "a02"]

metrics = ["mean WinDSpEed", "mean pitch", "mean-Yaw", "mean_power"]
# Assigning metrics="all" will retrieve all the metrics available for the
# specified sites and locations.

start_timestamp = "2020-03-01T00:00:00Z"
end_timestamp = "2020-06-01T00:00:00Z"

df.datasignals.get_data(sites, locations, metrics,
                        start_timestamp, end_timestamp)

Checking the metrics selected and the data

# %%
df.datasignals.selected_metrics
df

Split the data by site and location

The as_dict method is used to split the data by site and location and return a dictionary of dictionaries of DataFrames.

# %%
# Data is a dictionary of dictionary of DataFrames in the shape of:
# {
#   "site1": {
#     "location1": DataFrame,
#     "location2": DataFrame,
#     ...
#   },
#   ...
# }
data = df.datasignals.as_dict()
# %%
# Retrieve the DataFrame for the windfarm WFA01 only
data["windfarm"]["WFA02"]

If df was defined using local data, rather than from API call, the user can still use the as_dict method to split the data by site and location by passing a metrics_map dataframe (i.e., the metrics overview table) from the datasignals app. For example:

import pandas as pd
import api_24sea
df = pd.DataFrame({
    "timestamp": ["2021-01-01", "2021-01-02"],
    "mean_WF_A01_windspeed": [10.0, 11.0],
    "mean_WF_A02_windspeed": [12.0, 13.0]
})
metrics_map = pd.DataFrame({
    "site": ["wf", "wf"],
    "location": ["a01", "a02"],
    "metric": ["mean_WF_A01_windspeed", "mean_WF_A02_windspeed"]
})
df.datasignals.as_dict(metrics_map)
# output
# {
#     "wf": {
#         "a01": pd.DataFrame({
#             "timestamp": ["2021-01-01", "2021-01-02"],
#             "mean_WF_A01_windspeed": [10.0, 11.0]
#         }),
#         "a02": pd.DataFrame({
#             "timestamp": ["2021-01-01", "2021-01-02"],
#             "mean_WF_A02_windspeed": [12.0, 13.0]
#         })
#     }
# }

Project Structure

.
├── .azure/
├── api_24sea/
│   ├── __init__.py
│   ├── datasignals/
│      ├── __init__.py
│      ├── fatigue.py
│      └── schemas.py
│   ├── core.py
│   ├── exceptions.py
│   ├── singleton.py
│   ├── utils.py
│   └── version.py
├── tests/
├── docs/
├── notebooks/
├── pyproject.toml
├── LICENSE
├── VERSION
└── README.md

License

The package is licensed under the GNU General Public License v3.0.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

api_24sea-1.4.19.tar.gz (42.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

api_24sea-1.4.19-py3-none-any.whl (39.6 kB view details)

Uploaded Python 3

File details

Details for the file api_24sea-1.4.19.tar.gz.

File metadata

  • Download URL: api_24sea-1.4.19.tar.gz
  • Upload date:
  • Size: 42.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.30

File hashes

Hashes for api_24sea-1.4.19.tar.gz
Algorithm Hash digest
SHA256 2099bcad7c3ac045f02a2ed1839852bf8edf11947b1dfd03fce80fd2fae2b395
MD5 653bb7a4055870ed086a3c218e1432ed
BLAKE2b-256 88e8894ebe26881dc7883b5022c3c070aeba742936207ad4a3ea0f0c46f430ee

See more details on using hashes here.

File details

Details for the file api_24sea-1.4.19-py3-none-any.whl.

File metadata

  • Download URL: api_24sea-1.4.19-py3-none-any.whl
  • Upload date:
  • Size: 39.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.30

File hashes

Hashes for api_24sea-1.4.19-py3-none-any.whl
Algorithm Hash digest
SHA256 8f701e711134cbe89206d6936de482503809015562de662d66bd71aacf162081
MD5 edbfd979005b0c43b7bebc5b5f6703e1
BLAKE2b-256 0bab8baffe6fa762e22969c3984fc36066f9206b660f41da1885fac04f500377

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page