question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"write to read-only" Error in xarray.open_mfdataset() with opendap datasets

See original GitHub issue

Error in loading in data from a THREDDS server. Can’t find any info on what might be causing it based on the error messages themselves.

Code Sample

def list_dates(start, end):
    num_days = (end - start).days
    return [start + dt.timedelta(days=x) for x in range(num_days)]

start_date = dt.date(2017, 3, 1)
end_date = dt.date(2017, 3, 31)
date_list = list_dates(start_date, end_date)
window = dt.timedelta(days=5)

url = 'https://www.ncei.noaa.gov/thredds/dodsC/OisstBase/NetCDF/V2.0/AVHRR/{0:%Y%m}/avhrr-only-v2.{0:%Y%m%d}.nc'
data = []
cur_date = start_date
for cur_date in date_list:
    
    date_window = list_dates(cur_date - window, cur_date + window)
    url_list = [url.format(x) for x in date_window]
    window_data=xr.open_mfdataset(url_list).sst
    data.append(window_data.mean('time'))
    

dataf=xr.concat(data, dim=pd.DatetimeIndex(date_list, name='time'))

Expected Output No error with dataf containing a data array with the dates listed above.

Error Description Error 1:

KeyError: [<class 'netCDF4._netCDF4.Dataset'>, ('https://www.ncei.noaa.gov/thredds/dodsC/OisstBase/NetCDF/V2.0/AVHRR/201703/avhrr-only-v2.20170322.nc',), 'r', (('clobber', True), ('diskless', False), ('format', 'NETCDF4'), ('persist', False))]

Error 2: OSError: [Errno -37] NetCDF: Write to read only: b'https://www.ncei.noaa.gov/thredds/dodsC/OisstBase/NetCDF/V2.0/AVHRR/201703/avhrr-only-v2.20170322.nc'

Versions python: 3.7.4 xarray: 0.15.0 pandas: 0.25.1 numpy: 1.16.5 scipy: 1.3.1 netcdf4: 1.5.3

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:26 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
rsignell-usgscommented, Jun 4, 2020

@EliT1626 , I confirmed that this problem exists on Windows, but not on Linux.

The error:

 IOError: [Errno -37] NetCDF: Write to read only: 'https://www.ncei.noaa.gov/thredds/dodsC/OisstBase/NetCDF/V2.1/AVHRR/201703/oisst-avhrr-v02r01.20170304.nc'

suggested some kind of cache problem, and as you noted it always fails after a certain number of dates, so I tried increasing the number of cached files from the default 128 to 256:

xr.set_options(file_cache_maxsize=256)

but that had no effect.

Just to see if it would fail earlier, I then tried decreasing the number of cached files:

xr.set_options(file_cache_maxsize=10)

and to my surprise, it ran all the way through: https://nbviewer.jupyter.org/gist/rsignell-usgs/c52fadd8626734bdd32a432279bc6779

I’m hoping someone who worked on the caching (@shoyer?) might have some idea of what is going on, but at least you can execute your workflow now on windows!

0reactions
max-sixtycommented, Apr 9, 2022

I’m trying to close issues that won’t lead to changes — please reopen with a MCVE if this is still an issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

xarray.open_dataset
open_dataset opens the file with read-only access. When you modify values of a Dataset, even one linked to files on disk, only the...
Read more >
Error on using xarray open_mfdataset function - Stack Overflow
This argument is passed on to xarray.auto_combine() along with the dataset objects. You only need to provide this argument if the dimension ...
Read more >
OpeNDAP query via Python xarray using GLBy0.08-latest ...
OpeNDAP query via Python xarray using GLBy0.08-latest randomly fails ... Open the GLBy0.08-latest + forecast data set ... cageeye.no> wrote:.
Read more >
cfgrib: easy and efficient grib file access in xarray - ecmwf
Pros: xarray conversion, read-write, conda ... xarray read-only backend ... cfgrib/dataset.py", line 150, in enforce_unique_attributes.
Read more >
Handling NetCDF Files using XArray for Absolute Beginners
netCDF4-python for basic netCDF operation such as reading/writing; dask-array 0.16+ for parallel computing with dask. If you want to visualize ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found