﻿import numpy
import json
import codecs
import matplotlib.pyplot as plt
import matplotlib.ticker as fmt 
import sys
import os

from operator import itemgetter
from datetime import datetime
import collections
import math


#python MultiPhaseLineByFile5.py start_time=1 end_time=5000 logfile=d:\temp temppath=d:\temp datafile=d:\temp\results.dat headerfile=d:\temp\results.dat debug=true colsplit=, rowsplit=;
#python MultiPhaseLineByFile5.py starttime=1 endtime=1700 logfile=d:\temp temppath=d:\temp datafile=d:\prccounterapp\results\AutogeneratedResultFile_113253652.dat headerfile=d:\temp\header.dat debug=true colsplit=, rowsplit=; timetype=sec
#python MultiPhaseLineByFile5.py starttime=1 logfile=d:\temp temppath=d:\temp datafile=d:\prccounterapp\results\AutogeneratedResultFile_142298108.dat headerfile=d:\temp\header.dat debug=true colsplit=, rowsplit=; timetype=sec outlier=1E-4

measname    = 'none'
start_time  = '-2';
end_time    = '-2';
logfile     ='MultiPhaseByFile.log'
temppath    = ''
tempfile    = ''
datafile    = ''
debg        = 'False';
timetype    = 'none'
colsplit    = ","
rowsplit    = ";"
headerfile  = "";
outlier = 0;


def dateandtime():
    now = datetime.now()
    # dd/mm/YY H:M:S
    #now.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
    dt_string = now.strftime("%d/%m/%Y %H:%M:%S.%f")[:-3]
    return dt_string

def SetDefaults():
	global logfile
	global tempfile
	if(len(logfile) < 1): logfile = 'logs'
	if(len(tempfile) < 1): logfile = 'temp'

SetDefaults()

print(dateandtime()+'->Defaults->Set Start Time:'+start_time)
print(dateandtime()+'->Defaults->Set End Time:'+end_time)
print(dateandtime()+'->Defaults->Set Timetype:'+timetype)
print(dateandtime()+'->Defaults->Set Logfile:'+logfile)
print(dateandtime()+'->Defaults->Set Temppath:'+temppath)
print(dateandtime()+'->Defaults->Set Datafiles:'+datafile)
print(dateandtime()+'->Defaults->Set Debugmode:'+debg)
print(dateandtime()+'->Defaults->Set Timetype:'+timetype)
print(dateandtime()+'->Defaults->Set Colsplit:'+colsplit)
print(dateandtime()+'->Defaults->Set Rowsplit:'+rowsplit)
print(dateandtime()+'->Defaults->Set Headerfile:'+headerfile)
print(' ')
print(dateandtime()+'->Given COMMAND LINE Parameters for Script: MultiPhaseLineByFile5.py')

for line in sys.argv:
    if(line.endswith(';')): line = line[0:-1]
    linearr = line.split("=")
    if(len(linearr) > 1):
        if(linearr[0].startswith('starttime')):
            start_time = linearr[1]
            print(dateandtime()+'->Arguments->Set Start Time:'+start_time)
        if(linearr[0].startswith('endtime')):
            end_time = linearr[1]
            print(dateandtime()+'->Arguments->Set End Time:'+end_time)
        if(linearr[0].startswith('logfile')):
            logfile = linearr[1]
            print(dateandtime()+'->Arguments->Set Logfile:'+logfile)
        if(linearr[0].startswith('temp')):
            temppath = linearr[1]
            print(dateandtime()+'->Arguments->Set Temppath:'+temppath)
        if(linearr[0].startswith('datafile')):
            datafile = linearr[1]
            print(dateandtime()+'->Arguments->Set Datafiles:'+datafile)
        if(linearr[0].startswith('debug')):
            debg = linearr[1]
            print(dateandtime()+'->Arguments->Set Debugmode:'+debg)
        if(linearr[0].startswith('timetype')):
            timetype = linearr[1]
            print(dateandtime()+'->Arguments->Set Timetype:'+timetype)
        if(linearr[0].startswith('colsplit')):
            colsplit = linearr[1]
            print(dateandtime()+'->Arguments->Set Colsplit:'+colsplit)
        if(linearr[0].startswith('rowsplit')):
            rowsplit = linearr[1]
            print(dateandtime()+'->Arguments->Set Rowsplit:'+rowsplit)
        if(linearr[0].startswith('headerfile')):
            headerfile = linearr[1]
            print(dateandtime()+'->Arguments->Set Headerfile:'+headerfile)
        if(line.startswith('outlier')):
            outlier = float(linearr[1])
            print(dateandtime()+'->Arguments->Set Outlier:%f' % outlier)

