1

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

  • Welcome to StackOverflow. Please take the time to read this post on [how to provide a great R example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. – lmo Jun 26 '16 at 12:20

1 Answers1

0

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()
hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • Thank you for the fast answer! Using the ncdf4 package seems to work better! However, I'm still confused about how to set the start time to the 360st month. Using as.Date turns my data into days and not into months. Is there a way to use something like origin="1930-01" (for months), instead of origin="1930-01-01"? Thanks for the help! – Isabell Haag Jun 26 '16 at 19:31
  • Can you post a link to the `FULL_TMP_cru_ts3.23.1901.2014.tmp.dat_65-80E_35-45N.nc` file? – hrbrmstr Jun 26 '16 at 19:48
  • I downloaded the file from this website. I restricted the area to 35-45N and 65-80E and downloaded this restricted subset. https://climexp.knmi.nl/select.cgi?id=someone@somewhere&field=cru_tmp – Isabell Haag Jun 27 '16 at 12:28