zzo[iyi * r], 'or-', markersize=5, lw=.8, label='Interpolated' if label else None) P.legend(loc='best', framealpha=0.5) P.grid() P.title('Section') P.tight_layout() savefigs(code_file_name(ext='_0.png'), verbose=False, pdf=True) # Maps P.figure(figsize=(8, 4)) levels = auto_scale(vmin=zzi.min(), vmax=zzi.max(), nmax=30) P.subplot(121) P.contourf(xxi, yyi, zzi, levels=levels) P.contour(xxi, yyi, zzi, linewidths=0.1, levels=levels, colors='k') add_grid((xxi, yyi), alpha=.3, centers=True) for iyi in iyis: P.axhline(yi[iyi], linestyle='--', color='k') P.title('Original') P.subplot(122) P.contourf(xxo, yyo, zzo, levels=levels) P.contour(xxo, yyo, zzo, linewidths=0.1, levels=levels, colors='k') add_grid((xxo, yyo), alpha=.2, centers=True) for iyi in iyis: P.axhline(yi[iyi], linestyle='--', color='k') P.title('Interpolated') P.tight_layout() savefigs(code_file_name(ext='_1.png'), verbose=False, pdf=True) P.close()
iyis = [3, 4] for iyi in iyis: label = iyi==iyis[0] P.plot(xi, zzi[iyi], 'ob-', markersize=8, label='Original' if label else None) P.plot(xo, zzo[iyi*r], 'or-', markersize=5, lw=.8, label='Interpolated' if label else None) P.legend(loc='best', framealpha=0.5) P.grid() P.title('Section') P.tight_layout() savefigs(code_file_name(ext='_0.png'), verbose=False, pdf=True) # Maps P.figure(figsize=(8, 4)) levels = auto_scale(vmin=zzi.min(), vmax=zzi.max(), nmax=30) P.subplot(121) P.contourf(xxi, yyi, zzi, levels=levels) P.contour(xxi, yyi, zzi, linewidths=0.1, levels=levels, colors='k') add_grid((xxi, yyi), alpha=.3, centers=True) for iyi in iyis: P.axhline(yi[iyi], linestyle='--', color='k') P.title('Original') P.subplot(122) P.contourf(xxo, yyo, zzo, levels=levels) P.contour(xxo, yyo, zzo, linewidths=0.1, levels=levels, colors='k') add_grid((xxo, yyo), alpha=.2, centers=True) for iyi in iyis: P.axhline(yi[iyi], linestyle='--', color='k') P.title('Interpolated') P.tight_layout() savefigs(code_file_name(ext='_1.png'), verbose=False, pdf=True) P.close()
nxy = 15 xi = N.arange(nxy * 1.) yi = N.arange(nxy * 1.) xxi, yyi = N.meshgrid(xi, yi) zi = N.ma.array(yyi) zi[int(nxy * 0.3):int(nxy * 0.8), int(nxy * 0.3):int(nxy * 0.8)] = N.ma.masked zi.shape = 1, nxy, nxy # Output positions no = 1000 xo = N.random.uniform(-nxy / 4., nxy + nxy / 4., no) yo = N.random.uniform(-nxy / 4., nxy + nxy / 4., no) # Interpolate mv = zi.get_fill_value() zo = dstwgt2dto1d(xi, yi, zi.filled(mv), xo, yo, mv) zo = N.ma.masked_values(zo, mv) # Plot kw = dict(vmin=zi.min(), vmax=zi.max()) P.figure(figsize=(6, 6)) P.subplot(111, aspect=1) P.contourf(xxi, yyi, zi[0], **kw) add_grid((xi, yi), edges=False, centers=True, marker='o') P.scatter(xo, yo, c=zo[0], s=50, **kw) P.title('dstwgt2dto1d') figfile = code_file_name(ext='png') if os.path.exists(figfile): os.remove(figfile) P.savefig(figfile) P.close()
yi = N.arange(nxy*1.) xxi, yyi = N.meshgrid(xi, yi) zi = N.ma.array(yyi) zi[int(nxy*0.3):int(nxy*0.8), int(nxy*0.3):int(nxy*0.8)] = N.ma.masked zi.shape = 1, nxy, nxy # Output positions no = 1000 xo = N.random.uniform(-nxy/4., nxy+nxy/4., no) yo = N.random.uniform(-nxy/4., nxy+nxy/4., no) # Interpolate mv = zi.get_fill_value() zo = nearest2dto1d(xi,yi,zi.filled(mv),xo,yo,mv) zo = N.ma.masked_values(zo, mv) # Plot kw = dict(vmin=zi.min(), vmax=zi.max()) P.figure(figsize=(6, 6)) P.subplot(111, aspect=1) P.contourf(xxi, yyi, zi[0], **kw) add_grid((xi, yi), edges=False, centers=True, marker='o') P.scatter(xo, yo, c=zo[0], s=50, **kw) P.title('nearest2dto1d') figfile = code_file_name(ext='png') if os.path.exists(figfile): os.remove(figfile) P.savefig(figfile) P.close()
# Format and convert xx, yy = N.meshgrid(N.arange(-2, 4, 0.25), N.arange(-1, 5, 0.25)) nxy = xx.shape xx.shape = -1 yy.shape = -1 pp, qq = [], [] for x, y in zip(xx, yy): p, q = curv2rect(x1,x2,x3,x4,y1,y2,y3,y4,x,y) pp.append(p) qq.append(q) pp = N.array(pp) qq = N.array(qq) # Plot xp = [x1, x2, x3, x4, x1] yp = [y1, y2, y3, y4, y1] P.subplot(211) levels = N.array([-10, 0, 1, 10.]) o = P.contourf(xx.reshape(nxy), yy.reshape(nxy), pp.reshape(nxy), levels=levels) P.colorbar(o) P.plot(xp, yp, 'k') P.subplot(212) o = P.contourf(xx.reshape(nxy), yy.reshape(nxy), qq.reshape(nxy), levels=levels) P.colorbar(o) P.plot(xp, yp, 'k') figfile = code_file_name(ext='png') if os.path.exists(figfile): os.remove(figfile) P.savefig(figfile) P.close()
# Curved grid nxy = 10 nt = 5 lon = N.arange(nxy * 1.) lat = N.arange(nxy * 1.) time = create_time((nt, ), 'years since 2000') gridi = rotate_grid((lon, lat), 30) xxi = gridi.getLongitude()[:].filled() yyi = gridi.getLatitude()[:].filled() vari = MV2.resize(yyi, (nt, nxy, nxy)) vari.setAxis(0, time) set_grid(vari, gridi) kw = dict(vmin=vari.min(), vmax=vari.max()) P.figure(figsize=(10, 3.5)) P.subplot(131, aspect=1) P.contourf(xxi, yyi, vari[0].asma(), **kw) add_grid(gridi, edges=False, centers=-1) xylims = (xxi.min(), xxi.max(), yyi.min(), yyi.max()) P.axis(xylims) P.title('Curved grid') # Interpolate to grid xg, yg = N.meshgrid(N.arange(-3.5, 14.5), N.arange(-3.5, 14.5)) nxyg = xg.shape cig = CurvedInterpolator(gridi, (xg, yg), g2g=True) varog = cig(vari) P.subplot(132, aspect=1) P.scatter(xg, yg, c=varog[0].asma(), s=120, linewidth=0, **kw) add_grid(gridi, edges=False, centers=-1) xylims = (xxi.min(), xxi.max(), yyi.min(), yyi.max()) P.axis(xylims)
from vcmq import P, N, set_grid, plot2d, MV2, add_grid, code_file_name, os # Curved grid nxy = 10 lon = N.arange(nxy*1.) lat = N.arange(nxy*1.) gridi = rotate_grid((lon, lat), 30) xxi = gridi.getLongitude()[:].filled() yyi = gridi.getLatitude()[:].filled() vari = MV2.array(yyi) set_grid(vari, gridi) kw = dict(vmin=vari.min(), vmax=vari.max()) P.figure(figsize=(10, 3.5)) P.subplot(131, aspect=1) P.contourf(xxi, yyi, vari.asma(), **kw) add_grid(gridi, edges=False, centers=-1) xylims = (xxi.min(), xxi.max(), yyi.min(), yyi.max()) P.axis(xylims) P.title('Curved grid') # Interpolate to grid xg, yg = N.meshgrid(N.arange(-3.5, 14.5), N.arange(-3.5, 14.5)) nxyg = xg.shape cig = CurvedInterpolator(gridi, (xg, yg), g2g=True) varog = cig(vari) P.subplot(132, aspect=1) P.scatter(xg, yg, c=varog.asma(), s=120, linewidth=0, **kw) add_grid(gridi, edges=False, centers=-1) xylims = (xxi.min(), xxi.max(), yyi.min(), yyi.max()) P.axis(xylims)
yy.shape = -1 pp, qq = [], [] for x, y in zip(xx, yy): p, q = curv2rect(x1, x2, x3, x4, y1, y2, y3, y4, x, y) pp.append(p) qq.append(q) pp = N.array(pp) qq = N.array(qq) # Plot xp = [x1, x2, x3, x4, x1] yp = [y1, y2, y3, y4, y1] P.subplot(211) levels = N.array([-10, 0, 1, 10.]) o = P.contourf(xx.reshape(nxy), yy.reshape(nxy), pp.reshape(nxy), levels=levels) P.colorbar(o) P.plot(xp, yp, 'k') P.subplot(212) o = P.contourf(xx.reshape(nxy), yy.reshape(nxy), qq.reshape(nxy), levels=levels) P.colorbar(o) P.plot(xp, yp, 'k') figfile = code_file_name(ext='png') if os.path.exists(figfile): os.remove(figfile) P.savefig(figfile) P.close()