几周以来,我一直在尝试将 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 (正确的图)不同 - 属于南极洲的温度通过图进行维数。
有人知道我的问题出在哪里吗?
提前致谢!