Exploring Habitable Planets with Node Js

in #hive-1693212 years ago

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
IMG_20220930_213541.jpg

IMG_20220930_213611.jpg

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)
})

IMG_20220930_213418.jpg

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)
})


IMG_20220930_213358.jpg

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!

IMG_20220930_213502.jpg

8 potentially habitable planets!.

We can confirm our results by looking up the names of some of these planets in the Exo-planet Archive

IMG_20220930_220732.jpg

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♥️

Sort:  

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support. 
 

Nice job!! Programing is so useful to explore science data
!1UP


Thank you


The rewards earned on this comment will go directly to the people sharing the post on Twitter as long as they are registered with @poshtoken. Sign up at https://hiveposh.com.

1UP-PIZZA.png

You have received a 1UP from @gwajnberg!

The @oneup-cartel will soon upvote you with:
@stem-curator, @vyb-curator, @pob-curator
And they will bring !PIZZA 🍕.

Learn more about our delegation service to earn daily rewards. Join the Cartel on Discord.