【问题标题】:How to extract surface wind speed data from a NetCDF file for a specific location in R?如何从 NetCDF 文件中提取 R 中特定位置的表面风速数据?
【发布时间】:2021-04-15 23:15:31
【问题描述】:

我正在尝试让基础设施暴露在风中。我有一个包含它们的纬度和经度的数据集。

NetCDF 文件提供 2058 年的每日近地表风速数据预测。可通过以下 URL 下载:http://esg-dn2.nsc.liu.se/thredds/fileServer/esg_dataroot1/cmip6data/CMIP6/ScenarioMIP/EC-Earth-Consortium/EC-Earth3/ssp585/r1i1p1f1/day/sfcWind/gr/v20200310/sfcWind_day_EC-Earth3_ssp585_r1i1p1f1_gr_20580101-20581231.nc

我尝试了以下循环来获取每个位置(它们最近的网格点)的平均风速:

sfcWind_filepath<-paste0("sfcWind_day_EC-Earth3_ssp585_r1i1p1f1_gr_20580101-20581231.nc")
sfcWind_output<-nc_open(sfcWind_filepath)

lon<-ncvar_get(sfcWind_output,varid = "lon")
lat<-ncvar_get(sfcWind_output,varid = "lat")
sfcWind_time<-nc.get.time.series(sfcWind_output,v = "sfcWind",time.dim.name = "time")

sfcWind<-ncvar_get(sfcWind_output, "sfcWind")

for(i in 1:nrow(Infrast))
{sfcWind<-rep(i,nrow(Infrast))
x<-Infrast[i,4]
y<-Infrast[i,3]
Infrast[i,12]<-mean(sfcWind[which.min(abs(lon - (x))),
                          which.min(abs(lat - (y))),
                          c(which(format(sfcWind_time, "%Y-%m-%d") == "2058-01-01"):which(format(sfcWind_time, "%Y-%m-%d") == "2058-12-31"))])
}

Infrast 是我的基础设施数据集,它们的纬度在第 3 列,经度在第 4 列,我希望将输出保存在数据集的第 12 列。

我收到以下错误:

Error in sfcWind[which.min(abs(lon - (x))), which.min(abs(lat - (y))),  : 
  incorrect number of dimensions

我之前使用此代码来获取预计温度的平均值,它工作得很好。 NetCDF 文件具有与此相同的维度(纬度、经度、时间)。这就是为什么我不明白这里的错误。

我对 R 很陌生,我刚开始使用 NetCDF 文件,任何帮助或建议都将不胜感激。

【问题讨论】:

    标签: r netcdf


    【解决方案1】:

    我对 R 也比较陌生,但一种可能的方法是使用 cmsaf 包。在这里,您可以使用 selpoint 或 selpoint.multi 函数来提取变量在特定位置或多个位置的时间序列。您所需要的只是您所需位置的纬度/经度坐标列表。然后它将为输出创建一个新的 netcdf 或 csv 文件。然后您可以从提取的点数据中计算平均值。可能有更好、更有效的方法,但希望这可能会有所帮助。

    【讨论】:

      【解决方案2】:

      NB 我无法对此进行测试,因为没有提供可重现的示例。 尽管如此,这应该可行。

      首先将文件作为光栅砖打开

      library(raster)
      sfcWind_output <- brick(sfcWind_filepath, varname="sfcWind")
      

      现在您可以使用这样的坐标提取值

      extract(sfcWind_output, cbind(lon,lat))
      

      【讨论】:

        猜你喜欢
        • 2021-03-29
        • 2022-07-16
        • 1970-01-01
        • 2021-08-02
        • 1970-01-01
        • 2019-08-29
        • 2016-12-31
        • 2013-01-13
        • 1970-01-01
        相关资源
        最近更新 更多