# -*- coding: utf-8 -*- # Creation d'un jeu de precipitations horaires import MV2, cdms2, numpy as N from vacumm.misc.axes import create_time hours = create_time((12*60, 25.*60, 60), 'minutes since 2000') precip = MV2.sin(N.arange(len(hours))*.2)*10 precip.setAxis(0, hours) precip.units = 'mm' precip.long_name = 'Original' # Nouvel echantillonnage / 2h hours2 = create_time((10, 30., 2), 'hours since 2000') # Regrillage 1D conservatif from vacumm.misc.grid.regridding import regrid1d precip2 = regrid1d(precip, hours2, 'conservative') precip2.long_name = 'Regridded' # Verifications print 'Total precip.:' print '- original =', precip.sum() print '- remapped =', precip2.sum() # > Total precip.: # > - original = 89.957242832779755 # > - remapped = 89.957237 # Plots from vacumm.misc.plot import savefigs from vacumm.misc.plot import bar2 kwplot = dict(color='#00ffff',edgecolor='#55aaaa',
# Détermination des profondeurs d'après les sigma # - détection auto de la classe de sigma d'après fichier sigma_class = NcSigma.factory(f) # - initialisation du convertisseur sigma_converter = sigma_class(copyaxes=True) # - lecture de eta (sans sélection de domaine) et calcul des profondeurs depths_in = sigma_converter().filled() f.close() # Creation de l'axe des profondeurs cibles depth_out = create_depth(depths) # Interpolation xmap = (0, 2, 3) # la profondeur varie en T/Y/X xmapper = np.rollaxis(depths_in, 1, 4) # profondeur = dernier axe data_out = regrid1d(data_in, depth_out, axi=depths_in, axis=1, method='linear', extrap=1) # Plot kw = dict(show=False, vmin=10, vmax=14, xhide='auto', add_grid=True, ymax=0) section2(data_in[0, :, 10], yaxis=depths_in[0, :, 10], subplot=211, title='Sigma', **kw) s = section2(data_out[0, :, 10], subplot=212, title='Z', savefigs=__file__, **kw) # Sauvegarde outfile = __file__[:-2]+'nc' if os.path.isfile(outfile): os.remove(outfile) netcdf3() f2 = cdms2.open(outfile,'w') f2.write(data_out) f2.close() print 'Saved to', outfile
# Détermination des profondeurs d'après les sigma # - détection auto de la classe de sigma d'après fichier sigma_class = NcSigma.factory(f) # - initialisation du convertisseur sigma_converter = sigma_class(copyaxes=True) # - lecture de eta (sans sélection de domaine) et calcul des profondeurs depths_in = sigma_converter().filled() f.close() # Creation de l'axe des profondeurs cibles depth_out = create_depth(depths) # Interpolation xmap = (0, 2, 3) # la profondeur varie en T/Y/X xmapper = np.rollaxis(depths_in, 1, 4) # profondeur = dernier axe data_out = regrid1d(data_in, depth_out, axi=depths_in, axis=1, method='linear', extrap=1) # Plot kw = dict(show=False, vmin=10, vmax=14, xhide='auto', add_grid=True, ymax=0) section2(data_in[0, :, 10], yaxis=depths_in[0, :, 10], subplot=211, title='Sigma', **kw) s = section2(data_out[0, :, 10], subplot=212, title='Z', savefigs=__file__, close=True, **kw) # Sauvegarde outfile = __file__[:-2]+'nc' if os.path.isfile(outfile): os.remove(outfile) netcdf3() f2 = cdms2.open(outfile,'w') f2.write(data_out) f2.close()
# - basse résolution lon_lr = create_lon((lon.min()+dlon*.7, lon.max()+dlon, dlon*3.3)) # - haute résolution lon_hr = create_lon((lon.min()+dlon, lon.max()+dlon, dlon/3.3)) # - dict lons = dict(haute=lon_hr, basse=lon_lr) # Regrillages et plots from matplotlib import rcParams ; rcParams['font.size'] = 9 from vacumm.misc.grid.regridding import regrid1d from vacumm.misc.plot import curve2,savefigs, yscale ; import pylab as P P.figure(figsize=(5.5, 6)) kwplot = dict(show=False,vmin=v.min(),vmax=v.max(),alpha=.7) for ilh,resdst in enumerate(['basse', 'haute']): # Regrillage vlinear = regrid1d(v, lons[resdst], 'linear') vremap = regrid1d(v, lons[resdst], 'cellave') # Plots P.subplot(2, 1, ilh+1) curve2(v, 'o', markersize=4, color='k', label=u'Original', hspace=.3, **kwplot) curve2(vremap, 'o', markersize=2, label=u'Cellave', linewidth=1.2, color='b', **kwplot) curve2(vlinear, 'o', markersize=2, label=u'Linear', linewidth=1.2, color='r', **kwplot) yscale(1.1) P.title(u'Vers la %s resolution'%resdst) if not ilh: P.legend(loc='lower left').legendPatch.set_alpha(.6) savefigs(__file__, pdf=True)
h0 = f('h0', lon=(-4.9, -4.43)).filled(0.) f.close() t.long_name = 'Original' # Creation d'un axe de profondeur from vacumm.misc.axes import create_dep ddep = 5. dep = create_dep((-h0.max(), 0. + ddep, ddep)) # Creation de l'axe etendu (taille (ndep,nx)) depths = -N.outer(t.getAxis(0)[::-1], h0) dep[-1] = depths[-1].max() # Regrillage lineaire from vacumm.misc.grid.regridding import regrid1d tr = regrid1d(t, dep, 'linear', axi=depths, axis=0) tr.long_name = 'Interpolated' t.getAxis(0).designateLevel() # Plot from vacumm.misc.plot import savefigs, yscale, add_grid, section2 import pylab as P P.figure(figsize=(5.5, 6)) kwplot = dict(show=False, bgcolor='.5', ylim=(-80, 0)) # - original section2(t, yaxis=depths, subplot=211, hspace=.3, **kwplot) add_grid((t.getLongitude(), depths[:]), linewidth=.3) # - regridded section2(tr, subplot=212, **kwplot) add_grid((tr.getLongitude(), dep[:]), linewidth=.3)
P.subplot(133, sharex=ax, sharey=ax) P.pcolormesh(xbo, ybo, voc, **kw) P.ylim(ymin=min(ybi.min(), ybo.min()), ymax=max(ybi.max(), ybo.max())) P.title('Cellave') P.tight_layout() P.savefig(figfile) # 1d->1d depi1d = create_dep(N.arange(-4500., 1, 500)) depo1d = create_dep(N.arange(-4000., 1, 333.33)) nzi = depi1d.shape[0] vari = MV2.asarray( N.ma.resize(depi1d[:], (nt, ny, nx, nzi)).transpose([0, 3, 1, 2])) vari.setAxis(1, depi1d) varol1 = regrid1d(vari, depo1d, method='linear') varol2 = regrid1d(vari, depo1d, method='linear', iaxi=0, iaxo=0, axi=depi1d) result.append(('assertEqual', [(varol1 - varol2).std(), 0])) varoc = regrid1d(vari, depo1d, method='cellave') myplot(vari, depi1d, varol1, varoc, depo1d, code_file_name(ext='_0.png')) # 4d->1d depi1d = N.arange(-4500., 1, 500) nzi = depi1d.shape[0] depi4d = N.resize(N.resize(depi1d, (nx, ny, nzi)).T, (nt, nzi, ny, nx)) depi4d += 500 * (N.random.random(depi4d.shape) - 0.5) depo1d = create_dep(N.arange(-4000., 1, 333.33)) vari = MV2.array(depi4d, fill_value=1e20) vari.getAxis(1).designateLevel() depi4d = MV2.asarray(depi4d) depi4d.getAxis(1).designateLevel()
lon_lr = create_lon((lon.min()+dlon*.7, lon.max()+dlon, dlon*3.3)) # - haute résolution lon_hr = create_lon((lon.min()+dlon, lon.max()+dlon, dlon/3.3)) # - dict lons = dict(haute=lon_hr, basse=lon_lr) # Regrillages et plots from matplotlib import rcParams ; rcParams['font.size'] = 9 from vacumm.misc.grid.regridding import regrid1d from vacumm.misc.plot import curve2,savefigs, yscale ; import pylab as P P.figure(figsize=(5.5, 6)) kwplot = dict(show=False,vmin=v.min(),vmax=v.max(),alpha=.7) for ilh,resdst in enumerate(['basse', 'haute']): # Regrillage vlinear = regrid1d(v, lons[resdst], 'linear') vremap = regrid1d(v, lons[resdst], 'cellave') # Plots P.subplot(2, 1, ilh+1) curve2(v, 'o', markersize=4, color='k', label=u'Original', hspace=.3, **kwplot) curve2(vremap, 'o', markersize=2, label=u'Cellave', linewidth=1.2, color='b', **kwplot) curve2(vlinear, 'o', markersize=2, label=u'Linear', linewidth=1.2, color='r', **kwplot) yscale(1.1) P.title(u'Vers la %s resolution'%resdst) if not ilh: P.legend(loc='lower left').legendPatch.set_alpha(.6) savefigs(__file__, pdf=True) P.close()
h0 = f('h0', lon=(-4.9, -4.43)).filled(0.) f.close() t.long_name = 'Original' # Creation d'un axe de profondeur from vacumm.misc.axes import create_dep ddep = 5. dep = create_dep((-h0.max(),0.+ddep , ddep)) # Creation de l'axe etendu (taille (ndep,nx)) depths = -N.outer(t.getAxis(0)[::-1], h0) dep[-1] = depths[-1].max() # Regrillage lineaire from vacumm.misc.grid.regridding import regrid1d tr = regrid1d(t, dep, 'linear', axi=depths, axis=0) tr.long_name = 'Interpolated' t.getAxis(0).designateLevel() # Plot from vacumm.misc.plot import savefigs,yscale, add_grid, section2 import pylab as P P.figure(figsize=(5.5, 6)) kwplot = dict(show=False, bgcolor='.5', ylim=(-80, 0)) # - original section2(t, yaxis=depths, subplot=211, hspace=.3, **kwplot) add_grid((t.getLongitude(), depths[:]), linewidth=.3) # - regridded section2(tr, subplot=212, **kwplot) add_grid((tr.getLongitude(), dep[:]), linewidth=.3)
P.title('Linear') P.subplot(133, sharex=ax, sharey=ax) P.pcolormesh(xbo, ybo, voc, **kw) P.ylim(ymin=min(ybi.min(), ybo.min()), ymax=max(ybi.max(), ybo.max())) P.title('Cellave') P.tight_layout() P.savefig(figfile) # 1d->1d depi1d = create_dep(N.arange(-4500., 1, 500)) depo1d = create_dep(N.arange(-4000., 1, 333.33)) nzi = depi1d.shape[0] vari = MV2.asarray(N.ma.resize(depi1d[:], (nt, ny, nx, nzi)).transpose([0, 3, 1, 2])) vari.setAxis(1, depi1d) varol1 = regrid1d(vari, depo1d, method='linear') varol2 = regrid1d(vari, depo1d, method='linear', iaxi=0, iaxo=0, axi=depi1d) result.append(('assertEqual', [(varol1-varol2).std(), 0])) varoc = regrid1d(vari, depo1d, method='cellave') myplot(vari, depi1d, varol1, varoc, depo1d, code_file_name(ext='_0.png')) # 4d->1d depi1d = N.arange(-4500., 1, 500) nzi = depi1d.shape[0] depi4d = N.resize(N.resize(depi1d, (nx, ny, nzi)).T, (nt, nzi, ny, nx)) depi4d += 500*(N.random.random(depi4d.shape)-0.5) depo1d = create_dep(N.arange(-4000., 1, 333.33)) vari = MV2.array(depi4d, fill_value=1e20) vari.getAxis(1).designateLevel() depi4d = MV2.asarray(depi4d)