0

相关的 NCDF 文件在这里:https ://www.ncdc.noaa.gov/paleo-search/study/19419

我有一个 NCDF 文件,并且正在使用以下循环首先将每个文件保存为 CSV 文件,然后保存为 shapefile:

for (m in 1:500){
    #First I want to save my CSV files:

    drought.slice <- rotate(drought.array[m,,])
    drought.vec <- as.vector(drought.slice)
    length(drought.vec)
    drought.df01 <- data.frame(cbind(lonlat, drought.vec))
    names(drought.df01) <- c("lon", "lat", paste(dname, as.character(m), sep = "_"))
    head(na.omit(drought.df01))
    csvfile<-paste0("cru_drought_",m,".csv")

    #Then, I want to make some shapefiles:

    plot.locations_DROUGHT <- read.csv(paste0("cru_drought_",m,".csv"), stringsAsFactors = FALSE)
    plot.locationsSP_DROUGHT <-SpatialPointsDataFrame(plot.locations_DROUGHT[,1:2], plot.locations_DROUGHT)
    proj4string(plot.locationsSP_DROUGHT) <- CRS("+init=epsg:4326")
    dsn <- layer1 <- gsub(".csv","_shape",csvfile)
    shapefile(plot.locationsSP_DROUGHT, dsn, overwrite=TRUE)
    print(dsn)
    }

但是,当我运行上面的代码时,它给了我以下错误:

Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
  cannot open file 'cru_drought_4.csv': No such file or directory
>

我做错了什么?我不明白发生了什么,我尝试了各种代码组合和排列。是否应该通过上述代码创建文件“cru_drought_4.csv”?

以下是完整代码,供参考:

#Open and read the NCDF file, along with longitude and latitude
rm(list=ls())
library(lattice)
library(ncdf4)
library(chron)
library(rgdal)
library(sp)
library(raster)
library(RColorBrewer)
setwd('/Users/C/Drought Maps/Drought datafiles')
ncname <- "owda-orig"
ncfname <- paste(ncname,".nc",sep="")
dname <- "pdsi"
ncin <- nc_open(ncfname)
print(ncin)

#Assign longitude
lon <- ncvar_get(ncin, "lon")
nlon <- dim(lon)
head(lon)

#Assign latitude
lat <- ncvar_get(ncin, "lat", verbose = F)
nlat <- dim(lat)
head(lat)

print(c(nlon, nlat))

#Assign the time variable
t <- ncvar_get(ncin, "time")
nt <- dim(t)
head(t)

#Grab the PDSI drought data
drought.array <- ncvar_get(ncin, dname)
dlname <- ncatt_get(ncin, dname, "long_name")
dunits <- ncatt_get(ncin, dname, "units")
#fillvalue <- ncatt_get(ncin, dname, "_FillValue")
dim(drought.array)

creation_date <- ncatt_get(ncin, 0, "creation_date")
Description <- ncatt_get(ncin, 0, "Description")

nc_close(ncin)



#Check that the data actually print the drought data you need:
rotate <- function(x) t(apply(x, c(1, 2), rev))

m <- 304
drought.slice <- rotate(drought.array[m,,])
image(lon, lat, drought.slice, col = brewer.pal(10, "BrBG"))

lonlat <- expand.grid(lon, lat)
drought.vec <- as.vector(drought.slice)
length(drought.vec)

drought.df01 <- data.frame(cbind(lonlat, drought.vec))
names(drought.df01) <- c("lon", "lat", paste(dname, as.character(m), sep = "_"))
head(na.omit(drought.df01))

    ########################################################################################
#Write a loop to save the data in CSV and shapefile formats:

for (m in 1:500){
    #First I want to save my CSV files:

    drought.slice <- rotate(drought.array[m,,])
    drought.vec <- as.vector(drought.slice)
    length(drought.vec)
    drought.df01 <- data.frame(cbind(lonlat, drought.vec))
    names(drought.df01) <- c("lon", "lat", paste(dname, as.character(m), sep = "_"))
    head(na.omit(drought.df01))
    csvfile<-paste0("cru_drought_",m,".csv")

    #Then, I want to make some shapefiles:

    plot.locations_DROUGHT <- read.csv(paste0("cru_drought_",m,".csv"), stringsAsFactors = FALSE)
    plot.locationsSP_DROUGHT <-SpatialPointsDataFrame(plot.locations_DROUGHT[,1:2], plot.locations_DROUGHT)
    proj4string(plot.locationsSP_DROUGHT) <- CRS("+init=epsg:4326")
    dsn <- layer1 <- gsub(".csv","_shape",csvfile)
    shapefile(plot.locationsSP_DROUGHT, dsn, overwrite=TRUE)
    print(dsn)
    }
4

0 回答 0