Last updated: 2020-03-31

Checks: 7 0

Knit directory: Baltic_Productivity/

This reproducible R Markdown analysis was created with workflowr (version 1.6.0). 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 version displayed above was the version of the Git repository at the time these results were generated.

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/_merged_data_files_2019V1/
    Ignored:    data/_summarized_data_files/
    Ignored:    data/_summarized_data_files_2019V1/

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.


There are no past versions. Publish this analysis with wflow_publish() to start tracking its development.


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)


pull(fm_sss_ngs_monthlymean)

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)


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),
         SST_int = na.approx(SST, na.rm = FALSE)) %>% 
  filter(!is.na(pCO2_int))

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(gt_fm_flux_ngs$CT, order.by = gt_fm_flux_ngs$date)
names(ts_xts_CT) <- "CT"

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