netcdf info#
Information on a netcdf file
This module was written by Matthias Cuntz while at Department of Computational Hydrosystems, Helmholtz Centre for Environmental Research - UFZ, Leipzig, Germany, and continued while at Institut National de Recherche pour l’Agriculture, l’Alimentation et l’Environnement (INRAE), Nancy, France.
- copyright:
Copyright 2009-2023 Matthias Cuntz, Stephan Thober, see AUTHORS.rst for details.
- license:
MIT License, see LICENSE for details.
The following functions are provided
|
Get information on variables in a netcdf file |
|
Wrapper for |
- History
Written Mar 2022 from ncread by moving all info abilities into separate routine infonetcdf with wrapper function ncinfo, Mar 2022, Matthias Cuntz
Invert functions and wrapper functions, Feb 2023, Matthias Cuntz
Move ncinfo in separate file, Nov 2023, Matthias Cuntz
sort=True default, Nov 2023, Matthias Cuntz
Enforce keywords after var, Nov 2023, Matthias Cuntz
- ncinfo(ncfile, var='', *, code=-1, variables=False, codes=False, long_names=False, units=False, dims=False, shape=False, attributes=False, sort=True)[source]#
Get information on variables in a netcdf file
- Parameters:
ncfile (str) – netCDF file name
var (str, optional) – Variable name, only relevant if dims or attributes are True var takes precedence over code.
code (int, optional) – Variable code such as in files coming from GRIB, only relevant if dims or attributes are True. var takes precedence over code.
variables (bool, optional) – Get list of variables in ncfile
codes (bool, optional) – Get list of variable attributes code Missing codes will be filled with -1.
long_names (bool, optional) – Get list of variable attributes long_name Missing long_names will be filled with ‘’.
units (bool, optional) – Get list of variable attributes units. Missing units will be filled with ‘’.
dims (bool, optional) – Get tuple of dimension names for the variable with name var or number code.
shape (bool, optional) – Get shape of the variable with name var or number code.
attributes (bool, optional) – Get dictionary of all attributes of variable with name var or number code, or all file attributes of ncfile if var and code are not given
sort (bool, optional) – If True (default), sort output of variables, codes, units, and long_names with variable name as the sort key
- Returns:
tuple of variable dimension names,
tuple of variable dimensions,
list of variable names, codes, units, long_names, or
dictionary of attributes
Examples
Get variable names
>>> ncfile = 'test_ncread.nc' >>> print([ str(i) for i in ncinfo(ncfile, variables=True) ]) ['x', 'y', 'is1', 'is2'] >>> print([ str(i) ... for i in ncinfo(ncfile, variables=True, sort=True) ]) ['is1', 'is2', 'x', 'y']
Get codes
>>> print(ncinfo(ncfile, codes=True)) [128, 129, -1, -1] >>> print(ncinfo(ncfile, codes=True, sort=True)) [128, 129, -1, -1] >>> print(ncinfo(ncfile, codes=True, sort=False)) [-1, -1, 128, 129]
Get special attributes units and long_names
>>> print([ str(i) for i in ncinfo(ncfile, units=True) ]) ['arbitrary', 'arbitrary', 'xx', 'yy'] >>> print([ str(i) for i in ncinfo(ncfile, units=True, sort=False) ]) ['xx', 'yy', 'arbitrary', 'arbitrary'] >>> print([ str(i) for i in ncinfo(ncfile, long_names=True) ]) ['all ones', 'all twos', 'x-axis', 'y-axis']
Get dims
>>> print([ str(i) for i in ncinfo(ncfile, var='is1', dims=True) ]) ['y', 'x']
Get shape
>>> print(ncinfo(ncfile, 'is1', shape=True)) (2, 4)
Get attributes
>>> t1 = ncinfo(ncfile, 'is1', attributes=True) >>> print([ str(i) for i in sorted(t1) ]) ['code', 'long_name', 'units']