简体   繁体   中英

Converting Sentinel 3 LST image with netcdf format to tiff with proper coordinates python

I have a Sentinel 3 image which is stored in a number of netcdf files. The variable is stored in the file "LST_in.nc" with dimensions = rows and columns of the image. The lat and long are in another file "geodetic_in.nc". I want to export the image with the lat and long to tiff format.

To my understanding, the names of dimensions and coordinates should be the same, while I failed to do this

here are my attempts

import rioxarray as rio
import xarray as xr

xds = xr.open_dataset('LST_in.nc')
coord =xr.open_dataset('geodetic_in.nc')
lat, lon = coord.latitude_in.data, coord.longitude_in.data
xds = xds.assign_coords({"lat":(["rows","columns"], lat), "lon":(["rows","columns"], lon)})
xds = xds.rename_dims({"rows": "lon", "columns": 'lat'})

Here I received this error ValueError: Cannot rename rows to lon because lon already exists. Try using swap_dims instead.

Then I tried this

xds = xds.swap_dims({'rows' : 'lon', 'columns' : 'lat'})

but received another error ValueError: replacement dimension 'lon' is not a 1D variable along the old dimension 'rows'

Also this one

lst = xds.LST
lst.rio.set_spatial_dims(x_dim = 'lon', y_dim = 'lat', inplace = True)

Error: MissingSpatialDimensionError: x dimension (lon) not found. Data variable: LST

The only one that works but with the wrong coordinates is

lst = xds.LST
lst.rio.set_spatial_dims(x_dim = 'columns', y_dim = 'rows', inplace = True)
lst.rio.write_crs("epsg:4326", inplace = True)
lst.rio.to_raster("lst.tif")

I would appreciate your help. attached is the image files

https://wetransfer.com/downloads/e3711adf56f73cd07119b43d19f7360820220117154330/c46b21

The short answer is: you can't. Because both netCDF and grib are gridded data format and the current data points positions can't be described using a regular latitude/longitude grid.

I plotted a sample of your data points latitude and longitude: 在此处输入图像描述

As you can see, the data points are not placed on lines of constant latitude and longitude, they do not follow any pattern that could be describe with projected grid, rotated grid or curvilinear grid either.

If you want to make a gridded file with the LST values and latitude and longitude as coordinates, you will have to reproject your data. You can use the rasterio.warp module,see here for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM