在R中如何将正弦遥感数据导入栅格?

eiee3dmh  于 2022-12-20  发布在  其他
关注(0)|答案(2)|浏览(114)

我尝试使用terra包将Climate Change Initiative(CCI)中的NetCDF文件读入R,由于数据不在规则网格上,我尝试找到将这些数据投影到规则网格上的正确方法。

library(terra)
#> terra 1.6.47

# Read the data
r <- rast("/vsicurl/https://dap.ceda.ac.uk/neodc/esacci/ocean_colour/data/v5.0-release/sinusoidal/netcdf/chlor_a/daily/v5.0/2007/ESACCI-OC-L3S-CHLOR_A-MERGED-1D_DAILY_4km_SIN_PML_OCx-20070104-fv5.0.nc?download=1", lyrs = "chlor_a")
#> Warning: [rast] unknown extent

正如我们在这里看到的,没有与光栅关联的投影。

r
#> class       : SpatRaster 
#> dimensions  : 1, 23761676, 1  (nrow, ncol, nlyr)
#> resolution  : 1, 1  (x, y)
#> extent      : 0, 23761676, 0, 1  (xmin, xmax, ymin, ymax)
#> coord. ref. :  
#> source      : https://ESACCI-OC-L3S-CHLOR_A-MERGED-1D_DAILY_4km_SIN_PML_OCx-20070104-fv5.0.nc?download=1://chlor_a 
#> varname     : chlor_a 
#> name        : chlor_a

我猜这应该是要使用的“原始”投影。

sincrs <- "+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m"

在这一点上,我不知道如何继续正确地将数据投影到一个规则的网格上。任何帮助都将不胜感激。
创建于2022年12月6日,使用reprex v2.0.2

3pmvbmvn

3pmvbmvn1#

我得到

url = "https://dap.ceda.ac.uk/neodc/esacci/ocean_colour/data/v5.0-release/sinusoidal/netcdf/chlor_a/daily/v5.0/2007/ESACCI-OC-L3S-CHLOR_A-MERGED-1D_DAILY_4km_SIN_PML_OCx-20070104-fv5.0.nc"

download.file(url, basename(url), mode="wb")

library(terra)
r = rast(f, "chlor_a" )
#[1] "vobjtovarid4: **** WARNING **** I was asked to get a varid for dimension named bin_index 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 max(rs) : no non-missing arguments to max; returning -Inf
#3: In min(rs) : no non-missing arguments to min; returning Inf
#4: [rast] cells are not equally spaced; extent is not defined

这表明数据不在常规栅格上。

liwlm1x9

liwlm1x92#

基于(即完全复制)@ mdsummer建议:

library(terra)
#> terra 1.6.47
library(tidync)
library(palr)

url <- "https://dap.ceda.ac.uk/neodc/esacci/ocean_colour/data/v5.0-release/sinusoidal/netcdf/chlor_a/daily/v5.0/2007/ESACCI-OC-L3S-CHLOR_A-MERGED-1D_DAILY_4km_SIN_PML_OCx-20070704-fv5.0.nc?download=1"

f <- curl::curl_download(url, tempfile(fileext = ".nc"))

bins <- tidync(f) |>
  activate("D1") |>
  hyper_tibble()

bins
#> # A tibble: 23,761,676 × 3
#>      lat   lon bin_index
#>    <dbl> <dbl>     <int>
#>  1 -90.0  -120         1
#>  2 -90.0     0         2
#>  3 -90.0   120         3
#>  4 -89.9  -160         4
#>  5 -89.9  -120         5
#>  6 -89.9   -80         6
#>  7 -89.9   -40         7
#>  8 -89.9     0         8
#>  9 -89.9    40         9
#> 10 -89.9    80        10
#> # … with 23,761,666 more rows

## the bin is for joining on which bin_index is used here
d <- tidync::tidync(f) |>
  activate("D1,D0") |>
  hyper_tibble(select_var = c("chlor_a")) |>
  dplyr::inner_join(bins, "bin_index")

d
#> # A tibble: 3,953,869 × 5
#>    chlor_a bin_index  time   lat   lon
#>      <dbl>     <int> <dbl> <dbl> <dbl>
#>  1   0.202   3028676 13698 -48.1 -173.
#>  2   0.246   3028677 13698 -48.1 -173.
#>  3   0.268   3028678 13698 -48.1 -173.
#>  4   0.370   3034441 13698 -48.1 -173.
#>  5   0.370   3034442 13698 -48.1 -173.
#>  6   0.322   3034443 13698 -48.1 -173.
#>  7   0.108   3035236 13698 -48.1 -123.
#>  8   0.166   3035237 13698 -48.1 -123.
#>  9   0.131   3035238 13698 -48.1 -123.
#> 10   0.119   3035239 13698 -48.1 -123.
#> # … with 3,953,859 more rows

d$time <- NULL

plot(d$lon, d$lat, pch = ".", col = palr::chl_pal(d$chlor_a))

## define a raster
r <- terra::rast(
  terra::ext(c(-180, 180, -90, 90)),
  nrows = 900,
  ncols = 1800,
  crs = "OGC:CRS84"
)

r
#> class       : SpatRaster 
#> dimensions  : 900, 1800, 1  (nrow, ncol, nlyr)
#> resolution  : 0.2, 0.2  (x, y)
#> extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84

cells <- tibble::tibble(
  cell = terra::cellFromXY(r, cbind(d$lon, d$lat)),
  chlor_a = d$chlor_a
) |>
  dplyr::group_by(cell) |>
  dplyr::summarize(chlor_a = mean(chlor_a))

r[cells$cell] <- cells$chlor_a
pal <- palr::chl_pal(palette = TRUE)
image(r, col = pal$cols[-1], breaks = pal$breaks)

创建于2022年12月12日,使用reprex v2.0.2

相关问题