def convert_time(time, format  = 0):

    """
    convert time format from seconds from 1998.1.1 to dom or fractional year
    Input:  time    --- a list of time in seconds
            format  --- if 0, convert into dom. if 1, ydate,  otherwise, fractional year
    Output: timeconverted --- a list of conveted time
    """

    timeconverted = []
    for ent in time:
        stime = tcnv.convertCtimeToYdate(ent)
        atime = tcnv.dateFormatConAll(stime)

        if format == 0: 
            timeconverted.append(float(atime[7]))
        elif format == 1:
            ydate = float(atime[6])
            timeconverted.append(ydate)
        else:
            year  = float(atime[0])
            ydate = float(atime[6])
            chk   = 4.0 *  int(0.25 * year)
            if chk == year:
                base = 366
            else:
                base = 365
            year += ydate /base

            timeconverted.append(year)
        
    return timeconverted
示例#2
0
def convert_time(time, format  = 0):

    """
    convert time format from seconds from 1998.1.1 to dom or fractional year
    Input:  time    --- a list of time in seconds
            format  --- if 0, convert into dom, otherwise, fractional year
    Output: timeconverted --- a list of conveted time
    """

    timeconverted = []
    for ent in time:
        stime = tcnv.convertCtimeToYdate(ent)
        atime = tcnv.dateFormatConAll(stime)

        if format == 0: 
            timeconverted.append(float(atime[7]))
        else:
            year  = float(atime[0])
            ydate = float(atime[6])
            chk   = 4.0 *  int(0.25 * year)
            if chk == year:
                base = 366
            else:
                base = 365
            year += ydate /base

            timeconverted.append(year)
        
    return timeconverted
示例#3
0
def convertToYearDate(val):

    """
    convert seconds from 1988,1.1. to date in fractional year e.g, 2012.134
    input:   date in the format of of second from 1988.1.1
    output:  date in a fractional year. 
    """

    ntime = tcnv.convertCtimeToYdate(val)

    btemp = re.split(':', ntime)
    year  = float(btemp[0])
    ydate = float(btemp[1])
    hour  = float(btemp[2])
    mins  = float(btemp[3])

    chk   = int(0.25 * year)
    if chk == year:
        base = 366
    else: 
        base = 365

    yearDate = year + (ydate + hour/24.0 + mins/1440.0) / base

    return yearDate
示例#4
0
def convert_ytime(time):
    '''
    change time fromat from in second from 1.1.1998 to time in unit of fractional year
    input time in: seconds from 1.1.1998
    output time in : fractional year, e.g., 2011.1213
    '''

    atime = tcnv.convertCtimeToYdate(time)
    btemp = re.split(':', atime)
    year = float(btemp[0])
    ydate = float(btemp[1])
    hour = float(btemp[2])
    mins = float(btemp[3])
    sec = float(btemp[4])

    chk = 4.0 * int(0.25 * year)
    if chk == year:
        base = 366.0
    else:
        base = 365.0

    ydate = ydate + (hour / 24.0 + mins / 1440.0 + sec / 86400.0)
    frac = ydate / base

    ytime = year + frac

    return ytime
示例#5
0
def findTimeFromHead(file):
    """
    isolate time part from a file and convert to DOM
    Input:  file --- fits file name
            dom  --- file creation time in DOM
    """

    stime = extractTimePart(file)
    ctime = tcnv.convertCtimeToYdate(stime)
    (year, month, date, hours, minutes, seconds, ydate, dom,
     sectime) = tcnv.dateFormatConAll(ctime)

    return int(dom)
def findTimeFromHead(file):

    """
    isolate time part from a file and convert to DOM
    Input:  file --- fits file name
            dom  --- file creation time in DOM
    """

    stime = extractTimePart(file)
    ctime = tcnv.convertCtimeToYdate(stime)
    (year, month, date, hours, minutes, seconds, ydate, dom, sectime) = tcnv.dateFormatConAll(ctime)

    return int(dom)
def conv_to_ydate(time):
    """
    convert seconds from 1998.1.1. to ydate. year part is drropped
    input:  time    --- time in seconds from 1998.1.1
            day     --- ydate (no year part)
    """

    tmp    = tcnv.convertCtimeToYdate(time)
    atemp  = re.split(':', tmp)
    day    = float(atemp[1])
    hrs    = float(atemp[2])
    mins   = float(atemp[3])
    day   += hrs / 24 + mins / 1440

    return day
