[英]time and geographical subset of netcdf raster stack or raster brick using R
For the following netcdf file with daily global sea surface temperatures for 2016, I'm trying to (i) subset temporally, (ii) subset geographically, (iii) then take long-term means for each pixel and create a basic plot.对于以下具有 2016 年每日全球海面温度的 netcdf 文件,我尝试 (i) 时间上的子集,(ii) 地理上的子集,(iii) 然后对每个像素采取长期手段并创建一个基本图。
library(raster)
library(ncdf4)
open the netcdf after setting my working directory设置我的工作目录后打开 netcdf
nc_data <- nc_open('sst.day.mean.2016.v2.nc')
change the time variable so it's easy to interpret更改时间变量以便于解释
time <- ncdf4::ncvar_get(nc_data, varid="time")
head(time)
change to dates that I can interpret更改为我可以解释的日期
time_d <- as.Date(time, format="%j", origin=as.Date("1800-01-01"))
Now I'd like to subset only September 1 to October 15, but can't figure that out...现在我只想从 9 月 1 日到 10 月 15 日进行子集化,但无法弄清楚......
Following temporal subset, create raster brick (or stack) and geographical subset在时间子集之后,创建栅格砖(或堆栈)和地理子集
b <- brick('sst.day.mean.2016.v2.nc') # I would change this name to my file with time subest
subset geographically地理子集
b <- crop(b, extent(144, 146, 14, 16))
Finally, I'd like to take the average for each pixel across all my days of data, assign this to a single raster, and make a simple plot...最后,我想对我所有数据天的每个像素取平均值,将其分配给单个栅格,然后绘制一个简单的图...
Thanks for any help and guidance.感谢您的任何帮助和指导。
After b <- brick('sst.day.mean.2016.v2.nc')
, we can type b
to see information of the raster brick.在b <- brick('sst.day.mean.2016.v2.nc')
,我们可以输入b
来查看光栅砖的信息。
b
# class : RasterBrick
# dimensions : 720, 1440, 1036800, 366 (nrow, ncol, ncell, nlayers)
# resolution : 0.25, 0.25 (x, y)
# extent : 0, 360, -90, 90 (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
# data source : C:\Users\basaw\Downloads\sst.day.mean.2016.v2.nc
# names : X2016.01.01, X2016.01.02, X2016.01.03, X2016.01.04, X2016.01.05, X2016.01.06, X2016.01.07, X2016.01.08, X2016.01.09, X2016.01.10, X2016.01.11, X2016.01.12, X2016.01.13, X2016.01.14, X2016.01.15, ...
# Date : 2016-01-01, 2016-12-31 (min, max)
# varname : sst
Notice that the Date
slot has information from 2016-01-01
to 2016-12-31
, which means the Z values already has date information and we can use that to subset the raster brick.请注意, Date
槽具有从2016-01-01
到2016-12-31
,这意味着 Z 值已经具有日期信息,我们可以使用它来对栅格砖进行子集化。
We can use the getZ
function to access the values stored in the Z values.我们可以使用getZ
函数来访问存储在 Z 值中的值。 Type getZ(b)
we can see a series of dates.键入getZ(b)
我们可以看到一系列日期。
head(getZ(b))
# [1] "2016-01-01" "2016-01-02" "2016-01-03" "2016-01-04" "2016-01-05" "2016-01-06"
class(getZ(b))
# [1] "Date"
We can thus use the following code to subset the raster brick.因此,我们可以使用以下代码对光栅砖进行子集化。
b2 <- b[[which(getZ(b) >= as.Date("2016-09-01") & getZ(b) <= as.Date("2016-10-15"))]]
We can then crop the image based on the code you provided.然后我们可以根据您提供的代码裁剪图像。
b3 <- crop(b2, extent(144, 146, 14, 16))
To calculate the average, just use the mean
function.要计算平均值,只需使用mean
函数。
b4 <- mean(b3, na.rm = TRUE)
Finally, we can plot the average.最后,我们可以绘制平均值。
plot(b4)
Not in R, but just to point out that the subsetting and averaging task is easy to do in CDO from the command line:不是在 R 中,而是要指出子集和平均任务很容易在 CDO 中从命令行完成:
cdo timmean -sellonlatbox,lon1,lon2,lat1,lat2 -seldate,date1,date2 in.nc out.nc
where the lon1,lon2 etc define the lon-lat area to cut out and date1,date2 are the date bounds.其中 lon1,lon2 等定义要切出的 lon-lat 区域,而 date1,date2 是日期范围。
You can then read the resultant file into R for plotting, or take a quick look with ncview.然后,您可以将生成的文件读入 R 进行绘图,或使用 ncview 快速查看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.