0

几周以来,我一直在尝试将 NetCDF 文件放入数据框中。即使我成功地提取了变量/维度并从 ncdf 文件中绘制了一个切片,但在将其绑定到数据框然后绘制它时,所有数据都会被压缩。数据是来自哥白尼的天气数据,包含世界上每个经度和纬度点的数据。这里的最终目标是对数据框进行栅格化,以便能够随时间对每个栅格的天气进行分类。

可以从https://cds.climate.copernicus.eu/cdsapp#!/dataset/reanalysis-era5-pressure-levels?tab=overview检索数据。

我的代码如下所示:

library(ncdf4)
library(raster)
library(ggplot2)

##dataset, 5 augustus
###bron: https://cds.climate.copernicus.eu/cdsapp#!/dataset/reanalysis-era5-pressure-levels?tab=overview

weatherpress <- nc_open("C:/Users/heinj/OneDrive/Documenten/Universiteit/Master/Master Thesis/dataset/allecoors5augweer.nc")
{
  sink('WeatherPressure5aug.txt')
  print(weatherpress)
  sink()
}
print(weatherpress)


long<- ncvar_get(weatherpress, "longitude")
lat <- ncvar_get(weatherpress, "latitude")
tijd <- ncvar_get(weatherpress, "time")

rain <- ncvar_get(weatherpress,"crwc")
temperature <- ncvar_get(weatherpress, "t") #temperature
Uwind <- ncvar_get(weatherpress, "u") #u wind
Vwind <- ncvar_get(weatherpress, "v") # v wind

#removal NA's
fillvaluecrwc <- ncatt_get(weatherpress, "crwc", "_FillValue")
rain[rain == fillvaluecrwc$value] <- NA
rain <- na.omit(rain)


fillvaluet <- ncatt_get(weatherpress, "t", "_FillValue")
temperature[temperature == fillvaluet$value] <- NA
temperature <- na.omit(temperature)

fillvalueu <- ncatt_get(weatherpress, "u", "_FillValue")
Uwind[Uwind == fillvalueu$value] <- NA
Uwind <- na.omit(Uwind)


fillvaluev <- ncatt_get(weatherpress, "v", "_FillValue")
Vwind[Vwind == fillvaluev$value] <- NA
Vwind <- na.omit(Vwind)
min(temperature)

#correcting longitude

nc_close(weatherpress)
#plotje
temperature_slice <- temperature[, ,1]
r_temperature <- raster(t(temperature_slice), xmn=min(long),
                        xmx=max(long), ymn=min(lat), ymx=max(lat),
                        crs=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs+ towgs84=0,0,0"))
plot(r_temperature)

#binding into a dataframe
weather <- cbind(long, lat, rain, tijd, temperature, Uwind, Vwind)
weather <- as.data.frame(weather)
View(weather)

#subset temperature
only_temperature <- weather
only_temperature <- subset(only_temperature[,c(1:2, 5)])
head(only_temperature)
summary(only_temperature)

##ggplot###
ggplot(as.data.frame(poging1.df), aes(x = long, y = lat)) +
   geom_raster(aes(fill = temperature))


### rastering ###
only_temp <- only_temperature
r = raster(xmn = min(long), xmx=max(long),
           ymn=min(lat), ymx=max(lat), res = 10)
p = as(r@extent, 'SpatialPolygons')

cordi <- only_temp[c("long", "lat")]
coordinates(cordi) <- ~long + lat
only_temp <- SpatialPointsDataFrame(cordi, only_temp)
meanr <- rasterize(only_temp, r, "temperature", fun = mean)
plot(meanr)

其中 ggplot 和 meanr 图看起来与 r_plot (正确的图)不同 - 属于南极洲的温度通过图进行维数。

有人知道我的问题出在哪里吗?

提前致谢!

4

1 回答 1

0

如果您的目标是制作栅格,则可以terra像这样使用包:

library(terra)
f <- "allecoors5augweer.nc"
r <- rast(f)

或者,要获取单个变量,t在这种情况下:

r <- rast(f, "t")

另请参阅sds(f)。或者,您可以使用raster::brick(f). 要检查数据,您可以执行

plot(r)

只是为了回答您关于从 ncdf 文件制作 data.frame 的问题(这对于您的目标似乎不是必需的)。它可能在一定程度上取决于手头的文件(例如,文件中是否有子数据集,以及您希望如何处理这些子数据集——如果您共享了一个文件,这将非常有用)。您可以tidync用于任何 nc 文件,但如果它是空间(网格)数据,它可能最容易使用rasterterra. 有terra你可以做

library(terra)
f <- "allecoors5augweer.nc"
r <- rast(f)
d <- as.data.frame(r)
于 2021-10-22T16:43:50.033 回答