ArgoCanada / argoFloats

Tools for analyzing collections of oceanographic Argo floats
https://argocanada.github.io/argoFloats/index.html
17 stars 7 forks source link

Does useAdjusted copy <PARAM>_ADJUSTED into <PARAM> or is it simply [[ altered? #538

Closed j-harbin closed 2 years ago

j-harbin commented 2 years ago

I'm creating an issue for Clark's question he had regarding useAdjusted. We know if the user does useAdjusted, and then for example, does x[["temperature"]], they WILL get the adjusted data. We want to figure out if it's because A) copied the temperatureAdjusted into temperature OR B) because we told [[ to grab temperatureAdjusted even though temperature is specified.

This is relevant in his work because he:

  1. Reads argo data
  2. uses useAdjusted on it
  3. Turns it into a ctd object, which strictly reads salinity, temperature, and pressure from the argo object

The question is will the temperature in his ctd object be the ADJUSTED data (which would be the case if we copied the adjusted field in the useAdjusted) OR will his ctd object have the unadjusted data (which would be the case if we simply told [[ to extract adjusted data).

I tried to make a test case for this where I changed the adjusted values in an argo float to be all 100 (something that's easy to distinguish), but because of the restraints of argoFloats, which I agree with, I'm not allowed to go [[ <-. This is because it's highly discouraged for users to change data within a float. (See code below)

library(argoFloats)
file <- "SD5903586_001.nc"
argos <- readProfiles(system.file("extdata", file, package="argoFloats"))
#> Warning in readProfiles(system.file("extdata", file, package = "argoFloats")): Of 1 profiles read, 1 has >10% of BBP700 values with QC flag of 4, signalling bad data.
#>     The indices of the bad profiles are as follows.
#>     1
#> Warning in readProfiles(system.file("extdata", file, package = "argoFloats")): Of 1 profiles read, 1 has >10% of chlorophyllA values with QC flag of 4, signalling bad data.
#>     The indices of the bad profiles are as follows.
#>     1
#> Warning in readProfiles(system.file("extdata", file, package = "argoFloats")): Of 1 profiles read, 1 has >10% of oxygen values with QC flag of 4, signalling bad data.
#>     The indices of the bad profiles are as follows.
#>     1
matrix <- matrix(100, nrow=dim(argos[[1]][['temperatureAdjusted']])[1], ncol=dim(argos[[1]][['temperatureAdjusted']])[2])
argos[[1]][['temperatureAdjusted']] <- matrix
#> Error in `[[<-`(`*tmp*`, 1, value = new("argo", metadata = list(units = list(: [[<- defined for objects of type "S4" only for subclasses of environment

Created on 2021-12-02 by the reprex package (v2.0.0)

I'll take a deeper look at this, but it does suggest once I have a test case that the docs should be updated to be more clear. Further to that, I'll take this opportunity to suggest we add the useAdjusted diagram back into the help docs for useAdjusted. I say this because whenever I have a question about useAdjusted I refer back to this diagram in the tweet thread.

Screen Shot 2021-12-02 at 2 54 14 PM

dankelley commented 2 years ago

Perhaps the following answers the question from @richardsc but I'm not paying enough attention to be sure what the issue is actually about.

library(argoFloats)
file <- "SD5903586_001.nc"
A <- readProfiles(system.file("extdata", file, package="argoFloats"))
#> Warning in readProfiles(system.file("extdata", file, package = "argoFloats")): Of 1 profiles read, 1 has >10% of BBP700 values with QC flag of 4, signalling bad data.
#>     The indices of the bad profiles are as follows.
#>     1
#> Warning in readProfiles(system.file("extdata", file, package = "argoFloats")): Of 1 profiles read, 1 has >10% of chlorophyllA values with QC flag of 4, signalling bad data.
#>     The indices of the bad profiles are as follows.
#>     1
#> Warning in readProfiles(system.file("extdata", file, package = "argoFloats")): Of 1 profiles read, 1 has >10% of oxygen values with QC flag of 4, signalling bad data.
#>     The indices of the bad profiles are as follows.
#>     1
B <- useAdjusted(A)

cat("Original data\n")
#> Original data
cat(oce::vectorShow(A[[1]][["pressure"]]))
#> A[[1]][["pressure"]] [1:548, 1:1]: 4.2, 6.0, ..., 977.9, 999.9
cat(oce::vectorShow(A[[1]]@data$pressure))
#> A[[1]]@data$pressure [1:548, 1:1]: 4.2, 6.0, ..., 977.9, 999.9
cat(oce::vectorShow(A[[1]][["pressureAdjusted"]]))
#> A[[1]][["pressureAdjusted"]] [1:548, 1:1]: 4.23, 6.03, ..., 977.93, 999.93
cat(oce::vectorShow(A[[1]]@data$pressureAdjusted))
#> A[[1]]@data$pressureAdjusted [1:548, 1:1]: 4.23, 6.03, ..., 977.93, 999.93

cat("Result from useAdjusted()\n")
#> Result from useAdjusted()
cat(oce::vectorShow(B[[1]][["pressure"]]))
#> B[[1]][["pressure"]] [1:548, 1:1]: 4.23, 6.03, ..., 977.93, 999.93
cat(oce::vectorShow(B[[1]]@data$pressure))
#> B[[1]]@data$pressure [1:548, 1:1]: 4.23, 6.03, ..., 977.93, 999.93
cat(oce::vectorShow(B[[1]][["pressureAdjusted"]]))
#> B[[1]][["pressureAdjusted"]] [1:548, 1:1]: 4.23, 6.03, ..., 977.93, 999.93
cat(oce::vectorShow(B[[1]]@data$pressureAdjusted))
#> B[[1]]@data$pressureAdjusted [1:548, 1:1]: 4.23, 6.03, ..., 977.93, 999.93

Created on 2021-12-02 by the reprex package (v2.0.1)

dankelley commented 2 years ago

Also, if the Q is about as.ctd(), note that

library(argoFloats)
library(oce)
file <- "SD5903586_001.nc"
A <- readProfiles(system.file("extdata", file, package="argoFloats"))
B <- useAdjusted(A)

cat("Original data\n")
cat(oce::vectorShow(A[[1]][["pressure"]]))
cat(oce::vectorShow(A[[1]]@data$pressure))
cat(oce::vectorShow(A[[1]][["pressureAdjusted"]]))
cat(oce::vectorShow(A[[1]]@data$pressureAdjusted))
Actd <- as.ctd(A[[1]])
cat(oce::vectorShow(Actd[["pressure"]]))
cat(oce::vectorShow(Actd@data$pressure))
cat(oce::vectorShow(Actd[["pressureAdjusted"]]))
cat(oce::vectorShow(Actd@data$pressureAdjusted))

cat("Result from useAdjusted()\n")
cat(oce::vectorShow(B[[1]][["pressure"]]))
cat(oce::vectorShow(B[[1]]@data$pressure))
cat(oce::vectorShow(B[[1]][["pressureAdjusted"]]))
cat(oce::vectorShow(B[[1]]@data$pressureAdjusted))
Bctd <- as.ctd(B[[1]])
cat(oce::vectorShow(Bctd[["pressure"]]))
cat(oce::vectorShow(Bctd@data$pressure))
cat(oce::vectorShow(Bctd[["pressureAdjusted"]]))
cat(oce::vectorShow(Bctd@data$pressureAdjusted))

yields

Original data
A[[1]][["pressure"]] [1:548, 1:1]: 4.2, 6.0, ..., 977.9, 999.9
A[[1]]@data$pressure [1:548, 1:1]: 4.2, 6.0, ..., 977.9, 999.9
A[[1]][["pressureAdjusted"]] [1:548, 1:1]: 4.23, 6.03, ..., 977.93, 999.93
A[[1]]@data$pressureAdjusted [1:548, 1:1]: 4.23, 6.03, ..., 977.93, 999.93
Actd[["pressure"]] [1:548]: 4.2, 6.0, ..., 977.9, 999.9
Actd@data$pressure [1:548]: 4.2, 6.0, ..., 977.9, 999.9
Actd[["pressureAdjusted"]] [1:548]: 4.23, 6.03, ..., 977.93, 999.93
Actd@data$pressureAdjusted [1:548]: 4.23, 6.03, ..., 977.93, 999.93
Result from useAdjusted()
B[[1]][["pressure"]] [1:548, 1:1]: 4.23, 6.03, ..., 977.93, 999.93
B[[1]]@data$pressure [1:548, 1:1]: 4.23, 6.03, ..., 977.93, 999.93
B[[1]][["pressureAdjusted"]] [1:548, 1:1]: 4.23, 6.03, ..., 977.93, 999.93
B[[1]]@data$pressureAdjusted [1:548, 1:1]: 4.23, 6.03, ..., 977.93, 999.93
Bctd[["pressure"]] [1:548]: 4.23, 6.03, ..., 977.93, 999.93
Bctd@data$pressure [1:548]: 4.23, 6.03, ..., 977.93, 999.93
Bctd[["pressureAdjusted"]] [1:548]: 4.23, 6.03, ..., 977.93, 999.93
Bctd@data$pressureAdjusted [1:548]: 4.23, 6.03, ..., 977.93, 999.93
github-actions[bot] commented 2 years ago

The Stale-bot has marked this issue as Stale, because no new comments have been added in the past 30 days. Unless a comment is added within the next 7 days, the Stale-bot will close the issue. The purpose of these automated actions is to prevent the developers from forgetting about unattended tasks. Note that adding a "pinned" label will turn this action off for a given issue.

j-harbin commented 2 years ago

I see that you answered this on December 02nd, and I'm just noticing it now. In summary, useAdjusted works by copying over the adjusted field (ie. not because we told [[ to grab the adjusted field). This means if you do the following:

  1. Read argo data
  2. Apply useAdjusted
  3. Turns it into a ctd object, which strictly reads salinity, temperature, and pressure from the argo object

If you do ctd[["temperature"]] you will be receiving the adjusted values. Hope this helps @richardsc. Thanks for the code @dankelley .

library(argoFloats)
library(oce)
#> Loading required package: gsw
file <- "SD5903586_001.nc"
A <- readProfiles(system.file("extdata", file, package="argoFloats"))
B <- useAdjusted(A)
cat(oce::vectorShow(A[[1]][["pressure"]]))
#> A[[1]][["pressure"]] [1:548, 1:1]: 4.2, 6.0, ..., 977.9, 999.9
cat(oce::vectorShow(B[[1]][["pressure"]]))
#> B[[1]][["pressure"]] [1:548, 1:1]: 4.23, 6.03, ..., 977.93, 999.93
Bctd <- as.ctd(B[[1]])
cat(oce::vectorShow(Bctd[["pressure"]]))
#> Bctd[["pressure"]] [1:548]: 4.23, 6.03, ..., 977.93, 999.93

Created on 2022-01-20 by the reprex package (v2.0.0)