hier das komplette Script. Von meinem Dozenten, angepasst von mir auf meine Daten. ###################################
Code: Alles auswählen
##### HOBO Datenlogger #####
##### Rudi Sailer, 2019-09-11 #####
###################################
##### HOBO data export as CSV #####
# A) File > Export Table Data
# ---> ONLY Temperature > UNSELECT 'Coupler attached' to 'End Of File' when export data
# B) File > Export Table Data > PREFERENCES ('Wheel')
# ---> EXclude Line numbers
# ---> Column Seperator > Tab
# ---> Date and Time > in ONE column
####Read Data
getwd()
setwd("A:/Uni/Schneegleiten_Masterarbeit/Wetterdaten_Zorin/Weather stations")
getwd()
library (readxl)
#wtable= Wetterstation1_2014_2015
wtable <-read_excel("Wetterstation1_2014_2015_Lufttemp.xlsx",na="NA")
colnames(wtable) <- c('Date','T')
##### AGGREGATE VALUES to >>> DAILY <<< VALUES #####
# ---> solution with package lubridate >> works for different time formats without
# manipulation as shown above
library(lubridate) # for character to datum conversion
tmp.Date <- ymd_hms(wtable$Date)
tmp.Date <- dmy_hms(wtable$Date)
wtable$Date <- tmp.Date
#print(wtable)
### SUBSET --> to start axis label with specific date
#wtable <- subset(wtable, Date >= ("2014-10-01") & Date < ("2014-07-31"))
### calc MEAN and SD
# create a vector which containes only days or hours out of e.g. 10' minutes
days <- cut(wtable$Date,breaks='months') # 'days' 'hours' 'weeks' 'months' 'quarters'
days <- as.POSIXct(days)
# create data frame with days and t (Lufttemperatur)
data.days <- data.frame(days=days, date_time=wtable$Date, t=as.numeric(wtable$T))
data.days <- data.frame(days=days, t=as.numeric(wtable$T))
## MEAN / SD ---> with AGGREGATE
data.days.mean <- aggregate(as.numeric(data.days$t),
by=list(data.days$days),FUN=mean, na.rm=TRUE)
data.days.sd <- aggregate(as.numeric(data.days$t),
by=list(data.days$days),FUN=sd, na.rm=TRUE)
# combine date and calculated values
daily.val <- cbind(data.days.mean,data.days.sd$x)
colnames(daily.val) <- c('days','t_dmean','t_dsd')
# ## MEAN / SD ---> alternative with PLYR package
# # Function for calc SD and mean >> use in ddply below
# # calc SD and mean for t with function getcalc (package plyr, '.' to allow object names without quoting)
# library(plyr)
# getcalc <- function(Df) c(t_dmean = mean(Df$t,na.rm=T), # MEAN for t (air temperature)
# t_dsd = sd(Df$t,na.rm=T))
# daily.val <- ddply(data.days, .(days), getcalc)
### create vector +/- SD
# mean +/- SD
t.p.sd <- daily.val$t_dmean + daily.val$t_dsd
t.m.sd <- daily.val$t_dmean - daily.val$t_dsd
### plot daily values
axis(2, at = seq(-20, 20, 5))
r <- as.POSIXct(round(range(daily.val$days), "days"))
plot(t_dmean ~ days, data=daily.val,
type='b', pch=4,col='blue', lwd=1.5,cex=0.65, ylim=c(-20,20),
main="Wetterstation1 2014-2015",ylab='Mean monthly air temperature [°C]',xlab='',xaxt='n')
axis.POSIXct(1,daily.val$days,at = seq(r[1], r[2], by = "months"), # possibilities: day, days, weeks, months, quarter
format="%d/%m/%Y",las=2,cex.axis=0.95)
#axis.POSIXct(1,daily.val$days,at = c('2019-11-13','2019-12-01'), # possibilities: day, days, weeks, months, quarter
axis.POSIXct(1,daily.val$days,at = c('2014-10-01','2015-07-31'), # possibilities: day, days, weeks, months, quarter
format="%d/%m/%Y",las=2,cex.axis=0.95)
# SD borders
lines(t.p.sd~daily.val$days,col='red',lty=1,lwd=0.3)
lines(t.m.sd~daily.val$days,col='red',lty=1,lwd=0.3)
# SD polygon
polygon(
x = c(daily.val$days, rev(daily.val$days)),
y = c(t.p.sd, rev(t.m.sd)),
col = "#cc000033",
border=NA
)
abline(0,0)
#abline(-3,0,lty=3)
#axis(2,at=-3,labels=('-3'))
legend('topleft', c('Mean monthly air temperature [°C]','Mean monthly air temperature +/- SD [°C]'),
col=c('blue','#cc000033'),
lty=c(1,1),lwd=c(1.5,6.5),bty='n', pch=c(4,NA),
)
write.table(daily.val,w.f,quote=F, sep='\t', col.names = T,row.names = F)