Guest User

Untitled

a guest
May 21st, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. #from netCDF4 import Dataset
  2.  
  3. import xarray
  4. from scipy.stats import norm
  5. import numpy as np
  6. import matplotlib.pyplot as plt
  7.  
  8. list_indices = ['tasmax']
  9. indices = list_indices[0]
  10.  
  11. #test
  12. #files = '/g/data3/w97/dc8106/AMZ_def_EXPs/121GPsc_E0/AMZDEF.daily_tasmin.tasmax.pr.1978_2011_121GPsc_E0.nc'
  13.  
  14. #remove CTL diff to CTL file from analysis
  15. #del(files[11])
  16.  
  17. #var = np.zeros((len(files),12418,145,192),dtype=np.float32)
  18. #t = []
  19.  
  20. print "Open file"
  21. data = xarray.open_dataset('/g/data3/w97/dc8106/AMZ_def_EXPs/121GPsc_E0/AMZDEF.daily_tasmin.tasmax.pr.1978_2011_121GPsc_E0.nc')
  22. #tasmax = data.tasmax
  23. #tasmin = data.tasmin
  24. #lat = data.lat
  25. #lon = data.lon
  26. lons,lats = np.meshgrid(data.lon,data.lat)
  27.  
  28. print "File opened. Calculate stats"
  29.  
  30. # To calculate the PDF of the distribution using norm.pdf() we need to know the mean and std. deviation of the distribution.
  31. # First create an array to save the mean and std. deviation for each grid cell. This array needs to have the dimensions:
  32. # (2, nlat, nlon). 2 is because we save 2 stats per grid cell
  33. # We'll put the mean first so stats[0,:,:] will contain the means of all grid cells and stats[1,:,:] will be the std. dev.
  34. stats = np.zeros((2,data.tasmax.shape[1],data.tasmax.shape[2]),dtype=np.float32)
  35.  
  36. # Let's transform it into an xarray DataArray so we keep the information of what all the axes are with the data:
  37. stats_xr = xarray.DataArray(stats, coords=[[0,1],data.coords['lat'],data.coords['lon']],dims=['stats','lat','lon'])
  38.  
  39. stats_xr[0,:,:] = data.tasmax.mean(dim='time') # xarray methods allow you to refer to dimensions by name rather than position
  40. stats_xr[1,:,:] = data.tasmax.std(dim='time')
  41.  
  42. print "Stats calculated. Plot 1 PDF"
  43. # Now if we want to plot the PDF at a location we simply need to do:
  44. # Define our x axis. Here we'll plot the PDF for values ranging from [min-10, max+10] where min (resp. max) is the minimum
  45. # (resp. maximum) value of tasmax at the location we want to plot
  46. # Location:
  47. ilon=50
  48. ilat=30
  49.  
  50. # xarray comes with a handy way to select a point using the dimension names instead of the position in the array:
  51. tasmax_t = data.tasmax.isel(lat=ilat,lon=ilon)
  52. mean_loc = stats_xr.isel(stats=0,lat=ilat,lon=ilon)
  53. std_loc = stats_xr.isel(stats=1,lat=ilat,lon=ilon)
  54.  
  55. x = np.linspace(tasmax_t.min().values-10,tasmax_t.max().values-10,100) # The last number is the number of values
  56.  
  57. #norm.pdf(x,mean,std) calculates the PDF for a normal distribution with the given mean and stats and return the value of the PDF
  58. # at the x point. plt.plot() will plot those PDFs for all the points in the x array as defined above: 100 pts between min-10 and max+10.
  59.  
  60. pdf=plt.plot(x,norm.pdf(x,mean_loc,std_loc))
  61. plt.show()
Add Comment
Please, Sign In to add comment