terça-feira, 25 de novembro de 2014

Plotando dados no mapa no formato "Shaded"

O código abaixo mostra com plotar dados de um arquivo NetCDF em um mapa usando Python.

# -*- coding: utf-8 -*-

__author__ = "Marcelo Rodrigues"
__credits__ = ["Aurélio Noronha", "Fco Vasconcelos", "Arthur Costa"]
__license__ = "GPL"
__version__ = "1.0"
__email__ = "marcelorodriguesss@gmail.com"

from scipy.io import netcdf
from mpl_toolkits.basemap import Basemap
from matplotlib import colors as c
import matplotlib.pyplot as plt
import numpy as np

prate_file = "pcp.nc"
prate_data = netcdf.netcdf_file(prate_file, 'r')
# print(prate_data.variables)
lon = prate_data.variables['longitude'][:]
lat = prate_data.variables['latitude'][:]
time = prate_data.variables['time'][:]
pcp = prate_data.variables['pcp'][:]

m = Basemap(projection='cyl', llcrnrlat=lat[1], urcrnrlat=lat[-2], llcrnrlon=lon[1], urcrnrlon=lon[-2], resolution='h')

my_colors = ('#b43214', '#f05a32', '#ff9141', '#ffd750', '#fff0b4', '#e6ffe6', '#aae6b4', '#6ee66e', '#06be0a', '#058c00', '#005000', '#002300')
lons, lats = np.meshgrid(lon, lat)
x, y = m(lons, lats)
clevs = (0., 200., 400., 600., 800., 1000., 1200., 1400., 1600., 1800., 2000.)
bounds = clevs
my_cmap = c.ListedColormap(my_colors)
cs = plt.pcolormesh(x-2.8125/2., y-2.8125/2., pcp[0, :, :], cmap=my_cmap)

m.drawparallels(np.arange(-90., 90., 30.), labels=[1,0,0,0], linewidth=0., fontsize=10)
m.drawmeridians(np.arange(0., 360., 40.), labels=[0,0,0,1], linewidth=0., fontsize=10)
m.drawcountries(linewidth=0.5)
m.drawcoastlines(linewidth=0.5)
# m.drawstates(linewidth=0.5)

bar = m.colorbar(cs, location='bottom', pad="8%", ticks=clevs, boundaries=bounds, spacing='uniform', extendfrac='auto')
plt.title('Acumalated Precipitation')
figname = 'accum_precip.png'
print('Salvando figura:', figname)
# plt.show()
plt.savefig(figname)
plt.close()