Exemplo n.º 1
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)
Exemplo n.º 2
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(float(startYear))
        startMonth = raw_input('Start Month: ')
        startMonth = int(float(startMonth))

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

        lyear = str(year)

        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 ar5gl, get file names
#
            start = lyear + '-' + smonth + '-01T00:00:00'

            nextMonth = month + 1
            nyear = int(float(lyear))
            if nextMonth > 12:
                nextMonth = 1
                nyear += 1
            syear = str(nyear)
            smon  = str(nextMonth)
            if nextMonth < 10:
                smon = '0' + smon
            stop = syear + '-' + smon + '-01T00:00:00'

            line = 'operation=browse\n'
            line = line + 'dataset=flight\n'
            line = line + 'detector=acis\n'
            line = line + 'level=1\n'
            line = line + 'filetype=evt1\n'
            line = line + 'tstart=' + start + '\n'
            line = line + 'tstop=' +  stop  + '\n'
            line = line + 'go\n'
            f    = open('./zspace', 'w')
            f.write(line)
            f.close()
            cmd1 = "/usr/bin/env PERL5LIB= "
            #cmd2 =  ' /proj/axaf/simul/bin/arc5gl -user isobe -script ./zspace > ./zout'
            cmd2 =  ' /proj/sot/ska/bin/arc5gl -user isobe -script ./zspace > ./zout'
            cmd  = cmd1 + cmd2
            #bash(cmd,  env=ascdsenv)
            os.system(cmd2)
            mtac.rm_file('./zspace')

            f    = open('./zout', 'r')
            fitsList = [line.strip() for line in f.readlines()]
            f.close()
            mtac.rm_file('./zout')
#
#--- extract each evt1 file, extract the central part, and combine them into a one file
#
            i = 0
            acisCnt = 0

            for  line in fitsList:
                m = re.search('fits', line)
                if m is not None:
                    atemp = re.split('\s+', line)
                    file  = atemp[0]
                    line = 'operation=retrieve\n'
                    line = line + 'dataset=flight\n'
                    line = line + 'detector=acis\n'
                    line = line + 'level=1\n'
                    line = line + 'filetype=evt1\n'
                    line = line + 'filename=' + file + '\n'
                    line = line + 'go\n'
                    f    = open('./zspace', 'w')
                    f.write(line)
                    f.close()
                    cmd1 = "/usr/bin/env PERL5LIB="
                    #cmd2 =  ' /proj/axaf/simul/bin/arc5gl -user isobe -script ./zspace'
                    cmd2 =  ' /proj/sot/ska/bin/arc5gl -user isobe -script ./zspace > ./zout'
                    cmd  = cmd1 + cmd2
                    try:
                        #bash(cmd,  env=ascdsenv)
                        os.system(cmd2)
                        mtac.rm_file('./zspace')
                    except:
                        continue
                        
                    cmd = 'gzip -d ' + file + '.gz'
                    os.system(cmd)
                    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'
            cmd1 = "/usr/bin/env PERL5LIB="
            cmd2 = ' dmimgthresh infile=' + outfile + ' outfile=' + outfile2 + ' cut="0:' + str(upper) + '" value=0 clobber=yes'
            cmd = cmd1 + cmd2
            bash(cmd,  env=ascdsenv)

            cmd   = 'gzip ' + outfile
            os.system(cmd)
Exemplo n.º 3
0
def acis_dose_get_data(startYear='', startMonth='', stopYear='', stopMonth=''):
    """
    extract ACIS evt1 data from a month and create combined image file. 
    input:  startYear   --- year of starting time
            startMonth  --- month of starting time
            stopYear    --- year of stopping time
            stopMonth   --- month of stopping time
    """
    if startYear == '' or startMonth == '' or stopYear == '' or stopMonth == '':

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

        stopYear = raw_input('Stop Year: ')
        stopYear = int(float(stopYear))
        stopMonth = raw_input('Stop Month: ')
        stopMonth = int(float(stopMonth))
