0

我正在尝试使用 NOAA OI SST .nc 文件读取 R 中的温度数据。我每个月都有温度数据,但是,我无法从我想要的坐标中提取月平均温度数据并将其放入数据框中。

我是新手,非常感谢任何帮助或指示。

setwd("~/temperatura/noaa"
prueba<-nc_open("sst.mnmean.nc")

#EXTRAER DATOS
    lon<-ncvar_get(prueba,"lon")
    lat<-ncvar_get(prueba,"lat")
    time<-ncvar_get(prueba,"time")
    time=as.Date(time, origin="1800-1-1",tz="UTC")
    sst=ncvar_get(prueba,"sst")

unit<-ncatt_get(prueba,"sst","units")$value

我试图制作一个矩阵,但及时我只有数字而不是月份

matriz <- data.frame(cbind(time,lon,lat,sst))
names(matriz) <- c("time","lon","lat","temperature")

 time   lon   lat temperature
1    4352   0.5  89.5       -1.79
2    4383   1.5  88.5       -1.79
3    4414   2.5  87.5       -1.79
4    4442   3.5  86.5       -1.79
5    4473   4.5  85.5       -1.79
6    4503   5.5  84.5       -1.79
7    4534   6.5  83.5       -1.79
8    4564   7.5  82.5       -1.79
9    4595   8.5  81.5       -1.79

像这样

4

2 回答 2

1

你可以使用raster包这样的

library(raster)
r <- brick("sst.mnmean.nc")
r
plot(r, 1)

#Provide longitude & latitude
x1 <- c(0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5)
y1 <- c(89.5, 88.5, 87.5, 86.5, 85.5, 84.5, 83.5, 82.5, 81.5)
points <- cbind(y1,x1)

#Make points as saptial points
spts <- SpatialPoints(points, proj4string=CRS("+proj=longlat +datum=WGS84"))

#Plot those points
plot(spts, add = TRUE)

#Extract raster values
ex1 <- extract(r, spts, fun='mean', na.rm=TRUE, df=TRUE, weights = TRUE)

#To get the date of the raster you can use
idx <- getZ(r)

#Write the extracted vaues in .csv file for further processing
write.csv(t(ex1[-1]), "File_name.csv", row.names = idx) 
于 2020-08-03T05:26:03.307 回答
1

在 R 中将 .nc 文件读取到数据帧的最简单方法是 tidync。这很容易完成。您可能必须根据您的日历手动处理时间。我不认为 tidync 目前有能力解码它们。如果我是正确的,您正在使用的文件有一个日历,以便日期保存为自 1978-01-01 以来的天数。因此,您需要根据此计算每个文件中的日期。以下应该有效:

tidync::tidync("sst.mnmean.nc") %>% 
  tidync::hyper_tibble() %>%
  mutate(date = lubridate::ymd("1978/01/01") + lubridate::days(time))
于 2020-08-03T07:35:31.727 回答