# Xarray to netcdf (ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all())

**URL:** <https://openradar.discourse.group/t/xarray-to-netcdf-valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous-use-a-any-or-a-all/114>\
**Category:** General\
**Created:** [December 7, 2022, 12:17pm UTC](https://openradar.discourse.group/t/xarray-to-netcdf-valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous-use-a-any-or-a-all/114 "2022-12-07T12:17:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SandeepAllampalli](https://yyz2.discourse-cdn.com/free1/user_avatar/openradar.discourse.group/sandeepallampalli/32/17_2.png) [@SandeepAllampalli](https://openradar.discourse.group/u/SandeepAllampalli)\
**Post date:** [December 7, 2022, 12:17pm UTC](https://openradar.discourse.group/t/xarray-to-netcdf-valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous-use-a-any-or-a-all/114/1 "2022-12-07T12:17:56Z")

</div>

Hello People,

Does anyone come across this type of problem while saving xarray dataset to netcdf file?

```auto
<xarray.Dataset>
Dimensions: (time: 17518, y: 900, x: 900)
Coordinates:
  * time (time) datetime64[ns] 2006-01-01T00:45:00 ... 2021-12-31T23:50:00
  * y (y) float64 -4.658e+06 -4.657e+06 ... -3.76e+06 -3.759e+06
  * x (x) float64 -5.23e+05 -5.22e+05 -5.21e+05 ... 3.75e+05 3.76e+05
Data variables:
    RW (time, y, x) float32 dask.array<chunksize=(1, 900, 900), meta=np.ndarray>
Attributes:
    radarid: 10000
    formatversion: 2
    radolanversion: 01.01.00
    radarlocations: ['bln', 'drs', 'eis', 'emd', 'ess', 'fbg', 'fld', 'fra',...

```

When I tried to save xarray dataset to netcdf like below. I get the following error.

```auto
ds.to_netcdf("D:/Sandeep/Thesis/Data/dsM.nc")

```

Error:

```auto
ds.to_netcdf("D:/Sandeep/Thesis/Data/dsM.nc", engine="h5netcdf", invalid_netcdf=True)
Traceback (most recent call last):

  File "C:\Users\Admin\AppData\Local\Temp\ipykernel_7676\1512561570.py", line 1, in <cell line: 1>
    ds.to_netcdf("D:/Sandeep/Thesis/Data/dsM.nc", engine="h5netcdf", invalid_netcdf=True)

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\core\dataset.py", line 1899, in to_netcdf
    return to_netcdf( # type: ignore # mypy cannot resolve the overloads:(

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\backends\api.py", line 1230, in to_netcdf
    dump_to_store(

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\backends\api.py", line 1277, in dump_to_store
    store.store(variables, attrs, check_encoding, writer, unlimited_dims=unlimited_dims)

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\backends\common.py", line 266, in store
    variables, attributes = self.encode(variables, attributes)

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\backends\common.py", line 355, in encode
    variables, attributes = cf_encoder(variables, attributes)

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\conventions.py", line 868, in cf_encoder
    new_vars = {k: encode_cf_variable(v, name=k) for k, v in variables.items()}

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\conventions.py", line 868, in <dictcomp>
    new_vars = {k: encode_cf_variable(v, name=k) for k, v in variables.items()}

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\conventions.py", line 273, in encode_cf_variable
    var = coder.encode(var, name=name)

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\coding\variables.py", line 169, in encode
    if not pd.isnull(fill_value):

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

ds.RW.to_netcdf("D:/Sandeep/Thesis/Data/dsM.nc", engine="h5netcdf", invalid_netcdf=True)
Traceback (most recent call last):

  File "C:\Users\Admin\AppData\Local\Temp\ipykernel_7676\1590374310.py", line 1, in <cell line: 1>
    ds.RW.to_netcdf("D:/Sandeep/Thesis/Data/dsM.nc", engine="h5netcdf", invalid_netcdf=True)

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\core\dataarray.py", line 3752, in to_netcdf
    return to_netcdf( # type: ignore # mypy cannot resolve the overloads:(

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\backends\api.py", line 1230, in to_netcdf
    dump_to_store(

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\backends\api.py", line 1277, in dump_to_store
    store.store(variables, attrs, check_encoding, writer, unlimited_dims=unlimited_dims)

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\backends\common.py", line 266, in store
    variables, attributes = self.encode(variables, attributes)

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\backends\common.py", line 355, in encode
    variables, attributes = cf_encoder(variables, attributes)

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\conventions.py", line 868, in cf_encoder
    new_vars = {k: encode_cf_variable(v, name=k) for k, v in variables.items()}

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\conventions.py", line 868, in <dictcomp>
    new_vars = {k: encode_cf_variable(v, name=k) for k, v in variables.items()}

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\conventions.py", line 273, in encode_cf_variable
    var = coder.encode(var, name=name)

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\coding\variables.py", line 169, in encode
    if not pd.isnull(fill_value):

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

ds['RW'][0].plot()
Out[46]: <matplotlib.collections.QuadMesh at 0x1eb4091f250>

ds
Out[47]: 
<xarray.Dataset>
Dimensions: (time: 17518, y: 900, x: 900)
Coordinates:
  * time (time) datetime64[ns] 2006-01-01T00:45:00 ... 2021-12-31T23:50:00
  * y (y) float64 -4.658e+06 -4.657e+06 ... -3.76e+06 -3.759e+06
  * x (x) float64 -5.23e+05 -5.22e+05 -5.21e+05 ... 3.75e+05 3.76e+05
Data variables:
    RW (time, y, x) float32 dask.array<chunksize=(1, 900, 900), meta=np.ndarray>
Attributes:
    radarid: 10000
    formatversion: 2
    radolanversion: 01.01.00
    radarlocations: ['bln', 'drs', 'eis', 'emd', 'ess', 'fbg', 'fld', 'fra',...

ds.to_netcdf("D:/Sandeep/Thesis/Data/dsM.nc")
Traceback (most recent call last):

  File "C:\Users\Admin\AppData\Local\Temp\ipykernel_7676\2740476879.py", line 1, in <cell line: 1>
    ds.to_netcdf("D:/Sandeep/Thesis/Data/dsM.nc")

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\core\dataset.py", line 1899, in to_netcdf
    return to_netcdf( # type: ignore # mypy cannot resolve the overloads:(

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\backends\api.py", line 1230, in to_netcdf
    dump_to_store(

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\backends\api.py", line 1277, in dump_to_store
    store.store(variables, attrs, check_encoding, writer, unlimited_dims=unlimited_dims)

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\backends\common.py", line 266, in store
    variables, attributes = self.encode(variables, attributes)

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\backends\common.py", line 355, in encode
    variables, attributes = cf_encoder(variables, attributes)

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\conventions.py", line 868, in cf_encoder
    new_vars = {k: encode_cf_variable(v, name=k) for k, v in variables.items()}

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\conventions.py", line 868, in <dictcomp>
    new_vars = {k: encode_cf_variable(v, name=k) for k, v in variables.items()}

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\conventions.py", line 273, in encode_cf_variable
    var = coder.encode(var, name=name)

  File "C:\Users\Admin\anaconda3\envs\wradlib\lib\site-packages\xarray\coding\variables.py", line 169, in encode
    if not pd.isnull(fill_value):

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

```

---

<div class="post-metadata">

**Author:** ![kmuehlbauer](https://yyz2.discourse-cdn.com/free1/user_avatar/openradar.discourse.group/kmuehlbauer/32/439_2.png) [@kmuehlbauer](https://openradar.discourse.group/u/kmuehlbauer)\
**Post date:** [December 8, 2022, 8:40am UTC](https://openradar.discourse.group/t/xarray-to-netcdf-valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous-use-a-any-or-a-all/114/2 "2022-12-08T08:40:17Z")

</div>

Could you please have a look at the contents of `ds.RW.encoding`? As the RADOLAN data has three values for `_FillValue` (which get mapped to `NaN`) this is OK for reading, but not for writing. You would need to only keep one of those `_FillValue`.

---

<div class="post-metadata">

**Author:** ![SandeepAllampalli](https://yyz2.discourse-cdn.com/free1/user_avatar/openradar.discourse.group/sandeepallampalli/32/17_2.png) [@SandeepAllampalli](https://openradar.discourse.group/u/SandeepAllampalli)\
**Post date:** [December 8, 2022, 10:43am UTC](https://openradar.discourse.group/t/xarray-to-netcdf-valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous-use-a-any-or-a-all/114/3 "2022-12-08T10:43:30Z")

</div>

```auto
ds.RW.encoding
Out[149]: 
{'source': 'D:\\Sandeep\\Thesis\\Data\\BIN\\Extracted\\raa01-rw_10000-0601010050-dwd---bin.gz',
 '_FillValue': array([2490, 2500, 65535]),
 'scale_factor': 0.1,
 'add_offset': 0.0,
 'dtype': dtype('uint16'),
 'coordinates': 'time y x'}

```

---

<div class="post-metadata">

**Author:** ![mgrover1](https://yyz2.discourse-cdn.com/free1/user_avatar/openradar.discourse.group/mgrover1/32/7_2.png) [@mgrover1](https://openradar.discourse.group/u/mgrover1)\
**Post date:** [December 8, 2022, 3:48pm UTC](https://openradar.discourse.group/t/xarray-to-netcdf-valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous-use-a-any-or-a-all/114/4 "2022-12-08T15:48:03Z")

</div>

You would want to only use one of those fill values instead of all three. For example,

```python
ds.encoding['_FillValue'] = ds.encoding._FillValue[:1]

```

---

<div class="post-metadata">

**Author:** ![kmuehlbauer](https://yyz2.discourse-cdn.com/free1/user_avatar/openradar.discourse.group/kmuehlbauer/32/439_2.png) [@kmuehlbauer](https://openradar.discourse.group/u/kmuehlbauer)\
**Post date:** [December 9, 2022, 8:57am UTC](https://openradar.discourse.group/t/xarray-to-netcdf-valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous-use-a-any-or-a-all/114/5 "2022-12-09T08:57:48Z")

</div>

I’d propably pick `65535` in that case. But that’s just my personal view.
