简体   繁体   English

R中的crop.netcdf文件

[英]Crop netcdf files in R

I'm trying to crop a.netcdf file with a polygon with stars package from daily.netcdf data.我正在尝试从 daily.netcdf 数据中裁剪带有stars号 package 的多边形的 .netcdf 文件。 I think I have managed to do it and could get this plot我想我已经设法做到了并且可以获得这个 plot

在此处输入图像描述

with this script用这个脚本

library(tidyverse)
library(sf)
library(stars)

# Input nc file
nc.file <- "20220301120000-NCEI-L4_GHRSST-SSTblend-AVHRR_OI-GLOB-v02.0-fv02.1.nc"
# read nc data
nc.data <- read_ncdf(nc.file, var="analysed_sst")

# Read mask coordinates
coordenades.poligon <- read_csv("coordenades_poligon.csv")
colnames(coordenades.poligon) <- c("lon","lat")

# Build sf polygon to crop data
polygon <- coordenades.poligon %>%
  st_as_sf(coords = c("lon", "lat"), crs = 4326) %>%
  summarise(geometry = st_combine(geometry)) %>%
  st_cast("POLYGON")

# Crop data
nc.stars.crop <- st_crop(nc.data,polygon)

# plot
ggplot() + geom_stars(data=nc.stars.crop) +
  coord_equal() + theme_void() +
  scale_x_discrete(expand=c(0,0))+
  scale_y_discrete(expand=c(0,0))

Now I would like to combine lon, lat and analysed_sst in a data frame.现在我想在一个数据框中组合 lon、lat 和 analysed_sst。 I managed to extract coordinates with我设法提取坐标

nc.stars.coords <- as.data.frame(st_coordinates(nc.stars.crop))

But can't find how to get the corresponding sst values to cbind with longitude and latitude.但找不到如何将相应的 sst 值与经度和纬度进行 cbind。 Maybe there are other solutions with ncdf4 package.也许还有其他解决方案ncdf4 package。

Thank you very much for your help非常感谢您的帮助

EDIT 1编辑 1

Link to SST original data (nc file): SST data SST原始数据链接(nc文件): SST数据

EDIT 2 Added head of coordenades_poligons.csv.编辑 2添加了 coordenades_poligons.csv 的负责人。 First columns are longitude and latitude points, third column is the area ID and fourth one denotes the season.第一列是经度和纬度点,第三列是区域 ID,第四列表示季节。 These are just the coordinates of a single area filtered by ID and season.这些只是按 ID 和季节筛选的单个区域的坐标。

12.5,44.5,Z1,S
2,44.5,Z1,S
0,41.5,Z1,S
4,40,Z1,S
9,40,Z1,S
9,42,Z1,S
0,41.5,Z2,S

I am making assumptions here, because this is not my area of expertise, but you are able to simply transform this into a dataset using the raster -package.我在这里做假设,因为这不是我的专业领域,但您可以使用raster -package 将其简单地转换为数据集。 This seems to be the way to go, also according to this author.这似乎是通往 go 的方式,也是根据作者的说法。

raster::as.data.frame(nc.stars.crop, xy = TRUE)

At least for me this worked.至少对我来说这是有效的。 And then you could transform it back into a simple features object, if you are so inclined with然后你可以将它转换回一个简单的特征 object,如果你愿意的话

raster::as.data.frame(nc.stars.crop, xy = TRUE) %>% 
sf::st_as_sf(coords = c('lon','lat'))

However, the transformation to lon/lat is not really exact, because it produces point data, whereas the original information is raster data.然而,经度/纬度的转换并不准确,因为它产生的是点数据,而原始信息是栅格数据。 So there is clearly information that gets lost.所以很明显有信息丢失了。

sf::st_as_sf() seems to work out of the box for this, but I am not sure, because I have no way to validate the transformation of the original data. sf::st_as_sf()似乎开箱即用,但我不确定,因为我无法验证原始数据的转换。 For me the following worked:对我来说,以下工作:

read_ncdf('20220301120000-NCEI-L4_GHRSST-SSTblend-AVHRR_OI-GLOB-v02.0-fv02.1.nc', var="analysed_sst") %>%
  sf::st_as_sf()

This creates polygons, the size of your initial raster tiles and seems to conserve all necessary information.这会创建多边形,即初始栅格图块的大小,并且似乎可以保存所有必要的信息。

Finally, here is a work-around to extracting exactly the data you were plotting.最后,这里有一个解决方法来准确提取您正在绘制的数据。 You can access the data that ggplot used, by assigning the ggplot to a variable and then accessing the data layer.您可以通过将 ggplot 分配给变量然后访问数据层来访问 ggplot 使用的数据。

p <- ggplot() + geom_stars(data=nc.stars.crop) +
coord_equal() + theme_void() +
scale_x_discrete(expand=c(0,0))+
scale_y_discrete(expand=c(0,0))

p$layers[[1]]$data

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM