Skip to content

Commit 057cf71

Browse files
joferkingtonWeatherGod
authored andcommitted
Fix landmask updating script to avoid excessive memory use
1 parent 04b7588 commit 057cf71

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

utils/plotgrdlandmask.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
# this script reads the file generated by grdlandmask, plots it,
99
# and saves it in the format that Basemap._readlsmask expects.
1010

11-
# WARNING: This tends to use excessive amounts of RAM. Do not attempt to run
12-
# this unless you're on a machine with >=32GB of RAM (yes, 32 Gigabytes).
11+
UTILS_DIR = os.path.dirname(os.path.abspath(__file__))
12+
OUTPUT_DIR = os.path.join(UTILS_DIR, '..', 'lib', 'mpl_toolkits',
13+
'basemap', 'data')
1314

1415
# run grdlandmask
1516
athresh={}
@@ -19,7 +20,7 @@
1920
athresh['h']=10
2021
athresh['f']=1
2122

22-
for resolution in ['c']:#,'l','i']:
23+
for resolution in ['c','l','i','f']:
2324
for grid in [1.25,2.5,5,10]:
2425

2526
filename = 'grdlandmask%smin_%s.nc' % (grid,resolution)
@@ -31,26 +32,34 @@
3132

3233
# read in data.
3334
nc = Dataset(filename)
34-
print nc.variables.keys()
35-
lons = nc.variables['lon'][:]
36-
nlons = len(lons)
37-
lats = nc.variables['lat'][:]
38-
nlats = len(lats)
3935
lsmask = nc.variables['z'][:].astype(np.uint8)
4036

41-
# plot
42-
fig = plt.figure()
43-
m =\
44-
Basemap(llcrnrlon=-180,llcrnrlat=-90,urcrnrlon=180,urcrnrlat=90,resolution=resolution,projection='mill')
45-
m.drawcoastlines() # coastlines should line up with land/sea mask.
46-
m.drawlsmask(land_color='coral',ocean_color='aqua',lsmask=lsmask,lsmask_lons=lons,lsmask_lats=lats,lakes=True)
47-
plt.title('%s by %s land-sea mask (resolution = %s) from grdlandmask' %\
48-
(nlons,nlats,resolution))
49-
5037
# write out.
51-
f = gzip.open('lsmask_%smin_%s.bin' % (grid,resolution),'wb')
52-
print lsmask.dtype, lsmask.shape
38+
filename = os.path.join(OUTPUT_DIR,
39+
'lsmask_%smin_%s.bin' % (grid, resolution))
40+
f = gzip.open(filename, 'wb')
5341
f.write(lsmask.tostring())
5442
f.close()
5543

44+
# Plot data for debugging purposes.
45+
for resolution in ['c','l','i','f']:
46+
grid = 10
47+
filename = 'grdlandmask%smin_%s.nc' % (grid,resolution)
48+
49+
nc = Dataset(filename)
50+
lsmask = nc.variables['z'][:].astype(np.uint8)
51+
lons = nc.variables['lon'][:]
52+
lats = nc.variables['lat'][:]
53+
54+
fig = plt.figure()
55+
m = Basemap(llcrnrlon=-180, llcrnrlat=-90,
56+
urcrnrlon=180, urcrnrlat=90,
57+
resolution=resolution, projection='mill')
58+
59+
m.drawcoastlines() # coastlines should line up with land/sea mask.
60+
m.drawlsmask(land_color='coral', ocean_color='aqua',
61+
lsmask=lsmask, lsmask_lons=lons, lsmask_lats=lats,
62+
lakes=True)
63+
plt.title('Land-sea mask (res = %s) from grdlandmask' % resolution)
64+
5665
plt.show()

0 commit comments

Comments
 (0)