---
title: "Gulf of St. Lawrence Ecosystem Approach package (gslea)"
date: 25 January 2022
output: 
    html_document: 
      toc: true
---
# What is it?

An R package to house Gulf of St Lawrence environment and ecosystem data to promote ecosystem research and analysis in the Gulf of St Lawrence and move us closer to an ecosystem approach to fisheries. It should be readily understandable to a large swath of the research community who have extensive or minimal R skills.

Currently version 0.1 - beta. It will not be fixed until version 1 afterwhich updates should not break existing analyses. That could still happen now with updates.


# Quick start

Open R and install the gslea package and try some commands outlined in ?gslea:

```{r quickstart, eval=FALSE}
install.packages("devtools")
devtools::install_github("duplisea/gslea")
library(gslea)
?gslea
```

If it installed, it is recommended that you print out, or have handy on your computer, gslea.cheat.sheet.pdf. This one-pager provides examples of all the basic commands that you can modify for your needs. The greatest asset of this is to have the Gulf of St. Lawrence map available so you can quickly see the numbers for each ecosystem approach region.

The cheat sheet is available on the github repository and it is on your computer hard disc if you installed the package from github.

## Installation troubleshooting

IF you are having trouble installing gslea, firstly, you should try to update R and Rtools to the latest version. Unfortunately on DFO windows computers, the R version and the Rtools version are likely to be outdated and if you do not have administrator privileges, you are stuck with that.

Rtools (you do not need Rtools for Linux or Mac):
https://cran.r-project.org/bin/windows/Rtools/

So if you do not have administrator privileges the error I suspect you will get is:

"Error: (converted from warning) package 'data.table' was built under R version 4.0.3"

The solution to this is to make sure you have devtools installed (above) and then turnoff the convert warnings to error problem and then try to reinstall.

```{r troubleshoot, eval=FALSE}
library(devtools)
Sys.setenv("R_REMOTES_NO_ERRORS_FROM_WARNINGS"=TRUE)
devtools::install_github("duplisea/gslea")
library(gslea)
```

Hopefully, this will allow you to install gslea even though the default windows binary for data.table was built under a newer version of R than available on the DFO software repository.


# Purpose
This describes the building of, the structure of and the use of an R package that gathers up physical, chemical, planktonic, plankton phenological and fish survey data into one place. This is a standalone R package that can be called from scripts or other packages for use. The data are provided spatially by the GSL ecosystem approach regions (EAR) determined in Quebec Region in Spring 2019 (Fig. 1). Some data indices that cover areas larger than EARs are provided.

The package has been developed to allow for easy and consistent updating via automated scripts from tables provides by several individuals. This means that people should not have to keep pestering say Peter or Marjolaine to fullfill specific data requests for them. The package has a very simple data table structure with a minimal set of functions to understand the structure, query data and plot data roughly for initial data exploration. Data can then be brought into various analyses for the GSL that may fall under the banner of an ecosystem approach.

The primary end-user for this work has been envisioned as DFO regional biologists who are involved in stock assessment and want to begin doing analyses that incorporate data outside measures of biology of their specific stock in an effort to expand their analysis to something that may be considered an ecosystem approach to fisheries. We also, however, anticipate that this matrix will have a much wider appeal for researchers in DFO and elsewhere and it should also serve data dissemination and open data initiatives in the Government of Canada.

# Data coverage
Presently, this package consists of data for the Gulf of St Lawrence where collection and management of the data is done out of the Quebec Region. This means that physical, chemical and phenological data generally cover the entire Gulf of St Lawrence but fish survey data (not in the database yet) cover only the northern portion as the southern portion of the Gulf is surveyed by the Gulf Region in Moncton and with a different survey gear.

There are also some broad climatic, oceangraphic and atmospheric indices in the database (coded with EAR=-1) such as the North Atlantic Osciallation. We have presently reserved EAR=0 for GSL scale indices even though there are none in the database yet.

![](README_files/figure-markdown_strict/gslmap.plain-1.png){width=100%}

