Notes Python5
Notes Python5
Data preparation
Data processing and visualization
Jonathon S. Wright
21 March, 2017
Original paper
Data preparation
Data processing and visualization
Original paper
Introduction
Results
Discussion
Data preparation
The netCDF4 module
The datetime module
Pre-processing
Motivation
I Tropical cyclones in Western North Pacific account for about
one third of all TCs
I Changes in TC genesis location could affect billions of people
Figure 1. Monthly mean 200hPa wind field (vectors, m s1 ) and vertical shear
of zonal wind between 850 hPa and 200 hPa (shaded, m s1 ) during
JuneNovember. Blue dots indicate TC genesis locations and thick green lines
show the TUTT trough line.
Figure 2. Time series of (a) annual mean TC genesis longitude (blue) from the
JTWC dataset and annual mean TUTT longitude (red), and (b) annual mean
TC longitude from the ADT-HURSAT data set with (blue) and without (red)
the ENSO effect.
Figure 4. (a) JulyNovember mean zonal wind speed (contour, m s1 ) and the
associated trends (shaded, m s1 decade1 ) averaged over 5 N25 N during
19792012 and (b) JulyNovember mean temperature (contour, K) and the
associated trend (shaded, K decade1 ) averaged over 145 E170 W.
We then need to find the genesis locations, which in this case are
defined as the first time that the maximum windspeed exceeded 25
knots (13 m s1 ):
1 i m p o r t numpy as np
2
3 # loop through storms and find genesis locations
4 cgx = []; cgy = []; cgm = []
5 # start with all tracks that include cyclogenesis in the WP
6 sdx = np . where ((( ttyp == 0) | ( ttyp == 1) ) & ( genb == 2) ) [0]
7 # note that loop is over indices, not over a range!
8 f o r ss i n sdx :
9 # check to see if the wind ever exceeds 25 knots
10 i f np . any ( tc_v [ ss ,:] >= 25) :
11 cg = np . where ( tc_v [ ss ,:] >= 25) [0][0]
12 # append lat/lon/month of cyclogenesis
13 cgy . append ( tc_y [ ss , cg ])
14 cgx . append ( tc_x [ ss , cg ])
15 cgm . append ( mjd2month ( tc_t [ ss , cg ]) )
16 # convert to arrays
17 cgy = np . array ( cgy )
18 cgx = np . array ( cgx )
19 cgm = np . array ( cgm )
20 # longitude in IBTraCS is (180,180); convert to (0,360)
21 cgx [ cgx < 0] += 360
Original paper The netCDF4 module
Data preparation The datetime module
Data processing and visualization Pre-processing
We also need some reanalysis data for the winds and temperatures.
Here, we will use the JRA-55 reanalysis, which was not used in the
original paper. The data files are quite large, and it is therefore
convenient to preprocess the files to make them manageable. To
do this, I use the Climate Data Operators (CDO) utilities
developed at the Max-Planck Institut fur Meteorologie. First, I
select the temperature and wind data for June through November
using the selmon (select month) command:
cdo selmon,6,7,8,9,10,11 jra55nl_tmp_monthly_1958-2015.nc4 tmp_jjason.nc4
cdo selmon,6,7,8,9,10,11 jra55nl_ugrd_monthly_1958-2015.nc4 uwd_jjason.nc4
cdo selmon,6,7,8,9,10,11 jra55nl_vgrd_monthly_1958-2015.nc4 vwd_jjason.nc4
and then select the 200 and 850 hPa levels, removing the rest of
the vertical profile (recall that we only need 850 hPa zonal wind to
calculate vertical shear, and do not need 850 hPa meridional wind):
cdo sellevel,85000,20000 uwd_jjason_mm.nc4 fig1_uwd.nc4
cdo sellevel,20000 vwd_jjason_mm.nc4 fig1_vwd.nc4
Original paper The netCDF4 module
Data preparation The datetime module
Data processing and visualization Pre-processing
We can then read in the data and calculate the zonal wind shear:
1 from netCDF4 i m p o r t Dataset
2
3 # read in JRA55 wind data
4 ncdf = Dataset ( ddir + fig1_uwd . nc4 )
5 jlon = ncdf . variables [ lon ][:]
6 jlat = ncdf . variables [ lat ][:]
7 u850 = ncdf . variables [ ugrd ][: ,0 ,: ,:]
8 u200 = ncdf . variables [ ugrd ][: ,1 ,: ,:]
9 ncdf . close ()
10 ncdf = Dataset ( ddir + fig1_vwd . nc4 )
11 v200 = ncdf . variables [ vgrd ][: ,0 ,: ,:]
12 ncdf . close ()
13 # calculate vertical wind shear
14 ushr = u850 - u200
Original paper Mapping: the cartopy module
Data preparation Linear regression and correlations
Data processing and visualization More contour plots and masked arrays
30N
20N
10N
0
100E 120E 140E 160E 180 160W 140W 120W
40N
June 40N
July
30N 30N
20N 20N
10N 10N
0 0
100E 120E 140E 160E 180 160W 140W 120W 100E 120E 140E 160E 180 160W 140W 120W
40N
August 40N
September
30N 30N
20N 20N
10N 10N
0 0
100E 120E 140E 160E 180 160W 140W 120W 100E 120E 140E 160E 180 160W 140W 120W
40N
October 40N
November
30N 30N
20N 20N
10N 10N
0 0
100E 120E 140E 160E 180 160W 140W 120W 100E 120E 140E 160E 180 160W 140W 120W
Original paper Mapping: the cartopy module
Data preparation Linear regression and correlations
Data processing and visualization More contour plots and masked arrays
1 sns . regplot ( df . index , df [ jtwc ] , color = #377 eb8 , ax = axa , truncate = True )
2 sns . regplot ( df . ix [1979:]. index , df . ix [1979:][ jtwc ] , color = #377 eb8 ,
3 line_kws ={ linestyle : -- } , truncate = True , ci = None , ax = axa )
150 190
TC mean genesis longitude [E]
145 180
135 160
130 150
R = 0.64
125 140
1960 1970 1980 1990 2000 2010
Original paper Mapping: the cartopy module
Data preparation Linear regression and correlations
Data processing and visualization More contour plots and masked arrays
160
JTWC (original)
JTWC (ENSO influence removed)
TC mean genesis longitude [E]
150
140
130
120
1960 1970 1980 1990 2000 2010
Year
20
TC count
15
10
5
R = 0.27
0
1960 1970 1980 1990 2000 2010
Original paper Mapping: the cartopy module
Data preparation Linear regression and correlations
Data processing and visualization More contour plots and masked arrays
The actual subtraction of 850 hPa u from 200 hPa u is left for the
python code.
Original paper Mapping: the cartopy module
Data preparation Linear regression and correlations
Data processing and visualization More contour plots and masked arrays
2.1 14
1.8 12
Temperature gradient [K]
1.2 8
0.9 6
0.6 4
R = 0.97
0.3 2
0.0 0
1960 1970 1980 1990 2000 2010
Year
Our function returns both the slope of the trend and a boolean
(True or False) value indicating whether it is significant or not,
which we can use in tandem with numpy masked arrays:
1 i m p o r t pandas as pd
2
3 tru = np . ma . masked_all ( mm_u . shape [1:])
4 trt = np . ma . masked_all ( zm_t . shape [1:])
5 f o r zz i n r a n g e ( tru . shape [0]) :
6 f o r xx i n r a n g e ( tru . shape [1]) :
7 ser = pd . Series ( mm_u [: , zz , xx ])
8 m , s = pdtrend ( ser )
9 tru . data [ zz , xx ] = m *10 # convert trend to per decade
10 tru . mask [ zz , xx ] = ~ s
11 f o r yy i n r a n g e ( trt . shape [1]) :
12 ser = pd . Series ( zm_t [: , zz , yy ])
13 m , s = pdtrend ( ser )
14 trt . data [ zz , yy ] = m *10 # convert trend to per decade
15 trt . mask [ zz , yy ] = ~ s
Masked arrays make it convenient to emphasize significant trends
or hide insignificant trends in the contour plots.
Original paper Mapping: the cartopy module
Data preparation Linear regression and correlations
Data processing and visualization More contour plots and masked arrays
1 cs1 = ax . contour ( jlon , jlev , mm_u . mean ( axis =0) , np . arange ( -50 ,50 ,2) ,
2 colors = k )
3 cs2 = ax . contourf ( jlon , jlev , tru . data , np . linspace ( -1 ,1 ,11) ,
4 cmap = plt . cm . RdBu_r , extend = both )
5 ax . contourf ( jlon , jlev , tru . mask . astype ( int ) , [ -0.5 ,0.5] ,
6 hatches =[ xx , none ] , colors = none , edgecolor = #666666 ,
7 zorder =10)
8 cb = plt . colorbar ( cs2 , orientation = vertical , extend = both , aspect =50)
9 cb . set_ticks ([ -1 , -0.5 ,0 ,0.5 ,1])
10 cb . set_label ( Trend in zonal wind [ m s$ ^{ -1} $ dec$ ^{ -1} $ ] )
100 100
1.0 0.5
0.4
150 150
0.3
0.5
200 200
300
0.0 Pressure [hPa] 300
0.0
700 700
0.4
850 850
1.0 0.5
1000 1000
100E 120E 140E 160 180 45S 30S 15S 0 15N 30N 45N
Original paper Mapping: the cartopy module
Data preparation Linear regression and correlations
Data processing and visualization More contour plots and masked arrays
We can also limit our trend analysis to only the period after 1979,
in which case our results are more similar to those in the paper.
1 ncdf = Dataset ( ddir + fig4_uwd_pacmm . nc4 )
2 jlev = ncdf . variables [ lev ][:27]*0.01
3 jlon = ncdf . variables [ lon ][:]
4 mm_u = np . squeeze ( ncdf . variables [ ugrd ][21: ,:27 ,: ,:])
5 ncdf . close ()
100 100
1.0 0.5
0.4
150 150
0.3
0.5
200 200
300
0.0 Pressure [hPa] 300
0.0
700 700
0.4
850 850
1.0 0.5
1000 1000
100E 120E 140E 160 180 45S 30S 15S 0 15N 30N 45N