pydata / pydata/xarray

Support an `encoding` parameter when reading netCDF files with xarray

Open
#10,357 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

Is your feature request related to a problem?

When I use netCDF4.Dataset to open a file whose path contains Chinese characters, I get:

import netCDF4
file = "文件.nc"
ds = netCDF4.Dataset(file)
# FileNotFoundError: [Errno 2] No such file or directory: '文件.nc'

If I specify encoding='gbk', it opens correctly:

ds = netCDF4.Dataset(file, encoding='gbk')

However, xarray’s open_dataset does not support an encoding argument:

import xarray as xr
file = "文件.nc"
ds = xr.open_dataset(file)
# FileNotFoundError: [Errno 2] No such file or directory: 'E:\\netcdf\\文件.nc'

ds = xr.open_dataset(file, engine='netcdf4', encoding='gbk')
# TypeError: NetCDF4BackendEntrypoint.open_dataset() got an unexpected keyword argument 'encoding'

Related netCDF4 issue comment
(from netCDF4-python#997)

Describe the solution you'd like

I would like xarray to accept an encoding parameter so that files with Chinese paths open successfully.

Describe alternatives you've considered

No response

Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at NetCDF4BackendEntrypoint.open_dataset, which currently rejects the encoding argument, and trace how xarray passes arguments to the netCDF4 backend. Done means encoding is accepted for this backend and a file path containing Chinese characters opens successfully, with coverage for the reported case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.