CHIRPS data for variable in a loop using R - r

This program must download the CHIRPS data according to the detail entered, and then assign each year of information in a variable 'nc' and export it as '.csv'. However, the code does not work and I received the message "R Session Failed Failed Error" in RStudio.
### Importing libraries
library(parallel)
library(sp)
library(raster)
library(heavyRain)
### Working directory
setwd("D:/data-science_hydrology/g8/")
### Download CHIRPS monthly
data_CHIRPS <- getCHIRPS(region = "global",
format = "netcdf",
tres = "daily",
sres = 0.25,
begin = as.Date("1981-01-01"),
end = as.Date("2020-12-31"),
dsn = "data/chirps_daily",
overwrite = T,
cores = 1)
### THIS CRASH ME RSTUDIO
for (i in 1981:1982){
nc <- paste("nc", i, sep = "")
assign(nc, brick(paste("data/chirps_daily/chirps-v2.0.", i, ".days_p25.nc", sep ="")))
}

First, you should never use assign; rather store the objects you create in a list. You can nornally do that like this
library(raster)
y <- 1981:1982
ff <- paste0("data/chirps_daily/chirps-v2.0.", y, ".days_p25.nc")
nc <- lapply(ff, brick)
names(nc) <- y
That will probably give the same error, most likely because of a corrupted file. To find out which, use a loop to see where it fails.
nc <- list()
for (i in 1:length(ff)) {
print(ff[i]); flush.console()
nc[[i]] <- brick(ff[i])
}
And then re-download the bad file and try again.

Related

"error in nc_open trying to open file" when calling raster()

#Crop Vietnam
lst <- raster(destPath, varname = 'tbb_14')
lst_crop <- crop(lst, ext_VN)
lst_crop <- lst_crop - 273.15
#Write output raster
outFilename <- paste(substr(j,1, nchar(j)-15),'_tbb_14.tif', sep='')
writeRaster(lst_crop, paste(extractedFolder + '/tbb_14/', outFilename, sep=""))
How should I fix the problem? I've tried to change path, grant permission for user, diving on the Internet to find a way, but there was nothing solved
You do this
lst <- raster(destPath, varname = 'tbb_14')
That fails, suggesting that destPath does not exist. You can see for yourself with
file.exists(destPath)
Once you have the correct filename, you can also replace "raster" with "terra", like this
library(terra)
x <- rast(destPath, 'tbb_14')

R function displays object "obser" but won't write.CSV

My code works and displays the correct values on screen for print(obser) but will not write the .csv and when I tried str(obser) it gave error object 'obser' not found. I have tried various online help and books and the function is correctly written.
If instead of running the function in the console in RStudio, I run line by line in the scrip screen will the csv then be created?
complete <- function(directory= "specdata", id = 1:332){
# directory <- "specdata"
# id <- 1:332
files_list <- list.files(path=directory,full.names=T)[id]
NumOfFiles <- length(files_list)
obser <- data.frame()
indivFile <-data.frame()
nobserv <- vector(mode= "integer", length = NumOfFiles)
for (i in 1:NumOfFiles){
indivFile <- read.csv(files_list[i]) # read data file into df inc NA's
indivFile <- na.omit(indivFile) # removes NA prev file
x <- nrow(indivFile[1])
nobserv[i] <- x
}
x_name <-"ID"
y_name <-"nobs"
obser <- data.frame(id, nobserv)
return(obser) # object returned
print(obser)
wd <- getwd()
setwd(wd)
write.csv(obser, file="Observations2.csv")
}
Try and save the object returned from the function. Ergo:
output<-complete("specdata",id=1:332)
write.csv(output,"Observations2.csv",row.names=F)

Seasonally adjust several series on R

