def get_int_encoding(da, n=16, max_percentile=100):
    """
    Calculate encoing offset and scale factor for int conversion
    I recommend int16 for a balance between good compression and 
    preservation of precision. 
    
    Parameters
    ----------
    da: xr.DataArray
        the data array you'd like to compress
    n: int
        integer bit number [8, 16, 32]
        recommend 16
    max_percentile: float [100]
        Value to set the maximum limit of the data. 
        I recommend values greater than 99.99
    """
    
    if max_percentile == 100:
        vmin = da.min().values
        vmax = da.max().values
    elif max_percentile < 100:
        quantile = max_percentile / 100
        q = [1 - quantile, quantile] 
        vmin, vmax = da.quantile(q).values
    else:
        raise ValueError('max_percentile must be <= 100')
        
    n_ints = 2**n
    max_scaled = n_ints / 2 - 1
    min_scaled = 1 - n_ints / 2
    scaling_range = max_scaled - min_scaled
    
    # stretch/compress data to the available packed range
    scale_factor = (vmax - vmin) / scaling_range

    # translate the range to be symmetric about zero
    add_offset = vmin + max_scaled * scale_factor
    
    # fill_value is 1 less than the minimum scaled value
    fill_value = min_scaled - 1
    
    encoding = dict(
        _FillValue=fill_value,
        add_offset=add_offset,
        scale_factor=scale_factor,
        complevel=1,
        zlib=True, 
        dtype=f'int{n}',
    )

    return encoding