import sys
import os
import time
import datetime
import signal
import math

import matplotlib
# Allow running headless from the command line
matplotlib.use("agg")

from numpy import *
from pylab import *

def signal_handler(signal, frame):
  print('\nInterrupted with CTRL-C downloading stopped....\n\n')
  sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)

#############################################################
#                     Main code below 
#############################################################


str2combo = { 'GPS_L1CA'    : 0,
              'GPS_L2E'     : 1,
              'GPS_L2C'     : 2,
              'GPS_L5'      : 3,
              'GLONASS_G1C' : 4,
              'GLONASS_G1P' : 5,
              'GLONASS_G2C' : 6,
              'GLONASS_G2P' : 7,
              'Galileo_E1'  : 8,
              'Galileo_E5A' : 9,
              'Galileo_E5B' : 10,
              'Galileo_E5L' : 11,
              'Galileo_E6'  : 12,
              'Beidou_B1'   : 13,
              'Beidou_B2'   : 14,
              'Beidou_B3'   : 15,
              'Beidou_B1C'  : 16,
              'Beidou_B2A'  : 17,
              'Beidou_B2A'  : 18}


combo2str = { 0  : 'GPS_L1CA',
              1  : 'GPS_L2E',
              2  : 'GPS_L2C',
              3  : 'GPS_L5',
              4  : 'GLONASS_G1C',
              5  : 'GLONASS_G1P',
              6  : 'GLONASS_G2C',
              7  : 'GLONASS_G2P',
              8  : 'Galileo_E1',
              9  : 'Galileo_E5A',
              10 : 'Galileo_E5B',
              11 : 'Galileo_E5L',
              12 : 'Galileo_E6',
              13 : 'Beidou_B1',
              14 : 'Beidou_B2',
              15 : 'Beidou_B3',
              16 : 'Beidou_B1C',
              17 : 'Beidou_B2A',
              18 : 'Beidou_B2B'}

RXData = [ "BD935_BD935_Zero",
           "BD940_BD940_Zero",
           "BD935_BD935_Short",
           "BD940_BD940_Short" ]

MaxRX =  len(RXData)

def parseNoPiSummary(year,month,day,filename,outputSuffix):
  Day   = []
  MPXX  = []
  Slips = []
  
  # This range of dates was when we temporarily relocated, there was 
  # an antenna distribution issue for several days.
  if( (year == 2019) and (month == 11) and (day >= 15) and (day <= 18) ):
    return

  if(os.path.isfile(filename)): 
    with open(filename,'r') as f:
      for line in f:
        ## 18 Columns:
        # Col0 = ComboNum
        # Col1 = ComboType String
        # Col2 = Valid 
        # Col3 = DDCa_#epochs 
        # Col4 = DDCa_min 
        # Col5 = DDCa_max 
        # Col6 = DDCa_mean 
        # Col7 = DDCa_std 
        # Col8 = DDCd_#epochs 
        # Col9 = DDCd_min 
        # Col10 = DDCd_max 
        # Col11 = DDCd_mean 
        # Col12 = DDCd_std 
        # Col13 = SDCNo_#epochs 
        # Col14 = SDCNo_min 
        # Col15 = SDCNo_max 
        # Col16 = SDCNo_mean 
        # Col17 = SDCNo_std
        
        ## 27 Columns:
        # Col0 = ComboNum
        # Col1 = ComboType String
        # Col2 = Valid 

        # Carrier
        # Col3 = DDCa_#epochs 
        # Col4 = DDCa_min 
        # Col5 = DDCa_max 
        # Col6 = DDCa_mean 
        # Col7 = DDCa_std 
        # Col8 = DDCa_mav
        
        # Code
        # Col9 = DDCd_#epochs 
        # Col10 = DDCd_min 
        # Col11 = DDCd_max 
        # Col12 = DDCd_mean 
        # Col13 = DDCd_std 
        # Col14 = DDCd_mav
        
        # Doppler
        # Col15 = DDDd_#epochs 
        # Col16 = DDDd_min 
        # Col17 = DDDd_max 
        # Col18 = DDDd_mean 
        # Col19 = DDDd_std 
        # Col20 = DDDd_mav

        # Single diff C/No
        # Col121 = SDCNo_#epochs 
        # Col122 = SDCNo_min 
        # Col123 = SDCNo_max 
        # Col124 = SDCNo_mean 
        # Col125 = SDCNo_std
        # Col126 = SDCNo_mav
        
        data = line.rstrip().split()

        if((data[0] != '%%') and (int(data[2]) == 1) and (int(data[3]) > 40000)):
          delta = datetime.date(year,month,day) - datetime.date(2018,1,1)

          # B2B data before day 770 is unreliable (before then very early support)
          if( (data[1] == "Beidou_B2B") and (delta.days <= 770) ):
            continue

          fid = open('%s-%s.txt' % (data[1],outputSuffix),'a')
          fid.write("%d %d %d " % (year,month,day))

          if(len(data) == 18):
            # Old NoPi format
            for i in range (0,13):
              fid.write("%s " % data[i])
            for i in range(5):
              # We don't have Doppler
              fid.write("NaN ")
            for i in range (13,18):
              fid.write("%s " % data[i])
          elif(len(data) == 23):
            for i in range(0,len(data)):
              fid.write("%s " % data[i])
          elif(len(data) == 27):
            # Relative to 23 columns four "mav" fields were added, convert
            # the data back to the 23 column format by dropping the mav data
            for i in range (0,8):
              fid.write("%s " % data[i])
            for i in range (9,14):
              fid.write("%s " % data[i])
            for i in range (15,20):
              fid.write("%s " % data[i])
            for i in range (21,26):
              fid.write("%s " % data[i])
          else:
            print("Format Error")
          
          fid.write("\n")
          fid.close()


