WS21212

setup
if (!nzchar(system.file(package = "librarian"))) {
  install.packages("librarian")
}

librarian::shelf(
  quiet = TRUE,
  readr, here, fs, ggplot2, glue, "jiho/castr", dplyr, oce, patchwork, purrr, tidyr
)
Code
source(here("R/cruise_load.R"))
cruise_df <- cruise_load(params$cruise_id)
plot depth & pressure vs time elapsed
tryCatch({
  plots <- list()
  
  for (station_name in unique(cruise_df$station)) {
    subset_df <- filter(cruise_df, station == station_name)
    plots[[station_name]] <- ggplot(subset_df, aes(x = time_elapsed)) +
      geom_point(aes(y = depth), color = "blue") +  # Plot depth in blue
      geom_line(aes(y = sea_water_pressure), color = "red") +  # Plot sea water pressure in red
      ggtitle(glue("{station_name}")) +
      theme(
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.x = element_blank(),
        axis.ticks.y = element_blank()
      )  # Remove individual axis titles and text
  }
  
  # drop nulls
  plots <- purrr::compact(plots)
  
  # Combine all plots into a grid
  combined_plot <- wrap_plots(plots) + 
    plot_layout(ncol = 4) +  # Adjust ncol to set number of columns in the grid
    plot_annotation(
      title = "Depth (blue) and Pressure (red)",
      subtitle = "Each panel represents a different station",
      caption = "Time Elapsed (x-axis) vs Depth & Pressure (y-axis)"
    ) +
    theme(
      plot.tag = element_text(size = 12, face = "bold"),
      plot.tag.position = "topleft",
      axis.title.x = element_text(margin = margin(t = 10)),
      axis.title.y = element_text(margin = margin(r = 10))
    )
  
  print(combined_plot)
}, error = function(er){
  print(er)
});

plot across all stations
p <- ggplot(cruise_df, aes(x = time, y = depth, fill = station)) +
  geom_col() +  # This creates the bars
  # geom_text(aes(label = station), vjust = -0.3) +  # This adds labels to each bar, adjust vjust for position
  labs(x = "Time", y = "Depth", title = "Depth over Time by Station") +  # Set labels and title
  theme_minimal()  # Use a minimal theme
print(p)

create oce.ctd objects from dataframes
ctd_load <- function(data, other_params = NULL) {
  
  # create csv into ctd object
  test_ctd <-
    as.ctd(
      salinity    = data$sea_water_salinity,
      temperature = data$sea_water_temperature,
      pressure    = data$sea_water_pressure,
      station     = data$station
    )
  
  # add additional columns to ctd object
  if (!is.null(other_params)) {
    for (param_name in other_params) {
      test_ctd <-
        oceSetData(
          object = test_ctd,
          name   = param_name,
          value  = data[[param_name]]
        )
    }
  }
  print(glue("{data$station[1]}:\t{length(test_ctd@data$scan)} scans"))


  return(test_ctd)
}

# Define other parameters to add
other_params <- c(
  "cruise_id", "station", "time", "time_elapsed", 
  "latitude", "longitude", "sea_water_electrical_conductivity", 
  "CDOM", "dissolved_oxygen","oxygen_saturation", "chlorophyll_concentration", 
  "chlorophyll_fluorescence", "photosynthetically_available_radiation", 
  "beam_attenuation","beam_transmission", "depth", "sea_water_sigma_t",
  "descent_rate", "sound_velocity","altimeter"
)

# Split data by station and create data list
ctd_FK <- cruise_df %>%
  split(.$station) %>%
  map(~ ctd_load(.x, other_params = other_params)) # ~ is a lambda(x)
