Skip to content

Commit c1e06fe

Browse files
Constants v0.2.0a1
Now includes the submodule constants for astropy like constants
1 parent 41da3b0 commit c1e06fe

File tree

18 files changed

+387
-10
lines changed

18 files changed

+387
-10
lines changed

README.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<img width="100%" src="https://raw.githubusercontent.com/astroDimitrios/astroedu/e2d3007b867c3f5ac34d6bf4b2ca2f02aa38d824/assets/logo/astroeduLOGOtag.svg" alt='AP Logo'>
33
</center>
44

5+
[![PyPI version](https://badge.fury.io/py/astroedu.svg)](https://badge.fury.io/py/astroedu)
56
[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.0-4baaaa.svg)](code_of_conduct.md)
67

78
This package is in alpha.
@@ -46,4 +47,41 @@ Then you can load a data set by passing its name as a string to ```load_data```.
4647
```
4748
planets = load_data('planets')
4849
```
49-
The function returns a Pandas dataframe.
50+
The function returns a Pandas dataframe.
51+
52+
### Constants
53+
54+
Astropy like constants for ease of access.
55+
For full functionality use the astropy [constants](https://docs.astropy.org/en/stable/constants/) submodule.
56+
57+
```
58+
>>> from astroedu.constants import c
59+
>>> c
60+
Constant(c, 299792458, m/s, Speed of light)
61+
```
62+
or
63+
```
64+
>>> import astroedu.constants as const
65+
>>> print(const.c)
66+
Name = Speed of light
67+
Value = 299792458
68+
Unit = m/s
69+
```
70+
71+
Constants can perform simple maths with other constants or int/float/np.array.
72+
The returned value is an int/float/np.array not a Constant class instance:
73+
```
74+
>>> from astroedu.constants import c, m_e
75+
>>> c*m_e
76+
2.7309245302881346e-22
77+
```
78+
Caveat: Constant must be in front of the array.
79+
```
80+
>>> import numpy as np
81+
>>> from astroedu.constants import c
82+
>>> a = np.arange(4)
83+
>>> c*a
84+
array([ 0, 299792458, 599584916, 899377374])
85+
>>> a*c
86+
TypeError: unsupported operand type(s) for *: 'int' and 'Constant'
87+
```

build/lib/astroedu/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .functions import *
2+
from .classes import *

build/lib/astroedu/classes.py

Whitespace-only changes.

build/lib/astroedu/constants.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
wiens_displacement_const = 2.897771955*10**(-3) # m K
2+
boltzmans_const = 1.380649*10**(-23) # J/K
3+
speed_light = 299792458 # m/s
4+
plancks_const = 6.62607015*10**(-34) # Js
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .constants import *
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import numpy as np
2+
3+
class Constant:
4+
''' Class for defining all constants
5+
6+
For more functionality use astropy's constant instead
7+
https://docs.astropy.org/en/stable/constants/
8+
Most of the constants were taken from the page above
9+
10+
Example:
11+
>>> c = Constant('c', 299792458, 'm/s', 'Speed of light')
12+
>>> c
13+
299792458
14+
'''
15+
16+
def __init__(self, symbol, value, unit, name):
17+
self.symbol = symbol
18+
self.value = value
19+
self.unit = unit
20+
self.name = name
21+
22+
def __repr__(self):
23+
return f'Constant({self.symbol}, {self.value}, {self.unit}, {self.name})'
24+
25+
def __str__(self):
26+
return f'Name = {self.name}\nValue = {self.value}\nUnit = {self.unit}'
27+
28+
def __add__(self, other):
29+
if isinstance(other, Constant):
30+
return self.value + other.value
31+
else:
32+
return self.value + other
33+
34+
def __sub__(self, other):
35+
if isinstance(other, Constant):
36+
return self.value - other.value
37+
else:
38+
return self.value - other
39+
40+
def __mul__(self, other):
41+
if isinstance(other, Constant):
42+
return self.value * other.value
43+
else:
44+
return self.value * other
45+
46+
def __truediv__(self, other):
47+
if isinstance(other, Constant):
48+
return self.value / other.value
49+
else:
50+
return self.value / other
51+
52+
def __pow__(self, other):
53+
if isinstance(other, Constant):
54+
return self.value ** other.value
55+
else:
56+
return self.value ** other
57+
58+
atm = Constant('atm', 101325, 'Pa', 'Standard Atmosphere')
59+
60+
au = Constant('AU', 1.49597871e+11, 'm', 'Astronomical Unit')
61+
62+
b_wien = Constant('b', 2.897771955e-3, 'm K', 'Wiens Displacement constant')
63+
64+
c = Constant('c', 299792458, 'm/s', 'Speed of light')
65+
66+
e = Constant('e', 1.60217663e-19, 'C', 'Electron Charge')
67+
68+
g0 = Constant('g', 9.80665, 'm/s2', 'Standard acceleration of gravity')
69+
70+
G = Constant('G', 6.6743e-11, 'm3 / (kg s2)', 'Gravitational constant')
71+
72+
h = Constant('h', 6.62607015e-34, 'Js', 'Planck constant')
73+
74+
hbar = Constant('hbar', 1.05457182e-34, 'Js', 'Reduced Planck constant')
75+
76+
k_B = Constant('kB', 1.380649e-23, 'J/K', 'Boltzman constant')
77+
78+
L_bol0 = Constant('L_bol0', 3.0128e+28, 'W', 'Luminosity for absolute bolometric magnitude 0')
79+
80+
L_sun = Constant('L_sun', 3.828e+26, 'W', 'Nominal Solar Luminosity')
81+
82+
m_e = Constant('m_e', 9.1093837e-31, 'kg', 'Electron mass')
83+
84+
m_n = Constant('m_n', 1.6749275e-27, 'kg', 'Neutron mass')
85+
86+
m_p = Constant('m_p', 1.67262192e-27, 'kg', 'Proton mass')
87+
88+
M_earth = Constant('M_earth', 5.97216787e+24, 'kg', 'Earth mass')
89+
90+
M_jup = Constant('M_jup', 1.8981246e+27, 'kg', 'Jupiter mass')
91+
92+
M_sun = Constant('M_sun', 1.98840987e+30, 'kg', 'Solar mass')
93+
94+
pc = Constant('pc', 3.08567758e+16, 'm', 'Parsec')
95+
96+
R_earth = Constant('R_earth', 5.97216787e+24, 'm', 'Nominal Earth equatorial radius')
97+
98+
R_jup = Constant('R_jup', 1.8981246e+27, 'm', 'Nominal Jupiter equatorial radius')
99+
100+
R_sun = Constant('R_sun', 1.98840987e+30, 'm', 'Nominal solar radius')
101+
102+
sigma_sb = Constant('sigma_sb', 5.67037442e-08, 'W / (K4 m2)', 'Stefan-Boltzmann constant')
103+
104+
u = Constant('u', 1.66053907e-27, 'kg', 'Atomic mass unit')

build/lib/astroedu/functions.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import numpy as np
2+
from astroedu import constants as const
3+
4+
def wiens_law(a):
5+
''' Computes the peak wavelengths if temperatures are passed
6+
Computes the temperatures if peak wavelengths are passed
7+
8+
0 must not be passed as an argument since 1/0 will fail
9+
10+
Args:
11+
a -- float or 1D np.ndarray, peak wavelength or temperature
12+
13+
Returns:
14+
float or 1D np.ndarray, peak wavelength or temperature
15+
16+
Example:
17+
To get peak wavelengths pass the temperatures to arg a
18+
>>> T = np.arange(0.1, 3000, 10) # K
19+
>>> wiens_law(T)
20+
To get a temperature pass a wavelength
21+
>>> l = 500*10**(-9) # m
22+
>>> wiens_law(l)
23+
'''
24+
return const.b_wien/a
25+
26+
def plancks_law(T, l):
27+
''' Computes the blackbody curve over wavelength range l for temp T
28+
using Planck's law
29+
30+
Args:
31+
T -- float, temperature of blackbody
32+
l -- float or 1D np.ndarray, wavelengths
33+
34+
Returns:
35+
float or 1D np.ndarray, intensity in units: 10^4 W sr^-1 m^-2 nm^-1
36+
37+
Example:
38+
>>> plancks_law(300, 560*10**-9)
39+
'''
40+
return 2*const.h*const.c**2/l**5 * 1/(np.exp(const.h*const.c/(l*const.k_B*T))-1)

build/lib/astroedu/interactives/blackbody_curves.ipynb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5-
"id": "athletic-formula",
5+
"id": "sunset-james",
66
"metadata": {},
77
"source": [
88
"<center>\n",
@@ -19,7 +19,7 @@
1919
{
2020
"cell_type": "code",
2121
"execution_count": 1,
22-
"id": "located-produce",
22+
"id": "moral-increase",
2323
"metadata": {
2424
"jupyter": {
2525
"source_hidden": true
@@ -78,14 +78,14 @@
7878
{
7979
"cell_type": "code",
8080
"execution_count": 2,
81-
"id": "clean-terminal",
81+
"id": "swiss-blocking",
8282
"metadata": {},
8383
"outputs": [],
8484
"source": [
8585
"kb = 1.380649*10**(-23) # J/K\n",
8686
"c = 299792458 # m/s\n",
8787
"h = 6.62607015*10**(-34) # Js\n",
88-
"l = np.arange(0.01,4,0.01)*10**(-6) # micro m\n",
88+
"l = np.arange(0.01,4,0.01)*10**(-6) # m\n",
8989
"\n",
9090
"def blackBody(T, T2):\n",
9191
" B = 2*h*c**2/l**5 * 1/(np.exp(h*c/(l*kb*T))-1)\n",
@@ -96,13 +96,13 @@
9696
{
9797
"cell_type": "code",
9898
"execution_count": 3,
99-
"id": "overhead-flower",
99+
"id": "indian-victoria",
100100
"metadata": {},
101101
"outputs": [
102102
{
103103
"data": {
104104
"application/vnd.jupyter.widget-view+json": {
105-
"model_id": "57e45ca9d8494876becf55dddc31b8fd",
105+
"model_id": "1a02a112ce084847b3846c46a9c14de8",
106106
"version_major": 2,
107107
"version_minor": 0
108108
},
@@ -120,7 +120,7 @@
120120
},
121121
{
122122
"cell_type": "markdown",
123-
"id": "similar-covering",
123+
"id": "limiting-webster",
124124
"metadata": {},
125125
"source": [
126126
"## Info\n",
160 KB
Binary file not shown.

dist/astroedu-0.2.0a1.tar.gz

129 KB
Binary file not shown.

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[metadata]
22
# replace with your username:
33
name = astroedu
4-
version = 0.1.1a1
4+
version = 0.2.0a1
55
author = Dimitrios Theodorakis
66
author_email = [email protected]
77
description = A python package for astronomy educators

src/astroedu.egg-info/PKG-INFO

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Metadata-Version: 2.1
22
Name: astroedu
3-
Version: 0.1.1a1
3+
Version: 0.2.0a1
44
Summary: A python package for astronomy educators
55
Home-page: https://github.com/astroDimitrios/astroedu
66
Author: Dimitrios Theodorakis
@@ -56,6 +56,43 @@ Description: <center>
5656
planets = load_data('planets')
5757
```
5858
The function returns a Pandas dataframe.
59+
60+
### Constants
61+
62+
Astropy like constants for ease of access.
63+
For full functionality use the astropy [constants](https://docs.astropy.org/en/stable/constants/) submodule.
64+
65+
```
66+
>>> from astroedu.constants import c
67+
>>> c
68+
Constant(c, 299792458, m/s, Speed of light)
69+
```
70+
or
71+
```
72+
>>> import astroedu.constants as const
73+
>>> print(const.c)
74+
Name = Speed of light
75+
Value = 299792458
76+
Unit = m/s
77+
```
78+
79+
Constants can perform simple maths with other constants or int/float/np.array.
80+
The returned value is an int/float/np.array not a Constant class instance:
81+
```
82+
>>> from astroedu.constants import c, m_e
83+
>>> c*m_e
84+
2.7309245302881346e-22
85+
```
86+
Caveat: Constant must be in front of the array.
87+
```
88+
>>> import numpy as np
89+
>>> from astroedu.constants import c
90+
>>> a = np.arange(4)
91+
>>> c*a
92+
array([ 0, 299792458, 599584916, 899377374])
93+
>>> a*c
94+
TypeError: unsupported operand type(s) for *: 'int' and 'Constant'
95+
```
5996
Keywords: astronomy education astrophysics
6097
Platform: UNKNOWN
6198
Classifier: Development Status :: 3 - Alpha

src/astroedu.egg-info/SOURCES.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ setup.cfg
66
src/astroedu/__build__.py
77
src/astroedu/__init__.py
88
src/astroedu/__main__.py
9+
src/astroedu/classes.py
10+
src/astroedu/functions.py
911
src/astroedu.egg-info/PKG-INFO
1012
src/astroedu.egg-info/SOURCES.txt
1113
src/astroedu.egg-info/dependency_links.txt
1214
src/astroedu.egg-info/entry_points.txt
1315
src/astroedu.egg-info/requires.txt
1416
src/astroedu.egg-info/top_level.txt
17+
src/astroedu/constants/__init__.py
18+
src/astroedu/constants/constants.py
1519
src/astroedu/datasets/README.md
1620
src/astroedu/datasets/__init__.py
1721
src/astroedu/datasets/atmospheres.csv

src/astroedu/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .functions import *
2+
from .classes import *

src/astroedu/classes.py

Whitespace-only changes.

src/astroedu/constants/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .constants import *

0 commit comments

Comments
 (0)