USF-IMARS / seus-mbon-cruise-ctd-processing

CTD processing & reporting for the SEUS MBON research cruise data
https://usf-imars.github.io/seus-mbon-cruise-ctd-processing/
0 stars 0 forks source link

Minor fix to code #1

Open sebastiandig opened 6 months ago

sebastiandig commented 6 months ago

I found the reason why you needed to do cast <- ctd_FK[[i]][[1]] with the added [[1]] brace.

In the process of converting the data into a CTD object, an additional step was added that may or may not be preferred. We can discuss which version would be more preferable.

The data was split like this

# Split data by station and create data list
ctd_FK <- WS23061 %>%
  split(.$station) %>%
  map(~ ctd_load(.x, other_params = other_params))

# alternative, and won't need the extra `[[1]]`:
ctd_FK <- WS23061 %>% ctd_load(other_params = other_params)

This creates a nested list of CTD station objects in the form:

list(
  $station 1
    $station 1 CTD object
  $station 2
    $station 2 CTD object
)

The function takes care of the multiple stations using a loop which also creates a list. The reason why the list is nested is because of the creation of the list in the function while giving it a list. The original intent was to give it all cruise files and have a list of cruises with each cruise containing sites, but now it's a site nested within a list of sites.

list(
  $cruise1
    $station 1 CTD object
    $station 2 CTD object
  $cruise 2
    $station 1 CTD object
    $station 2 CTD object
)

There are two solutions:

  1. Change input code to function:
    WS23061 %>%
    ctd_load(.x, other_params = other_params)
  2. Change ctd_load code

    ctd_load <- function(data, other_params = NULL) {
    # stations <- unique(data$station) # <-- remove
    # final_ctd <- list() # <-- remove
    
    # for (station in stations) {# <-- remove
    # cat(station, "\n") # <-- remove
    test <- filter(data, station == station)
    
    # create csv into ctd object
    test_ctd <-
      as.ctd(
        salinity    = test$sea_water_salinity,
        temperature = test$sea_water_temperature,
        pressure    = test$sea_water_pressure,
        station     = test$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  = test[[param_name]]
          )
      }
    }
    
    # final_ctd[[station]] <- test_ctd# <-- remove
    # }
    
    # return(final_ctd)# <-- remove
    return(test_ctd)
    }
sebastiandig commented 6 months ago

Located: Dissertation_code/analysis/example_load_ctd_oce

Also, I updated the ctd_load function to also take a data.frame with better names and adding units. This will also check if the column names exist by comparing the column names of the raw data to the expected parameters.

Another improvement is the event level information like cruise ID, station, etc are in the metadata instead of a layer in the CTD object. Modified the as.ctd() function.

The original use will still work by giving a vector of column names.

sebastiandig commented 6 months ago

@7yl4r

Location: Dissertation_code/R/ctd_load_wrapper.R

I modified the ctd_load function to be two functions with one for a single station (ctd_load_station) and another as a cruise (ctd_load_cruise).

ctd_load_station - takes a single set of station data and converts to CTD object. The minimum column names are:

ctd_load_cruise - a wrapper function on ctd_load_station where you would need the same column names as above plus an additional column for stations names specified by the station_col argument. Optionally, you can add the cruise ID.

Hopefully, this makes it a little easier to give one CTD cast at a time or optionally more. I tested it on a "raw" CTD file and luckily still works.