In this little tutorial we will be exploring the Space and discovering what planets can be fit for Life. A whole lot of factors are considered when talking about habitable planets, metrics that are measured relative to earth.
What do we need?
Well, As long as you have Node Js installed on your system, then you are good to go. You can head over to their official website if you don't.
Okay, we would also need a file and that is the file about Keplers. Apparently, NASA has been conducting research about habitable planets and based on certain attributes as earlier mentioned and they have this information about planets stored on their website.
Head over to Exo-Planet Archive
Then you will find, under 'Kepler', Koi Table. Click on it and it will initialize the tables and you can download it from there
So, we have our Kepler Data.csv which we are parse just soon.
STEP(1)
Set up your workspace and create an Index.js in your folder(Planets)
STEP(2)
We are going to install an Npm package that is going to help us read our CSV file.
Run npm i csv-parse
in your terminal
STEP (3)
In your index.js file, require these two modules
const {parse} = require("csv-parse")
const fs = require("fs")
Basically, we will use 'fs' to createReadStream, streams are used when large data sets are involved. Then we use csv parse to create a writable data from the data stream.
STEP(4)
So we can call our fs.createReadStream at this point and pass the name of the csv file as the path, This file should be located in the same folder as the index.js. We then can listen to the data as it is being streamed line by line with the 'on' function. At this point, we want to create an array of planets and push the data in our call back function.
const {parse} = require("csv-parse")
const fs = require("fs")
const planets = [];
fs.createReadStream("kepler_data.csv")
.on("data", (data)=> {
planets.push(data);
})
(STEP 5)
Things does not really end here, as far as we are concerned, we are not returning any data. We can call an 'on' end function to console log our array and see how it looks like
const {parse} = require("csv-parse")
const fs = require("fs") const planets = [];
fs.createReadStream("kepler_data.csv")
.on("data", (data)=> {
planets.push(data);
})
.on("end", ()=> {
console.log(planets)
})
Our result looks like this and it does not really make sense. Not exactly what we want.
(STEP 6)
We are going to utilize our csv parse and make the results more presentable. We can achieve this by piping the readable stream to a writable stream - our csv parse. Right before the 'On' data, we could do that. We should take note that comments are written as # in our CSV file and we want to exclude that. We also want to return our data as columns
const {parse} = require("csv-parse")
const fs = require("fs")
const planets = [];
fs.createReadStream("kepler_data.csv")
.pipe(parse({
comment: '#',
columns: true
}))
.on("data", (data)=> {
planets.push(data);
})
.on("end", ()=> {
console.log(planets)
})
Our results start to make more sense now. If you compare with the csv files, we can see that the object fields match the column names
(STEP 7)
We can now start to sort by the attributes we mentioned earlier. THIS ARTICLE by Paul Glistercontains all the information we need about how we can determine how habitable a planet is. We will focus on Insolation Flux and the Planetary Radius [relative to earth]. We compare to earth because it is perfect for us. Leslie Rodgers postulated in this article that planets with radii not greater than 1.6 times that of the earth are habitable. An Insolation flux of > 0.36 and < than 1.11 is also perfect as contained in the article by Paul.
We proceed to create a function called isHabitable and pass in these metrics so it looks like this. Also take note that we are also working with only confirmed planets. The exoplanet Archive Disposition has to be confirmed[koi disposition]
function isHabitablePlanet(planet) {
return planet["koi_disposition"] === 'CONFIRMED' &&
planet['koi_insol'] > 0.36 && planet['koi_insol'] < 1.11 &&
planet['koi_prad'] <1.6;
}
Then we can pass each of the planets 'on' data into this function in order to check validate it.
Our program now looks like this
const {parse} = require("csv-parse")
const fs = require("fs")
const planets = [];
function isHabitablePlanet(planet) {
return planet["koi_disposition"] === 'CONFIRMED' &&
planet['koi_insol'] > 0.36 && planet['koi_insol'] < 1.11 &&
planet['koi_prad'] <1.6;
}
fs.createReadStream("kepler_data.csv")
.pipe(parse({
comment: '#',
columns: true
}))
.on("data", (data)=> {
if (isHabitablePlanet(data)){
planets.push(data);
}
})
.on("end", ()=> {
console.log(planets)
})
Our result is significantly smaller now. We can check for the name field and the length to make our data more meaningful to us. So instead of just console log the planets. We are going to map through it and check the name field. Then after the map, we call the length
The on 'end' function is re written as this
.on("end", ()=> {
console.log(planets.map((planet)=> {
return planet['kepler_name']
}))
console.log(`${planets.length} habitable planets were found `)
})
And our results look cleaner!
We can confirm our results by looking up the names of some of these planets in the Exo-planet Archive
Cover Photo Source
Disclaimer: All other unreferenced images are screenshots from me, links to articles and authors are contained in the write-up and this work was originally done by Zero to Mastery Academy in their NodeJs Course
Follow for more♥️