Graph: Python

Terms and conditions of use

We have no responsibility or liability for any unintended consequences or damages that the use of the following information may cause. Please use them at your own risk.

Installation: Linux & Mac

Python with "numpy", "matplotlib", "pandas", "netCDF4", "basemap" etc.
How to install Python

Utilities

To compute climatology

Examples

Time series

[Note]
* Temperature time series (HadCRUT5) were downloaded from here in August 2021.
* Morice, C.P., Kennedy, J.J., Rayner, N.A., Winn, J.P., Hogan, E., Killick, R.E., Dunn, R.J.H., Osborn, T.J., Jones, P.D., and Simpson, I.R., 2021: An updated assessment of near-surface temperature change from 1850: the HadCRUT5 dataset. Journal of Geophysical Research, doi:10.1029/2019JD032361.

Zonal mean plot

Longitude-Latitude contour

Latitude-Pressure contour

Polar stereographic plot

[Note]
* Sea ice concentration data (HadISST.2.2.0.0) were downloaded from here in June 2018.
* Titchner, H.A., and N.A. Rayner, 2014: The Met Office Hadley Centre sea ice and sea surface temperature data set, version 2: 1. Sea ice concentrations, J. Geophys. Res. Atmos., 119, 2864-2889, doi:10.1002/2013JD020316.

Python tips

To read CSV file

fin = "sample.csv"                   # specify input file name
d = pd.read_csv(fin, header=None)    # read data without header
# d = pd.read_csv(fin, header=None)  # read data with 1-line header
x = d.values.T[0,:]  # pandas to ndarray for the 1st column
y = d.values.T[1,:]  # pandas to ndarray for the 2nd column

Not to export display

import matplotlib as mpl
mpl.use('Agg')

NetCDF tips

To read netCDF file with "netCDF4"

from netCDF4 import Dataset
fin = "sample.nc"
f = Dataset(fin, 'r', format='NETCDF4')
data = f.variables['t2m']
time = f.variables['time']
lat = f.variables['latitude']
lon = f.variables['longitude']

To read netCDF file with "PyNio"

import Nio
fin = "sample.nc"
f = Nio.open_file(fin, "r")
data = f.variables['t2m'][:,:,:]
time = f.variables['time'][:]
lat = f.variables['latitude'][:]
lon = f.variables['longitude'][:]

To read netCDF file with "CDMS"

import cdms2 as cdms
fin = "sample.nc"
f = cdms.open(fin)
data = f('t2m')
time = data.getTime()[:]
lat = data.getLatitude()[:]
lon = data.getLongitude()[:]

To compute seasonal mean from monthy mean

import cdutil
# annual mean
wgt = np.array([31., 28., 31., 30., 31., 30., 31., 31., 30., 31., 30., 31.])
data_ann = cdutil.averager(data, axis=0, weights=wgt)
#data_ann = np.average(datai, axis = 0, weights = wgt)
#data_ann = np.ma.average(datai, axis = 0, weights = wgt)
# DJF mean
wgt = np.array([31., 28., 0., 0., 0., 0., 0., 0., 0., 0., 0., 31.])
data_djf = cdutil.averager(data, axis=0, weights=wgt)

Others

import pyspharm    # NCAR SPHEREPACK library
import windspharm  # wind-related fields (https://ajdawson.github.io/windspharm/latest/)
from cdo import *  # CDO in Python (https://code.mpimet.mpg.de/projects/cdo/wiki/Cdo%7Brbpy%7D)