Esempio n. 1
0
def createRadDataTable(event,
                       startYear,
                       startYday,
                       stopYear,
                       stopYday,
                       comp_test='NA'):

    "for a given event, interruption startYear, startYday, stopYear, stopYday, create a radiation data table <event>_dat.txt in data directory."

    if comp_test == 'test':
        out_name = test_data_dir + event + '_dat.txt'
    else:
        out_name = data_dir + event + '_dat.txt'

    out = open(out_name, 'w')

    #
    #--- set data collecting period
    #

    (pYearStart, periodStart, pYearStop, periodStop, plotYearStart, plotStart, plotYearStop, plotStop, pannelNum) \
                = itrf.findCollectingPeriod(startYear, startYday, stopYear, stopYday)
    #
    #--- start printing out the database
    #

    out.write('Science Run Interruption: $time\n\n')
    out.write(
        'dofy\telectron38\telectron175\tprotont47\tproton112\tproton310\tproton761\tproton1060\taniso\n'
    )
    out.write(
        '---------------------------------------------------------------------------------------------------------------------------\n'
    )
    out.write('\n')

    if pYearStart == pYearStop:
        printACEData(pYearStart, periodStart, periodStop, out)

    else:
        #
        #--- for the case, the data collectiing period goes over two years
        #
        printACEData(pYearStart, periodStart, base, out)
        printACEData(pYearStop, 1, periodStop, out)

    out.close()
Esempio n. 2
0
def createRadDataTable(event, startYear, startYday, stopYear, stopYday, comp_test = 'NA'):


    "for a given event, interruption startYear, startYday, stopYear, stopYday, create a radiation data table <event>_dat.txt in data directory."

    if comp_test == 'test':
        out_name = test_data_dir + event + '_dat.txt'
    else:
        out_name = data_dir + event + '_dat.txt'

    out = open(out_name, 'w')

#
#--- set data collecting period
#

    (pYearStart, periodStart, pYearStop, periodStop, plotYearStart, plotStart, plotYearStop, plotStop, pannelNum) \
                = itrf.findCollectingPeriod(startYear, startYday, stopYear, stopYday)
#
#--- start printing out the database
#

    out.write('Science Run Interruption: $time\n\n')
    out.write('dofy\telectron38\telectron175\tprotont47\tproton112\tproton310\tproton761\tproton1060\taniso\n')
    out.write('---------------------------------------------------------------------------------------------------------------------------\n')
    out.write('\n')

    if pYearStart == pYearStop:
        printACEData(pYearStart, periodStart, periodStop, out)

    else:
#
#--- for the case, the data collectiing period goes over two years
#
        printACEData(pYearStart, periodStart, base,       out)
        printACEData(pYearStop,  1,           periodStop, out)

    out.close()
Esempio n. 3
0
def ephinDataExtract(event, start, stop, comp_test='NA'):
    """
    extract EPIN related quantities and creates a data table 
    input:  event   --- event name (e.g.20170911)
            start   --- starting time   (format: 2012:03:13:22:41) 
            stop    --- stopping time   (format: 2012:03:13:22:41)
    output: <data_dir>/<event>_eph.txt
    """
    begin = start + ':00'  #---- to use dateFormatCon correctly, need to add "sec" part
    end = stop + ':00'
    #
    #--- convert time format
    #
    (year1, month1, day1, hours1, minutes1, seconds1,
     ydate1) = tcnv.dateFormatCon(begin)
    (year2, month2, day2, hours2, minutes2, seconds2,
     ydate2) = tcnv.dateFormatCon(end)
    #
    #--- change time format and find data collecting period (starts 2 days before the interruption
    #--- and ends at least 5 days after the stating)
    #
    (pYearStart, periodStart, pYearStop, periodStop, plotYearStart, plotStart,\
     plotYearStop, plotStop, pannelNum) = itrf.findCollectingPeriod(year1, ydate1, year2, ydate2)
    #
    #--- read ephin data using arc5gl
    #
    ephinList = itrf.useArc5gl('retrieve', 'flight', 'ephin', 1, 'ephrates', pYearStart,\
                                periodStart, pYearStop, periodStop, deposit='./Working_dir/')
    #
    #--- extract needed data
    #
    xdate = []
    p4 = []
    p41 = []
    e150 = []
    e1300 = []
    ecnt = 0

    for fits in ephinList:
        mc = re.search('fits.gz', fits)
        if mc is None:
            continue
#
#--- use dmlist
#
        infile = './Working_dir/' + fits
        [tcols, tbdata] = itrf.read_fits_file(infile)
        etime = list(tbdata.field('time'))

        xdate = xdate + convert_to_ydate(etime, pYearStart)

        if pYearStart < 2011:
            p4 = p4 + list(tbdata.field('scp4'))
            p41 = p41 + list(tbdata.field('scp41'))
        else:
            e150 = e150 + list(tbdata.field('sce150'))

        e1300 = e1300 + list(tbdata.field('sce1300'))

    ecnt = len(e1300)
    os.system('rm ./Working_dir/*fits*')

    #
    #--- using DataSeeker, extread HRC sheild rate (only if year > 2011)
    #
    if pYearStart >= 2011:

        [stime, veto] = itrf.useDataSeeker(pYearStart, periodStart, pYearStop,
                                           periodStop, 'shevart')
        #
        #--- converrt time format into day of year
        #
        time = []
        for ent in stime:
            ttime = tcnv.convertCtimeToYdate(float(ent))
            temp = re.split(':', ttime)
            year = int(temp[0])
            dofy = float(temp[1]) + float(temp[2]) / 24 + float(
                temp[3]) / 1440 + float(temp[4]) / 86400
            time.append(dofy)

        hcnt = len(time)
        #
        #--- matching timing between electron data and hrc data
        #
        hrc = len(e150) * [0]
        j = 0
        k = 0

        if isLeapYear(pYearStart) == 1:
            base = 366
        else:
            base = 365
#
#--- find the begining
#
        if time[0] < xdate[0]:
            while time[j] < xdate[0]:
                j += 1
                if j >= hcnt:
                    print "Time span does not overlap. Abort the process."
                    exit(1)

        elif time[0] > xdate[0]:
            while time[0] > xdate[k]:
                k += 1
                if k >= ecnt:
                    print "Time span does not overlap. Abort the process."
                    exit(1)

        hrc[k] = veto[j]

        tspace = 1.38888888888e-3 / base  #--- setting timing bin size: base is given in hrc loop

        for i in range(k + 1, ecnt):
            tbeg = xdate[i] - tspace
            tend = xdate[i] + tspace

            if j > hcnt - 2:
                #
                #--- if the hrc data runs out, just repeat the last data point value
                #
                hrc[i] = veto[hcnt - 1]

            elif time[j] >= tbeg and time[j] <= tend:
                hrc[i] = veto[j]

            elif time[j] < tbeg:
                while time[j] < tbeg:
                    j += 1
                hrc[i] = veto[j]

            elif time[j] > tend:
                while time[j] > tend:
                    j -= 1
                hrc[i] = veto[j]
#
#--- print out data
#
    line = 'Science Run Interruption: ' + start + '\n\n'

    if pYearStart < 2011:
        line = line + 'dofy\t\tp4\t\t\tp41\t\t\te1300\n'
        line = line + '-------------------------------------------------------------------\n'

        for m in range(0, ecnt):
            line = line + '%4.3f\t\t%4.3e\t%4.3e\t%4.3e\n'\
                        % (float(xdate[m]), float(p4[m]), float(p41[m]),  float(e1300[m]))
    else:
        line = line + 'dofy\t\thrc\t\te150\t\te1300\n'
        line = line + '-------------------------------------------------------------------\n'

        for m in range(0, ecnt):
            line = line +  '%4.3f\t\t%4.3e\t%4.3e\t%4.3e\n'\
                    % (float(xdate[m]), float(hrc[m]), float(e150[m]),  float(e1300[m]))

    if comp_test == 'test':
        file = test_data_dir + event + '_eph.txt'
    else:
        file = data_dir + event + '_eph.txt'

    f = open(file, 'w')
    f.write(line)
    f.close()
Esempio n. 4
0
def plotGOESMain(event, start, stop, comp_test='NA'):

    'read GOES data from data_dir and plot them. Input: event, interruption start and stop time (e.g. 20120313        2012:03:13:22:41        2012:03:14:13:57)'

    #
    #--- read radiation zone information
    #
    radZone = ptrf.readRadZone(event)

    #
    #--- read GOES data
    #

    if comp_test == 'test':
        file = test_data_dir + event + '_goes.txt'
        plot_out = test_goes_dir
    else:
        file = data_dir + event + '_goes.txt'
        plot_out = goes_dir

    f = open(file, 'r')
    data = [line.strip() for line in f.readlines()]
    f.close()

    dofy = []
    p1 = []
    p2 = []
    p5 = []
    dcnt = 0

    for ent in data:
        atemp = re.split('\s+|\t+', ent)
        btemp = re.split('\.', atemp[0])
        if ent and btemp[0].isdigit():

            dofy.append(atemp[0])

            val1 = float(atemp[1])
            if val1 <= 0:
                val1 = 1e-5

            val2 = float(atemp[2])
            if val2 <= 0:
                val2 = 1e-5

            val3 = float(atemp[3])
            if val3 <= 0:
                val3 = 1e-5

            p1.append(math.log10(val1))
            p2.append(math.log10(val2))
            p5.append(math.log10(val3))

#
#--- modify date formats
#
    begin = start + ':00'
    (year1, month1, date1, hours1, minutes1, seconds1, ydate1, dom1,
     sectime1) = tcnv.dateFormatConAll(begin)
    end = stop + ':00'
    (year2, month2, date2, hours2, minutes2, seconds2, ydate2, dom2,
     sectime2) = tcnv.dateFormatConAll(end)

    #
    #--- find plotting range
    #

    (pYearStart, periodStart, pYearStop, periodStop, plotYearStart, plotStart, plotYearStop, plotStop, pannelNum) \
                 = itrf.findCollectingPeriod(year1, ydate1, year2, ydate2)

    #
    #--- if the interuption go over two years, adjust the ending ydate to that of the previous year
    #
    if year2 > year1:
        chk = 4.0 * int(0.25 * year1)
        if chk == year1:
            base = 366
        else:
            base = 365

        ydate2 += base

#
#--- plot data
#
    if pannelNum == 1:
        plotGOES(dofy, p1, p2, p5, ydate1, ydate2, plotStart, plotStop,
                 radZone)
        cmd = 'mv ./out.png ' + plot_out + event + '_goes.png'
        os.system(cmd)
#
#--- if the interruption period cannot be covered by one plotting panel, create as many panels as we need to cover the period.
#
    else:
        pstart = plotStart
        prange = pannelNum + 1
        for i in range(1, prange):
            pend = pstart + 5
            if i == 1:
                plotGOES(dofy, p1, p2, p5, ydate1, 'NA', pstart, pend, radZone)
                cmd = 'mv ./out.png ' + plot_out + event + '_goes.png'
                os.system(cmd)
            elif i == pannelNum:
                plotGOES(dofy, p1, p2, p5, 'NA', ydate2, pstart, pend, radZone)
                cmd = 'mv ./out.png ' + plot_out + event + '_goes_pt' + str(
                    i) + '.png'
                os.system(cmd)
            else:
                plotGOES(dofy, p1, p2, p5, 'NA', 'NA', pstart, pend, radZone)
                cmd = 'mv ./out.png ' + plot_out + event + '_goes_pt' + str(
                    i) + '.png'
                os.system(cmd)
            pstart = pend
Esempio n. 5
0
def extractGOESData(event, start, stop, comp_test='NA'):
    """
    Extract GOES data from NOAA site, and create a locat data base. 
    input:  event       --- event name
            start       --- starting time
            stop        --- stopping time
                (e.g., 20120313        2012:03:13:22:41        2012:03:14:13:57'
           comp_test    --- option; if given, testing 
                            (if comp_test == test, the test data will be read)
    output: <data_dir>/<event>_goes.txt
            <stat_dir>/<event>_geos_stat
    """

    #
    #--- modify date formats
    #
    begin = start + ':00'
    (year1, month1, date1, hours1, minutes1, seconds1, ydate1, dom1,
     sectime1) = tcnv.dateFormatConAll(begin)
    end = stop + ':00'
    (year2, month2, date2, hours2, minutes2, seconds2, ydate2, dom2,
     sectime2) = tcnv.dateFormatConAll(end)

    #
    #--- find plotting range
    #

    (pYearStart, periodStart, pYearStop, periodStop, plotYearStart, plotStart, plotYearStop, plotStop, pannelNum) \
                 = itrf.findCollectingPeriod(year1, ydate1, year2, ydate2)
    #
    #--- reformat plotting start / stop time
    #
    (year3, month3, date3, hours3, minutes3, seconds3, ydate3, dom3,
     sectime3) = tcnv.dateFormatConAll(pYearStart, periodStart)
    (year4, month4, date4, hours4, minutes4, seconds4, ydate4, dom4,
     sectime4) = tcnv.dateFormatConAll(pYearStop, periodStop)

    #
    #--- set input data type: old if pre 2011, otherwise new
    #--- the first two is from http://goes.ngdc.noaa.gov/data/avg/ and the 'new' one is from http://www.swpc.noaa.gov/ftpdir/lists/pchan/
    #--- although they all use p1, p2, and p5, the value are not compatible.
    #
    if year3 <= 2009:
        dtype = 'G105'
    elif year3 < 2011:
        dtype = 'G115'
    else:
        dtype = 'new'

