# http://hrishichandanpurkar.blogspot.com/2015/10/xray-package-brilliant-little-tool-for.html

import numpy as np
import pandas as pd
import xarray
import matplotlib.pyplot as plt
import matplotlib
from mpl_toolkits import basemap

nc=xarray.open_dataset('..//Data/gpcp_1dd_v1.2_p1d.201309.nc')

#'nc' will have all the actual variables and metadata as object instances

# Print nc's info. Similar to ncdump
#print nc.dump

# Load preciptation data. precip will have all the dims and associated metadata with it
precip=nc.precip 

plt.imshow(precip.mean(dim='time'))
plt.colorbar(orientation='horizontal',pad=0.06).set_label(precip.units)
plt.savefig('gpcp_xarray_pcp_mpl.png')
plt.close()

# Calculate average across time
precip_avg = precip.mean(dim='time')

# Add coastlines and plot
m=basemap.Basemap(projection='cyl',lat_0=precip.lat[0],lon_0=precip.lon[len(precip.lon)/2])
m.drawcoastlines()
precip_avg.plot.contourf()
plt.savefig('gpcp_xarray_pcp_avg_mpl.png')
