valid_max='180.', axis='X')) LATITUDE = create_lat(lai, id='latitude', attributes=dict( long_name='Latitude of each location', standard_name='latitude', units='degrees_north', valid_min='-90.', valid_max='90.', axis='Y')) z = U.getLevel() DEPTH = create_dep(z, attributes=dict(long_name='sea water depth', standard_name='depth', units='m', valid_min='0.', valid_max='12000.')) #axes = [TIME,DEPTH,LATITUDE,LONGITUDE] axes = [DEPTH, LATITUDE, LONGITUDE] Uis = np.arange(U.getLevel().__len__() * yo.__len__() * xo.__len__()).reshape(U.getLevel().__len__(), yo.__len__(), xo.__len__()) Vis = np.arange(U.getLevel().__len__() * yo.__len__() * xo.__len__()).reshape(U.getLevel().__len__(), yo.__len__(), xo.__len__()) Uis = cdms2.createVariable(Uis, typecode='f',
def plot_layer_mod_on_pro(self, model, profiles, varname, depth, select=None, **kwargs): '''Get a layer of variable for a specified depth. :Params: - **varname**: variable to process - **depth**: output depth(s) Other params, see: :func:`coloc_mod_on_pro` ''' self.verbose( 'Plotting layer of colocalized %s on %s\nvarname: %s\ndepth: %s\nselect: %s\nkwargs: %s', model.__class__.__name__, profiles.__class__.__name__, varname, depth, select, kwargs) lons_mod, lats_mod, deps_mod, var_mod = \ self.coloc_mod_on_pro( model, profiles, # If varname is a list of possible names, wrap it into a # tuple of one element as we are requiring only one variable len(numpy.shape(varname)) and (varname,) or varname, select, **kwargs) odep = create_dep(is_iterable(depth) and depth or [depth]) var_mod = var_mod.reorder('-z') var_mod = interp1d(var_mod, axo=odep, xmap=0, xmapper=deps_mod) # Required order: ...z var_mod = var_mod.reorder('z-') model.verbose('layer: %s', var_mod) lons_pro, lats_pro, var_pro = profiles.get_layer(varname, depth, select=select) profiles.verbose('layer: %s', var_pro) # ===== # Tracés vmin = var_pro.min() vmax = var_pro.max() if var_mod.count(): vmin = min(var_mod.min(), vmin) vmax = max(var_mod.max(), vmax) else: self.warning('No model data') if 'latitude' in select: lat_min, lat_max = select['latitude'][:2] else: la = model.get_latitude() lat_min, lat_max = min(la), max(la) if 'longitude' in select: lon_min, lon_max = select['longitude'][:2] else: lo = model.get_longitude() lon_min, lon_max = min(lo), max(lo) levels = auto_scale((vmin, vmax)) vmin = levels[0] vmax = levels[-1] # Création de la palette cmap = cmap_magic(levels) # On trace de champ du modèle moyené sur la période choisie m = map2(lon=(lon_min, lon_max), lat=(lat_min, lat_max), show=False) # On trace le modèle en fond # ===== msca = None try: msca = m.map.scatter(lons_mod, lats_mod, s=100, c=var_mod, vmin=vmin, vmax=vmax, cmap=cmap, label='model') except: self.exception('Failed plotting model data') # ===== # Triangulation du modèle # import matplotlib.tri as tri # triang = tri.Triangulation(lons_mod, lats_mod) # # On trace le modèle en fond # mod = m.axes.tripcolor(triang, var_mod, vmin=vmin, vmax=vmax, cmap=cmap) # ===== # On trace les observations avec des points plus petits psca = None try: psca = m.map.scatter(lons_pro, lats_pro, s=25, c=var_pro, vmin=m.vmin, vmax=m.vmax, cmap=m.cmap, label='profiles') except: self.exception('Failed plotting profile data') # Colorbar if msca is not None: colorbar(msca) elif psca is not None: colorbar(psca)
def plot_mld_mod_on_pro(self, model, profiles, select=None, deep=False, **kwargs): '''Plot mixed layer depth of model data correspponding to profiles position :Params: - **deep**: deep water computation mode if true Other params, see: :func:`coloc_mod_on_pro` ''' self.verbose( 'Plotting MLD of colocalized %s on %s\nselect: %s\ndeep: %s\nkwargs: %s', model.__class__.__name__, profiles.__class__.__name__, select, deep, kwargs) # ===== # Lecture des donnees de stratification lons_mod, lats_mod, deps_mod, temp_mod, sal_mod, pres_mod, dens_mod = \ self.coloc_strat_mod_on_pro(model, profiles, select, **kwargs) # ===== # Calcul mld modele # MLD en eaux peu profondes if not deep: ddeps = meshweights(deps_mod, axis=0) # Densité min/max dmin = dens_mod.min(axis=0) dmax = dens_mod.max(axis=0) # Densité moyenne dmean = numpy.ma.average(dens_mod, axis=0, weights=ddeps) # Profondeur max (max+demi épaisseur) H = deps_mod[-1] + ddeps[-1] * .5 # MLD mld_mod = H * (dmax - dmean) / (dmax - dmin) # MLD en eaux profondes else: # Profondeur de référence dep = -10 # Axe pour interpolation from vacumm.misc.axes import create_dep depaxis = create_dep([dep]) # Interpolations dens_mod_ref = dens_mod_ref.reorder('-z') dens_mod_ref = regrid1dold( dens_mod, axo=depaxis, xmap=0, xmapper=deps_mod) # Required order: ...z dens_mod_ref = dens_mod_ref.reorder('z-') # Valeur du différentiel de densité (cf. de Boyer Montégut et all, 2003) delta_dens = 0.03 # Densité cible dens_mod_target = dens_mod_ref + 0.03 # Masques de la couche mélangée et des eaux profondes dens_mod_target3d = MV2.resize(dens_mod_target, dens_mod.shape) dens_mod_good = (dens_mod.asma() <= dens_mod_target3d.asma()).filled(False) dens_mod_bad = (dens_mod.asma() > dens_mod_target3d.asma()).filled(False) # Profondeurs juste au dessus et en dessous de la MLD deps_mod_above = MV2.masked_where(dens_mod_bad, deps_mod).min(axis=0) deps_mod_below = MV2.masked_where(dens_mod_good, deps_mod).max(axis=0) # Masques associés from vacumm.misc import closeto mask_above = closeto(deps_mod, MV2.resize(deps_mod_above, deps_mod.shape)) mask_below = closeto(deps_mod, MV2.resize(deps_mod_below, deps_mod.shape)) # Densités juste au dessus et en dessous de la MLD dens_mod_above = MV2.masked_where(dens_mod, mask_above).max(axis=0) dens_mod_below = MV2.masked_where(dens_mod, mask_below).min(axis=0) # Interpolation dens_mod_delta = dens_mod_above - dens_mod_below deps_mod_mld = (dens_mod_target - dens_mod_above) * deps_mod_above deps_mod_mld += (dens_mod_below - dens_mod_target) * deps_mod_below deps_mod_mld = MV2.where(dens_mod_delta.mask, MV2.masked, deps_mod_mld / dens_mod_delta) mld_mod = deps_mod_mld # Finalize mld_mod = MV2.array(mld_mod) mld_mod.units = 'm' mld_mod.long_name = u'Profondeur de la couche de melange' mld_mod.setAxisList(lons_mod.getAxisList()) model.verbose('MLD: %s', mld_mod) # ===== # Calcul mld profiles mld_pro, lats_pro, lons_pro = profiles.get_mld(select) profiles.verbose('MLD: %s', mld_pro) # ===== # Tracés vmin = mld_pro.min() vmax = mld_pro.max() if mld_mod.count(): vmin = min(mld_mod.min(), vmin) vmax = max(mld_mod.max(), vmax) else: self.warning('No model data') if 'latitude' in select: lat_min, lat_max = select['latitude'][:2] else: la = model.get_latitude() lat_min, lat_max = min(la), max(la) if 'longitude' in select: lon_min, lon_max = select['longitude'][:2] else: lo = model.get_longitude() lon_min, lon_max = min(lo), max(lo) levels = auto_scale((vmin, vmax)) vmin = levels[0] vmax = levels[-1] # Création de la palette cmap = cmap_magic(levels) # On trace de champ du modèle moyené sur la période choisie m = map2(lon=(lon_min, lon_max), lat=(lat_min, lat_max), show=False) # On trace le modèle en fond # ===== msca = None try: msca = m.map.scatter(lons_mod, lats_mod, s=100, c=mld_mod, vmin=vmin, vmax=vmax, cmap=cmap, label='model') except: self.exception('Failed plotting model data') # ===== # Triangulation du modèle # import matplotlib.tri as tri # triang = tri.Triangulation(lons_mod, lats_mod) # # On trace le modèle en fond # mod = m.axes.tripcolor(triang, mld_mod, vmin=vmin, vmax=vmax, cmap=cmap) # ===== # On trace les observations avec des points plus petits psca = None try: psca = m.map.scatter(lons_pro, lats_pro, s=25, c=mld_pro, vmin=m.vmin, vmax=m.vmax, cmap=m.cmap, label='profiles') except: self.exception('Failed plotting profiles data') # Colorbar if msca is not None: colorbar(msca) elif psca is not None: colorbar(psca)
# -*- coding: utf-8 -*- # Lecture de la temperature import cdms2, MV2, numpy as N from vacumm.config import data_sample f = cdms2.open(data_sample('mars3d.xz.nc')) t = f('temp', lon=(-4.9, -4.43)) h0 = f('h0', lon=(-4.9, -4.43)).filled(0.) f.close() t.long_name = 'Original' # Creation d'un axe de profondeur from vacumm.misc.axes import create_dep ddep = 5. dep = create_dep((-h0.max(), 0. + ddep, ddep)) # Creation de l'axe etendu (taille (ndep,nx)) depths = -N.outer(t.getAxis(0)[::-1], h0) dep[-1] = depths[-1].max() # Regrillage lineaire from vacumm.misc.grid.regridding import regrid1d tr = regrid1d(t, dep, 'linear', axi=depths, axis=0) tr.long_name = 'Interpolated' t.getAxis(0).designateLevel() # Plot from vacumm.misc.plot import savefigs, yscale, add_grid, section2 import pylab as P P.figure(figsize=(5.5, 6)) kwplot = dict(show=False, bgcolor='.5', ylim=(-80, 0)) # - original
# Longitudes toute les 1 minute loi = np.arange(lo[0],lo[-1],1./60.) # Latitudes toute les 1 minute lai = np.arange(la[0],la[-1],1./60.) xo = cdms2.createAxis(loi) yo = cdms2.createAxis(lai) LONGITUDE = create_lon(loi, id='longitude', attributes=dict(long_name='Longitude of each location',standard_name='longitude',units='degrees_east',valid_min='-180.',valid_max='180.',axis='X')) LATITUDE = create_lat(lai, id='latitude', attributes=dict(long_name='Latitude of each location',standard_name='latitude',units='degrees_north',valid_min='-90.',valid_max='90.',axis='Y')) z = U.getLevel() DEPTH = create_dep(z,attributes=dict(long_name='sea water depth',standard_name='depth',units='m',valid_min='0.',valid_max='12000.')) #axes = [TIME,DEPTH,LATITUDE,LONGITUDE] axes = [DEPTH,LATITUDE,LONGITUDE] Uis=np.arange(U.getLevel().__len__()*yo.__len__()*xo.__len__()).reshape(U.getLevel().__len__(),yo.__len__(),xo.__len__()) Vis=np.arange(U.getLevel().__len__()*yo.__len__()*xo.__len__()).reshape(U.getLevel().__len__(),yo.__len__(),xo.__len__()) Uis = cdms2.createVariable(Uis, typecode='f',id='UZ', axes=axes, attributes=dict(long_name='3d zonal velocity',standard_name='eastward_sea_water_velocity',units='m.s-1',valid_min='-100.',valid_max='100.')) Vis = cdms2.createVariable(Vis, typecode='f',id='VZ', axes=axes, attributes=dict(long_name='3d meridional velocity',standard_name='northward_sea_water_velocity',units='m.s-1',valid_min='-100.',valid_max='100.')) for iz, dep in enumerate(U.getLevel()): Ui2 = interp2d(U[0,iz,:,:], (xo,yo), method='bilinear')
# -*- coding: utf-8 -*- # Lecture de la temperature import cdms2, MV2, numpy as N from vacumm.config import data_sample f =cdms2.open(data_sample('mars3d.xz.nc')) t = f('temp', lon=(-4.9, -4.43)) h0 = f('h0', lon=(-4.9, -4.43)).filled(0.) f.close() t.long_name = 'Original' # Creation d'un axe de profondeur from vacumm.misc.axes import create_dep ddep = 5. dep = create_dep((-h0.max(),0.+ddep , ddep)) # Creation de l'axe etendu (taille (ndep,nx)) depths = -N.outer(t.getAxis(0)[::-1], h0) dep[-1] = depths[-1].max() # Regrillage lineaire from vacumm.misc.grid.regridding import regrid1d tr = regrid1d(t, dep, 'linear', axi=depths, axis=0) tr.long_name = 'Interpolated' t.getAxis(0).designateLevel() # Plot from vacumm.misc.plot import savefigs,yscale, add_grid, section2 import pylab as P P.figure(figsize=(5.5, 6)) kwplot = dict(show=False, bgcolor='.5', ylim=(-80, 0)) # - original
def plot_mld_mod_on_pro(self, model, profiles, select=None, deep=False, **kwargs): '''Plot mixed layer depth of model data correspponding to profiles position :Params: - **deep**: deep water computation mode if true Other params, see: :func:`coloc_mod_on_pro` ''' self.verbose('Plotting MLD of colocalized %s on %s\nselect: %s\ndeep: %s\nkwargs: %s', model.__class__.__name__, profiles.__class__.__name__, select, deep, kwargs) # ===== # Lecture des donnees de stratification lons_mod, lats_mod, deps_mod, temp_mod, sal_mod, pres_mod, dens_mod = \ self.coloc_strat_mod_on_pro(model, profiles, select, **kwargs) # ===== # Calcul mld modele # MLD en eaux peu profondes if not deep: ddeps = meshweights(deps_mod, axis=0) # Densité min/max dmin = dens_mod.min(axis=0) dmax = dens_mod.max(axis=0) # Densité moyenne dmean = numpy.ma.average(dens_mod, axis=0, weights=ddeps) # Profondeur max (max+demi épaisseur) H = deps_mod[-1]+ddeps[-1]*.5 # MLD mld_mod = H*(dmax-dmean)/(dmax-dmin) # MLD en eaux profondes else: # Profondeur de référence dep = -10 # Axe pour interpolation from vacumm.misc.axes import create_dep depaxis = create_dep([dep]) # Interpolations dens_mod_ref = dens_mod_ref.reorder('-z') dens_mod_ref = regrid1dold(dens_mod, axo=depaxis, xmap=0, xmapper=deps_mod) # Required order: ...z dens_mod_ref = dens_mod_ref.reorder('z-') # Valeur du différentiel de densité (cf. de Boyer Montégut et all, 2003) delta_dens = 0.03 # Densité cible dens_mod_target = dens_mod_ref+0.03 # Masques de la couche mélangée et des eaux profondes dens_mod_target3d = MV2.resize(dens_mod_target, dens_mod.shape) dens_mod_good = (dens_mod.asma() <= dens_mod_target3d.asma()).filled(False) dens_mod_bad = (dens_mod.asma() > dens_mod_target3d.asma()).filled(False) # Profondeurs juste au dessus et en dessous de la MLD deps_mod_above = MV2.masked_where(dens_mod_bad, deps_mod).min(axis=0) deps_mod_below = MV2.masked_where(dens_mod_good, deps_mod).max(axis=0) # Masques associés from vacumm.misc import closeto mask_above = closeto(deps_mod, MV2.resize(deps_mod_above, deps_mod.shape)) mask_below = closeto(deps_mod, MV2.resize(deps_mod_below, deps_mod.shape)) # Densités juste au dessus et en dessous de la MLD dens_mod_above = MV2.masked_where(dens_mod, mask_above).max(axis=0) dens_mod_below = MV2.masked_where(dens_mod, mask_below).min(axis=0) # Interpolation dens_mod_delta = dens_mod_above-dens_mod_below deps_mod_mld = (dens_mod_target-dens_mod_above)*deps_mod_above deps_mod_mld += (dens_mod_below-dens_mod_target)*deps_mod_below deps_mod_mld = MV2.where(dens_mod_delta.mask, MV2.masked, deps_mod_mld/dens_mod_delta) mld_mod = deps_mod_mld # Finalize mld_mod = MV2.array(mld_mod) mld_mod.units = 'm' mld_mod.long_name = u'Profondeur de la couche de melange' mld_mod.setAxisList(lons_mod.getAxisList()) model.verbose('MLD: %s', mld_mod) # ===== # Calcul mld profiles mld_pro, lats_pro, lons_pro = profiles.get_mld(select) profiles.verbose('MLD: %s', mld_pro) # ===== # Tracés vmin = mld_pro.min() vmax = mld_pro.max() if mld_mod.count(): vmin = min(mld_mod.min(), vmin) vmax = max(mld_mod.max(), vmax) else: self.warning('No model data') if 'latitude' in select: lat_min, lat_max = select['latitude'][:2] else: la = model.get_latitude() lat_min, lat_max = min(la), max(la) if 'longitude' in select: lon_min, lon_max = select['longitude'][:2] else: lo = model.get_longitude() lon_min, lon_max = min(lo), max(lo) levels = auto_scale((vmin, vmax)) vmin = levels[0] vmax = levels[-1] # Création de la palette cmap = cmap_magic(levels) # On trace de champ du modèle moyené sur la période choisie m = map2(lon=(lon_min, lon_max), lat=(lat_min, lat_max), show=False) # On trace le modèle en fond # ===== msca = None try: msca = m.map.scatter(lons_mod, lats_mod, s=100, c=mld_mod, vmin=vmin, vmax=vmax, cmap=cmap, label='model') except: self.exception('Failed plotting model data') # ===== # Triangulation du modèle # import matplotlib.tri as tri # triang = tri.Triangulation(lons_mod, lats_mod) # # On trace le modèle en fond # mod = m.axes.tripcolor(triang, mld_mod, vmin=vmin, vmax=vmax, cmap=cmap) # ===== # On trace les observations avec des points plus petits psca = None try: psca = m.map.scatter(lons_pro, lats_pro, s=25, c=mld_pro, vmin=m.vmin, vmax=m.vmax, cmap=m.cmap, label='profiles') except: self.exception('Failed plotting profiles data') # Colorbar if msca is not None: colorbar(msca) elif psca is not None: colorbar(psca)
def plot_layer_mod_on_pro(self, model, profiles, varname, depth, select=None, **kwargs): '''Get a layer of variable for a specified depth. :Params: - **varname**: variable to process - **depth**: output depth(s) Other params, see: :func:`coloc_mod_on_pro` ''' self.verbose('Plotting layer of colocalized %s on %s\nvarname: %s\ndepth: %s\nselect: %s\nkwargs: %s', model.__class__.__name__, profiles.__class__.__name__, varname, depth, select, kwargs) lons_mod, lats_mod, deps_mod, var_mod = \ self.coloc_mod_on_pro( model, profiles, # If varname is a list of possible names, wrap it into a # tuple of one element as we are requiring only one variable len(numpy.shape(varname)) and (varname,) or varname, select, **kwargs) odep = create_dep(is_iterable(depth) and depth or [depth]) var_mod = var_mod.reorder('-z') var_mod = interp1d(var_mod, axo=odep, xmap=0, xmapper=deps_mod) # Required order: ...z var_mod = var_mod.reorder('z-') model.verbose('layer: %s', var_mod) lons_pro, lats_pro, var_pro = profiles.get_layer(varname, depth, select=select) profiles.verbose('layer: %s', var_pro) # ===== # Tracés vmin = var_pro.min() vmax = var_pro.max() if var_mod.count(): vmin = min(var_mod.min(), vmin) vmax = max(var_mod.max(), vmax) else: self.warning('No model data') if 'latitude' in select: lat_min, lat_max = select['latitude'][:2] else: la = model.get_latitude() lat_min, lat_max = min(la), max(la) if 'longitude' in select: lon_min, lon_max = select['longitude'][:2] else: lo = model.get_longitude() lon_min, lon_max = min(lo), max(lo) levels = auto_scale((vmin, vmax)) vmin = levels[0] vmax = levels[-1] # Création de la palette cmap = cmap_magic(levels) # On trace de champ du modèle moyené sur la période choisie m = map2(lon=(lon_min, lon_max), lat=(lat_min, lat_max), show=False) # On trace le modèle en fond # ===== msca = None try: msca = m.map.scatter(lons_mod, lats_mod, s=100, c=var_mod, vmin=vmin, vmax=vmax, cmap=cmap, label='model') except: self.exception('Failed plotting model data') # ===== # Triangulation du modèle # import matplotlib.tri as tri # triang = tri.Triangulation(lons_mod, lats_mod) # # On trace le modèle en fond # mod = m.axes.tripcolor(triang, var_mod, vmin=vmin, vmax=vmax, cmap=cmap) # ===== # On trace les observations avec des points plus petits psca = None try: psca = m.map.scatter(lons_pro, lats_pro, s=25, c=var_pro, vmin=m.vmin, vmax=m.vmax, cmap=m.cmap, label='profiles') except: self.exception('Failed plotting profile data') # Colorbar if msca is not None: colorbar(msca) elif psca is not None: colorbar(psca)