## load libraries
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr)
library(here)
## here() starts at /home/sgclawson/github/ohiprep_v2021
setwd(here::here("globalprep","fp","v2021"))
## Load FAO-specific user-defined functions
source('http://ohi-science.org/ohiprep_v2021/workflow/R/common.R')
## This file makes it easier to process data for the OHI global assessment
## by creating the following objects:
##
## * dir_M = identifies correct file path to Mazu (internal server) based on your operating system
## * mollCRS = the crs code for the mollweide coordinate reference system we use in the global assessment
## * regions_shape() = function to load global shapefile for land/eez/high seas/antarctica regions
## * ohi_rasters() = function to load two rasters: global eez regions and ocean region
## * region_data() = function to load 2 dataframes describing global regions
## * rgn_syns() = function to load dataframe of region synonyms (used to convert country names to OHI regions)
## * low_pop() = function to load dataframe of regions with low and no human population
## * UNgeorgn = function to load dataframe of UN geopolitical designations used to gapfill missing data
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj
## = prefer_proj): Discarded datum Unknown based on WGS84 ellipsoid in Proj4
## definition
Mariculture production in tonnes.
<- read.csv('../../mar/v2021/output/MAR_FP_data.csv') mar
Fisheries data.
<- read.csv("../../fis/v2021/output/FP_fis_catch.csv") %>%
fis ::select(rgn_id, year, fis_t = fis_catch) dplyr
<- mar %>%
mar group_by(rgn_id, year) %>%
summarize(mar_t = sum(value, na.rm=TRUE)) %>%
::select(rgn_id, year, mar_t) %>%
dplyrungroup()
## `summarise()` has grouped output by 'rgn_id'. You can override using the `.groups` argument.
# this one is turning to NA in FP
filter(mar, rgn_id ==95) # ok, this makes sense
## # A tibble: 16 × 3
## rgn_id year mar_t
## <int> <int> <dbl>
## 1 95 2004 21
## 2 95 2005 2
## 3 95 2006 2
## 4 95 2007 2
## 5 95 2008 0
## 6 95 2009 0
## 7 95 2010 0
## 8 95 2011 0
## 9 95 2012 0
## 10 95 2013 0
## 11 95 2014 0
## 12 95 2015 0
## 13 95 2016 0
## 14 95 2017 0
## 15 95 2018 0
## 16 95 2019 0
<- fis %>%
fis mutate(fis_t = ifelse(fis_t==0, NA, fis_t)) %>% # 11 NA values is correct
group_by(rgn_id) %>%
arrange(year) %>%
fill(fis_t) %>%
ungroup()
Adjust years so they are equivalent.
<- max(mar$year) - max(fis$year)
adjust
<- mar %>%
mar mutate(year = year - adjust)
<- full_join(fis, mar, by=c('rgn_id', 'year'), all=TRUE)
tmp
## If NA, turn it into a 0 before weighting
<- tmp %>%
tmp mutate(fis_t = ifelse(is.na(fis_t), 0, fis_t)) %>%
mutate(mar_t = ifelse(is.na(mar_t), 0, mar_t)) %>%
mutate(w_fis = fis_t/(fis_t + mar_t)) %>%
mutate(w_fis = ifelse(mar_t==0 & fis_t == 0, NA, w_fis)) %>%
filter(year >= 2005) %>%
::select(rgn_id, year, w_fis)
dplyr
hist(tmp$w_fis)
Compare to previous year data (a big jump in fish data, so not super compatible, but should be correlated at least)
<- read.csv("../../fp/v2020/output/wildcaught_weight.csv") %>%
compare rename(w_fis_old = w_fis) %>%
left_join(tmp, by=c('rgn_id', 'year'))
plot(compare$w_fis_old, compare$w_fis)
abline(0, 1, col="red")
write.csv(tmp, 'output/wildcaught_weight.csv', row.names=FALSE)
## add gf file (no gapfilling)
<- tmp %>%
tmp_gf mutate(w_fis = 0) %>%
::select(rgn_id, year, gapfilled=w_fis)
dplyr
write.csv(tmp_gf, 'output/wildcaught_weight_gf.csv', row.names=FALSE)