Skip to content

Instantly share code, notes, and snippets.

@epassaro
Last active March 31, 2022 21:04
Show Gist options
  • Save epassaro/067a50c4a3158347c4ed01e342f514a0 to your computer and use it in GitHub Desktop.
Save epassaro/067a50c4a3158347c4ed01e342f514a0 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "6ec03352",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" ChiantiPy version 0.8.4 \n",
" found PyQt5 widgets\n",
" using PyQt5 widgets\n"
]
}
],
"source": [
"import pandas as pd\n",
"import warnings\n",
"from carsus.io.util import convert_atomic_number2symbol\n",
"\n",
"warnings.filterwarnings(\"ignore\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "82b70d6f",
"metadata": {},
"outputs": [],
"source": [
"ATOM_DATA = '/home/epassaro/Downloads/tardis-data/kurucz_cd23_chianti_H_He.h5'\n",
"store = pd.HDFStore(ATOM_DATA)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "9ced6a99",
"metadata": {},
"outputs": [],
"source": [
"UUID1 = store.root._v_attrs[\"uuid1\"].decode('ascii')\n",
"MD5 = store.root._v_attrs[\"md5\"].decode(\"ascii\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "cffa45a2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"MD5: 864f1753714343c41f99cb065710cace \n",
"UUID1: 6f7b09e887a311e7a06b246e96350010\n"
]
}
],
"source": [
"print(f'MD5: {MD5} \\nUUID1: {UUID1}')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "91ca7bed",
"metadata": {},
"outputs": [],
"source": [
"levels = store['levels']\n",
"levels = levels.reset_index(['level_number'])\n",
"lines = store['lines']\n",
"lines = lines.reset_index(['level_number_lower', 'level_number_upper'])\n",
"ions_all = sorted(lines.index.unique())"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "74762c1a",
"metadata": {},
"outputs": [],
"source": [
"MAX_IONIZ_STAGE = 5 # V\n",
"\n",
"ions_all = [(ion[0], ion[1]) for ion in ions_all if ion[1] < MAX_IONIZ_STAGE]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "7d5f1efb",
"metadata": {},
"outputs": [],
"source": [
"def make_latex_table(elements, longtable=False, column_format='lrrr', caption=None, label=None):\n",
"\n",
" ions = []\n",
" n_levels = []\n",
" m_levels = []\n",
" n_lines = []\n",
" \n",
" for ion in ions_all:\n",
" if ion[0] in elements:\n",
" \n",
" n1 = levels.loc[ion].count()[0]\n",
" m = levels.loc[ion]['metastable'].sum()\n",
" n2 = lines.loc[ion].count()[0]\n",
"\n",
" n_levels.append(n1)\n",
" m_levels.append(m)\n",
" n_lines.append(n2)\n",
"\n",
" ions.append(ion)\n",
"\n",
" table = pd.DataFrame(zip(ions, n_levels, m_levels, n_lines,), columns=['Ion', '$N_\\mathrm{levels}$', '$N_\\mathrm{meta}$' , '$N_\\mathrm{lines}$'])\n",
" table['Ion'] = table['Ion'].apply(lambda x: f'\\ion{{{convert_atomic_number2symbol(x[0])}}}{{{x[1]+1}}}')\n",
"\n",
" total = table.sum(numeric_only=True).to_frame().transpose()\n",
" total['Ion'] = 'Total'\n",
" total = total.applymap(lambda x: f'\\textbf{{{x}}}')\n",
"\n",
" table = pd.concat([table, total], ignore_index=True)\n",
" table = table.to_latex(index=False, escape=False, longtable=longtable, column_format=column_format, caption=caption)\n",
" \n",
" # replace rules with lines\n",
" table = table.replace('\\\\toprule', '\\\\hline')\n",
" table = table.replace('\\\\midrule', '\\\\hline')\n",
" table = table.replace('\\\\bottomrule', '\\\\hline')\n",
"\n",
" return table"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "78a0459d",
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"# C, O, Si, S, Ca, Ti\n",
"toy = [6, 8, 14, 16, 20, 22]\n",
"\n",
"# C, O, Ne, Na, Mg, Al, Si, S, Cl, Ar, K, Ca, Sc, Ti, V, Cr, Mn, Fe, Co, Ni\n",
"ddc = [6, 8, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "044e663f",
"metadata": {},
"outputs": [],
"source": [
"label = 'tardisdata'\n",
"caption = f'Bound-bound atomic data information from the Kurucz CD 23 linelist for all models computed with \\\\textsc{{tardis}}\\label{{tab:{label}}}.'"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "96ef0358",
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\\begin{longtable}{lrrr}\n",
"\\caption{Bound-bound atomic data information from the Kurucz CD 23 linelist for all models computed with \\textsc{tardis}\\label{tab:tardisdata}.}\\\\\n",
"\\hline\n",
" Ion & $N_\\mathrm{levels}$ & $N_\\mathrm{meta}$ & $N_\\mathrm{lines}$ \\\\\n",
"\\hline\n",
"\\endhead\n",
"\\hline\n",
"\\multicolumn{4}{r}{{Continued on next page}} \\\\\n",
"\\hline\n",
"\\endfoot\n",
"\n",
"\\hline\n",
"\\endlastfoot\n",
" \\ion{C}{1} & 833 & 603 & 3249 \\\\\n",
" \\ion{C}{2} & 86 & 6 & 374 \\\\\n",
" \\ion{C}{3} & 81 & 6 & 388 \\\\\n",
" \\ion{C}{4} & 36 & 1 & 192 \\\\\n",
" \\ion{O}{1} & 150 & 9 & 854 \\\\\n",
" \\ion{O}{2} & 173 & 6 & 1374 \\\\\n",
" \\ion{O}{3} & 141 & 9 & 766 \\\\\n",
" \\ion{O}{4} & 146 & 19 & 465 \\\\\n",
" \\ion{O}{5} & 97 & 5 & 459 \\\\\n",
" \\ion{Ne}{1} & 284 & 23 & 2422 \\\\\n",
" \\ion{Ne}{2} & 283 & 9 & 3468 \\\\\n",
" \\ion{Ne}{3} & 64 & 9 & 269 \\\\\n",
" \\ion{Ne}{4} & 99 & 12 & 340 \\\\\n",
" \\ion{Ne}{5} & 64 & 13 & 164 \\\\\n",
" \\ion{Na}{1} & 58 & 1 & 334 \\\\\n",
" \\ion{Na}{2} & 35 & 3 & 171 \\\\\n",
" \\ion{Na}{3} & 69 & 4 & 353 \\\\\n",
" \\ion{Na}{4} & 46 & 5 & 110 \\\\\n",
" \\ion{Na}{5} & 71 & 10 & 187 \\\\\n",
" \\ion{Mg}{1} & 552 & 366 & 1580 \\\\\n",
" \\ion{Mg}{2} & 75 & 2 & 510 \\\\\n",
" \\ion{Mg}{3} & 93 & 4 & 704 \\\\\n",
" \\ion{Mg}{4} & 54 & 5 & 169 \\\\\n",
" \\ion{Mg}{5} & 53 & 5 & 132 \\\\\n",
" \\ion{Al}{1} & 273 & 160 & 482 \\\\\n",
" \\ion{Al}{2} & 197 & 10 & 2602 \\\\\n",
" \\ion{Al}{3} & 58 & 1 & 342 \\\\\n",
" \\ion{Al}{4} & 31 & 3 & 142 \\\\\n",
" \\ion{Al}{5} & 56 & 9 & 77 \\\\\n",
" \\ion{Si}{1} & 558 & 230 & 3856 \\\\\n",
" \\ion{Si}{2} & 100 & 13 & 567 \\\\\n",
" \\ion{Si}{3} & 169 & 10 & 1248 \\\\\n",
" \\ion{Si}{4} & 52 & 1 & 307 \\\\\n",
" \\ion{Si}{5} & 35 & 3 & 125 \\\\\n",
" \\ion{S}{1} & 153 & 12 & 727 \\\\\n",
" \\ion{S}{2} & 85 & 7 & 500 \\\\\n",
" \\ion{S}{3} & 58 & 22 & 170 \\\\\n",
" \\ion{S}{4} & 28 & 5 & 50 \\\\\n",
" \\ion{S}{5} & 19 & 5 & 41 \\\\\n",
" \\ion{Cl}{1} & 229 & 23 & 2542 \\\\\n",
" \\ion{Cl}{2} & 128 & 21 & 973 \\\\\n",
" \\ion{Cl}{3} & 78 & 13 & 431 \\\\\n",
" \\ion{Cl}{4} & 33 & 5 & 121 \\\\\n",
" \\ion{Cl}{5} & 27 & 5 & 43 \\\\\n",
" \\ion{Ar}{1} & 215 & 8 & 2397 \\\\\n",
" \\ion{Ar}{2} & 314 & 26 & 4567 \\\\\n",
" \\ion{Ar}{3} & 96 & 16 & 655 \\\\\n",
" \\ion{Ar}{4} & 39 & 7 & 104 \\\\\n",
" \\ion{Ar}{5} & 22 & 6 & 49 \\\\\n",
" \\ion{K}{1} & 94 & 15 & 575 \\\\\n",
" \\ion{K}{2} & 22 & 4 & 66 \\\\\n",
" \\ion{K}{3} & 40 & 5 & 192 \\\\\n",
" \\ion{K}{4} & 24 & 5 & 57 \\\\\n",
" \\ion{K}{5} & 33 & 9 & 75 \\\\\n",
" \\ion{Ca}{1} & 198 & 5 & 2906 \\\\\n",
" \\ion{Ca}{2} & 93 & 3 & 752 \\\\\n",
" \\ion{Ca}{3} & 150 & 13 & 1766 \\\\\n",
" \\ion{Ca}{4} & 70 & 26 & 122 \\\\\n",
" \\ion{Ca}{5} & 39 & 5 & 91 \\\\\n",
" \\ion{Sc}{1} & 272 & 11 & 4221 \\\\\n",
" \\ion{Sc}{2} & 168 & 14 & 2215 \\\\\n",
" \\ion{Sc}{3} & 43 & 3 & 217 \\\\\n",
" \\ion{Sc}{4} & 127 & 13 & 953 \\\\\n",
" \\ion{Sc}{5} & 22 & 6 & 29 \\\\\n",
" \\ion{Ti}{1} & 441 & 38 & 8771 \\\\\n",
" \\ion{Ti}{2} & 204 & 37 & 2597 \\\\\n",
" \\ion{Ti}{3} & 199 & 13 & 2289 \\\\\n",
" \\ion{Ti}{4} & 39 & 3 & 139 \\\\\n",
" \\ion{Ti}{5} & 51 & 13 & 331 \\\\\n",
" \\ion{V}{1} & 502 & 60 & 6995 \\\\\n",
" \\ion{V}{2} & 323 & 75 & 4545 \\\\\n",
" \\ion{V}{3} & 299 & 35 & 5304 \\\\\n",
" \\ion{V}{4} & 98 & 13 & 995 \\\\\n",
" \\ion{V}{5} & 64 & 3 & 335 \\\\\n",
" \\ion{Cr}{1} & 394 & 83 & 4172 \\\\\n",
" \\ion{Cr}{2} & 733 & 97 & 17224 \\\\\n",
" \\ion{Cr}{3} & 214 & 71 & 2122 \\\\\n",
" \\ion{Cr}{4} & 154 & 35 & 1717 \\\\\n",
" \\ion{Cr}{5} & 46 & 13 & 220 \\\\\n",
" \\ion{Mn}{1} & 322 & 52 & 3023 \\\\\n",
" \\ion{Mn}{2} & 569 & 94 & 8362 \\\\\n",
" \\ion{Mn}{3} & 391 & 95 & 5848 \\\\\n",
" \\ion{Mn}{4} & 103 & 40 & 677 \\\\\n",
" \\ion{Mn}{5} & 84 & 35 & 602 \\\\\n",
" \\ion{Fe}{1} & 848 & 68 & 22905 \\\\\n",
" \\ion{Fe}{2} & 796 & 85 & 21753 \\\\\n",
" \\ion{Fe}{3} & 566 & 97 & 9860 \\\\\n",
" \\ion{Fe}{4} & 276 & 99 & 3559 \\\\\n",
" \\ion{Fe}{5} & 180 & 71 & 1865 \\\\\n",
" \\ion{Co}{1} & 317 & 46 & 5298 \\\\\n",
" \\ion{Co}{2} & 256 & 47 & 2853 \\\\\n",
" \\ion{Co}{3} & 213 & 58 & 2247 \\\\\n",
" \\ion{Co}{4} & 296 & 96 & 4092 \\\\\n",
" \\ion{Co}{5} & 267 & 94 & 3542 \\\\\n",
" \\ion{Ni}{1} & 180 & 17 & 2671 \\\\\n",
" \\ion{Ni}{2} & 717 & 25 & 17150 \\\\\n",
" \\ion{Ni}{3} & 344 & 47 & 5456 \\\\\n",
" \\ion{Ni}{4} & 235 & 70 & 2712 \\\\\n",
" \\ion{Ni}{5} & 323 & 101 & 4733 \\\\\n",
" \\textbf{Total} & \\textbf{19135} & \\textbf{3819} & \\textbf{243353} \\\\\n",
"\\end{longtable}\n",
"\n"
]
}
],
"source": [
"print(make_latex_table(ddc, longtable=True, caption=caption, label=label))"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "beea3952",
"metadata": {},
"outputs": [],
"source": [
"store.close()"
]
}
],
"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.6.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment