Пример #1
0
def repevt(clu,obsid):

	print
	print 'Reprocessing evt file for OBSID '+obsid+' ------------------------'
	print

	if not os.access('mfe_'+clu+'/repro3_evt'+obsid+'.fits', os.F_OK) :

		acao = asol6_loc.asol(obsid, 'asol')

		bpix = asol6_loc.asol(obsid,'bpix')
		evt1 = asol6_loc.asol(obsid,'evt1')
		flt1 = asol6_loc.asol(obsid,'flt1')
		mtl  = asol6_loc.asol(obsid,'mtl')

		readmode = commands.getoutput('dmkeypar '+evt1+' readmode echo+')
		datamode = commands.getoutput('dmkeypar '+evt1+' datamode echo+')

		if readmode == 'TIMED' :
			if datamode == 'VFAINT' or datamode == 'FAINT' : edef = 'stdlev1'
			if datamode == 'GRADED' : edef = 'grdlev1'

		if readmode == 'CONTINUOUS' :
			if 'FAINT' in datamode : edef = 'cclev1'
			if 'GRADED' in datamode : edef = 'ccgrdlev1'

		# ACIS_PROCESS_EVENTS
		os.system('punlearn acis_process_events')
		os.system('pset acis_process_events infile='+evt1)
		os.system('pset acis_process_events outfile=mfe_'+clu+'/repro3_evt1_'+obsid+'.fits')
		os.system('pset acis_process_events badpixfile='+bpix)
		os.system('pset acis_process_events acaofffile='+acao)
		os.system('pset acis_process_events eventdef=")'+edef+'"')
		#os.system('pset acis_process_events rand_pix_size=0.5') # deprecated keyword
		os.system('pset acis_process_events rand_pha=yes')
		os.system('pset acis_process_events apply_tgain=yes')
		os.system('pset acis_process_events apply_cti=yes')
		os.system('pset acis_process_events mtlfile='+mtl)
		os.system('pset acis_process_events mode=h')
		if datamode == 'VFAINT' : os.system('pset acis_process_events check_vf_pha=yes')
		os.system('acis_process_events clobber=yes') # clobber=yes here is permanent

		# Grade and Status filtering
		if   datamode == 'VFAINT' : sstat='0'
		elif datamode == 'FAINT'  : sstat='00000000x00000000000000000000000' 
		#                           this status string chosen so that we stay consistent with blank sky file filtering
		#                           even if for FAINT mode, this status filtering is the same as status=0
		os.system('punlearn dmcopy')
		os.system('dmcopy "mfe_'+clu+'/repro3_evt1_'+obsid+'.fits[EVENTS][grade=0,2,3,4,6,status='+sstat+']" mfe_'+clu+'/repro3_flt_evt1_'+obsid+'.fits clobber=yes') # clobber=yes here is permanent

		# More filtering
		os.system('punlearn dmcopy')
		os.system('dmcopy "mfe_'+clu+'/repro3_flt_evt1_'+obsid+'.fits[EVENTS][@'+flt1+'][cols -pha]" mfe_'+clu+'/repro3_evt'+obsid+'.fits')

	else : print 'The repro3cessed file already exists'

	print '------ End of repevt for cluster ' + clu + ', OBSID ' + obsid + ' ----------------'
	print
	print	
Пример #2
0
def arf (clu, ob, i, ccdname) :

	locclu = 'mfe_'+clu
	origclu= 'orig_'+clu


	# MKWARF --------------

	pbk = asol6_loc.asol(ob,'pbk')
	acao = asol6_loc.asol(ob, 'asol')

	os.system('punlearn mkwarf')
	os.system('pset mkwarf infile="'+locclu+'/radp'+ob+'/sp'+str(i)+'_ccd'+ccdname+'.fits[WMAP]"')
	os.system('pset mkwarf outfile='+locclu+'/radp'+ob+'/warf'+str(i)+'_ccd'+ccdname+'.fits')
	os.system('pset mkwarf weightfile='+locclu+'/radp'+ob+'/wfef'+str(i)+'_ccd'+ccdname+'.fits')
	#os.system('pset mkwarf pbkfile='+pbk)
	os.system('pset mkwarf egridspec="0.3:11.0:0.01"')
	os.system('pset mkwarf asolfile='+acao)
	os.system('mkwarf verbose=1 mode=h')
Пример #3
0
def arf(clu, ob, isp, ccd):

    locclu = 'mfe_' + clu

    # MKWARF --------------

    pbk = asol6_loc.asol(ob, 'pbk')
    acao = asol6_loc.asol(ob, 'asol')

    os.system('punlearn mkwarf')
    os.system('pset mkwarf infile="' + locclu + '/zofr/sp10simple' + ob + '_' +
              str(isp) + '_ccd' + ccd + '.fits[WMAP]"')
    os.system('pset mkwarf outfile=' + locclu + '/zofr/warf10_' + ob + '_' +
              str(isp) + '_ccd' + ccd + '.fits')
    os.system('pset mkwarf weightfile=' + locclu + '/zofr/wfef10_' + ob + '_' +
              str(isp) + '_ccd' + ccd + '.fits')
    #	os.system('pset mkwarf pbkfile='+pbk)
    os.system('pset mkwarf egridspec="0.3:11.0:0.01"')
    os.system('pset mkwarf asolfile=' + acao)
    os.system('mkwarf verbose=1 mode=h clobber=yes')

    os.system('rm ' + locclu + '/zofr/wfef10_' + ob + '_' + str(isp) + '_ccd' +
              ccd + '.fits')
def ktofr(clu, obsids, zz, nnhhlab, checkfit):

    print ' -----------> checkbgfit4 for ' + clu

    clean()
    gc.collect()
    set_stat('cstat')

    locclu = 'mfe_' + clu

    (rac, decc) = centradec.getrd(locclu)

    if not os.path.exists(locclu + '/bginfield/'):
        os.mkdir(locclu + '/bginfield/')
    if not os.path.exists(locclu + '/reg/'): os.mkdir(locclu + '/reg/')
    if not os.path.exists(locclu + '/spec/'): os.mkdir(locclu + '/spec/')
    if not os.path.exists(locclu + '/ktofrsp/'): os.mkdir(locclu + '/ktofrsp/')

    ccdname = 'i567'
    ccdlist = ['0,1,2,3', '5', '6', '7']
    ccdindex = {'i': 0, '5': 1, '6': 2, '7': 3}

    #	dataplot  = ChipsCurve()
    #	modelplot = ChipsHistogram()

    #	dataplot.symbol.style='plus'
    #	dataplot.symbol.size=3
    #	dataplot.line.style='none'
    #	modelplot.line.color = "red"
    #	modelplot.line.thickness = 3

    # create simplechipsreg regions.
    for ob in obsids:
        os.system('punlearn skyfov')
        os.system('skyfov ' + locclu + '/clean' + ob + '.fits ' + locclu +
                  '/reg/simplechipsreg' + ob + '.fits clobber=yes')

    # which CCD's are present in each OBSID
    presccdglob = []
    for iob in range(len(obsids)):
        presccdglob.append(whichccd2z_loc.find(clu, obsids[iob]))

    # Remove OBSID's which do not have any good CCD's:
    emptyobs = []
    for iob in range(len(obsids)):
        ob = obsids[iob]
        for ccd in str(presccdglob[iob]):
            if not os.path.exists(locclu+'/spec/sp'+ob+'_ccd'+ccd+'_center.wrmf') \
                         or not os.path.exists(locclu+'/spec/bgarf'+ob+'_ccd'+ccd+'.fits') \
                         or not os.path.exists(locclu+'/spec/bgspsimple'+ob+'_ccd'+ccd+'.fits') \
                         or not os.path.exists(locclu+'/spec/bgrmf'+ob+'_ccd'+ccd+'.fits') :
                presccdglob[iob] = presccdglob[iob].replace(ccd, '')
        if presccdglob[iob] == '': emptyobs.append(iob)
    emptyobs.reverse()
    for iob in emptyobs:
        del obsids[iob]
        del presccdglob[iob]

    anfile = open(locclu + '/bginfield_all.reg', 'r')
    anline = anfile.readline()
    anline = anfile.readline()
    pbg1 = float(anline.split(',')[2][0:-1]) * 60 / 0.492
    pbg2 = float(anline.split(',')[3][0:-3]) * 60 / 0.492
    anfile.close()

    # Create BG annulus for each OBSIDS, which excludes the pt srcs
    # Get presccd
    presccd = []
    for iob in range(len(obsids)):
        ob = obsids[iob]
        intermed1 = radreg4simple2.radreg(clu, ob, pbg1, pbg2,
                                          locclu + '/bginfield/outer' + ob)
        presccd.append(intermed1)

    # Make sure that some needed files were created. If not, remove from prescccd
    # any CCD wich is missing PI, ARF or RMF file
    for iob in range(len(obsids)):
        ob = obsids[iob]
        for ccd in str(presccd[iob]):
            if not os.path.exists(
                    locclu + '/spec/sp' + ob + '_ccd' + ccd +
                    '_center.wrmf') or ccd not in presccdglob[iob]:
                presccd[iob] = presccd[iob].replace(ccd, '')
            #if not os.path.exists(locclu+'/spec/sp'+ob+'_ccd'+ccd+'_center.wrmf') : presccd[iob] = presccd[iob].replace(ccd, '')

    srcstr = ''
    for iob in range(len(obsids)):
        ob = obsids[iob]
        for iccd in range(len(presccd[iob])):
            ccd = presccd[iob][iccd]

            os.system('punlearn dmextract')
            os.system(
                'dmextract "' + locclu + '/clean' + ob + '.fits[ccd_id=' +
                ccdlist[ccdindex[ccd]] + '][sky=region(' + locclu +
                '/bginfield/outer' + ob + '_ccd' + ccd +
                '_xfov_pt_simple.reg)][bin PI]" ' + locclu + '/bginfield/sp' +
                ob + '_ccd' + ccd +
                '.fits opt=pha1 wmap="[energy=300:2000][bin det=8]" clobber=yes'
            )

            # Make ARF given the WMAP from the above spectrum file
            #pbk = asol6_loc.asol(ob,'pbk')
            acao = asol6_loc.asol(ob, 'asol')
            os.system('punlearn mkwarf')
            os.system('pset mkwarf infile="' + locclu + '/bginfield/sp' + ob +
                      '_ccd' + ccd + '.fits[WMAP]"')
            os.system('pset mkwarf outfile=' + locclu + '/bginfield/warf' +
                      ob + '_ccd' + ccd + '.fits')
            os.system('pset mkwarf weightfile=' + locclu + '/bginfield/wfef' +
                      ob + '_ccd' + ccd + '.fits')
            #os.system('pset mkwarf pbkfile='+pbk)
            os.system('pset mkwarf egridspec="0.3:11.0:0.01"')
            os.system('pset mkwarf asolfile=' + acao)
            os.system('mkwarf verbose=1 mode=h clobber=yes')
            os.system('rm ' + locclu + '/bginfield/wfef' + ob + '_ccd' + ccd +
                      '.fits')

    # Make sure (AGAIN) that all the needed files were created. If not, remove from prescccd
    # any CCD wich is missing PI, ARF or RMF file
    for iob in range(len(obsids)):
        ob = obsids[iob]
        for ccd in str(presccd[iob]):
            if not os.path.exists(locclu + '/bginfield/warf' + ob + '_ccd' +
                                  ccd + '.fits') or not os.path.exists(
                                      locclu + '/bginfield/sp' + ob + '_ccd' +
                                      ccd +
                                      '.fits') or ccd not in presccdglob[iob]:
                presccd[iob] = presccd[iob].replace(ccd, '')
            if not os.path.exists(locclu + '/spec/sp' + ob + '_ccd' + ccd +
                                  '_center.wrmf'):
                presccdglob[iob] = presccdglob[iob].replace(ccd, '')
                presccd[iob] = presccd[iob].replace(ccd, '')

    # load infield BG
    for iob in range(len(obsids)):
        ob = obsids[iob]
        for iccd in range(len(presccd[iob])):
            ccd = presccd[iob][iccd]

            load_pha(ob + '.' + ccd,
                     locclu + '/bginfield/sp' + ob + '_ccd' + ccd + '.fits')
            load_rmf(ob + '.' + ccd,
                     locclu + '/spec/sp' + ob + '_ccd' + ccd + '_center.wrmf')
            load_arf(ob + '.' + ccd,
                     locclu + '/bginfield/warf' + ob + '_ccd' + ccd + '.fits')

            srcstr = srcstr + '"' + ob + '.' + ccd + '",'

    # Fit the BG using blank sky AND infield BG data
    loadbg4z8simple2_write.bg(clu, obsids, presccdglob, presccd, srcstr, zz,
                              nnhhlab, checkfit)

    # Make and save plots
    for iob in range(len(obsids)):
        ob = obsids[iob]
        for iccd in range(len(presccdglob[iob])):
            ccd = presccdglob[iob][iccd]

            if ccd in presccd[iob]:
                plot_fit_resid(ob + '.' + ccd)
                if os.path.exists('fitplots/checkbgfit4infield_' + clu + '_' +
                                  ob + '_ccd' + ccd + '.eps'):
                    os.remove('fitplots/checkbgfit4infield_' + clu + '_' + ob +
                              '_ccd' + ccd + '.eps')
                print_window(
                    'fitplots/checkbgfit4infield_' + clu + '_' + ob + '_ccd' +
                    ccd, ['format', 'eps', 'orientation', 'landscape'])
                delete_window('all')

            plot_fit_resid('bg' + ob + '.' + ccd)
            if os.path.exists('fitplots/checkbgfit4blanksky_' + clu + '_' +
                              ob + '_ccd' + ccd + '.eps'):
                os.remove('fitplots/checkbgfit4blanksky_' + clu + '_' + ob +
                          '_ccd' + ccd + '.eps')
            print_window(
                'fitplots/checkbgfit4blanksky_' + clu + '_' + ob + '_ccd' +
                ccd, ['format', 'eps', 'orientation', 'landscape'])
            delete_window('all')
