pythonradex 1.0.9__py3-none-any.whl → 2.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pythonradex/LAMDA_file.py CHANGED
@@ -8,16 +8,19 @@ from pythonradex import atomic_transition
8
8
  from scipy import constants
9
9
  import numpy as np
10
10
 
11
+
11
12
  def is_comment(line):
12
- if line.replace(' ','')[0] == '!':
13
+ if line.replace(" ", "")[0] == "!":
13
14
  return True
14
15
  else:
15
16
  return False
16
17
 
17
- def read(datafilepath,read_frequencies,read_quantum_numbers=False):
18
- '''
19
- Read a LAMDA data file. LAMDA files can be downloaded from the LAMDA database
20
- at http://home.strw.leidenuniv.nl/~moldata
18
+
19
+ def read(datafilepath, read_frequencies, read_quantum_numbers=False):
20
+ """
21
+ Read a data file that follows the LAMDA format (https://home.strw.leidenuniv.nl/~moldata/molformat.html).
22
+ Such files can for example be downloaded from the EMAA (https://emaa.osug.fr/)
23
+ or the LAMDA (http://home.strw.leidenuniv.nl/~moldata) databases.
21
24
 
22
25
  Args:
23
26
  datafilepath (:obj:`str`): The filepath to the LAMDA file.
@@ -30,100 +33,132 @@ def read(datafilepath,read_frequencies,read_quantum_numbers=False):
30
33
  read_quantum_numbers (:obj:`bool`): Whether to read the quantum numbers
31
34
  from the file. Defaults to False. The LAMDA format does not force a
32
35
  file to list quantum numbers, so they might not be present.
33
-
36
+
34
37
  Returns:
35
38
  dict: A dictionary containing the data read from the file.
36
39
  The following keys give acces to lists containing the data.
37
40
  The elements of these lists are in the order they appear in the file.
38
41
  'levels' (a list of pythonradex.atomic_transition.Level objects);
39
- 'radiative transitions' (a list of
42
+ 'radiative transitions' (a list of
40
43
  pythonradex.atomic_transitions.RadiativeTransition objects);
41
44
  'collisional transitions' (a list of
42
45
  pythonradex.atomic_transition.CollisionalTransition objects);
43
46
  'quantum numbers' (list of str, empty if read_quantum_numbers
44
47
  was set to False)
45
- '''
46
- #identifiers used in the LAMDA database files:
47
- LAMDA_coll_ID = {'1':'H2','2':'para-H2','3':'ortho-H2','4':'e',
48
- '5':'H','6':'He','7':'H+'}
49
- datafile = open(datafilepath,'r')
48
+ """
49
+ # identifiers used in the LAMDA database files:
50
+ LAMDA_coll_ID = {
51
+ "1": "H2",
52
+ "2": "para-H2",
53
+ "3": "ortho-H2",
54
+ "4": "e",
55
+ "5": "H",
56
+ "6": "He",
57
+ "7": "H+",
58
+ }
59
+ datafile = open(datafilepath, "r")
50
60
  levels = []
51
61
  rad_transitions = []
52
62
  coll_transitions = {}
53
63
  quantum_numbers = []
54
64
  comment_offset = 0
55
- for i,line in enumerate(datafile):
56
- if is_comment(line) or line=='' or line=='\n':
65
+ for i, line in enumerate(datafile):
66
+ if is_comment(line) or line == "" or line == "\n":
57
67
  comment_offset += 1
58
68
  continue
59
- if i == 0+comment_offset:
69
+ if i == 0 + comment_offset:
60
70
  continue
61
- if i == 1+comment_offset:
71
+ if i == 1 + comment_offset:
62
72
  continue
63
- if i == 2+comment_offset:
73
+ if i == 2 + comment_offset:
64
74
  n_levels = int(line)
65
75
  continue
66
- if 3+comment_offset <= i < 3+comment_offset+n_levels:
76
+ if 3 + comment_offset <= i < 3 + comment_offset + n_levels:
67
77
  line_entries = line.split()
68
78
  leveldata = [float(string) for string in line_entries[:3]]
69
- assert int(leveldata[0]) == i-2-comment_offset,\
70
- 'level numeration not consistent'
71
- #transforming energy from cm-1 to J; level numbers starting from 0:
79
+ assert (
80
+ int(leveldata[0]) == i - 2 - comment_offset
81
+ ), "level numeration not consistent"
82
+ # transforming energy from cm-1 to J; level indices starting from 0:
72
83
  lev = atomic_transition.Level(
73
- g=leveldata[2],
74
- E=constants.c*constants.h*leveldata[1]/constants.centi,
75
- number=int(leveldata[0])-1)
84
+ g=leveldata[2],
85
+ E=constants.c * constants.h * leveldata[1] / constants.centi,
86
+ index=int(leveldata[0]) - 1,
87
+ )
76
88
  levels.append(lev)
77
89
  if read_quantum_numbers:
78
90
  quantum_numbers.append(line_entries[3])
79
91
  continue
80
- if i == 3+comment_offset+n_levels:
92
+ if i == 3 + comment_offset + n_levels:
81
93
  n_rad_transitions = int(line)
82
94
  continue
83
- if 4+comment_offset+n_levels <= i < 4+comment_offset+n_levels+n_rad_transitions:
95
+ if (
96
+ 4 + comment_offset + n_levels
97
+ <= i
98
+ < 4 + comment_offset + n_levels + n_rad_transitions
99
+ ):
84
100
  radtransdata = [float(string) for string in line.split()]
85
- up = next(level for level in levels if level.number==radtransdata[1]-1)
86
- low = next(level for level in levels if level.number==radtransdata[2]-1)
87
- rad_trans_kwargs = {'up':up,'low':low,'A21':radtransdata[3]}
101
+ up = next(level for level in levels if level.index == radtransdata[1] - 1)
102
+ low = next(level for level in levels if level.index == radtransdata[2] - 1)
103
+ rad_trans_kwargs = {"up": up, "low": low, "A21": radtransdata[3]}
88
104
  if read_frequencies:
89
- nu0 = radtransdata[4]*constants.giga
90
- Delta_E = up.E-low.E
91
- assert np.isclose(nu0,Delta_E/constants.h,atol=0,rtol=1e-3),\
92
- 'read frequency is inconsistent with level energies'
93
- rad_trans_kwargs['nu0'] = nu0
94
- rad_trans = atomic_transition.RadiativeTransition(**rad_trans_kwargs)
105
+ nu0 = radtransdata[4] * constants.giga
106
+ Delta_E = up.E - low.E
107
+ assert np.isclose(
108
+ nu0, Delta_E / constants.h, atol=0, rtol=1e-3
109
+ ), "read frequency is inconsistent with level energies"
110
+ rad_trans_kwargs["nu0"] = nu0
111
+ rad_trans = atomic_transition.RadiativeTransition(**rad_trans_kwargs)
95
112
  rad_transitions.append(rad_trans)
96
113
  continue
97
- if i == 4+comment_offset+n_levels+n_rad_transitions:
114
+ if i == 4 + comment_offset + n_levels + n_rad_transitions:
98
115
  coll_partner_offset = 0
99
116
  continue
100
- if i == 5+comment_offset+n_levels+n_rad_transitions + coll_partner_offset:
117
+ if i == 5 + comment_offset + n_levels + n_rad_transitions + coll_partner_offset:
101
118
  coll_ID = LAMDA_coll_ID[line[0]]
102
119
  coll_transitions[coll_ID] = []
103
120
  continue
104
- if i == 6+comment_offset+n_levels+n_rad_transitions + coll_partner_offset:
121
+ if i == 6 + comment_offset + n_levels + n_rad_transitions + coll_partner_offset:
105
122
  n_coll_transitions = int(line)
106
123
  continue
107
- if i == 7+comment_offset+n_levels+n_rad_transitions + coll_partner_offset:
108
- continue #this lines contains the number of temperature elements
109
- if i == 8+comment_offset+n_levels+n_rad_transitions + coll_partner_offset:
124
+ if i == 7 + comment_offset + n_levels + n_rad_transitions + coll_partner_offset:
125
+ continue # this lines contains the number of temperature elements
126
+ if i == 8 + comment_offset + n_levels + n_rad_transitions + coll_partner_offset:
110
127
  coll_temperatures = np.array([float(string) for string in line.split()])
111
128
  continue
112
- last_coll_trans_i = 9+comment_offset+n_levels+n_rad_transitions\
113
- +coll_partner_offset+n_coll_transitions-1
114
- if 9+comment_offset+n_levels+n_rad_transitions+coll_partner_offset <= i <=\
115
- last_coll_trans_i:
129
+ last_coll_trans_i = (
130
+ 9
131
+ + comment_offset
132
+ + n_levels
133
+ + n_rad_transitions
134
+ + coll_partner_offset
135
+ + n_coll_transitions
136
+ - 1
137
+ )
138
+ if (
139
+ 9 + comment_offset + n_levels + n_rad_transitions + coll_partner_offset
140
+ <= i
141
+ <= last_coll_trans_i
142
+ ):
116
143
  coll_trans_data = [float(string) for string in line.split()]
117
- up = next(level for level in levels if level.number==coll_trans_data[1]-1)
118
- low = next(level for level in levels if level.number==coll_trans_data[2]-1)
119
- K21_data = np.array(coll_trans_data[3:])*constants.centi**3
144
+ up = next(
145
+ level for level in levels if level.index == coll_trans_data[1] - 1
146
+ )
147
+ low = next(
148
+ level for level in levels if level.index == coll_trans_data[2] - 1
149
+ )
150
+ K21_data = np.array(coll_trans_data[3:]) * constants.centi**3
120
151
  coll_trans = atomic_transition.CollisionalTransition(
121
- up=up,low=low,K21_data=K21_data,
122
- Tkin_data=coll_temperatures)
152
+ up=up, low=low, K21_data=K21_data, Tkin_data=coll_temperatures
153
+ )
123
154
  coll_transitions[coll_ID].append(coll_trans)
124
155
  if i == last_coll_trans_i:
125
- coll_partner_offset += 4+n_coll_transitions
156
+ coll_partner_offset += 4 + n_coll_transitions
126
157
  continue
127
158
  datafile.close()
128
- return {'levels':levels,'radiative transitions':rad_transitions,
129
- 'collisional transitions':coll_transitions,'quantum numbers':quantum_numbers}
159
+ return {
160
+ "levels": levels,
161
+ "radiative transitions": rad_transitions,
162
+ "collisional transitions": coll_transitions,
163
+ "quantum numbers": quantum_numbers,
164
+ }
pythonradex/__init__.py CHANGED
@@ -1,4 +1,9 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  """
3
3
  Python implementation of the RADEX radiative transfer code
4
- """
4
+ """
5
+
6
+ from . import helpers, LAMDA_file, radiative_transfer
7
+ from importlib.metadata import version
8
+
9
+ __version__ = version("pythonradex")