def gene_bathy(xc, yc, xr, yr, n=500, amp=30.0): noise = N.random.random(n) a = N.random.random(n) * N.pi * 2 r = N.random.random(n) x = xc + xr * r * N.cos(a) y = yc + yr * r * N.sin(a) return N.asarray([x, y, N.exp(-(x - xc) ** 2 / xr ** 2 - (y - yc) ** 2 / yr ** 2) * amp + noise])
def create_mv2_gridder_xyzt(nx=8, ny=7, nz=6, nt=5, xmin=-6., xmax=-3, ymin=46, ymax=48, zmin=-200, zmax=0, tmin='2016', tmax='2016-02', tunits='days since 2016-01-01', rotate=0): """Create a MV2 array on a grid Return ------ MV2.array """ # Axes shape = () axes = [] if nt != 0: time = create_time(lindates(tmin, tmax, nt), tunits) axes.append(time) shape += nt, if nz != 0: dep = create_dep(N.linspace(zmin, zmax, nz)) axes.append(dep) shape += nz, if ny != 0: lat = create_lat(N.linspace(ymin, ymax, ny)) axes.append(lat) shape += ny, if nx != 0: lon = create_lon(N.linspace(xmin, xmax, nx)) axes.append(lon) shape += nx, # Array data = MV2.array(N.arange(N.multiply.reduce(shape)).reshape(shape), copy=False, axes=axes, id='temp', dtype='d') # Rotate grid if rotate: grid = data.getGrid() if grid is not None: grid = rotate_grid(grid, rotate) set_grid(data, grid) return data
def gene_bathy(xc, yc, xr, yr, n=500, amp=30.): noise = N.random.random(n) a = N.random.random(n) * N.pi * 2 r = N.random.random(n) x = xc + xr * r * N.cos(a) y = yc + yr * r * N.sin(a) return N.asarray([ x, y, N.exp(-(x - xc)**2 / xr**2 - (y - yc)**2 / yr**2) * amp + noise ])
def plot(xx, yy, target, label, figfiles, figfile, lon=None, lat=None, show=False): xs, ys, mask = coord2slice(target, lon=lon, lat=lat) P.figure(figsize=(6, 3.5)) P.title('Target=%(label)s / select: lon=%(lon)s, lat=%(lat)s' % locals()) add_grid((xx, yy)) xx = xx.asma() yy = yy.asma() if isinstance(lon, tuple): P.axvline(lon[0], color='m', ls='--', lw=2) P.axvline(lon[1], color='m', ls='--', lw=2) elif isinstance(lon, slice): i, j, k = lon.indices(xx.shape[1]) P.plot(xx[:, i], yy[:, i], 'c--', lw=2) P.plot(xx[:, j - 1], yy[:, j - 1], 'c--', lw=2) if isinstance(lat, tuple): P.axhline(lat[0], color='m', ls='--', lw=2) P.axhline(lat[1], color='m', ls='--', lw=2) elif isinstance(lat, slice): i, j, k = lat.indices(yy.shape[0]) P.plot(xx[i], yy[i], 'c--', lw=2) P.plot(xx[j - 1], yy[j - 1], 'c--', lw=2) P.xticks(N.arange(xx.min() - 1, xx.max() + 1)) P.yticks(N.arange(yy.min() - 1, yy.max() + 1)) xxi, yyi = xx, yy xx = xx[ys, xs] yy = yy[ys, xs] # mask = mask[ys, xs] xxb, yyb = meshbounds(xx, yy) P.pcolormesh(xxb, yyb, mask, shading='faceted') P.scatter(xx.ravel(), yy.ravel(), c=(0, 1, 0)) P.grid(True) P.axis('image') P.tight_layout() i = len(figfiles) savefig = figfile % i if os.path.exists(savefig): os.remove(savefig) P.savefig(savefig) figfiles.append(savefig) if show: P.show() else: P.close()
def sync_norms(self, force=True): """Synchronise norms between variables whose id has the same prefix Parameters ---------- force: bool Force sync even if it seems at has already been synced. """ if not force and self._norm_synced: return False # Renorm for each prefix for prefix in self.get_prefixes(): # Indices indices = self.get_prefixed(prefix) # Unified norm psize = sum([self[i].psize for i in indices]) norm = N.sqrt( sum([(self[i].psize * self[i].norm**2) for i in indices]) / psize) # Set it for i in indices: self[i].set_norm(norm) self._norm_synced = True return True
def create_mv2_scattered_xyzt(np=10, nz=6, nt=5, xmin=-6., xmax=-3, ymin=46, ymax=48, zmin=-200, zmax=0, tmin='2016', tmax='2016-02', tunits='days since 2016-01-01'): """Create a VM2 array of scattered data Return ------ array: longitudes array: latitude MV2.array: data """ # Axes shape = () axes = [] if nt != 0: time = create_time(lindates(tmin, tmax, nt), tunits) shape += nt, axes.append(time) if nz != 0: dep = create_dep(N.linspace(zmin, zmax, nz)) axes.append(dep) shape += nz, shape += np, axes.append(create_axis((np, ))) # Array data = MV2.array(N.arange(N.multiply.reduce(shape)).reshape(shape), copy=False, axes=axes, id='temp', dtype='d') # Positiions lons = N.linspace(xmin, xmax, np) lats = N.linspace(ymin, ymax, np) return lons, lats, data
def plot(xx, yy, target, label, figfiles, figfile, lon=None, lat=None, show=False): xs, ys, mask = coord2slice(target, lon=lon, lat=lat) P.figure(figsize=(6, 3.5)) P.title('Target=%(label)s / select: lon=%(lon)s, lat=%(lat)s'%locals()) add_grid((xx, yy)) xx = xx.asma() yy = yy.asma() if isinstance(lon, tuple): P.axvline(lon[0], color='m', ls='--', lw=2) P.axvline(lon[1], color='m', ls='--', lw=2) elif isinstance(lon, slice): i, j, k = lon.indices(xx.shape[1]) P.plot(xx[:, i], yy[:, i], 'c--', lw=2) P.plot(xx[:, j-1], yy[:, j-1], 'c--', lw=2) if isinstance(lat, tuple): P.axhline(lat[0], color='m', ls='--', lw=2) P.axhline(lat[1], color='m', ls='--', lw=2) elif isinstance(lat, slice): i, j, k = lat.indices(yy.shape[0]) P.plot(xx[i], yy[i], 'c--', lw=2) P.plot(xx[j-1], yy[j-1], 'c--', lw=2) P.xticks(N.arange(xx.min()-1, xx.max()+1)) P.yticks(N.arange(yy.min()-1, yy.max()+1)) xxi, yyi = xx, yy xx = xx[ys, xs] yy = yy[ys, xs] # mask = mask[ys, xs] xxb, yyb = meshbounds(xx, yy) P.pcolor(xxb, yyb, mask, shading='faceted') P.scatter(xx.ravel(), yy.ravel(), c=(0, 1, 0)) P.grid('on') P.axis('image') P.tight_layout() i = len(figfiles) savefig = figfile%i if os.path.exists(savefig): os.remove(savefig) P.savefig(savefig) figfiles.append(savefig) if show: P.show() else: P.close()
def assert_compatible_with_obs(self, obsmanager, sync_norms=True, sync_missing_values=True): """Assert that the :class:`Ensemble` current instance is compatible with a :class:`~sonat.obs.ObsManager` instance It checks that observed variable are provided by the ensemble. It optinonally synchonise norms between model and observations. """ # Check varnames for varname in obsmanager.suffixed_varnames: if varname not in self.varnames: raise SONATError(('Observed variable name "{}" not found ' ' in ensemble').format(varname)) # Sync norms if sync_norms: self.sync_norms(force=False) dnorms = self.get_named_norms() obsmanager.set_named_norms(dnorms) # Missing values if sync_missing_values and self.missing_value != obsmanager.missing_value: obsmanager.missing_value = self.missing_value # Check projection oens = self.project_on_obs(obsmanager) poens = obsmanager.restack(oens) mask = N.isclose(poens, obsmanager.missing_value) if mask.ndim > 1: mask = mask.any(axis=1) valid = ~mask if not valid.any(): raise SONATError("Ensemble does project at all onto observations") if not valid.all(): self.warning("Ensemble does not fully projects onto observations") return dict(ens_on_obs=oens, packed_ens_on_obs=poens, packed_ens_on_obs_valid=valid)
"""Test the fortran function :f:func:`nearest2d`""" from vcmq import N, P, code_file_name, P, os, rotate_grid, add_grid, meshbounds from vacumm.misc.grid._interp_ import nearest2d # Input grid gridi = rotate_grid((N.arange(5), N.arange(4)), 30) xxi = gridi.getLongitude()[:].filled() yyi = gridi.getLatitude()[:].filled() vari = N.resize(yyi, (20, )+ yyi.shape) nb = 10 xxbi, yybi = meshbounds(xxi, yyi) # Output grid grido = rotate_grid((N.linspace(0, 6, 50)-1, N.linspace(0, 4, 35)+1.), -20) xxo = grido.getLongitude()[:].filled() yyo = grido.getLatitude()[:].filled() xxbo, yybo = meshbounds(xxo, yyo) # Nearest varo = nearest2d(vari, xxi, yyi, xxo, yyo, nb) # Plot vmin = varo.min() vmax = varo.max() P.figure(figsize=(8, 4)) P.subplot(121, aspect=1) P.pcolor(xxbi, yybi, vari[0], vmin=vmin, vmax=vmax) add_grid(grido) P.title('original') P.subplot(122, aspect=1)
var1.getAxis(0).id = 'time' var1.getAxis(1).id = 'lat' var1.getAxis(1).designateLatitude() var1.getAxis(2).id = 'lon' var1.getAxis(2).designateLongitude() var1.units = 'm' var1.id = 'ssh' var2 = var1.clone() var2[:] = N.random.random((nt, ny, nx)) var1[3:13, :1, :1] = MV2.masked var2[5:15, -1:, -1:] = MV2.masked var2.long_name = 'Sea level' var2.id = 'sla' mask = var1.mask|var2.mask # common mask vmax = var2.max() bins = N.linspace(-0.1*vmax, 0.9*vmax, 14) nbins = len(bins) restart_file5 = code_file_name(ext='5.nc') restart_file7 = code_file_name(ext='7.nc') print restart_file5 # Normal sab = StatAccum(tall=True, sall=True, bins=bins) sab += var1[:5], var2[:5] #print sab.get_tmean() # Dump sab.dump(restart_file5) sab += var1[5:7], var2[5:7] sab.dump(restart_file7)
"""Test function :func:`~vacumm.misc.grid.kriging.krig` for grid refinement""" nxi = 15 nyi = 10 r = 3 from vcmq import P, savefigs, code_file_name, N, auto_scale, add_grid from vacumm.misc.grid.kriging import gridded_gauss3, random_gauss3, random_points, krig # Generate random gridded field xi, yi, zzi = gridded_gauss3(nx=nxi, ny=nyi) xxi, yyi = N.meshgrid(xi, yi) # Refined grid xo = N.linspace(xi[0], xi[-1], (nxi-1)*r+1) yo = N.linspace(yi[0], yi[-1], (nyi-1)*r+1) xxo, yyo = N.meshgrid(xo, yo) # Interpolate zzo = krig(xxi.ravel(), yyi.ravel(), zzi.ravel(), xxo.ravel(), yyo.ravel()) zzo.shape = xxo.shape # Section P.figure(figsize=(8, 4)) 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()
nx = ny = 50 np = 500 mtype = 'gauss' distmax = 2.5 from vcmq import N, P, code_file_name, savefigs from vacumm.misc.grid.kriging import gridded_gauss3, random_gauss3, variogram, variogram_fit # Generate random field xxg, yyg, zzg = gridded_gauss3(nx=nx, ny=ny) x, y, z = random_gauss3(np=np) # Variogram from data d, v = variogram(x, y, z, distmax=distmax) # Variogram fit with null nugget vm = variogram_fit(x, y, z, mtype, n=0, distmax=distmax) D = N.linspace(0, d.max()) V = vm(D) # Compare P.figure(figsize=(6, 4)) P.title('Variogram') P.plot(d, v, 'ob', label='From data') P.plot(D, V, '-r', label='Fitted model (%s)' % mtype) P.legend(loc='best') P.ylim(ymin=0) savefigs(code_file_name(ext=False), pdf=True, verbose=False) P.close()
"""Test the fortran function :f:func:`interp1dxx`""" from vcmq import N, P,meshcells, minmax, code_file_name, os from vacumm.misc.grid._interp_ import interp1dxx nx = nyi = 10 mv = 1.e20 u, v = N.mgrid[-3:3:nx*1j, -3:3:10j]-2 vari = N.ma.asarray(u**2+v**2) vari.set_fill_value(mv) yi = N.linspace(-1000.,0., nyi) yo = N.linspace(-1200, 100, 30.) vari[nx/3:2*nx/3, nyi/3:2*nyi/3] = N.ma.masked x = N.arange(nx) dyi = (yi[1]-yi[0])*0.49 dyo = (yo[1]-yo[0])*0.49 yyi = N.resize(yi, vari.shape)+N.random.uniform(-dyi, dyi, vari.shape) yyo = N.resize(yo, (nx, len(yo)))+N.random.uniform(-dyo, dyo, (nx, len(yo))) yyib, xxib = meshcells(yyi, x) yyob, xxob = meshcells(yyo, x) varon = N.ma.masked_values(interp1dxx(vari.filled(), yyi, yyo, mv, 0, extrap=0), mv) varol = N.ma.masked_values(interp1dxx(vari.filled(), yyi, yyo, mv, 1, extrap=0), mv) varoh = N.ma.masked_values(interp1dxx(vari.filled(), yyi, yyo, mv, 3, extrap=0), mv) kw = dict(vmin=vari.min(), vmax=vari.max()) axlims = [x[0], x[-1], yo[0], yo[-1]] P.figure(figsize=(8, 8)) P.subplot(221) P.pcolor(xxib, yyib, vari) P.axis(axlims)
"""Test :func:`~vacumm.misc.plot.hov2` with a TZ variable""" # Imports from vcmq import N, MV2, create_dep, create_time, hov2, os, rc, code_file_name # Init data with z 1D nt = 10 nz = 8 var = N.dot(N.hanning(nt).reshape(nt, 1), N.hanning(nz).reshape(1, nz)) var = MV2.array(var) time = create_time((0., nt), units="days since 2000") z1d = create_dep((-nz+1, 1.)) var.setAxis(0, time) var.setAxis(1, z1d) z2d = N.resize(z1d, var.shape) z2d *= N.resize((N.arange(1., nt+1)/nt).reshape(1, nt), (nz, nt)).T # Plot with z 1D rc('font', size=8) kw = dict(show=False, bgcolor='0.5', date_fmt="%a") hov2(var, subplot=211, **kw) # Plot with z 2D figfile = code_file_name(ext='png') if os.path.exists(figfile): os.remove(figfile) hov2(var, xaxis=z2d, subplot=212, twin='x', savefig=figfile, close=True, **kw) # Unittest result = dict(files=figfile)
from vcmq import create_axis, isaxis, coord2slice, cdms2, N def subaxis2slice(cdaxis, values): if cdms2.isVariable(cdaxis): return [ subaxis2index(cdax, vv) for cdax, vv in zip(cdaxis.getAxisList(), values) ] cdaxis = create_axis(cdaxis) ijk = cdaxis.mapIntervalExt((values[0], values[-1], 'cc')) if ijk is None: return return slice(*ijk) cdaxis = create_axis(N.linspace(0, 11., 17)) subaxis = cdaxis[2:7] res = subaxis2slice(cdaxis, subaxis)
"""Test function :func:`~vacumm.misc.grid.kriging.krig` for grid refinement""" nxi = 15 nyi = 10 r = 3 from vcmq import P, savefigs, code_file_name, N, auto_scale, add_grid from vacumm.misc.grid.kriging import gridded_gauss3, random_gauss3, random_points, krig # Generate random gridded field xi, yi, zzi = gridded_gauss3(nx=nxi, ny=nyi) xxi, yyi = N.meshgrid(xi, yi) # Refined grid xo = N.linspace(xi[0], xi[-1], (nxi - 1) * r + 1) yo = N.linspace(yi[0], yi[-1], (nyi - 1) * r + 1) xxo, yyo = N.meshgrid(xo, yo) # Interpolate zzo = krig(xxi.ravel(), yyi.ravel(), zzi.ravel(), xxo.ravel(), yyo.ravel()) zzo.shape = xxo.shape # Section P.figure(figsize=(8, 4)) iyis = [3, 4] for iyi in iyis: label = iyi == iyis[0] P.plot(xi, zzi[iyi], 'ob-', markersize=8,
"""Test fortran function :f:func:`dstwgt2dto1dc`""" from vcmq import P, N, code_file_name, os, add_grid, rotate_grid from vacumm.misc.grid._interp_ import dstwgt2dto1dc # Input grid and data nxy = 15 xi = N.arange(nxy*1.) yi = N.arange(nxy*1.) gridi = rotate_grid((xi, yi), 30) xxi = gridi.getLongitude().getValue() yyi = gridi.getLatitude().getValue() zzi = N.ma.array(yyi) zzi[int(nxy*0.3):int(nxy*0.8), int(nxy*0.3):int(nxy*0.8)] = N.ma.masked zzi.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 = zzi.get_fill_value() zo = dstwgt2dto1dc(xxi,yyi,zzi.filled(mv),xo,yo,mv) zo = N.ma.masked_values(zo, mv) # Plot kw = dict(vmin=zzi.min(), vmax=zzi.max()) P.figure(figsize=(6, 6)) P.subplot(111, aspect=1)
"""Test the traditionnal CDAT regrid2 regridder""" from vcmq import MV2, create_grid, meshbounds, P, add_grid, N, bounds1d, plot2d, savefigs,code_file_name from regrid2 import Horizontal # Input nx, ny = 6, 4 vari = MV2.array(N.arange(nx*ny*1.).reshape(ny, nx), fill_value=1e20) xi = vari.getAxis(-1) xi[:] *= 2 yi = vari.getAxis(-2) yi[:] *= 3 xi.designateLongitude() yi.designateLatitude() xi.setBounds(bounds1d(xi)) yi.setBounds(bounds1d(yi)) vari[1:2, 2:4] = MV2.masked gridi = vari.getGrid() # Output grido = create_grid(xi[:]+2*2.5, yi[:]+3*1.5) xo = grido.getLongitude() yo = grido.getLatitude() xo.setBounds(bounds1d(xo)) yo.setBounds(bounds1d(yo)) xxob, yyob = meshbounds(xo, yo) # Regridding varo, wo = vari.regrid(grido, tool='regrid2', returnTuple=1)
"""Test the fortran function :f:func:`cellerr1d`, :f:func:`cellerr1dx` et :f:func:`cellerr1dxx`""" from vcmq import cdms2, data_sample, N, P, meshcells, minmax, code_file_name, os from vacumm.misc.grid._interp_ import cellerr1d, cellerr1dx, cellerr1dxx # Academic example pure 1d # - data yi = N.array([-2., 2., 6. , 12., 15.]) vari = N.ma.array(yi) vari[-1] = N.ma.masked errm = N.ma.array([1, 2, 1., 2., 3.]) yo = N.array([5., 15]) mv = 1e20 vari = vari.reshape(1, -1).filled(mv) errm = errm.reshape(1, -1).filled(mv) errl = N.ones(1) # - interp varo, erro = cellerr1d(vari, yi, yo, mv, errm, errl) # - truth errot = N.array([ 1/N.sqrt( 1/(errm[0, 1]**2+N.abs(yo[0]-yi[1])*errl[0]) + 1/(errm[0, 2]**2+N.abs(yo[0]-yi[2])*errl[0])), N.sqrt(errm[0, 3]**2+N.abs(yo[1]-yi[3])*errl[0]) ]) varot = N.array([ vari[0, 1]/(errm[0, 1]**2+N.abs(yo[0]-yi[1])*errl[0]) + vari[0, 2]/(errm[0, 2]**2+N.abs(yo[0]-yi[2])*errl[0]), vari[0, 3]/(errm[0, 3]**2+N.abs(yo[1]-yi[3])*errl[0]) ])*errot**2 # - check result = [(N.testing.assert_allclose, [erro.ravel(), errot]),
"""Test the fortran function :f:func:`curv2rect`""" from vcmq import N, P, code_file_name, P, os from vacumm.misc.grid._interp_ import curv2rect # Input x1, y1 = 0., 0. x2, y2 = 3., 1. x3, y3 = 2., 4. x4, y4 = -1., 2. # 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')
"""Test :meth:`vacumm.data.misc.arakawa.CGrid.interp`""" from vcmq import MV2, N, create_grid, create_dep, set_grid, map2, \ code_file_name, CGrid, minmax, curve2, add_grid # Initial variable grid = create_grid(N.arange(-7, 0.), N.arange(43, 50.)) dep = create_dep([-5000, -3000, -2000, -1000, -500, -300, -200, -100.]) var = {} var['t'] = MV2.reshape(N.arange(grid.size()*len(dep))*1., (len(dep), )+grid.shape) set_grid(var['t'], grid) var['t'].setAxis(0, dep) # Arakawa manager ag = CGrid() # Interpolations for p in 'u', 'v', 'f', 'w': var[p] = ag.interp(var['t'], 't', p, mode='extrap') # Surface plots vmin, vmax = minmax(*[var[p][-1] for p in ['u', 'v', 'f']]) kw = dict(show=False, res=None, vmin=vmin, vmax=vmax, colorbar=False, grid=False, cmap='jet') m = map2(var['t'][-1], fill='pcolor', title='Interpolations on an Arakawa C grid: T->U/V/F', **kw) add_grid(var['t'], linestyle='-') kw.update(fill='scatter', contour=False, fill_s=60) markers = dict(u='>', v='^', f='D', t='o') for p in 't', 'u', 'v', 'f': m = map2(var[p][-1], fill_marker=markers[p], shadow=True, zorder=100, **kw)
"""Test the fortran function :f:func:`curv2rel_single`""" from vcmq import N, P, code_file_name, P, os, meshbounds from vacumm.misc.grid._interp_ import curv2rel_single # Input grid x0, y0 = 0., 2. nxi = 3 nyi = 3 dxi = (2., 2.) dyi = (-2., 2.) xxi = N.zeros((nyi, nxi)) xxi[0] = x0 + N.arange(nxi) * dxi[0] for j in range(1, nyi): xxi[j] = xxi[j-1] + dyi[0] yyi = N.zeros((nyi, nxi)) yyi[:, 0] = y0 + N.arange(nyi) * dyi[1] for j in range(1, nyi): yyi[:, j] = yyi[:, j-1] + dxi[1] xxbi, yybi = meshbounds(xxi, yyi) relpos2index = lambda fi, fj, nyi: fj * nyi + fi ii, jj = N.meshgrid(N.arange(nxi)+.5, N.arange(nyi)+.5) iib, jjb = meshbounds(ii, jj) zzi = relpos2index(ii, jj, nyi) zzbi = relpos2index(iib, jjb, nyi) # Input random points N.random.seed(0) np = 100 xxo = N.random.random(np)*(xxbi.max()-xxbi.min()) + xxbi.min() yyo = N.random.random(np)*(yybi.max()-yybi.min()) + yybi.min()
"""Create fake observation netcdf files """ from vcmq import (cdms2, MV2, os, create_dep, create_lon, create_lat, N, masked_polygon, map2) cdms2.setAutoBounds(False) # Profiles ncfile = '../data/obs.profiles.nc' lons = MV2.array([-5.8, -5.7, -4.6, -2.8], id='lon') lats = MV2.array([48.1, 47.5, 47.4, 47.3], id='lat') daxis = create_dep((-100., 1, 5.)) nz = len(daxis) np = len(lons) temp_error = N.resize([.2, .5, .3, .8], (nz, np)) sal_error = N.resize([.3, .1, .15, .4], (nz, np)) temp_error = MV2.array(temp_error, id='temp_error') sal_error = MV2.array(sal_error, id='sal_error') temp_error[:nz / 2, 2] = MV2.masked sal_error[:nz / 2, 2] = MV2.masked temp_error[:3 * nz / 4, 3] = MV2.masked sal_error[:3 * nz / 4, 3] = MV2.masked mob = MV2.array([0, 1, 1, 0], id='mobility', fill_value=-1) paxis = lons.getAxis(0) paxis.id = 'station' axes = [daxis, paxis] f = cdms2.open(ncfile, 'w') for var in lons, lats, mob, temp_error, sal_error: var.setAxis(-1, paxis) if var.ndim == 2:
from vcmq import SimpleCloudKriger, N, P, variogram_model, code_file_name # Kriging params sill = 9. range = 40 farvalue = 20 # Input xi = [20., 26., 50., 70] yi = [20, 26., 70., 20] zi = [15., 6., 2., 4.] # Output positions nx = ny = 101 xg = N.linspace(1, 100, 101) yg = N.linspace(1, 100, 101) # Some inits xi = N.array(xi) yi = N.array(yi) zi = N.array(zi) xxg, yyg = N.meshgrid(xg, yg) xo = xxg.ravel() yo = yyg.ravel() vgm = variogram_model('linear', n=0, s=sill, r=range) # Setup the kriger sck = SimpleCloudKriger(xi, yi, zi, vgf=vgm, farvalue=farvalue) # Interpolate
"""Test function :func:`~vacumm.misc.plot.taylor` in different configurations""" from vcmq import taylor, rc, code_file_name, MV2, N # Make some data nt = 50 nens = 50 # - references ref1 = MV2.sin(MV2.arange(nt, dtype='f')) * 10. ref2 = ref1 * 1.2 # - ensemble 1 dat1 = MV2.resize(ref1, (nens, nt)) + N.resize(N.random.rand(nt * nens), (nens, nt)) * 20. dat1.long_name = 'Set 1' dat1.units = 'meters' # - modele 2 dat2 = MV2.resize(ref2, (nens, nt)) + N.resize(N.random.rand(nt * nens), (nens, nt)) * 40. dat2.long_name = 'Set 2' # Plot rc('font', size=8) kwplot = dict(figsize=(5, 5), savefigs_pdf=False, show=False, close=True, savefigs_verbose=False) basename = code_file_name(ext=False) + '_%i' # - single variable taylor(dat1[0], ref1, savefigs=basename % 0,
var1.getAxis(0).id = 'time' var1.getAxis(1).id = 'lat' var1.getAxis(1).designateLatitude() var1.getAxis(2).id = 'lon' var1.getAxis(2).designateLongitude() var1.units = 'm' var1.id = 'ssh' var2 = var1.clone() var2[:] = N.random.random((nt, ny, nx)) var1[3:13, :1, :1] = MV2.masked var2[5:15, -1:, -1:] = MV2.masked var2.long_name = 'Sea level' var2.id = 'sla' mask = var1.mask | var2.mask # common mask vmax = var2.max() bins = N.linspace(-0.1 * vmax, 0.9 * vmax, 14) nbins = len(bins) restart_file5 = code_file_name(ext='5.nc') restart_file7 = code_file_name(ext='7.nc') print restart_file5 # Normal sab = StatAccum(tall=True, sall=True, bins=bins) sab += var1[:5], var2[:5] #print sab.get_tmean() # Dump sab.dump(restart_file5) sab += var1[5:7], var2[5:7] sab.dump(restart_file7)
"""Test the fortran function :f:func:`cellerr1d`, :f:func:`cellerr1dx` et :f:func:`cellerr1dxx`""" from vcmq import cdms2, data_sample, N, P, meshcells, minmax, code_file_name, os from vacumm.misc.grid._interp_ import cellerr1d, cellerr1dx, cellerr1dxx # Academic example pure 1d # - data yi = N.array([-2., 2., 6., 12., 15.]) vari = N.ma.array(yi) vari[-1] = N.ma.masked errm = N.ma.array([1, 2, 1., 2., 3.]) yo = N.array([5., 15]) mv = 1e20 vari = vari.reshape(1, -1).filled(mv) errm = errm.reshape(1, -1).filled(mv) errl = N.ones(1) # - interp varo, erro = cellerr1d(vari, yi, yo, mv, errm, errl) # - truth errot = N.array([ 1 / N.sqrt(1 / (errm[0, 1]**2 + N.abs(yo[0] - yi[1]) * errl[0]) + 1 / (errm[0, 2]**2 + N.abs(yo[0] - yi[2]) * errl[0])), N.sqrt(errm[0, 3]**2 + N.abs(yo[1] - yi[3]) * errl[0]) ]) varot = N.array([ vari[0, 1] / (errm[0, 1]**2 + N.abs(yo[0] - yi[1]) * errl[0]) + vari[0, 2] / (errm[0, 2]**2 + N.abs(yo[0] - yi[2]) * errl[0]), vari[0, 3] / (errm[0, 3]**2 + N.abs(yo[1] - yi[3]) * errl[0]) ]) * errot**2 # - check result = [(N.testing.assert_allclose, [erro.ravel(), errot]),
"""Compare CDAT regridding speed with rectangular and rectangular grids""" config = { 'esmf':['linear', 'patch', 'conserv'], 'libcf':['linear'], } # Imports from vcmq import MV2, create_grid2d, code_file_name, os, CDATRegridder, N, set_grid, psinfo from vacumm.misc.grid import rotate_grid from time import time # Input nx = ny = 300 vari = MV2.array(N.arange(nx*ny*1.).reshape(ny, nx)) gridi = create_grid2d(vari.getAxis(1)[:]*50/nx, vari.getAxis(0)[:]*50/nx) set_grid(vari, gridi) # Output grid gridor = create_grid2d(vari.getAxis(1)[:]*0.09*50/nx, vari.getAxis(0)[:]*0.09*50/nx) gridoc = rotate_grid(gridi, 30) # Log logfile = code_file_name(ext='log') if os.path.exists(logfile): os.remove(logfile) f = open(logfile, 'w') print >>f, 'NY=%(ny)i, NX=%(nx)i'%locals() # Loop on methods for tool, methods in config.items():
"""Test the fortran function :f:func:`bilin`""" from vcmq import N, P, meshcells, minmax, code_file_name, os from vacumm.misc.grid._interp_ import dstwgt nxi = 15 nyi = 10 mv = 1.e20 u, v = N.mgrid[-3:3:nyi*1j, -3:3:nxi*1j]-2 vari = N.ma.asarray(u**2+v**2) vari.set_fill_value(mv) xi = N.arange(nxi) yi = N.arange(nyi) vari[int(nyi*0.4):int(nyi*0.4)+3, int(nxi*0.4):int(nxi*0.4)+2] = N.ma.masked xxib, yyib = meshcells(xi, yi) nxo = 40 nyo = 25 xo = N.linspace(int(nxi*0.2),int(nxi*1.2),nxo) yo = N.linspace(int(-nyi*0.2),int(nyi*0.8),nyo) xxob, yyob = meshcells(xo, yo) vari.shape = (1, )+vari.shape varo = N.ma.masked_values(dstwgt(vari.filled(), xi, yi, xo, yo, mv, 0), mv) kw = dict(vmin=vari.min(), vmax=vari.max()) axlims = [min(xi.min(), xo.min()), max(xi.max(), xo.max()), min(yi.min(), yo.min()), max(yi.max(), yo.max())] P.figure(figsize=(8, 4)) P.subplot(211) P.pcolor(xxib, yyib, vari[0], **kw)
"""Test :func:`~vacumm.misc.plot.section2` in quiver mode""" # Imports from vcmq import create_lon, N, MV2, create_dep, os, code_file_name, section2 from vacumm.misc.phys.units import deg2m # Init data with z 1D nz = 11 nx = 11 x = create_lon(N.arange(nx)) xm = deg2m(x[:],lat=45.) # meters dx = xm[:].ptp() z = create_dep((-nz+1, 1.), units='m', long_name='Depth') dz = z[:].ptp() scale = dz/dx u = MV2.ones((nz,nx)) # 1 m/s w = u*scale # 1 m/s * scale for var in u,w: var.setAxis(0, z) var.setAxis(1, x) var.units = 'm/s' # Plot figfile = code_file_name(ext='png') if os.path.exists(figfile): os.remove(figfile) s = section2((u,w), quiver_norm=1, fill=False, show=False, savefig=figfile, close=False, axes_aspect=1) # Result result = dict(files=figfile)
nx = ny = 50 np = 500 mtype = 'gauss' dmax = 2.5 from vcmq import N, P, code_file_name, savefigs from vacumm.misc.grid.kriging import gridded_gauss3, random_gauss3, variogram, variogram_fit # Generate random field xxg, yyg, zzg = gridded_gauss3(nx=nx, ny=ny) x, y, z = random_gauss3(np=np) # Variogram from data d, v = variogram(x, y, z, dmax=dmax) # Variogram fit with null nugget vm = variogram_fit(x, y, z, mtype, n=0, dmax=dmax) D = N.linspace(0, d.max()) V = vm(D) # Compare P.figure(figsize=(6, 4)) P.title('Variogram') P.plot(d, v, 'ob', label='From data') P.plot(D, V, '-r', label='Fitted model (%s)'%mtype) P.legend(loc='best') P.ylim(ymin=0) savefigs(code_file_name(ext=False), pdf=True, verbose=False) P.close()
#!/usr/bin/env python # -*- coding: utf8 -*- """Axes et grilles avec VACUMM""" from vcmq import N, MV2, create_lon, create_lat, create_grid, isgrid, isrect, islon, set_grid, get_grid, get_axis, varsel, resol, curv2rect, get_xy, meshgrid, meshcells, create_dep, isregular, P, rotate_grid, shiftgrid, extendgrid, create_axes2d, isdepthup, coord2slice, monotonic, xshift, depth2dz, get_closest # Créer # - axes lon = create_lon((2., 11, 2.)) # -> SPECIFIEZ LE LONG_NAME lat = create_lat(N.arange(43, 50.)) dep = create_dep((0., 10)) # -> AFFICHEZ LES INFOS xx, yy = N.meshgrid(N.arange(5.), N.arange(4.)) lon2d, lat2d = create_axes2d(xx, yy) ii = lon2d.getAxis(1) # - grille grid = create_grid(lon, lat) # -> ESSAYEZ AVEC LON EXPLICITE gridc = create_grid(lon2d, lat2d) # Verifier print islon(lon) print isgrid(grid) # -> TEST PARAM CURV=... print isrect(gridc) # -> CREEZ GRILLE NON RECT ET RETESTER print isdepthup(dep) # -> TESTEZ EN CHANGEANT ATTRIBUT POSITIVE ET VALEURS print isregular(lon) # Affecter
def create_Dthnc(self, fileout, TimeSeries): if '2D' in fileout: self.i23d = 2 else: self.i23d = 3 # create file if self.i23d == 3: Nlev = self.zz.shape[1] else: Nlev = 1 time_Series, nc = create_ncTH( fileout, len(self.llon), Nlev, self.ivs, np.round((TimeSeries - TimeSeries[0]) * 24 * 3600)) for n in range(0, len(TimeSeries)): tin = create_time(np.ones(len(self.llon) * Nlev) * (TimeSeries[n] + 1), units='days since 1-1-1') total = np.zeros(shape=(self.ivs, len(self.llon), Nlev)) # get tide if self.tidal: var = self.HC.keys() for i, v in enumerate(sorted(var)): # horizontal interpolation tmp = get_tide(self.constidx, self.tfreq, self.HC[v], np.array(TimeSeries[n]), self.lat0) if self.i23d > 2: # vertical interpolation tmp = vertical_extrapolation(tmp, self.zz, z0=self.z0) total[i, :, :] = total[i, :, :] + tmp if self.residual: var = self.res_vars for i, v in enumerate(sorted(var)): arri = self.res_file[v][:] if self.i23d > 2: dep = create_depth(arri.getAxis(1)[:]) extra = create_axis(N.arange(1), id='member') arri2 = np.tile(arri, [1, 1, 1, 1, 1]) arri3 = MV2.array(arri2, axes=[ extra, arri.getAxis(0), dep, arri.getAxis(2), arri.getAxis(3) ], copy=False, fill_value=1e20) zi = arri.getAxis(1)[:] if np.mean(zi) > 0: zi = zi * -1 tb = grid2xy(arri3, xo=np.tile(self.llon, [Nlev, 1]).T.flatten(), yo=np.tile(self.llat, [Nlev, 1]).T.flatten(), zo=self.zz.flatten(), method='linear', to=tin, zi=zi) else: tb = grid2xy(arri, xo=self.llon, yo=self.llat, method='linear', to=tin) if np.any(tb.mask == True): bad = tb.mask == True if len(bad.shape) > 1: bad = bad[0, :] tin_bad = create_time(np.ones(len(bad)) * (TimeSeries[n] + 1), units='days since 1-1-1') if self.i23d > 2: llon = np.tile(self.llon, [Nlev, 1]).T.flatten() llat = np.tile(self.llat, [Nlev, 1]).T.flatten() zz = self.zz.flatten() zi = arri.getAxis(1)[:] if np.mean(zi) > 0: zi = zi * -1 tb[0, bad] = grid2xy(arri3, xo=llon[bad], yo=llat[bad], zo=zz[bad], method='nearest', to=tin_bad, zi=zi) else: tb[bad] = grid2xy( arri, xo=np.array(self.llon)[bad].tolist(), yo=np.array(self.llat)[bad].tolist(), method='nearest', to=tin_bad) if np.any(tb.mask == True): print('probleme') total[i, :, :] = total[i, :, :] + np.reshape( tb, (len(self.llon), Nlev)) total = np.transpose(total, (1, 2, 0)) if np.isnan(total).any(): import pdb pdb.set_trace() if n % 100 == 0: self.logger.info( 'For timestep=%.f, max=%.4f, min=%.4f , max abs diff=%.4f' % (TimeSeries[n], total.max(), total.min(), abs(np.diff(total, n=1, axis=0)).max())) time_Series[n, :, :, :] = total nc.close()
lon1d = create_lon((0, 10)) result.append(('AssertEqual', (coord2slice(lon1d, lon=(2.5, 4., 'cc')), slice(3, 5, 1)))) result.append(('AssertEqual', (coord2slice(lon1d, lon=(2.5, 4., 'ccb')), slice(2, 5, 1)))) result.append(('AssertEqual', (coord2slice(lon1d, lon=slice(3, 6)), slice(3, 6, None)))) result.append(('AssertEqual', (coord2slice(lon1d, lat=(6, 8)), slice(0, 10, 1)))) result.append(('AssertEqual', (coord2slice(lon1d, lon=(60, 70)), None))) # Rect grid grid = create_grid((0, 10.), (20, 30.)) result.append(('AssertEqual', (coord2slice(grid, lon=(0., 3.5), lat=slice(3, 5)), (slice(0, 4, 1), slice(3, 5, None), None)))) result.append(('AssertEqual', (coord2slice(grid, lat=(21,21, 'ccb')), (slice(0, 10, 1), slice(1, 2, 1), None)))) # 2D axis lon2d = N.empty((10, 10.)) for i in xrange(10): lon2d[i] = lon1d[:]+i lat2d = N.resize((N.arange(10)+20), (10, 10)).T lon2d, lat2d = create_axes2d(lon2d, lat2d) kw = dict(show=False) plot(lon2d, lat2d, lon2d, 'lon2d', figfiles, figfile, lon=(2, 4), **kw) plot(lon2d, lat2d, lon2d, 'lon2d', figfiles, figfile, lon=(2, 4), lat=slice(0, 2), **kw) plot(lon2d, lat2d, lat2d, 'lat2d', figfiles, figfile, lat=(22, 26.6,'ccb'), **kw) # Curv grid grid = create_grid(lon2d, lat2d) plot(lon2d, lat2d, grid, 'grid', figfiles, figfile, lon=(8, 11, 'cc'), lat=(21.9, 26., 'cc'), **kw) plot(lon2d, lat2d, grid, 'grid', figfiles, figfile, lon=slice(2, 5), lat=(23.4, 23.6, 'ccb'), **kw) res = coord2slice(grid,lon=(8,8,'ccb'),lat=(24,24,'ccb')) result.append(('AssertEqual', (res[:2], (slice(3, 6, 1), slice(4, 5, 1)))))
nexz = 2 nxi = 7 nyi = 6 nzi = 5 nti = 4 mv = 1.e20 no = 1 #no = 1 vfunc = lambda t, z, y, x: (1*x + 2.35*y + 3.24*z -0.65*t) tti, zzi, yyi, xxi = N.mgrid[0:nti-1:nti*1j, 0:nzi-1:nzi*1j, 0:nyi-1:nyi*1j, 0:nxi-1:nxi*1j] zzi = zzi[None] zzi = N.repeat(zzi, nexz, axis=0) # %% Pure 1D axes xi = xxi[0, 0, 0:1, :] # (nyix=1,nxi) yi = yyi[0, 0, :, 0:1] # (nyi,nxiy=1) zi = zzi[0:1, 0:1, :, 0:1, 0:1] # (nexz=1,ntiz=1,nzi,nyiz=1,nxiz=1) ti = tti[:, 0, 0, 0] # (nti) vi = vfunc(tti, zzi, yyi, xxi) vi = N.resize(vi, (nex, )+vi.shape[1:]) # (nex,nti,nzi,nyi,nxi) N.random.seed(0) xyztomin = -0.5 xo = N.random.uniform(xyztomin, nxi-1.5, no) yo = N.random.uniform(xyztomin, nyi-1.5, no)
sp = f("speed") spe = f("speed_error") f.close() # Create hourly time axis taxi = sp.getTime() taxi.toRelativeTime("hours since 2000") ctimesi = taxi.asComponentTime() ct0 = round_date(ctimesi[0], "hour") ct1 = round_date(ctimesi[-1], "hour") taxo = create_time(lindates(ct0, ct1, 1, "hour"), taxi.units) # Lag error # - estimation els = [] lags = N.arange(1, 6) for lag in lags: els.append(N.sqrt(((sp[lag:] - sp[:-lag]) ** 2).mean())) els = N.array(els) a, b, _, _, _ = linregress(lags, els) # - plot P.figure(figsize=(6, 6)) P.subplot(211) P.plot(lags, els, "o") P.plot([0, lags[-1]], [b, a * lags[-1] + b], "g") P.axhline(b, color="0.8", ls="--") P.ylim(ymin=0) P.xlabel("Lag [hour]") P.ylabel("Error [m s-1]") add_key(1) P.title("Linear lag error model")
"""Test :func:`~vacumm.misc.plot.plot2d` with specified cell bounds""" # Imports from vcmq import MV2, N, code_file_name, plot2d, os # Init var = MV2.reshape(MV2.arange(4*3), (3, 4)) var.units = 'Count' var.long_name = 'Navets' x = var.getAxis(1) x.units = 'km' x.long_name = 'Distance' y = var.getAxis(0) y.units = 'm' y.long_name = 'Height' y.designateLevel() # Bounds y[:] = [1.5, 3.5, 4.5] y2db = N.array([0., 3, 4, 5]) # Plot figfile = code_file_name(ext='png') if os.path.exists(figfile): os.remove(figfile) plot2d(var, savefig=figfile, show=False, close=True, y2db=y2db, fill='pcolor', xmin=-.5, xmax=3.5, ymin=0, ymax=5, cmap='jet') # Unittest result = {"files":figfile}
nyi,nxi = vari2d.shape #if rank==0: print 'expand in time and depth' vari = MV2.resize(vari2d, (nt, nz)+vari2d.shape) cp_props(vari2d, vari) #if rank==0: print 'grido' loni = gridi.getLongitude() lati = gridi.getLatitude() xib, yib = bounds2d(loni, lati) loni.setBounds(xib) lati.setBounds(yib) xi = loni.getValue() yi = lati.getValue() dx = N.diff(xi[0]).mean() dy = N.diff(yi[:, 0]).mean() xo = N.arange(xi.min()+10*dx, -30*dx+xi.max(), dx) yo = N.arange(yi.min()-20*dy, yi.max()-20*dy, dy) lono = cdms2.createAxis(xo) lono.designateLongitude() ; lono.units= 'degrees_east' lato = cdms2.createAxis(yo) lato.designateLatitude() ; lato.units = 'degrees_north' xob = bounds1d(lono) ; lono.setBounds(xob) yob = bounds1d(lato) ; lato.setBounds(yob) grido = cdms2.createRectGrid(lato, lono) xmin, xmax = minmax(loni.asma(),lono) ymin, ymax = minmax(lati.asma(), lato) nyo,nxo = grido.shape #print 'rank',rank basefile = code_file_name(ext=False)
"""Test :func:`~vacumm.misc.plot.section2` with a Z- variable""" # Imports from vcmq import N, MV2, cdms2, create_dep, rc, section2, code_file_name, os # Init data with z 1D nz = 8 nd = 10 var = N.dot(N.hanning(nz).reshape(nz, 1), N.hanning(nd).reshape(1, nd)) var = MV2.array(var) d = cdms2.createAxis(N.arange(nd)) d.units='km' d.long_name='Distance' z1d = create_dep((-nz+1, 1.)) var.setAxis(0, z1d) var.setAxis(1, d) z2d = N.resize(z1d[:].reshape(1, nz), (nd, nz)).T z2d *= N.arange(1., nd+1)/nd # Plot with z 1D rc('font', size=8) kw = dict(show=False, bgcolor='0.5') section2(var, subplot=211, **kw) # Plot with z 2D figfile = code_file_name(ext='png') if os.path.exists(figfile): os.remove(figfile) section2(var, yaxis=z2d, subplot=212, savefig=figfile, close=True, **kw) # Result result = dict(files=figfile)
"""Test fortran function :f:func:`dstwgt2dto1d`""" from vcmq import P, N, code_file_name, os, add_grid from vacumm.misc.grid._interp_ import dstwgt2dto1d # Input grid and data 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)
zi[mask] = N.ma.masked # - output xo = N.random.uniform(xmin, xmax, 500) yo = N.random.uniform(ymin, ymax, 500) zot = N.ma.masked_array(get_values(xo, yo)) mask = (xo > (xmin + xmax) * .5) & (yo > (ymin + ymax) * .5) zot[mask] = 1e20 zot[mask] = N.ma.masked # Call and plot kw = dict(vmin=zi.min(), vmax=zi.max()) P.figure(figsize=(8, 8)) P.subplot(221) trii = get_tri((xi, yi), ttype='mpl', mask=zi) P.tripcolor(trii, zi, **kw) cc = N.zeros(len(xi), '|S3') cc[:] = 'k' cc[zi.mask] = '.5' P.scatter(xi, yi, c=cc, s=7, marker='o') P.title('Input') for i, method in enumerate(['nearest', 'linear', 'cubic']): # Interp zo = xy2xy(xi, yi, zi, xo, yo, method=method) #[0] # Triangles trio = get_tri((xo, yo), ttype='mpl', mask=zo) # Plot P.subplot(2, 2, i + 2) P.tripcolor(trio, zo, **kw)
sp = f('speed') spe = f('speed_error') f.close() # Create hourly time axis taxi = sp.getTime() taxi.toRelativeTime('hours since 2000') ctimesi = taxi.asComponentTime() ct0 = round_date(ctimesi[0], 'hour') ct1 = round_date(ctimesi[-1], 'hour') taxo = create_time(lindates(ct0, ct1, 1, 'hour'), taxi.units) # Lag error # - estimation els = [] lags = N.arange(1, 6) for lag in lags: els.append(N.sqrt(((sp[lag:] - sp[:-lag])**2).mean())) els = N.array(els) a, b, _, _, _ = linregress(lags, els) # - plot P.figure(figsize=(6, 6)) P.subplot(211) P.plot(lags, els, 'o') P.plot([0, lags[-1]], [b, a * lags[-1] + b], 'g') P.axhline(b, color='0.8', ls='--') P.ylim(ymin=0) P.xlabel('Lag [hour]') P.ylabel('Error [m s-1]') add_key(1) P.title('Linear lag error model')
def get_values(xx, yy): return (N.sin(xx * N.pi / 6) * N.sin(yy * N.pi / 6)) * 100.
"""Test the fortran function :f:func:`extrap1d`""" from vcmq import N, P,meshcells, minmax from vacumm.misc.grid._interp_ import extrap1d mv = 999. vari = N.zeros((5,5))+mv vari[1,2:4] = [2,3] vari[2,1] = 1 vari[2,3] = 3 vari[3,3:] = [3,4] vari[4,:2] = [0,1] vari = N.asfortranarray(vari) varo0 = extrap1d(vari, mv, 0) varop1 = extrap1d(vari, mv, 1) varom1 = extrap1d(vari, mv, -1) varop2 = extrap1d(vari, mv, 2) result = [ ('AssertTrue', N.allclose(vari, varo0)), ('AssertTrue', N.allclose(varop1[:, -1], [999., 3., 3., 4., 1.])), ('AssertTrue', N.allclose(varom1[:, 0], [999., 2., 1., 3., 0.])), ('AssertTrue', N.allclose(varop1[:, -1], varop2[:, -1])), ('AssertTrue', N.allclose(varom1[:, 0], varop2[:, 0])), ]
"""Test the fortran function :f:func:`interp1dxx`""" from vcmq import N, P, meshcells, minmax, code_file_name, os from vacumm.misc.grid._interp_ import interp1dxx nx = nyi = 10 mv = 1.e20 u, v = N.mgrid[-3:3:nx * 1j, -3:3:10j] - 2 vari = N.ma.asarray(u**2 + v**2) vari.set_fill_value(mv) yi = N.linspace(-1000., 0., nyi) yo = N.linspace(-1200, 100, 30.) vari[nx / 3:2 * nx / 3, nyi / 3:2 * nyi / 3] = N.ma.masked x = N.arange(nx) dyi = (yi[1] - yi[0]) * 0.49 dyo = (yo[1] - yo[0]) * 0.49 yyi = N.resize(yi, vari.shape) + N.random.uniform(-dyi, dyi, vari.shape) yyo = N.resize(yo, (nx, len(yo))) + N.random.uniform(-dyo, dyo, (nx, len(yo))) yyib, xxib = meshcells(yyi, x) yyob, xxob = meshcells(yyo, x) varon = N.ma.masked_values( interp1dxx(vari.filled(), yyi, yyo, mv, 0, extrap=0), mv) varol = N.ma.masked_values( interp1dxx(vari.filled(), yyi, yyo, mv, 1, extrap=0), mv) varoh = N.ma.masked_values( interp1dxx(vari.filled(), yyi, yyo, mv, 3, extrap=0), mv) kw = dict(vmin=vari.min(), vmax=vari.max()) axlims = [x[0], x[-1], yo[0], yo[-1]] P.figure(figsize=(8, 8)) P.subplot(221)
"""Test the :func:`~vacumm.misc.grid.masking.polygons` function""" from vcmq import N, P, polygons, create_polygon, plot_polygon, create_grid from _geoslib import Polygon # Data xx = N.array([0., 5., 4., 1.]) yy = N.array([0., 0., 2., 2.]) clip = [-3, -3, 0, 0] xaxis = yaxis = N.linspace(-2., 2., 20.) def plot_polygons(polys, **kwargs): for p in polys: plot_polygon(p, **kwargs) ## Single known argument #pp0 = polygons(Polygon(N.array([xx, yy]).T)) # ## Classic data #pp0 = polygons([N.array([xx, yy]), N.array([xx, yy]).T+6.]) # ## Classic with projection #proj = lambda x, y: (x*1.5, y*1.5) #pp1 = polygons([N.array([xx, yy])]) # ## Classic with clipping #pp2 = polygons([-N.array([xx, yy])], clip=clip) # From grid pp3 = polygons([create_grid(xaxis, yaxis)])
"""Test the :func:`~vacumm.misc.grid.masking.create_polygon` function""" from vcmq import N, P, create_polygon, plot_polygon, code_file_name from _geoslib import Polygon # Data xx = N.array([0., 5., 4., 1.]) yy = N.array([0., 0., 2., 2.]) # From Polygon P.figure(figsize=(5, 5)) p0 = create_polygon(Polygon(N.array([xx, yy]).T)) plot_polygon(p0, color='b', label='From poly') # From data p1 = create_polygon(N.array([xx, yy]).T-.25) plot_polygon(p1, color='r', label='From data') # From transposed data p2 = create_polygon(N.array([xx, yy])-0.5) plot_polygon(p2, color='g', label='From transposed data') # From min/max p3 = create_polygon([xx.min(), yy.min(), xx.max(), yy.max()]) plot_polygon(p3, color='m', label='From min/max') # With projection proj = lambda x, y: (x*1.5, y*1.5) p4 = create_polygon(p0, proj=proj) plot_polygon(p4, color='cyan', label='With projection') # Save
"""Test the :func:`~vacumm.misc.grid.regridding.regrid2d` function""" from vcmq import P, N, MV2, code_file_name, os, add_grid, rotate_grid, set_grid, \ create_grid, rc, rcdefaults, plot2d from vacumm.misc.grid.regridding import regrid2d # Input grid and data nxi = 20 nyi = 15 # - rect xi = N.arange(nxi*1.) yi = N.arange(nyi*1.) gridri = create_grid(xi, yi) xxri, yyri = N.meshgrid(xi, yi) zzri = N.ma.array(yyri) zzri[int(nyi*0.3):int(nyi*0.6), int(nxi*0.3):int(nxi*0.6)] = N.ma.masked varri = MV2.asarray(zzri) set_grid(varri, gridri) # - curv gridci = rotate_grid(gridri, 30) xxci = gridci.getLongitude().getValue() yyci = gridci.getLatitude().getValue() zzci = N.ma.array(yyci) zzci[int(nyi*0.3):int(nyi*0.6), int(nxi*0.3):int(nxi*0.6)] = N.ma.masked varci = MV2.asarray(zzci) set_grid(varci, gridci) # Output positions nxo = 25 nyo = 18 # - rect
"""Test the fortran function :f:func:`nearest2d`""" from vcmq import N, P, code_file_name, P, os, rotate_grid, add_grid, meshbounds from vacumm.misc.grid._interp_ import nearest2d # Input grid gridi = rotate_grid((N.arange(5), N.arange(4)), 30) xxi = gridi.getLongitude()[:].filled() yyi = gridi.getLatitude()[:].filled() vari = N.resize(yyi, (20, ) + yyi.shape) nb = 10 xxbi, yybi = meshbounds(xxi, yyi) # Output grid grido = rotate_grid((N.linspace(0, 6, 50) - 1, N.linspace(0, 4, 35) + 1.), -20) xxo = grido.getLongitude()[:].filled() yyo = grido.getLatitude()[:].filled() xxbo, yybo = meshbounds(xxo, yyo) # Nearest varo = nearest2d(vari, xxi, yyi, xxo, yyo, nb) # Plot vmin = varo.min() vmax = varo.max() P.figure(figsize=(8, 4)) P.subplot(121, aspect=1) P.pcolor(xxbi, yybi, vari[0], vmin=vmin, vmax=vmax) add_grid(grido) P.title('original') P.subplot(122, aspect=1) P.pcolor(xxbo, yybo, varo[0], vmin=vmin, vmax=vmax)
"""Test :func:`~vacumm.misc.grid.regridding.extend1d` and :func:`~vacumm.misc.grid.regridding.extend2d`""" from vcmq import N, create_grid2d, P, rc, plot2d, MV2, set_grid, savefigs, code_file_name from vacumm.misc.grid.regridding import extend1d, extend2d # Input data nxi = 4 nyi = 3 xxi, yyi = N.meshgrid(N.arange(nxi) + .25, N.arange(nyi) - .25) for j in xrange(nyi): xxi[j, :] -= j * 0.5 for i in xrange(nxi): yyi[:, i] += i * 0.5 gridi = create_grid2d(xxi, yyi) # input cdms grid vari = MV2.array(N.arange(nyi * nxi).reshape(nyi, nxi)) + 10. set_grid(vari, gridi) # set grid and axes # Extend and plot rc('font', size=9) P.figure(figsize=(6, 6)) kw = dict(xmin=xxi.min() - 3, xmax=xxi.max() + 3, ymin=yyi.min() - 3, ymax=yyi.max() + 3, show=False, xhide='auto', yhide='auto') # - original plot2d(vari, title='Original', subplot=(2, 2, 1), **kw) # - extend1d for i, (axis, ext, mode) in enumerate([(-1, (2, 2), 'same'),
"""Test CDAT conservative regridding""" from vcmq import N, meshbounds, bounds1d, cdms2, MV2, rc, P, add_grid, rcdefaults, \ create_lon, create_lat, savefigs, code_file_name # Input grid x0, y0, nx, ny, dx, dy = 0., 0., 20, 15, 5., 5. x = x0 + N.arange(0, nx * dx, dx) y = y0 + N.arange(0, ny * dy, dy) xxbi, yybi = meshbounds(x, y) xb = bounds1d(x) yb = bounds1d(y) lon = create_lon(x) lat = create_lat(y) lon.setBounds(xb) lat.setBounds(yb) gridi = cdms2.createRectGrid(lat, lon) # Input data vari = MV2.ones(gridi.shape) * 50 vari[5:10, 7:13] = -10 #vari[:, -1] = MV2.masked # <<< THIS MAKES IT WORK! vari.setAxisList([gridi.getLatitude(), gridi.getLongitude()]) vari.setGrid(gridi) # Output grid grido, xxbo, yybo = gridi, xxbi, yybi # Regrid diag = {'dstAreaFractions': None} varo = vari.regrid(grido,
# Lecture de la température f =cdms2.open(data_sample('mars3d.tsigyx.nc')) data_in = f('TEMP', time=slice(0,2)) # T-YX (- = level) # Détermination des profondeurs d'après les sigma sigma_converter = NcSigma.factory(f) # détection auto et initialisation du convertisseur # -> VERIFIER QUE sigma_class EST BIEN SigmaGeneralized depths_in = sigma_converter.sigma_to_depths(selector=dict(time=slice(0,2))).filled() # lecture eta, etc + conversion # (Equivalent à depths_in = sigma_converter(selector=dict(time=slice(0,2))).filled()) f.close() # Creation de l'axe des profondeurs cibles depths = N.array([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20, 22,24,26,28,30,32,34,36,38,40,45,50,55,60,65,70,75,80,85,90,95,100,120,140,160]) depths = -depths[::-1] # croissantes et négatives depth_out = create_depth(depths) # Interpolation data_out = regrid1d(data_in, depth_out, axi=depths_in, axis=1, method='linear', extrap=1) # Plot kw = dict(vmin=10, vmax=14, xhide='auto', add_grid=True, ymax=0, fill='contourf') # FILL=PCOLOR ? section2(data_in[0, :, 10], yaxis=depths_in[0, :, 10], subplot=211, title='Sigma', show=False, **kw) s = section2(data_out[0, :, 10], subplot=212, title='Z', show=False, savefig=__file__, **kw)
for i, np in enumerate(nps): xc = P.rand(1)*dxy*2.5 yc = P.rand(1)*dxy*2.5 x = P.rand(np)*dxy+xc y = P.rand(np)*dxy+yc xx.extend(x) yy.extend(y) P.scatter(x, y, c=colors[i:i+1], s=40) P.title('Original (mixed clouds)') # Spliting indices, centroids = cloud_split(xx, yy, getdist=False, getcent=True, npmax=npmax) # Plot P.subplot(122, aspect=1) xx = N.array(xx) yy = N.array(yy) msize = 0 for i, select in enumerate(indices): x = xx[select] y = yy[select] P.scatter(x, y, c=colors[i:i+1], s=40, label='size=%i'%len(x)) P.scatter(centroids[i:i+1, 0], centroids[i:i+1, 1], c=colors[i:i+1], s=100, lw=2) msize = max(len(x), msize) P.legend(loc='upper left').get_frame().set_alpha(.5) P.title('Split clouds: max size=%i'%npmax) savefigs(code_file_name(ext='png'), pdf=True, verbose=False) #P.tight_layout() P.close() # For unitest
nexz = 2 nxi = 7 nyi = 6 nzi = 5 nti = 4 mv = 1.e20 no = 1 #no = 1 vfunc = lambda t, z, y, x: (1 * x + 2.35 * y + 3.24 * z - 0.65 * t) tti, zzi, yyi, xxi = N.mgrid[0:nti - 1:nti * 1j, 0:nzi - 1:nzi * 1j, 0:nyi - 1:nyi * 1j, 0:nxi - 1:nxi * 1j] zzi = zzi[None] zzi = N.repeat(zzi, nexz, axis=0) # Pure 1D axes xi = xxi[0, 0, 0:1, :] # (nyix=1,nxi) yi = yyi[0, 0, :, 0:1] # (nyi,nxiy=1) zi = zzi[0:1, 0:1, :, 0:1, 0:1] # (nexz=1,ntiz=1,nzi,nyiz=1,nxiz=1) ti = tti[:, 0, 0, 0] # (nti) vi = vfunc(tti, zzi, yyi, xxi) vi = N.resize(vi, (nex, ) + vi.shape[1:]) # (nex,nti,nzi,nyi,nxi) N.random.seed(0) xyztomin = -0.5 xo = N.random.uniform(xyztomin, nxi - 1.5, no) yo = N.random.uniform(xyztomin, nyi - 1.5, no) zo = N.random.uniform(xyztomin, nzi - 1.5, no)
"""Test fortran function :f:func:`nearest2dto1d`""" from vcmq import P, N, code_file_name, os, add_grid from vacumm.misc.grid._interp_ import nearest2dto1d # Input grid and data 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 = 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')
"""Test the :func:`~vacumm.misc.grid.masking.create_polygon` function""" from vcmq import N, P, create_polygon, plot_polygon, code_file_name from _geoslib import Polygon # Data xx = N.array([0., 5., 4., 1.]) yy = N.array([0., 0., 2., 2.]) # From Polygon P.figure(figsize=(5, 5)) p0 = create_polygon(Polygon(N.array([xx, yy]).T)) plot_polygon(p0, color='b', label='From poly') # From data p1 = create_polygon(N.array([xx, yy]).T - .25) plot_polygon(p1, color='r', label='From data') # From transposed data p2 = create_polygon(N.array([xx, yy]) - 0.5) plot_polygon(p2, color='g', label='From transposed data') # From min/max p3 = create_polygon([xx.min(), yy.min(), xx.max(), yy.max()]) plot_polygon(p3, color='m', label='From min/max') # With projection proj = lambda x, y: (x * 1.5, y * 1.5) p4 = create_polygon(p0, proj=proj) plot_polygon(p4, color='cyan', label='With projection') # Save
"""Test the :func:`~vacumm.misc.grid.regridding.regrid2d` function""" from vcmq import (P, N, MV2, code_file_name, os, add_grid, rotate_grid, set_grid, create_grid, rc, rcdefaults, plot2d, regrid2d, create_time) # Input grid and data nxi = 20 nyi = 15 nt = 5 # - rect xi = N.arange(nxi*1.) yi = N.arange(nyi*1.) ti = create_time((nt, ), 'years since 2000') gridri = create_grid(xi, yi) xxri, yyri = N.meshgrid(xi, yi) zzri = N.ma.resize(yyri, (nt, nyi, nxi)) zzri[:, int(nyi*0.3):int(nyi*0.6), int(nxi*0.3):int(nxi*0.6)] = N.ma.masked zzri[1] = N.ma.masked varri = MV2.asarray(zzri) varri.setAxis(0, ti) set_grid(varri, gridri) # - curv gridci = rotate_grid(gridri, 30) xxci = gridci.getLongitude().getValue() yyci = gridci.getLatitude().getValue() zzci = N.ma.resize(yyci, (nt, nyi, nxi)) zzci[:, int(nyi*0.3):int(nyi*0.6), int(nxi*0.3):int(nxi*0.6)] = N.ma.masked zzci[1] = N.ma.masked varci = MV2.asarray(zzci) varci.setAxis(0, ti) set_grid(varci, gridci)