{ "cells": [ { "cell_type": "markdown", "id": "b2c9066b-db07-4ebc-aebe-41691abad1c0", "metadata": {}, "source": [ "## Writing to a file target" ] }, { "cell_type": "code", "execution_count": 1, "id": "c32a1f28-fd92-42df-ba34-5e5d410fa4ce", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "", "version_major": 2, "version_minor": 0 }, "text/plain": [ "test.grib: 0%| | 0.00/1.03k [00:00` is automatically guessed from the input data. Alternatively, we can create a target object with :func:`create_target` and directly write to it." ] }, { "cell_type": "code", "execution_count": 2, "id": "216afc9e-c6b3-48c2-a62a-9aca346447df", "metadata": {}, "outputs": [], "source": [ "# calling to_target\n", "ds.to_target(\"file\", \"_res_t_file_handler.grib\")\n", "\n", "# using write on the target object\n", "with ekd.create_target(\"file\", \"_res_t_file_handler.grib\") as t:\n", " t.write(ds)" ] }, { "cell_type": "markdown", "id": "253b32b5-d6cd-41f7-96d4-5933931aec3a", "metadata": {}, "source": [ "#### Using a file-like object" ] }, { "cell_type": "markdown", "id": "41473048-e8a1-42b2-a39a-9cc7cf46617e", "metadata": {}, "source": [ "A file-like object passed to the target is not closed, even when the target is closed or created with a context manager." ] }, { "cell_type": "code", "execution_count": 3, "id": "90375bdd-2f72-40ad-a94c-4ab635acb54d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "fp = open(\"_res_t_file_handler.grib\", \"wb\")\n", "ds.to_target(\"file\", fp)\n", "\n", "# the file object is still open\n", "print(fp.closed)\n", "\n", "# we need to close it manually\n", "fp.close()" ] }, { "cell_type": "code", "execution_count": 4, "id": "348834e3-0197-4c79-ba27-738855c4d2eb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "fp = open(\"_res_t_file_handler.grib\", \"wb\")\n", "\n", "# the context manager will call close() on the target\n", "with ekd.create_target(\"file\", fp) as t:\n", " t.write(ds)\n", "\n", "# the file object is still open\n", "print(fp.closed)\n", "\n", "# we need to close it manually\n", "fp.close()" ] }, { "cell_type": "markdown", "id": "278183b2-f502-4b24-9975-f4e704fc14a9", "metadata": {}, "source": [ "The simplest solution to this problem is to use a context manager for the file-like object. " ] }, { "cell_type": "code", "execution_count": 5, "id": "63a34551-35d8-4262-8759-3e69fca05bc7", "metadata": {}, "outputs": [], "source": [ "with open(\"_res_t_file_handler.grib\", \"wb\") as fp:\n", " ds.to_target(\"file\", fp)" ] }, { "cell_type": "markdown", "id": "233e46d1-c052-405c-bbf8-9bad9f512497", "metadata": {}, "source": [ "#### Appending to a file" ] }, { "cell_type": "markdown", "id": "92fad9cb-f9f4-4e79-9357-b3d888504890", "metadata": {}, "source": [ "When using a file path we can use the ``append=True`` option to append to the output." ] }, { "cell_type": "code", "execution_count": 6, "id": "afe1de08-866a-4943-9918-4755f1e5ce68", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# calling to_target\n", "out_file = \"_res_t1_file_handler.grib\"\n", "if os.path.isfile(out_file):\n", " os.remove(out_file)\n", " \n", "ds[0].to_target(\"file\", out_file, append=True)\n", "len(ekd.from_source(\"file\", out_file))" ] }, { "cell_type": "code", "execution_count": 7, "id": "96583f35-48bd-4bc9-a4e6-811758d5ea06", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds[1].to_target(\"file\", out_file, append=True)\n", "len(ekd.from_source(\"file\", out_file))" ] }, { "cell_type": "code", "execution_count": null, "id": "3709c465-551b-4369-9b03-61ea5f50e56f", "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 }