示例#1
0
def printACEData(byear, start, stop, out):

    "extract ACE data from rad_data<yyyy> then print it out. Input: year, start ydate, stop ydate, out (file destination) "

    file = house_keeping + 'rad_data' + str(byear)

    if byear <= 2000:
            file = house_keeping + 'rad_data_pre2001'

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

    for ent in data:
        m = re.search('#', ent)
        if ent and (m is None):
            atemp = re.split('\s+|\t+', ent)
            if atemp[0].isdigit() and int(atemp[0]) == byear:
                year  = int(atemp[0])
                month = int(atemp[1])
                day   = int(atemp[2])
                ydate = tcnv.findYearDate(year, month, day)
    
                btemp = atemp[3]
                shr   = btemp[0] + btemp[1]
                hours = float(shr)
                smin  = btemp[2] + btemp[3]
                mins  = float(smin)
                ydate += (hours/24 + mins/1440)
    
                if ydate >= start and ydate <= stop:
                    if atemp[7] and atemp[8] and atemp[10] and  atemp[11] and atemp[12] and atemp[13] and atemp[14] and atemp[15]:
                        line = '%6.4f\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' \
                            % (ydate, atemp[7], atemp[8], atemp[10], atemp[11], atemp[12], atemp[13], atemp[14], atemp[15])
                        out.write(line)
示例#2
0
def find_dom_from_mp_file(ent):
    """
    find dom date from the direoctry path name
    input:  ent --- full path to the file <mp_dir>/<date>/...
    output: dom --- day of mission
    """

    atemp = re.split(mp_dir, ent)
    btemp = atemp[1].replace('/', '')
    year = btemp[0] + btemp[1] + btemp[2] + btemp[3]
    month = btemp[4] + btemp[5]
    day = btemp[6] + btemp[7]
    year = int(float(year))
    month = int(float(month))
    day = int(float(day))

    ydate = tcnv.findYearDate(year, month, day)
    dom = tcnv.findDOM(year, ydate, 0, 0, 0)

    return dom
示例#3
0
def printACEData(byear, start, stop, out):

    "extract ACE data from rad_data<yyyy> then print it out. Input: year, start ydate, stop ydate, out (file destination) "

    file = house_keeping + 'rad_data' + str(byear)

    if byear <= 2000:
        file = house_keeping + 'rad_data_pre2001'

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

    for ent in data:
        m = re.search('#', ent)
        if ent and (m is None):
            atemp = re.split('\s+|\t+', ent)
            if atemp[0].isdigit() and int(atemp[0]) == byear:
                year = int(atemp[0])
                month = int(atemp[1])
                day = int(atemp[2])
                ydate = tcnv.findYearDate(year, month, day)

                btemp = atemp[3]
                shr = btemp[0] + btemp[1]
                hours = float(shr)
                smin = btemp[2] + btemp[3]
                mins = float(smin)
                ydate += (hours / 24 + mins / 1440)

                if ydate >= start and ydate <= stop:
                    if atemp[7] and atemp[8] and atemp[10] and atemp[
                            11] and atemp[12] and atemp[13] and atemp[
                                14] and atemp[15]:
                        line = '%6.4f\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' \
                            % (ydate, atemp[7], atemp[8], atemp[10], atemp[11], atemp[12], atemp[13], atemp[14], atemp[15])
                        out.write(line)
示例#4
0
def sci_run_compute_gap(file):
    """
    for a given file name which contains a list, recompute the lost science time (excluding radiation zone) 
    input:  file    --- the file containing information, e.g.:
        20120313        2012:03:13:22:41        2012:03:14:13:57         53.3   auto" 
    output: file    --- updated file
    """
    #
    #--- if file is not given (if it is NA), ask the file input
    #
    test = exc_dir + file
    if not os.path.isfile(test):
        file = raw_input('Please put the intrrupt timing list: ')

    f = open(file, 'r')
    data = [line.strip() for line in f.readlines()]
    f.close()
    #
    #--- a starting date of the interruption in yyyy:mm:dd:hh:mm (e.g., 2006:03:20:10:30)
    #--- there could be multiple lines of date; in that is the case, the scripts add the rad zone list
    #--- to each date
    #

    update = []

    for ent in data:

        if not ent:  #--- if it is a blank line end the operation
            break

        etemp = re.split('\s+', ent)
        atemp = re.split(':', etemp[1])
        year = atemp[0]
        month = atemp[1]
        date = atemp[2]
        hour = atemp[3]
        mins = atemp[4]
        #
        #--- convert to dom/sec1998
        #
        ydate = tcnv.findYearDate(int(year), int(month), int(date))
        dom = tcnv.findDOM(int(year), int(ydate), int(hour), int(mins), 0)
        line = year + ':' + str(ydate) + ':' + hour + ':' + mins + ':00'
        csec = tcnv.axTimeMTA(line)
        #
        #--- end date
        #

        atemp = re.split(':', etemp[2])
        eyear = atemp[0]
        emonth = atemp[1]
        edate = atemp[2]
        ehour = atemp[3]
        emins = atemp[4]

        ydate = tcnv.findYearDate(int(eyear), int(emonth), int(edate))
        dom2 = tcnv.findDOM(int(eyear), int(ydate), int(ehour), int(emins), 0)
        line = eyear + ':' + str(ydate) + ':' + ehour + ':' + emins + ':00'
        csec2 = tcnv.axTimeMTA(line)
        #
        #--- date stamp for the list
        #
        list_date = str(year) + str(month) + str(date)

        #
        #--- read radiation zone information from "rad_zone_list" and add up the time overlap with
        #--- radiatio zones with the interruption time period
        #

        line = house_keeping + '/rad_zone_list'
        f = open(line, 'r')
        rlist = [line.strip() for line in f.readlines()]
        f.close()

        sum = 0
        for record in rlist:
            atemp = re.split('\s+', record)
            if list_date == atemp[0]:
                btemp = re.split(':', atemp[1])
                for period in btemp:

                    t1 = re.split('\(', period)
                    t2 = re.split('\)', t1[1])
                    t3 = re.split('\,', t2[0])
                    pstart = float(t3[0])
                    pend = float(t3[1])

                    if pstart >= dom and pstart < dom2:
                        if pend <= dom2:
                            diff = pend - pstart
                            sum += diff
                        else:
                            diff = dom2 - pstart
                            sum += diff
                    elif pstart < dom2 and pend > dom:
                        if pend <= dom2:
                            diff = pend - dom
                            sum += diff
                        else:
                            diff = dom2 - dom
                            sum += diff

                break

        sum *= 86400  #--- change unit from day to sec

        sciLost = (
            csec2 - csec - sum
        ) / 1000  #--- total science time lost excluding radiation zone passes

        line = etemp[0] + '\t' + etemp[1] + '\t' + etemp[
            2] + '\t' + "%.1f" % sciLost + '\t' + etemp[4]

        update.append(line)

    fo = open(file, 'w')
    for line in update:
        fo.write(line)
        fo.write('\n')
    fo.close()

    return update
示例#5
0
def hrc_dose_get_data(startYear, startMonth, stopYear, stopMonth):

    'extract HRC evt1 data fro a month and create cumulative data fits file. Input: start year, start month, stop year, stop month'

    #
    #--- checking a depository directory. if there is, clean up. if not crate.
    #

    chk = expf.chkFile('./', 'Save')
    if chk == 1:
        chk = expf.chkFile('./Save', 'fits')
        if chk == 1:
            os.system('rm ./Save/*')
    else:
        os.system('mkdir ./Save')

#
#--- fill up the month list
#
    month_list1 = []
    month_list2 = []
    month_list3 = []

    chk = 0  #--- this chk will be used to monitor whether the period completes in a year or not

    if startYear == stopYear:
        #
        #--- the period is in the same year
        #

        month_list1 = range(startMonth, stopMonth + 1)
        chk = 1

    else:
        #
        #--- if the period is over two or more years, we need to set three sets of month list
        #
        month_list1 = range(startMonth, 13)
        month_list2 = range(1, 13)
        month_list3 = range(1, stopMonth + 1)

    for year in range(startYear, stopYear + 1):
        lyear = str(year)
        syear = lyear[2] + lyear[3]

        #
        #--- choose a month list for the specific year
        #
        if chk > 0:  #--- for the case, the list finishes in the same year
            month_list = month_list1
        else:
            if year == startYear:
                month_list = month_list1
            elif year == stopYear:
                month_list = month_list3
            else:
                month_list = month_list2
