Read data from a NetCDF file into a RasterModelGrid.
read_netcdf (nc_file[, just_grid]) |
Create a RasterModelGrid from a netcdf file. |
read_netcdf
(nc_file, just_grid=False)[source]¶Create a RasterModelGrid
from a netcdf file.
Create a new RasterModelGrid
from the netcdf file, nc_file.
If the netcdf file also contains data, add that data to the grid’s fields.
To create a new grid without any associated data from the netcdf file,
set the just_grid keyword to True
.
Parameters: | nc_file : str
just_grid : boolean, optional
|
---|---|
Returns: |
|
Examples
Import read_netcdf()
and the path to an example netcdf file included
with landlab.
>>> from landlab.io.netcdf import read_netcdf
>>> from landlab.io.netcdf import NETCDF4_EXAMPLE_FILE
Create a new grid from the netcdf file. The example grid is a uniform rectilinear grid with 4 rows and 3 columns of nodes with unit spacing. The data file also contains data defined at the nodes for the grid for a variable called, surface__elevation.
>>> grid = read_netcdf(NETCDF4_EXAMPLE_FILE)
>>> grid.shape == (4, 3)
True
>>> grid.dy, grid.dx
(1.0, 1.0)
>>> [str(k) for k in grid.at_node.keys()]
['surface__elevation']
>>> grid.at_node['surface__elevation']
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.,
11.])
read_netcdf()
will try to determine the format of the netcdf file.
For example, the same call will also work for netcdf3-formatted files.
>>> from landlab.io.netcdf import NETCDF3_64BIT_EXAMPLE_FILE
>>> grid = read_netcdf(NETCDF3_64BIT_EXAMPLE_FILE)
>>> grid.shape == (4, 3)
True
>>> grid.dy, grid.dx
(1.0, 1.0)
Write structured grids to NetCDF files.
write_netcdf (path, fields[, attrs, append, ...]) |
Write landlab fields to netcdf. |
write_netcdf
(path, fields, attrs=None, append=False, format='NETCDF3_64BIT', names=None, at=None)[source]¶Write landlab fields to netcdf.
Write the data and grid information for fields to path as NetCDF. If the append keyword argument in True, append the data to an existing file, if it exists. Otherwise, clobber an existing files.
Parameters: | path : str
fields : field-like
append : boolean, optional
format : {‘NETCDF3_CLASSIC’, ‘NETCDF3_64BIT’, ‘NETCDF4_CLASSIC’, ‘NETCDF4’}
attrs : dict
names : iterable of str, optional
at : {‘node’, ‘cell’}, optional
|
---|
Examples
>>> import numpy as np
>>> from landlab import RasterModelGrid
>>> from landlab.io.netcdf import write_netcdf
Create a uniform rectilinear grid with four rows and 3 columns, and add some data fields to it.
>>> rmg = RasterModelGrid(4, 3)
>>> _ = rmg.add_field('node', 'topographic__elevation', np.arange(12.))
>>> _ = rmg.add_field('node', 'uplift_rate', 2. * np.arange(12.))
Create a temporary directory to write the netcdf file into.
>>> import tempfile, os
>>> temp_dir = tempfile.mkdtemp()
>>> os.chdir(temp_dir)
Write the grid to a netcdf3 file but only include the uplift_rate data in the file.
>>> write_netcdf('test.nc', rmg, format='NETCDF3_64BIT',
... names='uplift_rate')
Read the file back in and check its contents.
>>> from scipy.io import netcdf
>>> fp = netcdf.netcdf_file('test.nc', 'r')
>>> 'uplift_rate' in fp.variables
True
>>> 'topographic__elevation' in fp.variables
False
>>> fp.variables['uplift_rate'][:].flatten()
array([ 0., 2., 4., 6., 8., 10., 12., 14., 16., 18., 20.,
22.])
>>> _ = rmg.add_field('cell', 'air__temperature', np.arange(2.))
>>> write_netcdf('test-cell.nc', rmg, format='NETCDF3_64BIT',
... names='air__temperature', at='cell')
Exceptions to raise for the netcdf module.
NotRasterGridError
[source]¶Bases: landlab.io.netcdf.errors.Error
Raise if grid is not uniform rectilinear.
Raise this error if the grid defined in the netcdf file is not uniform rectilinear with constant spacing in all dimensions.