示例#1
0
 def __init__(self, ncfile, readertype='generic',  **kwargs):
     self.ncfile = ncfile
     if not isinstance(readertype, basestring):
         self.f = readertype(ncfile,  **kwargs)
         self.type = 'callable'
     elif readertype=='cdms2':
         self.f = cdms2.open(ncfile,  **kwargs)
         self.type = readertype
     elif readertype=='netcdf4':
         import netcdf4
         self.f = netcdf4.Dataset(ncfile,  **kwargs)
         self.type = readertype
     else:
         self.f = DS(ncfile, readertype, **kwargs)
         self.type = 'dataset'
示例#2
0
def test_obs_obsmanager_project_model():

    # Load manager
    manager = test_obs_obsmanager_init_surf()

    # Load model
    f = DS(NCFILE_MANGA0, 'mars', level='surf', logger_level='error')
    temp = f('temp')
    f.close()
    temp.id += '_surf'

    # Interpolate model
    otemp = manager.project_model(temp)
    assert_allclose(otemp[1][0], [12.91558515, 10.58179214])

    return manager
示例#3
0
def test_obs_ncobsplatform_surf():

    # Load and stack obs
    obs = NcObsPlatform(NCFILE_OBS_SURF,
                        logger=LOGGER,
                        lat=(45, 47.8),
                        norms=[0.2, 0.1])
    stacked = obs.stacked_data.copy()
    assert obs.lons.shape == (3, )
    assert obs.ns == 6
    assert obs.ndim == 1
    assert_allclose(obs.means, [0, 0], atol=1e-7)
    assert obs.depths == 'surf'
    assert_allclose(stacked, [2.5, 1.5, 4., 1., 1.5, 4.])

    # Named norms
    notnormed = obs.set_named_norms(temp=0.1)
    assert obs.norms == [0.1, 0.1]
    assert_allclose(obs.stacked_data[:3], 2 * stacked[:3])
    assert_allclose(obs.stacked_data[3:], stacked[3:])
    obs.set_named_norms(temp=0.2)

    # Interpolate model
    f = DS(NCFILE_MANGA0, 'mars', level=obs.depths, logger_level='error')
    temp = f('temp')
    sal = f('sal')
    f.close()
    temp.id += '_surf'
    sal.id += '_surf'
    otemp = obs.project_model(temp)
    osal = obs.project_model(sal)
    otem_true = [12.97311556, 12.91558515, 10.58179214]
    assert_allclose(otemp[0], otem_true)

    # Stack model
    otemp[:] -= 11.5
    osal[:] -= 35.5
    stacked_data = obs.restack([otemp, osal])
    assert stacked_data.shape == (6, 15)
    assert_allclose(stacked_data[:3, 0] * obs.norms[0] + 11.5, otem_true)

    return obs
示例#4
0
"""Test :meth:`~vacumm.data.misc.dataset.OceanDataset.get_dens` in MENOR"""

# Inits
ncfile = "menor.nc"

# Imports
from vcmq import DS, os, map2, data_sample, code_file_name

# Read data
ds = DS(data_sample(ncfile), 'mars', logger_level='critical')
dens = ds.get_dens(squeeze=True)

# Plot surface density
figfile = code_file_name(ext='png')
if os.path.exists(figfile): os.remove(figfile)
map2(dens[-1], savefig=figfile, close=True, vmin=1025., show=False)

# For unittest
result = {'files': figfile}
示例#5
0
"""Test :meth:`~vacumm.data.misc.dataset.Dataset.get_var` on multiple files and time selections"""

# Inits
ncfiles = "mars2d.xyt.*.nc"

# Imports
from vcmq import DS, os, map2, data_sample

# Glob pattern
result = []
ds = DS(data_sample("mars2d.xyt.*.nc"),
        'mars',
        logger_level='critical',
        time=("2008-08-15 06", "2008-08-15 09"))
assert ds.get_ssh().shape == (4, 50, 50)
assert ds.get_ssh(time=("2008-08-15 06", "2008-08-15 07", 'co'),
                  squeeze=True).shape == (50, 50)

# With dates
ds = DS(data_sample("mars2d.xyt.%Y%m%d%H.nc"),
        'mars',
        logger_level='critical',
        time=("2008-08-15 06", "2008-08-15 09"))
assert ds.get_ssh(time=("2008-08-15 06", "2008-08-15 07",
                        'cc')).shape == (2, 50, 50)
示例#6
0
"""Test method :meth:`~vacumm.data.misc.dataset.Dataset.get_grid` on MENOR"""

# Inits
ncfile = "menor.nc"

# Imports
from vcmq import DS, data_sample

# Open
ds = DS(data_sample(ncfile), 'mars', logger_level='critical')

