我在 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!
这是新的.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' 所以另一个用户知道他们在处理什么?谢谢
UPDATE1:我根据@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!