{ "cells": [ { "cell_type": "markdown", "id": "stretch-villa", "metadata": {}, "source": [ "# Instrument reader\n", "When McStasScript performs a McStas simulation, a traditional instrument file is generated and used for the simulation. In that way a user can easily go from a McStasScript instrument description to a regular instrument file. McStasScript does contain an instrument reader that can attempt to do the reverse, read a regular instrument file and create the McStasScript object or even a file with the python code that would create such an object. The [instrument reader](../_autosummary/mcstasscript.interface.reader.rst) contains the interface, but the system is not yet finished and can be somewhat unreliable. \n", "\n", "The instrument reader is a good way to transfer a project to McStasScript, but may require fixing of a few issues created in the transfer process." ] }, { "cell_type": "markdown", "id": "neutral-moore", "metadata": {}, "source": [ "## Create a reader object\n", "The first step is to create a reader object with [*McStas_file*](../_autosummary/mcstasscript.interface.reader.McStas_file.rst), this requires just a path to the instrument file." ] }, { "cell_type": "code", "execution_count": null, "id": "alpine-yeast", "metadata": {}, "outputs": [], "source": [ "import mcstasscript as ms" ] }, { "cell_type": "code", "execution_count": null, "id": "burning-romance", "metadata": {}, "outputs": [], "source": [ "instrument_reader = ms.McStas_file(\"data_example.instr\")" ] }, { "cell_type": "markdown", "id": "corporate-offer", "metadata": {}, "source": [ "## Create instrument object\n", "The [*McStas_file*](../_autosummary/mcstasscript.interface.reader.McStas_file.rst) object contains a method called *add_to_instr* that can add the instrument information to an [instrument](../_autosummary/mcstasscript.interface.instr.McCode_instr) object. Here a [McStas_instr](../_autosummary/mcstasscript.interface.instr.McStas_instr) object is initialized, and then the information to the instrument file is added with the instrument reader object." ] }, { "cell_type": "code", "execution_count": null, "id": "living-inflation", "metadata": {}, "outputs": [], "source": [ "instrument = ms.McStas_instr(\"reader_test\")\n", "instrument_reader.add_to_instr(instrument)\n", "\n", "instrument.show_parameters()\n", "instrument.show_variables()\n", "instrument.show_components()" ] }, { "cell_type": "markdown", "id": "owned-particle", "metadata": {}, "source": [ "If the instrument file used is for McStas 3.X and uses the USERVARS feature, it will fail when being executed with McStas 2.X. McStasScript includes a method to move any USERVARS to declare, which may allow the instrument to run with a McStas 2.X backend. " ] }, { "cell_type": "code", "execution_count": null, "id": "divided-restoration", "metadata": {}, "outputs": [], "source": [ "instrument.move_user_vars_to_declare()" ] }, { "cell_type": "markdown", "id": "modern-happiness", "metadata": {}, "source": [ "## Create a python script file\n", "It is also possible to get a script file from the [McStas_file](../_autosummary/mcstasscript.interface.reader.McStas_file.rst) object. A name for the script needs to be provided. The force keyword can be used to overwrite the file." ] }, { "cell_type": "code", "execution_count": null, "id": "australian-possession", "metadata": {}, "outputs": [], "source": [ "instrument_reader.write_python_file(\"generated_python_script.py\", force=True)" ] }, { "cell_type": "markdown", "id": "religious-lotus", "metadata": {}, "source": [ "The generated file is loaded and displayed to show that the python script is of reasonable quality, but still has room for improvement. " ] }, { "cell_type": "code", "execution_count": null, "id": "operational-earthquake", "metadata": {}, "outputs": [], "source": [ "with open(\"generated_python_script.py\") as file:\n", " script = file.read()\n", "print(script)" ] }, { "cell_type": "code", "execution_count": null, "id": "expected-lambda", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "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.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }