landlab.io.netcdf.read¶
Read data from a NetCDF file into a RasterModelGrid.
Read netcdf¶
|
Create a |
- read_netcdf(nc_file, grid=None, name=None, just_grid=False, halo=0, nodata_value=-9999.0)[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 toTrue
.A halo can be added with the keyword halo.
If you want the fields to be added to an existing grid, it can be passed to the keyword argument grid.
- Parameters:
nc_file (str) – Name of a netcdf file.
grid (grid , optional) – Adds data to an existing grid instead of creating a new one.
name (str, optional) – Add only fields with NetCDF variable name to the grid. Default is to add all NetCDF varibles to the grid.
just_grid (boolean, optional) – Create a new grid but don’t add value data.
halo (integer, optional) – Adds outer border of depth halo to the grid.
nodata_value (float, optional) – Value that indicates an invalid value. Default is -9999.
- Returns:
A newly-created
RasterModelGrid
.- Return type:
Examples
Import
read_netcdf
and the path to an example netcdf file included with landlab.>>> from landlab.io.netcdf import read_netcdf
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("test-netcdf4.nc") >>> grid.shape == (4, 3) True >>> grid.dy, grid.dx (1.0, 1.0) >>> list(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.>>> grid = read_netcdf("test-netcdf3-64bit.nc") >>> grid.shape == (4, 3) True >>> grid.dy, grid.dx (1.0, 1.0)
A more complicated example might add data with a halo to an existing grid. Note that the lower left corner must be specified correctly for the data and the grid to align correctly.
>>> from landlab import RasterModelGrid >>> grid = RasterModelGrid((6, 5), xy_of_lower_left=(-1.0, -1.0)) >>> grid = read_netcdf( ... "test-netcdf4.nc", ... grid=grid, ... halo=1, ... nodata_value=-1, ... ) >>> grid.at_node["surface__elevation"].reshape(grid.shape) array([[ -1., -1., -1., -1., -1.], [ -1., 0., 1., 2., -1.], [ -1., 3., 4., 5., -1.], [ -1., 6., 7., 8., -1.], [ -1., 9., 10., 11., -1.], [ -1., -1., -1., -1., -1.]])