Nasa has just opened up their Exoplanet Archive for the world. The archive contains exoplanet sightings by the Kepler observatory.
Click here for more information The Exoplanet Archive data are accessed through the Exoplanet Archive’s application programming interface (API). Click here for more information Now for the fun stuff! Windows PowerShell Version 3.0 includes a CmdLet called Invoke-RestMethod. We can use the CmdLet to get data from Nasa’s API. Before we download the data we need a query. All queries against the API must begin with the following base URL.PS > $url = "http://exoplanetarchive.ipac.caltech.edu/cgi-bin/nstedAPI/nph-nstedAPI?"Next we specify which data table to query. In the example below query the exoplanets table.
PS > $url = $url + "table=exoplanets"We also add the names of the columns we want to be returned. In the example below we select the pl_hostname, and ra columns.
PS > $url = $url + "&select=pl_hostname,ra"Here’s what the complete URL looks like.
PS > $url http://exoplanetarchive.ipac.caltech.edu/cgi-bin/nstedAPI/nph-nstedAPI?table=exoplanets&select=pl_hostname,raNow we can use Invoke-Restmethod to download the data from Nasa.
PS > Invoke-RestMethod -Uri $url pl_hostname,ra HD 173416,280.900452 HD 175167,285.003479 HD 175541,283.920349 HD 176051,284.256714 HD 177830,286.336548 HD 177830,286.336548The example above displays the csv data as lines of text. Converting the text to objects is a simple task in PowerShell v 3.0 thanks to the ConvertFrom-Csv CmdLet.
PS > Invoke-RestMethod -Uri $url | ConvertFrom-Csv pl_hostname ra ----------- -- HD 173416 280.900452 HD 175167 285.003479 HD 175541 283.920349 HD 176051 284.256714 HD 177830 286.336548 HD 177830 286.336548Let’s wrap up the code in a reusable function instead. Click here to download the Get-ExoPlanet function.
function Get-ExoPlanet {
<#
.SYNOPSIS
Gets data from the Exoplanet Archive.
.DESCRIPITON
Uses the Exoplanet Archive Application Programming Interface (API) to retrieve information regarding Exoplanets.
.EXAMPLE
Get-ExoPlanet
#>
[CmdletBinding(HelpUri = 'http://exoplanetarchive.ipac.caltech.edu/docs/program_interfaces.html')]
param()
process {
# Check PowerShell Version.
if($Host.Version.Major -ne 3) {
Write-Warning -Message "This function requires PowerShell Version 3.0"
break
}
# Data table to access (exoplanets or koi or tce)
$table = "Exoplanets"
# Only retrieve Common Columns
[string[]]$Column = @("pl_hostname", "pl_letter", "pl_discmethod", "pl_orbper", "pl_orbsmax", "pl_orbeccen", "pl_massj", "pl_msinij", "pl_radj", "pl_dens", "pl_orbincl", "pl_ttvflag", "ra", "dec", "st_dist", "st_vj", "st_teff", "st_mass", "st_rad", "hd_name", "hip_name")
# Uri.
$uri = "http://exoplanetarchive.ipac.caltech.edu/cgi-bin/nstedAPI/nph-nstedAPI?table=" + $table + "&select=" + [string]::Join(",",$Column)
# Get Data
$result = Invoke-RestMethod -Uri $uri
# Convert from Csv to PowerShell Object.
$result | ConvertFrom-Csv | ForEach-Object {
# Return as a Custom PowerShell Object
New-Object PSObject -Property @{
PlanetHostStarName = [string]$($_.pl_hostname);
PlanetLetter = [string]$($_.pl_letter);
DiscoveryMethod = [string]$($_.pl_discmethod);
OrbitalPeriod = [double]$($_.pl_orbper);
SemiMajorAxis = [double]$($_.pl_orbsmax);
Eccentricity = [double]$($_.pl_orbeccen);
PlanetMass = [double]$($_.pl_massj);
PlanetMsin = [double]$($_.pl_msinij);
PlanetRadius = [double]$($_.pl_radj);
PlanetDensity = [double]$($_.pl_dens);
Inclination = [double]$($_.pl_orbincl);
TTVFlag = [double]$($_.pl_ttvflag);
RightAscension = [double]$($_.ra);
Declination = [double]$($_.dec);
Distance = [double]$($_.st_dist);
VJohnsonSystemMagnitude = [double]$($_.st_vj);
EffectiveStellarTemperature = [double]$($_.st_teff);
StellarMass = [double]$($_.st_mass);
StellarRadius = [double]$($_.st_rad);
HDName = [string]$($_.hd_name);
HIPName = [string]$($_.hip_name)
}
}
}
}
Now we can simply type Get-ExoPlanet to display the data from NASA’s Alien Planet Archive.
PS > $exoPlanets = Get-ExoPlanet PS > $exoPlanets PlanetLetter : b PlanetDensity : 0 StellarMass : 0 VJohnsonSystemMagnitude : 6.037 HDName : HD 173416 OrbitalPeriod : 323.6 PlanetRadius : 0 Declination : 36.556606 SemiMajorAxis : 1.16 StellarRadius : 0 Inclination : 0 Eccentricity : 0.21 RightAscension : 280.900452 Distance : 134.95 TTVFlag : 0 PlanetMsin : 2.7 DiscoveryMethod : Radial Velocity PlanetMass : 0 EffectiveStellarTemperature : 0 HIPName : HIP 91852 PlanetHostStarName : HD 173416The function displays the “default” columns from the Exoplanet Archive. Working with the data as powershell objects allows us to combine the objects with other PowerShell CmdLets. If we want to group the Exoplanets by Planet Letter we simply type:
PS > $exoPlanets | Group-Object PlanetLetter
Count Name Group
----- ---- -----
638 b {@{PlanetLetter=b;
124 c {@{PlanetLetter=c;
33 d {@{PlanetLetter=d;
12 e {@{PlanetLetter=e;
6 f {@{PlanetLetter=f;
3 g {@{PlanetLetter=g;
1 h {@{PlanetLetter=h;
Sorting the objects is also a simple task. Let’s say we want to display the top 10 planets with the largest Planet Mass (Jupiter).
PS > $exoPlanets | Sort-Object PlanetMass -Descending | Select-Object PlanetHostStarName , PlanetMass -First 10 PlanetHostStarName PlanetMass ------------------ ---------- 2MASS J22062280-2047058A 31.42 2MASS J07464256+2000321A 31.42 Kepler-47 28 KELT-1 27.23 nu Oph 27 nu Oph 24 HIP 78530 23.04 HD 180314 22 HN Peg 21.9987 CoRoT-3 21.66If we want to display the Data in a Gridview we can pipe the objects to the Out-GridView CmdLet.
PS > $exoPlanets | Out-GridView
And if we want to Export the data to a Csv file we type:
PS > $exoPlanets | Export-Csv C:\ExoPlanets.csvClick here to download the Get-ExoPlanet function.
[?]
Pingback: Episode 214 – David Davis from TrainSignal on technical training and certifications « PowerScripting Podcast