当 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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...