使用r从csv生成NetCDF



我有一个.csv文件,其中包含大西洋的经纬度坐标。我将每个坐标与1相关联。我的目标是生成这些数据的Netcdf,以便绘制它。下面是.csv文件的一个片段:

lat     lon    value
24.75  -97.25    1
24.25  -97.25    1
23.75  -97.25    1
23.25  -97.25    1
22.75  -97.25    1
22.25  -97.25    1
27.25  -96.75    1
26.75  -96.75    1
26.25  -96.75    1
25.75  -96.75    1
25.25  -96.75    1

我使用这段代码生成了。csv文件的Netcdf文件:

# read in csv file
atlantic <- read.csv(file = 'atlantic.csv')
# define dimensions
xvals <- unique(atlantic$lon)
xvals <- xvals[order(xvals)]
yvals <- unique(atlantic$lat)
yvals <- yvals[order(yvals)]
lon1 <- ncdim_def("longitude", "degrees_east", xvals)
lat2 <- ncdim_def("latitude", "degrees_north", yvals)
# define variables
mv <- -999 # missing value to use
var_value <- ncvar_def("Atlantic", "1",
list(lon1, lat2),
longname="Atlantic area", mv)
# add data
ncnew <- nc_create(filename = "atlantic.nc",list(var_value))
# create the indices for the dimensions
atlantic$idx_lon <- match(atlantic$lon,xvals)
atlantic$idx_lat <- match(atlantic$lat,yvals)
# create an array with the dimensions appropriate for the data
m <- array(mv,dim = c(length(yvals),length(xvals)))
# fill the array with the values
for(i in 1:NROW(atlantic)){
m[atlantic$idx_lat[i],atlantic$idx_lon[i]] <- atlantic$value[i]
}
# write the data and close
ncvar_put(ncnew, var_value, m)
nc_close(ncnew)

然而,当我使用Panoply绘制Netcdf时,它看起来很奇怪。

Atlantic_map

为什么会有一堆水平线?我希望它是连续的。我遗漏了什么?

.csv数据文件的链接:atlantic

水平条纹是原始数据被拉伸以填充其原始网格的结果。我通过在panoply上绘图之前将原始数据映射到360度x 180度阵列来解决这个问题。

最新更新