INTERACTIVE ONLINE COURSE

Python 3

FOR BEGINNERS


"The first Python course that simply amazed me. Very well explained and easy to understand." (Alexandru Cosmin)

"The best Python course in Romania." (Iulian Geană)


ALL REVIEWS
NEW PROJECT
PAG. 1 / 1
Mars Weather Report using Python
Home >>> Online Lessons, Python 3


Introduction

Mars is the fourth planet from the Sun in our Solar System. It is a rocky planet that is similar in many ways to Earth but is much colder and drier. The red color of the Martian surface is due to iron oxide (rust) on the surface. Mars has two small moons, Phobos and Deimos, which are believed to be captured asteroids.

The atmosphere of Mars is very thin and is composed mainly 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.

In recent years, there has been increased interest in Mars as 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. Additionally, there are feasible projects 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 the time of day. The atmosphere of Mars is also much thinner than Earth's atmosphere, which means there isn't much weather as we know it on Earth. There are no clouds, and there is very little water vapor in the air, so there is no rain.

However, there are winds on Mars that can reach speeds of up to 100 km/h. These winds can create dust storms that can cover the entire planet and last for months. There are also smaller storms called "dust devils," which are caused by the heating of the surface by the Sun. These are similar to tornadoes on Earth but are made up of dust and sand instead of air.

National Geographic | Mega Dust Storms
In general, the weather on Mars is very harsh and hostile to life as we know it. However, scientists continue to study the planet and its weather characteristics to learn more about how it has changed over time and to see if it is possible for life to exist on the planet or how easily it could support life.

Real-time Weather Data on Mars

NASA's InSight lander continuously measures the weather (temperature, wind, pressure) at the surface of Mars in the Elysium Planitia region, a flat and smooth plain near the equator:



The API provided by NASA offers summary data for each available sol from the last seven sols (Martian days). As more data from a particular sol is downloaded from the spacecraft (sometimes a few days later), these values are recalculated and, consequently, may change as more data is received back on Earth. Additionally, it should be noted that wind data and other sensor data may not exist for certain time periods.

The provided data is given in the form of a streaming object JSON. Let's take a look at the Python code below:

import json
import requests
url = "https://mars.nasa.gov/rss/api/?feed=weather&category=mars2020&feedtype=json"
data = requests.get(url).json()
print(data) #display raw data


Important. If you don't have the json and requests packages installed, you will surely get errors. Read more details [about PIP].

It's real magic! We use just 4 lines of code and get real data from Mars via NASA:



As you can see, the variable "data" holds a Python dictionary (class dict) with all the data received from the NASA API using a JSON request. Using the key "sols", we can access each day in the included list as follows:

print(data['sols'][0]) #day 1 from the dataset
print(data['sols'][1]) #day 2 from the dataset
...
print(data['sols'][6]) #day 7 from the dataset


We will get the corresponding data for any day we want from the available time range directly from the planet Mars:



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



That's it! You can now import real data from Mars into your Python projects!

Additionally, the numpy and matplotlib modules offer extra options for interactive data visualizations in Python and much more! They are all free and very interesting for anyone learning the Python programming language!

What do we observe? The simplicity of working in Python! We quickly imported two modules to make requests via JSON. We specified the link to the API and then retrieved the results easily and quickly!
This concludes the section.
 home   list  CONTENTS   perm_identity   arrow_upward