• 1 Select basin mask
  • 2 Subset cmorized data according to GLODAP observation
  • 3 Air-sea disequilibrium

Last updated: 2021-06-17

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 29561c9. 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/surface_ocean.Rmd) and HTML (docs/surface_ocean.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 29561c9 jens-daniel-mueller 2021-06-17 derive globally integrated scaling factor
html a64f00b jens-daniel-mueller 2021-06-16 Build site.
Rmd 366beab jens-daniel-mueller 2021-06-16 export globally integrated scaling factor
html c424d18 jens-daniel-mueller 2021-06-11 Build site.
Rmd a436128 jens-daniel-mueller 2021-06-11 Kx rather Kw for scaling air sea disequilibrium
html 834c523 jens-daniel-mueller 2021-06-09 Build site.
Rmd e904ada jens-daniel-mueller 2021-06-09 derive air sea disequilibrium
html 13659b9 jens-daniel-mueller 2021-06-09 Build site.
Rmd c1bee2f jens-daniel-mueller 2021-06-09 derive air sea disequilibrium
html 0e663e3 jens-daniel-mueller 2021-06-09 Build site.
Rmd 2799a45 jens-daniel-mueller 2021-06-09 derive air sea disequilibrium
html b256c65 jens-daniel-mueller 2021-06-09 Build site.
html 27f932d jens-daniel-mueller 2021-06-08 Build site.
Rmd 4664034 jens-daniel-mueller 2021-06-08 testrun surface ocean data
html fe31dfb jens-daniel-mueller 2021-06-08 Build site.
Rmd bb1bfa9 jens-daniel-mueller 2021-06-08 testrun surface ocean data

path_cmorized <-
  "/nfs/kryo/work/loher/CESM_output/RECCAP2/submit_June2021/"

path_preprocessing <-
  paste(path_root, "/model/preprocessing/", sep = "")

1 Select basin mask

# 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)

2 Subset cmorized data according to GLODAP observation

Here we subset cmorized (1x1) surface ocean data from the model with variable forcing.

Besides, Model results are given in [mol m-3], whereas GLODAP data are in [µmol kg-1]. This refers to the variables:

  • DIC
  • ALK
  • O2
  • NO3
  • PO4
  • SiO3
  • AOU (calculated)

For comparison, model results were converted from [mol m-3] to [µmol kg-1]

# set name of model to be subsetted
model_IDs <- c("A", "D")

# for loop across variables
variables <-
  c("spco2",
    "fgco2",
    "talkos",
    "dissicos",
    "po4os",
    "sios",
    "sos",
    "tos")


for (i_model_ID in model_IDs) {
  # i_model_ID <- model_IDs[1]
  
  for (i_variable in variables) {
    # i_variable <- variables[1]
    
    # read list of all files
    file <- paste(i_variable,
                  "_CESM-ETHZ_",
                  i_model_ID,
                  "_1_gr_1980-2018_v20210607.nc",
                  sep = "")
    print(file)
    
    
    # read in data
    variable_data <-
      read_ncdf(paste(path_cmorized,
                      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 %>%
      drop_na()
    
    # harmonize longitudes
    variable_data_tibble <- variable_data_tibble %>%
      mutate(lon = if_else(lon < 20, lon + 360, lon))
    
    # only consider model grids within basinmask
    variable_data_tibble <-
      inner_join(variable_data_tibble, basinmask) %>%
      select(-basin_AIP)
    
    # mutate variables
    variable_data_tibble <- variable_data_tibble %>%
      mutate(year = year(time_mon)) %>%
      select(-time_mon)
    
    
    # calculate annual average variable
    variable_data_tibble_annual_average <- variable_data_tibble %>%
      fgroup_by(lat, lon, year) %>% {
        add_vars(fgroup_vars(., "unique"),
                 fmean(., keep.group_vars = FALSE))
      }
    
    # calculate climatology
    variable_data_tibble_climatology <-
      variable_data_tibble_annual_average %>%
      fselect(-year) %>%
      fgroup_by(lat, lon) %>% {
        add_vars(fgroup_vars(., "unique"),
                 fmean(., keep.group_vars = FALSE))
      }
    
    # surface map of variable
    print(
    map +
      geom_raster(data = variable_data_tibble_climatology,
                  aes(lon, lat, fill = !!sym(i_variable))) +
      scale_fill_viridis_c(name = i_variable)
    )
    
    
    # write raw data file for GLODAP-based subsetting model variables
    variable_data_tibble_annual_average %>%
      write_csv(file = paste(path_preprocessing,
                             "surface_ocean_",
                             i_model_ID,
                             "/",
                             i_variable,
                             ".csv",
                             sep = ""))
    
    
  }
  
}

3 Air-sea disequilibrium

# set name of model to be subsetted
model_IDs <- c("A", "B", "C", "D")


file <- paste("area",
              "_CESM-ETHZ_",
              "1_gr.nc",
              sep = "")

area_grid <-
  variable_data <-
  read_ncdf(paste(path_cmorized,
                  file,
                  sep = ""),
            make_units = FALSE) %>% 
  as_tibble()

area_grid <- area_grid %>%
  mutate(lon = if_else(lon < 20, lon + 360, lon))

# for loop across variables
variables <-
  c("spco2",
    "Kw",
    "pco2atm",
    "alpha",
    "fice")


for (i_model_ID in model_IDs) {
  # i_model_ID <- model_IDs[1]
  
  for (i_variable in variables) {
    # i_variable <- variables[5]
    
    # read list of all files
    file <- paste(i_variable,
                  "_CESM-ETHZ_",
                  i_model_ID,
                  "_1_gr_1980-2018_v20210607.nc",
                  sep = "")
    print(file)

    # read in data
    variable_data <-
      read_ncdf(paste(path_cmorized,
                      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 %>%
      drop_na()
    
    # harmonize longitudes
    variable_data_tibble <- variable_data_tibble %>%
      mutate(lon = if_else(lon < 20, lon + 360, lon))
    
    # only consider model grids within basinmask
    variable_data_tibble <-
      inner_join(variable_data_tibble, basinmask) %>%
      select(-basin_AIP)
    
    if (exists("all_variables")) {
      all_variables <- inner_join(all_variables, variable_data_tibble)
    }
    
    if (!exists("all_variables")) {
      all_variables <- variable_data_tibble
    }
    
    
  }
  
  all_variables <- inner_join(all_variables,
                              area_grid)
  
  mol_to_g <- 12
  P <- 1e-15
  m3_to_kg <- 1030
  sec_to_yr <- 60*60*24*365

  unit_conversion_to_PgCyr <- mol_to_g * P * sec_to_yr / m3_to_kg

  all_variables <- all_variables %>%
    mutate(delta_pco2 = spco2 - pco2atm,
           scale = area * Kw * alpha * (1-fice),
           fgco2 = delta_pco2 * scale)
  
  delta_pco2_monthly <- all_variables %>%
    group_by(time_mon) %>%
    summarise(
      scaling_glob = sum(scale),
      fgco2_glob = sum(fgco2),
      delta_pco2_glob = fgco2_glob / scaling_glob
    ) %>%
    ungroup() %>% 
    mutate(fgco2_glob = fgco2_glob * unit_conversion_to_PgCyr)
  
  delta_pco2_annual <- delta_pco2_monthly %>%
    mutate(year = year(time_mon)) %>%
    group_by(year) %>%
    summarise(
      scaling_glob = mean(scaling_glob),
      fgco2_glob = mean(fgco2_glob),
      delta_pco2_glob = mean(delta_pco2_glob)
    ) %>%
    ungroup()
  
  print(
  ggplot() +
    geom_path(data = delta_pco2_monthly,
              aes(decimal_date(time_mon), delta_pco2_glob, col = "monthly")) +
    geom_path(data = delta_pco2_annual,
              aes(year, delta_pco2_glob, col = "annual")) +
    scale_color_brewer(palette = "Set1", name = "Average") +
    labs(title = paste("model_id:",i_model_ID),
         x = "year")
  )
  
  print(
  ggplot() +
    geom_path(data = delta_pco2_monthly,
              aes(decimal_date(time_mon), fgco2_glob, col = "monthly")) +
    geom_path(data = delta_pco2_annual,
              aes(year, fgco2_glob, col = "annual")) +
    scale_color_brewer(palette = "Set1", name = "Average") +
    labs(title = paste("model_id:",i_model_ID),
         x = "year")
  )
  
  print(
    ggplot() +
      geom_path(
        data = delta_pco2_annual,
        aes(
          year,
          scaling_glob * unit_conversion_to_PgCyr
        )
      )
  )
  
  print(
    ggplot() +
      geom_path(
        data = delta_pco2_annual,
        aes(
          year,
          scaling_glob * delta_pco2_glob * unit_conversion_to_PgCyr,
          col = "scaled"
        )
      ) +
      geom_path(
        data = delta_pco2_annual,
        aes(year, fgco2_glob, col = "integrated")
      ) +
      scale_color_brewer(palette = "Set1", name = "Estimate") +
      scale_y_continuous(name = "Air-sea flux [PgC yr-1]") +
      labs(title = paste("model_id:", i_model_ID),
           x = "year")
  )
  
  # write raw data file for GLODAP-based subsetting model variables
  delta_pco2_annual %>%
    select(-c(scaling_glob, fgco2_glob)) %>% 
    write_csv(
      file = paste(
        path_preprocessing,
        "surface_ocean_disequilibrium/",
        i_model_ID,
        "_annual.csv",
        sep = ""
      )
    )
  
  rm(i_variable, all_variables, delta_pco2_annual, delta_pco2_monthly)
  
}
[1] "spco2_CESM-ETHZ_A_1_gr_1980-2018_v20210607.nc"
[1] "Kw_CESM-ETHZ_A_1_gr_1980-2018_v20210607.nc"
[1] "pco2atm_CESM-ETHZ_A_1_gr_1980-2018_v20210607.nc"
[1] "alpha_CESM-ETHZ_A_1_gr_1980-2018_v20210607.nc"
[1] "fice_CESM-ETHZ_A_1_gr_1980-2018_v20210607.nc"

Version Author Date
a64f00b jens-daniel-mueller 2021-06-16
c424d18 jens-daniel-mueller 2021-06-11
13659b9 jens-daniel-mueller 2021-06-09
0e663e3 jens-daniel-mueller 2021-06-09

Version Author Date
a64f00b jens-daniel-mueller 2021-06-16
c424d18 jens-daniel-mueller 2021-06-11
13659b9 jens-daniel-mueller 2021-06-09

Version Author Date
a64f00b jens-daniel-mueller 2021-06-16
c424d18 jens-daniel-mueller 2021-06-11
13659b9 jens-daniel-mueller 2021-06-09

Version Author Date
a64f00b jens-daniel-mueller 2021-06-16
c424d18 jens-daniel-mueller 2021-06-11
13659b9 jens-daniel-mueller 2021-06-09
[1] "spco2_CESM-ETHZ_B_1_gr_1980-2018_v20210607.nc"
[1] "Kw_CESM-ETHZ_B_1_gr_1980-2018_v20210607.nc"
[1] "pco2atm_CESM-ETHZ_B_1_gr_1980-2018_v20210607.nc"
[1] "alpha_CESM-ETHZ_B_1_gr_1980-2018_v20210607.nc"
[1] "fice_CESM-ETHZ_B_1_gr_1980-2018_v20210607.nc"

Version Author Date
a64f00b jens-daniel-mueller 2021-06-16
c424d18 jens-daniel-mueller 2021-06-11

Version Author Date
a64f00b jens-daniel-mueller 2021-06-16
c424d18 jens-daniel-mueller 2021-06-11

Version Author Date
a64f00b jens-daniel-mueller 2021-06-16
c424d18 jens-daniel-mueller 2021-06-11

Version Author Date
a64f00b jens-daniel-mueller 2021-06-16
c424d18 jens-daniel-mueller 2021-06-11
[1] "spco2_CESM-ETHZ_C_1_gr_1980-2018_v20210607.nc"
[1] "Kw_CESM-ETHZ_C_1_gr_1980-2018_v20210607.nc"
[1] "pco2atm_CESM-ETHZ_C_1_gr_1980-2018_v20210607.nc"
[1] "alpha_CESM-ETHZ_C_1_gr_1980-2018_v20210607.nc"
[1] "fice_CESM-ETHZ_C_1_gr_1980-2018_v20210607.nc"

Version Author Date
a64f00b jens-daniel-mueller 2021-06-16

Version Author Date
a64f00b jens-daniel-mueller 2021-06-16

Version Author Date
a64f00b jens-daniel-mueller 2021-06-16

Version Author Date
a64f00b jens-daniel-mueller 2021-06-16
[1] "spco2_CESM-ETHZ_D_1_gr_1980-2018_v20210607.nc"
[1] "Kw_CESM-ETHZ_D_1_gr_1980-2018_v20210607.nc"
[1] "pco2atm_CESM-ETHZ_D_1_gr_1980-2018_v20210607.nc"
[1] "alpha_CESM-ETHZ_D_1_gr_1980-2018_v20210607.nc"
[1] "fice_CESM-ETHZ_D_1_gr_1980-2018_v20210607.nc"


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] reticulate_1.18   oce_1.2-0         marelac_2.1.10    shape_1.4.5      
 [5] gsw_1.0-5         testthat_2.3.2    rqdatatable_1.2.9 rquery_1.4.6     
 [9] wrapr_2.0.4       lubridate_1.7.9   stars_0.5-2       sf_0.9-8         
[13] abind_1.4-5       metR_0.9.0        scico_1.2.0       patchwork_1.1.1  
[17] collapse_1.5.0    forcats_0.5.0     stringr_1.4.0     dplyr_1.0.5      
[21] purrr_0.3.4       readr_1.4.0       tidyr_1.1.2       tibble_3.0.4     
[25] ggplot2_3.3.3     tidyverse_1.3.0   workflowr_1.6.2  

loaded via a namespace (and not attached):
 [1] fs_1.5.0                 RColorBrewer_1.1-2       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-17       DBI_1.1.0               
[10] colorspace_1.4-1         withr_2.3.0              tidyselect_1.1.0        
[13] compiler_4.0.3           git2r_0.27.1             cli_2.1.0               
[16] rvest_0.3.6              RNetCDF_2.4-2            xml2_1.3.2              
[19] labeling_0.4.2           scales_1.1.1             checkmate_2.0.0         
[22] classInt_0.4-3           digest_0.6.27            rmarkdown_2.5           
[25] pkgconfig_2.0.3          htmltools_0.5.0          dbplyr_1.4.4            
[28] rlang_0.4.10             readxl_1.3.1             rstudioapi_0.11         
[31] farver_2.0.3             generics_0.0.2           jsonlite_1.7.1          
[34] magrittr_1.5             ncmeta_0.3.0             Matrix_1.2-18           
[37] Rcpp_1.0.5               munsell_0.5.0            fansi_0.4.1             
[40] lifecycle_1.0.0          stringi_1.5.3            whisker_0.4             
[43] yaml_2.2.1               grid_4.0.3               blob_1.2.1              
[46] parallel_4.0.3           promises_1.1.1           crayon_1.3.4            
[49] lattice_0.20-41          haven_2.3.1              seacarb_3.2.14          
[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.0 data.table_1.13.2        modelr_0.1.8            
[61] vctrs_0.3.5              httpuv_1.5.4             cellranger_1.1.0        
[64] gtable_0.3.0             assertthat_0.2.1         xfun_0.18               
[67] lwgeom_0.2-5             broom_0.7.5              RcppEigen_0.3.3.7.0     
[70] e1071_1.7-4              later_1.1.0.1            class_7.3-17            
[73] units_0.6-7              ellipsis_0.3.1