In [1]:
import numpy as np
from cdo import *
cdo = Cdo()
from os import getenv
import time

HOME = getenv('HOME')
bbox_cdo = '-20,20,-20,20'
bbox_ocgis = [-20,-22,20,20]

# global dataset CMIP5
resources = HOME+'/birdhouse/flyingpigeon/flyingpigeon/tests/testdata/cmip5/tasmax_Amon_MPI-ESM-MR_rcp45_r1i1p1_200701-200712.nc'

In [2]:
resources


Out[2]:
'/home/nils/birdhouse/flyingpigeon/flyingpigeon/tests/testdata/cmip5/tasmax_Amon_MPI-ESM-MR_rcp45_r1i1p1_200701-200712.nc'

In [3]:
# get the North Atlanic region form global dataset with cdo
tic = time.time()
nc  = cdo.sellonlatbox( bbox_cdo, input=resources, output=HOME+'/data/tests/subset.nc' )
tac = time.time()
print 'sec: %s' % (tac-tic)


sec: 0.0446629524231

In [4]:
# read in the file
from netCDF4 import Dataset, num2date
from flyingpigeon.utils import get_variable

var = get_variable(nc)
#print 'variable name: %s' % var
ds = Dataset(nc)
psl = ds.variables[var]
lat = ds.variables['lat']
lon = ds.variables['lon']

In [5]:
print lon[:]


[-18.75  -16.875 -15.    -13.125 -11.25   -9.375  -7.5    -5.625  -3.75
  -1.875   0.      1.875   3.75    5.625   7.5     9.375  11.25   13.125
  15.     16.875  18.75 ]

In [7]:
from matplotlib import pyplot as plt
from cartopy import config
from cartopy.util import add_cyclic_point
import cartopy.crs as ccrs
from numpy import meshgrid
# to show the plots inline
%matplotlib inline

In [8]:
lons, lats = meshgrid(lon, lat)

# plot first time stepp:
ax = plt.axes(projection=ccrs.Robinson(central_longitude=0))
ax.coastlines()
cs = plt.contourf(lons, lats, psl[0,:,:], 60, transform=ccrs.PlateCarree(), interpolation='nearest')
plt.colorbar()


Out[8]:
<matplotlib.colorbar.Colorbar at 0x7ff924238490>

In [9]:
cs = plt.contourf(lons, lats, psl[0,:,:])



In [10]:
# same stepps with ocgis
from flyingpigeon.ocgis_module import call 

tic = time.time()
#from ocgis import RequestDataset ,OcgOperations
spatial_wrapping = 'wrap' # unwrap # None
nc = call(resources, geom=bbox_ocgis, spatial_wrapping=spatial_wrapping, dir_output=HOME+'/data/tests')
tac = time.time()
print 'sec: %s' % (tac-tic)


sec: 15.3538041115

In [11]:
#read in the data 
var = get_variable(nc)
#print 'variable name: %s' % var
ds = Dataset(nc)
psl = ds.variables[var]
lat = ds.variables['lat']
lon = ds.variables['lon']

In [12]:
print lon[:]


[-18.75  -16.875 -15.    -13.125 -11.25   -9.375  -7.5    -5.625  -3.75
  -1.875   0.      1.875   3.75    5.625   7.5     9.375  11.25   13.125
  15.     16.875  18.75 ]

In [13]:
lons, lats = meshgrid(lon, lat)

# plot first time step:
ax = plt.axes(projection=ccrs.Robinson(central_longitude=0))
ax.coastlines()
cs = plt.contourf(lons, lats, psl[0,:,:], 60, transform=ccrs.PlateCarree(), interpolation='nearest')
plt.colorbar()


Out[13]:
<matplotlib.colorbar.Colorbar at 0x7ff92376f250>

In [14]:
cs = plt.contourf(lons, lats, psl[0,:,:])



In [15]:
lat[:]


