def missing_interp(lon, lat, arr): grid0 = create_grid(lon[0, :], lat[:, 0]) varri = MV2.asarray(arr) varri = set_grid(varri, grid0) tempf = fill2d(varri, method='carg') lon = np.ma.masked_where(tempf == tempf.fill_value, lon) lat = np.ma.masked_where(tempf == tempf.fill_value, lat) arri = griddata(lon.compressed(), lat.compressed(), tempf.compressed(), (lon[0, :], lat[:, 0]), method='nat', ext=True, sub=10) return arri
"""Test :func:`~vacumm.misc.plot.plot2d` with a time axis""" # Imports from vcmq import MV2, code_file_name, plot2d, os # Init var = MV2.reshape(MV2.arange(10*8), (8,10)) var.units = 'Count' var.long_name = 'Carottes' x = var.getAxis(1) x.units = 'months since 2000' x.long_name = 'Time' y = var.getAxis(0) y.units = 'Hz' y.long_name = 'Frequency' # Plot figfile = code_file_name(ext='png') if os.path.exists(figfile): os.remove(figfile) plot2d(var, savefig=figfile, show=False, close=True, fill='pcolor', order='t-') # Unittest result = {"files":figfile}
#!/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)
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) varc = MV2.ones(gridc.shape) set_grid(varc, gridc) # -> VERIFIEZ ID DES AXES # Récupérer mygrid = get_grid( gridc) # -> TESTEZ AVEC (LON,LAT) ET PARAMS STRICT ET INTERCEPT mylon2d = get_axis(gridc, 1) # -> COMPAREZ AVEC .GETAXIS() # Sélectionner print coord2slice(lon, lon=(2, 4.5)) # -> COMPAREZ AVEC .GETINTERVALEXT(...) print coord2slice(grid, lon=(4, 8), lat=(44, 46)) # -> TESTEZ SUR GRIDC # -> TESTEZ VARSEL # Transformer
-8, va='center', ha='center', transform='data', fmt='%g') m.add_lat_label(-8, 47, 47, va='center', ha='center', transform='data') m.add_time_label(.1, .9, '2000') # Add place m.add_place(-2, 49, 'My place', text_family='monospace', text_size=15) # Add lines m.add_line([-8, 46, -1, 50], color='b') m.add_lines([-8, -1, -4, -8], [45, 45, 47, 45], color='r', linewidth=2, shadow=True) m.add_box([-8, 46, -1, 50], color='c') m.savefig(figfile % 0) m.close() # Test with dates var = MV2.array([3, 6]) time = create_time(['2000', '2005'], units='years since 2000') var.setAxis(0, time) c = curve2(var, show=False) c.add_point('2001', 4, marker='+', size=100, linewidth=2) c.savefig(figfile % 1) m.close()
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 dxi = xi[-1]-xi[0]
"""Test :func:`~vacumm.misc.plot.curve2` with a time axis""" # Imports from vcmq import MV2, code_file_name, os, code_file_name, curve2 # Init var = MV2.arange(5.) var.units = 'W' var.long_name = 'Power' axis = var.getAxis(0) axis.units = 'months since 2013-01-01' axis.long_name = 'Time' axis.axis = 'T' # Plot figfile = code_file_name(ext='png') if os.path.exists(figfile): os.remove(figfile) curve2(var, savefig=figfile, show=True, 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=False, axes_aspect=1) # Result result = dict(files=figfile)
"""Test :meth:`~vacumm.data.misc.dataset.OceanDataset.get_uvgbt` on MENOR""" # Inits ncfile = "menor.nc" # Imports from vcmq import data_sample, DS, N, MV2, shapiro2d, map2, code_file_name # Read data ds = DS(data_sample(ncfile), 'mars', logger_level='critical') u, v = ds.get_uvgbt() # Mask huge values mask = (N.ma.sqrt(u**2+v**2)>2.).filled(True) for var in u, v: var[:] = MV2.masked_where(mask, var, copy=False) # Shapiro filter for var in u, v: var[:] = shapiro2d(var) # Plot map2((u[0], v[0]), title='Geostrophic velocity', quiver_samp=3, xymasked=False, quiver_norm=3, contour=False, fill=False, figsize=(7, 7), quiver_linewidth=.3, quiver_width=0.003, quiver_scale=10, right=1, colorbar_shrink=0.8, bottom=0.05, show=False, savefig=code_file_name(), close=True)
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)) 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()
"""Test the class :class:`~vacumm.misc.grid.regridding.CurvedInterpolator`""" from vacumm.misc.grid.misc import rotate_grid from vacumm.misc.grid.regridding import CurvedInterpolator 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)
#!/usr/bin/env python # -*- coding: utf8 -*- """Utilitaires et conventions de formatage """ from vcmq import MV2, cdms2, N from vacumm.data.cf import VAR_SPECS, format_var, match_var, format_axis, AXIS_SPECS, GENERIC_VAR_NAMES # Création d'une variable 1D sst = MV2.arange(5.) # -> VERIFIER LES INFOS DE CET VARIABLE sst.getAxis(0).designateLongitude() # -> VERIFIER LES INFOS DE CET AXE # Formatage format_var(sst, 'sst') # -> VERIFIER LES NOUVELLES INFOS DE VAR+AXE # -> FORMATER L'AXE EN LATITUDE AU POINT U # Verification print match_var(sst, 'sst')
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)) 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)
"""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, colors='cyan', title='Basic', **kwplot) # - single variable without normalization taylor(dat1[0], ref1, savefigs=basename%1, normalize=False, reflabel='Reference', title='No normalization', **kwplot) # - several variables with same ref taylor([d for d in dat1], ref1, colors='cyan', savefigs=basename%2, title='Several variables', **kwplot) # - several variables with same ref with specified colors taylor([d for d in dat1[:3]], ref1, colors = ['r', 'g', 'b'],
"""Test :meth:`~vacumm.data.misc.dataset.OceanDataset.get_uvgbt` on MENOR""" # Inits ncfile = "menor.nc" # Imports from vcmq import data_sample, DS, N, MV2, shapiro2d, map2, code_file_name # Read data ds = DS(data_sample(ncfile), 'mars', logger_level='critical') u, v = ds.get_uvgbt() # Mask huge values mask = (N.ma.sqrt(u**2 + v**2) > 2.).filled(True) for var in u, v: var[:] = MV2.masked_where(mask, var, copy=False) # Shapiro filter for var in u, v: var[:] = shapiro2d(var) # Plot map2((u[0], v[0]), title='Geostrophic velocity', quiver_samp=3, xymasked=False, quiver_norm=3, contour=False, fill=False, figsize=(7, 7), quiver_linewidth=.3,
comm = MPI.COMM_WORLD rank = comm.Get_rank() size = comm.Get_size() #if rank==0: print 'read' f = cdms2.open(data_sample(ncfile)) vari2d = f(ncvar)[0,yslice,xslice] f.close() gridi = vari2d.getGrid() vari2d[:] += 3*vari2d.mean() vari2d[:, -1] = MV2.masked 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)
# 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) # Output positions nxo = 25
def generate_pseudo_ensemble(ncpat, varnames=None, nrens=50, enrich=2., norms=None, getmodes=False, logger=None, asdicts=False, anomaly=True, ncensfile=None, **kwargs): """Generate a static pseudo-ensemble from a single simulation Parameters ---------- ncpat: string netcdf file name or pattern nrens: int Ensemble size enrich: float Enrichment factor getmodes: bool Get also EOFs end eigen values **kwargs: Extra parameters are passed to :func:`load_model_at_dates` Return ------ list (or dict) of arrays: variables with their name as keys dict: eofs, ev and variance, optional eofs: list (or dict) of arrays(nmodes, ...), optional EOFs ev: array(nmodes), optional Eigen values var: array Variance """ # Logger kwlog = kwfilter(kwargs, 'logger_') if logger is None: logger = get_logger(**kwlog) logger.verbose('Generating pseudo-ensemble') # Ensembe size enrich = max(enrich, 1.) nt = int(nrens * enrich) logger.debug( ' enrich={enrich}, nt={nt}, ncpat={ncpat}, varnames={varnames}'. format(**locals())) # Read variables logger.debug('Reading the model at {} dates'.format(nt)) data = load_model_at_regular_dates(ncpat, varnames=varnames, nt=nt, asdict=False, **kwargs) single = not isinstance(data, list) # Norms if isinstance(norms, dict): norms = var_prop_dict2list(data, norms) # Enrichment witheofs = nrens != nt if witheofs: logger.debug('Computing reduced rank ensemble with EOFs analysis') # Stack packed variables together stacker = Stacker(data, norms=norms, logger=logger) meanstate = N.zeros(stacker.ns) states = N.asfortranarray(stacker.stacked_data.copy()) # Compute EOFs stddev, svals, svecs, status = f_eofcovar(dim_fields=stacker.ns, offsets=1, remove_mstate=0, do_mv=0, states=states, meanstate=meanstate) if status != 0: raise SONATError('Error while calling fortran eofcovar routine') neof = svals.size # computed neofr = nrens - 1 # retained svals = svals[:neofr] * N.sqrt( (neof - 1.) / neof) # to be consistent with total variance svecs = svecs[:, :neofr] # Generate ensemble sens = f_sampleens(svecs, svals, meanstate, flag=0) # Unstack ens = stacker.unstack(sens, format=2, rescale='norm' if anomaly else True) if getmodes: # Modes mode_axis = create_axis(N.arange(1, neofr + 1, dtype='i'), id='mode') eofs = stacker.unstack(svecs, firstdims=mode_axis, id='{id}_eof', rescale=False, format=1) svals = MV2.array(svals, axes=[mode_axis], id='ev', attributes={'long_name': 'Eigen values'}) svals.total_variance = float(stacker.ns) # Variance vv = stacker.format_arrays([d.var(axis=0) for d in stacker.datas], id='{id}_variance', mode=1) variance = stacker.unmap(vv) else: # No enrichment -> take the anomaly if requested logger.debug('Getting the anomaly to build the ensemble') ens = data if anomaly: if single: ens[:] = ens.asma() - ens.asma().mean(axis=0) else: for i, e in enumerate(ens): ens[i][:] = e.asma() - e.asma().mean(axis=0) # Finalize getmodes = getmodes and witheofs member_axis = create_axis(N.arange(nrens, dtype='i'), id='member', long_name='Member') if single: ens.setAxis(0, member_axis) else: for var in ens: var.setAxis(0, member_axis) # Dump to file if ncensfile: logger.debug('Dump the ensemble to netcdf') checkdir(ncensfile) f = cdms2.open(ncensfile, 'w') ensvars = list(ens) if not single else [ens] if getmodes: if single: ensvars.append(eofs) ensvars.append(variance) else: ensvars.extend(eofs) ensvars.extend(variance) ensvars.append(svals) for var in ensvars: f.write(var) f.close() logger.created(ncensfile) # As dicts if asdicts: if single: ens = OrderedDict([(ens.id, ens)]) if getmodes: eofs = OrderedDict([(eofs.id, eofs)]) variance = OrderedDict([(variance.id, variance)]) else: ens = OrderedDict([(var.id, var) for var in ens]) if getmodes: eofs = OrderedDict([(var.id, var) for var in eofs]) variance = OrderedDict([(var.id, var) for var in variance]) # Return if not getmodes: return ens return ens, dict(eofs=eofs, eigenvalues=svals, variance=variance)
# - 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) varc = MV2.ones(gridc.shape) set_grid(varc, gridc) # -> VERIFIEZ ID DES AXES # Récupérer mygrid = get_grid(gridc) # -> TESTEZ AVEC (LON,LAT) ET PARAMS STRICT ET INTERCEPT mylon2d = get_axis(gridc, 1) # -> COMPAREZ AVEC .GETAXIS() # Sélectionner print coord2slice(lon, lon=(2, 4.5))# -> COMPAREZ AVEC .GETINTERVALEXT(...) print coord2slice(grid, lon=(4, 8), lat=(44, 46)) # -> TESTEZ SUR GRIDC # -> TESTEZ VARSEL
# 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, tool='esmf', method='conservative', diag=diag, coordSys='cart') # Norm
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()
"""Test :func:`~vacumm.misc.atime.interp_clim`""" from vcmq import (code_file_name, interp_clim, MV2, N, create_time, lindates, curve, strftime) # Original clim N.random.seed(0) s = N.resize(N.sin(N.linspace(0, 1, 13)[:12] * 2 * N.pi), (2, 12)).T clim = MV2.array(s, fill_value=1e20) p = curve(clim[:, 0], 'o-', show=False, subplot=211, title='Original climatology', xmin=-.5, xmax=11.5, xticks=range(12), xticklabels=[strftime('%b', '2000-%i' % i) for i in range(1, 13)]) # Target times times = lindates('2000-01-01', '2001-12-31', 5, 'day') # Interpolations for i, method in enumerate(( 'linear', 'cubic', )): climo = interp_clim(clim, times, method=method) c = curve(climo[:, 0], 'o-', color='gr'[i], show=False,
try: lon = eval(options.lon) except: parser.error('--%s: wrong argument'%selname) select[selname] = val # Imports from vcmq import DS, map2, MV2, data_sample # The file ncfile = options.ncfile if not os.path.exists(ncfile): ncfile = data_sample(ncfile) if not os.path.exists(ncfile): parser.error('File not found: '+ncfile) # Read temperature and depth ds = DS(ncfile, options.model, **select) mld = ds.get_mld(mode='deltatemp', squeeze=1) # Time average (if needed) if mld.ndim==3: mld = MV2.average(mld, axis=0) # Plot map2(mld, proj='merc', figsize=(6, 6), autoresize=0, title=options.title, colorbar_shrink=0.7, right=1, show=options.disp, savefig=options.outputfig, savefig_verbose=True)
"""Test :func:`~vacumm.misc.grid.misc.varsel`""" # %% Imports from vcmq import N, MV2, create_grid, varsel, set_grid # %% With specified grid rgrid = create_grid((-14., -4, .25), (43., 52, .25), 'rect') var_ = MV2.reshape(MV2.arange(2 * rgrid.shape[0] * rgrid.shape[1]), (2, ) + rgrid.shape) var = var_.clone() cache = {} rvar0 = varsel(var, grid=rgrid, lon=(-9, -6, 'co'), lat=(44.1, 48.2), cache=cache) assert rvar0.shape == (2, 16, 12) assert rvar0.getLongitude()[-1] == -6.25 assert rvar0.getLatitude()[-1] == 48 set_grid(var, rgrid) # %% Without grid specification rvar1 = varsel(var, lon=(-9, -6, 'co'), lat=(44.1, 48.2)) N.testing.assert_allclose(rvar0.asma(), rvar1.asma()) # %% With cached gridded selector assert 'gridded_selector' in cache rvar2 = varsel(var_.clone(), lon=(-9, -6, 'co'), lat=(44.1, 48.2), cache=cache) N.testing.assert_allclose(rvar0.asma(), rvar2.asma())
xxi[j,:] -= j*0.5 #yyi[j,:] += j for i in xrange(nxi): yyi[:,i] += i*0.5 gridi = create_grid2d(xxi,yyi) # input cdms grid xxib,yyib = meshbounds(xxi,yyi) # coordinates of cell corners # Output curved grid nxo = 7 nyo = 7 xxo, yyo = N.meshgrid(N.arange(nxo)+.5, N.arange(nyo)-.5) grido = create_grid2d(xxo, yyo) # output cdms grid xxob,yyob = meshbounds(xxo,yyo) # coordinates of cell corners # Input field vari = MV2.array(N.arange(nyi*nxi).reshape(nyi,nxi))+10. vari[1,1:3] = 100 vari[:2,1:3] = MV2.masked set_grid(vari, gridi) # set grid and axes #gridi.setMask(vari.mask) # Define plot function figfile = code_file_name(ext=False)+'_%(ifig)i.png' #'%(tool)s_%(method)s.png' figfiles = [] rc('font',size=9) kw = dict(vmin=vari.min(),vmax=vari.max()) # Define logger logfile = code_file_name(ext='log') f = open(logfile, 'w')
"""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.plot2d` with arbitrary axes""" # %% Imports from vcmq import MV2, code_file_name, plot2d, os # %% Init var = MV2.reshape(MV2.arange(10 * 8), (8, 10)) var.units = 'Count' var.long_name = 'Navets' x = var.getAxis(1) x.units = 'km' x.long_name = 'Distance' y = var.getAxis(0) y.units = 'Hz' y.long_name = 'Frequency' # %% Plot figfile = code_file_name(ext='png') if os.path.exists(figfile): os.remove(figfile) plot2d(var, savefig=figfile, show=False, close=True)
"""Test the class :class:`~vacumm.misc.grid.regridding.CurvedInterpolator`""" from vcmq import (P, N, set_grid, plot2d, MV2, add_grid, code_file_name, os, create_time, CurvedInterpolator, rotate_grid) # 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)
"""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,
"""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 :class:`~vacumm.misc.stats.StatAccum` for a single variable""" # Imports from vcmq import MV2, N from vacumm.misc.stats import StatAccum # Setup masked data nt, ny, nx = 20, 15, 10 var = MV2.arange(1.*nt*ny*nx).reshape(nt, ny, nx) var[3:13, 5:9, 3:7] = MV2.masked var.getAxis(0).designateTime() var.getAxis(0).units = 'months since 2000' vmax = var.max() bins = N.linspace(-0.1*vmax, 0.9*vmax, 14) nbins = len(bins) maskyx = var.mask.all(axis=1) maskt = var.mask.reshape((nt, -1)).all(axis=1) # Direct varm = var.asma() var2d = var.asma().reshape(nt, -1) dstats = dict( tavail = 1.*(~var.mask).sum(axis=0)/var.shape[0], tmean = var.mean(axis=0), tstd = var.std(axis=0), tmin = var.min(axis=0), tmax = var.max(axis=0), savail = 1.*(~var2d.mask).sum(axis=1)/var2d.shape[1], smean = var2d.mean(axis=1), sstd = var2d.std(axis=1), smin = var2d.min(axis=1),
"""Test :func:`~vacumm.misc.grid.misc.GriddedSelector`""" # %% Imports from vcmq import N, MV2, create_grid, rotate_grid, GriddedSelector # %% Rectangular grid rgrid = create_grid((-14., -4, .25), (43., 52, .25), 'rect') var = MV2.reshape(MV2.arange(2 * rgrid.shape[0] * rgrid.shape[1]), (2, ) + rgrid.shape) gs = GriddedSelector(rgrid, lon=(-9, -6, 'co'), lat=(44.1, 48.2)) rvar = gs(var) assert rvar.shape == (2, 16, 12) assert rvar.getLongitude()[-1] == -6.25 assert rvar.getLatitude()[-1] == 48 # %% Curvilinear cgrid = rotate_grid(rgrid, 30.) gs = GriddedSelector(cgrid, lon=(-9, -6, 'co'), lat=(44.1, 48.2)) cvar = gs(var) assert cvar.shape == (2, 20, 18) assert cvar.getLongitude().asma()[~gs.mask].min() == -9 assert cvar.getLatitude().asma()[~gs.mask].max() <= 48.2 # %% Unstructured N.random.seed(0) ugrid = create_grid(N.random.uniform(-14, -4, 100), N.random.uniform(43, 52, 100), 'unstruct') var = MV2.resize(ugrid.getLatitude().asma(), (2, ) + ugrid.shape) gs = GriddedSelector(ugrid, lon=(-9, -6, 'co'), lat=(44.1, 48.2)) uvar = gs(var) assert uvar.getGrid().shape == (34, )
# 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, tool='esmf', method='conservative', diag=diag,
"""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 :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 (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) # Rectangular xyzt with 1d z vo = grid2xy(vi, xo=xo, yo=yo, zo=zo, to=to, method='linear') von = grid2xy(vi, xo=xo, yo=yo, zo=zo, to=to, method='nearest') assert vo.shape==(ne, np) N.testing.assert_allclose(vo[0], yo) kwp = dict(vmin=vi.min(), vmax=vi.max())
"""Test :func:`~vacumm.misc.plot.curve2` with an arbitrary axis""" # Imports from vcmq import MV2, code_file_name, os, code_file_name, curve2 # Init var = MV2.arange(5.) var.units = '$Kg$' var.long_name = 'Flour' axis = var.getAxis(0) axis.units = 'm' axis.long_name = 'Distance' # Plot figfile = code_file_name(ext='png') if os.path.exists(figfile): os.remove(figfile) curve2(var, savefig=figfile, show=False, close=True)
# Add text m.add_text(0.1, 0.1, 'My text', color='r', shadow=True, weight='bold') m.add_text(-2, 50.5, 'My text with transform', transform='data', ha='center', bbox=dict(facecolor='y', alpha=.3)) m.add_lon_label(-8, 47.5, -8, va='center', ha='center', transform='data', fmt='%g') m.add_lat_label(-8, 47, 47, va='center', ha='center', transform='data') m.add_time_label(.1, .9, '2000') # Add place m.add_place(-2, 49, 'My place', text_family='monospace', text_size=15) # Add lines m.add_line([-8, 46, -1, 50], color='b') m.add_lines([-8, -1, -4, -8], [45, 45, 47, 45], color='r', linewidth=2, shadow=True) m.add_box([-8, 46, -1, 50], color='c') m.savefig(figfile%0) m.show() m.close() # Test with dates var = MV2.array([3, 6]) time = create_time(['2000', '2005'], units='years since 2000') var.setAxis(0, time) c = curve2(var, show=False) c.add_point('2001', 4, marker='+', size=100, linewidth=2) c.savefig(figfile%1) m.close()
"""Test :class:`~vacumm.misc.stats.StatAccum` for a pair of variables""" # Imports from vcmq import MV2, N from vacumm.misc.stats import StatAccum import warnings warnings.filterwarnings('error', "boolean index did not match", N.VisibleDeprecationWarning) # Setup masked data nt, ny, nx = 20, 15, 10 var1 = MV2.arange(1. * nt * ny * nx).reshape(nt, ny, nx) var1.getAxis(0).designateTime() var1.getAxis(0).units = 'months since 2000' var2 = var1.clone() var2[:] += N.ma.sin(var1) * 100 var1[3:13, 5:9, 3:7] = MV2.masked var2[5:15, 7:12, 1:5] = MV2.masked mask = var1.mask | var2.mask # common mask vmax = var2.max() bins = N.linspace(-0.1 * vmax, 0.9 * vmax, 14) nbins = len(bins) maskyx = mask.all(axis=0) maskt = mask.reshape((nt, -1)).all(axis=1) # Direct varm1 = var1.asma().copy() varm2 = var2.asma().copy() varm1[mask] = N.ma.masked varm2[mask] = N.ma.masked var2dm1 = varm1.reshape(nt, -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)
"""Test :class:`~vacumm.misc.stats.StatAccum` with dumping and loading """ # Imports from vcmq import MV2, N, code_file_name, StatAccum from numpy.testing import assert_array_almost_equal from numpy.random import seed cdms2.setAutoBounds('off') # Setup masked data seed(0) nt, ny, nx = 20, 3, 3 var1 = MV2.array(N.random.random((nt, ny, nx))) var1.getAxis(0).designateTime() var1.getAxis(0).units = 'months since 2000' 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)
"""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.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}
# Generate data # - reference xr = N.arange(20.) - 25 yr = N.arange(10.) + 43. xxr, yyr = N.meshgrid(xr, yr) zzr = (N.sin(xxr*N.pi/6)*N.sin(yyr*N.pi/6) + \ N.exp(-((xxr-7.)**2+(yyr-7.)**2)/4.**2))*100. zzr -= zzr.mean() zzr = N.ma.asarray(zzr) zzr[5:, 10:] = N.ma.masked # - input at random locations ij = N.unique((N.random.rand(150) * zzr.size).astype('i')) xi, yi, zi = xxr.flat[ij], yyr.flat[ij], zzr.flat[ij] zi = N.ma.resize(zi, (3, zi.size)) # - format zi = MV2.array(zi, copy=False, id='sst') taxis = zi.getAxis(0) taxis.units = 'hours since 2000' taxis.axis = 'T' ggo = create_grid(xr, yr) zzr = MV2.array(zzr) set_grid(zzr, ggo) # Call and plot kw = dict(vmin=zzr.min(), vmax=zzr.max(), lon=(xr[0], xr[-1]), cmap_lum=0.7, linewidth=.5, lat=(yr[0], yr[-1]), show=False,
"""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'),
#!/usr/bin/env python # -*- coding: utf8 -*- """Utilitaires et conventions de formatage """ from vcmq import MV2, cdms2, N from vacumm.data.cf import VAR_SPECS, format_var, match_var, format_axis, AXIS_SPECS, GENERIC_VAR_NAMES # Création d'une variable 1D sst = MV2.arange(5.) # -> VERIFIER LES INFOS DE CET VARIABLE sst.getAxis(0).designateLongitude() # -> VERIFIER LES INFOS DE CET AXE # Formatage format_var(sst, 'sst') # -> VERIFIER LES NOUVELLES INFOS DE VAR+AXE # -> FORMATER L'AXE EN LATITUDE AU POINT U # Verification print match_var(sst,'sst')
"""Test :class:`~vacumm.misc.stats.StatAccum` with dumping and loading """ # Imports from vcmq import MV2, N, code_file_name, StatAccum from numpy.testing import assert_array_almost_equal from numpy.random import seed cdms2.setAutoBounds('off') # Setup masked data seed(0) nt, ny, nx = 20, 3, 3 var1 = MV2.array(N.random.random((nt, ny, nx))) var1.getAxis(0).designateTime() var1.getAxis(0).units = 'months since 2000' 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)
"""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)
# Imports from vcmq import DS, map2, MV2, data_sample # The file ncfile = options.ncfile if not os.path.exists(ncfile): ncfile = data_sample(ncfile) if not os.path.exists(ncfile): parser.error('File not found: ' + ncfile) # Read temperature and depth ds = DS(ncfile, options.model, **select) mld = ds.get_mld(mode='deltatemp', squeeze=1) # Time average (if needed) if mld.ndim == 3: mld = MV2.average(mld, axis=0) # Plot map2(mld, proj='merc', figsize=(6, 6), autoresize=0, title=options.title, colorbar_shrink=0.7, right=1, show=options.disp, savefig=options.outputfig, savefig_verbose=True, close=True)
"""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'), (-2, 2, 'linear')]): varo = extend1d(vari, ext=ext, axis=axis, mode=mode) plot2d(varo, subplot=(2, 2, i+3), title='interp1d: axis=%s\next=%s, mode=%s'%(axis, ext, mode), **kw) varo = extend2d(vari, iext=2, jext=2, mode='linear') plot2d(varo, subplot=(2, 2, 2),