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.
To import a single netCDF file as a dataset:
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:
ds = nc.open_data(file_list)
ds = nc.open_data("data/*.nc")
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:
ds = nc.open_url("www.foo.nc")
To work with data available over a thredds server or OPeNDAP, use open_thredds. The url must end with .nc:
ds = nc.open_thredds("www.foo.nc")
The method to_nc lets you export a dataset to a netCDF file. Use zip first if you want a compressed file:
ds = nc.open_data(infile) ds.tmean() ds.zip() ds.to_nc(outfile)
to_dataframe exports to a pandas dataframe, and to_xarray/from_xarray move a dataset to and from xarray:
ds = nc.open_data(infile) ds.tmean() df = ds.to_dataframe() xr_ds = ds.to_xarray() ds2 = nc.from_xarray(xr_ds)
Pass subset arguments straight to to_nc, to_xarray or to_dataframe to export only part of a dataset:
ds.to_xarray(year=2000) ds.to_xarray(lon=[0, 90], lat=[0, 90])