issues: 368833116
This data as json
id | node_id | number | title | user | state | locked | assignee | milestone | comments | created_at | updated_at | closed_at | author_association | active_lock_reason | draft | pull_request | body | reactions | performed_via_github_app | state_reason | repo | type |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
368833116 | MDU6SXNzdWUzNjg4MzMxMTY= | 2478 | masked_array write/read differences between xarray and netCDF4 | 16655388 | closed | 0 | 3 | 2018-10-10T20:12:19Z | 2023-09-13T12:41:03Z | 2023-09-13T12:41:02Z | NONE | Here is code used to read/write a masked_array with the netCDF4 and xarray modules. As seen if you run the code, for 3 cases the masked_value is read as a np.nan. However, for the netcdf file written by netCDF4 and read by xarray, the masked_value is the default _FillValue of 9.96920997e+36. I wonder if this is expected or if I am doing something wrong. ```python import xarray as xr import netCDF4 as nc import numpy as np import os data = np.ma.array([1.,2.], mask = [True, False]) create file with netcdf$nc_file = 'ncfile.nc' if os.path.exists(nc_file): os.remove(nc_file) ds = nc.Dataset(nc_file, 'w') ds.createDimension('dim1', 2) var = ds.createVariable('data', 'f8', dimensions = ('dim1')) var[:] = data ds.close() create file with xarrayda = xr.DataArray(data, name = 'data', dims = {'dim1':2}) nc_file = 'xrfile.nc' if os.path.exists(nc_file): os.remove(nc_file) da.to_netcdf(nc_file, 'w') da.close() print('original data: {}'.format(data)) da = xr.open_dataset('ncfile.nc').data print('data from nc read by xr: {}'.format(da.values)) da = xr.open_dataset('xrfile.nc').data print('data from xr read by xr: {}'.format(da.values)) data = nc.Dataset('ncfile.nc').variables['data'][:] print('data from nc read by nc: {}'.format(da.values)) data = nc.Dataset('xrfile.nc').variables['data'][:] print('data from xr read by nc: {}'.format(da.values)) print('done') ``` Here is the output I get: ``` original data: [-- 2.0] data from nc read by xr: [9.96920997e+36 2.00000000e+00] data from xr read by xr: [nan 2.] data from nc read by nc: [nan 2.] data from xr read by nc: [nan 2.] done ``` Output of
|
{ "url": "https://api.github.com/repos/pydata/xarray/issues/2478/reactions", "total_count": 0, "+1": 0, "-1": 0, "laugh": 0, "hooray": 0, "confused": 0, "heart": 0, "rocket": 0, "eyes": 0 } |
completed | 13221727 | issue |