-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmulti.py
89 lines (70 loc) · 2.42 KB
/
multi.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import constants as C
from copy import copy, deepcopy
import numpy as np
import re
from typing import Tuple
from dataclasses import dataclass
from atmosphere import AtmosphereConstructor, ScaleType
import astropy.units as u
@dataclass
class MultiMetadata:
name: str
logG: float
def read_multi_atmos(filename: str) -> Tuple[MultiMetadata, AtmosphereConstructor]:
try:
with open(filename, 'r') as f:
lines = f.readlines()
except FileNotFoundError:
raise ValueError('Atmosphere file not found (%s)' % filename)
def get_line(commentPattern='^\s*\*'):
while len(lines) > 0:
line = lines.pop(0)
if not re.match(commentPattern, line):
return line.strip()
return None
atmosName = get_line()
scaleStr = get_line()
logG = float(get_line()) - 2 # For conversion to log[m.s^-2]
Nspace = int(get_line())
dscale = np.zeros(Nspace)
temp = np.zeros(Nspace)
ne = np.zeros(Nspace)
vlos = np.zeros(Nspace)
vturb = np.zeros(Nspace)
for k in range(Nspace):
vals = get_line().split()
vals = [float(v) for v in vals]
dscale[k] = vals[0]
temp[k] = vals[1]
ne[k] = vals[2]
vlos[k] = vals[3]
vturb[k] = vals[4]
scaleMode = scaleStr[0].upper()
if scaleMode == 'M':
scaleType = ScaleType.ColumnMass
dscale = 10**dscale
dscaleunits = u.g / u.cm**2
elif scaleMode == 'T':
scaleType = ScaleType.Tau500
dscale = 10**dscale
dscaleUnits = u.one
elif scaleMode == 'H':
scaleType = ScaleType.Geometric
dscaleUnits = u.km
else:
raise ValueError('Unknown scale type: %s (expected M, T, or H)' % scaleStr)
if len(lines) <= Nspace:
raise ValueError('Hydrogen populations not supplied!')
hPops = np.zeros((6, Nspace))
for k in range(Nspace):
vals = get_line().split()
vals = [float(v) for v in vals]
hPops[:, k] = vals
meta = MultiMetadata(atmosName, logG)
atmos = AtmosphereConstructor(depthScale=dscale << dscaleUnits,
temperature=temp << u.K,
vlos=vlos << u.cm / u.s,
vturb=vturb << u.cm / u.s,
ne=ne << u.cm**(-3),
hydrogenPops=hPops << u.cm**(-3))
return (meta, atmos)