#
#--- start extracting the data for the year/month period
#
    for year in range(startYear, stopYear + 1):
        #
        #--- create a list of month appropriate for the year
        #
        month_list = expf.make_month_list(year, startYear, stopYear,
                                          startMonth, stopMonth)

        for month in month_list:
            smon = mcf.add_leading_zero(month)
            start = str(year) + '-' + smon + '-01T00:00:00'

            nextMonth = month + 1
            nyear = year
            if nextMonth > 12:
                nextMonth = 1
                nyear += 1
            smon = mcf.add_leading_zero(nextMonth)
            stop = str(nyear) + '-' + smon + '-01T00:00:00'
            #
            #--- using ar5gl, get a list of file names
            #
            line = 'operation=browse\n'
            line = line + 'dataset=flight\n'
            line = line + 'detector=acis\n'
            line = line + 'level=1\n'
            line = line + 'filetype=evt1\n'
            line = line + 'tstart=' + start + '\n'
            line = line + 'tstop=' + stop + '\n'
            line = line + 'go\n'

            fitsList = mcf.run_arc5gl_process(line)
            #
            #--- extract each evt1 file, extract the central part, and combine them into a one file
            #
            for fits in fitsList:
                print("FITS File: " + fits)
                atemp = re.split('\s+', line)
                line = 'operation=retrieve\n'
                line = line + 'dataset=flight\n'
                line = line + 'detector=acis\n'
                line = line + 'level=1\n'
                line = line + 'filetype=evt1\n'
                line = line + 'filename=' + fits + '\n'
                line = line + 'go\n'

                out = mcf.run_arc5gl_process(line)
                #
                #--- check whether the fits file actually extracted and if so, ungip the file
                #
                if len(out) < 1:
                    continue
                cmd = 'gzip -d ' + out[0]
                os.system(cmd)

                line = fits + '[EVENTS][bin tdetx=2800:5200:1, tdety=1650:4150:1][option type=i4]'
                #
                #--- create an image file
                #
                ichk = expf.create_image(line, 'ztemp.fits')
                #
                #--- combined images
                #
                if ichk > 0:
                    expf.combine_image('ztemp.fits', 'total.fits')

                mcf.rm_files(fits)
                mcf.rm_files('ztemp.fits')
#
#--- rename the file
#
            lyear = str(startYear)
            lmon = mcf.add_leading_zero(startMonth)
            outfile = './ACIS_' + lmon + '_' + lyear + '_full.fits'
            cmd = 'mv total.fits ' + outfile
            os.system(cmd)
            #
            #--- trim the extreme values
            #
            upper = find_10th(outfile)
            if mcf.is_neumeric(upper):
                outfile2 = './ACIS_' + lmon + '_' + lyear + '.fits'
                cmd = ' dmimgthresh infile=' + outfile + ' outfile='
                cmd = cmd + outfile2 + ' cut="0:' + str(
                    upper) + '" value=0 clobber=yes'
                expf.run_ascds(cmd)
            else:
                cmd = 'cp -f ' + outfile + ' ' + outfile2
                os.system(cmd)

            cmd = 'gzip ' + outfile
            os.system(cmd)
            #
            #--- move full one to the data dir; keep other in <exc_dir> to be used to create cumlative files
            #
            cmd = 'mv ' + outfile + '* ' + mon_acis_dir + '/.'
            os.system(cmd)
Exemplo n.º 4
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)
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_' + str(smonth) + '_' + str(lyear) + '.fits'
            outfile_s_c = './HRCS_' + str(smonth) + '_' + str(lyear) + '.fits'

            outfile_i = ['NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA']
            for i in range(0, 9):
                outfile_i[i] = './HRCI_' + str(smonth) + '_' + str(
                    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_' + str(smonth) + '_' + str(
                    lyear) + '_' + str(i) + '.fits'

