forked from emchristensen/weather_analysis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
yearly_winter_precip.R
55 lines (47 loc) · 1.84 KB
/
yearly_winter_precip.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# This script takes monthly weather summary and writes a new file of
# total winter precip.
# Written by EMC 3/11/15
#
# Input:
# Monthly_ppt_1980_present.csv
# year, month, sumprecip, maxtemp, mintemp
#
# Output:
#
#
# Scripts called:
# weather_daily_summary.r combines hourly dataset (1989-present) and daily dataset (1980-89)
# set working directory and run prelim scripts
setwd('C:/Users/EC/git_dir/')
source('portal_weather/weather_monthly_summary.r')
# =====================================================================================================
# functions
yearly_winter_precip = function(dataframe) {
#sums precipitation for winter months (Dec-March), returns yearly total
year= vector()
ppt = vector()
for (yr in 1980:2014) {
yr_mo = list(c(yr-1,10),c(yr-1,11),c(yr-1,12),c(yr,1),c(yr,2),c(yr,3),c(yr,4))
win_ppt = vector()
for (i in 1:length(dataframe$year)) {
if (is.element(list(c(dataframe$year[i],dataframe$month[i])),yr_mo)) {
win_ppt = append(win_ppt,dataframe$sumprecip[i])
}
}
year = append(year,yr)
ppt = append(ppt,sum(win_ppt))
}
return(data.frame(year,ppt))
}
# read in daily data, aggregate by month excluding NAs, only take data from months with <10 days NA
weathframe = read.csv("data/Daily_weather_1980_present_fixed_withgaps.csv")
monthly = aggregate(weathframe$Precipitation,by=list(weathframe$Year,weathframe$Month),FUN=sum,na.rm=T)
names(monthly) = c('year','month','sumprecip')
nas = weathframe[is.na(weathframe$Precipitation),]
nacount = aggregate(nas$Precipitation,by=list(nas$Year,nas$Month),FUN=length)
names(nacount) = c('year','month','na')
monthly = merge(monthly,nacount,by=c('year','month'),all=T)
monthly[is.na(monthly$na),4] = 0
# make precip NA if >10 days missing
monthly[monthly$na>10,3] = NA
winter_ppt = yearly_winter_precip(monthly)