Пример #5
0
def sp(clu, ob, r1mpc):

    locclu = 'mfe_' + clu
    origclu = 'orig_' + clu

    os.system('punlearn ardlib')

    acao = asol6_loc.asol(ob, 'asol')

    if not os.path.exists(locclu + '/spec'): os.mkdir(locclu + '/spec')

    (rac, decc) = centradec.getrd(locclu)

    # simple in, out and bg regions (w/o point source exclusion)
    reginfile = open(locclu + '/reg/in.reg', 'w')
    reginfile.write('circle(' + rac + ',' + decc + ',' + str(r1mpc / 10) +
                    '")\n')
    reginfile.close()

    regoutfile = open(locclu + '/reg/out.reg', 'w')
    regoutfile.write('annulus(' + rac + ',' + decc + ',' + str(r1mpc / 10) +
                     '",' + str(r1mpc) + '")\n')
    regoutfile.close()

    regbgfile = open(locclu + '/reg/bg.reg', 'w')
    regbgfile.write('circle(' + rac + ',' + decc + ',' + str(r1mpc) + '")\n')
    regbgfile.close()

    # make chip regions using skyfov
    os.system('punlearn skyfov')
    os.system('skyfov ' + locclu + '/clean' + ob + '.fits ' + locclu +
              '/reg/chipsreg' + ob + '.fits aspect=' + acao + ' mskfile=' +
              asol6_loc.asol(ob, 'msk1'))

    pbk = asol6_loc.asol(ob, 'pbk')

    regv = ['in2', 'out2']

    ctsin0 = commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                '.fits[sky=region(' + locclu +
                                '/reg/in.reg)][ccd_id=0]" counts')
    ctsin1 = commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                '.fits[sky=region(' + locclu +
                                '/reg/in.reg)][ccd_id=1]" counts')
    ctsin2 = commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                '.fits[sky=region(' + locclu +
                                '/reg/in.reg)][ccd_id=2]" counts')
    ctsin3 = commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                '.fits[sky=region(' + locclu +
                                '/reg/in.reg)][ccd_id=3]" counts')
    ctsin5 = commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                '.fits[sky=region(' + locclu +
                                '/reg/in.reg)][ccd_id=5]" counts')
    ctsin6 = commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                '.fits[sky=region(' + locclu +
                                '/reg/in.reg)][ccd_id=6]" counts')
    ctsin7 = commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                '.fits[sky=region(' + locclu +
                                '/reg/in.reg)][ccd_id=7]" counts')

    ctsout0 = commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                 '.fits[sky=region(' + locclu +
                                 '/reg/out.reg)][ccd_id=0]" counts')
    ctsout1 = commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                 '.fits[sky=region(' + locclu +
                                 '/reg/out.reg)][ccd_id=1]" counts')
    ctsout2 = commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                 '.fits[sky=region(' + locclu +
                                 '/reg/out.reg)][ccd_id=2]" counts')
    ctsout3 = commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                 '.fits[sky=region(' + locclu +
                                 '/reg/out.reg)][ccd_id=3]" counts')
    ctsout5 = commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                 '.fits[sky=region(' + locclu +
                                 '/reg/out.reg)][ccd_id=5]" counts')
    ctsout6 = commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                 '.fits[sky=region(' + locclu +
                                 '/reg/out.reg)][ccd_id=6]" counts')
    ctsout7 = commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                 '.fits[sky=region(' + locclu +
                                 '/reg/out.reg)][ccd_id=7]" counts')

    # Create regions file combining field-of-view regions and point source exclusion regions
    chipreg.make(clu, ob, r1mpc, float(ctsin0), float(ctsin1), float(ctsin2),
                 float(ctsin3), float(ctsin5), float(ctsin6), float(ctsin7),
                 float(ctsout0), float(ctsout1), float(ctsout2),
                 float(ctsout3), float(ctsout5), float(ctsout6),
                 float(ctsout7))

    ccdv = ['i', '5', '6', '7']
    ccdlist = ['0,1,2,3', '5', '6', '7']

    for iccd in range(len(ccdv)):
        # RMF -----------------------
        #if not os.path.exists(locclu+'/spec/sp'+ob+'_ccd'+ccdv[iccd]+'_center.wrmf') and
        #if float(commands.getoutput('dmlist "'+locclu+'/clean'+ob+'.fits[ccd_id='+ccdlist[iccd]+']" counts')) > 0
        if os.path.exists(locclu + '/bg/bg' + ob + '_ccd' + ccdv[iccd] +
                          'r.fits'):
            rmf4ent5.rmf(locclu, ob, ccdv[iccd])

    for ireg in range(len(regv)):
        for iccd in range(len(ccdv)):

            # the region file, below, is created if there are enough counts in the region
            # of interest.
            if os.path.exists(locclu + '/reg/' + regv[ireg] + '_' + ob +
                              '_ccd' + ccdv[iccd] + '.reg') and os.path.exists(
                                  locclu + '/bg/bg' + ob + '_ccd' +
                                  ccdv[iccd] + 'r.fits'):

                # DMEXTRACT -----------
                os.system('punlearn dmextract')
                os.system('pset dmextract infile="' + locclu + '/clean' + ob +
                          '.fits[sky=region(' + locclu + '/reg/' + regv[ireg] +
                          '_' + ob + '_ccd' + ccdv[iccd] + '.reg)][bin PI]"')
                os.system('pset dmextract outfile=' + locclu + '/spec/sp' +
                          ob + '_' + regv[ireg] + '_ccd' + ccdv[iccd] + '.pi')
                os.system('pset dmextract opt=pha1')
                os.system('pset dmextract wmap="[energy=300:2000][bin det=8]"')
                os.system('dmextract verbose=1 mode=h')

                # DMGROUP -----------
                os.system('punlearn dmgroup')
                os.system('pset dmgroup infile=' + locclu + '/spec/sp' + ob +
                          '_' + regv[ireg] + '_ccd' + ccdv[iccd] + '.pi')
                os.system('pset dmgroup outfile=' + locclu + '/spec/sp' + ob +
                          '_' + regv[ireg] + '_ccd' + ccdv[iccd] + '_grp.pi')
                os.system('pset dmgroup grouptype=ADAPTIVE grouptypeval=30')
                os.system('pset dmgroup xcolumn="channel" ycolumn="counts"')
                os.system('pset dmgroup binspec=""')
                os.system('dmgroup verbose=1 mode=h')

                # MKWARF --------------
                os.system('punlearn mkwarf')
                os.system('pset mkwarf infile="' + locclu + '/spec/sp' + ob +
                          '_' + regv[ireg] + '_ccd' + ccdv[iccd] +
                          '.pi[WMAP]"')
                os.system('pset mkwarf outfile=' + locclu + '/spec/sp' + ob +
                          '_' + regv[ireg] + '_ccd' + ccdv[iccd] + '.warf')
                os.system('pset mkwarf weightfile=' + locclu + '/spec/sp' +
                          ob + '_' + regv[ireg] + '_ccd' + ccdv[iccd] +
                          '.wfef')
                #os.system('pset mkwarf pbkfile='+pbk)
                os.system('pset mkwarf egridspec="0.3:11.0:0.01"')
                os.system('pset mkwarf asolfile=' + acao)
                os.system('mkwarf verbose=1 mode=h')

                # DMEXTRACT BG -----------
                os.system('punlearn dmextract')
                os.system('pset dmextract infile="' + locclu + '/bg/bg' + ob +
                          '_ccd' + ccdv[iccd] + 'r.fits[sky=region(' + locclu +
                          '/reg/' + regv[ireg] + '_' + ob + '_ccd' +
                          ccdv[iccd] + '.reg)][bin PI]"')
                os.system('pset dmextract outfile=' + locclu + '/spec/sp' +
                          ob + '_' + regv[ireg] + '_ccd' + ccdv[iccd] +
                          '_bgbg2.pi')
                os.system('pset dmextract opt=pha1')
                os.system('dmextract verbose=1 mode=h')
Пример #6
0
def radreg(clu, ob, rlo, rhi, prefix) :

	locclu = 'mfe_'+clu
	(rac, decc) = centradec.getrd(locclu)

	badskyfovob = ['3182', '897', '11708']

	acao = asol6_loc.asol(ob,'asol')

	ccdname='i567'
	ccdlist=['0,1,2,3','5','6','7']

	# Get the SKY coodinates of the center.
	os.system('punlearn dmcoords')
	os.system('dmcoords '+locclu+'/clean'+ob+'.fits asolfile="'+acao+'" option=cel ra='+rac+' dec='+decc+' celfmt=hms')
	xc = commands.getoutput('pget dmcoords x')
	yc = commands.getoutput('pget dmcoords y')
	xcf = float(xc)
	ycf = float(yc)

	# ra, dec in degrees
	os.system('punlearn dmcoords')
	os.system('dmcoords '+locclu+'/clean'+ob+'.fits asolfile="'+acao+'" option=sky x='+xc+' y='+yc+' celfmt=deg')
	racd = float(commands.getoutput('pget dmcoords ra'))
	decd = float(commands.getoutput('pget dmcoords dec'))

	# Make a list of point sources, to be able to choose which ones are close
	# to a given shell for it to be excluded.
	ptfile = open(locclu+'/reg/pt0mfe_wcs.reg', 'r')
	regline = ptfile.readline()
	regv = []
	ptr=[]  # how far from (rac,decc) in pixels
	ptdr=[] # 'radius' of circle/ellipse in pixels
	while regline != '\n' and regline != '' :

		if regline[0] != '#' :
			
			regv.append(regline)
			
			s1=regline.partition('(')

			s2=s1[2].partition(':')
			hh=float(s2[0])
			s3=s2[2].partition(':')
			mm=float(s3[0])
			s4=s3[2].partition(',')
			ss=float(s4[0])

			ptra = 15.*(hh+mm/60.+ss/3600.)

			s5=s4[2].partition(':')
			dd=float(s5[0])
			if s5[0][0]=='+' : ff=1.
			elif s5[0][0]=='-' : ff=-1.
			s6=s5[2].partition(':')
			am=float(s6[0])
			s7=s6[2].partition(',')
			ac=float(s7[0])

			ptdec= dd + ff * (am/60. + ac/3600.)

			d2 = ( ( (ptra-racd) * numpy.cos(decd/180.*numpy.pi) ) ** 2. + (ptdec-decd) ** 2. ) * (3600./0.492)**2.

			ptr.append( d2**0.5 )

			if s1[0] == 'ellipse' :
				s8=s7[2].partition("',")
				s9=s8[2].partition("',")
				ptdr.append(60./0.492*max(float(s8[0]),float(s9[0])))
			elif s1[0] == 'circle' :
				s8=s7[2].partition("'")
				ptdr.append(60./0.492*float(s8[0]))
			else : 
				print
				print 'BAAAAAAAAAAAAAD REGIOOOOOOOOOOOON'
				print

		regline = ptfile.readline()
	ptfile.close()


	# get CCD regions
	chipx=commands.getoutput('dmlist "'+locclu+'/reg/simplechipsreg'+ob+'.fits[cols x]" data,clean')
	t1 = chipx.split('\n')
	t1.remove(t1[0])
	chipy=commands.getoutput('dmlist "'+locclu+'/reg/simplechipsreg'+ob+'.fits[cols y]" data,clean')
	u1 = chipy.split('\n')
	u1.remove(u1[0])
	ccdid=commands.getoutput('dmlist "'+locclu+'/reg/simplechipsreg'+ob+'.fits[cols ccd_id]" data,clean')
	c1 = ccdid.split('\n')
	c1.remove(c1[0])

	# goodchips[0] contains indices to u1, t1 or c1 that pertain to chip I (ie ACIS-I)
	# goodchips[1] contains indices to u1, t1 or c1 that pertain to chip 5
	# goodchips[2] contains indices to u1, t1 or c1 that pertain to chip 6
	# goodchips[3] contains indices to u1, t1 or c1 that pertain to chip 7
	goodchips=[[],[],[],[]]
	#           I  5  6  7
	for i in range(len(c1)) : 
		if int(c1[i]) in (0,1,2,3) : goodchips[0].append(i)
		if int(c1[i]) == 5 : goodchips[1].append(i)
		if int(c1[i]) == 6 : goodchips[2].append(i)
		if int(c1[i]) == 7 : goodchips[3].append(i)

	# ccdreg will contain the regions (in string format) corresponding to
	# the CCD's I, 5, 6 and 7
	ccdreg=[[],[],[],[]]
	ccdregfileappend=[False,False,False,False]
	for iccd in range(4) :

		if len(goodchips[iccd]) > 0 :
			for igood in range(len(goodchips[iccd])) :

				i = goodchips[iccd][igood]

				polyx = []
				t2=t1[i]
				t3=t2.lstrip()
				t4=t3.partition(' ')
				while ('NaN' not in t4[0]) and (t4[1] != '') :
					polyx.append(float(t4[0]))
					t2=t4[2]
					t3=t2.lstrip()
					t4=t3.partition(' ')

				polyy = []
				u2=u1[i]
				u3=u2.lstrip()
				u4=u3.partition(' ')
				while ('NaN' not in u4[0]) and (u4[1] != '') :
					polyy.append(float(u4[0]))
					u2=u4[2]
					u3=u2.lstrip()
					u4=u3.partition(' ')

				ccdreg[iccd].append('polygon(')
				for j in range(len(polyx)) : 
					ccdreg[iccd][igood] = ccdreg[iccd][igood] + str(polyx[j]) + ',' + str(polyy[j]) + ','
				ccdreg[iccd][igood] = ccdreg[iccd][igood][0:-1]
				ccdreg[iccd][igood] = ccdreg[iccd][igood] + ')'

			if not os.path.exists(locclu+'/reg/obsimple'+ob+'ccd'+ccdname[iccd]+'.reg') and \
                        ob not in badskyfovob : # <--- list of obsids where ccd region was created mannually, because
                                                #      skyfov returned wrong region
				ccdregfile = open(locclu+'/reg/obsimple'+ob+'ccd'+ccdname[iccd]+'.reg', 'w')
				for igood in range(len(goodchips[iccd])) : ccdregfile.write(' +'+ccdreg[iccd][igood])
				ccdregfile.close()
				ccdregfileappend[iccd]=True

	# Hi BG corner region
	if os.path.exists(locclu+'/reg/hibgcorner'+ob+'.reg') :
		hibgfile = open(locclu+'/reg/hibgcorner'+ob+'.reg','r')
		hibgstr = hibgfile.readline()
		hibgfile.close()
		hibgx = float( hibgstr[7:-1].split(',')[0] )
		hibgy = float( hibgstr[7:-1].split(',')[1] )
		hibgr = ( (hibgx-xcf)**2. + (hibgy-ycf)**2. )**0.5


	# make region of intersection of annulus with chip regions MINUS PTSRCs
	presccd=''
	for iccd in range(4) : 
		if len(ccdreg[iccd]) > 0 :

			# write region to file
			if ob not in badskyfovob :
				fovptfile = open(prefix+'_ccd'+str(ccdname[iccd])+'_xfov_pt_simple.reg', 'w')
				for ii in range(len(ccdreg[iccd])) : 
					fovptfile.write('+annulus('+xc+','+yc+','+str(rlo)+','+str(rhi)+') * '+ccdreg[iccd][ii]+'\n')
					for jj in range(len(ptr)) : # add point source regions which lie within this annulus
						if ptr[jj]-ptdr[jj] <= rhi and ptr[jj]+ptdr[jj] >= rlo : fovptfile.write('-'+regv[jj][0:-1]+' ')
					if iccd == 0 and os.path.exists(locclu+'/reg/hibgcorner'+ob+'.reg') and hibgr-1095. <= rhi and hibgr+1095. >= rlo : fovptfile.write( '-'+hibgstr[0:-1] )
					fovptfile.write('\n')
				fovptfile.close()

			elif os.path.exists(locclu+'/reg/obsimple'+ob+'ccd'+ccdname[iccd]+'.reg') :
				fovptfile = open(prefix+'_ccd'+str(ccdname[iccd])+'_xfov_pt_simple.reg', 'w')
				for ii in range(len(ccdreg[iccd])) : 
					obsimplefile = open(locclu+'/reg/obsimple'+ob+'ccd'+ccdname[iccd]+'.reg', 'r')
					obsimpleline = obsimplefile.readline().replace('\n','').replace(' +',' ')
					fovptfile.write('+annulus('+xc+','+yc+','+str(rlo)+','+str(rhi)+') * '+obsimpleline+'\n')
					for jj in range(len(ptr)) : # add point source regions which lie within this annulus
						if ptr[jj]-ptdr[jj] <= rhi and ptr[jj]+ptdr[jj] >= rlo : fovptfile.write('-'+regv[jj][0:-1]+' ')
					if iccd == 0 and os.path.exists(locclu+'/reg/hibgcorner'+ob+'.reg') and hibgr-1095. <= rhi and hibgr+1095. >= rlo : fovptfile.write( '-'+hibgstr[0:-1] )
					fovptfile.write('\n')
					obsimplefile.close()
				fovptfile.close()

			# add a good CCD to presccd (radial shell dependent
			if float(commands.getoutput('dmlist "'+locclu+'/clean'+ob+'.fits[ccd_id='+ccdlist[iccd]+'][sky=region('+prefix+'_ccd'+str(ccdname[iccd])+'_xfov_pt_simple.reg)]" counts')) > 0 and os.path.exists(locclu+'/bg/bg'+ob+'_ccd'+ccdname[iccd]+'r_en.fits') : presccd=presccd+ccdname[iccd]
			else : 
				if ob not in badskyfovob : os.remove(prefix+'_ccd'+str(ccdname[iccd])+'_xfov_pt_simple.reg')

	# presccd is a string containing the CCD's found in this range rlo -> rhi
	return presccd