I built a function to seasonally adjust Brazilian economic data, due to Carnival.
But this way, I can adjust only one series at a time, in my clipboard.
I've been trying, then, to adjust more series (copy several series one next to the other) but unsuccessfully.
Can you help me?
Thanks!
seasbrasil<-function(y0,m0,yT,mT) {carnaval<-c(as.Date("2000-03-07"),as.Date("2001-02-27"),as.Date("2002-02-12"),as.Date("2003-03-04"),as.Date("2004-02-24"),as.Date("2005-02-08"),as.Date("2006-02-28"),as.Date("2007-02-20"),as.Date("2008-02-05"),as.Date("2009-02-24"),as.Date("2010-02-16"),as.Date("2011-03-08"),as.Date("2012-02-21"),as.Date("2013-02-12"),as.Date("2014-03-04"),as.Date("2015-02-17"),as.Date("2016-02-09"))
library(seasonal)
Sys.setenv(X13_PATH = "C:\\Users\\gfernandes\\Documents\\x13as")
checkX13()
data(holiday)
carnaval.ts <- genhol(carnaval, start = -1, end = 2, center = "calendar")
x <- read.table(file = "clipboard", sep = "\t", header=FALSE)
x <-ts(x,start=c(y0,m0),end=c(yT,mT),frequency=12)
xsa <-seas(x,xreg=carnaval.ts,regression.usertype="holiday",x11=list())
summary(xsa)
plot(xsa)
xsa<-final(xsa)
write.csv(xsa, file = "C:\\Users\\gfernandes\\Documents\\ajuste.csv")
getwd()
}
Using the clipboard to read data is not a scaleable solution instead would suggest
creating a list of file names using list.files and applying your function on this list.
#Load all libraries first
library(seasonal)
#Define your data directory
DIR="C:\\path-to-your-dir\\"
#Replace .dat with file extension applicable
# set recursive = TRUE if you have tree directory structure
TS_fileList <- list.files(path=DIR,pattern=".dat",full.names = TRUE,recursive=FALSE)
#define carnival dates
carnaval<-c(
"2000-03-07","2001-02-27","2002-02-12",
"2003-03-04","2004-02-24","2005-02-08",
"2006-02-28","2007-02-20","2008-02-05",
"2009-02-24","2010-02-16","2011-03-08",
"2012-02-21","2013-02-12","2014-03-04",
"2015-02-17","2016-02-09")
#format carnival variable as date
carnaval <- as.Date(carnaval,format="%Y-%m-%d")
data(holiday)
carnaval.ts <- genhol(carnaval, start = -1, end = 2, center = "calendar")
Function:
fn_adj_seasbrasil <-function(
filePath = "C:\\path-to-your-dir\\file1.dat",
carnivalTS = carnaval.ts,
y0,
m0,
yT,
mT) {
#moved few operations outside this function
#since they are common to all files
#instead now the carnival series is
#input as parameter
x <- read.table(file = filePath, sep = "\t", header=FALSE)
x <- ts(x,start=c(y0,m0),end=c(yT,mT),frequency=12)
xsa <-seas(x,xreg = carnivalTS,regression.usertype="holiday",x11=list())
summary(xsa)
plot(xsa)
xsa<-final(xsa)
#save seasonally adjusted file with different suffix
fileName = tail(unlist(strsplit(filePath,sep="/")),1)
suffix = "adjuste"
#for adjusted time series of file1.dat
# the name will be adjuste_file1.dat
newFilePath = head(unlist(strsplit(filePath,sep="/")),1)
newFileName = paste0(newFilePath,"/",suffix,"_",fileName)
write.csv(xsa, file = newFileName)
cat(paste0("Saved file:",newFileName,"\n"))
}
#define y0,m0,yT,mT and then for all files call the function
lapply(TS_fileList,function(x) fn_adj_seasbrasil(filePath = x,carnivalTS = carnaval.ts, y0,m0,yT,mT) )
This might not work for your in first pass but can be resolved by familiarising yourself
with tutorials like these ATS UCLA and also reading
function help of ?read.table,?list.files , ?strsplit etc.

Looping Issue -Store the data which is of a different format