#
#--- start extracting the data for the year/month period
#
        for month in month_list:
            smonth = str(month)
            if month < 10:
                smonth = '0' + smonth

            outfile_i = './HRCI_' + smonth + '_' + lyear + '.fits'
            outfile_s = './HRCS_' + smonth + '_' + lyear + '.fits'

            #
            #--- using ar4gl, get file names
            #
            ydate1 = tcnv.findYearDate(year, month, 1)

            nextMonth = month + 1
            if nextMonth > 12:
                lyear = year + 1
                ydate2 = tcnv.findYearDate(lyear, 1, 1)
            else:
                lyear = year
                ydate2 = tcnv.findYearDate(year, nextMonth, 1)

            fitsList = expf.useArc4gl('browse', 'flight', 'hrc', 1, 'evt1',
                                      year, ydate1, lyear, ydate2)

            #
            #--- extract each evt1 file, extract the central part, and combine them into a one file
            #
            hrciCnt = 0  #--- counters for how many hrc-i and hrc-s are extracted
            hrcsCnt = 0

            for file in fitsList:

                [fitsName] = expf.useArc4gl('retrieve',
                                            'flight',
                                            'hrc',
                                            1,
                                            'evt1',
                                            filename=file)

                detector = whichHRC(fitsName)  #--- checking which HRC (S or I)

                if detector == 'HRC-S':
                    line = fitsName + '[EVENTS][bin rawx=0:4095:1, rawy=22528:26623:1][status=xxxxxx00xxxxxxxxx000x000xx00xxxx][option type=i4 mem=80]'
                else:
                    line = fitsName + '[EVENTS][bin rawx=6144:10239:1, rawy=6144:10239:1][status=xxxxxx00xxxxxxxxx000x000xx00xxxx][option type=i4 mem=80]'

#
#--- create image file
#
                cmd = 'dmcopy "' + line + '" out.fits option=image clobber=yes'
                os.system(cmd)

                cmd = 'dmstat out.fits centroid=no > stest'
                os.system(cmd)
                #
                #--- if there is actually data, condense the iamge so that it won't take too much space
                #
                f = open('stest', 'r')
                sdata = [line.strip() for line in f.readlines()]
                f.close()
                os.system('rm stest')

                val = 'NA'
                for lent in sdata:
                    m = re.search('mean', lent)
                    if m is not None:
                        atemp = re.split('\s+|\t+', lent)
                        val = atemp[1]
                        break

                if val != 'NA' and float(val) > 0:
                    line = 'out.fits[opt type=i4,null=-99]'
                    cmd = 'dmcopy infile="' + line + '" outfile=ztemp.fits clobber=yes'
                    os.system(cmd)
                    #
                    #--- for HRC S
                    #
                    if detector == 'HRC-S':
                        if hrcsCnt == 0:
                            os.system('mv ztemp.fits total_s.fits')
                        else:
                            cmd = 'dmimgcalc infile=ztemp.fits infile2=total_s.fits outfile=mtemp.fits operation=add  clobber=yes'
                            os.system(cmd)
                            os.system('rm ztemp.fits')
                            os.system('mv mtemp.fits total_s.fits')

                        hrcsCnt += 1
#
#--- for HRC I
#
                    else:
                        if hrciCnt == 0:
                            os.system('mv ztemp.fits total_i.fits')
                        else:
                            cmd = 'dmimgcalc infile=ztemp.fits infile2=total_i.fits outfile=mtemp.fits operation=add  clobber=yes'
                            os.system(cmd)
                            os.system('rm ztemp.fits')
                            os.system('mv mtemp.fits total_i.fits')

                        hrciCnt += 1

                cmd = 'rm out.fits ' + fitsName
                os.system(cmd)
#
#--- move the file to a depository
#
            if hrcsCnt > 0:
                cmd = 'mv total_s.fits ./Save/' + outfile_s
                os.system(cmd)

            if hrciCnt > 0:
                cmd = 'mv total_i.fits ./Save/' + outfile_i
                os.system(cmd)

            os.system('gzip ./Save/HRC*fits')
            cmd = 'mv   ./Save/HRC*fits.gz ' + web_dir + '/Month_hrc/'
            os.system(cmd)
            #
            #---- create cumulative hrc data
            #
            createCumulative(year, month)
示例#6
0
def sci_run_add_to_rad_zone_list(file='NA'):

    'adding radiation zone list to rad_zone_list. input: file name containing: e.g. 20120313        2012:03:13:22:41        2012:03:14:13:57         53.3   auto'

    #
    #--- check whether the list alread exists; if it does, read which ones are already in the list
    #
    cmd = 'ls ' + house_keeping + '* > ./ztemp'
    os.system(cmd)
    #    f    = open('./ztemp', 'r')
    #    test = f.readlines()
    #    f.close()
    test = open('./ztemp').read()

    m1 = re.search('rad_zone_list', test)
    m2 = re.search('rad_zone_list~', test)

    eventList = []
    echk = 0
    if m1 is not None:
        line = house_keeping + 'rad_zone_list'
        f = open(line, 'r')
        data = [line.strip() for line in f.readlines()]
        f.close()

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

#
#--- if file is not given (if it is NA), ask the file input
#

    if file == 'NA':
        file = raw_input('Please put the intrrupt timing list: ')

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

    #
    #--- put the list in the reverse order
    #
    data.reverse()

    for ent in data:
        if not ent:
            break

#
#--- a starting date of the interruption in yyyy:mm:dd:hh:mm (e.g., 2006:03:20:10:30)
#--- there could be multiple lines of date; in that is the case, the scripts add the rad zone list
#--- to each date
#

        etemp = re.split('\s+', ent)
        echk = 0
        for comp in eventList:
            if comp == etemp[0]:
                echk = 1
                break

        if echk == 0:

            atemp = re.split(':', etemp[1])
            year = atemp[0]
            month = atemp[1]
            date = atemp[2]
            hour = atemp[3]
            mins = atemp[4]

            #
            #--- convert to dom/sec1998
            #
            ydate = tcnv.findYearDate(int(year), int(month), int(date))
            dom = tcnv.findDOM(int(year), int(ydate))
            line = year + ':' + str(
                int(ydate)) + ':' + hour + ':' + mins + ':00'
            csec = tcnv.axTimeMTA(line)

            #
            #--- end date
            #

            atemp = re.split(':', etemp[2])
            eyear = atemp[0]
            emonth = atemp[1]
            edate = atemp[2]
            ehour = atemp[3]
            emins = atemp[4]

            ydate = tcnv.findYearDate(int(eyear), int(emonth), int(edate))
            line = eyear + ':' + str(
                int(ydate)) + ':' + ehour + ':' + emins + ':00'
            csec2 = tcnv.axTimeMTA(line)

            #
            #--- date stamp for the list
            #
            list_date = str(year) + str(month) + str(date)

            #
            #--- check radiation zones for 3 days before to 5 days after from the interruptiondate
            #--- if the interruption lasted longer than 5 days, extend the range 7 more days
            #

            begin = dom - 3
            end = dom + 5

            diff = csec2 - csec
            if diff > 432000:
                end += 7

#
#--- read radiation zone infornation
#

            infile = house_keeping + '/rad_zone_info'
            f = open(infile, 'r')
            rdata = [line.strip() for line in f.readlines()]
            f.close()

            status = []
            rdate = []
            chk = 0
            last_st = ''
            cnt = 0

            for line in rdata:
                atemp = re.split('\s+', line)

                dtime = float(atemp[1])  #--- dom of the entry or exit

                if chk == 0 and atemp[0] == 'ENTRY' and dtime >= begin:
                    status.append(atemp[0])
                    rdate.append(dtime)
                    chk += 1
                    last_st = atemp[0]
                    cnt += 1
                elif chk > 0 and dtime >= begin and dtime <= end:
                    status.append(atemp[0])
                    rdate.append(dtime)
                    last_st = atemp[0]
                    cnt += 1
                elif atemp[1] > end and last_st == 'EXIT':
                    break
                elif atemp[1] > end and last_st == 'ENTRY':
                    status.append(atemp[0])
                    rdate.append(dtime)
                    cnt += 1
                    break

            f = open('./temp_zone', 'w')

            #
            #--- a format of the output is, e.g.: '20120313    (4614.2141112963,4614.67081268519):...'
            #

            line = list_date + '\t'
            f.write(line)

            upper = cnt - 1
            i = 0
            while i < cnt:
                line = '(' + str(rdate[i]) + ','
                f.write(line)
                i += 1
                if i < upper:
                    line = str(rdate[i]) + '):'
                    f.write(line)
                else:
                    line = str(rdate[i]) + ')\n'
                    f.write(line)
                i += 1

            f.close()

            #
            #--- append the past rad zone list
            #

            oldFile = house_keeping + '/rad_zone_list~'
            crtFile = house_keeping + '/rad_zone_list'

            if m1 is not None:
                cmd = 'cat ' + './temp_zone ' + crtFile + ' > ./temp_comb'
                os.system(cmd)

            else:
                os.system('mv .temp_zone ./temp_comb')

            os.system('rm ./temp_zone')

            #
            #--- save the old file and move the update file to rad_zone_list
            #

            if m2 is not None:
                cmd = 'chmod 775 ' + crtFile + ' ' + oldFile
                os.system(cmd)

            if m1 is not None:
                cmd = 'mv ' + crtFile + ' ' + oldFile
                os.system(cmd)

                cmd = 'chmod 644 ' + oldFile
                os.system(cmd)

            cmd = 'mv  ' + './temp_comb ' + crtFile
            os.system(cmd)