print(' ')
phase = []
time = []



def collectGraph2(pl,axs,phase,time,devregname,timetype,plotn,scale,titlename,xtitelname,ytitelname,legendname,colorname,markername,outlier):
	print(dateandtime()+'->collectGraph->START')
	minmax = []	
	global np
	global logfile
	global tempfile
	sm = time[0]
	em = time[len(time)-1]
	#print(time)
	#print(sm)
	#print(em)
	outval = 0.000000001
	
	ax = axs
	devreg = devregname.split("|")

	#print('devregname             :'+devreg[0])
	#print('devregtype             :'+devreg[1])
	print(dateandtime()+'->collectGraph->Titelname:'+titelname)
	print(dateandtime()+'->collectGraph->Legendname:'+legendname)
	
	if (sm < 0 or em < 0 or em < sm):
		print('Length of time:')
		print(len(time))
		print('Length of phase:')
		print(len(phase))
		print ('NO MAX and MIN MJD->RETURN from CollectGraph')

		#logg.debug('NO MAX and MIN MJD->RETURN from CollectGraph')
		return

	dt = "Time:'%s'" % (sm)
	py = []
	pu = [1]
	
	for row in phase:
		ro = (row+offset)
		val = (row+offset)*(math.pow(10, scale))
		#print(val)
		#print(ro)
		if(outlier == 0):
			py.append(val)
		elif((ro < outlier) and (ro > -outlier)):
			py.append(val)

	if(outlier == 0):
		px=time
	else:
		itime = 0
		px = time[0:len(py)]
		for row in phase:
			ro = (row+offset)
			val = (row+offset)*(math.pow(10, scale))
			#print(val)
			#print(ro)
			if((ro < outlier) and (ro > -outlier)):
				px[itime] = px[itime]
				itime = itime+1

	#print(len(py))
	#print(len(px))
	print(dateandtime()+'->collectGraph->px length:'+str(len(px)))
	print(dateandtime()+'->collectGraph->py length:'+str(len(py)))
	pu[0] = 1
	ncur = 0

	npoints = len(py)
	npstr = "->collectGraph->NPoints:%d" % npoints
	print (dateandtime()+npstr)
	#logg.debug(npstr)

	if(npoints <= 0) :
		print (dateandtime()+'->collectGraph->No datapoints')
		print (dateandtime()+'->collectGraph->RETURN')
		return

	if(em-sm <= 0) :
		print (dateandtime()+'->collectGraph->Start Time equal or bigger then End Time')
		print (dateandtime()+'->collectGraph->RETURN')
		return

	np=np+npoints
	#nrate = ((em-sm)*86400)/npoints
	if(timetype == 'mjd'):
		dt = "%s, MJD:(n=%8.2f) Points:(n=%d)" % (titlename,em-sm,np)
	if(timetype == 'sec'):
		dt = "%s, Seconds:(n=%8.2f) Points:(n=%d)" % (titlename,em-sm,np)

	yfmt = fmt.ScalarFormatter()
	xfmt = fmt.ScalarFormatter()
	yfmt.set_powerlimits((-9,9))	

	ax.set_xlabel(xtitelname,fontsize=labelsize)
	ax.set_ylabel(ytitelname,fontsize=labelsize)
	ax.grid(True)

	#dt = titlename
	ax.set_title(dt)
	print(dateandtime()+'->collectGraph->usemark: %d'%usemark)
	if(usemark > 0):
		ax.plot(px,py,color=colorname,linestyle='none',marker=markername, linewidth=1.0, label = legendname)
	elif(usemark == 0):
		ax.plot(px,py,'-',color=colorname, linewidth=0.5,label=legendname)

	pl.gca().yaxis.set_major_formatter(yfmt)
	pl.gca().xaxis.set_major_formatter(xfmt)
	
	ax.legend(bbox_to_anchor=(1.05, 1), loc='upper center', borderaxespad=0.)

	fn = logfile
	print(dateandtime()+'->collectGraph->Logfile:'+fn)
	file = open(fn,'w')  
	file.write('end')  
	file.close()  
	print(dateandtime()+'->collectGraph->END,RETURN')


scale = 9.0
offset = 0.0
usemark = 0.0
#subplots = 0.0
labelsize = 12.0
titlesize = 12.0
scalen = 2
legend = "phase"
color = "red"
marker = "."
n = -1
np = 0
#logfile     ='MultiPhaseByFile.log'

#fig, axs = plt.subplots(nrows=1, ncols=1, sharex='all')

fig = plt.figure(figsize=(16,8))
axs = fig.subplots(nrows=1, ncols=1, sharex='all')
axs.grid()

#fig.gca().set_xscale('log',basex=scalen)
#fig.gca().set_yscale('log',basey=scalen)

dfiles = datafile.split(",")
if(len(headerfile) > 0):
    print(dateandtime()+'->Given Parameters by Headerfile:'+headerfile)
    for filelineno, line in enumerate(open(headerfile, encoding="utf-8")):
        line = line.strip()
        if(len(line) < 1): continue
        if(line.startswith('#starttime:') and (float(start_time) < -1)):
            start_time = line[10:]
            print(dateandtime()+'->Headerfile->Set Start Time:'+start_time)
        elif(line.startswith('#endtime:') and (float(end_time) < -1)):
            end_time = line[8:]
            print(dateandtime()+'->Headerfile->Set End Time:'+end_time)
        elif(line.startswith('#offset:')):
            offset = float(line[8:])
            print(dateandtime()+'->Headerfile->Set Offset:'+line[8:])
        elif(line.startswith('#titel:')):
            titelname = line[7:]
            print(dateandtime()+'->Headerfile->Set Titelname:'+titelname)
        elif(line.startswith('#xtitel:')):
            xtitelname = line[8:]
            print(dateandtime()+'->Headerfile->Set X-Titel:'+line[8:])
        elif(line.startswith('#ytitel:')):
            ytitelname = line[8:]
            print(dateandtime()+'->Headerfile->Set Y-Titel:'+line[8:])
        elif(line.startswith('#yscale:')):
            scale = float(line[8:])
            print(dateandtime()+'->Headerfile->Set Y-Scale:'+line[8:])
        elif(line.startswith('#legend:')):
            legend = line[8:]
            print(dateandtime()+'->Headerfile->Set Legend:'+legend)
        elif(line.startswith('#color:')):
            color = line[7:]
        elif(line.startswith('#marker:')):
            marker = line[8:]
        elif(line.startswith('#timetype:') and timetype == 'none'):
            timetype = line[10:]
            print(dateandtime()+'->Headerfile->Set Timetype:'+timetype)
        elif(line.startswith('#logfile:') and (len(logfile) < 1)):
            logfile = line[9:]
            print(dateandtime()+'->Headerfile->Set Logfile:'+logfile)
        elif(line.startswith('#temppath:') and (len(temppath) < 1)):
            temppath = line[10:]
            print(dateandtime()+'->Headerfile->Set Temppath:'+temppath)
        elif(line.startswith('#colsplit:')):
            colsplit = line[10:]
            print(dateandtime()+'->Headerfile->Set Colsplit:'+colsplit)
        elif(line.startswith('#rowsplit:')):
            rowsplit = line[10:]
            print(dateandtime()+'->Headerfile->Set Rowsplit:'+rowsplit)
        elif(line.startswith('#outlier:')):
            outlier = float(line[9:])
            print(dateandtime()+'->Headerfile->Set Outlier:%f') % outlier
        elif(line.startswith('#measname:')):
            phase.clear()
            time.clear()
            measname = line[10:]
            print(dateandtime()+'->Clear phase and time')
            print(' ')
            print(dateandtime()+'->Headerfile->Set Measname:'+measname)
else:
    print('No Headerfile to load')

print(' ')

for dfile in dfiles:
    print(dateandtime()+'Datafile to load:'+dfile)
    enddone = False;
    for filelineno, line in enumerate(open(dfile, encoding="utf-8")):
        line = line.strip()
        if(len(line) < 1): continue
        if(line.startswith('#starttime:') and (float(start_time) < -1)):
            start_time = line[10:]
            print(dateandtime()+'->Datafile->Set start mjd:'+start_time)
        elif(line.startswith('#endtime:') and (float(end_time) < -1)):
            end_time = line[8:]
            print(dateandtime()+'->Datafile->Set end mjd:'+end_time)
        elif(line.startswith('#offset:')):
            offset = float(line[8:])
        elif(line.startswith('#titel:')):
            titelname = line[7:]
        elif(line.startswith('#xtitel:')):
            xtitelname = line[8:]
        elif(line.startswith('#ytitel:')):
            ytitelname = line[8:]
        elif(line.startswith('#yscale:')):
            scale = float(line[8:])
        elif(line.startswith('#legend:')):
            legend = line[8:]
        elif(line.startswith('#color:')):
            color = line[7:]
        elif(line.startswith('#marker:')):
            marker = line[8:]
        elif(line.startswith('#timetype:') and timetype == 'none'):
            timetype = line[10:]
            print(dateandtime()+'->Datafile->Set Timetype:'+timetype)
        elif(line.startswith('#logfile:') and (len(logfile) < 1)):
            logfile = line[9:]
            print(dateandtime()+'->Datafile->Set logfile:'+logfile)
        elif(line.startswith('#temppath:') and (len(temppath) < 1)):
            temppath = line[10:]
            print(dateandtime()+'->Datafile->Set temppath:'+temppath)
        elif(line.startswith('#colsplit:')):
            colsplit = line[10:]
        elif(line.startswith('#rowsplit:')):
            rowsplit = line[10:]
        elif(line.startswith('#end')):
            n = n+1
            print(dateandtime()+'->Datafile->Set end and start collectGraph')
            print(n)
            enddone = True
            SetDefaults()
            collectGraph2(fig,axs,phase,time,measname,float(start_time),float(end_time),n,-1,scale,titelname,xtitelname,ytitelname,legend,color,marker)
            phase.clear()
        elif(line.startswith('#outlier:')):
            outlier = float(line[9:])
            print(dateandtime()+'->Datafile->Set Outlier:%f') % outlier
        elif(line.startswith('#measname:')):
            phase.clear()
            time.clear()
            measname = line[10:]
        elif(line[:1] in '+-0123456789'):
             linearr = line.split(colsplit)
             st = float(start_time)
             mjd = float(linearr[0])
             if((float(end_time) < 0) and (float(start_time) < 0)):
                if(timetype == 'mjd'):
                    secday = float(linearr[0])/86400.0
                    time.append(secday+st)
                if(timetype == 'sec'):
                    secday = float(linearr[0])
                    time.append(secday)
                yval = 0.0
                if(linearr[1].endswith(rowsplit)): 
                    yval = float(linearr[1][0:-1])
                else: 
                    yval = float(linearr[1])
                phase.append(yval)
             elif((mjd < float(end_time)) and (mjd > float(start_time))):
                if(timetype == 'mjd'):
                    secday = float(linearr[0])/86400.0
                    time.append(secday+st)
                if(timetype == 'sec'):
                    secday = float(linearr[0])
                    time.append(secday)
                yval = 0.0
                if(linearr[1].endswith(rowsplit)): 
                    yval = float(linearr[1][0:-1])
                else: 
                    yval = float(linearr[1])
                phase.append(yval)



    if(enddone==False):
        n = n+1
        
        print(dateandtime()+'->End of Definition section->Start collectGraph')
        #print(n)
        enddone = True
        #print(len(time))
        #print(len(phase))
        
        collectGraph2(fig,axs,phase,time,measname,timetype,-1,scale,titelname,xtitelname,ytitelname,legend,color,marker,outlier)
        phase.clear()




plt.show()

