英文:
How to handle a ncdf raster in R
问题
我正在使用一个类似于以下的栅格图层:
class : RasterLayer
dimensions : 7040, 9020, 63500800 (nrow, ncol, ncell)
resolution : 1, 1 (x, y)
extent : 0.5, 9020.5, 0.5, 7040.5 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
source : omi_surface_no2_2005.nc
names : surface_no2_ppb
zvar : surface_no2_ppb
我需要重新投影它,以便能够裁剪到这个形状文件:
Simple feature collection with 1 feature and 0 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -80.24869 ymin: 43.05051 xmax: -78.43696 ymax: 44.51657
Geodetic CRS: NAD83
geometry
1 MULTIPOLYGON (((-79.38536 4...
但是当我尝试重新投影它时,它不起作用。
这是我使用的代码:
omi_reproj <- projectRaster(omi, crs = crs(gtha_shp))
栅格图层是否被错误地分配了CRS?我不确定我做错了什么。如果你需要数据图层,我可以提供一个链接。
栅格链接: link
英文:
I'm working with a raster layer that looks like this:
class : RasterLayer
dimensions : 7040, 9020, 63500800 (nrow, ncol, ncell)
resolution : 1, 1 (x, y)
extent : 0.5, 9020.5, 0.5, 7040.5 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
source : omi_surface_no2_2005.nc
names : surface_no2_ppb
zvar : surface_no2_ppb
And I need to re-project it so that I can crop it to this shape file:
Simple feature collection with 1 feature and 0 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -80.24869 ymin: 43.05051 xmax: -78.43696 ymax: 44.51657
Geodetic CRS: NAD83
geometry
1 MULTIPOLYGON (((-79.38536 4...
But when I try to re-project it, it doesn't work.
This is the code that I used:
omi_reproj <- projectRaster(omi, crs = crs(gtha_shp))
Has the raster layer been assigned the CRS incorrectly? I'm not sure what I'm doing wrong. If you need the data layers, I can provide a link.
Link to raster: link
答案1
得分: 0
这个文件不遵循存储栅格数据的CF规范。
library(terra)
#terra 1.7.11
x <- rast("omi_surface_no2_2005.nc")
#[1] "vobjtovarid4: **** WARNING **** I was asked to get a varid for dimension named dim1 BUT this dimension HAS NO DIMVAR! Code will probably fail at this point"
#Warning messages:
#1: In min(rs) : no non-missing arguments to min; returning Inf
#2: In min(rs) : no non-missing arguments to min; returning Inf
#3: In max(rs) : no non-missing arguments to max; returning -Inf
#4: [rast] cells are not equally spaced; extent is not defined
你仍然可以使用ncdf4包读取值并手动创建一个SpatRaster。
打开ncdf文件并检查其内容。
library(ncdf4)
nc <- nc_open("omi_surface_no2_2005.nc")
nc
# 3 variables (excluding dimension variables):
# double surface_no2_ppb[lon_dim,lat_dim] (Chunking: [752,587])
# double LON_CENTER[dim1,latdim] (Chunking: [1,9020])
# double LAT_CENTER[londim,dim1] (Chunking: [7040,1])
读取相关数据并关闭文件。
lon <- ncdf4::ncvar_get(nc, nc$var[["LON_CENTER"]])
lat <- ncdf4::ncvar_get(nc, nc$var[["LAT_CENTER"]])
d <- ncdf4::ncvar_get(nc, nc$var[["surface_no2_ppb"]])
nc_close(nc)
创建一个SpatRaster。
library(terra)
x <- rast(t(d[, ncol(d):1]), ext=c(range(lon), range(lat)), crs="+proj=lonlat")
plot(sqrt(x))
英文:
This file does not follow the CF conventions for storing raster data.
library(terra)
#terra 1.7.11
x <- rast("omi_surface_no2_2005.nc")
#[1] "vobjtovarid4: **** WARNING **** I was asked to get a varid for dimension named dim1 BUT this dimension HAS NO DIMVAR! Code will probably fail at this point"
#Warning messages:
#1: In min(rs) : no non-missing arguments to min; returning Inf
#2: In min(rs) : no non-missing arguments to min; returning Inf
#3: In max(rs) : no non-missing arguments to max; returning -Inf
#4: [rast] cells are not equally spaced; extent is not defined
You can still read the values with the ncdf4 package and create a SpatRaster "by hand".
Open the ncdf file and inspect its contents
library(ncdf4)
nc <- nc_open("omi_surface_no2_2005.nc")
nc
# 3 variables (excluding dimension variables):
# double surface_no2_ppb[lon_dim,lat_dim] (Chunking: [752,587])
# double LON_CENTER[dim1,latdim] (Chunking: [1,9020])
# double LAT_CENTER[londim,dim1] (Chunking: [7040,1])
Read the relevant data and close the file
lon <- ncdf4::ncvar_get(nc, nc$var[["LON_CENTER"]])
lat <- ncdf4::ncvar_get(nc, nc$var[["LAT_CENTER"]])
d <- ncdf4::ncvar_get(nc, nc$var[["surface_no2_ppb"]])
nc_close(nc)
Create a SpatRaster
library(terra)
x <- rast(t(d[, ncol(d):1]), ext=c(range(lon), range(lat)), crs="+proj=lonlat")
plot(sqrt(x))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论