example1 - cms.ncas.ac.ukcms.ncas.ac.uk/documents/training/March2017/example1.pdf · example1 May...

16
example1 May 5, 2017 1 Analysis and visualisation with cf-python and cf-plot 1.1 Import, read, inspect, select, write In [1]: import cf import cfplot as cfp In [2]: cf.__version__, cfp.__version__ Out[2]: ('1.2.1', '2.1.10') In [3]: f = cf.read('scicomp2/data1.nc') In [4]: f Out[4]: [<CF Field: long_name:Potential vorticity(time(1), pressure(23), latitude(1 <CF Field: long_name:Ozone mass mixing ratio(time(1), pressure(23), latitu <CF Field: air_temperature(time(1), pressure(23), latitude(160), longitude <CF Field: atmosphere_relative_vorticity(time(1), pressure(23), latitude(1 <CF Field: atmosphere_relative_vorticity(time(1), pressure(23), latitude(1 <CF Field: divergence_of_wind(time(1), pressure(23), latitude(160), longit <CF Field: divergence_of_wind(time(1), pressure(23), latitude(160), longit <CF Field: eastward_wind(time(1), pressure(23), latitude(160), longitude(3 <CF Field: geopotential(time(1), pressure(23), latitude(160), longitude(32 <CF Field: northward_wind(time(1), pressure(23), latitude(160), longitude( <CF Field: relative_humidity(time(1), pressure(23), latitude(160), longitu <CF Field: specific_humidity(time(1), pressure(23), latitude(160), longitu <CF Field: vertical_air_velocity_expressed_as_tendency_of_pressure(time(1) In [5]: temp = f.select('air_temperature') print temp air_temperature field summary ----------------------------- Data : air_temperature(time(1), pressure(23), latitude(160), longitude(32 Axes : time(1) = [1964-01-21 00:00:00] : pressure(23) = [1000.0, ..., 1.0] mbar : latitude(160) = [89.1415176392, ..., -89.1415176392] degrees_north 1

Transcript of example1 - cms.ncas.ac.ukcms.ncas.ac.uk/documents/training/March2017/example1.pdf · example1 May...

Page 1: example1 - cms.ncas.ac.ukcms.ncas.ac.uk/documents/training/March2017/example1.pdf · example1 May 5, 2017 1 Analysis and visualisation with cf-python and cf-plot 1.1 Import, read,

example1

May 5, 2017

1 Analysis and visualisation with cf-python and cf-plot

1.1 Import, read, inspect, select, write

In [1]: import cfimport cfplot as cfp

In [2]: cf.__version__, cfp.__version__

Out[2]: ('1.2.1', '2.1.10')

In [3]: f = cf.read('scicomp2/data1.nc')

In [4]: f

Out[4]: [<CF Field: long_name:Potential vorticity(time(1), pressure(23), latitude(160), longitude(320)) K m**2 kg**-1 s**-1>,<CF Field: long_name:Ozone mass mixing ratio(time(1), pressure(23), latitude(160), longitude(320)) kg kg**-1>,<CF Field: air_temperature(time(1), pressure(23), latitude(160), longitude(320)) K>,<CF Field: atmosphere_relative_vorticity(time(1), pressure(23), latitude(160), longitude(320)) m**2 s**-1>,<CF Field: atmosphere_relative_vorticity(time(1), pressure(23), latitude(160), longitude(320)) s**-1>,<CF Field: divergence_of_wind(time(1), pressure(23), latitude(160), longitude(320)) s**-1>,<CF Field: divergence_of_wind(time(1), pressure(23), latitude(160), longitude(320)) m**2 s**-1>,<CF Field: eastward_wind(time(1), pressure(23), latitude(160), longitude(320)) m s**-1>,<CF Field: geopotential(time(1), pressure(23), latitude(160), longitude(320)) m**2 s**-2>,<CF Field: northward_wind(time(1), pressure(23), latitude(160), longitude(320)) m s**-1>,<CF Field: relative_humidity(time(1), pressure(23), latitude(160), longitude(320)) %>,<CF Field: specific_humidity(time(1), pressure(23), latitude(160), longitude(320)) kg kg**-1>,<CF Field: vertical_air_velocity_expressed_as_tendency_of_pressure(time(1), pressure(23), latitude(160), longitude(320)) Pa s**-1>]

In [5]: temp = f.select('air_temperature')print temp

air_temperature field summary-----------------------------Data : air_temperature(time(1), pressure(23), latitude(160), longitude(320)) KAxes : time(1) = [1964-01-21 00:00:00]

: pressure(23) = [1000.0, ..., 1.0] mbar: latitude(160) = [89.1415176392, ..., -89.1415176392] degrees_north

1

Page 2: example1 - cms.ncas.ac.ukcms.ncas.ac.uk/documents/training/March2017/example1.pdf · example1 May 5, 2017 1 Analysis and visualisation with cf-python and cf-plot 1.1 Import, read,

: longitude(320) = [0.0, ..., 358.875] degrees_east

In [6]: temp.properties

Out[6]: {'Conventions': 'CF-1.5','_FillValue': 2e+20,'date': '21/01/64','history': 'Sun Sep 16 11:26:16 BST 2012 - CONVSH V1.92 16-February-2006','long_name': 'Temperature','missing_value': 2e+20,'name': 'T','source': 'GRIB data','standard_name': 'air_temperature','time': '00:00','title': 'Temperature','valid_max': 312.29285,'valid_min': 188.33594}

In [7]: temp.standard_name = 'cheese'print temp

cheese field summary--------------------Data : cheese(time(1), pressure(23), latitude(160), longitude(320)) KAxes : time(1) = [1964-01-21 00:00:00]

: pressure(23) = [1000.0, ..., 1.0] mbar: latitude(160) = [89.1415176392, ..., -89.1415176392] degrees_north: longitude(320) = [0.0, ..., 358.875] degrees_east

In [8]: cf.write(temp, 'newfile.nc')

Commands starting with ! are shell commands

In [9]: !ls -o newfile.nc

-rw-r--r-- 1 david 4713704 May 5 13:07 newfile.nc

1.2 Contour plots

In [10]: # Inline images in Ipython Notebook%matplotlib inline

In [11]: t_500 = temp.subspace(Z=500)print t_500

2

Page 3: example1 - cms.ncas.ac.ukcms.ncas.ac.uk/documents/training/March2017/example1.pdf · example1 May 5, 2017 1 Analysis and visualisation with cf-python and cf-plot 1.1 Import, read,

cheese field summary--------------------Data : cheese(time(1), pressure(1), latitude(160), longitude(320)) KAxes : time(1) = [1964-01-21 00:00:00]

: pressure(1) = [500.0] mbar: latitude(160) = [89.1415176392, ..., -89.1415176392] degrees_north: longitude(320) = [0.0, ..., 358.875] degrees_east

In [12]: cfp.con(t_500)

In [13]: cfp.mapset(lonmin=-30, lonmax=30, latmin=20, latmax=70)cfp.con(t_500)

3

Page 4: example1 - cms.ncas.ac.ukcms.ncas.ac.uk/documents/training/March2017/example1.pdf · example1 May 5, 2017 1 Analysis and visualisation with cf-python and cf-plot 1.1 Import, read,

In [14]: cfp.mapset(proj='npstere')cfp.con(t_500)

4

Page 5: example1 - cms.ncas.ac.ukcms.ncas.ac.uk/documents/training/March2017/example1.pdf · example1 May 5, 2017 1 Analysis and visualisation with cf-python and cf-plot 1.1 Import, read,

In [15]: u = f.select('eastward_wind')u

Out[15]: <CF Field: eastward_wind(time(1), pressure(23), latitude(160), longitude(320)) m s**-1>

In [16]: u_mean = u.collapse('mean', 'longitude')u_mean

Out[16]: <CF Field: eastward_wind(time(1), pressure(23), latitude(160), longitude(1)) m s**-1>

In [17]: cfp.reset()cfp.con(u_mean)

5

Page 6: example1 - cms.ncas.ac.ukcms.ncas.ac.uk/documents/training/March2017/example1.pdf · example1 May 5, 2017 1 Analysis and visualisation with cf-python and cf-plot 1.1 Import, read,

In [18]: cfp.con(u_mean, ylog=True)

6

Page 7: example1 - cms.ncas.ac.ukcms.ncas.ac.uk/documents/training/March2017/example1.pdf · example1 May 5, 2017 1 Analysis and visualisation with cf-python and cf-plot 1.1 Import, read,

1.3 Vector plots

In [19]: u = f.select('eastward_wind').subspace(pressure=500)v = f.select('northward_wind').subspace(pressure=500)print uprint v

eastward_wind field summary---------------------------Data : eastward_wind(time(1), pressure(1), latitude(160), longitude(320)) m s**-1Axes : time(1) = [1964-01-21 00:00:00]

: pressure(1) = [500.0] mbar: latitude(160) = [89.1415176392, ..., -89.1415176392] degrees_north: longitude(320) = [0.0, ..., 358.875] degrees_east

northward_wind field summary----------------------------Data : northward_wind(time(1), pressure(1), latitude(160), longitude(320)) m s**-1Axes : time(1) = [1964-01-21 00:00:00]

: pressure(1) = [500.0] mbar: latitude(160) = [89.1415176392, ..., -89.1415176392] degrees_north: longitude(320) = [0.0, ..., 358.875] degrees_east

In [20]: cfp.mapset(proj='npstere')cfp.vect(u=u, v=v, key_length=10, scale=100, pts=50)

7

Page 8: example1 - cms.ncas.ac.ukcms.ncas.ac.uk/documents/training/March2017/example1.pdf · example1 May 5, 2017 1 Analysis and visualisation with cf-python and cf-plot 1.1 Import, read,

1.4 Line plots

In [21]: u = f.select('eastward_wind')u_mean = u.collapse('X: mean')u_mean_100 = u_mean.subspace(pressure=100)print u_mean_100

eastward_wind field summary---------------------------Data : eastward_wind(time(1), pressure(1), latitude(160), longitude(1)) m s**-1Cell methods : longitude: meanAxes : longitude(1) = [179.4375] degrees_east

8

Page 9: example1 - cms.ncas.ac.ukcms.ncas.ac.uk/documents/training/March2017/example1.pdf · example1 May 5, 2017 1 Analysis and visualisation with cf-python and cf-plot 1.1 Import, read,

: time(1) = [1964-01-21 00:00:00]: pressure(1) = [100.0] mbar: latitude(160) = [89.1415176392, ..., -89.1415176392] degrees_north

In [22]: cfp.lineplot(u_mean_100, marker='o', color='blue', title='Zonal mean zonal wind at 100mb')

In [23]: cfp.gset(xmin=-30, xmax=30, ymin=-6, ymax=30)cfp.lineplot(u_mean_100, marker='s', color='red', title='Tropical zonal mean zonal wind ')

9

Page 10: example1 - cms.ncas.ac.ukcms.ncas.ac.uk/documents/training/March2017/example1.pdf · example1 May 5, 2017 1 Analysis and visualisation with cf-python and cf-plot 1.1 Import, read,

1.5 Regridding

In [24]: temp_era40 = cf.read('scicomp2/data2.nc')temp_era_in = cf.read('scicomp2/data3.nc')temp_era40, temp_era_in

Out[24]: (<CF Field: air_temperature(long_name:t(1), long_name:p(1), latitude(160), longitude(320)) K>,<CF Field: air_temperature(long_name:t(1), long_name:p(1), long_name:latitude(256), long_name:longitude(512)) K>)

In [25]: temp_regrid = temp_era_in.regrids(temp_era40, method='bilinear')temp_regrid

Out[25]: <CF Field: air_temperature(long_name:t(1), long_name:p(1), latitude(160), longitude(320)) K>

Multiple plots in one page

In [26]: cfp.reset()cfp.gopen(rows=3, columns=1, orientation='portrait')

cfp.gpos(1)cfp.con(temp_era40, title='era40')

10

Page 11: example1 - cms.ncas.ac.ukcms.ncas.ac.uk/documents/training/March2017/example1.pdf · example1 May 5, 2017 1 Analysis and visualisation with cf-python and cf-plot 1.1 Import, read,

cfp.gpos(2)cfp.con(temp_era_in, title='era interim')

cfp.gpos(3)cfp.con(temp_regrid-temp_era40, lines=False, title='era_interim - era40 difference')

cfp.gclose()

11

Page 12: example1 - cms.ncas.ac.ukcms.ncas.ac.uk/documents/training/March2017/example1.pdf · example1 May 5, 2017 1 Analysis and visualisation with cf-python and cf-plot 1.1 Import, read,

12

Page 13: example1 - cms.ncas.ac.ukcms.ncas.ac.uk/documents/training/March2017/example1.pdf · example1 May 5, 2017 1 Analysis and visualisation with cf-python and cf-plot 1.1 Import, read,

1.6 Timeseries of annual average global temperature

In [27]: f = cf.read('scicomp2/data4.nc')print f

air_temperature field summary-----------------------------Data : air_temperature(time(1680), latitude(73), longitude(96)) KCell methods : time: meanAxes : height(1) = [2.0] m

: latitude(73) = [-90.0, ..., 90.0] degrees_north: longitude(96) = [0.0, ..., 356.25] degrees_east: time(1680) = [1860-01-16 00:00:00, ..., 1999-12-16 00:00:00] 360_day

In [28]: temp2 = f.subspace(time=cf.wi(cf.dt('1900-01-01'), cf.dt('1980-01-01')))print temp2

air_temperature field summary-----------------------------Data : air_temperature(time(960), latitude(73), longitude(96)) KCell methods : time: meanAxes : height(1) = [2.0] m

: latitude(73) = [-90.0, ..., 90.0] degrees_north: longitude(96) = [0.0, ..., 356.25] degrees_east: time(960) = [1900-01-16 00:00:00, ..., 1979-12-16 00:00:00] 360_day

In [29]: temp_annual = temp2.collapse('T: mean', group=cf.Y())print temp_annual

air_temperature field summary-----------------------------Data : air_temperature(time(80), latitude(73), longitude(96)) KCell methods : time: meanAxes : height(1) = [2.0] m

: latitude(73) = [-90.0, ..., 90.0] degrees_north: time(80) = [1900-07-01 00:00:00, ..., 1979-07-01 00:00:00] 360_day: longitude(96) = [0.0, ..., 356.25] degrees_east

In [30]: temp_annual_global = temp_annual.collapse('area: mean')print temp_annual_global

13

Page 14: example1 - cms.ncas.ac.ukcms.ncas.ac.uk/documents/training/March2017/example1.pdf · example1 May 5, 2017 1 Analysis and visualisation with cf-python and cf-plot 1.1 Import, read,

air_temperature field summary-----------------------------Data : air_temperature(time(80), latitude(1), longitude(1)) KCell methods : time: mean latitude: longitude: meanAxes : longitude(1) = [178.125] degrees_east

: height(1) = [2.0] m: latitude(1) = [0.0] degrees_north: time(80) = [1900-07-01 00:00:00, ..., 1979-07-01 00:00:00] 360_day

In [31]: cfp.reset()cfp.lineplot(temp_annual_global, title='Annual average global temperature', color='red')

Convert units from Kelvin to Celcius

In [32]: temp_annual_global.Units -= 273.15

In [33]: cfp.lineplot(temp_annual_global, title='Annual average global temperature', color='red')

14

Page 15: example1 - cms.ncas.ac.ukcms.ncas.ac.uk/documents/training/March2017/example1.pdf · example1 May 5, 2017 1 Analysis and visualisation with cf-python and cf-plot 1.1 Import, read,

1.7 Movies

In [34]: print f

air_temperature field summary-----------------------------Data : air_temperature(time(1680), latitude(73), longitude(96)) KCell methods : time: meanAxes : height(1) = [2.0] m

: latitude(73) = [-90.0, ..., 90.0] degrees_north: longitude(96) = [0.0, ..., 356.25] degrees_east: time(1680) = [1860-01-16 00:00:00, ..., 1999-12-16 00:00:00] 360_day

In [35]: cfp.levs(250, 310, 3) # Set the plot levels

for y in range(1860, 1870): # Loop over yearsprint y,

for m in range(1, 13): # Loop over months in each yearprint m,

15

Page 16: example1 - cms.ncas.ac.ukcms.ncas.ac.uk/documents/training/March2017/example1.pdf · example1 May 5, 2017 1 Analysis and visualisation with cf-python and cf-plot 1.1 Import, read,

g = f.subspace(time=cf.year(y) & cf.month(m)) # Find the data for this year/month

filename = 'tas_'+str(y)+'-'+str(m).rjust(2, '0')+'.png' # Create a file name for the plotcfp.setvars(file=filename)

cfp.con(g, title=g.coord('time').dtarray, lines=False) # Plot the data or this year/month

print ''

print 'Finished'

1860 1 2 3 4 5 6 7 8 9 10 11 121861 1 2 3 4 5 6 7 8 9 10 11 121862 1 2 3 4 5 6 7 8 9 10 11 121863 1 2 3 4 5 6 7 8 9 10 11 121864 1 2 3 4 5 6 7 8 9 10 11 121865 1 2 3 4 5 6 7 8 9 10 11 121866 1 2 3 4 5 6 7 8 9 10 11 121867 1 2 3 4 5 6 7 8 9 10 11 121868 1 2 3 4 5 6 7 8 9 10 11 121869 1 2 3 4 5 6 7 8 9 10 11 12Finished

In [ ]: !ls tas_*.png

In [ ]: !display tas_1860-01.png

In [ ]: !convert -loop 0 -size 1400x700 -delay 10 tas_*.png movie.mp4!ls -o movie.mp4

Crop with size and offset in pixels

In [ ]: !convert -loop 0 -size 1400x700 -delay 10 -crop 935x467+141+122 tas_*.png movie_cropped.mp4!ls -o movie_cropped.mp4

In [ ]:

In [ ]:

16