Skip to content

Commit

Permalink
Constants v0.2.0a1
Browse files Browse the repository at this point in the history
Now includes the submodule constants for astropy like constants
  • Loading branch information
astroDimitrios committed May 2, 2021
1 parent 41da3b0 commit c1e06fe
Show file tree
Hide file tree
Showing 18 changed files with 387 additions and 10 deletions.
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<img width="100%" src="https://raw.githubusercontent.com/astroDimitrios/astroedu/e2d3007b867c3f5ac34d6bf4b2ca2f02aa38d824/assets/logo/astroeduLOGOtag.svg" alt='AP Logo'>
</center>

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

This package is in alpha.
Expand Down Expand Up @@ -46,4 +47,41 @@ Then you can load a data set by passing its name as a string to ```load_data```.
```
planets = load_data('planets')
```
The function returns a Pandas dataframe.
The function returns a Pandas dataframe.

### Constants

Astropy like constants for ease of access.
For full functionality use the astropy [constants](https://docs.astropy.org/en/stable/constants/) submodule.

```
>>> from astroedu.constants import c
>>> c
Constant(c, 299792458, m/s, Speed of light)
```
or
```
>>> import astroedu.constants as const
>>> print(const.c)
Name = Speed of light
Value = 299792458
Unit = m/s
```

Constants can perform simple maths with other constants or int/float/np.array.
The returned value is an int/float/np.array not a Constant class instance:
```
>>> from astroedu.constants import c, m_e
>>> c*m_e
2.7309245302881346e-22
```
Caveat: Constant must be in front of the array.
```
>>> import numpy as np
>>> from astroedu.constants import c
>>> a = np.arange(4)
>>> c*a
array([ 0, 299792458, 599584916, 899377374])
>>> a*c
TypeError: unsupported operand type(s) for *: 'int' and 'Constant'
```
2 changes: 2 additions & 0 deletions build/lib/astroedu/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .functions import *
from .classes import *
Empty file added build/lib/astroedu/classes.py
Empty file.
4 changes: 4 additions & 0 deletions build/lib/astroedu/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
wiens_displacement_const = 2.897771955*10**(-3) # m K
boltzmans_const = 1.380649*10**(-23) # J/K
speed_light = 299792458 # m/s
plancks_const = 6.62607015*10**(-34) # Js
1 change: 1 addition & 0 deletions build/lib/astroedu/constants/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .constants import *
104 changes: 104 additions & 0 deletions build/lib/astroedu/constants/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import numpy as np

class Constant:
''' Class for defining all constants
For more functionality use astropy's constant instead
https://docs.astropy.org/en/stable/constants/
Most of the constants were taken from the page above
Example:
>>> c = Constant('c', 299792458, 'm/s', 'Speed of light')
>>> c
299792458
'''

def __init__(self, symbol, value, unit, name):
self.symbol = symbol
self.value = value
self.unit = unit
self.name = name

def __repr__(self):
return f'Constant({self.symbol}, {self.value}, {self.unit}, {self.name})'

def __str__(self):
return f'Name = {self.name}\nValue = {self.value}\nUnit = {self.unit}'

def __add__(self, other):
if isinstance(other, Constant):
return self.value + other.value
else:
return self.value + other

def __sub__(self, other):
if isinstance(other, Constant):
return self.value - other.value
else:
return self.value - other

def __mul__(self, other):
if isinstance(other, Constant):
return self.value * other.value
else:
return self.value * other

def __truediv__(self, other):
if isinstance(other, Constant):
return self.value / other.value
else:
return self.value / other

def __pow__(self, other):
if isinstance(other, Constant):
return self.value ** other.value
else:
return self.value ** other

atm = Constant('atm', 101325, 'Pa', 'Standard Atmosphere')

au = Constant('AU', 1.49597871e+11, 'm', 'Astronomical Unit')

b_wien = Constant('b', 2.897771955e-3, 'm K', 'Wiens Displacement constant')

c = Constant('c', 299792458, 'm/s', 'Speed of light')

e = Constant('e', 1.60217663e-19, 'C', 'Electron Charge')

g0 = Constant('g', 9.80665, 'm/s2', 'Standard acceleration of gravity')

G = Constant('G', 6.6743e-11, 'm3 / (kg s2)', 'Gravitational constant')

h = Constant('h', 6.62607015e-34, 'Js', 'Planck constant')

hbar = Constant('hbar', 1.05457182e-34, 'Js', 'Reduced Planck constant')

k_B = Constant('kB', 1.380649e-23, 'J/K', 'Boltzman constant')

L_bol0 = Constant('L_bol0', 3.0128e+28, 'W', 'Luminosity for absolute bolometric magnitude 0')

L_sun = Constant('L_sun', 3.828e+26, 'W', 'Nominal Solar Luminosity')

m_e = Constant('m_e', 9.1093837e-31, 'kg', 'Electron mass')

m_n = Constant('m_n', 1.6749275e-27, 'kg', 'Neutron mass')

m_p = Constant('m_p', 1.67262192e-27, 'kg', 'Proton mass')

M_earth = Constant('M_earth', 5.97216787e+24, 'kg', 'Earth mass')

M_jup = Constant('M_jup', 1.8981246e+27, 'kg', 'Jupiter mass')

M_sun = Constant('M_sun', 1.98840987e+30, 'kg', 'Solar mass')

pc = Constant('pc', 3.08567758e+16, 'm', 'Parsec')

R_earth = Constant('R_earth', 5.97216787e+24, 'm', 'Nominal Earth equatorial radius')

R_jup = Constant('R_jup', 1.8981246e+27, 'm', 'Nominal Jupiter equatorial radius')

R_sun = Constant('R_sun', 1.98840987e+30, 'm', 'Nominal solar radius')

sigma_sb = Constant('sigma_sb', 5.67037442e-08, 'W / (K4 m2)', 'Stefan-Boltzmann constant')

u = Constant('u', 1.66053907e-27, 'kg', 'Atomic mass unit')
40 changes: 40 additions & 0 deletions build/lib/astroedu/functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import numpy as np
from astroedu import constants as const

def wiens_law(a):
''' Computes the peak wavelengths if temperatures are passed
Computes the temperatures if peak wavelengths are passed
0 must not be passed as an argument since 1/0 will fail
Args:
a -- float or 1D np.ndarray, peak wavelength or temperature
Returns:
float or 1D np.ndarray, peak wavelength or temperature
Example:
To get peak wavelengths pass the temperatures to arg a
>>> T = np.arange(0.1, 3000, 10) # K
>>> wiens_law(T)
To get a temperature pass a wavelength
>>> l = 500*10**(-9) # m
>>> wiens_law(l)
'''
return const.b_wien/a

def plancks_law(T, l):
''' Computes the blackbody curve over wavelength range l for temp T
using Planck's law
Args:
T -- float, temperature of blackbody
l -- float or 1D np.ndarray, wavelengths
Returns:
float or 1D np.ndarray, intensity in units: 10^4 W sr^-1 m^-2 nm^-1
Example:
>>> plancks_law(300, 560*10**-9)
'''
return 2*const.h*const.c**2/l**5 * 1/(np.exp(const.h*const.c/(l*const.k_B*T))-1)
14 changes: 7 additions & 7 deletions build/lib/astroedu/interactives/blackbody_curves.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "athletic-formula",
"id": "sunset-james",
"metadata": {},
"source": [
"<center>\n",
Expand All @@ -19,7 +19,7 @@
{
"cell_type": "code",
"execution_count": 1,
"id": "located-produce",
"id": "moral-increase",
"metadata": {
"jupyter": {
"source_hidden": true
Expand Down Expand Up @@ -78,14 +78,14 @@
{
"cell_type": "code",
"execution_count": 2,
"id": "clean-terminal",
"id": "swiss-blocking",
"metadata": {},
"outputs": [],
"source": [
"kb = 1.380649*10**(-23) # J/K\n",
"c = 299792458 # m/s\n",
"h = 6.62607015*10**(-34) # Js\n",
"l = np.arange(0.01,4,0.01)*10**(-6) # micro m\n",
"l = np.arange(0.01,4,0.01)*10**(-6) # m\n",
"\n",
"def blackBody(T, T2):\n",
" B = 2*h*c**2/l**5 * 1/(np.exp(h*c/(l*kb*T))-1)\n",
Expand All @@ -96,13 +96,13 @@
{
"cell_type": "code",
"execution_count": 3,
"id": "overhead-flower",
"id": "indian-victoria",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "57e45ca9d8494876becf55dddc31b8fd",
"model_id": "1a02a112ce084847b3846c46a9c14de8",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -120,7 +120,7 @@
},
{
"cell_type": "markdown",
"id": "similar-covering",
"id": "limiting-webster",
"metadata": {},
"source": [
"## Info\n",
Expand Down
Binary file added dist/astroedu-0.2.0a1-py3-none-any.whl
Binary file not shown.
Binary file added dist/astroedu-0.2.0a1.tar.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[metadata]
# replace with your username:
name = astroedu
version = 0.1.1a1
version = 0.2.0a1
author = Dimitrios Theodorakis
author_email = [email protected]
description = A python package for astronomy educators
Expand Down
39 changes: 38 additions & 1 deletion src/astroedu.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Metadata-Version: 2.1
Name: astroedu
Version: 0.1.1a1
Version: 0.2.0a1
Summary: A python package for astronomy educators
Home-page: https://github.com/astroDimitrios/astroedu
Author: Dimitrios Theodorakis
Expand Down Expand Up @@ -56,6 +56,43 @@ Description: <center>
planets = load_data('planets')
```
The function returns a Pandas dataframe.

### Constants

Astropy like constants for ease of access.
For full functionality use the astropy [constants](https://docs.astropy.org/en/stable/constants/) submodule.

```
>>> from astroedu.constants import c
>>> c
Constant(c, 299792458, m/s, Speed of light)
```
or
```
>>> import astroedu.constants as const
>>> print(const.c)
Name = Speed of light
Value = 299792458
Unit = m/s
```

Constants can perform simple maths with other constants or int/float/np.array.
The returned value is an int/float/np.array not a Constant class instance:
```
>>> from astroedu.constants import c, m_e
>>> c*m_e
2.7309245302881346e-22
```
Caveat: Constant must be in front of the array.
```
>>> import numpy as np
>>> from astroedu.constants import c
>>> a = np.arange(4)
>>> c*a
array([ 0, 299792458, 599584916, 899377374])
>>> a*c
TypeError: unsupported operand type(s) for *: 'int' and 'Constant'
```
Keywords: astronomy education astrophysics
Platform: UNKNOWN
Classifier: Development Status :: 3 - Alpha
Expand Down
4 changes: 4 additions & 0 deletions src/astroedu.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ setup.cfg
src/astroedu/__build__.py
src/astroedu/__init__.py
src/astroedu/__main__.py
src/astroedu/classes.py
src/astroedu/functions.py
src/astroedu.egg-info/PKG-INFO
src/astroedu.egg-info/SOURCES.txt
src/astroedu.egg-info/dependency_links.txt
src/astroedu.egg-info/entry_points.txt
src/astroedu.egg-info/requires.txt
src/astroedu.egg-info/top_level.txt
src/astroedu/constants/__init__.py
src/astroedu/constants/constants.py
src/astroedu/datasets/README.md
src/astroedu/datasets/__init__.py
src/astroedu/datasets/atmospheres.csv
Expand Down
2 changes: 2 additions & 0 deletions src/astroedu/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .functions import *
from .classes import *
Empty file added src/astroedu/classes.py
Empty file.
1 change: 1 addition & 0 deletions src/astroedu/constants/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .constants import *
Loading

0 comments on commit c1e06fe

Please sign in to comment.