示例#8
0
def conv_to_ydate(time):
    """
    convert seconds from 1998.1.1. to ydate. year part is drropped
    input:  time    --- time in seconds from 1998.1.1
            day     --- ydate (no year part)
    """

    tmp = tcnv.convertCtimeToYdate(time)
    atemp = re.split(':', tmp)
    day = float(atemp[1])
    hrs = float(atemp[2])
    mins = float(atemp[3])
    day += hrs / 24 + mins / 1440

    return day
def run_test():
    """
    check the latest count Il count rate and xmm orbital altitude and
    send out warning if mta_XMM_alert file does not exist.
    input: none
    output: /pool1/mta_XMM_alert if it does not exist already
            warning eamil
    """
#
#--- find the latest 30 mins of l1 average and their time span as there are
#--- often slight delay in time in the data aquisition
#
    [l1, start, stop] = l1_median()
#
#--- read xmm altitude data
#
    [atime, alt]      = read_xmm_orbit()
#
#--- if the altitude of the satellite is lower then "alt_limit" during the time period,
#--- condtion is not met; stop the program
#    
    height = 0
    for i in range(0, len(atime)):
        if atime[i] < start:
            continue

        elif atime[i] > stop:
            break

        else:
            if alt[i] > height:
                height = alt[i]
                stime  = atime[i]
#
#--- keep the record
#
    r_time = 0.5 * (start + stop)
    stime  = tcnv.convertCtimeToYdate(r_time)
    line   =  str(stime) + ' : ' + str(r_time) + '\t\t' + str(round(l1,1)) + '\t\t' + str(round(height,1)) + '\n'
    fo     = open('./l1_alt_records', 'a')
    fo.write(line)
    fo.close()

    if l1 < l1_limit:
        exit(1)

    if height < alt_limit:
        exit(1)
#
#--- both conditions are met; check alert file already exists
#
#
#--- keep the record of alert time
#
    keep_record(stime, height, l1)
    go = 0
#
#--- file does not exist
#
    if mcf.chkFile(alert_file) == 0:
        go = 1
#
#--- file was created more than 18 hrs ago.
#
    else:
        if check_time_span(alert_file, 64800):
            go = 2
#
#--- if the file does not exist or more than 18 hrs past after creating the file,
#--- create/recreate the file and also send out a warning email.
#
    if go > 0:
#
#--- read the last 30 mins of data
#
        f     = open('./l1_alt_records', 'r')
        adata = [line.strip() for line in f.readlines()]
        f.close()

        dline = ''
        dlen  = len(adata)
        for i in range(dlen-6, dlen):
            dline = dline + adata[i] + '\n'
#
#--- alt in kkm
#
        chigh = round((height/1000.0), 3)
#
#--- create email content
#
        line = 'Test threshold crossed, Altitude = ' + str(chigh) + ' kkm with '
        #line = line + 'L1 30 min average counts @ ' + str(round(l1,2)) + '.'
        line = line + 'L1 30 min median counts @ ' + str(round(l1,2)) + '.'

        line = line + '\n\n\n'
        line = line + 'Time              \t  (sec)         \t\t L1 cnt      Alt\n'
        line = line + '------------------------------------------------------\n'
        line = line + dline
        line = line + '\n\n\n'

        line = line + 'see:\n\n '  
        line = line + '\t\thttps://cxc.cfa.harvard.edu/mta/RADIATION/XMM/ '
        line = line + '\n\nfor the current condition.\n'

        fo   = open(zspace, 'w')
        fo.write(line)
        fo.close()

        cmd = 'cat ' + zspace + '|mailx -s\"Subject: mta_XMM_alert \n\" [email protected]' 
        os.system(cmd)
        cmd = 'cat ' + zspace + '|mailx -s\"Subject: mta_XMM_alert \n\" [email protected]' 
        os.system(cmd)

        cmd = 'rm ' + zspace
        os.system(cmd)
#
#--- create/renew alert_file
#
        mcf.rm_file(alert_file)

        file  = alert_file
        fo    = open(file, 'w')
        fo.close()
示例#10
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()
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()
示例#12
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()