002:    3517 scans
007:    2660 scans
009:    1545 scans
009.5:  7129 scans
010:    1534 scans
012:    3807 scans
016:    1060 scans
018:    2942 scans
021LK:  4301 scans
030:    3165 scans
031:    3025 scans
033:    1758 scans
041:    2915 scans
045:    2772 scans
047:    2751 scans
049:    2473 scans
051:    2008 scans
054:    364 scans
055:    41 scans
056:    53 scans
057:    1985 scans
057.1:  900 scans
057.2:  3312 scans
057.3:  3851 scans
058:    1737 scans
060:    707 scans
064:    194 scans
065:    441 scans
068:    67 scans
AMI1:   2897 scans
AMI2:   1584 scans
AMI3:   1819 scans
AMI4:   2264 scans
AMI5:   3470 scans
AMI6:   3345 scans
AMI7:   2782 scans
AMI8:   3547 scans
AMI9:   3438 scans
BG1:    3260 scans
BG2:    4752 scans
BG3:    4039 scans
BG4:    3313 scans
CAL1:   2729 scans
CAL2:   3372 scans
CAL3:   4220 scans
CAL4:   1910 scans
CAL5:   2228 scans
CAL6:   2932 scans
CW1:    132 scans
CW2:    3760 scans
CW3:    2728 scans
CW4:    3473 scans
GP5:    4622 scans
KW1:    3080 scans
KW2:    2775 scans
KW4:    4339 scans
MR: 3733 scans
RP1:    3493 scans
RP2:    3805 scans
RP3:    3772 scans
RP4:    3459 scans
TB1:    2895 scans
TB10:   5203 scans
TB2:    2727 scans
TB3:    3730 scans
TB4:    4740 scans
TB5:    4354 scans
V1: 2911 scans
V2: 4073 scans
V3: 3899 scans
V4: 3986 scans
V5: 4406 scans
V6: 6245 scans
V7: 4541 scans
V8: 4966 scans
V9: 6442 scans
WS: 4593 scans
WS2:    671 scans
plotting scans for each cast in the first list
for (i in seq(ctd_FK)){
  cast <- ctd_FK[[i]]  # 1 is selecting only the first sublist
  # print(i)
  print(glue("=== station: {cast@metadata$station[1]}"))
  print(glue("# scans: {length(cast@data$scan)}"))
  plotScan(cast)
}
=== station: 002
# scans: 3517

=== station: 007
# scans: 2660

=== station: 009
# scans: 1545

=== station: 009.5
# scans: 7129

=== station: 010
# scans: 1534

=== station: 012
# scans: 3807

=== station: 016
# scans: 1060

=== station: 018
# scans: 2942

=== station: 021LK
# scans: 4301

=== station: 030
# scans: 3165

=== station: 031
# scans: 3025

=== station: 033
# scans: 1758

=== station: 041
# scans: 2915

=== station: 045
# scans: 2772

=== station: 047
# scans: 2751

=== station: 049
# scans: 2473

=== station: 051
# scans: 2008

=== station: 054
# scans: 364

=== station: 055
# scans: 41

=== station: 056
# scans: 53

=== station: 057
# scans: 1985

=== station: 057.1
# scans: 900

=== station: 057.2
# scans: 3312

=== station: 057.3
# scans: 3851

=== station: 058
# scans: 1737

=== station: 060
# scans: 707

=== station: 064
# scans: 194

=== station: 065
# scans: 441

=== station: 068
# scans: 67

=== station: AMI1
# scans: 2897

=== station: AMI2
# scans: 1584

=== station: AMI3
# scans: 1819

=== station: AMI4
# scans: 2264

=== station: AMI5
# scans: 3470

=== station: AMI6
# scans: 3345

=== station: AMI7
# scans: 2782

=== station: AMI8
# scans: 3547

=== station: AMI9
# scans: 3438

=== station: BG1
# scans: 3260

=== station: BG2
# scans: 4752

=== station: BG3
# scans: 4039

=== station: BG4
# scans: 3313

=== station: CAL1
# scans: 2729

=== station: CAL2
# scans: 3372

=== station: CAL3
# scans: 4220

=== station: CAL4
# scans: 1910

=== station: CAL5
# scans: 2228

=== station: CAL6
# scans: 2932

=== station: CW1
# scans: 132

=== station: CW2
# scans: 3760

=== station: CW3
# scans: 2728

=== station: CW4
# scans: 3473

=== station: GP5
# scans: 4622

