writeRaster vs write.table或.tif vs .csv

时间:2014-03-18 19:18:44

标签: r tiff raster netcdf

有人向我发送了一些R代码,使用netCDFR等来阅读raster文件。该代码创建了一系列*.tif个文件。不幸的是,我对raster*.tif文件或netCDF文件不太熟悉。因此,我尝试更改R代码以编写*.csv个文件。我认为下面的代码以*.tif格式和*.csv格式写入相同的网格单元格数据。但是,我不确定。我希望有人能够验证两种格式的数据是否相同。理想情况下,我希望能够打开*.tif文件并自行进行验证。我怎么能这样做?

如果*.tif文件只包含图像而不是数字,则可能无法进行直接比较。在这种情况下,我想验证*.tif文件中的图片是否与*.csv文件中的数据相对应

R代码下方是*.csv文件的内容,其中包含有关列和行标题的后续问题'。

setwd('c:/users/mark w miller/netCDF/')

my.file <- "my.netCDF.nc"
my.var1 <- "my.variable"

library(ncdf) 
library(rgdal) 
library(chron) 
library(fields) 

file    <- open.ncdf(my.file)
long    <- get.var.ncdf(file, varid="lon")
lat     <- get.var.ncdf(file, varid="lat")
time    <- get.var.ncdf(file, varid="time")
my.varb <- get.var.ncdf(file, varid=my.var1)

#netCDF to raster
library(raster)

r       <- brick(my.file, varname = my.var1)

#Crop spatial coverage
e       <- extent(255,265,35,45)
rc      <- crop(r, e, bylayer=TRUE)

lat2    <- lat[  lat >=  35 & lat  <=  45]
long2   <- long[long >= 255 & long <= 265]

list1   <- unstack(rc)
rs      <- stack(list1)

for(i in 1:5){
     r2 <- 1+(i-1)*12
     s2 <- 2+(i-1)*12
     a2 <- rs[[r2]]
     b2 <- rs[[s2]]
     m2 <- stack(a2,b2)
     my.var <- overlay(m2, fun=function(x,y) {(x+y)}, unstack=TRUE, recycle=FALSE) 
     f2 <- 1999+i

     writeRaster(my.var, filename=paste("my.var", f2, ".tif"), format="GTiff")

     my.var2 <- as.matrix(my.var, nrow=length(lat2), byrow=TRUE)

     write.table(my.var2, file = paste0("my.var", f2, ".csv"), quote = FALSE, sep=",", col.names = FALSE, row.names = FALSE)
}

以下是一个*.csv文件的四舍五入的内容:

1.0,0.9,0.8,0.8,0.7,0.7,0.8,0.8,1.0,1.0
1.0,0.8,0.6,0.5,0.4,0.5,0.7,0.9,1.0,1.0
1.0,0.7,0.5,0.4,0.3,0.4,0.7,1.0,1.0,1.0
0.0,0.5,0.4,0.4,0.4,0.6,1.0,1.0,1.0,1.0
0.0,0.6,0.5,0.4,0.5,0.8,1.0,2.0,2.0,2.0
1.0,0.7,0.6,0.5,0.6,1.0,1.0,2.0,2.0,2.0
1.0,0.9,0.8,0.7,0.9,1.0,2.0,2.0,2.0,2.0
1.0,1.0,1.0,1.0,1.0,2.0,2.0,2.0,1.0,2.0
2.0,1.0,1.0,1.0,2.0,2.0,2.0,1.0,1.0,2.0
1.0,1.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,3.0

鉴于:

lat2
# [1] 44.5 43.5 42.5 41.5 40.5 39.5 38.5 37.5 36.5 35.5

long2
# [1] 255.5 256.5 257.5 258.5 259.5 260.5 261.5 262.5 263.5 264.5

我可以安全地将以下列名和行名添加到每个*.csv文件中吗?

      255.5 256.5 257.5 258.5 259.5 260.5 261.5 262.5 263.5 264.5
44.5  1.0,0.9,0.8,0.8,0.7,0.7,0.8,0.8,1.0,1.0
43.5  1.0,0.8,0.6,0.5,0.4,0.5,0.7,0.9,1.0,1.0
42.5  1.0,0.7,0.5,0.4,0.3,0.4,0.7,1.0,1.0,1.0
41.5  0.0,0.5,0.4,0.4,0.4,0.6,1.0,1.0,1.0,1.0
40.5  0.0,0.6,0.5,0.4,0.5,0.8,1.0,2.0,2.0,2.0
39.5  1.0,0.7,0.6,0.5,0.6,1.0,1.0,2.0,2.0,2.0
38.5  1.0,0.9,0.8,0.7,0.9,1.0,2.0,2.0,2.0,2.0
37.5  1.0,1.0,1.0,1.0,1.0,2.0,2.0,2.0,1.0,2.0
36.5  2.0,1.0,1.0,1.0,2.0,2.0,2.0,1.0,1.0,2.0
35.5  1.0,1.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,3.0

