Hello, everybody.
Welcome back to another code tutorial.
This week, we're gonna learn how to filter for clouds,
so how to make cloud-free imagery,
and also how to make false color composites.
So last week, we learned how to make true color composites
and how to make an ROI.
So again, here's my ROI.
Let's use the same ROI that we used last week,
and you can use the same script that you started last week
or you can make a new script.
I'm just gonna continue on in the script from last week.
So in order to make this a new script for Module 3,
I'm gonna say save as and save it as Module 3.
So that M2 script still exists.
I'm just creating a new one called M3 that I'll work in.
I also wanna do a little bit of housekeeping
in terms of organizing my script.
So first off, it's important to label your,
the top of your script with some information.
So for example, I might say created by my name,
created and the date,
oh, 2024,
and then I'm also gonna do some block organization.
So I'm creating these blocks of comments,
and I'm gonna label last week's code as TRUE COLOR.
So this is just like a way
to visually break apart and organize your code,
and then for the new code this week,
I'm gonna label that as CLOUDS and FALSE COLOR.
So now when I'm just, like, quickly looking at my code,
I can see the different sections really easily.
I also am going to, well, I guess we can run the code
from last week to remind ourselves what we did.
Run.
It's taking a minute to load,
but last week, remember we used the Landsat 8 satellite
and used the red, green and blue bands
in the red, green and blue channels
and created this true color image.
I'm gonna start with some new code today.
So I'm actually gonna comment out this code
that we did last time,
and I'm gonna import a new satellite to work with.
So last week, we used Landsat 8 and the top of atmosphere.
We learned in the lecture this week the difference between
top of atmosphere and surface reflectance.
So let's grab a surface reflectance dataset this week.
I also wanna use a different Landsat sensor
to just show the differences between
Landsat 8 and another one.
So let's use Landsat 5.
Again, we don't want raw scenes
'cause those are typically lowest quality,
and then the top of atmosphere,
as we talked about in lecture, can have some haze effects.
So what we're gonna use is this Tier 1,
and we can click on it and see this represents
the atmospherically corrected surface reflectance values.
So great. That's what we want.
Some other important things to look at when you're looking
at datasets is the date range of your dataset.
So Landsat 5 goes from 1984 to 2012.
That's good to know.
We can also look at some other qualities of this dataset,
including the band names.
So the band names are named a little bit different
in this dataset than they were in the last.
So because these represent surface reflectance values,
the band names are SR_ and then the band number.
I also wanna point out that if you remember last week,
Landsat 8's red, green and blue were B4, B3 and B2,
but for Landsat 5, because they don't have the coastal blue
that in Landsat 8 is labeled B1,
the B3 band is red, B2 is green,
and B1 is blue.
So that's important to note.
Also, you might notice that the min and max values
are different in this satellite than they were in Landsat 8.
So that's important to note.
I also wanna go to IMAGE PROPERTIES really quick
and show you this property name called CLOUD_COVER
'cause we'll be using it today.
So the name is CLOUD_COVER,
and then the description is that it represents
the percent cloud cover in each Landsat tile
between 0 and 100.
Great. So I'm gonna import this image collection.
I'm gonna name it L5,
and now we can work with it down here.
So first things first,
I'm gonna create a variable called L5_notcloudy,
and so within this variable,
we're gonna summarize that entire Landsat 5 image collection
into just one single image that we will visualize.
So we're gonna start off
with that Landsat 5 image collection.
First, I'm gonna filter it again to the bounds of my ROI.
So my ROI is called FortCollins.
I'm gonna filter it by date as well.
This is not necessary this week,
but I'm just doing it because it gives the computer
less data to sift through, and so it'll just process faster.
So I'm using this filterDate function,
and within this filterDate function,
you can put just years.
You could put in entire date ranges,
so including days, months and years.
For now, I'm just gonna put the year.
I'm gonna get data from 2000 to 2002.
Again, this is just kind of random
because I don't want to work with so much data,
and then finally, now we're going to filter by clouds,
and the syntax for filtering clouds is a little clunky.
First we're gonna tell it that we're filtering something,
and then we're gonna use this Filter.lte function.
This represents less than or equal to.
If we wanna learn more about this, again,
we can go to the docs and search
for ee.Filter.lte,
and click on it, and it says,
"Filter on metadata less than or equal to a given value."
So there's two arguments that are required,
the name of the property that you're filtering
and then the value.
So in our case,
the name of the property is CLOUD_COVER,
and then we can add a number to represent the cloud cover
that we want less than or equal to
to be included in our final image collection.
So I'm gonna put 20.
I also wanna point out that there's ee.Filter.gte.
So same concept, it'll just do all the numbers
above the number that you list.
There's also greater than.
So there's a lot of different options here
that you could explore, and then again,
if I left this variable as is right now,
it would still be an image collection.
So it might contain a couple of images
that are within my filter bounds, within my filter dates,
and that have cloud cover that's less than 20.
So I need to summarize that image collection
into just one image somehow.
So I'm gonna put median.
So I'll take a median of all of those images.
Great, so now we can add
that notcloudy Landsat 5 image.
We can add it to our map.
So I'm gonna first put the name
of my image variable.
Then I'm gonna add some visualization parameters.
So the important one to put here is the band names.
So SR_B3 is red.
SR_B2 is green.
SR_B1 is blue, and then this is a list,
so I have to put it in brackets.
Let's also put the min and max values.
So if you remember, min they said was 1.
Max was around 60000.
We can edit that in a second,
and then let's add a name to our map, so I'm gonna say L5,
and then this is a true color image.
So let's run that.
Oh, okay.
So what happened here is I forgot to clip my image
to exactly to my ROI.
So what it's giving me here is the entire Landsat tile,
and actually it's given me two Landsat tiles
'cause it seems like my ROI,
there's two tiles that both sit on top of my ROI.
So after this variable, I need to put .clip(FortCollins),
and then it'll clip it just to my little ROI.
Okay, so one of the first things I'm seeing here
is that this image is pretty dark.
So I'm gonna go to the Inspector tool
and click around a little bit.
I can see the values here.
So you can click on some low reflectance value pixels
and see what those are.
You can click on some high reflectance value pixels,
and this is just giving me an idea of what the, like,
max, min and max might be.
I could also manually look in here
and say stretch the image by 100%,
and it'll give me the range of my image.
Okay, so that's good to know.
I'm gonna change my min to, let's say,
5000 and my max to 20000, and I'm gonna run it again,
and yep, this is a bit clearer.
I can see more in the landscape now, which is good.
Okay, so again, we filtered for cloud cover
to get less than 20% clouds.
What if we did less than
1% clouds?
So it kinda looks the same.
Colorado's not the cloudiest place ever,
but just for fun, we could say,
"What if I want an image that has greater than 30% clouds?"
I'm gonna run that, and let's see.
Yeah, so just to show you that the code works,
if I put greater than 30% cloud cover,
it'll give me a pretty cloudy photo.
So let's go back less than or equal to 20% cloud cover,
and now I want to visualize
this image but with a false color composite,
not a true color composite.
So I'm just gonna copy-paste this code,
and then I'm gonna edit it down here.
So this is my false color.
This was my true color.
So in order to make this false color,
I need to change the band names.
So I'm gonna make this an infrared false color,
infrared,
and to do that,
I need to assign the red channel to the infrared,
the green channel to the red band,
and the blue channel to the green band.
I'm gonna make sure I label my map
so I know what it's visualizing, and now I can run,
and the image looks quite different.
So the areas that were green before are red
in this image, and I'll let you think about why that is.
So in the assignment for this week for Yellowdig,
I asked you to create a infrared false color
and a short wave false color image within your ROI.
So what you'll do next is take this code
and then change the bands again
to represent a short wave false color image.
So don't forget to save your code,
and then again, when you're sharing your code,
click Get Link and then copy-paste what's here.
All right, thanks, and see you next week.