info/readnetcdf¶
Get variables from or print information of 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-2022 Matthias Cuntz, Stephan Thober, see AUTHORS.rst for details.
- license:
MIT License, see LICENSE for details.
The following functions are provided
|
Wrapper for |
|
Get information on variables in a netcdf file |
|
Wrapper for |
|
Gets variables of a netcdf file |
- History
Written Jul 2009 by Matthias Cuntz (mc (at) macu (dot) de)
Removed quiet keyword, Jun 2012, Matthias Cuntz
Ported to Python 3, Feb 2013, Matthias Cuntz
Wrapper functions netcdfread, ncread, readnc, Oct 2013, Matthias Cuntz
overwrite keyword, Apr 2014, Stephan Thober
dims keyword, May 2014, Stephan Thober
attributes keyword, Jun 2016, Stephan Thober
Restrict overwrite to files with only one variables, Aug 2016, Stephan Thober
Do not count dimension variables in variable count for overwrite, Oct 2016, Matthias Cuntz
Remove wrappers netcdfread and readnc, Mar 2022, Matthias Cuntz
Remove reform keyword, Mar 2022, Matthias Cuntz
Put all info abilities into separate routine infonetcdf with wrapper function ncinfo, Mar 2022, Matthias Cuntz
Invert functions and wrapper functions, Feb 2023, Matthias Cuntz
- ncinfo(ncfile, var='', code=-1, dims=False, shape=False, attributes=False, variables=False, codes=False, long_names=False, units=False, sort=False)[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.
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
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 ‘’.
sort (bool, optional) – 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_readnetcdf.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)) [-1, -1, 128, 129] >>> print(ncinfo(ncfile, codes=True, sort=True)) [128, 129, -1, -1]
Get special attributes units and long_names
>>> print([ str(i) for i in ncinfo(ncfile, units=True) ]) ['xx', 'yy', 'arbitrary', 'arbitrary'] >>> print([ str(i) for i in ncinfo(ncfile, units=True, sort=True) ]) ['arbitrary', 'arbitrary', 'xx', 'yy'] >>> print([ str(i) for i in ncinfo(ncfile, long_names=True) ]) ['x-axis', 'y-axis', 'all ones', 'all twos'] >>> print([ str(i) ... for i in ncinfo(ncfile, long_names=True, sort=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, var='is1', shape=True)) (2, 4)
Get attributes
>>> t1 = ncinfo(ncfile, var='is1', attributes=True) >>> print([ str(i) for i in sorted(t1) ]) ['code', 'long_name', 'units']
- ncread(ncfile, var='', code=-1, squeeze=False, pointer=False, overwrite=False)[source]¶
Gets variables of a netcdf file
- Parameters:
ncfile (str) – netCDF file name
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.
squeeze (bool, optional) – Squeeze output array, i.e. remove dimensions of size 1.
pointer (bool, optional) – Return pointers to the open file and to the variable if True, i.e. without actually reading the variable. The file will be open in read-only ‘r’ mode. overwrite precedes over pointer.
overwrite (bool, optional) – Return pointers to the open file and to the variable if True, where the file is opened in append ‘a’ mode allowing to modify the variable.
ncread
allows overwrite only if the file contains a single variable (without the dimension variables). overwrite precedes over pointer.
- Returns:
numpy array of the variable with name *var or number code, or*
(file pointer, variable pointer)
Examples
Read variable or code
>>> ncfile = 'test_readnetcdf.nc' >>> print(ncread(ncfile, var='is1')) [[1. 1. 1. 1.] [1. 1. 1. 1.]] >>> print(ncread(ncfile, code=129)) [[2. 2. 2. 2.] [2. 2. 2. 2.]]
Just get file handle so that read is done later at indexing useful for example to inquire remote netcdf files first
>>> fh, var = ncread(ncfile, var='is1', pointer=True) >>> print(var.shape) (2, 4) >>> print(var[:]) [[1. 1. 1. 1.] [1. 1. 1. 1.]] >>> fh.close()
Change a variable in a file
>>> ncfile = 'test_readnetcdf1.nc' >>> print(ncread(ncfile, var='is1')) [[1. 1. 1. 1.] [1. 1. 1. 1.]] >>> fh, var = ncread(ncfile, var='is1', overwrite=True) >>> var[:] *= 2. >>> fh.close() >>> print(ncread(ncfile, var='is1')) [[2. 2. 2. 2.] [2. 2. 2. 2.]] >>> fh, var = ncread(ncfile, var='is1', overwrite=True) >>> var[:] *= 0.5 >>> fh.close()