/
create_plot_as_html.py
147 lines (90 loc) · 3.39 KB
/
create_plot_as_html.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
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
# coding: utf-8
# <table style="float:left; border:none">
# <tr style="border:none; background-color: #ffffff">
# <td style="border:none">
# <a href="http://www.nilu.no/">
# <img
# src="http://www.nilu.no/Portals/0/Logo_transparent.png"
# style="width:100%"
# >
# </a>
# </td>
#
# </tr>
# </table>
# <div style="float:right;"><h2>Retriving data from EBAS:</h2><h3>02. Basic data retrieval and plotting</h3></div>
# In[1]:
from bokeh.io import output_notebook, show
from bokeh.plotting import figure
from netCDF4 import num2date
from pydap.client import open_dods
# In[2]:
#The bokeh plotting library useually outputs the content to html, therefore we must explicitly call the output to the notebook
#output_notebook()
# **Syntax of the OPeNDAP request**
#
# FI_REF = Instrument reference
#
# ME_REF = Method reference
#
# Syntax:
#
# *ST_STATION_CODE.FT_TYPE.RE_REGIME_CODE.MA_MATRIX_NAME.CO_COMP_NAME.DS_RESCODE.FI_REF.ME_REF.DL_DATA_LEVEL*
#
# *Both could be found using the EBAS web, selecting the metadata tab for a selected dataset
#
# Example (no data level):
#
# *NO0042G.Hg_mon.IMG.air.mercury.1h.NO01L_tekran_42_dup.NO01L_afs..*
#
# Ends up in the following request:
# http://dev-ebas-pydap.nilu.no/NO0042G.Hg_mon.IMG.air.mercury.1h.NO01L_tekran_42_dup.NO01L_afs..dods
# **We will do a quick example with mercury data from the Zeppelin station**
#
# We access the data using the OPeNDAP protocol, we send a dods request and recieve binary data.
#
# This is now parsed by the PyDAP library.
# In[4]:
mercury_ds = open_dods('http://dev-ebas-pydap.nilu.no/NO0042G.Hg_mon.IMG.air.mercury.1h.NO01L_tekran_42_dup.NO01L_afs..dods')
# We get the keys
keys = mercury_ds.keys()
print(keys)
# **Now we can append the data to some variables**
# In[5]:
mercury = mercury_ds['mercury']
# Let's have a quicklook at the data
print(mercury.keys())
print(mercury.mercury.data)
y = mercury.mercury.data
# **In EBAS we specify dates as days since a specified date**
#
# This is not very user friendly, so we must convert the dates
# In order to find the unit, we must actually check the metadata calling the Dataset Attribute Structure (DAS) which provides information about the variables themselves
#
# http://dev-ebas-pydap.nilu.no/NO0042G.Hg_mon.IMG.air.mercury.1h.NO01L_tekran_42_dup.NO01L_afs..das
#
# This is the same for all datasets using the OPeNDAP protocol
# In[6]:
x = num2date(mercury.time.data,units='days since 1900-01-01 00:00:00',calendar='gregorian')
# **Now that we have a x and y variable, we could plot the data**
# In[7]:
plot = figure(plot_width=800, plot_height=400, title="Mercury Zeppelin",x_axis_type="datetime")
plot.line(x, y, line_width=2)
plot.xaxis.axis_label = 'Date'
plot.yaxis.axis_label = ''
show(plot) # show the results
# **So how do we now figure out the unit of the mercury plotted above?**
# In[8]:
mercury_ds.mercury_ebasmetadata
# In[9]:
print len(mercury_ds.mercury_ebasmetadata.mercury_ebasmetadata.data)
# In[10]:
mercury_metadata = mercury_ds.mercury_ebasmetadata.mercury_ebasmetadata.data[0]
print(mercury_metadata)
# In[10]:
plot = figure(plot_width=800, plot_height=400, title="Mercury Zeppelin",x_axis_type="datetime")
plot.line(x, y, line_width=2)
plot.xaxis.axis_label = 'Date'
plot.yaxis.axis_label = 'ng/m3'
show(plot) # show the results
# In[ ]: