【问题标题】:Plotting a time-temperature graph using NetCDF file in R在 R 中使用 NetCDF 文件绘制时间-温度图
【发布时间】:2016-10-28 13:13:16
【问题描述】:

我的总体目标是创建一个图表,其中温度随时间绘制。 我为我的研究区域下载了网格化气候数据(NetCDF 文件),其中包含每个网格单元的经度、纬度、温度值和时间。我使用 RNetCDF 在 R 中导入了文件。这就是我使用的代码:

Grid_Temp <- "FULL_TMP_cru_ts3.23.1901.2014.tmp.dat_65-80E_35-45N.nc"
fid_T <- open.nc (Grid_Temp)
print.nc(fid_T)
Temp <- read.nc(fid_T)
close.nc(fid_T)

结果,我得到一个包含 4 个元素的列表:时间 (1d)、lon (1D)、lat (1D) 和 tmp (3D:Long,Lat,Time)。

我不知道如何使用这个输出来生成我的情节。 首先,我想限制时间元素。目前时间是从 1901 年到 2014 年。但是,我需要从 1930 开始。时间以月为单位,这意味着时间元素包含从 0:1368 开始的数字,因为月份是连续编号的。我需要从 360 号开始。谁能帮帮我,我该如何限制我的时间?

对于情节:我需要 x 轴和 y 轴上的时间(1930-2014)应该是研究区域的平均温度。我目前不知道如何处理这种列表,因此不知道如何创建这个情节。

非常感谢您的每一个回答! 我正在为我的硕士论文做这个,我是 R 的初学者,所以我感谢各种帮助和提示! 谢谢你

【问题讨论】:

标签: r plot netcdf


【解决方案1】:

尝试切换到ncdf4 pkg 看看是否有帮助:

library(ncdf4)
library(ggplot2)
library(dplyr)

# open the HadCRUT temp netcdf
had <- nc_open("HadCRUT.4.4.0.0.median.nc")

print(had) # not shown

# get the data

lon <- ncvar_get(had, "longitude")
lat <- ncvar_get(had, "latitude")
time <- ncvar_get(had, "time")
temperature_anomaly <- ncvar_get(had, "temperature_anomaly")

nc_close(had) # done with the file

# ref origin for this one is diff than 
time <- as.Date(time, origin="1850-01-01")

# pick a lat/lon pair

(lon[10])
## [1] -132.5

(lat[10])
## [1] -42.5

data_frame(month=time,
           temp=temperature_anomaly[10,10,],
           col=ifelse(temp<0, "#b2182b", "#2166ac")) %>% 
  ggplot(aes(month, temp)) +
  geom_point(aes(color=col), size=0.15) +
  scale_color_identity() +
  theme_bw()

【讨论】:

  • 感谢您的快速答复!使用 ncdf4 包似乎效果更好!但是,我仍然对如何将开始时间设置为第 360 个月感到困惑。使用 as.Date 将我的数据变成几天而不是几个月。有没有办法使用 origin="1930-01" (几个月),而不是 origin="1930-01-01"?感谢您的帮助!
  • 您能发布指向FULL_TMP_cru_ts3.23.1901.2014.tmp.dat_65-80E_35-45N.nc 文件的链接吗?
  • 我从这个网站下载了文件。我将该区域限制为 35-45N 和 65-80E 并下载了这个受限子集。 climexp.knmi.nl/…
猜你喜欢
  • 1970-01-01
  • 2015-04-17
  • 2020-11-18
  • 1970-01-01
  • 2014-12-19
  • 2018-05-12
  • 1970-01-01
  • 1970-01-01
  • 2019-07-28
相关资源
最近更新 更多