# Read grids
grid = ds.get_grid()
grid_t = ds.get_grid_t()
grid_u = ds.get_grid_u()
grid_v = ds.get_grid_v()

# Read with selection
subgrid = ds.get_grid(lon=(4, 5))
"""Test :meth:`~vacumm.data.misc.dataset.Dataset.get_var` on multiple files and time selections"""

# Inits
ncfiles = "mars2d.xyt.*.nc"

# Imports
from vcmq import DS, os, map2, data_sample

# Glob pattern
result = []
ds = DS(data_sample("mars2d.xyt.*.nc"), 'mars', logger_level='critical', 
    time=("2008-08-15 06", "2008-08-15 09"))
result.append(('assertTrue', ds.get_ssh().shape==(4, 50, 50)))
result.append(('assertTrue', ds.get_ssh(time=("2008-08-15 06", "2008-08-15 07", 'co'), 
    squeeze=True).shape==(50, 50)))

# With dates
ds = DS(data_sample("mars2d.xyt.%Y%m%d%H.nc"), 'mars', logger_level='critical', 
    time=("2008-08-15 06", "2008-08-15 09"))
result.append(('assertTrue', ds.get_ssh(
    time=("2008-08-15 06", "2008-08-15 07", 'cc')).shape==(2, 50, 50)))


# Parameters
ncfile = "menor.nc"
lon = (3.0, 4.5)
lat = (42, 42.8)

# Imports
from vcmq import DS, map2, data_sample
from vacumm.diag.thermdyn import mixed_layer_depth

# Read temperature and depth
ncfile = data_sample(ncfile)
ds = DS(ncfile, "mars", lon=lon, lat=lat)
temp = ds.get_temp(squeeze=1)
depth = ds.get_depth(squeeze=1)

# Compute MLD
mld = mixed_layer_depth(temp, depth, mode="deltatemp")

# Plot
map2(mld, proj="merc", figsize=(6, 6), autoresize=0, colorbar_shrink=0.7, right=1, savefigs=__file__, show=False)


print "Done"
示例#9
0
"""Test :func:`~vacumm.diag.thermdyn.mixed_layer_depth` on MENOR"""

# Inits
ncfile = "menor.nc"

# Imports
from vcmq import DS, data_sample, mixed_layer_depth, rc, map2, os, code_file_name

# Read data
ds = DS(data_sample(ncfile),'mars', logger_level='critical')
temp = ds.get_temp(squeeze=True)
sal = ds.get_sal(squeeze=True)
depth = ds.get_depth(squeeze=True)
kz = ds.get_kz(squeeze=True)

# Compute MLD
kw = dict(depth=depth, format_axes=True)
mld = {}
mld['deltatemp'] = mixed_layer_depth(temp, mode='deltatemp',**kw)
mld['deltadens'] = mixed_layer_depth((temp,sal), mode='deltadens', **kw)
mld['kz'] = mixed_layer_depth(kz, mode='kz', **kw)
vmin = min([v.min() for v in mld.values()])
vmax = max([v.max() for v in mld.values()])

# Plot it
rc('font', size=8)
for i,(mode, var) in enumerate(mld.items()):
    m = map2(var, fill='pcolormesh', nmax=20, vmin=vmin, vmax=vmax,
        subplot=(len(mld),1,i+1), figsize=(4.1,8),
        contour_linewidths=0.7, cmap='vacumm_rnb2_hymex', hspace=0.25, bottom=0.08,
        title='%%(long_name)s: mode = "%s"'%mode, show=False)
示例#10
0
    if RE_MATCH_TIME(arg) and not arg.isdigit():  # date
        return arg
    if re.match('[\w\s]*:[\w\s]*(:[\w\s]*)?$', arg):  # args to slice
        return slice(*[(None if a == '' else eval(a)) for a in arg.split(':')])
    arg = eval(arg)
    if isinstance(arg, int):  # slice at a single index
        return slice(arg, (arg + 1) if arg != -1 else None)
    if isinstance(arg, float):  # selection of a single position
        return (arg, arg, 'cob')
    return arg


# Open reader or file
if args.generic:
    f = DS(args.ncfile,
           args.generic,
           logger_level='info' if args.verbose else 'error')
else:
    f = cdms2.open(args.ncfile)

# Find a variable name if not provided
if not args.var:
    if args.generic:
        allvars = f.dataset[0].listvariables()
    else:
        allvars = f.listvariables()
    for var in allvars:
        if 'bounds' not in var:
            if args.generic:
                var = '+' + var
            args.var = [var]
"""Test :meth:`~vacumm.data.misc.dataset.OceanDataset.get_variable` with level as a string"""

# Inits
ncfile = "menor.nc"

# Imports
from vcmq import DS, os, map2, data_sample, code_file_name

