forked from sunpy/sunpy
/
unit_conversion.py
53 lines (45 loc) · 2.21 KB
/
unit_conversion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from __future__ import absolute_import
import numpy as np
from scipy.constants import constants
from astropy import units
__all__ = ['degrees_to_hours', 'degrees_to_arc', 'kelvin_to_keV',
'keV_to_kelvin', 'to_angstrom']
boltz_unit = units.J / units.K
def degrees_to_hours(angle):
"""Converts an angle from the degree notation to the hour, arcmin, arcsec
notation (returned as a tuple)."""
if not isinstance(angle, units.Quantity):
raise ValueError("angle should be a Quantity")
hour = (np.floor(angle / 15)) / units.deg
remainder = ((angle / 15.0) / units.deg) - hour
arcminute = (np.floor(remainder * 60))
remainder = remainder * 60 - arcminute
arcsecond = remainder * 60.0
return [hour * units.hourangle, arcminute * units.arcmin, arcsecond * units.arcsec]
def degrees_to_arc(angle):
"""Converts decimal degrees to degree, arcminute,
arcsecond (returned as a tuple)."""
if not isinstance(angle, units.Quantity):
raise ValueError("angle should be a Quantity")
degree = (np.floor(angle)) / units.deg
remainder = (angle / units.deg) - degree
arcminute = (np.floor(remainder * 60))
remainder = remainder * 60 - arcminute
arcsecond = remainder * 60.0
return [degree * units.degree, arcminute * units.arcmin, arcsecond * units.arcsec]
def to_angstrom(value, unit):
"""Given a value with a unit (given in a string), convert to angstroms"""
value_quantity = value * units.Unit(unit)
return value_quantity.to(units.angstrom, equivalencies=units.spectral()).value
def kelvin_to_keV(temperature):
"""Convert from temperature expressed in Kelvin to a
temperature expressed in keV."""
if not isinstance(temperature, units.Quantity):
raise ValueError("temperature should be kelvin Quantity")
return temperature / ((constants.e * units.J / units.eV) / (constants.k * boltz_unit) * 1000.0)
def keV_to_kelvin(temperature):
"""Convert from temperature expressed in keV to a temperature
expressed in Kelvin."""
if not isinstance(temperature, units.Quantity):
raise ValueError("temperature should be keV Quantity")
return temperature * ((constants.e * units.J / units.eV) / (constants.k * boltz_unit) * 1000.0)