Accessing data on THREDDS OpenDAP via python (netcdf4 or xarray): Dealing with _FillValue type mismatch error

If you try to access OOI data via http://thredds.dataexplorer.oceanobservatories.org/ OpenDAP, you may encounter the following error:

NetCDF: Not a valid data type or _FillValue type mismatch

This is due to a bug in THREDDS that will be fixed in a future release.

In the meantime, you can work around this issue simply by appending #fillmismatch to the end of your url.

Here’s a code snippet to illustrate this issue and the workaround:

import netCDF4 as nc
import xarray as xr

url = 'http://thredds.dataexplorer.oceanobservatories.org/thredds/dodsC/ooigoldcopy/public/CE02SHSM-RID27-03-CTDBPC000-recovered_host-ctdbp_cdef_dcl_instrument_recovered/deployment0010_CE02SHSM-RID27-03-CTDBPC000-recovered_host-ctdbp_cdef_dcl_instrument_recovered_20191022T140001.866000-20191122T204500.220000.nc'

# Running either of the commands below results in an error:
#   OSError: [Errno -45] NetCDF: Not a valid data type or _FillValue type mismatch
xr.open_dataset(url)
nc.Dataset(url)

# Running either of these commands will open the file successfully
url_with_fillmismatch = f'{url}#fillmismatch'
ds = xr.open_dataset(url_with_fillmismatch)
print(ds)
with nc.Dataset(url_with_fillmismatch) as nc_ds:
    print(nc_ds)