landlab.io.netcdf.write¶
Write structured grids to NetCDF files.
Write netcdf¶
|
Write landlab fields to netcdf. |
- write_netcdf(path, grid, attrs=None, append=False, format='NETCDF3_64BIT', names=None, at=None, time=None, raster=False)[source]¶
Write landlab fields to netcdf.
Write the data and grid information for grid 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) – Path to output file.
grid (RasterModelGrid) – Landlab RasterModelGrid object that holds a grid and associated values.
append (boolean, optional) – Append data to an existing file, otherwise clobber the file.
format ({'NETCDF3_CLASSIC', 'NETCDF3_64BIT', 'NETCDF4_CLASSIC', 'NETCDF4'}) – Format of output netcdf file.
attrs (dict) – Attributes to add to netcdf file.
names (iterable of str, optional) – Names of the fields to include in the netcdf file. If not provided, write all fields.
at ({'node', 'cell'}, optional) – The location where values are defined.
raster (bool, optional) – Indicate whether spatial dimensions are written as full value arrays (default) or just as coordinate dimensions.
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.at_node["topographic__elevation"] = np.arange(12.0) >>> rmg.at_node["uplift_rate"] = 2.0 * np.arange(12.0)
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_file >>> fp = netcdf_file("test.nc", "r") >>> "uplift_rate" in fp.variables True >>> "topographic__elevation" in fp.variables False >>> fp.variables["uplift_rate"][:].flatten().astype("=f8") array([ 0., 2., 4., 6., 8., 10., 12., 14., 16., 18., 20., 22.])
>>> rmg.at_cell["air__temperature"] = np.arange(2.0) >>> write_netcdf( ... "test-cell.nc", ... rmg, ... format="NETCDF3_64BIT", ... names="air__temperature", ... at="cell", ... )
- write_raster_netcdf(path, fields, attrs=None, append=False, time=None, format='NETCDF4', names=None, at=None)[source]¶
Write Raster Model Grid landlab fields to netcdf.
Write the data and grid information for fields to path as NetCDF.
This method is for Raster Grids only and takes advantage of regular x and y spacing to save memory.
Rather that writing x and y of node locations at all (nr x nc) locations, it writes a 1D array each for x and y.
A more modern version of this might write x and y location as a netcdf coordinate. However, the original version of this function wrote x and y as data variables rather than coordinates.
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) – Path to output file.
fields (field-like) – Landlab field object that holds a grid and associated values. This must be a Raster type.
append (boolean, optional) – Append data to an existing file, otherwise clobber the file.
time (float, optional) – Add a time to the time variable.
format ({'NETCDF4'}) – Format of output netcdf file.
attrs (dict) – Attributes to add to netcdf file.
names (iterable of str, optional) – Names of the fields to include in the netcdf file. If not provided, write all fields.
at ({'node'}, optional) – The location where values are defined. Presently only implemented for type ‘node’.
Examples
>>> import numpy as np >>> from landlab import RasterModelGrid >>> from landlab.io.netcdf import write_raster_netcdf
Create a uniform rectilinear grid with four rows and 3 columns, and add some data fields to it.
>>> rmg = RasterModelGrid((4, 3)) >>> rmg.shape (4, 3) >>> rmg.at_node["topographic__elevation"] = np.arange(12.0) >>> rmg.at_node["uplift_rate"] = 2.0 * np.arange(12.0)
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 netcdf4 file but only include the uplift_rate data in the file.
>>> write_raster_netcdf( ... "test.nc", ... rmg, ... format="NETCDF3_64BIT", ... names="uplift_rate", ... )
Read the file back in and check its contents.
>>> from scipy.io import netcdf_file >>> fp = netcdf_file("test.nc", "r") >>> "uplift_rate" in fp.variables True >>> "topographic__elevation" in fp.variables False >>> fp.variables["uplift_rate"][:].flatten().astype("=f8") array([ 0., 2., 4., 6., 8., 10., 12., 14., 16., 18., 20., 22.]) >>> fp.variables["x"][:].astype("=f8") array([0., 1., 2.]) >>> fp.variables["y"][:].astype("=f8") array([0., 1., 2., 3.])
Read now with read_netcdf
>>> from landlab.io.netcdf import read_netcdf >>> grid = read_netcdf("test.nc") >>> grid.shape (4, 3) >>> grid.x_of_node array([0., 1., 2., 0., 1., 2., 0., 1., 2., 0., 1., 2.]) >>> grid.y_of_node array([0., 0., 0., 1., 1., 1., 2., 2., 2., 3., 3., 3.]) >>> grid.at_node["uplift_rate"] array([ 0., 2., 4., 6., 8., 10., 12., 14., 16., 18., 20., 22.])