# Read data
ncfile = data_sample(ncfile)
ds = DS(ncfile, 'mars', logger_level='critical')
tbot = ds.get_temp(level='bottom', squeeze=True)
tsurf = ds.get_temp(level='surf', squeeze=True)

# Plot bottom
figfile = code_file_name(ext='png')
if os.path.exists(figfile): os.remove(figfile)
kw = dict(contour=True,
          fill='contourf',
          colorbar_shrink=0.8,
          cmap='cmocean_thermal',
          linewidth=.3)
m = map2(tsurf,
         subplot=211,
         close=False,
         show=False,
         figsize=(5, 7),
         title='Testing Dataset.get_temp(level="surf")',
         **kw)
map2(tbot,
     subplot=212,
示例#12
0
"""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,
示例#13
0
class NcReader(object):
    """A generic interface to open and read a netcdf file

    Examples
    --------

    >>> f = NcReader(ncfile, 'mars3d') # VACUMM Mars3D reader
    >>> u = f('u', lon=(10, 12))    # generic var name
    >>> u = f('+sst', lon=(10, 12)) # netcdf var name
    >>> f.close()

    >>> f = NcReader(ncfile, 'cdms2') # direct cdms2 reader
    >>> u = f('u', lon=(10, 12)) # netcdf var name
    >>> f.close()

    >>> f = NcReader(ncfile, 'netcdf') # python netcdf4 reader
    >>> u = f('u', slice(0, 10)) # <=> f['u'][:10]
    >>> f.close()

    """

    def __init__(self, ncfile, readertype='generic',  **kwargs):
        self.ncfile = ncfile
        if not isinstance(readertype, basestring):
            self.f = readertype(ncfile,  **kwargs)
            self.type = 'callable'
        elif readertype=='cdms2':
            self.f = cdms2.open(ncfile,  **kwargs)
            self.type = readertype
        elif readertype=='netcdf4':
            import netcdf4
            self.f = netcdf4.Dataset(ncfile,  **kwargs)
            self.type = readertype
        else:
            self.f = DS(ncfile, readertype, **kwargs)
            self.type = 'dataset'


    def __call__(self, vname, *args, **kwargs):

        if self.type=='netcdf4':

            args = tuple(filter(lambda x: isinstance(x, slice), args))

            return self.f[vname][args]

        else:

            return self.f(vname, *args, **kwargs)

    def __getitem__(self, vname):
        return self.f[vname]

    def get_variables(self):
        if self.type=='netcdf4' or self.type=='cdms2':
            return self.variables.keys()
        if self.type=='dataset':
            return self.f.dataset[0].variables.keys()
        raise SONATError('Method yet not implemented for read type "{}"'.format(self.type))

    def close(self):
        self.f.close()
示例#14
0
#!/usr/bin/env python
# -*- coding: utf8 -*-
"""Diagnostics thermodynamiques et dynamiques (:mod:`vacumm.diag.thermdyn` et :mod:`vacumm.diag.dynamics`)"""

from vcmq import DS, data_sample, map2, density, mixed_layer_depth,  barotropic_geostrophic_velocity, kinetic_energy, shapiro2d

# Lecture des données de base
ds = DS(data_sample('menor.nc'))
temp = ds.get_temp(squeeze=1)
sal = ds.get_sal(squeeze=1)
depth = ds.get_depth(squeeze=1)
ssh = ds.get_ssh(squeeze=1)


# Densité
dens_surf = density(temp[-1], sal[-1])                      # -> CALCULER DENSITE 3D
map2(dens_surf)


# Couche mélangée
mld = mixed_layer_depth((temp, sal), depth, mode='deltadens') # -> ESSAYER AUTRES MODES
map2(mld, vmax=600.)


# Vitesse geostrophique
# - vitesses
ug, vg = barotropic_geostrophic_velocity(ssh)
# - energie cinetique
ke = kinetic_energy((ug, vg))                                 # -> TESTER AVEC SSH
ke.long_name = 'Surface geostrophique kinetic energy'
ke = shapiro2d(ke)
示例#15
0
# Parameters
ncfile = "menor.nc"
lon = (3., 4.5)
lat = (42, 42.8)

# Imports
from vcmq import DS, map2, data_sample
from vacumm.diag.thermdyn import mixed_layer_depth

# Read temperature and depth
ncfile = data_sample(ncfile)
ds = DS(ncfile, 'mars', lon=lon, lat=lat)
temp = ds.get_temp(squeeze=1)
depth = ds.get_depth(squeeze=1)

# Compute MLD
mld = mixed_layer_depth(temp, depth, mode='deltatemp')

# Plot
map2(mld,
     proj='merc',
     figsize=(6, 6),
     autoresize=0,
     colorbar_shrink=0.7,
     right=1,
     savefigs=__file__,
     show=False,
     close=True)

print 'Done'
"""Test :meth:`~vacumm.data.misc.dataset.Dataset.plot_transect` on MFS"""

# Inits
ncfile = "mfs.nc"
lons = (3.125,5.5)
lats = (43.4375,42.125)

# Imports
from vcmq import DS, data_sample, os, code_file_name

# Setup dataset
ds = DS(data_sample(ncfile), 'nemo', logger_level='critical')

# Plot transect
figfile = code_file_name(ext='png')
if os.path.exists(figfile): os.remove(figfile)
ds.plot_transect('temp', lons, lats, outaxis='dist', show=False, fill='contourf',
    figsize=(6,4), savefig=figfile, top=0.9, right=0.89, close=True)

示例#17
0
# Parameters
ncfile = "menor.nc"
lon = (3., 4.5)
lat = (42, 42.8)

# Imports
from vcmq import DS, map2, data_sample

# Read temperature and depth
ncfile = data_sample(ncfile)
ds = DS(ncfile, 'mars', lon=lon, lat=lat)
mld = ds.get_mld(mode='deltatemp', squeeze=1)

# Plot
map2(mld,
     proj='merc',
     figsize=(6, 6),
     autoresize=0,
     colorbar_shrink=0.7,
     right=1,
     savefigs=__file__,
     show=False,
     close=True)

print 'Done'
示例#18
0
"""Test :meth:`~vacumm.data.misc.dataset.OceanDataset.get_mld` with deltadens in MENOR"""

# Inits
ncfile = "menor.nc"

# Imports
from vcmq import DS, os, map2, data_sample, code_file_name

# Read data
ds = DS(data_sample(ncfile), 'mars', logger_level='critical')
mld = ds.get_mld(mode='deltadens', deltadens=0.06, squeeze=True)

# Plot surface density
figfile = code_file_name(ext='png')
if os.path.exists(figfile): os.remove(figfile)
map2(mld,
     savefig=figfile,
     close=True,
     show=False,
     fill='pcolormesh',
     cmap='vacumm_previmer',
     contour=False)

# For unittest
result = {'files': figfile}
示例#19
0
        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 :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)

示例#21
0
"""Test :func:`~vacumm.misc.grid.regridding.transect`"""

# Inits
ncfile = "menor.nc"
lon0 = 3.31
lat0 = 43.19
lon1 = 4.78
lat1 = 42.01

# Imports
from vcmq import DS, data_sample, mixed_layer_depth, N, transect, section2, curve2, code_file_name, os
from vacumm.misc.plot import add_map_lines

# Read data
ds = DS(data_sample(ncfile), 'mars', logger_level='critical')
temp = ds.get_temp(squeeze=True)
dens = ds.get_dens(squeeze=True)
depth = ds.get_depth(squeeze=True)

# Compute MLD
mld = mixed_layer_depth(dens, depth=depth, mode='deltadens', format_axes=True)
del dens

# Compute transect
tlons = (lon0,lon1)
tlats = (lat0,lat1)
tlons = N.concatenate([N.linspace(lon0,lon1,100.),N.linspace(lon1,lon1,100.)])
tlats = N.concatenate([N.linspace(lat0,lat1,100.),N.linspace(lat1,lat0,100.)])
xtemp, xlons, xlats = transect(temp, tlons, tlats, getcoords=True, outaxis='dist')
xdepth = transect(depth, tlons, tlats)
xmld = transect(mld, tlons, tlats)
示例#22
0
def load_model_at_regular_dates(ncpat,
                                varnames=None,
                                time=None,
                                lat=None,
                                lon=None,
                                level=None,
                                depths=None,
                                modeltype='mars',
                                nt=50,
                                dtfile=None,
                                sort=True,
                                asdict=False,
                                logger=None,
                                **kwargs):
    """Read model output at nearest unique dates with optional linear interpolation


    Parameters
    ----------
    ncpat: string or list of strings
    varnames: string, strings
        Generic var names. If None, all variables that are known from the
        :mod:`vacumm.data.cf` module are used.
    level: string, None, list of floats, array, tuple of them, dict
        Here are some possible values:

        - "surf" or "bottom": self explenatory
        - None or "3d": No slice, so get all levels with no interpolation.
        - A list or array of negative depths: get all levels and
          interpolate at these depths.

        Variables sliced with "surf" and "bottom" are returned with
        an id suffixed with "_surf" or "_bottom".
        You can speficy different slicings  using a tuple
        of depth specifications.
        You can specialise slicings of a variable using a dictionary with
        the key as the variable name.

    See also
    --------
    :func:`sonat.misc.list_files_from_pattern` for more options


    Examples
    --------
    >>> mdict = load_model_at_regular_dates('myfile.nc', level='surf')
    >>> mdict = load_model_at_regular_dates('myfile.nc', level=('surf', 'bottom')
    >>> mdict = load_model_at_regular_dates('myfile.nc', varnames=['temp', 'sal'],
        level={'temp':('surf', 'bottom'), 'sal':[-50, -10]})
    >>> mdict = load_model_at_regular_dates('myfile.nc', varnames=['temp', 'sal'],
        level={'temp':('surf', '3d'), 'sal':None}, depths=[-50, -10])


    """
    # Logger
    kwlog = kwfilter(kwargs, 'logger_')
    if logger is None:
        logger = get_logger(**kwlog)
    logger.debug('Loading model at regular dates')

    # Get file list
    ncfiles = list_files_from_pattern(ncpat, time, dtfile=dtfile, sort=True)
    if not ncfiles:
        raise SONATError('No file found')

    # Time interval
    reqtime = time
    if time is None:

        # First
        taxis = ncget_time(ncfiles[0])
        if taxis is None:
            raise SONATError("Can't get time axis for: " + ncfiles[0])
        ctimes = taxis.asComponentTime()
        ct0 = ctimes[0]

        # Last
        if ncfiles[0] != ncfiles[-1]:
            taxis = ncget_time(ncfiles[-1])
            if taxis is None:
                raise SONATError("Can't get time axis for: " + ncfiles[-1])
            ctimes = taxis.asComponentTime()
        ct1 = ctimes[-1]

        # Time
        time = (ct0, ct1)

    # Generate dates
    dates = lindates(time[0], time[1], nt)

    # Get time indices
    iidict, iiinfo = ncfiles_time_indices(ncfiles,
                                          dates,
                                          getinfo=True,
                                          asslices=True)
    if iiinfo['missed'] or iiinfo['duplicates']:
        msg = ("You must provide at least {nt} model time steps to read "
               "independant dates")
        if reqtime:
            msg = msg + (", and your requested time range must be enclosed "
                         "by model time range.")
        raise SONATError(msg)

    # Read
    single = isinstance(varnames, basestring)
    if single:
        varnames = [varnames]
    out = OrderedDict()
    vlevels = {}
    if not isinstance(level, dict):
        level = {'__default__': level}
    for ncfile, tslices in iidict.items():

        # Dataset instance
        ds = DS(ncfile,
                modeltype,
                logger_name='SONAT.Dataset',
                logger_level='error')

        # List of well known variables
        if varnames is None:
            varnames = []
            for ncvarname in ds.get_variable_names():
                varname = match_known_var(ds[0][ncvarname])
                if varname:
                    varnames.append(varname)

        # Loop on variables
        vardepth = None
        kwvar = dict(lat=lat, lon=lon, verbose=False, bestestimate=False)
        for vname in list(varnames):

            # Level selector for this variable
            if vname in vlevels:  # cached
                vlevel = vlevels[vname]
            else:
                vlevel = interpret_level(dicttree_get(level, vname),
                                         astuple=True)
                vlevels[vname] = vlevel  # cache it

            # Loop on level specs
            for vlev in vlevel:

                # Output vname and vlev check
                if not isinstance(vlev, basestring):
                    vnameo = vname
                elif vlev not in ('surf', "bottom", "3d"):
                    raise SONATError('Depth string must one of '
                                     'surf, bottom, 3d')
                elif vlev != '3d':
                    vnameo = vname + '_' + vlev
                else:
                    vlev = None
                    vnameo = vname

                # Slicing level and output depths
                if vlev not in ['surf', 'bottom']:

                    # numeric so interpolation
                    if vlev is None:
                        vdep = depths if depths is not None else None
                    else:
                        vdep = vlev
                    interp = vdep is not None
                    if interp:
                        vlev = None

                else:

                    interp = False

                # Read and aggregate
                vout = out.setdefault(vnameo, [])
                #                vinterp = None
                for tslice in tslices:

                    # Get var
                    kwvar['time'] = tslice
                    var = ds(vname, level=vlev, **kwvar)

                    # Interpolate at numeric depths
                    if interp and var.getLevel() is not None:

                        # Get depths
                        if True or vardepth is None:  #FIXME: bad to always read it
                            vardepth = ds.get_depth(level=vlev,
                                                    zerolid=True,
                                                    **kwvar)

                        # Interpolate
                        var = ds._interp_at_depths_(var,
                                                    vardepth,
                                                    vdep,
                                                    extrap='top')

                    # Id with suffix
                    var.id = vnameo

                    # Store results
                    vout.append(var)

    # Concatenate
    for vname, vout in out.items():
        out[vname] = MV2_concatenate(vout)

    # Dict
    if asdict:
        return out

    # Single
    out = out.values()
    if single:
        return out[0]
    return out
"""Test :meth:`~vacumm.data.misc.dataset.Dataset.get_var` on multiple files and time selections"""

# Inits
ncfiles = "mars2d.xyt.*.nc"

# Imports
from vcmq import DS, os, map2, data_sample

# Glob pattern
result = []
ds = DS(data_sample("mars2d.xyt.*.nc"), 'mars', logger_level='critical',
    time=("2008-08-15 06", "2008-08-15 09"))
assert ds.get_ssh().shape == (4, 50, 50)
assert ds.get_ssh(time=("2008-08-15 06", "2008-08-15 07", 'co'),squeeze=True).shape==(50, 50)

# With dates
ds = DS(data_sample("mars2d.xyt.%Y%m%d%H.nc"), 'mars', logger_level='critical',
    time=("2008-08-15 06", "2008-08-15 09"))
assert ds.get_ssh(
    time=("2008-08-15 06", "2008-08-15 07", 'cc')).shape==(2, 50, 50)


示例#24
0
#    if re.match(r'[\(\[].*', arg): # classic selector
#        return tuple(eval(arg))
    if RE_MATCH_TIME(arg) and not arg.isdigit(): # date
        return arg
    if re.match('[\w\s]*:[\w\s]*(:[\w\s]*)?$', arg): # args to slice
        return slice(*[(None if a=='' else eval(a)) for a in arg.split(':')])
    arg = eval(arg)
    if isinstance(arg, int): # slice at a single index
        return slice(arg, (arg+1) if arg!=-1 else None)
    if isinstance(arg, float): # selection of a single position
        return (arg, arg, 'cob')
    return arg

# Open reader or file
if args.generic:
    f = DS(args.ncfile, args.generic, logger_level='info' if args.verbose else 'error')
else:
    f = cdms2.open(args.ncfile)

# Find a variable name if not provided
if not args.var:
    if args.generic:
        allvars = f.dataset[0].listvariables()
    else:
        allvars = f.listvariables()
    for var in allvars:
        if 'bounds' not in var:
            if args.generic:
                var = '+'+var
            args.var = [var]
            break
示例#25
0
"""Test :meth:`~vacumm.data.misc.OceanDataset.plot_transect` with MENOR"""

# Inits
ncfile = "menor.nc"
lons = (3.76, 4.7)
lats = (43.1, 42)

# Imports
from vcmq import DS, data_sample, os, code_file_name

# Setup dataset
ds = DS(data_sample(ncfile), 'mars', logger_level='critical')

# Plot transect
figfile = code_file_name(ext='png')
if os.path.exists(figfile): os.remove(figfile)
ds.plot_transect('temp',
                 lons,
                 lats,
                 figsize=(6, 4),
                 fill='contourf',
                 nmax=30,
                 savefig=figfile,
                 top=0.9,
                 right=0.89,
                 show=False,
                 close=True)
"""Test :meth:`~vacumm.data.misc.OceanDataset.plot_transect` with MENOR"""

# Inits
ncfile = "menor.nc"
lons = (3.76, 4.7)
lats = (43.1, 42)

# Imports
from vcmq import DS, data_sample, os, code_file_name

# Setup dataset
ds = DS(data_sample(ncfile), 'mars', logger_level='critical')

# Plot transect
figfile = code_file_name(ext='png')
if os.path.exists(figfile): os.remove(figfile)
ds.plot_transect('temp', lons, lats, figsize=(6,4), fill='contourf', nmax=30, 
    savefig=figfile, top=0.9, right=0.89, show=False, close=True)
"""Test :meth:`~vacumm.data.misc.dataset.OceanDataset.get_depth` in MENOR"""

# Inits
ncfile = "menor.nc"

# Imports
from vcmq import DS, os, map2, data_sample, code_file_name

# Read data
ncfile = data_sample(ncfile)
ds = DS(ncfile, 'mars', logger_level='critical') 
depth = ds.get_depth(squeeze=True)

# Plot midlevel
figfile = code_file_name(ext='png')
if os.path.exists(figfile): os.remove(figfile)
map2(depth[depth.shape[0]/2], savefig=figfile, close=True, vmax=0, show=False, 
    colorbar_shrink=0.6, title='Testing Dataset.get_depth()')

# For unittest
result = [
    ('files', figfile), 
    ]
示例#28
0
"""Test :func:`~vacumm.misc.grid.regridding.transect`"""

# Inits
ncfile = "menor.nc"
lon0 = 3.31
lat0 = 43.19
lon1 = 4.78
lat1 = 42.01

# Imports
from vcmq import DS, data_sample, mixed_layer_depth, N, transect, section2, curve2, code_file_name, os
from vacumm.misc.plot import add_map_lines

# Read data
ds = DS(data_sample(ncfile), 'mars', logger_level='critical')
temp = ds.get_temp(squeeze=True)
dens = ds.get_dens(squeeze=True, potential=True)
depth = ds.get_depth(squeeze=True)

# Compute MLD
mld = mixed_layer_depth(dens, depth=depth, mode='deltadens', format_axes=True)
del dens

# Compute transect
tlons = (lon0, lon1)
tlats = (lat0, lat1)
tlons = N.concatenate(
    [N.linspace(lon0, lon1, 100.),
     N.linspace(lon1, lon1, 100.)])
tlats = N.concatenate(
    [N.linspace(lat0, lat1, 100.),
"""Test the asvar keyword of :meth:`~vacumm.data.misc.dataset.Dataset.finalize_object` on MENOR"""

# Inits
ncfile = "menor.nc"

# Imports
from vcmq import DS, data_sample

# Read data
ds = DS(data_sample(ncfile), 'mars', logger_level='critical')
temp = ds.get_temp()
bathy = ds.get_bathy(asvar=temp)

# For unittest
result = {'assertTupleEqual': [bathy.shape, temp.shape]}
示例#30
0
"""Test :meth:`~vacumm.data.misc.dataset.OceanDataset.get_temp` on MFS"""

# Inits
ncfile = "mfs.nc"

# Imports
from vcmq import DS, data_sample

# Read data
ds = DS(data_sample(ncfile), 'nemo', logger_level='critical')
temp = ds.get_temp()
lon = temp.getLongitude()

# For unittest
result = {'assertIsNotNone':lon}
示例#31
0
"""Test :meth:`~vacumm.data.misc.dataset.OceanDataset.get_dens` in MENOR"""

# Inits
ncfile = "menor.nc"

# Imports
from vcmq import DS, os, map2, data_sample, code_file_name

# Read data
ds = DS(data_sample(ncfile), 'mars', logger_level='critical')
dens = ds.get_dens(squeeze=True)

# Plot surface density
figfile = code_file_name(ext='png')
if os.path.exists(figfile): os.remove(figfile)
map2(dens[-1], savefig=figfile, close=True, vmin=1025.,show=False)

# For unittest
result = {'files':figfile}
示例#32
0
#!/usr/bin/env python
# -*- coding: utf8 -*-
"""Exploitation generique de donnes grilles (:mod:`vacumm.data.misc.dataset`)"""

from vcmq import DS, data_sample, map2

# Initialisation
ds = DS(data_sample('menor.nc'), 'mars', lon=(4, 5), lat=(42.5, 43.5), log_level='debug')
print ds.dataset


# Logging
ds.info('Pratique')                                 # -> ESSAYER WARNING
ds.set_loglevel('debug')


# Coordonnees
lon = ds.get_lon()                                  # -> ESSAYER AU POINT U
grid = ds.get_grid()


# Variables
temp = ds.get_temp(level=slice(-1, None), squeeze=True)
sst = ds.get_sst(squeeze=True)                      # -> VERIFIER ATTRIBUTS
ds.info('Plot SST')
map2(sst)
sal = ds.get_sal(lon=(4., 4.5))
print temp.shape, sal.shape

# Generique
ds2 = DS(data_sample('mfs.nc'), 'nemo', lon=(4, 5), lat=(42.5, 43.5))
示例#33
0
"""Test :meth:`~vacumm.data.misc.dataset.OceanDataset.get_depth` in MENOR"""

# Inits
ncfile = "menor.nc"

# Imports
from vcmq import DS, os, map2, data_sample, code_file_name

# Read data
ncfile = data_sample(ncfile)
ds = DS(ncfile, 'mars', logger_level='critical')
depth = ds.get_depth(squeeze=True)

# Plot midlevel
figfile = code_file_name(ext='png')
if os.path.exists(figfile): os.remove(figfile)
map2(depth[depth.shape[0] / 2],
     savefig=figfile,
     close=True,
     vmax=0,
     show=False,
     colorbar_shrink=0.6,
     title='Testing Dataset.get_depth()')

# For unittest
result = [
    ('files', figfile),
]
示例#34
0
"""Test :meth:`~vacumm.data.misc.dataset.Dataset.plot_transect` on MFS"""

# Inits
ncfile = "mfs.nc"
lons = (3.125, 5.5)
lats = (43.4375, 42.125)

# Imports
from vcmq import DS, data_sample, os, code_file_name

# Setup dataset
ds = DS(data_sample(ncfile), 'nemo', logger_level='critical')

# Plot transect
figfile = code_file_name(ext='png')
if os.path.exists(figfile): os.remove(figfile)
ds.plot_transect('temp',
                 lons,
                 lats,
                 outaxis='dist',
                 show=False,
                 fill='contourf',
                 figsize=(6, 4),
                 savefig=figfile,
                 top=0.9,
                 right=0.89,
                 close=True)
"""Test :meth:`~vacumm.data.misc.dataset.OceanDataset.get_variable` with level as a string"""

# Inits
ncfile = "menor.nc"

# Imports
from vcmq import DS, os, map2, data_sample, code_file_name

# Read data
ncfile = data_sample(ncfile)
ds = DS(ncfile, 'mars', logger_level='critical')
tbot = ds.get_temp(level='bottom', squeeze=True)
tsurf = ds.get_temp(level='surf', squeeze=True)

# Plot bottom
figfile = code_file_name(ext='png')
if os.path.exists(figfile): os.remove(figfile)
kw = dict(contour=True, fill='contourf',  colorbar_shrink=0.8,
    cmap='cmocean_thermal', linewidth=.3)
m = map2(tsurf, subplot=211, close=False, show=False, figsize=(5, 7),
    title='Testing Dataset.get_temp(level="surf")',  **kw)
map2(tbot, subplot=212, close=True, show=True, m=m,
    title='Testing Dataset.get_temp(level="bottom")',
    savefig=figfile, **kw)

示例#36
0
"""Test :meth:`~vacumm.data.misc.OceanDataset.plot_hsection` with MENOR"""

# Inits
ncfile = "menor.nc"
depth = -1000.

# Imports
from vcmq import DS, data_sample, os, os, code_file_name

# Setup dataset
ds = DS(data_sample(ncfile), 'mars', logger_level='critical')

# Plot hsection
figfile = code_file_name(ext='png')
if os.path.exists(figfile): os.remove(figfile)
ds.plot_hsection('temp', depth, savefig=figfile, fill='contourf', show=False, close=True)

示例#37
0
"""Test :meth:`~vacumm.data.misc.dataset.OceanDataset.get_temp` with ``at`` keyword and :meth:`~vacumm.data.misc.dataset.OceanDataset.get_temp_u`"""


# Inits
ncfile = "menor.nc"

# Imports
from vcmq import DS, data_sample

# Read data
ds = DS(data_sample(ncfile), 'mars', logger_level='critical')
uu = ds.get_u3d()
uv1 = ds.get_u3d(at='v')
uv2 = ds.get_u3d_v()

# For unittest
result = [
    ('assertNotEqual', [uu.mean(), uv1.mean()]), 
    ('assertEqual', [uv1.mean(), uv2.mean()]), 
    ('assertEqual', [uv1.id, "u3d_v"]), 
    ('assertEqual', [uv2.id, "u3d_v"]), 
]
示例#38
0
"""Test the asvar keyword of :meth:`~vacumm.data.misc.dataset.Dataset.finalize_object` on MFS"""

# Inits
ncfile = "mfs.nc"

# Imports
from vcmq import DS, data_sample

# Read data
ds = DS(data_sample(ncfile), 'nemo', logger_level='critical')
temp = ds.get_temp()
depth = ds.get_depth(asvar=temp)

# For unittest
result = {'assertTupleEqual':[depth.shape, temp.shape]}
"""Test :meth:`~vacumm.data.misc.OceanDataset.plot_hsection` with MFS"""

# Inits
ncfile = "mfs.nc"
depth = -1000.0

# Imports
from vcmq import DS, data_sample, os, code_file_name

# Setup dataset
ds = DS(data_sample(ncfile), "nemo", logger_level="critical")

# Plot hsection
figfile = code_file_name(ext="png")
if os.path.exists(figfile):
    os.remove(figfile)
ds.plot_hsection("temp", depth, savefig=figfile, fill="contourf", show=False, close=True)
示例#40
0
"""Test :meth:`~vacumm.data.misc.dataset.OceanDataset.get_mld` with deltadens in MENOR"""

# Inits
ncfile = "menor.nc"

# Imports
from vcmq import DS, os, map2, data_sample, code_file_name

# Read data
ds = DS(data_sample(ncfile), 'mars', logger_level='critical')
mld = ds.get_mld(mode='deltadens', deltadens=0.06, squeeze=True)

# Plot surface density
figfile = code_file_name(ext='png')
if os.path.exists(figfile): os.remove(figfile)
map2(mld, savefig=figfile, close=True, show=False, fill='pcolormesh',
    cmap='vacumm_previmer', contour=False)

# For unittest
result = {'files':figfile}
示例#41
0
        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,
"""Test the asvar keyword of :meth:`~vacumm.data.misc.dataset.Dataset.finalize_object` on MENOR"""

# Inits
ncfile="menor.nc"

# Imports
from vcmq import DS, data_sample

# Read data
ds = DS(data_sample(ncfile), 'mars', logger_level='critical')
temp = ds.get_temp()
bathy = ds.get_bathy(asvar=temp)

# For unittest
result = {'assertTupleEqual':[bathy.shape, temp.shape]}