-
Notifications
You must be signed in to change notification settings - Fork 2
/
zruntoics.py
112 lines (82 loc) · 2.76 KB
/
zruntoics.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'''
file created args : contiune_h5name_groupname_timestep_datenow
If using the lumped function it reads groups from a mechanism file.
Must use format from lumper.jl for this to work.
'''
import pandas as pd
import numpy as np
import datetime
import zhdf
import re
def lumpfn(df,mech):
kpp = ' '.join(open(mech).readlines())
gs = re.findall(r'(LMP\d),\s*String(["\'\w\d/ ,]+)\n+?',re.sub(r'\n */+','',kpp))
print df
for g in gs:
cols = list(eval(g[1].replace(' ','')))
new = df[cols].sum(axis=1)
new.Species = g[0]
new.Index = '_'.join(cols)
df = df.drop(cols,axis=1)
df[g[0]] = new
return df
def newics(
lump = False,
filename = False,
write = False,
h5file = 'ensemble.h5',
group = 0,
timestep=144,
constrain = ['NOX'],
df = pd.DataFrame(
[
['ii', 'TIME', '0',str(24*60*60*5)],
['ii', 'TEMP', '0', '298'],
['ii', 'LAT', '0', '36.9'],
['ii', 'LON', '0', '116.31'],
['ii', 'JDAY', '0', '173.5'],
['ii', 'H2O', '0', '0.02'],
['ii', 'ALBEDO', '0', '0'],
['ii', 'PRESS', '0', '1013'],
['ii', 'NOx', '1', '0'],
['ii', 'DEPOS', '1', '0'],
['ii', 'FEMISS', '1', '0'],
['s', 'SPINUP', '0', '1e99']
]
),
):
ignore = ['R','RO2','M','NO','NO2','HO2','CH3O2','OH','TRICLETH','HONO','BUT2CHO', 'C3ME3CHO', 'C5H11CHO', 'CH2CL2', 'LIMONENE', 'MACR', 'MVK']
df.columns = ['Index', 'Species', 'Constrain', 'base']
a = zhdf.new(h5file,group)
cols = filter(lambda x: x not in ignore,a.spec.columns)
try:
specs = a.spec.loc[a.timesteps[timestep],cols].compute()
except:
specs = a.spec.loc[timestep,cols].compute()
df.index = df.Species
df = df.T
for s in specs.columns:
M = 1
if s in ['LAT','LON','TEMP']: M = a.M.compute()
df[s] = ['continue',s,int(s in constrain),specs[s][0]*M]
if lump:
df = lumpfn(df,lump)
df = df.astype(str)
if write:
if not filename:
filename = 'contiunue_%s_%s_%s_%s'%(h5file.split('.h5')[0],group,timestep,datetime.date.today().strftime("%d%m%y"))
df = df.astype(str).T
with open('InitCons/'+filename+'.csv','w') as f:
f.write('A continuation ics file'+','*(df.shape[1]-1))
f.write('\n')
f.write(','*(df.shape[1]-1))
f.write('\n')
f.write(','.join(df.columns))
f.write('\n')
f.write('\n'.join([','.join(i) for i in df.values]))
print filename, 'written'
return df
'''
df = newics(filename = 'normalcri', write =True,lump = False)
df = newics(filename = 'lumpedcri', write =True,lump = './mechanisms/lumped_formatted_CRI_FULL_2.2.kpp')
'''