CRN Data input functions

First off I wanted a list of sites. Since each site has multiple entries that are in the yearly subdirectories I wrote a function to cycle through the directories and make a list of sites from the file names.


# cycles through the parent directory and retieves the filenames
# of the files in the sub directories extracting the name of the site
#from the file path and creating a list of unique site names
getsites<- function(){
crnpath = "~/Documents/CRN/"
crndata crndir
list.files(path = crndir)->crnfiles
list.files(path = crndir,full.names = TRUE)->crnfilepaths
crnsites crnsites

}

This function is similar but returns all of the file paths

getfilepaths <- function(){
crnpath = "~/Documents/CRN/"
crndata crndir
list.files(path = crndir)->crnfiles
crnfilepaths <- list.files(path = crndir,full.names = TRUE)
}

This function organizes the file paths into a list of lists:

getsitefilenames <-function(sitename, filepaths){
crnfilepaths[grep(sitename, filepaths)]
}

And this takes this information and gets the data for each site
creating a list of the data and a list of the sites meta data.

getsitefiles <-function(sitepaths){

sitedata <- lapply(sitepaths, read.table, na.strings = "-9999.0")
sitedatamerged 1){
for (i in 2: length(sitedata)){
sitedatamerged <- rbind(sitedatamerged, sitedata[[i]])
}
}
# name of the site from the file path
crnsitename <- substr(sitepaths[[1]], regexpr("CRNDAILY01", sitepaths[[1]])+16, regexpr("txt", sitepaths[[1]])-2)
crnlat <- as.double(sitedatamerged $V6[1])
crnlon <- as.double(sitedatamerged$V5[1])
crnsite <- c(name = crnsitename, lat = crnlat, lon = crnlon)
datestr <- sitedatamerged$V3
yearstr <- substring(datestr,1,4)
monstr <- substring(datestr,5,6)
daystr <- substring(datestr,7,8)
dateval <- strptime(datestr,format = "%Y%m%d")
juldate <- as.numeric(julian(dateval))
dayofyr <- as.character(dateval, format = "%j")

crndata = data.frame( crnsitename , as.factor(sitedatamerged$V1), yearstr , monstr ,daystr, juldate, dayofyr,sitedatamerged$V7, sitedatamerged$V8, sitedatamerged$V9, sitedatamerged$V10)
names(crndata)<-c("name" ,"wban", "year", "month", "day","julian", "dayofyr", "tmax", "tmin", "tmean", "tavg")

return(list(meta = crnsite, data = crndata))
}

Advertisements

Leave a reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s