├── environment.yml
├── README.md
└── isq-calculator.ipynb
/environment.yml:
--------------------------------------------------------------------------------
1 | channels:
2 | - conda-forge
3 | dependencies:
4 | - ipywidgets
5 | - appmode
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # astro-calculator
2 |
3 | [](https://mybinder.org/v2/gh/mwcraig/astro-calculator/master?urlpath=apps%2Fisq-calculator.ipynb)
4 |
--------------------------------------------------------------------------------
/isq-calculator.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Inverse square law calculator"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": null,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "import ipywidgets as ipw\n",
17 | "from math import pi"
18 | ]
19 | },
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {},
23 | "source": [
24 | "## How to enter numbers in scientific notation\n",
25 | "\n",
26 | "+ Replace \"×10\" with the letter \"E\" (or \"e\", upper and lower case are the same). \n",
27 | "+ For example, write $3.5 \\times 10^{-8}$ as `3.5e-8`. \n",
28 | "+ Another example: $9.2 \\times 10^{15} $ is `9.2e15`."
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": null,
34 | "metadata": {},
35 | "outputs": [],
36 | "source": [
37 | "def lum(B, d):\n",
38 | " return 4 * pi * d**2 * B"
39 | ]
40 | },
41 | {
42 | "cell_type": "code",
43 | "execution_count": null,
44 | "metadata": {},
45 | "outputs": [],
46 | "source": [
47 | "four_pi = ipw.Label('$4 \\pi$')\n",
48 | "equal = ipw.Label('$=$')\n",
49 | "\n",
50 | "textbox_layout = ipw.Layout(width='12em')\n",
51 | "\n",
52 | "def new_dist():\n",
53 | " return ipw.Text(description='', placeholder='Enter d (in parsec)', \n",
54 | " layout=textbox_layout)\n",
55 | "\n",
56 | "def new_bright():\n",
57 | " return ipw.Text(description='', placeholder='Enter B (in L_sun/pc^2)',\n",
58 | " layout=textbox_layout)\n",
59 | "\n",
60 | "def new_lum():\n",
61 | " return ipw.Text(description='', placeholder='Enter L (in L_sun)',\n",
62 | " layout=textbox_layout)"
63 | ]
64 | },
65 | {
66 | "cell_type": "code",
67 | "execution_count": null,
68 | "metadata": {},
69 | "outputs": [],
70 | "source": [
71 | "lum_holder = ipw.VBox()\n",
72 | "lum_header = ipw.HTML('
Calculate luminosity if you know distance and brightness
')\n",
73 | "\n",
74 | "lum_version = ipw.Box()\n",
75 | "l_eq = ipw.Label('$L = 4\\pi d^2 B = $')\n",
76 | "squared = ipw.Label('$^2$')\n",
77 | "lum_result = ipw.Label()\n",
78 | "dist_lum = new_dist()\n",
79 | "bright = new_bright()\n",
80 | "lum_version.children = [l_eq, four_pi, dist_lum, squared, bright, equal, lum_result]\n",
81 | "\n",
82 | "def input_changed(change):\n",
83 | " try:\n",
84 | " B = float(bright.value)\n",
85 | " d = float(dist_lum.value)\n",
86 | " except (ValueError, ZeroDivisionError):\n",
87 | " lum_result.value = ''\n",
88 | " else:\n",
89 | " L = lum(B, d)\n",
90 | " lum_result.value = '{:15.7g} L_sun'.format(L)\n",
91 | "\n",
92 | "dist_lum.observe(input_changed, names='value')\n",
93 | "bright.observe(input_changed, names='value')\n",
94 | "lum_holder.children = [lum_header, lum_version]"
95 | ]
96 | },
97 | {
98 | "cell_type": "code",
99 | "execution_count": null,
100 | "metadata": {},
101 | "outputs": [],
102 | "source": [
103 | "def dist(B, L):\n",
104 | " from math import sqrt\n",
105 | " try:\n",
106 | " return sqrt(L/(4 * pi * B))\n",
107 | " except ZeroDivisionError:\n",
108 | " return None"
109 | ]
110 | },
111 | {
112 | "cell_type": "code",
113 | "execution_count": null,
114 | "metadata": {},
115 | "outputs": [],
116 | "source": [
117 | "dist_holder = ipw.VBox()\n",
118 | "dist_version = ipw.Box()\n",
119 | "\n",
120 | "dist_header = ipw.HTML(\"Calculate distance if you know luminosity and brightness
\")\n",
121 | "bright_dist = new_bright()\n",
122 | "lum_box = new_lum()\n",
123 | "d_eq = ipw.Label('$d = \\sqrt{L/(4\\pi B)} = $')\n",
124 | "squirt = ipw.Label('$\\sqrt{}$')\n",
125 | "div_by = ipw.Label('/($4\\pi$')\n",
126 | "rpen = ipw.Label(')')\n",
127 | "\n",
128 | "dist_result = ipw.Label()\n",
129 | "\n",
130 | "def dist_input_changed(change):\n",
131 | " try:\n",
132 | " B = float(bright_dist.value)\n",
133 | " L = float(lum_box.value)\n",
134 | " except ValueError:\n",
135 | " dist_result.value = ''\n",
136 | " else:\n",
137 | " d = dist(B, L) \n",
138 | " if d is not None:\n",
139 | " dist_result.value = '{:15.7g} pc, which is {:15.7g} Mpc'.format(d, d/1e6)\n",
140 | " else:\n",
141 | " dist_result.value = ''\n",
142 | "\n",
143 | "bright_dist.observe(dist_input_changed, names='value')\n",
144 | "lum_box.observe(dist_input_changed, names='value')\n",
145 | "\n",
146 | "dist_version.children = [d_eq, squirt, lum_box, div_by, bright_dist, rpen, equal, dist_result]\n",
147 | "\n",
148 | "dist_holder.children = [dist_header, dist_version]"
149 | ]
150 | },
151 | {
152 | "cell_type": "code",
153 | "execution_count": null,
154 | "metadata": {},
155 | "outputs": [],
156 | "source": [
157 | "tabs = ipw.Tab()\n",
158 | "tabs.children = [dist_holder, lum_holder]\n",
159 | "tabs.set_title(0, 'Find distance')\n",
160 | "tabs.set_title(1, 'Find luminosity')\n",
161 | "tabs"
162 | ]
163 | }
164 | ],
165 | "metadata": {
166 | "kernelspec": {
167 | "display_name": "Python 3",
168 | "language": "python",
169 | "name": "python3"
170 | },
171 | "language_info": {
172 | "codemirror_mode": {
173 | "name": "ipython",
174 | "version": 3
175 | },
176 | "file_extension": ".py",
177 | "mimetype": "text/x-python",
178 | "name": "python",
179 | "nbconvert_exporter": "python",
180 | "pygments_lexer": "ipython3",
181 | "version": "3.7.3"
182 | },
183 | "toc": {
184 | "base_numbering": 1,
185 | "nav_menu": {},
186 | "number_sections": true,
187 | "sideBar": true,
188 | "skip_h1_title": true,
189 | "title_cell": "Table of Contents",
190 | "title_sidebar": "Contents",
191 | "toc_cell": true,
192 | "toc_position": {},
193 | "toc_section_display": true,
194 | "toc_window_display": false
195 | }
196 | },
197 | "nbformat": 4,
198 | "nbformat_minor": 2
199 | }
200 |
--------------------------------------------------------------------------------