Lv1_ngc300_mathgrp_pha.py
def mathpha(bin_size,filetype):
    """
    Function that takes in a bin size, and does MATHPHA on the set of pha files.
    The file names are already saved in the binned .ffphot files. The function
    will output pha files of the format 'MJD_binsize_' + filetype + '_cl50.pha'!

    bin_size - bin size in days
    filetype - either 'bgsub' or 'bg' or 'cl' or 'x' or 'xbgsub'
    x means "extra background"
    """

    normfile = Lv0_dirs.NGC300_2020 + 'n300_ulx.bgsub_cl50_g2020norm_' + bin_size + '.fffphot'
    mjds = np.genfromtxt(normfile,usecols=(0),unpack=True)
    spectra_files = np.genfromtxt(normfile,dtype='str',usecols=(9),unpack=True)

    for i in range(len(spectra_files)): #so for each BINNED MJD/counts
        exposures = []
        truncated_name = []
        spectra_files_list = spectra_files[i].split(',') #get a list where each entry = one pha file
        cl50_files = [spectra_files_list[j][:34] + spectra_files_list[j][40:49] + spectra_files_list[j][55:] for j in range(len(spectra_files_list))] #takes away the bgsub_ prefix

        if filetype == 'bg':
            filetype_spectra_files_list = [spectra_files_list[j][:36] + spectra_files_list[j][39:51] + spectra_files_list[j][54:] for j in range(len(spectra_files_list))] #takes away the 'sub' part of the name
        elif filetype == 'cl':
            filetype_spectra_files_list = [spectra_files_list[j][:34] + spectra_files_list[j][40:49] + spectra_files_list[j][55:] for j in range(len(spectra_files_list))] #takes away the bgsub_ prefix
        elif filetype == 'bgsub':
            filetype_spectra_files_list = spectra_files_list
        elif filetype == 'x':
            filetype_spectra_files_list = [spectra_files_list[j][:36] + spectra_files_list[j][39:45] + 'xpha/xbg' + spectra_files_list[j][54:] for j in range(len(spectra_files_list))]
        elif filetype == 'xbgsub':
            filetype_spectra_files_list = [spectra_files_list[j][:45] + 'xpha/xbgsub_' + spectra_files_list[j][55:] for j in range(len(spectra_files_list))]
        elif filetype == 'xsbg':
            filetype_spectra_files_list = [spectra_files_list[j][:36] + spectra_files_list[j][39:45] + 'xspha/xsbg' + spectra_files_list[j][54:] for j in range(len(spectra_files_list))]
        elif filetype == 'xsbgsub':
            filetype_spectra_files_list = [spectra_files_list[j][:45] + 'xspha/xsbgsub_' + spectra_files_list[j][55:] for j in range(len(spectra_files_list))]

        for j in range(len(filetype_spectra_files_list)): #for each individual pha file
            fits_file = fits.open(filetype_spectra_files_list[j])
            exptime = fits_file[1].header['EXPOSURE'] #get exposure time
            if filetype=='bg' or filetype=='x' or filetype=='xbgsub' or filetype=='xsbg' or filetype=='xsbgsub':
                exptime = fits.open(cl50_files[j])[1].header['EXPOSURE']
            exposures.append(exptime) #now we have a list of exposure times corresponding to each pha file for a given (binned) MJD/count rate

        total_exptime = sum(exposures) #get total exposure time for THAT MJD/count rate bin

        for j in range(len(filetype_spectra_files_list)):
            long_name = filetype_spectra_files_list[j]
            short_name = str(pathlib.Path(long_name).name)
            subprocess.run(['cp',long_name,short_name])
            truncated_name.append(short_name)

        #### now to build the mathpha command
        ################## NOTE, FOR BG/BGSUB FILES, MAKE SURE YOU USE EXPOSURE
        ################## TIMES FROM THE CL50 FILES... SINCE THAT'S HOW MUCH THE
        ################## OBSERVATION WAS EXPOSED FOR...
        mathpha = 'mathpha'
        calculation = ''
        for j in range(len(exposures)):
            if filetype == 'bg' or filetype == 'bgsub' or filetype == 'x' or filetype == 'xbgsub' or filetype == 'xsbg' or filetype == 'xsbgsub':
                if j == 0:
                    calculation += '('
                if j != len(exposures)-1:
                    if int(exposures[j]) == exposures[j]:
                        calculation += str(int(exposures[j])) + '*' + truncated_name[j] + '+'
                    else:
                        calculation += str(exposures[j]) + '*' + truncated_name[j] + '+'
                else:
                    calculation += str(exposures[j]) + '*' + truncated_name[j] + ') / ' + str(total_exptime)

            if filetype == 'cl':
                if j == 0:
                    calculation += '('
                if j != len(exposures)-1:
                    calculation += truncated_name[j] + '+'
                else:
                    calculation += truncated_name[j] + ')'

        if mjds[i] == 58449:
            print(calculation)

        if filetype == 'cl':
            unit = 'C'
        if filetype == 'bg' or filetype == 'bgsub' or filetype == 'x' or filetype == 'xbgsub' or filetype == 'xsbg' or filetype == 'xsbgsub':
            unit = 'R'
        outfile = 'outfil=' + str(int(mjds[i])) + '_' + bin_size + '_' + filetype + '_cl50.pha'
        exposure = 'exposure=' + str(total_exptime)
        errmeth = 'errmeth=gaussian'
        properr = 'properr=yes'
        ncomments = 'ncomments=0'
        areascal = 'areascal=NULL'
        clobber = 'clobber=YES'

        logfile = str(int(mjds[i])) + '_' + bin_size + '_' + filetype + '_mathpha.log'
        #print(mathpha+' "'+calculation+ '" '+unit+' '+outfile+' '+exposure+' '+errmeth+' '+properr+' '+ncomments+' '+areascal+' '+clobber)
        with open(logfile,'w') as logtextfile:
            output = subprocess.run([mathpha,calculation,unit,outfile,exposure,errmeth,properr,ncomments,areascal,clobber],capture_output=True,text=True)
            logtextfile.write(output.stdout)
            logtextfile.write('*------------------------------* \n')
            logtextfile.write(output.stderr)
            logtextfile.close()

    ### removing the temporary spectra
    for i in range(len(truncated_name)):
        subprocess.run(['rm','-r',truncated_name[i]])

    return

    Function that takes in a bin size, and does MATHPHA on the set of pha files.
    The file names are already saved in the binned .ffphot files. The function
    will output pha files of the format 'MJD_binsize_' + filetype + '_cl50.pha'!

    bin_size - bin size in days
    filetype - either 'bgsub' or 'bg' or 'cl' or 'x' or 'xbgsub'
    x means "extra background"
    
Function that takes in a bin size, and does MATHPHA on the set of pha files.
The file names are already saved in the binned .ffphot files. The function
will output pha files of the format 'MJD_binsize_' + filetype + '_cl50.pha'!


Function that takes in a bin size and does MATHPHA on the set of pha files . The file names are already saved in the binned . ffphot files . The function will output pha files of the format ' MJD_binsize_ ' + filetype + ' _cl50 . pha ' !
def mathpha(bin_size,filetype):
    normfile = Lv0_dirs.NGC300_2020 + 'n300_ulx.bgsub_cl50_g2020norm_' + bin_size + '.fffphot'
    mjds = np.genfromtxt(normfile,usecols=(0),unpack=True)
    spectra_files = np.genfromtxt(normfile,dtype='str',usecols=(9),unpack=True)
    for i in range(len(spectra_files)): 
        exposures = []
        truncated_name = []
        spectra_files_list = spectra_files[i].split(',') 
        cl50_files = [spectra_files_list[j][:34] + spectra_files_list[j][40:49] + spectra_files_list[j][55:] for j in range(len(spectra_files_list))] 
        if filetype == 'bg':
            filetype_spectra_files_list = [spectra_files_list[j][:36] + spectra_files_list[j][39:51] + spectra_files_list[j][54:] for j in range(len(spectra_files_list))] 
        elif filetype == 'cl':
            filetype_spectra_files_list = [spectra_files_list[j][:34] + spectra_files_list[j][40:49] + spectra_files_list[j][55:] for j in range(len(spectra_files_list))] 
        elif filetype == 'bgsub':
            filetype_spectra_files_list = spectra_files_list
        elif filetype == 'x':
            filetype_spectra_files_list = [spectra_files_list[j][:36] + spectra_files_list[j][39:45] + 'xpha/xbg' + spectra_files_list[j][54:] for j in range(len(spectra_files_list))]
        elif filetype == 'xbgsub':
            filetype_spectra_files_list = [spectra_files_list[j][:45] + 'xpha/xbgsub_' + spectra_files_list[j][55:] for j in range(len(spectra_files_list))]
        elif filetype == 'xsbg':
            filetype_spectra_files_list = [spectra_files_list[j][:36] + spectra_files_list[j][39:45] + 'xspha/xsbg' + spectra_files_list[j][54:] for j in range(len(spectra_files_list))]
        elif filetype == 'xsbgsub':
            filetype_spectra_files_list = [spectra_files_list[j][:45] + 'xspha/xsbgsub_' + spectra_files_list[j][55:] for j in range(len(spectra_files_list))]
        for j in range(len(filetype_spectra_files_list)): 
            fits_file = fits.open(filetype_spectra_files_list[j])
            exptime = fits_file[1].header['EXPOSURE'] 
            if filetype=='bg' or filetype=='x' or filetype=='xbgsub' or filetype=='xsbg' or filetype=='xsbgsub':
                exptime = fits.open(cl50_files[j])[1].header['EXPOSURE']
            exposures.append(exptime) 
        total_exptime = sum(exposures) 
        for j in range(len(filetype_spectra_files_list)):
            long_name = filetype_spectra_files_list[j]
            short_name = str(pathlib.Path(long_name).name)
            subprocess.run(['cp',long_name,short_name])
            truncated_name.append(short_name)
        mathpha = 'mathpha'
        calculation = ''
        for j in range(len(exposures)):
            if filetype == 'bg' or filetype == 'bgsub' or filetype == 'x' or filetype == 'xbgsub' or filetype == 'xsbg' or filetype == 'xsbgsub':
                if j == 0:
                    calculation += '('
                if j != len(exposures)-1:
                    if int(exposures[j]) == exposures[j]:
                        calculation += str(int(exposures[j])) + '*' + truncated_name[j] + '+'
                    else:
                        calculation += str(exposures[j]) + '*' + truncated_name[j] + '+'
                else:
                    calculation += str(exposures[j]) + '*' + truncated_name[j] + ') / ' + str(total_exptime)
            if filetype == 'cl':
                if j == 0:
                    calculation += '('
                if j != len(exposures)-1:
                    calculation += truncated_name[j] + '+'
                else:
                    calculation += truncated_name[j] + ')'
        if mjds[i] == 58449:
            print(calculation)
        if filetype == 'cl':
            unit = 'C'
        if filetype == 'bg' or filetype == 'bgsub' or filetype == 'x' or filetype == 'xbgsub' or filetype == 'xsbg' or filetype == 'xsbgsub':
            unit = 'R'
        outfile = 'outfil=' + str(int(mjds[i])) + '_' + bin_size + '_' + filetype + '_cl50.pha'
        exposure = 'exposure=' + str(total_exptime)
        errmeth = 'errmeth=gaussian'
        properr = 'properr=yes'
        ncomments = 'ncomments=0'
        areascal = 'areascal=NULL'
        clobber = 'clobber=YES'
        logfile = str(int(mjds[i])) + '_' + bin_size + '_' + filetype + '_mathpha.log'
        with open(logfile,'w') as logtextfile:
            output = subprocess.run([mathpha,calculation,unit,outfile,exposure,errmeth,properr,ncomments,areascal,clobber],capture_output=True,text=True)
            logtextfile.write(output.stdout)
            logtextfile.write('*------------------------------* \n')
            logtextfile.write(output.stderr)
            logtextfile.close()
    for i in range(len(truncated_name)):
        subprocess.run(['rm','-r',truncated_name[i]])
    return
