Importing & exporting

Move data in and out of NCToolkit — open files from disk, a url, or a thredds server, then save to netCDF or hand off to pandas and xarray.

NCToolkit can work with data available on local file systems, at urls, and over thredds and OPeNDAP.

Opening single files & ensembles

To import a single netCDF file as a dataset:

python
import nctoolkit as nc
ds = nc.open_data(infile)

open_data can also import multiple files, either as a list, or using a wildcard over a folder:

python
ds = nc.open_data(file_list)
ds = nc.open_data("data/*.nc")

Opening files from urls/ftp

To work with a file available at a url or ftp address, use open_url. This downloads the file to a temporary folder first, then opens it:

python
ds = nc.open_url("www.foo.nc")

Opening data over thredds servers or OPeNDAP

To work with data available over a thredds server or OPeNDAP, use open_thredds. The url must end with .nc:

python
ds = nc.open_thredds("www.foo.nc")

Save as netCDF

The method to_nc lets you export a dataset to a netCDF file. Use zip first if you want a compressed file:

python
ds = nc.open_data(infile)
ds.tmean()
ds.zip()
ds.to_nc(outfile)

pandas & xarray

to_dataframe exports to a pandas dataframe, and to_xarray/from_xarray move a dataset to and from xarray:

python
ds = nc.open_data(infile)
ds.tmean()
df = ds.to_dataframe()

xr_ds = ds.to_xarray()
ds2 = nc.from_xarray(xr_ds)

Exporting a subset

Pass subset arguments straight to to_nc, to_xarray or to_dataframe to export only part of a dataset:

python
ds.to_xarray(year=2000)
ds.to_xarray(lon=[0, 90], lat=[0, 90])