#
#--- create a list of html address from which we extract GOES data
#
    htmlList = []
    #
    #--- for the starting year and the ending year is same
    #
    if year3 == year4:
        #
        #--- for the case the starting month and ending month is same
        #
        if month3 == month4:
            tmon = str(month3)
            if month3 < 10:
                tmon = '0' + tmon

            for tday in range(date3, date4 + 1):
                if tday < 10:
                    tday = '0' + str(tday)
                else:
                    tday = str(tday)

                timeStamp = str(year3) + tmon + tday
                #
                #--- after 2012, the data are obtained from different site. data are largely missing
                #--- 2011, and previous to that we have all record at ngdc site
                #
                if dtype == 'new':
                    #                    html  = 'http://www.swpc.noaa.gov/ftpdir/lists/pchan/' + timeStamp + '_Gp_pchan_5m.txt'
                    html = '/data/mta4/www/DAILY/mta_rad/GOES/' + timeStamp + '_Gp_pchan_5m.txt'
                else:
                    syear = str(year3)
                    html = 'http://goes.ngdc.noaa.gov/data/avg/' + str(
                        year3
                    ) + '/' + dtype + syear[2] + syear[3] + tmon + '.TXT'

                htmlList.append(html)

        else:
            #
            #---- for the case, the period goes over two months
            #
            if month3 == 2:
                chk = 4.0 * int(0.25 * year3)
                if chk == year3:
                    endDate = 29
                else:
                    endDate = 28
            elif month3 == 1 or month3 == 3 or month3 == 5 or month3 == 7 or month3 == 8 or month3 == 10:
                endDate = 31
            else:
                endDate = 30

            tmon = str(month3)
            if month3 < 10:
                tmon = '0' + tmon

            for tday in range(date3, endDate + 1):
                timeStamp = str(year3) + tmon + str(tday)
                if dtype == 'new':
                    html = '/data/mta4/www/DAILY/mta_rad/GOES/' + timeStamp + '_Gp_pchan_5m.txt'
                else:
                    syear = str(year3)
                    html = 'http://goes.ngdc.noaa.gov/data/avg/' + str(year3)
                    html = html + '/' + dtype + syear[2] + syear[
                        3] + tmon + '.TXT'

                htmlList.append(html)

            tmon = str(month4)
            if month4 < 10:
                tmon = '0' + tmon

            for tday in range(1, date4 + 1):
                if tday < 10:
                    tday = '0' + str(tday)
                else:
                    tday = str(tday)

                timeStamp = str(year3) + tmon + tday
                if dtype == 'new':
                    html = '/data/mta4/www/DAILY/mta_rad/GOES/' + timeStamp + '_Gp_pchan_5m.txt'
                else:
                    syear = str(year3)
                    html = 'http://goes.ngdc.noaa.gov/data/avg/' + str(
                        year3) + '/'
                    html = html + dtype + syear[2] + syear[3] + tmon + '.TXT'

                htmlList.append(html)
    else:
        #
        #--- for the case the period goes over two years
        #
        for tday in range(date3, 32):
            timeStamp = str(year3) + tmon + str(tday)
            if dtype == 'new':
                html = '/data/mta4/www/DAILY/mta_rad/GOES/' + timeStamp + '_Gp_pchan_5m.txt'
            else:
                syear = str(year3)
                html = 'http://goes.ngdc.noaa.gov/data/avg/' + str(year3) + '/'
                html = html + dtype + syear[2] + syear[3] + tmon + '.TXT'

            htmlList.append(html)

        for tday in range(1, date4 + 1):
            if tday < 10:
                tday = '0' + str(tday)
            else:
                tday = str(tday)

            timeStamp = str(year4) + tmon + tday
            if dtype == 'new':
                html = '/data/mta4/www/DAILY/mta_rad/GOES/' + timeStamp + '_Gp_pchan_5m.txt'
            else:
                syear = str(year3)
                html = 'http://goes.ngdc.noaa.gov/data/avg/' + str(year4) + '/'
                html = html + dtype + syear[2] + syear[3] + tmon + '.TXT'

            htmlList.append(html)
#
#--- prepare to print out data
#
    if comp_test == 'test':
        ofile = test_data_dir + event + '_goes.txt'
    else:
        ofile = data_dir + event + '_goes.txt'

    out = open(ofile, 'w')
    line = 'Science Run Interruption: ' + str(start) + '\n\n'
    out.write(line)
    out.write('dofy\t\tp1\t\t\tp2\t\t\tp5\n')
    out.write(
        "-------------------------------------------------------------------\n"
    )
    #
    #--- now extract data from NOAA web site
    #
    for html in htmlList:
        f = open(html, 'r')
        data = [line.strip() for line in f.readlines()]
        f.close()
        #
        #--- extract needed data and print them out in a data file
        #

        #
        #--- if data are collected after 2011, start here
        #

        if dtype == 'new':
            for ent in data:
                atemp = re.split('\s+|\t+', ent)

                if ent and atemp[0].isdigit():
                    timestamp = atemp[0] + ':' + atemp[1] + ':' + atemp[
                        2] + ':' + atemp[3][0]
                    timestamp = timestamp + atemp[3][1] + ':' + atemp[3][
                        2] + atemp[3][3] + ':00'

                    (dyear, dmonth, dday, dhours, dminutes, dseconds,
                     dydate) = tcnv.dateFormatCon(timestamp)

                    if dyear == pYearStart:
                        if dydate >= plotStart and dydate <= plotStop:

                            line = '%4.3f\t\t%3.2e\t%3.2e\t%3.2e\n' %\
                                   (dydate, float(atemp[6]), float(atemp[7]), float(atemp[10]))
                            out.write(line)
                    else:
                        #
                        #--- for the case, the period goes over two years
                        #
                        chk = 4.0 * int(0.25 * pYearStart)
                        if chk == pYearStart:
                            base = 366
                        else:
                            base = 365

                        dydate += base
                        if dydate >= plotStart and dydate <= plotStop:

                            line = '%4.3f\t\t%3.2e\t%3.2e\t%3.2e\n' %\
                                    (dydate, float(atemp[6]), float(atemp[7]), float(atemp[10]))
                            out.write(line)
#
#--- if the data is collected before 2011, start here
#
        else:
            for ent in data:
                atemp = re.split('\s+|\t+', ent)

                if ent and atemp[0].isdigit():
                    dyear = atemp[0][0] + atemp[0][1]
                    dyear = int(dyear)
                    if dyear > 90:
                        dyear += 1900
                    else:
                        dyear += 2000

                    dydate = float(atemp[2])
                    dhours = float(atemp[1][0] + atemp[1][1])
                    dminutes = float(atemp[1][2] + atemp[1][3])
                    dydate += (dhours / 24.0 + dminutes / 1440.0)

                    if dyear == pYearStart:
                        if dydate >= plotStart and dydate <= plotStop:

                            line = '%4.3f\t\t%3.2e\t%3.2e\t%3.2e\n' %\
                                    (dydate, float(atemp[10]), float(atemp[11]), float(atemp[14]))
                            out.write(line)
                    else:
                        #
                        #--- for the case, the period goes over two years
                        #
                        chk = 4.0 * int(0.25 * pYearStart)
                        if chk == pYearStart:
                            base = 366
                        else:
                            base = 365

                        dydate += base
                        dydate += base
                        if dydate >= plotStart and dydate <= plotStop:

                            line = '%4.3f\t\t%3.2e\t%3.2e\t%3.2e\n' %\
                                    (dydate, float(atemp[10]), float(atemp[11]), float(atemp[14]))
                            out.write(line)

    if len(htmlList) > 0:
        out.close()
Esempio n. 6
0
def plotEphinMain(event, start, stop, comp_test='NA'):

    'read Ephin data from data_dir and plot them. Input: event, interruption start and stop time (e.g. 20120313        2012:03:13:22:41        2012:03:14:13:57)'

    #
    #--- read radiation zone information
    #
    radZone = ptrf.readRadZone(event)

    #
    #--- read EPHIN data
    #

    if comp_test == 'test':
        file = test_data_dir + event + '_eph.txt'
        plot_out = test_ephin_dir
    else:
        file = data_dir + event + '_eph.txt'
        plot_out = ephin_dir

    f = open(file, 'r')
    data = [line.strip() for line in f.readlines()]
    f.close()

    dofy = []
    prtn1 = []
    prtn2 = []
    prtn3 = []
    dcnt = 0
    dataset = 0

    for ent in data:
        atemp = re.split('\s+|\t+', ent)
        btemp = re.split('\.', atemp[0])
        if ent and btemp[0].isdigit():
            dofy.append(atemp[0])

            val1 = float(atemp[1])
            if val1 <= 0:
                val1 = 1e-5

            val2 = float(atemp[2])
            if val2 <= 0:
                val2 = 1e-5

            val3 = float(atemp[3])
            if val3 <= 0:
                val3 = 1e-5

            prtn1.append(math.log10(val1))
            prtn2.append(math.log10(val2))
            prtn3.append(math.log10(val3))
        else:
            #
            #--- checking which data set, old one: p4, p41, e1300. new one: hrc, e150, e1300
            #
            m = re.search('hrc', ent)
            if m is not None:
                dataset = 1

#
#--- modify date formats
#
    begin = start + ':00'
    (year1, month1, date1, hours1, minutes1, seconds1, ydate1, dom1,
     sectime1) = tcnv.dateFormatConAll(begin)
    end = stop + ':00'
    (year2, month2, date2, hours2, minutes2, seconds2, ydate2, dom2,
     sectime2) = tcnv.dateFormatConAll(end)

    #
    #--- find plotting range
    #

    (pYearStart, periodStart, pYearStop, periodStop, plotYearStart, plotStart, plotYearStop, plotStop, pannelNum) \
                 = itrf.findCollectingPeriod(year1, ydate1, year2, ydate2)

    #
    #--- if the interuption go over two years, adjust the ending ydate to that of the previous year
    #
    if year2 > year1:
        chk = 4.0 * int(0.25 * year1)
        if chk == year1:
            base = 366
        else:
            base = 365

        ydate2 += base

#
#--- plot data
#
    if pannelNum == 1:
        if year1 < 2014:
            plotEphin(dofy, prtn1, prtn2, prtn3, ydate1, ydate2, plotStart,
                      plotStop, radZone, dataset)
        else:
            plotEphin(dofy, prtn1, 'NA', 'NA', ydate1, ydate2, plotStart,
                      plotStop, radZone, dataset)
        cmd = 'mv ./out.png ' + plot_out + event + '_eph.png'
        os.system(cmd)
#
#--- if the interruption period cannot be covered by one plotting panel, create as many panels as we need to cover the period.
#
    else:
        pstart = plotStart
        prange = pannelNum + 1
        if year1 < 2014:
            for i in range(1, prange):
                pend = pstart + 5
                if i == 1:
                    plotEphin(dofy, prtn1, prtn2, prtn3, ydate1, 'NA', pstart,
                              pend, radZone, dataset)
                    cmd = 'mv ./out.png ' + plot_out + event + '_eph.png'
                    os.system(cmd)
                elif i == pannelNum:
                    plotEphin(dofy, prtn1, prtn2, prtn3, 'NA', ydate2, pstart,
                              pend, radZone, dataset)
                    cmd = 'mv ./out.png ' + plot_out + event + '_eph_pt' + str(
                        i) + '.png'
                    os.system(cmd)
                else:
                    plotEphin(dofy, prtn1, prtn2, prtn3, 'NA', 'NA', pstart,
                              pend, radZone, dataset)
                    cmd = 'mv ./out.png ' + plot_out + event + '_eph_pt' + str(
                        i) + '.png'
                    os.system(cmd)
                pstart = pend

        else:
            for i in range(1, prange):
                pend = pstart + 5
                if i == 1:
                    plotEphin(dofy, prtn1, 'NA', 'NA', ydate1, 'NA', pstart,
                              pend, radZone, dataset)
                    cmd = 'mv ./out.png ' + plot_out + event + '_eph.png'
                    os.system(cmd)
                elif i == pannelNum:
                    plotEphin(dofy, prtn1, 'NA', 'NA', 'NA', ydate2, pstart,
                              pend, radZone, dataset)
                    cmd = 'mv ./out.png ' + plot_out + event + '_eph_pt' + str(
                        i) + '.png'
                    os.system(cmd)
                else:
                    plotEphin(dofy, prtn1, 'NA', 'NA', 'NA', 'NA', pstart,
                              pend, radZone, dataset)
                    cmd = 'mv ./out.png ' + plot_out + event + '_eph_pt' + str(
                        i) + '.png'
                    os.system(cmd)
                pstart = pend

#
#--- plot intro page
#
    pend = plotStart + 5
    if year1 < 2014:
        plotIntro(dofy, prtn2, ydate1, ydate2, plotStart, pend, radZone,
                  dataset, year1)
    else:
        plotIntro(dofy, prtn1, ydate1, ydate2, plotStart, pend, radZone,
                  dataset, year1)
    cmd = 'mv ./intro_out.png ' + intro_dir + event + '_intro.png'
    os.system(cmd)
def printEachHtml(event, start, stop, gap, stopType):

    'create indivisual event html page. input event name, interruption start/stop time, gap, and type (auto/manual): example: 20031202        2003:12:02:17:31        2003:12:04:14:27        139.8   auto'

#
#--- modify date formats
#   
    begin = start + ':00'
    (year1, month1, date1, hours1, minutes1, seconds1, ydate1, dom1, sectime1) = tcnv.dateFormatConAll(begin)
    end   = stop  + ':00'
    (year2, month2, date2, hours2, minutes2, seconds2, ydate2, dom2, sectime2) = tcnv.dateFormatConAll(end)

#
#--- find plotting range
#

    (pYearStart, periodStart, pYearStop, periodStop, plotYearStart, plotStart, plotYearStop, plotStop, pannelNum) \
                 = itrf.findCollectingPeriod(year1, ydate1, year2, ydate2)

#
#--- check whether we need multiple pannels
#
    pannelNum  = int((plotStop - plotStart) / 5)

#
#--- choose a template
#
    atemp = re.split(':', start)
    year  = int(atemp[0])
    if year < 2011:
        file = house_keeping + 'sub_html_template'
    elif year < 2014:
        file = house_keeping + 'sub_html_template_2011'
    else:
        file = house_keeping + 'sub_html_template_2014'

#
#--- read the template and start substituting 
#

    data = open(file).read()

    data = re.sub('#header_title#',  event,    data)
    data = re.sub('#main_title#',    event,    data)
    data = re.sub('#sci_run_stop#',  start,    data)
    data = re.sub('#sci_run_start#', stop,     data)
    data = re.sub('#interruption#',  gap,      data)
    data = re.sub('#trigger#',       stopType, data)

    noteN = event + '.txt'
    data = re.sub('#note_name#',     noteN,  data)

#
#--- ACA (NOAA) radiation data
#

    aceData = event + '_dat.txt'
    data = re.sub('#ace_data#',     aceData, data)

    file = stat_dir + event + '_ace_stat'
    stat = open(file).read()
    data = re.sub('#ace_table#',    stat,    data)

    line =  event + '.png"'
    for i in range(2, pannelNum+1):
        padd = ' alt="main plot" style="width:100%">\n<br />\n<img src = "../Main_plot/' + event + '_pt' + str(i) + '.png '
	line = line + padd

    data = re.sub('#ace_plot#', line , data)

#
#---EPHIN data
#

    ephData = event + '_eph.txt'
    data = re.sub('#eph_data#', ephData, data)

    file = stat_dir + event + '_ephin_stat'
    stat = open(file).read()
    data = re.sub('#eph_table#',    stat,    data)

    line =  event + '_eph.png"'
    for i in range(2, pannelNum+1):
        padd = ' alt="eph plot" style="width:100%">\n<br />\n<img src = "../Ephin_plot/' + event + '_eph_pt' + str(i) + '.png '
    line = line + padd

    data = re.sub('#eph_plot#', line , data)

