Last updated: 2020-04-06

Checks: 7 0

Knit directory: Baltic_Productivity/

This reproducible R Markdown analysis was created with workflowr (version 1.6.1). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20191017) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version 5d352db. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    data/ARGO/
    Ignored:    data/Finnmaid/
    Ignored:    data/GETM/
    Ignored:    data/OSTIA/
    Ignored:    data/_merged_data_files/
    Ignored:    data/_summarized_data_files/

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/CT.Rmd) and HTML (docs/CT.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd 5d352db jens-daniel-mueller 2020-04-06 calculated CT in equilibrium with atmosphere and updated CT dygraph
Rmd 662c74a jens-daniel-mueller 2020-04-03 added explanatory comments
Rmd c619811 LSBurchardt 2020-04-03 #3 comments
html a4ab61e jens-daniel-mueller 2020-03-31 Build site.
Rmd 1518256 jens-daniel-mueller 2020-03-31 updated dygraphs
html 67a6e2b jens-daniel-mueller 2020-03-31 Build site.
Rmd 5e275d2 jens-daniel-mueller 2020-03-31 CT and flux calculation included
Rmd ef65647 LSBurchardt 2020-03-31 #3 flux calculations and graphs prepared, calculations are not running on my PC though
Rmd 3f94314 LSBurchardt 2020-03-27 #3 windspeed extraction working
Rmd 2ca3f71 LSBurchardt 2020-03-23 #3 extracting S,T, pco2 for all routes; windspeed from GETM with problem
html abc5f7d jens-daniel-mueller 2020-03-18 Build site.
Rmd 10176c9 jens-daniel-mueller 2020-03-18 knit after Lara updated CT
html b80d3a6 jens-daniel-mueller 2020-03-18 Build site.
Rmd 8023757 jens-daniel-mueller 2020-03-18 knit after Lara updated CT
Rmd 03ea2de LSBurchardt 2020-03-18 #1 ready to knit to html: CT with Plot, probably little more explanatory background needed for webside
html 03646b4 jens-daniel-mueller 2020-03-16 Build site.
html 6394f58 jens-daniel-mueller 2020-03-16 Build site.
Rmd 9b0eb49 LSBurchardt 2020-03-15 #1 ready to knit html: SSS working, CT, MLD_pCO2 and SST updated to new data
html e1acb2d jens-daniel-mueller 2020-03-09 Build site.
html c15b71c jens-daniel-mueller 2020-03-09 Build site.
Rmd 9332cae jens-daniel-mueller 2020-03-09 restructered content into chapters, rebuild site, except SSS
Rmd fa8ef89 Burchardt 2020-02-28 #1 new CT.rmd and sss.rmd
Rmd 1663eee Burchardt 2020-02-13 #1 CT new

library(tidyverse)
library(ncdf4)
library(vroom)
library(lubridate)
library(geosphere)
library(dygraphs)
library(xts)
library(here)
library(seacarb)
library(zoo)
# route
select_route <- c("E", "F", "G", "W", "X") 

# variable names in 2d and 3d GETM files
var <- "SSS_east"

# latitude limits
low_lat <- 57.5
high_lat <- 58.8

1 Regional mean salinity

The mean salinity was calculated across all measurments made between march - september in the NGS subregion.

nc <- nc_open(paste("data/Finnmaid/", "FM_all_2019_on_standard_tracks.nc", sep = ""))

# read required vectors from netcdf file
route <- ncvar_get(nc, "route")
route <- unlist(strsplit(route, ""))
date_time <- ncvar_get(nc, "time")
latitude_east <- ncvar_get(nc, "latitude_east")

array <- ncvar_get(nc, var) # store the data in a 2-dimensional array
#dim(array) # should have 2 dimensions: 544 coordinate, 2089 time steps
  
  fillvalue <- ncatt_get(nc, var, "_FillValue")
  array[array == fillvalue$value] <- NA
  rm(fillvalue)
  
  #i <- 5
  for (i in seq(1,length(route),1)){
  
      
    if(route[i] %in% select_route) {
      slice <- array[i,]
      
      value <- mean(slice[latitude_east > low_lat & latitude_east < high_lat], na.rm = TRUE)
      sd    <- sd(slice[latitude_east > low_lat & latitude_east < high_lat], na.rm = TRUE)
      date <- ymd("2000-01-01") + date_time[i]
      
      temp <- bind_cols(date = date, var=var, value = value, sd = sd)
      
      if (exists("timeseries", inherits = FALSE)){
        timeseries <- bind_rows(timeseries, temp)
      } else{timeseries <- temp}
      
      rm(temp, value, date, sd)
      
    } 
  }
nc_close(nc)

fm_sss__ngs <- timeseries %>% 
  mutate(sss = value,
         year = year(date),
         month = month(date))

fm_sss_ngs_monthlymean <- fm_sss__ngs %>% 
  filter(month >=3 , month <=9) %>% 
  summarise(sss_mean = mean(sss, na.rm = TRUE))


rm(array,fm_sss__ngs,nc, timeseries, date_time, 
  i, latitude_east, route, slice, var)

The mean salinity between March and September for the NGS subregion for all years is 6.67.

2 Finnmaid data extraction

pCO2 and SST observations in NGS were extracted for all crossings.

nc <- nc_open(paste("data/Finnmaid/", "FM_all_2019_on_standard_tracks.nc", sep = ""))

# read required vectors from netcdf file
route <- ncvar_get(nc, "route")
route <- unlist(strsplit(route, ""))
date_time <- ncvar_get(nc, "time")
latitude_east <- ncvar_get(nc, "latitude_east")

for (var in c("SST_east", "pCO2_east")) {
  
  #print(var)
  
  array <- ncvar_get(nc, var) # store the data in a 2-dimensional array
  #dim(array) # should have 2 dimensions: 544 coordinate, 2089 time steps
  
  fillvalue <- ncatt_get(nc, var, "_FillValue")
  array[array == fillvalue$value] <- NA
  rm(fillvalue)
  
    for (i in seq(1,length(route),1)){
  
      
    if(route[i] %in% select_route) {
      slice <- array[i,]
      
      value <- mean(slice[latitude_east > low_lat & latitude_east < high_lat], na.rm = TRUE)
      sd    <- sd(slice[latitude_east > low_lat & latitude_east < high_lat], na.rm = TRUE)
      date <- ymd("2000-01-01") + date_time[i]
      
      fm_ngs_all_routes_part <- bind_cols(date = date, var=var, value = value, sd = sd, route=route[i])
      
      if (exists("fm_ngs_all_routes", inherits = FALSE)){
        fm_ngs_all_routes <- bind_rows(fm_ngs_all_routes, fm_ngs_all_routes_part)
      } else{fm_ngs_all_routes <- fm_ngs_all_routes_part}
      
      rm(fm_ngs_all_routes_part, value, date, sd, slice)
      
      } 
    }
  rm(array, var,i)
}   
      
nc_close(nc)

fm_ngs_all_routes %>%  
  vroom_write(here::here("data/_summarized_data_files/", file = "fm_ngs_all_routes.csv"))

rm(nc, fm_ngs_all_routes, latitude_east, route,date_time)

3 GETM windspeed

Reanalysis windspeed data as used in the GETM model run were used.

var_all <- c("u10", "v10")
filesList_2d <- list.files(path= "data", pattern = "Finnmaid.E.2d.20", recursive = TRUE)

file <- filesList_2d[1]
nc <- nc_open(paste("data/", file, sep = ""))

lon <- ncvar_get(nc, "lonc")
lat <- ncvar_get(nc, "latc", verbose = F)
nc_close(nc)

rm(file, nc)

for (var in var_all){

for (n in 1:length(filesList_2d)) {

file <- filesList_2d[n]

nc <- nc_open(paste("data/", file, sep = ""))

time_units <- nc$dim$time$units %>%     #we read the time unit from the netcdf file to calibrate the time 
    substr(start = 15, stop = 33) %>%   #calculation, we take the relevant information from the string
    ymd_hms()                           # and transform it to the right format

t <- time_units + ncvar_get(nc, "time")

array <- ncvar_get(nc, var) # store the data in a 2-dimensional array
dim(array) # should be 2d with dimensions: 544 coordinate, 31d*(24h/d/3h)=248 time steps

array <- as.data.frame(t(array), xy=TRUE)
array <- as_tibble(array)

gt_windspeed_ngs_part <- array %>%
  set_names(as.character(lat)) %>%
  mutate(date_time = t) %>%
  gather("lat", "value", 1:length(lat)) %>%
  mutate(lat = as.numeric(lat)) %>%
  filter(lat > low_lat, lat<high_lat) %>%
  group_by(date_time) %>%
  summarise_all("mean") %>%
  ungroup() %>%
  mutate(var = var)


if (exists("gt_windspeed_ngs")) {gt_windspeed_ngs <- bind_rows(gt_windspeed_ngs, gt_windspeed_ngs_part)
  }else {gt_windspeed_ngs <- gt_windspeed_ngs_part}

  nc_close(nc)
  rm(array, nc, t, gt_windspeed_ngs_part)
  print(n) # to see working progress
}

print(paste("gt_", var, "_ngs.csv", sep = "")) # to see working progress


rm(n, file, time_units)
}

rm(filesList_2d, var, var_all, lat, lon)


gt_windspeed_ngs <- gt_windspeed_ngs %>% 
  group_by(date_time, var) %>% 
  summarise(mean_value= mean(value)) %>% 
  pivot_wider(values_from = mean_value, names_from = var) %>% 
  mutate(U_10 = round(sqrt(u10^2 + v10^2), 3)) %>% 
  select(-c(u10, v10))

gt_windspeed_ngs %>% 
  vroom_write(here::here("data/_summarized_data_files/", file = paste("gt_windspeed_ngs.csv", sep = "")))

rm(gt_windspeed_ngs)

4 CT calculation

CT was calculated from measured pCO2 based on a fixed mean alkalinity value of 1650 µmol kg-1.

df <- vroom::vroom(here::here("data/_summarized_data_files/", file = "fm_ngs_all_routes.csv"))

df <- df %>% 
  select(date, var, value, route) 

df <- df %>%
 pivot_wider(values_from = value, names_from = var) %>% 
  drop_na()

#df <- df%>%
#  mutate(sal = SSS_east,
#         tem = SST_east,
#         pCO2 = pCO2_east) %>%
#  select(pCO2,tem, sal, date)

# not enough RAM for pivot_wider
# alternative with "filter"

# df_sst <- df %>% 
#   filter( var == "SST_east") %>% 
#   mutate( tem = value) %>% 
#   select (tem)
# 
# df_sss <- df %>% 
#   filter( var == "SSS_east") %>% 
#   mutate( sal = value) %>% 
#   select( sal)
# 
# df_pco2 <- df %>% 
#   filter( var == "pCO2_east") %>% 
#   mutate (pCO2 = value) %>% 
#   select( pCO2, date)
# 
# df <- cbind(df_pco2, df_sst, df_sss) %>% 
#   select(date, tem, sal, pCO2)

#calculation of CT based on pCO2 (var1) and alkalinity (var2) as input parameters
#calculation of CT in theoretical equilibrium with atmosphere (CT_equi) based on pCO2_air (var1) and alkalinity (var2) as input parameters

df <- df %>%
  rename(SST = SST_east,
         pCO2 = pCO2_east) %>% 
  mutate(CT = carb(24,
                   var1=pCO2,
                   var2=1650*1e-6,
                   S=pull(fm_sss_ngs_monthlymean),
                   T=SST,
                   k1k2="m10", kf="dg", ks="d", gas="insitu")[,16]*1e6) %>% 
  mutate(year = year(date),
         pCO2_air = 400 - 2*(2015-year),
         CT_equi = carb(24,
                   var1=pCO2_air,
                   var2=1650*1e-6,
                   S=pull(fm_sss_ngs_monthlymean),
                   T=SST,
                   k1k2="m10", kf="dg", ks="d", gas="insitu")[,16]*1e6) %>% 
  select(-c(pCO2_air, year))


df %>% 
   vroom_write(here::here("data/_summarized_data_files/", file = "fm_CT_ngs.csv"))

rm(df)

5 Air-sea CO2 flux

The CO2 flux across the sea surface was calculated according to Wanninkhof (2014).

df_1 <- vroom::vroom(here::here("data/_summarized_data_files/", file = "fm_CT_ngs.csv"))
df_2 <- vroom::vroom(here::here("data/_summarized_data_files/", file = "gt_windspeed_ngs.csv"))

df_2$date_time <- round_date(df_2$date_time, unit = "day")
df_2 <- df_2 %>% 
  mutate(date = as.Date(date_time)) %>% 
  select(date, U_10) %>% 
  group_by(date) %>% 
  summarise_all("mean") %>% 
  ungroup()


df <- full_join(df_1, df_2, by = "date") %>% 
  arrange(date)
rm(df_1,df_2)

df <- df %>% 
  mutate(year = year(date),
         pCO2_int = na.approx(pCO2, na.rm = FALSE),                  #na.approx: replacing NA with interpolated values
         SST_int = na.approx(SST, na.rm = FALSE)) %>% 
  filter(!is.na(pCO2_int))

#Calculation of the Schmidt number as a funktion of temperature according to Wanninkhof (2014)
Sc_W14 <- function(tem) {
  2116.8 - 136.25 * tem + 4.7353 * tem^2 - 0.092307 * tem^3 + 0.0007555 * tem^4
}

Sc_W14(20)

# calculate flux F [mol m–2 d–1]
df <- df %>%
  mutate(pCO2_air = 400 - 2*(2015-year),
         dpCO2 = pCO2_int - pCO2_air,
         dCO2  = dpCO2 * K0(S=pull(fm_sss_ngs_monthlymean), T=SST_int),
         k     = 0.251 * U_10^2 * (Sc_W14(SST_int)/660)^(-0.5),
         flux_daily = k*dCO2*1e-5*24)


df %>% 
   vroom_write(here::here("data/_merged_data_files/", file = paste("gt_fm_flux_ngs.csv", sep = "")))

rm(df, Sc_W14)

6 Time series CT, MLD, SST, windspeed, and air-sea fluxes

# read CT and flux data
gt_fm_flux_ngs <- vroom::vroom(here::here("data/_merged_data_files/", file = "gt_fm_flux_ngs.csv"))

ts_xts_CT <- xts(cbind(gt_fm_flux_ngs$CT, gt_fm_flux_ngs$CT_equi), order.by = gt_fm_flux_ngs$date)
names(ts_xts_CT) <- c("CT", "CT_equi")

ts_xts_SST <- xts(gt_fm_flux_ngs$SST, order.by = gt_fm_flux_ngs$date)
names(ts_xts_SST) <- "SST"

ts_xts_windspeed <- xts(gt_fm_flux_ngs$U_10, order.by = gt_fm_flux_ngs$date)
names(ts_xts_windspeed) <- "Windspeed"

ts_xts_flux <- xts(gt_fm_flux_ngs$flux_daily, order.by = gt_fm_flux_ngs$date)
names(ts_xts_flux) <- "Daily Flux"


# read MLD data
gt_mld_fm_pco2_ngs <-
vroom::vroom(here::here("data/_merged_data_files/", file = "gt_mld_fm_pco2_ngs.csv"))

ts_xts_mld5 <- xts(gt_mld_fm_pco2_ngs$value_mld5, order.by = gt_mld_fm_pco2_ngs$date)
names(ts_xts_mld5) <- "mld_age_5"

ts_xts_CT %>% 
  dygraph(group = "Fluxes") %>% 
  dyRangeSelector(dateWindow = c("2014-01-01", "2016-12-31")) %>% 
  dySeries("CT") %>% 
  dyAxis("y", label = "CT") %>% 
  dyOptions(drawPoints = TRUE, pointSize = 1.5, connectSeparatedPoints=TRUE, strokeWidth=0.5,
            drawAxesAtZero=TRUE)
ts_xts_SST %>% 
  dygraph(group = "Fluxes") %>% 
  dyRangeSelector(dateWindow = c("2014-01-01", "2016-12-31")) %>% 
  dySeries("SST") %>% 
  dyAxis("y", label = "SST") %>% 
  dyOptions(drawPoints = TRUE, pointSize = 1.5, connectSeparatedPoints=TRUE, strokeWidth=0.5,
            drawAxesAtZero=TRUE)
ts_xts_mld5 %>% 
  dygraph(group = "Fluxes") %>% 
  dyRangeSelector(dateWindow = c("2014-01-01", "2016-12-31")) %>% 
  dySeries("mld_age_5") %>% 
  dyAxis("y", label = "mld_age_5") %>% 
  dyOptions(drawPoints = TRUE, pointSize = 1.5, connectSeparatedPoints=TRUE, strokeWidth=0.5,
            drawAxesAtZero=TRUE)
ts_xts_windspeed %>% 
  dygraph(group = "Fluxes") %>% 
  dyRangeSelector(dateWindow = c("2014-01-01", "2016-12-31")) %>% 
  dySeries("Windspeed") %>% 
  dyAxis("y", label = "Windspeed [m/s]") %>% 
  dyOptions(drawPoints = TRUE, pointSize = 1.5, connectSeparatedPoints=TRUE, strokeWidth=0.5,
            drawAxesAtZero=TRUE)
ts_xts_flux %>% 
  dygraph(group = "Fluxes") %>% 
  dyRangeSelector(dateWindow = c("2014-01-01", "2016-12-31")) %>% 
  dySeries("Daily Flux") %>% 
  dyAxis("y", label = "Daily Flux") %>% 
  dyOptions(drawPoints = TRUE, pointSize = 1.5, connectSeparatedPoints=TRUE, strokeWidth=0.5)
rm(gt_fm_flux_ngs, gt_windspeed_ngs, fm_CT_ngs, ts_xts_CT, ts_xts_flux, ts_xts_windspeed)

sessionInfo()
R version 3.5.0 (2018-04-23)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_Germany.1252  LC_CTYPE=English_Germany.1252   
[3] LC_MONETARY=English_Germany.1252 LC_NUMERIC=C                    
[5] LC_TIME=English_Germany.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] seacarb_3.2.12   oce_1.2-0        gsw_1.0-5        testthat_2.3.1  
 [5] here_0.1         xts_0.11-2       zoo_1.8-6        dygraphs_1.1.1.6
 [9] geosphere_1.5-10 lubridate_1.7.4  vroom_1.2.0      ncdf4_1.17      
