#Run List
#2022 01 27
#2022 01 31
#Rewritten to inlcude simplified code at each stage
#Ed Lefley

#Run order of R Scripts for comlpete import from XLSX file to all.

#Load all R Packages if running in the Cloud, also change all WD
#ource("Install_R_Packages_Ag7.R")

#1 Source Files - Code unchanged but now includes a clean up for the import files that are surplus
st <- Sys.time()
setwd("C:/Users/Ed/OneDrive/PhD/R/2022_Ag7")
source("Merging_All_XLSX_to_CSV_Ag.R")
#setwd("C:/Users/Ed/OneDrive/PhD/R/2022_Ag7")
#source("2022_01_31_Remove_Import_Ag7.R")
##Output dataframe: WoS_All_Record

#2 Regex Clean - change here any regex errors from later on
setwd("C:/Users/Ed/OneDrive/PhD/R/2022_Ag7")
source("2022_10_28_Regex_Clean_Ag7.R")
##Output dataframe WoS_All and WoS_All_D (joined to WoS_All)

#3 Address Split from Authors
setwd("C:/Users/Ed/OneDrive/PhD/R/2022_Ag7")
source("2022_10_28_Address_Split_Ag7.R")
##Output dataframe WoS_All_Addresses 

#4 Split Addresses by semicolon and comma
setwd("C:/Users/Ed/OneDrive/PhD/R/2022_Ag7")
source("2022_01_31_Vector_Ag7.R")
##Output dataframes: unique: {WoS_AAU, WoS_U_D} all: {WoS_AA, WoS_A_D}, WoS_Address_Vector for Combined Addresses

#5 Rearrange Addresses
setwd("C:/Users/Ed/OneDrive/PhD/R/2022_Ag7")
source("2022_01_31_Rearrange_Addresses_Use_Ag7.R")
##Output dataframe: WoS_A_REV_DD and WoS_U_REV_DD

#Clean out department information and standardise addresses
#Split off reversed country, state, city, postcode and then add back to the 

#6 Reorder & Join the new list to the original
setwd("C:/Users/Ed/OneDrive/PhD/R/2022_Ag7")
source("2022_01_31_Reorder_and_Clean_All_Ag7.R")
#85mins run
##Output dataframes WoS_AA_All & WoS_AA_All_U (Australia only), WoS_Address_All (Global)

#7 Geocode these shorter lists
setwd("C:/Users/Ed/OneDrive/PhD/R/2022_Ag7")
source("2022_01_31_Country_Ag7.R")
##Output dataframe Country_List_C (country only), Country_List_C_NN (No US postcodes), Country_List_C_U (unique countries)
setwd("C:/Users/Ed/OneDrive/PhD/R/2022_Ag7")
source("2022_01_31_CSC_Ag7.R")
##Output dataframe CSC (Country, State, City) unique, all and Australia only (CSC_C_NN_U_A)
setwd("C:/Users/Ed/OneDrive/PhD/R/2022_Ag7")
source("2022_01_31_Combing_Addresses_Ag7.R")

##Output dataframe Geocode_this, Geocoded Addressses, Combined_Addresses

st2 <- Sys.time()
#8 TH Allocation
setwd("C:/Users/Ed/OneDrive/PhD/R/2022_Ag7")
source("2022_10_28_TH_IF_Ag7.R")
##Output dataframe WoS_All_Record_TH_SS and WoS_All_Record
#Allow at least 51hrs
et <- Sys.time()

tt <- (et-st)

#Attach Geocode to the full list using Find and Replace

#Then find and Replace over the original affiliation details
#setwd("C:/Users/Ed/OneDrive/PhD/R/2022_Ag7")
#source("2021_11_02_Merging_addresses_Ag7.R")
#source("2021_11_09_Combing_Addresses_Ag7.R")

#Entropy Calc
setwd("C:/Users/Ed/OneDrive/PhD/R/2022_Ag7")
source("2022_03_25_Entropy_Setup_5_Ag7.R")
aggregate(Ag7_TH_M_TH_SS_5$Set, list(Ag7_TH_M_TH_SS_5$Set), length)

#Percentage
setwd("C:/Users/Ed/OneDrive/PhD/R/2022_Ag7")
source("2022_09_09_Calculation_of_annual_sets.R")

#Geocoding
setwd("C:/Users/Ed/OneDrive/PhD/R/2022_Ag7")
source("2022_10_28_GeoCode_TH.R")


et2 <- Sys.time()

tt2 <- (et2-st)
