# downloading external data files

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

options(stringsAsFactors = FALSE)

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

setwd(path_to_external_data)

######################
## GADM Shape Files ##
###################### 
download.file('http://biogeo.ucdavis.edu/data/gadm2.8/rds/ESP_adm2.rds', 'ESP_adm2.rds')

download.file('http://biogeo.ucdavis.edu/data/gadm2.8/rds/ESP_adm2.rds', 'ESP_adm4.rds')

#####################
## INE Shape Files ##
#####################
download.file('http://centrodedescargas.cnig.es/CentroDescargas/equipamiento.do?method=mostrarEquipamiento') # this needs to then be extracted in the same folder from terminal

 
#############################
## Municipal register data ##
#############################
i = 1
for(i in 1:52){
	this_n = i
	if(i<10){
		this_n = paste0(0, i)
	}

	download.file(paste0('http://www.ine.es/jaxi/files/_px/en/csv_sc/t20/e245/p05/a2015/l1/000', this_n, '006.px'), paste0('padron2015_', this_n, '.csv'))
}

i = 1
padron_list = lapply(1:52, function(i){
	print(i)
	flush.console()
	this_n = i
	if(i<10){
		this_n = paste0(0, i)
	}

	this_padron_table = read.csv(paste0('padron2015_', this_n, '.csv'), skip=5, colClasses=c("character", "numeric", rep('NULL', 102)), strip.white=T, sep=";")
	this_padron_table = this_padron_table[1:(which(this_padron_table$X=="Males")-1),]
	this_padron_table = this_padron_table[-which(this_padron_table$X %in% c("Both sexes", "Both genders", "Total", "Both")),]
	this_padron_table$PROVMUN = substring(this_padron_table$X, 1, 5)
	result = data.table(this_padron_table)
	return(result)
})

padron2015_mun = rbindlist(padron_list)

saveRDS(padron2015_mun, file="padron2015_mun.Rds")


###################################
## Active Population Survey Data ##
###################################
# Download ascii data files from microdata tab at http://www.ine.es/dyngs/INEbase/en/operacion.htm?c=Estadistica_C&cid=1254736176918&menu=resultados&secc=1254736030639&idp=1254735976595 for all four quarters of 2014 and first three of 2015. Save in external data directory as EPAT2014Q01.txt, EPAT2014Q02.txt, EPAT2014Q03.txt, EPAT2014Q04.txt, EPAT2015Q01.txt, EPAT2015Q02.txt, EPAT2015Q03.txt