-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_hydra.R
70 lines (59 loc) · 2.75 KB
/
get_hydra.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
get_hydra <- function(){
#send hydra data to groundfish MSE?
#for now load in data I already have for testing
load(here("hydraDataList_msk.rda"))
#rearrange to look like data that we need
species <- data.frame(name=hydraDataList_msk$speciesList, species= c(1:10))
K <-as.numeric(hydraDataList_msk$growthK)
Linf <- as.numeric(hydraDataList_msk$growthLinf)
t0 <- 0 # is there something else I should be using here?
vonbert <- data.frame(species,K, Linf,t0)
# size bins
binwidth <- hydraDataList_msk$binwidth%>%
cbind(species)
sizebin0 <- 0
sizebin1 <- hydraDataList_msk$binwidth$sizebin1
sizebin2 <- sizebin1 + hydraDataList_msk$binwidth$sizebin2
sizebin3 <- sizebin2 + hydraDataList_msk$binwidth$sizebin3
sizebin4 <- sizebin3 + hydraDataList_msk$binwidth$sizebin4
sizebin5 <- sizebin4 + hydraDataList_msk$binwidth$sizebin5
sizebin <- as.data.frame(cbind(sizebin0,sizebin1, sizebin2, sizebin3, sizebin4, sizebin5, species))%>%
full_join(vonbert)
sizebins <- as.data.frame(cbind(sizebin0,sizebin1, sizebin2, sizebin3, sizebin4, sizebin5, species))%>%
gather(bin,endbin, sizebin0:sizebin5)%>%
group_by(species)%>%
mutate(startbin= lag(endbin))%>%
filter(bin!="sizebin0")
# calculate the number of fish in each bin- Problem this is not an integer
observedSurvSize<- hydraDataList_msk$observedSurvSize
SurvData <- gather(observedSurvSize, bin, prop, sizebin1:sizebin5)%>%
mutate(N= ceiling(inpN*prop))%>% #fix by rounding?
full_join(sizebins, by=c("species", "bin"))%>%
full_join(vonbert)
observedCatchSize <- hydraDataList_msk$observedCatchSize
catchData <- gather(observedCatchSize, bin, prop, sizebin1:sizebin5)%>%
mutate(N= ceiling(inpN*prop))%>% #fix by rounding?
full_join(sizebins, by=c("species", "bin"))%>%
full_join(vonbert)
Survnew_end<-SurvData%>%
rowwise()%>%
mutate(end = min(endbin, Linf)) #fix by choosing whichever is smallest?
Catchnew_end <- catchData%>%
rowwise()%>%
mutate(end = min(endbin, Linf))
#repeat rows based on N- each individual has its own row
SurvRep <- as.data.frame(lapply(Survnew_end, rep, Survnew_end$N))
CatchRep <- as.data.frame(lapply(Catchnew_end, rep, Catchnew_end$N))
# some start bins are larger than Linf which will cause errors for calculating length
SurvCheck <- SurvRep%>%
filter(startbin>end)
CatchCheck <- CatchRep%>%
filter(startbin>end)
Survey <- anti_join(SurvRep,SurvCheck) # leave out problem lengths
Catch <- anti_join(CatchRep,CatchCheck)
hydraData <- list(observedSurSize=Survey,
observedCatchSize=Catch,
observedBiomass=hydraDataList_msk$observedBiomass,
observedCatch=hydraDataList_msk$observedCatch)
return(hydraData)
}