How to create an animated time-lapse of temperature using netCDF grid data (Part 5)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to the part 5 of handling netcdf files to toriel series now in this tutorial I'm going to show you how to create a time lapse from your netcdf plots she learned how to do in the part 4 of this tutorial series and to create a time lapse or an animation that looks like this pretty cool isn't it so let's go ahead and learn how to do this alright so at the end of the part 4 of this tutorial series I showed you how to generate this kind of a matplotlib graph how to basically transfer the details of your netcdf files into a graph like this so I'm actually going to use the same script over here in order to do this task of creating a time lapse so let me go ahead and get rid of this plot and and even though I'm going to use the same script for the most part of it I'm actually going to make a few slight changes so let's navigate back to the default that you would like to work in select the folder and go to file explorer and let's create a new script temperature data time-lapse alright so let me just go ahead and select this whole thing and copy it to over here and I'm going to go ahead and close this one now so now if I run this script again you see that we get the same output so everything is everything seems to be working fine alright so I'm just going to go ahead and close this one so at the end of the previous tutorial part four of this tutorial series I actually told you that the zero refers to the first day of the year 1962 that's the first of January so let's say for example we select one of those dates maybe from the mid of the year let's say around 150 so accordingly the title should be changed to average temperature on the 150th day of year 1962 alright so now I can just go ahead and run the script all right now you see that we get the corresponding data we get the data which is which corresponds to the 150th day so you basically get the idea how to access the different dates by changing the value over here so for example we have 365 days but since the index value of the time starts from 0 we have the values only up to 364 for example if I put over here 365 and if I try to run this one I will definitely get an error yes so just like that so what I'm going to do is actually I'm going to create not only one graph per one time I'm actually going to loop through all these 365 days and I'm going to create different graphs at each different day and I'm going to save those graphs into let's say into a JPEG and I'm to collect all those graphs into a separate folder so let's see how we can do that so I guess you agree that all the way up to here it should be the same the code should be the same because here what we do is only specify our boundary and it's just over here where we specify in the program the data corresponding to which day which exact day are we actually going to plot in our figure so what I'm going to do is I'm actually going to go ahead and create a variable called days and now for the year 1962 I know that we have 365 days and I'm going to create a numpy array using NP dot a range which starts from 0 and ends at 365 all right so if I show you now what happens if I execute this small piece of code you see that we have an array which starts from 0 and ends at 365 so I'm actually going to iterate through each and every entry like this which basically corresponds to the zero day the first day the second day the third day of year 1962 so what I'm going to do over here is actually for each particular day which corresponds to index number 0 1 2 3 all the way up to 364 which actually corresponds to the first second third fourth all the way up to 360 fifth day I'm going to loop through each and every day and I'm going to generate the corresponding temperature map and I'm going to save it into a jpg all right so now since this is done I'm actually going to go ahead and create a for loop so I can say something like for I in this now keep in mind that this is actually a variable that you're assigning if you would like to assign different name for that you're very welcome to do so but just for the purposes of this tutorial I'm going to use it use this one as I and now I'm actually going to push this whole thing by one tab and instead of specifying a solid figure over here I'm actually going to specify I so what happens is when this is looping through 0 up to 360 for the value of I is also going to change from 0 all the way until 364 that means the value over here is also going to get changed from 0 up to 360 for during each iteration so that's precisely what I want isn't it all right so these three lines I can leave it like that because I would like to draw the coastlines the states and also the the countries and the color bar I think I can still retain it no need to make any changes now just in the title because as you can see that now for each iteration the title should actually change so just to keep the things a bit simple all I'm going to do is I'm just going to say it's the average temperature day and now I want to actually use this I well because during each iteration the value of I is going to change but actually let's say when the I the value of I is 0 my actual date is actually day number 1 so what I'm going to do is I want to put this as I plus 1 these values are integers now I'm trying to add an integer into a string which is not really not really possible so what I'm going to do is alright so my objective over here is actually during each iteration I would like to specify which day it is going to be exactly so for example when I the value of I is 0 I would like to say that this is the average temperature for day number 1 and when the when the value of I is 1 I would like to say that this is the average temperature for day number 2 I guess you get the idea behind that because what we specify over here as I is basically the index which is one unit less than the actual date itself so what I can do over here is I'm actually going to create another variable call day and that's basically going to be equal to I plus one just for the simple reason which I just explained so now I'm going to specify the title as average temperature day and now I'm going to add this day variable over here now here we've got a small problem because this is actually an integer so during each iteration no matter what the value of this is it's going to get it's going to get its value increased by one but still it's going to end up as an integer so but the issue is we cannot add integers with strings so before adding this I actually have to convert this into a string and you can do that on the spot you don't need to create a different separate variable actually if you want you can but there's no no real necessity of doing that so for example during the first iteration it's going to display something like average temperature they one off here 1962 all right so during the second iteration it's going to say that average temperature day two of year 1962 alright so if you can recall the figure back over here you see that we have we do have a scale now when we go through different iterations actually this scale can change but if the scale keeps on changing from one figure to another actually we won't really have a very accurate representation of how the temperature changes over the over the different days over the different weeks because it will automatically adjust to to sort of represent data in the best way so because of that if we need to create a time lapse it's very important that we set the upper bound and the lower bound of the temperature range to be a constant so you can set that simply by saying PLT dot limb so in this case I'm actually going to set the range to be from minus 40 up to let's say plus 40 all right and now I'm going to enter the statement which actually saves this figure which is PLT dot savefig now we hear what you can do is you can you can specify the file name over here if you want to save it in into a separate folder you can do that you can do that as well but in this case but in this case since my working foal is actually over here I would like to save all the files into this this fold itself what I can simply do is I can actually specify the day over here and I'm going to convert that into a string STR plus it's really important that you specify the file format which is jpg JPEG in this in this case and finally during each iteration you actually want to clear your figure which means whenever you record a figure onto this onto this plot you actually want to sort of get rid of the previous plot so in order to do that you can just go ahead and type PLT dot C CLF and that should that should do it so just to give you an example let's go ahead and shorten this to maybe only about 10 days so and let's run the command and see what happens if I open the file explorer now here you'll see that we actually get different JPEGs and finally ten all right so if I just go ahead and open my open the corresponding folder now you see that we actually have the average temperature starting from day number one of the year 1962 and we can go to day number two day number three day number four five six seven eight nine ten and so on and you can see how the temperature actually varies from each day to the other so let's go ahead and not do this only for ten days let's do it for the whole 365 days so I'm just going to enter 365 over here and let me go ahead and get rid of all these files well I feel like it's better to collect all the generated JPEGs into a separate folder so maybe I will create another folder over here maybe I will name it as JPEGs simple and then what I can do is I can specify the path over here to which the files should be saved so that I can specify by are alright like this so just going to go ahead and run this script now and then you will see how the how each and every file actually gets collected into this particular folder all right now since the process is actually going to take some time I'm just going to it might take a few minutes I guess you all right so once it's done you can see that we have gotten the image file starting from day number one all the way up to the number 365 isn't it all right so now let's go ahead and see how we can do the animation in order to change the figure from one to another all the way up all the way from the day number one up to day number 365 fully automatically so we are going to do this actually by creating a gif out of all these images starting from the first image up until the 365th image so I'm just going to go ahead and close this one now if you want to create gifts one way that you can do is actually simply by using the using one of the Python imaging libraries which is called pillow so so for this tutorial you will have to actually in order to generate the gif to automate the the figures you will have to actually install the pillow using pip so I'm just going to simply go ahead and open my anaconda prompt and right as the administrator and what I can do is I can actually go ahead and type hip install pillow just like it is mentioned over here with a capital P alright and press Enter and since I have already installed this package you see that it says the requirement is already satisfied but in your case if you haven't installed this library before you will see that it will get actually installed without an issue so after that you can just go ahead and close get rid of all of this and the code in order to generate the gifts I'm actually going to write it in a separate dot py file I'm just going to go ahead and open the file explorer and let me create a new Python module and I'm going to name this as temperature data time lapse generate gif generate a gif file alright so what you can do is you can just go ahead and import the pillow library which you just installed but keep in mind that when you import the library of pillow you actually don't import it like this instead what you do is you just type PIL all in capital all right now you can run the command and see whether you have any errors or not now you can see it's all clear so we can proceed now I'm going to create another variable called image frames which is going to be an empty list to start with and let me import the numpy library as well import numpy as NP just because i would like to create another variable called maybe this that's going to be equal to NP dot arrange starting from 1 up to 366 all right now let's go ahead and run this one now if huge if you're just wondering why I specified it to be one from 366 because this is how it looks we need when you expand it it starts from 1 ends at 365 and if I go back over here to my files first file starts from 1 and the final file ends in 365 what what I'm basically going to do is I'm actually going to look through each and every file starting from 1 to 365 that's basically the the trick behind it alright so let's go ahead and look through each and every file so I can do that by creating a simple for loop I'm going to pick a variable maybe called K days so in each iteration the value of K will be 1 2 3 all the way up to 366 sorry all the way up to 365 now let me go ahead and create a new variable called new frame which is going to be equal to now we are going to use this pillow library PIL dot image dot open alright so in each iteration I would like to open each of these JPEG files so let's say during the first iteration it should be this part R let me go ahead and squeeze this one a bit plus two more back slashes plus alright if it's the first picture it'll be something like 1 dot jpg isn't it if it's the second picture it will be 2 dot jpg if it's a third picture will be 3 dot jpg all right so now you get the idea what I'm going to do is I'm also going to create leave a bit of space over here and I'm going to add this K variable over here and I'm going to add that I think now it's clear however there is a bit of a problem during each loop this K is actually going to get recorded as an integer so now since I'm going to sort of incorporate that integer into this part I have to convert this into a string only then it'll get added into the rest of the path all right now what I'm going to do is I'm going to take this empty image frames list and I'm going to append what I'm going to append is basically this new frame so what what it's going to do is okay during the first iteration this image frames is actually an empty list so at the end I'm going to add this new frame which corresponds to the first JPEG in to this and in the second loop it's actually going to append means we already have the first one and it's actually going to create the second entry using the second image and so on so let's run this one and let's go ahead and open the variables Explorer now if I open this image frames now over here you can either actually it's still a list but now it has different image files as you can see over here because it's now storing all the images so for example if I say that image frames and if I check the type you will see that the type is actually still a list so the way you access different elements of a list is you just type the name of the variable image frames and over here if you specify number 0 that means you're actually calling the first entry which should give you this image similarly if you try to select the second entry you have to enter number 1 and that should give you the day number 2 and if you want to select the third entry you have to press enter day number 2 because we are talking about indexes over here it's going to give you the number 3 and similarly if you if you want to access day number 365 you need to give the index which is 364 that's corresponding to the day number 365 now you see that it's just like a simple list but this lit list is now full of images 365 images quite a heavy list isn't it alright now let's go ahead and try to put all these images into one gif so you can simply do that by specifying image frames let's say I'm starting from the first image and I'm going to save now I need to give a file name for the gif file let's say temperature time-lapse alright dot gif quite important and then over here you need to specify the format what's the format is equal to or maybe I can specify it over here the format that's equal to gif alright with a comma now I can append images now these append images is also actually an argument so the images that you're going to append is basically these image frames since we have the first entry over here which corresponds to the 0th index we can actually specify everything else by specifying one and leaving a colon over here basically that selects the entire set of the other pictures apart from the first one alright now since you want to save all those images we specify the save all argument to be true and we can also specify a duration maybe I can put it as 300 now the units of this duration is actually in milliseconds and finally I can also specify a loop and I'm going to put the value as 0 what this does is actually it's going to make create an infinite loop so after it creates this temporary timeless temperature time-lapse gif it's actually going to keep on looping all the way from the first image up to the 365th image and again starts from first to the 365th and so on until you stop the image until you close the image it's quite normal if you are just currently wondering how on earth I'm actually going getting these different arguments so I'm not actually doing anything by heart what you can do is you can also actually go to the official documentation of this pillow library and over here you can see that there are many other parameters that you specify for example this duration which we specified to be 300 it's in milliseconds and yeah loop may not be present the number of times the gif should look zero means that it'll actually look forever so this kind of things actually you can even go through this documentation which I'll put the link down in the description below so that's basically now we can just run the command and you can see that it's still running because it's going to be quite a heavy file you're sort of combining 365 JPEG images into one gif so that's definitely going to be a heavy file alright now it seems like it's done so since I did not specify a part over here where this is going to go is to the place which is actually my current working directory which is this one so just going to go back one step and this is the temperature time-lapse gif which I created now you can see it's actually of 26.6 twenty four point six megabytes so quite a heavy file now if I open this one you can see that it's actually opening in your browser and it's basically producing an animation for you starting from day number one all the way up to day number 365 now if you would like to in case if you would like to speed it up a bit maybe you can specify the duration to be a bit faster maybe about let's say 200 milliseconds then you can run this and you may wait well while it gets executed all right it seems like it's done all right now you can see that starting from day number one all the way up to day number 365 how the temperature actually ranges now you think you can see that as we approach the mid of the year about June July you can see that actually the temperature is sort of getting quite red over here which means it's going to move to the upper bounds of this scale which is almost in its 30s to 40s over here and by the time we reach to the end of the year probably by October November December the temperature the temps will actually cool down a little bit so that's what you see over here now slowly as we approaching the end of the year you can see that somewhere over here in China it's already blue over here now it's actually going again from day number one just like an infinite loop so I guess you got the basic idea of this tutorial how to create a gif how to create an animation or basically a time-lapse of temperature data which you just created which you created from the from the netcdf information which you downloaded for a specific variable in this case temperature so I guess you enjoyed this tutorial if you did learn something new you can always hit the like button and show your support and if you did really liked the video you can even consider subscribing so thanks a lot for watching guys I'll see you in the next one
Info
Channel: GeoDelta Labs
Views: 6,264
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, time-lapse, timelapse, Pillow, PIL
Id: dSv3-obKv3M
Channel Id: undefined
Length: 28min 10sec (1690 seconds)
Published: Sun Mar 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.