I have been trying to read a raster file in netcdf format which I will later on sample. I need to read using the DatasetReader as float.

When I read:

ds = rasterio.open(f'netcdf:{file}:AOT_L2_Mean', dtype=rasterio.float64)
aot = ds.sample(120.57577514648, 16.003829956055)

The sampled data are still in Int16. These data are aerosol optical thickness thus only have value 0 - 5 and not 0 - 20,000. How to correctly read it as float and have values that are 0 - 5 like as I open it on QGIS?

Here is a sample data: https://drive.google.com/file/d/11ZRiLCIId1G1Dfjlc3TbvdgLOb4B8mit/view?usp=sharing

2 Answers 2


You have to apply any scaling/offsets yourself, as per: https://github.com/mapbox/rasterio/issues/1882#issuecomment-623697774

The values are available in ds.scales and ds.offsets if you want to do so programatically, which I think would make it:

aot = ds.sample(120.57577514648, 16.003829956055) * ds.scales + ds.offsets

  • Wow thanks! Still confirming results from QGIS.
    – Nikko
    Mar 30, 2021 at 7:19

From: Extracting data from a raster

import rioxarray
rds = rioxarray.open_rasterio(file, variable="AOT_L2_Mean", mask_and_scale=True)

# get value from grid
value = rds["AOT_L2_Mean"].sel(x=120.57577514648, y=16.003829956055, method="nearest").values

Or with xarray:

import xarray
xds = xarray.open_dataset(file)

# get value from grid
value = xds["AOT_L2_Mean"].sel(x=120.57577514648, y=16.003829956055, method="nearest").values
array(0.2708, dtype=float32)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.