Пример #7
0
def bg(clu, ob):

    print
    print 'Making BG files for OBSID ' + ob + ' ---------------------------'
    print

    locclu = 'mfe_' + clu

    acao = asol6_loc.asol(ob, 'asol')
    datamode = commands.getoutput('dmkeypar ' + locclu + '/clean' + ob +
                                  '.fits datamode echo+')

    sd = commands.getoutput('dmkeypar ' + locclu + '/clean' + ob +
                            '.fits date-obs echo+')
    intdat = int(sd[0:4] + sd[5:7] + sd[8:10])
    if intdat < 19990916:
        bgd = 'period a'  # period A : BG too uncertain because CTI rapidly increasing!
    elif 19990916 <= intdat and intdat <= 20000128:
        bgd = '1999-09-16'  # period B
    elif 20000129 <= intdat and intdat <= 20001130:
        if datamode == 'FAINT':
            bgd = '2000-01-29'  # period C
        elif datamode == 'VFAINT':
            bgd = '2000-12-01'  # period C by date, but assign to period D because no VFAINT mode in period C blank-sky files
    elif 20001201 <= intdat and intdat <= 20050831:
        bgd = '2000-12-01'  # period D
    elif 20050901 <= intdat:
        bgd = '2005-09-01'  # period E

    hardob = hardsoft3.hardsoft(clu, [ob], 'h')
    softob = hardsoft3.hardsoft(clu, [ob], 's')
    if len(hardob) == 1: bgaim = 'i'
    elif len(softob) == 1: bgaim = 's'
    else:
        print '------------ CANT DETERMINE AIM POINT -----------------'
        print
        bgaim = 'unknown'

    chipnum = []
    allchips = '0123567'
    for c in allchips:
        if float(
                commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                   '.fits[ccd_id=' + c + ']" counts')) > 0:
            chipnum.append(c)

    ####### Make region for the high-BG corner
    if '1' in chipnum:
        os.system('punlearn dmcoords')
        os.system('dmcoords ' + locclu + '/clean' + ob +
                  '.fits option=chip chip_id=1 chipx=900 chipy=512 asol=' +
                  acao)
        hibgx = commands.getoutput('pget dmcoords x')
        hibgy = commands.getoutput('pget dmcoords y')
        roll = float(
            commands.getoutput('dmkeypar ' + locclu + '/clean' + ob +
                               '.fits roll_pnt echo+'))

        hibgfile = open(locclu + '/reg/hibgcorner' + ob + '.reg', 'w')
        hibgfile.write('rotbox(' + hibgx + ',' + hibgy + ',1050,300,' +
                       str(360. - roll) + ')\n')
        hibgfile.close()
    ############################################3

    if commands.getoutput('dmkeypar ' + locclu + '/clean' + ob +
                          '.fits cti_corr echo+') == '1':
        bgcti = '_cti'
    elif commands.getoutput('dmkeypar ' + locclu + '/clean' + ob +
                            '.fits cti_corr echo+') == '0':
        bgcti = ''

    ciaodir = commands.getoutput('echo $ASCDS_INSTALL')
    bgf = ciaodir + '/CALDB/data/chandra/acis/bkgrnd'
    if bgaim != 'unknown' and bgd != 'period a':

        for c in chipnum:

            bgfound = glob.glob(bgf + '/acis' + c + bgaim + 'D' + bgd +
                                'bkgrnd' + bgcti + 'N000*.fits')
            bgfound.sort()

            if len(bgfound) == 0:
                print "---- no BG files found for cluster " + clu + ", OBSID " + ob + ", chip# " + c + " ------------------ "
                print 'The following was not found: '
                print bgf + '/acis' + c + bgaim + 'D' + bgd + 'bkgrnd' + bgcti + 'N000*.fits'
                print
            elif len(bgfound) == 1:
                if os.path.exists(locclu + '/bg/bg' + ob + '_ccd' + c +
                                  '.fits'):
                    os.remove(locclu + '/bg/bg' + ob + '_ccd' + c + '.fits')
                os.symlink(bgfound[0],
                           locclu + '/bg/bg' + ob + '_ccd' + c + '.fits')
            elif len(bgfound) > 1:
                print
                print ' -------------- MANY BG FILES FOUND FOR cluster ' + clu + ', OBSID ' + ob + ', CCD ' + c
                print ' -------------- CHOOSING THE LATEST: ' + bgfound[-1]
                print
                if os.path.exists(locclu + '/bg/bg' + ob + '_ccd' + c +
                                  '.fits'):
                    os.remove(locclu + '/bg/bg' + ob + '_ccd' + c + '.fits')
                os.symlink(bgfound[-1],
                           locclu + '/bg/bg' + ob + '_ccd' + c + '.fits')

    chipnums = []
    for s in ['5', '6', '7']:
        if s in chipnum: chipnums.append(s)

    chipnumi = []
    for s in ['0', '1', '2', '3']:
        if s in chipnum: chipnumi.append(s)

    if len(chipnumi) > 0:
        makebgi(ob, locclu, datamode, bgd, acao)
    if len(chipnums) > 0:
        makebgs(ob, locclu, chipnums, datamode, bgd, acao)

    print
    print
Пример #8
0
def mrg( clu, obsids, longest ) :

	origclu = 'orig_' + clu
	locclu = 'mfe_' + clu

	acao = asol6_loc.asol(longest, 'asol')

	if not os.path.exists(locclu+'/mrgevt.fits')  :

		print
		print 'Merging evt files for cluster '+clu+' ------------------------'
		print

		hardob = hardsoft3.hardsoft(clu, [longest], 'h')
		softob = hardsoft3.hardsoft(clu, [longest], 's')

		# determining which chips to use in computing the centroid
		if len(hardob)==1 : chipstr0 = '[ccd_id=0,1,2,3]' # ACIS-I: straightforward: chips 0,1,2,3
		else : 
			# choose the soft chip with most count to compute centroid
			chipcount=numpy.zeros(3)
			softchip=['5','6','7']
			for ichip in range(3)  : chipcount[ichip] = int(commands.getoutput('dmlist "mfe_'+clu+'/repro3_evt'+longest+'.fits[ccd_id='+softchip[ichip]+']" counts'))
			chipstr0='[ccd_id='+softchip[numpy.argmax(chipcount)]+']'

		# (x1, y1)
		os.system('punlearn dmstat')
		os.system('dmstat "mfe_'+clu+'/repro3_evt'+longest+'.fits'+chipstr0+'[energy=300:7000][cols x]" median=yes')
		x1=commands.getoutput('pget dmstat out_median')
		sx1=commands.getoutput('pget dmstat out_sigma')
		os.system('punlearn dmstat')
		os.system('dmstat "mfe_'+clu+'/repro3_evt'+longest+'.fits'+chipstr0+'[energy=300:7000][cols y]" median=yes')
		y1=commands.getoutput('pget dmstat out_median')
		sy1=commands.getoutput('pget dmstat out_sigma')

		# (x2, y2)
		os.system('punlearn dmstat')
		os.system('dmstat "mfe_'+clu+'/repro3_evt'+longest+'.fits'+chipstr0+'[energy=300:7000][sky=ellipse('+x1+','+y1+','+str(2.*float(sx1))+','+str(2.*float(sy1))+',0)][cols x]" median=yes')
		x2=commands.getoutput('pget dmstat out_median')
		sx2=commands.getoutput('pget dmstat out_sigma')
		os.system('punlearn dmstat')
		os.system('dmstat "mfe_'+clu+'/repro3_evt'+longest+'.fits'+chipstr0+'[energy=300:7000][sky=ellipse('+x1+','+y1+','+str(2.*float(sx1))+','+str(2.*float(sy1))+',0)][cols y]" median=yes')
		y2=commands.getoutput('pget dmstat out_median')
		sy2=commands.getoutput('pget dmstat out_sigma')

		# (x3, y3)
		os.system('punlearn dmstat')
		os.system('dmstat "mfe_'+clu+'/repro3_evt'+longest+'.fits'+chipstr0+'[energy=300:7000][sky=ellipse('+x2+','+y2+','+str(1.*float(sx2))+','+str(1.*float(sy2))+',0)][cols x]" median=yes')
		x3=commands.getoutput('pget dmstat out_median')
		sx3=commands.getoutput('pget dmstat out_sigma')
		os.system('punlearn dmstat')
		os.system('dmstat "mfe_'+clu+'/repro3_evt'+longest+'.fits'+chipstr0+'[energy=300:7000][sky=ellipse('+x2+','+y2+','+str(1.*float(sx2))+','+str(1.*float(sy2))+',0)][cols y]" median=yes')
		y3=commands.getoutput('pget dmstat out_median')
		sy3=commands.getoutput('pget dmstat out_sigma')

		# (x4, y4)
		os.system('punlearn dmstat')
		os.system('dmstat "mfe_'+clu+'/repro3_evt'+longest+'.fits'+chipstr0+'[energy=300:7000][sky=ellipse('+x3+','+y3+','+str(0.75*float(sx3))+','+str(0.75*float(sy3))+',0)][cols x]" median=yes')
		x4=commands.getoutput('pget dmstat out_median')
		sx4=commands.getoutput('pget dmstat out_sigma')
		os.system('punlearn dmstat')
		os.system('dmstat "mfe_'+clu+'/repro3_evt'+longest+'.fits'+chipstr0+'[energy=300:7000][sky=ellipse('+x3+','+y3+','+str(0.75*float(sx3))+','+str(0.75*float(sy3))+',0)][cols y]" median=yes')
		y4=commands.getoutput('pget dmstat out_median')
		sy4=commands.getoutput('pget dmstat out_sigma')

		if len(obsids) > 1 :

			os.system('punlearn merge_obs')

			evtstr = ''
			asolstr= ''
			for j in range(len(obsids)) :
				evtstr = evtstr + ','+locclu+'/repro3_evt_nophas'+obsids[j]+'.fits'
				asolstr= asolstr+ ','+asol6_loc.asol(obsids[j], 'asol')
				os.system('dmcopy "mfe_'+clu+'/repro3_evt'+obsids[j]+'.fits[ccd_id=0,1,2,3,5,6,7][col -phas]" '+locclu+'/repro3_evt_nophas'+obsids[j]+'.fits')

			evtstr = evtstr[1:]
			asolstr= asolstr[1:]

			os.system('pset merge_obs infiles="'+evtstr+'"')
			#os.system('pset merge_all chip="0,1,2,3,5,6,7" \n')

			os.system('pset merge_obs refcoord=mfe_'+clu+'/repro3_evt'+longest+'.fits')
			os.system('pset merge_obs outroot='+locclu+'/mrgevt') 
			os.system('pset merge_obs asolfiles='+asolstr)
			os.system('merge_obs mode=h ')

			os.system('cp '+locclu+'/mrgevt_merged_evt.fits '+locclu+'/mrgevt.fits')

			#mrg_cmd_file.close()

			#os.system('source temp_merge_all.csh')
			#os.remove('temp_merge_all.csh')

		elif len(obsids) == 1 : shutil.copy('mfe_'+clu+'/repro3_evt'+obsids[0]+'.fits',  locclu+'/mrgevt.fits')

		# (xc, yc)
		os.system('punlearn dmstat')
		os.system('dmstat "'+locclu+'/mrgevt.fits[sky=ellipse('+x4+','+y4+','+sx4+','+sy4+',0.)][cols x]" median=yes')
		xc=commands.getoutput('pget dmstat out_median')
		xcf = float(xc)
		os.system('punlearn dmstat')
		os.system('dmstat "'+locclu+'/mrgevt.fits[sky=ellipse('+x4+','+y4+','+sx4+','+sy4+',0.)][cols y]" median=yes')
		yc=commands.getoutput('pget dmstat out_median')
		ycf=float(yc)

		# (rac, decc)
		os.system('punlearn dmcoords')
		os.system('dmcoords '+locclu+'/mrgevt.fits asolfile='+acao+' option=sky x='+xc+' y='+yc+' celfmt=hms')
		rac  = commands.getoutput('pget dmcoords ra')
		decc = commands.getoutput('pget dmcoords dec')		

		# writing rac, dec to file
		racfile = open(locclu+'/rac.txt', 'w')
		racfile.write(rac+'\n')
		racfile.close()
		deccfile = open(locclu+'/decc.txt', 'w')
		deccfile.write(decc+'\n')
		deccfile.close()

	else :
		print
		print '------- Merged file exists already, skipping mrg procedure'

	print
	print
Пример #9
0
def preradial(clu, obsids, r1mpc):

    locclu = 'mfe_' + clu

    exptime = numpy.zeros(len(obsids))
    for j in range(len(obsids)):
        exptime[j] = float(
            commands.getoutput('dmkeypar ' + locclu + '/clean' + obsids[j] +
                               '.fits livetime echo+'))

    longob = obsids[numpy.argmax(exptime)]

    if not os.path.exists(locclu + '/ctofr'): os.mkdir(locclu + '/ctofr')

    if len(obsids) > 1:

        os.system('punlearn obs')

        evtstr = ''
        asolstr = ''
        for j in range(len(obsids)):
            evtstr = evtstr + ',' + locclu + '/clean_nophas' + obsids[
                j] + '_en.fits'
            asolstr = asolstr + ',' + asol6_loc.asol(obsids[j], 'asol')
            os.system('punlearn dmcopy')
            os.system(
                'dmcopy "' + locclu + '/clean' + obsids[j] +
                '.fits[energy=300:7000][ccd_id=0,1,2,3,5,6,7][col -phas]" ' +
                locclu + '/clean_nophas' + obsids[j] + '_en.fits')
        evtstr = evtstr[1:]
        asolstr = asolstr[1:]

        os.system('pset merge_obs infiles="' + evtstr + '"')
        os.system('pset merge_obs asolfiles=' + asolstr)
        os.system('pset merge_obs refcoord=' + locclu + '/clean' + longob +
                  '.fits')
        os.system('pset merge_obs outroot=' + locclu + '/mrgclean_en')
        os.system('merge_obs mode=h ')

        os.system('cp ' + locclu + '/mrgclean_en_merged_evt.fits ' + locclu +
                  '/mrgclean_en.fits')

        # clean up
        os.system('rm ' + locclu + '/clean_nophas[1-9]*_en.fits')

    else:
        os.system('punlearn dmcopy')
        os.system('dmcopy "' + locclu + '/clean' + obsids[0] +
                  '.fits[ccd_id=0,1,2,3,5,6,7][energy=300:7000][col -phas]" ' +
                  locclu + '/mrgclean_en.fits')

    acao = asol6_loc.asol(longob, 'asol')

    # get the coordinates of the center in SKY units
    os.system('punlearn dmcoords')
    os.system('dmcoords ' + locclu + '/mrgclean_en.fits asolfile=' + acao +
              ' ra=`more ' + locclu + '/rac.txt` dec=`more ' + locclu +
              '/decc.txt` option=cel celfmt=hms')
    xs = commands.getoutput('pget dmcoords x')
    ys = commands.getoutput('pget dmcoords y')
    centerccd = int(commands.getoutput('pget dmcoords chip_id'))

    # Make file with the radial coordinate of each photon count
    os.system('punlearn dmtcalc')
    os.system(
        'dmtcalc "' + locclu +
        '/mrgclean_en.fits[col -time,-ccd_id,-node_id,-expno,-chip,-tdet,-det,-phas,-pha_ro,-energy,-pi,-fltgrade,-grade,-status][exclude sky=region('
        + locclu + '/reg/pt0mfe_wcs.reg)]" "' + locclu +
        '/evtrad_mrgclean.txt[opt kernel=text/simple]" expr="r2=((' + xs +
        '-sky[0])^2)+((' + ys + '-sky[1])^2)" clobber=no')

    r2 = []
    rfile = open(locclu + '/evtrad_mrgclean.txt', 'r')
    intermed = rfile.readline()
    intermed = rfile.readline()
    intermed = rfile.readline()  # first data-containing line
    while intermed != '' and intermed != '\n':
        r2.append(float(intermed.split(' ')[2][0:-1]))
        intermed = rfile.readline()
    rfile.close()

    rmax = (max(r2))**0.5

    os.system('rm ' + locclu + '/radbins5.txt' + ' ' + locclu + '/hienrlo.txt')
    for ob in obsids:
        radial10.radial(clu, ob, r1mpc / 0.492, rmax, obsids)
