-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
178 lines (147 loc) · 5.68 KB
/
index.js
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
const { RandomAccessFile } = require('./classes/RandomAccessFile')
const { Level2Record } = require('./classes/Level2Record')
const { BIG_ENDIAN, FILE_HEADER_SIZE } = require('./constants')
class Level2Radar {
constructor(file) {
this.elevation = 0
this.scan = 0
return new Promise(resolve => {
this.parseData(file).then(() => {
resolve(this)
})
})
}
setElevation(elevation) {
this.elevation = elevation - 1
}
setScan(scan) {
this.scan = scan - 1
}
setSweep(sweep) {
this.setScan(sweep)
}
getAzimuth(scan) {
if(scan) {
return this.data[this.elevation][scan].record.azimuth
} else {
return null
}
}
getScans() {
return this.data[this.elevation].length
}
// return reflectivity data for the current elevation and scan
getHighresReflectivity(scan) {
if(scan) {
return this.data[this.elevation][scan].record.reflect
} else {
let scans = []
for(let i = 0; i < this.data[this.elevation].length; i++) {
scans.push(this.data[this.elevation][i].record.reflect)
}
return scans
}
}
// return velocity data for the current elevation and scan
getHighresVelocity() {
return this.data[this.elevation][this.scan].record.velocity
}
// return spectrum data for the current elevation and scan
getHighresSpectrum() {
return this.data[this.elevation][this.scan].record.spectrum
}
// return diff reflectivity data for the current elevation and scan
getHighresDiffReflectivity() {
return this.data[this.elevation][this.scan].record.zdr
}
// return diff phase data for the current elevation and scan
getHighresDiffPhase() {
return this.data[this.elevation][this.scan].record.phi
}
// return correlation coefficient data for the current elevation and scan
getHighresCorrelationCoefficient() {
return this.data[this.elevation][this.scan].record.rho
}
/**
* Loads the file and parses the data.
* Returns a promise when completed
*/
parseData(file) {
return new Promise(resolve => {
/**
* Load and access the radar archive file.
* The constructor for RandomAccessFile returns
* a promise. This allows for parsing the data
* after the file has been fully loaded into the
* buffer.
*/
new RandomAccessFile(file).then(raf => {
let data = []
raf.endianOrder(BIG_ENDIAN) // Set binary ordering to Big Endian
raf.seek(FILE_HEADER_SIZE) // Jump to the bytes at 24, past the file header
let message_offset31 = 0 // the current message 31 offset
let recno = 0 // the record number
/**
* Loop through all of the messages
* contained within the radar archive file.
* Save all the data we find to it's respective array
*/
while(true) {
let r = new Level2Record(raf, recno++, message_offset31)
if(r.finished) break // no more messages, exit the loop
if(r.message_type === 31) {
// found a message 31 type, update the offset
message_offset31 = message_offset31 + (r.message_size * 2 + 12 - 2432)
}
// skip any messages that aren't type of 1 (generic radar data) or 31 (highres radar data)
if(r.message_type != 1 && r.message_type != 31) continue
// If data is found, push the record to the data array
if(r.record.reflect) data.push(r)
if(r.record.velocity) data.push(r)
if(r.record.spectrum) data.push(r)
if(r.record.zdr) data.push(r)
if(r.record.phi) data.push(r)
if(r.record.rho) data.push(r)
}
// sort and group the scans by elevation asc
this.data = this.groupAndSortScans(data)
resolve(true)
})
})
}
/**
* This takes the scans (aka sweeps) and groups them
* by their elevation numbers. This allows switching
* between different elevations, if available.
*/
groupAndSortScans(scans) {
let groups = []
// map the scans
scans.map(scan => {
let elevation_number = scan.record.elevation_number
/**
* If the group has already been created
* just push the current scan into the array
* or create a new group for the elevation
* NOTE: !! we need to convert the numbers to a
* string so that javascript doesn't freak out
* look into fixing !!
*/
if(groups[elevation_number + '']) {
groups[elevation_number + ''].push(scan)
} else {
groups[elevation_number + ''] = [scan]
}
})
// Sort by elevation number ascending
groups = groups.sort((a, b) => {
let a_elevation = a[0].record.elevation_number
let b_elevation = b[0].record.elevation_number
if(a_elevation > b_elevation) return 1
if(a_elevation < b_elevation) return -1
return 0
})
return groups
}
}
module.exports.Level2Radar = Level2Radar