La idea del proyecto es probar:
La versión final esta dos archivos python para que puedan ser ejecutados desde la linea de comandos. Los archivos son:
from ftplib import FTP #conectarme a un ftp para conseguir los datos
import os
import mpl_toolkits.basemap as bm # import Basemap matplotlib toolkit
import numpy as np
import matplotlib.pyplot as plt
import pygrib #para trabajar con archivos grib
%matplotlib inline
#conecto con el servidor ftp
url = 'ftp.cpc.ncep.noaa.gov'
ftp = FTP(url) # connect to host, default port
ftp.login() # user anonymous, passwd [email protected] = 'ftp.cpc.ncep.noaa.gov/NMME/clim/tmp2m.01.CFSv2.clim.1x1.grb'
#manipulo archivos dentro del directorio clim
ftp.cwd('/NMME/clim/') #ingreso al directorio
ftp.dir() #listo los archivos
filename = 'tmpsfc.01.CFSv2.clim.1x1.grb' #archivo ejemplo
local_filename = os.path.join('data', filename) #ruta donde voy a generar el archivo que se escribe
print(local_filename)
file = open(local_filename, 'wb') #abro el archivo donde se va a alojar
ftp.retrbinary('RETR '+ filename, file.write) #recibo info y guardo en archivo
file.close() #cierro archivo
ftp.quit() # This is the “polite” way to close a connection me voy del ftp
#grib_file = 'tmp2m.12.CFSv2.clim.1x1.grb'
grbs = pygrib.open(local_filename)
for grb in grbs:
print (grb)
file.close()
grbs.rewind() # rewind the iterator
t2mens = []
i_month = 1
e_month = 3
for grb in grbs[1:4]:
print(grb.messagenumber)
# if grb.validDate == date_valid and grb.parameterName == 'Temperature' and grb.level == 2:
t2mens.append(grb.values)#t2mens = np.array(t2mens)
t2mens = np.array(t2mens)
t2mens=np.mean(t2mens,0)
#print t2mens.shape, t2mens.min(), t2mens.max()
lats, lons = grb.latlons() # get the lats and lons for the grid.
lats = lats[:,1]
lons = lons[1,:]
print(t2mens.shape)
#print 'min/max lat and lon',lats.min(), lats.max(), lons.min(), lons.max()
mapproj = bm.Basemap(llcrnrlon = 360-85,llcrnrlat = -60, urcrnrlon = 360-25,
urcrnrlat = 10 , projection = 'mill', area_thresh =10000 ,
resolution='l')
mapproj.drawcoastlines()
mapproj.drawcountries()
mapproj.drawparallels(np.array([-60, -40, -20, 0]))#, labels=[1,0,0,0])
mapproj.drawmeridians(np.arange(0,360,60))#, labels=[0,0,0,1])
lonall, latall = np.meshgrid(lons, lats)
lonproj, latproj = mapproj(lonall, latall)
#print(lonproj.shape)
#print(latproj.shape)
cs = plt.contourf(lonproj,latproj,t2mens[:,:],10)
t = plt.title('Prueba')
plt.show()
#Este css es trabajo de @LorenaABarba y su grupo
from IPython.core.display import HTML
css_file = '../../css/personal.css'
HTML(open(css_file, "r").read())