简体   繁体   English

如何使用多边形shapefile按区域提取NetCDF数据帧

[英]How to extract NetCDF data frame by region using a polygon shapefile

I'am trying to extract the variable "swh_ku" from multiple NetCDF files together with their corresponding latitude and longitude values into csv files using a polygon shapefile or it's extent. 我正在尝试使用多边形shapefile或它的范围从多个NetCDF文件及其相应的纬度和经度值中提取变量“ swh_ku”到csv文件中。 I'm working with Jason-1 global altimetry swath data but I only need the data for the domain represented by the shapefile. 我正在使用Jason-1全局测高测绘数据,但是我只需要shapefile表示的域的数据。 I just need help with some lines of code that would complete the working code bellow so I can extract only the data for the region I'm interested in. 我只需要一些代码行的帮助即可完成下面的工作代码,因此我只能提取我感兴趣的区域的数据。

I've tried several software applications such as QGIS, ESA SNAP, Broadview Radar Altimetry Toolbox (BRAT) with no success unfortunately because I couldn't find a way automate the extraction process for the hundreds of NetCDF files. 不幸的是,我尝试了多个软件应用程序,例如QGIS,ESA SNAP,Broadview雷达测高仪工具箱(BRAT),但没有成功,因为我找不到一种自动处理数百个NetCDF文件的提取方法。 So I resorted to code with which I'm fairly new but managed to get it working after reading other posts. 因此,我求助于我刚刚接触的新代码,但在阅读其他文章后设法使其正常工作。 I've tried opening the files as raster or brick to use the #extract or #mask functions because they seem more straightforward but I couldn't manage to work them out. 我尝试使用#extract或#mask函数以栅格或砖块形式打开文件,因为它们看起来更简单,但我无法设法解决它们。

Link to data: https://drive.google.com/drive/folders/1d_XVYFe__-ynxbJNUwlyl74SPJi8GybR?usp=sharing 链接到数据: https : //drive.google.com/drive/folders/1d_XVYFe__-ynxbJNUwlyl74SPJi8GybR?usp=sharing

library(ncdf4)
library(rgdal)
library(raster)
my_read_function <- function(ncname) {
  setwd("D:/Jason-1/cycle_030")
  bs_shp=readOGR("D:/Black_Sea.shp")
  e<-extent(bs_shp)
  ncfname = ncname
  names(ncin[['var']])
  dname = "swh_ku"
  ncin = nc_open(ncfname)
  print(ncin)
  vars<-(names(ncin[['var']]))
  vars
  lon <- ncvar_get(ncin, "lon")
  nlon <- dim(lon)
  head(lon)
  lat <- ncvar_get(ncin, "lat", verbose = F)
  nlat <- dim(lat)
  head(lat)
  print(c(nlon, nlat))
  sm_array <- ncvar_get(ncin,dname)
  dlname <- ncatt_get(ncin,dname,"long_name")
  dunits <- ncatt_get(ncin,dname,"units")
  fillvalue <- ncatt_get(ncin,dname,"_FillValue")
  dim(sm_array)
  ls()
  sm.slice <- sm_array[]
  sm.vec <- as.vector(sm.slice)
  length(sm.vec)
  lonlat <- expand.grid(lon, lat)
  sm.df01 <- data.frame(cbind(lonlat, sm.vec))
  names(sm.df01) <- c("lon", "lat", paste(dname, sep = "_"))
  head(na.omit(sm.df01), 20)
  csvfile <- paste0(ncname,".csv")  
  write.table(na.omit(sm.df01), csvfile, row.names = FALSE, sep = ",")
}
my_files <- list.files("D:/Jason-1/cycle_030/")
lapply(my_files, my_read_function)  

Looks like your data is not gridded. 看起来您的数据未网格化。

library(ncdf4)
library(raster)

bs <- shapefile("Black_Sea.shp")
# simplify so that the data will look better later
bs <- as(bs, "SpatialPolygons")

f <- list.files("cycle_022", pattern="nc$", full=TRUE)

Loop would start here 循环将从此处开始

ncfname = f[1]
dname = "swh_ku"
ncin = nc_open(ncfname)
lon <- ncvar_get(ncin, "lon")
lat <- ncvar_get(ncin, "lat", verbose = F)
sm_array <- ncvar_get(ncin, dname)
xyz <- na.omit(cbind(lon, lat, sm_array))
p <- SpatialPoints(xyz[,1:2], proj4string=crs(bs))
p <- SpatialPointsDataFrame(p, data.frame(xyz))
x <- intersect(p, bs) 

x has the points that intersect with the Black Sea x的点与黑海相交

plot(bs)
points(x)
head(x@data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用由 R 中的多个多边形组成的 shapefile 从 NetCDF 中提取数据? - Extract data from NetCDF using shapefile that consist of mulitiple polygon in R? 如何根据R中的shapefile提取netcdf文件中的数据? - How to extract the data in a netcdf file based on a shapefile in R? 编写循环以通过多边形shapefile裁剪多个空间数据框 - Writing a loop for cliping multiple spatial data frame by a polygon shapefile 在 R 中将 shapefile(多边形)data.frame 转换为 S4 - Converting a shapefile (polygon) data.frame to S4 in R 如何使用 R 将区域从 shapefile 传输到 netcdf 文件? - How to transfer regions from a shapefile to a netcdf file using R? 如何使用Shapefile多边形聚合地理编码数据以使用R进行可视化? - How can I aggregate geocoded data by a shapefile polygon for visualisation using R? R中如何将数据框写入.netcdf文件 - How to write a data frame to netcdf file in R 绘制选定区域的netcdf数据 - plotting netcdf data for selected region 如何通过 R 中的另一个多边形 shapefile 剪辑多边形 shapefile? - How to clip a polygon shapefile by another polygon shapefile in R? 如何从多边形数据中提取栅格值然后加入空间数据框? - How do I extract raster values from polygon data then join into spatial data frame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM