import pip

requires = ['numpy','xmltodict']

installed_packages = pip.get_installed_distributions()
installed_packages_list = sorted(["%s==%s" % (i.key, i.version) for i in installed_packages])

matching = [[libs for libs in installed_packages_list if x in libs] for x in requires]

if len(matching) == len(requires):
    print "requirement fullfilled"
else:
    print "something is missing"

"""
Explanation: MDCS REST api demo -- data from Diffusion couple
End of explanation
"""


#!conda install numpy
#!pip install xmltodict

import os

file_check = ['DataModelDict.py','GE-DiffusionCouple-IN100-IN718.xml','GE-DiffusionCouple-IN718-R95.xml'
              ,'GE-DiffusionCouple-R88-R95.xml','mdcs','data','fig']

files = os.listdir(".")
matching = filter(None, [[fs for fs in files if x in fs] for x in file_check])


if len(matching) == len(file_check):
    print "files in the folder"
else:
    print "something is missing"

# standard python libraries
import numpy as np
import glob
import requests
import xmltodict

# -------------------------------------------- Matplotlib for plotting
                                          # NEED to be installed (anaconda)
# http://matplotlib.org/
import matplotlib.pyplot as plt
import matplotlib.colorbar as clb
import matplotlib.patches as mpatches
import matplotlib.lines as mlines
import matplotlib.gridspec as gridspec
from matplotlib import ticker
from matplotlib.ticker import LogLocator
%matplotlib inline


# customized library by Zachary Trautt
import mdcs
# customized library by Lucas Hale
from DataModelDict import DataModelDict

from IPython.display import Image
Image("fig/file_problem.png", width=600)

"""
Explanation: NOTE: if the check is failed, execute the following cell
End of explanation
"""


# ---- setup user information to sign in MDCS
USER = "admin"
PSWD = "admin"
MDCS_URL = "http://127.0.0.1:8000"

"""
Explanation: How to manage .xml and image files? -- theupdate files are prepared in the folder "data"
steps:
(1) upload the draft XMLs
(2) upload image files
(3) query the data in MDCS
(4) update the content with image URL
(5) delete the draft XMLs from MDCS
(6) upload the updated XMLs
End of explanation
"""


xml_files = glob.glob("data/*.xml")
print "Files:  ",len(xml_files), xml_files

filetitle = []
xmlfile = []
fig_files = []

for i in range(len(xml_files)):
    filetitle.append(xml_files[i][5:-4])
    fig_files.append(str(xml_files[i][0:len(xml_files[i])-3] + 'tif'))

print "XML files: ",filetitle
print "Image files: ",fig_files

"""
Explanation: obtain the file names
End of explanation
"""


figURL = []

for i in range(len(xml_files)):
    figURL.append(mdcs.blob.upload(fig_files[i],MDCS_URL,USER,PSWD,'cert'))

print figURL

"""
Explanation: Using blob function to upload the image files and return the URL
End of explanation
"""


for i in range(len(xml_files)):
    print mdcs.curate_as(xml_files[i],filetitle[i],MDCS_URL, \
        USER,PSWD,'cert',template_title="mod_demo_diffusion0224")

"""
Explanation: upload the XML files
make suer the template_title is correct on your MDCS instance
End of explanation
"""


qres = []
file_id = []

for i in range(len(xml_files)):
    content = mdcs.explore.query(MDCS_URL,USER,PSWD,'cert',  \
                                 None,str({'title':filetitle[i]}))
    qres.append(xmltodict.parse(content[0]['content']))
    file_id.append(content[0]['_id'])


"""
Explanation: demo the function "query" to search the data by filetitles in database:
(1) the data content is stored in python list "qres"
(2) the specific "id" assigned by MDCS is kept in file_id
End of explanation
"""


res = []

for i in range(len(xml_files)):
    qres[i]['experiment']['experimentType']['chemicalDiffusivity'] \
    ['dataAnalysis']['micrographOfStructure']['fileExtension'] = figURL[i]
    res.append(xmltodict.unparse(qres[i]))


"""
Explanation: update XML files with image URL
End of explanation
"""


filename = []

# write information to interdiffusion.xml
for i in range(len(xml_files)):
    filename.append(str(filetitle[i] + str('.xml')))
    diffusion = open(filename[i], 'w')
    diffusion.write(res[0].encode('utf8'))
    diffusion.close()

"""
Explanation: output the content as new XMLs
WARNING: Occassionally, Jupyter may cause issues to output large file. You may try to run this cell multiple times.
End of explanation
"""


for i in range(len(xml_files)):
    mdcs.explore.delete(file_id[i],MDCS_URL,USER,PSWD,cert=None)

"""
Explanation: delete the old XMLs in MDCS
End of explanation
"""


for i in range(len(xml_files)):
    print filename[i],mdcs.curate_as(filename[i],filetitle[i]
    ,MDCS_URL,USER,PSWD,'cert',template_title="mod_demo_diffusion0224")

"""
Explanation: upload the revised XMLs
End of explanation
"""


content = mdcs.explore.query(MDCS_URL,USER,PSWD,'cert',
                             None,str({'title':filetitle[0]}))
qdata = DataModelDict(content[0]['content'])

table = qdata.find('table')

distable = []

for row in table['rows'].iterlist('row'):
    disrow = []
    for column in row.iterlist('column'):
        disrow.append(column['#text'])

    distable.append(disrow)

del distable[0]


"""
Explanation: Vidualize data
query, load, and plot the data
End of explanation
"""


distable = np.array(distable)

fig, ax = plt.subplots(figsize=(9, 6))
for i in range(2,len(distable[0])):
    ax.plot(distable[:,0],distable[:,i],'o-')

fig.tight_layout()
plt.show()


"""
Explanation: Plot the data
End of explanation
"""


