在带有ncdf4的R中,打开netcdf文件,进行修改并保存到磁盘

时间:2018-07-10 18:45:04

标签: r netcdf ncdf4

我正在尝试在R中修改并保存netcdf文件。我可以修改值,但是似乎无法获取文件来保存并保留修改后的值。这是我在做什么:

使用ncdf4软件包打开文件

library(ncdf4)
nc=nc_open('RMinit_newvalues2017.nc', write=T)

重新分配缺失值-该文件有2000个变量,大约一半需要修改其缺失值,我可以成功做到这一点:

for (i in 1:10){
   print(nc$var[[i]]$name) # use to match aaR/aaS
   test=grep("_Nums", nc$var[[i]]$name) # search for Nums, skip to next
   if (length(test) ==1){
     next
   }
   test2=grep("_ResN", nc$var[[i]]$name) # search for RN
    if (length(test2) == 1) {
      print(nc$var[[i]]$missval) # print orginal value
      print(xR$RN[which(xR$Variables==nc$var[[i]]$name)]) # print new value
      nc$var[[i]]$missval=xR$RN[which(xR$Variables==nc$var[[i]]$name)] # assign new value
      print(nc$var[[i]]$missval) # print assigned value

    } else if (length(test2) != 1) {
       print(nc$var[[i]]$missval)
       print(xS$SN[which(xS$Variables==nc$var[[i]]$name)])
       nc$var[[i]]$missval=xS$SN[which(xS$Variables==nc$var[[i]]$name)]
       print(nc$var[[i]]$missval)

    }
}

这里有带注释的输出,表明它正在更改'_ResN'和'_StructN'的值:

[1] "Anchovies10_Nums" (nothing done here, next)
[1] "Anchovies10_ResN" (first replacement)
[1] 987.7794 (original value)
[1] 200.1975 (replacement value, printed after assignment)
[1] "Anchovies10_StructN" (repeat pattern...)
[1] 372.7469
[1] 75.54621
[1] "Anchovies1_Nums"
[1] "Anchovies1_ResN"
[1] 130.3665
[1] 15.67022
[1] "Anchovies1_StructN"
[1] 49.19491
[1] 5.913289
[1] "Anchovies2_Nums"
[1] "Anchovies2_ResN"
[1] 228.1891
[1] 34.30677
[1] "Anchovies2_StructN"
[1] 86.10911
[1] 12.94595
[1] "Anchovies3_Nums"

最后,我同步并关闭了nc文件,该文件应将其保存到磁盘。但是这里什么也没有发生。没有错误,一切似乎都很好,只是没有保存文件。我想念什么?

nc_sync(nc)
nc_close(nc)

编辑:在循环中切换命令后,我能够保存文件。 使用ncatt_put而不是分配nc$var[[i]]$missval=...似乎可以解决此问题。我还意识到,我一开始就编辑了错误的值,因为我需要更改_Fillvalue而不是missval。现在可以使用:

for (i in 1:dim(a)[1]){
test=grep("_Nums", nc$var[[i]]$name) # search for Nums, skip to next
if (length(test) ==1){
  next
}
test2=grep("_ResN", nc$var[[i]]$name) # search for RN
if (length(test2) == 1) {
  ncatt_put(nc,a[i,1],"_FillValue", xR$RN[which(xR$Variables==nc$var[[i]]$name)])
} else if (length(test2) != 1) {
  ncatt_put(nc,a[i,1],"_FillValue", xS$SN[which(xS$Variables==nc$var[[i]]$name)])  
}
nc_sync(nc)
}
nc_sync(nc)
nc_close(nc)

1 个答案:

答案 0 :(得分:0)

发布我的解决方案以供参考,以帮助某人...

我暂时解决了我的问题。仍然不确定为什么第一次尝试没有更新文件。我需要使用ncdf4::ncatt_put编辑_FillValue的属性,并使用ncdf4::ncvar_put添加数据。然后在循环中执行nc_sync会有所帮助。

for (i in 1:length(b)){
  vv=as.numeric(b[i]) # index of ResN and StructN in entire list of nc file 'a'
  testSN=grep("_StructN", a[vv,1]) # use to select SN values
  testRN=grep("_ResN", a[vv,1]) # use to select RN values

  if (length(testRN) ==1){
    ncatt_put(nc,a[vv,1],"_FillValue", xR$RN[which(xR$Variables==a[vv,1])])
    ncvar_put(nc,a[vv,1], rep(xR$RN[which(xR$Variables==a[vv,1])],150)) # replicate 5x30
  } else if (length(testSN) ==1){
    ncatt_put(nc,a[vv,1],"_FillValue", xS$SN[which(xS$Variables==a[vv,1])])
    ncvar_put(nc,a[vv,1], rep(xS$SN[which(xS$Variables==a[vv,1])],150))
  }
  nc_sync(nc)
}
nc_sync(nc)
nc_close(nc)