def mathpha ( bin_size , filetype ) : normfile = Lv0_dirs . NGC300_2020 + 'n300_ulx.bgsub_cl50_g2020norm_' + bin_size + '.fffphot' mjds = np . genfromtxt ( normfile , usecols = ( 0 ) , unpack = True ) spectra_files = np . genfromtxt ( normfile , dtype = 'str' , usecols = ( 9 ) , unpack = True ) for i in range ( len ( spectra_files ) ) : exposures = [ ] truncated_name = [ ] spectra_files_list = spectra_files [ i ] . split ( ',' ) cl50_files = [ spectra_files_list [ j ] [ : 34 ] + spectra_files_list [ j ] [ 40 : 49 ] + spectra_files_list [ j ] [ 55 : ] for j in range ( len ( spectra_files_list ) ) ] if filetype == 'bg' : filetype_spectra_files_list = [ spectra_files_list [ j ] [ : 36 ] + spectra_files_list [ j ] [ 39 : 51 ] + spectra_files_list [ j ] [ 54 : ] for j in range ( len ( spectra_files_list ) ) ] elif filetype == 'cl' : filetype_spectra_files_list = [ spectra_files_list [ j ] [ : 34 ] + spectra_files_list [ j ] [ 40 : 49 ] + spectra_files_list [ j ] [ 55 : ] for j in range ( len ( spectra_files_list ) ) ] elif filetype == 'bgsub' : filetype_spectra_files_list = spectra_files_list elif filetype == 'x' : filetype_spectra_files_list = [ spectra_files_list [ j ] [ : 36 ] + spectra_files_list [ j ] [ 39 : 45 ] + 'xpha/xbg' + spectra_files_list [ j ] [ 54 : ] for j in range ( len ( spectra_files_list ) ) ] elif filetype == 'xbgsub' : filetype_spectra_files_list = [ spectra_files_list [ j ] [ : 45 ] + 'xpha/xbgsub_' + spectra_files_list [ j ] [ 55 : ] for j in range ( len ( spectra_files_list ) ) ] elif filetype == 'xsbg' : filetype_spectra_files_list = [ spectra_files_list [ j ] [ : 36 ] + spectra_files_list [ j ] [ 39 : 45 ] + 'xspha/xsbg' + spectra_files_list [ j ] [ 54 : ] for j in range ( len ( spectra_files_list ) ) ] elif filetype == 'xsbgsub' : filetype_spectra_files_list = [ spectra_files_list [ j ] [ : 45 ] + 'xspha/xsbgsub_' + spectra_files_list [ j ] [ 55 : ] for j in range ( len ( spectra_files_list ) ) ] for j in range ( len ( filetype_spectra_files_list ) ) : fits_file = fits . open ( filetype_spectra_files_list [ j ] ) exptime = fits_file [ 1 ] . header [ 'EXPOSURE' ] if filetype == 'bg' or filetype == 'x' or filetype == 'xbgsub' or filetype == 'xsbg' or filetype == 'xsbgsub' : exptime = fits . open ( cl50_files [ j ] ) [ 1 ] . header [ 'EXPOSURE' ] exposures . append ( exptime ) total_exptime = sum ( exposures ) for j in range ( len ( filetype_spectra_files_list ) ) : long_name = filetype_spectra_files_list [ j ] short_name = str ( pathlib . Path ( long_name ) . name ) subprocess . run ( [ 'cp' , long_name , short_name ] ) truncated_name . append ( short_name ) mathpha = 'mathpha' calculation = '' for j in range ( len ( exposures ) ) : if filetype == 'bg' or filetype == 'bgsub' or filetype == 'x' or filetype == 'xbgsub' or filetype == 'xsbg' or filetype == 'xsbgsub' : if j == 0 : calculation += '(' if j != len ( exposures ) - 1 : if int ( exposures [ j ] ) == exposures [ j ] : calculation += str ( int ( exposures [ j ] ) ) + '*' + truncated_name [ j ] + '+' else : calculation += str ( exposures [ j ] ) + '*' + truncated_name [ j ] + '+' else : calculation += str ( exposures [ j ] ) + '*' + truncated_name [ j ] + ') / ' + str ( total_exptime ) if filetype == 'cl' : if j == 0 : calculation += '(' if j != len ( exposures ) - 1 : calculation += truncated_name [ j ] + '+' else : calculation += truncated_name [ j ] + ')' if mjds [ i ] == 58449 : print ( calculation ) if filetype == 'cl' : unit = 'C' if filetype == 'bg' or filetype == 'bgsub' or filetype == 'x' or filetype == 'xbgsub' or filetype == 'xsbg' or filetype == 'xsbgsub' : unit = 'R' outfile = 'outfil=' + str ( int ( mjds [ i ] ) ) + '_' + bin_size + '_' + filetype + '_cl50.pha' exposure = 'exposure=' + str ( total_exptime ) errmeth = 'errmeth=gaussian' properr = 'properr=yes' ncomments = 'ncomments=0' areascal = 'areascal=NULL' clobber = 'clobber=YES' logfile = str ( int ( mjds [ i ] ) ) + '_' + bin_size + '_' + filetype + '_mathpha.log' with open ( logfile , 'w' ) as logtextfile : output = subprocess . run ( [ mathpha , calculation , unit , outfile , exposure , errmeth , properr , ncomments , areascal , clobber ] , capture_output = True , text = True ) logtextfile . write ( output . stdout ) logtextfile . write ( '*------------------------------* \n' ) logtextfile . write ( output . stderr ) logtextfile . close ( ) for i in range ( len ( truncated_name ) ) : subprocess . run ( [ 'rm' , '-r' , truncated_name [ i ] ] ) return
Function that takes in a bin size, and does MATHPHA on the set of pha files.
Function that takes in a bin size and does MATHPHA on the set of pha files .
"""
    Function that takes in a bin size, and does MATHPHA on the set of pha files.
    The file names are already saved in the binned .ffphot files. The function
    will output pha files of the format 'MJD_binsize_' + filetype + '_cl50.pha'!

    bin_size - bin size in days
    filetype - either 'bgsub' or 'bg' or 'cl' or 'x' or 'xbgsub'
    x means "extra background"
    """ #so for each BINNED MJD/counts #get a list where each entry = one pha file #takes away the bgsub_ prefix #takes away the 'sub' part of the name #takes away the bgsub_ prefix #for each individual pha file #get exposure time #now we have a list of exposure times corresponding to each pha file for a given (binned) MJD/count rate #get total exposure time for THAT MJD/count rate bin #### now to build the mathpha command ################## NOTE, FOR BG/BGSUB FILES, MAKE SURE YOU USE EXPOSURE ################## TIMES FROM THE CL50 FILES... SINCE THAT'S HOW MUCH THE ################## OBSERVATION WAS EXPOSED FOR... #print(mathpha+' "'+calculation+ '" '+unit+' '+outfile+' '+exposure+' '+errmeth+' '+properr+' '+ncomments+' '+areascal+' '+clobber) ### removing the temporary spectra
bin_size
filetype