def getFloatData(valueStr,limit):
  if(float(valueStr) > limit):
    ret = float('nan')
  else:
    ret = float(valueStr)
  return ret

def writeDDTableData(fid,rxStr,year,month,day,output,DOY,DDMean,DDSigma):          

  fid.write(  "<tr>")
  fid.write(  "<td><a href='%s' target='_blank'>%s-%s-%s</a></td>" %
              (   'http://teller.eng.trimble.com/GPSRxEng/GNSSResults/'
                + year + '-Res/'
                + year + month.zfill(2) + day.zfill(2)
                + '_' + rxStr + '_' + rxStr + '_' + output
                + '/NoPiDI_Top.html',
                  year,month.zfill(2),day.zfill(2)))
  fid.write("<td>" + str(DOY) + "</td>")
  fid.write("<td>" + str.format('{0:.2f}',DDMean) + "</td>")
  fid.write("<td>" + str.format('{0:.2f}',DDSigma) + "</td>")
  fid.write("</tr>")

def plotCodeCarrierDoppler(fileSuffix1,fileSuffix2,output):
  for i in range(len(combo2str)):
    DOY1 = []
    DDCodeMean1  = []
    DDCodeSigma1 = []
    DDCarrMean1  = []
    DDCarrSigma1 = []
    DDDoppMean1  = []
    DDDoppSigma1 = []
    got1 = False
    
    DOY2 = []
    DDCodeMean2  = []
    DDCodeSigma2 = []
    DDCarrMean2  = []
    DDCarrSigma2 = []
    DDDoppMean2  = []
    DDDoppSigma2 = []
    got2 = False
    
    codeLimit = 5
    carrLimit = 200

    outputFilenameCarr = combo2str.get(i) + '-BD935-' + output + ".DDCarrSigma.html"
    fidCarr = open(outputFilenameCarr,'w')
    fidCarr.write("<html><body><table border='1'>") 
    fidCarr.write("<tr><th>Date</th><th>DOY</th><th>Mean</th><th>Sigma</th></tr>")
    
    outputFilenameCode = combo2str.get(i) + '-BD935-' + output + ".DDCodeSigma.html"
    fidCode = open(outputFilenameCode,'w')
    fidCode.write("<html><body><table border='1'>") 
    fidCode.write("<tr><th>Date</th><th>DOY</th><th>Mean</th><th>Sigma</th></tr>")

    outputFilenameDopp = combo2str.get(i) + '-BD935-' + output + ".DDDoppSigma.html"
    fidDopp = open(outputFilenameDopp,'w')
    fidDopp.write("<html><body><table border='1'>") 
    fidDopp.write("<tr><th>Date</th><th>DOY</th><th>Mean</th><th>Sigma</th></tr>")

    filename = combo2str.get(i) + '-' + fileSuffix1
    if(os.path.isfile(filename + '.txt')): 
      got1 = True
      with open(filename + '.txt','r') as f:
        for line in f:
          data = line.rstrip().split()
          #DOY1.append(datetime.datetime((int)(data[0]), (int)(data[1]), (int)(data[2]), 0, 0).timetuple().tm_yday)
          dayDelta = datetime.date( (int)(data[0]), (int)(data[1]), (int)(data[2])) - datetime.date(2018,1,1)
          DOY1.append(dayDelta.days)

          # Code
          DDCodeMean1.append(getFloatData(data[14],codeLimit))
          DDCodeSigma1.append(getFloatData(data[15],codeLimit))
          writeDDTableData(fidCode,'BD935',data[0],data[1],data[2],output,DOY1[-1],DDCodeMean1[-1],DDCodeSigma1[-1]) 

          # Carr
          DDCarrMean1.append(getFloatData(data[9],carrLimit))
          DDCarrSigma1.append(getFloatData(data[10],carrLimit))
          writeDDTableData(fidCarr,'BD935',data[0],data[1],data[2],output,DOY1[-1],DDCarrMean1[-1],DDCarrSigma1[-1]) 

          # Doppler
          DDDoppMean1.append(getFloatData(data[19],carrLimit))
          DDDoppSigma1.append(getFloatData(data[20],carrLimit))
          writeDDTableData(fidDopp,'BD935',data[0],data[1],data[2],output,DOY1[-1],DDDoppMean1[-1],DDDoppSigma1[-1]) 

    fidCarr.write("</table></body></html>")
    fidCode.write("</table></body></html>")

    outputFilenameCarr = combo2str.get(i) + '-BD940-' + output + ".DDCarrSigma.html"
    fidCarr = open(outputFilenameCarr,'w')
    fidCarr.write("<html><body><table border='1'>") 
    fidCarr.write("<tr><th>Date</th><th>DOY</th><th>Mean</th><th>Sigma</th></tr>")
    
    outputFilenameCode = combo2str.get(i) + '-BD940-' + output + ".DDCodeSigma.html"
    fidCode = open(outputFilenameCode,'w')
    fidCode.write("<html><body><table border='1'>") 
    fidCode.write("<tr><th>Date</th><th>DOY</th><th>Mean</th><th>Sigma</th></tr>")

    outputFilenameDopp = combo2str.get(i) + '-BD940-' + output + ".DDDoppSigma.html"
    fidDopp = open(outputFilenameDopp,'w')
    fidDopp.write("<html><body><table border='1'>") 
    fidDopp.write("<tr><th>Date</th><th>DOY</th><th>Mean</th><th>Sigma</th></tr>")

    filename = combo2str.get(i) + '-' + fileSuffix2
    if(os.path.isfile(filename + '.txt')): 
      got2 = True
      with open(filename + '.txt','r') as f:
        for line in f:
          data = line.rstrip().split()
          dayDelta = datetime.date( (int)(data[0]), (int)(data[1]), (int)(data[2])) - datetime.date(2018,1,1)
          #DOY2.append(datetime.datetime((int)(data[0]), (int)(data[1]), (int)(data[2]), 0, 0).timetuple().tm_yday)
          DOY2.append(dayDelta.days)
          
          # Code
          DDCodeMean2.append(getFloatData(data[14],codeLimit))
          DDCodeSigma2.append(getFloatData(data[15],codeLimit))
          writeDDTableData(fidCode,'BD940',data[0],data[1],data[2],output,DOY2[-1],DDCodeMean2[-1],DDCodeSigma2[-1]) 

          # Carr
          DDCarrMean2.append(getFloatData(data[9],carrLimit))
          DDCarrSigma2.append(getFloatData(data[10],carrLimit))
          writeDDTableData(fidCarr,'BD940',data[0],data[1],data[2],output,DOY2[-1],DDCarrMean2[-1],DDCarrSigma2[-1]) 
          
          # Doppler
          DDDoppMean2.append(getFloatData(data[19],carrLimit))
          DDDoppSigma2.append(getFloatData(data[20],carrLimit))
          writeDDTableData(fidDopp,'BD935',data[0],data[1],data[2],output,DOY2[-1],DDDoppMean2[-1],DDDoppSigma2[-1]) 
    
    fidCarr.write("</table></body></html>")
    fidCode.write("</table></body></html>")
    fidDopp.write("</table></body></html>")

    if( (got1 == True) or (got2 == True) ):
      fig=figure()
      ax=fig.add_subplot(111)
      
      if(got1 == True):
        plot( array(DOY1),array(DDCodeSigma1),'bx',label='BD935')

      if(got2 == True):
        plot( array(DOY2),array(DDCodeSigma2),'rx',label='BD940')

      #print ax.get_ylim(),sigma1,sigma2,med1,med2

      xlabel('Time [Day of Year]')
      ylabel(r'DD Code Sigma [m]')
      title(combo2str.get(i) + ' Code')
      grid(True)
      legend()
      tight_layout()
      # Prevent the axis numers having an offset
      ax.get_xaxis().get_major_formatter().set_useOffset(False)
      ax.get_yaxis().get_major_formatter().set_useOffset(False)
      show()
      # Save the data as a PNG file
      savefig(combo2str.get(i) + '-' + output + ".DDCodeSigma.png",dpi=150)
      close()

      fig2=figure()
      ax=fig2.add_subplot(111)
      if(got1 == True):
        plot( array(DOY1),array(DDCarrSigma1),'bx',label='BD935')
      if(got2 == True):
        plot( array(DOY2),array(DDCarrSigma2),'rx',label='BD940')
      xlabel('Time [Day of Year]')
      ylabel(r'DD Carrier Sigma [mcycles]')
      title(combo2str.get(i) + ' Carrier')
      grid(True)
      legend()
      tight_layout()
      # Prevent the axis numers having an offset
      ax.get_xaxis().get_major_formatter().set_useOffset(False)
      ax.get_yaxis().get_major_formatter().set_useOffset(False)
      show()
      # Save the data as a PNG file
      savefig(combo2str.get(i) + '-' + output + ".DDCarrSigma.png",dpi=150)
      close()

      fig2=figure()
      ax=fig2.add_subplot(111)
      if(got1 == True):
        plot( array(DOY1),array(DDDoppSigma1),'bx',label='BD935')
      if(got2 == True):
        plot( array(DOY2),array(DDDoppSigma2),'rx',label='BD940')
      xlabel('Time [Day of Year]')
      ylabel(r'DD Doppler Sigma [m/s]')
      title(combo2str.get(i) + ' Doppler')
      grid(True)
      legend()
      tight_layout()
      # Prevent the axis numers having an offset
      ax.get_xaxis().get_major_formatter().set_useOffset(False)
      ax.get_yaxis().get_major_formatter().set_useOffset(False)
      show()
      # Save the data as a PNG file
      savefig(combo2str.get(i) + '-' + output + ".DDDoppSigma.png",dpi=150)
      close()

print(len(combo2str))

datetimeformat = "%Y-%m-%d %H:%M:%S"

for RXNum in range(0,MaxRX):
  start  = datetime.datetime.strptime("2018-01-01 00:00:00",datetimeformat)
  #stop   = datetime.datetime.strptime("2018-02-11 13:00:00",datetimeformat)
  stop   = datetime.datetime.now()
  delta = ((stop-start).total_seconds()/86400)

  for i in range((int)(ceil(delta))):
    print(RXData[RXNum],start.day,start.month,start.year)
    parseNoPiSummary(start.year,start.month,start.day,
                     '/net/daffy/mnt/data_drive/%4d/%4d%02d%02d_%s/NoPiDI_Stats_Summary.txt' % 
                     (start.year,start.year,start.month,start.day,RXData[RXNum]),
                     RXData[RXNum])
    start = start + datetime.timedelta(days=1)

plotCodeCarrierDoppler(RXData[0],RXData[1],'Zero')
plotCodeCarrierDoppler(RXData[2],RXData[3],'Short')



