我发现很难使用我拥有的netcdf数据。我浏览了这里的示例(Plotting netcdf file with levels in R),但我认为我缺少一些东西。
我正在尝试绘制35度以南区域的混合层深度。数据可以在这里找到(页面底部的最后一个文件):http://www.ifremer.fr/cerweb/deboyer/mld/Surface_Mixed_Layer_Depth.php
该文件有7个变量,每个变量包含纬度,经度,时间(12个月)以及作为混合层深度的值。
到目前为止,我有:
MLD <- "mld_DReqDTm02_c1m_reg2.0.nc"
MLD <- nc_open(MLD)
print(MLD)
有7个变量,我只想要'mld'
lon <- ncvar_get(MLD, varid = "lon")
lat <- ncvar_get(MLD, varid = "lat")
summary(lon)
summary(lat)
MLD$dim$time$units
MLD_1.array <- ncvar_get(MLD, "mld")
dim(MLD_1.array)
length(lon)
length(lat)
ndvi.slice <- MLD_1.array[, , 12]
dim(ndvi.slice)
mld.vec.long <- as.vector(MLD_1.array)
length(mld.vec.long)
nlon <- dim(lon)
nlat <- dim(lat)
lonlat <- expand.grid(lon, lat)
t <- ncvar_get(MLD, "time")
tunits <- ncatt_get(MLD, "time", "units")
nt <- dim(t)
dname <- "mld"
tmp.mat <- matrix(mld.vec.long, nrow = nlon * nlat, ncol = nt)
dim(tmp.mat)
head(na.omit(tmp.mat))
lonlat <- expand.grid(lon, lat)
tmp.df02 <- data.frame(cbind(lonlat, tmp.mat))
names(tmp.df02) <- c("lon", "lat", "Jan", "Feb", "Mar", "Apr", "May",
"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
library(reshape)
tmp.df03 <- melt(tmp.df02, id=c("lat","lon"))
tmp.df04 <- subset(tmp.df03, lat >= "-35" & lat <="-80")
tmp.df04[tmp.df04 ==1.000e+09] <- NA
tmp.df04
summary(tmp.df04)
target = c(-180, 180, -90, -20)
w <- crop(rgeos::gBuffer(spTransform(countriesLow, CRS(pprj)), width = 0))
在将数据提取到.csv中,删除掩码值,重新导入然后重新绘制之后,我可以使用ggplot进行绘制(需要一段时间...)。
有没有一种方法可以提取一个变量(mld),然后找到每个月(时间)的最大值并作图?我知道我的代码确实凌乱不堪...
任何帮助将不胜感激!谢谢!
答案 0 :(得分:3)
希望这会有所帮助!
library(ncdf4)
library(raster)
#Reading netcdf file and extracting lat, lon and variable
MLD <- nc_open("D:/Personal/test/mld_DReqDTm02_c1m_reg2.0.nc")
lon <- ncvar_get(MLD, varid = "lon")
lat <- ncvar_get(MLD, varid = "lat")
mld <- ncvar_get(MLD, "mld")
# using 'raster' package to read the temporal values of variable into a raster
#r1<-flip(raster(t(matrix(mld[,,1], nrow = 180, ncol = 90))),direction="y")
e<-extent(min(lon),max(lon),min(lat),max(lat))
#extent(r1)<-e
R<-stack()
# creating raster stack of the time series data
for(i in 1:12){
r1<-flip(raster(t(matrix(mld[,,i], nrow = 180, ncol = 90))),direction="y")
extent(r1)<-e
R<-stack(R,r1)
} # IMPORTANT: This raster stack could be additionally cropped to extract the user's area of interest.
plot(R)
# Extracting max/min values for each time (raster layer) into a dataframe.
df<-data.frame(Months=month.abb)
df$Months <- factor(df$Months, levels = df$Months)
df$months<-c(1:12)
df$MLD_max<-maxValue(R)
df$MLD_min<-minValue(R)
# using 'ggplot2' package
ggplot(df)+geom_point(aes(Months,MLD_min))+geom_line(aes(months,MLD_min))
答案 1 :(得分:1)
WKURLSessionRefreshBackgroundTask
失败的原因是有一处纬度不正常(最后一纬度应为90,或者不存在)
brick(f)
由于只有一排(最北端的一排),因此可以忽略它,并按照@rar所示进行操作。这里的方法略有不同
library(ncdf4)
library(raster)
MLD <- nc_open("c:/temp/mld_DReqDTm02_c1m_reg2.0.nc")
lat <- ncvar_get(MLD, varid = "lat")
tail(lat)
#[1] 80.0 82.0 84.0 86.0 88.0 89.5
您现在可以使用作物
mld <- ncvar_get(MLD, "mld")
a <- aperm(mld, c(2,1,3))
b <- flip(brick(a), 'y')
extent(b) <- c(0,360,-90,90)
b <- rotate(b)
b <- reclassify(b, cbind( 1e+09, NA))
names(b) = month.abb
plot(b,1)
或按坐标提取
bb <- crop(b, extent(-180,180,-90,35))
plot(bb,1)