How to Plot netCDF data onto a Map using Python (with Matplotlib Basemap toolkit) (Part 4)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys welcome to the part 4 of handling netcdf files using python tutorial series now in this tutorial I'm going to show you how to plot the net CD the data in your netcdf file into a map so if you're just joining us for the first time in the part 1 2 & 3 I have already shown you how to download netcdf data for free in this case as an example I have shown you how to download the netcdf files which contains data on rainfall and temperature and also how to explore the netcdf files just to know what sort of variables are there and how to sort of extract the information of the netcdf files into a time series for a certain location of your choice so if you would like to know what I just mentioned in very precise details I would recommend you to check those three tutorials out but if not if you just want to know how to plot the netcdf files directly onto a map you're still welcome to follow along if there I'll still try to touch up on the on the things which we discussed in the previous tutorials in here as well wherever it becomes relevant alright so let's get started now apart from installing the netcdf library you will have to install one more library for this for the purposes of this tutorial which is the base map matplotlib toolkit because this toolkit actually offers a wide array of functionalities that we can use in order to set the background of our map but installing this toolkit is actually to be honest can be a bit of a daunting task I have tried using pip I have also tried using condor but unfortunately in both the cases I was actually not that successful so I found another method to install this library directly using the WHL file which I'm going to now share with you which is this one now this is sort of a repository maintained by the by the unity of california-irvine so from here what you can do is you can directly download the WHL file do the installation manually so let's see how we can do that so so the name of this toolkit is actually base map so rather than trying to find the file like this I can just a simple control-f search and try to type basemap and that should give me the location over here all right now it says that base map a map plot Lib toolkit for plotting 2d data on maps based on Gio's so the version of Python which I'm currently using is three point seven point four so what we can do is over here we can find the version which is three point seven windows and 64-bit so I'm actually going to download this one just by clicking over here and we can save this file I'm going to save it to the same location and I'm going to come to my C folder and I'm going to create a new folder over here called the base map and after that I will take the downloaded WHL file from the Downloads folder and put it somewhere over here alright so we have the WHL file over here now let's see how we can install this using the pipe anaconda prompt so I'm just going to go to my Start menu and we can search for anaconda prompt right click and run it as the administrator so I'm actually still going to use the PIP to install this pip install and now I'm going to take this part and copy it over here and set it right here put another backslash and I would now require to copy the the name of this base map file including the WHL extension so you copy the whole thing and you can paste it over here now let's hit enter and as you can see now it has started the installation already alright now it says that successfully installed base map one point two point one so that's how easy it is actually to install this using the WHL file manually all right so now since that's out of the way we can get started with the with the actual coding so for this I'm actually going to import for libraries I'm going to import yeah before that maybe let's go ahead and save our file so I'm going to navigate to the place where I would like to actually save my file all right displaying the temperature data in a map now I can go to the file explorer over here you can see that I have still those netcdf files which contains the average temperature information for es 1961 up to 1965 so I'm just going to create the new file somewhere over here and I'm going to name it as displaying netcdf data in a map all right so now let's go ahead and get started by importing the required libraries for this task so first of all as you might know we definitely going to require the netcdf library so I'm going to import that then we will also need the numpy library finally we are going to import the matplotlib library as PLT and followed by the basemap toolkit which we just installed so that one we can import buy from MPL toolkits dot base map import base map and this B should be a capital B all right so first let's go ahead and run this one and just to see whether we configured all the libraries without any issue or not all right now you can see that we got no errors so everything seems to be fine now I'm actually going to read in one of these netcdf files now mind you I got a few comments after the previous tutorial asking how to sort of merge the data from each of these files all into one so this part actually will be covered in the next tutorial but for the purposes of this tutorial I'll only maybe pick one of the one of the years just to show you how to sort of project the data into a map now rather than mixing of everything so so for this tutorial let's pick maybe 1962 so I can read the file directly using using the netcdf library I'm going to create a variable called data that's going to be equal to later set which is what you imported over here now we can give the path to the location where you keep your netcdf file so this one I'm going to copy this followed by the file name over here all right now we can hit run and if I type data over here you can see that it already imported the data for me save the latitude longitude time and the average information in two separate variables now recalling back from the previous tutorial if you would like to know what are the variables inside this this netcdf file you can just data dot variables and over here you can see or you can directly access the dot keys now this one actually gives you what sort of variables that we have in the in the in the netcdf file so you see that we have the longitude latitude time T average and rst n so for this tutorial I would only be focusing on the first four variables the longitude latitude time and the T average so let's go ahead and create separate variables for each so I'm going to create a variable called flats and that's going to be equal to data dot variables and out of the variables now I'm actually going to specify as specifically select the latitude and by putting a call and over here I'm actually directly calling all the data that's sort of stored in this latitude variable so if you would like to see that maybe if I only put data dot variables left you can see that it actually gives me the information such as the law the long name which which is latitude and the current shape also the units degrees north but if you would like to access all the data you can actually just add this colon inside the square brackets and it will actually give you the array of data all right so similarly I'm actually going to also save this information of longitude variables now over here the real name of the longitude variable is actually launched which comes from which comes from here and over there I'm also going to save all the information next I would also like to see have a look at the time so time equals data dot variables yeah as you might guess this should be time and all the information and finally the TA v which is the T average that's going to be data variables ta ve this name is supposed to be the same as the original name of the variable and all the data alright now I can run this one and if you go to the variables explorer now you can see that we have four different variables already created for each of these alright alright now I'm going to create a new variable called MP basically representing the map and this one is going to be equal to base map all right so now when we are using the base map we can specify a certain number of parameters like which projection that we want to use and what is the area of of the world that you actually focusing on because this is as you can as you can read over here it's it's just a base map so it's actually setting the platform for you to sort of plot all the all the data that you that you might have on top of that so you have to first get your base correct isn't it so we have certain number of parameters that you can specify over here for example you can specify the projection to be equal to em er sis em er see which stands for Mercator projection also you can specify a value called ll C R & R elbow in so even though it might be a bit confusing when I say it like that this one refers to the lower left corner longitude value all right so there's another parameter that you can specify the lower left corner of the latitude all right now you can guess I think the next parameter that we might need is the upper right corner of the longitude and the upper right corner of the latitude because once the program has those four points I mean those two points which has the latitude and the longitude it can create the boundary without any issue that one would be you are see are in our upper right corner longitude and you are see are in our latitude all right and finally you can also specify the resolution this one I will put as I so you actually have to specify these relatives in let's say decimal degrees so how do you exactly know now I'll actually give you a quick example if you are using a software like Google Earth you can actually just simply open that all right now if you are focusing on a country let's say in Asia some something like let's say for example you're focusing on on Malaysia so the Peninsular Malaysia which is this part so if you would actually like to draw a box covering this this country itself and if you would like to know the boundaries what one thing you can do is actually you can go to this add place mark and you can add a placemark over here as the program to show the units in decimal degrees just to be coherent with the units of the of the netcdf file so after that you can again click create a place maker over here and when you move it to maybe somewhere over here you now see what is the latitude and the longitude so this is the latitude of the lower left corner so I'm just going to copy this one directly from here and latitude which is this similarly this is the longitude and I'm just going to move this place maker to somewhere over here which I can assume to be the upper right corner and again I'm going to copy the latitudes and the longitudes in a similar way this one was latitude and this is the longitude all right so that should that should be it so now we can run this is now we have a invalid syntax error because after each entry you actually have to specify a comma over here which I missed all right now it should be fine now let's run this again now you can see over here that it's running and we did not get any errors all right so once you set the point to your preferred location which would actually represent the lower left corner of our hypothetical boundary you can get the latitude and the longitude information over here but now you can see that actually the information is in degrees minutes and seconds so let's go back to our Python script and I'm going to have a quick look again on one of these variables where it shows the latitude or the longitude information and over here it says actually in the units the units are in degrees now here the degrees actually refer to the decimal degrees so instead of getting the units in degrees minutes and seconds I'm actually going to change by going to tools options so now I'm going to create a mesh grid using the the latitude and the longitude information that we saved over here so for that we can actually use the numpy library so what we can do is we can create maybe another variable called lawn lat that's going to be equal to NP dot mesh grid and that will be created based on the longitude values that we saved over here and the latitude reliefs that we saved over here and then we can specify our XY coordinates as NP that's the base map that we specified over here now in the base map I'm actually going to pass these two arguments of longitude and latitude over here alright now let's do some work on the color skips so I'm going to create another variable called C scheme basically for specifying the color scheme and now I'm going to actually use the base map dot P color over here we specify the same X&Y that we define over here so that's going to be X Y and as the third variable I'm actually going to use numpy dot squeeze for the T average which is this one now if you recall again some information about the T average maybe I can directly copied from here it shows the long name is the daily mean temperature analyst is interpolated on to 0.25 degree grits and this is the current shape this 365 283 60s basically referring to the time time latitude and longitude so over here let's say that we have the time starting from zero up to 364 because the starting index is zero we have to specify three variables over here so the first one was is going to be the time and we separated by a comma the second one is actually going to be the latitude and the longitude so for the latitude and the longitude I'm actually going to use all the values so that's why I put that call and over here so for latitude and longitude but for time let's see if I only want to see the temperature of the day number zero so I can actually put it over here as zero but if you want to see the temperature of let's say day number 165 you can even set it over here like that so just to get started I will actually put the temperature at zero all right sorry the the time at zero which which actually means the first which actually means the first day of 1962 because here we read the data for 1962 so that's actually first of January of 1962 all right and over here we can also specify the the color map that's the C map so the type of color map I'm actually going to specify it as jet so if you care to go through the documentation of matplotlib it actually explains where these these key words come from for example over here you can see that we have different types of names for different color schemes if you scroll down over here we will find the one that I just specified which is which is jet over here which actually ranges from blue to blue to red so if you would like to go with rainbow that's possible even with another color scheme that's also possible so I'll put the link for this down in the description below as well all right so we can keep it like that yeah over here actually we have to specify the name of the variable which is T average because this is the variable for which we are actually trying to access the data all right once you're done with that you can specify you can use the basemap toolkit actually to draw the cost lines as well so how we can do that is by specifying MP because this is our base map over here because this is our base map specification over here MP dot draw cost lines and that should draw the cost lines for you even you can draw the state's MP draw States and finally I'm also going to draw the countries all right now we have almost come to the final part of the tutorial now we are going to specify a title for our plot so that we can do by simply typing PLT dot title now this PLT is directly coming from the matplotlib library which we imported at the beginning of the tutorial and as the title I'm going to specify average temperature on 1962 first of January 1962 all right so finally we can go ahead and type P LT Cho because that will show the the figure when we run the command all right now you can go ahead and run the script directly by hitting f5 either you can click over here and you can see that it's still running over here and once it's done you can see that based on the based on the lower left corner coordinates and the upper right corner coordinates which I specified you can see that it has actually extracted me the information of around Malaysia like that but there are some issues with this figure because first of all there is no color there's no scale that we can see which we can use to interpret the different colors so I'm just going to close this for the for the time being and I'm going to create a color bar over here specifying a variable C bar that's going to be equal to MP dot color bar and that caliber we are going to apply to the color scheme over here and we also can specify the location of the color bar now we can set it to be on the right side of the map which I think would be a good location to place the color bar and I'm also going to specify another argument pad close to about 10% all right now we can go ahead and run this one and see what happens all right now you can see actually the color variation is quite clear now you can see these values are referring to mostly ranging from upper 20s to 30s so that's why the whole thing looks red now just to just before I conclude the tutorial I would actually also like to see the see a wide array see a wide range of countries so instead of using my lower left corner and the upper right corner just to box one country I'm actually going to expand the boundaries maybe I will use a boundary which is somewhere over somewhere over here as the lower left corner and a boundary which is somewhere over here as the upper right corner so that I can actually get a very a clear picture of the color variation across different countries especially in this monsoon Asia region so so I have already acquired the coordinates for that so I'm just going to directly type it over here over here it's 42.8 again if you would like to do this by yourself you're very welcome to do so you can just go go back to Google Earth and move your location around and based on that you can actually obtain your lower left and the upper right corner coordinates this one I'm going to specify as minus two and the upper right corner is going to be one or five point three seven and the upper right corner lad is going to be thirty eight point seven eight all right now I'll run this again now that's a much better picture isn't it which actually shows the color variation across different countries in Asia as you can see over here around China it's quite cold which might be in about probably about negative 10 to negative twenty also there are some locations I see actually which is quite blue which might also reach to negative 25 over here as we come to the southern part you can see the temperature is actually increasing even for the Southeast Asian region countries so I think that concludes the tutorial for today I guess you learn something new you learn how to plot your netcdf data into into a map like this and we show lies it like this so you can actually play around with the different different components of this base map toolkit and also with the different color schemes which gets offered with this extensive mass map plot label library to make your map look much nicer but for this tutorial the main aim was for you to actually give you the context of how to sort of transfer your information into a map when you have these gridded information how to sort of create the base map first and then project your data on top of that base map so that it looks like this alright so that's about it for the for this tutorial now next tutorial I'm actually going to show you a cool trick how to create a time-lapse let's say in this case we have the data starting from 1962 first of January up to 31st of December 1962 this is the map which is corresponding to that in the next tutorial I will show you how to acquire all the maps let's say in a jpg format and how to record all the maps into one continuous a sort of an animation which shows you time starting from the first day all the way up to the day 356 T 5th day and through the variation of the colors you can actually see the temperature variation throughout the months of the of the different months and weeks of the different year so that's going to be explained in the next tutorial so so so if you like this tutorial please hit the like button and if you did and if you did really liked it you can actually consider subscribing as well and I'll see you in the next one
Info
Channel: GeoDelta Labs
Views: 22,973
Rating: undefined out of 5
Keywords: CHIRPS, Gridded, Satellite, Rainfall, Data, GIS, Python, how to, convert, rasterio, ArcGIS, QGIS, numpy, pandas, Os, DEM, Raster, vector, APHRODITE, APHRODITES, APHRODITE'S, Japan, monsoon, asia, netCDF, multidimensional, data, xarray, matplotlib, pyplot, basemap, seaborn, toolkit
Id: r5m_aU5V6oY
Channel Id: undefined
Length: 28min 23sec (1703 seconds)
Published: Sat Mar 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.