Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The variable from a netcdf file comes out flipped

Tags:

r

netcdf

I have downloaded a nc file from

f=open.ncdf("file.nc")
[1] "file Lfile.nc has  2 dimensions:"
[1] "Longitude   Size: 1440"
[1] "Latitude   Size: 720"
[1] "------------------------"
[1] "file filr.nc has   8 variables:"
[1] "short ts[Latitude,Longitude]  Longname:Skin Temperature (2mm) Missval:NA"

I then wanted to work with the variable soil_moisture_c

A = get.var.ncdf(nc=f,varid="soil_moisture_c",verbose=TRUE)

I then plot A with image(A). I got the map shown below,I even transposed it image(t(a)) but that was changed to other direction,and not how it should be. Anyway,in order to know what is wrong,I used the netcdf viewer Panoply and the map was correctly plotted as you can see below.

like image 486
sacvf Avatar asked Dec 04 '12 10:12

sacvf


People also ask

Is netCDF a vector?

The netCDF vector driver supports reading and writing netCDF files following the Climate and Forecast (CF) Metadata Conventions. Vector datasets can be written using the simple geometry specification of the CF-1.8 convention, or by using the CF-1.6 convention and by writing non-point geometry items as WKT.


1 Answers

The reason is that the NetCDF interface you are using is very low-level, and all you have done is read out the variable without any of its dimension information. The orientation of the grid is really arbitrary, and the coordinate information needs to be understood in a particular context.

library(raster) ## requires ncdf package for this file  
d <- raster("LPRM-AMSR_E_L3_D_SOILM3_V002-20120520T185959Z_20040114.nc", varname = "soil_moisture_c")

(I used a different file to yours, but it should work the same).

It turns out that even raster does not get this right without work, but it does make it easy to rectify:

d <-  flip(t(d), direction = "x")

That transposed the data and flipped around "x" (longitude), keeping the georeferencing from the original context.

Plot it up with a map from maptools to check:

plot(d)

library(maptools)
data(wrld_simpl)
plot(wrld_simpl, add = TRUE)

There are many other ways to achieve this by reading the dimension information from the file, but this is at least a shortcut to do most of the hard work for you.

plot of image from raster

like image 53
mdsumner Avatar answered Sep 18 '22 21:09

mdsumner