读取Netcdf文件时指定纬度尺寸时出错

时间:2013-07-25 10:54:18

标签: r

我想阅读两个netcdf文件(气候数据两年)并将一个网格(lon [15]和lat [15])的数据组合到一个数组(名为prec_year),下面的脚本。

我收到了一个错误:

"Error in R_nc_get_vara_double: NetCDF: Index exceeds dimension bound
Var: precip  Ndims: 3   Start: 0,4294967284,59Count: 366,1,1Error in get.var.ncdf   (ex.nc, "precip", start = c(lon[1], lat[15], 1),  : 

C函数R_nc_get_vara_double返回错误“

但是当我没有指定lat []时,例如说“start = c(lon [1],1,1)”,它就可以了。请帮助(1)为什么我收到此错误,以及(2)如何修复它?

非常感谢!     --------------------

setwd ("D://Historic Climate data/Aphrodyte/Prec_025/join 2 years")
Prec_origin <- c(1:2)
filenames <- list.files(pattern = NULL, all.files = FALSE)
 for(i in filenames){
filepath <- file.path("D://Historic Climate data/Aphrodyte/Prec_025/join 2 years/",paste    (i,sep=""))
    ex.nc = open.ncdf (filepath, readunlim=FALSE)
    lon = get.var.ncdf( ex.nc, "longitude")          # coordinate variable
  lat = get.var.ncdf( ex.nc, "latitude")          # coordinate variable
  day =  get.var.ncdf( ex.nc, "time")            # time variable
  #change lon[x] and lat[y] to select different files/locations for comparison
  prec_year = get.var.ncdf(ex.nc, "precip", start = c(lon[1],lat[15],1), count = c(1,1,-1))
    Prec_origin <- c(Prec_origin, prec_year)  #combine all data into one array
  close.ncdf (ex.nc)
 }
 Prec_origin <- Prec_origin[-(1:2)] # delete first two rows-
--------------
[1] "file D://Historic Climate data/Aphrodyte/Prec_025/join 2     years//APHRO_MA_025deg_V1003R1.1952.nc has 3 dimensions:"
[1] "time   Size: 366"
[1] "longitude   Size: 360"
[1] "latitude   Size: 280"
[1] "------------------------"
[1] "file D://Historic Climate data/Aphrodyte/Prec_025/join 2 years//APHRO_MA_025deg_V1003R1.1952.nc has 2 variables:"
[1] "float precip[longitude,latitude,time]  Longname:daily precipitation analysis interpolated onto 0.25deg grids [mm/day] Missval:-99.9000015258789"
[1] "float rstn[longitude,latitude,time]  Longname:ratio of 0.05deg-grids with station [%]     Missval:-99.9000015258789"

1 个答案:

答案 0 :(得分:2)

在&#34;开始&#34;您需要定义索引号的函数,而不是您希望从哪里开始的实际值。

我相信这正是您所寻找的:

prec_year = get.var.ncdf(ex.nc, "precip", start = c(1,15,1), count = c(1,1,-1))

lon [1]可能不会产生错误,因为它会为您提供一个可以作为索引的坐标(经度为1:360)。但是,输出并不是你想要的(如果我理解你很好的话)。

作为信息,如果您想根据预先选择的坐标选择区域,可以按照以下方式执行此操作(可能不是最短的代码):

lon_select=which(x<=10 & x>=20)
lat_select=which(y<=40 & y>=50)

precip=get.var.ncdf(ex.nc,"precip",start=c(lon_select[1],lat_select[1],1), count=c(length(lon_select),length(lat_select),-1))