#
#---GOES data
#

    goesData = event + '_goes.txt'
    data = re.sub('#goes_data#', goesData, data)

    file = stat_dir + event + '_goes_stat'
    stat = open(file).read()
    data = re.sub('#goes_table#',    stat,    data)

    line =  event + '_goes.png"'
    for i in range(2, pannelNum+1):
        padd = ' alt="goes plot" style="width:100%"> \n<br />\n<img src = "../GOES_plot/' + event + '_goes_pt' + str(i) + '.png '
	line = line + padd

    data = re.sub('#goes_plot#', line , data)

    if year1 >= 2011:
        data = re.sub('GOES-11', 'GOES-15', data)


    file = web_dir + 'Html_dir/' + event + '.html'
    f    = open(file, 'w')
    f.write(data)
    f.close()
Esempio n. 8
0
def printEachHtml(event, start, stop, gap, stopType):
    """
    create indivisual event html page. 
    input:  event       ---   event name
            start       --- start time
            stop        --- stop time
            gap         --- aount of interrution in sec
            stopType    --- auto/manual
    
    example: 20031202        2003:12:02:17:31        2003:12:04:14:27        139.8   auto
    output: html pages
    """
#
#--- modify date formats
#   
    begin = start + ':00'
    (year1, month1, date1, hours1, minutes1, seconds1, ydate1, dom1, sectime1) = tcnv.dateFormatConAll(begin)

    end   = stop  + ':00'
    (year2, month2, date2, hours2, minutes2, seconds2, ydate2, dom2, sectime2) = tcnv.dateFormatConAll(end)
#
#--- find plotting range
#
    (pYearStart, periodStart, pYearStop, periodStop, plotYearStart,\
     plotStart, plotYearStop, plotStop, pannelNum)  = itrf.findCollectingPeriod(year1, ydate1, year2, ydate2)
#
#--- check whether we need multiple pannels
#
    pannelNum  = int((plotStop - plotStart) / 5)
#
#--- choose a template
#
    atemp = re.split(':', start)
    year  = int(atemp[0])
    if year < 2011:
        file = house_keeping + 'sub_html_template'
    elif year < 2014:
        file = house_keeping + 'sub_html_template_2011'
    else:
        file = house_keeping + 'sub_html_template_2014'
#
#--- read the template and start substituting 
#
    data = open(file).read()

    data = re.sub('#header_title#',  event,    data)
    data = re.sub('#main_title#',    event,    data)
    data = re.sub('#sci_run_stop#',  start,    data)
    data = re.sub('#sci_run_start#', stop,     data)
    data = re.sub('#interruption#',  gap,      data)
    data = re.sub('#trigger#',       stopType, data)

    noteN = event + '.txt'
    data = re.sub('#note_name#',     noteN,  data)
#
#--- ACA (NOAA) radiation data
#
    aceData = event + '_dat.txt'
    data = re.sub('#ace_data#',     aceData, data)

    file = stat_dir + event + '_ace_stat'
    try:
        stat = open(file).read()
        data = re.sub('#ace_table#',    stat,    data)
    
        line =  event + '.png"'
        for i in range(2, pannelNum+1):
            padd = ' alt="main plot" style="width:100%">\n<br />\n<img src = "../Main_plot/' 
            padd = padd + event + '_pt' + str(i) + '.png '
            line = line + padd
    
        data = re.sub('#ace_plot#', line , data)
    except:
        pass
#
#---EPHIN data
#

    ephData = event + '_eph.txt'
    data = re.sub('#eph_data#', ephData, data)

    file = stat_dir + event + '_ephin_stat'
    try:
        stat = open(file).read()
        data = re.sub('#eph_table#',    stat,    data)
    
        line =  event + '_eph.png"'
        for i in range(2, pannelNum+1):
            padd = ' alt="eph plot" style="width:100%">\n<br />\n<img src = "../Ephin_plot/' 
            padd = padd + event + '_eph_pt' + str(i) + '.png '
            line = line + padd
    
        data = re.sub('#eph_plot#', line , data)
    except:
        pass
#
#---GOES data
#
    goesData = event + '_goes.txt'
    data = re.sub('#goes_data#', goesData, data)

    file = stat_dir + event + '_goes_stat'
    try:
        stat = open(file).read()
        data = re.sub('#goes_table#',    stat,    data)
    
        line =  event + '_goes.png"'
        for i in range(2, pannelNum+1):
            padd = ' alt="goes plot" style="width:100%"> \n<br />\n<img src = "../GOES_plot/' 
            padd = padd + event + '_goes_pt' + str(i) + '.png '
            line = line + padd
    
        data = re.sub('#goes_plot#', line , data)
    
        if year1 >= 2011:
            data = re.sub('GOES-11', 'GOES-15', data)
    except:
        pass
#
#---XMM data
#
    xmmData = event + '_xmm.txt'
    data = re.sub('#xmm_data#', xmmData, data)

    file = stat_dir + event + '_xmm_stat'
    try:
        stat = open(file).read()
        data = re.sub('#xmm_table#',    stat,    data)
    
        line =  event + '_xmm.png"'
        for i in range(2, pannelNum+1):
            padd = ' alt="xmm plot" style="width:100%"> \n<br />\n<img src = "../XMM_plot/' 
            padd = padd + event + '_xmm_pt' + str(i) + '.png '
            line = line + padd
    
        data = re.sub('#xmm_plot#', line , data)
    except:
        pass
#
#--- ACIS
#
    fin = open('./acis_list', 'r')
    alist = [line.strip() for line in fin.readlines()]
    fin.close()
    for ent in alist:
        mc = re.search(event, ent)
        if mc is not None:
            atemp = re.split('::', ent)
            blist = re.split(':', atemp[1])
            break
    k = 0
    aline = ''
    for ent in blist:
        aline = aline + "<img src='http://acis.mit.edu/asc/txgif/gifs/"
        aline = aline + ent + ".gif' style='width:45%; padding-bottom:30px;'>\n"
        k += 1
        if k % 2 == 0:
            aline = aline + '<br />\n'

    data = re.sub('#acis_plot#', aline, data)
#
#--- print the page
#
    file = web_dir + 'Html_dir/' + event + '.html'
    f    = open(file, 'w')
    f.write(data)
    f.close()
Esempio n. 9
0
def plotGOESMain(event, start, stop, comp_test='NA'):

    'read GOES data from data_dir and plot them. Input: event, interruption start and stop time (e.g. 20120313        2012:03:13:22:41        2012:03:14:13:57)'

#
#--- read radiation zone information
#
    radZone = ptrf.readRadZone(event)

#
#--- read GOES data
#

    if comp_test == 'test':
        file = test_data_dir + event + '_goes.txt'
        plot_out = test_goes_dir
    else:
        file = data_dir + event + '_goes.txt'
        plot_out = goes_dir

    f    = open(file, 'r')
    data = [line.strip() for line in f.readlines()]
    f.close()

    dofy  = []
    p1    = []
    p2    = []
    p5    = []
    dcnt  = 0

    for ent in data:
        atemp = re.split('\s+|\t+', ent)
        btemp = re.split('\.', atemp[0])
        if ent and btemp[0].isdigit():

            dofy.append(atemp[0])

            val1 = float(atemp[1])
            if val1 <= 0:
                val1 = 1e-5

            val2 = float(atemp[2])
            if val2 <= 0:
                val2 = 1e-5

            val3 = float(atemp[3])
            if val3 <= 0:
                val3 = 1e-5

            p1.append(math.log10(val1))
            p2.append(math.log10(val2))
            p5.append(math.log10(val3))

#
#--- modify date formats
#
    begin = start + ':00'
    (year1, month1, date1, hours1, minutes1, seconds1, ydate1, dom1, sectime1) = tcnv.dateFormatConAll(begin)
    end   = stop  + ':00'
    (year2, month2, date2, hours2, minutes2, seconds2, ydate2, dom2, sectime2) = tcnv.dateFormatConAll(end)

#
#--- find plotting range
#

    (pYearStart, periodStart, pYearStop, periodStop, plotYearStart, plotStart, plotYearStop, plotStop, pannelNum) \
                 = itrf.findCollectingPeriod(year1, ydate1, year2, ydate2)

#
#--- if the interuption go over two years, adjust the ending ydate to that of the previous year
#
    if year2 > year1:
        chk = 4.0 * int(0.25 * year1)
        if chk == year1:
            base = 366
        else:
            base = 365

        ydate2 += base

#
#--- plot data
#
    if pannelNum == 1:
        plotGOES(dofy, p1, p2, p5, ydate1, ydate2, plotStart, plotStop, radZone)
        cmd = 'mv ./out.png ' + plot_out + event + '_goes.png'
        os.system(cmd)
#
#--- if the interruption period cannot be covered by one plotting panel, create as many panels as we need to cover the period.
#
    else:
        pstart = plotStart
        prange = pannelNum + 1
        for i in range(1, prange):
            pend = pstart + 5
            if i == 1:
                plotGOES(dofy, p1, p2, p5, ydate1, 'NA', pstart, pend, radZone)
                cmd = 'mv ./out.png ' + plot_out + event + '_goes.png'
                os.system(cmd)
            elif i == pannelNum:
                plotGOES(dofy, p1, p2, p5, 'NA', ydate2, pstart, pend, radZone)
                cmd = 'mv ./out.png ' + plot_out + event + '_goes_pt'+ str(i) +  '.png'
                os.system(cmd)
            else:
                plotGOES(dofy, p1, p2, p5, 'NA', 'NA', pstart, pend, radZone)
                cmd = 'mv ./out.png ' + plot_out + event + '_goes_pt'+ str(i) +  '.png'
                os.system(cmd)
            pstart = pend
def ephinDataExtract(event, start, stop, comp_test='NA'):

    "extract EPIN related quantities and creates a data table for given event, start and stop time (format: 2012:03:13:22:41)"
    begin = start + ':00'  #---- to use dateFormatCon correctly, need to add "sec" part
    end = stop + ':00'

    #
    #--- convert time format
    #
    (year1, month1, day1, hours1, minutes1, seconds1,
     ydate1) = tcnv.dateFormatCon(begin)
    (year2, month2, day2, hours2, minutes2, seconds2,
     ydate2) = tcnv.dateFormatCon(end)

    #
    #--- change time format and find data collecting period (starts 2 days before the interruption and ends at least 5 days after the stating)
    #

    (pYearStart, periodStart, pYearStop, periodStop, plotYearStart, plotStart, plotYearStop, plotStop, pannelNum)   \
            = itrf.findCollectingPeriod(year1, ydate1, year2, ydate2)

    #
    #--- read ephin data using arc4gl
    #
    ephinList = itrf.useArc4gl(
        'retrieve',
        'flight',
        'ephin',
        1,
        'ephrates',
        pYearStart,
        periodStart,
        pYearStop,
        periodStop,
        deposit='./Working_dir',
    )

    #
    #--- extract needed data
    #
    xdate = []
    p4 = []
    p41 = []
    e150 = []
    e1300 = []
    ecnt = 0

    for fits in ephinList:
        #
        #--- use dmlist
        #
        if pYearStart < 2011:
            line = fits + '[cols TIME, SCP4, SCP41, SCE1300]'
        else:
            line = fits + '[cols TIME, SCE150, SCE1300]'

        cmd = 'dmlist "' + line + '" opt="data" > ./zout'
        os.system(cmd)

        f = open('./zout', 'r')
        input = [line.strip() for line in f.readlines()]
        f.close()
        os.system('rm ./zout')

        for ent in input:
            ent.lstrip()
            atemp = re.split('\s+|\t+', ent)
            if atemp[0].isdigit():
                line = tcnv.convertCtimeToYdate(float(atemp[1]))
                (year, month, day, hours, minute, second, ydate, dom,
                 sectime) = tcnv.dateFormatConAll(line)
                #
                #--- checking the case the year change
                #
                if ecnt > 0 and year != startYear:
                    ydate += base
                elif ecnt == 0:
                    startYear = year
                    chk = 4.0 * int(0.25 * year)
                    if chk == year:
                        base = 366
                    else:
                        base = 365

                xdate.append(ydate)

                if pYearStart < 2011:

                    if float(atemp[3]) == 0:
                        atemp[3] = 1.0e-4
                    if float(atemp[4]) == 0:
                        atemp[4] = 1.0e-4

                    p4.append(atemp[2])
                    p41.append(atemp[3])
                    e1300.append(atemp[4])
                else:
                    if float(atemp[2]) == 0:
                        atemp[2] = 1.0e-4
                    if float(atemp[3]) == 0:
                        atemp[3] = 1.0e-4

                    e150.append(atemp[2])
                    e1300.append(atemp[3])

                ecnt += 1

    os.system('rm ./Working_dir/*fits')

    #
    #--- using DataSeeker, extread HRC sheild rate (only if year > 2011)
    #
    if pYearStart >= 2011:

        hrcData = itrf.useDataSeeker(pYearStart, periodStart, pYearStop,
                                     periodStop, 'mtahrc..hrcveto_avg',
                                     'time,shevart_avg')

        time = []
        veto = []
        hcnt = 0

        #
        #--- there are slightly different input line format; so we need to different ways to read the line
        #
        for ent in hrcData:
            m1 = re.search('----', ent)
            m2 = re.search('ROW', ent)
            m3 = re.search('DATASEEK', ent)

            if ent and m1 == None and m2 == None and m3 == None:

                atemp = re.split('\s+|\t+', ent)

                if len(atemp) == 4:
                    ttime = tcnv.convertCtimeToYdate(float(atemp[2]))
                    temp = re.split(':', ttime)
                    year = int(temp[0])
                    dofy = float(temp[1]) + float(temp[2]) / 24 + float(
                        temp[3]) / 1440 + float(temp[4]) / 86400
                    #
                    #--- checking the case the year changes
                    #
                    if hcnt > 0 and year != startYear:
                        dofy += base
                    elif hcnt == 0:
                        startYear = year
                        chk = 4.0 * int(0.25 * year)
                        if chk == year:
                            base = 366
                        else:
                            base = 365

                    time.append(dofy)
                    veto.append(atemp[3])
                    hcnt += 1

                else:
                    ttime = tcnv.convertCtimeToYdate(float(atemp[1]))
                    temp = re.split(':', ttime)
                    year = int(temp[0])
                    dofy = float(temp[1]) + float(temp[2]) / 24 + float(
                        temp[3]) / 1440 + float(temp[4]) / 86400
                    if hcnt > 0 and year != startYear:
                        dofy += base
                    elif hcnt == 0:
                        startYear = year
                        chk = 4.0 * int(0.25 * year)
                        if chk == year:
                            base = 366
                        else:
                            base = 365

                    time.append(dofy)
                    veto.append(atemp[2])
                    hcnt += 1

