|
1 | 1 | const csv = require("csvtojson"); |
2 | 2 | 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"); |
4 | 5 |
|
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); |
7 | 8 |
|
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 | + }); |
44 | 43 |
|
45 | 44 | writeJsonToFile(destPath, tornadoSeedRows); |
46 | 45 | }; |
47 | 46 |
|
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 | + }); |
59 | 52 | } |
60 | 53 |
|
61 | 54 | 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 |
64 | 57 | ); |
0 commit comments