How To Plot NetCDF File in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome back so today i'm gonna show you how to plot net cdf file in python so if you are familiar with how to read net cdf file so you can easily plot that using panoply you can even plot using gis that's what i'll show you in the next tutorial but today i'm going to show you how to plot that net cdf file in python and if you want to plot that and after that if you want to even create an animation if you have many plots and for many time tests so you can easily generate any gif file as animation and i'll show you how to do that it's pretty simple because this is necessary to show the animations to other people right to even display or to present our results from our experiment right or for even publishing paper or even adding to this report or presenting in front of other people we need to even show the animations because the general people they may not understand what is actually happening if you show them animation then they will understand okay this that is the way any storm is moving or any stream flow is flowing or anything so you will be able to do that if you finish this video so let me quickly show that if you are familiar with what we did previously so i think you already know what we are gonna do okay so i'm sharing my screen here here is the codes what i'm gonna do i can show i just mentioned this one right i can't even i already know right how to subset an lcd file and if you want to create the animation for this stereo area you can do that okay and for this one what i'm going to do because the storm is just covering the atlantic ocean and it is generating from the middle of the atlantic and then it is passing and making a landfall so i'm going to show you the entire data set right so in order to do that we need to specify the boundary the entire boundary so if you know the boundary or if you don't know the boundary you can open it using piano please quickly and then you can get the boundary here right we can read that from the t1 either it is minus 97 or 95 it is 95 we can take it as 95 and here it is it is uh 50 minus 50 since it is west and for a latitude we have that 5 degrees right not to 45 degree north so that's it so that is the idea we know right longitude is from minus 50 to all the way to 95 and it is from 5 or we can take it as 10 okay from 10 to 45 that's it so here is the code nothing else it is the same thing i'm doing i'm importing this module data set that's what we need and for plotting purpose i'm just uh importing that matplotlib as pi plot right or plt whatever you need and also i'm adding that mpl toolkits and i need to add the base maf so that's why i'm adding the base map from that module and i'm suppressing some warning as it is and at the beginning what i do this time i'm giving the variable name nc file and i'm reading it right i'm just importing the file from my directory the same file i processed previously it is the same name and now i'm reading as data set same data i can read it here as well it is the same one and next time what i'm doing here as a next step i'm extracting all these latitude values this is how we read right if you don't have any idea how to read netsidio file you can go back to the previous videos where i already demonstrated how to read near cdf file and then i'm also extracting the and i'm just plotting for this case only the pressure plot okay so i'm just extracting the pressure variable there and for this case i'm extracting everything but i'm plotting only pressure so these are the variables i have i have also time because it will be uh time dependent so for first time i have to create one plot for second one i'll generate another plot and i'll save it as png or gpac or means jpg right so whatever it is we can do that so these are the step where we are just reading all those variables right that we have and here we have to even specify our domain right or the extent of our math so for this case i'm gonna just specify the entire domain that we have so it is lower right corner okay so what will be the value of this one so usually what we have to specify that will be the maximum value it is our longitude is 50 right we already mentioned that and what will th with the latitude it is the minimum one so that will be the 10 okay 10 and then the longitude the minimum value it will be 95 okay that's it and what will be that maximum value of latitude it is 45 right so that's it and i'm keeping that resolution as i and that is the resolution we can use and then what i'm doing here i'm creating a mesh grid it is another way to that's why we need m numpy so if you pass this latitude and longitude it will create mesh grid so how it works it works like that right it is going to create this mesh for you and based on this mesh it will interpolate this data so it will plot this way so it will create this mesh if you specify your latitude all this latitude and longitude because we have longitude values here we have here right we are there there here but we have a latitude this way it is varying from north and south direction right so okay i'm clearing everything let's see okay so i have a 38 time step right when you open the net cdf file you will see all these dimensions and property and how to do that we already have idea about that one so i'm just creating the list of right array of the time and i'm just looking through each of these times and okay i'm extracting all these variables for that time step and i'm saving that so that is the logic here i'm looking through the hours i'm creating the list of hours or the area of hours here from 0 to 38 and it will the first one will be 0 1 2 and all the way to 37 that means we have 38 times step these are the uh integer value we can use that value as our index as as well so here these are the command i'm using that uh c scheme and then that mp that my base map the dimension i created here i'm passing that and p color and i'm passing the x y and then i'm squeezing that np that squeeze and i'm reading this pressure because i mentioned the variable known as pressure here and i'm using the same thing i for first uh first time instead and then all this latitude and longitude and it will the color map will be the jet okay it will show you the changes from red to blue and then that is the command if you because that is the map base map it will automatically download that coast line right for your study area you don't need to add separately and it will also download the state map state boundaries as well you can add more uh feature if you want and it will also draw the countries as well so that's what we need and also it will show the color bar okay and that is the command and it will be showing on the right side and 10 percent is the pad and next it will increase the our number because we are going to create the map for every time step right initially it was 0 and i'm creating it as 1 i plus 1 so that we can get 38 there is no because python it starts from 0 right we can even specify here as well 1 to 39 then it will be 1 to 38 and we don't need to do that i plus 1. whatever you want you can do both way why i'm gonna do that because i'm just trying to create that file name this way right so and then it is just uh it will show you that title pressure distribution of florence for that hour for zero hour one hour two hour right so in order to do that i'm just giving this one okay so that's it but the file it is not for zero hour it is for six hour interval right from 9th september 2018. so what we have to do we have to multiply this hour by 6. initially it will be six hour from this ninth of september that is the starting point and then it will just uh multiply by six because the time step we have it is for six hour interval so we have to multiply that one and i'm just specifying the limit of the color bar i specified so that we can easily identify the pressure plot the eyes moving right where the pressure is very low you can see the red and it will move and blue as well right and i'm just uh saving it in my c drive i created one jpeg that folder by this name that folder it will save all this now files jpg files and then what it will do it will just save the file name as our number okay that our first one will be 1 2 3 up to 38 right so that's it so if i run this one let's see if it works it really works or not i'm running the code see we don't have any module name this one we have to install that module right this one so how can we do that so that is the way you have to install pip because when i did it i did it in my python if it won't work not a problem we can use our linux machine you can use that okay we may need to could not find a version that satisfied this requirement okay let me install there sometimes you have to do that way um okay okay here i'm closing this one i'm gonna open another command prompt if you need to install if you need to install any module what you need you need to use that okay pip command p install and what is the name of the module mpl toolkit okay let's see otherwise we'll use our linux machine no problem we already have that maybe it's not working here because the python code it is a 3.9 version i installed the latest one maybe i don't have that one so okay uh let me quickly copy that part and i only have i think the code in my linux machine okay that's it let me delete everything and here it is this is how i do all this processing in my linux machine not there okay so here it is i have the file name i have the file here the same file i uploaded there and i'm gonna just uh change okay let me open the file otherwise i'll edit one and then it will plot in a different folder so what i'm gonna do just um generate gis okay that one image generator at first i'm gonna generate the image i need so i have to specify the file name how can i do that i have the file name there i'm gonna rename means i'm gonna copy it and i'm gonna change the file name because it is in the current directory that is the file name it is reading this way so so far i can remember that will be 50 and that latitude right it was 10 and that was basically 95 and that was maybe 45 as i can remember everything is same because i'm using the same file here it will be 38 because i have 38 then what i did jp easy maybe i already have that here right see as ap easy maybe i already have that uh where it is where is the file name jpg maybe i can create new right i don't need that one i can create new so it is a j p e g i'm copying at the same time i have the folder everything will be inside that one okay so then what i'm gonna do i'm gonna change the name there and everything is same the same code i'm using here nothing different i'm saving the file it's that one but in order to run that i have to i have to activate the python environment where i have installed that mpl okay so that is the command contact event and i hope that i have everything there let me see and in order to run that because i created that for now that i'm not telling you can regional re-analysis i'll show you that no module name by this so here i have to also install maybe um deactivate this one okay so i can i can activate my other python three okay this one okay i think hopefully we have that yeah but uh oh i see we have problem there it may be showing the previous directory i just missed that one it should be this one or yeah so this is how because double dot means it is specifying one directory back but i don't have the file there that's where it's showing we don't have that file one directory back so it is in the present directory right and it is showing another problem that i don't have that variable name this one so this variable name is not that it is the p it is only the p it is not that because i stored that p variable for nar data not this one so i have to specify everything maybe i will have other problems or i don't know maybe this time it is working let's see if it is going to create because it is a huge area right yeah it's creating the map and where is jp easy let me show you yeah maybe it is creating the map domain first now it is taking time because what it is doing it is downloading that right this uh share files for the coastal area coastal boundary the state boundary and also the country right country boundaries so that's what is downloading and taking time and once it's done it will just quickly do the same step but still it will take a while to create all this because that's why the map is pretty big and that's why i told you that in order to process your data you need to make the subset and by this time you know right how to substitute all this data and if you don't know that if you didn't watch the video you can go back to the previous video and you can watch that then you will see how to subset once you are done with subsetting your data then you can yeah the processing will be faster so in order to yeah ensuite that thing i mentioned that okay we need to subset our data because we don't need the data for that is covering the entire usa right we need only data for our study area because our study area may not be that much big but this data they generate it covers right pretty big areas so see the file one it already saved if i refresh it will be there and if i open it you'll see the image for the first time steph so that one okay that is the image it's just coming from this side to that side so that's that's the way we can go right and okay so this is how we can even specify our latitude or longitude then we can specify everything and then it will yeah it will show you everything we need right this is how we can plot and yeah and i'll show you in the next tutorial that how to how to make gis when you have all this because it will take a while so it will take a while to see still it is taking to plot the second one so this is how we can plot the net studio file using python code and once you are done with your image generation you will have 38 times right plots for other cases you may have more than 100 whatever the number of images you have you can then merge them together and you can specify a time delay in between these two image and it will show you the animation gis and animation i have the second one as well see i have the second one so it is this one for the next one we can't see the difference it is moving to the shore right shoreline it is moving towards the shoreline so this is how we can plot and in the next tutorial i'll show when this plotting is done i'll use this plot to create the animation and ultimately will show this animation while presenting or report or presenting or data set right so that's it you can try by yourself and if you have any different data set like for precipitation for the wind speed and for pressure and for any other spatial distribution for temperature or any any spatially distributed data in net cdf format we can use uh python code to plot it and once we are done we can even create the animation out of this uh images that we are going to plot so that's it if you have any query or question you can let me know or you can try by yourself and see you in the next tutorial thank you for watching and stay safe
Info
Channel: Md Arifur Rahmahn
Views: 791
Rating: undefined out of 5
Keywords: Python programming, listing directory, for loop, read csv file, multiple directories, time series conversion, time series interpolation, read nercdf file, mrms, nldas, ghcn
Id: psvGW0cGBEc
Channel Id: undefined
Length: 19min 13sec (1153 seconds)
Published: Sat Sep 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.