感谢您的任何建议。实际的netCDF文件非常大。如果我可以弄清楚如何对其进行子集化并将其保存为相同的netCDF格式,我可能会尝试将其上传到某处。

修改

下面是创建模拟数据的代码,将这些模拟数据转换为netCDF文件,并按照上面的代码分析该netCDF文件:

setwd('c:/users/mark w miller/netCDF/')

library(raster)
library(ncdf) 
library(rgdal) 
library(chron) 
library(fields)
library(sp)

set.seed(1234)

x = seq( 255, 269, length =  8)
y = seq(  36,  40, length =  5)
xy <- expand.grid(x,y)

z  <- rnorm(nrow(xy), 10, 1)
rc <- data.frame(xy,z)

raster.rc1 <- rasterFromXYZ(rc, res=c(2,1), crs=CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))

z  <- rnorm(nrow(xy), 10, 1)
rc <- data.frame(xy,z)

raster.rc2 <- rasterFromXYZ(rc, res=c(2,1), crs=CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))

z  <- rnorm(nrow(xy), 10, 1)
rc <- data.frame(xy,z)

raster.rc3 <- rasterFromXYZ(rc, res=c(2,1), crs=CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))

z  <- rnorm(nrow(xy), 10, 1)
rc <- data.frame(xy,z)

raster.rc4 <- rasterFromXYZ(rc, res=c(2,1), crs=CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))


all.years <- list(raster.rc1, raster.rc2, raster.rc3, raster.rc4)

#all.rc    <- stack(all.years)

all.rc    <- brick(all.years)

writeRaster(all.rc, filename="example.netCDF.nc", format="CDF", bylayer=TRUE, overwrite=TRUE)


my.file   <- open.ncdf('example.netCDF.nc')
my.file

long   <- get.var.ncdf(my.file, varid="longitude")
lat    <- get.var.ncdf(my.file, varid="latitude")
time   <- get.var.ncdf(my.file, varid="value")
my.var <- get.var.ncdf(my.file, varid="variable")

long
# [1] 255 257 259 261 263 265 267 269

lat
# [1] 40 39 38 37 36

time
# [1] 1 2 3 4

my.var

r       <- brick('example.netCDF.nc', varname = 'variable')

#Crop spatial coverage
e       <- extent(257,267,37,39)
rc      <- crop(r, e, bylayer=TRUE)

lat2    <- lat[  lat >=  37 & lat  <=  39]
lat2
long2   <- long[long >= 257 & long <= 267]
long2

list1   <- unstack(rc)
rs      <- stack(list1)

for(i in 1:2){
     r2 <- 1+(i-1)*2
     s2 <- 2+(i-1)*2
     a2 <- rs[[r2]]
     b2 <- rs[[s2]]
     m2 <- stack(a2,b2)
     my.sim <- overlay(m2, fun=function(x,y) {(x+y)}, unstack=TRUE, recycle=FALSE) 
     f2 <- 2010+i

     writeRaster(my.sim, filename=paste("my.sim", f2, ".tif"), format="GTiff")

     my.sim2 <- as.matrix(my.sim, nrow=length(lat2), byrow=TRUE)

     write.table(my.sim2, file = paste0("my.sim", f2, ".csv"), quote = FALSE, sep=",", col.names = FALSE, row.names = FALSE)
}

1 个答案:

答案 0 :(得分:0)

以下是比较*.tiff个文件和*.csv个文件内容的一般方法。将示例*.csv文件的内容与其*.tiff图表进行比较会让我确信其内容是相同的。

下面我将展示如何在*.tiff文件中显示数据。

setwd('c:/users/mmiller21/netCDF/')

library(raster)

# Here are the contents of 'my.sim2011.csv':
#
# 18.31545067  20.22907639  20.34417152  18.11485672  17.93542576  19.52469158
# 19.20878696  19.43614769  18.41953754  16.42925882  22.05830574  18.31794167
#
# compared with the plot of 'my.sim 2011 .tif'

jpeg(filename = "my.sim.2011.jpeg")

  r <- raster('my.sim 2011 .tif')
  plot(r)
  title(main='my.sim 2011 .tif')

dev.off()

enter image description here

# Here are the contents of 'my.sim2012.csv':
#
# 18.92995739  20.68585968  20.44407845  20.53401566  19.1156435   20.70266819
# 19.04809856  20.76659107  20.50794601  18.52109146  20.92043018  19.91858768
#
# compared with the plot of 'my.sim 2012 .tif'

jpeg(filename = "my.sim.2012.jpeg")

  r <- raster('my.sim 2012 .tif')
  plot(r)
  title(main='my.sim 2012 .tif')

dev.off()

enter image description here

以下是在一个示例*.tiff文件中显示数据的代码。该数据与相应的*.csv文件中的数据匹配。

r <- raster('my.sim 2011 .tif')

r[1,]
[1] 18.31545 20.22908 20.34417 18.11486 17.93543 19.52469

r[2,]
[1] 19.20879 19.43615 18.41954 16.42926 22.05831 18.31794