netCDF files in R

我与影子孤独终老i 提交于 2019-11-29 08:49:12

When you extract your variable, you need to specify which dimensions you want. Currently you're asking R to get everything and so I suspect it's creating a 3D array which will likely be enormous.

The ncdf4 package generally supersedes ncdf, you should try using that instead. You need to decide if you want to read data by location for time or by time step for location. This is easier to envisage on a plain 2D grid:

  • Single cell at all time steps
  • All locations single time step

Yours is a 3D grid through time (albeit with the 3rd dimension only two bands), however it looks like your variable isn't using the bands dimension. Here's a 2D workflow based on ncdf4, ignoring your bands:

Package:

install.packages("ncdf4")
library(ncdf4)

Open connection:

nc = nc_open("~/dir/dir/file.nc")

For a grid at one time step

Read dimensions:

precip = list()
precip$x = ncvar_get(nc, "lon")
precip$y = ncvar_get(nc, "lat")

Read data (note start is the index in dimensions to begin and count is how many observations from that point, so here we read the whole grid at the first time step):

precip$z = ncvar_get(nc, "precip", start=c(1, 1, 1), count=c(-1, -1, 1))
# Convert to a raster if required
precip.r = raster(precip)

To read a single cell at all time steps

You need to find your cell index, precip$x and precip$y will help. Once you have it (e.g. cell x=5 and y=10):

precip.cell = ncvar_get(nc, "precip", start=c(5, 10, 1), count=c(1, 1, -1))

(a) memory:

If on a linux box [sudo apt-get install cdo] (or also windows with cygwin installed) you can use cdo to help you out.

For example, if you are only interested in a specific date you can select that first to keep the file size down:

cdo seldate,date in.nc out.nc 

or you might have wanted to view the time mean:

cdo timmean in.nc out.nc 

That will keep the file size down, and you can then open it in R to make your plot (or use ncview for a quickview investigation).

(b) remapping

cdo can also interpolate the file to 0.25 degrees, (although I am not sure why you would want to do this, as you are not adding any information and you are making the files four times larger!!!)

cdo remapcon,r1440x720 in.nc out.nc

or

cdo remapnn,r1440x720 in.nc out.nc

But as I said, if you want to interpolate to compare to another 0.25 degree product (e.g. TRMM), better to go the other way and interpolate the finer dataset to 0.5 degree.

By the way, in 2015, v7 was released of GPCC, still at 0.5 degree.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!