{ "cells": [ { "cell_type": "markdown", "id": "nervous-sight", "metadata": {}, "source": [ "# Using !-string and #-string commands\n", "\n", "## !-strings are for setting simulation parameters\n", "\n", "!-strings are a convenient way of accessing multiple layers of a nested dictionary structure with a single string using the format:\n", "\n", " \"!.....\"\n", " \n", "Any level of the nested dictionary can be reached by truncating the keyword.\n", "\n", "**Note: !-strings only work on `UserCommands` objects**\n", "\n", "Below is an example of how to use !-strings, using the example optical train." ] }, { "cell_type": "code", "execution_count": null, "id": "loved-franchise", "metadata": {}, "outputs": [], "source": [ "import scopesim as sim\n", "opt = sim.load_example_optical_train()" ] }, { "cell_type": "code", "execution_count": null, "id": "uniform-cursor", "metadata": {}, "outputs": [], "source": [ "opt.cmds[\"!ATMO\"]" ] }, { "cell_type": "code", "execution_count": null, "id": "domestic-chemical", "metadata": {}, "outputs": [], "source": [ "opt.cmds[\"!ATMO.background\"]" ] }, { "cell_type": "code", "execution_count": null, "id": "earned-indicator", "metadata": {}, "outputs": [], "source": [ "opt.cmds[\"!ATMO.background.filter_name\"]" ] }, { "cell_type": "markdown", "id": "selected-backup", "metadata": {}, "source": [ "## #-strings are for accessing Effect object parameters\n", "\n", "Similar to !-strings, #-strings allow us to get at the preset values inside the Effect-objects of the optical system. #-strings allow us to pring the contents of an effect's meta dictionary.\n", "\n", "**Note: !-strings only work on `OpticalTrain` objects**\n", "\n", "Here, we're again using the example optical train defined above. First let's list the effects:" ] }, { "cell_type": "code", "execution_count": null, "id": "hydraulic-astrology", "metadata": {}, "outputs": [], "source": [ "opt.effects" ] }, { "cell_type": "markdown", "id": "paperback-outreach", "metadata": {}, "source": [ "We list the meta dictionary contents by using the string format \n", "\n", " \"#.\"\n", " \n", "**Note: The `.` at the end is important, otherwise the optical train will look for a non-existant effect named `#`**" ] }, { "cell_type": "code", "execution_count": null, "id": "exterior-romania", "metadata": {}, "outputs": [], "source": [ "opt[\"#exposure_action.\"]" ] }, { "cell_type": "markdown", "id": "invisible-contrary", "metadata": {}, "source": [ "We print a specific meta parameter by adding it after the `.`" ] }, { "cell_type": "code", "execution_count": null, "id": "independent-benjamin", "metadata": {}, "outputs": [], "source": [ "opt[\"#exposure_action.ndit\"]" ] }, { "cell_type": "markdown", "id": "dying-appeal", "metadata": {}, "source": [ "Notice that the value of this dictionary entry is itself a !-string. We can resolve this by adding a `!` to the end of the string, to force it to get the actual value from `opt.cmds`:" ] }, { "cell_type": "code", "execution_count": null, "id": "internal-capital", "metadata": {}, "outputs": [], "source": [ "opt[\"#exposure_action.ndit!\"]" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.16" } }, "nbformat": 4, "nbformat_minor": 5 }