def __init__(self, msl=None, names=None, **kwargs): if msl is None: msl = get_config_value(__name__, 'meansealevel_file') # msl = _get_option_('meansealevel_file') if msl is None: raise MeanSeaLevel("Can't find a default file of mean sea levels") if isinstance(msl, str): # file if not os.path.isabs(msl): msl = os.path.abspath(os.path.join(dirname, '../../../../data', msl)) if not os.path.exists(msl): raise MeanSeaLevel("File of mean sea levels not found: %s"%msl) data = [] names = [] xx = [] ; yy = [] ; zz = [] f = open(msl) for line in f: name = unicode(line[:29].strip(), 'utf8') y, x, z = [float(v) for v in line[29:].split()] names.append(name) xx.append(x) yy.append(y) zz.append(z) f.close() msl = (xx, yy, zz) elif isinstance(msl, MeanSeaLevel): # instance names = list(msl.names()) kwargs.setdefault('units', 'm') kwargs.setdefault('long_name', 'Mean sea level') XYZBathy.__init__(self, msl, **kwargs) self._names = names
def __init__(self, nom=None, regexp=True, verbose=True, file=None, *args, **kwargs): self.nom = None # File name if file is None: # file = _get_option_('ports_file') file = get_config_value(__name__, 'ports_file', ispath=True) if file is not None: # if not os.path.isabs(file): # file = os.path.abspath(os.path.join(dirname, '../../../../data', file)) if not os.path.exists(file): raise StationInfoError('File of ports not found: ' + file) else: raise StationInfoError('No valid file of ports found') # Chargement du fichier self.set_file(file) # Send optional argument to search() if nom is not None or len(kwargs) or len(args): self.find(nom=nom, regexp=regexp, verbose=verbose, *args, **kwargs)
def bathy_list(name=None, cfgfile=None, minstatus=0): """Get the list of bathymetries as dict :Params: - **name**, optional: Request this bathymetry. - **cfgfile**, optional: Load this bathymetry config file. - **minstatus**, optional: Minimal status of availability of the bathymetry (see :func:`vacumm.config.check_data_file`): - ``0``: Bathy file not on disk and not downloadable (however, user can specify the path manually). - ``1``: File is not on disk, but downloadable. - ``2``: File is on disk. :Return: - If ``name`` is not provided, a dict of subdicts like this one:: {'etopo2':{'file':'/home/me/bathy.nc', 'var':'btdata', ...}, ...} - Else, the subdict of the requested bathy:: {'file':'/home/me/bathy.nc', 'var':'btdata', ...} """ # Read config file sections, cfg = get_config_sections(cfg=cfgfile, parent_section=__name__, parent_option='cfgfile_gridded', getcfg=True) # if not cfgfile: cfgfile = get_cfgfiles_gridded() # File name # dvars = get_dir_dict(__names__) # Various directory names # bathys = {} # f = ConfigParser() # f.read(cfgfile) # Loop on section bathies = {} for section in sections: status = _check_bathy_file_(section, avail=True) # if not os.path.exists(f.get(section, 'file', vars=dvars)): continue if status <= minstatus: continue bathies[section] = {'status': status} for key in 'file', 'file_url', 'file_license', 'var', 'lon', 'lat': if cfg.has_option(section, key): bathies[section][key] = get_config_value(section, key, cfg=cfg) # Found any bathy? if name is not None: if not len(bathies): raise NcGriddedBathyError('No bathy are available') if not name in bathies: raise NcGriddedBathyError( 'Wrong bathymetry name: "%s". Please choose one of these: %s.' % (name, ', '.join(bathies.keys()))) return bathies[name] return bathies
def __init__(self, msl=None, names=None, **kwargs): if msl is None: msl = get_config_value(__name__, 'meansealevel_file') # msl = _get_option_('meansealevel_file') if msl is None: raise MeanSeaLevel("Can't find a default file of mean sea levels") if isinstance(msl, str): # file if not os.path.isabs(msl): msl = os.path.abspath(os.path.join(dirname, '../../../../data', msl)) if not os.path.exists(msl): raise MeanSeaLevel("File of mean sea levels not found: %s"%msl) data = [] names = [] xx = [] ; yy = [] ; zz = [] f = open(msl) for line in f: name = str(line[:29].strip(), 'utf8') y, x, z = [float(v) for v in line[29:].split()] names.append(name) xx.append(x) yy.append(y) zz.append(z) f.close() msl = (xx, yy, zz) elif isinstance(msl, MeanSeaLevel): # instance names = list(msl.names()) kwargs.setdefault('units', 'm') kwargs.setdefault('long_name', 'Mean sea level') XYZBathy.__init__(self, msl, **kwargs) self._names = names
def clean_cache(mapdir=None, maxsize=None): """Clean cache directory by checking its size :Params: - **mapdir**, optional: Directory where maps are cached - **maxsize**, optional: Maximal size of directory in bytes. Default value from :confopt:`[vacumm.misc.grid.basemap]max_cache_size` configuration value. """ from ...misc.misc import dirsize mapdir = get_map_dir(mapdir) if mapdir is None: mapdir = os.path.join(get_configdir(), 'basemap', 'cached_maps') cache_size = dirsize(mapdir) if maxsize is None: maxsize = eval(get_config_value('vacumm.misc.grid.basemap', 'max_cache_size')) if cache_size>maxsize: files = [os.path.join(mapdir, ff) for ff in os.listdir(mapdir)] files.sort(cmp=lambda f1, f2: cmp(os.stat(f1)[8], os.stat(f2)[8])) for ff in files: cache_size -= os.path.getsize(ff) try: os.remove(ff) except: vacumm_warn('Error while trying to clean basemap cache in: '+ os.path.dirname(ff)) return if cache_size<=maxsize: break
def get_proj(gg=None, proj=None, **kwargs): """Setup a default projection using x,y coordinates and :class:`~mpl_toolkits.basemap.proj.Proj` or :func:`basic_proj` Projection is set by default to "basic". :Params: - **gg**, optional: Grid or coordinates (see :func:`~vacumm.misc.grid.misc.get_xy`). If not provided, lon bounds are set to (-180,180) and lat bounds to (-89.99,89.99). - Other keywords are passed to :class:`~mpl_toolkits.basemap.proj.Proj`. One of them is the projection type, which defaults to configuration option :confopt:`[vacumm.misc.grid.basemap] proj`. :Return: A :class:`mpl_toolkits.basemap.proj.Proj` instance or :func:`basic_proj` :Examples: >>> proj = get_proj(sst.getGrid(), proj='laea') >>> x, y = proj(lon, lat) >>> proj = get_proj((lon, lat)) >>> xx, yy = N.meshgrid(lon, lat) >>> xx, yy = proj(xx, yy) >>> print proj(xx, yy, inverse=True) >>> proj = get_proj(R=6000000.) """ if proj is False: return False if proj is None or proj is True: proj = 'basic' if proj == 'basic': return basic_proj if callable(proj): return proj if gg is not None: x,y = get_xy(gg, num=True) xmin, ymin, xmax, ymax = x.min(), y.min(), x.max(), y.max() else: xmin, ymin, xmax, ymax = -180, -90, 180, 90 y = [0] projparams = kwargs.copy() ymax = min(ymax, 89.99) ymin = max(ymin, -89.99) if not isinstance(proj, str): proj = get_config_value('vacumm.misc.grid.basemap', 'proj') dict_check_defaults(projparams, R=rsphere_mean, units='m', proj=proj, lat_ts = N.median(y) if len(y)>10 else N.mean(y), lon_0 = N.median(x) if len(x)>10 else N.mean(x)) dict_check_defaults(projparams, lat_0=projparams['lat_ts']) return Proj(projparams, xmin, ymin, xmax, ymax)
def _shorelines_list_(name=None): # Get list a dict shorelines = config.get_config_value(__name__, 'shapefile_.*', regexp=True, ispath=True) # Skip bad keys for key in shorelines.keys(): if key.endswith('_url') or key.endswith('_license'): del shorelines[key] # Check requested name if name is not None: if not shorelines.has_key('shapefile_'+name): raise VACUMMShorelineError, 'Shoreline not available: %s. Please use list_shorelines() to print the list of available shorelines.'%name return shorelines['shapefile_'+name] return shorelines
def get_proj(gg=None, proj=None, **kwargs): """Setup a default projection using x,y coordinates and :class:`~mpl_toolkits.basemap.proj.Proj` Projection is set by default to "laea" and cover the coordinates. :Params: - **gg**, optional: Grid or coordinates (see :func:`~vacumm.misc.grid.misc.get_xy`). If not provided, lon bounds are set to (-180,180) and lat bounds to (-89.99,89.99). - Other keywords are passed to :class:`~mpl_toolkits.basemap.proj.Proj`. One of them is the projection type, which defaults to configuration option :confopt:`[vacumm.misc.grid.basemap] proj`. :Return: A :class:`mpl_toolkits.basemap.proj.Proj` instance. :Examples: >>> proj = get_proj(sst.getGrid(), proj='laea') >>> x, y = proj(lon, lat) >>> proj = get_proj((lon, lat)) >>> xx, yy = N.meshgrid(lon, lat) >>> xx, yy = proj(xx, yy) >>> print proj(xx, yy, inverse=True) >>> proj = get_proj(R=6000000.) """ if callable(proj): return proj if gg is not None: x,y = get_xy(gg, num=True) xmin, ymin, xmax, ymax = x.min(), y.min(), x.max(), y.max() else: xmin, ymin, xmax, ymax = -180, -90, 180, 90 y = [0] projparams = kwargs.copy() ymax = min(ymax, 89.99) ymin = max(ymin, -89.99) if not isinstance(proj, str): proj = get_config_value('vacumm.misc.grid.basemap', 'proj') dict_check_defaults(projparams, R=rsphere_mean, units='m', proj=proj, lat_ts = N.median(y) if len(y)>10 else N.mean(y), lon_0 = N.median(x) if len(x)>10 else N.mean(x)) dict_check_defaults(projparams, lat_0=projparams['lat_ts']) return Proj(projparams, xmin, ymin, xmax, ymax)
def __init__(self, cfgfile=None): # Guess file name if cfgfile is None: f = ConfigParser() f.read(get_config_value(__name__, 'cfgfile_xyz')) if f.has_option('banks', 'main'): cfgfile = f.get('banks', 'main') #else: # raise Exception, 'Cannot read config file to guess bank file name' # Read file self.cfg = SafeConfigParser() self.cfgfile = cfgfile if cfgfile and os.path.exists(cfgfile): self.cfg.read(cfgfile) self._clients = {} for id in self.cfg.sections(): self._clients[id] = XYZBathyBankClient(self, id) self._order = None
def __init__(self,nom=None, regexp=True, verbose=True, file=None, *args, **kwargs): self.nom = None # File name if file is None: # file = _get_option_('ports_file') file = get_config_value(__name__, 'ports_file', ispath=True) if file is not None: # if not os.path.isabs(file): # file = os.path.abspath(os.path.join(dirname, '../../../../data', file)) if not os.path.exists(file): raise StationInfoError('File of ports not found: '+file) else: raise StationInfoError('No valid file of ports found') # Chargement du fichier self.set_file(file) # Send optional argument to search() if nom is not None or len(kwargs) or len(args): self.find(nom=nom,regexp=regexp,verbose=verbose,*args,**kwargs)
def clean_cache(mapdir=None, maxsize=None): """Clean cache directory by checking its size :Params: - **mapdir**, optional: Directory where maps are cached - **maxsize**, optional: Maximal size of directory in bytes. Default value from :confopt:`[vacumm.misc.grid.basemap]max_cache_size` configuration value. """ from ...misc.misc import dirsize mapdir = get_map_dir(mapdir) if mapdir is None: mapdir = os.path.join(get_configdir(), 'basemap', 'cached_maps') cache_size = dirsize(mapdir) if maxsize is None: maxsize = eval(get_config_value('vacumm.misc.grid.basemap', 'max_cache_size')) if cache_size>maxsize: files = [os.path.join(mapdir, ff) for ff in os.listdir(mapdir)] files.sort(cmp=lambda f1, f2: cmp(os.stat(f1)[8], os.stat(f2)[8])) for ff in files: cache_size -= os.path.getsize(ff) os.remove(ff) if cache_size<=maxsize: break