CoCalc Shared Filesatms391geodata / Week 4 / Week 4 Homework (answer key).ipynb
Author: Steve Nesbitt
Views : 4

# Problem 1

Using the netCDF file we used in exercise 1,

(1) Calculate the mean global temperature from Jan 1 1981 - Dec 31 2010.

In [23]:
import netCDF4 as nc
import datetime as dt
import numpy as np

data=nc.Dataset('air.mon.mean.nc')

date1=nc.date2num(dt.datetime(1981,1,1),units=data['time'].units)
date2=nc.date2num(dt.datetime(2010,12,31),units=data['time'].units)

mean_temp = np.mean(data['air'][((data['time'][:] >= date1) & (data['time'][:] < date2)),:,:])
print("Mean global temperature from Jan 1 1981 - Dec 31 2010 is: %f" %(mean_temp))

Mean global temperature from Jan 1 1981 - Dec 31 2010 is: 4.600965

(2) Calculate the mean tropical temperature (|lat| <= 20 degrees) from Jan 1 1981 - Dec 31 2010.

In [24]:
mean_tropical_temp = np.mean(data['air'][((data['time'][:] >= date1) & (data['time'][:] < date2)),(data['lat'][:] > -20) \
& (data['lat'][:] < 20),:])
print("Mean tropical temperature from Jan 1 1981 - Dec 31 2010 is: %f" %(mean_tropical_temp))

Mean tropical temperature from Jan 1 1981 - Dec 31 2010 is: 25.444757

(3) Write a function that can be used to calculate and return the time series of temperature (between a min_time and a max_time) averaged over a space domain (between bounds of latitude and longitude). Arguments to the function will be the beginning and end time and the minimum and maximum longitude and latitude.

In [25]:
def time_series_subset(input_data,min_time,max_time,min_lat,max_lat,min_lon,max_lon):
#input_data - 3-D array of input data (time,lon,lat)
#other variables are the bounds for time, lon, lat
#if time is inputted in a datetime object then use the appropriate functions in Example 1

#need to average over space - use fancy indexing to get the subset, which will end up being a 1-D array, with dimension time
time_series_out = np.mean(input_data['air'][((input_data['time'][:] >= min_time) & (input_data['time'][:] < max_time)), \
(input_data['lat'][:] > min_lat) & (input_data['lat'][:] < max_lat), \
(input_data['lon'][:] > min_lon) & (input_data['lon'][:] < max_lon)],axis=(1,2))

return time_series_out

In [26]:
# Test the function


# Problem 2

Using the values returned from the function created in Problem 1, part 3, write the time series of surface temperature out to a netCDF file for the monthly time series averaged over a domain including points between 140-60 degrees west longitude, 25-45 degrees north, and between Jan 1 1981 - Dec 31 2010. Be sure to define coordinate variables and attributes to the file, as well as the data.

In [27]:
date1=nc.date2num(dt.datetime(1981,1,1),units=data['time'].units)
date2=nc.date2num(dt.datetime(2010,12,31),units=data['time'].units)

time_series = time_series_subset(data, date1, date2, 25, 45, 180-140+180, 180-60+180)

try: ncfile.close()
except: pass
ncfile = nc.Dataset('new_time_series.nc',mode='w',format='NETCDF4_CLASSIC')
time_dim = ncfile.createDimension('time', len(time_series))

ncfile.title='Time series of surface temperature'
ncfile.description = 'New nc file stroing the time series of surface temperature over a domain including points between 140-60 degrees west longitude, 25-45 degrees north and between Jan 1 1981 - Dec 31 2010'

time = ncfile.createVariable('time', np.float64, ('time',))
time.units = 'hours since 1800-01-01'
time.long_name = 'time'
temp = ncfile.createVariable('temp',np.float64,('time',))
temp.units = 'K'
temp.standard_name = 'air_temperature'

temp[:] = time_series
print(ncfile)
data.close()
ncfile.close()

<type 'netCDF4._netCDF4.Dataset'> root group (NETCDF4_CLASSIC data model, file format UNDEFINED): title: Time series of surface temperature description: New nc file stroing the time series of surface temperature over a domain including points between 140-60 degrees west longitude, 25-45 degrees north and between Jan 1 1981 - Dec 31 2010 dimensions(sizes): time(360) variables(dimensions): float64 time(time), float64 temp(time) groups: 
In [ ]: