1

I have read in a netcdf file of observed climate data (global surface temperature) to R and would like to plot some species locality data (latitude/longitude coordinate points) over the top of this but am having difficulties doing so.

I have generated my map of temperature using:

obsula.pdclann <- open.ncdf("obsula.pdclann.nc")

temp_var <- "temp_mm_1_5m"

temp <- get.var.ncdf(obsula.pdclann, varid=temp_var)

image.plot(temp)

My species occurrence data are read in via the following:

speciesall <- read.table("C:/Users/Waterson/Documents/UOB/Data/speciesData/P.subrufa.txt", header=T, sep="\t")

coordinates <- cbind(speciesall$longitude, speciesall$latitude)

I have then created a SpatialPointsDataFrame with the coordinates:

speciesall_df <- SpatialPointsDataFrame(coordinates, speciesall, proj4string=CRS("+proj=longlat +ellps=WGS84"))

Now I am unsure of how I go about combining the point data with the netcdf map?

Jaap
  • 81,064
  • 34
  • 182
  • 193
  • Simply `plot(speciesall_df, col=2, add=T)`? – koekenbakker Apr 03 '14 at 09:53
  • Thanks, I have tried that and it doesn't return an error message but I am not able to see my points on the map. – user3493038 Apr 03 '14 at 10:47
  • Is there a way of reducing the lat/long extent of the netcdf map that is plotted? My species occurrence data are just in Africa so I wonder if this is why I am not able to see the point data? Thanks for your help! – user3493038 Apr 03 '14 at 10:49
  • Your points should be visible if they are in the same extent as your image. Check `world(); image.plot(temp, add=T); plot(speciesall_df, add=T, col=2)` to see how both datasets align. – koekenbakker Apr 03 '14 at 11:37
  • Thanks - I am able to plot world and the data points fine, but when using image.plot(temp, add=T) the legend of the temperature plot comes up but nothing else - this is what I used: – user3493038 Apr 03 '14 at 11:53
  • > world() > image.plot(temp_uk, add=T) > plot(speciesall_df, add=T, col=2) > extent(world) class : Extent xmin : -179.9572 xmax : 190.2908 ymin : -59.46889 ymax : 69.86917 > extent(temp_uk) Error in .local(x, ...) : matrix should not have more than 2 columns > extent(speciesall_df) class : Extent xmin : -119.5969 xmax : 47.14225 ymin : -34.11667 ymax : 37.34 > class(temp_uk) [1] "matrix" – user3493038 Apr 03 '14 at 11:53
  • That means your `temp_uk` is missing coordinate information. You have to add those in order to plot the data correctly. Try to extract the coordinates from the NetCDF file and plot like `image.plot(x=lon, y=lat, z=temp_uk)`. – koekenbakker Apr 03 '14 at 11:59
  • Thanks for your help, I have tried using the following but keep getting an error message - is get.var.ncdf the command you would recommend for adding the long/lat values? – user3493038 Apr 04 '14 at 11:13
  • > get.var.ncdf(obsula.pdclann, varid="longitude") Error in R_nc_get_vara_double: Invalid argument Var: longitude Ndims: 1 Start: 0Count: 2160Error in get.var.ncdf(obsula.pdclann, varid = "longitude") : C function R_nc_get_vara_double returned error – user3493038 Apr 04 '14 at 11:14
  • The file structure looks like this if that helps - – user3493038 Apr 04 '14 at 11:14
  • > obsula.pdclann [1] "file obsula.pdclann.nc has 4 dimensions:" [1] "longitude Size: 2160" [1] "latitude Size: 1080" [1] "surface Size: 1" [1] "t Size: 1" [1] "------------------------" [1] "file obsula.pdclann.nc has 12 variables:" – user3493038 Apr 04 '14 at 11:15
  • Try `get.var.ncdf(obsula.pdclann, varid='longitude', start=1, count=-1)`. Is it a large file? Do you have the opportunity to upload it somewhere so I can have a look at it? – koekenbakker Apr 04 '14 at 11:36
  • Thanks - also get the same error with that: Error in R_nc_get_vara_double: Invalid argument Var: longitude Ndims: 1 Start: 0Count: 2160Error in get.var.ncdf(obsula.pdclann, varid = "longitude", start = 1, : C function R_nc_get_vara_double returned error – user3493038 Apr 04 '14 at 12:19
  • I will see if it is possible to upload the file! – user3493038 Apr 04 '14 at 12:19
  • Unfortnately I cannot upload it as I do not own the right to the climate model..are there any other details of the file that might be useful to look at? Thanks for your help – user3493038 Apr 04 '14 at 13:30
  • I'm afraid I can't help you any further than. Download ncBrowse to look at your NetCDF file. Perhaps you can figure out the lat/lon extent that way and recreate the coordinates in R (http://www.epic.noaa.gov/java/ncBrowse/). – koekenbakker Apr 04 '14 at 13:40

0 Answers0