Newer
Older
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env python
import sys
sys.path.append('../../')
import os
import numpy as np
import string
import datetime as dt
import logging
import da.tools.io4 as io
from da.tools.general import CreateDirs
fontsize=10
def nice_lat(cls):
#
# Convert latitude from decimal to cardinal
#
if cls > 0:
h = 'N'
else:
h = 'S'
dec, deg = np.math.modf(cls)
return string.strip('%2d %2d\'%s' % (abs(deg), round(abs(60 * dec), 0), h))
def nice_lon(cls):
#
# Convert longitude from decimal to cardinal
#
if cls > 0:
h = 'E'
else:
h = 'W'
dec, deg = np.math.modf(cls)
return string.strip('%3d %2d\'%s' % (abs(deg), round(abs(60 * dec), 0), h))
def nice_alt(cls):
#
# Reformat elevation or altitude
#
return string.strip('%10.1f masl' % round(cls, -1))
def SummarizeObs(DaCycle,printfmt='html'):
"""***************************************************************************************
Call example:
python summarize_obs.py
Option printfmt : [tex,scr,html] print summary table in latex, terminal, or html format
Other options are all those needed to create a DaCycle object
OR:
call directly from a python script as:
q=SummarizeObs(DaCycle,printfmt='html')
***************************************************************************************"""
sumdir=os.path.join(DaCycle['dir.analysis'],'summary')
if not os.path.exists(sumdir):
logging.info( "Creating new directory "+sumdir )
os.makedirs(sumdir)
mrdir=os.path.join(DaCycle['dir.analysis'],'data_molefractions')
if not os.path.exists(mrdir):
logging.error( "Input directory does not exist (%s), exiting... "%mrdir )
return None
mrfiles = os.listdir(mrdir)
infiles = [os.path.join(mrdir,f) for f in mrfiles if f.endswith('.nc')]
if printfmt == 'tex':
print '\\begin{tabular*}{\\textheight}{l l l l r r r r}'
print 'Code & Name & Lat, Lon, Elev & Lab & N (flagged) & $\\sqrt{R}$ &Inn \\XS &Bias\\\\'
print '\hline\\\\ \n\multicolumn{8}{ c }{Semi-Continuous Surface Samples}\\\\[3pt] '
fmt= '%8s & '+' %55s & '+'%20s &'+'%6s &'+' %4d (%d) & '+' %5.2f & '+' %5.2f & '+'%+5.2f \\\\'
elif printfmt == 'html':
tablehead = \
"<TR>\n <TH> Site code </TH> \
<TH> Sampling Type </TH> \
<TH> Lab. </TH> \
<TH> Country </TH> \
<TH> Lat, Lon, Elev. (m ASL) </TH> \
<TH> No. Obs. Avail. </TH> \
<TH> √R (μmol mol<sup>-1</sup>) </TH> \
<TH> √HPH (μmol mol<sup>-1</sup>) </TH> \
<TH> H(x)-y (μmol mol<sup>-1</sup>) </TH> \
<TH> H(x)-y (JJAS) (μmol mol<sup>-1</sup>) </TH> \
<TH> H(x)-y (NDJFMA) (μmol mol<sup>-1</sup>) </TH> \n \
</TR>\n"
fmt= """<TR> \n \
<TD><a href='javascript:LoadCO2Tseries("%s")'>%s </a></TD>\
<TD>%s</TD>\
<TD>%s</TD>\
<TD>%40s</TD>\
<TD>%s</TD>\
<TD>%d</TD>\
<TD>%+5.2f</TD>\
<TD>%+5.2f</TD>\
<TD>%+5.2f±%5.2f</TD>\
<TD>%+5.2f±%5.2f</TD>\
<TD>%+5.2f±%5.2f</TD>\n \
</TR>\n"""
elif printfmt == 'scr':
print 'Code Site NObs flagged R Inn X2'
fmt= '%8s '+' %55s %s %s'+' %4d '+' %4d '+' %5.2f '+' %5.2f'
table=[]
lons=[]
lats=[]
names=[]
nobs=[]
for infile in infiles:
#logging.debug( infile )
f = io.CT_CDF(infile,'read')
date = f.GetVariable('time')
obs = f.GetVariable('value')*1e6
mdm = f.GetVariable('modeldatamismatch')*1e6
simulated = f.GetVariable('modelsamplesmean')*1e6
simulated_std = f.GetVariable('modelsamplesstandarddeviation')*1e6
pydates = [dt.datetime(1970,1,1)+dt.timedelta(seconds=int(d)) for d in date]
summer = [i for i,d in enumerate(pydates) if d.month in [6,7,8,9] ]
winter = [i for i,d in enumerate(pydates) if d.month in [11,12,1,2,3,4] ]
diff=((simulated-obs).mean())
diffsummer=((simulated-obs).take(summer).mean())
diffwinter=((simulated-obs).take(winter).mean())
diffstd=((simulated-obs).std())
diffsummerstd=((simulated-obs).take(summer).std())
diffwinterstd=((simulated-obs).take(winter).std())
longsitestring=f.site_name+', '+f.site_country
location=nice_lat(f.site_latitude)+', '+ nice_lon(f.site_longitude)+', '+nice_alt(f.site_elevation)
if printfmt == 'html':
ss=(f.site_code.upper(),
f.site_code.upper(),
f.dataset_project,
f.lab_abbr,
f.site_country,
location,
len(mdm.compressed()),
mdm.mean(),
np.sqrt((simulated_std**2).mean()),
diff,diffstd,
diffsummer,diffsummerstd,
diffwinter,diffwinterstd)
table.append(ss)
lons.append(f.site_longitude)
lats.append(f.site_latitude)
names.append(f.site_code)
nobs.append(len(mdm.compressed()))
f.close()
if printfmt == 'tex':
saveas=os.path.join(sumdir,'site_table.tex')
f=open(saveas,'w')
elif printfmt == 'html':
saveas=os.path.join(sumdir,'site_table.html')
f=open(saveas,'w')
txt = "<meta http-equiv='content-type' content='text/html;charset=utf-8' />\n"
f.write(txt)
txt="<table border=1 cellpadding=2 cellspacing=2 width='100%' bgcolor='#EEEEEE'>\n"
f.write(txt)
f.write(tablehead)
for i,ss in enumerate(table):
f.write(fmt%ss)
if (i+1)%15 == 0:
f.write(tablehead)
if printfmt == 'tex':
f.write( '\cline{2-8}\\\\' )
f.write( '\hline \\\\')
f.write( '\end{tabular*}')
else:
txt="\n</table>"
f.write(txt)
f.close()
logging.info("File written with summary: %s" % saveas)
# main body if called as script
if __name__ == '__main__': # started as script
from da.tools.initexit import CycleControl
from da.ct.dasystem import CtDaSystem
from da.ct.statevector import CtStateVector
sys.path.append('../../')
logging.root.setLevel(logging.DEBUG)
DaCycle = CycleControl(args={'rc':'../../ctdas-od-gfed2-glb6x4-obspack.rc'})
DaCycle.Initialize()
DaCycle.ParseTimes()
DaSystem = CtDaSystem('../rc/carbontracker_ct09_op.rc')
DaSystem.Initialize()
DaCycle.DaSystem = DaSystem
q=SummarizeObs(DaCycle)
sys.exit(0)