rm(list=ls())
library(Hmisc)
library(data.table)

setwd('~/research/gTiger/Article_citsci/open_code')
source('settings/parameters.r')
source('settings/paths.r')
source('settings/functions.r')

setwd(path_to_data)

trs = readRDS('user_locations_propensities.Rds')

MA_total_SE = trs[ , list(SE=sum(propensity)), by=list(masked_lon, masked_lat)]


Ovitraps = readRDS('ovitrap_model_dataset.Rds')

Ovitraps_total_SE = Ovitraps[, list(SE=.N), by=list(TigacellID)]

x = strsplit(Ovitraps_total_SE$TigacellID, "_")

set(Ovitraps_total_SE, i=NULL, j="masked_lon", value=unlist(lapply(x, function(this_x) this_x[1])))
set(Ovitraps_total_SE, i=NULL, j="masked_lat", value=unlist(lapply(x, function(this_x) this_x[2])))

write.csv(Ovitraps_total_SE, "ovitraps_total_se.csv")

write.csv(MA_total_SE, "ma_total_se.csv")

spain_gpdf = readRDS('spain_grid.Rds')

spain_cells = spain_gpdf$TigacellID

trs = trs[which(trs$TigacellID %in% spain_cells),]
length(which(spain_cells %in% unique(trs$TigacellID)))

MA_total_SE_spain = trs[ , list(SE=sum(propensity)), by=list(masked_lon, masked_lat)]

write.csv(MA_total_SE_spain, "ma_total_se_spain.csv")

