import matplotlib.patches as patches from scipy.spatial import ConvexHull from astropy.io import fits,ascii import astropy import gc import numpy as np import os import time import copy import gzip import sys import pickle import shutil import statistics as stats import glob from multiprocessing import Pool from contextlib import closing from matplotlib import path from scipy.interpolate import interp1d from astropy import units as u import astropy.coordinates as ac #from shapely.geometry import Point, Polygon np.set_printoptions(threshold=sys.maxsize) from astropy.table import Table, vstack, hstack,Column, join ''' The following functions are used to checks which objects fall inside each obsid by going through the directory_address.txt database, and matching with source positions. They are useful in the create_obsid_pointing_file step. ''' def convexhull(p): ''' ordered vertices of a polygon ''' p = np.array(p) hull = ConvexHull(p) return p[hull.vertices,:] def chandra_acis_i_fov(ra_ctr,dec_ctr,rot): ''' convert convexxhull vertices to polygon by using pointing center of observations ''' box_sz=0.142 # degrees csrt=np.cos(rot/RADEG) snrt=np.sin(rot/RADEG) ra1 = (box_sz*(csrt - snrt) / np.cos(dec_ctr/RADEG)) + ra_ctr ra2 = (-box_sz*(csrt - snrt) / np.cos(dec_ctr/RADEG)) + ra_ctr ra3 = (box_sz*(csrt + snrt) / np.cos(dec_ctr/RADEG)) + ra_ctr ra4 = (box_sz*(-csrt - snrt) / np.cos(dec_ctr/RADEG)) + ra_ctr dec1 = ((box_sz*snrt) + (box_sz*csrt))+dec_ctr dec2 = ((-box_sz*snrt) + (-box_sz*csrt))+dec_ctr dec3 = ((box_sz*snrt) + (-box_sz*csrt))+dec_ctr dec4 = ((-box_sz*snrt) + (box_sz*csrt))+dec_ctr #ra_out=np.array([ra1,ra2,ra3,ra4]) #dec_out=np.array([dec1,dec2,dec3,dec4]) coords=convexhull([(ra1,dec1),(ra2,dec2),(ra3,dec3),(ra4,dec4)]) #print("coords",coords) return coords def chandra_acis_i_fov_coord_output_by_obs_num(obs_num): ''' anticlockwise rotation around rotated center (to accommodate Chandra chip offset of 0.056 degrees) ''' innd=np.where(DIRECTORY_ADDRESS["obsid"].data==obs_num)[0] ra_ctr,dec_ctr,rot=DIRECTORY_ADDRESS["rapnt"].data[innd][0],DIRECTORY_ADDRESS["decpnt"].data[innd][0],\ DIRECTORY_ADDRESS["roll"].data[innd][0] box_sz=0.142 # degrees #ra_ctr,dec_ctr=rotate_center(ra_ctr,dec_ctr,rot) csrt=np.cos(rot/RADEG) snrt=np.sin(rot/RADEG) ra1 = (box_sz*(csrt - snrt) / np.cos(dec_ctr/RADEG)) + ra_ctr ra2 = (-box_sz*(csrt - snrt) / np.cos(dec_ctr/RADEG)) + ra_ctr ra3 = (box_sz*(csrt + snrt) / np.cos(dec_ctr/RADEG)) + ra_ctr ra4 = (box_sz*(-csrt - snrt) / np.cos(dec_ctr/RADEG)) + ra_ctr dec1 = ((box_sz*snrt) + (box_sz*csrt))+dec_ctr dec2 = ((-box_sz*snrt) + (-box_sz*csrt))+dec_ctr dec3 = ((box_sz*snrt) + (-box_sz*csrt))+dec_ctr dec4 = ((-box_sz*snrt) + (box_sz*csrt))+dec_ctr #ra_out=np.array([ra1,ra2,ra3,ra4]) #dec_out=np.array([dec1,dec2,dec3,dec4]) coords=convexhull([(ra1,dec1),(ra2,dec2),(ra3,dec3),(ra4,dec4)]) #print("coords",coords) return coords def which_points_are_inside_an_obs(ssrc_id=0,ssrc_ra=0,ssrc_dec=0): ''' find which objects fall within an observation using simple polygons ''' all_points=np.array([ssrc_ra,ssrc_dec]).T for row in DIRECTORY_ADDRESS: ra_ctr,dec_ctr,rot,obsid=row['rapnt'],row['decpnt'],row["roll"],row['obsid'] coords=chandra_acis_i_fov(ra_ctr,dec_ctr,rot) p=path.Path(coords) is_point_in_poly=np.where(p.contains_points(all_points))[0] for mem in is_point_in_poly: print(obsid,ssrc_id[mem],ssrc_ra[mem],ssrc_dec[mem]) return def plot_polygon_all_obs(fntsz,prnt_src=False,ssrc_ra=0,ssrc_dec=0): ''' not in use - was used for checks to see how sources and observations overlap definitely not recommended if there are too many distant sources and observations as matplotlib won't be able to find the space to plot it ''' fig, ax = plt.subplots() for row in DIRECTORY_ADDRESS: ra_ctr,dec_ctr,rot,obsid=row['rapnt'],row['decpnt'],row["roll"],row['obsid'] coords=chandra_acis_i_fov(ra_ctr,dec_ctr,rot)#chandra_acis_i_fov_coord_output_by_obs_num(obs_num) poly = plt.Polygon(convexhull(coords), ec="k",fc="None")#,alpha=0.3) ax.add_patch(poly) ax.text(ra_ctr,dec_ctr,str(obsid),fontsize=fntsz) if prnt_src: ax.scatter(ssrc_ra,ssrc_dec,color="green",s=1) print("Can set xlim and ylim on the returned axis by setting ax= output and then ax.set_xlim(xmin, xmax) etc") return ax def plot_polygon(obs_num): ''' diagnostic plots to see source-observation overlap given an observation number ''' coords=chandra_acis_i_fov_coord_output_by_obs_num(obs_num) fig, ax = plt.subplots() poly = plt.Polygon(convexhull(coords), ec="k",fc="None") ax.add_patch(poly) print("Can set xlim and ylim on the returned axis by setting ax= output and then ax.set_xlim(xmin, xmax) etc") return ax def rotate_center(ra_ctr,dec_ctr,rot): ''' center needs to go through anticlockwise (and subtracted from original) rotation of about 0.056 degrees this routine is no longer needed as we found the correct ra, dec position using pointing_ra_dec_file_fixer() ''' xp,yp=-0.005,0.056#0.008,0.055 csrt=np.cos(rot/RADEG) snrt=np.sin(rot/RADEG) return -(csrt*xp-snrt*yp)+ ra_ctr, -(snrt*xp+csrt*yp) +dec_ctr def chandra_acis_i_fov_reg_file(ra_ctr,dec_ctr,rot): ''' anticlockwise rotation around rotated center (to accommodate offset of 0.056 degrees) ''' box_sz=0.142 # degrees #ra_ctr,dec_ctr=rotate_center(ra_ctr,dec_ctr,rot) csrt=np.cos(rot/RADEG) snrt=np.sin(rot/RADEG) ra1 = (box_sz*(csrt - snrt) / np.cos(dec_ctr/RADEG)) + ra_ctr ra2 = (-box_sz*(csrt - snrt) / np.cos(dec_ctr/RADEG)) + ra_ctr ra3 = (box_sz*(csrt + snrt) / np.cos(dec_ctr/RADEG)) + ra_ctr ra4 = (box_sz*(-csrt - snrt) / np.cos(dec_ctr/RADEG)) + ra_ctr dec1 = ((box_sz*snrt) + (box_sz*csrt))+dec_ctr dec2 = ((-box_sz*snrt) + (-box_sz*csrt))+dec_ctr dec3 = ((box_sz*snrt) + (-box_sz*csrt))+dec_ctr dec4 = ((-box_sz*snrt) + (box_sz*csrt))+dec_ctr ra_out=np.array([ra_ctr,ra1,ra2,ra3,ra4]) dec_out=np.array([dec_ctr,dec1,dec2,dec3,dec4]) with open('reg_file.reg', 'w') as file: file.write('global color=orange dashlist=8 3 width=1 font="helvetica 10 normal roman" select=1 highlite=1 dash=0 fixed=0 edit=1 move=1 delete=1 include=1 source=1\n') file.write("fk5\n") for rra,ddec in zip(ra_out,dec_out): file.write('circle('+str(rra)+','+str(ddec)+',18")\n') #print("coords",coords) return def make_all_region_files(): ''' make DS9 region files for all input sources ''' with open(results_dir+'/all_sources.reg', 'w') as file: file.write('global color=cyan dashlist=8 3 width=1 font="helvetica 10 normal roman" select=1 highlite=1 dash=0 fixed=0 edit=1 move=1 delete=1 include=1 source=1\n') file.write("fk5\n") for rra,ddec,srcidd in zip(sources_ra,sources_dec,sources_id): file.write('circle('+str(rra)+','+str(ddec)+',18") # text={'+str(srcidd)+'}\n') #print("coords",coords) return def chandra_acis_i_fov_reg_file_obs_num(obs_num): ''' anticlockwise rotation around rotated center (to accommodate offset of 0.056 degrees) ''' innd=np.where(DIRECTORY_ADDRESS["obsid"].data==obs_num)[0] ra_ctr,dec_ctr,rot=DIRECTORY_ADDRESS["rapnt"].data[innd][0],DIRECTORY_ADDRESS["decpnt"].data[innd][0],DIRECTORY_ADDRESS["roll"].data[innd][0] box_sz=0.142 # degrees #ra_ctr,dec_ctr=rotate_center(ra_ctr,dec_ctr,rot) csrt=np.cos(rot/RADEG) snrt=np.sin(rot/RADEG) ra1 = (box_sz*(csrt - snrt) / np.cos(dec_ctr/RADEG)) + ra_ctr ra2 = (-box_sz*(csrt - snrt) / np.cos(dec_ctr/RADEG)) + ra_ctr ra3 = (box_sz*(csrt + snrt) / np.cos(dec_ctr/RADEG)) + ra_ctr ra4 = (box_sz*(-csrt - snrt) / np.cos(dec_ctr/RADEG)) + ra_ctr dec1 = ((box_sz*snrt) + (box_sz*csrt))+dec_ctr dec2 = ((-box_sz*snrt) + (-box_sz*csrt))+dec_ctr dec3 = ((box_sz*snrt) + (-box_sz*csrt))+dec_ctr dec4 = ((-box_sz*snrt) + (box_sz*csrt))+dec_ctr ra_out=np.array([ra_ctr,ra1,ra2,ra3,ra4]) dec_out=np.array([dec_ctr,dec1,dec2,dec3,dec4]) with open('reg_file_'+str(obs_num)+'.reg', 'w') as file: file.write('global color=red dashlist=8 3 width=1 font="helvetica 10 normal roman" select=1 highlite=1 dash=0 fixed=0 edit=1 move=1 delete=1 include=1 source=1\n') file.write("fk5\n") for rra,ddec in zip(ra_out,dec_out): file.write('circle('+str(rra)+','+str(ddec)+',18")\n') #print("coords",coords) return