# Design and development philosophy
The package is GPL-3 licenced and thus is available globally without warranty. The package is designed to have as few data containers as possible and in a common and consistent format to allow generic extraction. The package has only one dependence which is the library data.table and data.table itself has no dependencies. The data.table library is used because of its efficient use of computing resources making it very fast for processing data (https://h2oai.github.io/db-benchmark/) which is important if in someone’s analysis they make repeated queries to the data in loops or in bootstrapping directly from the full database. The data are structured in what has become termed “tidy data” for people in the tidyverse as opposed to messy data I suppose. You can use your own tidyverse code on it. The data class "data.table" inherits a secondary class of data.frame, therefore they are compatible with most of the base R data.frame operations. The package is designed such that it is consistent, should be scalable to when new data types become available and should not break existing analyses when updated (I hope).

## List of development goals and guidelines
* The package needs to be technically accessible to as wide a swath of the envisioned end user community as possible (see Purpose section for an explanation of who this is).
* It must not require permissions to access and use the data and using it should be possible within minutes
* It must be fast to access and have minimal dependencies
* It must be operating system agnostic
* It should easily integrate into people's work flow and analysis
* Data updates or functionality updates should not break existing analyses
* It should have only minimal data exploration functionality
* It must conform to Transparent, Traceable and Transferable (TTT) ideas (Edwards et al. 2018)
* It must create a clear flow from data supplier to user and make it easy to acknowledge and contact data suppliers
* It must be relatively easily updatable, updated often and not go data-stale
* It is a secondary data product and is not a primary relational database, i.e. it should not contain data that is not available or derivable from other existing databases. This also means that quality control in gslea is not on the data itself but only specific derived products.

These guidelines should be followed closely to prevent "mission creep" which is likely to lead to failure of the usability of matrix at a later point.

# Components of gslea

## Data objects
The package consists of three main tables presently:

<ins>EA.data</ins> This is where all the measurements reside. The data.table (inherits data.frame as second choice) has four columns: <b>year</b>, <b>EAR</b>, <b>variable</b>, <b>value</b>. Where year is the year (integer) of data collection, <b>EAR</b> is the ecosystem approach region (see fig 1) (character), <b>variable</b> is the name of the variable (character), <b>value</b> is the measured values (numeric). <b>variable</b> is set as the key variable

<ins>variable.descriptions</ins> this provides a description of the variable in EA.data. This table contains five columns: <b>variable</b> is the name of the variable (character), <b>description</b> is a description of the variable and what is represents, <b>units</b> are the units of measure of the variable, <b>contact</b> is the name of the contact person who provided the data, <b>type</b> is the type of data (“physical”, “chemical”, “planktonic”, “phenological”, “fish”), <b>extraction.date</b> is the date which the contact person extracted the data from their database. <b>variable</b> is the key variable.
Some of the variables are not just single measures per year but monthly measures. It was a conscious decision not to make a sub-year time column in these cases which makes the extraction result more difficult since often people want data in two-dimensional tabular format. So for example some of the plankton data are available by month. In these cases, there is a separate variable for each month and if it were for September it would end in ...month9.

<ins>field.descriptions</ins> this gives a description of the field names in the EA.data especially as these might need elaboration in some cases. The table contains three columns: <b>field</b> which is the field name in the EA.data table, <b>description</b> which describes what is represented by that column, <b>elaboration</b> which provides more details on the column when needed. So the elaboration column for <b>EAR</b> describes the areas represented by each ecoregion code. Elaboration for variable describes specifically what is meant by a variable containing a name that may include “early summer”. <b>field</b> is the key variable.

Another data table describes the coordinates of the EAR boundaries in decimal degrees but you never see that here.

## Functions

The package consists of limited number of functions:

<ins>metadata.f(verbosity)</ins> a description of the data available with three levels of <b>verbosity</b> ("low" "med", "high") or EASTER EGG information on everyone's favourite Dutch post-impressionist: metadata.f("vangogh").

<ins>vars.f(variable.type)</ins> shows the variables available in a particular <b>variable.type</b> e.g. "physical", "chemical" gives a description of each and its units.

<ins>find.vars.f(search.term)</ins> finds variable names based on partial matches. It search not just the variable names but also their descriptions, sources and references.

<ins>EA.query.f(variables, years, EARs) </ins> the function you use to query the data and the output is in long data format. <b>variables</b> (e.g. "t150","sst") is a character vector, <b>years</b> is a numeric vector (e.g. 2002:2012), <b>EARs</b> is the ecoregion and is a numeric vector (e.g. 1:3).

<ins>EA.plot.f(variables, years, EARs, ...)</ins> this will plot the variables over time. It will make a matrix of variable x EAR with up to 25 plots per page (i.e. 25 variable*EAR combinations). <b>variables</b> (e.g. "t150") is a character vector, <b>years</b> is a numeric vector (e.g. 2002:2012), <b>EARs</b> is the ecoregion and is a numeric vector (e.g. 1:3), <b>smoothing</b> is a logical on whether the smooth.spline should be run through the data series to help give a general idea of the tendencies in time. It will only try to smooth if the data has more than 5 observations. <b>....</b> will accept parameters to par for plotting. This is mostly for quick exploration of the data rather than for making good quality graphics.

<ins>EA.cor.f(variables, years, EARs, ...)</ins> commputes the cross corelation between two variables with lags. It has the option of differencing the variables to make them stationary and therefore correlate on the how the values of each of the variables changes from year to year as opposed to their absolute values. ... gives arguments to the <b>ccf</b> function.

<ins>sources.f(variable.name)</ins> gives a source and reference for any variable in the database. If NULL then it returns the full list of sources and references. Please cite these references if using the data.

# Installing gslea

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r install, eval=FALSE}
devtools::install_github("duplisea/gslea", build_vignettes = TRUE)
library(gslea)
```

```{r ldlib, eval=TRUE, include=FALSE}
library(gslea)
```

# Accessing the data
## Data content overviews
A few minimal extraction functions are provided that should be fast and relatively generic. A function called <b>metadata.f</b> is provided with three levels of verbosity to give you an overview. “low” verbosity just gives a few stats on the size of the database and the number of variables and EARs. “med” verbosity will give you names of variables and units. “high” is not that useful because it pretty well outputs the entire content of the variable.description table.

```{r metadata, eval=TRUE}
metadata.f(verbosity="low")
```

Another perhaps more useful way to know what the database contains is with the function <b>var.f</b>. <b>var.f</b> accepts as an argument one of the data types with the default being “all”. The options are the adjectives for a data type, e.g. "physical", "chemical", "planktonic" which for some data types seems awkward but it is consistent. It will give you the exact name of the variable, its description and units. The output can be long and the descriptions are sometimes quite wordy so it is difficult to read. I suggest you save the result of a large query to var.f as an object and then use the library formattable to make it into a more readable table. So for example formattable, e.g.:

```{r variables, eval=TRUE}
phys.var= vars.f(variable.type="physical")
formattable::formattable(phys.var)
```

You can also try to find a variable through partial matching of a term (case insensitive). So for example if you were interested in just temperature you might search "temp". Or anything that is from 200m deep then search "200". It will then give you a list of variable that have that term in their description.

```{r vargrep, eval=TRUE}
find.vars.f(search.term= "200")
```

You will see that t.deep and t.shallow come up in this because in their descriptions, the distinction between shallow and deep waters is 200m. You will also see AMO variable coming up and this is because the reference for the AMO was published in 2001 and 200 is a substring of that. So you can see it will find things fairly broadly

This search function will search most of the main fields of the variable.description table. So for example you may be interested in products which Peter Galbraith was involved with so you could try find.vars.f(search.term= "galbra") or say something to do with plankton blooms find.vars.f("bloom").

## Data extraction
Extracting the data is done with a single function called <b>EA.query.f</b>. This query wants a character vector or scalar for variable, an integer vector or scalar for year and an integer vector or scalar for EAR:

```{r extraction1, eval=TRUE}
EA.query.f(years=1999:2012, variables=c("t150", "ph_bot.fall", "t250"), EARs=1:2)
```

You need to name all the variables you want to extract but you can access all the years or all the EARs by putting a wide range on them

```{r extraction2, eval=TRUE}
EA.query.f(years=1900:2021, variables=c("t150", "ph_bot.fall", "t250"), EARs=1:99)
```

You may want to save the results of a query to an object and then export it to csv (<b>fwrite</b>) or some other format.

### Recasting data and showing when there were no observations
The data are in long format (tidyverse speak = "tidy data") which is the common way to store data in databases. It means that for a variable x year x EAR comination where there is no observation, there is not a row in the database either. If you want tabular data (wide) to show when say and observation was not made for a particular year and variable and EAR, then you can widen the data using the "dcast" function from data.table

```{r recasting, eval=TRUE}
dat= EA.query.f(years=1900:2021, variables=c("t150","ph_bot.fall","ice.max","o2.late_summer.sat.mean50_100"), EARs=1)
dcast(dat, year~ variable)
```

This puts each variable as a separate column, it preserves all the years where at least one of the variables had an observation and it puts NA for variable x year combinations where there was no observation.

It is important to know that when you do this as above, you are making 2-dimensional table data which is fine if your data are two dimensional. If you have more than one EAR, then your initial data are 3-dimensional and when you cast the data to 2-dimensions, a decision needs to made on how to reduce it to 2-dimensions. This is done with a "group by" function. By default, dcast will do a group-by as count but you can also specify other groub-by functions such as sum or mean. You can also, however, cast multidimension data into a table but it will repeat the columns for each EAR (note that "EAR" is now in the right hand side of the formula)

```{r recasting2, eval=TRUE}
dat= EA.query.f(years=2015:2021, variables=c("t150","ph_bot.fall","ice.max","o2.late_summer.sat.mean50_100"), EARs=1:100)
dcast(dat, year~ variable+EAR)
```
This wide data now has as many rows as years and as many columns as variable x EAR. The columns are named with the variable followed by "_EAR" to identify the EAR it represents.

## Data plotting
The data plotting function <b>EA.plot.f</b> just queries the EA.data with <b>EA.query.f</b> and then plots them. It puts all the plots on one page as a matrix of plots with each row being a variable and each column being an EAR:

```{r plotting1, eval=TRUE}
EA.plot.f(years=1900:2021, variables=c("t150", "ph_bot.fall", "t250"), EARs=1:4, smoothing=T)
```

It will plot a maximum of 25 plots per page. What you might want to do is call pdf("EA.plots.pdf") xxx dev.off() when doing this and it will put them all in one pdf in your working directory.

Another example of the plot without smoothing and different graphical parameters:

```{r plotting2, eval=TRUE}
EA.plot.f(years=1900:2021, variables=c("t150", "ph_bot.fall", "t250"), EARs=1:4, smoothing=F, pch=20, lwd=2, col="blue", type="b")
```

You can see that if there are no data for the variable by EAR combination, a blank plot is produced in the plot matrix.

You may want to plot all variables of a particular type. You can do this by selecting the variables with the vars.f function and selecting just the <b>variable</b> column from its output using "$"
```{r plotting3, eval=TRUE}
EA.plot.f(years=1900:2021, variables=vars.f(variable.type="chemical")$variable, EARs=1:2, smoothing=T)
```

## Finding variables and data
You might be interested in anything to do with large scale oscillation indices, e.g. North Atlantic Oscillation. These all have an EAR=-1 indicating that the they are measures at scales larger than the EA regions. You can do search for them with a key word or partial string and then with that information select the NAO monthly data.

```{r plotting4, eval=TRUE}
find.vars.f("oscilla")
# ah ha, seems that something like "NAO.mon" will do it for us but you don't need to worry about the case
NAO.vars= find.vars.f("nao.mon")
#but because variable names are character and you may want them ordered by month you need to sort the names vector
NAO.vars= NAO.vars[order(nchar(NAO.vars), NAO.vars)]
EA.plot.f(years=1800:2021, variables=NAO.vars[1:5], EARs=-1, smoothing=T,pch=20)
```

## Discovering relationship between variables in the database
If you have a hunch that one variable may be driving another, you can do a fairly simple analysis to at least give you a first crack at testing your hypothesis by using cross correlation (ccf). ccf is a base R function that looks at the correlation between two variables at different time lags. It has been repackaged here to query the data from the EA.data table with the function <b>EA.cor.f</b>.

So let's assume for this example that you think that sea surface temperature in the central Gulf (EAR 3) is related to the North Atlantic Oscillation at some earlier time (<b>climatic</b> variables always have EAR=-1) but you are not sure what time lag might be most appropriate. Here you are assuming NAO is the independent variable and, SST is the dependent variable

```{r crosscor1, eval=TRUE}
EA.plot.f(variables=c("h.nao","sst"), years=1900:2021, EARs=c(-1,3), smoothing=T,pch=20)
```

It is hard to say from just pltotting the data because the length of the time series are quite different. The cross correlation testing at various temporal lags will probably help you formulate your hypotheses better.

```{r crosscor2, eval=TRUE}
EA.cor.f(x="h.nao", y="sst", years=1900:2021, x.EAR=-1, y.EAR=3)
```

It is a bit of a downer because your best correlations is between NAO and SST in the same year (0 lag) and the relationship is not that strong (about -0.3) and not significant. 

To imply the causality you are looking for in such an analysis (because you specified x as the independent variable and y as the dependent), you are looking for negative or 0 lags. Positive lags suggest that the y variable is leading the x. These are all just correlations, only your hypothesis implies causality.

Let's try an easy one by choosing two variable you know must be related: SST in EAR 3 (central Gulf) and SST in EAR 1 (NW Gulf).

```{r crosscor3, eval=TRUE}
EA.cor.f(x="sst",y="sst", years=1900:2021, x.EAR=1, y.EAR=3)
```

Yes indeed, they are very tightly positively correlated.

An important thing to note with cross correlation is that it will give the same result as "cor" only with lag 0. That is, if you try truncating series yourself and then run "cor" between the two series, you will not get the same result as ccf. This has to do with the normalisation of the data at the beginning before lagging in ccf. See the help for EA.cor.f for more details.


## Using down-scaled atmospheric climate projections to predict oceanographic variables

The database contains atmospheric projections from 24 different global climate models that have been down-scaled to boxes roughly in the same area as the EARs. The ensemble medians and confidence intervals of selected variables are provided and for some variables the distributional characteristics over the ensemble are also provided. Ideally, we want and will include the direct oceanographic variable projections under different carbon emission scenarios but this is detailed work that is currently underway at IML. The atmospheric variables are provided here in the meantime (and they will remain) having been downloaded from www.climateatlas.ca (this is an excellent site, please check it out).

So as an example of what could be done with this, the annual mean surface temperature for an EAR has been correlated against the deep water temperature. The linear model resulting from this is not too bad and could potential inform a semi-trustable projection (or at least better than guessing). Follow this code as an example of what could be done.

```{r climproject, eval=TRUE}
  EA.cor.f("ann.mean.t.med.rcp45","t.deep",1950:2021,1,1)