示例#7
0
def hrc_dose_get_data(startYear='NA',
                      startMonth='NA',
                      stopYear='NA',
                      stopMonth='NA',
                      comp_test='NA'):
    """
    extract HRC evt1 data from a month and create cumulative data fits file. Input: start year, start month, stop year, stop month
    """

    if startYear == 'NA' or startMonth == 'NA' or stopYear == 'NA' or stopMonth == 'NA':
        startYear = raw_input('Starting Year: ')
        startyear = int(startYear)
        startMonth = raw_input('Starting Month: ')
        startMonth = int(startMonth)

        stopYear = raw_input('Stopping Year: ')
        stopyear = int(stopYear)
        stopMonth = raw_input('Stopping Month: ')
        stopMonth = int(stopMonth)

#
#--- start extracting the data for the year/month period
#
    for year in range(startYear, stopYear + 1):

        lyear = str(year)
        syear = lyear[2] + lyear[3]

        month_list = expf.make_month_list(
            year, startYear, stopYear, startMonth,
            stopMonth)  #---- create a list of month appropriate for the year

        for month in month_list:

            smonth = str(month)
            if month < 10:
                smonth = '0' + smonth
#
#--- output file name settings
#
            outfile_i_c = './HRCI_' + smonth + '_' + lyear + '.fits'
            outfile_s_c = './HRCS_' + smonth + '_' + lyear + '.fits'

            outfile_i = ['NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA']
            for i in range(0, 9):
                outfile_i[i] = './HRCI_' + smonth + '_' + lyear + '_' + str(
                    i) + '.fits'

            outfile_s = [
                'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA'
            ]
            for i in range(0, 10):
                outfile_s[i] = './HRCS_' + smonth + '_' + lyear + '_' + str(
                    i) + '.fits'

#
#--- using ar4gl, get file names
#
            ydate1 = tcnv.findYearDate(year, month, 1)

            nextMonth = month + 1
            if nextMonth > 12:
                lyear = year + 1
                ydate2 = tcnv.findYearDate(lyear, 1, 1)
            else:
                lyear = year
                ydate2 = tcnv.findYearDate(year, nextMonth, 1)

            fitsList = mtac.useArc4gl('browse', 'flight', 'hrc', 1, 'evt1',
                                      year, ydate1, lyear, ydate2)

            #
            #--- extract each evt1 file, extract the central part, and combine them into a one file
            #
            hrciCnt = [
                0, 0, 0, 0, 0, 0, 0, 0, 0
            ]  #--- counters for how many hrc-i and hrc-s are extracted
            hrcsCnt = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
            hrciCnt_c = 0
            hrcsCnt_c = 0

            for file in fitsList:
                try:
                    [fitsName] = mtac.useArc4gl('retrieve',
                                                'flight',
                                                'hrc',
                                                1,
                                                'evt1',
                                                filename=file)
                except:
                    continue

                detector = whichHRC(fitsName)  #--- checking which HRC (S or I)
                #
                #--- creating the center part image ----------------------------------
                #
                line = set_cmd_line(fitsName, detector, 'center',
                                    0)  #---- set command line

                ichk = expf.create_image(
                    line, 'ztemp.fits')  #---  create an image file
                #
                #--- for HRC S
                #
                if detector == 'HRC-S' and ichk > 0:
                    expf.combine_image('ztemp.fits', 'total_s.fits')
                    hrcsCnt_c += 1
#
#--- for HRC I
#
                elif detector == 'HRC-I' and ichk > 0:
                    expf.combine_image('ztemp.fits', 'total_i.fits')
                    hrciCnt_c += 1

#
#---- now work on the full image ----------------------------------------
#

                for i in range(0, 10):

                    if detector == 'HRC-I' and i == 9:  #---- HRC-I has only 9 sections HRC-S has 10 sections
                        break

                    line = set_cmd_line(fitsName, detector, 'full',
                                        i)  #---- set command line

                    ichk = expf.create_image(
                        line, 'ztemp.fits')  #---- create an image file
                    #
                    #--- for HRC S
                    #
                    if detector == 'HRC-S' and ichk > 0:
                        fits = 'total_s' + str(i) + '.fits'
                        expf.combine_image(
                            'ztemp.fits', fits
                        )  #--- add ztemp.fits to fits, if there if no fits, mv ztempfits to fits
                        hrcsCnt[i] += 1
#
#--- for HRC I
#
                    elif detector == 'HRC-I' and ichk > 0:
                        fits = 'total_i' + str(i) + '.fits'
                        expf.combine_image('ztemp.fits', fits)
                        hrciCnt[i] += 1

                cmd = 'rm out.fits ' + fitsName
                os.system(cmd)
#
#--- move the file to a depository ; first the center image -----------------------------
#
            if comp_test == 'test':
                dp_web_dir = test_web_dir
            else:
                dp_web_dir = web_dir

            if hrcsCnt_c > 0:
                cmd = 'mv total_s.fits ' + dp_web_dir + 'Month_hrc/' + outfile_s_c
                os.system(cmd)
                cmd = 'gzip ' + dp_web_dir + '/Month_hrc/*.fits'
                os.system(cmd)

            createCumulative(year, month, 'HRC-S', 'center', dp_web_dir, i=0)

            if hrciCnt_c > 0:
                cmd = 'mv total_i.fits ' + dp_web_dir + 'Month_hrc/' + outfile_i_c
                os.system(cmd)
                cmd = 'gzip ' + dp_web_dir + '/Month_hrc/*.fits'
                os.system(cmd)

            createCumulative(year, month, 'HRC-I', 'center', dp_web_dir, i=0)

            #
            #---full image
            #
            if comp_test == 'test':
                dp_hrc_full_data = test_hrc_full_data
            else:
                dp_hrc_full_data = hrc_full_data

            for i in range(0, 10):
                if hrcsCnt[i] > 0:
                    cmd = 'mv total_s' + str(
                        i
                    ) + '.fits ' + dp_hrc_full_data + '/Month_hrc/' + outfile_s[
                        i]
                    os.system(cmd)
                    cmd = 'gzip ' + dp_hrc_full_data + '/Month_hrc/*.fits'
                    os.system(cmd)

                createCumulative(year, month, 'HRC-S', 'full',
                                 dp_hrc_full_data, i)

            for i in range(0, 9):
                if hrciCnt[i] > 0:
                    cmd = 'mv total_i' + str(
                        i
                    ) + '.fits ' + dp_hrc_full_data + '/Month_hrc/' + outfile_i[
                        i]
                    os.system(cmd)
                    cmd = 'gzip ' + dp_hrc_full_data + '/Month_hrc/*.fits'
                    os.system(cmd)

                createCumulative(year, month, 'HRC-I', 'full',
                                 dp_hrc_full_data, i)
def sci_run_compute_gap(file = 'NA'):

    'for a given file name which contains a list like: "20120313        2012:03:13:22:41        2012:03:14:13:57         53.3   auto", recompute the lost science time (excluding radiation zone) '

#
#--- if file is not given (if it is NA), ask the file input
#

    if file == 'NA':
        file = raw_input('Please put the intrrupt timing list: ')

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

#
#--- a starting date of the interruption in yyyy:mm:dd:hh:mm (e.g., 2006:03:20:10:30)
#--- there could be multiple lines of date; in that is the case, the scripts add the rad zone list
#--- to each date
#

    update = []

    for ent in data:

        if not ent:                         #--- if it is a blank line end the operation
            break

        etemp = re.split('\s+', ent)
        atemp = re.split(':', etemp[1])
        year  = atemp[0]
        month = atemp[1]
        date  = atemp[2]
        hour  = atemp[3]
        mins  = atemp[4]

#
#--- convert to dom/sec1998
#
        ydate = tcnv.findYearDate(int(year), int(month), int(date))              #--- a function from convertTimeFormat
        dom   = tcnv.findDOM(int(year), int(ydate), int(hour), int(mins), 0)     #--- a function from convertTimeFormat
        line  = year + ':' + str(ydate) + ':' + hour + ':' + mins + ':00'
        csec  = tcnv.axTimeMTA(line)                                             #--- a function from convertTimeFormat

#
#--- end date
#

        atemp  = re.split(':', etemp[2])
        eyear  = atemp[0]
        emonth = atemp[1]
        edate  = atemp[2]
        ehour  = atemp[3]
        emins  = atemp[4]

        ydate = tcnv.findYearDate(int(eyear), int(emonth), int(edate))
        dom2  = tcnv.findDOM(int(eyear), int(ydate), int(ehour), int(emins), 0)
        line  = eyear + ':' + str(ydate) + ':' + ehour + ':' + emins + ':00'
        csec2 = tcnv.axTimeMTA(line)
    
#
#--- date stamp for the list
#
        list_date = str(year) + str(month) + str(date)

#
#--- read radiation zone information from "rad_zone_list" and add up the time overlap with 
#--- radiatio zones with the interruption time period
#

        line  = house_keeping + '/rad_zone_list'
        f     = open(line, 'r')
        rlist = [line.strip() for line in f.readlines()]
        f.close()

        sum = 0
        for record in rlist:
            atemp = re.split('\s+', record)
            if list_date == atemp[0]:
                btemp = re.split(':', atemp[1])

                for period in btemp:

                    t1 = re.split('\(', period)
                    t2 = re.split('\)', t1[1])
                    t3 = re.split('\,', t2[0])
                    pstart = float(t3[0])
                    pend   = float(t3[1])

                    if pstart >= dom and  pstart < dom2:
                        if pend <= dom2:
                            diff = pend - pstart
                            sum += diff
                        else:
                            diff = dom2 - pstart
                            sum += diff
                    elif pstart < dom2 and pend > dom:
                        if pend <= dom2:
                            diff = pend - dom
                            sum += diff
                        else:
                            diff = dom2 - dom
                            sum += diff

                break
                
        sum *= 86400                            #--- change unit from day to sec

        sciLost = (csec2 - csec - sum) / 1000   #--- total science time lost excluding radiation zone passes

        line = etemp[0] + '\t' + etemp[1] + '\t' + etemp[2] + '\t' + "%.1f" %  sciLost  + '\t' + etemp[4]

        update.append(line)
#
#--- update the file 
#

    oldfile = file + '~'
    cmd = 'mv ' + file + ' ' + oldfile
    os.system(cmd)
    f = open(file, 'w')
    for ent in update:
        f.write(ent)
        f.write('\n')

    f.close()
def sci_run_add_to_rad_zone_list(file='NA'):

    'adding radiation zone list to rad_zone_list. input: file name containing: e.g. 20120313        2012:03:13:22:41        2012:03:14:13:57         53.3   auto'

    #
    #--- if file is not given (if it is NA), ask the file input
    #

    if file == 'NA':
        file = raw_input('Please put the intrrupt timing list: ')

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

    for ent in data:

        #
        #--- a starting date of the interruption in yyyy:mm:dd:hh:mm (e.g., 2006:03:20:10:30)
        #--- there could be multiple lines of date; in that is the case, the scripts add the rad zone list
        #--- to each date
        #

        etemp = re.split('\s+', ent)

        atemp = re.split(':', etemp[1])
        year = atemp[0]
        month = atemp[1]
        date = atemp[2]
        hour = atemp[3]
        mins = atemp[4]

        #
        #--- convert to dom/sec1998
        #
        ydate = tcnv.findYearDate(int(year), int(month), int(date))
        dom = tcnv.findDOM(int(year), int(ydate))
        line = year + ':' + str(int(ydate)) + ':' + hour + ':' + mins + ':00'
        csec = tcnv.axTimeMTA(line)

        #
        #--- end date
        #

        atemp = re.split(':', etemp[2])
        eyear = atemp[0]
        emonth = atemp[1]
        edate = atemp[2]
        ehour = atemp[3]
        emins = atemp[4]

        ydate = tcnv.findYearDate(int(eyear), int(emonth), int(edate))
        dom2 = tcnv.findDOM(int(eyear), int(ydate))
        line = eyear + ':' + str(
            int(ydate)) + ':' + ehour + ':' + emins + ':00'
        csec2 = tcnv.axTimeMTA(line)

        #
        #--- date stamp for the list
        #
        list_date = str(year) + str(month) + str(date)

        #
        #--- check radiation zones for 3 days before to 5 days after from the interruptiondate
        #

        begin = dom - 3
        end = dom2 + 5

        #
        #--- read radiation zone infornation
        #

        infile = house_keeping + '/rad_zone_info'
        f = open(infile, 'r')
        rdata = [line.strip() for line in f.readlines()]
        f.close()

        status = []
        rdate = []
        chk = 0
        last_st = ''
        cnt = 0

        for line in rdata:
            atemp = re.split('\s+', line)

            dtime = float(atemp[1])  #--- dom of the entry or exit

            if chk == 0 and atemp[0] == 'ENTRY' and dtime >= begin:
                status.append(atemp[0])
                rdate.append(dtime)
                chk += 1
                last_st = atemp[0]
                cnt += 1
            elif chk > 0 and dtime >= begin and dtime <= end:
                status.append(atemp[0])
                rdate.append(dtime)
                last_st = atemp[0]
                cnt += 1
            elif atemp[1] > end and last_st == 'EXIT':
                break
            elif atemp[1] > end and last_st == 'ENTRY':
                status.append(atemp[0])
                rdate.append(dtime)
                cnt += 1
                break

        f = open('./temp_zone', 'w')

        #
        #--- a format of the output is, e.g.: '20120313    (4614.2141112963,4614.67081268519):...'
        #

        line = list_date + '\t'
        f.write(line)

        i = 0
        while i < cnt:
            line = '(' + str(rdate[i]) + ','
            f.write(line)
            i += 1
            if i < cnt - 1:
                line = str(rdate[i]) + '):'
                f.write(line)
            else:
                line = str(rdate[i]) + ')\n'
                f.write(line)
            i += 1

        f.close()

        #
        #--- append the past rad zone list
        #

        oldFile = house_keeping + '/rad_zone_list~'
        crtFile = house_keeping + '/rad_zone_list'

        #>        cmd = 'cat ' './temp_zone ' + crtFile + ' > /tmp/mta/temp_comb'
        cmd = 'cat ' + './temp_zone ' + crtFile + ' > ./temp_comb'
        os.system(cmd)

        #>        os.systm('rm /tmp/mta/temp_zone')
        os.system('rm ./temp_zone')

        #
        #--- save the old file and move the update file to rad_zone_list
        #

        cmd = 'chmod 775 ' + crtFile + ' ' + oldFile
        os.system(cmd)

        cmd = 'mv ' + crtFile + ' ' + oldFile
        os.system(cmd)

        cmd = 'chmod 644 ' + oldFile
        os.system(cmd)

        #>        cmd     = 'mv  ' + '/tmp/mta/temp_comb ' + crtFile
        cmd = 'mv  ' + './temp_comb ' + crtFile
        os.system(cmd)
示例#10
0
def acis_dose_get_data(startYear='NA',
                       startMonth='NA',
                       stopYear='NA',
                       stopMonth='NA'):
    """
    extract ACIS evt1 data from a month and create combined image file. Input: start year, start month, stop year, stop month
    """

    if startYear == 'NA' or startMonth == 'NA' or stopYear == 'NA' or stopMonth == 'NA':

        startYear = raw_input('Start Year: ')
        startyear = int(startYear)
        startMonth = raw_input('Start Month: ')
        startMonth = int(startMonth)

        stopYear = raw_input('Stop Year: ')
        stopyear = int(stopYear)
        stopMonth = raw_input('Stop Month: ')
        stopMonth = int(stopMonth)
