-
Notifications
You must be signed in to change notification settings - Fork 0
/
reprobus.py
89 lines (79 loc) · 2.92 KB
/
reprobus.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
87
88
89
from init import *
from utility import *
from repro import readfile
class Reprobus:
def __init__(self, filename, nbcon=44, ncm=15, islev=np.arange(350, 801, 5)):
p0, rascp = 1000.0, 2.0 / 7.0
self.filename = filename
self.date = pd.to_datetime(filename.split("_")[-2], format="%Y%m%d%H")
if self.date > pd.to_datetime("2019-07-31"):
self.nlev = 137
else:
self.nlev = 60
nlev = self.nlev
pj1, uj1, vj1, alt, tj1, qj1, hc = readfile(filename, nlev, nbcon, ncm)
print(pj1)
aa, bb = self.get_coeffs()
pmb = np.zeros(tj1.shape)
for i in np.arange(nlev):
pmb[:, :, i] = aa[i] + bb[i] * pj1
theta = tj1 * ((p0 / pmb) ** rascp)
tr1 = {"Ozone": 4, "ClO": 10, "NO2": 6}
tr2 = {
"HNO3g": 42,
"HNO3": 3,
"N2O": 0,
"POx": 10,
"ClONO2": 13,
"NOx": 20,
"ClOx": 22,
"BrOx": 23,
"HCl": 12,
}
self.long_name = {
"Ozone": "O$_3$",
"ClO": "ClO",
"HNO3g": "HNO$_3$ Gas",
"HNO3": "HNO$_3$",
"N2O": "N$_2$O",
"NO2": "NO$_2$",
"POx": "Passive Ox",
"ClONO2": "ClONO$_2$",
"NOx": "NO$_x$",
"ClOx": "ClO$_x$",
"BrOx": "BrO$_x$",
"HCl": "HCl",
}
data = xr.Dataset()
for k, v in tr1.items():
data[k] = self.isentropic_tracers(hc[:, :, :, v], theta, islev, k)
for k, v in tr2.items():
data[k] = self.isentropic_tracers(qj1[:, :, :, v], theta, islev, k)
self.data = data
def get_coeffs(self, cdir="."):
cfile = "%s/ecmwf_%s_levels.txt" % (cdir, self.nlev)
# print(cfile)
df = pd.read_csv(cfile, sep="\s+", skiprows=[1])
df.columns = ["N", "a", "b", "c", "d"]
aa = 0.01 * df["a"].rolling(1).mean()[1:].values
bb = df["b"].rolling(1).mean()[1:].values
# print(df["a"])
# print(df["b"])
return aa, bb
def isentropic_tracers(self, tmp, theta, islev, tr):
lats = xr.DataArray(
np.arange(90, -91, -2), dims="Latitude", attrs={"long_name": "Latitude"}
)
lons = xr.DataArray(
np.arange(0, 360, 2), dims="Longitude", attrs={"long_name": "Longitude"}
)
levs = np.arange(self.nlev) # [::-1]
coords = [lons, lats, levs]
dims = ["Longitude", "Latitude", "Level"]
tattrs = {"long_name": "Temperature", "units": "K"}
theta = xr.DataArray(theta, coords=coords, dims=dims, attrs=tattrs)
islev = xr.DataArray(islev, dims="Theta", attrs=tattrs)
tmp = xr.DataArray(tmp / 1e-9, dims=dims, coords=coords)
tmp = xrvinterp(tmp, theta, islev, "Level", "Theta")
tmp.attrs = {"long_name": "%s [ppbv]" % self.long_name[tr]}
return tmp