pythongisnetcdfpython-xarraysatellite-image

Get nearest pixel value from satellite image using latitude longitude coordinates


I have a satellite image file. Loaded into dask array. I want to get pixel value (nearest) of a latitude, longitude of interest.

Satellite image is in GEOS projection. I have longitude and latitude information as 2D numpy arrays.

Satellite Image file

I have loaded it into a dask data array

from satpy import Scene
import matplotlib as plt
import os

cwd = os.getcwd()

fn = os.path.join(cwd, 'EUMETSAT_data/1Jan21/MSG1-SEVI-MSG15-0100-NA-20210101185741.815000000Z-20210101185757-1479430.nat')

files = [fn]

scn = Scene(filenames=files, reader='seviri_l1b_native')
scn.load(["VIS006"])
da = scn['VIS006']

This is what the dask array looks like: enter image description here enter image description here

I read lon lats from the area attribute with the help of satpy:

lon, lat = scn['VIS006'].attrs['area'].get_lonlats()
print(lon.shape)
print(lat.shape)

(1179, 808)
(1179, 808)

I get a 2d numpy array each, for longitude and latitude that are coordinates but I can not use them for slicing or selecting.

What is the best practice/method to get nearest lat long, pixel information? How do I project the data onto lat long coordinates that I can then use for indexing to arrive at the pixel value.

At the end, I want to get pixel value (nearest) of lat long of interest.

Thanks in advance!!!


Solution

  • The AreaDefinition object you are using (.attrs['area']) has a few methods for getting different coordinate information.

    area = scn['VIS006'].attrs['area']
    col_idx, row_idx = area.get_xy_from_lonlat(lons, lats)
    
    scn['VIS006'].values[row_idx, col_idx]
    

    Note that row and column are flipped. The get_xy_from_lonlat method should work for arrays or scalars.

    There are other methods for getting X/Y coordinates of each pixel if that is what you're interesting in.