#
#--- using ar5gl, get file names
#
            smonth = str(month)
            if month < 10:
                smonth = '0' + smonth

            syear = str(year)
            #start = smonth + '/01/' + syear[2] + syear[3] + ',00:00:00'
            start = syear + '-' + smonth + '-01T00:00:00'

            nextMonth = month + 1
            if nextMonth > 12:
                lyear = year + 1
                nextMonth = 1
            else:
                lyear = year

            smonth = str(nextMonth)
            if nextMonth < 10:
                smonth = '0' + smonth

            syear = str(lyear)
            #stop  = smonth + '/01/' + syear[2] + syear[3] + ',00:00:00'
            stop = str(lyear) + '-' + smonth + '-01T00:00:00'

            line = 'operation=browse\n'
            line = line + 'dataset=flight\n'
            line = line + 'detector=hrc\n'
            line = line + 'level=1\n'
            line = line + 'filetype=evt1\n'
            line = line + 'tstart=' + start + '\n'
            line = line + 'tstop=' + stop + '\n'
            line = line + 'go\n'
            f = open('./zspace', 'w')
            f.write(line)
            f.close()
            cmd1 = "/usr/bin/env PERL5LIB="
            #cmd2 =  '  /proj/axaf/simul/bin/arc5gl -user isobe -script ./zspace > ./zout'
            cmd2 = ' /proj/sot/ska/bin/arc5gl -user isobe -script ./zspace > ./zout'
            cmd = cmd1 + cmd2
            #bash(cmd,  env=ascdsenv)
            os.system(cmd2)
            mtac.rm_file('./zspace')

            f = open('./zout', 'r')
            fitsList = [line.strip() for line in f.readlines()]
            f.close()
            mtac.rm_file('./zout')

            #
            #--- 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

            chk = 0
            for line in fitsList:
                m = re.search('fits', line)
                if m is None:
                    continue
                chk = 1

                try:
                    #                    [fitsName] = mtac.useArc4gl('retrieve','flight', 'hrc', 1, 'evt1', filename=file)
                    atemp = re.split('\s+', line)
                    fitsName = atemp[0]
                    line = 'operation=retrieve\n'
                    line = line + 'dataset=flight\n'
                    line = line + 'detector=hrc\n'
                    line = line + 'level=1\n'
                    line = line + 'filetype=evt1\n'
                    line = line + 'filename=' + fitsName + '\n'
                    line = line + 'go\n'
                    f = open('./zspace', 'w')
                    f.write(line)
                    f.close()
                    cmd1 = "/usr/bin/env PERL5LIB="
                    #cmd2 =  ' /proj/axaf/simul/bin/arc5gl -user isobe -script  ./zspace'
                    cmd2 = ' /proj/sot/ska/bin/arc5gl -user isobe -script ./zspace > ./zout'
                    cmd = cmd1 + cmd2
                    #bash(cmd,  env=ascdsenv)
                    os.system(cmd2)
                    mtac.rm_file('./zspace')

                    cmd = 'gzip -d ' + fitsName + '.gz'
                    os.system(cmd)
                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 chk > 0:
                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)

                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-S', 'center', dp_web_dir, i=0)
            createCumulative(year, month, 'HRC-I', 'center', dp_web_dir, i=0)
def hrc_dose_get_data(startYear='NA',
                      startMonth='NA',
                      stopYear='NA',
                      stopMonth='NA'):
    """
    extract HRC evt1 data from a month and create cumulative data fits file. 
    input:  startYear   --- start year 
            startMonth  --- start month 
            stopYear    --- stop year 
            stopMonth   --- stop month
    output: image fits files for the month and cumulative cases
    """
    #
    #--- if the dates are not given, set them to the last month
    #
    if startYear == 'NA':
        [stopYear, stopMonth, day] = mcf.today_date()

        startYear = stopYear
        stopMonth -= 1
        startMonth = stopMonth
        if startMonth < 1:
            startMonth = 12
            startYear -= 1
#
#--- start extracting the data for the year/month period
#
    for year in range(startYear, stopYear + 1):
        lyear = str(year)
        syear = lyear[2] + lyear[3]
        #
        #--- create a list of month appropriate for the year
        #
        month_list = expf.make_month_list(year, startYear, stopYear,
                                          startMonth, stopMonth)

        for month in month_list:
            smonth = mcf.add_leading_zero(month)
            #
            #--- output file name settings
            #
            outfile_i = './HRCI_' + str(smonth) + '_' + str(lyear) + '.fits'
            outfile_s = './HRCS_' + str(smonth) + '_' + str(lyear) + '.fits'
            #
            #--- using ar5gl, get file names
            #
            smonth = mcf.add_leading_zero(month)
            syear = str(year)
            start = syear + '-' + smonth + '-01T00:00:00'

            nextMonth = month + 1
            if nextMonth > 12:
                lyear = year + 1
                nextMonth = 1
            else:
                lyear = year

            smonth = mcf.add_leading_zero(nextMonth)
            syear = str(lyear)
            stop = str(lyear) + '-' + smonth + '-01T00:00:00'

            line = 'operation=browse\n'
            line = line + 'dataset=flight\n'
            line = line + 'detector=hrc\n'
            line = line + 'level=1\n'
            line = line + 'filetype=evt1\n'
            line = line + 'tstart=' + start + '\n'
            line = line + 'tstop=' + stop + '\n'
            line = line + 'go\n'

            fitsList = mcf.run_arc5gl_process(line)
            #
            #--- extract each evt1 file, extract the central part, and combine them into a one file
            #
            #--- set counters for how many hrc-i and hrc-s are extracted
            #
            hrci_cnt = 0
            hrcs_cnt = 0
            for fitsName in fitsList:
                print("Fits file: " + fitsName)
                m = re.search('fits', fitsName)
                if m is None:
                    continue
                try:
                    line = 'operation=retrieve\n'
                    line = line + 'dataset=flight\n'
                    line = line + 'detector=hrc\n'
                    line = line + 'level=1\n'
                    line = line + 'filetype=evt1\n'
                    line = line + 'filename=' + fitsName + '\n'
                    line = line + 'go\n'
                    out = mcf.run_arc5gl_process(line)

                    if len(out) < 1:
                        continue

                    ofits = out[0]
                    cmd = 'gzip -d ' + ofits
                    os.system(cmd)

                    ofits = ofits.replace('.gz', '')
                except:
                    continue
#
#--- checking which HRC (S or I)
#
                hout = pyfits.open(ofits)
                data = hout[1].header
                detector = data['DETNAM']
                hout.close()
                #
                #--- creating the center part image
                #
                line = set_cmd_line(ofits, detector)
                ichk = expf.create_image(line, 'ztemp.fits')
                #
                #--- for HRC S
                #
                if detector == 'HRC-S' and ichk > 0:
                    expf.combine_image('ztemp.fits', 'total_s.fits')
                    hrcs_cnt += 1
#
#--- for HRC I
#
                elif detector == 'HRC-I' and ichk > 0:
                    expf.combine_image('ztemp.fits', 'total_i.fits')
                    hrci_cnt += 1

                mcf.rm_files('out.fits')
                mcf.rm_files(ofits)
#
#--- move the file to a depository
#
            if hrcs_cnt > 0:
                cmd = 'mv total_s.fits ' + web_dir + 'Month_hrc/' + outfile_s
                os.system(cmd)
                cmd = 'gzip ' + web_dir + '/Month_hrc/*.fits'
                os.system(cmd)

            if hrci_cnt > 0:
                cmd = 'mv total_i.fits ' + web_dir + 'Month_hrc/' + outfile_i
                os.system(cmd)
                cmd = 'gzip ' + web_dir + '/Month_hrc/*.fits'
                os.system(cmd)

            createCumulative(year, month, 'HRC-S', web_dir)
            createCumulative(year, month, 'HRC-I', web_dir)
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 ar5gl, get file names
#
            smonth = str(month)
            if month < 10:
                smonth = '0' + smonth

            syear = str(year)
            #start = smonth + '/01/' + syear[2] + syear[3] + ',00:00:00'
            start = syear + '-' + smonth + '-01T00:00:00'

            nextMonth = month + 1
            if nextMonth > 12:
                lyear     = year + 1
                nextMonth = 1
            else:
                lyear  = year                

            smonth = str(nextMonth)
            if nextMonth < 10:
                smonth = '0' + smonth

            syear = str(lyear)
            #stop  = smonth + '/01/' + syear[2] + syear[3] + ',00:00:00'
            stop  = lyear + '-' + smonth + '-01T00:00:00'

            line = 'operation=browse\n'
            line = line + 'dataset=flight\n'
            line = line + 'detector=hrc\n'
            line = line + 'level=1\n'
            line = line + 'filetype=evt1\n'
            line = line + 'tstart=' + start + '\n'
            line = line + 'tstop=' +  stop  + '\n'
            line = line + 'go\n'
            f    = open('./zspace', 'w')
            f.write(line)
            f.close()
            cmd1 = "/usr/bin/env PERL5LIB="
            cmd2 =  '  /proj/axaf/simul/bin/arc5gl -user isobe -script ./zspace > ./zout'
            cmd  = cmd1 + cmd2
            bash(cmd,  env=ascdsenv)
            mtac.rm_file('./zspace')

            f    = open('./zout', 'r')
            fitsList = [line.strip() for line in f.readlines()]
            f.close()
            mtac.rm_file('./zout')

#
#--- 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 line in fitsList:
                m = re.search('fits', line)
                if m is None:
                    continue

                try:
#                    [fitsName] = mtac.useArc4gl('retrieve','flight', 'hrc', 1, 'evt1', filename=file)
                    atemp = re.split('\s+', line)
                    fitsName  = atemp[0]
                    line = 'operation=retrieve\n'
                    line = line + 'dataset=flight\n'
                    line = line + 'detector=hrc\n'
                    line = line + 'level=1\n'
                    line = line + 'filetype=evt1\n'
                    line = line + 'filename=' + fitsName + '\n'
                    line = line + 'go\n'
                    f    = open('./zspace', 'w')
                    f.write(line)
                    f.close()
                    cmd1 = "/usr/bin/env PERL5LIB="
                    cmd2 =  ' /proj/axaf/simul/bin/arc5gl -user isobe -script  ./zspace'
                    cmd  = cmd1 + cmd2
                    bash(cmd,  env=ascdsenv)
                    mtac.rm_file('./zspace')

                    cmd = 'gzip -d ' + fitsName + '.gz'
                    os.system(cmd)
                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)