Search code examples
pythongeospatialpython-xarray

Convert xarray dimensions to latitude and longitude


I am looking at satellite netcdf format data using xarray, but I first need to convert the dimensions from scanline (the y index corresponding with the satellite scan direction) and ground-pixel (the x index corresponding with the direction adjacent to the scan direction) to latitude and longitude. The latitude and longitude dimensions are currently defined as coordinates in the format: latitude(scanline, ground_pixel). How can I convert these into dimensions of latitude(latitude) and longitude(longitude)? I'd like to be able to plot and query the xarray using lat lon coordinates and xarray query/plotting functions.

Here's a picture of the xarray. I've not yet been able to reproduce a simple example of this data format, with the two dimensions defined for the latitude and longitude coordinates.

enter image description here

The latitude and longitudes in geographical coordinates can be found using: ds.latitude.values and ds.longitude.values, but these are subset into the scanline and ground-pixel arrays. I think I need to collapse these into a single list of latitudes/longitudes.


Solution

  • The data was on an irregular grid and I used the following method to regrid it (see the XESMF docs for further info: https://xesmf.readthedocs.io/en/latest/notebooks/Pure_numpy.html):

    %%time
    import xesmf as xe
    
    lats = ds_subset.latitude.values
    lons = ds_subset.longitude.values
    
    # creating the lat and lon regular grid arrays
    # I've just used 100 to test the method quickly, a larger value is required to best represent the resolution of the original data
    
    grid_lats = np.linspace(lats.min(), lats.max(), 100)
    grid_lons = np.linspace(lons.min(), lons.max(), 100)
    
    # make the grid that the data will be regridded to
    new_grid = xr.Dataset({'lat':(['lat'],grid_lats), 'lon':(['lon'],grid_lons)})
    
    # use periodic=False if either or both the lat and lon dimensions are not regular. I found that the nearest neighbour method works the best. There's data loss with bilinear.
    regridder = xe.Regridder(ds_subset,new_grid, 'nearest_s2d', periodic=False,)
    
    # regrid the data
    ds_new = regridder(ds_subset)
    
    ds_new