#
#--- start extracting the data for the year/month period
#
    for year in range(startYear, stopYear + 1):

        lyear = str(year)
        syear = lyear[2] + lyear[3]

        month_list = expf.make_month_list(
            year, startYear, stopYear, startMonth,
            stopMonth)  #---- create a list of month appropriate for the year

        for month in month_list:

            smonth = str(month)
            if month < 10:
                smonth = '0' + smonth
#
#--- using ar4gl, get file names
#
            ydate1 = tcnv.findYearDate(year, month, 1)

            nextMonth = month + 1
            if nextMonth > 12:
                nyear = year + 1
                ydate2 = tcnv.findYearDate(nyear, 1, 1)
            else:
                nyear = year
                ydate2 = tcnv.findYearDate(year, nextMonth, 1)

            fitsList = mtac.useArc4gl('browse', 'flight', 'acis', 1, 'evt1',
                                      year, ydate1, nyear, ydate2)

            #
            #--- extract each evt1 file, extract the central part, and combine them into a one file
            #
            i = 0
            acisCnt = 0

            for file in fitsList:
                m = re.search('fits', file)
                if m is not None:

                    fitsName = mtac.useArc4gl('retrieve',
                                              'flight',
                                              'acis',
                                              1,
                                              'evt1',
                                              filename=file)

                    line = file + '[EVENTS][bin tdetx=2800:5200:1, tdety=1650:4150:1][option type=i4]'

                    ichk = expf.create_image(
                        line, 'ztemp.fits')  #---  create an image file
                    #
                    #--- combined images
                    #
                    if ichk > 0:
                        expf.combine_image('ztemp.fits', 'total.fits')
                        acisCnt += 1

                    cmd = 'rm ' + file
                    os.system(cmd)

#
#--- rename the file
#

            outfile = './ACIS_' + smonth + '_' + lyear + '_full.fits'
            cmd = 'mv total.fits ' + outfile
            os.system(cmd)
            #
            #--- trim the extreme values
            #
            upper = find_10th(outfile)

            outfile2 = './ACIS_' + smonth + '_' + lyear + '.fits'
            cmd = 'dmimgthresh infile=' + outfile + ' outfile=' + outfile2 + ' cut="0:' + str(
                upper) + '" value=0 clobber=yes'
            os.system(cmd)

            cmd = 'gzip ' + outfile
            os.system(cmd)
示例#11
0
def extract_new_data_sub(dir, col_list, start, stop, year, month):

    #
    #---  extract data
    #
    line = 'operation=retrieve\n'
    line = line + 'dataset = mta\n'
    line = line + 'detector = grad\n'
    line = line + 'level = 0.5\n'
    line = line + 'filetype =' + dir + '\n'
    line = line + 'tstart=' + start + '\n'
    line = line + 'tstop=' + stop + '\n'
    line = line + 'go\n'
    fo = open(zspace, 'w')
    fo.write(line)
    fo.close()

    cmd1 = "/usr/bin/env PERL5LIB="
    #cmd2 =  '  echo ' +  hakama + ' |arc4gl -U' + dare + ' -Sarcocc -i' + zspace
    cmd2 = ' /proj/axaf/simul/bin/arc5gl -user isobe  -script ' + zspace
    cmd = cmd1 + cmd2
    bash(cmd, env=ascdsenv)
    mcf.rm_file(zspace)
    cmd = 'mv *gz ./Working_dir/.'
    os.system(cmd)

    cmd = 'gzip -d ./Working_dir/*.gz'
    os.system(cmd)

    cmd = 'ls ./Working_dir/*.fits > ' + zspace
    os.system(cmd)
    f = open(zspace, 'r')
    data = [line.strip() for line in f.readlines()]
    f.close()
    mcf.rm_file(zspace)

    if len(data) == 0:
        return 'na'
#
#--- extract each column data and stored in arrays
#
    elen = len(col_list)
    dat_save = [[] for x in range(0, elen)]
    time_dat = []

    for file in data:
        dout = fits.getdata(file, 1)
        time = dout.field('time')
        for ent in time:
            time_dat.append(ent)

        for k in range(0, elen):
            col = col_list[k]
            sta = 'ST_' + col
            val = dout.field(col)
            chk = dout.field(sta)
            for j in range(0, len(chk)):
                if chk[j] >= 0:
                    dat_save[k].append(val[j])
#
#--- set the beginning of the and the end of the first day of the data
#
    ydate = tcnv.findYearDate(year, month, 1)
    tent = str(year) + ':' + str(ydate) + ':00:00:00'
    begin = tcnv.axTimeMTA(tent)

    smonth = month + 1
    syear = year
    if smonth > 12:
        smonth = 1
        syear += 1
    ydate = tcnv.findYearDate(syear, smonth, 1)
    tent = str(syear) + ':' + str(ydate) + ':00:00:00'
    stop = tcnv.axTimeMTA(tent)

    end = begin + 86400
    if end > stop:
        end = stop

    dom = int(tcnv.stimeToDom(begin))
    sline = str(dom)

    dsumming = [[] for x in range(0, elen)]
    #
    #--- find mean and std of each columns
    #
    for k in range(0, len(time_dat)):
        if time_dat[k] >= begin and time_dat[k] < end:
            for i in range(0, elen):
                dsumming[i].append(dat_save[i][k])
        elif time_dat[k] < begin:
            continue
        elif time_dat[k] >= end:
            for i in range(0, elen):
                narray = numpy.array(dsumming[i])
                avg = numpy.mean(narray)
                avg = '%.4f' % round(avg, 4)
                err = numpy.std(narray)
                err = '%.4f' % round(err, 4)
                sline = sline + '\t' + str(avg) + '\t' + str(err)

            sline = sline + '\n'
            #
            #--- reintialize for the next day
            #
            begin += 86400
            if begin >= stop:
                break
            end = begin + 86400
            if end > stop:
                end = stop
            dom = int(tcnv.stimeToDom(begin))

            sline = sline + str(dom)
            dsumming = [[] for x in range(0, elen)]

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

    return sline
示例#12
0
def acis_sci_run_print_html(html_dir, pyear, pmonth, pday, pchk):
    """
    update three html pages according for the year (pyear)
    Input: html_dir --- web directory path
           pyear  --- the year you want to update the html pages
           pmonth --- current month
           pday   --- current month date
           pchk   --- if it is "yes", it will also update the main html, otherwise, just update subs
    Output: science_run.html
            science_long_term.html
            science_run<year>.html

    """
    pyear = int(pyear)

    update = str(pyear) + '-' + str(pmonth) + '-' + str(pday)
    dom = int(tcnv.findDOM(pyear, pmonth, pday, 0, 0, 0))
    ydate = tcnv.findYearDate(pyear, pmonth, pday)
    #
    #---- update the main html page
    #
    if pchk == 'yes':
        ylist = ''
        j = 0
        for ryear in range(1999, pyear + 1):
            ylist = ylist + '<td><a href=' + http_dir + '/Year' + str(
                ryear) + '/science_run' + str(ryear) + '.html>'
            ylist = ylist + '<strong>Year ' + str(
                ryear) + '</strong></a><br /><td />\n'
            if j == 5:
                j = 0
                ylist = ylist + '</tr><tr>\n'
            else:
                j += 1

        line = house_keeping + 'science_run.html'
        f = open(line, 'r')
        hfile = f.read()
        f.close()
        temp = hfile.replace('#UPDATE#', update)
        temp1 = temp.replace('#DOY#', str(ydate))
        temp2 = temp1.replace('#DOM#', str(dom))
        temp3 = temp2.replace('#YEAR#', str(pyear))
        temp4 = temp3.replace('#YLIST#', str(ylist))

        line = html_dir + 'science_run.html'
        f = open(line, 'w')
        f.write(temp4)
        f.close()

#
#--- update this year's sub directory html page
#

    line = house_keeping + 'sub_year.html'
    f = open(line, 'r')
    hfile = f.read()
    f.close()
    temp = hfile.replace('#UPDATE#', update)
    temp1 = temp.replace('#DOY#', str(ydate))
    temp2 = temp1.replace('#DOM#', str(dom))
    temp3 = temp2.replace('#YEAR#', str(pyear))

    line = html_dir + 'Year' + str(pyear) + '/science_run' + str(
        pyear) + '.html'
    f = open(line, 'w')
    f.write(temp3)
    f.close()
    #
    #--- update long term html page
    #
    line = house_keeping + 'science_long_term.html'
    f = open(line, 'r')
    hfile = f.read()
    f.close()
    temp = hfile.replace('#UPDATE#', update)
    temp1 = temp.replace('#DOY#', str(ydate))
    temp2 = temp1.replace('#DOM#', str(dom))

    line = html_dir + 'Long_term/science_long_term.html'
    f = open(line, 'w')
    f.write(temp2)
    f.close()
