1

I need some help with 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 structure, but that didn't really help me either.

Any help would be highly appreciated!

csmaster
  • 579
  • 4
  • 14

2 Answers2

2

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)

Sean McKenzie
  • 707
  • 3
  • 13
  • Hi! Thanks a lot already, this worked pretty well so far. I didn't realize this set included daily data and I only needed the mean data for 1990, but from what it looks like "mean_1990 <- mean(MMAT)" did the trick! – Jakob Meyer Dec 22 '21 at 12:11
  • Glad it worked for you. Both the raster and terra packages handle raster algebra gracefully using base operators like `mean()` (Robert wrote both packages so he may be able to give you more specifics if you're interested), so I think that line you add should be exactly what you need for either my approach or Robert's. – Sean McKenzie Dec 22 '21 at 20:28
1

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)

Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • Thanks a lot! I went with Sean McKenzies approach which worked fine. Tho I later realized that might have to go with a whole other approach anyways, so your suggestions might come in helpful later on as well! – Jakob Meyer Dec 22 '21 at 16:28