# lets look from 2009 when the deep water really started warming up, it is a pretty good predictor
  EA.cor.f("ann.mean.t.med.rcp45","t.deep",2009:2021,1,1)

# fit a linear model and project that model based on the ensemble median prediction until 2095
  tmp= EA.query.f(c("ann.mean.t.med.rcp45","t.deep"),1950:2100,1)
  tmp2= dcast(tmp, year~variable)
  plot(tmp2$ann.mean.t.med.rcp45,tmp2$t.deep)
  rug(tmp2$ann.mean.t.med.rcp45)
  pred.lm= lm(t.deep~ann.mean.t.med.rcp45,data=tmp2)
  summary(pred.lm)
  tmp2$t.deep.pred= predict(pred.lm,newdata=tmp2)
  plot(tmp2$ann.mean.t.med.rcp45,tmp2$t.deep.pred,type="l",col="blue",lwd=3,
       xlab="Annual mean surface temperature down-scaled to EAR 1", ylab= "Bottom temperature of deep (>200 m) waters EAR 1")
  points(tmp2$ann.mean.t.med.rcp45,tmp2$t.deep,pch=20)
  rug(tmp2$ann.mean.t.med.rcp45)
  title(main="RCP 4.5 climate projection until 2095, ensemble median")
```

If one thinks it is valid to link the atmospheric variable so closely with deep water temperature at such scales 70+ years into the future then it can be a basis for extrapolation. As above, perhaps it is better than guessing but one needs to put a bit of water in their wine for the interpretation.

# Source and references for data
It is important to acknowledge to the individuals and organisation who collected the data and or processed it to come up with the indices that are presented here. In some cases, this downstream acknowledgement may be the primary means of showing efficacy of their work so please be diligent about including citations and acknowledgements in your work.

The function sources.f accept a variable name as an argument. It will give you the name and or link to the person or organisation responsible for the data represented by that variable. It will also provide the main citation for that variable.

```{r sources, eval=TRUE}
formattable::formattable(sources.f(c("t.200","h.nao","o2.fall.doxy2.bottom")))
```

If you just type source.f() you will get the person/organisation responsible and main reference for all variables in the database.

# Forget the R-package, I just want the data
You are not obliged to use this R-package if you want the data. The data table and variable description table have been merged and written to an excel file sheet. The field descriptions have been written to another sheet in the same excel file. This will be downloaded as part of the R package from github but you can access just that file directly from the gslea github root directory if you do not want to download the R package. It is call "<b>EAdata.dump.xlsx</b>". You might just download it and filter the variable column or other columns to choose the data you want from excel. This file is automatically updated everytime the gslea library is updated so there should be no discrepency in the data from the two places.

Please do not forget to acknowledge the sources of the data and cite the appropriate references that are included in the excel file.

# Work using gslea and links to associated code

## Where gslea has been used in research and advice
* Conditioning advice for Gulf of St. Lawrence shrimp based on an assessment of environment and palusible future climate scenarios. January 2020.
* Risk of climate change impacts on the Gulf of St. Lawrence turbot fishery. February 2020.
* 4R herring assessment. November 2020

## Code and packages that have drawn up gslea
### [Andrew Smith](https://github.com/adsmithca) created a script as a [github gist](https://gist.github.com/adsmithca/8c00a360292e127cfaef4564df0a7b1d) that makes some nice maps drawing on the data in the package using several of the tidyverse libraries.

### Climate stripes
You can use a package by the same author to make climate stripes plots from data in this package. They have intentionally been kept separate in order to keep gslea as minimal and streamlined as possible. Here we use SST from the central GSL, and then a longer series using the down-scaled atmospheric climate projections for RCP 8.5 surface temperature

```{r climatestripes, eval=TRUE}
# to install the climate stripes package
# devtools::install_github("duplisea/climatestripes")
library(climatestripes)
tempdata= EA.query.f(var="ann.mean.t.med.rcp85", EAR=3, years=1900:2100)
title.name= "RCP 8.5 surface temperature Magdalen Islands"
climate.col.stripes.f(time.vector=tempdata$year,temperature.vector=tempdata$value, colour.vec=c("navyblue","lightblue","white","red","darkred"),title=title.name,legend=T)
superimpose.data.f(time.vector=tempdata$year,temperature.vector=tempdata$value, data.colour="yellow", spline=T, spline.colour="white",lwd=4)
```

# A shiny implementation

You can run this as a shiny application on your webbrowser. You will need to install the package shiny from CRAN and then use the command

```{r shinyapp, eval=FALSE}
if (!require('shiny')) install.packages('shiny'); library('shiny')
if (!require('shinybusy')) install.packages('shinybusy')
if (!require('devtools')) install.packages('devtools')
if (!require('gslea')) devtools::install_github('duplisea/gslea')
runGitHub(repo="gslea", username="duplisea", subdir="shiny")
```

# Development plan and data inclusion
Presently, the development is occuring in Quebec Region but this will be expanded to include data that are stored and processed by researchers in the Gulf Region (Moncton) of DFO. Our goal is to get a good example from Quebec Region and then approach Gulf Region with specific examples that they could follow. A joint meeting in the Spring of 2020 was the first step in this cross region data sharing in the matrix.

## Multispecies fish and invertebrate survey data
We have added a preliminary extraction of Quebec Region multispecies fish and invertebrate biomass data but these will require more quality control and checking before we will consider them verified. We have applied species filtering criteria and identified 19 core species that are well represented by the survey and biomass by ecosystem approach region and year has been extracted for each of these. We have further identified 9 commercial species that are in a subset of the core species and we have further extracted their biomass in juvenile and adult categories. Finally, we have taken all species caught in the survey and lumped them into various functional guilds and extracted biomass by guild.

## Fishing pressure indicators
We have begun working on developing indicators of fishing pressure in each region which will have multiple measures such as biomass extracted by fishing as well as effort of various gear types in each of the regions for as many years as possible. There are many issues with some of these data such as improper location assignment. We are presently working on this to try to develop useful pressure indicators related to fishery removals and effort.

## Down-scaled oceanographic projections
Down-scaled oceanographic variable (physical, chemical, biological) projections under different ICCP RCP scenarios and ensembles means and variances will also be provided in the matrix eventually. We have been in discussion with our regional oceanographers and are developing a plan to include this information in a future update of the matrix.

## Other data
There have been considerations of including coastal data, stock assessment results, fine-scaled information. These may be possible to include in the current structure of gslea and we have also considered that this kind of information might be better suited to another similar kind of library but specifically aimed at this information. We need to keep a consistent approach to the development philoshophy and goals which can help us decide this.

# Updating the package
<mark>Unless you need to update this database, you do not need to read this</mark>

It is important that the database can be updated consistently and quickly. This is done through a series of system calls to bash while running R in linux using text processing programs like awk and sed and then manipulation in R.

To update the package you will need the standard packages for doing it like roxygen and devtools.

## Computing requirements for updating

This package requires linux to update. The reason linux is needed is because it uses BASH system calls and programs like awk and sed to pre-process data to make names consisitent, e.g. “Year” to “year” or other inconsistencies between how raw data is provided by different people. If data gets provided by people differently between updates then this will require updating of these scripts.

Once the data are processed and brought into the R package, then it should be useable by any platform that runs R but I would not know how to process the raw data in windows. You may be able to do this in windows10 powershell but I have never tried it so I cannot say it will work. I do note that powershell does not have "sed" installed by default and you cannot run R from powershell so I am not sure you could send R systemcalls to the powershell and if you can the script will fail without "sed".

## Raw data
Raw data has been provided in various forms by individual data providers. Sometimes it is in tabular format while other times it is in a long format. We need to turn it all into long format and this also involves standardising variable names.

## Running the update script
The update script is XXXX (I will make a vignette on this including the data but I have not done this yet) which is run from R. It makes system calls to the working directory you set. That working directory can be anywhere on your machine and you need to make sure there are sub-directories of that which are named by the data provider. So Peter Galbraith has supplied the physical oceanographic data and therefore the subdirectory is called galbraith. His raw .dat files are located there. These are text files of a sort that Peter extracts with commented (#) header lines describing the data and finishing with the data itself. Marjolaine Blais has supplied the chemical, planktonic and phenological variables is various forms. The subdirectory blais also has subdirectories for zooplankton, oxygen, pH etc.
Aside from the data itself, the two other tables need to be imported into R. These sheets in an excel file describing the data. At first I was pulling this information from the headers but there were a lot of differences and this was creating very one-off fragile scripts that I knew would likely break on each update. Therefore, the excel sheets have been created to keep this information. You will need to edit them in excel. The good thing is that all you will have to alter for a simple update is the extraction date.  If you add new variables though, you will need to add a new line with all the information about that variable.

If this is all in order on your machine, you just need to run the update script in the R command line. The script will manipulate the data and save each data.table as an .rda file in your data directory for the R package.

I doubt it will go this smoothly but I hope so.

## Updating the R package

### Compiling documentation
If you changed the documentation for the datasets or functions, you need to recompile the documentation using roxygen2.

### Making the R package
Clean and rebuild

# Project participants (past and present)

Jérôme Beaulieu, Hugues Benoît, Marjolaine Blais, Hugo Bourdages, Daniel Duplisea, Peter Galbraith, Mike Hammill, Cédric Juillet, David Merette, Stéphane Plourde, Marie-Julie Roux, Bernard Sainte-Marie, Antoine Rivierre, Virginie Roy

# If you have issues
For comments, questions, bugs etc, you can send this to the package maintainer, Daniel Duplisea, by email (daniel.duplisea@gmail.com, daniel.duplisea@dfo-mpo.gc.ca) or file a bug report or issue on github. 

Pull requests conforming to the development philosophy are welcome.

# Citation for this package

Duplisea, DE. Merette, D., Roux, M-J., Benoît, H., Blais, M., Galbraith, P., Plourde, S. 2020. gslea: the Gulf of St Lawrence ecosystem approach data matrix R-package. R package version 0.1 https://github.com/duplisea/gslea.


# References

Edwards, A.M., Duplisea, D.E., Grinnell, M.H., Anderson, S.C., Grandin, C.J., Ricard, D., Keppel, E.A., Anderson, E.D., Baker, K.D., Benoît, H.P., Cleary, J.S., Connors, B.M., Desgagnés, M., English, P.A., Fishman, D.J., Freshwater, C., Hedges, K.J., Holt, C.A., Holt, K.R., Kronlund, A.R., Mariscak, A., Obradovich, S.G., Patten, B.A., Rogers, B., Rooper, C.N., Simpson, M.R., Surette, T.J., Tallman, R.F., Wheeland, L.J., Wor, C., and Zhu, X. 2018. Proceedings of the Technical Expertise in Stock Assessment (TESA) national workshop on ‘Tools for transparent, traceable, and transferable assessments,’ 27–30 November 2018 in Nanaimo, British Columbia. Can. Tech. Rep. Fish. Aquat. Sci. 3290: v + 10 p. https://waves-vagues.dfo-mpo.gc.ca/Library/40750152.pdf