[13] forcats_0.4.0    stringr_1.4.0    dplyr_0.8.3      purrr_0.3.3     
[17] readr_1.3.1      tidyr_1.0.0      tibble_2.1.3     ggplot2_3.3.0   
[21] tidyverse_1.3.0 

loaded via a namespace (and not attached):
 [1] httr_1.4.1        bit64_0.9-7       jsonlite_1.6      modelr_0.1.5     
 [5] assertthat_0.2.1  sp_1.3-2          cellranger_1.1.0  yaml_2.2.0       
 [9] pillar_1.4.2      backports_1.1.5   lattice_0.20-35   glue_1.3.1       
[13] digest_0.6.22     promises_1.1.0    rvest_0.3.5       colorspace_1.4-1 
[17] htmltools_0.4.0   httpuv_1.5.2      pkgconfig_2.0.3   broom_0.5.3      
[21] haven_2.2.0       scales_1.0.0      whisker_0.4       later_1.0.0      
[25] git2r_0.26.1      generics_0.0.2    withr_2.1.2       cli_1.1.0        
[29] magrittr_1.5      crayon_1.3.4      readxl_1.3.1      evaluate_0.14    
[33] fs_1.3.1          fansi_0.4.0       nlme_3.1-137      xml2_1.2.2       
[37] tools_3.5.0       hms_0.5.2         lifecycle_0.1.0   munsell_0.5.0    
[41] reprex_0.3.0      compiler_3.5.0    rlang_0.4.5       grid_3.5.0       
[45] rstudioapi_0.10   htmlwidgets_1.5.1 rmarkdown_2.0     gtable_0.3.0     
[49] DBI_1.0.0         R6_2.4.0          knitr_1.26        bit_1.1-14       
[53] zeallot_0.1.0     utf8_1.1.4        workflowr_1.6.1   rprojroot_1.3-2  
[57] stringi_1.4.3     parallel_3.5.0    Rcpp_1.0.2        vctrs_0.2.0      
[61] dbplyr_1.4.2      tidyselect_0.2.5  xfun_0.10