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?
1 Answer 1
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?
-
@mdsummer, I have the same problem. How can you help me?Raimundo– Raimundo2018年08月01日 19:39:40 +00:00Commented Aug 1, 2018 at 19:39
-
I'm only here to answer questions.mdsumner– mdsumner2018年08月04日 13:35:50 +00:00Commented Aug 4, 2018 at 13:35
-
simple question: how can you plot the Chlor_a band of this: oceandata.sci.gsfc.nasa.gov/cgi/getfile/…, as lat and lon variables have more than 1 dimension.Raimundo– Raimundo2018年08月10日 19:35:23 +00:00Commented Aug 10, 2018 at 19:35
-
1That's pretty tough to do because it's L2, and these are arrays of geophysical variables from swaths. Every data point has a corresponding lon-lat but they are effectively also data, there's no grid mapping as such.mdsumner– mdsumner2018年08月13日 03:34:08 +00:00Commented Aug 13, 2018 at 3:34
-
I see, thanks so much. Is there any good example out there on how to work with these images, for example get the statistics of the surroundings of a point, etc?Raimundo– Raimundo2018年08月15日 15:25:50 +00:00Commented Aug 15, 2018 at 15:25