-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbiomass.py
294 lines (266 loc) · 12 KB
/
biomass.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# -*- coding: utf-8 -*-
"""
Biomass Urban Forestry calculator.
Calculates biomass, carbon, and equivalent CO2 for a tree given the species,
region, dbh, and height.
"""
import sqlite3
from math import exp, log, log10
import growth
# alter depending on install path.
UrbForDB = "/home/adh/UrbanForests/UrbanForestCC.sqlite"
roots = 0.78
carbon_fraction = 0.5
co2_fraction = 3.67
dbconn = sqlite3.connect(UrbForDB)
# in the below how do I handle TypeError: 'NoneType' object is not iterable?
def equation_dw12(dbconn, speccode, biomassassign, dbh, ht):
""" Equation form: a * (dbh^b) * c. """
qstr = "SELECT a, b FROM VolBioCoeffs WHERE SpecCode = '%s'" % (biomassassign)
c = dbconn.cursor()
c.execute(qstr)
coeffs = c.fetchone()
(a, b) = coeffs
biomass = float(a) * (dbh** float(b))
carbon = carbon_fraction * biomass / roots
co2 = carbon * co2_fraction
return (biomass, carbon, co2)
def equation_dw13(dbconn, speccode, biomassassign, dbh, ht):
""" Equation form: (a*ht+b)+(c*ht+d) """
qstr = "SELECT a, b, c, d FROM VolBioCoeffs WHERE SpecCode = '%s'" % (biomassassign)
c = dbconn.cursor()
c.execute(qstr)
coeffs = c.fetchone()
(a, b, c1, d) = coeffs
biomass = float(a) * ht + float(b) + float(c1) * ht + float(d)
carbon = carbon_fraction * biomass / roots
co2 = carbon * co2_fraction
return (biomass, carbon, co2)
def equation_dw14(dbconn, speccode, biomassassign, dbh, ht):
""" Equation form: (exp(a+b*(ln(dbh)))+exp(c+d*(ln(dbh))) """
qstr = "SELECT a, b, c, d FROM VolBioCoeffs WHERE SpecCode = '%s'" % (biomassassign)
c = dbconn.cursor()
c.execute(qstr)
coeffs = c.fetchone()
(a, b, c1, d) = coeffs
biomass = exp(float(a) + float(b) * log(dbh)) + exp(float(c1) + float(d) * log(dbh))
carbon = carbon_fraction * biomass / roots
co2 = carbon * co2_fraction
return (biomass, carbon, co2)
def equation_dw15(dbconn, speccode, biomassassign, dbh, ht):
""" Equation form: (exp(a+b*ln(dbh)))*c """
qstr = "SELECT a, b FROM VolBioCoeffs WHERE SpecCode = '%s'" % (biomassassign)
c = dbconn.cursor()
c.execute(qstr)
coeffs = c.fetchone()
(a, b) = coeffs
biomass = exp(float(a) + float(b) * log(dbh))
carbon = carbon_fraction * biomass / roots
co2 = carbon * co2_fraction
return (biomass, carbon, co2)
def equation_dw16(dbconn, speccode, biomassassign, dbh, ht):
""" Equation form: a^(b+c*(log10(dbh^d))) """
qstr = "SELECT a, b, c, d FROM VolBioCoeffs WHERE SpecCode = '%s'" % (biomassassign)
c = dbconn.cursor()
c.execute(qstr)
coeffs = c.fetchone()
(a, b, c1, d) = coeffs
biomass = float(a) ** (float(b) + float(c1) * log10(dbh ** float(d)))
carbon = carbon_fraction * biomass / roots
co2 = carbon * co2_fraction
return (biomass, carbon, co2)
def equation_dw18(dbconn, speccode, biomassassign, dbh, ht):
""" Equation form: ((a + b * dbh + c * (dbh ^ d))-(e + f * dbh + g * (dbh ^ h))) """
qstr = "SELECT a, b, c, d, e, f, g, h FROM VolBioCoeffs WHERE SpecCode = '%s'" % (biomassassign)
c = dbconn.cursor()
c.execute(qstr)
coeffs = c.fetchone()
(a, b, c1, d, e, f, g, h) = coeffs
biomass = float(a) + float(b) * dbh + float(c1) * (dbh ** float(d)) - (float(e) + float(f) * dbh + float(g) * (dbh ** float(h)))
carbon = carbon_fraction * biomass / roots
co2 = carbon * co2_fraction
return (biomass, carbon, co2)
def equation_dw19(dbconn, speccode, biomassassign, dbh, ht):
""" Equation form: a*((SGsp)dbh^2*ht)^c where SGsp = specific gravity for species."""
qstr = "SELECT a, c FROM VolBioCoeffs WHERE SpecCode = '%s'" % (biomassassign)
c = dbconn.cursor()
c.execute(qstr)
coeffs = c.fetchone()
(a, c1) = coeffs
qstr = "SELECT DWDensity FROM SpeciesCodeList WHERE SpeciesCode = '%s'" % (speccode)
c.execute(qstr)
(density,) = c.fetchone()
biomass = float(a) * ((float(density)/1000.0) * (dbh ** 2) * ht) ** float(c1)
carbon = carbon_fraction * biomass / roots
co2 = carbon * co2_fraction
return (biomass, carbon, co2)
def equation_dw20(dbconn, speccode, biomassassign, dbh, ht):
""" Equation form: exp(a+b*log(dbh))-exp(c+(d/dbh)) """
qstr = "SELECT a, b, c, d FROM VolBioCoeffs WHERE SpecCode = '%s'" % (biomassassign)
c = dbconn.cursor()
c.execute(qstr)
coeffs = c.fetchone()
(a, b, c1, d) = coeffs
biomass = exp(float(a) + float(b) * log(dbh)) - exp(float(c1) + float(d)/dbh)
carbon = carbon_fraction * biomass / roots
co2 = carbon * co2_fraction
return (biomass, carbon, co2)
def equation_dw21(dbconn, speccode, biomassassign, dbh, ht):
"""Equation form: a*(dbh^b)-(exp(c+(d/dbh)) """
qstr = "SELECT a, b, c, d FROM VolBioCoeffs WHERE SpecCode = '%s'" % (biomassassign)
c = dbconn.cursor()
c.execute(qstr)
coeffs = c.fetchone()
(a, b, c1, d) = coeffs
#print "coeffs: ", a, b, c1, d
biomass = float(a) * (dbh ** float(b)) - exp(float(c1) + float(d)/dbh)
carbon = carbon_fraction * biomass / roots
co2 = carbon * co2_fraction
return (biomass, carbon, co2)
def equation_dw22(dbconn, speccode, biomassassign, dbh, ht):
"""Equation form: exp(a+b*ln(dbh)) """
qstr = "SELECT a, b FROM VolBioCoeffs WHERE SpecCode = '%s'" % (biomassassign)
c = dbconn.cursor()
c.execute(qstr)
coeffs = c.fetchone()
(a, b) = coeffs
biomass = exp(float(a) + float(b) * log(dbh))
carbon = carbon_fraction * biomass / roots
co2 = carbon * co2_fraction
return (biomass, carbon, co2)
def equation_dw23(dbconn, speccode, biomassassign, dbh, ht):
"""Equation form: exp(a+b*ln(dbh)+c*ln(ht)+d*ln(e/f) """
qstr = "SELECT a, b, c, d, f FROM VolBioCoeffs WHERE SpecCode = '%s'" % (biomassassign)
c = dbconn.cursor()
c.execute(qstr)
coeffs = c.fetchone()
(a, b, c1, d, f) = coeffs
qstr = "SELECT DWDensity FROM SpeciesCodeList WHERE SpeciesCode = '%s'" % (speccode)
c.execute(qstr)
(density,) = c.fetchone()
biomass = exp(float(a) + float(b) * log(dbh) + float(c1) * log(ht) + float(d) * log(float(density)/float(f)) )
carbon = carbon_fraction * biomass / roots
co2 = carbon * co2_fraction
return (biomass, carbon, co2)
def equation_fw1(dbconn, speccode, biomassassign, dbh, ht):
"""Equation form: a*(dbh)^b """
qstr = "SELECT a, b FROM VolBioCoeffs WHERE SpecCode = '%s'" % (biomassassign)
c = dbconn.cursor()
c.execute(qstr)
coeffs = c.fetchone()
(a, b) = coeffs
qstr = "SELECT DWDensity FROM SpeciesCodeList WHERE SpeciesCode = '%s'" % (speccode)
c.execute(qstr)
(density,) = c.fetchone()
biomass = float(a) * (dbh ** float(b)) * (float(density)/1000.0)
carbon = carbon_fraction * biomass / roots
co2 = carbon * co2_fraction
return (biomass, carbon, co2)
def equation_vol10(dbconn, speccode, biomassassign, dbh, ht):
"""Equation form: a*dbh^b*ht^c """
qstr = "SELECT a, b, c FROM VolBioCoeffs WHERE SpecCode = '%s'" % (biomassassign)
c = dbconn.cursor()
c.execute(qstr)
coeffs = c.fetchone()
(a, b, c1) = coeffs
qstr = "SELECT DWDensity FROM SpeciesCodeList WHERE SpeciesCode = '%s'" % (speccode)
c.execute(qstr)
(density,) = c.fetchone()
biomass = float(a) * (dbh ** float(b)) * (ht ** float(c1)) * float(density)
carbon = carbon_fraction * biomass / roots
co2 = carbon * co2_fraction
return (biomass, carbon, co2)
def equation_vol11(dbconn, speccode, biomassassign, dbh, ht):
"""Equation form: a(b*(dbh/c)^d*(e*ht)^f) """
qstr = "SELECT a, b, c, d, e, f FROM VolBioCoeffs WHERE SpecCode = '%s'" % (biomassassign)
c = dbconn.cursor()
c.execute(qstr)
coeffs = c.fetchone()
(a, b, c1, d, e, f) = coeffs
qstr = "SELECT DWDensity FROM SpeciesCodeList WHERE SpeciesCode = '%s'" % (speccode)
c.execute(qstr)
(density,) = c.fetchone()
# biomass = float(a) * (float(b) * (dbh/float(c1)) ** float(d)) * (float(e) * (ht ** float(f))) * float(density)
# JdG correction below.
biomass = float(a) * (float(b) * (dbh/float(c1)) ** float(d)) * ((float(e) * ht) ** float(f)) * float(density)
carbon = carbon_fraction * biomass / roots
co2 = carbon * co2_fraction
return (biomass, carbon, co2)
eqn_lookup = {'dw12': equation_dw12, 'dw13': equation_dw13, 'dw14': equation_dw14,
'dw15': equation_dw15, 'dw16': equation_dw16, 'dw18': equation_dw18,
'dw19': equation_dw19, 'dw20': equation_dw20, 'dw21': equation_dw21,
'dw22': equation_dw22, 'dw23': equation_dw23, 'fw1': equation_fw1,
'vol10': equation_vol10, 'vol11': equation_vol11}
minmaxtypedict = {'dw12': 'dbh', 'dw13': 'ht', 'dw14': 'dbh',
'dw15': 'dbh', 'dw16': 'dbh', 'dw18': 'dbh',
'dw19': 'dbh', 'dw20': 'dbh', 'dw21': 'dbh', # dw19, dw23, vol10, vol11 both
'dw22': 'dbh', 'dw23': 'dbh', 'fw1': 'dbh',
'vol10': 'dbh', 'vol11': 'dbh'}
def biomass_calc(dbconn, speccode, region, dbh=0, ht=0, useminmax=False, negcorrect=False):
"""Calculate biomass, carbon, and CO2 equivalent given tree data.
Args:
dbconn - database connection handle
speccode - species code
region - region code
dbh - tree dbh in cm
ht - tree height in m
useminmax - flag to limit dbh or height over which equations are applicable
negcorrect - iteration correction upwards if calculated biomass < 0
Returns:
biomass - biomass of tree in kg
carbon - carbon stored by tree in kg
co2 - CO2 equivalent of tree biomass
"""
#qstr = "SELECT BiomassEqn FROM SpeciesCodeList WHERE SpeciesCode = '%s'" % (speccode)
qstr = "SELECT b.EqnName, b.SpecCode FROM SpeciesCodeList a, VolBioCoeffs b WHERE a.BioMassAssign = b.SpecCode AND a.SpeciesCode = '%s' AND a.Region = '%s'" % (speccode, region)
c = dbconn.cursor()
c.execute(qstr)
qresult = c.fetchone()
(eqn, biomassassign) = qresult
if useminmax:
minmaxtype = minmaxtypedict[eqn]
growthspecies = growth.growth_calc_species(dbconn, speccode, region)
# qstr = "SELECT AppsMin, AppsMax FROM GrowCoeffs WHERE SpecCode = '%s' AND Region = '%s'" % (growthspecies, region)
if minmaxtype == 'dbh':
qstr = "SELECT AppsMin, AppsMax FROM GrowCoeffs WHERE SpecCode = '%s' AND Region = '%s' AND Component = 'd.b.h.'" % (growthspecies, region)
else:
qstr = "SELECT AppsMin, AppsMax FROM GrowCoeffs WHERE SpecCode = '%s' AND Region = '%s' AND Component = 'tree ht'" % (growthspecies, region)
c.execute(qstr)
qresult = c.fetchone()
(appsmin, appsmax) = qresult
#print appsmin, appsmax
appsmin = float(appsmin)
appsmax = float(appsmax)
if minmaxtype == 'dbh':
if dbh < appsmin:
dbh = appsmin
elif dbh > appsmax:
dbh = appsmax
else:
if ht < appsmin:
ht = appsmin
elif ht > appsmax:
ht = appsmax
#print eqn, biomassassign
(biomass, carbon, co2) = eqn_lookup[eqn](dbconn, speccode, biomassassign, dbh, ht)
# If biomass ends up negative in calculation, iterate upwards in terms of integral age until
# biomass no longer negative
if negcorrect:
if biomass < 0:
testage = 0
testbiomass = biomass
while testbiomass < 0:
(testsizeval, testtype) = growth.inv_age_calc(dbconn, speccode, region, testage)
if testsizeval < appsmin:
testsizeval = appsmin
elif testsizeval > appsmax:
testsizeval = appsmax
if testtype == 'dbh':
(testbiomass, testcarbon, testco2) = eqn_lookup[eqn](dbconn, speccode, biomassassign, testsizeval, ht)
else:
(testbiomass, testcarbon, testco2) = eqn_lookup[eqn](dbconn, speccode, biomassassign, dbh, testsizeval)
testage += 1
biomass = testbiomass
carbon = testcarbon
co2 = testco2
return (biomass, carbon, co2)