Skip to content

Commit

Permalink
add the extract_sd and get_qc function
Browse files Browse the repository at this point in the history
  • Loading branch information
Flavi1P committed Apr 1, 2021
1 parent cd931fb commit 7a72b70
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 3 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ Imports:
lubridate,
tidyr,
dplyr,
magrittr
magrittr,
stringr,
janitor
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

export(extract_sd)
export(get_date)
export(get_qc)
import(dplyr)
import(magrittr)
import(ncdf4)
import(stringr)
importFrom(lubridate,date)
importFrom(tidyr,pivot_wider)
9 changes: 8 additions & 1 deletion R/extract_sd.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
#' @return a table with your BGC variables, depth, date, lon and lat
#' @import dplyr
#' @importFrom tidyr pivot_wider
#' @importFROM janitor clean_names
#' @import magrittr
#' @export

extract_sd <- function(nc_path, vars){
float <- NULL
nc <- nc_open(nc_path)
float_name <- str_extract(nc_path, '[0-9]{6,}')
long_df <- data.frame('depth' = numeric(), 'variable' = character(), value = numeric())
for(i in vars){
var <- ncvar_get(nc, i)
Expand All @@ -26,6 +29,10 @@ extract_sd <- function(nc_path, vars){
final_df <- long_df %>% pivot_wider(names_from = 'variable', values_from = 'value') %>%
mutate('date' = date,
'lon' = lon,
'lat' = lat)
'lat' = lat,
'float' = float_name)
qc_df <- get_qc(vars, nc_path)
final_df <- left_join(final_df, qc_df, by = 'depth') %>% janitor::clean_names() %>%
dplyr::select(float, date, lon, lat, depth, everything())
return(final_df)
}
36 changes: 36 additions & 0 deletions R/get_qc.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#' Extract QC of variables
#'
#' @description Create a data frame with the QC of selected variable along the depth of the profile
#'
#' @import stringr
#' @importFROM tidyr pivot_wider
#'
#' @param vars is a vector of variable
#' @param nc_path is the path of the ncdf file
#'
#' @return a tbale with QC value of each variable along the depth
#' @export

get_qc <- function(vars, nc_path){
nc <- nc_open(nc_path)
qc_table <- data.frame('vars' = character(),
'qc'= numeric())

for(i in vars){
t <- ncvar_get(nc, paste(i, 'QC', sep = '_'))
if(grepl('/SD[0-9]', nc_path)){
qc_vec <- unlist(str_split(t, pattern = ''))
}
else if(grepl('/BD[0-9]', nc_path)){
qc_vec <- unlist(str_split(t[[3]], pattern = ''))
}
qc_vec <- as.numeric(gsub(' ', NA, qc_vec))
depth <- seq(1:length(qc_vec))
qc_var <- data.frame('vars' = paste(i, '_qc'), 'qc' = qc_vec, 'depth' = depth)
qc_table <- bind_rows(qc_table, qc_var)
}

qc_table <- pivot_wider(qc_table, names_from = 'vars', values_from = 'qc')
nc_close(nc)
return(qc_table)
}
2 changes: 2 additions & 0 deletions devtools_history.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ usethis::use_package("ncdf4")
usethis::use_package('tidyr')
usethis::use_package('dplyr')
usethis::use_package('magrittr')
usethis::use_package('stringr')
usethis::use_package('janitor')

19 changes: 19 additions & 0 deletions man/get_qc.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 42 additions & 1 deletion vignettes/how_to_use_tidybgc.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ library(ncdf4)
library(dplyr)
library(tidyr)
library(magrittr)
library(stringr)
```

# Extract the date from the profile
Expand Down Expand Up @@ -54,11 +55,45 @@ get_date <- function(nc){
date <- get_date(nc)
```


#Get QC from SD or BD

```{r}
get_qc <- function(vars, nc_path){
nc <- nc_open(nc_path)
qc_table <- data.frame('vars' = character(),
'qc'= numeric())
for(i in vars){
t <- ncvar_get(nc, paste(i, 'QC', sep = '_'))
if(grepl('/SD[0-9]', nc_path)){
qc_vec <- unlist(str_split(t, pattern = ''))
}
else if(grepl('/BD[0-9]', nc_path)){
qc_vec <- unlist(str_split(t[[3]], pattern = ''))
}
qc_vec <- as.numeric(gsub(' ', NA, qc_vec))
depth <- seq(1:length(qc_vec))
qc_var <- data.frame('vars' = paste(i, '_qc'), 'qc' = qc_vec, 'depth' = depth)
qc_table <- bind_rows(qc_table, qc_var)
}
qc_table <- pivot_wider(qc_table, names_from = 'vars', values_from = 'qc')
nc_close(nc)
return(qc_table)
}
my_qc_table <- get_qc(c('CHLA', 'CHLA_ADJUSTED'), nc_path)
```

#Extract data from SD profiles

```{r}
extract_sd <- function(nc_path, vars){
nc <- nc_open(nc_path)
float_name <- str_extract(nc_path, '[0-9]{6,}')
long_df <- data.frame('depth' = numeric(), 'variable' = character(), value = numeric())
for(i in vars){
var <- ncvar_get(nc, i)
Expand All @@ -72,7 +107,11 @@ extract_sd <- function(nc_path, vars){
final_df <- long_df %>% pivot_wider(names_from = 'variable', values_from = 'value') %>%
mutate('date' = date,
'lon' = lon,
'lat' = lat)
'lat' = lat,
'float' = float_name)
qc_df <- get_qc(vars, nc_path)
final_df <- left_join(final_df, qc_df, by = 'depth') %>% janitor::clean_names() %>%
dplyr::select(float, date, lon, lat, depth, everything())
return(final_df)
}
Expand All @@ -81,3 +120,5 @@ head(my_clean_table)
```




0 comments on commit 7a72b70

Please sign in to comment.