Skip to content

Commit 77a5877

Browse files
committed
add FILE_PATHS constants
1 parent 125f12f commit 77a5877

File tree

1 file changed

+45
-52
lines changed

1 file changed

+45
-52
lines changed

utils/json-from-csv.seed.js

Lines changed: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,57 @@
11
const csv = require("csvtojson");
22
const fs = require("fs");
3-
const STATES = require("../constants/states-map");
3+
const { STATES } = require("../constants/states-map");
4+
const { FILE_PATHS } = require("../constants/file-paths");
45

5-
const createJsonFileFromCsv = async (csvFile, destPath) => {
6-
const jsonRowsFromCsvFile = await csv().fromFile(csvFile);
6+
const createJsonFileFromCsv = async (csvPath, destPath) => {
7+
const jsonRowsFromCsvFile = await csv().fromFile(csvPath);
78

8-
const tornadoSeedRows = jsonRowsFromCsvFile.map(
9-
(tornado) => {
10-
return {
11-
nwsNumber: tornado.om, // after 2006, assign sequentially
12-
year: tornado.yr,
13-
month: tornado.mo,
14-
day: tornado.dy,
15-
date: tornado.date,
16-
time: tornado.time,
17-
timezone: tornado.tz,
18-
stateAbbreviation: tornado.st,
19-
// stateName: STATES[tornado.st], // ** ADDED PROPERTY **
20-
stateFips: tornado.stf,
21-
stateNumForYear: tornado.stn, // discontinued in 2008
22-
fScale: tornado.mag || tornado.f, // went to EF scale in February 2007
23-
injuries: tornado.inj,
24-
fatalities: tornado.fat,
25-
propertyDamage: tornado.loss, // currently in millions but may change
26-
cropDamage: tornado.closs,
27-
startLatitude: tornado.slat,
28-
startLongitude: tornado.slon,
29-
endLatitude: tornado.elat,
30-
endLongitude: tornado.elon,
31-
lengthInMiles: tornado.len,
32-
widthInYards: tornado.wid,
33-
affectedStatesCount: tornado.ns,
34-
isEntireTrackInState: tornado.sn, // use when summing injuries, fatalities, property damage, & crop damage
35-
segmentNumber: tornado.sg,
36-
county1fips: tornado.f1,
37-
county2fips: tornado.f2,
38-
county3fips: tornado.f3,
39-
county4fips: tornado.f4,
40-
isChangedFScale: tornado.fc, // added in 2016 to account for records altered between 1950-1982
41-
};
42-
}
43-
);
9+
const tornadoSeedRows = jsonRowsFromCsvFile.map((tornado) => {
10+
return {
11+
nwsNumber: tornado.om, // after 2006, assign sequentially
12+
year: tornado.yr,
13+
month: tornado.mo,
14+
day: tornado.dy,
15+
date: tornado.date,
16+
time: tornado.time,
17+
timezone: tornado.tz,
18+
stateAbbreviation: tornado.st,
19+
// stateName: STATES[tornado.st], // ** ADDED PROPERTY **
20+
stateFips: tornado.stf,
21+
stateNumForYear: tornado.stn, // discontinued in 2008
22+
fScale: tornado.mag || tornado.f, // went to EF scale in February 2007
23+
injuries: tornado.inj,
24+
fatalities: tornado.fat,
25+
propertyDamage: tornado.loss, // currently in millions but may change
26+
cropDamage: tornado.closs,
27+
startLatitude: tornado.slat,
28+
startLongitude: tornado.slon,
29+
endLatitude: tornado.elat,
30+
endLongitude: tornado.elon,
31+
lengthInMiles: tornado.len,
32+
widthInYards: tornado.wid,
33+
affectedStatesCount: tornado.ns,
34+
isEntireTrackInState: tornado.sn, // use when summing injuries, fatalities, property damage, & crop damage
35+
segmentNumber: tornado.sg,
36+
county1fips: tornado.f1,
37+
county2fips: tornado.f2,
38+
county3fips: tornado.f3,
39+
county4fips: tornado.f4,
40+
isChangedFScale: tornado.fc, // added in 2016 to account for records altered between 1950-1982
41+
};
42+
});
4443

4544
writeJsonToFile(destPath, tornadoSeedRows);
4645
};
4746

48-
function writeJsonToFile(destFilePath, jsonData) {
49-
fs.writeFileSync(
50-
destFilePath,
51-
JSON.stringify(jsonData),
52-
(error) => {
53-
if (error) throw Error;
54-
console.log(
55-
"Single-Track Tornado JSON File Created!!"
56-
);
57-
}
58-
);
47+
function writeJsonToFile(destPath, jsonData) {
48+
fs.writeFileSync(destPath, JSON.stringify(jsonData), (error) => {
49+
if (error) throw Error;
50+
console.log("Single-Track Tornado JSON File Created!!");
51+
});
5952
}
6053

6154
createJsonFileFromCsv(
62-
"data/spc_1950_2022_single_track_tornadoes.csv",
63-
"data/seed_1950_2022_single_track_tornadoes.json"
55+
FILE_PATHS.spc_single_track_tornadoes_csv,
56+
FILE_PATHS.single_track_tornadoes_json
6457
);

0 commit comments

Comments
 (0)