"""
Tested with Python 3.4
This program uses Tsorter by Terrill Dent, licensed under the MIT licence:
http://terrill.ca/sorting/tsorter/LICENSE

This is a very early draft of a program. Use more as food for though rather than
as a production tool. Please excuse the lengthy code. I am only a beginner
programmer :)
"""

import pandas as pd
from bokeh.plotting import figure, output_file, save
from bokeh.embed import file_html
from bokeh.resources import CDN
import collections
from sys import argv
from gexf import Gexf
from itertools import combinations
import numpy as np
# import matplotlib.pyplot as plt # Not used right now
# from wordcloud import WordCloud, STOPWORDS # Not used right now
# import image # Not used right now.

# Takes filename from command prompt as input
script, filename = argv

"""
This script makes use of a number of dictionaries and lists
for creating the html output files. The counter variables
are mostly used as control data but also in the html file.
"""

plotyears = []
cr = []
authors = []
journals = []
keywords = []
keywordset = []

records = 0
citrefcounter = 0
authorcounter = 0
journalcounter = 0
keywordcounter = 0

#indexbody = [] Use to write everything to index. Memory overload, see below
authorbody = []
journalbody = []
keywordbody = []

wclist = []

indexbodydict= {}
citationnetworkdict = {}

# Open up the Web of Science source data as a tsv file.
with open(filename,'r') as tsv: # change file-name here
    next(tsv) #Skips the first line in the file, containing the TSV headers
    WoSdata = [line.strip().split('\t') for line in tsv] #everything as a list

# This begins the creation of a .gexf graph for keyword co-occurrence analysis.
# It comes already here because nodes are added in the loop below.
gexf = Gexf("Keyword Co-occurrence Network", "File:" + filename + ".")
graph = gexf.addGraph("directed", "static", "Web of Science Keyword network")

"""
This loop parses, redefines and writes the values selecte to 'index.html'
However, it also contains surplus variables for future uses, following the
non-existent documentation of the ISI format. Use however you feel like.
"""
for W in WoSdata:
    PT = W[0] # Publication Type
    AU = W[1] # Authors
    BA = W[2] # ?
    BE = W[3] # Editors of Proceedings
    GP = W[4] # ?
    AF = W[5] # Authors Full
    BF = W[6] # ?
    CA = W[7] # Group Authors
    TI = W[8] # Title
    SO = W[9] # Source (Journal title, full)
    SE = W[10] # Book Series title
    BS = W[11] # ?
    LA = W[12] # Language
    DT = W[13] # Document Type
    CT = W[14] # Conference Title
    CY = W[15] # Conference Date
    CL = W[16] # Conference Location
    SP = W[17] # Conference Sponsors
    HO = W[18] # Conference Host
    DE = W[19] # Original Keywords
    ID = W[20] # New Keywords by ISI (keywords plus)
    AB = W[21] # Abstract
    C1 = W[22] # Research Addresses: Note [] in fields.
    RP = W[23] # Reprint Address
    EM = W[24] # E-mail (Semi-colon separated)
    RI = W[25] # Researcher ID
    OI = W[26] # ?
    FU = W[27] # Funding agency and grant number
    FX = W[28] # Funding text
    CR = W[29] # Cited references (Semi-colon separated)
    NR = int(W[30]) # Cited reference count (Numerical value)
    TC = int(W[31]) # Times cited (Numerical value)
    Z9 = int(W[32]) # Total times Cited
    U1 = int(W[33]) # ?
    U2 = int(W[34]) # ?
    PU = W[35] # Publisher
    PI = W[36] # Publisher city
    PA = W[37] # Publisher Address
    SN = W[38] # ISSN (String value)
    EI = W[39] # ?
    BN = W[40] # ISBN
    J9 = W[41] # 29 Character Journal Abbreviation
    JI = W[42] # ISO Journal Title Abbreviation
    PD = W[43] # Publication date (mixed string and possible integer value)
    PY = int(W[44]) # Publication Year (Could also be parsed with date module)