我将 Sentinel-3 SLSTR LST 数据存储在不同的 NetCDF 文件中。我知道如何通过 SNAP 从 Sentinal-3 中提取地表温度 (LST),但是,我想使用 R 来处理此类数据。 LST 变量存储在“LST_in.nc”文件中。纬度和经度位于另一个文件“geodetic_in.nc”中。因此,我想将 Sentinel-3 LST NetCDF 转换为 GeoTiff 格式。
这是我的尝试:
dir <- "/home/user/S3A_SL_2_LST____20221125T125014_20221125T125314_20221126T214452_0179_092_266_3060_PS1_O_NT_004.SEN3/"
output_raster = "20221126T214452_0179_092_266_3060_PS1_O_NT_004"
# Loading libraries.
library(ncdf4)
library(raster)
library(dplyr)
library(ggplot2)
library(terra)
# Creating filepath names
climate_filepath <- paste0(dir, "LST_in.nc")
cart_filepath <- paste0(dir, "geodetic_in.nc")
# Reading them in using nc_open
nc <- nc_open(climate_filepath)
cord <- nc_open(cart_filepath)
# All three files have a 1200 x 1500 cell matrix. Thus, I collapsed the matrix, and bound them all into a dataframe:
latitude <- ncvar_get(cord, "latitude_in") %>% as.vector()
longitude <- ncvar_get(cord, "longitude_in") %>% as.vector()
lst <- ncvar_get(nc, "LST") %>% as.vector()
LST_DF = data.frame(lon = longitude,
lat = latitude,
LST = lst) %>%
#Convert from Kelvin to Celcius
dplyr::mutate(LST = LST - 273.15)
# I converted all the variables in a data frame to a matrix
LST_DF_matrix <- data.matrix(LST_DF, rownames.force = NA)
colnames(LST_DF_matrix) <- c('X', 'Y', 'Z')
head(LST_DF_matrix)
# Set up a raster geometry, using terra package
e <- ext(apply(LST_DF_matrix[,1:2], 2, range))
# Set up the raster. I choose this ncol and nrow, however, I don't know if it is correct.
# The dimension of the data is 1200, 1500, 1800000 (nrow, ncol, ncell) with the 1 km grid resolution. In another attempt, I opened each NetCDF as a raster too.
r <- rast(e, ncol=1000, nrow=1000)
# I apply rasterize
x <- rasterize(LST_DF_matrix[, 1:2], r, LST_DF_matrix[,3]) #, fun=mean)
plot(x) #, col = rev(rainbow(25)))
# Set CRS
crs(x) = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
# Saving to a GeoTIFF
writeRaster(x = x, filename = paste0(dir, output_raster, "_v3.tif"), overwrite=TRUE)
但是,当使用脚本 R 绘制并保存为栅格时,我得到了奇怪的结果:
数据可以从这里下载。
如果有人能够指出我所犯的错误并指出任何解决方案,我将不胜感激。
您遇到的问题是,您的目标网格的分辨率比输入数据的分辨率更高。当使用
terra::rasterize()
进行栅格化时,如果源中的多个像元值适合目标网格的同一像元,则值会被聚合。但请考虑相反的情况,即您没有来自源的单元格值。在这种情况下,NA
将被写入目标网格。我不确定 SNAP 工具如何处理地理编码,但我怀疑它是通过 GCP 和仿射变换完成的