=== station: KW1
# scans: 3080

=== station: KW2
# scans: 2775

=== station: KW4
# scans: 4339

=== station: MR
# scans: 3733

=== station: RP1
# scans: 3493

=== station: RP2
# scans: 3805

=== station: RP3
# scans: 3772

=== station: RP4
# scans: 3459

=== station: TB1
# scans: 2895

=== station: TB10
# scans: 5203

=== station: TB2
# scans: 2727

=== station: TB3
# scans: 3730

=== station: TB4
# scans: 4740

=== station: TB5
# scans: 4354

=== station: V1
# scans: 2911

=== station: V2
# scans: 4073

=== station: V3
# scans: 3899

=== station: V4
# scans: 3986

=== station: V5
# scans: 4406

=== station: V6
# scans: 6245

=== station: V7
# scans: 4541

=== station: V8
# scans: 4966

=== station: V9
# scans: 6442

=== station: WS
# scans: 4593

=== station: WS2
# scans: 671

plotting each cast in the first list
for (i in seq(ctd_FK)){
  cast <- ctd_FK[[i]]  # 1 is selecting only the first sublist
  tryCatch({
    plot(ctdDecimate(ctdTrim(cast)))
  }, error = function(e){
    print(e)
  })
}

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3517' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2660' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 1545' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 7129' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 1534' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3807' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2942' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4301' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3165' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3025' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 1758' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2915' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2772' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2751' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2473' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2008' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 1985' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3312' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3851' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 1737' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 194' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 441' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2897' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 1584' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 1819' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2264' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3470' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3345' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2782' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3547' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3438' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3260' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4752' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4039' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3313' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2729' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3372' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4220' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 1910' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2228' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2932' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 132' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3760' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2728' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3473' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4622' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3080' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2775' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4339' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3733' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3493' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3805' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3772' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3459' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2895' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 5203' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2727' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3730' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4740' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4354' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 2911' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4073' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3899' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 3986' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4406' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 6245' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4541' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4966' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 6442' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 4593' in coercion to 'logical(1)'>

<simpleError in !is.null(x@metadata$station) && !is.na(x@metadata$station): 'length = 671' in coercion to 'logical(1)'>
plotting other physical parameters for each cast
# Loop through each CTD cast
for (i in seq(ctd_FK)){
  cast <- ctd_FK[[i]]  # Assuming each sublist contains only one relevant CTD object
  tryCatch({
    # Extract metadata for station name and cruise ID
    station_name <- cast@metadata$station[1]
    cruise_id <- cast@data$cruise_id[1]

    # Generate a title with station name and cruise ID
    overall_title <- glue::glue("Station: {station_name}, Cruise ID: {cruise_id}")

    # Set margins: increase the outer margin for the title
    par(oma = c(0, 0, 3, 0))  # Top outer margin increased for title

    # Plotting function with specific parameters
    oce::plot(
      x = ctdDecimate(ctdTrim(cast)),
      which = c(
        "sea_water_electrical_conductivity",
        "descent_rate", "sound_velocity",
        "sea_water_sigma_t","altimeter"
      ),
      main = ""  # No main title for individual subplots
    )

    # Place a single overall title at the top of the plot frame
    mtext(overall_title, side = 3, line = 1, outer = TRUE, cex = 1.5)

    # Reset outer margins to default
    par(oma = c(0, 0, 0, 0))

  }, error = function(e) {
    print(e$message)  # Print any errors that occur during plotting
  })
}

[1] "need finite 'xlim' values"
[1] "need finite 'xlim' values"
[1] "need finite 'xlim' values"
[1] "need finite 'xlim' values"
[1] "need finite 'xlim' values"
[1] "need finite 'xlim' values"
[1] "need finite 'xlim' values"
[1] "need finite 'xlim' values"
[1] "need finite 'xlim' values"
[1] "need finite 'xlim' values"
[1] "need finite 'xlim' values"
[1] "need finite 'xlim' values"
plotting other nutrient parameters for each cast
# Loop through each CTD cast
for (i in seq(ctd_FK)){
  cast <- ctd_FK[[i]]  # Assuming each sublist contains only one relevant CTD object
  tryCatch({
    # Extract metadata for station name and cruise ID
    station_name <- cast@metadata$station[1]
    cruise_id <- cast@data$cruise_id[1]

    # Generate a title with station name and cruise ID
    overall_title <- glue::glue("Station: {station_name}, Cruise ID: {cruise_id}")

    # Set margins: increase the outer margin for the title
    par(oma = c(0, 0, 3, 0))  # Top outer margin increased for title

    # Plotting function with specific parameters
    oce::plot(
      x = ctdDecimate(ctdTrim(cast)),
      which = c(
        "CDOM", "dissolved_oxygen",
        "oxygen_saturation",
        "chlorophyll_concentration", "chlorophyll_fluorescence"
      ),
      main = ""  # No main title for individual subplots
    )

    # Place a single overall title at the top of the plot frame
    mtext(overall_title, side = 3, line = 1, outer = TRUE, cex = 1.5)

    # Reset outer margins to default
    par(oma = c(0, 0, 0, 0))

  }, error = function(e) {
    print(e$message)  # Print any errors that occur during plotting
  })
}
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "plot.new has not been called yet"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "plot.new has not been called yet"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
[1] "In plot,ctd-method() : which=\"CDOMdissolved_oxygenoxygen_saturationchlorophyll_concentrationchlorophyll_fluorescence\" cannot be handled"
plotting other optical parameters for each cast
# Loop through each CTD cast
for (i in seq(ctd_FK)){
  cast <- ctd_FK[[i]]  # Assuming each sublist contains only one relevant CTD object
  tryCatch({
    # Extract metadata for station name and cruise ID
    station_name <- cast@metadata$station[1]
    cruise_id <- cast@data$cruise_id[1]

    # Generate a title with station name and cruise ID
    overall_title <- glue::glue("Station: {station_name}, Cruise ID: {cruise_id}")

    # Set margins: increase the outer margin for the title
    par(oma = c(0, 0, 3, 0))  # Top outer margin increased for title

    # Plotting function with specific parameters
    oce::plot(
      x = ctdDecimate(ctdTrim(cast)),
      which = c(
        "photosynthetically_available_radiation",
        "beam_attenuation","beam_transmission"
      ),
      main = ""  # No main title for individual subplots
    )

    # Place a single overall title at the top of the plot frame
    mtext(overall_title, side = 3, line = 1, outer = TRUE, cex = 1.5)

    # Reset outer margins to default
    par(oma = c(0, 0, 0, 0))

  }, error = function(e) {
    print(e$message)  # Print any errors that occur during plotting
  })
}

[1] "need finite 'xlim' values"
[1] "need finite 'xlim' values"
[1] "need finite 'xlim' values"
[1] "need finite 'xlim' values"
[1] "need finite 'xlim' values"
loop through every cast, clean, & save
combined_df <- data.frame()
for (i in seq(ctd_FK)){
  tryCatch({
    cast <- ctd_FK[[i]]  # 1 is selecting only the first sublist
    
    # print(class(cast))
    # clean cast 
    trimmed_cast <- ctdTrim(cast)
    decimated_cast <- ctdDecimate(trimmed_cast, p = 0.5)  # binned to 0.5 m
    
    # convert to df
    cast_df <- as.data.frame(decimated_cast@data)
    
    # Add metadata
    # assumes station ID and cruise ID the same for all & just uses 1st one
    cast_df <- mutate(
      cast_df,
      station = cast@data$station[1],
      cruise_id = cast@data$cruise_id[1]
    )
  
    # drop NA rows left by cleaning
    cast_df <- subset(cast_df, !is.na(scan))
    
    # Append the data to the combined dataframe
    combined_df <- rbind(combined_df, cast_df)
  }, error = function(e){
    print(glue("error in cast {cast@metadata$station[1]}"))
    print(e)
  })
}
# Save to CSV
file_path <- here(glue("data/cleaned/{cruise_id}.csv"))
write.csv(combined_df, file_path, row.names = FALSE)