In [1]:
    
# Needed in IPython notebook only
from IPython.display import Image   
%matplotlib inline
from nansat import *
    
In [ ]:
    
# open data on THREDDS
n1 = Nansat('https://rsg.pml.ac.uk/thredds/dodsC/CCI_ALL-v2.0-MONTHLY', date='2010-06-01')
# 
d = Domain(NSR().wkt, '-te -20 50 10 70 -tr 0.01 0.01')
n.reproject(d)
# get matrix with chlorophyll and show
chlor_a1 = n1['chlor_a']
plt.figure(figsize=(7,7))
plt.imshow(chlor_a1)
    
    
In [4]:
    
# create map from the Nansat object
nmap = Nansatmap(n1, resolution='l')
# make plot chlrophyll (color) and add colorbar
nmap.pcolormesh(chlor_a1, vmin=0, vmax=0.5)
nmap.add_colorbar(fontsize=10)
# add continents on top
nmap.draw_continents()
# draw grid of parallels and meridians
# at given locations
nmap.drawparallels([-65, -55, -45, -35], labels=[1,0,0,0])
nmap.drawmeridians([-5, 0, 5, 10, 15, 20, 25], labels=[0,0,1,0])
# add title
plt.suptitle('Chlorophyll-a, 1 Oct 2010')
# set size of the figure (inches)
nmap.fig.set_figheight(8)
nmap.fig.set_figwidth(8)
# save figure to a PNG file
nmap.save('usecase4a.png')
    
    
In [5]:
    
# get grids of longitude and latitude
lon, lat = n1.get_geolocation_grids()
# calculate zonal average (i.e. averae chlorophyll along parallels)
zonalAverage = np.nanmean(chlor_a1, axis=1)
# simple plot
plt.plot(zonalAverage, lat[:, 0], '.')
plt.xlabel('chlorophyll-a')
plt.ylabel('latitude')
    
    Out[5]:
    
In [6]:
    
# example of datetime library
import datetime
dt0 = datetime.datetime(2010,1,1)
dt1 = dt0 + datetime.timedelta(8)
print dt0, dt1
print dt0.strftime('%Y-%m-%d')
    
    
In [7]:
    
period = 100 # days
step = 8     # days
dt0 = datetime.datetime(2010,1,1)
# typical for other languages
#dates = []
#for d in range(0, period, step):
#    dt1 = dt0 + datetime.timedelta(d)
#    dates.append(dt1)
# list comprehensions are VERY convenient
# list of dates
dates = [dt0 + datetime.timedelta(d) for d in range(0, period, step)]
# list of strings representing dates
dateStrings = [date.strftime('%Y-%m-%d') for date in dates]
print dateStrings
    
    
In [8]:
    
# list of filenames
filenames = ['occci_online:8D:chlor_a:%s' % date for date in dateStrings]
print filenames
    
    
In [9]:
    
# define smaller area of interest
lons=[10, 20]
lats=[-55, -50]
chloraArrays = []
for filename in filenames:
    print filename
    n = Nansat(filename, lons=lons, lats=lats)
    chlor_a = n[1]
    chloraArrays.append(chlor_a)
# equal to
#chloraArrays = [Nansat(filename, lons=lons, lats=lats)[1] for filename in filenames]
# but we want to print filename for logging
    
    
    
    
    
    
    
    
    
In [10]:
    
# convert list of arrays into 3D array
chloraArrays = np.array(chloraArrays)
print chloraArrays.shape
    
    
In [11]:
    
# calculate zonal averages for each time step
zonalAverages = np.nanmean(chloraArrays, axis=1)
print zonalAverages.shape
# show the matrix
plt.figure(figsize=(10,10))
plt.imshow(zonalAverages.T,
           interpolation='nearest',
           aspect=.15,
           extent=[0, len(dates), lat.min(), lat.max()],
           vmin=0,
           vmax=0.6)
ticks = plt.xticks(range(0,len(dates),2), dateStrings[::2])
    
    
    
In [12]:
    
# calculate average and standard deviation
chlorAvg = np.nanmean(zonalAverages, axis=1)
chlorStd = np.nanstd(zonalAverages, axis=1)
    
In [13]:
    
# plot as error bar
plt.figure(figsize=(9,9))
plt.errorbar(dates, chlorAvg, chlorStd, fmt='o-')
plt.xlabel('dates')
plt.ylabel('chlorophyll-a')
    
    Out[13]:
    
In [ ]: