R-绘制netcdf气候数据 [英] R - Plotting netcdf climate data
问题描述
我一直在尝试绘制以下网格化的netcdf文件:在以下网站上找到的"air.1999.nc":
I have been trying plot the following gridded netcdf file: "air.1999.nc" found at the following website:
http://www.esrl.noaa.gov /psd/data/gridded/data.ncep.html
我已经根据在这里和其他地方找到的答案尝试了以下代码,但是没有运气.
I have tried the code below based on answers I have found here and elsewhere, but no luck.
library(ncdf);
temp.nc <- open.ncdf("air.1999.nc");
temp <- get.var.ncdf(temp.nc,"air");
temp.nc$dim$lon$vals -> lon
temp.nc$dim$lat$vals -> lat
lat <- rev(lat)
temp <- temp[nrow(temp):1,]
temp[temp==-32767] <- NA
temp <- t(temp)
image(lon,lat,temp)
library(maptools)
data(wrld_simpl)
plot(wrld_simpl, add = TRUE)
此代码是从以下代码中修改而来的:来自netcdf文件的变量被翻转
This code was from modified from the one found here: The variable from a netcdf file comes out flipped
有人使用这些类型的netcdf文件有任何想法或经验吗?谢谢
Does anyone have any ideas or experience with using these type of netcdf files? Thanks
推荐答案
在这个问题中,您将lat <- rev(lat)
到temp <- t(temp)
的整个部分链接到该特定OP数据集是非常特定的,并且绝对没有通用值.>
In the question you linked the whole part from lat <- rev(lat)
to temp <- t(temp)
was very specific to that particular OP dataset and have absolutely no universal value.
temp.nc <- open.ncdf("~/Downloads/air.1999.nc")
temp.nc
[1] "file ~/Downloads/air.1999.nc has 4 dimensions:"
[1] "lon Size: 144"
[1] "lat Size: 73"
[1] "level Size: 12"
[1] "time Size: 365"
[1] "------------------------"
[1] "file ~/Downloads/air.1999.nc has 2 variables:"
[1] "short air[lon,lat,level,time] Longname:Air temperature Missval:32767"
[1] "short head[level,time] Longname:Missing Missval:NA"
从这些信息中可以看到,在您的情况下,缺少的值由值32767
表示,因此以下步骤应为您的第一步:
As you can see from these informations, in your case, missing values are represented by the value 32767
so the following should be your first step:
temp <- get.var.ncdf(temp.nc,"air")
temp[temp=="32767"] <- NA
此外,在您的情况下,您的数据有4个维度,而不仅仅是2个维度,它们是经度,纬度,水平(我假设是代表高度)和时间.
Additionnaly in your case you have 4 dimensions to your data, not just 2, they are longitude, latitude, level (which I'm assuming represent the height) and time.
temp.nc$dim$lon$vals -> lon
temp.nc$dim$lat$vals -> lat
temp.nc$dim$time$vals -> time
temp.nc$dim$level$vals -> lev
如果您查看lat
,您会发现这些值是相反的(image
会皱眉),所以我们将它们反转:
If you have a look at lat
you see that the values are in reverse (which image
will frown upon) so let's reverse them:
lat <- rev(lat)
temp <- temp[, ncol(temp):1, , ] #lat being our dimension number 2
然后,经度从0到360表示,这不是标准值,应该从-180到180,所以我们将其更改为:
Then the longitude is expressed from 0 to 360 which is not standard, it should be from -180 to 180 so let's change that:
lon <- lon -180
因此,现在让我们以1000(即第一个)和第一个日期的水平绘制数据:
So now let's plot the data for a level of 1000 (i. e. the first one) and the first date:
temp11 <- temp[ , , 1, 1] #Level is the third dimension and time the fourth.
image(lon,lat,temp11)
然后叠加一张世界地图:
And then let's superimpose a world map:
library(maptools)
data(wrld_simpl)
plot(wrld_simpl,add=TRUE)
这篇关于R-绘制netcdf气候数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!