# -*- coding: utf-8 -*-
"""
Created on Wed May 27 14:26:42 2020

@author: Javier.Mardones
"""

import pythonaddins

def storm(opath):

    outfile1 = opath + r'/design_storm/stormanalysis.txt'
    outfile2 = opath + r'/design_storm/designstorm.txt'

    outfile1_str = 'STORM ANALYSIS:' + '\n'
    outfile2_str = 'RAINFALL DISTRIBUTION:' + '\n'

    stormin = open(opath +  r"/design_storm/stormdata.txt", "r")

    lines = stormin.readlines()
    stormin.close()
    lines = [line[:-1] for line in lines]
    nstorm = int(lines[0])
    iarc = int(lines[1])

    tdist = [5, 10, 15, 30, 60, 120, 180, 360, 720, 1440, 2880]
    tdist = [x/60 for x in tdist]
    dt = 0.1

    imax_rd = 0
    for nn in range(nstorm):

        stormid = str(lines[2 + imax_rd])
        iduration = int(lines[3 + imax_rd])

        if iduration == 6:
            imax = 7
        elif iduration == 12:
            imax = 8
        elif iduration == 24:
            imax = 9
        elif iduration == 48:
            imax = 10

        pdist = []
        for i in range(11):
            pdist.append(float(lines[4+i + imax_rd]))

        imax_rd = imax_rd + 13

        tmax = float(iduration)
        itypeii = 0

        if pdist[0] < 0:
            itypeii = 1
        stormdepth = round(pdist[imax],2)

        if itypeii == 0:
            outfile1_str = outfile1_str + stormid.rjust(4) + str('gage ' + str(nn)).rjust(8) + str(stormdepth).rjust(8) + str('rt' + str(stormid)).rjust(12)  + str(iarc).rjust(4) + '\n'
        else:
            outfile1_str = outfile1_str + stormid.rjust(4) + str('gage ' + str(nn)).rjust(8) + str(stormdepth).rjust(8) + str('Type II ' + str(iarc)).rjust(12) + '\n'

        sp = [0]*(2*(imax+1)+1)
        st = [0]*(2*(imax+1)+1)
        pdiff = []
        tdiff = []

        if itypeii == 0:
            pdiff.append(pdist[0]/2)
            tdiff.append(tdist[0]/2)
            for i in range(10):
                 pdiff.append((pdist[i+1] - pdist[i])/2)
                 tdiff.append((tdist[i+1] - tdist[i])/2)
            sp[imax+1] = pdist[imax]/2
            st[imax+1] = tdist[imax]/2
            for i in range(imax+1):
                 sp[imax+i+2] = sp[imax + i + 1] + pdiff[i]
                 st[imax+i+2] = st[imax + i + 1] + tdiff[i]
            for i in range(imax+1):
                 sp[i+1] = pdist[imax] - sp[2*(imax + 1) - i]
                 st[i+1] = tdist[imax] - st[2*(imax + 1) - i]

            n = int(tmax/dt)

            p = []
            for i in range(n+1):
                tt = round(dt * i,1)
                ii = 1
                ifound = 0
                while ifound == 0:
                     ii = ii + 1
                     try:
                         if tt >= st[ii-2] and tt < st[ii-1]:
                             ifound = ii
                             p.append(sp[ii-2] + (sp[ii-1] - sp[ii-2]) / (st[ii-1] - st[ii-2]) * (tt - st[ii-2]))
                     except:
                         ifound = ii
                         pass
            pythonaddins.MessageBox("test", "1")
            p.append(pdist[imax])

            outfile2_str = outfile2_str + str('rt' + stormid).rjust(8) + str(dt).rjust(6) + '\n'
            pythonaddins.MessageBox("test", "2")
            for i in range(int((n+1)/5)):
                pythonaddins.MessageBox("test", "3")
                j =[(i+1)*5-5,(i+1)*5-4,(i+1)*5-3,(i+1)*5-2,(i+1)*5-1]
                theprecip = [p[j[0]]/pdist[imax],p[j[1]]/pdist[imax],p[j[2]]/pdist[imax],p[j[3]]/pdist[imax],p[j[4]]/pdist[imax]]
                theprecip = [round(num, 4) for num in theprecip]
                theprecip = [str(num) for num in theprecip]
                outfile2_str = outfile2_str + theprecip[0].ljust(8) + theprecip[1].rjust(12) + theprecip[2].rjust(12) + theprecip[3].rjust(12) + theprecip[4].rjust(12) + '\n'
            if (n+1)%5 > 0:
                for i in range((n+1)%5):
                    pythonaddins.MessageBox("test", "4")
                    j = int((n+1)/5) * 5 + i
                    theprecip = p[j]/pdist[imax]
                    theprecip = str(round(theprecip, 4))
                    outfile2_str = outfile2_str + theprecip + "      "
            outfile2_str = outfile2_str + '\n'
            pythonaddins.MessageBox("test", "5")

    stormout1 = outfile1
    stormout11 = open(stormout1, "w")
    stormout11.write(outfile1_str)
    stormout11.close()

    stormout2 = outfile2
    stormout22 = open(stormout2, "w")
    stormout22.write(outfile2_str)
    stormout22.close()