def ktofr(clu, obsids, r1mpc, zz, nnhhlab):

    print ' -----------> ktofr14loc for ' + clu

    clean()
    gc.collect()
    set_stat('cstat')

    # local directory for this cluster
    locclu = 'mfe_' + clu

    # coords of center of the cluster
    (rac, decc) = centradec.getrd(locclu)

    # don't use these observations
    badhienobs = [
        '7686', '7688', '7689', '7690', '7692', '7693', '7694', '7696', '7701'
    ]

    if not os.path.exists(locclu + '/ktofrsp/'): os.mkdir(locclu + '/ktofrsp/')

    # Chandra CCDs to look for data in
    ccdname = 'i567'
    ccdlist = ['0,1,2,3', '5', '6', '7']

    # Plotting settings
    dataplot = ChipsCurve()
    modelplot = ChipsHistogram()

    dataplot.symbol.style = 'plus'
    dataplot.symbol.size = 3
    dataplot.line.style = 'none'
    modelplot.line.color = "red"
    modelplot.line.thickness = 3

    # create simplechipsreg regions.
    for ob in obsids:
        os.system('punlearn skyfov')
        os.system('skyfov ' + locclu + '/clean' + ob + '.fits ' + locclu +
                  '/reg/simplechipsreg' + ob + '.fits clobber=yes')

    # which CCD's are present in each OBSID
    presccdglob = []
    for iob in range(len(obsids)):
        presccdglob.append(whichccd2z_loc.find(clu, obsids[iob]))

    # Reset these global variables
    globmod.globstr1 = ''
    globmod.globstr2 = ''

    for iob in range(len(obsids)):

        ob = obsids[iob]

        # Make file with only counts in energy range .3-7keV
        os.system('punlearn dmcopy')
        os.system('dmcopy "' + locclu + '/clean' + ob +
                  '.fits[energy=300:7000]" ' + locclu + '/evt' + ob +
                  '_b.fits')

        # get the coordinates of the center in SKY units
        os.system('punlearn dmcoords')
        os.system('dmcoords ' + locclu + '/clean' + ob + '.fits asolfile=' +
                  asol6_loc.asol(ob, 'asol') + ' ra=' + rac + ' dec=' + decc +
                  ' option=cel celfmt=hms')
        xs = commands.getoutput('pget dmcoords x')
        ys = commands.getoutput('pget dmcoords y')

        # Make region containing all ptsrcs and hi-BG corner region
        os.system('cp ' + locclu + '/reg/pt0mfe_wcs.reg ' + locclu +
                  '/reg/pt_hibg' + ob + '.reg')
        if os.path.exists(locclu + '/reg/hibgcorner' + ob + '.reg'):
            os.system('more ' + locclu + '/reg/hibgcorner' + ob + '.reg >> ' +
                      locclu + '/reg/pt_hibg' + ob + '.reg')

        # Make file with the radial coordinate of each photon count
        os.system(
            'dmtcalc "' + locclu + '/evt' + ob +
            '_b.fits[col -time,-ccd_id,-node_id,-expno,-chip,-tdet,-det,-phas,-pha_ro,-energy,-pi,-fltgrade,-grade,-status][exclude sky=region('
            + locclu + '/reg/pt_hibg' + ob + '.reg)]" "' + locclu + '/evtrad' +
            ob + '.txt[opt kernel=text/simple]" expr="r2=((' + xs +
            '-sky[0])^2)+((' + ys + '-sky[1])^2)"')

    # Get the radial coord of EACH photon count!
    # To be used to determine if we have enough counts
    # for a given radial bin to get 10% error on best-fit
    # kT value.
    r = []  # in Mpc
    for ob in obsids:
        rfile = open(locclu + '/evtrad' + ob + '.txt', 'r')
        intermed = rfile.readline()
        intermed = rfile.readline()
        intermed = rfile.readline()  # first data-containing line
        while intermed != '' and intermed != '\n':
            r.append(0.492 / r1mpc * float(intermed.split(' ')[2][0:-1])**0.5)
            intermed = rfile.readline()
        rfile.close()
    r.sort()

    # Define rmin and rmax
    rmax = r[-1]
    rmin = 1.2 / r1mpc

    # The first radius, defined by rmin
    for imin in range(len(r)):
        if r[imin] >= rmin: break  # imin is the index where r >= rmin

    # Read abundance from cluname_TZ.txt
    tzfile = open('cluname_TZ.txt', 'r')
    intermed = 'init'
    while intermed != '':
        intermed = tzfile.readline()
        if intermed.split()[0] == clu:
            abin = float(intermed.split()[2])
            about = float(intermed.split()[4])
            ktin = float(intermed.split()[1])
            ktout = float(intermed.split()[3])
            break
    tzfile.close()

    # Bin the radial data
    rbnd = [rmin]  # the 1st radius
    ct1bin = 100
    i = imin + ct1bin
    while i < len(r):
        rbnd.append(r[i])
        i = i + ct1bin
    rbnd = n.array(rbnd)

    # Load Hi-energy count in losrc regions
    losrchienct = n.ones((len(obsids), 4)) * -1
    for iob in range(len(obsids)):
        ob = obsids[iob]
        sclfile = open(locclu + '/ctofr/scl' + ob + '.txt', 'r')
        for iccd in range(4):
            intermed = sclfile.readline()
            if intermed.split()[2].lower() != 'nan':
                losrchienct[iob, iccd] = float(intermed.split()[2])
        sclfile.close()

    # Load string containing exculsion of all point sources
    excstr = []
    for ob in obsids:
        excfile = open(locclu + '/reg/exclpt' + ob + '.reg', 'r')
        excstr.append(excfile.readline())
        excfile.close()

    ################### make radial bins which contain enough counts for a best-fit kT with 10% error ########################
    ccdindex = {'i': 0, '5': 1, '6': 2, '7': 3}
    vrstat = []
    vkt = []
    vktp = []
    vktm = []
    ishell = 0
    rz1 = []
    rz2 = []
    ishell = 0  # the index of everything created new, in this loop. only increases, when we get a satisfactory fit.
    i1 = 0  # index to rbnd indicating inner radius
    i2 = 1  # index to rbnd indicating outer radius
    while i2 <= len(rbnd) - 1:

        ct = 1
        while ct <= 10:

            print '======== loop ct =', ct

            # get annulus temperature from cluname_TZ.txt, based on in/out regions
            if ishell == 0:
                if (rbnd[i1] + rbnd[i2]) / 2 <= 0.1 and ktin > 0: tshell = ktin
                elif (rbnd[i1] + rbnd[i2]) / 2 > 0.1 and ktout > 0:
                    tshell = ktout
                if ktin < 0 and ktout > 0: tshell = ktout
                elif ktout < 0 and ktin > 0: tshell = ktin
                else: tshell = 5.
            else: tshell = vkt[ishell - 1]

            # Calculate BG fraction in the annulus
            sclbgct = 0
            for iob in range(len(obsids)):
                ob = obsids[iob]
                annfile = open('antemp.reg', 'w')
                annfile.write('annulus(' + rac + ',' + decc + ',' +
                              str(rbnd[i1] * r1mpc) + '",' +
                              str(rbnd[i2] * r1mpc) + '")\n')
                annfile.write(excstr[iob])
                annfile.close()
                for ccd in presccdglob[iob]:
                    sclbgct = sclbgct + float(
                        commands.getoutput(
                            'dmlist "' + locclu + '/bg/bg' + ob + '_ccd' +
                            ccd + 'r_en.fits[sky=region(antemp.reg)]" counts')
                    ) / float(
                        commands.getoutput(
                            'dmkeypar ' + locclu + '/bg/bg' + ob + '_ccd' +
                            ccd + 'r_en.fits exposure echo+')) * float(
                                commands.getoutput('dmkeypar ' + locclu +
                                                   '/evt' + ob +
                                                   '_b.fits exposure echo+'))
                os.remove('antemp.reg')
            bgfrac = sclbgct / float(i2 - i1) / ct1bin

            # given approx. kT and BGfrac, how many counts do we need for 10% error?
            nnec = (500 * 10.**(1.976 * bgfrac)) * (tshell / 2.)**1.7
            nnec = max(nnec, 153.)  # 153counts is the value at BGf=0, kT=1keV.
            # We take this as the min number of counts for making a kT bin

            inec0 = min(int(nnec / ct1bin) + i1 + 1, len(rbnd) - 1)

            # making sure that rhi/rlo >= 1.25:
            i125 = n.where(rbnd >= 1.25 * rbnd[i1])
            if len(i125[0]) == 0: i125 = [[len(rbnd) - 1]]
            inec = max(inec0, i125[0][0])

            if inec <= i2 or inec == len(rbnd) - 1:
                i2 = inec
                break
            else:
                i2 = inec

            ct = ct + 1

        if ct >= 11:
            os.system('echo ' + str(ishell) + ' >> ' + locclu +
                      '/shells_nocnvg.txt')

        rz1.append(rbnd[i1])
        rz2.append(rbnd[i2])

        ################# make spec and fit ####################################

        # Get the CCD's present in this shell
        presccd = []  # the ccd's that are present in this SHELL!
        for iob in range(len(obsids)):
            ob = obsids[iob]

            # radreg4 returns the ccd's that are present in this SHELL!
            # and makes the regions necessary for them to be used in fits..
            intermed1 = radreg4simple3.radreg(
                clu, ob, rz1[-1] * r1mpc / .492, rz2[-1] * r1mpc / .492,
                locclu + '/ktofrsp/kt14an' + ob + '_' + str(ishell))
            presccd.append(intermed1)

            for ccd in str(presccd[iob]):

                # Make the spectra for SHELL x CCD
                os.system('punlearn dmextract')
                os.system(
                    'dmextract "' + locclu + '/clean' + ob + '.fits[ccd_id=' +
                    ccdlist[ccdindex[ccd]] + '][sky=region(' + locclu +
                    '/ktofrsp/kt14an' + ob + '_' + str(ishell) + '_ccd' + ccd +
                    '_xfov_pt_simple.reg)][bin PI]" ' + locclu +
                    '/ktofrsp/sp14_' + ob + '_' + str(ishell) + '_ccd' + ccd +
                    '.fits opt=pha1 wmap="[energy=300:2000][bin det=8]" clobber=yes'
                )

                # Make ARF given the WMAP from the above spectrum file
                #pbk = asol6_loc.asol(ob,'pbk')
                acao = asol6_loc.asol(ob, 'asol')
                os.system('punlearn mkwarf')
                os.system('pset mkwarf infile="' + locclu + '/ktofrsp/sp14_' +
                          ob + '_' + str(ishell) + '_ccd' + ccd +
                          '.fits[WMAP]"')
                os.system('pset mkwarf outfile=' + locclu +
                          '/ktofrsp/warf14_' + ob + '_' + str(ishell) +
                          '_ccd' + ccd + '.fits')
                os.system('pset mkwarf weightfile=' + locclu +
                          '/ktofrsp/wfef14_' + ob + '_' + str(ishell) +
                          '_ccd' + ccd + '.fits')
                #os.system('pset mkwarf pbkfile='+pbk)
                os.system('pset mkwarf egridspec="0.3:11.0:0.01"')
                os.system('pset mkwarf asolfile=' + acao)
                os.system('mkwarf verbose=1 mode=h clobber=yes')
                os.system('rm ' + locclu + '/ktofrsp/wfef14_' + ob + '_' +
                          str(ishell) + '_ccd' + ccd + '.fits')

        # Make BG spectra and load them, only once for ishell=0
        # This is here because it uses ktofrsp/ktan14*.reg, created just
        # above in radreg4simple2.radreg()
        if ishell == 0:

            infieldccd, infieldgalperbs, infieldcxbperbs = loadbg4z8simple3.bg(
                clu, obsids, presccdglob)

            for iob in range(len(obsids)):
                ob = obsids[iob]
                for ccd in str(presccdglob[iob]):
                    exec 'gal1bg_' + ob + '_' + ccd + '_normval = float( gal1bg_' + ob + '_' + ccd + '.norm.val )'
                    exec 'cxbbg_' + ob + '_' + ccd + '_amplval = float( cxbbg_' + ob + '_' + ccd + '.ampl.val )'

        bginf_present = False
        iob_in = -1
        for iob in range(len(obsids)):
            if len(infieldccd[iob]) > 0:
                bginf_present = True
                iob_in = iob

        # Make sure that all the needed files were created. If not, remove from prescccd
        # any CCD wich is missing PI, ARF or RMF file
        firstiob = -1
        for iob in range(len(obsids)):
            ob = obsids[iob]
            for ccd in str(presccd[iob]):
                if not os.path.exists(locclu+'/ktofrsp/warf14_'+ob+'_'+str(ishell)+'_ccd'+ccd+'.fits') \
                       or not os.path.exists(locclu+'/spec/sp'+ob+'_ccd'+ccd+'_center.wrmf') \
                       or not os.path.exists(locclu+'/ktofrsp/sp14_'+ob+'_'+str(ishell)+'_ccd'+ccd+'.fits') \
                       or ccd not in presccdglob[iob] :
                    presccd[iob] = presccd[iob].replace(ccd, '')
            if len(presccd[iob]) > 0 and firstiob == -1: firstiob = iob

        print
        print '))))))))))))) clu = ', clu
        print '))))))))))))) obsids = ', obsids
        print '))))))))))))) ishell = ', ishell
        print '))))))))))))) presccdglob = ', presccdglob
        print '))))))))))))) presccd = ', presccd
        print

        # Load data we just made
        srcstr = ''
        for iob in range(len(obsids)):
            ob = obsids[iob]

            for ccd in presccd[iob]:

                load_pha(
                    ob + '.' + str(ishell) + '.' + ccd,
                    locclu + '/ktofrsp/sp14_' + ob + '_' + str(ishell) +
                    '_ccd' + ccd + '.fits')
                load_rmf(
                    ob + '.' + str(ishell) + '.' + ccd,
                    locclu + '/spec/sp' + ob + '_ccd' + ccd + '_center.wrmf')
                load_arf(
                    ob + '.' + str(ishell) + '.' + ccd,
                    locclu + '/ktofrsp/warf14_' + ob + '_' + str(ishell) +
                    '_ccd' + ccd + '.fits')

                bgscl = get_data(ob + '.' + str(ishell) + '.' +
                                 ccd).backscal * get_data(ob + '.' + str(
                                     ishell) + '.' + ccd).exposure / get_data(
                                         'bg' + ob + '.' +
                                         ccd).backscal / get_data('bg' + ob +
                                                                  '.' +
                                                                  ccd).exposure
                newhienbgscl = calc_data_sum(
                    id=ob + '.' + str(ishell) + '.' + ccd, lo=9.5,
                    hi=12) / calc_data_sum(
                        id='bg' + ob + '.' + ccd, lo=9.5, hi=12)
                srcstr = srcstr + '"' + ob + '.' + str(
                    ishell) + '.' + ccd + '",'

                # the source model
                rsp = get_response(ob + '.' + str(ishell) + '.' + ccd)
                bgrsp = get_response('bg' + ob + '.' + ccd)
                groupzeros.grp(ob + '.' + str(ishell) + '.' + ccd, ':.3,7:')

                if ob not in badhienobs:
                    exec 'set_full_model( "' + ob + '.' + str(
                        ishell
                    ) + '.' + ccd + '", rsp(xsphabs.ab * xsapec.em' + str(
                        ishell
                    ) + '_' + ob + '_' + ccd + ' ) + newhienbgscl * bgrsp( powlaw1d.p1_' + ob + '_' + ccd + ' + powlaw1d.p2_' + ob + '_' + ccd + ' + exp.e1_' + ob + '_' + ccd + ' + gauss1d.g1_' + ob + '_' + ccd + ' + gauss1d.g2_' + ob + '_' + ccd + ' + gauss1d.g3_' + ob + '_' + ccd + ' + gauss1d.g5_' + ob + '_' + ccd + ' ) + rsp( xsphabs.ab * powlaw1d.cxbbg_' + ob + '_' + ccd + ' + xsapec.gal1bg_' + ob + '_' + ccd + ' ) )'
                else:
                    exec 'set_full_model( "' + ob + '.' + str(
                        ishell
                    ) + '.' + ccd + '", rsp(xsphabs.ab * xsapec.em' + str(
                        ishell
                    ) + '_' + ob + '_' + ccd + ' ) + bgscl * bgrsp( powlaw1d.p1_' + ob + '_' + ccd + ' + powlaw1d.p2_' + ob + '_' + ccd + ' + exp.e1_' + ob + '_' + ccd + ' + gauss1d.g1_' + ob + '_' + ccd + ' + gauss1d.g2_' + ob + '_' + ccd + ' + gauss1d.g3_' + ob + '_' + ccd + ' + gauss1d.g5_' + ob + '_' + ccd + ' ) + rsp( xsphabs.ab * powlaw1d.cxbbg_' + ob + '_' + ccd + ' + xsapec.gal1bg_' + ob + '_' + ccd + ' ) )'

                # If we fit the in-field BG, then obtain gal1 and cxb components from it
                if bginf_present:
                    exec 'set_par( gal1bg_' + ob + '_' + ccd + '.norm, val=infieldgalperbs[iob_in][0]*get_data("' + ob + '.' + str(
                        ishell) + '.' + ccd + '").backscal )'
                    exec 'set_par(  cxbbg_' + ob + '_' + ccd + '.ampl, val=infieldcxbperbs[iob_in][0]*get_data("' + ob + '.' + str(
                        ishell) + '.' + ccd + '").backscal )'

                # if no in-field BG, then scale down the values of gal1 and cxb, which were already
                # read by loadbg4z8simple. scaling is according to ratio of backscal of source spec
                # to backscal of BG spec
                else:
                    exec 'set_par( gal1bg_' + ob + '_' + ccd + '.norm, val=get_data("' + ob + '.' + str(
                        ishell
                    ) + '.' + ccd + '").backscal * gal1bg_' + ob + '_' + ccd + '_normval )'
                    exec 'set_par(  cxbbg_' + ob + '_' + ccd + '.ampl, val=get_data("' + ob + '.' + str(
                        ishell
                    ) + '.' + ccd + '").backscal * cxbbg_' + ob + '_' + ccd + '_amplval )'

            # Setting abundance parameter, kT and redshift
            for ccd in presccd[iob]:
                exec 'set_par( em' + str(
                    ishell) + '_' + ob + '_' + ccd + '.redshift, val=' + str(
                        zz) + ')'
                exec 'set_par( em' + str(
                    ishell
                ) + '_' + ob + '_' + ccd + '.abundanc, val=0.3, min=0, max=5, frozen=False)'
                exec 'set_par( em' + str(
                    ishell
                ) + '_' + ob + '_' + ccd + '.kt,       val=5, min=0, max=40, frozen=False)'

                if iob != firstiob or ccd != presccd[firstiob][0]:
                    exec 'link( em' + str(
                        ishell) + '_' + ob + '_' + ccd + '.kt,       em' + str(
                            ishell) + '_' + obsids[firstiob] + '_' + presccd[
                                firstiob][0] + '.kt )'
                    exec 'link( em' + str(
                        ishell) + '_' + ob + '_' + ccd + '.abundanc, em' + str(
                            ishell) + '_' + obsids[firstiob] + '_' + presccd[
                                firstiob][0] + '.abundanc )'

        if srcstr[0:-1] != '':  # if there is data to fit:

            # Set nH
            if clu != 'a478':
                set_par(ab.nh,
                        val=nnhhlab,
                        min=nnhhlab / 10.,
                        max=nnhhlab * 10.,
                        frozen=True)
            else:
                set_par(ab.nh,
                        val=nnhhlab * 1.1,
                        min=nnhhlab,
                        max=nnhhlab * 100.,
                        frozen=False)

            # Setting normalizations, link norms if backscal covers 95-105 % of annulus area
            iobiccd1 = [-1, -1]
            anarea = n.pi * ((rz2[-1] * r1mpc / .492)**2. -
                             (rz1[-1] * r1mpc / .492)**2.)
            for iob in range(len(obsids)):
                ob = obsids[iob]
                for iccd in range(len(presccd[iob])):
                    ccd = presccd[iob][iccd]
                    bspix2 = float(
                        commands.getoutput(
                            'dmkeypar ' + locclu + '/ktofrsp/sp14_' + ob +
                            '_' + str(ishell) + '_ccd' + ccd +
                            '.fits backscal echo+')) * 64.0 * 1024.**2.

                    if iobiccd1 == [
                            -1, -1
                    ] or bspix2 / anarea < 0.95 or bspix2 / anarea > 1.05:
                        exec 'set_par(em' + str(
                            ishell
                        ) + '_' + ob + '_' + ccd + '.norm, val=0.02, min=2e-4, max=2.)'
                        exec 'emnorm0=abs(float(em' + str(
                            ishell
                        ) + '_' + ob + '_' + ccd + '.norm.val)*calc_data_sum(id="' + ob + '.' + str(
                            ishell
                        ) + '.' + ccd + '",lo=0.3,hi=7.0)/calc_model_sum(id="' + ob + '.' + str(
                            ishell) + '.' + ccd + '",lo=0.3,hi=7.0))'
                        exec 'set_par(em' + str(
                            ishell
                        ) + '_' + ob + '_' + ccd + '.norm, val=float(emnorm0), min=0., max=float(emnorm0*1e3) )'
                        if iobiccd1 == [
                                -1, -1
                        ] and bspix2 / anarea >= 0.95 and bspix2 / anarea <= 1.05:
                            iobiccd1 = [iob, iccd]
                    else:  #link to first norm value
                        relbs = bspix2 / float(
                            commands.getoutput(
                                'dmkeypar ' + locclu + '/ktofrsp/sp14_' +
                                obsids[iobiccd1[0]] + '_' + str(ishell) +
                                '_ccd' + presccd[iobiccd1[0]][iobiccd1[1]] +
                                '.fits backscal echo+')) / (64.0 * 1024.**2.)
                        exec 'link(em' + str(
                            ishell) + '_' + ob + '_' + ccd + '.norm, em' + str(
                                ishell) + '_' + obsids[
                                    iobiccd1[0]] + '_' + presccd[iobiccd1[0]][
                                        iobiccd1[1]] + '.norm * relbs )'

            # Fitting
            exec 'fit(' + srcstr[0:-1] + ')'

            # plotting
            if ishell != 0: delete_window('all')
            x1 = [0., 0.5, 0., 0.5]
            y1 = [0.5, 0.5, 0., 0.]
            for iob in range(len(obsids)):
                ob = obsids[iob]
                if len(presccd[iob]) > 0:
                    add_window(8.5, 8.5, 'inches')
                for iccd in range(len(presccd[iob])):
                    ccd = presccd[iob][iccd]
                    add_frame(x1[iccd], y1[iccd], x1[iccd] + 0.5,
                              y1[iccd] + 0.5)
                    add_curve(
                        get_data_plot(ob + '.' + str(ishell) + '.' + ccd).x,
                        get_data_plot(ob + '.' + str(ishell) + '.' + ccd).y,
                        dataplot)
                    add_histogram(
                        get_model_plot(ob + '.' + str(ishell) + '.' + ccd).xlo,
                        get_model_plot(ob + '.' + str(ishell) + '.' + ccd).xhi,
                        get_model_plot(ob + '.' + str(ishell) + '.' + ccd).y,
                        modelplot)
                    set_plot_title(clu + ' OB' + ob + ' CCD' + ccd)
                    split(2)
                    add_curve(
                        get_resid_plot(ob + '.' + str(ishell) + '.' + ccd).x,
                        get_resid_plot(ob + '.' + str(ishell) + '.' + ccd).y,
                        dataplot)
                    add_hline(0)
                if os.path.exists('fitplots/ktofr14_' + clu + '_' + ob + '_' +
                                  str(ishell) + '_hien.eps'):
                    os.remove('fitplots/ktofr14_' + clu + '_' + ob + '_' +
                              str(ishell) + '_hien.eps')
                if len(presccd[iob]) > 0:
                    print_window(
                        'fitplots/ktofr14_' + clu + '_' + ob + '_' +
                        str(ishell) + '_hien',
                        ['format', 'eps', 'orientation', 'landscape'])

            normv = -1 * n.ones((len(obsids), 4))
            if get_fit_results().rstat < 3:

                # Error calculation
                exec 'proj(' + srcstr + ' em' + str(ishell) + '_' + obsids[
                    firstiob] + '_' + presccd[firstiob][0] + '.kt )'

                #15 if get_proj_results().parmaxes[0]!=None or ishell2==len(r1)-1 :

                vkt.append(get_proj_results().parvals[0])
                vktp.append(get_proj_results().parmaxes[0])
                vktm.append(get_proj_results().parmins[0])
                vrstat.append(get_fit_results().rstat)

                # save normalizations
                for iob in range(len(obsids)):
                    ob = obsids[iob]
                    for iccd in range(len(presccd[iob])):
                        ccd = presccd[iob][iccd]
                        exec 'normv[iob,ccdindex[ccd]]=em' + str(
                            ishell) + '_' + ob + '_' + ccd + '.norm.val'

                # best-fit Z
                exec 'bestfitz = em' + str(ishell) + '_' + obsids[
                    firstiob] + '_' + presccd[firstiob][0] + '.abundanc.val'
                # best-fit nH
                bestfitnh = ab.nh.val

            else:

                # No error calculation for bad fits
                vkt.append(n.nan)
                vktp.append(n.nan)
                vktm.append(n.nan)
                vrstat.append(get_fit_results().rstat)

                # save normalizations
                for iob in range(len(obsids)):
                    ob = obsids[iob]
                    for iccd in range(len(presccd[iob])):
                        ccd = presccd[iob][iccd]
                        normv[iob, ccdindex[ccd]] = n.nan

                # best-fit Z
                bestfitz = n.nan
                # best-fit nH
                bestfitnh = n.nan

            globmod.globstr1 = globmod.globstr1 + str(rz1[-1]) + ' ' + str(
                rz2[-1]) + ' ' + str(vkt[-1]) + ' ' + str(
                    vktm[-1]) + ' ' + str(vktp[-1]) + ' ' + str(
                        vrstat[-1]) + ' ' + str(-1) + ' ' + str(
                            -1) + ' ' + str(-1) + ' ' + str(
                                bestfitz) + ' ' + str(bestfitnh) + '\n'

            # save normalization results:

            # rin, rout
            globmod.globstr3 = globmod.globstr3 + str(rz1[-1]) + '\t' + str(
                rz2[-1]) + '\t'

            # the normalization for each (ob,ccd)
            for iob in range(len(obsids)):
                ob = obsids[iob]
                for ccd in presccd[iob]:
                    globmod.globstr3 = globmod.globstr3 + '-1' + ' '  #13 used to be: +str(normv[iob,ccdindex[ccd]]) +' '
            globmod.globstr3 = globmod.globstr3 + '\t'

            # the solid angle covered by dataset as a fraction of total
            # annulus solid angle, PI (Rout^2-Rin^2)
            for iob in range(len(obsids)):
                ob = obsids[iob]
                for ccd in presccd[iob]:
                    bspix2 = float(
                        commands.getoutput(
                            'dmkeypar ' + locclu + '/ktofrsp/sp14_' + ob +
                            '_' + str(ishell) + '_ccd' + ccd +
                            '.fits backscal echo+')) * 64.0 * 1024.**2.
                    globmod.globstr3 = globmod.globstr3 + str(
                        bspix2 / anarea) + ' '

            # Reduced statistic
            globmod.globstr3 = globmod.globstr3 + '\t' + '-1' + '\t'  #13 used too be: str(vrstat[-1]) + '\t'

            # Number of net counts, using backscal scaling
            for iob in range(len(obsids)):
                ob = obsids[iob]
                for ccd in presccd[iob]:
                    bgct = get_data(ob + '.' + str(
                        ishell) + '.' + ccd).backscal * get_data(
                            ob + '.' + str(ishell) + '.' +
                            ccd).exposure / get_data(
                                'bg' + ob + '.' + ccd).backscal / get_data(
                                    'bg' + ob + '.' + ccd
                                ).exposure * calc_data_sum(
                                    id='bg' + ob + '.' + ccd, lo=0.3, hi=7.0
                                )  # the number of BG counts to be subtracted
                    globmod.globstr3 = globmod.globstr3 + str(
                        max(
                            calc_data_sum(
                                id=ob + '.' + str(ishell) + '.' + ccd,
                                lo=0.3,
                                hi=7.0) - bgct, 1.)) + " "
            globmod.globstr3 = globmod.globstr3 + '\t'

            # the --HE-- normalization for each (ob,ccd)
            for iob in range(len(obsids)):
                ob = obsids[iob]
                for ccd in presccd[iob]:
                    globmod.globstr3 = globmod.globstr3 + str(normv[
                        iob,
                        ccdindex[ccd]]) + ' '  #13 normv used to be normhev
            globmod.globstr3 = globmod.globstr3 + '\t'

            # Reduced statistic -- HE --
            globmod.globstr3 = globmod.globstr3 + '\t' + str(
                vrstat[-1])  #13 vrstat used to be vrstathien

            globmod.globstr3 = globmod.globstr3 + '\n'

            ############### end of fitting if statement ############################

        # For the next ishell loop:
        i1 = i2
        i2 = i1 + 1
        ishell = ishell + 1

        ########################### end of ishell loop ##############################

    kthfile = open(locclu + '/ktofr14_hien.txt', 'w')
    kthfile.write(globmod.globstr1)
    kthfile.close()

    normfile = open(locclu + '/ktofr14_norm3.txt', 'w')
    normfile.write(globmod.globstr3)
    normfile.close()

    os.system('sed -i s/None/Nan/g ' + locclu + '/ktofr14_hien.txt')
    os.system('sed -i s/None/Nan/g ' + locclu + '/ktofr14_norm3.txt')
