R中的NetCDF:无法创建地图

时间:2018-04-04 22:54:19

标签: r gis netcdf netcdf4 ncdf4

所以我一直在使用R中的以下教程,习惯于NetCDF(我使用netcdf4包):http://geog.uoregon.edu/bartlein/courses/geog607/Rmd/netCDF_01.htm

但是,我无法在我的数据集中创建任何特定年份的精美图片,可以在此处找到:https://www.ncdc.noaa.gov/paleo-search/study/19419

这是我的代码,我试图获得公元100年的地图:

library(lattice)
library(ncdf4)
library(chron)
library(RColorBrewer)
setwd('/Users/Nikki/Dropbox/Europe/Drought Maps')
ncname <- "owda"
ncfname <- paste(ncname,".nc",sep="")
dname <- "pdsi"
ncin <- nc_open(ncfname)
print(ncin)

lon <- ncvar_get(ncin, "lon")
nlon <- dim(lon)
head(lon)

lat <- ncvar_get(ncin, "lat", verbose = F)
nlat <- dim(lat)
head(lat)

print(c(nlon, nlat))

t <- ncvar_get(ncin, "time")
nt <- dim(t)
head(t)

drought.array <- ncvar_get(ncin, dname)
dlname <- ncatt_get(ncin, dname, "long_name")
dunits <- ncatt_get(ncin, dname, "units")
fillvalue <- ncatt_get(ncin, dname, "_FillValue")
dim(drought.array)

creation_date <- ncatt_get(ncin, 0, "creation_date")
Description <- ncatt_get(ncin, 0, "Description")

nc_close(ncin)


m <- 100
drought.slice <- drought.array[m,,]
image(lon, lat, drought.slice, col = rev(brewer.pal(10, "RdBu")))

特别是,当我尝试运行最后一行时,我收到以下错误消息:

  

image.default中的错误(lon,lat,drought.slice,col = rev(brewer.pal(10,:     z的尺寸不是长度(x)( - 1)乘以长度(y)( - 1)

有人可以解释一下我做错了什么吗?

顺便说一下,为了让您对我的数据结构有所了解,请点击此处:

>print(ncin)
File owda.nc (NC_FORMAT_NETCDF4_CLASSIC):
 1 variables (excluding dimension variables):
    double pdsi[time,lat,lon]   
        longname: Palmer Drought Severity Index
        units: unitless

 3 dimensions:
    time  Size:2013
    lat  Size:88
        longname: latitude
        units: degrees
    lon  Size:114
        longname: longitude
        units: degrees

2 global attributes:
    creation_date: 10-Mar-2015 15:53:08
    Description: Reconstructed PDSI for Europe-Mediterranean Region (OWDA v1.0)

1 个答案:

答案 0 :(得分:1)

看起来您的图片需要旋转:

rotate <- function(x) t(apply(x, 2, rev))
image(lon, lat, rotate(drought.slice), col = rev(brewer.pal(10, "RdBu")))

适合我。