1
+ """
2
+ Class and code to easily switch between astropy constants and values taken directly from SSW for
3
+ comparision.
4
+ """
5
+
6
+ from astropy import constants as const
7
+
8
+ CONSTANTS = {
9
+ 'idl' : {
10
+ 'mc2' : 510.98 , # electron rest mass keV
11
+ 'clight' : 2.9979e10 , # cm s^-1
12
+ 'au' : 1.496e13 , # cm
13
+ 'r0' : 2.8179e-13 , # classical electron radius cm
14
+ 'alpha' : 7.29735308e-3 , # alpha
15
+ 'twoar02' : 1.15893512e-27 # 2 * alpha * r0^2
16
+ },
17
+
18
+ 'astropy' : {
19
+ 'mc2' : (const .m_e * const .c ** 2 ).to ('keV' ).value ,
20
+ 'clight' : const .c .cgs .value ,
21
+ 'au' : const .au .cgs .value ,
22
+ 'r0' : (const .a0 * const .alpha ** 2 ).cgs .value ,
23
+ 'alpha' : const .alpha .cgs .value ,
24
+ 'twoar02' : 2.0 * const .alpha .cgs .value * (const .a0 * const .alpha ** 2 ).cgs .value ** 2
25
+ }
26
+ }
27
+
28
+
29
+ class Singleton (type ):
30
+ """
31
+ A singleton metaclass
32
+ """
33
+ _instances = {}
34
+
35
+ def __call__ (cls , * args , ** kwargs ):
36
+ if cls not in cls ._instances :
37
+ cls ._instances [cls ] = super (Singleton , cls ).__call__ (* args , ** kwargs )
38
+ return cls ._instances [cls ]
39
+
40
+
41
+ class Constants (metaclass = Singleton ):
42
+ """
43
+ Centralised constant representation
44
+ """
45
+ def __init__ (self , ref = 'idl' ):
46
+ """
47
+
48
+ Parameters
49
+ ----------
50
+ ref
51
+ """
52
+ self .ref = ref
53
+
54
+ def get_constant (self , name ):
55
+ """
56
+ Return the value of the constant.
57
+ Parameters
58
+ ----------
59
+ name : str
60
+ Name of constant
61
+
62
+ Returns
63
+ -------
64
+ float
65
+ Value of constant
66
+ """
67
+ if self .ref not in ['astropy' , 'idl' ]:
68
+ raise ValueError (f'Valid values for ref are astropy or idl not { self .ref } .' )
69
+
70
+ if name not in CONSTANTS [self .ref ].keys ():
71
+ raise ValueError (f'Valid names are { CONSTANTS [self .ref ].keys ()} or idl not { name } .' )
72
+
73
+ return CONSTANTS [self .ref ][name ]
0 commit comments