{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Spectra exported from Topspin as follows\n", "- process (1D) spectrum with \"fp\"\n", "- aplly automatic phase correction of orders 0 and 1 with \"apk\"\n", "- autozoom\n", "- rightclick in display window to choose \"Save Display Region To...\" (chose not to save imaginary part)\n", "- -> a file is created with some header lines and the y-values for the real part of the spectrum\n", "- this python script searches the header lines for the x-range and pieces x- and y-values together." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "filename input: 298K-c\n" ] } ], "source": [ "#option to set a fixed path:\n", "# in pieces\n", "path_piece = \"/autohome/saeckech/Promotion/Messungen/Bayreuth/1GHz/Spectr1H/\"\n", "file_piece = str(input(\"filename input: \"))\n", "filedir = path_piece+file_piece\n", "# full path\n", "#filedir = \"/autohome/saeckech/Promotion/Messungen/Auswertung/PNIP-Bulk-in-D2O_NIPAM-Bulk-in-D2O_P90-T1/PNIPAM-CN0E_2H-NMR_InvRec_rTemp-MonoExp_2022-Sep_PTS46704360Hz_fitparameter_Aligned.dat\"" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "filename:\n", " 298K-c\n", "location:\n", " /autohome/saeckech/Promotion/Messungen/Bayreuth/1GHz/Spectra_1H/\n" ] } ], "source": [ "filename = filedir.split(\"/\")[-1]\n", "location = filedir.strip(filename)\n", "\n", "print(\"filename:\\n\",filename)\n", "print(\"location:\\n\",location)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#fixed search patterns\n", "identifier_chemshift_preline = \"# Spectral Region:\"\n", "identifier_numpoints = \"# SIZE = \"\n", "chemshift_regex_search = \"# LEFT = (-?\\d{1,3}.\\d+) ppm. RIGHT = (-?\\d{1,3}.\\d+) ppm.\"\n", "size_regex_search = \"# SIZE = (\\d+) \\( = number of points\\)\"\n", "\n", "#clear variables from potential previous executions of the cell\n", "chemshift_linenum = None\n", "numpoints_linenum = None\n", "chemshift_left = None\n", "chemshift_right = None\n", "size = None\n", "xvals = []\n", "yvals = []\n", "broken1 = \"bad\"\n", "broken2 = \"bad\"\n", "\n", "#process file\n", "with open(filedir) as file:\n", " lines = file.readlines()\n", " for line_num in range(len(lines)):\n", " if lines[line_num].startswith(identifier_chemshift_preline):\n", " if chemshift_linenum is None:\n", " chemshift_linenum = line_num+1\n", " elif chemshift_linenum is not None:\n", " print(\"found more than one match to identifier '\"+identifier_chemshift_preline+\"'! Breaking\")\n", " break\n", " if lines[line_num].startswith(identifier_numpoints):\n", " if numpoints_linenum is None:\n", " numpoints_linenum = line_num\n", " elif numpoints_linenum is not None:\n", " print(\"found more than one match to identifier '\"+identifier_numpoints+\"'! Breaking\")\n", " break\n", " if not lines[line_num].startswith(\"#\"):\n", " yvals.append(float(lines[line_num]))\n", " if chemshift_linenum is None:\n", " print(\"Could not find a match to identifier '\"+identifier_chemshift_preline+\"'.\")\n", " if numpoints_linenum is None:\n", " print(\"Could not find a match to identifier '\"+identifier_numpoints+\"'.\")\n", " chemshift_match = re.findall(chemshift_regex_search,lines[chemshift_linenum])\n", " if len(chemshift_match) == 1:\n", " chemshift_left = float(chemshift_match[0][0])\n", " chemshift_right = float(chemshift_match[0][1])\n", " size_match = re.findall(size_regex_search,lines[numpoints_linenum])\n", " if len(size_match) == 1:\n", " size = float(size_match[0])\n", " if not size.is_integer():\n", " print(\"Number of points in spectrum not recognized as integer!\")\n", " print(\"Prompting user input to draw attention to this.\")\n", " broken1 = input(\"This should not have happened\")\n", " for row_counter in range(len(yvals)):\n", " xvals.append(chemshift_left-row_counter*(chemshift_left-chemshift_right)/(size-1))\n", " if not xvals[0]==chemshift_left or not xvals[-1]==chemshift_right:\n", " print(\"xvalue assignment is off!\")\n", " print(\"Prompting user input to draw attention to this.\")\n", " broken2 = input(\"This should not have happened\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "df = pd.DataFrame(list(zip(xvals, yvals)), columns=[\"#chemical_shift_in_ppm\", \"spectrum_real_in_au\"])" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "#option to view spectrum\n", "view_plot = False\n", "if view_plot:\n", " df.plot(x=\"#chemical_shift_in_ppm\", y=\"spectrum_real_in_au\")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "save_as = location+filename #overwrite old file. May choose new filename instead." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Saved as:\n", " /autohome/saeckech/Promotion/Messungen/Bayreuth/1GHz/Spectra_1H/298K-c\n" ] } ], "source": [ "with open(save_as, \"w\") as file:\n", " file.write(df.to_csv(index=False, sep=\"\\t\"))\n", " #file.write(\"\\n\") #write end of line to please xmgrace's parser\n", " print(\"Saved as:\\n\", save_as)" ] }, { "cell_type": "code", "execution_count": null, "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.5" } }, "nbformat": 4, "nbformat_minor": 4 }