Out[15]:
array([-21.45047569, -19.58521843, -17.71996117, -15.8547039 ,
       -13.98944569, -12.12418747, -10.2589283 ,  -8.39366913,
        -6.52840948,  -4.66314983,  -2.79788971,  -0.93263   ,
         0.93262988,   2.79788995,   4.66314983,   6.52840948,
         8.39366913,  10.2589283 ,  12.12418747,  13.98944569,
        15.8547039 ,  17.71996117,  19.58521843])

In [16]:
from flyingpigeon import subset as sb
# from flyingpigeon.subset import clipping

In [ ]:


In [17]:
africa = sb.clipping(resource=resources, 
                     #historical_concatination=True,
                     prefix='test_africa', 
                     spatial_wrapping='wrap', 
                     polygons='Africa', 
                     dir_output=HOME+'/data/tests/')

In [ ]:


In [18]:
var = get_variable(africa[0])
#print 'variable name: %s' % var
ds = Dataset(africa[0])
psl = ds.variables[var]
lat = ds.variables['lat']
lon = ds.variables['lon']

In [19]:
lons, lats = meshgrid(lon, lat)

# plot first time stepp:
ax = plt.axes(projection=ccrs.Robinson(central_longitude=0))
ax.coastlines()
cs = plt.contourf(lons, lats, psl[0,:,:], 60, transform=ccrs.PlateCarree(), interpolation='nearest')
plt.colorbar()


Out[19]:
<matplotlib.colorbar.Colorbar at 0x7ff923520e10>

In [20]:
lon[:]


Out[20]:
array([-16.875, -15.   , -13.125, -11.25 ,  -9.375,  -7.5  ,  -5.625,
        -3.75 ,  -1.875,   0.   ,   1.875,   3.75 ,   5.625,   7.5  ,
         9.375,  11.25 ,  13.125,  15.   ,  16.875,  18.75 ,  20.625,
        22.5  ,  24.375,  26.25 ,  28.125,  30.   ,  31.875,  33.75 ,
        35.625,  37.5  ,  39.375,  41.25 ,  43.125,  45.   ,  46.875,
        48.75 ,  50.625])

In [21]:
cs = plt.contourf(lons, lats, psl[0,:,:])



In [22]:
from flyingpigeon import utils


res_rot = HOME+'/birdhouse/flyingpigeon/flyingpigeon/tests/testdata/cordex/tasmax_EUR-44_MPI-M-MPI-ESM-LR_rcp45_r1i1p1_MPI-CSC-REMO2009_v1_mon_200602-200612.nc' 

europe = sb.clipping(resource=res_rot, historical_concatination=True, memory_limit=1, 
            prefix='test_europe', spatial_wrapping='wrap', polygons='Europe', dir_output=HOME+'/data/tests/')
europe


/home/nils/.conda/envs/flyingpigeon/lib/python2.7/site-packages/ocgis/interface/base/crs.py:755: MaskedArrayFutureWarning: setting an item on a masked array which has a shared mask will not copy the mask and also change the original mask array in the future.
Check the NumPy 1.11 release notes for more information.
  new_value[0, :, :] = new_row
Out[22]:
['/home/nils/data/tests/test_europe.nc']

In [31]:
lats, lons = utils.unrotate_pole(europe[0], write_to_file=False)
var = get_variable(europe[0])

ds = Dataset(europe[0])
val = np.squeeze(ds.variables[var])



# plot first time stepp:
ax = plt.axes(projection=ccrs.PlateCarree()) #Robinson(central_longitude=0)
ax.coastlines()
cs = plt.contourf(lons, lats, val[0,:,:], 60, transform=ccrs.PlateCarree(), interpolation='nearest',
                  )
plt.colorbar()
ds.close()



In [ ]:
val_m = np.ma.array(val[:], mask=val[:] > 1000)
cs = plt.contourf(lons, lats, val_m[0,:,:], 60, vmin=200, vmax=300)
ticks = np.linspace(200,300, num=11, endpoint=True)
cb =plt.colorbar(ticks=ticks, )
cb.vmin=200
cb.vmax=300