I have around 100 stations of data spread over around 60 deg lon x 50 deg lon which I want to save as a netcdf using R. Do I have to make a netcdf with all grid points with non-stations being NAN or can I just store the stations (with positions) as netcdf and plot lon/lat wise when needed on a map? I need to make a NETCDF so that I can plot this dataset using other software like GRADS or compare with topography etc.

  • Why do you want to save it as a netcdf? If it is just station data (x,y,m) then another file format might be better. – Spacedman Aug 21 '17 at 10:44
  • netcdf so that I can plot using other plotters such as grads if required. – Trina Aug 21 '17 at 11:05
  • if you want to compare data sampled at points across an area defined by a grid you need to interpolate your sampled points to the grid first. – Spacedman Aug 21 '17 at 15:53
  • I know that and that's doable – Trina Aug 22 '17 at 3:16
  • Okay, so does my answer below answer your question? – Spacedman Aug 22 '17 at 9:19

If you have netCDF support in your rgdal package you can do this.

First, some test data:

> library(sp)
> library(rgdal)
> library(raster)
> pts = data.frame(x=runif(50),y=runif(50),z1=rnorm(50),z2=rnorm(50))
> coordinates(pts)=~x+y
> projection(pts)="+init=epsg:4326"

Then use something like this:

> writeOGR(pts,"/tmp/test.nc",layer="pts", driver="netCDF")

This is not a grid, it is the raw x,y,z1,z2 data points.

> library(ncdf4)
> nc = nc_open("/tmp/test.nc")
> print(nc)

will show you the structure of the ncdf file.

  • This will solve the problem for plotting in R as lat, lon are separate variables being stored. But this file is not plottable in other software like GRADS. – Trina Aug 23 '17 at 8:20

A netcdf file do not have to be a grid. This can be a list of positions not regularly spread. You will have a vector of longitude, a vector of latitude and a vector of observations.
To plot it in R, you will need to use library ncdf4 for instance and extract vars of lat, lon and obs to create a spatial object to be plotted.

  • But can I plot Lat, lon, time and value (in color) in a 4-d plot from an ungridded netcdf? – Trina Aug 21 '17 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.