5

获取多年月度温度数据的栅格文件,其名称可通过以下格式“Jan.1981”、“Feb.1981”等访问(使用以下names(object)代码的两年示例文件- 添加所有文件让它太大了。

使用以下代码将其读入并将其写入 NetCDF:

#Load Packages
library(raster)
library(ncdf4)

#Read in temperature files
r1 <- brick('TavgM_1981.grd')
r2 <- brick('TavgM_1982.grd')

#stack them together 
TempStack = stack(r1, r2)

#set the coordinate system (as it was missing)
crs(TempStack) <- ('+proj=lcc +lat_1=53.5 +lat_2=53.5 +lat_0=46.834 +lon_0=5 +x_0=1488375 +y_0=-203375 +datum=WGS84 +to_meter=2500 +no_defs +ellps=WGS84 +towgs84=0,0,0')

#reproject to get in lat/lon instead of meters
TempStack<-projectRaster(TempStack, crs=CRS("+init=epsg:4326"))

#Extract monthly data names to assign to netCDf later
names <- names(TempStack)

#write the raster file to NetCDF
writeRaster(TempStack, "Temp.nc", overwrite=TRUE, format="CDF",     varname="Temperature", varunit="degC", 
        longname="Temperature -- raster stack to netCDF, monthly average", xname="Longitude",   yname="Latitude", zname='Time', zunit=names)

当我将其写入 NetCDF 并绘制从第 1 个月到第 24 个月组织的月度数据时,但我希望它具有“1981 年 1 月”、“1981 年 2 月”等。

我认为通过在 writeRaster 中添加 zunit 参数会起作用,但事实并非如此,数字仍然是 1-24 而不是 Jan、Feb 等。

4

1 回答 1

9

您的示例中有几个误解。首先,您应该意识到 netcdf 维度中的值必须是数字。它们不仅仅是层的标签,它们是该维度的实际值,因此不能采用像"Jan.1980"字符串这样的值。解决此问题的一种方法是保存您的 netcdf 文件,然后将 z 维度值作为数值添加到其中。不幸的是,这意味着我们也不能使用日期/时间变量类型,但必须首先将它们转换为数字等价物。在这里,我使用lubridate包来做到这一点。

# first we write the netcdf file to disk
writeRaster(TempStack, "Temp.nc", overwrite=TRUE, 
            format="CDF",     varname="Temperature", varunit="degC", 
            longname="Temperature -- raster stack to netCDF, monthly average", 
            xname="Longitude",   yname="Latitude", zname='Time', zunit='seconds')

# and open a connection to it to make changes.
# note that we use write=TRUE so that we can change it
nc = nc_open('Temp.nc', write = TRUE)

# now convert the strings to numeric values based on their dates
zvals = lubridate::parse_date_time(names, orders = 'm.y', tz = "UTC")
zvals = as.integer(zvals)

# and we can write these numeric dates to the z dimension
ncdf4::ncvar_put(nc, 'Time', zvals)

像这样将日期写入 z 维度,如果要将数字 z 值转换回看起来像“Jan.1908”等的栅格图层名称,我们还需要反转该过程。同样,lubridate 可以提供帮助。

ncb = brick('Temp.nc')
zvals = ncvar_get(nc, 'Time')
zvals =  as.POSIXct(zvals, origin = lubridate::origin, tz = "UTC")
znames = paste0(lubridate::month(zvals, label=T), '.', lubridate::year(zvals))
names(ncb) = znames

让我们检查一下是否有效:

plot(ncb)

在此处输入图像描述

于 2018-05-06T21:30:16.633 回答