[GIS] Plot in rstudio a NetCDF File – latbox by lonbox

netcdfrscatterplot

I have two NetCDF-Files. One of them holds information about the temperature in 2m (t2m) , the other one about outgoing long wave radiation (olr). I want to make a scatter plot. The first part of this exercise is already done: the task was to select a lon-lat-box from each NetCDF-file. It's going from 50N to 70N with -180W to 180 E. That's where the "50N70N comes from in the file name). Both files have a lot of values.

It's the first time I am working with the package ncdf in Rstudio. So what I already have done was to get all the variables from the files. As there is only one variable within the files, this was no problem at all.

Now comes my question: I just tried to plot the two files as a scatter plot, but I have the feeling that the program is plotting for each point all temperature and radiation values. I have too many points. Of course this is not what I want. I want that every box has only one t2m to one olr value.

Can you guys help me how to say in the script that it should only plot one temperature value to one radiation value? How can I say to plot separately box by box ? I hope you guys understand my question. I will upload the current plot.

enter image description here

Maybe I am all wrong and the program is already doing it like I want.
Here are the print.ncdf from both files:

"file olr_50N70N.nc has 3 dimensions:"
"lon Size: 144"
"lat Size: 9"
"time Size: 366"
"————————"
"file olr_50N70N.nc has 1 variables:"
"float var179[lon,lat,time] Longname:var179 Missval:1e+30"

"file t2m_50N70N.nc has 3 dimensions:"
"lon Size: 144"
"lat Size: 9"
"time Size: 366"
"————————"
"file t2m_50N70N.nc has 1 variables:"
"float var167[lon,lat,time] Longname:var167 Missval:1e+30"

Here is my script:

library(ncdf)

library(maps)

t2m_50N70N <- open.ncdf("t2m_50N70N.nc")
float_t2m_50N70N <- get.var.ncdf(t2m_50N70N,"var167")
print.ncdf(t2m_50N70N)

olr_50N70N <- open.ncdf("olr_50N70N.nc")
float_olr_50N70N <- get.var.ncdf(olr_50N70N,"var179")
print.ncdf(olr_50N70N)

plot(float_t2m_50N70N, float_olr_50N70N, type="p", pch=20, col="red", lty=3)

Best Answer

1) It will be vastly easier if this works:

library(raster)
olr <- raster("t2m_50N70N.nc", varname = "var167")
tm2 <- brick("t2m_50N70N.nc", varname = "var179")

plot(olr[[1]])) ## first of nlayers(olr) bands

If this kind of mapping of the data is what you are after, I'd re-focus and work on learning raster. You'll also need ncdf4 package installed.

2) ncdf is no longer supported, you can now use ncdf4 as a safe default (raster does this for you).

3) To use NetCDF in the raw like this, you need to learn the start and count arguments to the "get var" function, in ncdf4 this is called ncvar_get. But, this is really painful since you have to juggle your time-subset across all the variables and work out from other variables what the right subset indexes for the three dimensions must be.

In a limited context, raster does everything for you - it's not general enough for all of NetCDF, but it's worth trying because if your files conform to its assumptions you get a lot for free.