{ "cells": [ { "cell_type": "markdown", "id": "1273fa5f-9ab5-4a4c-857a-0f212e759f12", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## Configuration environment variables" ] }, { "cell_type": "code", "execution_count": 1, "id": "75bffc4a-24e8-4d2d-948d-2cb84bc11020", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "from earthkit.data import config" ] }, { "cell_type": "raw", "id": "022b7ed2-29f2-49fb-a619-ff86f45d7a48", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "For the rest of this notebook we disable the :ref:`config` autosave so the changes will not be written into our configuration file." ] }, { "cell_type": "code", "execution_count": 2, "id": "2072ae67-13df-4e8a-a1a4-d54a56639569", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "config.autosave = False" ] }, { "cell_type": "raw", "id": "0c497859-6d2a-4fb7-bfb0-0979eb0d2279", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "Each :ref:`config` parameter has a corresponding environment variable (see the full list :ref:`here `). When an environment variable is set, it takes precedence over the settings parameter as the following example demonstrates it." ] }, { "cell_type": "markdown", "id": "2888d0b6-beea-4178-9c01-5a08a5be7ad2", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "Assuming no environmental variable is set the value is read form the config file." ] }, { "cell_type": "code", "execution_count": 3, "id": "21f853d3-c961-4ad2-b200-c24f7b03a54e", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "30" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config.get(\"url-download-timeout\")" ] }, { "cell_type": "raw", "id": "95036166-0eec-437b-9988-771e522ada2e", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "When the environment variable is set :func:`get ` returns its value." ] }, { "cell_type": "code", "execution_count": 4, "id": "d4a8c3bd-19ca-454a-8f39-07de7a2d318c", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "env: EARTHKIT_DATA_URL_DOWNLOAD_TIMEOUT=26\n" ] } ], "source": [ "%env EARTHKIT_DATA_URL_DOWNLOAD_TIMEOUT=26" ] }, { "cell_type": "code", "execution_count": 5, "id": "dbc54f27-7b5e-467b-bcba-28daea0e3c46", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "26" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config.get(\"url-download-timeout\")" ] }, { "cell_type": "raw", "id": "4d784930-1a1e-482c-ae2d-7491a4066b47", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "Setting the value generates a warning. The new value is saved into the config file, but :func:`get ` still returns the value of the environment variable." ] }, { "cell_type": "code", "execution_count": 6, "id": "8ee6f835-ae42-4eae-bc96-a53c47f3ce06", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/cgr/git/earthkit-data/src/earthkit/data/core/config.py:407: UserWarning: Config option 'url-download-timeout' is also set by environment variable 'EARTHKIT_DATA_URL_DOWNLOAD_TIMEOUT'.The environment variable takes precedence and its value is returned when calling get().\n", " warnings.warn(msg)\n" ] }, { "data": { "text/plain": [ "26" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config.set(\"url-download-timeout\", 10)\n", "config.get(\"url-download-timeout\")" ] }, { "cell_type": "raw", "id": "0a24b839-a4d1-4340-bd87-7d690f0511f7", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "The :func:`env` method gives details about the set environment variables." ] }, { "cell_type": "code", "execution_count": 7, "id": "9c56e84d-ff1e-4415-b924-b461d62ddbbd", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "{'url-download-timeout': ('EARTHKIT_DATA_URL_DOWNLOAD_TIMEOUT', '26')}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config.env()" ] }, { "cell_type": "markdown", "id": "d3e74276-cf79-42f0-9b66-b59ac15829d3", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "When we dump the configuration the values set via environment variables are clearly indicated." ] }, { "cell_type": "code", "execution_count": 8, "id": "89a92e24-f7f5-4f04-bc9a-1d0d00f7c8cf", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/html": [ "
NameValueDefault
cache-policy'off''off'
check-out-of-date-urlsTrueTrue
download-out-of-date-urlsFalseFalse
grib-field-policy'persistent''persistent'
grib-handle-cache-size11
grib-handle-policy'cache''cache'
maximum-cache-disk-usage'95%''95%'
maximum-cache-sizeNoneNone
number-of-download-threads55
reader-type-check-bytes6464
temporary-cache-directory-rootNoneNone
temporary-directory-rootNoneNone
url-download-timeoutEARTHKIT_DATA_URL_DOWNLOAD_TIMEOUT='26'
(10)
'30s'
use-grib-metadata-cacheTrueTrue
use-message-position-index-cacheFalseFalse
use-standalone-mars-client-when-availableTrueTrue
user-cache-directory'/var/folders/93/w0p869rx17q98wxk83gn9ys40000gn/T/earthkit-data-cgr''/var/folders/93/w0p869rx17q98wxk83gn9ys40000gn/T/earthkit-data-cgr'
version'0.11.5.dev2+g384bbb0.d20241209'''
" ], "text/plain": [ "cache-policy: (off, off)\n", "check-out-of-date-urls: (True, True)\n", "download-out-of-date-urls: (False, False)\n", "grib-field-policy: (persistent, persistent)\n", "grib-handle-cache-size: (1, 1)\n", "grib-handle-policy: (cache, cache)\n", "maximum-cache-disk-usage: (95%, 95%)\n", "maximum-cache-size: (None, None)\n", "number-of-download-threads: (5, 5)\n", "reader-type-check-bytes: (64, 64)\n", "temporary-cache-directory-root: (None, None)\n", "temporary-directory-root: (None, None)\n", "url-download-timeout: (EARTHKIT_DATA_URL_DOWNLOAD_TIMEOUT=26, 10, 30s)\n", "use-grib-metadata-cache: (True, True)\n", "use-message-position-index-cache: (False, False)\n", "use-standalone-mars-client-when-available: (True, True)\n", "user-cache-directory: (/var/folders/93/w0p869rx17q98wxk83gn9ys40000gn/T/earthkit-data-cgr, /var/folders/93/w0p869rx17q98wxk83gn9ys40000gn/T/earthkit-data-cgr)\n", "version: (0.11.5.dev2+g384bbb0.d20241209, )" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config" ] }, { "cell_type": "code", "execution_count": null, "id": "943ec49a-b11f-490f-9c58-7b942eaaeaa9", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "dev_ecc", "language": "python", "name": "dev_ecc" }, "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.10.13" } }, "nbformat": 4, "nbformat_minor": 5 }