【问题标题】:set Coordinate System when writing netcdf编写netcdf时设置坐标系
【发布时间】:2013-08-07 06:18:34
【问题描述】:

我在 R 中有一个 SpatialPointsDataFrame,我想将其保存为 netCDF。我可以在格式化方面使用一些帮助。

> str(swe)
Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
  ..@ data       :'data.frame': 3487 obs. of  5 variables:
  .. ..$ site   : Factor w/ 6 levels "Dry Lake","Joe Wright",..: 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ depth  : num [1:3487] 151 157 138 155 145 ...
  .. ..$ density: num [1:3487] 0.37 0.37 0.37 0.37 0.37 0.37 0.37 0.37 0.37 0.37 ...
  .. ..$ swe.obs: num [1:3487] 0.56 0.582 0.512 0.572 0.535 ...
  .. ..$ date   : Date[1:3487], format: "2008-04-04" "2008-04-04" ...
  ..@ coords.nrs : num(0) 
  ..@ coords     : num [1:3487, 1:2] -107 -107 -107 -107 -107 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : NULL
  .. .. ..$ : chr [1:2] "utm.e" "utm.n"
  ..@ bbox       : num [1:2, 1:2] -107.9 37.8 -104.1 43.8
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:2] "utm.e" "utm.n"
  .. .. ..$ : chr [1:2] "min" "max"
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
  .. .. ..@ projargs: chr "+proj=longlat +datum=NAD83 +ellps=GRS80 +towgs84=0,0,0"

我有一个不规则的date 字段,所以我认为我不能将它用作维度。这些是对雪的现场测量,因此调查在两个不同的年份大约每月一次。但是,我确实需要包含每次测量的日期和站点名称。我还需要保留投影信息。以下是我目前所拥有的:

require(ncdf4)
varlat=ncdim_def(name='latitude',units='deg',vals=coordinates(swe)[,2])
varlong=ncdim_def(name='longitude',units='deg',vals=coordinates(swe)[,1])
varswe=ncvar_def(name='swe.obs',units='meters',dim=list(varlat,varlong),missval=-9999)
varsite=ncvar_def(name='site',units='site',dim=list(varlat,varlong),missval=-9999)
vardate=ncvar_def(name='date',units='day',dim=list(varlat,varlong),missval=-9999)
new.nc=nc_create('snow.survey.nc',vars=list(varswe,varsite,vardate))

但是当我尝试使用以下内容填充值时:

 ncvar_put(new.nc,varid=varswe,vals=swe$swe.obs)
Error in ncvar_put(new.nc, varid = varswe, vals = swe$swe.obs) : 
  ncvar_put: error: you asked to write 12159169 values, but the passed data array only has 3487 entries!

这里是 new.nc:

> new.nc
[1] "File snow.survey.nc (NC_FORMAT_CLASSIC):"
[1] ""
[1] "     3 variables:"
[1] "        float swe.obs[latitude,longitude]   "
[1] "            units: meters"
[1] "            _FillValue: -9999"
[1] "        float site[latitude,longitude]   "
[1] "            units: site"
[1] "            _FillValue: -9999"
[1] "        float date[latitude,longitude]   "
[1] "            units: day"
[1] "            _FillValue: -9999"
[1] ""
[1] "     2 dimensions:"
[1] "        latitude  Size:3487"
[1] "            units: deg"
[1] "            long_name: latitude"
[1] "        longitude  Size:3487"
[1] "            units: deg"
[1] "            long_name: longitude"

另外,如何在 netcdf 文件中定义投影字符串?在这种情况下 '+proj=longlat +datum=NAD83' 所以另一个用户知道他们在处理什么? 谢谢

更新1: 我根据@Spacedman 尝试过这个,但我仍然得到一个错误。我是否需要为我的坐标范围制作一个规则网格,然后尝试将其用作我的维度范围?在我没有数据点的任何地方,我都不会填写 NA。我也没有均匀间隔的测量值。

coords=cbind(coordinates(swe)[,1],coordinates(swe)[,2])
nccoords=ncdim_def(name='coords',units='site',vals=coords)
varswe=ncvar_def(name='swe.obs',units='meters',dim=nccoords,missval=-9999,longname='survey points')

创建新的“new.nc”目前简化为一个变量。

new.nc=nc_create('snow.survey.nc',vars=varswe)
ncvar_put(new.nc,varid=varswe,vals=swe$swe.obs)
Error in ncvar_put(new.nc, varid = varswe, vals = swe$swe.obs) : 
  ncvar_put: error: you asked to write 6974 values, but the passed data array only has 3487 entries!

【问题讨论】:

    标签: r netcdf


    【解决方案1】:

    您的维度有所不同。您的经纬度数据应保存在 3487x2 维度变量中,您的属性应保存在 3487x1 变量中。您已经创建了尺寸为 3487x3487 的东西(varswe、varsite...)。这不是网格。

    如果您的点在网格上(如果完整,则必须是 317x11 网格),那么您需要获取唯一的纬度和经度值并为它们创建长度为 317 和 11 的维度。

    【讨论】:

    • hmmm @Spacedman 我以为我明白了,但仍然遇到类似的错误(请参阅问题,UPDATE1)
    猜你喜欢
    • 2021-11-07
    • 1970-01-01
    • 2021-08-09
    • 2019-07-30
    • 2014-06-13
    • 2012-10-02
    • 1970-01-01
    • 2017-11-21
    • 2012-09-06
    相关资源
    最近更新 更多