示例#13
0
def acis_hist_extract_data(year, month, comp_test=''):

    #
    #--- clean up
    #
    cmd = 'rm ' + exc_dir + '*.fits* 2>/dev/null'
    os.system(cmd)
    cmd = 'rm ' + exc_dir + 'Temp_dir/* 2>/dev/null'
    os.system(cmd)

    #
    #--- define extracting period in seconds from 1.1.1998
    #
    end_year = year
    end_month = month + 1

    if end_month > 12:
        end_month = 1
        end_year += 1

    ydate = tcnv.findYearDate(year, month, 1)
    pstart = tcnv.convertDateToCTime(year, ydate)
    ydate = tcnv.findYearDate(end_year, end_month, 1)
    pend = tcnv.convertDateToCTime(end_year, ydate)

    mdate = 0.5 * (pstart + pend)
    #
    #--- extract sim position information
    #
    twoarray = extract_sim_position(year, pstart, pend)

    sim_time = twoarray[0]
    sim_pos = twoarray[1]
    #
    #----extract acis hist data from archive using arc5gl
    #

    data_list = use_arc5gl_acis_hist(year, month, end_year, end_month)
    #
    #--- create data array: histogram row: 4000, ccd #: 10, node #: 4, position: 0, 1
    #
    hdata = numpy.zeros((4000, 10, 4, 2))
    duration = numpy.zeros((10, 4, 2))
    #
    #--- go though each extracted acis hist data
    #
    for file in data_list:

        atemp = re.split('\/', file)
        fname = atemp[len(atemp) - 1]
        fname = fname.replace('.fits', '')
        #
        #--- extract head info
        #
        ccd_info = extract_head_info(file)
        [fep, ccd, node, pblock, tstart, tstop, expcount, date_obs,
         date_end] = ccd_info
        tdiff = float(tstop) - float(tstart)
        #
        #--- we need to check only ccd = 1, 2, 3, 6, and 7
        #
        ccd = int(ccd)
        if (ccd == 1) or (ccd == 2) or (ccd == 3) or (ccd == 6) or (ccd == 7):

            pblock = int(pblock)
            tstart = int(tstart)
            tstop = int(tstop)
            #
            #--- find average sim position
            #
            sim_info = find_sim_position(sim_time, sim_pos, tstart, tstop)
            [sim_avg, sim_min, sim_max] = sim_info
            #
            #--- for the case that the sim is at the external calibration source position
            #
            if (sim_min > -100800
                    and sim_min < -98400) and (sim_max > -100800
                                               and sim_max < -98400):
                #
                #
                #--- pblock values were changed in year 2012 (2012.1.1 == 441763197). ---------
                #
                if float(tstart) < 441763197:
                    #
                    #--- ccd rows are between 801 and 1001 before Year 2012
                    #
                    if pblock == 2334740:
                        hist_data = extract_hist_data(file)
                        duration[ccd][node][1] += tdiff
                        for i in range(0, 4000):
                            hdata[i][ccd][node][1] += hist_data[i]
#
#--- ccd rows are between 21 and 221 before Year 2012
#
                    elif pblock == 2342932:
                        hist_data = extract_hist_data(file)
                        duration[ccd][node][0] += tdiff
                        for i in range(0, 4000):
                            hdata[i][ccd][node][0] += hist_data[i]

#
#--- after 2012.1.1 ---------
#
                else:
                    #
                    #--- ccd rows are between 801 and 1001 from Year 2012
                    #
                    if pblock == 2334756:
                        hist_data = extract_hist_data(file)
                        duration[ccd][node][1] += tdiff
                        for i in range(0, 4000):
                            hdata[i][ccd][node][1] += hist_data[i]
#
#--- ccd rows are between 21 and 221 from Year 2012
#
                    elif pblock == 2342948:
                        hist_data = extract_hist_data(file)
                        duration[ccd][node][0] += tdiff
                        for i in range(0, 4000):
                            hdata[i][ccd][node][0] += hist_data[i]

        mcf.rm_file(file)

#
#--- now fit the parameters for a combined data set
#
    test_out = []
    for pos in (0, 1):
        for ccd in (1, 2, 3, 6, 7):
            for node in range(0, 4):
                darray = [0 for x in range(0, 4000)]
                sum = 0
                for i in range(0, 4000):
                    darray[i] = hdata[i][ccd][node][pos]
                    sum += hdata[i][ccd][node][pos]
                if sum > 10:
                    peak_info = find_peaks(darray)

                    if pos == 0:
                        lpos = 'low'
                    else:
                        lpos = 'high'
                    if comp_test != 'test':
                        file = data_dir + 'ccd' + str(ccd) + '_node' + str(
                            node) + '_' + lpos
                        fo = open(file, 'a')
                        line = str(year) + ':' + str(month) + '\t' + str(
                            mdate) + '\t'
                        line = line + str(peak_info[0]) + '\t' + str(
                            peak_info[9]) + '\t'
                        line = line + str(peak_info[1]) + '\t' + str(
                            peak_info[10]) + '\t'
                        line = line + str(peak_info[2]) + '\t' + str(
                            peak_info[11]) + '\t'
                        line = line + str(peak_info[3]) + '\t' + str(
                            peak_info[12]) + '\t'
                        line = line + str(peak_info[4]) + '\t' + str(
                            peak_info[13]) + '\t'
                        line = line + str(peak_info[5]) + '\t' + str(
                            peak_info[14]) + '\t'
                        line = line + str(peak_info[6]) + '\t' + str(
                            peak_info[15]) + '\t'
                        line = line + str(peak_info[7]) + '\t' + str(
                            peak_info[16]) + '\t'
                        line = line + str(peak_info[8]) + '\t' + str(
                            peak_info[17]) + '\t'
                        line = line + str(duration[ccd][node][pos]) + '\n'
                        fo.write(line)
                        fo.close()

                    lmonth = str(month)
                    if month < 10:
                        lmonth = '0' + lmonth
                    fname = 'hist_' + str(year) + '_' + lmonth

                    eparam = peak_info[0:9]

                    if comp_test != 'test':
                        plot_fit(darray, eparam, fname, ccd, node, lpos)
                    else:
                        test_out.append(eparam)

    if comp_test == 'test':
        return test_out
def sci_run_add_to_rad_zone_list(file='NA'):

    'adding radiation zone list to rad_zone_list. input: file name containing: e.g. 20120313        2012:03:13:22:41        2012:03:14:13:57         53.3   auto'
    
#
#--- if file is not given (if it is NA), ask the file input
#

    if file == 'NA':
        file = raw_input('Please put the intrrupt timing list: ')

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

    for ent in data:

#
#--- a starting date of the interruption in yyyy:mm:dd:hh:mm (e.g., 2006:03:20:10:30)
#--- there could be multiple lines of date; in that is the case, the scripts add the rad zone list
#--- to each date
#

        etemp = re.split('\s+', ent)
    
        atemp = re.split(':', etemp[1])
        year  = atemp[0]
        month = atemp[1]
        date  = atemp[2]
        hour  = atemp[3]
        mins  = atemp[4]

#
#--- convert to dom/sec1998
#
        ydate = tcnv.findYearDate(int(year), int(month), int(date))
        dom   = tcnv.findDOM(int(year), int(ydate))
        line  = year + ':' + str(int(ydate)) + ':' + hour + ':' + mins + ':00'
        csec  = tcnv.axTimeMTA(line)

#
#--- end date
#

        atemp  = re.split(':', etemp[2])
        eyear  = atemp[0]
        emonth = atemp[1]
        edate  = atemp[2]
        ehour  = atemp[3]
        emins  = atemp[4]
 
        ydate = tcnv.findYearDate(int(eyear), int(emonth), int(edate))
        dom2  = tcnv.findDOM(int(eyear), int(ydate))
        line  = eyear + ':' + str(int(ydate)) + ':' + ehour + ':' + emins + ':00'
        csec2 = tcnv.axTimeMTA(line)

#
#--- date stamp for the list
#
        list_date = str(year) + str(month) + str(date)

#
#--- check radiation zones for 3 days before to 5 days after from the interruptiondate
#

        begin = dom - 3
        end   = dom2 + 5

