# NHANES survey cycles, their file names, and their weights.
#
# A cycle is named by its years ("2005-2006"). Files are named as CDC names them: the component
# and the cycle's letter ("DEMO_D"), no letter for 1999-2000 ("DEMO"), a "P_" prefix for the
# 2017-March 2020 pre-pandemic files ("P_DEMO"), and "_L" for August 2021-August 2023. Some
# components had other names in early cycles; FILE_NAMES lists those.

CYCLES <- data.frame(
  cycle = c("1999-2000", "2001-2002", "2003-2004", "2005-2006", "2007-2008", "2009-2010",
            "2011-2012", "2013-2014", "2015-2016", "2017-2018", "2017-2020", "2021-2023", "lexicon"),
  letter = c("", "B", "C", "D", "E", "F", "G", "H", "I", "J", "P", "L", ""),
  # The year CDC's download path uses for the cycle. "lexicon" holds the prescription drug
  # lexicon (RXQ_DRUG), one file for every cycle, which CDC keeps under 1988.
  path_year = c(1999, 2001, 2003, 2005, 2007, 2009, 2011, 2013, 2015, 2017, 2017, 2021, 1988),
  # Years the cycle's weights represent: 3.2 for the 2017-March 2020 files.
  years = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3.2, 2, NA),
  stringsAsFactors = FALSE
)

REPLICATION_CYCLE <- "2021-2023"

# Components whose file in some cycle isn't named <component>_<letter>.
FILE_NAMES <- list(
  # component = c(cycle = file name)
  GHB = c("1999-2000" = "LAB10", "2001-2002" = "L10_B", "2003-2004" = "L10_C"),
  TCHOL = c("1999-2000" = "LAB13", "2001-2002" = "L13_B", "2003-2004" = "L13_C"),
  HDL = c("1999-2000" = "LAB13", "2001-2002" = "L13_B", "2003-2004" = "L13_C"),
  TRIGLY = c("1999-2000" = "LAB13AM", "2001-2002" = "L13AM_B", "2003-2004" = "L13AM_C"),
  GLU = c("1999-2000" = "LAB10AM", "2001-2002" = "L10AM_B", "2003-2004" = "L10AM_C"),
  BIOPRO = c("1999-2000" = "LAB18", "2001-2002" = "L40_B", "2003-2004" = "L40_C"),
  CBC = c("1999-2000" = "LAB25", "2001-2002" = "L25_B", "2003-2004" = "L25_C"),
  CRP = c("1999-2000" = "LAB11", "2001-2002" = "L11_B", "2003-2004" = "L11_C"),
  PBCD = c("1999-2000" = "LAB06", "2001-2002" = "L06_B", "2003-2004" = "L06BMT_C"),
  ALB_CR = c("1999-2000" = "LAB16", "2001-2002" = "L16_B", "2003-2004" = "L16_C"),
  BPXO = c("2017-2018" = "BPXO_J"),
  DR1TOT = c("1999-2000" = "DRXTOT", "2001-2002" = "DRXTOT_B"),
  KIQ_U = c("1999-2000" = "KIQ", "2001-2002" = "KIQ_U_B"),
  COT = c("1999-2000" = "LAB06", "2001-2002" = "L06_B", "2003-2004" = "L06COT_C",
          "2007-2008" = "COTNAL_E", "2009-2010" = "COTNAL_F", "2011-2012" = "COTNAL_G")
)

cycle_info <- function(cycle) {
  info <- CYCLES[CYCLES$cycle == cycle, ]
  if (nrow(info) != 1) stop("Unknown cycle ", cycle)
  info
}

file_name <- function(component, cycle) {
  special <- FILE_NAMES[[component]]
  if (!is.null(special) && cycle %in% names(special)) return(unname(special[cycle]))
  letter <- cycle_info(cycle)$letter
  if (letter == "") component else if (letter == "P") paste0("P_", component) else paste0(component, "_", letter)
}

file_url <- function(name, cycle) {
  sprintf("https://wwwn.cdc.gov/Nchs/Data/Nhanes/Public/%d/DataFiles/%s.xpt", cycle_info(cycle)$path_year, name)
}
