【问题标题】:Extracting subset from netCDF file using lat/lon and converting into .csv in R使用 lat/lon 从 netCDF 文件中提取子集并在 R 中转换为 .csv
【发布时间】:2017-08-28 12:06:32
【问题描述】:

我有一系列 nertCDF 文件,其中包含特定变量的全局数据,例如tmin/tmax/precipiation/windspeed/relative Humudity/radiation 等。在 R 中使用 nc_open 函数时,我得到以下信息:

数据文件:https://www.dropbox.com/s/xpo7zklcmtm3g5r/gfdl_preci.nc?dl=0

文件 gfdl_preci.nc (NC_FORMAT_NETCDF4_CLASSIC):

     1 variables (excluding dimension variables):
        float prAdjust[lon,lat,time]   
            _FillValue: 1.00000002004088e+20
            missing_value: 1.00000002004088e+20
            comment: includes all types (rain, snow, large-scale, convective, etc.)
            long_name: Bias-Corrected Precipitation
            units: kg m-2 s-1
            standard_name: precipitation_flux

     3 dimensions:
        lon  Size:720
            standard_name: longitude
            long_name: longitude
            units: degrees_east
            axis: X
        lat  Size:360
            standard_name: latitude
            long_name: latitude
            units: degrees_north
            axis: Y
        time  Size:365   *** is unlimited ***
            standard_name: time
            units: days since 1860-1-1 00:00:00
            calendar: standard
            axis: T

    14 global attributes:
        CDI: Climate Data Interface version 1.7.0 (http://mpimet.mpg.de/cdi)
        Conventions: CF-1.4
        title: Model output climate of GFDL-ESM2M r1i1p1 Interpolated to 0.5 degree and bias corrected using observations from 1960 - 1999 for EU WATCH project
        CDO: Climate Data Operators version 1.7.0 (http://mpimet.mpg.de/cdo)
        product_id: input
        model_id: gfdl-esm2m
        institute_id: PIK
        experiment_id: historical
        ensemble_id: r1i1p1
        time_frequency: daily
        creator: isimip@pik-potsdam.de
        description: GFDL-ESM2M bias corrected impact model input prepared for ISIMIP2.

我已经能够读取 netCDF 文件(变量和维度)并将时间分段到字段中。但是,我仍然需要根据位置(使用正方形的 4 个坐标)提取信息片段,例如欧洲。稍后,我必须将切片转换为 .csv 格式。

到目前为止,我可以弥补这一步:

# load the ncdf4 package
library(ncdf4)

# set path and filename
setwd("D:/netcdf")
ncname <- "gfdl_preci"
ncfname <- paste(ncname, ".nc", sep = "")
dname <- "prAdjust" 

# open a netCDF file
ncin <- nc_open(ncfname)
print(ncin)
# get longitude and latitude
lon <- ncvar_get(ncin,"lon")
nlon <- dim(lon)
head(lon)

lat <- ncvar_get(ncin,"lat")
nlat <- dim(lat)
head(lat)

print(c(nlon,nlat))

# get time
time <- ncvar_get(ncin,"time")
time

tunits <- ncatt_get(ncin,"time","units")
nt <- dim(time)
nt
tunits

# get variable
preci.array <- ncvar_get(ncin,dname)

dlname <- ncatt_get(ncin,"prAdjust","long_name")

dunits <- ncatt_get(ncin,"prAdjust","units")

fillvalue <- ncatt_get(ncin,"prAdjust","_FillValue")

dim(preci.array)

# split the time units string into fields
tustr <- strsplit(tunits$value, " ")

tdstr <- strsplit(unlist(tustr)[3], "-")

tmonth = as.integer(unlist(tdstr)[2])

tday = as.integer(unlist(tdstr)[3])

tyear = as.integer(unlist(tdstr)[1])

chron(time, origin = c(tmonth, tday, tyear))

任何帮助将不胜感激!

【问题讨论】:

  • 您能否发布您的 R 代码以获得此结果。
  • # 加载 ncdf4 包库(ncdf4) # 设置路径和文件名 setwd("D:/netcdf") ncname
  • 嗨,您的问题取得了一些进展。看起来你的问题得到了回答?如果是这样考虑关闭问题。顺便说一句,你问这个问题的方式不是很好。人们不欣赏这一点。尝试使您的代码示例可重现,但删除与问题无关的信息。你可以在这里阅读更多信息:stackoverflow.com/help/how-to-ask
  • 非常感谢您的友好评论。对于无法以标准格式提出我的问题,我深表歉意。尽管如此,我已经能够读取 netCDF 文件(变量和维度)并将时间分段到字段中。但是,我仍然需要根据位置(使用正方形的 4 个坐标点)例如欧洲来提取信息片段。稍后,我必须将切片转换为 .csv 格式。

标签: r netcdf netcdf4


【解决方案1】:

1.) 我们不知道您的文件,但您可以像这样在 R 中获取 netCDF 对象的一些内部信息:

data <- ncvar_get(ncin)
data

或者您直接寻址插槽。您还可以尝试使用其他数字(例如 11 或 7)来寻址列表对象上的其他插槽。

ncin[[7]]
ncin[[11]]

2.) 这是您使用的软件包的文档,我认为您的问题的答案就在其中:
https://cran.r-project.org/web/packages/ncdf4/ncdf4.pdf

3.) 您将 R 中的信息保存在这样的文件中:

write.csv(cbind(lat, lon), "result.csv", row.names=F)

【讨论】:

  • 我添加如何保存到csv
【解决方案2】:

你可以用library raster在nc中提取点

library(raster)
library(sp)

r <- brick("csiromk3.6-rcp45-2010-2099-pr.nc", varname = "pr")
vals <- extract(r, matrix(c(95.46400, 5.40400), ncol = 2))
vals

您可以通过将 vals 转换为数据框将其写入 csv

vals <- as.data.frame(t(vals),row.names = FALSE)

write.csv(vals, "D:\\ch_2010_2099_rcp45.csv")

【讨论】:

    猜你喜欢
    • 2021-08-14
    • 2014-01-04
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    • 2018-04-16
    • 1970-01-01
    • 2015-04-21
    • 1970-01-01
    相关资源
    最近更新 更多