#
#--- read radiation zone infornation
#

        infile = house_keeping + '/rad_zone_info'
        f      = open(infile, 'r')
        rdata  = [line.strip() for line in f.readlines()]
        f.close()
    
        status = []
        rdate  = []
        chk    = 0
        last_st= ''
        cnt    = 0
    
        for line in rdata:
            atemp = re.split('\s+', line)
    
            dtime = float(atemp[1])                 #--- dom of the entry or exit
    
            if chk  == 0 and atemp[0] == 'ENTRY' and dtime >= begin:
                status.append(atemp[0])
                rdate.append(dtime)
                chk += 1
                last_st = atemp[0]
                cnt += 1
            elif chk > 0 and dtime >= begin and dtime <= end:
                status.append(atemp[0])
                rdate.append(dtime)
                last_st = atemp[0]
                cnt += 1
            elif atemp[1] > end and last_st == 'EXIT':
                break
            elif atemp[1] > end and last_st == 'ENTRY':
                status.append(atemp[0])
                rdate.append(dtime)
                cnt += 1
                break
            
        f = open('./temp_zone', 'w')

#
#--- a format of the output is, e.g.: '20120313    (4614.2141112963,4614.67081268519):...'
#

        line = list_date + '\t'
        f.write(line)
    
        i = 0
        while i < cnt:
            line = '(' + str(rdate[i]) + ','
            f.write(line)
            i += 1
            if i < cnt-1:
                line = str(rdate[i]) + '):'
                f.write(line)
            else:
                line = str(rdate[i]) + ')\n'
                f.write(line)
            i += 1
    
        f.close()

#
#--- append the past rad zone list 
#

        oldFile = house_keeping + '/rad_zone_list~'
        crtFile = house_keeping + '/rad_zone_list'

#>        cmd = 'cat ' './temp_zone ' + crtFile + ' > /tmp/mta/temp_comb'
        cmd = 'cat '+ './temp_zone ' + crtFile +  ' > ./temp_comb'
        os.system(cmd)

#>        os.systm('rm /tmp/mta/temp_zone')
        os.system('rm ./temp_zone')

#
#--- save the old file and move the update file to rad_zone_list
#

        cmd     = 'chmod 775 ' + crtFile + ' ' +  oldFile
        os.system(cmd)
    
        cmd     = 'mv ' + crtFile + ' ' + oldFile
        os.system(cmd)
    
        cmd     = 'chmod 644 ' +  oldFile
        os.system(cmd)
    
#>        cmd     = 'mv  ' + '/tmp/mta/temp_comb ' + crtFile
        cmd     = 'mv  ' + './temp_comb ' + crtFile
        os.system(cmd)
示例#15
0
def acis_hist_extract_data(year, month):

    #
    #--- define extracting period in seconds from 1.1.1998
    #
    end_year = year
    end_month = month + 1
    if end_month > 12:
        end_month = 1
        end_year += 1

    ydate = tcnv.findYearDate(year, month, 1)
    period_start = tcnv.convertDateToCTime(year, ydate)
    ydate = tcnv.findYearDate(end_year, end_month, 1)
    period_end = tcnv.convertDateToCTime(end_year, ydate)
    #
    #--- extract sim position information
    #
    twoarray = extract_sim_position(year, period_start, period_end)

    sim_time = twoarray[0]
    sim_pos = twoarray[1]

    #
    #----extract acis hist data from archive using arc4gl
    #

    prep_for_arc4gl()

    data_list = use_arc4gl_acis_hist(year, month, end_year, end_month)
    #
    #--- go though each extracted acis hist data
    #
    for file in data_list:
        #
        #---- isolate id of the file
        #
        atemp = re.split('\/', file)
        btemp = re.split('\.fits', atemp[len(atemp) - 1])
        name = btemp[0]
        #
        #--- extract head info
        #
        ccd_info = extract_head_info(file)
        [fep, ccd, node, pblock, tstart, tstop, expcount, date_obs,
         date_end] = ccd_info

        #
        #--- extract hist data
        #
        hist_data = extract_hist_data(file)

        #
        #--- find average sim position
        #
        sim_info = find_sim_position(sim_time, sim_pos, tstart, tstop)
        [sim_avg, sim_min, sim_max] = sim_info

        #
        #--- open info file
        #
        info_file = dir_name + 'CCD' + ccd + '/info_file'
        wrt = open(info_file, 'a')

        #
        #--- for the case that the sim is at the external calibration source position
        #

        if (sim_min > -100800 and sim_min < -98400) and (sim_max > -100800
                                                         and sim_max < -98400):

            #
            #--- ccd raws are between 801 and 1001
            #

            if int(pblock) == 2334740:
                print file
                peak_info = find_peaks(hist_data)

                line = name + ': source: high\n'
                wrt.write(line)

                print_sim_data(peak_info, sim_info, ccd_info, 'high', web_dir,
                               name)
                plot_fit(hist_data, peak_info, name, ccd, node, 'high')
#
#--- ccd raws are between 21 and 221
#
            if int(pblock) == 2342932:
                print file
                peak_info = find_peaks(hist_data)

                line = name + ': source: low\n'
                wrt.write(line)

                print_sim_data(peak_info, sim_info, ccd_info, 'low', web_dir,
                               name)
                plot_fit(hist_data, peak_info, name, ccd, node, 'low')

        cmd = 'rm ' + file
        os.system(cmd)

        wrt.close()
def sci_run_add_to_rad_zone_list(file='NA'):

    'adding radiation zone list to rad_zone_list. input: file name containing: e.g. 20120313        2012:03:13:22:41        2012:03:14:13:57         53.3   auto'
    
#
#--- check whether the list alread exists; if it does, read which ones are already in the list
#
    cmd = 'ls ' + house_keeping + '* > ./ztemp'
    os.system(cmd)
#    f    = open('./ztemp', 'r')
#    test = f.readlines()
#    f.close()
    test = open('./ztemp').read()
 
    m1   = re.search('rad_zone_list',  test)
    m2   = re.search('rad_zone_list~', test)

    eventList = []
    echk      = 0
    if m1 is not None:
        line = house_keeping + 'rad_zone_list'
        f    = open(line, 'r')
        data = [line.strip() for line in f.readlines()]
        f.close()

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


#
#--- if file is not given (if it is NA), ask the file input
#

    if file == 'NA':
        file = raw_input('Please put the intrrupt timing list: ')

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

#
#--- put the list in the reverse order
#
    data.reverse()

    for ent in data:
        if not ent:
            break

#
#--- a starting date of the interruption in yyyy:mm:dd:hh:mm (e.g., 2006:03:20:10:30)
#--- there could be multiple lines of date; in that is the case, the scripts add the rad zone list
#--- to each date
#

        etemp = re.split('\s+', ent)
        echk = 0
        for comp in eventList:
           if comp == etemp[0]:
               echk = 1
               break

        if echk == 0:
    
            atemp = re.split(':', etemp[1])
            year  = atemp[0]
            month = atemp[1]
            date  = atemp[2]
            hour  = atemp[3]
            mins  = atemp[4]

#
#--- convert to dom/sec1998
#
            ydate = tcnv.findYearDate(int(year), int(month), int(date))
            dom   = tcnv.findDOM(int(year), int(ydate))
            line  = year + ':' + str(int(ydate)) + ':' + hour + ':' + mins + ':00'
            csec  = tcnv.axTimeMTA(line)

#
#--- end date
#

            atemp  = re.split(':', etemp[2])
            eyear  = atemp[0]
            emonth = atemp[1]
            edate  = atemp[2]
            ehour  = atemp[3]
            emins  = atemp[4]
     
            ydate = tcnv.findYearDate(int(eyear), int(emonth), int(edate))
            line  = eyear + ':' + str(int(ydate)) + ':' + ehour + ':' + emins + ':00'
            csec2 = tcnv.axTimeMTA(line)

#
#--- date stamp for the list
#
            list_date = str(year) + str(month) + str(date)

#
#--- check radiation zones for 3 days before to 5 days after from the interruptiondate
#--- if the interruption lasted longer than 5 days, extend the range 7 more days
#

            begin = dom - 3
            end   = dom + 5

            diff = csec2 - csec
            if diff > 432000:
                end += 7

