我有 NetCDF 文件(例如https://data.ceda.ac.uk/neodc/esacci/lakes/data/lake_products/L3S/v1.0/2019全局域),我想根据 shapefile 提取数据边界(在这种情况下是湖 - https://www.sciencebase.gov/catalog/item/530f8a0ee4b0e7e46bd300dd),然后将剪辑的数据保存为 NetCDF 文件,但保留剪辑文件中的所有原始元数据和变量名称。这是我到目前为止所做的
library(rgdal)
library(sf)
library(ncdf4)
library(terra)
#Read in the shapefile of Lake
Lake_shape <- readOGR("C:/Users/CEDA/hydro_p_LakeA/hydro_p_A.shp")
# Reading the netcdf file using Terra Package function rast
test <- rast("ESACCI-LAKES-L3S-LK_PRODUCTS-MERGED-20190705-fv1.0.nc")
# List of some of variables names for orginal dataset
head(names(test))
[1] "water_surface_height_above_reference_datum" "water_surface_height_uncertainty" "lake_surface_water_extent"
[4] "lake_surface_water_extent_uncertainty" "lake_surface_water_temperature" "lswt_uncertainty"
#Clipping data to smaller Lake domain using the crop function in Terra Package
test3 <- crop(test, Lake_shape)
#Listing the some variables names for clipped data
head(names(test3))
[1] "water_surface_height_above_reference_datum" "water_surface_height_uncertainty" "lake_surface_water_extent"
[4] "lake_surface_water_extent_uncertainty" "lake_surface_water_temperature" "lswt_uncertainty"
# Writing the crop dataset as netcdf or Raster Layer using the WriteCDF function
filepath<-"Lake_A_ESACCI-LAKES-L3S-LK_PRODUCTS-MERGED-20020501-fv1.0"
fname <- paste0( "C:/Users/CEDA/",filepath,".nc")
rnc <- writeCDF(test3, filename =fname, overwrite=T)”
我的主要问题是当我读入剪辑的 netCDF 文件时,我似乎无法保留原始 NetCDF 的数据变量的名称。当我使用 writeCDF 函数将剪辑的数据集保存为新的 netCDF 时,它们都会自动重命名。
#Reading in the new clipped file
LakeA<-rast("Lake_A_ESACCI-LAKES-L3S-LK_PRODUCTS-MERGED-20020501-fv1.0.nc")
> head(names(LakeA))
[1] "Lake_A_ESACCI-LAKES-L3S-LK_PRODUCTS-MERGED-20020501-fv1.0_1" "Lake_A_ESACCI-LAKES-L3S-LK_PRODUCTS-MERGED-20020501-fv1.0_2"
[3] "Lake_A_ESACCI-LAKES-L3S-LK_PRODUCTS-MERGED-20020501-fv1.0_3" "Lake_A_ESACCI-LAKES-L3S-LK_PRODUCTS-MERGED-20020501-fv1.0_4"
[5] "Lake_A_ESACCI-LAKES-L3S-LK_PRODUCTS-MERGED-20020501-fv1.0_5" "Lake_A_ESACCI-LAKES-L3S-LK_PRODUCTS-MERGED-20020501-fv1.0_6"
那么,当剪辑到 R 中较小的域/shapefile 时,是否可以从原始 NetCDF 数据集中克隆/复制所有元数据变量,然后另存为 NetCDF?任何有关如何在 R 中执行此操作的指导将不胜感激。(NetCDF 和 R 对我来说都是新的,所以我不确定我缺少什么或有深入的知识来排序)。