【问题标题】:Generating a NetCDF from a csv using R使用 R 从 csv 生成 NetCDF
【发布时间】:2021-05-25 22:12:37
【问题描述】:

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

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

【问题讨论】:

  • 欢迎来到 SO。这很可能是由于 m 中的数据顺序错误造成的。但是,如果没有看到原始数据,就很难确定是什么导致了问题
  • 嗨@RobertWilson,我在帖子底部添加了指向该文件的链接。

标签: r csv gis netcdf


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2016-03-12
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多