def _set_area(self, area): """ Returns the area covered by the catalogue and the corresponding MOC, if defined. Parameters ---------- area : ``str``, ``MOC`` or ``Quantity`` area can be defined as a path to the catalogue MOC, a mocpy ``MOC`` object or an Astropy ``Quantity`` with units consistents with square deg. """ # If area is a string, we assume is the path for a moc file if isinstance(area, str): moc = MOC.from_fits(area) area = moc.sky_fraction * ALLSKY_AREA_DEG elif isinstance(area, MOC): moc, area = area, area.sky_fraction * ALLSKY_AREA_DEG elif isinstance(area, Quantity): area = area.to(u.deg**2) moc = None else: raise ValueError('Invalid `area` value!') return area, moc
def test_set_fromtable_areafrommoc_mags(): from mocpy import MOC from astropy.table import Table mocfile = get_pkg_data_filename('data/testcat_moc_2.moc') moc = MOC.from_fits(mocfile) datafile = get_pkg_data_filename('data/testcat_moc_2.fits') data = Table.read(datafile) mag_cols = ['uMag', 'gMag'] cat = Catalogue(data, area=moc, name='test', coord_cols=['RA', 'DEC'], poserr_cols=['raErr', 'decErr'], poserr_type='rcd_dec_ellipse', mag_cols=mag_cols) assert cat.name == 'test' assert cat.area > 0 assert cat.poserr_type == 'rcd_dec_ellipse' assert isinstance(cat.coords, SkyCoord) assert isinstance(cat.poserr, SkyCoordErr) assert len(cat) == len(data) assert len(cat.coords) == len(data) assert len(cat.poserr) == len(data) assert len(cat.mags) == len(data) assert len(cat.mags.colnames) == len(mag_cols)
def read_area_cache_files(cache_dir): """ read_area_cache_files This function reads all the files in the cache directories and keeps them in memory in a list called "arealist". Each area is a dictionary: ar_id: the id from tha database moc : the ingestred moc Args: cache_dir: """ arealist = [] for ar_file in os.listdir(cache_dir): # every file in the cache should be of the form ar_<nn>.fits # where nn is the area id tok = ar_file.split('.') if tok[1] != 'fits': continue try: ar_id = int(tok[0][3:]) except: continue gfile = cache_dir + '/' + ar_file moc = MOC.from_fits(gfile) area = {'ar_id':ar_id, 'moc':moc} arealist.append(area) return arealist
def test_randomise_withmoc(): from mocpy import MOC mocfile = get_pkg_data_filename('data/testcat_moc_1.moc') moc = MOC.from_fits(mocfile) datafile = get_pkg_data_filename('data/testcat_moc_1.fits') cat = Catalogue(datafile, area=moc, name='test') numrepeat = 3 rndcat = cat.randomise(numrepeat=numrepeat) assert len(rndcat) > len(cat) assert all(moc.contains(rndcat.coords.ra, rndcat.coords.dec))
def test_set_fromfile_areafrommoc_nomags(): from mocpy import MOC datafile = get_pkg_data_filename('data/testcat_moc_1.fits') mocfile = get_pkg_data_filename('data/testcat_moc_1.moc') moc = MOC.from_fits(mocfile) cat = Catalogue(datafile, area=moc, name='test') assert cat.name == 'test' assert cat.area > 0 assert cat.poserr_type == 'circle' assert isinstance(cat.coords, SkyCoord) assert isinstance(cat.poserr, SkyCoordErr) assert cat.mags is None
def mocFilter(apiobjects, moc_path): # Use object table with only unique OIDs unique_df = apiobjects.drop_duplicates(subset=['oid']) # Stipulation if object table is empty if len(unique_df) == 0: return unique_df # Running mocpy filter mocLocation = moc_path moc = MOC.from_fits(mocLocation) mask_in_moc = moc.contains(unique_df['meanra'] * u.deg, unique_df['meandec'] * u.deg) ztfobjects = Table.from_pandas(unique_df[mask_in_moc]) return ztfobjects
def test_apply_moc(): from mocpy import MOC datafile = get_pkg_data_filename('data/testcat_moc_2.fits') mocfile = get_pkg_data_filename('data/testcat_moc_2.moc') cat = Catalogue(datafile, area=mocfile, name='test', poserr_cols=['raErr', 'decErr'], poserr_type='rcd_dec_ellipse') mocfile = get_pkg_data_filename('data/testcat_moc_1.moc') moc = MOC.from_fits(mocfile) newcat = cat.apply_moc(moc) assert len(cat) > len(newcat) assert moc.intersection(newcat.moc) is not None
def _mockcat_area(**kwargs): geometry = kwargs['geometry'] if geometry == 'allsky': area = ALLSKY_AREA_DEG elif geometry == 'cone': r = kwargs['r'] * u.deg area = np.pi * r**2 elif geometry == 'moc': area = MOC.from_fits(kwargs['mocfile']) else: raise ValueError('Unknown geometry: {}'.format(geometry)) return area
def test_set_fromtable_areafrommoc_nomags(): from mocpy import MOC from astropy.table import Table mocfile = get_pkg_data_filename('data/testcat_moc_1.moc') moc = MOC.from_fits(mocfile) datafile = get_pkg_data_filename('data/testcat_moc_1.fits') data = Table.read(datafile) cat = Catalogue(data, area=moc, name='test') assert cat.name == 'test' assert cat.area > 0 assert cat.poserr_type == 'circle' assert isinstance(cat.coords, SkyCoord) assert isinstance(cat.poserr, SkyCoordErr) assert cat.mags is None assert len(cat) == len(data) assert len(cat.coords) == len(data) assert len(cat.poserr) == len(data)
def make_image_of_MOC(fits_bytes): """make_image_of_MOC. Args: fits_bytes: """ inbuf = io.BytesIO(fits_bytes) moc = MOC.from_fits(inbuf) notmoc = moc.complement() fig = plt.figure(111, figsize=(10, 5)) with World2ScreenMPL(fig, fov=360 * u.deg, projection="AIT") as wcs: ax = fig.add_subplot(1, 1, 1, projection=wcs) notmoc.fill(ax=ax, wcs=wcs, alpha=1.0, fill=True, color="lightgray", linewidth=None) moc.fill (ax=ax, wcs=wcs, alpha=1.0, fill=True, color="red", linewidth=None) moc.border(ax=ax, wcs=wcs, alpha=1, color="red") plt.grid(color="black", linestyle="dotted") outbuf = io.BytesIO() plt.savefig(outbuf, format='png', bbox_inches='tight', dpi=200) bytes = outbuf.getvalue() outbuf.close() return bytes
return temp_moc co = 100000 st = 1 en = st + co query = "SELECT objID,ra,dec FROM objectCoords WITH (NOLOCK) WHERE objID between {} AND {}".format( st, en) df = query_db(TIC_CONN, query) # Generate MOC start_time = time.time() pool = ThreadPoolExecutor(max_workers=4) results = list(pool.map(get_catalog_moc, [row for _, row in df.iterrows()])) end_time = time.time() print('Total time : {} seconds'.format(end_time - start_time)) start_time = time.time() # moc = MOC.union(results[0], *results[1:]) moc = MOC.union(*results) end_time = time.time() print('Total time : {} seconds'.format(end_time - start_time)) # Save MOC # hdulist = moc.serialize(format='fits') # hdulist.writeto('TIC_v70_{}.fits'.format(MAX_DEPTH)) moc.write('TIC_v70_{}.fits'.format(MAX_DEPTH), format='fits', overwrite=True) # Read from file moc = MOC.from_fits('TIC_v70_15.fits')
from mocpy import MOC, World2ScreenMPL from astropy.coordinates import SkyCoord, Angle from matplotlib.path import Path from matplotlib.patches import PathPatch import astropy.units as u moc = MOC.from_fits('polygon_moc.fits') skycoords = moc.get_boundaries() # Plot the MOC using matplotlib import matplotlib.pyplot as plt fig = plt.figure(111, figsize=(10, 10)) # Define a astropy WCS easily with World2ScreenMPL( fig, fov=20 * u.deg, center=SkyCoord(10, 5, unit='deg', frame='icrs'), coordsys="icrs", rotation=Angle(0, u.degree), # The gnomonic projection transforms great circles into straight lines. projection="TAN") as wcs: ax = fig.add_subplot(1, 1, 1, projection=wcs) # Call fill with a matplotlib axe and the `~astropy.wcs.WCS` wcs object. moc.fill(ax=ax, wcs=wcs, alpha=0.5, fill=True, color="red", linewidth=1) moc.border(ax=ax, wcs=wcs, alpha=1, color="red") # Plot the border from astropy.wcs.utils import skycoord_to_pixel x, y = skycoord_to_pixel(skycoords[0], wcs)
def read_watchlist_cache_files(cache_dir): """read_watchlist_cache_files. This function reads all the files in the cache directories and keeps them in memory in a list called "watchlistlist". Each watchlist is a dictionary: cone_ids: the integer ids of the watchlist cones ra, de: the lists of ra and dec posiitons of the cones, in degrees radius: the list of radii of the cones about those points names: the naems give to the cones by the user Args: cache_dir: """ watchlistlist = [] try: dir_list = os.listdir(cache_dir) except: print( 'ERROR in filter/check_alerts_watchlists: cannot read watchlist cache directory' ) sys.stdout.flush() for wl_dir in dir_list: # every directory in the cache should be of the form wl_<nn> # where nn is the watchlist id try: wl_id = int(wl_dir[3:]) except: continue # id of the watchlist watchlist = {'wl_id': wl_id} moclist = [] filelist = os.listdir(cache_dir + '/' + wl_dir) filelist.sort() for file in filelist: gfile = cache_dir + '/' + wl_dir + '/' + file # read in the mocs if file.startswith('moc'): moclist.append(MOC.from_fits(gfile)) # read in the csv files of watchlist cones if file.startswith('watchlist'): cone_ids = [] ralist = [] delist = [] radius = [] names = [] for line in open(gfile).readlines(): tok = line.split(',') cone_ids.append(int(tok[0])) ralist.append(float(tok[1])) delist.append(float(tok[2])) radius.append(float(tok[3])) names.append(tok[4].strip()) watchlist['cones'] = { 'cone_ids': cone_ids, 'ra': ralist, 'de': delist, 'radius': radius, 'names': names } watchlist['moclist'] = moclist watchlistlist.append(watchlist) return watchlistlist
def __init__(self, moc_fits: fits.HDUList, *args, **kwargs): super().__init__(*args, **kwargs) self.moc = MOC.from_fits(moc_fits)
moc_list = [] for s in range(17,19): print(s) moc = get_data(s) moc.write('data/tess_S{}_{}.fits'.format(s, MAX_DEPTH), format='fits', overwrite=True) moc_list.append(moc) # Load MOCs moc_list = [] for filename in os.listdir('data'): if filename.endswith('fits'): t = re.findall(r'tess_S(\d+)_', filename) s = t[0] if int(s) < 17: continue moc = MOC.from_fits(os.path.join('data', filename)) moc_list.append((moc,s)) # Generate the figure(s) for t in moc_list: moc, s = t print(s) my_plot(moc, frame=Galactic(), save='figures/tess_S{:04d}.png'.format(int(s))) # fig = plt.figure(111, figsize=(12.5, 10.5)) # # with WCS(fig, # fov=360 * u.deg,
from mocpy import MOC, WCS from astropy.coordinates import Angle, SkyCoord import astropy.units as u # Load Galex and SDSS sdss = MOC.from_fits('./../../resources/P-SDSS9-r.fits') galex = MOC.from_fits('./../../resources/P-GALEXGR6-AIS-FUV.fits') # Compute their intersection inter = sdss.intersection(galex) union = sdss.union(galex) # Plot the MOC using matplotlib import matplotlib.pyplot as plt fig = plt.figure(111, figsize=(10, 10)) # Define a astropy WCS easily with WCS(fig, fov=160 * u.deg, center=SkyCoord(0, 0, unit='deg', frame='icrs'), coordsys="icrs", rotation=Angle(0, u.degree), projection="AIT") as wcs: ax = fig.add_subplot(1, 1, 1, projection=wcs) # Call fill with a matplotlib axe and the `~astropy.wcs.WCS` wcs object. union.fill(ax=ax, wcs=wcs, alpha=0.5, fill=True, color="red", linewidth=0, label="Union") union.border(ax=ax, wcs=wcs, alpha=1, color="red") inter.fill(ax=ax,
start_time = time.time() moc = MOC.union(*results) end_time = time.time() print('Total time : {} seconds'.format(end_time - start_time)) # Save MOC # hdulist = moc.serialize(format='fits') # hdulist.writeto('tess_s0001_{}.fits'.format(MAX_DEPTH)) moc.write('tess_s0001_{}.fits'.format(MAX_DEPTH), format='fits', overwrite=True) # moc.write('tess_{}.fits'.format(MAX_DEPTH), format='fits', overwrite=True) # moc.write('hst_{}.fits'.format(MAX_DEPTH), format='fits', overwrite=True) # Read from file moc = MOC.from_fits('tess_s0001_14.fits') moc = MOC.from_fits('tess_9.fits') moc.plot(title='TESS') plt.savefig('full_tess.png') # Plot the MOC using matplotlib fig = plt.figure(111, figsize=(10, 8)) # Define a astropy WCS easily with WCS(fig, fov=360 * u.deg, center=SkyCoord(0, 0, unit='deg', frame='galactic'), coordsys="galactic", rotation=Angle(0, u.degree), projection="AIT") as wcs: ax = fig.add_subplot(1, 1, 1, projection=wcs)
from mocpy import MOC, WCS from astropy.coordinates import SkyCoord, Angle from matplotlib.path import Path from matplotlib.patches import PathPatch import astropy.units as u moc = MOC.from_fits('polygon_moc.fits') skycoords = moc.get_boundaries() # Plot the MOC using matplotlib import matplotlib.pyplot as plt fig = plt.figure(111, figsize=(10, 10)) # Define a astropy WCS easily with WCS(fig, fov=20 * u.deg, center=SkyCoord(10, 5, unit='deg', frame='icrs'), coordsys="icrs", rotation=Angle(0, u.degree), # The gnomonic projection transforms great circles into straight lines. projection="TAN") as wcs: ax = fig.add_subplot(1, 1, 1, projection=wcs) # Call fill with a matplotlib axe and the `~astropy.wcs.WCS` wcs object. moc.fill(ax=ax, wcs=wcs, alpha=0.5, fill=True, color="red", linewidth=1) moc.border(ax=ax, wcs=wcs, alpha=1, color="red") # Plot the border from astropy.wcs.utils import skycoord_to_pixel x, y = skycoord_to_pixel(skycoords[0], wcs) p = Path(np.vstack((x, y)).T)
from astropy.io import fits from astropy.wcs import WCS as WCS from mocpy import MOC from astropy.visualization import simple_norm import astropy.units as u import numpy as np # load 2MASS cutout covering the galactic plane hdu = fits.open( 'http://alasky.u-strasbg.fr/hips-image-services/hips2fits?hips=CDS%2FP%2F2MASS%2FK&width=1200&height=700&fov=30&projection=TAN&coordsys=galactic&rotation_angle=0.0&object=gal%20center&format=fits' ) # load Spitzer MOC moc = MOC.from_fits( 'http://skies.esac.esa.int/Spitzer/IRAC1_bright_ISM/Moc.fits') # create WCS from 2MASS image header twomass_wcs = WCS(header=hdu[0].header) # compute skycoords for every pixel of the image width = hdu[0].header['NAXIS1'] height = hdu[0].header['NAXIS2'] xv, yv = np.meshgrid(np.arange(0, width), np.arange(0, height)) skycoords = twomass_wcs.pixel_to_world(xv, yv) ra, dec = skycoords.icrs.ra.deg, skycoords.icrs.dec.deg # Get the skycoord lying in the MOC mask mask_in_moc = moc.contains(ra * u.deg, dec * u.deg) img = hdu[0].data
from mocpy import MOC, WCS from astropy.coordinates import Angle, SkyCoord import astropy.units as u # Load Galex and SDSS sdss = MOC.from_fits('./../../resources/P-SDSS9-r.fits') galex = MOC.from_fits('./../../resources/P-GALEXGR6-AIS-FUV.fits') # Compute their intersection inter = sdss.intersection(galex) union = sdss.union(galex) # Plot the MOC using matplotlib import matplotlib.pyplot as plt fig = plt.figure(111, figsize=(10, 10)) # Define a astropy WCS easily with WCS(fig, fov=160 * u.deg, center=SkyCoord(0, 0, unit='deg', frame='icrs'), coordsys="icrs", rotation=Angle(0, u.degree), projection="AIT") as wcs: ax = fig.add_subplot(1, 1, 1, projection=wcs) # Call fill with a matplotlib axe and the `~astropy.wcs.WCS` wcs object. union.fill(ax=ax, wcs=wcs, alpha=0.5, fill=True, color="red", linewidth=0, label="Union") union.border(ax=ax, wcs=wcs, alpha=1, color="red") inter.fill(ax=ax, wcs=wcs, alpha=0.5, fill=True, color="green", linewidth=0, label="Intersection") inter.border(ax=ax, wcs=wcs, alpha=1, color="green") ax.legend() plt.xlabel('ra') plt.ylabel('dec') plt.title('Logical operations between SDSS and GALEX')
df['coords'] = df.apply(lambda x: parse_s_region(x['s_region']), axis=1) # Generate MOC results = [get_polygon_moc(row) for _, row in df.iterrows()] # Union of MOCs if len(results) > 1: moc = MOC.union(*results) else: moc = results return moc # Build the MOCs moc = get_data() moc.write('data/k2/k2_{}.fits'.format(MAX_DEPTH), format='fits', overwrite=True) # Load MOCs moc = MOC.from_fits(os.path.join('data/k2', 'k2_{}.fits'.format(MAX_DEPTH))) # Plot MOCs my_plot(moc, frame=Galactic(), save='caom_K2_galactic.png', grid=True, labels=True, color='blue')
from mocpy import MOC, World2ScreenMPL from astropy.coordinates import Angle, SkyCoord import astropy.units as u # Load a MOC filename = './../../resources/P-SDSS9-r.fits' moc = MOC.from_fits(filename) # Plot the MOC using matplotlib import matplotlib.pyplot as plt fig = plt.figure(111, figsize=(15, 10)) # Define a astropy WCS easily with World2ScreenMPL(fig, fov=200 * u.deg, center=SkyCoord(0, 20, unit='deg', frame='icrs'), coordsys="icrs", rotation=Angle(0, u.degree), projection="AIT") as wcs: ax = fig.add_subplot(1, 1, 1, projection=wcs) # Call fill with a matplotlib axe and the `~astropy.wcs.WCS` wcs object. moc.fill(ax=ax, wcs=wcs, alpha=0.5, fill=True, color="green") moc.border(ax=ax, wcs=wcs, alpha=0.5, color="black") plt.xlabel('ra') plt.ylabel('dec') plt.title('Coverage of P-SDSS9-r') plt.grid(color="black", linestyle="dotted") plt.show()
def _set_moc(self, mocfile): if mocfile is not None: return MOC.from_fits(mocfile)
from mocpy import MOC, WCS from astropy.coordinates import Angle, SkyCoord import astropy.units as u # Load a MOC filename = './../../resources/P-SDSS9-r.fits' moc = MOC.from_fits(filename) # Plot the MOC using matplotlib import matplotlib.pyplot as plt fig = plt.figure(111, figsize=(15, 10)) # Define a astropy WCS easily with WCS(fig, fov=200 * u.deg, center=SkyCoord(0, 20, unit='deg', frame='icrs'), coordsys="icrs", rotation=Angle(0, u.degree), projection="AIT") as wcs: ax = fig.add_subplot(1, 1, 1, projection=wcs) # Call fill with a matplotlib axe and the `~astropy.wcs.WCS` wcs object. moc.fill(ax=ax, wcs=wcs, alpha=0.5, fill=True, color="green") moc.border(ax=ax, wcs=wcs, alpha=0.5, color="black") plt.xlabel('ra') plt.ylabel('dec') plt.title('Coverage of P-SDSS9-r') plt.grid(color="black", linestyle="dotted") plt.show()
def moc_confidence_region(prob, id_object, percentage): """It returns a confidence region that encloses a given percentage of the localization probability using the Multi Order Coverage map (MOC) method. The sky localization area (in square degrees) is also provided for the confidence region. Parameters ---------- infile : `str` the HEALPix fits file name, the local file or the link location percentage : `float` the decimal percentage of the location probability (from 0 to 1) Returns ------- A local file in which the MOC is serialized in "fits" format. The file is named : "moc_"+'%.1f' % percentage+'_'+skymap ---------------------------------------------------- "moc_" :`str, default` default string as file prefix '%.1f' % percentage+' : `str, default` decimal percentage passed to the function skymap : `str, default` string after the last slash and parsing for "." from infile parameter ---------------------------------------------------- area_sq2 : `float, default` the area of the moc confidence region in square degrees. """ # reading skymap #prob = hp.read_map(infile, verbose = False) npix = len(prob) nside = hp.npix2nside(npix) #healpix resolution cumsum = np.sort(prob)[::-1].cumsum() # sort and cumulative sum # finding the minimum credible region how_many_ipixs, cut_percentage = min(enumerate(cumsum), key=lambda x: abs(x[1] - percentage)) del (cumsum) #print ('number of ipixs',how_many_ipixs,'; cut@', round(cut_percentage,3)) indices = range(0, len(prob)) prob_indices = np.c_[prob, indices] sort = prob_indices[prob_indices[:, 0].argsort()[::-1]] ipixs = sort[0:how_many_ipixs + 1, [1]].astype(int) # from index to polar coordinates theta, phi = hp.pix2ang(nside, ipixs) # converting these to right ascension and declination in degrees ra = np.rad2deg(phi) dec = np.rad2deg(0.5 * np.pi - theta) # creating an astropy.table with RA[deg] and DEC[deg] contour_ipix = Table([ra, dec], names=('RA', 'DEC'), meta={'name': 'first table'}) order = int(log(nside, 2)) # moc from table moc = MOC.from_lonlat(contour_ipix['RA'].T * u.deg, contour_ipix['DEC'].T * u.deg, order) # getting skymap name skymap = id_object.rsplit('/', 1)[-1].rsplit('.')[0] moc_name = "moc_" + '%.1f' % percentage + "_" + skymap # return moc region in fits format moc.write( moc_name, format="fits", ) # square degrees in a whole sphere from math import pi square_degrees_sphere = (360.0**2) / pi moc = MOC.from_fits(moc_name) # printing sky area at the given percentage area_sq2 = round((moc.sky_fraction * square_degrees_sphere), 1) print('The ' + str((percentage * 100)) + '% of ' + moc_name + ' is ', area_sq2, 'sq. deg.')