将NETCDF数据转换为r中的Tidy DataFrame



我正试图获得2021年每个坐标的平均海面温度。我从NOAA下载了2021年海温的每日平均值。它存储在netcdf文件中,但我在将其转换为数据帧时遇到了麻烦。

我已经得到了下面的代码,我有一个数组,但无法找出从这里继续。

我希望它有列:纬度----经度----地表座温----日期

https://downloads.psl.noaa.gov/Datasets/noaa.oisst.v2.highres/sst.day.mean.2021.v2.nc

这是数据的链接。

library(ncdf4)

ncpath <- "/Users/Downloads/"
ncname <- "sst.day.mean.2021.v2"  
ncfname <- paste(ncpath, ncname, ".nc", sep="")
dname <- "sst"  

ncin <- nc_open(ncfname)
print(ncin)

lon <- ncvar_get(ncin,"lon")
nlon <- dim(lon)
head(lon)
lat <- ncvar_get(ncin,"lat")
nlat <- dim(lat)
head(lat)
time <- ncvar_get(ncin,"time")
time
tunits <- ncatt_get(ncin,"time","units")
tunits
nt <- dim(time)
nt
sstunits <- ncatt_get(ncin,"sst","units")
sstunits
sst_array <- ncvar_get(ncin,dname)
dlname <- ncatt_get(ncin,dname,"long_name")
dunits <- ncatt_get(ncin,dname,"units")
missing_value <- ncatt_get(ncin,dname,"missing_value")
dim(sst_array)

title <- ncatt_get(ncin,0,"title")
institution <- ncatt_get(ncin,0,"institution")
datasource <- ncatt_get(ncin,0,"source")
references <- ncatt_get(ncin,0,"References")
dataset_title <- ncatt_get(ncin,0,"dataset_title")
version <- ncatt_get(ncin,0,"version")

我讨厌使用netcdf文件,发现它们在光栅中更容易管理。因此,我通常使用brick()将文件切换为光栅。根据数据中的不同层(气候模型、时间、其他变量等),它真的可以像那一个函数一样简单。如果你有更多的层,你必须添加一些额外的参数。

例如,把它放在栅格砖中可能会把它放在每个时间段的一个层中(不知道你的数据的确切设置),所以如果你的数据是每天的,那么它将每天有一个层,然后你可以从那里变成一个数据帧,每天的平均值或类似的东西,添加额外的处理步骤,你需要把东西变成一个数据帧格式。否则,将netdf数据直接放入数据框架就有点困难了,因为有几个维度和变量不能整齐地放入一个表中。

最新更新