Пример #11
0
def radial(clu, ob, r1mpcpix, rmax, obsids):

    # obsids param only used to get bg count to compare with counts from merged file.
    # all operations done on ob, which is one element of obsids.

    locclu = 'mfe_' + clu

    if not os.path.exists(locclu + '/radp' + ob):
        os.mkdir(locclu + '/radp' + ob)

    # create high-energy photons only event file, to use to estimate BG level
    os.system('punlearn dmcopy')
    os.system('dmcopy "' + locclu + '/clean' + ob +
              '.fits[energy=9500:12000]" ' + locclu + '/hien' + ob + '.fits')

    # center of the cluster
    acao = asol6_loc.asol(ob, 'asol')
    (rac, decc) = centradec.getrd(locclu)
    os.system('punlearn dmcoords')
    os.system('dmcoords ' + locclu + '/clean' + ob + '.fits asolfile="' +
              acao + '" option=cel ra=' + rac + ' dec=' + decc + ' celfmt=hms')
    xc = commands.getoutput('pget dmcoords x')
    yc = commands.getoutput('pget dmcoords y')
    xcf = float(xc)
    ycf = float(yc)

    # ra, dec in degrees
    os.system('punlearn dmcoords')
    os.system('dmcoords ' + locclu + '/clean' + ob + '.fits asolfile="' +
              acao + '" option=sky x=' + xc + ' y=' + yc + ' celfmt=deg')
    racd = float(commands.getoutput('pget dmcoords ra'))
    decd = float(commands.getoutput('pget dmcoords dec'))

    # read all point source areas to subtract them later from observed area
    ptfile = open(locclu + '/reg/pt0mfe_wcs.reg', 'r')
    regline = ptfile.readline()
    regv = []
    ptr = []  # how far from (rac,decc) in pixels
    ptdr = []  # 'radius' of circle/ellipse in pixels
    while regline != '\n' and regline != '':

        if regline[0] != '#':

            regv.append(regline)

            s1 = regline.partition('(')

            s2 = s1[2].partition(':')
            hh = float(s2[0])
            s3 = s2[2].partition(':')
            mm = float(s3[0])
            s4 = s3[2].partition(',')
            ss = float(s4[0])

            ptra = 15. * (hh + mm / 60. + ss / 3600.)

            s5 = s4[2].partition(':')
            dd = float(s5[0])
            if s5[0][0] == '+': ff = 1.
            elif s5[0][0] == '-': ff = -1.
            s6 = s5[2].partition(':')
            am = float(s6[0])
            s7 = s6[2].partition(',')
            ac = float(s7[0])

            ptdec = dd + ff * (am / 60. + ac / 3600.)

            d2 = (((ptra - racd) * numpy.cos(decd / 180. * numpy.pi))**2. +
                  (ptdec - decd)**2.) * (3600. / 0.492)**2.

            ptr.append(d2**0.5)

            if s1[0] == 'ellipse':
                s8 = s7[2].partition("',")
                s9 = s8[2].partition("',")
                ptdr.append(60. / 0.492 * max(float(s8[0]), float(s9[0])))
            elif s1[0] == 'circle':
                s8 = s7[2].partition("'")
                ptdr.append(60. / 0.492 * float(s8[0]))
            else:
                print
                print 'BAAAAAAAAAAAAAD REGIOOOOOOOOOOOON'
                print

        regline = ptfile.readline()
    ptfile.close()

    # get chip regions
    chipx = commands.getoutput('dmlist "' + locclu + '/reg/chipsreg' + ob +
                               '.fits[cols x]" data,clean')
    t1 = chipx.split('\n')
    t1.remove(t1[0])

    chipy = commands.getoutput('dmlist "' + locclu + '/reg/chipsreg' + ob +
                               '.fits[cols y]" data,clean')
    u1 = chipy.split('\n')
    u1.remove(u1[0])

    # find chips 0,1,2,3
    ccdid = commands.getoutput('dmlist "' + locclu + '/reg/chipsreg' + ob +
                               '.fits[cols ccd_id]" data,clean')
    c1 = ccdid.split('\n')
    c1.remove(c1[0])

    goodchips = [[], [], [], []]
    #           I  5  6  7
    for i in range(len(c1)):
        if int(c1[i]) in (0, 1, 2, 3): goodchips[0].append(i)
        if int(c1[i]) == 5: goodchips[1].append(i)
        if int(c1[i]) == 6: goodchips[2].append(i)
        if int(c1[i]) == 7: goodchips[3].append(i)

    ccdreg = [[], [], [], []]
    for iccd in range(4):

        if len(goodchips[iccd]) > 0:
            for igood in range(len(goodchips[iccd])):

                i = goodchips[iccd][igood]

                polyx = []
                t2 = t1[i]
                t3 = t2.lstrip()
                t4 = t3.partition(' ')
                while ('NaN' not in t4[0]) and (t4[1] != ''):
                    polyx.append(float(t4[0]))
                    t2 = t4[2]
                    t3 = t2.lstrip()
                    t4 = t3.partition(' ')

                polyy = []
                u2 = u1[i]
                u3 = u2.lstrip()
                u4 = u3.partition(' ')
                while ('NaN' not in u4[0]) and (u4[1] != ''):
                    polyy.append(float(u4[0]))
                    u2 = u4[2]
                    u3 = u2.lstrip()
                    u4 = u3.partition(' ')

                ccdreg[iccd].append('polygon(')
                for j in range(len(polyx)):
                    ccdreg[iccd][igood] = ccdreg[iccd][igood] + str(
                        polyx[j]) + ',' + str(polyy[j]) + ','
                ccdreg[iccd][igood] = ccdreg[iccd][igood][0:-1]
                ccdreg[iccd][igood] = ccdreg[iccd][igood] + ')'

    ccdname = ['i', '5', '6', '7']
    for iccd in range(4):
        if len(ccdreg[iccd]) > 0:
            os.system('punlearn dmcopy')
            os.system('dmcopy "' + locclu + '/bg/bg' + ob + '_ccd' +
                      ccdname[iccd] + 'r.fits[energy=300:7000]" ' + locclu +
                      '/bg/bg' + ob + '_ccd' + ccdname[iccd] + 'r_en.fits')
            if not os.path.exists(locclu + '/bg/bg' + ob + '_ccd' +
                                  ccdname[iccd] + 'r_en.fits'):
                ccdreg[iccd] = []

    exposure = float(
        commands.getoutput('dmkeypar ' + locclu + '/evt' + ob +
                           '_c.fits exposure echo+'))

    # Making radbins5.txt
    vrlo = []
    vrhi = []
    if not os.path.exists(locclu + '/radbins5.txt'):

        rmin = 1.2 / .492  # in pixels.  1.2" = angular size of 10kpc at z=1

        rlo = rmin
        rhi = rmin * 1.25
        hienrlo1 = -1
        hienrlo2 = -1
        hienrlo3 = -1
        ct1 = 0
        ct2 = 0
        ct3 = 0
        print 'rmax ', rmax
        while rhi <= rmax:

            # make annulus-minus-ptsrc region for the shell between rlo and rhi.
            regfile = open(locclu + '/an_test.reg', 'w')
            regfile.write('annulus(' + xc + ',' + yc + ',' + str(rlo) + ',' +
                          str(rhi) + ')\n')
            for ii in range(len(ptr)):
                if ptr[ii] - ptdr[ii] <= rhi and ptr[ii] + ptdr[ii] >= rlo:
                    regfile.write('-' + regv[ii])
            regfile.close()

            ct = float(
                commands.getoutput('dmlist "' + locclu +
                                   '/mrgclean_en.fits[sky=region(' + locclu +
                                   '/an_test.reg)]" counts'))

            sclbgct = 0.
            for oobb in obsids:
                for ccdn in ['i', '5', '6', '7']:

                    if os.path.exists(locclu + '/bg/bg' + oobb + '_ccd' +
                                      ccdn + 'r_en.fits'):
                        sclbgct = sclbgct + float(
                            commands.getoutput('dmkeypar ' + locclu +
                                               '/clean' + oobb +
                                               '.fits exposure echo+')
                        ) / float(
                            commands.getoutput(
                                'dmkeypar ' + locclu + '/bg/bg' + oobb +
                                '_ccd' + ccdn + 'r_en.fits exposure echo+'
                            )) * float(
                                commands.getoutput(
                                    'dmlist "' + locclu + '/bg/bg' + oobb +
                                    '_ccd' + ccdn + 'r_en.fits[sky=region(' +
                                    locclu + '/an_test.reg)]" counts'))

            shellct = ct - sclbgct

            os.remove(locclu + '/an_test.reg')
            print rlo, rhi, shellct

            # shell has enough counts, OR we've reached rmax
            if shellct > 100 or rhi > 0.99 * rmax:
                vrlo.append(rlo)
                vrhi.append(rhi)

                if rhi > 0.99 * rmax: break

                # radius where we start measuring hi-energy counts
                if ct <= 1.5 * sclbgct:
                    if ct1 == 0: hienrlo1 = float(rlo)  # first choice
                    ct1 = ct1 + int(ct)
                if ct <= 2.0 * sclbgct:
                    if ct2 == 0:
                        hienrlo2 = float(
                            rlo)  # second choice, in case 1st doesnt exist
                    ct2 = ct2 + int(ct)
                if ct <= 3.0 * sclbgct:
                    if ct3 == 0: hienrlo3 = float(rlo)  # 3rd choice
                    ct3 = ct3 + int(ct)

                rlo = float(rhi)
                rhi = rhi * 1.25

            else:
                if ct == 0 and len(vrlo) == 0: rlo = float(rhi)
                rhi = rhi * 1.25

            if rhi > rmax: rhi = rmax

        hienrlofile = open(locclu + '/hienrlo.txt', 'w')
        hienrlofile.write('1.5 ' + str(hienrlo1) + ' ' + str(ct1) + '\n')
        hienrlofile.write('2 ' + str(hienrlo2) + ' ' + str(ct2) + '\n')
        hienrlofile.write('3 ' + str(hienrlo3) + ' ' + str(ct3) + '\n')
        hienrlofile.close()

        binfile = open(locclu + '/radbins5.txt', 'w')
        for ir in range(len(vrlo)):
            binfile.write(str(vrlo[ir]) + ' ' + str(vrhi[ir]) + '\n')
        binfile.close()

        xinfile = open(locclu + '/ctofr/xin.txt', 'w')
        for ir in range(len(vrlo)):
            xinfile.write(str(vrlo[ir] / r1mpcpix) + '\n')
        xinfile.close()

        xoutfile = open(locclu + '/ctofr/xout.txt', 'w')
        for ir in range(len(vrhi)):
            xoutfile.write(str(vrhi[ir] / r1mpcpix) + '\n')
        xoutfile.close()

    # the case where radbins5.txt has been created in a previous ob:
    else:

        # Loading radial bin edges, from radbins5.txt
        binfile = open(locclu + '/radbins5.txt', 'r')
        linestr = binfile.readline()
        while linestr != '':
            intermed = linestr.split(' ')
            vrlo.append(float(intermed[0]))
            vrhi.append(float(intermed[1][0:-1]))
            linestr = binfile.readline()
        binfile.close()

    # Hi BG corner region
    if os.path.exists(locclu + '/reg/hibgcorner' + ob + '.reg'):
        hibgfile = open(locclu + '/reg/hibgcorner' + ob + '.reg', 'r')
        hibgstr = hibgfile.readline()
        hibgfile.close()
        hibgx = float(hibgstr[7:-1].split(',')[0])
        hibgy = float(hibgstr[7:-1].split(',')[1])
        hibgr = ((hibgx - xcf)**2. + (hibgy - ycf)**2.)**0.5

    hien = [[], [], [], []]
    bghien = [[], [], [], []]
    hienomega = [[], [], [], []]
    ccdlist = ['0,1,2,3', '5', '6', '7']
    scl = [-1, -1, -1, -1]
    for iccd in range(4):

        if len(ccdreg[iccd]) > 0:

            scl[iccd] = exposure / float(
                commands.getoutput('dmkeypar "' + locclu + '/bg/bg' + ob +
                                   '_ccd' + ccdname[iccd] +
                                   'r_en.fits" exposure echo+'))

            radintfile2err = open(
                locclu + '/ctofr/srcprof' + ob + '_ccd' + ccdname[iccd] +
                '.txt',
                'w')  # poisson and systematic errors added in quadrature
            radintfile2err.write("# R1 R2 SB SBERR\n")
            bgfile = open(
                locclu + '/ctofr/bkgprof' + ob + '_ccd' + ccdname[iccd] +
                '.txt', 'w')
            bsfile = open(
                locclu + '/ctofr/omega' + ob + '_ccd' + ccdname[iccd] + '.txt',
                'w')

            bgexp = float(
                commands.getoutput('dmkeypar ' + locclu + '/bg/bg' + ob +
                                   '_ccd' + ccdname[iccd] +
                                   'r_en.fits exposure echo+'))

            for i in range(len(vrlo)):

                # make region of intersection of annulus with chip regions
                fovfile = open(
                    locclu + '/radp' + ob + '/an' + str(i) + '_ccd' +
                    ccdname[iccd] + '_xfov.reg', 'w')
                for ii in range(len(ccdreg[iccd])):
                    fovfile.write('+annulus(' + xc + ',' + yc + ',' +
                                  str(vrlo[i]) + ',' + str(vrhi[i]) + ') * ' +
                                  ccdreg[iccd][ii] + '\n')
                fovfile.close()

                # make region of intersection of annulus with chip regions MINUS PTSRCs
                fovptfile = open(
                    locclu + '/radp' + ob + '/an' + str(i) + '_ccd' +
                    ccdname[iccd] + '_xfov_pt.reg', 'w')
                for ii in range(len(ccdreg[iccd])):
                    fovptfile.write('+annulus(' + xc + ',' + yc + ',' +
                                    str(vrlo[i]) + ',' + str(vrhi[i]) +
                                    ') * ' + ccdreg[iccd][ii] + '\n')
                    for jj in range(len(ptr)):
                        if ptr[jj] - ptdr[jj] <= vrhi[
                                i] and ptr[jj] + ptdr[jj] >= vrlo[i]:
                            fovptfile.write('-' + regv[jj][0:-1] + ' ')
                    if os.path.exists(locclu + '/reg/hibgcorner' + ob +
                                      '.reg') and hibgr - 1095. <= vrhi[
                                          i] and hibgr + 1095. >= vrlo[i]:
                        fovptfile.write('-' + hibgstr[0:-1])
                    fovptfile.write('\n')
                fovptfile.close()

                # calculate counts/area/solidangle/second
                ct = float(
                    commands.getoutput('dmlist "' + locclu + '/evt' + ob +
                                       '_c.fits[sky=region(' + locclu +
                                       '/radp' + ob + '/an' + str(i) + '_ccd' +
                                       ccdname[iccd] +
                                       '_xfov_pt.reg)]" counts'))
                bgct = float(
                    commands.getoutput(
                        'dmlist "' + locclu + '/bg/bg' + ob + '_ccd' +
                        ccdname[iccd] +
                        'r_en.fits[energy=700:2000][sky=region(' + locclu +
                        '/radp' + ob + '/an' + str(i) + '_ccd' +
                        ccdname[iccd] + '_xfov_pt.reg)]" counts')
                )  # more exact than above (deleted) line, but could be slower if many ptsrcs
                #netct = ct - bgct * scl[iccd]

                # make spectrum of annulus
                os.system('punlearn dmextract')
                os.system('dmextract "' + locclu + '/clean' + ob +
                          '.fits[ccd_id=' + ccdlist[iccd] + '][sky=region(' +
                          locclu + '/radp' + ob + '/an' + str(i) + '_ccd' +
                          ccdname[iccd] + '_xfov_pt.reg)][bin PI]" ' + locclu +
                          '/radp' + ob + '/sp' + str(i) + '_ccd' +
                          ccdname[iccd] +
                          '.fits opt=pha1 wmap="[energy=700:2000][bin det=8]"')

                # calculate hi-energy SB
                hien[iccd].append(
                    float(
                        commands.getoutput('dmlist "' + locclu + '/hien' + ob +
                                           '.fits[ccd_id=' + ccdlist[iccd] +
                                           '][sky=region(' + locclu + '/radp' +
                                           ob + '/an' + str(i) + '_ccd' +
                                           ccdname[iccd] +
                                           '_xfov_pt.reg)]" counts')))
                bghien[iccd].append(
                    float(
                        commands.getoutput(
                            'dmlist "' + locclu + '/bg/bg' + ob + '_ccd' +
                            ccdname[iccd] +
                            'r.fits[energy=9500:12000][sky=region(' + locclu +
                            '/radp' + ob + '/an' + str(i) + '_ccd' +
                            ccdname[iccd] + '_xfov_pt.reg)]" counts')))
                hienomega[iccd].append(
                    float(
                        commands.getoutput('dmkeypar ' + locclu + '/radp' +
                                           ob + '/sp' + str(i) + '_ccd' +
                                           ccdname[iccd] +
                                           '.fits backscal echo+')))

                # make ARF for radial bin
                arf4radp4.arf(clu, ob, i, ccdname[iccd])

                # calculate solid angle from BACKSCAL keyword in spectrum file
                bs = float(
                    commands.getoutput('dmkeypar ' + locclu + '/radp' + ob +
                                       '/sp' + str(i) + '_ccd' +
                                       ccdname[iccd] + '.fits backscal echo+'))

                # calculate effective area
                os.system('punlearn dmstat')
                # this if statement handles the case when the annulus thickness is less than a pixel,
                # which causes dmstat to return area=0
                if vrhi[i] - vrlo[i] > 1.5:
                    if vrhi[i] <= 0.1 * r1mpcpix:
                        os.system('dmstat "' + locclu + '/instmap/expmap' +
                                  ob + '_ccd' + ccdname[iccd] +
                                  '_in.fits[sky=region(' + locclu + '/radp' +
                                  ob + '/an' + str(i) + '_ccd' +
                                  ccdname[iccd] + '_xfov.reg)]" centroid=no')
                    else:
                        os.system('dmstat "' + locclu + '/instmap/expmap' +
                                  ob + '_ccd' + ccdname[iccd] +
                                  '_out.fits[sky=region(' + locclu + '/radp' +
                                  ob + '/an' + str(i) + '_ccd' +
                                  ccdname[iccd] + '_xfov.reg)]" centroid=no')
                else:
                    if vrhi[i] <= 0.1 * r1mpcpix:
                        os.system('dmstat "' + locclu + '/instmap/expmap' +
                                  ob + '_ccd' + ccdname[iccd] +
                                  '_in.fits[sky=circle(' + xc + ',' + yc +
                                  ',' + str(vrhi[i]) + ')]" centroid=no')
                    else:
                        os.system('dmstat "' + locclu + '/instmap/expmap' +
                                  ob + '_ccd' + ccdname[iccd] +
                                  '_out.fits[sky=circle(' + xc + ',' + yc +
                                  ',' + str(vrhi[i]) + ')]" centroid=no')

                try:
                    area = float(commands.getoutput('pget dmstat out_mean'))
                except ValueError:
                    area = 0
                # the above try-exept is for the case, where the expmap needed to calulate area doesnt exist.
                # this can happen when the there is no intersection between the in/out region and a ccd.

                if bs != 0 and area != 0 and ct != 0:

                    sb = ct / bs / area / exposure
                    #sberr = ( ct + scl**2.*bgct )**.5 / bs / area / exposure # poisson error
                    sberr = ct**.5 / bs / area / exposure  # poisson error
                    radintfile2err.write(
                        str(vrlo[i] / r1mpcpix) + ' ' +
                        str(vrhi[i] / r1mpcpix) + ' ' + str(sb) + ' ' + str(
                            (sberr**2. + sb**2. / 25.)**0.5) +
                        '\n')  # poisson and 1/5 fraction, added in quadrature

                    bgsb = bgct / bs / area / bgexp
                    bgsberr = bgct**0.5 / bs / area / bgexp
                    bgfile.write(
                        str(vrlo[i] / r1mpcpix) + ' ' +
                        str(vrhi[i] / r1mpcpix) + ' ' + str(bgsb) + ' ' +
                        str(bgsberr) + '\n')
                    bsfile.write(
                        str(vrlo[i] / r1mpcpix) + ' ' + str(bs) + ' ' +
                        str(bs * 64.0 * 1024.**2. / (numpy.pi) /
                            (vrhi[i]**2 - vrlo[i]**2)) + '\n')

            radintfile2err.close()
            bgfile.close()
            bsfile.close()

    # write BG scaling factors from hi-energy counts
    # get the 1000 outermost counts, and save their region
    hientot = [0., 0., 0., 0.]
    bghientot = [0., 0., 0., 0.]
    hienomegatot = [0., 0., 0., 0.]
    sclfile = open(locclu + '/ctofr/scl' + ob + '.txt', 'w')
    for iccd in range(4):
        if len(ccdreg[iccd]) > 0:
            irlo = len(vrlo) - 1
            while hientot[iccd] < 1e3 and irlo >= 0:
                hientot[iccd] = hientot[iccd] + hien[iccd][irlo]
                bghientot[iccd] = bghientot[iccd] + bghien[iccd][irlo]
                hienomegatot[iccd] = hienomegatot[iccd] + hienomega[iccd][irlo]
                irlo = irlo - 1
            irlo = irlo + 1

            if bghientot[iccd] != 0:

                # write the BG counts and backscales
                sclfile.write(
                    str(scl[iccd]) + ' ' +
                    str(hientot[iccd] / bghientot[iccd]) + ' ' +
                    str(hientot[iccd]) + ' ' + str(bghientot[iccd]) + ' ' +
                    str(hienomegatot[iccd]) + ' ' + str(irlo) + ' ' +
                    str(vrlo[irlo]) + '\n')

                # make region out of which we estimate the Hi-energy cts in the src dataset
                losrcregfile = open(
                    locclu + '/reg/losrc' + ob + '_ccd' + ccdname[iccd] +
                    '.reg', 'w')
                for polyg in ccdreg[iccd]:
                    losrcregfile.write('+annulus(' + xc + ',' + yc + ',' +
                                       str(vrlo[irlo]) + ',' + str(vrhi[-1]) +
                                       ') * ' + polyg + '\n')
                    for jj in range(len(ptr)):
                        if ptr[jj] - ptdr[jj] <= vrhi[
                                -1] and ptr[jj] + ptdr[jj] >= vrlo[irlo]:
                            losrcregfile.write('-' + regv[jj][0:-1] + ' ')
                    if iccd == 0 and os.path.exists(
                            locclu + '/reg/hibgcorner' + ob +
                            '.reg') and hibgr - 1095. <= vrhi[
                                -1] and hibgr + 1095. >= vrlo[irlo]:
                        losrcregfile.write('-' + hibgstr[0:-1])
                    losrcregfile.write('\n')
                losrcregfile.close()

                # extract spectrum from the above region, to get the BACKSCAL value from the spectrum
                os.system('punlearn dmextract')
                os.system('dmextract "' + locclu + '/clean' + ob +
                          '.fits[sky=region(' + locclu + '/reg/losrc' + ob +
                          '_ccd' + ccdname[iccd] + '.reg)][bin PI]" ' +
                          locclu + '/spec/losrc' + ob + '_ccd' +
                          ccdname[iccd] +
                          '.fits opt=pha1 wmap="[energy=700:2000][bin det=8]"')

            else:
                sclfile.write(
                    str(scl[iccd]) + ' ' + 'NaN ' + str(hientot[iccd]) + ' ' +
                    str(bghientot[iccd]) + ' ' + str(hienomegatot[iccd]) +
                    ' ' + str(irlo) + ' ' + str(vrlo[irlo]) + '\n')

        # if this ccd is not in this obsid
        else:
            sclfile.write('-1 -1 -1 -1 -1 -1 -1\n')
    sclfile.close()
