Temperature and salinity data from Argo but Error appeared $ operator is invalid for atomic vectors

52 Views Asked by At

I am trying to extract temperature and salinity data from Argovis website "https://argovis.colorado.edu/selection/profiles/" to run modeling analysis with fish catch. I used the code from "https://www.itsonlyamodel.us/argovis-r-api.html" assigned especially for extracting temperature and salinity for the different oceans depth profiles.

    library(httr)
options(warn=-1)

get.profile <- function(profileName){
  baseURL <- "https://argovis.colorado.edu/catalog/profiles/"
  url <- paste(baseURL, profileName, sep="")
  resp <- GET(url)
  if(resp$status_code==200) {
    profile = content(resp, "parsed")
  }
  else {
    profile = profile = content(resp, "raw")
  }
  return(profile)
}

parse.into.df <- function(profile) {
  paramNames <- names(profile)
  meas <- profile$measurements
  names = unique(names(unlist(meas)))
  df <- data.frame(matrix(unlist(meas), nrow=length(meas), byrow=T))
  colnames(df) <- names
  df$profile_id <- profile$`_id`
  df$date <- profile$date
  df$cycle_number <- profile$cycle_number
  df$lat <- profile$lat
  df$lon <- profile$lon
  return (df)
}
profileName = "3900737_279"
profile = get.profile(profileName)
df = parse.into.df(profile)
head(df)

get.platform <- function(platformNumber){
  baseURL <- "https://argovis.colorado.edu/catalog/platforms/"
  url <- paste(baseURL, platformNumber, sep="")
  resp <- GET(url)
  if(resp$status_code==200) {
    profiles <- content(resp, "parsed")
  }
  else {
    profiles = content(resp, "raw")
  }
  return(profiles)
}
platformNumber <- '3900737'
platformProfiles <- get.platform(platformNumber)

platformDf <- data.frame()
for (profile in platformProfiles)
{
  df <- parse.into.df(profile)
  platformDf <- rbind(platformDf, df)
}
tail(platformDf)

get.selection <- function(startDate, endDate, shape, presRange){
  baseURL <- "https://argovis.colorado.edu/selection/profiles/"
  startDateQuery <- paste('?startDate=', startDate, sep="")
  endDateQuery <- paste('&endDate;=', endDate, sep="")
  shapeQuery <- paste('&shape;=', shape, sep="")

  if(missing(presRange)) {
    url <- paste(baseURL, startDateQuery, endDateQuery, shapeQuery, sep="")
  }
  else {
    presRangeQuery = paste('&presRange;=', presRange, sep="")
    url <- paste(baseURL, startDateQuery, endDateQuery, presRangeQuery, shapeQuery, sep="")   
  }
  resp <- GET(url)
  if(resp$status_code==200) {
    profiles <- content(resp, "parsed")
  }
  else {
    profiles <- content(resp, "raw")
  }
  return(profiles)
}
startDate='2017-9-15'
endDate='2017-10-31'
shape = '[[[-18.6,31.7],[-18.6,37.7],[-5.9,37.7],[-5.9,31.7],[-18.6,31.7]]]'
presRange='[0,30]'

selectionProfiles = get.selection(startDate, endDate, shape, presRange)
selectionDf <- data.frame()
for (profile in selectionProfiles)
{
  df <- parse.into.df(profile)
  selectionDf <- rbind(selectionDf, df)
}

but that what I get when I reach the last step:

Error: $ operator is invalid for atomic vectors

Could you please help me

1

There are 1 best solutions below

2
On

Your problem comes from line 22:

df$profile_id <- profile$`_id`

should probably be:

df$profile_id <- profile$_id

Perhaps this fix won't work because I dont really know what the profiles object contains.

But your issue stemmed from trying to use $ to call an atomic vector ( vector with length = 1) which was the "_id" reference.