def test_ens_load_model_at_regular_dates(): # Specs ncpat = NCPAT_MANGA time = ('2014-01-01 12', '2014-01-25 12') nt = 15 lat = (47.2, 47.9) lon = (-5.5, -5) dtfile = (12, 'day') # 3D varnames = ['temp', 'depth'] level = None temp, depth = load_model_at_regular_dates(ncpat, varnames=varnames, time=time, lat=lat, lon=lon, level=level, modeltype='mars', nt=nt, dtfile=dtfile, sort=True) assert temp.shape==(nt, 15, 10, 4) assert depth.shape==(nt, 15, 10, 4) ctimes = temp.getTime().asComponentTime() assert ctimes[1] == comptime('2014-1-3 0:0:0.0') assert ctimes[-2] == comptime('2014-1-24 0:0:0.0') # 3D with z interpolation depths = create_dep([-40., -30, -20, -10, 0.]) temp = load_model_at_regular_dates(ncpat, varnames=varnames[0], time=time, lat=lat, lon=lon, modeltype='mars', nt=2, dtfile=dtfile, sort=True, depths=depths) assert temp.shape == (2, len(depths), 10, 4) # Surf varnames = 'temp' level = {'temp':'surf'} temp = load_model_at_regular_dates(ncpat, varnames=varnames, time=time, lat=lat, lon=lon, level=level, modeltype='mars', nt=nt, dtfile=dtfile, sort=True) assert temp.shape==(nt, 10, 4) assert temp.id == 'temp_surf'
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 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
#!/usr/bin/env python """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)
#!/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 var = MV2.ones(grid.shape) set_grid(var, grid)
"""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],
"""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)
time1 = "2008-08-15 16:00" ne = 4 nez = 2 # Imports from vcmq import (N, MV2, code_file_name, os, P, create_lon, create_lat, create_dep, create_time, lindates, create_axis, reltime, grid2xy, comptime, set_grid, rotate_grid, add_grid) # Rectangular xyzt with 1d z data and coords # - data lon = create_lon(N.linspace(lon0, lon1, nx)) lat = create_lat(N.linspace(lat0, lat1, ny)) dep = create_dep(N.linspace(dep0, dep1, nz)) time = create_time(lindates(time0, time1, nt)) extra = create_axis(N.arange(ne), id='member') data = N.resize(lat[:], (ne, nt, nz, nx, ny)) # function of y data = N.moveaxis(data, -1, -2) #data = N.arange(nx*ny*nz*nt*ne, dtype='d').reshape(ne, nt, nz, ny, nx) vi = MV2.array(data, axes=[extra, time, dep, lat, lon], copy=False, fill_value=1e20) N.random.seed(0) xo = N.random.uniform(lon0, lon1, np) yo = N.random.uniform(lat0, lat1, np) zo = N.random.uniform(dep0, dep1, np) to = comptime(N.random.uniform(reltime(time0, time.units).value, reltime(time1, time.units).value, np), time.units)
def slice_gridded_var(var, member=None, time=None, depth=None, lat=None, lon=None): """Make slices of a variable and squeeze out singletons to reduce it The "member" axis is considered here as a generic name for the first axis of unkown type. .. warning:: All axes must be 1D """ # Check order var = var(squeeze=1) order = var.getOrder() # Unkown axis if '-' in order and member is not None: i = order.find('-') id = var.getAxisIds()[i] if isinstance(member, slice): kw = {id:member} var = var(**kw) else: axo = create_axis(member) cp_atts(var.getAxis(i), axo) var = regrid1d(var, axo, iaxi=i)(squeeze=N.isscalar(member)) # Time interpolation if 't' in order and time is not None: axi = var.getTime() if isinstance(time, slice): var = var(time=time) else: axo = create_time(time, axi.units) var = regrid1d(var, axo)(squeeze=N.isscalar(time)) # Depth interpolation if 'z' in order and depth is not None: if depth=='bottom': var = slice_bottom(var) else: if depth=='surf': depth = slice(-1, None) if isinstance(depth, slice): var = var(level=depth, squeeze=1) # z squeeze only? elif (N.isscalar(depth) and var.getLevel()[:].ndim==1 and depth in var.getLevel()): var = var(level=depth) else: axo = create_dep(depth) if axo[:].max()>10: sonat_warn('Interpolation depth is positive. Taking this opposite') axo[:] *=-1 var = regrid1d(var, axo)(squeeze=N.isscalar(depth)) # Point if (order.endswith('yx') and lon is not None and lat is not None and not isinstance(lat, slice) and not isinstance(lon, slice)): var = grid2xy(var, lon, lat)(squeeze=N.isscalar(lon)) else: # Latitude interpolation if 'y' in order and lat: if isinstance(lat, slice): var = var(lat=lat) else: axo = create_lat(lat) var = regrid1d(var, axo)(squeeze=N.isscalar(lat)) # Longitude interpolation if 'x' in order and lon: if isinstance(lon, slice): var = var(lon=lon) else: axo = create_lon(lon) var = regrid1d(var, axo)(squeeze=N.isscalar(lon)) return var
"""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)
ax = P.subplot(131) P.pcolormesh(xbi, ybi, vi, **kw) P.title('Original') P.subplot(132, sharex=ax, sharey=ax) P.pcolormesh(xbo, ybo, vol, **kw) 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))
def test_ens_generate_pseudo_ensemble(): # Specs ncpat = NCPAT_MANGA varnames = ['temp', 'sal', 'u', 'v'] time = ('2014-01-01 13', '2014-01-25 12') nrens = 14 # nrens = 2 enrich = 1.5 dtfile = (15, 'day') ncfile = ENS_NCFILE level = {'temp':('3d', 'surf'), 'u':'surf', 'v':'surf'} depths = create_dep([-40., -30, -20, -10, 0.]) # Direct enrich = 0 # <= 1 (temp, temp_surf, sal, u_surf, v_surf) = generate_pseudo_ensemble(ncpat, nrens=nrens, enrich=enrich, time=time, varnames=varnames, dtfile=dtfile, logger=LOGGER, anomaly=False, level=level, depths=depths) assert temp.shape[0]==nrens assert sal.shape[0]==nrens assert temp.ndim==4 assert temp.shape[1]==len(depths) assert temp_surf.ndim==3 assert v_surf.ndim==3 f = cdms2.open(NCFILE_MANGA0) temp0 = f('TEMP', time=slice(1, 2), level=slice(-1, None), squeeze=1) f.close() assert_allclose(temp0, temp_surf[0]) tsum = temp.sum() # Enrichment enrich = 1.5 ens = generate_pseudo_ensemble(ncpat, nrens=nrens, enrich=enrich, varnames=varnames, time=time, dtfile=dtfile, logger=LOGGER, getmodes=True, level=level, depths=depths) (temp, temp_surf, sal, u_surf, v_surf), modes = ens (temp_eof, temp_surf_eof, sal_eof, u_surf_eof, v_surf_eof) = modes['eofs'] ev = modes['eigenvalues'] temp_var, temp_surf_var, sal_var, u_surf_var, v_surf_var = modes['variance'] assert tsum!=temp.sum() assert temp.shape[0]==nrens assert sal.shape[0]==nrens eof0 = N.concatenate( (temp_eof[0].compressed(), temp_surf_eof[0].compressed(), sal_eof[0].compressed(), u_surf_eof[0].compressed(), v_surf_eof[0].compressed())) assert_allclose((eof0**2).sum(), 1) eof1 = N.concatenate( (temp_eof[1].compressed(), temp_surf_eof[1].compressed(), sal_eof[1].compressed(), u_surf_eof[1].compressed(), v_surf_eof[1].compressed())) assert_allclose((eof0*eof1).sum(), 0, atol=1e-7) assert_allclose(ev.total_variance, eof0.size) expv = (ev**2).sum()/ev.total_variance assert expv > .8 and expv < 1 expvm = temp.var(axis=0).mean()/temp_var.mean() assert expvm > .8 and expvm < 1 # Save ensemble f = cdms2.open(ncfile, 'w') for var in ( temp, temp_surf, sal, u_surf, v_surf, temp_eof, temp_surf_eof, sal_eof, u_surf_eof, v_surf_eof, temp_var, temp_surf_var, sal_var, u_surf_var, v_surf_var, ev ): f.write(var) f.close()
ax = P.subplot(131) P.pcolormesh(xbi, ybi, vi, **kw) P.title('Original') P.subplot(132, sharex=ax, sharey=ax) P.pcolormesh(xbo, ybo, vol, **kw) 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))
"""Test :func:`~vacumm.misc.plot.curve2` with a depth axis""" # Imports from vcmq import MV2, code_file_name, os, code_file_name, curve2, create_dep # Init var = MV2.arange(5.0) var.units = "${}^{\circ}C$" var.long_name = "Temperature" depth = create_dep((-80, 0.1, 20.0)) var.setAxis(0, depth) # Plot figfile = code_file_name(ext="png") if os.path.exists(figfile): os.remove(figfile) curve2(var, savefig=figfile, show=False, close=True)
"""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=True,
"""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)
"""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)
#!/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
"""Test :func:`~vacumm.misc.plot.curve2` with a depth axis""" # Imports from vcmq import MV2, code_file_name, os, code_file_name, curve2, create_dep # Init var = MV2.arange(5.) var.units = '${}^{\circ}C$' var.long_name = 'Temperature' depth = create_dep((-80, 0.1, 20.)) var.setAxis(0, depth) # Plot figfile = code_file_name(ext='png') if os.path.exists(figfile): os.remove(figfile) curve2(var, savefig=figfile, show=False, close=True)