Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Link to file: here

library(raster)
library(ncdf4)

open the netcdf after setting my working directory

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...

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.

like image 700
Peter Houk Avatar asked Dec 28 '17 17:12

Peter Houk


People also ask

How do I open and work with netCDF data in R?

You can read netCDF data in R using the ncdf4 package. You can use the nc_open function from the ncdf4 package to open a connection to a netCDF file.

Is netCDF a raster?

By design, netCDF (network Common Data Form) files storing multidimensional scientific data (variables) must be converted to raster layers before they can be projected in ArcMap. A projected netCDF file shows the variables at the most accurate location.

What is a raster brick?

A RasterBrick is a multi-layer raster object. They are typically created from a multi-layer (band) file; but they can also exist entirely in memory. They are similar to a RasterStack (that can be created with stack ), but processing time should be shorter when using a RasterBrick.


1 Answers

After b <- brick('sst.day.mean.2016.v2.nc'), we can type b to see information of the raster brick.

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.

We can use the getZ function to access the values stored in the Z values. Type getZ(b) we can see a series of dates.

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.

b4 <- mean(b3, na.rm = TRUE)

Finally, we can plot the average.

plot(b4)

enter image description here

like image 196
www Avatar answered Oct 20 '22 18:10

www