简体   繁体   English

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

[英]How to import sinusoidal remotesensed data into raster in R?

I am trying to read a NetCDF file from the Climate Change Initiative ( CCI ) into R with the terra package. Given that the data is not on a regular grid, I am trying to find the proper way to project such data onto a regular grid.我正在尝试将来自气候变化倡议 ( CCI ) 的 NetCDF 文件读入 R 和terra package。鉴于数据不在规则网格上,我试图找到将此类数据投影到规则网格上的正确方法.

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

As we can see here, there is no projection associated to the raster.正如我们在这里看到的,没有与栅格关联的投影。

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

I am guessing this should be the “original” projection to use.我猜这应该是要使用的“原始”投影。

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

At this point, I am not sure how to proceed to properly project the data on a regular grid.此时,我不确定如何继续将数据正确投影到常规网格上。 Any help would be appreciated.任何帮助,将不胜感激。

Created on 2022-12-06 with reprex v2.0.2创建于 2022-12-06,使用reprex v2.0.2

I get我得到

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 

This suggest that data are not on a regular raster.这表明数据不在常规栅格上。

Based (ie entirely copied) on @mdsumner suggestion:基于(即完全复制)@mdsumner 建议:

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)

Created on 2022-12-12 with reprex v2.0.2创建于 2022-12-12,使用reprex v2.0.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM