-
Notifications
You must be signed in to change notification settings - Fork 0
/
8. Calculating Monthly averages_Council_github.Rmd
182 lines (127 loc) · 4.38 KB
/
8. Calculating Monthly averages_Council_github.Rmd
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
---
title: "Council monthly averages" - Same as step 7 for making daily averages, but doing monthly averages (optional)
output: html_document
date: "2024-09-27"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
rm(list = ls())
library(data.table)
library(ggplot2)
library(cowplot)
library(openair)
library(plotrix)
library(signal)
library(svMisc)
library(zoo)
library(stringr)
library(plyr)
library(viridis)
library(lubridate)
library(tidyverse)
library(gridExtra)
library(plotly)
library(RColorBrewer)
library(openair)
```
#read in cleaned and Ameriflux prepped half-hourly dataset
```{r}
#commented out - updated version following adding wind direction to RF in Github folder
#df = fread(input = "C:/Users/kkent/Documents/Council Data/Council BASE gapfilling/US-NGC2_HH_201701010000_202309010000.csv", na.strings="-9999")
df = fread(input = "C:/Users/kkent/Documents/Github Flux Network/Council BASE prepping/Council_BASE-data-prepping/US-NGC2_HH_201701010000_202309010000.csv", na.strings = "-9999")
##na.strings = c('-9999','NA','NaN','NAN','-7999')
```
#Create useable timestamp variable
```{r}
df$TIMESTAMP_END = as.character(df$TIMESTAMP_END)
df$TIMESTAMP_START = as.character(df$TIMESTAMP_START)
df$TIMESTAMP_END = as.POSIXct(df$TIMESTAMP_END, tz="UTC", format = "%Y%m%d%H%M")
df$TIMESTAMP_START = as.POSIXct(df$TIMESTAMP_START, tz="UTC", format = "%Y%m%d%H%M")
```
#Create a new df for monthly averages
```{r}
#using "date" instead of "day" in order to use the openAir and timeAverage packages/functions
df$date = as.Date(df$TIMESTAMP_END)
date = unique(df$date)
df_avg = as.data.frame(date)
```
#Create FC_night
```{r}
# Filter CO2 Flux data by incoming shortwave values
df <- df %>%
mutate(FC_night = ifelse(SW_IN <= 0, FC, NA))
df <- df %>%
mutate(FC_night_F = ifelse(SW_IN <= 0, FC_F, NA))
```
#Average by month
```{r}
#Units are umol/m2/s for CO2, and nmol/m2/s for CH4
library(openair)
#average entire dataframe with tighter threshold
df_monthly_avg <- as.data.frame(date)
df_monthly_avg <-timeAverage(df, avg.time = "month", data.thresh = 50)
#average dataframe with lighter threshold to get good nighttime data coverage
df_avg_night <- as.data.frame(date)
df_avg_night <-timeAverage(df, avg.time = "month", data.thresh = 10)
#add nighttime data back into the full dataset dataframe
df_monthly_avg$FC_night = df_avg_night$FC_night
df_monthly_avg$FC_night_F = df_avg_night$FC_night_F
#Optionally, print the resulting dataframe with monthly averages to screen / make sure it looks ok
print(df_monthly_avg)
```
#Based on Dani's old code using a loop
```{r}
# Step 1: Create the month column from TIMESTAMP_END
# df$month = format(as.Date(df$TIMESTAMP_END), "%Y-%m") # Extract month in YYYY-MM format
# Create a dataframe with unique months
# unique_months = unique(df$month)
# df_monthly_avg = data.frame(month = unique_months, stringsAsFactors = FALSE) # Initialize df_avg with unique months
#
# # Step 2: Average by month to create monthly average dataframe
# for (i in 1:ncol(df)) {
# if (class(df[[i]])[1] == 'numeric') {
#
# colname = colnames(df)[i]
#
# # Calculate monthly averages
# monthly_avg_val <- aggregate(df[[colname]] ~ month, data = df, FUN = mean, na.action = na.omit)
#
# # Rename the average column
# colnames(monthly_avg_val)[2] <- colname
#
# # Join the monthly averages to df_avg
# df_monthly_avg = left_join(df_monthly_avg, monthly_avg_val, by = 'month')
#
# } else {
# next
# }
# }
#
#print the resulting dataframe with monthly averages
#print(df_monthly_avg)
```
#check out the averages to make sure they look ok
```{r}
plot(df_monthly_avg$date, df_monthly_avg$FC_F)
plot(df_monthly_avg$date, df_monthly_avg$GPP_F)
plot(df_monthly_avg$date, df_monthly_avg$RECO_F)
plot(df_monthly_avg$date, df_monthly_avg$FC_night_F)
plot(df_monthly_avg$TS_3_1_1, df_monthly_avg$FC_night_F)
plot(df_monthly_avg$TS_3_1_1, df_monthly_avg$RECO_F)
plot(df_monthly_avg$TS_3_1_1, df_monthly_avg$GPP_F)
# ggplot(df_avg,aes(x=month,y=FC_F))+
# geom_point()
#
# ggplot(df_avg,aes(x=month,y=GPP_F))+
# geom_point()
#
#
# ggplot(df_avg,aes(x=month,y=RECO_F))+
# geom_point()
```
# Save Data
```{r}
write.csv(x = df_monthly_avg,file = 'C:/Users/kkent/Documents/Council Data/Council BASE gapfilling/council_monthly_AVG_gapfilled_clean_2017_2023_for analysis.csv',quote = F,row.names = F)
```