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.
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.
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.