|
1 | | -from __future__ import (absolute_import, division, print_function) |
2 | | - |
3 | 1 | from mpl_toolkits.basemap import Basemap |
4 | 2 | from netCDF4 import Dataset, date2index |
5 | 3 | import numpy as np |
6 | 4 | import matplotlib.pyplot as plt |
7 | 5 | from datetime import datetime |
| 6 | +try: |
| 7 | + from urllib.request import urlretrieve |
| 8 | +except ImportError: |
| 9 | + from urllib import urlretrieve |
8 | 10 | date = datetime(2007,12,15,0) # date to plot. |
9 | 11 | # open dataset. |
10 | | -dataset = \ |
11 | | -Dataset('http://www.ncdc.noaa.gov/thredds/dodsC/OISST-V2-AVHRR_agg') |
| 12 | +sstpath, sstheader = urlretrieve("https://downloads.psl.noaa.gov/Datasets/noaa.oisst.v2.highres/sst.day.mean.{0}.nc".format(date.year)) |
| 13 | +dataset = Dataset(sstpath) |
12 | 14 | timevar = dataset.variables['time'] |
13 | 15 | timeindex = date2index(date,timevar) # find time index for desired date. |
14 | 16 | # read sst. Will automatically create a masked array using |
15 | 17 | # missing_value variable attribute. 'squeeze out' singleton dimensions. |
16 | 18 | sst = dataset.variables['sst'][timeindex,:].squeeze() |
17 | 19 | # read ice. |
18 | | -ice = dataset.variables['ice'][timeindex,:].squeeze() |
| 20 | +dataset.close() |
| 21 | +icepath, iceheader = urlretrieve("https://downloads.psl.noaa.gov/Datasets/noaa.oisst.v2.highres/icec.day.mean.{0}.nc".format(date.year)) |
| 22 | +dataset = Dataset(icepath) |
| 23 | +ice = dataset.variables['icec'][timeindex,:].squeeze() |
19 | 24 | # read lats and lons (representing centers of grid boxes). |
20 | 25 | lats = dataset.variables['lat'][:] |
21 | 26 | lons = dataset.variables['lon'][:] |
| 27 | +dataset.close() |
| 28 | +latstep, lonstep = np.diff(lats[:2]), np.diff(lons[:2]) |
| 29 | +lats = np.append(lats - 0.5 * latstep, lats[-1] + 0.5 * latstep) |
| 30 | +lons = np.append(lons - 0.5 * lonstep, lons[-1] + 0.5 * lonstep) |
22 | 31 | lons, lats = np.meshgrid(lons,lats) |
23 | 32 | # create figure, axes instances. |
24 | 33 | fig = plt.figure() |
|
0 commit comments