0

To proceed my study, I downloaded the NCAR datasets from the CMIP website, including tasmax, tasmin, pr, and rsds variables. However, I cannot read these datasets properly using R codes though those outcomes that look wrong can be obtained. The NCAR datasets seem to be very different from other CMIP ones, in which my R codes can be operated smoothly. Here is my R codes: library(ncdf4) library(raster) library(sp)

# read ncdf file
nc<-nc_open('D:/Study/Data/CMIP6/tasmax/tasmax_Amon_CESM1-CAM5-SE-HR_highres-future_r1i1p1f1_gn_201501-205012.nc')
v <- nc$var[[1]]
varsize <- v$varsize
ndims <- v$ndims
nt <- varsize[ndims] # Remember timelike dim is always the LAST dimension!
lat=nc$dim$ncol$vals
lon=nc$dim$ncol$vals
r<-list()
for (i in 1:nt) {
  start <- rep(1,ndims)     # begin with start=(1,1,...,1)
  start[ndims] <- i             # change to start=(1,1,...,i) to read    timestep i
  count <- varsize                # begin with count=(nx,ny,...,nt), reads entire var
  count[ndims] <- 1             # change to count=(nx,ny,...,1) to read 1 tstep
  dt=matrix(ncvar_get(nc, v, start = start, count = count),nrow = 1247,ncol=624,byrow=TRUE)

  # convert to raster
  r[i]<-raster(dt)
}
# create layer stack with time dimension
r<-stack(r)
rt=t(r)

extent(rt)<-extent(c(range(lon), range(lat)))
crs(rt) <- "+proj=longlat"
writeRaster(rt,"D:/Study/Data/CMIP6/tasmax/tasmax_Amon_CESM1-CAM5-SE-HR_highres- 
future_r1i1p1f1_gn_201501-205012.tif",format="GTiff",overwrite=TRUE)

The following are the data format of this nc file and comparison with other nc file that can be read properly.

dimensions (NCAR file):
nbnd = 2;
grid_corners = 5;
ncol = 777602;
time = UNLIMITED;   // (432 currently)

dimensions (other nc file):
axis_nbounds = 2;
lat = 360;
lon = 720;
time = UNLIMITED;   // (120 currently)

How can I address this?

zongxu li
  • 1
  • 2
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Dec 19 '21 at 08:30

1 Answers1

1

You are not saying what does not work, you show no error messages, and you are not providing a link to the file (e.g. google drive), making it rather hard to help.

Also, why you use such a complex approach to reading ncdf files? With the raster package you should be able to do

library(raster)
rt <- brick("tasmax_Amon_CESM1-CAM5-SE-HR_highres-future_r1i1p1f1_gn_201501-205012.nc")

But I would recommend terra (the replacement for raster) and do

library(terra)
rt <- rast("tasmax_Amon_CESM1-CAM5-SE-HR_highres-future_r1i1p1f1_gn_201501-205012.nc")
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • Thank you, Robert. You gave me a very simple and valuable method to read the nc file. This method is good for other data sources, but for this dataset, I still received some warning messages. When I used the stack or brick function in the raster package, the message is Error in .rasterObjectFromCDF(x, type = objecttype, band = band, ...) : cells are not equally spaced; you should extract values as points. In addition: Warning message: In .varName(nc, varname, warn = warn) : varname used is: tasmax If that is not correct, you can set it to one of: tasmax, time_bnds, lat_bnds, lon_bnds – zongxu li Dec 14 '21 at 08:30
  • For the rast function in the terra package, the warning message is [rast] GDAL did not find an extent. Cells not equally spaced? How can I avoid this? – zongxu li Dec 14 '21 at 08:36
  • Here is the google drive link for this nc file: https://drive.google.com/file/d/1cUClJQkxPvzTS7FPldALBb6ueEvq-DOa/view?usp=sharing Thanks! – zongxu li Dec 14 '21 at 11:28
  • the file is not accessible – Robert Hijmans Dec 14 '21 at 17:14
  • Sorry, I ignored the accessibility of this link. Now, I have shared this file with anyone. Hope this works. https://drive.google.com/file/d/1cUClJQkxPvzTS7FPldALBb6ueEvq-DOa/view?usp=sharing – zongxu li Dec 15 '21 at 14:24