如何在R中处理ncdf栅格数据。

huangapple go评论57阅读模式
英文:

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 &lt;- 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))

如何在R中处理ncdf栅格数据。

英文:

This file does not follow the CF conventions for storing raster data.

library(terra)
#terra 1.7.11
x &lt;- rast(&quot;omi_surface_no2_2005.nc&quot;)
#[1] &quot;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&quot;
#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 &lt;- nc_open(&quot;omi_surface_no2_2005.nc&quot;)
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 &lt;- ncdf4::ncvar_get(nc, nc$var[[&quot;LON_CENTER&quot;]])
lat &lt;- ncdf4::ncvar_get(nc, nc$var[[&quot;LAT_CENTER&quot;]])
d &lt;- ncdf4::ncvar_get(nc, nc$var[[&quot;surface_no2_ppb&quot;]])
nc_close(nc)

Create a SpatRaster

library(terra)
x &lt;- rast(t(d[, ncol(d):1]), ext=c(range(lon), range(lat)), crs=&quot;+proj=lonlat&quot;)

plot(sqrt(x))

如何在R中处理ncdf栅格数据。

huangapple
  • 本文由 发表于 2023年2月18日 01:30:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定