# -*- coding: utf-8 -*- """Figure 8: the GRDC gauges of the Columbia, nested by number (a-d). Input: ../Data (Data06c, Data06j). Output: ../Figures: png and pdf. Usage: python Fig08_columbia_gauges_nested_by_number.py """ import os, re, glob, json, warnings warnings.filterwarnings('ignore', message='Geometry is in a geographic CRS') # the middle of a short line, in degrees: enough os.environ.setdefault('OMP_NUM_THREADS', '1'); os.environ.setdefault('PROJ_NETWORK', 'OFF') import numpy as np, pandas as pd, geopandas as gpd, rasterio import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import matplotlib.patheffects as pe from matplotlib.colors import to_rgb from matplotlib.patches import Rectangle, ConnectionPatch from matplotlib.font_manager import FontProperties from matplotlib.textpath import text_to_path from rasterio.windows import Window, transform as window_transform # ---- the style of the paper's figures; every text at least 10 pt, panel titles 12 pt FS, FT = 10, 12 plt.rcParams.update({'pdf.fonttype': 42, 'ps.fonttype': 42, 'font.family': 'sans-serif', 'font.sans-serif': ['Helvetica', 'Arial'], 'font.size': FS, 'mathtext.fontset': 'custom', 'mathtext.rm': 'Arial', 'mathtext.it': 'Arial:italic'}) BOLD = dict(fontfamily='Arial', fontweight='bold') T_DARK, T_GREY, F_OTHER, RIVER, NOG = '#1f1f1f', '#555555', '#f0f0f0', '#0d1b2a', '#c8c8c8' EDGE = '#f28e2b' # (b): the edge of the land of a gauge OUTLET = dict(marker='v', fc='#e34948', ec='black', lw=0.6) # every outlet: a red triangle, point down, black edge HALO = [pe.withStroke(linewidth=2.6, foreground='white')] def text_inches(text, fontsize, **kw): return text_to_path.get_text_width_height_descent(text, FontProperties(family=kw.get('fontfamily'), weight=kw.get('fontweight'), size=fontsize), False)[0] / 72 num = lambda v: '{:,}'.format(int(round(v))) HERE = os.path.dirname(os.path.abspath(__file__)) PLOT = os.path.dirname(os.path.dirname(HERE)) DATD = os.path.join(os.path.dirname(HERE), 'Data'); FIGD = os.path.join(os.path.dirname(HERE), 'Figures') # Data06c and Data06j (made on _20261113_195421) os.makedirs(FIGD, exist_ok=True) D06 = os.path.join(DATD, 'Data06j_columbia_%s') BINS = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 11), (12, 18)] # calibration rounds; round 1: no gauge upstream RAMP = ['#d6e6f7', '#aecdef', '#86b3e6', '#5e98da', '#3a7cc8', '#2460a6', '#163f7a', '#0b2752'] # one shade per round 1-6: the Clark Fork has 1-6 CG = 50 # Clark Fork below Missoula: the downstream end of reach 65,452,614 # ================================================================ the files, and every number checked J = json.load(open(D06 % 'summary.json')) K = pd.read_csv(D06 % 'gauges.csv') lo, hi, dep = K.pixel_local_first.values.astype('int64'), K.pixel_local_last.values.astype('int64'), K.depth.values NPIX = J['last'] - J['first'] + 1 # the pixels of the Columbia, 1 to NPIX from the outlet n = len(K); assert n == J['gauges'] == 174 and (K.k.values == np.arange(1, n + 1)).all() and (np.diff(lo) >= 0).all() assert (lo == K.pixel_seq_id - J['first'] + 1).all() and (hi == K.pixel_seq_id + K.upstream_channel_pixel_count - J['first']).all() and 1 <= lo.min() and hi.max() <= NPIX assert NPIX == J['pixels'] == 5294775 for k in range(n): # the tree from the ranges alone: the gauge downstream is the innermost range around around = [j for j in range(k) if lo[j] <= lo[k] <= hi[j]] assert dep[k] == len(around) and K.next_down_k[k] == (around[-1] + 1 if around else 0) and (not around or hi[k] <= hi[around[-1]]) nup = K.n_all_up.values for k in range(n): # the gauges upstream of gauge k are k+1 ... k+n_all_up: one range of numbers assert (np.flatnonzero((np.arange(n) > k) & (lo >= lo[k]) & (lo <= hi[k])) == np.arange(k + 1, k + 1 + nup[k])).all() RND = np.ones(n, 'int64') # in descending number every gauge comes after all gauges upstream of it for k in range(n - 1, 0, -1): d = K.next_down_k[k] - 1 if d >= 0: assert d < k; RND[d] = max(RND[d], RND[k] + 1) assert (RND == K['round'].values).all() and RND.max() == J['rounds'] == 18 assert np.bincount(RND)[1:].tolist() == [70, 34, 17, 13, 9, 7, 4, 4, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1] BIN = np.array([next(b for b, (a, z) in enumerate(BINS) if a <= r <= z) for r in RND]) assert [int((BIN == b).sum()) for b in range(len(BINS))] == [70, 34, 17, 13, 9, 7, 14, 10] and RND.max() == BINS[-1][1] assert dep[0] == 0 and RND[0] == 18 and K.grdc_station_id[0] == 4115201 and (dep[1:] > 0).all() # gauge 1: every other gauge is upstream of it assert K.upstream_area_km2[0] == K.upstream_area_km2.max() c0k = CG - 1; X = np.arange(c0k, c0k + 1 + nup[c0k]) # gauge 50 and the 5 gauges upstream of it, 51-55 assert K.grdc_station_id[c0k] == 4115348 and nup[c0k] == 5 and X[-1] == 54 and lo[X].min() == lo[c0k] and hi[X].max() == hi[c0k] assert RND[X].tolist() == [3, 2, 1, 1, 2, 1] and (dep[X] - dep[c0k]).tolist() == [0, 1, 2, 2, 1, 2] PS, PN = K.pixel_seq_id.values.astype('int64'), K.upstream_channel_pixel_count.values.astype('int64') # [PS, PS + PN): the pixels upstream assert (PS[c0k], PS[c0k] + PN[c0k]) == (1088820338, 1089010146) # = the upstream pixels of reach 65,452,614 of the 1 km² release assert (PS - J['first'] + 1 == lo).all() and (PS + PN - J['first'] == hi).all() GI = pd.read_csv(os.path.join(DATD, 'Data06c_grdc_gauges.csv')) # the ids of the gauge table GI = GI[GI.basin_id == J['basin_id']].reset_index(drop=True) # the GRDC table of step 10 OFFS = int(GI.station_seq_id[0] - GI.basin_station_seq_id[0]) # gauge k of the Columbia is station k + 9,373 of the globe assert OFFS == 9373 and (GI.station_seq_id.values - GI.basin_station_seq_id.values == OFFS).all() assert (GI.basin_station_seq_id.values == K.k.values).all() and (GI.grdc_station_id.values == K.grdc_station_id.values).all() and (GI.upstream_station_count.values - 1 == nup).all() # step 10 counts the gauge itself with rasterio.open(D06 % 'owner_008.tif') as s: R = s.read(1); TR = s.transform with rasterio.open(D06 % 'clarkfork_owner_004.tif') as s: # the window of gauge 47; (b) cuts the land of 50-55 from it RX = s.read(1); TX = s.transform b_ = rasterio.transform.array_bounds(*RX.shape, TX); assert np.allclose((b_[0], b_[2], b_[1], b_[3]), J['clark_box'], atol=0.01) r_, c_ = np.nonzero((RX >= CG) & (RX <= X[-1] + 1)); PAD = 15 # 15 samples of 0.00333°: 0.05° around the land win = Window(c_.min() - PAD, r_.min() - PAD, c_.max() - c_.min() + 1 + 2 * PAD, r_.max() - r_.min() + 1 + 2 * PAD) assert win.col_off >= 0 and win.row_off >= 0 and win.col_off + win.width <= RX.shape[1] and win.row_off + win.height <= RX.shape[0] TX = window_transform(win, TX); RX = RX[win.row_off:win.row_off + win.height, win.col_off:win.col_off + win.width] inb = R >= 0 miss = sorted(set(range(n + 1)) - set(np.unique(R[inb]))) # land below one sample (0.37 km²): just below another gauge assert all(K.own_area_km2[m - 1] < 1 for m in miss) and not set(miss) & set(X + 1), miss lon0, lat0, lon1, lat1 = rasterio.transform.array_bounds(*R.shape, TR) xlon0, xlat0, xlon1, xlat1 = rasterio.transform.array_bounds(*RX.shape, TX) xr = (RX >= CG) & (RX <= X[-1] + 1) # the land of gauges 50-55 in its window assert not (xr[0].any() or xr[-1].any() or xr[:, 0].any() or xr[:, -1].any()) assert set(np.unique(RX[xr])) == set(X + 1) RV = pd.read_parquet(D06 % 'rivers.parquet') RV = gpd.GeoDataFrame(RV, geometry=gpd.GeoSeries.from_wkb(RV.geometry), crs=4326) assert RV.upstream_area_km2.min() >= 100 and abs(RV.upstream_area_km2.max() / J['area_km2'] - 1) < 0.01 CH_A, CH_B = 2e3, 2e2 # km², the reaches drawn in (a) and (b): drainage area at their downstream end RVA = RV[RV.upstream_area_km2 >= CH_A] RVB = RV[RV.upstream_area_km2 >= CH_B] mid = RVB.geometry.interpolate(0.5, normalized=True) # a river of (b): its middle on Clark Fork land cm, rm = ~TX * (mid.x.values, mid.y.values) ok = (rm >= 0) & (rm < RX.shape[0]) & (cm >= 0) & (cm < RX.shape[1]) RVX = RVB[ok & xr[rm.astype(int).clip(0, RX.shape[0] - 1), cm.astype(int).clip(0, RX.shape[1] - 1)]] assert abs(RVX.upstream_area_km2.max() / K.upstream_area_km2[c0k] - 1) < 0.02, RVX.upstream_area_km2.max() # ---- the pictures: the colour of every sample, white where two lands meet lut = np.array([to_rgb(RAMP[b]) for b in BIN]) def picture(Rw, own, edges=True): rgb = np.ones(Rw.shape + (3,)); rgb[:] = to_rgb(F_OTHER) rgb[Rw == 0] = to_rgb(NOG) rgb[own] = lut[Rw[own] - 1] edge = np.zeros(Rw.shape, bool) dx = (Rw[:, 1:] != Rw[:, :-1]) & own[:, 1:] & own[:, :-1]; edge[:, 1:] |= dx; edge[:, :-1] |= dx dy = (Rw[1:, :] != Rw[:-1, :]) & own[1:, :] & own[:-1, :]; edge[1:, :] |= dy; edge[:-1, :] |= dy if edges: rgb[edge] = 1.0 return rgb RGBA = picture(R, R > 0) RGBX = picture(np.where(xr, RX, -1), xr, False) # ================================================================ the layout (inches, y down) W, ML = 7.48, 0.12 TA = 0.22; MY0 = TA + 0.14 ASP = 1 / np.cos(np.radians((lat0 + lat1) / 2)); ASPX = 1 / np.cos(np.radians((xlat0 + xlat1) / 2)) ra = (lat1 - lat0) * ASP / (lon1 - lon0); rb = (xlat1 - xlat0) * ASPX / (xlon1 - xlon0) # height / width of (a) and (b) HEADS = ('basin_\nstation_\nseq_id', 'grdc_station_id', 'pixel_seq_id') # broken after an underscore, as the headers of Figures 9 and 11) ROW = lambda k: ('%d' % (k + 1), '%d' % K.grdc_station_id[k], num(PS[k])) CW = [max(max(text_inches(h, FS, **BOLD) for h in HEADS[j].split('\n')), max(text_inches(ROW(k)[j], FS, **BOLD) for k in X)) + 0.16 for j in range(3)] KW, GAPAB, GAPBK = sum(CW), 0.32, 0.22 MH = (W - 2 * ML - GAPAB - GAPBK - KW) / (1 / ra + 1 / rb) MW, XW = MH / ra, MH / rb XX = ML + MW + GAPAB; LX = XX + XW + GAPBK assert LX + KW <= W - ML + 1e-9 KY = MY0 + MH + 0.34 # the key: two lines under the maps TC = KY + 0.24 + 0.46 # the title of (d) LAB = text_inches('Clark Fork', FS) + 0.10 GX0, GX1 = ML + LAB, W - 0.14 AY = TC + 0.42 # the Columbia range; the Clark Fork rows below it RH = 0.24; GY0 = AY + 0.58 NR = int((dep[X] - dep[c0k]).max()) + 1 H = GY0 + RH * NR + 0.40 fig = plt.figure(figsize=(W, H)) A = fig.add_axes([0, 0, 1, 1]); A.set_xlim(0, W); A.set_ylim(H, 0); A.set_axis_off(); A.set_zorder(5) T_ = lambda x, y, s, **kw: A.text(x, y, s, **{**dict(fontsize=FS, color=T_DARK, ha='left', va='baseline'), **kw}) def fits(x, s, size, limit, **kw): assert x + text_inches(s, size, **kw) <= limit, 'does not fit: %s' % s return s def title(x, y, a, limit): T_(x, y, fits(x, a, FT, limit, **BOLD), fontsize=FT, **BOLD) def lw_of(upa, top, base, w0, w1): return w0 + w1 * (np.log10(upa) - np.log10(base)) / (np.log10(top) - np.log10(base)) # ================================================================ (a) the Columbia title(ML, TA, '(a) Columbia River Basin', XX - 0.05) ax = fig.add_axes([ML / W, 1 - (MY0 + MH) / H, MW / W, MH / H]); ax.set_axis_off() ax.imshow(RGBA, extent=(lon0, lon1, lat0, lat1), interpolation='nearest', aspect=ASP, zorder=1) ax.contour(np.linspace(lon0, lon1, R.shape[1]), np.linspace(lat1, lat0, R.shape[0]), inb.astype(float), [0.5], colors=T_GREY, linewidths=0.5, zorder=2) RVA.plot(ax=ax, color=RIVER, linewidth=lw_of(RVA.upstream_area_km2.values, J['area_km2'], CH_A, 0.25, 1.1), alpha=0.75, zorder=3, aspect=None) ax.scatter([J['outlet_lon']], [J['outlet_lat']], s=40, zorder=5, **OUTLET) ax.scatter(K.lon, K.lat, s=6, fc='white', ec=T_DARK, lw=0.35, zorder=6) ax.annotate('1', (K.lon[0], K.lat[0]), (K.lon[0] + 0.8, K.lat[0] + 1.2), fontsize=FS, color=T_DARK, ha='center', va='center', # gauge 1, by the outlet path_effects=HALO, zorder=7, arrowprops=dict(arrowstyle='-', lw=0.5, color=T_DARK, shrinkA=0, shrinkB=3), **BOLD) ax.add_patch(Rectangle((xlon0, xlat0), xlon1 - xlon0, xlat1 - xlat0, fc='none', ec=T_DARK, lw=0.9, zorder=8)) ax.set_xlim(lon0, lon1); ax.set_ylim(lat0, lat1) assert lon0 < J['outlet_lon'] < lon1 and lat0 < J['outlet_lat'] < lat1 and lon0 < xlon0 and xlon1 < lon1 sx, sy = lon1 - 0.3, lat1 - 1.2; dl = 200 / (111.32 * np.cos(np.radians(sy))) # 200 km at the latitude of the bar, ending at the right (top right: no land) ax.plot([sx - dl, sx], [sy, sy], color=T_DARK, lw=1.3, solid_capstyle='butt', zorder=6) ax.text(sx - dl / 2, sy + 0.15, '200 km', fontsize=FS, ha='center', va='bottom', color=T_DARK, zorder=6) # ================================================================ (b) the Clark Fork title(XX, TA, '(b) Clark Fork', LX - 0.03) bx = fig.add_axes([XX / W, 1 - (MY0 + MH) / H, XW / W, MH / H]); bx.set_axis_off() bx.imshow(RGBX, extent=(xlon0, xlon1, xlat0, xlat1), interpolation='nearest', aspect=ASPX, zorder=1) gx_, gy_ = np.linspace(xlon0, xlon1, RX.shape[1]), np.linspace(xlat1, xlat0, RX.shape[0]) bx.contour(gx_, gy_, xr.astype(float), [0.5], colors=T_GREY, linewidths=0.5, zorder=2) for k in X: # the land of every gauge, edged bx.contour(gx_, gy_, (RX == k + 1).astype(float), [0.5], colors=EDGE, linewidths=0.9, zorder=2.5) RVX.plot(ax=bx, color=RIVER, linewidth=lw_of(RVX.upstream_area_km2.values, K.upstream_area_km2[c0k], CH_B, 0.2, 1.0), alpha=0.75, zorder=3, aspect=None) bx.scatter(K.lon[X], K.lat[X], s=16, fc='white', ec=T_DARK, lw=0.6, zorder=4) OFF = {50: (-0.22, 0.20, 'center'), 51: (-0.13, 0.33, 'center'), 52: (0.18, 0.10, 'left'), 53: (0.10, 0.27, 'center'), 54: (-0.02, -0.30, 'center'), 55: (0.20, -0.05, 'left')} for k in X: ox, oy, ha = OFF[k + 1] bx.annotate('%d' % (k + 1), (K.lon[k], K.lat[k]), (K.lon[k] + ox, K.lat[k] + oy), fontsize=FS, color=T_DARK, ha=ha, va='center', path_effects=HALO, zorder=6, arrowprops=dict(arrowstyle='-', lw=0.5, color=T_DARK, shrinkA=0, shrinkB=3), **BOLD) sx, sy = xlon1 - 0.08, xlat0 + 0.12; dl = 50 / (111.32 * np.cos(np.radians(sy))) # 50 km, ending at the right bx.plot([sx - dl, sx], [sy, sy], color=T_DARK, lw=1.3, solid_capstyle='butt', zorder=6) bx.text(sx - dl / 2, sy + 0.04, '50 km', fontsize=FS, ha='center', va='bottom', color=T_DARK, zorder=6) bx.set_xlim(xlon0, xlon1); bx.set_ylim(xlat0, xlat1) bx.add_patch(Rectangle((xlon0, xlat0), xlon1 - xlon0, xlat1 - xlat0, fc='none', ec=T_DARK, lw=0.9, zorder=7, clip_on=False)) for yy in (xlat0, xlat1): # the zoom: the box of (a) to the frame of (b) fig.add_artist(ConnectionPatch((xlon1, yy), (xlon0, yy), 'data', 'data', axesA=ax, axesB=bx, color=T_GREY, lw=0.6, zorder=8)) # ---- (c) the table: new number and GRDC id (style of the tables of FullBasin Figures 2 and 3) HEAD_C, ALT = '#1F4E79', '#f7f7f7' title(LX, TA, '(c) Gauges %d to %d' % (CG, X[-1] + 1), W - 0.03) HH = 0.62; TR_ = (MH - HH) / len(X) def cell(x, y, w, h, s, fc, **kw): A.add_patch(Rectangle((x, y), w, h, fc=fc, ec='black', lw=0.6, zorder=2)) T_(x + w / 2, y + h / 2, s, ha='center', va='center', zorder=3, **kw) xc = LX + np.concatenate([[0], np.cumsum(CW)[:-1]]) for j, h in enumerate(HEADS): cell(xc[j], MY0, CW[j], HH, h, HEAD_C, color='white', **BOLD) for i, k in enumerate(X): y = MY0 + HH + TR_ * i; fc = ALT if i % 2 else 'white' for j, v in enumerate(ROW(k)): cell(xc[j], y, CW[j], TR_, v, fc, **(BOLD if j == 0 else {})) assert abs(MY0 + HH + TR_ * len(X) - (MY0 + MH)) < 1e-9 and TR_ >= 0.20 # ---- the key: two lines under the maps SW, SH = 0.22, 0.15 def swatch(x, y, fc, s): A.add_patch(Rectangle((x, y - SH + 0.03), SW, SH, fc=fc, ec='none')); T_(x + SW + 0.05, y, s) return x + SW + 0.05 + text_inches(s, FS) + 0.14 x = ML; T_(x, KY, 'Calibration round', **BOLD); x += text_inches('Calibration round', FS, **BOLD) + 0.16 for (a, z), c in zip(BINS, RAMP): x = swatch(x, KY, c, '%d' % a if a == z else '%d to %d' % (a, z)) assert x <= W - ML y = KY + 0.24; x = ML x = swatch(x, y, NOG, 'no gauge downstream') A.scatter([x + SW / 2], [y - 0.045], s=16, fc='white', ec=T_DARK, lw=0.6, zorder=6); T_(x + SW + 0.05, y, 'gauge'); x += SW + 0.05 + text_inches('gauge', FS) + 0.14 A.scatter([x + SW / 2], [y - 0.05], s=34, zorder=6, **OUTLET); T_(x + SW + 0.05, y, 'basin outlet'); x += SW + 0.05 + text_inches('basin outlet', FS) + 0.14 A.plot([x, x + SW], [y - 0.045] * 2, color=RIVER, lw=1.0, alpha=0.75); T_(x + SW + 0.05, y, 'river'); x += SW + 0.05 + text_inches('river', FS) + 0.14 A.plot([x, x + SW], [y - 0.045] * 2, color=EDGE, lw=1.0); T_(x + SW + 0.05, y, 'unit catchment of each gauge'); x += SW + 0.05 + text_inches('unit catchment of each gauge', FS) assert x <= W - ML # ================================================================ (d) the Clark Fork gauges on the pixel axis title(ML, TC, '(d) Upstream catchments as ranges of pixel_seq_id', W - 0.05) P0, P1 = J['first'], J['last'] + 1 # the Columbia: pixels [P0, P1) ea = lambda v: GX0 + (GX1 - GX0) * (v - P0) / (P1 - P0) xa = lambda v: GX0 + (GX1 - GX0) * (v - PS[c0k]) / PN[c0k] # the range of gauge 50 A.plot([ea(P0), ea(P1)], [AY, AY], color=T_GREY, lw=0.9, solid_capstyle='butt') for xe in (ea(P0), ea(P1)): A.plot([xe, xe], [AY - 0.05, AY + 0.05], color=T_GREY, lw=0.9) T_(GX0 - 0.08, AY, 'Columbia', color=T_GREY, ha='right', va='center') T_(ea(P1), AY - 0.10, 'pixel_seq_id', color=T_GREY, ha='right') # the axis e0, e1 = ea(PS[c0k]), ea(PS[c0k] + PN[c0k]) A.add_patch(Rectangle((e0, AY - 0.06), e1 - e0, 0.12, fc=RAMP[BIN[c0k]], ec=T_DARK, lw=0.5, zorder=3)) for ex, gx in ((e0, GX0), (e1, GX1)): A.plot([ex, gx], [AY + 0.06, GY0], color=T_GREY, lw=0.5) T_(GX0 - 0.08, GY0 + RH * 0.5, 'Clark Fork', color=T_GREY, ha='right', va='center') MINW = 1 / 72 for k in X: x0 = xa(PS[k]); wd = max(MINW, xa(PS[k] + PN[k]) - x0); yr = GY0 + RH * (dep[k] - dep[c0k]) A.add_patch(Rectangle((x0, yr + 0.02), wd, RH - 0.04, fc=RAMP[BIN[k]], ec='white', lw=0.6, zorder=2)) A.scatter([x0], [yr + RH / 2], s=14, fc='white', ec=T_DARK, lw=0.6, zorder=4) # the gauge: the first pixel of its range s_ = '%d (%d)' % (k + 1, nup[k] + 1) if text_inches(s_, FS, **BOLD) + 0.10 < wd: T_(x0 + wd / 2, yr + RH / 2, s_, color='white' if BIN[k] >= 4 else T_DARK, ha='center', va='center', zorder=3, **BOLD) else: # still too narrow: the number beside it nxt = [xa(PS[j]) for j in X if dep[j] == dep[k] and PS[j] > PS[k]] assert x0 + wd + 0.05 + text_inches(s_, FS, **BOLD) < min(nxt + [GX1 + 0.1]), k + 1 T_(x0 + wd + 0.05, yr + RH / 2, s_, ha='left', va='center', zorder=3, **BOLD) ya = GY0 + RH * NR + 0.30 # the order of calibration: descending number, gauge 55 first, gauge 50 last A.annotate('', (GX0, ya), (GX1, ya), arrowprops=dict(arrowstyle='-|>', lw=0.9, color=T_DARK, mutation_scale=8, shrinkA=0, shrinkB=0)) T_((GX0 + GX1) / 2, ya - 0.05, 'calibration order', color=T_DARK, ha='center', va='bottom', path_effects=HALO) assert ya <= H - 0.05 def save(fig, stem): used = [int(m.group(1)) for m in (re.search(r'_v(\d+)\.(png|pdf)$', f) for f in glob.glob(os.path.join(FIGD, stem + '_v*'))) if m] out = os.path.join(FIGD, '%s_v%d.png' % (stem, 1 + max(used or [0]))) fig.savefig(out, dpi=300); fig.savefig(out[:-4] + '.pdf'); plt.close(fig) print(out, 'and .pdf; %.0f x %.0f mm' % (W * 25.4, H * 25.4)) print('maps: (a) %.2f x %.2f in, (b) %.2f x %.2f in; missing from the 0.00667° raster: %s' % (MW, MH, XW, MH, miss)) save(fig, 'Fig08_columbia_gauges_nested_by_number')