#
#--- matching timing between electron data and hrc data
#
        hrc = len(e150) * [0]
        j = 0
        k = 0

        #
        #--- find the begining
        #
        if time[0] < xdate[0]:
            while time[j] < xdate[0]:
                j += 1
                if j >= hcnt:
                    print "Time span does not overlap. Abort the process."
                    exit(1)

        elif time[0] > xdate[0]:
            while time[0] > xdate[k]:
                k += 1
                if k >= ecnt:
                    print "Time span does not overlap. Abort the process."
                    exit(1)

        hrc[k] = veto[j]

        tspace = 1.38888888888e-3 / base  #--- setting timing bin size: base is given in hrc loop

        for i in range(k + 1, ecnt):
            tbeg = xdate[i] - tspace
            tend = xdate[i] + tspace

            if j > hcnt - 2:
                hrc[i] = veto[
                    hcnt -
                    1]  #---- if the hrc data runs out, just repeat the last data point value

            elif time[j] >= tbeg and time[j] <= tend:
                hrc[i] = veto[j]

            elif time[j] < tbeg:
                while time[j] < tbeg:
                    j += 1
                hrc[i] = veto[j]

            elif time[j] > tend:
                while time[j] > tend:
                    j -= 1
                hrc[i] = veto[j]

#
#--- print out data
#

    if comp_test == 'test':
        file = test_data_dir + event + '_eph.txt'
    else:
        file = data_dir + event + '_eph.txt'

    f = open(file, 'w')
    line = 'Science Run Interruption: ' + start + '\n\n'
    f.write(line)

    if pYearStart < 2011:
        f.write('dofy\t\tp4\t\t\tp41\t\t\te1300\n')
        f.write(
            '-------------------------------------------------------------------\n'
        )

        for m in range(0, ecnt):
            line = '%4.3f\t\t%4.3e\t%4.3e\t%4.3e\n' % (float(
                xdate[m]), float(p4[m]), float(p41[m]), float(e1300[m]))
            f.write(line)

    else:
        f.write('dofy\t\thrc\t\te150\t\te1300\n')
        f.write(
            '-------------------------------------------------------------------\n'
        )

        for m in range(0, ecnt):
            line = '%4.3f\t\t%4.3e\t%4.3e\t%4.3e\n' % (float(
                xdate[m]), float(hrc[m]), float(e150[m]), float(e1300[m]))
            f.write(line)

    f.close()
Esempio n. 11
0
def computeACEStat(event, start, stop, comp_test = 'NA'):

    'for a gien event, start and stop data, compute ACE statistics. format: 20110804        2011:08:04:07:03        2011:08:07:10:25'

#
#--- change time format to year and ydate (float)
#
    begin = start + ':00'                #---- need to add "seconds" part to dateFormtCon to work correctly
    end   = stop  + ':00'

    (year1, month1, day1, hours1, minutes1, seconds1, ydate1) = tcnv.dateFormatCon(begin)
    (year2, month2, day2, hours2, minutes2, seconds2, ydate2) = tcnv.dateFormatCon(end)

#
#--- find plotting range
#
    (pYearStart, periodStart, pYearStop, periodStop, plotYearStart, plotStart, plotYearStop, plotStop, pannelNum) \
                 = itrf.findCollectingPeriod(year1, ydate1, year2, ydate2)

#
#--- read ACE data
#

    if comp_test == 'test':
        line = test_data_dir + event + '_dat.txt'
    else:
        line = data_dir + event + '_dat.txt'

    f    = open(line, 'r')
    data = [line.strip() for line in f.readlines()]
    f.close()
#
#--- initialization
#
    cnt1            = 0
    cnt2            = 0
    cnt3            = 0
    cnt4            = 0
    cnt5            = 0
    cnt6            = 0
    cnt7            = 0
    cnt8            = 0
    cnt9            = 0
    cnt10           = 0
    cnt11           = 0
    cnt12           = 0
    cnt13           = 0
    e38_a           = 0
    e38_a2          = 0
    e175_a          = 0
    e175_a2         = 0
    p47_a           = 0
    p47_a2          = 0
    p112_a          = 0
    p112_a2         = 0
    p310_a          = 0
    p310_a2         = 0
    p761_a          = 0
    p761_a2         = 0
    p1060_a         = 0
    p1060_a2        = 0
    aniso_a         = 0
    aniso_a2        = 0
    r38_175_a       = 0
    r38_175_a2      = 0
    r47_1060_a      = 0
    r47_1060_a2     = 0
    r112_1060_a     = 0
    r112_1060_a2    = 0
    r310_1060_a     = 0
    r310_1060_a2    = 0
    r761_1060_a     = 0
    r761_1060_a2    = 0

    e38_max         = 0
    e38_min         = 1.0e10
    e175_max        = 0
    e175_min        = 1.0e10
    p47_max         = 0
    p47_min         = 1.0e10
    p112_max        = 0
    p112_min        = 1.0e10
    p310_max        = 0
    p310_min        = 1.0e10
    p761_max        = 0
    p761_min        = 1.0e10
    p1060_max       = 0
    p1060_min       = 1.0e10
    aniso_max       = 0
    aniso_min       = 1.0e10
    r38_175_max     = 0
    r38_175_min     = 1.0e10
    r47_1060_max    = 0
    r47_1060_min    = 1.0e10
    r112_1060_max   = 0
    r112_1060_min   = 1.0e10
    r310_1060_max   = 0
    r310_1060_min   = 1.0e10
    r761_1060_max   = 0
    r761_1060_min   = 1.0e10

    e38_max_t       = 0
    e38_min_t       = 0
    e175_max_t      = 0
    e175_min_t      = 0
    p47_max_t       = 0
    p47_min_t       = 0
    p112_max_t      = 0
    p112_min_t      = 0
    p310_max_t      = 0
    p310_min_t      = 0
    p761_max_t      = 0
    p761_min_t      = 0
    p1060_max_t     = 0
    p1060_min_t     = 0
    aniso_max_t     = 0
    aniso_min_t     = 0
    r38_175_max_t   = 0
    r38_175_min_t   = 0
    r47_1060_max_t  = 0
    r47_1060_min_t  = 0
    r112_1060_max_t = 0
    r112_1060_min_t = 0
    r310_1060_max_t = 0
    r310_1060_min_t = 0
    r761_1060_max_t = 0
    r761_1060_min_t = 0

    e38_int         = 0
    e175_int        = 0
    p47_int         = 0
    p112_int        = 0
    p310_int        = 0
    p761_int        = 0
    p1060_int       = 0
    aniso_int       = 0
    r38_175_int     = 0
    r47_1060_int    = 0
    r112_1060_int   = 0
    r310_1060_int   = 0
    r761_1060_int   = 0

#
#--- start accumulating the values
#
    for ent in data:    
        atemp = re.split('\s+|\t+', ent)
        btemp = re.split('\.', atemp[0])

        if atemp[0]  and btemp[0].isdigit():

            time = float(atemp[0])

            e38  = float(atemp[1])
            if e38 > 0: 
                cnt1 += 1
                if e38 > e38_max:
                    e38_max = e38
                    e38_max_t = time
                 
                if e38 < e38_min:
                    e38_min = e38
                    e38_min_t = time
                
                e38_a    += e38
                e38_a2   += e38*e38
            

            e175 = float(atemp[2])
            if e175 > 0:
                cnt2 += 1
                if e175 > e175_max:
                    e175_max = e175
                    e175_max_t = time
                 
                if e175 < e175_min:
                    e175_min = e175
                    e175_min_t = time
                 
                e175_a   += e175
                e175_a2  += e175*e175
             

            p47  = float(atemp[3])
            if p47 > 0:
                cnt3 += 1
                if p47 > p47_max:
                    p47_max = p47
                    p47_max_t = time
                 
                if p47 < p47_min:
                    p47_min = p47
                    p47_min_t = time
                
                p47_a    += p47
                p47_a2   += p47*p47
             

            p112 = float(atemp[4])
            if p112 > 0:
                cnt4 += 1
                if p112 > p112_max:
                    p112_max = p112
                    p112_max_t = time
                 
                if p112 < p112_min:
                    p112_min = p112
                    p112_min_t = time
                 
                p112_a   += p112
                p112_a2  += p112*p112
             
            p310 = float(atemp[5])
            if p310 > 0:
                cnt5 += 1
                if p310 > p310_max:
                    p310_max = p310
                    p310_max_t = time
                 
                if p310 < p310_min:
                    p310_min = p310
                    p310_min_t = time

                p310_a   += p310
                p310_a2  += p310*p310
             
            p761 = float(atemp[6])
            if p761 > 0:
                cnt6 += 1
                if p761 > p761_max:
                    p761_max = p761
                    p761_max_t = time
                 
                if p761 < p761_min:
                    p761_min = p761
                    p761_min_t = time
                 
                p761_a   += p761
                p761_a2  += p761*p761
             
            p1060= float(atemp[7])
            if p1060 > 0:
                cnt7 += 1
                if p1060 > p1060_max:
                    p1060_max = p1060
                    p1060_max_t = time

                if p1060 < p1060_min:
                    p1060_min = p1060
                    p1060_min_t = time
                 
                p1060_a  += p1060
                p1060_a2 += p1060*p1060
             
            aniso = float(atemp[8])
            if aniso > 0:
                cnt8 += 1
                if aniso > aniso_max:
                    aniso_max = aniso
                    aniso_max_t = time
                 
                if aniso < aniso_min:
                    aniso_min = aniso
                    aniso_min_t = time
                 
                aniso_a  += aniso
                aniso_a2 += aniso*aniso

            if e175 > 0:
                r38_175   = e38/e175
                if r38_175 > 0:
                    cnt9 += 1
                    if r38_175 > r38_175_max:
                        r38_175_max = r38_175
                        r38_175_max_t = time
                     
                    if r38_175 < r38_175_min:
                        r38_175_min = r38_175
                        r38_175_min_t = time
                     
                    r38_175_a    += r38_175
                    r38_175_a2   += r38_175*r38_175
                 

            if p1060 > 0:
                r47_1060  = p47/p1060
                if r47_1060 > 0:
                    cnt10 += 1
                    if r47_1060 > r47_1060_max:
                        r47_1060_max = r47_1060
                        r47_1060_max_t = time
                     
                    if r47_1060 < r47_1060_min:
                        r47_1060_min = r47_1060
                        r47_1060_min_t = time
                     
                    r47_1060_a   += r47_1060
                    r47_1060_a2  += r47_1060*r47_1060
                 

                r112_1060 = p112/p1060
                if r112_1060 > 0:
                    cnt11 += 1
                    if r112_1060 > r112_1060_max:
                        r112_1060_max = r112_1060
                        r112_1060_max_t = time
                     
                    if r112_1060 < r112_1060_min:
                        r112_1060_min = r112_1060
                        r112_1060_min_t = time
                     
                    r112_1060_a  += r112_1060
                    r112_1060_a2 += r112_1060*r112_1060
                 

                r310_1060 = p310/p1060
                if r310_1060 > 0:
                    cnt12 += 1
                    if r310_1060 > r310_1060_max:
                        r310_1060_max = r310_1060
                        r310_1060_max_t = time
                     
                    if r310_1060 < r310_1060_min:
                        r310_1060_min = r310_1060
                        r310_1060_min_t = time
                     
                    r310_1060_a  += r310_1060
                    r310_1060_a2 += r310_1060*r310_1060

                r761_1060 = p761/p1060
                if r761_1060 > 0:
                    cnt13 += 1
                    if r761_1060 > r761_1060_max:
                        r761_1060_max = r761_1060
                        r761_1060_max_t = time
                     
                    if r761_1060 < r761_1060_min:
                        r761_1060_min = r761_1060
                        r761_1060_min_t = time
                     
                    r761_1060_a  += r761_1060
                    r761_1060_a2 += r761_1060*r761_1060
                 

                e38_int       = e38
                e175_int      = e175
                p47_int       = p47
                p112_int      = p112
                p310_int      = p310
                p761_int      = p761
                p1060_int     = p1060
                aniso_int     = aniso
                r38_175_int   = r38_175
                r47_1060_int  = r47_1060
                r112_1060_int = r112_1060
                r310_1060_int = r310_1060
                r761_1060_int = r761_1060

#
#----a big loop ends here; now compute avg and std
#
     
    if cnt1 == 0:
        e38_avg   =    0
        e38_var   =    0
    else:
        e38_avg   =    e38_a/cnt1
        e38_var   =    math.sqrt(e38_a2/cnt1  - e38_avg * e38_avg)
     
    if cnt2 == 0:
        e175_avg  =    0
        e175_var  =    0
    else:
        e175_avg  =    e175_a/cnt2
        e175_var  =    math.sqrt(e175_a2/cnt2 - e175_avg*e175_avg)
     

    if cnt3 == 0:
        p47_avg   =    0
        p47_var   =    0
    else:
        p47_avg   =    p47_a/cnt3
        p47_var   =    math.sqrt(p47_a2/cnt3  - p47_avg * p47_avg)
     

    if cnt4 == 0:
        p112_avg  =    0
        p112_var  =    0
    else:
        p112_avg  =    p112_a/cnt4
        p112_var  =    math.sqrt(p112_a2/cnt4 - p112_avg * p112_avg)
     

    if cnt5 == 0:
        p310_avg  =    0
        p310_var  =    0
    else:
        p310_avg  =    p310_a/cnt5
        p310_var  =    math.sqrt(p310_a2/cnt5 - p310_avg * p310_avg)
     

    if cnt6 == 0:
        p761_avg  =    0
        p761_var  =    0
    else:
        p761_avg  =    p761_a/cnt6
        p761_var  =    math.sqrt(p761_a2/cnt6 - p761_avg * p761_avg)
     

    if cnt7 == 0:
        p1060_avg =    0
        p1060_var =    0
    else:
        p1060_avg =    p1060_a/cnt7
        p1060_var =    math.sqrt(p1060_a2/cnt7 - p1060_avg * p1060_avg)
     

    if cnt8 == 0:
        aniso_avg =    0
        aniso_var =    0
    else:
        aniso_avg =    aniso_a/cnt8
        aniso_var =    math.sqrt(aniso_a2/cnt8 - aniso_avg * aniso_avg)
     

    if cnt9 == 0:
        r38_175_avg   =  0
        r38_175_var   =  0
    else:
        r38_175_avg   =  r38_175_a/cnt9
        r38_175_var   =  math.sqrt(r38_175_a2/cnt9 - r38_175_avg * r38_175_avg)
     

    if cnt10 == 0:
        r47_1060_avg  =  0
        r47_1060_var  =  0
    else:
        r47_1060_avg  =  r47_1060_a/cnt10
        r47_1060_var  =  math.sqrt(r47_1060_a2/cnt10 - r47_1060_avg * r47_1060_avg)
    

    if cnt11 == 0:
        r112_1060_avg =  0
        r112_1060_var =  0
    else:
        r112_1060_avg =  r112_1060_a/cnt11
        r112_1060_var =  math.sqrt(r112_1060_a2/cnt11 - r112_1060_avg * r112_1060_avg)
     

    if cnt12 == 0:
        r310_1060_avg =  0
        r310_1060_var =  0
    else:
        r310_1060_avg =  r310_1060_a/cnt12
        r310_1060_var =  math.sqrt(r310_1060_a2/cnt12 - r310_1060_avg * r310_1060_avg)
     

    if cnt13 == 0:
        r761_1060_avg =  0
        r761_1060_var =  0
    else:
        r761_1060_avg =  r761_1060_a/cnt13
        r761_1060_var =  math.sqrt(r761_1060_a2/cnt13 - r761_1060_avg * r761_1060_avg)

#
#--- create stat table
#

    if comp_test == 'test':
        out = test_stat_dir + event + '_ace_stat'
    else:
        out = stat_dir + event + '_ace_stat'

    f   = open(out, 'w')

    line = 'Data Period  (dom): %6.4f - %6.4f\n' % (plotStart, plotStop)
    f.write(line)

    line = 'Interruption (dom): %6.4f - %6.4f\n' % (ydate1,    ydate2)

    f.write(line)
    f.write('\t\t\tAvg\t\t Max\t\tTime\tMin\t\tTime\t\tValue at Interruption Started\n')
    f.write('--------------------------------------------------------------------------------------------------------------------------\n')

    line = 'e38 \t\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n' % (e38_avg,e38_var,e38_max,e38_max_t,e38_min,e38_min_t,e38_int)
    f.write(line)

    line = 'e175\t\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n' % (e175_avg,e175_var,e175_max,e175_max_t,e175_min,e175_min_t,e175_int)
    f.write(line)

    line = 'p47 \t\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n' % (p47_avg,p47_var,p47_max,p47_max_t,p47_min,p47_min_t,p47_int)
    f.write(line)

    line = 'p112\t\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n' % (p112_avg,p112_var,p112_max,p112_max_t,p112_min,p112_min_t,p112_int)
    f.write(line)

    line = 'p310\t\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n' % (p310_avg,p310_var,p310_max,p310_max_t,p310_min,p310_min_t,p310_int)
    f.write(line)

    line = 'p761\t\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n' % (p761_avg,p761_var,p761_max,p761_max_t,p761_min,p761_min_t,p761_int)
    f.write(line)

    line = 'p1060\t\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n' % (p1060_avg,p1060_var,p1060_max,p1060_max_t,p1060_min,p1060_min_t,p1060_int)
    f.write(line)

    if year1 < 2014:
        line = 'anisotropy\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n'  % (aniso_avg,aniso_var,aniso_max,aniso_max_t,aniso_min,aniso_min_t,aniso_int)
        f.write(line)

    f.write('\nHardness:\n')

    line = 'e38/e175\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n'  \
                % (r38_175_avg,r38_175_var,r38_175_max,r38_175_max_t,r38_175_min,r38_175_min_t,r38_175_int)
    f.write(line)

    line = 'p47/p1060\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n' \
                % (r47_1060_avg,r47_1060_var,r47_1060_max,r47_1060_max_t,r47_1060_min,r47_1060_min_t,r47_1060_int)
    f.write(line)

    line = 'p112/p1060\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n' \
                % (r112_1060_avg,r112_1060_var,r112_1060_max,r112_1060_max_t,r112_1060_min,r112_1060_min_t,r112_1060_int)
    f.write(line)

    line = 'p310/p1060\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n' \
                % (r310_1060_avg,r310_1060_var,r310_1060_max,r310_1060_max_t,r310_1060_min,r310_1060_min_t,r310_1060_int)
    f.write(line)

    line = 'p761/p1060\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n' \
                % (r761_1060_avg,r761_1060_var,r761_1060_max,r761_1060_max_t,r761_1060_min,r761_1060_min_t,r761_1060_int)
    f.write(line)
    f.write('\n')

    

#
#---- find gradient and chooes the steepest rising point
#

    time = []
    e1   = []
    e2   = []
    p1   = []
    p2   = []
    p3   = []
    p4   = []
    p5   = []
    ans  = []

    for ent in data:    
        atemp = re.split('\s+|\t+', ent)
        btemp = re.split('\.', atemp[0])

        if atemp[0]  and btemp[0].isdigit():

            time.append(float(atemp[0]))
            for j in range(1, 9):
                if float(atemp[j]) <= 0:
                    atemp[j] = 1.0e-5

            e1.append(float(atemp[1]))
            e2.append(float(atemp[2]))
            p1.append(float(atemp[3]))
            p2.append(float(atemp[4]))
            p3.append(float(atemp[5]))
            p4.append(float(atemp[6]))
            p5.append(float(atemp[7]))

    f.write('Steepest Rise\n')
    f.write('------------\n')
    f.write('\tTime\t\tSlope(in log per hr)\n')
    f.write('----------------------------------------\n')

    (max_pos, max_slope) = find_jump(e1, time)
    f.write('e1  \t')
    if max_pos == -999:
        line = 'na\t\tna\n'
    else:
        line = '%5.4f\t\t%3.4f\n' % (time[max_pos], max_slope)

    f.write(line)

    (max_pos, max_slope) = find_jump(e2, time)
    f.write('e175\t')
    if max_pos == -999:
        line = 'na\t\tna\n'
    else:
        line = '%5.4f\t\t%3.4f\n' % (time[max_pos], max_slope)
    f.write(line)

    (max_pos, max_slope) = find_jump(p1, time)
    f.write('p47 \t')
    if max_pos == -999:
        line = 'na\t\tna\n'
    else:
        line = '%5.4f\t\t%3.4f\n' % (time[max_pos], max_slope)
    f.write(line)

    (max_pos, max_slope) = find_jump(p2, time)
    f.write('p112\t')
    if max_pos == -999:
        line = 'na\t\tna\n'
    else:
        line = '%5.4f\t\t%3.4f\n' % (time[max_pos], max_slope)
    f.write(line)

    (max_pos, max_slope) = find_jump(p3, time)
    f.write('p310\t')
    if max_pos == -999:
        line = 'na\t\tna\n'
    else:
        line = '%5.4f\t\t%3.4f\n' % (time[max_pos], max_slope)
    f.write(line)

    (max_pos, max_slope) = find_jump(p4, time)
    f.write('p761\t')
    if max_pos == -999:
        line = 'na\t\tna\n'
    else:
        line = '%5.4f\t\t%3.4f\n' % (time[max_pos], max_slope)
    f.write(line)

    (max_pos, max_slope) = find_jump(p5, time)
    f.write('p1060\t')
    if max_pos == -999:
        line = 'na\t\tna\n'
    else:
        line = '%5.4f\t\t%3.4f\n' % (time[max_pos], max_slope)
    f.write(line)

    f.close()
def printEachHtml(event, start, stop, gap, stopType):

    'create indivisual event html page. input event name, interruption start/stop time, gap, and type (auto/manual): example: 20031202        2003:12:02:17:31        2003:12:04:14:27        139.8   auto'

    #
    #--- modify date formats
    #
    begin = start + ':00'
    (year1, month1, date1, hours1, minutes1, seconds1, ydate1, dom1,
     sectime1) = tcnv.dateFormatConAll(begin)
    end = stop + ':00'
    (year2, month2, date2, hours2, minutes2, seconds2, ydate2, dom2,
     sectime2) = tcnv.dateFormatConAll(end)

    #
    #--- find plotting range
    #

    (pYearStart, periodStart, pYearStop, periodStop, plotYearStart, plotStart, plotYearStop, plotStop, pannelNum) \
                 = itrf.findCollectingPeriod(year1, ydate1, year2, ydate2)

    #
    #--- check whether we need multiple pannels
    #
    pannelNum = int((plotStop - plotStart) / 5)

    #
    #--- choose a template
    #
    atemp = re.split(':', start)
    year = int(atemp[0])
    if year < 2011:
        file = house_keeping + 'sub_html_template'
    elif year < 2014:
        file = house_keeping + 'sub_html_template_2011'
    else:
        file = house_keeping + 'sub_html_template_2014'

#
#--- read the template and start substituting
#

    data = open(file).read()

    data = re.sub('#header_title#', event, data)
    data = re.sub('#main_title#', event, data)
    data = re.sub('#sci_run_stop#', start, data)
    data = re.sub('#sci_run_start#', stop, data)
    data = re.sub('#interruption#', gap, data)
    data = re.sub('#trigger#', stopType, data)

    noteN = event + '.txt'
    data = re.sub('#note_name#', noteN, data)

    #
    #--- ACA (NOAA) radiation data
    #

    aceData = event + '_dat.txt'
    data = re.sub('#ace_data#', aceData, data)

    file = stat_dir + event + '_ace_stat'
    stat = open(file).read()
    data = re.sub('#ace_table#', stat, data)

    line = event + '.png"'
    for i in range(2, pannelNum + 1):
        padd = ' alt="main plot" style="width:100%">\n<br />\n<img src = "../Main_plot/' + event + '_pt' + str(
            i) + '.png '
        line = line + padd

    data = re.sub('#ace_plot#', line, data)

    #
    #---EPHIN data
    #

    ephData = event + '_eph.txt'
    data = re.sub('#eph_data#', ephData, data)

    file = stat_dir + event + '_ephin_stat'
    stat = open(file).read()
    data = re.sub('#eph_table#', stat, data)

    line = event + '_eph.png"'
    for i in range(2, pannelNum + 1):
        padd = ' alt="eph plot" style="width:100%">\n<br />\n<img src = "../Ephin_plot/' + event + '_eph_pt' + str(
            i) + '.png '
    line = line + padd

    data = re.sub('#eph_plot#', line, data)

    #
    #---GOES data
    #

    goesData = event + '_goes.txt'
    data = re.sub('#goes_data#', goesData, data)

    file = stat_dir + event + '_goes_stat'
    stat = open(file).read()
    data = re.sub('#goes_table#', stat, data)

    line = event + '_goes.png"'
    for i in range(2, pannelNum + 1):
        padd = ' alt="goes plot" style="width:100%"> \n<br />\n<img src = "../GOES_plot/' + event + '_goes_pt' + str(
            i) + '.png '
        line = line + padd

    data = re.sub('#goes_plot#', line, data)

    if year1 >= 2011:
        data = re.sub('GOES-11', 'GOES-15', data)

    file = web_dir + 'Html_dir/' + event + '.html'
    f = open(file, 'w')
    f.write(data)
    f.close()
Esempio n. 13
0
def ephinDataExtract(event, start, stop, comp_test ='NA'):

    "extract EPIN related quantities and creates a data table for given event, start and stop time (format: 2012:03:13:22:41)"
    begin = start + ':00'               #---- to use dateFormatCon correctly, need to add "sec" part
    end   = stop  + ':00'

#
#--- convert time format
#
    (year1, month1, day1, hours1, minutes1, seconds1, ydate1) = tcnv.dateFormatCon(begin)
    (year2, month2, day2, hours2, minutes2, seconds2, ydate2) = tcnv.dateFormatCon(end)

#
#--- change time format and find data collecting period (starts 2 days before the interruption and ends at least 5 days after the stating)
#

    (pYearStart, periodStart, pYearStop, periodStop, plotYearStart, plotStart, plotYearStop, plotStop, pannelNum)   \
            = itrf.findCollectingPeriod(year1, ydate1, year2, ydate2)

#
#--- read ephin data using arc4gl
#
    ephinList = itrf.useArc4gl('retrieve', 'flight', 'ephin', 1, 'ephrates', pYearStart, periodStart, pYearStop, periodStop, deposit='./Working_dir',) 

#
#--- extract needed data
#
    xdate = []
    p4    = []
    p41   = []
    e150  = []
    e1300 = []
    ecnt  = 0

    for fits in ephinList:
#
#--- use dmlist
#
        if pYearStart < 2011:
            line = fits + '[cols TIME, SCP4, SCP41, SCE1300]'
        else:    
            line = fits + '[cols TIME, SCE150, SCE1300]'

        cmd = 'dmlist "' + line + '" opt="data" > ./zout'
        os.system(cmd)

        f = open('./zout', 'r')
        input = [line.strip() for line in f.readlines()]
        f.close()
        os.system('rm ./zout')

        for ent in input:
            ent.lstrip()
            atemp = re.split('\s+|\t+', ent)
            if atemp[0].isdigit():
                line = tcnv.convertCtimeToYdate(float(atemp[1]))
                (year, month, day, hours, minute, second, ydate, dom, sectime) = tcnv.dateFormatConAll(line)
#
#--- checking the case the year change
#
                if ecnt > 0 and year != startYear:
                        ydate += base
                elif ecnt == 0:
                        startYear = year
                        chk = 4.0 * int(0.25 * year)
                        if chk == year:
                            base = 366
                        else:
                            base = 365

                xdate.append(ydate)

                if pYearStart < 2011:
   
                    if float(atemp[3]) == 0:
                        atemp[3] = 1.0e-4
                    if float(atemp[4]) == 0:
                        atemp[4] = 1.0e-4

                    p4.append(atemp[2])
                    p41.append(atemp[3])
                    e1300.append(atemp[4])
                else:
                    if float(atemp[2]) == 0:
                        atemp[2] = 1.0e-4
                    if float(atemp[3]) == 0:
                        atemp[3] = 1.0e-4

                    e150.append(atemp[2])
                    e1300.append(atemp[3])

                ecnt += 1

    os.system('rm ./Working_dir/*fits')

#
#--- using DataSeeker, extread HRC sheild rate (only if year > 2011)
#
    if pYearStart >= 2011:

        hrcData = itrf.useDataSeeker(pYearStart, periodStart, pYearStop, periodStop, 'mtahrc..hrcveto_avg', 'time,shevart_avg')

        time = []
        veto = []
        hcnt = 0

#
#--- there are slightly different input line format; so we need to different ways to read the line
#
        for ent in hrcData:
            m1 = re.search('----', ent)
            m2 = re.search('ROW',  ent)
            m3 = re.search('DATASEEK', ent)
    
            if ent and m1 == None and m2 == None and m3 == None:
    
                atemp = re.split('\s+|\t+', ent)
    
                if len(atemp) == 4:
                    ttime = tcnv.convertCtimeToYdate(float(atemp[2]))
                    temp  = re.split(':', ttime)
                    year  = int(temp[0])
                    dofy  = float(temp[1]) + float(temp[2]) / 24 + float(temp[3]) / 1440 + float(temp[4]) / 86400
