from vacumm.config import data_sample
select = dict(lon=(-5.3, -4.72), lat=(47.9, 48.8), time=slice(0, 24))
f = cdms2.open(data_sample('mars2d.xyt.nc'))
v = MV2.masked_values(f('v', **select), 0., copy=False)
f.close()

# Diagonale
lon = v.getLongitude()
lat = v.getLatitude()
nd = N.sqrt(len(lon)**2. + len(lat)**2) / 2.
xo = N.linspace(lon[0], lon[-1], nd)
yo = N.linspace(lat[0], lat[-1], nd)

# Interpolation
from vacumm.misc.grid.regridding import grid2xy
vo = grid2xy(v, xo, yo, method='bilinear')

# Plot
# - variable interpolee
from vacumm.misc.plot import hov2 as hov, savefigs, map2 as map
hov(vo,
    cmap='jet',
    show=False,
    top=.9,
    date_fmt='%H',
    colorbar_shrink=.5,
    left=.13)
# - carte + diagonal
import pylab as P
m = map(v[0],
        xhide=True,
select = dict(lon=(-5.3, -4.72), lat=(47.9, 48.8), time=slice(0, 24))
f = cdms2.open(data_sample("mars2d.xyt.nc"))
v = MV2.masked_values(f("v", **select), 0.0, copy=False)
f.close()

# Diagonale
lon = v.getLongitude()
lat = v.getLatitude()
nd = N.sqrt(len(lon) ** 2.0 + len(lat) ** 2) / 2.0
xo = N.linspace(lon[0], lon[-1], nd)
yo = N.linspace(lat[0], lat[-1], nd)

# Interpolation
from vacumm.misc.grid.regridding import grid2xy

vo = grid2xy(v, xo, yo, method="bilinear")

# Plot
# - variable interpolee
from vacumm.misc.plot import hov2 as hov, savefigs, map2 as map

hov(vo, cmap="jet", show=False, top=0.9, date_fmt="%H", colorbar_shrink=0.5, left=0.13)
# - carte + diagonal
import pylab as P

m = map(
    v[0],
    xhide=True,
    yhide=True,
    contour=False,
    title=False,
Esempio n. 3
0
    def coloc_mod_on_pro(self,
                         model,
                         profiles,
                         varnames,
                         select=None,
                         method='nearest'):
        '''Colocalize model on profile data.

        Load model data corresponding to the selected profiles positions and time.

        Returns loaded model longitudes, latitudes, depths and requested variable(s)

        :Params:
            - **model**: model data :class:`~vacumm.data.misc.dataset.Dataset`
            - **profiles**: profile data :class:`~vacumm.data.misc.profile.ProfilesDataset`
            - **varnames**: variables to load (ex: ('temp','sal') or (('temp','temperature'),('sal','salinity'))
            - **select**: selector
            - **method**: coloc method (**nearest** or **interp**)

        :Return:
            - **lons_mod**: model longitude coordinates, shape: (profile)
            - **lats_mod**: model latitude coordinates, shape: (profile)
            - **deps_mod**: model depth coordinates, shape: (level,profile)
            - **var1**: requested variables, shape: (level,profile)
            - ...
            - **varN**

        .. todo::
            - also load and return profile data here
            - exclude coords where profile data is masked (no data for specified depth)
            - return time coordinates
            - return depth and vars with shape (profile,level)

        '''

        self.verbose(
            'Colocalizing %s on %s\nvarnames: %s\nselect: %s\n method: %s',
            model.__class__.__name__, profiles.__class__.__name__, varnames,
            select, method)
        prof_pro = profiles.get_axis('profile', select=select)
        if prof_pro is None or not len(prof_pro):
            raise Exception('No profiles found, aborting')
        lev_pro = profiles.get_axis('level', select=select)
        time_pro = profiles.get_variable('time', select=select)
        lons_pro = profiles.get_variable('longitude', select=select)
        lats_pro = profiles.get_variable('latitude', select=select)
        dates = create_time(time_pro).asComponentTime()

        self.info('Number of profiles: %s', len(dates))
        self.info('Profiles time coverage: %s to %s', dates[0], dates[-1])

        # Init model
        td = model.get_time_res()
        dtmax = (td.days * 86400 + td.seconds, 'seconds')
        self.info('Detected model time step: %s', td)
        grid_mod = model.get_grid()
        xres, yres = resol(grid_mod)
        time_mod = model.get_time()
        ctime_mod = time_mod.asComponentTime()
        self.info('Model time coverage: %s to %s', ctime_mod[0], ctime_mod[-1])

        level_mod = model.get_level(select=select)
        lons_mod = MV2.zeros((len(prof_pro), )) + MV2.masked
        lats_mod = lons_mod.clone()
        deps_mod = MV2.zeros((len(level_mod), len(prof_pro))) + MV2.masked
        deps_mod.setAxis(1, prof_pro)
        lons_mod.id, lats_mod.id, deps_mod.id = 'longitude', 'latitude', 'depth'

        # Creation des variables demandees
        variables = []
        for n in varnames:
            v = MV2.zeros((len(level_mod), len(prof_pro))) + MV2.masked
            v.setAxis(1, prof_pro)
            v.id = is_iterable(n) and n[0] or n
            variables.append(v)

        cdms2.setAutoBounds(1)  # ???

        # Boucle temporelle
        for ip, date in enumerate(dates):
            try:
                # Limites spatiales
                lon = lons_pro[ip]
                lat = lats_pro[ip]
                lon_min = lon - 2 * xres
                lon_max = lon + 2 * xres
                lat_min = lat - 2 * yres
                lat_max = lat + 2 * yres
                date_interval = (add_time(date, -dtmax[0], dtmax[1]),
                                 add_time(date, dtmax[0], dtmax[1]), 'ccb')

                self.info('Colocalizing data for date %s, lon: %s, lat: %s',
                          date, lon, lat)

                # Methode 1 : donnees les plus proches
                if method == 'nearest':
                    sel = dict(time=(date, date, 'ccb'),
                               longitude=(lon, lon, 'ccb'),
                               latitude=(lat, lat, 'ccb'))
                    # Verifier la disponibilite des donnees
                    if time_mod.mapIntervalExt(sel['time']) is None:
                        self.warning('Time interval %s not found', sel['time'])
                        continue
                    if grid_mod.getLatitude().mapInterval(
                            sel['latitude']) is None:
                        self.warning('Latitude coordinate %s not found',
                                     sel['latitude'])
                        continue
                    if grid_mod.getLongitude().mapInterval(
                            sel['longitude']) is None:
                        self.warning('Longitude coordinate %s not found',
                                     sel['longitude'])
                        continue

                    # Load tmp depth to get lon & lat coordinates
                    #tmp = model.get_depth(select=sel, squeeze=False) # tmp squeezed !!! see sigma ?
                    tmp = model.get_variable(varnames[0],
                                             select=sel,
                                             squeeze=False)
                    lons_mod[ip] = tmp.getLongitude()[0]
                    lats_mod[ip] = tmp.getLatitude()[0]
                    deps_mod[:, ip] = model.get_depth(select=sel, squeeze=True)
                    for iv, vn in enumerate(varnames):
                        variables[iv][:, ip] = model.get_variable(vn,
                                                                  select=sel,
                                                                  squeeze=True)

                # Methode 2 : interpolation
                elif method == 'interp':
                    sel = dict(time=date_interval,
                               longitude=(lon_min, lon_max),
                               latitude=(lat_min, lat_max))
                    if time_mod.mapIntervalExt(sel['time']) is None:
                        self.warning('Time interval %s not found', sel['time'])
                        continue
                    if grid_mod.getLatitude().mapInterval(
                            sel['latitude']) is None:
                        self.warning('Latitude coordinate %s not found',
                                     sel['latitude'])
                        continue
                    if grid_mod.getLongitude().mapInterval(
                            sel['longitude']) is None:
                        self.warning('Longitude coordinate %s not found',
                                     sel['longitude'])
                        continue

                    # Lectures
                    order = 'tzyx'
                    # lon & lat du profile car interp sur cette position
                    lons_mod[ip], lats_mod[ip] = lon, lat
                    deps_mod_tzyx = model.get_depth(select=sel,
                                                    order=order,
                                                    squeeze=True)
                    tmp_tzyx = []
                    for iv, vn in enumerate(varnames):
                        tmp_tzyx.append(
                            model.get_variable(vn,
                                               select=sel,
                                               order=order,
                                               squeeze=True))

                    # Interpolations temporelles
                    mctime = tmp_tzyx[0].getTime()
                    mrtime = mctime.asRelativeTime()
                    d0 = date.torel(mctime.units).value - mrtime[0].value
                    d1 = mrtime[1].value - date.torel(mctime.units).value
                    f0 = d0 / (d0 + d1)
                    f1 = d1 / (d0 + d1)
                    deps_mod_zyx = f0 * deps_mod_tzyx[0] + f1 * deps_mod_tzyx[1]
                    tmp_zyx = []
                    for iv, vn in enumerate(varnames):
                        tmp_zyx.append(f0 * tmp_tzyx[iv][0] +
                                       f1 * tmp_tzyx[iv][1])
                    del tmp_tzyx

                    # Interpolations spatiales
                    deps_mod[:, ip] = numpy.squeeze(
                        grid2xy(deps_mod_zyx,
                                numpy.array([lon]),
                                numpy.array([lat]),
                                method='nat'))
                    for iv, vn in enumerate(varnames):
                        variables[iv][:, ip] = numpy.squeeze(
                            grid2xy(tmp_zyx[iv],
                                    numpy.array([lon]),
                                    numpy.array([lat]),
                                    method='nat'))
                    del tmp_zyx

                else:
                    raise ValueError('Invalid colocation method: %s' %
                                     (method))
            except:
                self.exception('Failed to colocalize data for date %s', date)

        for v in [deps_mod] + variables:
            v.getAxis(0).id = 'level'
            v.getAxis(0).designateLevel()

        data = tuple([lons_mod, lats_mod, deps_mod] + variables)
        self.verbose('Colocalized data:\n  %s',
                     '\n  '.join(self.describe(o) for o in data))
        return data
Esempio n. 4
0
    from vacumm.misc.plot import curve2, minimap
    from vacumm.misc.color import cmap_custom
except:
    sys.exit("Can't import needed vacumm modules")

# Load bathy
b = NcGriddedBathy(lon=lon, lat=lat, name=options.name,
    cfgfile=options.cfgfile, varname=options.varname, lonname=options.lonname,
    latname=options.latname, imargin=2, jmargin=2, maxvalue=None,
    reverse=options.reverse)

# Interpolate on section
bathy = b.bathy()
lons, lats, xx, yy = transect_specs(bathy.getGrid(),
    options.x0, options.y0, options.x1, options.y1, getxy=True)
tbathy = grid2xy(bathy, lons, lats)
if not isinstance(options.along, basestring) or options.along not in ['lon', 'lat', 'm', 'km', 'dist', 'auto']:
    options.along = 'auto'
if options.along=='auto':
    options.along = 'lon' if xx.max()-xx.min() > yy.max()-yy.min() else 'lat'
if options.along=='lon':
    taxis = create_lon(lons)
elif options.along=='lat':
    taxis = create_lat(lats)
else:
    xy = N.sqrt((xx-xx[0])**2+(yy-yy[0])**2)
    if xy.max()-xy.min()>2500 or options.along=='km':
        units = 'km'
        xy *= 1e-3
    else:
        units = 'm'
Esempio n. 5
0
                   lonname=options.lonname,
                   latname=options.latname,
                   imargin=2,
                   jmargin=2,
                   maxvalue=None,
                   reverse=options.reverse)

# Interpolate on section
bathy = b.bathy()
lons, lats, xx, yy = transect_specs(bathy.getGrid(),
                                    options.x0,
                                    options.y0,
                                    options.x1,
                                    options.y1,
                                    getxy=True)
tbathy = grid2xy(bathy, lons, lats)
if not isinstance(options.along, basestring) or options.along not in [
        'lon', 'lat', 'm', 'km', 'dist', 'auto'
]:
    options.along = 'auto'
if options.along == 'auto':
    options.along = 'lon' if xx.max() - xx.min() > yy.max() - yy.min(
    ) else 'lat'
if options.along == 'lon':
    taxis = create_lon(lons)
elif options.along == 'lat':
    taxis = create_lat(lats)
else:
    xy = N.sqrt((xx - xx[0])**2 + (yy - yy[0])**2)
    if xy.max() - xy.min() > 2500 or options.along == 'km':
        units = 'km'
Esempio n. 6
0
    def coloc_mod_on_pro(self, model, profiles, varnames, select=None, method='nearest'):
        '''Colocalize model on profile data.

        Load model data corresponding to the selected profiles positions and time.

        Returns loaded model longitudes, latitudes, depths and requested variable(s)

        :Params:
            - **model**: model data :class:`~vacumm.data.misc.dataset.Dataset`
            - **profiles**: profile data :class:`~vacumm.data.misc.profile.ProfilesDataset`
            - **varnames**: variables to load (ex: ('temp','sal') or (('temp','temperature'),('sal','salinity'))
            - **select**: selector
            - **method**: coloc method (**nearest** or **interp**)

        :Return:
            - **lons_mod**: model longitude coordinates, shape: (profile)
            - **lats_mod**: model latitude coordinates, shape: (profile)
            - **deps_mod**: model depth coordinates, shape: (level,profile)
            - **var1**: requested variables, shape: (level,profile)
            - ...
            - **varN**

        .. todo::
            - also load and return profile data here
            - exclude coords where profile data is masked (no data for specified depth)
            - return time coordinates
            - return depth and vars with shape (profile,level)

        '''

        self.verbose('Colocalizing %s on %s\nvarnames: %s\nselect: %s\n method: %s', model.__class__.__name__, profiles.__class__.__name__, varnames, select, method)
        prof_pro = profiles.get_axis('profile', select=select)
        if prof_pro is None or not len(prof_pro):
            raise Exception('No profiles found, aborting')
        lev_pro = profiles.get_axis('level', select=select)
        time_pro = profiles.get_variable('time', select=select)
        lons_pro = profiles.get_variable('longitude', select=select)
        lats_pro = profiles.get_variable('latitude', select=select)
        dates = create_time(time_pro).asComponentTime()

        self.info('Number of profiles: %s', len(dates))
        self.info('Profiles time coverage: %s to %s', dates[0], dates[-1])

        # Init model
        td = model.get_time_res()
        dtmax = (td.days*86400+td.seconds, 'seconds')
        self.info('Detected model time step: %s', td)
        grid_mod = model.get_grid()
        xres, yres = resol(grid_mod)
        time_mod = model.get_time()
        ctime_mod = time_mod.asComponentTime()
        self.info('Model time coverage: %s to %s', ctime_mod[0], ctime_mod[-1])

        level_mod = model.get_level(select=select)
        lons_mod = MV2.zeros((len(prof_pro),))+MV2.masked
        lats_mod = lons_mod.clone()
        deps_mod = MV2.zeros((len(level_mod), len(prof_pro)))+MV2.masked
        deps_mod.setAxis(1, prof_pro)
        lons_mod.id, lats_mod.id, deps_mod.id = 'longitude', 'latitude', 'depth'

        # Creation des variables demandees
        variables = []
        for n in varnames:
            v = MV2.zeros((len(level_mod), len(prof_pro)))+MV2.masked
            v.setAxis(1, prof_pro)
            v.id = is_iterable(n) and n[0] or n
            variables.append(v)

        cdms2.setAutoBounds(1) # ???

        # Boucle temporelle
        for ip, date in enumerate(dates):
            try:
                # Limites spatiales
                lon = lons_pro[ip]
                lat = lats_pro[ip]
                lon_min = lon-2*xres
                lon_max = lon+2*xres
                lat_min = lat-2*yres
                lat_max = lat+2*yres
                date_interval = (add_time(date, - dtmax[0], dtmax[1]), add_time(date, dtmax[0], dtmax[1]), 'ccb')

                self.info('Colocalizing data for date %s, lon: %s, lat: %s', date, lon, lat)

                # Methode 1 : donnees les plus proches
                if method == 'nearest':
                    sel = dict(time=(date, date, 'ccb'), longitude=(lon, lon, 'ccb'), latitude=(lat, lat, 'ccb'))
                    # Verifier la disponibilite des donnees
                    if time_mod.mapIntervalExt(sel['time']) is None:
                        self.warning('Time interval %s not found', sel['time'])
                        continue
                    if grid_mod.getLatitude().mapInterval(sel['latitude']) is None:
                        self.warning('Latitude coordinate %s not found', sel['latitude'])
                        continue
                    if grid_mod.getLongitude().mapInterval(sel['longitude']) is None:
                        self.warning('Longitude coordinate %s not found', sel['longitude'])
                        continue

                    # Load tmp depth to get lon & lat coordinates
                    #tmp = model.get_depth(select=sel, squeeze=False) # tmp squeezed !!! see sigma ?
                    tmp = model.get_variable(varnames[0], select=sel, squeeze=False)
                    lons_mod[ip] = tmp.getLongitude()[0]
                    lats_mod[ip] = tmp.getLatitude()[0]
                    deps_mod[:, ip] =  model.get_depth(select=sel, squeeze=True)
                    for iv,vn in enumerate(varnames):
                        variables[iv][:,ip] = model.get_variable(vn, select=sel, squeeze=True)

                # Methode 2 : interpolation
                elif method == 'interp':
                    sel = dict(time=date_interval, longitude=(lon_min, lon_max), latitude=(lat_min, lat_max))
                    if time_mod.mapIntervalExt(sel['time']) is None:
                        self.warning('Time interval %s not found', sel['time'])
                        continue
                    if grid_mod.getLatitude().mapInterval(sel['latitude']) is None:
                        self.warning('Latitude coordinate %s not found', sel['latitude'])
                        continue
                    if grid_mod.getLongitude().mapInterval(sel['longitude']) is None:
                        self.warning('Longitude coordinate %s not found', sel['longitude'])
                        continue

                    # Lectures
                    order = 'tzyx'
                    # lon & lat du profile car interp sur cette position
                    lons_mod[ip], lats_mod[ip] = lon, lat
                    deps_mod_tzyx = model.get_depth(select=sel, order=order, squeeze=True)
                    tmp_tzyx = []
                    for iv,vn in enumerate(varnames):
                        tmp_tzyx.append(model.get_variable(vn, select=sel, order=order, squeeze=True))

                    # Interpolations temporelles
                    mctime = tmp_tzyx[0].getTime()
                    mrtime = mctime.asRelativeTime()
                    d0 = date.torel(mctime.units).value - mrtime[0].value
                    d1 = mrtime[1].value - date.torel(mctime.units).value
                    f0 = d0 / (d0 + d1)
                    f1 = d1 / (d0 + d1)
                    deps_mod_zyx = f0 * deps_mod_tzyx[0] + f1 * deps_mod_tzyx[1]
                    tmp_zyx = []
                    for iv,vn in enumerate(varnames):
                        tmp_zyx.append(f0 * tmp_tzyx[iv][0] + f1 * tmp_tzyx[iv][1])
                    del tmp_tzyx

                    # Interpolations spatiales
                    deps_mod[:,ip] = numpy.squeeze(grid2xy(deps_mod_zyx, numpy.array([lon]), numpy.array([lat]), method='nat'))
                    for iv,vn in enumerate(varnames):
                        variables[iv][:,ip] = numpy.squeeze(grid2xy(tmp_zyx[iv], numpy.array([lon]), numpy.array([lat]), method='nat'))
                    del tmp_zyx

                else:
                    raise ValueError('Invalid colocation method: %s'%(method))
            except:
                self.exception('Failed to colocalize data for date %s', date)

        for v in [deps_mod] + variables:
            v.getAxis(0).id = 'level'
            v.getAxis(0).designateLevel()

        data = tuple([lons_mod, lats_mod, deps_mod] + variables)
        self.verbose('Colocalized data:\n  %s', '\n  '.join(self.describe(o) for o in data))
        return data