-
Notifications
You must be signed in to change notification settings - Fork 0
/
7. Calculating Daily Averages_Council_github.Rmd
141 lines (104 loc) · 3.83 KB
/
7. Calculating Daily 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
---
title: "Council daily averages" #Some analyses need continuous data, so we calculate daily averages to better organize our visualization and to analyze
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 dataset
```{r}
#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 daily 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 in the half-hourly cleaned Ameriflux-prepped dataset from step 6
```{r}
# Filter CO2 Flux data by incoming shortwave values to get nighttime fluxes
df <- df %>%
mutate(FC_night = ifelse(SW_IN <= 0, FC, NA)) #variable for not gapfilled
df <- df %>%
mutate(FC_night_F = ifelse(SW_IN <= 0, FC_F, NA)) #variable for gapfilled
```
#Average by day to create continuous daily averages - NOTE: need to make sure to use "date" for openAir and timeAverage code to work*
```{r}
library(openair)
#average entire dataframe with tighter data availability threshold to avoid using large data gaps in calculating the avg -- having at least 50% of the data
df_avg <- as.data.frame(date)
df_avg <-timeAverage(df, avg.time = "day", data.thresh = 50)
#average dataframe with lighter threshold to get better nighttime data coverage
df_avg_night <- as.data.frame(date)
df_avg_night <-timeAverage(df, avg.time = "day", data.thresh = 10)
#add avg nighttime flux back into the dataset
df_avg$FC_night = df_avg_night$FC_night
df_avg$FC_night_F = df_avg_night$FC_night_F
#From Dani's old code using "day" and a loop to create daily avgs
## average variables by day to create daily average dataframe
# for (i in 1:ncol(df)) {
# if (class(df[[i]])[1] == 'numeric'){
#
# colname = colnames(df)[i]
#
# daily_avg_val <- aggregate(df[[colname]] ~ day, data = df, FUN = mean, na.action=na.omit)
#
# colnames(daily_avg_val)[2] <- colname
#
# df_avg = left_join(df_avg, daily_avg_val, by='day')
#
# }
# else{
# next
# }
# }
```
#check out the averages to make sure they look reasonable / screen
```{r}
plot(df_avg$date, df_avg$FC_F)
plot(df_avg$date, df_avg$GPP_F)
plot(df_avg$date, df_avg$RECO)
plot(df_avg$date, df_avg$FC_night_F)
plot(df_avg$TS_3_1_1, df_avg$FC_night_F)
plot(df_avg$TA_ERA5, df_avg$FC_night_F)
```
# Save Data
```{r}
#these dataframes now include FC_night and FC_night_F
#half-hourly df with night fluxes
write.csv(x = df,file = 'C:/Users/kkent/Documents/Council Data/Council BASE gapfilling/council_gapfilled_clean_2017_2023_for analysis.csv',quote = F,row.names = F)
#daily avg df with night fluxes
write.csv(x = df_avg,file = 'C:/Users/kkent/Documents/Council Data/Council BASE gapfilling/council_AVG_gapfilled_clean_2017_2023_for analysis.csv',quote = F,row.names = F)
```