Created
December 26, 2013 21:53
-
-
Save veltman/8139181 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cities = ["Albuquerque","Austin","Baltimore","Boston","Charlotte","Chicago","Columbus","Dallas","Denver","Detroit","El Paso","Fairbanks","Fort Worth","Fresno","Houston","Indianapolis","Jacksonville","Kansas City","Las Vegas","Long Beach","Los Angeles","Louisville","Memphis","Mesa","Milwaukee","Nashville","New York","Oklahoma City","Philadelphia","Phoenix","Portland","Sacramento","San Antonio","San Diego","San Francisco","San Jose","Seattle","Tucson","Washington"]; | |
var output = {}; | |
var d3 = require('d3'); | |
var http = require('http'); | |
var fs = require('fs'); | |
cities.forEach(function(city){ | |
fs.readFile("data/"+city.replace(" ","_")+".json","utf8",function(err,data) { | |
var sorted = d3.entries(JSON.parse(data)).map(function(d){ | |
d.year = parseInt(d.key); | |
delete d.key; | |
if (!d.value.history.dailysummary[0].meantempi.match(/^-?[0-9]{1,2}$/)) { | |
//If it's a weird temperature value, leave it empty | |
var temp = ""; | |
} else { | |
var temp = parseInt(d.value.history.dailysummary[0].meantempi); | |
} | |
d.value = { | |
"temp": temp, | |
"fall": d.value.history.dailysummary[0].snowfalli.length ? parseFloat(d.value.history.dailysummary[0].snowfalli) : 0, | |
"depth": d.value.history.dailysummary[0].snowdepthi.length ? parseFloat(d.value.history.dailysummary[0].snowdepthi) : 0, | |
"any": (d.value.history.observations.filter(function(f){ | |
return f.icon == "snow"; | |
}).length) ? 1 : 0 | |
}; | |
return d; | |
}).sort(function(a,b){ | |
//Sort array by year ascending | |
return a.year - b.year; | |
}); | |
output[city] = sorted; | |
//Output is full, print it out | |
if (d3.keys(output).length == cities.length) console.log(JSON.stringify(d3.entries(output).map(function(d){ | |
return { | |
"city": d.key, | |
"data": d.value | |
}; | |
}))); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment