From 445931907d329d6e5c2a65ae17972e2f48121904 Mon Sep 17 00:00:00 2001
From: Nathalie Triches <ntriches@bgc-jena.mpg.de>
Date: Thu, 5 Oct 2023 16:43:44 +0200
Subject: [PATCH] changes during and between field work in Abisko 2023

* changed scripts according to data that was sampled
* differences between May (all GHG on data logger), July (mess), and
Sept (all GHG on data logger)
* added micro habitat and plot number to plot
* all scripts not clean
---
 20230314_simple_habitat_comparison.R          |  23 +++-
 preprocessing/functions/plot_ghg_conc_2023.R  | 121 +++++++++++++-----
 preprocessing/functions/read_raw_data_2023.R  |  54 +++++++-
 .../sort_ch4_conc_by_meas_period_2023.R       |   4 +-
 .../sort_co2_conc_by_meas_period_2023.R       |   6 +-
 .../functions/sort_n2o_conc_by_meas_period.R  |  36 ++++--
 .../sort_n2o_conc_by_meas_period_2023.R       |   2 +-
 preprocessing/main_script.R                   |   9 +-
 8 files changed, 197 insertions(+), 58 deletions(-)

diff --git a/20230314_simple_habitat_comparison.R b/20230314_simple_habitat_comparison.R
index d5efa6f..3e29b85 100644
--- a/20230314_simple_habitat_comparison.R
+++ b/20230314_simple_habitat_comparison.R
@@ -72,17 +72,32 @@ abline(h=c(335,339),col="red")
 library(ggplot2) 
 
 # combine data from single measurements from each plot to one boxplot
+# order boxplots in right way from palsa_lichen to pm to bog to fen
+# palsa_lichen plots: 2, 11, 18, 25, 29, 33
+# palsa_moss plots: 8, 15, 17, 27, 30, 32
+# bog plots: 7, 12, 20, 26, 28, 31 
+# fen plots: 10,19,23,34,35,36
 # remove plot 1 
 all_plots_sorted_by_habitat <- mPN2oPlotHabitat %>%
   filter(plot_no != 1) %>%
-  ggplot(aes(x = micro_habitat, y = n2oppb,
-                       group=plot_no, color = micro_habitat)) +
+  ggplot(aes(x = factor(micro_habitat, levels = c("palsa_lichen", "palsa_moss","bog","fen")), y = n2oppb,
+             group=plot_no, 
+             color = micro_habitat)) +
   geom_boxplot() +
   # geom_text(aes(label = plot_no)) +
