WEATHER ON MARS
|
PYTHON
|
How can we get real weather data from Mars using Python language?
#codingwithpython #mars #realdata #weather #nasaapi #json

Mars is the fourth planet from the Sun in the Solar System. It is a rocky planet that is similar in many ways to Earth, but it is much colder and dryer. The reddish color of the Martian surface is due to iron oxide (rust) on the surface. Mars has two small moons, Phobos and Deimos, which are thought to be captured asteroids.
The atmosphere of Mars is very thin and is mostly made up of carbon dioxide. There is also a small amount of nitrogen and argon in the atmosphere. Water exists on Mars, but it is mostly in the form of ice, and there is very little liquid water on the surface.
There has been a lot of interest in Mars in recent years because it is considered one of the most likely places in the Solar System to find evidence of past or present life. NASA and other space agencies have sent several spacecraft to Mars to study the planet and search for signs of life. In addition, there are plans to send humans to Mars in the future.
Weather on Mars
The weather on Mars is quite different from the weather on Earth. Mars is farther from the Sun than Earth, so it is much colder. The average temperature on Mars is about -80°F (-62°C). However, the temperature can vary greatly depending on where you are on the planet and what time of day it is.
The atmosphere of Mars is also much thinner than the Earth's atmosphere, which means that there is not much weather as we think of it on Earth. There are no clouds, and there is very little water vapor in the air, so there is no rain.
There are, however, winds on Mars that can reach speeds of up to 60mph (100kmph). These winds can create dust storms that can cover the entire planet and last for months. There are also smaller storms that are called "dust devils", which are caused by the heating of the surface by the Sun. These dust devils are similar to tornadoes on Earth, but they are made up of dust and sand instead of air.
Real weather data from Mars
NASA’s InSight Mars lander takes continuous weather measurements (temperature, wind, pressure) on the surface of Mars at Elysium Planitia, a flat, smooth plain near Mars’ equator:

(more info at https://mars.nasa.gov/insight/weather/)
The API provides per-Sol summary data for each of the last seven available Sols (Martian Days). As more data from a particular Sol are downlinked from the spacecraft (sometimes several days later), these values are recalculated, and consequently may change as more data are received on Earth. Additionally, please note that wind and other sensor data may not exist for certain date ranges.
The summary data are provided as an object in a JSON stream. Let's look at the Python code bellow:
import json
import requests
url = "https://mars.nasa.gov/rss/api/?feed=weather&category=mars2020&feedtype=json"
data = requests.get(url).json()
print(data) #showing the raw data
It's real magic! We just use 4 lines of code and get real data from Mars via NASA:

As you can see, data variable retains a Python dictionary (dict class) with all the data received from the NASA's API using a JSON request. Using the 'sols' key, we can access every day from the included list like this:
print(data['sols'][0]) #day 1 from the data set
...
print(data['sols'][6]) #day 7 from the data set
We will get the corresponding data for any day we want from the available time span:

Note that data['sols'][6] contains a dictionary that can be accessed easily as bellow:

That's it! You can import real data from Mars into your Python projects! Also, numpy and matplotlib modules offer extra options for interactive data visualizations in Python and so on! It's all free and very cool for any student that learns Python language!