Public
Edited
Nov 10, 2022
Importers
1 star
Insert cell
Insert cell
earthquakeData = {
// Set the first year
const startYear = 2016;
// Set the last year (inclusive)
const endYear = 2021;
// Create a months array that will hold each month's worth of data.
const months = []
// Loop over the years, as JS has no builtin range function.
for (let year = startYear; year <= endYear; year += 1) {
// Now loop over the months. Note that in JS Date objects,
// month is zero-indexed (January is 0 and December is 11)
for (let jsMonth = 0; jsMonth < 12; jsMonth += 1) {
// Start the month at midnight (local time) on the first of the month.
const monthStart = new Date(year, jsMonth, 1, 0, 0, 0).toISOString();
// End the month at 23:59:59 on the 0th of the following month
// (which is the last day of the previous month)
// NOTE: days are *not* zero-indexed, unlike months. Confusing!
const monthEnd = new Date(year, jsMonth + 1, 0, 23, 59, 59).toISOString();
// Combine the two ISO strings into the query URL.
const queryUrl = `https://earthquake.usgs.gov/fdsnws/event/1/query.csv?starttime=${monthStart}&endtime=${monthEnd}&minmagnitude=2.0&orderby=time`;
// Fetch the data, then parse the csv with d3. Once the fetch
// promise resolves, iterate over the data and prepare it.
//
// In the meantime, push the promise (and, hence, its result) into the months array.
months.push(d3.csv(queryUrl).then(response => response.map(event => {
// Destructure each event to capture only the properties ("columns") we want.
const { id, latitude, longitude, mag, place, time, type, depth } = event;
// Return the destructured event into the array for the month in question.
return ({
id,
latitude: parseFloat(latitude, 10),
longitude: parseFloat(longitude, 10),
mag: parseFloat(mag, 10),
place,
time: new Date(time),
type,
depth: parseFloat(depth, 10)
})
}))
)
}
}
// Resolve every promise in the months array and then flatten the array of arrays.
return Promise.all(months).then( d => d.flat())
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more