Last updated: 2022-10-23

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 e855cad. 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

Unstaged changes:
    Modified:   code/Workflowr_project_managment.R

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/GLODAP_subset_A_monthly_CESM.Rmd) and HTML (docs/GLODAP_subset_A_monthly_CESM.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 e829a11 jens-daniel-mueller 2022-07-05 Build site.
html 89cbb74 jens-daniel-mueller 2022-05-26 Build site.
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.
html 2319f2e jens-daniel-mueller 2022-05-21 Build site.
html 2257c34 jens-daniel-mueller 2022-05-20 Build site.
Rmd 6f7af17 jens-daniel-mueller 2022-05-20 rebuild with revised output structure
html 06054b2 jens-daniel-mueller 2022-05-10 Build site.
Rmd d43e8c0 jens-daniel-mueller 2022-05-10 rerun all with multi model subsetting

path_GLODAP_preprocessing <-
  paste(path_root, "/observations/preprocessing/", sep = "")

path_cmorized <-
  "/nfs/kryo/work/loher/CESM_output/RECCAP2/cmorized_March2021/split_monthly/"

path_preprocessing <-
  paste0(path_root, "/model/preprocessing/GLODAP_subset_A_all_models/")

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 Read GLODAPv2_2021 preprocessed files

GLODAP <-
  read_csv(paste(path_GLODAP_preprocessing,
                 "GLODAPv2.2021_preprocessed.csv",
                 sep = ""))

GLODAP <- GLODAP %>%
  mutate(month = month(date))

# GLODAP <- GLODAP %>%
#   filter(year <= 2019)

3 Subset cmorized data according to GLODAP observation

Here we subset cmorized (1x1) data from the model with variable forcing, according to the presence of GLODAP observations in a previously cleaned file.

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_ID <- "A"

# for loop across years
years <- c("1982":"2020")

for (i_year in years) {
  # i_year <- years[39]
  
  # select GLODAP data for that year
  GLODAP_year <- GLODAP %>%
    filter(year == i_year)
  
  # create month x lat x lon grid of observations
  Glodap_year_grid_horizontal <- GLODAP_year %>%
    distinct(month, lat, lon)
  
  # create month x lat x lon x depth grid of observations
  Glodap_year_grid_depth <- GLODAP_year %>%
    distinct(month, lat, lon, depth)
  
  # for loop across variables
  variables <-
    c("so", "thetao", "dissic", "talk", "o2", "no3", "po4", "si")
  
  for (i_variable in variables) {
    # i_variable <- variables[3]
    
    # read list of all files
    
    if (i_year <= 2019) {
      file <-
        list.files(
          path = path_cmorized,
          pattern = paste(
            "^",
            i_variable,
            "_CESM-ETHZ_",
            model_ID,
            "_1_gr_",
            i_year,
            ".nc",
            sep = ""
          )
        )
    } else {
      file <-
        list.files(
          path = path_cmorized,
          pattern = paste(
            "^",
            i_variable,
            "_CESM-ETHZ_",
            model_ID,
            "_1_gr_",
            i_year-1,
            ".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 %>%
      filter(!is.na(!!sym(i_variable)))
    
    # 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(month = month(time_mon)) %>%
      select(-time_mon)
    
    # calculate model summary stats
    stats <- variable_data_tibble %>%
      pull(!!sym(i_variable)) %>%
      summary()
    
    stats <- c(year = i_year, variable = i_variable, stats)
    
    if (exists("stats_summary")) {
      stats_summary <- bind_rows(stats_summary, stats)
    }
    
    if (!exists("stats_summary")) {
      stats_summary <- stats
    }
    
    rm(stats)
    
    # subset model at month x lat x lon grid of observations
    model_grid_horizontal <-
      inner_join(Glodap_year_grid_horizontal, variable_data_tibble)
    
    # join model and month x lat x lon x depth grid of observations
    model_obs <-
      full_join(model_grid_horizontal, Glodap_year_grid_depth)
    
    # calculate nr of observations per month x lat x lon grid
    model_obs <- model_obs %>%
      group_by(month, lat, lon) %>%
      mutate(n = sum(!is.na(!!sym(i_variable)))) %>%
      ungroup()
    
    # set variable value from model for observation depth, if only one model depth available
    model_obs_set <- model_obs %>%
      filter(n == 1) %>%
      group_by(lon, lat, month) %>%
      mutate(!!sym(i_variable) := mean(!!sym(i_variable), na.rm = TRUE)) %>%
      ungroup()
    
    # interpolate variable value from model to observation depth
    model_obs_interpo <- model_obs %>%
      filter(n > 1) %>%
      group_by(lon, lat, month) %>%
      arrange(depth) %>%
      mutate(!!sym(i_variable) := approxfun(depth, !!sym(i_variable), rule = 2)(depth)) %>%
      ungroup()
    
    # join interpolated and set model values
    model_obs_interpo <- full_join(model_obs_interpo, model_obs_set)
    rm(model_obs_set)
    
    # subsetted interpolated values at observation depth
    model_obs_interpo <-
      inner_join(Glodap_year_grid_depth, model_obs_interpo) %>%
      select(-n) %>%
      mutate(year = as.numeric(i_year))
    
    # select observation grids without corresponding model subset
    na_model <-
      full_join(Glodap_year_grid_depth, model_obs_interpo) %>%
      filter(is.na(!!sym(i_variable))) %>%
      select(month, lat, lon) %>%
      unique()
    
    # rename interpolated model variable to indicate as model output
    model_obs_interpo <- model_obs_interpo %>%
      rename(!!sym(paste(i_variable, "model", sep = "_")) := !!sym(i_variable))
    
    # add model subset to GLODAP
    GLODAP <-
      natural_join(
        GLODAP,
        model_obs_interpo,
        by = c("year", "month", "lat", "lon", "depth"),
        jointype = "FULL"
      )
    
    # calculate annual average variable
    variable_data_tibble_annual_average <- variable_data_tibble %>%
      fselect(-month) %>%
      fgroup_by(lat, lon, depth) %>% {
        add_vars(fgroup_vars(., "unique"),
                 fmean(., keep.group_vars = FALSE))
      }
    
    # select surface annual average variable
    variable_data_tibble_annual_average_surface <-
      variable_data_tibble_annual_average %>%
      filter(depth == min(depth))
    
    # surface map of variable
    map +
      geom_raster(data = variable_data_tibble_annual_average_surface,
                  aes(lon, lat, fill = !!sym(i_variable))) +
      scale_fill_viridis_c(name = i_variable) +
      geom_raster(data = model_obs_interpo,
                  aes(lon, lat), fill = "black") +
      geom_raster(data = na_model,
                  aes(lon, lat), fill = "red") +
      labs(
        title = "GLODAP-based cmorized subset distribution",
        subtitle = paste(
          "Model depth: 5m | Annual average of year",
          i_year,
          "| red = subsetting failed"
        ),
        x = "Longitude",
        y = "Latitude"
      )
    
    ggsave(
      paste(
        path_preprocessing,
        "regular_subset_distribution_runA_2021/",
        "CESM-ETHZ_v20211122_monthly_",
        i_variable,
        "_",
        i_year,
        ".png",
        sep = ""
      ),
      width = 5,
      height = 3
    )
  }
  
}
[1] "so_CESM-ETHZ_A_1_gr_1982.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_1982.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_1982.nc"
[1] "talk_CESM-ETHZ_A_1_gr_1982.nc"
[1] "o2_CESM-ETHZ_A_1_gr_1982.nc"
[1] "no3_CESM-ETHZ_A_1_gr_1982.nc"
[1] "po4_CESM-ETHZ_A_1_gr_1982.nc"
[1] "si_CESM-ETHZ_A_1_gr_1982.nc"
[1] "so_CESM-ETHZ_A_1_gr_1983.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_1983.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_1983.nc"
[1] "talk_CESM-ETHZ_A_1_gr_1983.nc"
[1] "o2_CESM-ETHZ_A_1_gr_1983.nc"
[1] "no3_CESM-ETHZ_A_1_gr_1983.nc"
[1] "po4_CESM-ETHZ_A_1_gr_1983.nc"
[1] "si_CESM-ETHZ_A_1_gr_1983.nc"
[1] "so_CESM-ETHZ_A_1_gr_1984.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_1984.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_1984.nc"
[1] "talk_CESM-ETHZ_A_1_gr_1984.nc"
[1] "o2_CESM-ETHZ_A_1_gr_1984.nc"
[1] "no3_CESM-ETHZ_A_1_gr_1984.nc"
[1] "po4_CESM-ETHZ_A_1_gr_1984.nc"
[1] "si_CESM-ETHZ_A_1_gr_1984.nc"
[1] "so_CESM-ETHZ_A_1_gr_1985.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_1985.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_1985.nc"
[1] "talk_CESM-ETHZ_A_1_gr_1985.nc"
[1] "o2_CESM-ETHZ_A_1_gr_1985.nc"
[1] "no3_CESM-ETHZ_A_1_gr_1985.nc"
[1] "po4_CESM-ETHZ_A_1_gr_1985.nc"
[1] "si_CESM-ETHZ_A_1_gr_1985.nc"
[1] "so_CESM-ETHZ_A_1_gr_1986.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_1986.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_1986.nc"
[1] "talk_CESM-ETHZ_A_1_gr_1986.nc"
[1] "o2_CESM-ETHZ_A_1_gr_1986.nc"
[1] "no3_CESM-ETHZ_A_1_gr_1986.nc"
[1] "po4_CESM-ETHZ_A_1_gr_1986.nc"
[1] "si_CESM-ETHZ_A_1_gr_1986.nc"
[1] "so_CESM-ETHZ_A_1_gr_1987.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_1987.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_1987.nc"
[1] "talk_CESM-ETHZ_A_1_gr_1987.nc"
[1] "o2_CESM-ETHZ_A_1_gr_1987.nc"
[1] "no3_CESM-ETHZ_A_1_gr_1987.nc"
[1] "po4_CESM-ETHZ_A_1_gr_1987.nc"
[1] "si_CESM-ETHZ_A_1_gr_1987.nc"
[1] "so_CESM-ETHZ_A_1_gr_1988.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_1988.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_1988.nc"
[1] "talk_CESM-ETHZ_A_1_gr_1988.nc"
[1] "o2_CESM-ETHZ_A_1_gr_1988.nc"
[1] "no3_CESM-ETHZ_A_1_gr_1988.nc"
[1] "po4_CESM-ETHZ_A_1_gr_1988.nc"
[1] "si_CESM-ETHZ_A_1_gr_1988.nc"
[1] "so_CESM-ETHZ_A_1_gr_1989.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_1989.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_1989.nc"
[1] "talk_CESM-ETHZ_A_1_gr_1989.nc"
[1] "o2_CESM-ETHZ_A_1_gr_1989.nc"
[1] "no3_CESM-ETHZ_A_1_gr_1989.nc"
[1] "po4_CESM-ETHZ_A_1_gr_1989.nc"
[1] "si_CESM-ETHZ_A_1_gr_1989.nc"
[1] "so_CESM-ETHZ_A_1_gr_1990.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_1990.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_1990.nc"
[1] "talk_CESM-ETHZ_A_1_gr_1990.nc"
[1] "o2_CESM-ETHZ_A_1_gr_1990.nc"
[1] "no3_CESM-ETHZ_A_1_gr_1990.nc"
[1] "po4_CESM-ETHZ_A_1_gr_1990.nc"
[1] "si_CESM-ETHZ_A_1_gr_1990.nc"
[1] "so_CESM-ETHZ_A_1_gr_1991.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_1991.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_1991.nc"
[1] "talk_CESM-ETHZ_A_1_gr_1991.nc"
[1] "o2_CESM-ETHZ_A_1_gr_1991.nc"
[1] "no3_CESM-ETHZ_A_1_gr_1991.nc"
[1] "po4_CESM-ETHZ_A_1_gr_1991.nc"
[1] "si_CESM-ETHZ_A_1_gr_1991.nc"
[1] "so_CESM-ETHZ_A_1_gr_1992.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_1992.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_1992.nc"
[1] "talk_CESM-ETHZ_A_1_gr_1992.nc"
[1] "o2_CESM-ETHZ_A_1_gr_1992.nc"
[1] "no3_CESM-ETHZ_A_1_gr_1992.nc"
[1] "po4_CESM-ETHZ_A_1_gr_1992.nc"
[1] "si_CESM-ETHZ_A_1_gr_1992.nc"
[1] "so_CESM-ETHZ_A_1_gr_1993.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_1993.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_1993.nc"
[1] "talk_CESM-ETHZ_A_1_gr_1993.nc"
[1] "o2_CESM-ETHZ_A_1_gr_1993.nc"
[1] "no3_CESM-ETHZ_A_1_gr_1993.nc"
[1] "po4_CESM-ETHZ_A_1_gr_1993.nc"
[1] "si_CESM-ETHZ_A_1_gr_1993.nc"
[1] "so_CESM-ETHZ_A_1_gr_1994.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_1994.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_1994.nc"
[1] "talk_CESM-ETHZ_A_1_gr_1994.nc"
[1] "o2_CESM-ETHZ_A_1_gr_1994.nc"
[1] "no3_CESM-ETHZ_A_1_gr_1994.nc"
[1] "po4_CESM-ETHZ_A_1_gr_1994.nc"
[1] "si_CESM-ETHZ_A_1_gr_1994.nc"
[1] "so_CESM-ETHZ_A_1_gr_1995.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_1995.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_1995.nc"
[1] "talk_CESM-ETHZ_A_1_gr_1995.nc"
[1] "o2_CESM-ETHZ_A_1_gr_1995.nc"
[1] "no3_CESM-ETHZ_A_1_gr_1995.nc"
[1] "po4_CESM-ETHZ_A_1_gr_1995.nc"
[1] "si_CESM-ETHZ_A_1_gr_1995.nc"
[1] "so_CESM-ETHZ_A_1_gr_1996.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_1996.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_1996.nc"
[1] "talk_CESM-ETHZ_A_1_gr_1996.nc"
[1] "o2_CESM-ETHZ_A_1_gr_1996.nc"
[1] "no3_CESM-ETHZ_A_1_gr_1996.nc"
[1] "po4_CESM-ETHZ_A_1_gr_1996.nc"
[1] "si_CESM-ETHZ_A_1_gr_1996.nc"
[1] "so_CESM-ETHZ_A_1_gr_1997.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_1997.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_1997.nc"
[1] "talk_CESM-ETHZ_A_1_gr_1997.nc"
[1] "o2_CESM-ETHZ_A_1_gr_1997.nc"
[1] "no3_CESM-ETHZ_A_1_gr_1997.nc"
[1] "po4_CESM-ETHZ_A_1_gr_1997.nc"
[1] "si_CESM-ETHZ_A_1_gr_1997.nc"
[1] "so_CESM-ETHZ_A_1_gr_1998.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_1998.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_1998.nc"
[1] "talk_CESM-ETHZ_A_1_gr_1998.nc"
[1] "o2_CESM-ETHZ_A_1_gr_1998.nc"
[1] "no3_CESM-ETHZ_A_1_gr_1998.nc"
[1] "po4_CESM-ETHZ_A_1_gr_1998.nc"
[1] "si_CESM-ETHZ_A_1_gr_1998.nc"
[1] "so_CESM-ETHZ_A_1_gr_1999.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_1999.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_1999.nc"
[1] "talk_CESM-ETHZ_A_1_gr_1999.nc"
[1] "o2_CESM-ETHZ_A_1_gr_1999.nc"
[1] "no3_CESM-ETHZ_A_1_gr_1999.nc"
[1] "po4_CESM-ETHZ_A_1_gr_1999.nc"
[1] "si_CESM-ETHZ_A_1_gr_1999.nc"
[1] "so_CESM-ETHZ_A_1_gr_2000.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_2000.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_2000.nc"
[1] "talk_CESM-ETHZ_A_1_gr_2000.nc"
[1] "o2_CESM-ETHZ_A_1_gr_2000.nc"
[1] "no3_CESM-ETHZ_A_1_gr_2000.nc"
[1] "po4_CESM-ETHZ_A_1_gr_2000.nc"
[1] "si_CESM-ETHZ_A_1_gr_2000.nc"
[1] "so_CESM-ETHZ_A_1_gr_2001.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_2001.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_2001.nc"
[1] "talk_CESM-ETHZ_A_1_gr_2001.nc"
[1] "o2_CESM-ETHZ_A_1_gr_2001.nc"
[1] "no3_CESM-ETHZ_A_1_gr_2001.nc"
[1] "po4_CESM-ETHZ_A_1_gr_2001.nc"
[1] "si_CESM-ETHZ_A_1_gr_2001.nc"
[1] "so_CESM-ETHZ_A_1_gr_2002.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_2002.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_2002.nc"
[1] "talk_CESM-ETHZ_A_1_gr_2002.nc"
[1] "o2_CESM-ETHZ_A_1_gr_2002.nc"
[1] "no3_CESM-ETHZ_A_1_gr_2002.nc"
[1] "po4_CESM-ETHZ_A_1_gr_2002.nc"
[1] "si_CESM-ETHZ_A_1_gr_2002.nc"
[1] "so_CESM-ETHZ_A_1_gr_2003.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_2003.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_2003.nc"
[1] "talk_CESM-ETHZ_A_1_gr_2003.nc"
[1] "o2_CESM-ETHZ_A_1_gr_2003.nc"
[1] "no3_CESM-ETHZ_A_1_gr_2003.nc"
[1] "po4_CESM-ETHZ_A_1_gr_2003.nc"
[1] "si_CESM-ETHZ_A_1_gr_2003.nc"
[1] "so_CESM-ETHZ_A_1_gr_2004.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_2004.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_2004.nc"
[1] "talk_CESM-ETHZ_A_1_gr_2004.nc"
[1] "o2_CESM-ETHZ_A_1_gr_2004.nc"
[1] "no3_CESM-ETHZ_A_1_gr_2004.nc"
[1] "po4_CESM-ETHZ_A_1_gr_2004.nc"
[1] "si_CESM-ETHZ_A_1_gr_2004.nc"
[1] "so_CESM-ETHZ_A_1_gr_2005.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_2005.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_2005.nc"
[1] "talk_CESM-ETHZ_A_1_gr_2005.nc"
[1] "o2_CESM-ETHZ_A_1_gr_2005.nc"
[1] "no3_CESM-ETHZ_A_1_gr_2005.nc"
[1] "po4_CESM-ETHZ_A_1_gr_2005.nc"
[1] "si_CESM-ETHZ_A_1_gr_2005.nc"
[1] "so_CESM-ETHZ_A_1_gr_2006.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_2006.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_2006.nc"
[1] "talk_CESM-ETHZ_A_1_gr_2006.nc"
[1] "o2_CESM-ETHZ_A_1_gr_2006.nc"
[1] "no3_CESM-ETHZ_A_1_gr_2006.nc"
[1] "po4_CESM-ETHZ_A_1_gr_2006.nc"
[1] "si_CESM-ETHZ_A_1_gr_2006.nc"
[1] "so_CESM-ETHZ_A_1_gr_2007.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_2007.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_2007.nc"
[1] "talk_CESM-ETHZ_A_1_gr_2007.nc"
[1] "o2_CESM-ETHZ_A_1_gr_2007.nc"
[1] "no3_CESM-ETHZ_A_1_gr_2007.nc"
[1] "po4_CESM-ETHZ_A_1_gr_2007.nc"
[1] "si_CESM-ETHZ_A_1_gr_2007.nc"
[1] "so_CESM-ETHZ_A_1_gr_2008.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_2008.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_2008.nc"
[1] "talk_CESM-ETHZ_A_1_gr_2008.nc"
[1] "o2_CESM-ETHZ_A_1_gr_2008.nc"
[1] "no3_CESM-ETHZ_A_1_gr_2008.nc"
[1] "po4_CESM-ETHZ_A_1_gr_2008.nc"
[1] "si_CESM-ETHZ_A_1_gr_2008.nc"
[1] "so_CESM-ETHZ_A_1_gr_2009.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_2009.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_2009.nc"
[1] "talk_CESM-ETHZ_A_1_gr_2009.nc"
[1] "o2_CESM-ETHZ_A_1_gr_2009.nc"
[1] "no3_CESM-ETHZ_A_1_gr_2009.nc"
[1] "po4_CESM-ETHZ_A_1_gr_2009.nc"
[1] "si_CESM-ETHZ_A_1_gr_2009.nc"
[1] "so_CESM-ETHZ_A_1_gr_2010.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_2010.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_2010.nc"
[1] "talk_CESM-ETHZ_A_1_gr_2010.nc"
[1] "o2_CESM-ETHZ_A_1_gr_2010.nc"
[1] "no3_CESM-ETHZ_A_1_gr_2010.nc"
[1] "po4_CESM-ETHZ_A_1_gr_2010.nc"
[1] "si_CESM-ETHZ_A_1_gr_2010.nc"
[1] "so_CESM-ETHZ_A_1_gr_2011.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_2011.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_2011.nc"
[1] "talk_CESM-ETHZ_A_1_gr_2011.nc"
[1] "o2_CESM-ETHZ_A_1_gr_2011.nc"
[1] "no3_CESM-ETHZ_A_1_gr_2011.nc"
[1] "po4_CESM-ETHZ_A_1_gr_2011.nc"
[1] "si_CESM-ETHZ_A_1_gr_2011.nc"
[1] "so_CESM-ETHZ_A_1_gr_2012.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_2012.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_2012.nc"
[1] "talk_CESM-ETHZ_A_1_gr_2012.nc"
[1] "o2_CESM-ETHZ_A_1_gr_2012.nc"
[1] "no3_CESM-ETHZ_A_1_gr_2012.nc"
[1] "po4_CESM-ETHZ_A_1_gr_2012.nc"
[1] "si_CESM-ETHZ_A_1_gr_2012.nc"
[1] "so_CESM-ETHZ_A_1_gr_2013.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_2013.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_2013.nc"
[1] "talk_CESM-ETHZ_A_1_gr_2013.nc"
[1] "o2_CESM-ETHZ_A_1_gr_2013.nc"
[1] "no3_CESM-ETHZ_A_1_gr_2013.nc"
[1] "po4_CESM-ETHZ_A_1_gr_2013.nc"
[1] "si_CESM-ETHZ_A_1_gr_2013.nc"
[1] "so_CESM-ETHZ_A_1_gr_2014.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_2014.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_2014.nc"
[1] "talk_CESM-ETHZ_A_1_gr_2014.nc"
[1] "o2_CESM-ETHZ_A_1_gr_2014.nc"
[1] "no3_CESM-ETHZ_A_1_gr_2014.nc"
[1] "po4_CESM-ETHZ_A_1_gr_2014.nc"
[1] "si_CESM-ETHZ_A_1_gr_2014.nc"
[1] "so_CESM-ETHZ_A_1_gr_2015.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_2015.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_2015.nc"
[1] "talk_CESM-ETHZ_A_1_gr_2015.nc"
[1] "o2_CESM-ETHZ_A_1_gr_2015.nc"
[1] "no3_CESM-ETHZ_A_1_gr_2015.nc"
[1] "po4_CESM-ETHZ_A_1_gr_2015.nc"
[1] "si_CESM-ETHZ_A_1_gr_2015.nc"
[1] "so_CESM-ETHZ_A_1_gr_2016.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_2016.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_2016.nc"
[1] "talk_CESM-ETHZ_A_1_gr_2016.nc"
[1] "o2_CESM-ETHZ_A_1_gr_2016.nc"
[1] "no3_CESM-ETHZ_A_1_gr_2016.nc"
[1] "po4_CESM-ETHZ_A_1_gr_2016.nc"
[1] "si_CESM-ETHZ_A_1_gr_2016.nc"
[1] "so_CESM-ETHZ_A_1_gr_2017.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_2017.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_2017.nc"
[1] "talk_CESM-ETHZ_A_1_gr_2017.nc"
[1] "o2_CESM-ETHZ_A_1_gr_2017.nc"
[1] "no3_CESM-ETHZ_A_1_gr_2017.nc"
[1] "po4_CESM-ETHZ_A_1_gr_2017.nc"
[1] "si_CESM-ETHZ_A_1_gr_2017.nc"
[1] "so_CESM-ETHZ_A_1_gr_2018.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_2018.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_2018.nc"
[1] "talk_CESM-ETHZ_A_1_gr_2018.nc"
[1] "o2_CESM-ETHZ_A_1_gr_2018.nc"
[1] "no3_CESM-ETHZ_A_1_gr_2018.nc"
[1] "po4_CESM-ETHZ_A_1_gr_2018.nc"
[1] "si_CESM-ETHZ_A_1_gr_2018.nc"
[1] "so_CESM-ETHZ_A_1_gr_2019.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_2019.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_2019.nc"
[1] "talk_CESM-ETHZ_A_1_gr_2019.nc"
[1] "o2_CESM-ETHZ_A_1_gr_2019.nc"
[1] "no3_CESM-ETHZ_A_1_gr_2019.nc"
[1] "po4_CESM-ETHZ_A_1_gr_2019.nc"
[1] "si_CESM-ETHZ_A_1_gr_2019.nc"
[1] "so_CESM-ETHZ_A_1_gr_2019.nc"
[1] "thetao_CESM-ETHZ_A_1_gr_2019.nc"
[1] "dissic_CESM-ETHZ_A_1_gr_2019.nc"
[1] "talk_CESM-ETHZ_A_1_gr_2019.nc"
[1] "o2_CESM-ETHZ_A_1_gr_2019.nc"
[1] "no3_CESM-ETHZ_A_1_gr_2019.nc"
[1] "po4_CESM-ETHZ_A_1_gr_2019.nc"
[1] "si_CESM-ETHZ_A_1_gr_2019.nc"
# write raw data file for GLODAP-based subsetting model variables
GLODAP %>%
  write_csv(
    paste(
      path_preprocessing,
      "CESM-ETHZ_v20211122_monthly_GLODAPv2.2021_preprocessed_model_runA_raw_subset.csv",
      sep = ""
    )
  )

# write file for model summary statistics (original cmorized unit)
stats_summary %>%
  write_csv(
    paste(
      path_preprocessing,
      "regular_subset_distribution_runA_2021/",
      "CESM-ETHZ_v20211122_monthly",
      "_",
      model_ID,
      "_summary_stats.csv",
      sep = ""
    )
  )
# read file for GLODAP-based subsetting model variables
GLODAP <-
  read_csv(
    paste(
      path_preprocessing,
      "CESM-ETHZ_v20211122_monthly_GLODAPv2.2021_preprocessed_model_runA_raw_subset.csv",
      sep = ""
    ),
    col_types = cols(
      .default = col_double(),
      basin_AIP = col_character(),
      date = col_date(format = ""),
      dissic_model = col_double(),
      no3_model = col_double(),
      o2_model = col_double(),
      po4_model = col_double(),
      si_model = col_double(),
      so_model = col_double(),
      talk_model = col_double(),
      thetao_model = col_double()
    )
  )


# filter out GLODAP grids without model subset
GLODAP <- GLODAP %>%
  filter(!is.na(so_model))

# calculate model temperature
GLODAP <- GLODAP %>%
  mutate(temp_model = gsw_pt_from_t(
    SA = so_model,
    t = thetao_model,
    p = 10.1325,
    p_ref = depth
  ))

# unit conversion from mol/m3 to µmol/kg
GLODAP <- GLODAP %>%
  mutate(
    rho = gsw_pot_rho_t_exact(
      SA = so_model,
      t = temp_model,
      p = depth,
      p_ref = 10.1325
    ),
    dissic_model = dissic_model * (1e+6 / rho),
    talk_model = talk_model * (1e+6 / rho),
    o2_model = o2_model * (1e+6 / rho),
    no3_model = no3_model * (1e+6 / rho),
    po4_model = po4_model * (1e+6 / rho),
    si_model = si_model * (1e+6 / rho)
  )

# calculate AOU
GLODAP <- GLODAP %>%
  mutate(
    oxygen_sat_m3 = gas_satconc(
      S = so_model,
      t = temp_model,
      P = 1.013253,
      species = "O2"
    ),
    oxygen_sat_kg = oxygen_sat_m3 * (1e+3 / rho),
    aou_model = oxygen_sat_kg - o2_model
  ) %>%
  select(-oxygen_sat_kg, -oxygen_sat_m3)

# calculate gamma model in temporary data frame
GLODAP_gamma <- GLODAP %>%
  select(depth, lat,
         so_model, thetao_model, lon)

GLODAP_gamma <- GLODAP_gamma %>%
  mutate(CTDPRS = gsw_p_from_z(-depth,
                               lat))

GLODAP_gamma <- GLODAP_gamma %>%
  rename(
    LATITUDE = lat,
    LONGITUDE = lon,
    SALNTY = so_model,
    THETA = thetao_model
  )

source_python(
  paste(
    path_root,
    "/utilities/functions/python_scripts/",
    "Gamma_GLODAP_python.py",
    sep = ""
  )
)

GLODAP_gamma <- calculate_gamma(GLODAP_gamma)

# join gamma column with original data frame
GLODAP <- GLODAP %>%
  mutate(gamma_model = GLODAP_gamma$GAMMA)

GLODAP_NA_filled <- GLODAP

rm(GLODAP_gamma)

# Replace model value with NA, if observations are NA
GLODAP$so_model <-
  ifelse(is.na(GLODAP$sal), NA, GLODAP$so_model)

GLODAP$thetao_model <-
  ifelse(is.na(GLODAP$theta), NA, GLODAP$thetao_model)

GLODAP$temp_model <-
  ifelse(is.na(GLODAP$temp), NA, GLODAP$temp_model)

GLODAP$dissic_model <-
  ifelse(is.na(GLODAP$tco2), NA, GLODAP$dissic_model)

GLODAP$talk_model <-
  ifelse(is.na(GLODAP$talk), NA, GLODAP$talk_model)

GLODAP$o2_model <-
  ifelse(is.na(GLODAP$oxygen), NA, GLODAP$o2_model)

GLODAP$no3_model <-
  ifelse(is.na(GLODAP$nitrate), NA, GLODAP$no3_model)

GLODAP$po4_model <-
  ifelse(is.na(GLODAP$phosphate), NA, GLODAP$po4_model)

GLODAP$si_model <-
  ifelse(is.na(GLODAP$silicate), NA, GLODAP$si_model)

GLODAP$aou_model <-
  ifelse(is.na(GLODAP$aou), NA, GLODAP$aou_model)


# write file for GLODAP observations + subsetted model variables
GLODAP %>%
  write_csv(
    paste(
      path_preprocessing,
      "CESM-ETHZ_v20211122_monthly_GLODAPv2.2021_preprocessed_model_runA_both.csv",
      sep = ""
    )
  )

GLODAP_NA_filled %>%
  write_csv(
    paste(
      path_preprocessing,
      "CESM-ETHZ_v20211122_monthly_GLODAPv2.2021_preprocessed_model_runA_both_NA_filled.csv",
      sep = ""
    )
  )

# remove GLODAP observations and rename model subset
GLODAP <- GLODAP %>%
  select(-c(
    sal,
    theta,
    temp,
    tco2,
    talk,
    oxygen,
    nitrate,
    phosphate,
    silicate,
    aou,
    gamma
  )) %>%
  rename(
    sal = so_model,
    theta = thetao_model,
    temp = temp_model,
    tco2 = dissic_model,
    talk = talk_model,
    oxygen = o2_model,
    nitrate = no3_model,
    phosphate = po4_model,
    silicate = si_model,
    aou = aou_model,
    gamma = gamma_model
  )


GLODAP_NA_filled <- GLODAP_NA_filled %>%
  select(-c(
    sal,
    theta,
    temp,
    tco2,
    talk,
    oxygen,
    nitrate,
    phosphate,
    silicate,
    aou,
    gamma
  )) %>%
  rename(
    sal = so_model,
    theta = thetao_model,
    temp = temp_model,
    tco2 = dissic_model,
    talk = talk_model,
    oxygen = o2_model,
    nitrate = no3_model,
    phosphate = po4_model,
    silicate = si_model,
    aou = aou_model,
    gamma = gamma_model
  )

4 Write files

# write final file for GLODAP-based subsetting model variables
GLODAP %>%
  select(-row_number) %>% 
  write_csv(paste(path_preprocessing,
                  "CESM-ETHZ_v20211122_monthly_GLODAPv2.2021_preprocessed_model_runA_final.csv",
                  sep = ""))

GLODAP_NA_filled %>%
  select(-row_number) %>% 
  write_csv(paste(path_preprocessing,
                  "CESM-ETHZ_v20211122_monthly_GLODAPv2.2021_preprocessed_model_runA_final_NA_filled.csv",
                  sep = ""))

5 Test for interpolation

GLODAP <-
  read_csv(paste(path_GLODAP_preprocessing,
                 "GLODAPv2.2021_preprocessed.csv",
                 sep = ""))

GLODAP <- GLODAP %>%
  mutate(month = month(date))

# select GLODAP data for 2019
GLODAP_2019 <- GLODAP %>%
  filter(year == 2019)

# create month x lat x lon grid of observations
Glodap_year_grid_horizontal <- GLODAP_2019 %>%
  select(month, lat, lon) %>%
  unique()

# create month x lat x lon x depth grid of observations
Glodap_year_grid_depth <- GLODAP_2019 %>%
  select(month, lat, lon, depth) %>%
  unique()

# read in cmorized tco2 output in year 2019
variable_data_tibble <-
  read_ncdf(paste(path_cmorized, 
    "dissic_CESM-ETHZ_A_1_gr_2019.nc",
    sep = ""
  )) %>%
  as_tibble()

# remove na values
variable_data_tibble <-
  variable_data_tibble %>%
  filter(!is.na(dissic))

# 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(month = month(time_mon), dissic = as.numeric(dissic)) %>%
  select(-time_mon)

# subset model at month x lat x lon grid of observations
model_grid_horizontal <-
  inner_join(Glodap_year_grid_horizontal, variable_data_tibble)

# join model and month x lat x lon x depth grid of observations
model_obs <-
  full_join(model_grid_horizontal, Glodap_year_grid_depth)

# calculate nr of observations per month x lat x lon grid
model_obs <- model_obs %>%
  group_by(month, lat, lon) %>%
  mutate(n = sum(!is.na(dissic))) %>%
  ungroup()

# interpolate variable value from model to observation depth
model_obs_interpo <- model_obs %>%
  filter(n > 1) %>%
  group_by(lon, lat, month) %>%
  arrange(month, lat, lon, depth) %>%
  mutate(dissic_interpolate = approxfun(depth, dissic, rule = 2)(depth)) %>%
  ungroup()

ggplot() +
  geom_path(
    data = model_obs_interpo %>%
      filter(lat == -47.5, lon == 306.5, !is.na(dissic), month == 4) %>%
      arrange(depth),
    aes(dissic, depth, col = "model")
  ) +
  geom_point(
    data = model_obs_interpo %>%
      filter(lat == -47.5, lon == 306.5, !is.na(dissic), month == 4) %>%
      arrange(depth),
    aes(dissic, depth, col = "model")
  ) +
  geom_point(
    data = model_obs_interpo %>%
      filter(lat == -47.5, lon == 306.5, is.na(dissic), month == 4),
    aes(dissic_interpolate, depth, col = "interpolated")
  ) +
  scale_y_reverse() +
  scale_color_brewer(palette = "Dark2", name = "") +
  labs(title = "Interpolation to sampling depth - tco2 of year 2019")

6 Test for obs-model depth offset

GLODAP_max_depth <- GLODAP %>%
  select(lon, lat, depth) %>%
  group_by(lon, lat) %>%
  summarise(max_depth_GLODAP = max(depth))

model_max_depth <- variable_data_tibble %>%
  select(lon, lat, depth) %>%
  group_by(lon, lat) %>%
  summarise(max_depth_model = max(depth))

GLODAP_model_depth_offset <-
  left_join(GLODAP_max_depth, model_max_depth) %>%
  mutate(offset = max_depth_GLODAP - max_depth_model)

map +
  geom_raster(
    data = GLODAP_model_depth_offset %>% 
      filter(offset > 500),
    aes(lon, lat, fill = offset)
  ) +
  scale_fill_viridis_c(direction =  -1) +
  labs(
    title = "GLODAP-model max sampling depth offset",
    subtitle = "Year 1971 - 2019",
    x = "Longitude",
    y = "Latitude"
  )

7 GLODAP observations vs cmorized subset comparison

GLODAP_cmorized <- read_csv(paste(path_preprocessing,
                                  "CESM-ETHZ_v20211122_monthly_GLODAPv2.2021_preprocessed_model_runA_both.csv",
                                  sep = ""))
# for loop across variables
obs_var <-
  c(
    "tco2",
    "talk",
    "oxygen",
    "aou",
    "nitrate",
    "phosphate",
    "silicate",
    "sal",
    "temp",
    "theta",
    "gamma"
  )

model_var <-
  c(
    "dissic_model",
    "talk_model",
    "o2_model",
    "aou_model",
    "no3_model",
    "po4_model",
    "si_model",
    "so_model",
    "temp_model",
    "thetao_model",
    "gamma_model"
  )

for (i in 1:11) {
#  i <- 1
  
  # select correlated observation and model variable
  GLODAP_cmorized_var <- GLODAP_cmorized %>%
    select(year,
           month,
           lat,
           lon,
           depth,
           basin_AIP,
           !!sym(obs_var[i]),
           !!sym(model_var[i])) %>%
    drop_na() %>%
    mutate(
      season = case_when(
        month %in% c(3, 4, 5) ~ "Spring",
        month %in% c(6, 7, 8) ~ "Summer",
        month %in% c(9, 10, 11) ~ "Autumn",
        month %in% c(12, 1, 2) ~ "Winter"
      )
    )
  
  # calculate equal axis limits and binwidth
  axis_lims <- GLODAP_cmorized_var %>%
    summarise(max_value = max(c(max(!!sym(
      obs_var[i]
    )),
    max(
      !!sym(model_var[i])
    ))),
    min_value = min(c(min(!!sym(
      obs_var[i]
    )),
    min(
      !!sym(model_var[i])
    ))))
  
  binwidth_value <- (axis_lims$max_value - axis_lims$min_value) / 40
  axis_lims <- c(axis_lims$min_value, axis_lims$max_value)
  
  # obs-model plot (season)
  print(
    ggplot(GLODAP_cmorized_var, aes(
      x = !!sym(obs_var[i]),
      y = !!sym(model_var[i])
    )) +
      geom_bin2d(binwidth = binwidth_value) +
      labs(title = "Observation (x) vs Model (y)",
           subtitle = "Seasonal comparison") +
      scale_fill_viridis_c(trans = "log10") +
      geom_abline(slope = 1,
                  col = 'red') +
      coord_equal(xlim = axis_lims,
                  ylim = axis_lims) +
      facet_wrap( ~ season)
  )
  
  # obs-model plot (year)
  print(
    ggplot(GLODAP_cmorized_var, aes(
      x = !!sym(obs_var[i]),
      y = !!sym(model_var[i])
    )) +
      geom_bin2d(binwidth = binwidth_value) +
      labs(title = "Observation (x) vs Model (y)",
           subtitle = "All years") +
      scale_fill_viridis_c(trans = "log10") +
      geom_abline(slope = 1, col = 'red') +
      coord_equal(xlim = axis_lims,
                  ylim = axis_lims)
  )
  
  
  # Calculate variable offset
  GLODAP_cmorized_var <- GLODAP_cmorized_var %>%
    mutate(offset = !!sym(model_var[i]) -!!sym(obs_var[i]))
  
  # Calculate annual mean offset
  GLODAP_cmorized_var_year <- GLODAP_cmorized_var %>%
    group_by(year) %>%
    summarise(offset = mean(offset)) %>%
    ungroup()
  
  # plot annual mean offset
  print(
    GLODAP_cmorized_var_year %>%
      ggplot(aes(year, offset)) +
      geom_point() +
      geom_line() +
      labs(title = "Annual mean offset",
           subtitle = paste(sym(model_var[i]), "-", sym(obs_var[i])))
  )
  
  # spatial distribution of the model-observations offset for 4 depth intervals
  intervals <- c(0, 150, 500, 2000, 8000)
  for (j in 1:4) {
    # j <- 1
    GLODAP_cmorized_var_grid <- GLODAP_cmorized_var %>%
      filter(depth >= intervals[j],
             depth < intervals[j + 1]) %>%
      group_by(lat, lon) %>%
      summarise(offset = mean(offset)) %>%
      ungroup()
    
    # plot mean spatial distribution (model - obs)
    limit <-
      quantile(abs(GLODAP_cmorized_var_grid$offset), 0.98) * c(-1, 1)
    print(
      map +
        geom_raster(data = GLODAP_cmorized_var_grid, aes(lon, lat, fill = offset)) +
        scale_fill_scico(
          palette = "vik",
          name = "offset",
          limit = limit,
          na.value = "red"
        ) +
        coord_quickmap(expand = 0) +
        labs(
          title = paste(model_var[i],
                        "-",
                        obs_var[i],
                        " | mean offset per grid cell"),
          subtitle = paste(
            "Depth interval:",
            intervals[j],
            "-",
            intervals[j + 1],
            "m | red: outside 98th percentile"
          ),
          x = "Longitude",
          y = "Latitude"
        )
    )
    
    print(
      GLODAP_cmorized_var_grid %>%
        ggplot(aes(offset)) +
        geom_vline(xintercept = limit) +
        geom_histogram() +
        labs(
          title = paste(
            model_var[i],
            " - ",
            obs_var[i],
            "mean offset per grid cell | histogram + abs. 98th Percentile"
          ),
          subtitle = paste("Depth interval:",
                           intervals[j], "-", intervals[j + 1], "m")
        ) +
        coord_cartesian(expand = FALSE)
    )
  }
  
  # zonal mean section of the model-observations offset for each basin
  for (i_basin_AIP in unique(GLODAP_cmorized_var$basin_AIP)) {
   # i_basin_AIP = "Atlantic"
    GLODAP_cmorized_var_zonal <- GLODAP_cmorized_var %>%
      filter(basin_AIP == i_basin_AIP) %>%
      group_by(lat, depth) %>%
      summarise(offset = mean(offset, na.rm=TRUE)) %>%
      ungroup()
    
    # plot zonal mean section (model - obs)
    lat_max <- params_global$lat_max
    lat_min <- params_global$lat_min
    limit <-
      quantile(abs(GLODAP_cmorized_var_zonal$offset), 0.98) * c(-1, 1)
    
    print(
      GLODAP_cmorized_var_zonal %>%
        ggplot(aes(lat, depth, z = offset)) +
        stat_summary_2d(binwidth = c(1,100)) +
        scale_fill_scico(
          palette = "vik",
          name = "offset",
          limit = limit,
          na.value = "red"
        ) +
        coord_cartesian(expand = 0,
                        xlim = c(lat_min, lat_max)) +
        scale_y_reverse() +
        labs(
          title = paste(model_var[i],
                        "-",
                        obs_var[i],
                        " | zonal mean offset"),
          subtitle = paste("Basin:",
                           i_basin_AIP),
          x = "Latitude",
          y = "Depth"
        )
    )
  }
}

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

Version Author Date
06054b2 jens-daniel-mueller 2022-05-10

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] reticulate_1.23    oce_1.5-0          gsw_1.0-6          rqdatatable_1.3.0 
 [5] rquery_1.4.7       wrapr_2.0.8        lubridate_1.8.0    stars_0.5-5       
 [9] sf_1.0-5           abind_1.4-5        geomtextpath_0.1.0 colorspace_2.0-2  
[13] marelac_2.1.10     shape_1.4.6        ggforce_0.3.3      metR_0.11.0       
[17] scico_1.3.0        patchwork_1.1.1    collapse_1.7.0     forcats_0.5.1     
[21] stringr_1.4.0      dplyr_1.0.7        purrr_0.3.4        readr_2.1.1       
[25] tidyr_1.1.4        tibble_3.1.6       ggplot2_3.3.5      tidyverse_1.3.1   
[29] 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         knitr_1.37         polyclip_1.10-0   
[13] jsonlite_1.7.3     broom_0.7.11       dbplyr_2.1.1       png_0.1-7         
[17] compiler_4.1.2     httr_1.4.2         backports_1.4.1    assertthat_0.2.1  
[21] Matrix_1.4-0       fastmap_1.1.0      cli_3.1.1          later_1.3.0       
[25] tweenr_1.0.2       htmltools_0.5.2    tools_4.1.2        gtable_0.3.0      
[29] glue_1.6.0         rappdirs_0.3.3     Rcpp_1.0.8         RNetCDF_2.5-2     
[33] cellranger_1.1.0   jquerylib_0.1.4    vctrs_0.3.8        lwgeom_0.2-8      
[37] xfun_0.29          ps_1.6.0           rvest_1.0.2        ncmeta_0.3.0      
[41] lifecycle_1.0.1    getPass_0.2-2      MASS_7.3-55        scales_1.1.1      
[45] vroom_1.5.7        hms_1.1.1          promises_1.2.0.1   parallel_4.1.2    
[49] yaml_2.2.1         sass_0.4.0         stringi_1.7.6      highr_0.9         
[53] e1071_1.7-9        checkmate_2.0.0    rlang_1.0.2        pkgconfig_2.0.3   
[57] systemfonts_1.0.3  evaluate_0.14      lattice_0.20-45    SolveSAPHE_2.1.0  
[61] labeling_0.4.2     bit_4.0.4          processx_3.5.2     tidyselect_1.1.1  
[65] here_1.0.1         seacarb_3.3.0      magrittr_2.0.1     R6_2.5.1          
[69] generics_0.1.1     DBI_1.1.2          pillar_1.6.4       haven_2.4.3       
[73] whisker_0.4        withr_2.4.3        units_0.7-2        modelr_0.1.8      
[77] crayon_1.4.2       KernSmooth_2.23-20 utf8_1.2.2         tzdb_0.2.0        
[81] rmarkdown_2.11     grid_4.1.2         readxl_1.3.1       data.table_1.14.2 
[85] callr_3.7.0        git2r_0.29.0       reprex_2.0.1       digest_0.6.29     
[89] classInt_0.4-3     httpuv_1.6.5       textshaping_0.3.6  munsell_0.5.0     
[93] viridisLite_0.4.0  bslib_0.3.1