Skip to main content

np_exercise_annmean.py


"""
Solution for exercise: x-y plot
Author: L J Gregoire
"""
import numpy as np
import matplotlib.pyplot as plt
import datetime
###
# read in Bradford weather data
###
filename='bradforddata.txt'  # put full path to data if needed
data=np.genfromtxt(filename,
           skip_header=7,usecols=(0,1,2,3),
           missing_values='---')
mintemp=data[:,2]
maxtemp=data[:,3]
##########
# caculate mean monthly temperature
# as the average of min and max tempeartures
meantemp=(mintemp+maxtemp)/2.
print meantemp[:5]
##########
# calculate annual mean temperature
#Method 1: using a loop
def annmean1(date_years,timeseries):
    """ Calculate annual mean from a monthly time series dataset
    Method 1:
    Loop over the years
    subset monthly data for each year and average it.
    Input:
        date_years and timeseries should be the same lenght.
        date_years contains the year corresponding to each monthly value
        timeseries is the vector of monthly values
    Returns:
        - a vector of the unique years in the monthly data
        - a vector of the same lenght with annual mean for each year
    """
    allyears=date_years.astype('int') # all the years as integers
    uniqueyears=np.unique(allyears)  # only unique years, no repetition
    # or years=data[0::12,0].astype('int')  to pick year every 12 row
    L=[] # initialise empty list
    for yr in uniqueyears:
        yrmean=timeseries[allyears==yr].mean()
        L.append(yrmean)
    result=np.array(L) # turn list into array.
    return uniqueyears,result
(years1,annmeantemp1)=annmean1(data[:,0],meantemp)
print years1[:5]
print annmeantemp1[:5]
#Method 2: using array operations (shorter and easier)
def annmean2(date_years,timeseries):
    """ Calculate annual mean from a monthly time series dataset
    Method 2:
    reshape array into years x months
    make sure you've got the new shape and axis for the mean correct
    Input:
        date_years and timeseries should be the same lenght.
        date_years contains the year corresponding to each monthly value
        timeseries is the vector of monthly values
    Returns:
        - a vector of the unique years in the monthly data
        - a vector of the same lenght with annual mean for each year
    """
    result=timeseries.reshape(-1,12).mean(1)
    #annmeantemp2=meantemp.reshape(-1,12).mean(1)
    # calculate years in the same way as a test of the method
    uniqueyears=date_years.reshape(-1,12).mean(1)
    return uniqueyears,result
(years2,annmeantemp2)=annmean1(data[:,0],meantemp)
print years2[:5]
print annmeantemp2[:5]
##################
# Plotting results
# convert month and year into dates for the labels
# using the date function of the datetime module
dates=[datetime.date(day=15,month=int(data[i,1]),year=int(data[i,0])) for i in range(len(data[:,0]))]
plt.figure(1)
plt.plot(dates[-12:],mintemp[-12:],'b--',
         dates[-12:],maxtemp[-12:],'r--',
         dates[-12:],meantemp[-12:],'k-',)
plt.show()
# convert years into dates for plotting monthly and yearly data on same axis
# using the date function of the datetime module
yrdates=[datetime.date(day=15,month=6,year=int(yr)) for yr in years2]
plt.figure(2)
plt.plot(yrdates,annmeantemp2,'r-',dates,meantemp,'k:')
plt.show()