I am having some trouble storing the data after it runs. The code is picking the files up correctly and running the forecast model but it somehow stores the value for the last file. All the others are lost. Is there anyway that I can have all the results stored in a different array. The problem is that the format of the output is in "forecast" format and because of that I am getting stuck on it. I have looked through all the websites but couldn't find something like that.
Here is the code:
library(forecast)
library(quantmod)
library(forecast)
fileList <-as.array(length(50))
Forecast1 <- as.array(length(50))
fileList<-list.files(path ='C:\\Users\\User\\Downloads\\wOOLWORTHS\\',recursive =T, pattern = ".csv")
i<- integer()
j<-integer()
i=1
setwd("C:\\Users\\User\\Downloads\\wOOLWORTHS\\")
while (i<51)
{
a<-fileList[i]
print(a)
a <- read.csv(a)
fileSales<-a$sales
fileTransform<-log(fileSales)
plot.ts(fileTransform)
result1<-HoltWinters(fileTransform,beta = FALSE,gamma =FALSE,seasonal ="multiplicative",optim.control =TRUE)
result2<-forecast.HoltWinters(result1,h=1)
summary(result1)
accuracy(result2)
#Forecast1[i] <- result2(forecast)
#print(Forecast1[i])
i=i+1
}
It may just be how you are storing your results. Try filling an empty list instead (e.g.Forecast1):
setwd("C:\\Users\\User\\Downloads\\wOOLWORTHS\\")
library(forecast)
library(quantmod)
library(forecast)
fileList <- list.files(path ='C:\\Users\\User\\Downloads\\wOOLWORTHS\\',recursive =T, pattern = ".csv")
Forecast1 <- vector(mode="list", 50)
for(i in seq(length(fileList)){
a <- fileList[[i]]
#print(a)
a <- read.csv(a)
fileSales<-a$sales
fileTransform<-log(fileSales)
plot.ts(fileTransform)
result1<-HoltWinters(fileTransform,beta = FALSE,gamma =FALSE,seasonal ="multiplicative",optim.control =TRUE)
result2<-forecast.HoltWinters(result1,h=1)
#summary(result1)
#accuracy(result2)
Forecast1[[i]] <- result2
#print(Forecast1[i])
print(paste(i, "of", length(fileList), "completed"))
}

How can I produce wig files from .sam files using a faster R script?

I have an R script in which I can read the lines from a .sam file after mapping and I want to parse lines of sam file into strings in order to be easier to manipulate them and create the wig files that I want or to calculate the cov3 and cov5 that I need.
Can you help me please to make this script work faster? How can I parse lines of a huge .sam file into a data frame faster? Here is my script:
gc()
rm(list=ls())
exptPath <- "/home/dimitris/INDEX3PerfectUnique31cov5.sam"
lines <- readLines(exptPath)
pos = lines
pos
chrom = lines
chrom
pos = ""
chrom = ""
nn = length(lines)
nn
# parse lines of sam file into strings(this part is very very slow)
rr = strsplit(lines,"\t", fixed = TRUE)
rr
trr = do.call(rbind.data.frame, rr)
pos = as.numeric(as.character(trr[8:nn,4]))
# for cov3
#pos = pos+25
#pos
chrom = trr[8:nn,3]
pos = as.numeric(pos)
pos
tab1 = table(chrom,pos, exclude="")
tab1
ftab1 = as.data.frame(tab1)
ftab1 = subset(ftab1, ftab1[3] != 0)
ftab1 = subset(ftab1, ftab1[1] != "<NA>")
oftab1 = ftab1[ order(ftab1[,1]), ]
final.ftab1 = oftab1[,2:3]
write.table(final.ftab1, "ind3_cov5_wig.txt", row.names=FALSE,
sep=" ", quote=FALSE)
It's hard to provide a detailed answer without access to sample inputs and outputs (e.g., subsets of your data on dropbox). The Bioconductor solution would convert the sam file to bam
library(Rsamtools)
bam <- "/path/to/new.bam")
asBam("/path/to/old.sam", bam)
then read the data in, perhaps directly (see ?scanBam and ?ScanBamParam to import just the fields / regions of interest)
rr <- scanBam(bam)
or in the end more conveniently
library(GenomicAlignments)
aln <- readGAlignments(bam)
## maybe cvg <- coverage(bam) ?
There would be several steps to do your manipulations, ending with a GRanges object (sort of like a data.frame, but where the rows have genomic coordinates) or related object
## ...???
## gr <- GRanges(seqnames, IRanges(start, end), strand=..., score=...)
The end goal is to export to a wig / bigWig / bed file using
library(rtracklayer)
export(gr, "/path/to.wig")
There are extensive help resources, including package vignettes, man pages, and the Bioconductor mailing list

Resources