#
#--- checking the case the year changes
#
                    if hcnt > 0 and year != startYear:
                        dofy += base
                    elif hcnt == 0:
                        startYear = year
                        chk = 4.0 * int(0.25 * year)
                        if chk == year:
                            base = 366
                        else:
                            base = 365
             
                    time.append(dofy)
                    veto.append(atemp[3])
                    hcnt += 1
     
                else:     
                    ttime = tcnv.convertCtimeToYdate(float(atemp[1]))
                    temp  = re.split(':', ttime)
                    year  = int(temp[0])
                    dofy  = float(temp[1]) + float(temp[2]) / 24 + float(temp[3]) / 1440 + float(temp[4]) / 86400
                    if hcnt > 0 and year != startYear:
                        dofy += base
                    elif hcnt == 0:
                        startYear = year
                        chk = 4.0 * int(0.25 * year)
                        if chk == year:
                            base = 366
                        else:
                            base = 365
             
                    time.append(dofy)
                    veto.append(atemp[2])
                    hcnt += 1

#
#--- matching timing between electron data and hrc data
#
        hrc = len(e150) * [0]
        j   = 0
        k   = 0

#
#--- find the begining
#
        if time[0] < xdate[0]:
            while time[j] < xdate[0]:
                j += 1
                if j >= hcnt:
                    print "Time span does not overlap. Abort the process."
                    exit(1)
    
        elif  time[0] > xdate[0]:
            while time[0] > xdate[k]:
                k += 1
                if k >= ecnt:
                    print "Time span does not overlap. Abort the process."
                    exit(1)
    
        hrc[k] = veto[j]
        
        tspace = 1.38888888888e-3 / base            #--- setting timing bin size: base is given in hrc loop
    
        for i in range(k+1, ecnt):
            tbeg = xdate[i] - tspace
            tend = xdate[i] + tspace
    
            if j > hcnt - 2:
                hrc[i] = veto[hcnt -1]              #---- if the hrc data runs out, just repeat the last data point value
    
            elif time[j] >= tbeg and time[j] <= tend:
                hrc[i] = veto[j]
    
            elif time[j] < tbeg:
                while time[j] < tbeg:
                    j += 1
                hrc[i] = veto[j]
    
            elif time[j] > tend:
                while time[j] > tend:
                    j -= 1
                hrc[i] = veto[j]

#
#--- print out data
#

    if comp_test == 'test':
        file = test_data_dir + event + '_eph.txt'
    else:
        file = data_dir + event + '_eph.txt'

    f    = open(file, 'w')
    line = 'Science Run Interruption: ' + start + '\n\n'
    f.write(line)

    if pYearStart < 2011:
        f.write('dofy\t\tp4\t\t\tp41\t\t\te1300\n')
        f.write('-------------------------------------------------------------------\n')
    
        for m in range(0, ecnt):
            line = '%4.3f\t\t%4.3e\t%4.3e\t%4.3e\n' % (float(xdate[m]), float(p4[m]), float(p41[m]),  float(e1300[m]))
            f.write(line)

    else:
        f.write('dofy\t\thrc\t\te150\t\te1300\n')
        f.write('-------------------------------------------------------------------\n')
    
        for m in range(0, ecnt):
            line = '%4.3f\t\t%4.3e\t%4.3e\t%4.3e\n' % (float(xdate[m]), float(hrc[m]), float(e150[m]),  float(e1300[m]))
            f.write(line)

    f.close()
Esempio n. 14
0
def aceDataPlot(name, startYear, startYday, stopYear, stopYday, comp_test = 'NA'):

    'manage ACE data plot routines. Input: event name, interruption starting time, and interruption ending time in year:yday format'

#
#--- check whether this is a test case
#
    if comp_test == 'test':
        plot_out = test_plot_dir
    else:
        plot_out = plot_dir

#
#--- set the plotting range
#

    (pYearStart, periodStart, pYearStop, periodStop, plotYearStart, plotStart, plotYearStop, plotStop, period) \
                = itrf.findCollectingPeriod( startYear, startYday, stopYear, stopYday)

#
#--- read radation zone information
#

    radZone = ptrf.readRadZone(name)

#
#--- initialize data lists and read in the data
#
    dofy  = []
    e38   = []
    e175  = []
    p47   = []
    p112  = []
    p310  = []
    p761  = []
    p1060 = []
    ani   = []

    readACEData(name, dofy, e38, e175, p47, p112, p310, p761, p1060, ani, comp_test)
#
#--- if the ending data is in the following year
#
    if stopYear > startYear:
       chk = 4.0 * int (0.25 * startYear)
       if chk == startYear:
           base = 366
       else:
           base = 365
       stopYday += base
#
#--- plot data
#

    if period == 1:
        if startYear < 2014:
            plotACE(dofy, e38, e175, p47, p112, p310, p761, p1060, ani, startYday, stopYday, plotStart, plotStop, radZone)
        else:
            plotACE(dofy, e38, e175, p47, p112, p310, p761, p1060, 'NA', startYday, stopYday, plotStart, plotStop, radZone)

        cmd = 'mv ./out.png ' + plot_out + name + '.png'
        os.system(cmd)

#
#--- if the interruption period cannot be covered by one plotting panel, create as many panels as we need to cover the period.
#

    else:
        pstart = plotStart 
        prange = period + 1
        if startYear < 2014:
            for i in range(1, prange):
                pend = pstart + 5
                if i == 1:
                    plotACE(dofy, e38, e175, p47, p112, p310, p761, p1060, ani, startYday, 'NA', pstart, pend, radZone)
                    cmd = 'mv ./out.png ' + plot_out + name + '.png'
                    os.system(cmd)
                elif i == period:
                    plotACE(dofy, e38, e175, p47, p112, p310, p761, p1060, ani, 'NA', stopYday, pstart, pend, radZone)
                    cmd = 'mv ./out.png ' + plot_out + name + '_pt'+ str(i) + '.png'
                    os.system(cmd)
                else:
                    plotACE(dofy, e38, e175, p47, p112, p310, p761, p1060, ani, 'NA', 'NA', pstart, pend, radZone)
                    cmd = 'mv ./out.png ' + plot_out + name + '_pt'+ str(i) + '.png'
                    os.system(cmd)
    
                pstart  = pend

        else:
            for i in range(1, prange):
                pend = pstart + 5
                if i == 1:
                    plotACE(dofy, e38, e175, p47, p112, p310, p761, p1060, 'NA', startYday, 'NA', pstart, pend, radZone)
                    cmd = 'mv ./out.png ' + plot_out + name + '.png'
                    os.system(cmd)
                elif i == period:
                    plotACE(dofy, e38, e175, p47, p112, p310, p761, p1060, 'NA', 'NA', stopYday, pstart, pend, radZone)
                    cmd = 'mv ./out.png ' + plot_out + name + '_pt'+ str(i) + '.png'
                    os.system(cmd)
                else:
                    plotACE(dofy, e38, e175, p47, p112, p310, p761, p1060, 'NA', 'NA', 'NA', pstart, pend, radZone)
                    cmd = 'mv ./out.png ' + plot_out + name + '_pt'+ str(i) + '.png'
                    os.system(cmd)
    
                pstart  = pend
Esempio n. 15
0
def plotEphinMain(event, start, stop, comp_test = 'NA'):

    'read Ephin data from data_dir and plot them. Input: event, interruption start and stop time (e.g. 20120313        2012:03:13:22:41        2012:03:14:13:57)'

#
#--- read radiation zone information
#
    radZone = ptrf.readRadZone(event)

#
#--- read EPHIN data
#

    if comp_test == 'test':
        file     = test_data_dir + event + '_eph.txt'
        plot_out = test_ephin_dir
    else:
        file     = data_dir + event + '_eph.txt'
        plot_out = ephin_dir

    f    = open(file, 'r')
    data = [line.strip() for line in f.readlines()]
    f.close()

    dofy  = []
    prtn1 = []
    prtn2 = []
    prtn3 = []
    dcnt  = 0
    dataset= 0

    for ent in data:
        atemp = re.split('\s+|\t+', ent)
        btemp = re.split('\.', atemp[0])
        if ent and btemp[0].isdigit():
            dofy.append(atemp[0])

            val1 = float(atemp[1])
            if val1 <= 0:
                val1 = 1e-5

            val2 = float(atemp[2])
            if val2 <= 0:
                val2 = 1e-5

            val3 = float(atemp[3])
            if val3 <= 0:
                val3 = 1e-5



            prtn1.append(math.log10(val1))
            prtn2.append(math.log10(val2))
            prtn3.append(math.log10(val3))
        else:
#
#--- checking which data set, old one: p4, p41, e1300. new one: hrc, e150, e1300
#
            m = re.search('hrc', ent)
            if m is not None:
                    dataset = 1

#
#--- modify date formats
#
    begin = start + ':00'
    (year1, month1, date1, hours1, minutes1, seconds1, ydate1, dom1, sectime1) = tcnv.dateFormatConAll(begin)
    end   = stop  + ':00'
    (year2, month2, date2, hours2, minutes2, seconds2, ydate2, dom2, sectime2) = tcnv.dateFormatConAll(end)

#
#--- find plotting range
#

    (pYearStart, periodStart, pYearStop, periodStop, plotYearStart, plotStart, plotYearStop, plotStop, pannelNum) \
                 = itrf.findCollectingPeriod(year1, ydate1, year2, ydate2)

#
#--- if the interuption go over two years, adjust the ending ydate to that of the previous year
#
    if year2 > year1:
        chk = 4.0 * int(0.25 * year1)
        if chk == year1:
            base = 366
        else:
            base = 365

        ydate2 += base

#
#--- plot data
#
    if pannelNum == 1:
        if year1 < 2014:
            plotEphin(dofy, prtn1, prtn2, prtn3, ydate1, ydate2, plotStart, plotStop, radZone, dataset)
        else:
            plotEphin(dofy, prtn1, 'NA', 'NA', ydate1, ydate2, plotStart, plotStop, radZone, dataset)
        cmd = 'mv ./out.png ' + plot_out + event + '_eph.png'
        os.system(cmd)
#
#--- if the interruption period cannot be covered by one plotting panel, create as many panels as we need to cover the period.
#
    else:
        pstart = plotStart
        prange = pannelNum + 1
        if year1 < 2014:
            for i in range(1, prange):
                pend = pstart + 5
                if i == 1:
                    plotEphin(dofy, prtn1, prtn2, prtn3, ydate1, 'NA', pstart, pend, radZone, dataset)
                    cmd = 'mv ./out.png ' + plot_out + event + '_eph.png'
                    os.system(cmd)
                elif i == pannelNum:
                    plotEphin(dofy, prtn1, prtn2, prtn3, 'NA', ydate2, pstart, pend, radZone, dataset)
                    cmd = 'mv ./out.png ' + plot_out + event + '_eph_pt'+ str(i) +  '.png'
                    os.system(cmd)
                else:
                    plotEphin(dofy, prtn1, prtn2, prtn3, 'NA', 'NA', pstart, pend, radZone, dataset)
                    cmd = 'mv ./out.png ' + plot_out + event + '_eph_pt'+ str(i) +  '.png'
                    os.system(cmd)
                pstart = pend

        else:
            for i in range(1, prange):
                pend = pstart + 5
                if i == 1:
                    plotEphin(dofy, prtn1, 'NA', 'NA', ydate1, 'NA', pstart, pend, radZone, dataset)
                    cmd = 'mv ./out.png ' + plot_out + event + '_eph.png'
                    os.system(cmd)
                elif i == pannelNum:
                    plotEphin(dofy, prtn1, 'NA', 'NA', 'NA', ydate2, pstart, pend, radZone, dataset)
                    cmd = 'mv ./out.png ' + plot_out + event + '_eph_pt'+ str(i) +  '.png'
                    os.system(cmd)
                else:
                    plotEphin(dofy, prtn1, 'NA', 'NA', 'NA', 'NA', pstart, pend, radZone, dataset)
                    cmd = 'mv ./out.png ' + plot_out + event + '_eph_pt'+ str(i) +  '.png'
                    os.system(cmd)
                pstart = pend

#
#--- plot intro page
#
    pend = plotStart + 5
    if year1 < 2014:
        plotIntro(dofy, prtn2, ydate1, ydate2, plotStart, pend, radZone, dataset, year1)
    else:
        plotIntro(dofy, prtn1, ydate1, ydate2, plotStart, pend, radZone, dataset, year1)
    cmd = 'mv ./intro_out.png ' + intro_dir + event + '_intro.png'
    os.system(cmd)
