[英]Extracting subset from netCDF file using lat/lon and converting into .csv in R
我有一系列nertCDF文件,其中包含特定變量的全局數據,例如tmin / tmax / precipiation / windspeed /相對濕度/輻射等。在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))
任何幫助,將不勝感激!!
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)
您可以使用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轉換為dataframe將其寫入csv
vals <- as.data.frame(t(vals),row.names = FALSE)
write.csv(vals, "D:\\ch_2010_2099_rcp45.csv")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.