CoCalc Public Filesatms391geodata / Week5a / Homework Week 5 (answer key).ipynbOpen with one click!
Author: Steve Nesbitt
Views : 42
Compute Environment: Ubuntu 18.04 (Deprecated)

ATMS 391: Homework 5a


Problem 1

I have placed a file called precip.mon.total.nc, which is a netCDF file containing monthly averaged 5° lat-lon gridded precipitation data. Use xray to load the dataset, and matplotlib to plot the results of your calculations.

(a) Load in the data using xray, and plot the monthly time series of precipitation averaged globally.

In [6]:
%matplotlib inline import xray import numpy as np import matplotlib.pyplot as plt nc = xray.open_dataset('precip.mon.total.nc') var='precip' ncvar = nc[var] plt.plot(ncvar.time,np.mean(ncvar,axis=(1,2))) plt.xlabel('Year') plt.ylabel('Precipitation') plt.title("Monthly global time series of precipitation")
<matplotlib.text.Text at 0x7f449d9db810>

(b) Extract the time series over the nearest point over Champaign. Create a two panel plot. Plot the time series from 1900-2014 in the left panel, and the time series from 2000-2014 in the right panel.

In [7]:
nc_cmi=nc.sel(lon=[-88.9],lat=[40.], method='nearest') ncvar_cmi = nc_cmi[var] plt.figure(1,figsize=(11,5)) plt.subplot(121) plt.plot(ncvar_cmi.time,np.mean(ncvar_cmi,axis=(1,2)), 'k') plt.xlim(['1900-01-01','2014-12-31']) plt.subplot(122) plt.plot(ncvar_cmi.time,np.mean(ncvar_cmi,axis=(1,2)), 'r') plt.xlim(['2000-01-01','2014-12-31'])
(730120.0, 735598.0)

(c) Repeat (b), but average the data every year (January-December) before plotting.

In [8]:
nc_cmi_ann=nc_cmi.resample('12MS', dim='time', how='mean') plt.figure(1) plt.subplot(121) plt.plot(nc_cmi_ann['time'],np.squeeze(nc_cmi_ann[var]), 'k') plt.xlim(['1900-01-01','2014-12-31']) plt.subplot(122) plt.plot(nc_cmi_ann['time'],np.squeeze(nc_cmi_ann[var]), 'r') plt.xlim(['2000-01-01','2014-12-31'])
(730120.0, 735598.0)

Problem 2

For this problem, you will use the dataset we used in Exercise 2 in addition to the data in Problem 1.

(a) Create a yearly time series from 1901 to 2014 of annually-averaged temperature and precipitation globally, and at the grid point over Champaign-Urbana.

In [9]:
import datetime as dt import numpy as np date1=np.datetime64('1901-01-01') date2=np.datetime64('2014-12-31') #precipitation global t_series_precip = np.mean(ncvar,axis=(1,2)) precip_global = t_series_precip[(t_series_precip.time >= date1) & (t_series_precip.time <= date2)] precip_global_ann = precip_global.resample('12MS', dim='time', how='mean') #precipitation Champaign t_series_precip_cmi = np.mean(ncvar_cmi, axis=(1,2)) precip_cmi = t_series_precip_cmi[(t_series_precip_cmi.time >= date1) & (t_series_precip_cmi.time <= date2)] precip_cmi_ann = precip_cmi.resample('12MS', dim='time', how='mean') #temperature global nc_temp = xray.open_dataset('gistemp1200_ERSSTv4.nc') var_temp ='tempanomaly' ncvar_temp = nc_temp[var_temp] t_series_temp = np.mean(ncvar_temp,axis=(1,2)) temp_global = t_series_temp[(t_series_temp.time >= date1) & (t_series_temp.time <= date2)] temp_global_ann = temp_global.resample('12MS', dim='time', how='mean') #temperature Champaign ncvar_temp_cmi = nc_temp.sel(lon=[-88.9],lat=[40.], method='nearest')[var_temp] t_series_temp_cmi = np.mean(ncvar_temp_cmi,axis=(1,2)) temp_cmi = t_series_temp_cmi[(t_series_temp_cmi.time >= date1) & (t_series_temp_cmi.time <= date2)] temp_cmi_ann = temp_cmi.resample('12MS', dim='time', how='mean')

(b) Plot time series of each of these variables in a two panel plot, stacked vertically. Use symbols and lines for each time series. Do this globally and for the grid point closest to Champaign-Urbana.

In [10]:
plt.figure(1) plt.subplot(211) plt.plot(precip_global_ann['time'],np.squeeze(precip_global_ann), 'b') plt.plot(precip_cmi_ann['time'],np.squeeze(precip_cmi_ann), 'k') plt.legend(['Global','Champaign']) plt.title('Precipitation') plt.subplot(212) plt.plot(temp_global_ann['time'],np.squeeze(temp_global_ann), 'b') plt.plot(temp_cmi_ann['time'],np.squeeze(temp_cmi_ann), 'k') plt.legend(['Global','Champaign']) plt.title('Temperature anomaly')
<matplotlib.text.Text at 0x7f44a18ad850>

(c) Plot annual average temperature versus annual average precipitation globally and at the grid point closest to Champaign-Urbana. Color the points by year using the scatter function, and add a colorbar using the colorbar function. Do you see any trends?

Hint: A code that has similar functionality is given here:

In [19]:
import numpy as np import matplotlib.pyplot as plt x=np.squeeze(precip_global_ann) #precipitation would go here y=np.squeeze(temp_global_ann) #temperature would go here #plt.plot(x,y,'.k') z=temp_global_ann['time'].values z=temp_global_ann['time'].values.astype('datetime64[Y]').astype('string').astype('float') # this would be the year plt.scatter(x, y, c=z, s=100, cmap=plt.cm.cool, edgecolors='None', alpha=0.75) #play around with different cmaps if you want plt.xlabel('Precipitation') plt.ylabel('Temperature anomaly') #plt.clim([1900,2015]) plt.colorbar()
<matplotlib.colorbar.Colorbar instance at 0x7f44a0f575f0>
In [25]:
temp_global_ann['time'].values.astype('datetime64[Y]').astype('string').astype('float')
array([ 1901., 1902., 1903., 1904., 1905., 1906., 1907., 1908., 1909., 1910., 1911., 1912., 1913., 1914., 1915., 1916., 1917., 1918., 1919., 1920., 1921., 1922., 1923., 1924., 1925., 1926., 1927., 1928., 1929., 1930., 1931., 1932., 1933., 1934., 1935., 1936., 1937., 1938., 1939., 1940., 1941., 1942., 1943., 1944., 1945., 1946., 1947., 1948., 1949., 1950., 1951., 1952., 1953., 1954., 1955., 1956., 1957., 1958., 1959., 1960., 1961., 1962., 1963., 1964., 1965., 1966., 1967., 1968., 1969., 1970., 1971., 1972., 1973., 1974., 1975., 1976., 1977., 1978., 1979., 1980., 1981., 1982., 1983., 1984., 1985., 1986., 1987., 1988., 1989., 1990., 1991., 1992., 1993., 1994., 1995., 1996., 1997., 1998., 1999., 2000., 2001., 2002., 2003., 2004., 2005., 2006., 2007., 2008., 2009., 2010., 2011., 2012., 2013., 2014.])
In [ ]: