当lat和lon在R中为双精度时,从NetCDF文件中提取“tos”数据
这是我第一次使用堆栈溢出,而且我的编码技能相当差 我正在处理历史tos的NetCDF文件。 我想提取特定lat和lon的tos数据。我有一个三维数组中的tos数据,lon和lat都在一个二维矩阵中。 问题是我选择的lon和lat的行-列组合与tos数组的行-列组合不对应。 下面是我到目前为止的代码当lat和lon在R中为双精度时,从NetCDF文件中提取“tos”数据,r,arrays,matrix,netcdf,temperature,R,Arrays,Matrix,Netcdf,Temperature,这是我第一次使用堆栈溢出,而且我的编码技能相当差 我正在处理历史tos的NetCDF文件。 我想提取特定lat和lon的tos数据。我有一个三维数组中的tos数据,lon和lat都在一个二维矩阵中。 问题是我选择的lon和lat的行-列组合与tos数组的行-列组合不对应。 下面是我到目前为止的代码 # Open a NetCDF file library('ncdf4') library('raster') library('rgdal') library('ggplot2') hist_a
# Open a NetCDF file
library('ncdf4')
library('raster')
library('rgdal')
library('ggplot2')
hist_acc <- "tos_Omon_ACCESS-ESM1-5_historical_r1i1p1f1_gn_185001-201412.nc"
hist_acc1 <- nc_open(hist_acc)
# Get Latitude, Longitude and time from the Netcdf File
lon_1 <- ncvar_get(hist_acc1, "longitude")
lat_1 <- ncvar_get(hist_acc1, "latitude")
tt <- ncvar_get(hist_acc1, "time")
#Extract Temperature of the surface data and substitue fillvalue with NA and close the NetCDF file to leave more space
tos_array <- ncvar_get(hist_acc1, "tos")
fillvalue<- ncatt_get(hist_acc1, "tos", "_FillValue")
tos_array[tos_array == fillvalue$value] <- NA
nc_close(hist_acc1)
# Extract row and column numbers with specific longitudes
wnf_lon <- which(lon_1 > 7 & lon_1 < 8, arr.ind=TRUE)
rows_wnf_lon <- wnf_lon[,1]
col_wnf_lon <- wnf_lon[, 2]
#Extract row and column numbers with specific latitudes
wnf_lat <- which(lat_1 > 61 & lat_1 < 62, arr.ind=TRUE)
rows_wnf_lat <- wnf_lat[,1]
col_wnf_lat <- wnf_lat[,2]
# Extract 1 matrix from array (e g day 1)
day1 <- tos_array[, ,1]
我被困在这里是因为我的lat和lon矩阵的行数和列数与tos day1数组的行数和列数不对应
如果你不明白我的问题,请告诉我,我会尽量说得更具体一些。
如果您在R中有NetCDF文件操作的资源,请让我知道
先谢谢你
Riccardo访问此类数据的简单方法如下
library(terra)
f <- "tos_Omon_ACCESS-ESM1-5_historical_r1i1p1f1_gn_185001-201412.nc"
r <- rast(f)
r
plot(r, 1)
如果您不熟悉R中的空间光栅数据操作,请看,谢谢Robert,我试过了,但该文件似乎不是常规栅格,因此当我将其转换为光栅时,投影是错误的,可以显示吗?