0

関連する NCDF ファイルはこちら: https://www.ncdc.noaa.gov/paleo-search/study/19419

私はNCDFファイルを持っており、次のループを使用して、最初に各ファイルをCSVファイルとして保存し、次にシェープファイルとして保存しています:

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