{ "cells": [ { "cell_type": "markdown", "id": "2f80383f", "metadata": {}, "source": [ "# Oldest timestamp example\n", "\n", "This notebook shows how to retrieve the oldest available timestamp for one or more locations.\n", "\n", "API-24SEA endpoint: [https://api.24sea.eu/routes/v1/datasignals/oldest_timestamp](https://api.24sea.eu/docs/v1/#/operations/datasignals_metrics_oldest_timestamp)\n" ] }, { "cell_type": "code", "execution_count": 10, "id": "08d7bbbb", "metadata": {}, "outputs": [], "source": [ "# **Package Imports**\n", "# - From the Python Standard Library\n", "import logging\n", "import os\n", "import sys\n", "\n", "# - API-24SEA\n", "from api_24sea.version import __version__, parse_version\n", "from api_24sea.datasignals.core import API, AsyncAPI\n" ] }, { "cell_type": "code", "execution_count": 11, "id": "29c30e70", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Package Version(major=2, minor=1, patch=6, release=None, num=None)\n" ] } ], "source": [ "# **Package Version**\n", "print(f\"Package {parse_version(__version__)}\")\n", "\n", "# **Notebook Configuration**\n", "logger = logging.getLogger()\n", "logger.setLevel(logging.WARNING)\n" ] }, { "cell_type": "markdown", "id": "4efc9f22", "metadata": {}, "source": [ "
\n", "

Login Credentials

\n", " \n", " \n", " \n", " \n", "

Do not store API credentials in plain text in your notebook. Rather use the python-dotenv package to load environment variables from a .env file.

\n", "
" ] }, { "cell_type": "code", "execution_count": 12, "id": "983b6e2e", "metadata": {}, "outputs": [], "source": [ "# **Set Sample API Credentials**\n", "os.environ[\"API_24SEA_USERNAME\"] = \"Sample.User\"\n", "os.environ[\"API_24SEA_PASSWORD\"] = \"CheckOutSomeData!\"\n" ] }, { "cell_type": "code", "execution_count": null, "id": "e7b5b41d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Package Version(major=2, minor=1, patch=6, release=None, num=None)\n" ] } ], "source": [ "# **Package Versions**\n", "print(f\"Package {parse_version(__version__)}\")\n", "# **Notebook Configuration**\n", "logger = logging.getLogger()\n", "logger.setLevel(logging.INFO)\n" ] }, { "cell_type": "code", "execution_count": 14, "id": "75cc3229", "metadata": { "lines_to_next_cell": 2 }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "HTTP Request: GET https://api.24sea.eu/routes/v1/datasignals/profile/?username=Sample.User \"HTTP/1.1 200 OK\"\n", "\u001b[32;1mSample.User has access to \u001b[4mhttps://api.24sea.eu/routes/v1/.\u001b[0m\n", "Now getting your metrics_overview table...\n", "HTTP Request: GET https://api.24sea.eu/routes/v1/datasignals/metrics/?project=&locations=&metrics= \"HTTP/1.1 200 OK\"\n", "HTTP Request: GET https://api.24sea.eu/routes/v1/datasignals/profile/?username=Sample.User \"HTTP/1.1 200 OK\"\n", "\u001b[32;1mSample.User has access to \u001b[4mhttps://api.24sea.eu/routes/v1/.\u001b[0m\n", "Now getting your metrics_overview table...\n", "HTTP Request: GET https://api.24sea.eu/routes/v1/datasignals/metrics/?project=&locations=&metrics= \"HTTP/1.1 200 OK\"\n", "\u001b[32;1mLocations selected for the query:\u001b[0m\n", "\n", " site location\n", "0 WindFarm WFA01\n", "150 WindFarm WFA02\n", "\u001b[32;1m⏳ Getting oldest timestamps for windfarm at the following locations:\n", " • WFA01\n", " • WFA02\n", "\u001b[0m\n", "HTTP Request: GET https://api.24sea.eu/routes/v1/datasignals/oldest_timestamp/?project=windfarm&locations=WFA01%2CWFA02 \"HTTP/1.1 200 OK\"\n", "\u001b[32;1mLocations selected for the query:\u001b[0m\n", "\n", " site location\n", "0 WindFarm WFA01\n", "150 WindFarm WFA02\n", "HTTP Request: GET https://api.24sea.eu/routes/v1/datasignals/oldest_timestamp/?project=windfarm&locations=wfa01%2Cwfa02 \"HTTP/1.1 200 OK\"\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sitelocationoldest_timestamp
0windfarmwfa012020-03-01T00:00:00+00:00
1windfarmwfa022020-03-01T00:00:00+00:00
\n", "
" ], "text/plain": [ " site location oldest_timestamp\n", "0 windfarm wfa01 2020-03-01T00:00:00+00:00\n", "1 windfarm wfa02 2020-03-01T00:00:00+00:00" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sitelocationoldest_timestamp
0windfarmwfa012020-03-01T00:00:00+00:00
1windfarmwfa022020-03-01T00:00:00+00:00
\n", "
" ], "text/plain": [ " site location oldest_timestamp\n", "0 windfarm wfa01 2020-03-01T00:00:00+00:00\n", "1 windfarm wfa02 2020-03-01T00:00:00+00:00" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# **API Initialization**\n", "# API initialization can happen either explicitly via the API class, or\n", "# implicitly when using the pandas `datasignals` accessor.\n", "# In this example we will use the Pandas accessor.\n", "api = API()\n", "asyncapi = AsyncAPI()\n", "# The Metrics Overview is a table containing all the locations and metrics\n", "# available in the API, together with their metadata. It is useful to query it\n", "# before getting data, to check which locations and metrics are available for\n", "# the site you want to query.\n", "m_o = api.metrics_overview\n", "\n", "# site is the wind farm name you want to query. The parameter can also be\n", "# a list of site names, e.g. [\"windfarm\", \"windfarm2\"]. Matching is\n", "# case-insensitive and passing the \"site-id\" is also accepted, e.g. \"windfarm\"\n", "# will match \"WindFarm\", but also \"WF\", \"wf\", etc.\n", "site = \"windfarm\"\n", "# Matching locations from Metrics Overview for the specified site\n", "# Also partial names of locations are accepted, e.g. \"a01\" will match\n", "# all locations containing \"a01\" in their name, such as \"A01\", \"a01\". Matching\n", "# is case-insensitive.\n", "locations = m_o[m_o[\"site\"].str.lower() == site][\"location\"].unique().tolist()\n", "oldestdt_df_async = await asyncapi.get_oldest_timestamp(site, locations)\n", "oldestdt_df_sync = api.get_oldest_timestamp(site, locations)\n", "\n", "display(oldestdt_df_async)\n", "display(oldestdt_df_sync)\n" ] } ], "metadata": { "jupytext": { "custom_cell_magics": "kql", "encoding": "# -*- coding: utf-8 -*-" }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.1" } }, "nbformat": 4, "nbformat_minor": 5 }