-  labs(x = "micro habitat", y = "N2O concentration [ppb]") +
-  theme(text=element_text(size=20))
+  labs(x = "Micro habitat", y = "N2O concentration [ppb]", title = "Fig. 3: Spatial variability of N2O concentrations in spring 2023") +
+  labs(caption = "  24 plots with 6 replicates grouped in micro habitats, i. e., palsa lichen (dry), palsa moss (dry), bog (wet), fen (wetter). 
+  Each single boxplot represents all N2O concentrations measured in spring 2023 in one plot (= chamber placement area) 
+  during chamber closure time (between 5 and 10 min). Please note that fluxes have not been calculated yet.") +
+  theme(axis.text=element_text(size=24), axis.title = element_text(size=24),
+        plot.caption = element_text(size = 22, hjust = -0, vjust = 0.2),
+        plot.title = element_text(size = 30), panel.background = element_rect(fill = "#FFFFFF"),
+        panel.border = element_rect(colour = "black", fill = NA), panel.grid.major = element_line(colour = "light grey"),
+        legend.title = element_text(size = 22), legend.text = element_text(size = 22)) 
+  #theme_bw()
 all_plots_sorted_by_habitat
 
+
 # Differences within micro habitat #### 
 # combine data from single measurements from each plot to one boxplot
 # look at palsa lichen 
diff --git a/preprocessing/functions/plot_ghg_conc_2023.R b/preprocessing/functions/plot_ghg_conc_2023.R
index 8328def..6d9e2b4 100644
--- a/preprocessing/functions/plot_ghg_conc_2023.R
+++ b/preprocessing/functions/plot_ghg_conc_2023.R
@@ -4,18 +4,54 @@
 ############### Plots for CO2 ##################################################
 ################################################################################
 
+# CO2 scatter and boxplot with plot number and meas_ID
+meas_ID <- measPeriodCo2$meas_ID
+
+for (i in unique(meas_ID)){
+  fileID <- i
+  measID_loc <- i
+  rows_measID_loc  <- which(measPeriodCo2$meas_ID == measID_loc) 
+  first_row_measID_loc <- rows_measID_loc[1]
+  
+  d <- subset(measPeriodCo2, meas_ID == i)
+  # summer 2023
+  # png(filename=paste0("/home/ntriches/Nextcloud/aniwa/01_qarctic/data_analysis/figures_processing/2023/07_summer/202307_co2mp_", fileID, ".png"), 
+  #    height=8, width=12, pointsize=8, bg="white", units="in", res=200)
+  # autumn 2023
+  png(filename=paste0("/home/ntriches/Nextcloud/aniwa/01_qarctic/data_analysis/figures_processing/2023/09_autumn/202309_co2mp_", fileID, ".png"), 
+      height=4, width=8, pointsize=8, bg="white", units="in", res=200)
+  par(mfrow = c(2,2))
+  plot(d$datetime_datalogger, d$co2ppm_licor)
+  title(main = paste("measurement period", i, "plot", measPeriodCo2$plot[first_row_measID_loc]
+                     ))
+  # hist(d$n2oppm_aeris, breaks = 15)
+  boxplot(d$co2ppm_licor)
+  plot(d$par)
+  plot(d$h2oppm_licor)
+  par(mfrow = c(1,1))
+  dev.off()
+}
+
 # original data without any modifications (5 min), CO2 data from Licor
+# WITHOUT TITLES 
 meas_ID <- measPeriodCo2$meas_ID
 
 for (i in unique(meas_ID)){
   fileID <- i
   d <- subset(measPeriodCo2, meas_ID == i)
-  png(filename=paste0("/home/ntriches/Nextcloud/aniwa/01_qarctic/data_analysis/figures_processing/2023/co2-licor-mp_", fileID, ".png"), 
+  # summer 2023
+  png(filename=paste0("/home/ntriches/Nextcloud/aniwa/01_qarctic/data_analysis/figures_processing/2023/07_summer/202307_co2-licor-mp_", fileID, ".png"), 
       height=4, width=8, pointsize=8, bg="white", units="in", res=200)
+  # autumn 2023
+ # png(filename=paste0("/home/ntriches/Nextcloud/aniwa/01_qarctic/data_analysis/figures_processing/2023/09_autumn/co2-licor-mp_", fileID, ".png"), 
+  #    height=4, width=8, pointsize=8, bg="white", units="in", res=200)
   par(mfrow = c(1,2))
   plot(d$datetime_datalogger, d$co2ppm_licor)
   # abline(reg)
   title(main = i)
+  title(main = paste("measurement period", i, "plot", measPeriodCo2$plot,  
+                     mPN2oPlotHabitat$plot_no[first_row_measID_loc], 
+                     mPN2oPlotHabitat$micro_habitat[first_row_measID_loc]))
   #hist(d$co2ppm_licor, breaks = 15)
   boxplot(d$co2ppm_licor)
   #plot(d$airTdegC)
@@ -66,18 +102,27 @@ for (i in unique(meas_ID)){
 #################### CH4 plots #################################################
 ################################################################################
 
-meas_ID <- measPeriodCh4$meas_ID
+# CH4 plots and boxplots with plot and meas_ID 
 
-# str(measPeriodCh4)
+meas_ID <- measPeriodCh4$meas_ID
 
 for (i in unique(meas_ID)){
   fileID <- i
+  measID_loc <- i
+  rows_measID_loc  <- which(measPeriodCh4$meas_ID == measID_loc) 
+  first_row_measID_loc <- rows_measID_loc[1]
+  
   d <- subset(measPeriodCh4, meas_ID == i)
-  png(filename=paste0("/home/ntriches/Nextcloud/aniwa/01_qarctic/data_analysis/figures_processing/2023/ch4mp_", fileID, ".png"), 
+  # summer 2023
+  # png(filename=paste0("/home/ntriches/Nextcloud/aniwa/01_qarctic/data_analysis/figures_processing/2023/07_summer/202307_ch4mp_", fileID, ".png"), 
+  #    height=4, width=8, pointsize=8, bg="white", units="in", res=200)
+  # autumn 2023
+  png(filename=paste0("/home/ntriches/Nextcloud/aniwa/01_qarctic/data_analysis/figures_processing/2023/09_autumn/202309_ch4mp_", fileID, ".png"), 
       height=4, width=8, pointsize=8, bg="white", units="in", res=200)
   par(mfrow = c(1,2))
   plot(d$datetime_datalogger, d$ch4ppb)
-  title(main = i)
+  title(main = paste("measurement period", i, "plot", measPeriodCh4$plot[first_row_measID_loc]
+  ))
   #hist(d$ch4ppm, breaks = 15)
   boxplot(d$ch4ppb)
   #plot(d$airTdegC)
@@ -107,6 +152,7 @@ for (i in unique(meas_ID)){
 ################# Plots for N2O ################################################
 ################################################################################
 
+# RUN THIS FIRST 
 # enable plot comparison (copy-paste from 20230414_simple_habitat_comparison)
 # code needs to be cleaned! 
 
@@ -155,34 +201,8 @@ for (iLine in 1:nrow(measPeriodMetaDataDF)){
   }
 } 
 
-# non-modified measurement periods (5 min) 
-# check what fucks the Aeris up
-meas_ID <-mPN2oPlotHabitat$meas_ID  
 
-for (i in unique(meas_ID)){
-  fileID <- i
-  measID_loc <- i
-  rows_measID_loc  <- which(mPN2oPlotHabitat$meas_ID == measID_loc)
-  first_row_measID_loc <- rows_measID_loc[1]
-  
-  d <- subset(mPN2oPlotHabitat, meas_ID == i)
-  png(filename=paste0("/home/ntriches/Nextcloud/aniwa/01_qarctic/data_analysis/figures_processing/2023/n2omp_aeris-check2_", fileID, ".png"), 
-      height=8, width=12, pointsize=8, bg="white", units="in", res=200)
-  par(mfrow = c(2,2))
-  plot(d$datetime_datalogger, d$n2oppb)
-  title(main = paste("measurement period", i, "plot", mPN2oPlotHabitat$plot_no[first_row_measID_loc], 
-                     mPN2oPlotHabitat$micro_habitat[first_row_measID_loc]))
-  plot(d$datetime_datalogger, d$h2oppm_aeris)
-  plot(d$datetime_datalogger, d$airpreshPa)
-  plot(d$datetime_datalogger, d$chamberRH)
-  #boxplot(d$n2oppb)
-  #plot(d$airTdegC)
-  par(mfrow = c(1,1))
-  dev.off()
-}
-
-
-####### TRIAL with habitat and collar number! ############
+####### plots with habitat and collar number! ############
 meas_ID <-mPN2oPlotHabitat$meas_ID  
 
 for (i in unique(meas_ID)){
@@ -192,7 +212,11 @@ for (i in unique(meas_ID)){
   first_row_measID_loc <- rows_measID_loc[1]
   
   d <- subset(mPN2oPlotHabitat, meas_ID == i)
-  png(filename=paste0("/home/ntriches/Nextcloud/aniwa/01_qarctic/data_analysis/figures_processing/2023/n2omp_", fileID, ".png"), 
+  # summer 2023
+  # png(filename=paste0("/home/ntriches/Nextcloud/aniwa/01_qarctic/data_analysis/figures_processing/2023/07_summer/202307_n2omp_", fileID, ".png"), 
+  #    height=4, width=8, pointsize=8, bg="white", units="in", res=200)
+  # autumn 2023
+   png(filename=paste0("/home/ntriches/Nextcloud/aniwa/01_qarctic/data_analysis/figures_processing/2023/09_autumn/202309_n2omp_", fileID, ".png"), 
       height=4, width=8, pointsize=8, bg="white", units="in", res=200)
   par(mfrow = c(1,2))
   plot(d$datetime_datalogger, d$n2oppb)
@@ -219,6 +243,35 @@ for (i in unique(meas_ID)){
 # same with
 ### mPN2oPlotHabitat$micro_habitat[first_row_measID_loc]
 
+# non-modified measurement periods (5 min) 
+# check what fucks the Aeris up
+meas_ID <-mPN2oPlotHabitat$meas_ID  
+
+for (i in unique(meas_ID)){
+  fileID <- i
+  measID_loc <- i
+  rows_measID_loc  <- which(mPN2oPlotHabitat$meas_ID == measID_loc)
+  first_row_measID_loc <- rows_measID_loc[1]
+  
+  d <- subset(mPN2oPlotHabitat, meas_ID == i)
+  png(filename=paste0("/home/ntriches/Nextcloud/aniwa/01_qarctic/data_analysis/figures_processing/2023/n2omp_aeris-check2_", fileID, ".png"), 
+      height=8, width=12, pointsize=8, bg="white", units="in", res=200)
+  par(mfrow = c(2,2))
+  plot(d$datetime_datalogger, d$n2oppb)
+  title(main = paste("measurement period", i, "plot", mPN2oPlotHabitat$plot_no[first_row_measID_loc], 
+                     mPN2oPlotHabitat$micro_habitat[first_row_measID_loc]))
+  plot(d$datetime_datalogger, d$h2oppm_aeris)
+  plot(d$datetime_datalogger, d$airpreshPa)
+  plot(d$datetime_datalogger, d$chamberRH)
+  #boxplot(d$n2oppb)
+  #plot(d$airTdegC)
+  par(mfrow = c(1,1))
+  dev.off()
+}
+
+
+
+
 
 
 
@@ -230,7 +283,7 @@ meas_ID <- measPeriodN2o$meas_ID
 for (i in unique(meas_ID)){
   fileID <- i
   d <- subset(measPeriodN2o, meas_ID == i)
-  png(filename=paste0("/home/ntriches/Nextcloud/aniwa/01_qarctic/data_analysis/figures_processing/2023/n2omp_", fileID, ".png"), 
+  png(filename=paste0("/home/ntriches/Nextcloud/aniwa/01_qarctic/data_analysis/figures_processing/2023/09_autumn/n2omp_", fileID, ".png"), 
       height=4, width=8, pointsize=8, bg="white", units="in", res=200)
   par(mfrow = c(1,2))
   plot(d$datetime_datalogger, d$n2oppm_aeris)
diff --git a/preprocessing/functions/read_raw_data_2023.R b/preprocessing/functions/read_raw_data_2023.R
index 4b09de8..45050de 100644
--- a/preprocessing/functions/read_raw_data_2023.R
+++ b/preprocessing/functions/read_raw_data_2023.R
@@ -13,7 +13,12 @@ get_datalogger_raw_data  <- function(mainDir, dataDir, expYear){
   print("start get_datalogger_raw_data")
   # import data from data logger (CO2 + CH4 fluxes, soil moisture and temp data)
   if (expYear == "2023"){
-    inputFile <- "20230511-30_stordalen_datalogger_data.csv"
+    # spring 2023
+    # inputFile <- "20230511-30_stordalen_datalogger_data.csv"
+    # summer 2023
+    # inputFile <- "202307_stordalen_datalogger_data.csv"
+    # autumn 2023
+    inputFile <- "20230922_stordalen_datalogger_data.csv"
   } else if (expYear == "2024"){
     print("NOT YET available")
   }
@@ -80,7 +85,11 @@ get_field_mp_meta_data_DF <- function(mainDir, dataDir, expYear){
   
   print("start get_field_mp_meta_data_DF")
   if (expYear == "2023"){
-    inputFile <- "202305_field-data.csv"
+    # spring 2023
+    # summer 2023
+    inputFile <- "202309_field-data_final.csv"
+    # autumn 2023
+    # inputFile <- "20230909_field-data_final.csv"
   } else if (expYear == "2024"){
     print(" NOT YET available")
   }
@@ -126,3 +135,44 @@ get_field_mp_meta_data_DF <- function(mainDir, dataDir, expYear){
   print("end get_field_mp_meta_data_DF")
   return(field_mp_meta_data_DF_polished)
 }
+
+###############################################################################
+################ get data from Aeris JULY 2023 ################################
+###############################################################################
+
+# "Aeris" data = N2O/CO2 concentrations stored on Aeris analyser 
+# function needs R dplyr package (load in main script)
+
+get_aeris_raw_data <- function(mainDir, dataDir, expYear){
+  
+  print("start get_aeris_raw_data")
+  # import data from Aeris (N2O and CO2 fluxes)
+  if (expYear == "2023"){
+    inputFile <- "20230709-17_stordalen_aeris-n2o_data.csv"
+  } else if (expYear == "2024"){
+    print("NOT YET available")
+  }
+  inputFile      <- paste0(dataDir, "/" ,expYear, "/", inputFile)
+  aeris_raw_data <- read.csv(inputFile)
+  
+  
+  # polish dataframe
+  aeris_data_polished <- aeris_raw_data %>%
+    # remove columns that are not needed
+    select(-2:-4, -8, -9) %>% 
+    # rename columns
+    rename(datetime_aeris=1, co2ppm=2, h2oppm=3, n2oppm=4) %>% 
+    # change date to POSIXct and characters to numeric
+    mutate(
+      datetime_aeris = as.POSIXct(datetime_aeris, format="%m/%d/%Y %H:%M:%OS", tz="UTC"),
+      mutate(across(where(is.character), as.numeric))
+    ) 
+  
+  # change format to UTC
+  # aeris_data_polished$datetime_aeris <- format(aeris_data_polished$datetime_aeris, 
+  #                                             "%m/%d/%Y %H:%M:%OS %Z", tz="UTC")
+  
+  setwd(mainDir)
+  print("end get_aeris_raw_data")
+  return(aeris_data_polished)
+}
diff --git a/preprocessing/functions/sort_ch4_conc_by_meas_period_2023.R b/preprocessing/functions/sort_ch4_conc_by_meas_period_2023.R
index c29d622..323bade 100644
--- a/preprocessing/functions/sort_ch4_conc_by_meas_period_2023.R
+++ b/preprocessing/functions/sort_ch4_conc_by_meas_period_2023.R
@@ -57,7 +57,7 @@ create_ch4_conc_sorted_by_mp  <- function(mainDir, dataDir, expYear){
     # ... look for start of the measurement period 10 seconds later (= add) 
     if (length(first_row) < 1){
       print("search start after ..")
-      for (i in 1:10){
+      for (i in 1:20){
         print(paste0("adding start time by: ", i))
         added_start_time <- meas_start_end$datetime_start + as.difftime(i, units="secs", tz = "UTC")
         first_row <- which(fluxMetaLicor$datetime_datalogger == added_start_time)
@@ -76,7 +76,7 @@ create_ch4_conc_sorted_by_mp  <- function(mainDir, dataDir, expYear){
     # look for start of the measurement period 15 seconds earlier (= reduce) 
     if (length(last_row) < 1){
       print("searching end ..")
-      for (i in 1:15){
+      for (i in 1:20){
         print(paste0("reducing end time by: ", i))
         reduced_end_time <- meas_start_end$datetime_end - as.difftime(i, units="secs", tz = "UTC")
         last_row <- which(fluxMetaLicor$datetime_datalogger == reduced_end_time)
diff --git a/preprocessing/functions/sort_co2_conc_by_meas_period_2023.R b/preprocessing/functions/sort_co2_conc_by_meas_period_2023.R
index f2b3304..028bca4 100644
--- a/preprocessing/functions/sort_co2_conc_by_meas_period_2023.R
+++ b/preprocessing/functions/sort_co2_conc_by_meas_period_2023.R
@@ -17,7 +17,7 @@ create_co2_conc_sorted_by_mp  <- function(mainDir, dataDir, expYear){
   # VECTOR SOLUTION: fluxMetaLicor <- c("datetime_datalogger", "co2ppm", "champreskPa", "chamtempdegC") 
   fluxMetaLicor <- ch4_co2_n2o_conc_st_sm_par_data %>%
     select("datetime_datalogger", "plot", "co2ppm_licor", "co2ppm_aeris", 
-           "par", 
+           "par", "h2oppm_licor",
            "soilT1degC", "soilT2degC", "soilT3degC", "soilT4degC",
            "vwc12", "vwc30")
   
@@ -59,7 +59,7 @@ create_co2_conc_sorted_by_mp  <- function(mainDir, dataDir, expYear){
     # ... look for start of the measurement period 20 seconds later (= add) 
     if (length(first_row) < 1){
       print("search start after ..")
-      for (i in 1:10){
+      for (i in 1:20){
         print(paste0("adding start time by: ", i))
         added_start_time <- meas_start_end$datetime_start + as.difftime(i, units="secs", tz = "UTC")
         first_row <- which(fluxMetaLicor$datetime_datalogger == added_start_time)
@@ -78,7 +78,7 @@ create_co2_conc_sorted_by_mp  <- function(mainDir, dataDir, expYear){
     # look for start of the measurement period 15 seconds earlier (= reduce) 
     if (length(last_row) < 1){
       print("searching end ..")
-      for (i in 1:15){
+      for (i in 1:20){
         print(paste0("reducing end time by: ", i))
         reduced_end_time <- meas_start_end$datetime_end - as.difftime(i, units="secs", tz = "UTC")
         last_row <- which(fluxMetaLicor$datetime_datalogger == reduced_end_time)
diff --git a/preprocessing/functions/sort_n2o_conc_by_meas_period.R b/preprocessing/functions/sort_n2o_conc_by_meas_period.R
index ac1aa1a..3f68d53 100644
--- a/preprocessing/functions/sort_n2o_conc_by_meas_period.R
+++ b/preprocessing/functions/sort_n2o_conc_by_meas_period.R
@@ -18,11 +18,18 @@ create_n2o_data_sorted_by_measurements  <- function(mainDir, dataDir, expYear){
   fluxMetaAeris <- n2o_co2_conc_data %>%
     select("datetime_aeris", "n2oppm")
   # get air temperature, pressure, and homidity from datalogger, i.e., from ch4_co2_conc_st_sm_par_data
-  fluxMetaLicor <- ch4_co2_conc_st_sm_par_data %>%
-    select("datetime_datalogger", "co2ppm", "airTdegC", "airRH", "airpreshPa")
+  # 2022
+  #fluxMetaLicor <- ch4_co2_conc_st_sm_par_data %>%
+    #select("datetime_datalogger", "co2ppm", "airTdegC", "airRH", "airpreshPa")
+  # July 2023
+  fluxMetaLicor <- ch4_co2_n2o_conc_st_sm_par_data %>%
+    select("datetime_datalogger", "co2ppm_licor", "airTdegC")
   
   ## get row numbers in field meta-data DF with data from "Aeris measurement periods"
-  rowNum_aeris_mp <- which(field_mp_meta_data_DF$measurement_device == "aeris")
+  # 2022
+  # rowNum_aeris_mp <- which(field_mp_meta_data_DF$measurement_device == "aeris")
+  # 2023
+  rowNum_aeris_mp <- which(field_mp_meta_data_DF$measurement_device == "aeris_7810")
   print(paste0("number of measurement periods with aeris: ", length(rowNum_aeris_mp)))
   print("")
   
@@ -44,13 +51,19 @@ create_n2o_data_sorted_by_measurements  <- function(mainDir, dataDir, expYear){
     # get correct according start of measurement period in case there is a time lag between the instruments
     # look for start of the measurement period 15 seconds earlier (= reduce) 
     if (length(first_row) < 1){
-      print("searching start..")
+      print("searching start after..")
       for (i in 1:10){
-        print(paste0("reducing start time by: ", i))
-        reduced_start_time <- meas_start_end$datetime_start - as.difftime(15, units="secs", tz = "UTC")
-        first_row_reduced <- which(fluxMetaAeris$datetime_aeris == reduced_start_time)
-        if (length(last) == 1){
-          print(paste0("found ", reduced_start_time))
+        print(paste0("adding start time by: ", i))
+        # 2022
+        #reduced_start_time <- meas_start_end$datetime_start - as.difftime(15, units="secs", tz = "UTC")
+        # reduced_start_time <- meas_start_end$datetime_start - as.difftime(i, units="secs", tz = "UTC")
+        # 2022
+        #first_row_reduced <- which(fluxMetaAeris$datetime_aeris == reduced_start_time)
+        # 2023
+        added_start_time <- meas_start_end$datetime_start + as.difftime(i, units="secs", tz = "UTC")
+        first_row <- which(fluxMetaAeris$datetime_aeris == added_start_time)
+        if (length(first_row) == 1){
+          print(paste0("found ", added_start_time))
           break
         }
       }
@@ -65,7 +78,10 @@ create_n2o_data_sorted_by_measurements  <- function(mainDir, dataDir, expYear){
       print("searching end ..")
       for (i in 1:15){
         print(paste0("reducing end time by: ", i))
-        reduced_end_time <- meas_start_end$datetime_end - as.difftime(30, units="secs", tz = "UTC")
+        # 2022
+        #reduced_end_time <- meas_start_end$datetime_end - as.difftime(30, units="secs", tz = "UTC")
+        # 2023
+        reduced_end_time <- meas_start_end$datetime_end - as.difftime(i, units="secs", tz = "UTC")
         last_row <- which(fluxMetaAeris$datetime_aeris == reduced_end_time)
         if (length(last) == 1){
           print(paste0("found ", reduced_end_time))
diff --git a/preprocessing/functions/sort_n2o_conc_by_meas_period_2023.R b/preprocessing/functions/sort_n2o_conc_by_meas_period_2023.R
index fabd6e2..04ecf76 100644
--- a/preprocessing/functions/sort_n2o_conc_by_meas_period_2023.R
+++ b/preprocessing/functions/sort_n2o_conc_by_meas_period_2023.R
@@ -57,7 +57,7 @@ create_n2o_conc_sorted_by_measurements  <- function(mainDir, dataDir, expYear){
     # ... look for start of the measurement period 10 seconds later (= add) 
     if (length(first_row) < 1){
       print("search start after ..")
-      for (i in 1:10){
+      for (i in 1:20){
         print(paste0("adding start time by: ", i))
         added_start_time <- meas_start_end$datetime_start + as.difftime(i, units="secs", tz = "UTC")
         first_row <- which(fluxMetaLicor$datetime_datalogger == added_start_time)
diff --git a/preprocessing/main_script.R b/preprocessing/main_script.R
index 14eb1fb..2437b9c 100644
--- a/preprocessing/main_script.R
+++ b/preprocessing/main_script.R
@@ -42,6 +42,13 @@ source("functions/read_raw_data_2023.R")
 ch4_co2_n2o_conc_st_sm_par_data <- get_datalogger_raw_data(mainDir, dataDir, expYear) 
 field_mp_meta_data_DF           <- get_field_mp_meta_data_DF(mainDir, dataDir, expYear)
 
+# July 2023
+# There were problems with the connection between the data logger and the Aeris
+# Only data for CH4 + CO2 25 June, 29 June, and 3 July (= no N2O data)
+# 9, 10, 13, 16, 17 July: CH4 + CO2 data on data logger, N2O in Aeris SD 
+# 22 - 27 July: CH4, CO2 + N2O data on data logger
+n2o_co2_conc_data           <- get_aeris_raw_data(mainDir, dataDir, expYear)
+
 
 ## 2022 #################
 source("functions/read_raw_data.R")
@@ -118,8 +125,6 @@ source("functions/calc_co2_flux_licor_all_measurement_periods.R")
 
 co2Fluxes                   <- calculate_co2_fluxes_licor_all_mps(measPeriodMetaDataDF, measPeriodCo2_corr)
 
-
-
 # important!!! all is in UTC but needs to be in Sweden time in the end
 
 
-- 
GitLab