{ "cells": [ { "cell_type": "markdown", "id": "53d7b7af-fc34-450c-9c39-493f3d881c49", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## Configuration" ] }, { "cell_type": "code", "execution_count": 1, "id": "55715969-c15d-4b7f-bf0f-459591359d76", "metadata": {}, "outputs": [], "source": [ "from earthkit.data import config" ] }, { "cell_type": "markdown", "id": "01b48692-a39d-4cfd-99dd-4047501edb54", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Config basics" ] }, { "attachments": {}, "cell_type": "raw", "id": "492ab3b0-edf8-421f-8b77-c508447d391c", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "The :ref:`config` object is loaded from the ``~/.config/earthkit/data/config.yaml`` file. Changes are immediately saved back into this file unless we explicitly disable it with ``config.autosave` or use a :ref:`temporary configuration `." ] }, { "cell_type": "markdown", "id": "bb06b2f7-5552-4247-843e-2e328beaabe9", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "For the rest of this notebook we disable the configuration autosave so the changes will not be written into our configuration file." ] }, { "cell_type": "code", "execution_count": 2, "id": "c5d47016-6f5b-4cbc-ac15-1d231546ac10", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "config.autosave = False" ] }, { "attachments": {}, "cell_type": "markdown", "id": "27d1d708-8090-43af-8be8-5d81eec4791b", "metadata": {}, "source": [ "We can display the current configuration and the default values with:" ] }, { "cell_type": "code", "execution_count": 3, "id": "953cdbe7-e159-47cf-bb27-613f1ec3fba3", "metadata": {}, "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-timeout'30s''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: (30s, 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": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config" ] }, { "cell_type": "raw", "id": "23b0893d-f6cd-42bc-b030-0917684b7786", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "We can use :ref:`get() ` to access the config values." ] }, { "cell_type": "code", "execution_count": 4, "id": "c477d8a3-eef8-4f73-86f1-64bdbabd13d5", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "30" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config.get(\"url-download-timeout\")" ] }, { "cell_type": "raw", "id": "c61b5316-04cc-455a-a51a-66717af22bfd", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "We can use :ref:`set() ` to change the values." ] }, { "cell_type": "code", "execution_count": 5, "id": "373e772d-52ac-4166-94a5-b7f501a4490f", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config.set(\"url-download-timeout\", 5)\n", "config.get(\"url-download-timeout\")" ] }, { "cell_type": "markdown", "id": "27182b2a-f49b-406d-af81-bbd77bef3b1b", "metadata": {}, "source": [ "Multiple values can be set together. The argument list can be a dictionary:" ] }, { "cell_type": "code", "execution_count": 6, "id": "f2c4e61b-5e91-4986-8987-21a3deeed750", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "True\n" ] } ], "source": [ "config.set({\"url-download-timeout\": 10, \"check-out-of-date-urls\": True})\n", "print(config.get(\"url-download-timeout\"))\n", "print(config.get(\"check-out-of-date-urls\"))" ] }, { "cell_type": "markdown", "id": "2b0e80b7-cc1d-47df-ac9c-8815581f2b87", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "Alternatively, we can use keyword arguments. However, because the \"-\" character is not allowed in variable names in Python we have to replace \"-\" with \"_\" in all the keyword arguments:" ] }, { "cell_type": "code", "execution_count": 7, "id": "ea398b6b-5d51-42fd-ae59-76b6140f7456", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "True\n" ] } ], "source": [ "config.set(url_download_timeout=10, check_out_of_date_urls=True)\n", "print(config.get(\"url-download-timeout\"))\n", "print(config.get(\"check-out-of-date-urls\"))" ] }, { "cell_type": "markdown", "id": "b7c8edff-9355-4144-aee9-e0a2a55f8eba", "metadata": {}, "source": [ "#### Temporary configuration" ] }, { "attachments": {}, "cell_type": "raw", "id": "08bca9e3-65f3-4557-8be0-be2c79f98fb7", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "We can create a :ref:`temporary configuration ` (as a context manager) as a copy of the original configuration. We will still refer to it as \"config\", but it is completely independent from the original object and changes are not saved into the yaml file (even when *config.auto_save* is True)." ] }, { "cell_type": "code", "execution_count": 8, "id": "5f63aca8-b609-4436-91d8-5e9b0e56a7c0", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "12\n" ] } ], "source": [ "with config.temporary():\n", " print(config.get(\"url-download-timeout\"))\n", " config.set(\"url-download-timeout\", 12)\n", " print(config.get(\"url-download-timeout\"))" ] }, { "cell_type": "markdown", "id": "b1f883bd-c63b-45dd-b333-de6ed682a62f", "metadata": {}, "source": [ "When we leave the context the config is reverted to the original one:" ] }, { "cell_type": "code", "execution_count": 9, "id": "f44da09d-4d5e-4ee1-babc-943e901aeb69", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config.get(\"url-download-timeout\")" ] }, { "cell_type": "markdown", "id": "2670d106-7e09-42ba-bc66-335d84c38905", "metadata": {}, "source": [ "A temporary configuration can also be created with arguments:" ] }, { "cell_type": "code", "execution_count": 10, "id": "15870fd7-e2a1-4674-a701-f85861e6eb8a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "12\n", "10\n" ] } ], "source": [ "with config.temporary(\"url-download-timeout\", 12):\n", " print(config.get(\"url-download-timeout\"))\n", "\n", "print(config.get(\"url-download-timeout\"))" ] }, { "cell_type": "markdown", "id": "313fbf8f-a540-449e-b340-5c46014d931c", "metadata": {}, "source": [ "#### Resetting to defaults" ] }, { "attachments": {}, "cell_type": "raw", "id": "323f7e0b-9126-4af5-9197-be98c60e2e1c", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "The :ref:`reset() ` method resets the config to the defaults. We demonstrate it on a temporary configuration:" ] }, { "cell_type": "code", "execution_count": 11, "id": "2cc244ba-e80f-4111-b309-6ea7bf1b8119", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "12\n", "10\n" ] } ], "source": [ "with config.temporary(\"url-download-timeout\", 12):\n", " print(config.get(\"url-download-timeout\"))\n", "\n", "print(config.get(\"url-download-timeout\"))" ] }, { "cell_type": "code", "execution_count": null, "id": "13f10d70-de59-4086-82d5-8d04d4c0907c", "metadata": {}, "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 }