Last updated: 2022-11-02

Checks: 7 0

Knit directory: emlr_mod_preprocessing/

This reproducible R Markdown analysis was created with workflowr (version 1.7.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(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 d0575c0. 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/

Untracked files:
    Untracked:  code/backup_analysis_scripts/Cant_CB_annual_all_models_backup_20220704.Rmd

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_AD_annual_all_models.Rmd) and HTML (docs/Cant_AD_annual_all_models.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 d0575c0 jens-daniel-mueller 2022-11-02 included calculation of cstar variants
html 297418e jens-daniel-mueller 2022-10-24 Build site.
html e829a11 jens-daniel-mueller 2022-07-05 Build site.
Rmd 923e07e jens-daniel-mueller 2022-07-04 rerun for all models
html 791fe64 jens-daniel-mueller 2022-07-04 Build site.
Rmd 5d7c0ea jens-daniel-mueller 2022-07-04 rerun with writing cstar run D fields
html 9fdecac jens-daniel-mueller 2022-06-29 Build site.
Rmd 55a3fb6 jens-daniel-mueller 2022-06-29 rerun with writing dissic fields
html 89cbb74 jens-daniel-mueller 2022-05-26 Build site.
Rmd 379a012 jens-daniel-mueller 2022-05-26 rerun for all models
html 9e85627 jens-daniel-mueller 2022-05-24 Build site.
Rmd 30f99f7 jens-daniel-mueller 2022-05-24 rerun only for planktom12 with factor /100 for po4
html 42bbb89 jens-daniel-mueller 2022-05-24 Build site.
Rmd e82a302 jens-daniel-mueller 2022-05-23 bug fixes for 5 models
html 40863a2 jens-daniel-mueller 2022-05-22 Build site.
Rmd d9d4d68 jens-daniel-mueller 2022-05-21 rebuild with revised output structure and all models
html 31ae80a jens-daniel-mueller 2022-05-20 Build site.
Rmd b6d47dd jens-daniel-mueller 2022-05-20 write files without 3D_ALL insert
html 8cfec6a jens-daniel-mueller 2022-05-17 Build site.
Rmd 71c48ae jens-daniel-mueller 2022-05-17 write files with betaD included
html 5ebbe11 jens-daniel-mueller 2022-05-15 Build site.
Rmd 3db4fce jens-daniel-mueller 2022-05-15 include planktom12
html f742a85 jens-daniel-mueller 2022-05-15 Build site.
Rmd 9fbdbc2 jens-daniel-mueller 2022-05-15 include planktom12
html 0dd6813 jens-daniel-mueller 2022-05-15 Build site.
Rmd 1e98fee jens-daniel-mueller 2022-05-15 included more models excp. planktom12
html 06054b2 jens-daniel-mueller 2022-05-10 Build site.
Rmd d43e8c0 jens-daniel-mueller 2022-05-10 rerun all with multi model subsetting

# 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)
models <- list.files(path_cmorized)

models <-
  models[!str_detect(models, pattern = "\\.t|\\.z")]

models <-
  models[str_detect(
    models,
    pattern = c(
      "CESM|CNRM|EC-Earth3|FESOM_REcoM_LR|MOM6-Princeton|MRI-ESM2-1|NorESM-OC1.2|ORCA025-GEOMAR|ORCA1-LIM3-PISCES|planktom12"
    )
  )]

# depth levels not available
models <-
  models[!str_detect(models, pattern = "CNRM")]

# files chunked into decades
models <-
  models[!str_detect(models, pattern = "ORCA025-GEOMAR")]

# no run D
models <-
  models[!str_detect(models, pattern = "MOM6-Princeton")]

1 Calculate annual Cant field

# for loop across variables
# 3d variables
variables <-
  c("so", "thetao", "dissic", "talk", "po4", "no3")


# models <- models[7]

for (i_model in models) {
  # i_model <- models[4]
  print(i_model)
  
  for (i_model_ID in c("A", "D")) {
    # i_model_ID <- c("A", "D")[1]
    
    print(i_model_ID)
    
    variables_available <-
      list.files(
        path = paste0(path_cmorized, i_model),
        pattern = paste0("_", i_model_ID, "_")
      )
    
    variables_available <-
      str_split(variables_available,
                pattern = "_",
                simplify = TRUE)[, 1]
    variables_available <-
      variables_available[variables_available %in% variables]
    variables_available <- unique(variables_available)
    print(variables_available)
    
    for (i_variable in variables_available) {
      #  i_variable <- variables_available[1]
      
      # read list of all files
      file <-
        list.files(
          path = paste0(path_cmorized, i_model),
          pattern = paste0(i_variable, "_")
        )
      
      if (i_model == "MRI-ESM2-1_3D_ALL_v20220502" &
          i_model_ID == "D" &
          i_variable %in% c("so", "thetao", "talk", "po4", "no3")) {
        file <-
          file[str_detect(file, pattern = paste0("_", "A", "_"))]
      } else {
        file <-
          file[str_detect(file, pattern = paste0("_", i_model_ID, "_"))]
      }
      
      print(file)
      
      # read in data
      if (i_model == "NorESM-OC1.2_3D_ALL_v20211125") {
        variable_data <-
          tidync::tidync(paste(paste0(path_cmorized, i_model),
                               file,
                               sep = "/")) %>%
          tidync::hyper_tibble() %>%
          mutate(time = as.Date(time, origin = "1980-01-01"))
      } else if (i_model %in% c("EC-Earth3_3D_ALL_v20220323",
                                "CNRM-ESM2-1_3D_ALL_v20211208")) {
        variable_data <-
          read_ncdf(paste(paste0(path_cmorized, i_model),
                          file,
                          sep = "/"),
                    make_units = FALSE,
                    make_time = FALSE)
      } else {
        variable_data <-
          read_ncdf(paste(paste0(path_cmorized, i_model),
                          file,
                          sep = "/"),
                    make_units = FALSE)
      }
      
      # convert to tibble
      variable_data_tibble <- variable_data %>%
        as_tibble()
      
      # remove open link to nc file
      rm(variable_data)
      
      # remove na values
      variable_data_tibble <-
        variable_data_tibble %>%
        filter(!is.na(!!sym(i_variable)))
      
      # sort(unique(variable_data_tibble$lev))
      # sort(unique(variable_data_tibble$lon))
      
      if (i_model == "CESM-ETHZ_3D_ALL_v20211122") {
        variable_data_tibble <- variable_data_tibble %>%
          rename(time = time_ann) %>%
          mutate(depth = as.numeric(depth))
      }
      
      if (i_model == "CNRM-ESM2-1_3D_ALL_v20211208") {
        variable_data_tibble <- variable_data_tibble %>%
          rename(depth = lev) %>%
          group_by(time) %>%
          mutate(year = as.character(1979 + cur_group_id())) %>%
          ungroup()
        
        variable_data_tibble <- variable_data_tibble %>%
          mutate(time = as.Date(year, format = "%Y")) %>%
          select(-year)
      }
      
      if (i_model == "EC-Earth3_3D_ALL_v20220323") {
        variable_data_tibble <- variable_data_tibble %>%
          mutate(time = as.Date(time, origin = "1850-06-01"))
      }
      
      if (i_model == "FESOM_REcoM_LR_3D_all_v20211119") {
        variable_data_tibble <- variable_data_tibble %>%
          rename(
            lat = Lat,
            lon = Lon,
            depth = Depth,
            time = Time
          ) %>%
          mutate(time = as.Date(time, origin = '1980-01-01'),
                 depth = as.numeric(depth))
      }
      
      if (i_model == "MOM6-Princeton_3D_ALL_v20220125") {
        variable_data_tibble <- variable_data_tibble %>%
          rename(depth = z_l)
      }
      
      if (i_model == "MRI-ESM2-1_3D_ALL_v20220502") {
        variable_data_tibble <- variable_data_tibble %>%
          rename(depth = lev)
      }
      
      if (i_model == "planktom12_3d_all_v20220404") {
        variable_data_tibble <- variable_data_tibble %>%
          rename(
            lon = LONGITUDE,
            lat = LATITUDE,
            depth = DEPTH,
            time = TIME
          ) %>%
          mutate(depth = as.numeric(depth))
      }
      
      if (i_model == "planktom12_3d_all_v20220404" &
          i_variable == "po4") {
        variable_data_tibble <-
          variable_data_tibble %>%
          mutate(po4 = po4 / 100)
      }
      
      variable_data_tibble <- variable_data_tibble %>%
        mutate(time = year(time))
      
      if (exists("annual")) {
        annual <- left_join(annual, variable_data_tibble)
      }
      
      if (!exists("annual")) {
        annual <- variable_data_tibble
      }
      
      rm(variable_data_tibble)
      
    }
    
    if (i_model == "FESOM_REcoM_LR_3D_all_v20211119") {
      annual <- annual %>%
        mutate(po4 = no3 / 16)
    }
    
    if (i_model == "planktom12_3d_all_v20220404") {
      annual <- annual %>%
        mutate(no3 = po4 * 16)
    }
    
    if (i_model == "MRI-ESM2-1_3D_ALL_v20220502" &
        i_model_ID == "D") {
      annual <- right_join(annual,
                           AD_annual %>%
                             select(-dissic))
    }
    
    annual <- annual %>%
      mutate(model_ID = i_model_ID)
    
    if (exists("AD_annual")) {
      AD_annual <- bind_rows(AD_annual, annual)
    }
    
    if (!exists("AD_annual")) {
      AD_annual <- annual
    }
    
    rm(annual)
    
  }
  
  # harmonize column names and coordinates
  AD_annual <- AD_annual %>%
    select(
      year = time,
      lon,
      lat,
      depth,
      sal = so,
      theta = thetao,
      dissic,
      talk,
      phosphate = po4,
      nitrate = no3,
      model_ID
    ) %>%
    mutate(lon = if_else(lon < 20, lon + 360, lon))
  
  # calculate model temperature
  AD_annual <- AD_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
  AD_annual <- AD_annual %>%
    mutate(
      rho = gsw_pot_rho_t_exact(
        SA = sal,
        t = temp,
        p = depth,
        p_ref = 10.1325
      ),
      dissic = dissic * (1e+6 / rho),
      talk = talk * (1e+6 / rho),
      phosphate = phosphate * (1e+6 / rho),
      nitrate = nitrate * (1e+6 / rho)
    )
  
  AD_annual <- AD_annual %>%
    select(-c(sal, temp, theta, rho))
  
  
  # sort(unique(AD_annual$depth))
  # sort(unique(AD_annual$lon))
  
  AD_annual <- AD_annual %>%
    mutate(depth = round(depth))
  
  ## Apply basin mask
  AD_annual <- inner_join(AD_annual, basinmask)
  
  # calculate cstar field
  AD_annual_cstar <- AD_annual

  params_local <- lst(rCP = 117,
                      rNP = 16)
  
  
  AD_annual_cstar <- AD_annual_cstar %>%
    mutate(
      cstar_nitrate_talk = b_cstar_nitrate_talk(
        tco2 = dissic,
        nitrate = nitrate,
        talk = talk
      ),
      cstar_phosphate_talk = b_cstar_phosphate_talk(
        tco2 = dissic,
        phosphate = phosphate,
        talk = talk
      ),
      cstar_nitrate = b_cstar_nitrate(
        tco2 = dissic,
        nitrate = nitrate
      ),
      cstar_phosphate = b_cstar_phosphate(
        tco2 = dissic,
        phosphate = phosphate
      ),
      cstar_talk = b_cstar_talk(
        tco2 = dissic,
        talk = talk
      )
    )
  
  rm(params_local)
  
  # Calculate Cant fields
  
  AD_annual <- AD_annual %>%
    select(year, lon, lat, depth, dissic, model_ID, basin_AIP)
  
  AD_annual <- AD_annual %>% pivot_wider(names_from = model_ID,
                                         values_from = dissic)
  
  
  AD_annual_dissic <- AD_annual
  
  AD_annual <- AD_annual %>%
    mutate(tcant = A - D) %>%
    select(-c(A, D))
  
  
  # Remove 3D_all from model name
  i_model <- str_remove(i_model, "3D_ALL_|3D_all_|3d_all_")
  
  # write annual Cant files
  for (i_year in unique(AD_annual$year)) {
    # i_year = unique(AD_annual$year)[1]
    AD_annual %>%
      filter(year == i_year) %>%
      write_csv(
        paste0(
          path_preprocessing,
          "Cant_AD_annual_all_models/",
          i_year,
          "_",
          i_model,
          ".csv"
        )
      )

    AD_annual_dissic %>%
      filter(year == i_year) %>%
      write_csv(
        paste0(
          path_preprocessing,
          "Cant_AD_annual_all_models/dissic/",
          i_year,
          "_",
          i_model,
          ".csv"
        )
      )
    
    AD_annual_cstar %>%
      filter(year == i_year,
             model_ID == "A") %>%
      write_csv(
        paste0(
          path_preprocessing,
          "Cant_AD_annual_all_models/Cstar_A_annual_all_models/",
          i_year,
          "_",
          i_model,
          ".csv"
        )
      )
    
    AD_annual_cstar %>%
      filter(year == i_year,
             model_ID == "D") %>%
      write_csv(
        paste0(
          path_preprocessing,
          "Cant_AD_annual_all_models/Cstar_D_annual_all_models/",
          i_year,
          "_",
          i_model,
          ".csv"
        )
      )
    
  }
  
  rm(AD_annual)
  
}
[1] "CESM-ETHZ_3D_ALL_v20211122"
[1] "A"
[1] "dissic" "no3"    "po4"    "so"     "talk"   "thetao"
[1] "dissic_CESM-ETHZ_A_1_gr_1980-2018_v20211122.nc"
[1] "no3_CESM-ETHZ_A_1_gr_1980-2018_v20211122.nc"
[1] "po4_CESM-ETHZ_A_1_gr_1980-2018_v20211122.nc"
[1] "so_CESM-ETHZ_A_1_gr_1980-2018_v20211122.nc"
[1] "talk_CESM-ETHZ_A_1_gr_1980-2018_v20211122.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_1980-2018_v20211122.nc"
[1] "D"
[1] "dissic" "no3"    "po4"    "so"     "talk"   "thetao"
[1] "dissic_CESM-ETHZ_D_1_gr_1980-2018_v20211122.nc"
[1] "no3_CESM-ETHZ_D_1_gr_1980-2018_v20211122.nc"
[1] "po4_CESM-ETHZ_D_1_gr_1980-2018_v20211122.nc"
[1] "so_CESM-ETHZ_D_1_gr_1980-2018_v20211122.nc"
[1] "talk_CESM-ETHZ_D_1_gr_1980-2018_v20211122.nc"
[1] "thetao_CESM-ETHZ_D_1_gr_1980-2018_v20211122.nc"
[1] "EC-Earth3_3D_ALL_v20220323"
[1] "A"
[1] "dissic" "no3"    "po4"    "so"     "talk"   "thetao"
[1] "dissic_EC-Earth3_A_1_gr_1980-2018_v20220323.nc"
[1] "no3_EC-Earth3_A_1_gr_1980-2018_v20220323.nc"
[1] "po4_EC-Earth3_A_1_gr_1980-2018_v20220323.nc"
[1] "so_EC-Earth3_A_1_gr_1980-2018_v20220323.nc"
[1] "talk_EC-Earth3_A_1_gr_1980-2018_v20220323.nc"
[1] "thetao_EC-Earth3_A_1_gr_1980-2018_v20220323.nc"
[1] "D"
[1] "dissic" "no3"    "po4"    "so"     "talk"   "thetao"
[1] "dissic_EC-Earth3_D_1_gr_1980-2018_v20220323.nc"
[1] "no3_EC-Earth3_D_1_gr_1980-2018_v20220323.nc"
[1] "po4_EC-Earth3_D_1_gr_1980-2018_v20220323.nc"
[1] "so_EC-Earth3_D_1_gr_1980-2018_v20220323.nc"
[1] "talk_EC-Earth3_D_1_gr_1980-2018_v20220323.nc"
[1] "thetao_EC-Earth3_D_1_gr_1980-2018_v20220323.nc"
[1] "FESOM_REcoM_LR_3D_all_v20211119"
[1] "A"
[1] "dissic" "no3"    "so"     "talk"   "thetao"
[1] "dissic_FESOM_REcoM_LR_A_1_gr_1980-2018_v20211119.nc"
[1] "no3_FESOM_REcoM_LR_A_1_gr_1980-2018_v20211119.nc"
[1] "so_FESOM_REcoM_LR_A_1_gr_1980-2018_v20211119.nc"
[1] "talk_FESOM_REcoM_LR_A_1_gr_1980-2018_v20211119.nc"
[1] "thetao_FESOM_REcoM_LR_A_1_gr_1980-2018_v20211119.nc"
[1] "D"
[1] "dissic" "no3"    "so"     "talk"   "thetao"
[1] "dissic_FESOM_REcoM_LR_D_1_gr_1980-2018_v20211119.nc"
[1] "no3_FESOM_REcoM_LR_D_1_gr_1980-2018_v20211119.nc"
[1] "so_FESOM_REcoM_LR_D_1_gr_1980-2018_v20211119.nc"
[1] "talk_FESOM_REcoM_LR_D_1_gr_1980-2018_v20211119.nc"
[1] "thetao_FESOM_REcoM_LR_D_1_gr_1980-2018_v20211119.nc"
[1] "MRI-ESM2-1_3D_ALL_v20220502"
[1] "A"
[1] "dissic" "no3"    "po4"    "so"     "talk"   "thetao"
[1] "dissic_MRI-ESM2-1_A_1_gr_1980-2018_v20220502.nc"
[1] "no3_MRI-ESM2-1_A_1_gr_1980-2018_v20220502.nc"
[1] "po4_MRI-ESM2-1_A_1_gr_1980-2018_v20220502.nc"
[1] "so_MRI-ESM2-1_A_1_gr_1980-2018_v20220502.nc"
[1] "talk_MRI-ESM2-1_A_1_gr_1980-2018_v20220502.nc"
[1] "thetao_MRI-ESM2-1_A_1_gr_1980-2018_v20220502.nc"
[1] "D"
[1] "dissic"
[1] "dissic_MRI-ESM2-1_D_1_gr_1980-2018_v20220502.nc"
[1] "NorESM-OC1.2_3D_ALL_v20211125"
[1] "A"
[1] "dissic" "no3"    "po4"    "so"     "talk"   "thetao"
[1] "dissic_NorESM-OC1.2_A_1_gr_1980-2018_v20211125.nc"
[1] "no3_NorESM-OC1.2_A_1_gr_1980-2018_v20211125.nc"
[1] "po4_NorESM-OC1.2_A_1_gr_1980-2018_v20211125.nc"
[1] "so_NorESM-OC1.2_A_1_gr_1980-2018_v20211125.nc"
[1] "talk_NorESM-OC1.2_A_1_gr_1980-2018_v20211125.nc"
[1] "thetao_NorESM-OC1.2_A_1_gr_1980-2018_v20211125.nc"
[1] "D"
[1] "dissic" "no3"    "po4"    "so"     "talk"   "thetao"
[1] "dissic_NorESM-OC1.2_D_1_gr_1980-2018_v20211125.nc"
[1] "no3_NorESM-OC1.2_D_1_gr_1980-2018_v20211125.nc"
[1] "po4_NorESM-OC1.2_D_1_gr_1980-2018_v20211125.nc"
[1] "so_NorESM-OC1.2_D_1_gr_1980-2018_v20211125.nc"
[1] "talk_NorESM-OC1.2_D_1_gr_1980-2018_v20211125.nc"
[1] "thetao_NorESM-OC1.2_D_1_gr_1980-2018_v20211125.nc"
[1] "ORCA1-LIM3-PISCES_3D_ALL_v20211215"
[1] "A"
[1] "dissic" "no3"    "po4"    "so"     "talk"   "thetao"
[1] "dissic_ORCA1-LIM3-PISCES_A_1_gr_1980-2018_v20211215.nc"
[1] "no3_ORCA1-LIM3-PISCES_A_1_gr_1980-2018_v20211215.nc"
[1] "po4_ORCA1-LIM3-PISCES_A_1_gr_1980-2018_v20211215.nc"
[1] "so_ORCA1-LIM3-PISCES_A_1_gr_1980-2018_v20211215.nc"
[1] "talk_ORCA1-LIM3-PISCES_A_1_gr_1980-2018_v20211215.nc"
[1] "thetao_ORCA1-LIM3-PISCES_A_1_gr_1980-2018_v20211215.nc"
[1] "D"
[1] "dissic" "no3"    "po4"    "so"     "talk"   "thetao"
[1] "dissic_ORCA1-LIM3-PISCES_D_1_gr_1980-2018_v20211215.nc"
[1] "no3_ORCA1-LIM3-PISCES_D_1_gr_1980-2018_v20211215.nc"
[1] "po4_ORCA1-LIM3-PISCES_D_1_gr_1980-2018_v20211215.nc"
[1] "so_ORCA1-LIM3-PISCES_D_1_gr_1980-2018_v20211215.nc"
[1] "talk_ORCA1-LIM3-PISCES_D_1_gr_1980-2018_v20211215.nc"
[1] "thetao_ORCA1-LIM3-PISCES_D_1_gr_1980-2018_v20211215.nc"
[1] "planktom12_3d_all_v20220404"
[1] "A"
[1] "dissic" "po4"    "so"     "talk"   "thetao"
[1] "dissic_PlankTOM12_A_1_gr_1980-2018_v20220404.nc"
[1] "po4_PlankTOM12_A_1_gr_1980-2018_v20220404.nc"
[1] "so_PlankTOM12_A_1_gr_1980-2018_v20220404.nc"
[1] "talk_PlankTOM12_A_1_gr_1980-2018_v20220404.nc"
[1] "thetao_PlankTOM12_A_1_gr_1980-2018_v20220404.nc"
[1] "D"
[1] "dissic" "po4"    "so"     "talk"   "thetao"
[1] "dissic_PlankTOM12_D_1_gr_1980-2018_v20220404.nc"
[1] "po4_PlankTOM12_D_1_gr_1980-2018_v20220404.nc"
[1] "so_PlankTOM12_D_1_gr_1980-2018_v20220404.nc"
[1] "talk_PlankTOM12_D_1_gr_1980-2018_v20220404.nc"
[1] "thetao_PlankTOM12_D_1_gr_1980-2018_v20220404.nc"

2 Calculate change in Cant 1994 - 2004 - 2014

for (i_model in models) {
  # i_model <- models[1]
  print(i_model)
  
  for (i_year in c("1994", "2004", "2014")) {
    # i_year = "1994"
    
    cant_year <-
      read_csv(paste0(path_preprocessing,
                      "Cant_AD_annual_all_models/",
                      i_year,
                      "_",
                      str_remove(i_model, "3D_ALL_|3D_all_|3d_all_"),
                      ".csv"))
    
    cant_year <- cant_year %>% 
      mutate(model = i_model)
    
    if (exists("tcant")) {
      tcant <- bind_rows(tcant, cant_year)
    }
    
    if (!exists("tcant")) {
      tcant <- cant_year
    }
    
  }
  
}
[1] "CESM-ETHZ_3D_ALL_v20211122"
[1] "EC-Earth3_3D_ALL_v20220323"
[1] "FESOM_REcoM_LR_3D_all_v20211119"
[1] "MRI-ESM2-1_3D_ALL_v20220502"
[1] "NorESM-OC1.2_3D_ALL_v20211125"
[1] "ORCA1-LIM3-PISCES_3D_ALL_v20211215"
[1] "planktom12_3d_all_v20220404"
tcant <- tcant %>%
  # head(10) %>%
  arrange(year) %>%
  group_by(model, lon, lat, depth, basin_AIP) %>%
  mutate(dcant = tcant - lag(tcant),
         eras = paste(year, "-" , lag(year))) %>%
  ungroup()

3 Zonal mean sections

tcant <- tcant %>% 
  rename(data_source = model)

tcant_zonal <- tcant %>%
  select(-year) %>% 
  filter(!is.na(dcant)) %>% 
  group_by(data_source, eras) %>%
  nest() %>%
  mutate(zonal = map(.x = data, ~m_zonal_mean_sd(.x))) %>%
  select(-data) %>%
  unnest(zonal)

3.1 delta Cant

tcant_zonal %>%
  filter(depth <= params_global$inventory_depth_standard) %>%
  group_by(basin_AIP) %>%
  group_split() %>%
  head(1) %>%
  map(
    ~ p_section_zonal_continous_depth(
      df = .x,
      var = "dcant_mean",
      plot_slabs = "n",
      subtitle_text = .x$basin_AIP
    ) +
      facet_grid(data_source ~ eras)
  )
[[1]]

Version Author Date
297418e jens-daniel-mueller 2022-10-24
e829a11 jens-daniel-mueller 2022-07-05
9fdecac jens-daniel-mueller 2022-06-29
89cbb74 jens-daniel-mueller 2022-05-26
9e85627 jens-daniel-mueller 2022-05-24
42bbb89 jens-daniel-mueller 2022-05-24
40863a2 jens-daniel-mueller 2022-05-22
31ae80a jens-daniel-mueller 2022-05-20
8cfec6a jens-daniel-mueller 2022-05-17
5ebbe11 jens-daniel-mueller 2022-05-15
f742a85 jens-daniel-mueller 2022-05-15
0dd6813 jens-daniel-mueller 2022-05-15
06054b2 jens-daniel-mueller 2022-05-10

3.2 total Cant

tcant_zonal %>%
  filter(depth <= params_global$inventory_depth_standard) %>%
  group_by(basin_AIP) %>%
  group_split() %>%
  # head(1) %>%
  map(
    ~ p_section_zonal_continous_depth(
      df = .x,
      var = "tcant_mean",
      plot_slabs = "n",
      subtitle_text = .x$basin_AIP,
      breaks = c(-Inf,seq(0,80,10), Inf)
    ) +
      facet_grid(data_source ~ eras)
  )
[[1]]

Version Author Date
297418e jens-daniel-mueller 2022-10-24
e829a11 jens-daniel-mueller 2022-07-05
9fdecac jens-daniel-mueller 2022-06-29
89cbb74 jens-daniel-mueller 2022-05-26
9e85627 jens-daniel-mueller 2022-05-24
42bbb89 jens-daniel-mueller 2022-05-24
40863a2 jens-daniel-mueller 2022-05-22
31ae80a jens-daniel-mueller 2022-05-20
8cfec6a jens-daniel-mueller 2022-05-17
5ebbe11 jens-daniel-mueller 2022-05-15
f742a85 jens-daniel-mueller 2022-05-15
0dd6813 jens-daniel-mueller 2022-05-15
06054b2 jens-daniel-mueller 2022-05-10

[[2]]

Version Author Date
297418e jens-daniel-mueller 2022-10-24
e829a11 jens-daniel-mueller 2022-07-05
9fdecac jens-daniel-mueller 2022-06-29
89cbb74 jens-daniel-mueller 2022-05-26
9e85627 jens-daniel-mueller 2022-05-24
42bbb89 jens-daniel-mueller 2022-05-24
40863a2 jens-daniel-mueller 2022-05-22
31ae80a jens-daniel-mueller 2022-05-20
8cfec6a jens-daniel-mueller 2022-05-17
5ebbe11 jens-daniel-mueller 2022-05-15
f742a85 jens-daniel-mueller 2022-05-15
0dd6813 jens-daniel-mueller 2022-05-15
06054b2 jens-daniel-mueller 2022-05-10

[[3]]

Version Author Date
297418e jens-daniel-mueller 2022-10-24
e829a11 jens-daniel-mueller 2022-07-05
9fdecac jens-daniel-mueller 2022-06-29
89cbb74 jens-daniel-mueller 2022-05-26
9e85627 jens-daniel-mueller 2022-05-24
42bbb89 jens-daniel-mueller 2022-05-24
40863a2 jens-daniel-mueller 2022-05-22
31ae80a jens-daniel-mueller 2022-05-20
8cfec6a jens-daniel-mueller 2022-05-17
5ebbe11 jens-daniel-mueller 2022-05-15
f742a85 jens-daniel-mueller 2022-05-15
0dd6813 jens-daniel-mueller 2022-05-15
06054b2 jens-daniel-mueller 2022-05-10

4 Column inventory

4.1 delta Cant

dcant_inv <- tcant %>%
  select(-year) %>%
  filter(!is.na(dcant)) %>%
  mutate(dcant_pos = 0) %>%
  group_by(data_source, eras) %>%
  nest() %>%
  mutate(inv = map(.x = data, ~ m_dcant_inv(.x))) %>%
  select(-data) %>%
  unnest(inv)

dcant_inv %>%
  filter(inv_depth == params_global$inventory_depth_standard) %>%
  p_map_cant_inv(var = "dcant",
                 subtitle_text = "for predefined integration depths") +
  facet_grid(data_source ~ eras)

Version Author Date
297418e jens-daniel-mueller 2022-10-24
e829a11 jens-daniel-mueller 2022-07-05
89cbb74 jens-daniel-mueller 2022-05-26
9e85627 jens-daniel-mueller 2022-05-24
42bbb89 jens-daniel-mueller 2022-05-24
40863a2 jens-daniel-mueller 2022-05-22
31ae80a jens-daniel-mueller 2022-05-20
8cfec6a jens-daniel-mueller 2022-05-17
5ebbe11 jens-daniel-mueller 2022-05-15
f742a85 jens-daniel-mueller 2022-05-15
0dd6813 jens-daniel-mueller 2022-05-15
06054b2 jens-daniel-mueller 2022-05-10

4.2 Cant total

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

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

sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: openSUSE Leap 15.3

Matrix products: default
BLAS:   /usr/local/R-4.1.2/lib64/R/lib/libRblas.so
LAPACK: /usr/local/R-4.1.2/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] lubridate_1.8.0    gsw_1.0-6          stars_0.5-5        sf_1.0-5          
 [5] abind_1.4-5        tidync_0.2.4       geomtextpath_0.1.0 colorspace_2.0-2  
 [9] marelac_2.1.10     shape_1.4.6        ggforce_0.3.3      metR_0.11.0       
[13] scico_1.3.0        patchwork_1.1.1    collapse_1.7.0     forcats_0.5.1     
[17] stringr_1.4.0      dplyr_1.0.7        purrr_0.3.4        readr_2.1.1       
[21] tidyr_1.1.4        tibble_3.1.6       ggplot2_3.3.5      tidyverse_1.3.1   
[25] workflowr_1.7.0   

loaded via a namespace (and not attached):
 [1] ellipsis_0.3.2     class_7.3-20       rprojroot_2.0.2    fs_1.5.2          
 [5] rstudioapi_0.13    proxy_0.4-26       farver_2.1.0       bit64_4.0.5       
 [9] fansi_1.0.2        xml2_1.3.3         ncdf4_1.19         knitr_1.37        
[13] polyclip_1.10-0    jsonlite_1.7.3     broom_0.7.11       dbplyr_2.1.1      
[17] compiler_4.1.2     httr_1.4.2         backports_1.4.1    assertthat_0.2.1  
[21] fastmap_1.1.0      cli_3.1.1          later_1.3.0        tweenr_1.0.2      
[25] htmltools_0.5.2    tools_4.1.2        gtable_0.3.0       glue_1.6.0        
[29] Rcpp_1.0.8         cellranger_1.1.0   jquerylib_0.1.4    RNetCDF_2.5-2     
[33] vctrs_0.3.8        lwgeom_0.2-8       xfun_0.29          ps_1.6.0          
[37] rvest_1.0.2        lifecycle_1.0.1    ncmeta_0.3.0       oce_1.5-0         
[41] getPass_0.2-2      MASS_7.3-55        scales_1.1.1       vroom_1.5.7       
[45] hms_1.1.1          promises_1.2.0.1   parallel_4.1.2     yaml_2.2.1        
[49] sass_0.4.0         stringi_1.7.6      highr_0.9          e1071_1.7-9       
[53] checkmate_2.0.0    rlang_1.0.2        pkgconfig_2.0.3    systemfonts_1.0.3 
[57] evaluate_0.14      SolveSAPHE_2.1.0   labeling_0.4.2     bit_4.0.4         
[61] processx_3.5.2     tidyselect_1.1.1   seacarb_3.3.0      magrittr_2.0.1    
[65] R6_2.5.1           generics_0.1.1     DBI_1.1.2          pillar_1.6.4      
[69] haven_2.4.3        whisker_0.4        withr_2.4.3        units_0.7-2       
[73] modelr_0.1.8       crayon_1.4.2       KernSmooth_2.23-20 utf8_1.2.2        
[77] tzdb_0.2.0         rmarkdown_2.11     grid_4.1.2         readxl_1.3.1      
[81] isoband_0.2.5      data.table_1.14.2  callr_3.7.0        git2r_0.29.0      
[85] reprex_2.0.1       digest_0.6.29      classInt_0.4-3     httpuv_1.6.5      
[89] textshaping_0.3.6  munsell_0.5.0      viridisLite_0.4.0  bslib_0.3.1