Esempio n. 16
0
def computeACEStat(event, start, stop, comp_test='NA'):

    'for a gien event, start and stop data, compute ACE statistics. format: 20110804        2011:08:04:07:03        2011:08:07:10:25'

    #
    #--- change time format to year and ydate (float)
    #
    begin = start + ':00'  #---- need to add "seconds" part to dateFormtCon to work correctly
    end = stop + ':00'

    (year1, month1, day1, hours1, minutes1, seconds1,
     ydate1) = tcnv.dateFormatCon(begin)
    (year2, month2, day2, hours2, minutes2, seconds2,
     ydate2) = tcnv.dateFormatCon(end)

    #
    #--- find plotting range
    #
    (pYearStart, periodStart, pYearStop, periodStop, plotYearStart, plotStart, plotYearStop, plotStop, pannelNum) \
                 = itrf.findCollectingPeriod(year1, ydate1, year2, ydate2)

    #
    #--- read ACE data
    #

    if comp_test == 'test':
        line = test_data_dir + event + '_dat.txt'
    else:
        line = data_dir + event + '_dat.txt'

    f = open(line, 'r')
    data = [line.strip() for line in f.readlines()]
    f.close()
    #
    #--- initialization
    #
    cnt1 = 0
    cnt2 = 0
    cnt3 = 0
    cnt4 = 0
    cnt5 = 0
    cnt6 = 0
    cnt7 = 0
    cnt8 = 0
    cnt9 = 0
    cnt10 = 0
    cnt11 = 0
    cnt12 = 0
    cnt13 = 0
    e38_a = 0
    e38_a2 = 0
    e175_a = 0
    e175_a2 = 0
    p47_a = 0
    p47_a2 = 0
    p112_a = 0
    p112_a2 = 0
    p310_a = 0
    p310_a2 = 0
    p761_a = 0
    p761_a2 = 0
    p1060_a = 0
    p1060_a2 = 0
    aniso_a = 0
    aniso_a2 = 0
    r38_175_a = 0
    r38_175_a2 = 0
    r47_1060_a = 0
    r47_1060_a2 = 0
    r112_1060_a = 0
    r112_1060_a2 = 0
    r310_1060_a = 0
    r310_1060_a2 = 0
    r761_1060_a = 0
    r761_1060_a2 = 0

    e38_max = 0
    e38_min = 1.0e10
    e175_max = 0
    e175_min = 1.0e10
    p47_max = 0
    p47_min = 1.0e10
    p112_max = 0
    p112_min = 1.0e10
    p310_max = 0
    p310_min = 1.0e10
    p761_max = 0
    p761_min = 1.0e10
    p1060_max = 0
    p1060_min = 1.0e10
    aniso_max = 0
    aniso_min = 1.0e10
    r38_175_max = 0
    r38_175_min = 1.0e10
    r47_1060_max = 0
    r47_1060_min = 1.0e10
    r112_1060_max = 0
    r112_1060_min = 1.0e10
    r310_1060_max = 0
    r310_1060_min = 1.0e10
    r761_1060_max = 0
    r761_1060_min = 1.0e10

    e38_max_t = 0
    e38_min_t = 0
    e175_max_t = 0
    e175_min_t = 0
    p47_max_t = 0
    p47_min_t = 0
    p112_max_t = 0
    p112_min_t = 0
    p310_max_t = 0
    p310_min_t = 0
    p761_max_t = 0
    p761_min_t = 0
    p1060_max_t = 0
    p1060_min_t = 0
    aniso_max_t = 0
    aniso_min_t = 0
    r38_175_max_t = 0
    r38_175_min_t = 0
    r47_1060_max_t = 0
    r47_1060_min_t = 0
    r112_1060_max_t = 0
    r112_1060_min_t = 0
    r310_1060_max_t = 0
    r310_1060_min_t = 0
    r761_1060_max_t = 0
    r761_1060_min_t = 0

    e38_int = 0
    e175_int = 0
    p47_int = 0
    p112_int = 0
    p310_int = 0
    p761_int = 0
    p1060_int = 0
    aniso_int = 0
    r38_175_int = 0
    r47_1060_int = 0
    r112_1060_int = 0
    r310_1060_int = 0
    r761_1060_int = 0

    #
    #--- start accumulating the values
    #
    for ent in data:
        atemp = re.split('\s+|\t+', ent)
        btemp = re.split('\.', atemp[0])

        if atemp[0] and btemp[0].isdigit():

            time = float(atemp[0])

            e38 = float(atemp[1])
            if e38 > 0:
                cnt1 += 1
                if e38 > e38_max:
                    e38_max = e38
                    e38_max_t = time

                if e38 < e38_min:
                    e38_min = e38
                    e38_min_t = time

                e38_a += e38
                e38_a2 += e38 * e38

            e175 = float(atemp[2])
            if e175 > 0:
                cnt2 += 1
                if e175 > e175_max:
                    e175_max = e175
                    e175_max_t = time

                if e175 < e175_min:
                    e175_min = e175
                    e175_min_t = time

                e175_a += e175
                e175_a2 += e175 * e175

            p47 = float(atemp[3])
            if p47 > 0:
                cnt3 += 1
                if p47 > p47_max:
                    p47_max = p47
                    p47_max_t = time

                if p47 < p47_min:
                    p47_min = p47
                    p47_min_t = time

                p47_a += p47
                p47_a2 += p47 * p47

            p112 = float(atemp[4])
            if p112 > 0:
                cnt4 += 1
                if p112 > p112_max:
                    p112_max = p112
                    p112_max_t = time

                if p112 < p112_min:
                    p112_min = p112
                    p112_min_t = time

                p112_a += p112
                p112_a2 += p112 * p112

            p310 = float(atemp[5])
            if p310 > 0:
                cnt5 += 1
                if p310 > p310_max:
                    p310_max = p310
                    p310_max_t = time

                if p310 < p310_min:
                    p310_min = p310
                    p310_min_t = time

                p310_a += p310
                p310_a2 += p310 * p310

            p761 = float(atemp[6])
            if p761 > 0:
                cnt6 += 1
                if p761 > p761_max:
                    p761_max = p761
                    p761_max_t = time

                if p761 < p761_min:
                    p761_min = p761
                    p761_min_t = time

                p761_a += p761
                p761_a2 += p761 * p761

            p1060 = float(atemp[7])
            if p1060 > 0:
                cnt7 += 1
                if p1060 > p1060_max:
                    p1060_max = p1060
                    p1060_max_t = time

                if p1060 < p1060_min:
                    p1060_min = p1060
                    p1060_min_t = time

                p1060_a += p1060
                p1060_a2 += p1060 * p1060

            aniso = float(atemp[8])
            if aniso > 0:
                cnt8 += 1
                if aniso > aniso_max:
                    aniso_max = aniso
                    aniso_max_t = time

                if aniso < aniso_min:
                    aniso_min = aniso
                    aniso_min_t = time

                aniso_a += aniso
                aniso_a2 += aniso * aniso

            if e175 > 0:
                r38_175 = e38 / e175
                if r38_175 > 0:
                    cnt9 += 1
                    if r38_175 > r38_175_max:
                        r38_175_max = r38_175
                        r38_175_max_t = time

                    if r38_175 < r38_175_min:
                        r38_175_min = r38_175
                        r38_175_min_t = time

                    r38_175_a += r38_175
                    r38_175_a2 += r38_175 * r38_175

            if p1060 > 0:
                r47_1060 = p47 / p1060
                if r47_1060 > 0:
                    cnt10 += 1
                    if r47_1060 > r47_1060_max:
                        r47_1060_max = r47_1060
                        r47_1060_max_t = time

                    if r47_1060 < r47_1060_min:
                        r47_1060_min = r47_1060
                        r47_1060_min_t = time

                    r47_1060_a += r47_1060
                    r47_1060_a2 += r47_1060 * r47_1060

                r112_1060 = p112 / p1060
                if r112_1060 > 0:
                    cnt11 += 1
                    if r112_1060 > r112_1060_max:
                        r112_1060_max = r112_1060
                        r112_1060_max_t = time

                    if r112_1060 < r112_1060_min:
                        r112_1060_min = r112_1060
                        r112_1060_min_t = time

                    r112_1060_a += r112_1060
                    r112_1060_a2 += r112_1060 * r112_1060

                r310_1060 = p310 / p1060
                if r310_1060 > 0:
                    cnt12 += 1
                    if r310_1060 > r310_1060_max:
                        r310_1060_max = r310_1060
                        r310_1060_max_t = time

                    if r310_1060 < r310_1060_min:
                        r310_1060_min = r310_1060
                        r310_1060_min_t = time

                    r310_1060_a += r310_1060
                    r310_1060_a2 += r310_1060 * r310_1060

                r761_1060 = p761 / p1060
                if r761_1060 > 0:
                    cnt13 += 1
                    if r761_1060 > r761_1060_max:
                        r761_1060_max = r761_1060
                        r761_1060_max_t = time

                    if r761_1060 < r761_1060_min:
                        r761_1060_min = r761_1060
                        r761_1060_min_t = time

                    r761_1060_a += r761_1060
                    r761_1060_a2 += r761_1060 * r761_1060

                e38_int = e38
                e175_int = e175
                p47_int = p47
                p112_int = p112
                p310_int = p310
                p761_int = p761
                p1060_int = p1060
                aniso_int = aniso
                r38_175_int = r38_175
                r47_1060_int = r47_1060
                r112_1060_int = r112_1060
                r310_1060_int = r310_1060
                r761_1060_int = r761_1060

#
#----a big loop ends here; now compute avg and std
#

    if cnt1 == 0:
        e38_avg = 0
        e38_var = 0
    else:
        e38_avg = e38_a / cnt1
        e38_var = math.sqrt(e38_a2 / cnt1 - e38_avg * e38_avg)

    if cnt2 == 0:
        e175_avg = 0
        e175_var = 0
    else:
        e175_avg = e175_a / cnt2
        e175_var = math.sqrt(e175_a2 / cnt2 - e175_avg * e175_avg)

    if cnt3 == 0:
        p47_avg = 0
        p47_var = 0
    else:
        p47_avg = p47_a / cnt3
        p47_var = math.sqrt(p47_a2 / cnt3 - p47_avg * p47_avg)

    if cnt4 == 0:
        p112_avg = 0
        p112_var = 0
    else:
        p112_avg = p112_a / cnt4
        p112_var = math.sqrt(p112_a2 / cnt4 - p112_avg * p112_avg)

    if cnt5 == 0:
        p310_avg = 0
        p310_var = 0
    else:
        p310_avg = p310_a / cnt5
        p310_var = math.sqrt(p310_a2 / cnt5 - p310_avg * p310_avg)

    if cnt6 == 0:
        p761_avg = 0
        p761_var = 0
    else:
        p761_avg = p761_a / cnt6
        p761_var = math.sqrt(p761_a2 / cnt6 - p761_avg * p761_avg)

    if cnt7 == 0:
        p1060_avg = 0
        p1060_var = 0
    else:
        p1060_avg = p1060_a / cnt7
        p1060_var = math.sqrt(p1060_a2 / cnt7 - p1060_avg * p1060_avg)

    if cnt8 == 0:
        aniso_avg = 0
        aniso_var = 0
    else:
        aniso_avg = aniso_a / cnt8
        aniso_var = math.sqrt(aniso_a2 / cnt8 - aniso_avg * aniso_avg)

    if cnt9 == 0:
        r38_175_avg = 0
        r38_175_var = 0
    else:
        r38_175_avg = r38_175_a / cnt9
        r38_175_var = math.sqrt(r38_175_a2 / cnt9 - r38_175_avg * r38_175_avg)

    if cnt10 == 0:
        r47_1060_avg = 0
        r47_1060_var = 0
    else:
        r47_1060_avg = r47_1060_a / cnt10
        r47_1060_var = math.sqrt(r47_1060_a2 / cnt10 -
                                 r47_1060_avg * r47_1060_avg)

    if cnt11 == 0:
        r112_1060_avg = 0
        r112_1060_var = 0
    else:
        r112_1060_avg = r112_1060_a / cnt11
        r112_1060_var = math.sqrt(r112_1060_a2 / cnt11 -
                                  r112_1060_avg * r112_1060_avg)

    if cnt12 == 0:
        r310_1060_avg = 0
        r310_1060_var = 0
    else:
        r310_1060_avg = r310_1060_a / cnt12
        r310_1060_var = math.sqrt(r310_1060_a2 / cnt12 -
                                  r310_1060_avg * r310_1060_avg)

    if cnt13 == 0:
        r761_1060_avg = 0
        r761_1060_var = 0
    else:
        r761_1060_avg = r761_1060_a / cnt13
        r761_1060_var = math.sqrt(r761_1060_a2 / cnt13 -
                                  r761_1060_avg * r761_1060_avg)

#
#--- create stat table
#

    line = 'Data Period  (dom): %6.4f - %6.4f\n' % (plotStart, plotStop)
    line = line + 'Interruption (dom): %6.4f - %6.4f\n\n' % (ydate1, ydate2)

    line = line + '\t\t\tAvg\t\t Max\t\tTime\tMin\t\tTime\t\tValue at Interruption Started\n'
    line = line + '-' * 95 + '\n'

    line = line + 'e38 \t\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n'\
                        % (e38_avg,e38_var,e38_max,e38_max_t,e38_min,e38_min_t,e38_int)

    line = line + 'e175\t\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n'\
                        % (e175_avg,e175_var,e175_max,e175_max_t,e175_min,e175_min_t,e175_int)

    line = line + 'p47 \t\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n'\
                        % (p47_avg,p47_var,p47_max,p47_max_t,p47_min,p47_min_t,p47_int)

    line = line + 'p112\t\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n'\
                        % (p112_avg,p112_var,p112_max,p112_max_t,p112_min,p112_min_t,p112_int)

    line = line + 'p310\t\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n'\
                        % (p310_avg,p310_var,p310_max,p310_max_t,p310_min,p310_min_t,p310_int)

    line = line + 'p761\t\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n'\
                        % (p761_avg,p761_var,p761_max,p761_max_t,p761_min,p761_min_t,p761_int)

    line = line + 'p1060\t\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n'\
                        % (p1060_avg,p1060_var,p1060_max,p1060_max_t,p1060_min,p1060_min_t,p1060_int)

    if year1 < 2014:
        line = line + 'anisotropy\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n'\
                        % (aniso_avg,aniso_var,aniso_max,aniso_max_t,aniso_min,aniso_min_t,aniso_int)

    line = line + '\nHardness:\n'

    line = line + 'e38/e175\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n'  \
                % (r38_175_avg,r38_175_var,r38_175_max,r38_175_max_t,r38_175_min,\
                   r38_175_min_t,r38_175_int)

    line = line + 'p47/p1060\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n' \
                % (r47_1060_avg,r47_1060_var,r47_1060_max,r47_1060_max_t,r47_1060_min,\
                   r47_1060_min_t,r47_1060_int)

    line = line + 'p112/p1060\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n' \
                % (r112_1060_avg,r112_1060_var,r112_1060_max,r112_1060_max_t,r112_1060_min,\
                   r112_1060_min_t,r112_1060_int)

    line = line + 'p310/p1060\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n' \
                % (r310_1060_avg,r310_1060_var,r310_1060_max,r310_1060_max_t,r310_1060_min,\
                   r310_1060_min_t,r310_1060_int)

    line = line + 'p761/p1060\t%4.3e+/-%4.3e\t%4.3e\t%4.3f \t%4.3e\t%4.3f\t\t%4.3e\n' \
                % (r761_1060_avg,r761_1060_var,r761_1060_max,r761_1060_max_t,r761_1060_min,\
                   r761_1060_min_t,r761_1060_int)
    #
    #---- find gradient and chooes the steepest rising point
    #
    time = []
    e1 = []
    e2 = []
    p1 = []
    p2 = []
    p3 = []
    p4 = []
    p5 = []
    ans = []

    for ent in data:
        atemp = re.split('\s+|\t+', ent)
        btemp = re.split('\.', atemp[0])

        if atemp[0] and btemp[0].isdigit():

            time.append(float(atemp[0]))
            for j in range(1, 9):
                if float(atemp[j]) <= 0:
                    atemp[j] = 1.0e-5

            e1.append(float(atemp[1]))
            e2.append(float(atemp[2]))
            p1.append(float(atemp[3]))
            p2.append(float(atemp[4]))
            p3.append(float(atemp[5]))
            p4.append(float(atemp[6]))
            p5.append(float(atemp[7]))

    line = line + '\nSteepest Rise\n'
    line = line + '------------\n'
    line = line + '\tTime\t\tSlope(in log per hr)\n'
    line = line + '----------------------------------------\n'

    (max_pos, max_slope) = find_jump(e1, time)
    line = line + 'e1  \t'
    if max_pos == -999:
        line = line + 'na\t\tna\n'
    else:
        line = line + '%5.4f\t\t%3.4f\n' % (time[max_pos], max_slope)

    (max_pos, max_slope) = find_jump(e2, time)
    line = line + 'e175\t'
    if max_pos == -999:
        line = line + 'na\t\tna\n'
    else:
        line = line + '%5.4f\t\t%3.4f\n' % (time[max_pos], max_slope)

    (max_pos, max_slope) = find_jump(p1, time)
    line = line + 'p47 \t'
    if max_pos == -999:
        line = line + 'na\t\tna\n'
    else:
        line = line + '%5.4f\t\t%3.4f\n' % (time[max_pos], max_slope)

    (max_pos, max_slope) = find_jump(p2, time)
    line = line + 'p112\t'
    if max_pos == -999:
        line = line + 'na\t\tna\n'
    else:
        line = line + '%5.4f\t\t%3.4f\n' % (time[max_pos], max_slope)

    (max_pos, max_slope) = find_jump(p3, time)
    line = line + 'p310\t'
    if max_pos == -999:
        line = line + 'na\t\tna\n'
    else:
        line = line + '%5.4f\t\t%3.4f\n' % (time[max_pos], max_slope)

    (max_pos, max_slope) = find_jump(p4, time)
    line = line + 'p761\t'
    if max_pos == -999:
        line = line + 'na\t\tna\n'
    else:
        line = line + '%5.4f\t\t%3.4f\n' % (time[max_pos], max_slope)

    (max_pos, max_slope) = find_jump(p5, time)
    line = line + 'p1060\t'
    if max_pos == -999:
        line = line + 'na\t\tna\n'
    else:
        line = line + '%5.4f\t\t%3.4f\n' % (time[max_pos], max_slope)

    if comp_test == 'test':
        out = test_stat_dir + event + '_ace_stat'
    else:
        out = stat_dir + event + '_ace_stat'

    f = open(out, 'w')
    f.write(line)

    f.close()
