forked from erosita/uds
evening job
This commit is contained in:
146
uds/uds/utils.py
146
uds/uds/utils.py
@@ -17,11 +17,27 @@ from astropy.coordinates import SkyCoord # High-level coordinates
|
||||
from astropy.coordinates import ICRS, Galactic, FK4, FK5 # Low-level frames
|
||||
from astropy.coordinates import Angle, Latitude, Longitude # Angles
|
||||
|
||||
from astropy.time import Time, TimeDelta, TimezoneInfo, TimeFromEpoch
|
||||
|
||||
import statistics
|
||||
import shutil
|
||||
|
||||
MJDREF = 51543.875
|
||||
TZ_UTC = TimezoneInfo(utc_offset=0*u.hour)
|
||||
TZ_MSK = TimezoneInfo(utc_offset=3*u.hour)
|
||||
TIMESTR_FORMAT = '%Y.%m.%d %H:%M:%S'
|
||||
|
||||
def mission2date_utc(timesec):
|
||||
mjdref = Time(MJDREF, format='mjd')
|
||||
delta = TimeDelta(timesec, format='sec')
|
||||
dtime = mjdref + delta
|
||||
return dtime.to_datetime(timezone=TZ_UTC)
|
||||
|
||||
def mission2date_msk(timesec):
|
||||
mjdref = Time(MJDREF, format='mjd')
|
||||
delta = TimeDelta(timesec, format='sec')
|
||||
dtime = mjdref + delta + 3*u.hour
|
||||
return dtime.to_datetime(timezone=TZ_MSK)
|
||||
|
||||
def create_folder(folder):
|
||||
if not (os.path.exists(folder)):
|
||||
@@ -31,7 +47,7 @@ def remove_file(filename):
|
||||
if(os.path.isfile(filename)==True):
|
||||
os.remove(filename)
|
||||
|
||||
def do_evtool_esass(events=None,outfile=None,evlist=None,gti=None,region=None,emin=None,emax=None):
|
||||
def do_evtool_esass(events=None,outfile=None,evlist=None,gti=None,region=None,emin=None,emax=None, rmlock=False):
|
||||
|
||||
eventfiles=None
|
||||
if(events):
|
||||
@@ -60,6 +76,14 @@ def do_evtool_esass(events=None,outfile=None,evlist=None,gti=None,region=None,em
|
||||
test_exe('evtool')
|
||||
os.system((" ").join(cmd))
|
||||
|
||||
if(rmlock==True):
|
||||
lockfiles=outfile.replace(".fits", "*.lock")
|
||||
for filename in glob.glob(lockfiles):
|
||||
print("Removing {}".format(filename))
|
||||
if(os.path.isfile(filename)==True):
|
||||
os.remove(filename)
|
||||
pass
|
||||
|
||||
def set_bit(value, bit):
|
||||
return value | (1<<bit)
|
||||
|
||||
@@ -122,7 +146,7 @@ def do_badpix_tm6(filename):
|
||||
f.close()
|
||||
|
||||
def init_events(key=None, eband_selected=[0], eband_index=None,
|
||||
ra_cen=None, de_cen=None,do_init=True,
|
||||
ra_cen=None, de_cen=None,do_init=True,vign=True,
|
||||
emin_kev=None, emax_kev=None, infile_dir=None, outfile_dir=None,
|
||||
do_obsmode=False,do_center=False,do_evtool=False,do_expmap=False):
|
||||
expmaps=[]
|
||||
@@ -216,8 +240,13 @@ def init_events(key=None, eband_selected=[0], eband_index=None,
|
||||
h.header['OBS_MODE'] = changeto
|
||||
hdu.writeto(outfile_evtool,overwrite=True)
|
||||
|
||||
if(vign==True):
|
||||
withvignetting='yes'
|
||||
outfile_expmap="{}_ExposureMap_en{}.vign{}".format(os.path.join(outfile_dir,key), eband_index, outfile_post)
|
||||
else:
|
||||
withvignetting='no'
|
||||
outfile_expmap="{}_ExposureMap_en{}.novign{}".format(os.path.join(outfile_dir,key), eband_index, outfile_post)
|
||||
|
||||
outfile_expmap="{}_ExposureMap_en{}{}".format(os.path.join(outfile_dir,key), eband_index, outfile_post)
|
||||
if(do_expmap==True and do_init==True):
|
||||
cmd=["expmap",
|
||||
"inputdatasets=%s" %(outfile_evtool),
|
||||
@@ -225,7 +254,7 @@ def init_events(key=None, eband_selected=[0], eband_index=None,
|
||||
"emax=%s" %(emax_kev),
|
||||
"templateimage=%s" %(outfile_evtool),
|
||||
"singlemaps=%s" %(outfile_expmap),
|
||||
"withvignetting=yes",
|
||||
"withvignetting={}".format(withvignetting),
|
||||
"withsinglemaps=yes","withfilebadpix=yes",
|
||||
"withmergedmaps=no",
|
||||
]
|
||||
@@ -235,21 +264,37 @@ def init_events(key=None, eband_selected=[0], eband_index=None,
|
||||
|
||||
return outfile_evtool,outfile_expmap
|
||||
|
||||
def merge_expmaps_esass(expmaps,outfile):
|
||||
def do_expmap_esass(expmaps=None,outfile=None,emin_kev=None,emax_kev=None,refimage=None):
|
||||
cmd=["expmap",
|
||||
"inputdatasets={}".format(outfile_evtool),
|
||||
"inputdatasets=\'{}\'".format((" ").join(expmaps)),
|
||||
"emin=%s" %(emin_kev),
|
||||
"emax=%s" %(emax_kev),
|
||||
"templateimage=%s" %(outfile_evtool),
|
||||
"singlemaps=%s" %(outfile_expmap),
|
||||
"templateimage=%s" %(refimage),
|
||||
"singlemaps=%s" %(outfile),
|
||||
"withvignetting=yes",
|
||||
"withsinglemaps=yes","withfilebadpix=yes",
|
||||
"withmergedmaps=no",
|
||||
"withsinglemaps=no","withfilebadpix=yes",
|
||||
"withmergedmaps=yes",
|
||||
]
|
||||
print((" ").join(cmd))
|
||||
test_exe('expmap')
|
||||
os.system((" ").join(cmd))
|
||||
|
||||
def do_expmap_ftools(expmaps=None,outfile=None):
|
||||
""" takes first exposure as reference and merges the rest """
|
||||
tmp="expmaps.txt"
|
||||
f = open(tmp, "w")
|
||||
for jj in range(1,len(expmaps)):
|
||||
f.write("{},0,0\n".format(expmaps[jj]))
|
||||
f.close()
|
||||
cmd=["fimgmerge",
|
||||
"infile={}".format(expmaps[0]),
|
||||
"list=@{}".format(tmp),
|
||||
"outfile={}".format(outfile),
|
||||
"clobber=yes",
|
||||
]
|
||||
print((" ").join(cmd))
|
||||
os.system((" ").join(cmd))
|
||||
|
||||
def test_exe(program):
|
||||
""" Tests if executable exists in PATH """
|
||||
import os
|
||||
@@ -326,7 +371,6 @@ def crossmatch_shu2019(filename,refimage=None,scale=1200,crval=None,devmax=30,dl
|
||||
w.wcs.crval=crval
|
||||
naxis1=hdulist[0].header['NAXIS1']
|
||||
naxis2=hdulist[0].header['NAXIS2']
|
||||
date_obs=hdulist[0].header['DATE-OBS']
|
||||
|
||||
if not catalog:
|
||||
print("ERROR: Please provide reference catalog: catalog=")
|
||||
@@ -428,14 +472,17 @@ def crossmatch_shu2019(filename,refimage=None,scale=1200,crval=None,devmax=30,dl
|
||||
print("delta [arcsec] RA, Dec, sep {:.4f} {:.4f}".format((rec['src_ra']-rec['ref_ra'])*3600,
|
||||
(rec['src_dec']-rec['ref_dec'])*3600,sep))
|
||||
|
||||
tstart, tstop = read_tstart_tstop(infile=refimage)
|
||||
date_obs = mission2date_utc(tstart)
|
||||
date_end = mission2date_utc(tstop)
|
||||
stat_fout=fout.replace(".cross.fits", ".dat")
|
||||
with open(stat_fout, 'w') as writer:
|
||||
writer.write("[DATE-OBS {}] nref, mean [arcsec] dRA, dDec, mean/median sep (mean/median): {} & {:.4f} & {:.4f} & {:.4f}/{:.4f} \\\\ \n".format(date_obs,
|
||||
len(delta_ra),
|
||||
statistics.mean(delta_ra)*3600,
|
||||
statistics.mean(delta_dec)*3600,
|
||||
statistics.mean(delta_sep),
|
||||
statistics.median(delta_sep)))
|
||||
writer.write("[DATE-OBS {}, DATE-END {}] nref, mean [arcsec] dRA, dDec, mean/median sep (mean/median): {} & {:.4f} & {:.4f} & {:.4f}/{:.4f}\n".format(
|
||||
date_obs,date_end,len(delta_ra),
|
||||
statistics.mean(delta_ra)*3600,
|
||||
statistics.mean(delta_dec)*3600,
|
||||
statistics.mean(delta_sep),
|
||||
statistics.median(delta_sep)))
|
||||
|
||||
src_tab.write(src_fout, format='fits', overwrite=True)
|
||||
with fits.open(src_fout) as f:
|
||||
@@ -456,31 +503,55 @@ def crossmatch_shu2019(filename,refimage=None,scale=1200,crval=None,devmax=30,dl
|
||||
#f[1].header=f[1].header+w.to_header()
|
||||
|
||||
|
||||
def do_adapt_ciao(infile=None,outfile=None):
|
||||
if not infile:
|
||||
print("ERROR: Please provide input file: infile=")
|
||||
return
|
||||
|
||||
if not outfile:
|
||||
print("ERROR: file for output? outfile=")
|
||||
def do_adapt_ciao(infile=None,outfile=None,expmap=None,function='tophat',expcut=None):
|
||||
if not (infile and expmap and outfile):
|
||||
print("ERROR: Please provide input and output files")
|
||||
return
|
||||
|
||||
radfile=infile.replace(".fits", ".adapt.scale.fits")
|
||||
sumfile=infile.replace(".fits", ".sum.scale.fits")
|
||||
test_exe('dmimgadapt')
|
||||
cmd=["dmimgadapt",
|
||||
"infile={}".format(infile),
|
||||
"outfile={}".format(outfile),
|
||||
"function=tophat",
|
||||
"function={}".format(function),
|
||||
"minrad=1",
|
||||
"maxrad=30",
|
||||
"numrad=30",
|
||||
"radscale=log",
|
||||
"counts=30",
|
||||
"radfile=map.scale.fits".format(infile.replace(".fits", ".adapt.scale.fits")),
|
||||
"sumfile={}".format(infile.replace(".fits", ".sum.scale.fits")),
|
||||
"radfile={}".format(radfile),
|
||||
"sumfile={}".format(sumfile),
|
||||
"clobber=yes",]
|
||||
remove_file(outfile)
|
||||
print((" ").join(cmd))
|
||||
os.system((" ").join(cmd))
|
||||
|
||||
cmd=["dmimgadapt",
|
||||
"infile={}".format(infile),
|
||||
"innormfile={}".format(expmap),
|
||||
"outfile={}".format(outfile),
|
||||
"function={}".format(function),
|
||||
"minrad=1",
|
||||
"maxrad=30",
|
||||
"numrad=30",
|
||||
"radscale=log",
|
||||
"counts=30",
|
||||
"inradfile={}".format(radfile),
|
||||
"sumfile={}".format(sumfile),
|
||||
"clobber=yes",]
|
||||
remove_file(outfile)
|
||||
print((" ").join(cmd))
|
||||
os.system((" ").join(cmd))
|
||||
|
||||
if (expcut):
|
||||
cntmap_data, cntmap_hdr = fits.getdata(outfile, ext=0, header=True)
|
||||
expmap_data, expmap_hdr = fits.getdata(expmap, ext=0, header=True)
|
||||
index = np.where(expmap_data > expcut)
|
||||
cntmap_data[index]=0.0
|
||||
fits.writeto(outfile, cntmap_data, cntmap_hdr, overwrite=True)
|
||||
|
||||
|
||||
"""
|
||||
dmimgadapt artmap_ait.fits adapt.fits tophat 1 30 30 log 30 radfile=map.scale.fits sumfile=sum.fits clobber=yes
|
||||
#dmimgadapt artmap_ait.fits adapt.fits func=gaussian min=1.0 max=30 num=30 rad=linear counts=30 sumfile=sum.fits normfile=norm.fits radfile=scale.fits
|
||||
@@ -492,13 +563,28 @@ def read_tstart_tstop(infile=None):
|
||||
if not infile:
|
||||
print("ERROR: Please provide input file: infile=")
|
||||
return
|
||||
print("Reading {}".format(infile))
|
||||
#print("Reading {}".format(infile))
|
||||
hdul = fits.open(infile)
|
||||
tbdata_ref = hdul[1].data
|
||||
tstart=tbdata_ref['TIME'][0]
|
||||
tstop=tbdata_ref['TIME'][-1]
|
||||
print("*** TSTART diff {:.1f} ks".format((tstart-hdul[1].header['TSTART'])/1000))
|
||||
print("*** TSTOP diff {:.1f} ks".format((hdul[1].header['TSTOP']-tstop)/1000))
|
||||
#print("*** TSTART diff {:.1f} ks".format((tstart-hdul[1].header['TSTART'])/1000))
|
||||
#print("*** TSTOP diff {:.1f} ks".format((hdul[1].header['TSTOP']-tstop)/1000))
|
||||
|
||||
return tstart, tstop
|
||||
|
||||
def make_rate_map(cntmap=None, expmap=None, outfile=None):
|
||||
|
||||
if not (cntmap and expmap and outfile):
|
||||
print("ERROR: Please provide counts, exposure ane output file")
|
||||
return
|
||||
|
||||
cntmap_data, cntmap_hdr = fits.getdata(cntmap, ext=0, header=True)
|
||||
expmap_data, expmap_hdr = fits.getdata(expmap, ext=0, header=True)
|
||||
rate = np.zeros(expmap_data.shape)
|
||||
|
||||
index = np.where(expmap_data > 0.0)
|
||||
rate[index]=cntmap_data[index]/expmap_data[index]
|
||||
fits.writeto(outfile, rate, overwrite=True)
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user