簡體   English   中英

屏蔽 R 中特定區域的 NetCDF 文件時出錯?

[英]Getting error while masking the NetCDF file for a particular region in R?

我編寫了以下腳本來根據我的Shapefile剪輯netCDF文件。 NetCDF文件包含土壤信息。 當我使用shapefile masking NetCDF文件時,出現錯誤。 任何有關如何解決問題的建議將不勝感激。 我的netCDF文件很重,這就是為什么我沒有在此處附加它,因此我共享了NetCDF文件和Shapefile GoogleDriveLink的鏈接。

library(ncdf4)
library(rgdal)
library(raster)

NC_File <- "ADD_PROP1_NCRB.nc"
print(nc_open(NC_File))
 

NetCDF 文件的描述

1 variables (excluding dimension variables):
        byte ADD_PROP[lon,lat]   
            long_name: additional property 
            _FillValue: -100
            missing_value: -100

     2 dimensions:
        lon  Size:3377
            standard_name: longitude
            long_name: longitude
            units: degrees_east
            axis: X
        lat  Size:1725
            standard_name: latitude
            long_name: latitude
            units: degrees_north
            axis: Y

使用磚函數進行光柵轉換

b <- brick(NC_File)
b
> b 
class      : RasterBrick 
dimensions : 1725, 3377, 5825325, 1  (nrow, ncol, ncell, nlayers)
resolution : 0.00833333, 0.00833333  (x, y)
extent     : -117.6917, -89.55003, 45.31663, 59.69162  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : G:/Nelson_MIP/ForcingFilesFromOneDrive/Soil/GSDE_NCRB/ADD_PROP1_NCRB.nc 
names      : layer 
varname    : ADD_PROP

讀取我的 shapefile 以屏蔽 NetCDF

SHP <- readOGR("G:/Nelson_MIP/WatershedFile/ForSoilNetCDFprocessing.shp")
SHP
> SHP
class       : SpatialPolygonsDataFrame 
features    : 1 
extent      : -103.7103, -101.7075, 50.83711, 52.35122  (xmin, xmax, ymin, ymax)
crs         : +proj=longlat +datum=NAD83 +no_defs 
variables   : 1
names       : Strahler_O 
value       :          5

將 shapefile 的投影與 NetCDF 文件的投影匹配

SHP <- spTransform(SHP, crs(NC_File))

使用我的 shapefile 剪輯 NetCDF 文件

Masking <- mask(b, SHP)
> Masking <- mask(b, SHP)
Error in ncvar_get_inner(ncid2use, varid2use, nc$var[[li]]$missval, addOffset,  : 
  Error: variable has 2 dims, but start has 3 entries.  They must match!

這是我在raster開發版本中修復的錯誤。 使用當前版本,您可以使用raster而不是brick來解決它,因為這是一個單層數據集

library(raster)
b <- raster("ADD_PROP1_NCRB.nc")
s <- shapefile("ForSoilNetCDFprocessing.shp")
s <- spTransform(s, crs(b))
m <- mask(b, s)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM