Last updated: 2021-06-07

Checks: 7 0

Knit directory: emlr_mod_preprocessing/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). 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(20200707) 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 360a061. 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/

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/Cant_CB.Rmd) and HTML (docs/Cant_CB.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
html 72ba3bc jens-daniel-mueller 2021-06-02 Build site.
Rmd 125387e jens-daniel-mueller 2021-06-02 rerun all with GLODAPv2.2021 beta subset, and march2021 cmorization
html 45a1c9e jens-daniel-mueller 2021-05-20 Build site.
html 6aa4f34 Donghe-Zhu 2021-02-06 Build site.
html 2eb6652 Donghe-Zhu 2021-01-27 Build site.
Rmd e36b6ae Donghe-Zhu 2021-01-20 zonal mean plot for offset
html 843587f Donghe-Zhu 2021-01-11 Build site.
Rmd 0269854 Donghe-Zhu 2021-01-10 adding constant climate for regular and random sampling

# use only three basin to assign general basin mask
# ie this is not specific to the MLR fitting
basinmask <- basinmask %>%
  filter(MLR_basins == "2") %>%
  select(lat, lon, basin_AIP)

1 Calculate annual Cant field

1.1 Read in Run C

# read in cmorized constant forcing model file
C_annual <- tidync(paste(path_cmorized,
                         "RECCAP2_RunC.nc",
                         sep = ""))

C_annual <- C_annual %>% hyper_tibble()


# harmonize column names and coordinates
C_annual <- C_annual  %>%
  select(
    year = time_ann,
    lon,
    lat,
    depth,
    tco2_C = dissic,
    sal = so,
    theta = thetao
  ) %>%
  # compute year from days since Jan 1, 1980
  mutate(year = (year - 181) / 365 + 1980) %>%
  mutate(lon = if_else(lon < 20, lon + 360, lon))

# calculate model temperature
C_annual <- C_annual %>%
  mutate(temp = gsw_pt_from_t(
    SA = sal,
    t = theta,
    p = 10.1325,
    p_ref = depth
  ))

# unit transfer from mol/m3 to µmol/kg
C_annual <- C_annual %>%
  mutate(
    rho = gsw_pot_rho_t_exact(
      SA = sal,
      t = temp,
      p = depth,
      p_ref = 10.1325
    ),
    tco2_C = tco2_C * (1e+6 / rho)
  ) %>%
  select(year, lon, lat, depth, tco2_C)

1.2 Read in Run

# read in cmorized constant forcing model file
B_annual <- tidync(paste(path_cmorized,
                         "RECCAP2_RunB.nc",
                         sep = ""))

B_annual <- B_annual %>% hyper_tibble()

# harmonize column names and coordinates
B_annual <- B_annual  %>%
  select(
    year = time_ann,
    lon,
    lat,
    depth,
    tco2_B = dissic,
    sal = so,
    theta = thetao
  ) %>%
  # compute year from days since Jan 1, 1980
  mutate(year = (year - 181) / 365 + 1980) %>%
  mutate(lon = if_else(lon < 20, lon + 360, lon))

# calculate model temperature
B_annual <- B_annual %>%
  mutate(temp = gsw_pt_from_t(
    SA = sal,
    t = theta,
    p = 10.1325,
    p_ref = depth
  ))

# unit transfer from mol/m3 to µmol/kg
B_annual <- B_annual %>%
  mutate(
    rho = gsw_pot_rho_t_exact(
      SA = sal,
      t = temp,
      p = depth,
      p_ref = 10.1325
    ),
    tco2_B = tco2_B * (1e+6 / rho)
  ) %>%
  select(year, lon, lat, depth, tco2_B)

1.3 Join tco2 fields

cant <- inner_join(C_annual, B_annual)
rm(C_annual, B_annual)

1.4 Round depth levels

cant <- cant %>%
  mutate(depth = round(depth))

1.5 Apply basin mask

# restrict Cant field to basin mask grid
cant <- inner_join(cant, basinmask)

1.6 Calculate Cant total

cant <- cant %>%
  mutate(cant_total = tco2_C - tco2_B) %>% 
  select(-c(tco2_C, tco2_B))

1.7 Write Cant files

# write annual Cant files
years <- c(1982:2019)

for (i_year in years) {
  
 # i_year = years[1]
  cant_year <- cant %>%
    filter(year == i_year)
  
  cant_year %>%
    write_csv(paste(path_preprocessing,
                    "cant_annual_field_CB/cant_", i_year, ".csv",
                    sep = ""))
}

2 Calculate change in Cant 1994 - 2007

cant_1994 <- cant %>%
  filter(year == 1994) %>%
  select(-c(year)) %>%
  rename(cant_1994 = cant_total)

cant_2007 <- cant %>%
  filter(year == 2007) %>%
  select(-c(year)) %>%
  rename(cant_2007 = cant_total)

cant_gruber <- left_join(cant_1994, cant_2007) %>%
  mutate(cant = cant_2007 - cant_1994)

cant_gruber <- cant_gruber %>% 
   mutate(cant_pos = if_else(cant <= 0, 0, cant))

rm(cant_1994, cant_2007)

3 Zonal mean sections

cant_gruber <- cant_gruber %>% 
  mutate(eras = "JGOFS/WOCE")

cant_gruber_zonal <- m_zonal_mean_section(cant_gruber)

3.1 1994 - 2007

for (i_basin_AIP in unique(cant_gruber_zonal$basin_AIP)) {
  print(
    p_section_zonal(
      cant_gruber_zonal %>% filter(basin_AIP == i_basin_AIP),
      var = "cant_pos_mean",
      plot_slabs = "n",
      subtitle_text = paste("Basin: ", i_basin_AIP)
    )
  )
}

Version Author Date
843587f Donghe-Zhu 2021-01-11

Version Author Date
45a1c9e jens-daniel-mueller 2021-05-20
843587f Donghe-Zhu 2021-01-11

Version Author Date
843587f Donghe-Zhu 2021-01-11

3.2 Cant total 1994

for (i_basin_AIP in unique(cant_gruber_zonal$basin_AIP)) {
  print(
    p_section_zonal(
      cant_gruber_zonal %>% filter(basin_AIP == i_basin_AIP),
      var = "cant_1994_mean",
      plot_slabs = "n",
      breaks = seq(0,100,10),
      subtitle_text = paste("Basin: ", i_basin_AIP)
    )
  )
}

Version Author Date
2eb6652 Donghe-Zhu 2021-01-27

Version Author Date
45a1c9e jens-daniel-mueller 2021-05-20
2eb6652 Donghe-Zhu 2021-01-27

Version Author Date
2eb6652 Donghe-Zhu 2021-01-27

3.3 Cant total 2007

for (i_basin_AIP in unique(cant_gruber_zonal$basin_AIP)) {
  print(
    p_section_zonal(
      cant_gruber_zonal %>% filter(basin_AIP == i_basin_AIP),
      var = "cant_2007_mean",
      plot_slabs = "n",
      breaks = seq(0,100,10),
      subtitle_text = paste("Basin: ", i_basin_AIP)
    )
  )
}

Version Author Date
843587f Donghe-Zhu 2021-01-11

Version Author Date
45a1c9e jens-daniel-mueller 2021-05-20
843587f Donghe-Zhu 2021-01-11

Version Author Date
843587f Donghe-Zhu 2021-01-11

4 Column inventory

4.1 1994 - 2007

cant_gruber_inv <- m_cant_inv(cant_gruber)
p_map_cant_inv(cant_gruber_inv %>% filter(inv_depth == 3000))

Version Author Date
72ba3bc jens-daniel-mueller 2021-06-02
45a1c9e jens-daniel-mueller 2021-05-20
843587f Donghe-Zhu 2021-01-11

4.2 Cant total 1994

# this is just a work around, because the function is designed to calculate cant inventories, but not cant_total inventories

cant_gruber_total_inv <- m_cant_inv(
  cant_gruber %>% 
    select(-cant_pos) %>% 
    rename(cant_pos = cant_1994))
p_map_cant_inv(cant_gruber_total_inv %>% filter(inv_depth == 3000),
               breaks = seq(0,100,10),
               subtitle_text = "Cant total in 1994")

Version Author Date
72ba3bc jens-daniel-mueller 2021-06-02
45a1c9e jens-daniel-mueller 2021-05-20
2eb6652 Donghe-Zhu 2021-01-27

4.3 Cant total 2007

# this is just a work around, because the function is designed to calculate cant inventories, but not cant_total inventories

cant_gruber_total_inv <- m_cant_inv(
  cant_gruber %>% 
    select(-cant_pos) %>% 
    rename(cant_pos = cant_2007))
p_map_cant_inv(cant_gruber_total_inv %>% filter(inv_depth == 3000),
               breaks = seq(0,100,10),
               subtitle_text = "Cant total in 2007")

Version Author Date
72ba3bc jens-daniel-mueller 2021-06-02
45a1c9e jens-daniel-mueller 2021-05-20
843587f Donghe-Zhu 2021-01-11

sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: openSUSE Leap 15.2

Matrix products: default
BLAS:   /usr/local/R-4.0.3/lib64/R/lib/libRblas.so
LAPACK: /usr/local/R-4.0.3/lib64/R/lib/libRlapack.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
 [1] gsw_1.0-5       testthat_3.0.1  stars_0.4-3     sf_0.9-8       
 [5] abind_1.4-5     tidync_0.2.4    metR_0.9.0      scico_1.2.0    
 [9] patchwork_1.1.1 collapse_1.5.0  forcats_0.5.0   stringr_1.4.0  
[13] dplyr_1.0.5     purrr_0.3.4     readr_1.4.0     tidyr_1.1.2    
[17] tibble_3.0.4    ggplot2_3.3.3   tidyverse_1.3.0 workflowr_1.6.2

loaded via a namespace (and not attached):
 [1] fs_1.5.0                 lubridate_1.7.9          httr_1.4.2              
 [4] rprojroot_2.0.2          tools_4.0.3              backports_1.1.10        
 [7] R6_2.5.0                 KernSmooth_2.23-18       DBI_1.1.0               
[10] colorspace_2.0-0         withr_2.3.0              tidyselect_1.1.0        
[13] compiler_4.0.3           git2r_0.27.1             cli_2.2.0               
[16] rvest_0.3.6              RNetCDF_2.4-2            xml2_1.3.2              
[19] isoband_0.2.3            labeling_0.4.2           scales_1.1.1            
[22] checkmate_2.0.0          classInt_0.4-3           digest_0.6.27           
[25] rmarkdown_2.5            pkgconfig_2.0.3          htmltools_0.5.0         
[28] dbplyr_1.4.4             rlang_0.4.10             readxl_1.3.1            
[31] rstudioapi_0.13          farver_2.0.3             generics_0.1.0          
[34] jsonlite_1.7.2           magrittr_2.0.1           ncmeta_0.3.0            
[37] Matrix_1.2-18            Rcpp_1.0.5               munsell_0.5.0           
[40] fansi_0.4.1              lifecycle_1.0.0          stringi_1.5.3           
[43] whisker_0.4              yaml_2.2.1               grid_4.0.3              
[46] blob_1.2.1               parallel_4.0.3           promises_1.1.1          
[49] crayon_1.3.4             lattice_0.20-41          haven_2.3.1             
[52] hms_0.5.3                knitr_1.30               pillar_1.4.7            
[55] reprex_0.3.0             glue_1.4.2               evaluate_0.14           
[58] RcppArmadillo_0.10.1.2.2 data.table_1.13.6        modelr_0.1.8            
[61] vctrs_0.3.6              httpuv_1.5.4             cellranger_1.1.0        
[64] gtable_0.3.0             assertthat_0.2.1         xfun_0.20               
[67] lwgeom_0.2-5             broom_0.7.5              RcppEigen_0.3.3.9.1     
[70] e1071_1.7-4              later_1.1.0.1            class_7.3-17            
[73] ncdf4_1.17               units_0.6-7              ellipsis_0.3.1