Reputation: 535
I have a list of climate files in a folder (EDU link: https://cloudstor.aarnet.edu.au/plus/index.php/s/QFpaBDR7q2GbDCN). They are named as follow p[year][month]_0p5.asc
.
Example:
p198001_0p5.asc
p198002_0p5.asc
...
p198012_0p5.asc
p198101_0p5.asc
...
p201012_0p5.asc
I wish to create 2000 years of data by repeating in that order these 31 years (1980 to 2010) and renaming them to have a list of files from p198001_0p5.asc
to p397912_0p5.asc
As example, the final folder will include p201101_0p5.asc
which contains the same data as p198001_0p5.asc
but will be named p201101_0p5.asc
.
I have no idea how to do this in R. Any help would be much appreciated!
Update 19-07-2017: @Mako212 suggestion seems to work but I had memory issues (error message # protect(): protection stack overflow). I changed my strategy and created another script based on his kind suggestion to generate 131 years of data from 1880 to 2010. I did this by repeating the series of 31 years (1980-2010). Here is the code in case you'd like to have a look at that strategy too. It works fine:
require(purrr)
require(data.table)
library(raster)
setwd('...')
files <- list.files(pattern= "*.asc")
files
length(files)
fileDF <- files %>% map(raster)
#set the output directory
output_dir <- "..."
# set month and year counters
startYear <- 2010
startMonth <- 12
fileNumber <- 1
for (i in fileDF){
startYear <- 2010 - (fileNumber-1) %/% 12
for (x in 1:6){
print(startYear)
print(startMonth)
print(fileNumber)
writeRaster(i, paste(output_dir, sprintf("p%d%s%d_0p5.asc", startYear, ifelse(startMonth<10,0,""), startMonth), sep="/"), format = "ascii")
startYear <- startYear - 31
if (startYear < 1880) break # don't create files before December, 1880
}
if (startMonth > 1 ) {
startMonth <- startMonth - 1
}
else{
startMonth <- 12
}
fileNumber <- fileNumber + 1
}
Upvotes: 1
Views: 102
Reputation: 7312
Okay, here's the general idea:
require(purrr)
require(data.table)
# after playing with SDMTools::read.asc, data.table::fread seems to
# be more reliable. That said, if fread() isn't reading your data
# correctly, you might try using the SDMTools function instead.
# I also chose to save everything as .csv, but again, you can try
# using the SDMTools read/write.asc functions if you want
files <- list.files(pattern= "*.asc")
fileDF <- files %>% map(fread)
# set month and year counters
startYear <- 1980
startMonth <- 1
fileNumber <- 1
for (i in fileDF){
# increment startYear by 1 every 13th file
startYear <- 1980 + (fileNumber-1) %/% 12
for (x in 1:65){
# added underscore for clarity between year and month
# format is p1980_01_0p5.csv
write.csv(i, sprintf("p%d_%s%d_0p5.csv", startYear, ifelse(startMonth<10,0,""), startMonth))
startYear <- startYear + 31
# don't create files past December, 3979
if (startYear > 3979) break
}
if (startMonth < 12) {
startMonth <- startMonth +1
}
else{
startMonth <- 1
}
fileNumber <- fileNumber + 1
}
The loop counters are set assuming you have exactly 31 years worth of data (12*31 files)
Make sure you write to a new folder (not the one that contains the source data)
Upvotes: 1