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

拈花ヽ惹草 提交于 2021-01-29 10:31:32

问题


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. I'm working with Jason-1 global altimetry swath data but I only need the data for the domain represented by the 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. 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.

Link to data: 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)  

回答1:


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

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


来源:https://stackoverflow.com/questions/57479741/how-to-extract-netcdf-data-frame-by-region-using-a-polygon-shapefile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!