In this lesson, you'll use the Electricity Maps API to explore carbon intensity on the grid. You can use this API to get information on where electricity in a specific area comes from, and how much carbon was admitted to produce it. All right. Let's get to coding. So, in order to use the Electricity Maps API, you will need an API key. And we've set that up for you already in this environment. So you can just import the load env function and execute the cell. But let me quickly show you how you would get your own API key if you wanted to run this outside of this online classroom. So this is the Electricity Maps website, and you can just click over here and to get API key. And you can get access to the free personal tier to explore this API on your own. Back in the notebook, the first thing we're going to try out is the electricity maps live carbon intensity endpoint. This endpoint will retrieve the last known carbon intensity in grams of CO2 equivalent per kilowatt hours. So in other words, it's going to tell us how much CO2 is emitted per kilowatt hours of electricity consumed in a particular area. And, as a quick reminder, when we say grams of CO2 equivalent, this is a measure used to compare the emissions from various greenhouse gases on the basis of their global warming potential. So to use this endpoint, you're going to need to specify the location of where you want to retrieve this information for. You can specify location using lat long coordinates. Or you can query with one of electricity maps predefined zone identifiers. We're going to use the lat long coordinates. So you can actually get lat long coordinates for any location. If you open up Google Maps. So let me show you how to do that. So here we are in Google Maps and I'm going to search for a location. We're going to look for the Santa Monica Pier in Los Angeles which is right over here. And if you right click, you'll see that the coordinates pop up right here. And you can click on them and it'll copy them for you. So you can even do this for your own location. And in addition to coordinates, you can even check out some of the cool latest pictures there that have been uploaded. All right, so now that we have these coordinates, we can hop back over it to the notebook. And what we're going to do is we're going to create a dictionary with these coordinates. So we'll say coordinates equals and then we'll make one key for lat. And I'm going to paste in this number. And then we'll make another key for one. And we'll paste in the next part of this. And now that we have this dictionary we're going to make a request to the endpoint using the Python requests library. We'll start out by defining the URL. And this is just something that I got from the electricity maps documentation. So I'm going to paste in what this URL is right now. And you can see here that it's electricity maps. And then this is the carbon intensity endpoint. And later on in this lesson we'll use a different endpoint. And you'll see that this this will change. But for right now we're going to use carbon intensity latest. And the way you specify these coordinates these lat long coordinates is through query parameters. So we'll take question mark and then we'll type lat We'll extract out the value from our dictionary. And then we'll do the same thing for longitude. So we'll type another 'and' sign time type another end symbol type lon equals and then coordinates and extract out that value. And we're doing all of this with f-string formatting as you can see over here. So let's go ahead and print this out. To make this a little bit clearer and to print out the endpoint. We'll add a little bit of text here just to make it a little bit easier to see. And we'll print this URL. Let's execute this. So you can see here This is the endpoint that we want to hit. And it has our coordinates in here as a query parameters. So now we are ready to make our Get Request. So I'm just going to quickly import, the request library from Python as well as helper and that's because we need to make sure we pass in our API key that we defined earlier in order to make the request. So let's say request equals and we'll make a get request. And in this get request we're going to pass that URL we just defined. And then we need to pass in our API key to the headers value as off token. So the way we do that it looks something like this. We say headers equals auth token. And then the value there is going to be the API key which will load from this function here. And our helper file. So we've made our request to the endpoint. So let's print out the returned object. We can format this nicely with json. So I'll import json and then we'll say. json dot loads. And we'll print out the results here. And this is just a way of viewing the content from this request in a nice dictionary. So at the point of recording this, you can see that the carbon intensity on the grid in Los Angeles is 242g of CO2 equivalent per kilowatt hours. So what that means is that if you are in LA, you're emitting 242g of CO2 for each kilowatt hour of electricity consumed. Now note this is real-time data, so you're probably going to see a different number than I do when you actually run this query. And that's okay. All this is live real-time data. And the numbers are going to change throughout the day because the carbon intensity and mix of energy sources is just going to fluctuate throughout the day. In addition to just seeing the carbon intensity, what if we wanted to see what energy sources actually are, contribute these carbon emissions. So this time, instead of just receiving this one single number, we're going to use a different endpoint and see what the different mix of energy sources is. So we'll use the live power breakdown endpoint. And this retrieves the last known data about the origin of electricity in an area. So like before you first need to define the URL. And this URL is going to look pretty similar. I'll paste it in right now we'll use f-string formatting to add coordinates. So this one is called a power breakdowns. We scroll up a little bit over here. You can see that earlier we were using the carbon intensity endpoint. And now we are carbon intensity endpoint. And now we're using a power breakdown. So again like before we need to pass in our coordinates as query parameters. So I'm going to go ahead and paste them in this time. And this is just saying get me the value for the key lat and my coordinate dictionary. And then get me the value for the key lon and my coordinate dictionary. And then we can go ahead and print out the URL just to take a look. And it looks something like this. So this is the power breakdown with our coordinates as query parameters. Now that we've done that we are ready to make the request. So again we'll say request equals. And we'll use this Python requests library to make a Get request. And we pass in the URL. And then we need to pass in our header which has our API key. And we can paste that in right now and we'll make our request. So let's go ahead and print out the content again. So we'll call this power breakdown and we'll use json loads. And we'll get the content from this request. And then we can print this out. And there's a lot of information that's come back. This is a big dictionary with a whole bunch of information. So let's look at some of the most important keys here. The first key we're going to look at is called the renewable percentage. So let's print this out right now. And that's 67%. So this refers to the percent of power consumption coming from renewable energy. So, at this point in time Los Angeles has 67% of its energy mix coming from renewable energy. And that's actually pretty good. We can look at another key here, which is called the fossil-free percentage. And if we print that out we'll see that is actually also 67%. So this refers to the power consumption coming from renewables and nuclear. So what this means is that there's actually no energy coming from nuclear right now. If there was you would see this number 67 be slightly higher because it would be whatever the renewable percentage was, plus whatever was coming from something like nuclear, which is considered to be carbon-free but not renewable. So again, these are all live numbers. So you'll probably see something completely different when you're running these cells than what I'm seeing right now. Lastly, we can look at this power consumption breakdown. And this will show us here the breakdown of energy sources and this particular location. This is all in megawatts. So we can do a little bit of math and turn these numbers into percentages just to make it a little bit easier to understand. So, first do this math we're going to import numpy. The next thing I'm going to do is I'm going to grab one more key, from this dictionary, which is power consumption total. So if we scroll up a little bit you can see power consumption total is right here. So let's grab that and call that total consumption. So we can print that out. We'll say total consumption. And that's 1962 MW. So that's the total consumption of electricity right now in Los Angeles. So now we'll do a little bit of dictionary comprehension. And what we're going to do is loop over the keys in this dictionary here. And we're going to divide each one of these numbers by the total consumption. And we'll multiply by 100. And that will give us a nice breakdown in percentages for each of these different energy sources. So to do that the first thing I'm going to do is say for our keys here, we're going to take the value and we're going to divide by total consumption. And then we're going to multiply it by 100. And again that's taking one of these values here divided by the total. And then multiplying by 100. And we're going to round that off just to again make it a little bit easier to understand. And we'll do this for each key and value in our power breakdown dictionary. And we need to do this for the items. And then we're going to print this out. And you can see here that now we have the numbers nicely reformatted for us in terms of percentages. So actually a lot of energy right now is coming from solar on the the LA grid right now, which is pretty cool. Just a quick note, if you do see all of these numbers as being zero, which can happen sometimes if you look at the results, you might see everything is zero. That can happen if the energy provider for some reason, just isn't providing data. This again is live data, it's real-time data. So sometimes the data sources fail for some reason and you don't get anything back. Don't worry about it. If you do see that on the off chance you do, you can just try out another location or try this at a different time in the numbers. And you should actually see, numbers other than zero coming through. So now it's your turn. Go find the coordinates for your location and query these endpoints to figure out the carbon intensity and power breakdown. So, for example, let's try out a Landmark in Taipei, Taiwan. This is the Chiang Kai-shek Memorial Hall. So let's go check that out. And we can even see some of the latest of what's going on here. In case you're curious what this memorial looks like. So now that we have that, we can grab the coordinates and copy those to the clipboard back in the notebook. There are some helper functions. So you can import all these helper functions to get all of this information. So you don't have to run through all of the cells all over again. So there's a helper function here called power stats which is going to return the carbon intensity as well as an abridged version of all of the breakdown of carbon and non-carbon energy sources. If we import this helper function we can call power stats. We'll get back the intensity and breakdown for the coordinates here which are the coordinates of that landmark in Taiwan. So let's run that cell and then we can see what the intensity is. The intensity is 579. So it's higher right now than it was when we looked at LA. And then we can type the breakdown and we'll get a dictionary here which shows you the different energy sources powering the grid there. So we've got some coal, quite a lot of coal, natural gas. It's also got a lot of solar, a little bit of biomass. And you can check out these numbers. So note that electricity maps doesn't have coverage for the whole world. So if you do happen to in a place where they don't supply any data, you can try out a different location or someplace nearby or just a landmark or area that you're really curious about. So with that, I will see you in the next lesson where we will put this information in action and actually train a machine learning model in a location that has a lot of carbon free energy available. I'll see you there.