forked from spatialmodel/inmap
/
inventory.go
470 lines (408 loc) · 13 KB
/
inventory.go
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
Copyright (C) 2012-2014 the InMAP authors.
This file is part of InMAP.
InMAP is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
InMAP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with InMAP. If not, see <http://www.gnu.org/licenses/>.
*/
package aep
import (
"encoding/gob"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/ctessum/geom"
"github.com/ctessum/geom/proj"
"github.com/ctessum/unit"
"github.com/ctessum/unit/badunit"
)
const (
nullVal = "-9"
commentRune = '#'
)
func init() {
gob.Register(geom.Polygon{})
gob.Register(geom.Point{})
gob.Register(geom.LineString{})
}
type Location struct {
geom.Geom
SR *proj.SR
Name string
}
func (l *Location) String() string {
if l.Name == "" {
panic("location must have name")
}
return l.Name
}
func (l *Location) Reproject(sr *proj.SR) (geom.Geom, error) {
ct, err := l.SR.NewTransform(sr)
if err != nil {
return nil, err
}
return l.Geom.Transform(ct)
}
// A Record holds data from a parsed emissions inventory record.
type Record interface {
// Key returns a unique identifier for this record.
Key() string
// Location returns the polygon representing the location of emissions.
Location() *Location
// GetSCC returns the SCC associated with this record.
GetSCC() string
// GetFIPS returns the FIPS associated with this record.
GetFIPS() string
// GetCountry returns the Country associated with this record.
GetCountry() Country
// GetEmissions returns the emissions associated with this record
GetEmissions() *Emissions
// CombineEmissions combines emissions from r2 into this record.
CombineEmissions(r2 Record)
// Totals returns the total emissions in units of grams.
Totals() map[Pollutant]*unit.Unit
// PeriodTotals returns the total emissions from this emissions source between
// the times begin and end.
PeriodTotals(begin, end time.Time) map[Pollutant]*unit.Unit
}
// RecordElevated describes emissions that are released from above ground.
type RecordElevated interface {
Record
// StackParameters describes the parameters of the emissions release
// from a elevated stack.
StackParameters() (StackHeight, StackDiameter, StackTemp, StackFlow, StackVelocity *unit.Unit)
// GroundLevel returns true if the receiver emissions are
// at ground level and false if they are elevated.
GroundLevel() bool
}
// PointRecord holds information about an emissions source that has a point
// location.
type PointRecord struct {
SourceData
PointSourceData
EconomicData
ControlData
Emissions
}
// Key returns a unique key for this record.
func (r *PointRecord) Key() string {
return r.SourceData.Key() + r.PointSourceData.Key()
}
// PolygonRecord holds information about an emissions source that has an area
// (i.e., polygon) location. The polygon is designated by the SourceData.FIPS code.
type PolygonRecord struct {
SourceDataLocation
EconomicData
ControlData
Emissions
}
// PointData exists to fulfill the Record interface but always returns
// nil because this is not a point source.
func (r *PolygonRecord) PointData() *PointSourceData { return nil }
// nobusinessPolygonRecord is a nonpoint record that does not have any
// economic information.
type nobusinessPolygonRecord struct {
SourceDataLocation
ControlData
Emissions
}
// PointData exists to fulfill the Record interface but always returns
// nil because this is not a point source.
func (r *nobusinessPolygonRecord) PointData() *PointSourceData { return nil }
// nocontrolPolygonRecord is a polygon record without any control information.
type nocontrolPolygonRecord struct {
SourceDataLocation
EconomicData
Emissions
}
// basicPolygonRecord is a basic polygon record information.
type basicPolygonRecord struct {
geom.Polygonal
SR *proj.SR
SourceData
Emissions
LocationName string
}
// PointData exists to fulfill the Record interface but always returns
// nil because this is not a point source.
func (r *basicPolygonRecord) PointData() *PointSourceData { return nil }
// Location returns the polygon representing the location of emissions.
func (r *basicPolygonRecord) Location() *Location {
return &Location{Geom: r.Polygonal, SR: r.SR, Name: r.LocationName}
}
type supplementalPointRecord struct {
SourceData
PointSourceData
Emissions
}
// Key returns a unique key for this record.
func (r *supplementalPointRecord) Key() string {
return r.SourceData.Key() + r.PointSourceData.Key()
}
// stringToFloat converts a string to a floating point number.
// If the string is "" or "-9" it returns 0.
func stringToFloat(s string) (float64, error) {
s = strings.TrimSpace(s)
if s == "" || s == nullVal {
return 0, nil
}
f, err := strconv.ParseFloat(s, 64)
return f, err
}
// Get rid of extra quotation marks and copy the string so the
// whole line from the input file isn't held in memory
func trimString(s string) string {
return string([]byte(strings.Trim(s, "\" ")))
}
// InventoryFrequency describes how many often new inventory files are required.
type InventoryFrequency string
// Inventory frequencies can either be annual or monthly
const (
Annually InventoryFrequency = "annual"
Monthly InventoryFrequency = "monthly"
)
// An EmissionsReader reads SMOKE formatted emissions files.
type EmissionsReader struct {
polsToKeep Speciation
freq InventoryFrequency
inputConv func(float64) *unit.Unit
sourceDataLocator *sourceDataLocator
// Group specifies a group name for files read by this reader.
// It is used for report creation
Group string
}
// InputUnits specify available options for emissions input units.
type InputUnits int
const (
// Ton is short tons
Ton InputUnits = iota
// Tonne is metric tons
Tonne
// Kg is kilograms
Kg
// G is grams
G
// Lb is pounds
Lb
)
// ParseInputUnits parses a string representation of an input unit
// type. Currently supported options are "tons", "tonnes", "kg", "lbs", and "g".
func ParseInputUnits(units string) (InputUnits, error) {
switch units {
case "tons":
return Ton, nil
case "tonnes":
return Tonne, nil
case "kg":
return Kg, nil
case "lbs":
return Lb, nil
case "g":
return G, nil
default:
return -1, fmt.Errorf("aep.ParseInputUnits: invalid input units '%s'", units)
}
}
// Conversion returns a function that converts a value to units of kilograms.
// factor reprents an additional factor the value should be multiplied by.
func (u InputUnits) Conversion(factor float64) func(v float64) *unit.Unit {
switch u {
case Ton:
return func(v float64) *unit.Unit { return badunit.Ton(v * factor) }
case Tonne:
return func(v float64) *unit.Unit { return unit.New(v/1000.*factor, unit.Kilogram) }
case Kg:
return func(v float64) *unit.Unit { return unit.New(v*factor, unit.Kilogram) }
case G:
return func(v float64) *unit.Unit { return unit.New(v/1000.*factor, unit.Kilogram) }
case Lb:
return func(v float64) *unit.Unit { return badunit.Pound(v * factor) }
default:
panic(fmt.Errorf("aep.NewEmissionsReader: unknown value %d"+
" for variable InputUnits. Acceptable values are Ton, "+
"Tonne, Kg, G, and Lb.", u))
}
}
// NewEmissionsReader creates a new EmissionsReader. polsToKeep specifies which
// pollutants from the inventory to keep. If it is nil, all pollutants are kept.
// InputUnits is the units of input data. Acceptable values are `tons',
// `tonnes', `kg', `g', and `lbs'.
// gr and sp are used to reference emissions records to geographic locations;
// if they are both nil, the location referencing is skipped.
func NewEmissionsReader(polsToKeep Speciation, freq InventoryFrequency, InputUnits InputUnits, gr *GridRef, sp *SrgSpecs) (*EmissionsReader, error) {
e := new(EmissionsReader)
e.polsToKeep = polsToKeep
e.freq = freq
e.sourceDataLocator = newSourceDataLocator(gr, sp)
var monthlyConv = 1.
if freq == Monthly {
// If the freqency is monthly, the emissions need to be divided by 12 because
// they are represented as annual emissions in the files but they only happen
// for 1/12 of the year.
monthlyConv = 1. / 12.
}
e.inputConv = InputUnits.Conversion(monthlyConv)
return e, nil
}
// OpenFilesFromTemplate opens the files that match the template.
func (e *EmissionsReader) OpenFilesFromTemplate(filetemplate string) ([]*InventoryFile, error) {
var files []*InventoryFile
if e.freq == Monthly {
files = make([]*InventoryFile, 12)
for i, p := range []Period{Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec} {
file := strings.Replace(filetemplate, "[month]", strings.ToLower(p.String()), -1)
file = os.ExpandEnv(file)
f, err := os.Open(file)
if err != nil {
return nil, fmt.Errorf("aep: opening emissions file: %v", err)
}
files[i], err = NewInventoryFile(file, f, p, e.inputConv)
if err != nil {
return nil, err
}
files[i].group = e.Group
}
return files, nil
}
if e.freq == Annually {
file := os.ExpandEnv(filetemplate)
f, err := os.Open(file)
if err != nil {
return nil, fmt.Errorf("aep: opening emissions file: %v", err)
}
invF, err := NewInventoryFile(file, f, Annual, e.inputConv)
if err != nil {
return nil, err
}
invF.group = e.Group
return []*InventoryFile{invF}, nil
}
panic(fmt.Errorf("unsupported inventory frequency '%v'", e.freq))
}
// RecFilter is a class of functions that return true if a record should be kept
// and processed.
type RecFilter func(Record) bool
type sourceDataLocationer interface {
getSourceDataLocation() *SourceDataLocation
}
// TODO: Double-counting tracking has been removed from here.
// Make sure to add it back somewhere else.
// ReadFiles reads emissions associated with period p from the specified files,
// and returns emissions records and a summary report.
// The specified filenames are only used for reporting. If multiple files have
// data for the same specific facility (for instance, if one file has CAPs
// emissions and the other has HAPs emissions) they need to be processed in this
// function together to avoid double counting in speciation. (If you will
// not be speciating the emissions, then it doesn't matter.) f is an optional
// filter function to determine which records should be kept. If f is nil, all
// records will be kept. The caller is responsible for closing the files.
func (e *EmissionsReader) ReadFiles(files []*InventoryFile, f RecFilter) ([]Record, *InventoryReport, error) {
report := new(InventoryReport)
records := make(map[string]Record)
fileRecordChan := make(chan recordErr)
var wg sync.WaitGroup
wg.Add(len(files))
for _, file := range files {
go file.parseLines(e, f, fileRecordChan, &wg)
}
go func() {
wg.Wait()
close(fileRecordChan)
}()
for recordErr := range fileRecordChan {
if recordErr.err != nil {
return nil, nil, recordErr.err
}
record := recordErr.rec
key := record.Key()
if rec, ok := records[key]; !ok {
// We don't yet have a record for this key
records[key] = record
} else {
rec.CombineEmissions(record)
records[key] = rec
}
}
for _, file := range files {
report.AddData(file)
}
recordList := make([]Record, 0, len(records))
for _, r := range records {
if ar, ok := r.(sourceDataLocationer); ok {
if err := e.sourceDataLocator.Locate(ar.getSourceDataLocation()); err != nil {
log.Println(err)
continue // Drop records we can't find a location for.
// return nil, nil, err
}
}
recordList = append(recordList, r)
}
return recordList, report, nil
}
// splitPol splits a pollutant name from its prefix.
func splitPol(pol string) (polname, prefix string) {
pol = trimString(pol)
if strings.Index(pol, "__") != -1 {
polname = strings.Split(pol, "__")[1]
prefix = strings.Split(pol, "__")[0]
} else {
polname = pol
}
return
}
type recordErr struct {
rec Record
err error
}
// parseLines parses the lines of a file. f, if non-nil, specifies which
// records should be kept.
func (f *InventoryFile) parseLines(e *EmissionsReader, filter RecFilter, recordChan chan recordErr, wg *sync.WaitGroup) {
for {
record, err := f.parseLine()
if err != nil {
if err == io.EOF {
break
}
err = fmt.Errorf("aep.InventoryFile.parseLines: file: %s\nerr: %v", f.Name(), err)
recordChan <- recordErr{rec: nil, err: err}
return
}
if record == nil {
continue
}
if filter != nil && !filter(record) {
continue // Skip this record if it doesn't match our filter.
}
// add emissions to totals for report
droppedTotals := record.GetEmissions().DropPols(e.polsToKeep)
for pol, val := range droppedTotals {
if _, ok := f.droppedTotals[pol]; !ok {
f.droppedTotals[pol] = val
} else {
f.droppedTotals[pol].Add(val)
}
}
totals := record.Totals()
for pol, val := range totals {
if _, ok := f.totals[pol]; !ok {
f.totals[pol] = val
} else {
f.totals[pol].Add(val)
}
}
recordChan <- recordErr{rec: record, err: nil}
}
wg.Done()
}