0

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

snowman2
  • 7,321
  • 12
  • 29
  • 54
Nikko
  • 581
  • 4
  • 16

2 Answers2

1

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

mikewatt
  • 5,083
  • 9
  • 21
1

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

array([0.27079999])

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)
snowman2
  • 7,321
  • 12
  • 29
  • 54