微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当 R

如何解决当 R

这是我第一次堆栈溢出,我的编码技术很差 我正在使用历史 tos 的 NetCDF 文件。 我想提取特定纬度和经度的 tos 数据。我将 tos 数据放在一个具有三个维度的数组中,而 lon 和 lat 每个都在一个具有 2 个维度的矩阵中。 问题是我选择的 lon 和 lat 的行列组合与 tos 数组的行列组合不对应。 下面是我到目前为止的代码

# 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]

我被困在这里是因为我的经纬度矩阵的行号和列号与 tos day1 数组的行号和列号不对应。

如果您不明白我的问题,请告诉我,我会尽量更具体。 如果您有 R 中 NetCDF 文件操作的资源,请告诉我 先感谢您 里卡尔多

解决方法

访问此类数据的更简单方法是这样的

library(terra)
f <- "tos_Omon_ACCESS-ESM1-5_historical_r1i1p1f1_gn_185001-201412.nc"
r <- rast(f)
r
plot(r,1)

如果您不熟悉 R 中的空间(栅格)数据操作,不妨看看 here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。