-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.R
94 lines (71 loc) · 3.36 KB
/
import.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
## This script imports all Excel STAR templates from a specific stock assessment
## event (stock assessment working group, benchmark workshop, or the like) and
## year.
##
## For example, if event is "WGSASP_General" and year is 2021, then this script
## will look for Excel STAR templates inside
## ~/StockAssessmentResults/uploads/2021/WGSASP_General
## and Excel file with SharePoint properties
## ~/StockAssessmentResults/uploads/2021/properties/WGSASP_General.xlsx
##
## The stock assessment results are then exported to CSV files inside
## /mnt/star-templates/2021/WGSASP_General
##
## The purpose of this script is to semi-automate the STAR import and export
## procedure to generate CSV files for the STAR database. Two steps require a
## human review and possible intervention:
##
## 1. If any errors were raised while reading in Excel STAR templates, these may
## require intervention to manually edit and correct Excel templates,
## possibly by contacting the stock assessor. See cbind(sapply(cluster,
## class)) below, and the qc(directory) functionality can be helpful to
## examine errors.
##
## 2. If any duplicated Assessment_ID are encountered, then two or more Excel
## STAR templates have results for the same stock (year, species, gsa). If
## the results from both Excel STAR templates should be imported into the
## database, then these should be imported using read.template(...,
## suffix="this") and read.template(..., suffix="that") functionality. See
## any(duplicated(id)) below, and the help page for read.template.
event <- "WGSASP_General"
year <- 2021
## Load package and specify directories
library(gfcmSTAR)
uploads <- file.path("~/StockAssessmentResults/uploads", year)
star.dir <- file.path(uploads, event)
prop.file <- file.path(uploads, "properties", paste0(event, ".xlsx"))
## Read SharePoint properties
prop <- read.properties(prop.file)
## Import STAR templates
cluster <- import.many.templates(star.dir, prop=prop)
cluster <- import.many.templates(star.dir, prop=prop, qc=TRUE)
cbind(sapply(cluster, class))
errors <- sapply(cluster, class)
cbind(errors[errors=="try-error"])
qc.vector <- qc(star.dir, quiet=TRUE)
## Exclude STAR templates that have errors
cluster.ok <- cluster[sapply(cluster, class) != "try-error"]
cbind(sapply(cluster.ok, class))
## Check that the Assessment_ID fields are unique
id <- peek(cluster.ok)
cbind(id)
if(any(duplicated(id)))
id[duplicated(id)]
################################################################################
## Actions specific to this script
## Remove star_template, draft version of STAR_PIL_17_18
cluster.ok$"star_template.xlsx" <- NULL
## Remove STAR_PIL_17_18.xlsx, strings prevent calculation of stock status
cluster.ok$"STAR_PIL_17_18.xlsx" <- NULL
## Give two analyses of STAR_2019_ANE_6 distinct names
s1 <- cluster.ok$"star_ANE_GSA06 ref2019_model1.xlsx"
s2 <- cluster.ok$"star_ANE_GSA06 ref2019_model2.xlsx"
diff.stars(s1, s2)
cluster.ok$"star_ANE_GSA06 ref2019_model1.xlsx" <- append.id(s1, "a4a_spict")
cluster.ok$"star_ANE_GSA06 ref2019_model2.xlsx" <- append.id(s2, "a4a")
################################################################################
## Overall summary
report(cluster, cluster.ok, qc.vector)
## Export into subdirectories inside /mnt/star-templates
topdir <- file.path("/mnt/star-templates", year, event)
export.many.csv(cluster.ok, topdir=topdir, force=TRUE)