Пример #12
0
def rmf(locclu, ob, ccdstr):

    acao = asol6_loc.asol(ob, 'asol')
    (rac, decc) = centradec.getrd(locclu)
    caldbfolder = commands.getoutput('echo $CALDB')

    if ccdstr == 'i': ccdlist = '0,1,2,3'
    else: ccdlist = ccdstr

    os.system('punlearn dmcoords')
    os.system('dmcoords ' + locclu + '/clean' + ob + '.fits ' + acao + ' ra=' +
              rac + ' dec=' + decc + ' option=cel celfmt=hms mode=h')
    chipx = commands.getoutput('pget dmcoords chipx')
    chipy = commands.getoutput('pget dmcoords chipy')
    centerccd = commands.getoutput('pget dmcoords chip_id')

    if centerccd not in ccdlist:

        os.system('punlearn dmstat')
        os.system('dmstat "' + locclu + '/clean' + ob + '.fits[ccd_id=' +
                  ccdlist + '][cols x]" median=yes')
        xx = commands.getoutput('pget dmstat out_median')
        os.system('punlearn dmstat')
        os.system('dmstat "' + locclu + '/clean' + ob + '.fits[ccd_id=' +
                  ccdlist + '][cols y]" median=yes')
        yy = commands.getoutput('pget dmstat out_median')

        os.system('punlearn dmcoords')
        os.system('dmcoords ' + locclu + '/clean' + ob + '.fits ' + acao +
                  ' x=' + xx + ' y=' + yy + ' option=sky mode=h')
        chipx = commands.getoutput('pget dmcoords chipx')
        chipy = commands.getoutput('pget dmcoords chipy')
        centerccd = commands.getoutput('pget dmcoords chip_id')

    if float(chipx) < 1: chipx = '1'
    if float(chipx) > 1024: chipx = '1024'
    if float(chipy) < 1: chipy = '1'
    if float(chipy) > 1024: chipy = '1024'

    # RMF -----------------------
    fptemp = float(
        commands.getoutput('dmkeypar ' + locclu + '/clean' + ob +
                           '.fits fp_temp echo+'))
    tgaincor = commands.getoutput('dmkeypar ' + locclu + '/clean' + ob +
                                  '.fits tgaincor echo+')
    cticor = commands.getoutput('dmkeypar ' + locclu + '/clean' + ob +
                                '.fits cti_corr echo+')
    ctiapp = commands.getoutput('dmkeypar ' + locclu + '/clean' + ob +
                                '.fits cti_app echo+')

    # using mkacisrmf or...
    if (fptemp < 158. or (fptemp >= 158. and ccdstr in ['5', '7'])) and (
            ctiapp != 'NNNNNNNNNN' or cticor == 'TRUE'
            or cticor == '1') and (tgaincor == 'T'):

        gainfile = commands.getoutput('dmkeypar ' + locclu + '/clean' + ob +
                                      '.fits gainfile echo+')
        gainver = gainfile.split('.fits')[0][-1]
        caldbls = commands.getoutput('ls $CALDB/data/chandra/acis/p2_resp/' +
                                     gainfile[0:15] + '*' + gainver + '.fits')
        caldbresp = caldbls.split('\n')

        if len(caldbresp) != 1:
            print
            print
            print '  ############### MORE OR LESS THAN ONE P2RESP FILES FOUND !!!!!!!!!! ###################'
            print
            print

        os.system('punlearn mkacisrmf')
        os.system('pset mkacisrmf infile=' + caldbresp[0])
        os.system('pset mkacisrmf outfile=' + locclu + '/spec/sp' + ob +
                  '_ccd' + ccdstr + '_center.wrmf')
        os.system('pset mkacisrmf energy="0.3:11.0:0.01" channel="1:1024:1"')
        os.system('pset mkacisrmf chantype="PI"')
        os.system('pset mkacisrmf wmap=none')
        os.system('pset mkacisrmf chipx=' + chipx)
        os.system('pset mkacisrmf chipy=' + chipy)
        os.system('pset mkacisrmf ccd_id=' + centerccd)
        os.system('pset mkacisrmf gain=' + caldbfolder +
                  '/data/chandra/acis/det_gain/' + gainfile)
        os.system('pset mkacisrmf asolfile=none')
        os.system('mkacisrmf mode=h verbose=1')

    # using mkrmf
    else:

        os.system('punlearn acis_fef_lookup')
        os.system('acis_fef_lookup ' + locclu + '/clean' + ob +
                  '.fits chipx=' + chipx + ' chipy=' + chipy + ' chipid=' +
                  centerccd)

        os.system('punlearn mkrmf')
        os.system('pset mkrmf infile=' +
                  commands.getoutput('pget acis_fef_lookup outfile'))
        os.system('pset mkrmf outfile=' + locclu + '/spec/sp' + ob + '_ccd' +
                  ccdstr + '_center.wrmf')
        os.system('pset mkrmf axis1="energy=0.3:11.0:0.01"')
        os.system('pset mkrmf axis2="pi=1:1024:1"')
        os.system('pset mkrmf weights=none')
        os.system('mkrmf verbose=1 mode=h')
