[GIS] How to deal with two dimensional lat and lon variables in a NetCDF file in R

data-framenetcdfr

I'm trying to convert a NetCDF file to a dataframe in R using the RNetCDF package. The NetCDF file is very different from what I have experience with, in that the lat and lon values each have two dimensions:

data<-open.nc("myfile.nc")

lon<-var.get.nc(data,"lon")

dim(lon)

[1] 232 196

and also when I inquire about "lon":

var.inq.nc(data,"lon")

$id

[1] 1

$name

[1] "lon"

$type

[1] "NC_DOUBLE"

$ndims

[1] 2

$dimids

[1] 1 0

$natts

[1] 4

and here is the summary related to lat and lon:

print.nc(data)

dimensions:

    south_north = 196 ;

    west_east = 232 ;

    time = UNLIMITED ; // (2920 currently)

variables:

    double lat(west_east, south_north) ;

            lat:units = "degrees_north" ;

            lat:long_name = "latitude coordinate" ;

            lat:standard_name = "latitude" ;

            lat:description = "Latitude, South is negative" ;

    double lon(west_east, south_north) ;

            lon:units = "degrees_east" ;

            lon:long_name = "longitude coordinate" ;

            lon:standard_name = "longitude" ;

            lon:description = "Longitude, West is negative" ;

I don't understand what the two dimensions mean and how I can convert them into a vector and use it to create a data frame. Has anyone had experience with a data set like this? What is a convenient way to deal with it in R?

Best Answer

The short answer is that if you can use raster package to read these as index-referenced layers then you can simply treat the coordinates as "values". Raster still makes this much easier than juggling NetCDF calls, but you need to know what's going on.

It may be that the coordinate values are actually just stored redundantly, and you can collapse to the normal affine-transform assumed by raster. It's not as unlikely as it sounds, since NetCDF is not geared to using an affine-transform and often used to store 1D-x/y coordinate arrays even if they are redundant.

If they really are curvilinear, then there may be a map projection interpretation that allows treating these as regular affine grids - but that's getting pretty expertise-needing.

Unfortunately this is generally not well understood or explained. NetCDF tends to be used in "raw array" form and these more GIS-like concepts are ignored.

I've got tools to work with stuff like this but it's not ready for general use, happy to explore your data along these lines if you can point to a file?

Related Question