I need some help for a geospatial data project:
I downloaded this dataset of yearly mean temperatures: https://psl.noaa.gov/data/gridded/data.UDel_AirT_Precip.html (this one) from which I need to data of 1990.
I imported the data into R with the following command:
ras_tempdata_v5 = raster("PATH/air.mon.mean.v501.nc")
and was hoping I could go with something like
tempdata_v5_90 <- raster(ras_tempdata_v5@history == 1990)
(would the "raster" here be needed again or would it automatically be raster data?)
to end up with just the data for 1990. But I have no idea where is the dataset the years are stored and how to access only the data for one specific year.
This is what the dataset looks like in RSTudio.
I also installed Panoply (looked like this) to find out something about the variable sturcture, but that didn't really help me either.
Any help would be highly appreaciated!
CodePudding user response:
I think you can accomplish what you are after by first calling the dataset as a RasterStack, which only reads a little bit of data about the rasters. Then you can look at how the raster layers are named. From there, you will use a regular expression to get the layer numbers for all layers representing 1990. Then simply do a subset select of your rasterStack, and create a rasterBrick from your desired layers. You can then remove the original rasterStack from you environment to save memory if you want. Below is a reproducible example:
library(raster)
path<-"Path_to_your_rasters/air.mon.mean.v501.nc"
test<-stack(path)
lyrs<-names(test)
usethese<-grep(pattern="X1990",lyrs)#My machine reads each layer in with an "X", print lyrs to see if yours does the same.
MMAT<-brick(test[[usethese]])
rm(test)
CodePudding user response:
I would use terra
(the replacement of raster
); perhaps like this:
library(terra)
r <- rast("air.mon.mean.v501.nc")
i <- grep("1990", time(r))
# or something like this but that is more tricky
#i <- which((time(r) > "1989-12-31") & (time(r) < "1990-12-31"))
i
# [1] 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
x <- r[[i]]
time(x)
# [1] "1990-01-01 UTC" "1990-02-01 UTC" "1990-03-01 UTC" "1990-04-01 UTC"
# [5] "1990-05-01 UTC" "1990-06-01 UTC" "1990-07-01 UTC" "1990-08-01 UTC"
# [9] "1990-09-01 UTC" "1990-10-01 UTC" "1990-11-01 UTC" "1990-12-01 UTC"
(Otherwise the same as Sean McKenzie's approach)