-
Notifications
You must be signed in to change notification settings - Fork 2
/
plot_fluxes_sankey.py
executable file
·205 lines (156 loc) · 5.76 KB
/
plot_fluxes_sankey.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
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
#!/usr/bin/env python
# Tobey Carman
# Spatial Ecology Lab
# Aug 2013
try:
import os
import sys
import string
import math
import argparse
import netCDF4 as nc
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.sankey import Sankey
except ImportError as e:
print "%s" % e
sys.exit()
def main(args):
print "Loading dataset..."
dsA = nc.Dataset(args.inputfile)
print '(A): ', args.inputfile
year = args.year
month = args.month
cht = args.cohort
numpfts = len(dsA.dimensions['PFTS'])
# needs to have one value per pft
litterfall = dsA.variables['LTRFALC'][cht,year*month,:]
# needs to be a single value (for whole soil pool)
rh = dsA.variables['RH'][0,year*month]
# one value per pft
gpp = dsA.variables['GPP'][cht, year*month,:]
# one value per pft
npp = dsA.variables['NPP'][cht, year*month, :]
ra = (npp-gpp)
pfts = []
for i in range(numpfts):
pfts.append({'gpp': gpp[i],
'ltfl': litterfall[i],
'Ra': ra[i],
})
for pft in pfts:
print pft
fig = plt.figure(figsize=(18,6))
ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[], title="Veg and Soil Fluxes")
sankey = Sankey(ax=ax,
tolerance=1e-23,
#scale=1.00,
offset=-1.75,
head_angle=130,
gap=3.0,
radius=0.5,
unit='gC/m2',
format='%.0f',
)
soil_flows = np.append(litterfall, rh)
# [gpp1, gpp2, ... rh, ]
print "litterfall: ", litterfall
print "rh: ", rh
print "gpp: ", gpp
print "ra: ", ra
print "soil_flows: ", soil_flows
# make each pft a bit longer
pathlens = [1+(numpfts-i) for i,x in enumerate(gpp)]
pathlens.append(0.5) # make the departing rh flow shorter
# make all the pft flows come in from top
orients = [1 for i in range(len(pathlens))]
orients[-1] = 0 # change the lasts one (rh) to a side flow
labels = ['pft%s'%i for i, v in enumerate(soil_flows)]
labels[-1] = 'RH'
sankey.add(patchlabel="Soil",
flows=soil_flows,
fc='brown', label='soil',
labels=labels,
orientations=orients,
#pathlengths=1.0,
pathlengths=pathlens,
rotation=0,
trunklength=7.0,
)
print "[PFT ]: gpp ltrfl ra"
for pft, val in enumerate(gpp):
print "[PFT%s]: %.05f %.05f %.05f"%(pft, gpp[pft], -1.0*litterfall[pft], ra[pft])
print "-------"
# pft = 1
# print gpp[pft]
# print -1.0*litterfall[pft]
# print ra[pft]
#
# sankey.add(flows=[gpp[pft], -1.0*litterfall[pft], ra[pft]],
# fc=(0.0, 0.8, 0.0), # rgb
# label='pft%s' % (pft),
# labels=['gpp%s'%pft, 'lf%s'%pft, 'ra%s'%pft,],
# orientations=[-1,1,0], # 1(top), 0(l/r), -1(bottom)
# # seems to be in relation to prev. diagram...
# pathlengths=[1,pft*10,1],
# trunklength=12.0,
# prior=0, connect=(pft,1)
# )
# VEGETATION FLOWS
for pft, val in enumerate(gpp):
if (-1.0*litterfall[pft] < 0) or (-1.0*litterfall[pft] > 0):
sankey.add(flows=[gpp[pft], -1.0*litterfall[pft], ra[pft]],
fc=(0.0, 0.8, 0.0), # rgb
label='pft%s' % (pft),
labels=['gpp%s'%pft, 'lf%s'%pft, 'ra%s'%pft,],
orientations=[-1,1,-1], # 1(top), 0(l/r), -1(bottom)
# seems to be in relation to prev. diagram...
#pathlengths=[1,pft+10,1],
trunklength=12.0,
prior=0, connect=(pft,1)
)
else:
pass
# sankey.add(flows=[gpp[pft], -1.0*litterfall[pft], ra[pft]],
# fc=(0.0, 0.8, 0.0), # rgb
# label='pft%s' % (pft),
# labels=['gpp%s'%pft, 'lf%s'%pft, 'ra%s'%pft,],
# orientations=[-1,1,-1], # 1(top), 0(l/r), -1(bottom)
# # seems to be in relation to prev. diagram...
# pathlengths=[1,6,1],
# #trunklength=2.0,
# #prior=0, connect=(pft,1)
# )
diagrams = sankey.finish()
#print diagrams
#diagrams[-1].patch.set_hatch('/')
plt.legend(loc='best')
plt.title("The default settings produce a diagram like this.")
wrapup(args)
def wrapup(args):
if (args.display):
print "Displaying..."
plt.show()
if (args.save):
outputfile = args.save
print "Saving %s..." % outputfile
plt.savefig(outputfile, dpi=300)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='''Plots a Sankey diagram of
the fluxes from an output-*.nc file from dvm-dos-tem.
This plot captured only one timestep. There might be a way to animate it to
cover the time range?
Another option might be some sort of yearly averaging...
''')
#group = parser.add_mutually_exclusive_group()
#group.add_argument('-n', '--normal', action="store_true")
#group.add_argument('-e', '--explorer', action="store_true")
parser.add_argument('-d', '--display', action='store_true', help="Display the plot")
parser.add_argument('-s', '--save', default=False, help="Save the plot to simple-plot.png")
parser.add_argument('-c', '--cohort', required=True, type=int, help='Which cohort to plot')
parser.add_argument('-y', '--year', required=True, type=int, help='Which year to plot.')
parser.add_argument('-m', '--month', required=True, type=int, help='Which month to plot.')
parser.add_argument('inputfile', help='path to a NetCDF file to read from (A).')
args = parser.parse_args()
#print args
main(args)