Пример #13
0
def exp(clu, obsids, inorout):

    locclu = 'mfe_' + clu
    origclu = 'orig_' + clu

    #os.system( 'echo "binsrc2, \''+clu+'\' , \'src'+inorout+'.txt\' " | idl' )

    binsrc2.bin(clu, 'src' + inorout + '.txt')

    (rac, decc) = centradec.getrd(locclu)

    if not os.path.exists(locclu + '/instmap'): os.mkdir(locclu + '/instmap')

    hardobs = hardsoft3.hardsoft(clu, obsids, 'h')
    softobs = hardsoft3.hardsoft(clu, obsids, 's')

    for i in range(len(obsids)):
        ob = obsids[i]

        acao = asol6_loc.asol(ob, 'asol')

        os.system('punlearn ardlib')
        os.system('acis_set_ardlib ' + asol6_loc.asol(ob, 'bpix'))

        ccds = ''

        # if this is the in region, then check if there are counts in in region
        # but if it's the out region, only check if there are counts in the ccd of interest:
        # the reason is we want to include data that is beyond the out region, which stops
        # at 1Mpc
        if inorout == 'in':
            if int(
                    commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                       '.fits[sky=region(' + locclu + '/reg/' +
                                       inorout +
                                       '.reg)][ccd_id=0]" counts')) > 0:
                ccds = ccds + '0'
            if int(
                    commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                       '.fits[sky=region(' + locclu + '/reg/' +
                                       inorout +
                                       '.reg)][ccd_id=1]" counts')) > 0:
                ccds = ccds + '1'
            if int(
                    commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                       '.fits[sky=region(' + locclu + '/reg/' +
                                       inorout +
                                       '.reg)][ccd_id=2]" counts')) > 0:
                ccds = ccds + '2'
            if int(
                    commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                       '.fits[sky=region(' + locclu + '/reg/' +
                                       inorout +
                                       '.reg)][ccd_id=3]" counts')) > 0:
                ccds = ccds + '3'
            if int(
                    commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                       '.fits[sky=region(' + locclu + '/reg/' +
                                       inorout +
                                       '.reg)][ccd_id=5]" counts')) > 0:
                ccds = ccds + '5'
            if int(
                    commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                       '.fits[sky=region(' + locclu + '/reg/' +
                                       inorout +
                                       '.reg)][ccd_id=6]" counts')) > 0:
                ccds = ccds + '6'
            if int(
                    commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                       '.fits[sky=region(' + locclu + '/reg/' +
                                       inorout +
                                       '.reg)][ccd_id=7]" counts')) > 0:
                ccds = ccds + '7'
        else:
            if int(
                    commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                       '.fits[ccd_id=0]" counts')) > 0:
                ccds = ccds + '0'
            if int(
                    commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                       '.fits[ccd_id=1]" counts')) > 0:
                ccds = ccds + '1'
            if int(
                    commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                       '.fits[ccd_id=2]" counts')) > 0:
                ccds = ccds + '2'
            if int(
                    commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                       '.fits[ccd_id=3]" counts')) > 0:
                ccds = ccds + '3'
            if int(
                    commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                       '.fits[ccd_id=5]" counts')) > 0:
                ccds = ccds + '5'
            if int(
                    commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                       '.fits[ccd_id=6]" counts')) > 0:
                ccds = ccds + '6'
            if int(
                    commands.getoutput('dmlist "' + locclu + '/clean' + ob +
                                       '.fits[ccd_id=7]" counts')) > 0:
                ccds = ccds + '7'

#		ccdscoma=''
#		for j in range(len(ccds)-1) : ccdscoma=ccdscoma+ccds[j]+','
#		ccdscoma=ccdscoma+ccds[-1]

        if not os.path.exists(locclu + '/evt' + ob + '_c.fits'):
            os.system('punlearn dmcopy')
            os.system('dmcopy "' + locclu + '/clean' + ob +
                      '.fits[energy=700:2000]" ' + locclu + '/evt' + ob +
                      '_c.fits')
            #[ccd_id='+ccdscoma+']

        for j in range(len(ccds)):
            if not os.path.exists(locclu + '/instmap/asphist' + ob + '_ccd' +
                                  ccds[j] + '.fits'):
                os.system('punlearn asphist')
                os.system('pset asphist infile=' + acao)
                os.system('pset asphist outfile=' + locclu +
                          '/instmap/asphist' + ob + '_ccd' + ccds[j] + '.fits')
                os.system('pset asphist evtfile="' + locclu + '/clean' + ob +
                          '.fits[ccd_id=' + ccds[j] + ']"')
                os.system('asphist verbose=1 mode=h')

        # Make instrument map
        for j in range(len(ccds)):
            os.system('punlearn mkinstmap')
            os.system('pset mkinstmap pixelgrid="1:1024:#1024,1:1024:#1024"')
            os.system('pset mkinstmap maskfile=' + asol6_loc.asol(ob, 'msk1'))
            #os.system('pset mkinstmap pbkfile='+asol6_loc.asol(ob,'pbk'))
            os.system('pset mkinstmap dafile=CALDB')
            os.system('pset mkinstmap spectrumfile=' + locclu +
                      '/spec/binsrc' + inorout + '.txt')
            os.system('pset mkinstmap obsfile=' + locclu + '/clean' + ob +
                      '.fits')
            os.system('pset mkinstmap detsubsys=ACIS-' + ccds[j])
            os.system('pset mkinstmap outfile=' + locclu +
                      '/instmap/winstmap' + ob + '_ccd' + ccds[j] + '_' +
                      inorout + '.fits')
            os.system('mkinstmap mode=h verbose=1')

        os.system('punlearn dmcoords')
        os.system('dmcoords ' + locclu + '/clean' + ob + '.fits asolfile=' +
                  acao + ' option=cel ra=' + rac + ' dec=' + decc +
                  ' celfmt=hms')
        xc = commands.getoutput('pget dmcoords x')
        yc = commands.getoutput('pget dmcoords y')
        xcf = float(xc)
        ycf = float(yc)

        xs = commands.getoutput('dmlist "' + locclu + '/evt' + ob +
                                '_c.fits[cols x]" data,clean')
        xsv = xs.split('\n')
        xsv.remove(xsv[0])
        for ii in range(len(xsv)):
            xsv[ii] = float(xsv[ii])

        ys = commands.getoutput('dmlist "' + locclu + '/evt' + ob +
                                '_c.fits[cols y]" data,clean')
        ysv = ys.split('\n')
        ysv.remove(ysv[0])
        for ii in range(len(ysv)):
            ysv[ii] = float(ysv[ii])

        xmin = min(xsv)
        xmax = max(xsv)
        ymin = min(ysv)
        ymax = max(ysv)

        # Make exposure map
        os.system('punlearn mkexpmap')
        os.system('pset mkexpmap normalize=yes')
        os.system('pset mkexpmap xygrid=' + str(xmin) + ':' + str(xmax) +
                  ':4,' + str(ymin) + ':' + str(ymax) + ':4')
        os.system('pset mkexpmap useavgaspect=no')
        os.system('pset mkexpmap mode=h')

        for j in range(len(ccds)):
            os.system('mkexpmap instmapfile=' + locclu + '/instmap/winstmap' +
                      ob + '_ccd' + ccds[j] + '_' + inorout +
                      '.fits outfile=' + locclu + '/instmap/expmap' + ob +
                      '_ccd' + ccds[j] + '_' + inorout + '.fits asphistfile=' +
                      locclu + '/instmap/asphist' + ob + '_ccd' + ccds[j] +
                      '.fits verbose=1')

        # if ACIS-I ccd's are preset, merge them.
        if '0' in ccds or '1' in ccds or '2' in ccds or '3' in ccds:

            # Reproject exposure map
            hmrgstr = ''
            for ccd in '0123':
                if ccd in ccds:
                    hmrgstr = hmrgstr + ',' + locclu + '/instmap/expmap' + ob + '_ccd' + ccd + '_' + inorout + '.fits'
            hmrgstr = hmrgstr[1:]

            os.system('punlearn reproject_image')
            os.system('pset reproject_image infile="' + hmrgstr + '"')
            os.system('pset reproject_image outfile=' + locclu +
                      '/instmap/expmap' + ob + '_ccdi_' + inorout + '.fits')
            os.system('pset reproject_image matchfile=' + locclu +
                      '/instmap/expmap' + ob + '_ccd' + ccds[0] + '_' +
                      inorout + '.fits')
            os.system('pset reproject_image method=average')
            os.system('reproject_image mode=h')

            os.system(
                'dmhedit ' + locclu + '/instmap/expmap' + ob + '_ccdi_' +
                inorout +
                '.fits filelist="" op=add key=BUNIT value="cm**2" mode=h')

        # clean up files not needed
        for j in range(len(ccds)):
            os.remove(locclu + '/instmap/winstmap' + ob + '_ccd' + ccds[j] +
                      '_' + inorout + '.fits')

    # more cleanup
    os.system('rm ' + locclu + '/instmap/expmap*_ccd[0123]_*.fits')