Skip to content

Commit 2b187c1

Browse files
committed
Fix race condition in plothighsandlows example
Based on the cadence in which the remote files are updated, it is safer to plot the dataset from yesterday than the dataset from today, because sometimes the file for today is still not present in the remote.
1 parent 3d1000d commit 2b187c1

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

doc/examples/plothighsandlows.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ def extrema(mat, mode="wrap", window=10):
2626
def main():
2727
"""Main function."""
2828

29-
# Plot 00 UTC today.
29+
# Plot 00 UTC yesterday.
3030
url = "http://nomads.ncep.noaa.gov/dods/gfs_0p50/gfs%Y%m%d/gfs_0p50_00z"
31-
date = dt.datetime.now()
31+
date = dt.datetime.now() - dt.timedelta(days=1)
3232

3333
# Open OPeNDAP dataset.
3434
data = netCDF4.Dataset(date.strftime(url))
@@ -100,7 +100,8 @@ def main():
100100
xyplotted.append((x, y))
101101

102102
# Set plot title and show.
103-
plt.title("Mean Sea-Level Pressure (with Highs and Lows) %s" % date)
103+
datestr = date.strftime("%Y%m%d00")
104+
plt.title("Mean Sea-Level Pressure (with Highs and Lows) %s" % datestr)
104105
plt.show()
105106

106107

doc/source/users/figures/plothighsandlows.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
plot H's and L's on a sea-level pressure map
33
(uses scipy.ndimage.filters and netcdf4-python)
44
"""
5+
import datetime as dt
56
import numpy as np
67
import matplotlib.pyplot as plt
7-
from datetime import datetime
88
from mpl_toolkits.basemap import Basemap, addcyclic
99
from scipy.ndimage.filters import minimum_filter, maximum_filter
1010
from netCDF4 import Dataset
1111

12+
1213
def extrema(mat,mode='wrap',window=10):
1314
"""find the indices of local extrema (min and max)
1415
in the input array."""
@@ -19,13 +20,13 @@ def extrema(mat,mode='wrap',window=10):
1920
# Return the indices of the maxima, minima
2021
return np.nonzero(mat == mn), np.nonzero(mat == mx)
2122

22-
# plot 00 UTC today.
23-
date = datetime.now().strftime('%Y%m%d')+'00'
2423

25-
# open OpenDAP dataset.
26-
data=Dataset("https://nomads.ncep.noaa.gov/dods/gfs_0p50/gfs%s/gfs_0p50_%sz"%\
27-
(date[0:8],date[8:10]))
24+
# Plot 00 UTC yesterday.
25+
url = "http://nomads.ncep.noaa.gov/dods/gfs_0p50/gfs%Y%m%d/gfs_0p50_00z"
26+
date = dt.datetime.now() - dt.timedelta(days=1)
2827

28+
# open OpenDAP dataset.
29+
data = Dataset(date.strftime(url))
2930

3031
# read lats,lons.
3132
lats = data.variables['lat'][:]
@@ -85,5 +86,7 @@ def extrema(mat,mode='wrap',window=10):
8586
ha='center',va='top',color='r',
8687
bbox = dict(boxstyle="square",ec='None',fc=(1,1,1,0.5)))
8788
xyplotted.append((x,y))
88-
plt.title('Mean Sea-Level Pressure (with Highs and Lows) %s' % date)
89+
90+
datestr = date.strftime("%Y%m%d00")
91+
plt.title('Mean Sea-Level Pressure (with Highs and Lows) %s' % datestr)
8992
plt.show()

0 commit comments

Comments
 (0)