数据描述
我从哥白尼网站下载了 MULTIOBS_GLO_PHY_S_SURFACE_MYNRT_015_013 2012 年至 2024 年每日海面密度数据。一个人一次只能下载2年的数据;因此,我有五个 .nc 文件,时间跨度超过 12 年。
哥白尼网站
https://data.marine.copernicus.eu/product/MULTIOBS_GLO_PHY_S_SURFACE_MYNRT_015_013/download
所需输出
我的目标是从 netcdf 文件中提取 2012 年至 2024 年我们在实地工作期间进行的特定调查日期的点数据,并将这些值作为列插入到我的数据框中。
问题
当我运行 SSS 行从 netcdf 文件中选择可变海面密度时,收到此错误消息
Error: Not compatible with requested type: [type=character; target=double].
我已经使用完全相同的方法从 Lm3 AQUA MODIS 文件中成功提取了叶绿素-a 和海面温度点数据,但我不明白我在海面盐度方面做错了什么。
由于所有权问题,我无法共享数据框
非常感谢,如果有人可以提供帮助
代码
读取所有 5 个 netcdf 文件以获得海面密度
#SS1: January 1st 2012 to December 31st 2014
sss1<-terra::rast("~/Documents/cmems_obs-mob_glo_phy-sss_my_multi_P1D_1729236771868.nc")
plot(sss1)
sss1<-subset(sss1, 1:1096)
#SS2: January 1st 2015 to December 31st 2017
ss2<-terra::rast("~/Documents/cmems_obs-mob_glo_phy-sss_my_multi_P1D_1729236939325.nc")
#Subset to extract sea surface density only
ss2<-subset(ss2, 1:1096)
#SS3: January 1st 2018 to December 31st 2020
ss3<-terra::rast("~/Documents/cmems_obs-mob_glo_phy-sss_my_multi_P1D_1729237129644.nc")
#Subset to extract sea surface density only
ss3<-subset(ss3, 1:1096)
#SS4: January 1st 2021 to December 16th 2023
ss4<-terra::rast("~/Documents/cmems_obs-mob_glo_phy-sss_my_multi_P1D_1729252813869.nc")
#Subset to extract sea surface density only
ss4<-subset(ss3, 1:1080)
#SS5: December 17th 2023 to October 12th 2024
ss5<-terra::rast("~/Documents/cmems_obs-mob_glo_phy-sss_nrt_multi_P1D_1729253007917.nc")
#Subset to extract sea surface density only
ss5<-subset(ss3, 1:301)
将 2012 年 1 月到 2024 年 10 月 12 日的所有子集 netcdf 文件连接在一起
total_sss<-c(sss1, ss2, ss3, ss4, ss5)
提取点数据
#Select the sea surface density variable from the netcdf files
SSSs <- terra::rast(total_sss, "dos")
#Make the dataframe a spatial object of class = "sf" with a CRS of 4326
Ds_Points <- st_as_sf(x=MyDf,
coords = c("Longitude_E_DD", "Latitude_N_DD"),
crs = 4326)
#Extract the SSS values from the netcdf files
SSSs_Data <- terra::extract(SSSs, Ds_Points)
#Add the SSS results to the dataframe with the function cbind
MyDf <- cbind(MyDf, SSS = SSSs_Data$dos)
这是'total_sss'中存储的信息
class : SpatRaster
dimensions : 189, 154, 4669 (nrow, ncol, nlyr)
resolution : 0.125, 0.125 (x, y)
extent : 28.75, 48, 8.625, 32.25 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84
sources : cmems_obs-mob_glo_phy-sss_my_multi_P1D_1729236771868.nc:dos (1096 layers)
cmems_obs-mob_glo_phy-sss_my_multi_P1D_1729236939325.nc:dos (1096 layers)
cmems_obs-mob_glo_phy-sss_my_multi_P1D_1729237129644.nc:dos (1096 layers)
... and 2 more source(s)
varnames : dos (Sea surface density)
dos (Sea surface density)
dos (Sea surface density)
...
names : dos_d~=-0_1, dos_d~=-0_2, dos_d~=-0_3, dos_d~=-0_4, dos_d~=-0_5, dos_d~=-0_6, ...
unit : kg/m3, kg/m3, kg/m3, kg/m3, kg/m3, kg/m3, ...
time : 2012-01-01 to 2020-12-31 UTC
这是SS1中存储的信息
class : SpatRaster
dimensions : 189, 154, 1096 (nrow, ncol, nlyr)
resolution : 0.125, 0.125 (x, y)
extent : 28.75, 48, 8.625, 32.25 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84
source : cmems_obs-mob_glo_phy-sss_my_multi_P1D_1729236771868.nc:dos
varname : dos (Sea surface density)
names : dos_d~=-0_1, dos_d~=-0_2, dos_d~=-0_3, dos_d~=-0_4, dos_d~=-0_5, dos_d~=-0_6, ...
unit : kg/m3, kg/m3, kg/m3, kg/m3, kg/m3, kg/m3, ...
time : 2012-01-01 to 2014-12-31 UTC
更换:
SSSs <- terra::rast(total_sss, "dos")
与
SSSs <- terra::subset(total_sss, 1)
或与
SSSs <- total_sss[[1]]
理论上
total_sss$dos
也应该有效。请查看terra:subset()
功能。
顺便说一句,要临时共享文件,您可以使用 google Drive/github。