forked from spatialmodel/inmap
/
ida.go
288 lines (249 loc) · 8.59 KB
/
ida.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
/*
Copyright © 2017 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 (
"bufio"
"fmt"
"io"
"strings"
"time"
"github.com/ctessum/unit"
)
// pointRecordIDA holds information about an emissions source that has a point
// location. IDA records have pollutant-specific control information.
type pointRecordIDA struct {
SourceData
PointSourceData
EconomicData
ControlData map[string]ControlData
Emissions
}
// Key returns a unique key for this record.
func (r *pointRecordIDA) Key() string {
return r.SourceData.Key() + r.PointSourceData.Key()
}
// polyonRecordIDA holds information about an emissions source that has a polygon
// location. IDA records have pollutant-specific control information.
type polygonRecordIDA struct {
SourceDataLocation
ControlData map[string]ControlData
Emissions
}
// PointData exists to fulfill the Record interface but always returns
// nil because this is not a point source.
func (r *polygonRecordIDA) PointData() *PointSourceData { return nil }
// mobilePolygonRecordIDA holds information about an emissions source that has a polygon
// location and only has source and emissions data.
type mobilePolygonRecordIDA struct {
SourceDataLocation
Emissions
}
// PointData exists to fulfill the Record interface but always returns
// nil because this is not a point source.
func (r *mobilePolygonRecordIDA) PointData() *PointSourceData { return nil }
func (f *InventoryFile) readHeaderIDA(inputConverter func(float64) *unit.Unit) error {
year, country, err := f.readHeaderGeneral()
if err != nil {
return err
}
begin, end, err := f.Period.TimeInterval(year)
if err != nil {
return err
}
buf := bufio.NewScanner(f.ReadSeeker)
// get pollutant IDs
var polids []string
var record string
for buf.Scan() {
record = buf.Text()
if len(record) > 0 && record[0] != commentRune {
break
}
if len(record) > 6 && record[1:6] == "POLID" && len(polids) == 0 {
polids = strings.Split(strings.TrimSpace(record[6:]), " ")
}
if len(record) > 5 && record[1:5] == "DATA" && len(polids) == 0 {
polids = strings.Split(strings.TrimSpace(record[5:]), " ")
}
}
if err = buf.Err(); err != nil {
return fmt.Errorf("aep: in file %s: %v", f.Name(), err)
}
if len(polids) >= 10 {
// If there are more than 10 pollutants, there are situations where
// it can be ambiguous about which file type we are dealing with based
// on the line length.
return fmt.Errorf("aep: in file %s: too many pollutants (must be less than 10)", f.Name())
}
if err = buf.Err(); err != nil {
return fmt.Errorf("aep: in file %s: %v", f.Name(), err)
}
var recFunc func(string, []string, Country, time.Time, time.Time, func(float64) *unit.Unit) (Record, error)
switch len(record) {
case 249 + 52*len(polids): // Point record
recFunc = NewIDAPoint
case 15 + 47*len(polids): // Area record
recFunc = NewIDAArea
case 25 + 20*len(polids): // Mobile record
recFunc = NewIDAMobile
default:
return fmt.Errorf("in aep.readHeaderIDA: unsupported line length %d with %d pollutants", len(record), len(polids))
}
// rewind the file
_, err = f.ReadSeeker.Seek(0, 0)
if err != nil {
return err
}
buf = bufio.NewScanner(f.ReadSeeker)
f.parseLine = func() (Record, error) {
var line string
var err error
for buf.Scan() { // loop until we find a non-commented line.
line = buf.Text()
if line[0] != commentRune {
break
}
}
if err = buf.Err(); err != nil {
return nil, err
}
if len(line) == 0 {
return nil, io.EOF
}
return recFunc(line, polids, country, begin, end, inputConverter)
}
return nil
}
// NewIDAPoint creates a new record from the IDA point record rec, where
// pollutants are the names of the pollutants in the record, country is the country
// and year of the emissions, begin and end specify the time period
// this record covers, and inputConv specifies the factor
// to multiply emissions by to convert them to SI units.
func NewIDAPoint(rec string, pollutants []string, country Country, begin, end time.Time, inputConv func(float64) *unit.Unit) (Record, error) {
if len(rec) != 249+52*len(pollutants) {
return nil, fmt.Errorf("aep.NewIDAPoint: record should have a length of "+
"%d but instead it is %d", 249+52*len(pollutants), len(rec))
}
r := new(pointRecordIDA)
r.SourceData.Country = country
r.parseFIPS(rec[0:5])
r.PointSourceData.PlantID = trimString(rec[5:20])
r.PointSourceData.PointID = trimString(rec[20:35])
r.PointSourceData.StackID = trimString(rec[35:47])
r.PointSourceData.ORISFacilityCode = trimString(rec[47:53])
r.PointSourceData.ORISBoilerID = trimString(rec[53:59])
r.PointSourceData.Segment = trimString(rec[59:61])
r.PointSourceData.Plant = trimString(rec[61:101])
r.parseSCC(rec[101:111])
err := r.setStackParams(rec[119:123], rec[123:129], rec[129:133], rec[133:143], rec[143:152])
if err != nil {
return nil, err
}
r.parseSIC(rec[226:230])
lat := rec[230:239]
lon := rec[239:248]
err = r.setupLocation(lon, lat, "L", "", "")
if err != nil {
return nil, err
}
r.ControlData = make(map[string]ControlData)
for i, pol := range pollutants {
start := 249 + 52*i
ann, avd := rec[start:start+13], rec[start+13:start+26]
emisRate, err := parseEmisRateAnnual(ann, avd, inputConv)
if err != nil {
return nil, err
}
pol, prefix := splitPol(pol)
r.Emissions.Add(begin, end, pol, prefix, emisRate)
cd := new(ControlData)
err = cd.setCEff(rec[start+26 : start+33])
if err != nil {
return nil, err
}
err = cd.setREff(rec[start+33 : start+36])
if err != nil {
return nil, err
}
r.ControlData[pol] = *cd
}
return r, nil
}
// NewIDAArea creates a new record from the IDA area record rec, where
// pollutants are the names of the pollutants in the record, country is the country
// and year of the emissions, begin and end specify the time period
// this record covers, and inputConv specifies the factor
// to multiply emissions by to convert them to SI units.
func NewIDAArea(rec string, pollutants []string, country Country, begin, end time.Time, inputConv func(float64) *unit.Unit) (Record, error) {
if len(rec) != 15+47*len(pollutants) {
return nil, fmt.Errorf("aep.NewIDAArea: record should have a length of "+
"%d but instead it is %d", 15+47*len(pollutants), len(rec))
}
r := new(polygonRecordIDA)
r.SourceData.Country = country
r.parseFIPS(rec[0:5])
r.parseSCC(rec[5:15])
r.ControlData = make(map[string]ControlData)
for i, pol := range pollutants {
start := 15 + 47*i
ann, avd := rec[start:start+10], rec[start+10:start+20]
emisRate, err := parseEmisRateAnnual(ann, avd, inputConv)
if err != nil {
return nil, err
}
pol, prefix := splitPol(pol)
r.Emissions.Add(begin, end, pol, prefix, emisRate)
cd := new(ControlData)
err = cd.setCEff(rec[start+31 : start+38])
if err != nil {
return nil, err
}
err = cd.setREff(rec[start+38 : start+41])
if err != nil {
return nil, err
}
err = cd.setRPen(rec[start+41 : start+47])
if err != nil {
return nil, err
}
r.ControlData[pol] = *cd
}
return r, nil
}
// NewIDAMobile creates a new record from the IDA mobile record rec, where
// pollutants are the names of the pollutants in the record, country is the country
// and year of the emissions, begin and end specify the time period
// this record covers, and inputConv specifies the factor
// to multiply emissions by to convert them to SI units.
func NewIDAMobile(rec string, pollutants []string, country Country, begin, end time.Time, inputConv func(float64) *unit.Unit) (Record, error) {
if len(rec) != 25+20*len(pollutants) {
return nil, fmt.Errorf("aep.NewORLMobile: record should have a length of "+
"%d but instead it is %d", 25+20*len(pollutants), len(rec))
}
r := new(mobilePolygonRecordIDA)
r.SourceData.Country = country
r.parseFIPS(rec[0:5])
r.parseSCC(rec[15:25])
for i, pol := range pollutants {
start := 25 + 20*i
ann, avd := rec[start:start+10], rec[start+10:start+20]
emisRate, err := parseEmisRateAnnual(ann, avd, inputConv)
if err != nil {
return nil, err
}
pol, prefix := splitPol(pol)
r.Emissions.Add(begin, end, pol, prefix, emisRate)
}
return r, nil
}