#
#--- read radiation zone infornation
#

            infile = house_keeping + '/rad_zone_info'
            f      = open(infile, 'r')
            rdata  = [line.strip() for line in f.readlines()]
            f.close()
        
            status = []
            rdate  = []
            chk    = 0
            last_st= ''
            cnt    = 0
        
            for line in rdata:
                atemp = re.split('\s+', line)
        
                dtime = float(atemp[1])                 #--- dom of the entry or exit
        
                if chk  == 0 and atemp[0] == 'ENTRY' and dtime >= begin:
                    status.append(atemp[0])
                    rdate.append(dtime)
                    chk += 1
                    last_st = atemp[0]
                    cnt += 1
                elif chk > 0 and dtime >= begin and dtime <= end:
                    status.append(atemp[0])
                    rdate.append(dtime)
                    last_st = atemp[0]
                    cnt += 1
                elif atemp[1] > end and last_st == 'EXIT':
                    break
                elif atemp[1] > end and last_st == 'ENTRY':
                    status.append(atemp[0])
                    rdate.append(dtime)
                    cnt += 1
                    break
            
            f = open('./temp_zone', 'w')

#
#--- a format of the output is, e.g.: '20120313    (4614.2141112963,4614.67081268519):...'
#

            line = list_date + '\t'
            f.write(line)
        
            upper = cnt -1
            i = 0;
            while i < cnt:
                line = '(' + str(rdate[i]) + ','
                f.write(line)
                i += 1
                if i < upper:
                    line = str(rdate[i]) + '):'
                    f.write(line)
                else:
                    line = str(rdate[i]) + ')\n'
                    f.write(line)
                i += 1
        
            f.close()

#
#--- append the past rad zone list 
#

            oldFile = house_keeping + '/rad_zone_list~'
            crtFile = house_keeping + '/rad_zone_list'
    
            if m1 is not None:
                cmd = 'cat '+ './temp_zone ' + crtFile +  ' > ./temp_comb'
                os.system(cmd)
    
            else:
                os.system('mv .temp_zone ./temp_comb')

            os.system('rm ./temp_zone')

#
#--- save the old file and move the update file to rad_zone_list
#

            if m2 is not None:
                cmd     = 'chmod 775 ' + crtFile + ' ' +  oldFile
                os.system(cmd)
        
            if m1 is not None:
                cmd     = 'mv ' + crtFile + ' ' + oldFile
                os.system(cmd)
        
                cmd     = 'chmod 644 ' +  oldFile
                os.system(cmd)
        
            cmd     = 'mv  ' + './temp_comb ' + crtFile
            os.system(cmd)
示例#17
0
def get_new_value(year, month):

    """
    extract aorwspd values from dataseeker
    input: year/month
    output: [av1, av2, av3, av4, av5, av6] --- six values of aorwspd(1-6)
    """
#
#--- set month long time interval in sec from 1998.1.1
#
    year2 = year
    month2 = month -1
    if month2 < 1:
        mont2  = 12
        year2 -= 1

    ydate = tcnv.findYearDate(year,  month,  15)
    t_in  = str(year) + ':' + str(ydate) + ':00:00:00'
    time1 = tcnv.axTimeMTA(t_in)
    ydate = tcnv.findYearDate(year2, month2, 15)
    t_in  = str(year2) + ':' + str(ydate) + ':00:00:00'
    time2 = tcnv.axTimeMTA(t_in)
#
#--- set command to call dataseeker
#
    f    = open('./test', 'w')      #-- we need an empty "test" file to run dataseeker
    f.close()
    mcf.mk_empty_dir('param')       #-- make empty param directory

    line = 'columns=_aorwspd1_avg,'
    line = line + '_aorwspd2_avg,'
    line = line + '_aorwspd3_avg,'
    line = line + '_aorwspd4_avg,'
    line = line + '_aorwspd5_avg,'
    line = line + '_aorwspd6_avg'
    line = line + ' timestart=' + str(time2)
    line = line + ' timestop='  + str(time1)

    cmd = 'punlearn dataseeker;  dataseeker.pl infile=test outfile=ztemp.fits search_crit=\''
    cmd = 'dataseeker.pl infile=test outfile=ztemp.fits search_crit="'
    cmd = cmd + line + '"  loginFile="' + loginfile + '"'

#
#--- run dataseeker
#
    bash("/usr/bin/env PERL5LIB='' " + cmd, env=ascdsenv)

    mcf.rm_file(zspace)
    mcf.rm_file('./test')
    os.system('rm -rf ./param')
#
#--- read fits file
#
    try:
        dout = pyfits.getdata('./ztemp.fits')
        aw1  = dout.field('AORWSPD1_AVG')
        aw2  = dout.field('AORWSPD2_AVG')
        aw3  = dout.field('AORWSPD3_AVG')
        aw4  = dout.field('AORWSPD4_AVG')
        aw5  = dout.field('AORWSPD5_AVG')
        aw6  = dout.field('AORWSPD6_AVG')
    except:
        aw1 = aw2 = aw3 = aw4 = aw5 = aw6 = []

    mcf.rm_file("./ztemp.fits")
#
#--- create monthly "sum" of the reaction wheel rotations
#--- dataseeker gives 5 min avg of the value; one day is 24 hr x 60 min / 5min =  288.
#
    sum1 = sum2 = sum3 = sum4 = sum5 = sum6 = 0.0
    for i in range(0, len(aw1)):
        sum1 += abs(aw1[i])
        sum2 += abs(aw2[i])
        sum3 += abs(aw3[i])
        sum4 += abs(aw4[i])
        sum5 += abs(aw5[i])
        sum6 += abs(aw6[i])

    av1 = sum1 / 288
    av2 = sum2 / 288
    av3 = sum3 / 288
    av4 = sum4 / 288
    av5 = sum5 / 288
    av6 = sum6 / 288

    return [av1, av2, av3, av4, av5, av6]
def acis_sci_run_print_html(html_dir, pyear, pmonth, pday,  pchk):
    """
    update three html pages according for the year (pyear)
    Input: html_dir --- web directory path
           pyear  --- the year you want to update the html pages
           pmonth --- current month
           pday   --- current month date
           pchk   --- if it is "yes", it will also update the main html, otherwise, just update subs
    Output: science_run.html
            science_long_term.html
            science_run<year>.html

    """
    pyear = int(pyear)

    update = str(pyear) + '-' + str(pmonth) + '-' + str(pday)
    dom    = int(tcnv.findDOM(pyear, pmonth, pday, 0, 0, 0))
    ydate  = tcnv.findYearDate(pyear, pmonth, pday)
#
#---- update the main html page
#
    if pchk == 'yes':
        ylist = ''
        j    = 0
        for ryear in range(1999, pyear+1):
            ylist = ylist + '<td><a href=' + http_dir + '/Year' + str(ryear) + '/science_run' + str(ryear) + '.html>'
            ylist = ylist + '<strong>Year ' + str(ryear) + '</strong></a><br /><td />\n'
            if j == 5:
                j = 0
                ylist = ylist + '</tr><tr>\n'
            else:
                j += 1
    
    
        line  = house_keeping + 'science_run.html'
        f     = open(line, 'r')
        hfile = f.read()
        f.close()
        temp  = hfile.replace('#UPDATE#', update)
        temp1 = temp.replace('#DOY#',     str(ydate))
        temp2 = temp1.replace('#DOM#',    str(dom))
        temp3 = temp2.replace('#YEAR#',   str(pyear))
        temp4 = temp3.replace('#YLIST#',  str(ylist))
    
        line = html_dir + 'science_run.html'
        f    = open(line, 'w')
        f.write(temp4)
        f.close()

#
#--- update this year's sub directory html page
#

    line  = house_keeping + 'sub_year.html'
    f     = open(line, 'r')
    hfile = f.read()
    f.close()
    temp  = hfile.replace('#UPDATE#', update)
    temp1 = temp.replace('#DOY#',     str(ydate))
    temp2 = temp1.replace('#DOM#',    str(dom))
    temp3 = temp2.replace('#YEAR#',   str(pyear))

    line = html_dir + 'Year' + str(pyear) + '/science_run' + str(pyear) + '.html'
    f    = open(line, 'w')
    f.write(temp3)
    f.close()
#
#--- update long term html page
#
    line  = house_keeping + 'science_long_term.html'
    f     = open(line, 'r')
    hfile = f.read()
    f.close()
    temp  = hfile.replace('#UPDATE#', update)
    temp1 = temp.replace('#DOY#',     str(ydate))
    temp2 = temp1.replace('#DOM#',    str(dom))

    line = html_dir + 'Long_term/science_long_term.html'
    f    = open(line, 'w')
    f.write(temp2)
    f.close()