a_scale parameter for writing GTiff file


calba@...
 

Hi, 

I'm trying to transform the dtype of my input raster from float32 to int16 (I'm using dask, dask-rasterio and rasterio). So I multiplied the data in my matrix by 100 and convert the dtype of my dask array to rasterio.int16. 
def multiply(array):
multipliedArray = np.where(array >= 14, 100*array, -9999)
multipliedDaskArray = da.from_array(multipliedArray, chunks=(1, 5, 365))
multipliedDaskArray.dtype = rasterio.int16
return multipliedDaskArray

Now, I want to write a new GTiff file with scale 0.01 using rasterio.open and the equivalent in rasterio of the a_scale parameter from gdal_translate function. But I don't find this equivalent yet.

After numerous tests and researches, I still haven't found the a_scale option in rasterio... So can someone explain to me if this is possible ? And if so, how ?

Thank you in advance :)


Sean Gillies
 

Hi,

On Fri, Apr 8, 2022 at 6:58 AM calba via groups.io <calba=lillemetropole.fr@groups.io> wrote:
Hi, 

I'm trying to transform the dtype of my input raster from float32 to int16 (I'm using dask, dask-rasterio and rasterio). So I multiplied the data in my matrix by 100 and convert the dtype of my dask array to rasterio.int16. 
def multiply(array):
multipliedArray = np.where(array >= 14, 100*array, -9999)
multipliedDaskArray = da.from_array(multipliedArray, chunks=(1, 5, 365))
multipliedDaskArray.dtype = rasterio.int16
return multipliedDaskArray

Now, I want to write a new GTiff file with scale 0.01 using rasterio.open and the equivalent in rasterio of the a_scale parameter from gdal_translate function. But I don't find this equivalent yet.

After numerous tests and researches, I still haven't found the a_scale option in rasterio... So can someone explain to me if this is possible ? And if so, how ?

Thank you in advance :)

Rasterio has no option to scale on opening or on reading. It's my understanding that gdal_translate's -a_scale sets a scaling metadata value but that I/O methods like https://gdal.org/api/gdaldataset_cpp.html#_CPPv4N11GDALDataset8RasterIOE10GDALRWFlagiiiiPvii12GDALDataTypeiPi8GSpacing8GSpacing8GSpacingP20GDALRasterIOExtraArg do not apply the scaling automatically. It is up to the caller to perform the scaling.

--
Sean Gillies


calba@...
 

Hello, 

Firtsly, thanks for your response ! Even if there is no option to scale on opening or on reading, do you know if it's possible to write metadatas with rasterio ?

Thank you in advance :)


Alan Snow
 

The scales and offsets properties on the DatasetWriter object should allow you to update the raster.
I recommend masking your data before scaling your data as the mask value is for the raw data.

Reference for how rioxarray does it:
  • Writing the scales/offsets to file: code
  • Reading with scales and offsets: code


calba@...
 

Ok thanks, I will try it !