Open devos0024 opened 1 month ago
Thanks for opening your first issue here at xarray! Be sure to follow the issue template! If you have an idea for a solution, we would really welcome a Pull Request with proposed changes. See the Contributing Guide for more. It may take us a while to respond here, but we really value your contribution. Contributors like you help make xarray better. Thank you!
I get an error in netCDF
— any ideas why yours succeeds in nc
? Is it Windows vs Mac?
(also note I needed to change the MCVE path, worth updating the example)
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
Cell In[3], line 11
7 with tmp.TemporaryDirectory() as temp_dir:
8
9 # Creating a netCDF file
10 file_path = Path(temp_dir) / "bépo.nc"
---> 11 with nc.Dataset(file_path, mode="w", encoding="Latin-1") as ds:
12 print(f"{file_path} successfully created")
14 # Open with netCDF
File src/netCDF4/_netCDF4.pyx:2469, in netCDF4._netCDF4.Dataset.__init__()
File src/netCDF4/_netCDF4.pyx:2027, in netCDF4._netCDF4._ensure_nc_success()
Yes, there is an error in the path. Sorry about that.
What I see is that Windows can't manage without the encoding parameter or the file created on the file system is bépo.nc.
With Linux, you don't need to specify it, the file created is correct. Creating a netCDF file with Latin-1 encoding doesn't cause an error, but it ends up as b?po.nc on the file system
I don't have a Mac, so I can't do the test. But it's possible that encoding on Mac is managed differently from Windows and Linux...
Yes, there is an error in the path. Sorry about that.
Is the example you posted correct? If not, could you update it?
Example fixed (using temporary folder from context manager)
Thanks.
FYI I still get this error — I'm guessing that's down to it being a Mac vs Linux issue...
Try to created /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/tmp8rywv9ge/bèpo/bépo.nc
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
Cell In[1], line 14
12 file_path = os.path.join(tmp_folder, "bépo.nc")
13 print(f"Try to created {file_path}")
---> 14 with nc.Dataset(file_path, mode="w", encoding="Latin-1") as ds:
15 print(f"{file_path} successfully created")
17 # Open with netCDF
File src/netCDF4/_netCDF4.pyx:2469, in netCDF4._netCDF4.Dataset.__init__()
File src/netCDF4/_netCDF4.pyx:2027, in netCDF4._netCDF4._ensure_nc_success()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe8 in position 62: invalid continuation byte
This test demonstrates the issue on a Windows platform configured as CP1252. To make it work, such a Windows is required.
Forwarding encoding
to netCDF4 seems like a good idea in general. Though since that clashes with an Xarray kwargs perhaps rename it to filename_encoding
at the Xarray level./
What happened?
When you try to open an existing netCDF file named "bépo.nc", for example, you get the following error:
What did you expect to happen?
Internally, netCDF transforms the file path into an array of bytes. This transformation can be configured by setting the appropriate encoding in netCDF4.Dataset constructor What I expect is to be able to pass this encoding to netCDF when the file is opened with xarray.
Perhaps the solution would be to send the encoding along with the backend_kwargs parameter :
Transmitting the encoding would also be necessary in the to_netcdf() function.
Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
Anything else we need to know?
No response
Environment