1
votes

My general aim is to create a graph, where temperature is plotted against time. I downloaded gridded climate data (NetCDF file) for my research area, containing Long, Lat, Temperature Value and Time, for each grid cell. I imported the file in R using RNetCDF. That's to code I used for that:

Grid_Temp <- "FULL_TMP_cru_ts3.23.1901.2014.tmp.dat_65-80E_35-45N.nc"
fid_T <- open.nc (Grid_Temp)
print.nc(fid_T)
Temp <- read.nc(fid_T)
close.nc(fid_T)

As a result, I get a list with 4 elements: time (1d), lon (1D), lat (1D), and tmp (3D:Long,Lat,Time).

I don't know how I can use this output to produce my plot. First of all, I want to limit the time element. At the moment time is going from 1901-2014. However, I need to start with 1930. Time is in months, meaning the time element contains numbers from 0:1368, as months are numbered consecutively. I need to start at number 360. Could anyone help me, how I can restrict my time?

And for the plot: I need the time (1930-2014) on the x-axis and on the y-axis should be the mean temperature of the research area. I don't know at the moment how to deal with this kind of list and therefore, don't know how I can create this plot.

Thanks a lot for every answer! I am doing this for my Master thesis and I am a beginner in R, so I appreciate every kind of help and tips! Thank you

1
Welcome to StackOverflow. Please take the time to read this post on how to provide a great R example as well as how to provide a minimal, complete, and verifiable example and revise your question accordingly. - lmo

1 Answers

0
votes

Try switching to the ncdf4 pkg and seeing if this helps:

library(ncdf4)
library(ggplot2)
library(dplyr)

# open the HadCRUT temp netcdf
had <- nc_open("HadCRUT.4.4.0.0.median.nc")

print(had) # not shown

# get the data

lon <- ncvar_get(had, "longitude")
lat <- ncvar_get(had, "latitude")
time <- ncvar_get(had, "time")
temperature_anomaly <- ncvar_get(had, "temperature_anomaly")

nc_close(had) # done with the file

# ref origin for this one is diff than 
time <- as.Date(time, origin="1850-01-01")

# pick a lat/lon pair

(lon[10])
## [1] -132.5

(lat[10])
## [1] -42.5

data_frame(month=time,
           temp=temperature_anomaly[10,10,],
           col=ifelse(temp<0, "#b2182b", "#2166ac")) %>% 
  ggplot(aes(month, temp)) +
  geom_point(aes(color=col), size=0.15) +
  scale_color_identity() +
  theme_bw()