forked from xmm/arches
SAS
This commit is contained in:
@@ -60,6 +60,19 @@ def remove_file(filename):
|
||||
if(os.path.isfile(filename)==True):
|
||||
os.remove(filename)
|
||||
|
||||
def move_files(pattern, destination):
|
||||
for file in glob.glob(pattern):
|
||||
shutil.move(file, os.path.join(destination,file))
|
||||
|
||||
def get_first_file(pattern):
|
||||
files = glob.glob(pattern)
|
||||
if not (files):
|
||||
print(f"Files not found from pattern {pattern}")
|
||||
sys.exit()
|
||||
filename = files[0]
|
||||
return filename
|
||||
|
||||
|
||||
# Function to plot Lightcurve
|
||||
def plotLC(plt,threshold,fileName):
|
||||
if fileName != "NOT FOUND":
|
||||
@@ -119,6 +132,9 @@ def init_work_dir(obsid,products_dir=None):
|
||||
events_dir=root_path+'/data/processed'
|
||||
ds9reg_dir=root_path+'/data/ds9reg'
|
||||
|
||||
if (products_dir==None):
|
||||
print('products_dir is not defined')
|
||||
sys.exit()
|
||||
create_folder(products_dir)
|
||||
|
||||
#inargs = ['--version']
|
||||
@@ -153,6 +169,10 @@ def init_work_dir(obsid,products_dir=None):
|
||||
|
||||
""" See also convregion task, for conversion of regions """
|
||||
def get_det_coords(ra,dec,image,key):
|
||||
if(os.path.isfile(image)==False):
|
||||
print(f"{image} is not found")
|
||||
sys.exit()
|
||||
|
||||
logfile=f'{key}-ecoordconv.log'
|
||||
remove_file(logfile)
|
||||
""" Convert sky coordinates to correct DETX/Y """
|
||||
@@ -174,12 +194,39 @@ def get_det_coords(ra,dec,image,key):
|
||||
dety=float(dy)
|
||||
return detx,dety
|
||||
|
||||
def run_mosspectra(key,ccds,check=True,elow="350",ehigh="1100"):
|
||||
def run_mosspectra(key,ccds,check=False,elow="350",ehigh="1100"):
|
||||
|
||||
eventfile = f'{key}-allevc.fits'
|
||||
if(os.path.isfile(eventfile)==False):
|
||||
print(f"{eventfile} is not found, run 01_init_events*.py and above")
|
||||
cwd = os.getcwd()
|
||||
print(f"Current directory: {cwd}")
|
||||
sys.exit()
|
||||
|
||||
unfilt_image = eventfile.replace("allevc","ffov")
|
||||
inargs = [f'table={eventfile}',
|
||||
'withimageset=yes', f'imageset={unfilt_image}',
|
||||
#'filtertype=expression', f'expression={expression}',
|
||||
'ignorelegallimits=yes', 'imagebinning=imageSize',
|
||||
'xcolumn=DETX','ycolumn=DETY','ximagesize=780','yimagesize=780',
|
||||
'ximagemax=19500','yimagemax=19500', 'ximagemin=-19499', 'yimagemin=-19499',]
|
||||
w('evselect', inargs).run()
|
||||
if(os.path.isfile(unfilt_image)==False):
|
||||
print(f"{unfilt_image} is not found")
|
||||
cwd = os.getcwd()
|
||||
print(f"Current directory: {cwd}")
|
||||
sys.exit()
|
||||
|
||||
if(check):
|
||||
d = pyds9.DS9()
|
||||
d.set("file "+unfilt_image)
|
||||
d.set('cmap bb')
|
||||
d.set('scale log')
|
||||
#return
|
||||
|
||||
(detx,dety)=get_det_coords(arches_ra,arches_dec,f'{key}-fovimt.fits',key)
|
||||
|
||||
#(detx,dety)=get_det_coords(arches_ra,arches_dec,f'{key}-fovimt.fits',key)
|
||||
(detx,dety)=get_det_coords(arches_ra,arches_dec,unfilt_image,key)
|
||||
|
||||
arches_Rmax_pix=int(np.round(arches_Rmax_as/det_pix_as))
|
||||
region_fn=f'{key}.txt'
|
||||
remove_file(region_fn)
|
||||
@@ -188,24 +235,15 @@ def run_mosspectra(key,ccds,check=True,elow="350",ehigh="1100"):
|
||||
freg.write(f'&&{expression}\n')
|
||||
freg.close()
|
||||
|
||||
# check selected region
|
||||
if(check):
|
||||
unfilt_image = eventfile.replace("allevc","region")
|
||||
inargs = [f'table={eventfile}',
|
||||
'withimageset=yes', f'imageset={unfilt_image}',
|
||||
'filtertype=expression', f'expression={expression}',
|
||||
'ignorelegallimits=yes', 'imagebinning=imageSize',
|
||||
'xcolumn=DETX','ycolumn=DETY','ximagesize=780','yimagesize=780',
|
||||
'ximagemax=19500','yimagemax=19500', 'ximagemin=-19499', 'yimagemin=-19499',]
|
||||
w('evselect', inargs).run()
|
||||
|
||||
#d = pyds9.DS9()
|
||||
#d.set("file "+unfilt_image)
|
||||
#d.set('cmap bb')
|
||||
#d.set('scale log')
|
||||
#return
|
||||
|
||||
|
||||
unfilt_image = eventfile.replace("allevc","region")
|
||||
inargs = [f'table={eventfile}',
|
||||
'withimageset=yes', f'imageset={unfilt_image}',
|
||||
'filtertype=expression', f'expression={expression}',
|
||||
'ignorelegallimits=yes', 'imagebinning=imageSize',
|
||||
'xcolumn=DETX','ycolumn=DETY','ximagesize=780','yimagesize=780',
|
||||
'ximagemax=19500','yimagemax=19500', 'ximagemin=-19499', 'yimagemin=-19499',]
|
||||
w('evselect', inargs).run()
|
||||
|
||||
inargs = [f'eventfile={eventfile}',
|
||||
'withsrcrem=yes',
|
||||
f'cornerfile={key}-corevc.fits',
|
||||
@@ -221,6 +259,11 @@ def run_mosspectra(key,ccds,check=True,elow="350",ehigh="1100"):
|
||||
f'ccds="{ccds}"',]
|
||||
|
||||
w('mosspectra', inargs).run()
|
||||
|
||||
# check coordinate conversion
|
||||
#(detx,dety)=get_det_coords(arches_ra,arches_dec,f'{key}-fovimt.fits',key) # image input in X,Y
|
||||
#(detx,dety)=get_det_coords(arches_ra,arches_dec,unfilt_image,key) # image input in DETX,DETY
|
||||
#sys.exit()
|
||||
|
||||
|
||||
def run_bkgimsky(key,elow="350",ehigh="1100"):
|
||||
@@ -233,10 +276,32 @@ def run_bkgimsky(key,elow="350",ehigh="1100"):
|
||||
'withdetxy=false', 'withskyxy=false',]
|
||||
w('rotdet2sky', inargs).run()
|
||||
|
||||
def run_pnspectra(key,quads,check=True,elow="350",ehigh="1100"):
|
||||
def run_pnspectra(key,quads,check=True,elow="350",ehigh="1100",pattern=0):
|
||||
eventfile = f'{key}-allevc.fits'
|
||||
if(os.path.isfile(eventfile)==False):
|
||||
print(f"{eventfile} is not found, run 01_init_events*.py and above")
|
||||
sys.exit()
|
||||
|
||||
(detx,dety)=get_det_coords(arches_ra,arches_dec,f'{key}-fovimt.fits',key)
|
||||
# write image file with full fov
|
||||
unfilt_image = eventfile.replace("allevc","ffov")
|
||||
inargs = [f'table={eventfile}',
|
||||
'withimageset=yes', f'imageset={unfilt_image}',
|
||||
#'filtertype=expression', f'expression={expression}',
|
||||
'ignorelegallimits=yes', 'imagebinning=imageSize',
|
||||
'xcolumn=DETX','ycolumn=DETY','ximagesize=780','yimagesize=780',
|
||||
'ximagemax=19500','yimagemax=19500', 'ximagemin=-19499', 'yimagemin=-19499',]
|
||||
w('evselect', inargs).run()
|
||||
|
||||
if(check):
|
||||
d = pyds9.DS9()
|
||||
d.set("file "+unfilt_image)
|
||||
d.set('cmap bb')
|
||||
d.set('scale log')
|
||||
#return
|
||||
|
||||
#(detx,dety)=get_det_coords(arches_ra,arches_dec,f'{key}-fovimt.fits',key)
|
||||
(detx,dety)=get_det_coords(arches_ra,arches_dec,unfilt_image,key)
|
||||
|
||||
|
||||
arches_Rmax_pix=int(np.round(arches_Rmax_as/det_pix_as))
|
||||
region_fn=f'{key}.txt'
|
||||
@@ -245,23 +310,15 @@ def run_pnspectra(key,quads,check=True,elow="350",ehigh="1100"):
|
||||
expression=f"((DETX,DETY) IN circle({detx},{dety},{arches_Rmax_pix}))"
|
||||
freg.write(f'&&{expression}\n')
|
||||
freg.close()
|
||||
|
||||
# check selected region
|
||||
if(check):
|
||||
unfilt_image = eventfile.replace("allevc","region")
|
||||
inargs = [f'table={eventfile}',
|
||||
'withimageset=yes', f'imageset={unfilt_image}',
|
||||
'filtertype=expression', f'expression={expression}',
|
||||
'ignorelegallimits=yes', 'imagebinning=imageSize',
|
||||
'xcolumn=DETX','ycolumn=DETY','ximagesize=780','yimagesize=780',
|
||||
'ximagemax=19500','yimagemax=19500', 'ximagemin=-19499', 'yimagemin=-19499',]
|
||||
w('evselect', inargs).run()
|
||||
|
||||
#d = pyds9.DS9()
|
||||
#d.set("file "+unfilt_image)
|
||||
#d.set('cmap bb')
|
||||
#d.set('scale log')
|
||||
#return
|
||||
|
||||
unfilt_image = eventfile.replace("allevc","region")
|
||||
inargs = [f'table={eventfile}',
|
||||
'withimageset=yes', f'imageset={unfilt_image}',
|
||||
'filtertype=expression', f'expression={expression}',
|
||||
'ignorelegallimits=yes', 'imagebinning=imageSize',
|
||||
'xcolumn=DETX','ycolumn=DETY','ximagesize=780','yimagesize=780',
|
||||
'ximagemax=19500','yimagemax=19500', 'ximagemin=-19499', 'yimagemin=-19499',]
|
||||
w('evselect', inargs).run()
|
||||
|
||||
inargs = [f'eventfile={key}-allevc.fits',
|
||||
'withsrcrem=yes',
|
||||
@@ -274,7 +331,7 @@ def run_pnspectra(key,quads,check=True,elow="350",ehigh="1100"):
|
||||
'withregion=yes',
|
||||
f'regionfile={region_fn}',
|
||||
'keepinterfiles=yes',
|
||||
'pattern=0',
|
||||
f'pattern={pattern}',
|
||||
f'elow={elow}',
|
||||
f'ehigh={ehigh}',
|
||||
f'quads="{quads}"',]
|
||||
@@ -299,18 +356,77 @@ def run_pnback(key,quads):
|
||||
|
||||
w('pnback', inargs).run()
|
||||
|
||||
def group_spectrum(key=None, group_min=25, oot=False):
|
||||
|
||||
|
||||
def run_mossave(key,elow="350",ehigh="1100"):
|
||||
dd=f'ffov_{key}'
|
||||
create_folder(dd)
|
||||
move_files(f'{key}*-{elow}-{ehigh}*',dd)
|
||||
move_files(f'{key}*-{elow}-{ehigh}*',dd)
|
||||
move_files(f'{key}*.pi',dd)
|
||||
move_files(f'{key}*.qdp',dd)
|
||||
move_files(f'{key}*.ps',dd)
|
||||
move_files(f'{key}*imt*',dd)
|
||||
move_files(f'{key}*.arf',dd)
|
||||
move_files(f'{key}*.rmf',dd)
|
||||
move_files(f'{key}*imspdet*',dd)
|
||||
move_files(f'{key}*bkgimsky*',dd)
|
||||
|
||||
|
||||
def run_pnsave(key,elow="350",ehigh="1100",pattern=0):
|
||||
dd=f'ffov_{key}_{pattern}'
|
||||
create_folder(dd)
|
||||
move_files(f'pn*-{elow}-{ehigh}*',dd)
|
||||
move_files(f'pn*-{elow}-{ehigh}*',dd)
|
||||
move_files(f'pn*.pi',dd)
|
||||
move_files(f'pn*.qdp',dd)
|
||||
move_files(f'pn*.ps',dd)
|
||||
move_files(f'pn*imt*',dd)
|
||||
move_files(f'pn*.arf',dd)
|
||||
move_files(f'pn*.rmf',dd)
|
||||
move_files(f'pn*imspdet*',dd)
|
||||
move_files(f'pn*bkgimsky*',dd)
|
||||
|
||||
|
||||
def group_spectrum(key=None, group_min=25, oot=False, chdir=None, cpdir=None, pattern=None):
|
||||
if not (key):
|
||||
print("Provide key= parameter, e.g. mos1S001")
|
||||
return
|
||||
|
||||
|
||||
prefix=''
|
||||
try:
|
||||
if(chdir):
|
||||
prefix='../'
|
||||
os.chdir(chdir)
|
||||
except:
|
||||
return
|
||||
|
||||
test_exe('grppha')
|
||||
specfile=f'{key}-fovt.pi' if not (oot) else f'{key}-fovtootsub.pi'
|
||||
backfile=f'{key}-bkg.pi'
|
||||
respfile=f'{key}.rmf'
|
||||
ancrfile=f'{key}.arf'
|
||||
fout=f'{specfile}.grp'
|
||||
|
||||
|
||||
if(pattern!=None):
|
||||
specfile0=specfile
|
||||
backfile0=backfile
|
||||
respfile0=respfile
|
||||
ancrfile0=ancrfile
|
||||
fout0=fout
|
||||
|
||||
specfile=f'{key}_{pattern}-fovt.pi' if not (oot) else f'{key}_{pattern}-fovtootsub.pi'
|
||||
backfile=f'{key}_{pattern}-bkg.pi'
|
||||
respfile=f'{key}_{pattern}.rmf'
|
||||
ancrfile=f'{key}_{pattern}.arf'
|
||||
fout=f'{specfile}.grp'
|
||||
|
||||
shutil.copyfile(specfile0, specfile)
|
||||
shutil.copyfile(backfile0, backfile)
|
||||
shutil.copyfile(respfile0, respfile)
|
||||
shutil.copyfile(ancrfile0, ancrfile)
|
||||
shutil.copyfile(fout0, fout)
|
||||
|
||||
cmd=["grppha",
|
||||
"infile={}".format(specfile),
|
||||
"outfile={}".format(fout),
|
||||
@@ -321,14 +437,107 @@ def group_spectrum(key=None, group_min=25, oot=False):
|
||||
]
|
||||
print((" ").join(cmd))
|
||||
os.system((" ").join(cmd))
|
||||
|
||||
fxcm = open(fout.replace("grp","xcm"), "w")
|
||||
|
||||
with fits.open(specfile) as hdul:
|
||||
header = hdul['SPECTRUM'].header
|
||||
backscal = header['BACKSCAL']
|
||||
area_arcmin2 = float(backscal)*(det_pix_as/60)**2
|
||||
print(f"Value of BACKSCAL: {backscal} area: {area_arcmin2:.4f} arcmin2")
|
||||
|
||||
fout_xcm=fout.replace("grp","xcm")
|
||||
fxcm = open(fout_xcm, "w")
|
||||
fxcm.write(f'# backscal {backscal} pix2\n')
|
||||
fxcm.write(f'# area {area_arcmin2:.8f} arcmin2\n')
|
||||
fxcm.write(f'data {fout}\n')
|
||||
fxcm.write('ign **-2.\n')
|
||||
fxcm.write('ign 11.-**\n')
|
||||
fxcm.write('ign bad\n')
|
||||
fxcm.write('cpd /xs\n')
|
||||
fxcm.write('setpl en\n')
|
||||
fxcm.write('@../../../data/xspec/wabs_apec_cflux.xcm\n')
|
||||
fxcm.write(f'@{prefix}../../../data/xspec/wabs_apec_cflux.xcm\n')
|
||||
fxcm.write('pl lda delchi\n')
|
||||
fxcm.close()
|
||||
|
||||
if(cpdir):
|
||||
shutil.copyfile(specfile, os.path.join('../',cpdir,specfile))
|
||||
shutil.copyfile(backfile, os.path.join('../',cpdir,backfile))
|
||||
shutil.copyfile(respfile, os.path.join('../',cpdir,respfile))
|
||||
shutil.copyfile(ancrfile, os.path.join('../',cpdir,ancrfile))
|
||||
shutil.copyfile(fout, os.path.join('../',cpdir,fout))
|
||||
shutil.copyfile(fout_xcm, os.path.join('../',cpdir,fout_xcm))
|
||||
|
||||
if(chdir):
|
||||
os.chdir('../')
|
||||
|
||||
|
||||
|
||||
def get_ds9_regions(image, src_fn, bkg_fn):
|
||||
# Visualize the image with ds9
|
||||
d = pyds9.DS9()
|
||||
d.set("file "+image)
|
||||
d.set('cmap bb')
|
||||
d.set('scale log')
|
||||
#d.set(f"region {ds9reg_dir}/arches.reg")
|
||||
|
||||
if(os.path.isfile(src_fn)==True):
|
||||
d.set(f"region {src_fn}")
|
||||
else:
|
||||
print(f'src_fn: {src_fn} is not found')
|
||||
sys.exit()
|
||||
|
||||
if(os.path.isfile(bkg_fn)==True):
|
||||
d.set(f"region {bkg_fn}")
|
||||
else:
|
||||
print(f'bkg_fn: {bkg_fn} is not found')
|
||||
sys.exit()
|
||||
|
||||
#reply = input(f"{obsid}: Proceed to make spectrum/lightcurve? [y/[n]] ")
|
||||
#if reply!='y':
|
||||
# continue
|
||||
|
||||
print(d.get("regions"))
|
||||
|
||||
# Extract the relevant information from the ds9 regions.
|
||||
|
||||
region1=(re.split("circle|annulus",d.get("regions").partition("physical")[2]))[1].replace('(','').replace(')','').replace('#',',')
|
||||
region2=(re.split("circle|annulus",d.get("regions").partition("physical")[2]))[2].replace('(','').replace(')','').replace('#',',')
|
||||
print("Identified first region: ", region1)
|
||||
print("Identified second region: ", region2)
|
||||
|
||||
#print("region1 "+region1.partition("color")[2].replace('=','').replace('\n',''))
|
||||
#print("region2 "+region2.partition("color")[2].replace('=','').replace('\n',''))
|
||||
|
||||
# Identify source and background regions using the 'white' color.
|
||||
c1=region1.partition("color")[2].replace('=','').replace('\n','')
|
||||
print("Region1 color: "+c1)
|
||||
if(c1=='white'):
|
||||
regionsrc = region1
|
||||
regionbkg = region2
|
||||
else:
|
||||
regionsrc = region2
|
||||
regionbkg = region1
|
||||
|
||||
# Save and print selected region coordinates.
|
||||
x_source = regionsrc.split(",")[0].replace('\n','')
|
||||
|
||||
y_source = regionsrc.split(",")[1].replace('\n','')
|
||||
r_source = regionsrc.split(",")[2].replace('\n','')
|
||||
print("The coordinates of the selected source region are: \n")
|
||||
print(" x_source = ", x_source, "(physical)")
|
||||
print(" y_source = ", y_source, "(physical)")
|
||||
print(" r_source = ", r_source, "(physical) \n")
|
||||
|
||||
x_bkg = regionbkg.split(",")[0].replace('\n','')
|
||||
y_bkg = regionbkg.split(",")[1].replace('\n','')
|
||||
r_bkg = regionbkg.split(",")[2].replace('\n','')
|
||||
print("The coordinates of the selected background region are: \n")
|
||||
print(" x_bkg = ", x_bkg, "(physical)")
|
||||
print(" y_bkg = ", y_bkg, "(physical)")
|
||||
print(" r_bkg = ", r_bkg, "(physical) \n")
|
||||
|
||||
# If the background is an annulus, save and print R2.
|
||||
r2_bkg=None
|
||||
if "annulus" in str(d.get("regions")):
|
||||
r2_bkg = regionbkg.split(",")[3].replace('\n','')
|
||||
print(" r2_bkg = ", r2_bkg, "(physical, annulus)")
|
||||
return x_source,y_source,r_source,x_bkg,y_bkg,r_bkg,r2_bkg
|
||||
|
||||
Reference in New Issue
Block a user