def extractGOESData(event, start, stop, comp_test='NA'):

    """
    Extract GOES data from NOAA site, and create a locat data base. 
    input: event, interruption start/stop time (e.g., 20120313        2012:03:13:22:41        2012:03:14:13:57'
           option comp_test  is for testing (if comp_test == test, the test data will be read)
    """

#
#--- modify date formats
#
    begin = start + ':00'
    (year1, month1, date1, hours1, minutes1, seconds1, ydate1, dom1, sectime1) = tcnv.dateFormatConAll(begin)
    end   = stop  + ':00'
    (year2, month2, date2, hours2, minutes2, seconds2, ydate2, dom2, sectime2) = tcnv.dateFormatConAll(end)

#
#--- find plotting range
#

    (pYearStart, periodStart, pYearStop, periodStop, plotYearStart, plotStart, plotYearStop, plotStop, pannelNum) \
                 = itrf.findCollectingPeriod(year1, ydate1, year2, ydate2)
#
#--- reformat plotting start / stop time 
#
    (year3, month3, date3, hours3, minutes3, seconds3, ydate3, dom3, sectime3) = tcnv.dateFormatConAll(pYearStart, periodStart)
    (year4, month4, date4, hours4, minutes4, seconds4, ydate4, dom4, sectime4) = tcnv.dateFormatConAll(pYearStop, periodStop)

#
#--- set input data type: old if pre 2011, otherwise new
#--- the first two is from http://goes.ngdc.noaa.gov/data/avg/ and the 'new' one is from http://www.swpc.noaa.gov/ftpdir/lists/pchan/
#--- although they all use p1, p2, and p5, the value are not compatible.
#
    if year3 <= 2009:
        dtype = 'G105'
    elif year3 < 2011:
        dtype = 'G115'
    else:
        dtype = 'new'

#
#--- create a list of html address from which we extract GOES data
#
    htmlList = []
#
#--- for the starting year and the ending year is same
#
    if year3 == year4:
#
#--- for the case the starting month and ending month is same
#
        if month3 == month4:
            tmon = str(month3)
            if month3 < 10:
                tmon = '0' + tmon

            for tday in range(date3, date4+1):
                if tday < 10:
                    tday = '0' + str(tday)
                else:
                    tday = str(tday)

                timeStamp = str(year3) + tmon + tday
#
#--- after 2012, the data are obtained from different site. data are largely missing
#--- 2011, and previous to that we have all record at ngdc site
#
                if dtype == 'new':
#                    html  = 'http://www.swpc.noaa.gov/ftpdir/lists/pchan/' + timeStamp + '_Gp_pchan_5m.txt'
                    html  = 'http://services.swpc.noaa.gov/ftpdir/lists/pchan/' + timeStamp + '_Gp_pchan_5m.txt'
                else:
                    syear = str(year3)
                    html  = 'http://goes.ngdc.noaa.gov/data/avg/' + str(year3) + '/' + dtype + syear[2] + syear[3] + tmon + '.TXT'

                htmlList.append(html)

        else:
#
#---- for the case, the period goes over two months
#
            if month3 == 2:
                chk = 4.0 * int(0.25 * year3)
                if chk == year3:
                    endDate = 29
                else:
                    endDate = 28
            elif month3 == 1 or month3 == 3 or month3 == 5 or month3 == 7 or month3 == 8 or month3 == 10:
                endDate = 31
            else:
                endDate = 30

            tmon = str(month3)
            if month3 < 10:
                tmon = '0' + tmon

            for tday in range(date3, endDate+1):
                timeStamp = str(year3) + tmon + str(tday)
                if dtype == 'new':
                    html  = 'http://www.swpc.noaa.gov/ftpdir/lists/pchan/' + timeStamp + '_Gp_pchan_5m.txt'
                else:
                    syear = str(year3)
                    html  = 'http://goes.ngdc.noaa.gov/data/avg/' + str(year3) + '/' + dtype + syear[2] + syear[3] + tmon + '.TXT'

                htmlList.append(html)

            tmon = str(month4)
            if month4 < 10:
                tmon = '0' + tmon

            for tday in range(1, date4+1):
                if tday < 10:
                    tday = '0' + str(tday)
                else:
                    tday = str(tday)

                timeStamp = str(year3) + tmon + tday
                if dtype == 'new':
                    html      = 'http://www.swpc.noaa.gov/ftpdir/lists/pchan/' + timeStamp + '_Gp_pchan_5m.txt'
                else:
                    syear = str(year3)
                    html = 'http://goes.ngdc.noaa.gov/data/avg/' + str(year3) + '/' + dtype + syear[2] + syear[3] + tmon + '.TXT'

                htmlList.append(html)
    else:
#
#--- for the case the period goes over two years
#

        for tday in range(date3, 32):
            timeStamp = str(year3) + tmon + str(tday)
            if dtype == 'new':
                html      = 'http://www.swpc.noaa.gov/ftpdir/lists/pchan/' + timeStamp + '_Gp_pchan_5m.txt'
            else:
                syear = str(year3)
                html = 'http://goes.ngdc.noaa.gov/data/avg/' + str(year3) + '/' + dtype + syear[2] + syear[3] + tmon + '.TXT'

            htmlList.append(html)

        for tday in range(1, date4+1):
            if tday < 10:
                tday = '0' + str(tday)
            else:
                tday = str(tday)

            timeStamp = str(year4) + tmon + tday
            if dtype == 'new':
                html      = 'http://www.swpc.noaa.gov/ftpdir/lists/pchan/' + timeStamp + '_Gp_pchan_5m.txt'
            else:
                syear = str(year3)
                html = 'http://goes.ngdc.noaa.gov/data/avg/' + str(year4) + '/' + dtype + syear[2] + syear[3] + tmon + '.TXT'

            htmlList.append(html)

#
#--- prepare to print out data
#

    if comp_test == 'test':
        ofile = test_data_dir + event + '_goes.txt'
    else:
        ofile = data_dir + event + '_goes.txt'

    out   = open(ofile, 'w')
    line  = 'Science Run Interruption: ' + str(start) +'\n\n'
    out.write(line)
    out.write('dofy\t\tp1\t\t\tp2\t\t\tp5\n')
    out.write("-------------------------------------------------------------------\n")

#
#--- now extract data from NOAA web site
#

    
    for html in htmlList:
        if comp_test == 'test':
            atemp = re.split('pchan\/', html)
            afile = house_keeping + 'NOAO_data/' + atemp[1]
            cmd   = 'cp ' + afile + '  ./Working_dir/temp_data'
        else:
            cmd = 'lynx -source ' + html + '>./Working_dir/temp_data'

        os.system(cmd)
        
        f = open('./Working_dir/temp_data', 'r')
        data = [line.strip() for line in f.readlines()]
        f.close()
        os.system('rm ./Working_dir/temp_data')
        
#
#--- extract needed data and print them out in a data file
#

#
#--- if data are collected after 2011, start here
#

        if dtype == 'new':
            for ent in data:
                atemp = re.split('\s+|\t+', ent)
    
                if ent and atemp[0].isdigit():
                    timestamp = atemp[0] + ':' + atemp[1] + ':' + atemp[2] + ':' + atemp[3][0] + atemp[3][1] + ':' + atemp[3][2] + atemp[3][3] + ':00'
    
                    (dyear, dmonth, dday, dhours, dminutes, dseconds, dydate) = tcnv.dateFormatCon(timestamp)
    
                    if dyear == pYearStart:
                        if dydate >= plotStart and dydate <= plotStop:
    
                            line = '%4.3f\t\t%3.2e\t%3.2e\t%3.2e\n' % (dydate, float(atemp[6]), float(atemp[7]), float(atemp[10]))
                            out.write(line)
                    else:
#
#--- for the case, the period goes over two years
#
                        chk = 4.0 * int(0.25 * pYearStart)
                        if chk == pYearStart:
                            base = 366
                        else:
                            base = 365
    
                        dydate += base
                        if dydate >= plotStart and dydate <= plotStop:
    
                            line = '%4.3f\t\t%3.2e\t%3.2e\t%3.2e\n' % (dydate, float(atemp[6]), float(atemp[7]), float(atemp[10]))
                            out.write(line)

#
#--- if the data is collected before 2011, start here
#

        else:
            for ent in data:
                atemp = re.split('\s+|\t+', ent)
    
                if ent and atemp[0].isdigit():
                    dyear = atemp[0][0] + atemp[0][1]
                    dyear = int(dyear)
                    if dyear > 90:
                        dyear += 1900
                    else:
                        dyear += 2000

                    dydate   = float(atemp[2])
                    dhours   = float(atemp[1][0] + atemp[1][1])
                    dminutes = float(atemp[1][2] + atemp[1][3])
                    dydate  += (dhours/24.0 + dminutes / 1440.0)
    
                    if dyear == pYearStart:
                        if dydate >= plotStart and dydate <= plotStop:
    
                            line = '%4.3f\t\t%3.2e\t%3.2e\t%3.2e\n' % (dydate, float(atemp[10]), float(atemp[11]), float(atemp[14]))
                            out.write(line)
                    else:
#
#--- for the case, the period goes over two years
#
                        chk = 4.0 * int(0.25 * pYearStart)
                        if chk == pYearStart:
                            base = 366
                        else:
                            base = 365
    
                        dydate += base
                        dydate += base
                        if dydate >= plotStart and dydate <= plotStop:
    
                            line = '%4.3f\t\t%3.2e\t%3.2e\t%3.2e\n' % (dydate, float(atemp[10]), float(atemp[11]), float(atemp[14]))
                            out.write(line)


    if len(htmlList) > 0:
        out.close()
Esempio n. 18
0
def aceDataPlot(name,
                startYear,
                startYday,
                stopYear,
                stopYday,
                comp_test='NA'):

    'manage ACE data plot routines. Input: event name, interruption starting time, and interruption ending time in year:yday format'

    #
    #--- check whether this is a test case
    #
    if comp_test == 'test':
        plot_out = test_plot_dir
    else:
        plot_out = plot_dir

#
#--- set the plotting range
#

    (pYearStart, periodStart, pYearStop, periodStop, plotYearStart, plotStart, plotYearStop, plotStop, period) \
                = itrf.findCollectingPeriod( startYear, startYday, stopYear, stopYday)

    #
    #--- read radation zone information
    #

    radZone = ptrf.readRadZone(name)

    #
    #--- initialize data lists and read in the data
    #
    dofy = []
    e38 = []
    e175 = []
    p47 = []
    p112 = []
    p310 = []
    p761 = []
    p1060 = []
    ani = []

    readACEData(name, dofy, e38, e175, p47, p112, p310, p761, p1060, ani,
                comp_test)
    #
    #--- if the ending data is in the following year
    #
    if stopYear > startYear:
        chk = 4.0 * int(0.25 * startYear)
        if chk == startYear:
            base = 366
        else:
            base = 365
        stopYday += base
#
#--- plot data
#

    if period == 1:
        if startYear < 2014:
            plotACE(dofy, e38, e175, p47, p112, p310, p761, p1060, ani,
                    startYday, stopYday, plotStart, plotStop, radZone)
        else:
            plotACE(dofy, e38, e175, p47, p112, p310, p761, p1060, 'NA',
                    startYday, stopYday, plotStart, plotStop, radZone)

        cmd = 'mv ./out.png ' + plot_out + name + '.png'
        os.system(cmd)

#
#--- if the interruption period cannot be covered by one plotting panel, create as many panels as we need to cover the period.
#

    else:
        pstart = plotStart
        prange = period + 1
        if startYear < 2014:
            for i in range(1, prange):
                pend = pstart + 5
                if i == 1:
                    plotACE(dofy, e38, e175, p47, p112, p310, p761, p1060, ani,
                            startYday, 'NA', pstart, pend, radZone)
                    cmd = 'mv ./out.png ' + plot_out + name + '.png'
                    os.system(cmd)
                elif i == period:
                    plotACE(dofy, e38, e175, p47, p112, p310, p761, p1060, ani,
                            'NA', stopYday, pstart, pend, radZone)
                    cmd = 'mv ./out.png ' + plot_out + name + '_pt' + str(
                        i) + '.png'
                    os.system(cmd)
                else:
                    plotACE(dofy, e38, e175, p47, p112, p310, p761, p1060, ani,
                            'NA', 'NA', pstart, pend, radZone)
                    cmd = 'mv ./out.png ' + plot_out + name + '_pt' + str(
                        i) + '.png'
                    os.system(cmd)

                pstart = pend

        else:
            for i in range(1, prange):
                pend = pstart + 5
                if i == 1:
                    plotACE(dofy, e38, e175, p47, p112, p310, p761, p1060,
                            'NA', startYday, 'NA', pstart, pend, radZone)
                    cmd = 'mv ./out.png ' + plot_out + name + '.png'
                    os.system(cmd)
                elif i == period:
                    plotACE(dofy, e38, e175, p47, p112, p310, p761, p1060,
                            'NA', 'NA', stopYday, pstart, pend, radZone)
                    cmd = 'mv ./out.png ' + plot_out + name + '_pt' + str(
                        i) + '.png'
                    os.system(cmd)
                else:
                    plotACE(dofy, e38, e175, p47, p112, p310, p761, p1060,
                            'NA', 'NA', 'NA', pstart, pend, radZone)
                    cmd = 'mv ./out.png ' + plot_out + name + '_pt' + str(
                        i) + '.png'
                    os.system(cmd)

                pstart = pend