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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With