TransWikia.com

How to mask and scale raster using rasterio?

Geographic Information Systems Asked on June 13, 2021

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

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

Correct answer by mikewatt on June 13, 2021

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)

Answered by snowman2 on June 13, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP