【问题标题】:Extract 'tos' data from NetCDF file when lat and lon are double in R当 R 中的 lat 和 lon 为双倍时,从 NetCDF 文件中提取“tos”数据
【发布时间】:2021-08-14 06:37:25
【问题描述】:

这是我第一次遇到堆栈溢出,我的编码能力很差 我正在使用历史 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]

我卡在这里是因为我的 lat 和 lon 矩阵的行号和列号与 tos day1 数组的行号和列号不对应。

如果您不理解我的问题,请告诉我,我会尝试更具体。 如果您有在 R 中进行 NetCDF 文件操作的资源,请告诉我 先感谢您 里卡多

【问题讨论】:

    标签: r arrays matrix netcdf temperature


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢罗伯特,我试过了,但文件似乎不是常规网格,所以当我将其转换为光栅时,投影是错误的
    猜你喜欢
    • 2014-01-04
    • 1970-01-01
    • 2018-04-16
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    • 2015-04-21
    • 1970-01-01
    • 2021-08-02
    相关资源
    最近更新 更多