从 netcdf 中提取特定经纬度的值
Extracting values for specific lat long from netcdf
我正在尝试将 netCDF 文件读入 R。 netcdf chirps-v2.0.1981.days_p05.nc
从这里下载:
ftp://ftp.chg.ucsb.edu/pub/org/chg/products/CHIRPS-2.0/global_daily/netcdf/p05/
此 netCDF 文件将全球每日降雨量描述为经度、纬度的函数,大小为
1.1 GB
我也有一组经纬度
dat <- structure(list(locatioID = paste0('ID', 1:16), lon = c(73.73, 86, 73.45, 86.41, 85.36, 81.95, 82.57, 75.66, 82.03,
81.73, 85.66, 85.31, 81.03, 81.70, 87.03, 73.38),
lat = c(24.59, 20.08, 22.61, 23.33, 23.99, 19.09, 18.85, 15.25, 26.78,
16.63, 25.98, 23.28, 24.5, 21.23, 25.08, 21.11)),
row.names = c(1L, 3L, 5L, 8L, 11L, 14L, 17L, 18L, 19L, 21L,
23L, 26L, 29L, 32L, 33L, 35L), class = "data.frame")
library(ncdf4)
library(raster)
temp <- nc_open("chirps-v2.0.1981.days_p05.nc")
precip = list()
precip$x = ncvar_get(temp, "longitude")
precip$y = ncvar_get(temp, "latitude")
precip$z = ncvar_get(temp, "precip", start=c(1, 1, 1), count=c(-1, -1, 1))
precip.r = raster(precip)
plot(precip.r)
我有两个问题:
谁能给我解释一下 start 和 count 参数的作用是什么? ?ncvar_get
给我的直观感觉不是很好。如果我想创建儒略日 252 的光栅,
我需要更改哪个参数?
如何提取 dat
中每个纬度的所有 365 天的每日降雨量值,这样我的 matrix/dataframe 为 16 * 365 天
您可以使用以下代码从.nc 文件中提取数据
dat <- structure(list(locatioID = paste0('ID', 1:16), lon = c(73.73, 86, 73.45, 86.41, 85.36, 81.95, 82.57, 75.66, 82.03,
81.73, 85.66, 85.31, 81.03, 81.70, 87.03, 73.38),
lat = c(24.59, 20.08, 22.61, 23.33, 23.99, 19.09, 18.85, 15.25, 26.78,
16.63, 25.98, 23.28, 24.5, 21.23, 25.08, 21.11)),
row.names = c(1L, 3L, 5L, 8L, 11L, 14L, 17L, 18L, 19L, 21L,
23L, 26L, 29L, 32L, 33L, 35L), class = "data.frame")
temp <- brick("chirps-v2.0.1981.days_p05.nc")
xy <- dat[,2:3] #Column 1 is longitude and column 2 is latitude
xy
spts <- SpatialPoints(xy, proj4string=CRS("+proj=longlat +datum=WGS84"))
#Extract data by spatial point
temp2 <- extract(temp, spts)
temp3 <- t(temp2) #transpose raster object
colnames(temp3) <- dat[,1] #It would be better if you have the location names corresponding to the points
head(temp3)
write.csv(temp3, "Rainfall.csv")
我正在尝试将 netCDF 文件读入 R。 netcdf chirps-v2.0.1981.days_p05.nc
从这里下载:
ftp://ftp.chg.ucsb.edu/pub/org/chg/products/CHIRPS-2.0/global_daily/netcdf/p05/
此 netCDF 文件将全球每日降雨量描述为经度、纬度的函数,大小为 1.1 GB
我也有一组经纬度
dat <- structure(list(locatioID = paste0('ID', 1:16), lon = c(73.73, 86, 73.45, 86.41, 85.36, 81.95, 82.57, 75.66, 82.03,
81.73, 85.66, 85.31, 81.03, 81.70, 87.03, 73.38),
lat = c(24.59, 20.08, 22.61, 23.33, 23.99, 19.09, 18.85, 15.25, 26.78,
16.63, 25.98, 23.28, 24.5, 21.23, 25.08, 21.11)),
row.names = c(1L, 3L, 5L, 8L, 11L, 14L, 17L, 18L, 19L, 21L,
23L, 26L, 29L, 32L, 33L, 35L), class = "data.frame")
library(ncdf4)
library(raster)
temp <- nc_open("chirps-v2.0.1981.days_p05.nc")
precip = list()
precip$x = ncvar_get(temp, "longitude")
precip$y = ncvar_get(temp, "latitude")
precip$z = ncvar_get(temp, "precip", start=c(1, 1, 1), count=c(-1, -1, 1))
precip.r = raster(precip)
plot(precip.r)
我有两个问题:
谁能给我解释一下 start 和 count 参数的作用是什么?
?ncvar_get
给我的直观感觉不是很好。如果我想创建儒略日 252 的光栅, 我需要更改哪个参数?如何提取
dat
中每个纬度的所有 365 天的每日降雨量值,这样我的 matrix/dataframe 为 16 * 365 天
您可以使用以下代码从.nc 文件中提取数据
dat <- structure(list(locatioID = paste0('ID', 1:16), lon = c(73.73, 86, 73.45, 86.41, 85.36, 81.95, 82.57, 75.66, 82.03,
81.73, 85.66, 85.31, 81.03, 81.70, 87.03, 73.38),
lat = c(24.59, 20.08, 22.61, 23.33, 23.99, 19.09, 18.85, 15.25, 26.78,
16.63, 25.98, 23.28, 24.5, 21.23, 25.08, 21.11)),
row.names = c(1L, 3L, 5L, 8L, 11L, 14L, 17L, 18L, 19L, 21L,
23L, 26L, 29L, 32L, 33L, 35L), class = "data.frame")
temp <- brick("chirps-v2.0.1981.days_p05.nc")
xy <- dat[,2:3] #Column 1 is longitude and column 2 is latitude
xy
spts <- SpatialPoints(xy, proj4string=CRS("+proj=longlat +datum=WGS84"))
#Extract data by spatial point
temp2 <- extract(temp, spts)
temp3 <- t(temp2) #transpose raster object
colnames(temp3) <- dat[,1] #It would be better if you have the location names corresponding to the points
head(temp3)
write.csv(temp3, "Rainfall.csv")