# Scale\_factor and add\_offset xarray.to\_netcdf

**URL:** <https://discourse.pangeo.io/t/scale-factor-and-add-offset-xarray-to-netcdf/1944>\
**Category:** Science\
**Created:** [November 21, 2021, 2:17am UTC](https://discourse.pangeo.io/t/scale-factor-and-add-offset-xarray-to-netcdf/1944 "2021-11-21T02:17:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![WilcoT](https://avatars.discourse-cdn.com/v4/letter/w/7c8e57/32.png) [@WilcoT](https://discourse.pangeo.io/u/WilcoT)\
**Post date:** [November 21, 2021, 2:17am UTC](https://discourse.pangeo.io/t/scale-factor-and-add-offset-xarray-to-netcdf/1944/1 "2021-11-21T02:17:16Z")

</div>

Hi,

I am trying to write an xarray dataset to netcdf using xr.to\_netdf. I am calculating the scale\_factor and add\_offset values for each individual data variable in the dataset using the code below. I like the data to be written as int16. mapMax is the maximum map value that is calculated for each data variable in the dataset. Using the code below results in some weird values (something like -0.0027…) for some cells (but not all) that have zero as the maximum map value when trying to view the written netcdf in panoply or qgis. The maps are correctly written if I do not pack dataset using scale\_factor and add\_offset. Does anyone know how this is possible? Could this be a bug in the .to\_netcdf function?

encoding = {}  
for data\_var in ds.data\_vars:

```
   def compute_scale_and_offset(minValue, maxValue, n):
        """
        Computes the scale_factor and offset for the dataset using a minValue and maxValue, and int n
        """
        # stretch/compress data to the available packed range
        scale_factor = (maxValue - minValue) / (2 ** n - 1)
        # translate the range to be symmetric about zero
        add_offset = minValue + 2 ** (n - 1) * scale_factor
        return scale_factor, add_offset

   # Missing value is set to -9999 and is lowest value in each data array
   mv = -9999
   ds = xr.where(ds.isnull(), mv, ds)
   scale, offset = compute_scale_and_offset(mv, mapMax, 16)
   # Calculate packed value for mv
   fvalue = (mv - offset) / scale
   
   encoding[data_var] = {'_FillValue': fvalue, 'scale_factor': scale, 'add_offset': offset, 'dtype': 'int16'}

```

ds.to\_netcdf(os.path.join(s.outPath, ‘day.nc’), encoding=encoding, engine=‘h5netcdf’)

---

<div class="post-metadata">

**Author:** ![dougiesquire](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/dougiesquire/32/1323_2.png) [@dougiesquire](https://discourse.pangeo.io/u/dougiesquire)\
**Post date:** [November 24, 2021, 9:13pm UTC](https://discourse.pangeo.io/t/scale-factor-and-add-offset-xarray-to-netcdf/1944/2 "2021-11-24T21:13:48Z")

</div>

This is not an answer to your questions (sorry), but this issue is possibly related: [Writing and reopening introduces bad values · Issue #5739 · pydata/xarray · GitHub](https://github.com/pydata/xarray/issues/5739)

---

<div class="post-metadata">

**Author:** ![rabernat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/rabernat/32/22_2.png) [@rabernat](https://discourse.pangeo.io/u/rabernat)\
**Post date:** [November 29, 2021, 2:45pm UTC](https://discourse.pangeo.io/t/scale-factor-and-add-offset-xarray-to-netcdf/1944/3 "2021-11-29T14:45:34Z")

</div>

Hi @WilcoT - thanks for asking this question on the Pangeo forum. My impression is that you would get a better response by opening an issue on the Xarray issue tracker

> **[Issues · pydata/xarray](https://github.com/pydata/xarray/issues)**
>
> N-D labeled arrays and datasets in Python. Contribute to pydata/xarray development by creating an account on GitHub.
