def __init__(self, field_list, max_depth=7): if not os.path.exists(field_list): raise IOError("Cannot find file %s" % field_list) self.fields_df = pd.read_csv(field_list) self.max_depth = max_depth self.region_list = regions.ShapeList() self.moc = MOC() for _, field in self.fields_df.iterrows(): center = SkyCoord(ra=field.RA, dec=field.Dec, unit="hourangle,deg") sep = np.hypot(self.WIDTH, self.HEIGHT) / 2.0 self.region_list.append( regions.RectangleSkyRegion( center, self.WIDTH, self.HEIGHT, self.ASKAP_ROTATION + (field.Rotation * u.deg), )) self.vertices = SkyCoord([ center.directional_offset_by( self.ASKAP_ROTATION + pa + (field.Rotation * u.deg), sep) for pa in [45, 135, 225, 315] * u.deg ]) self.moc = self.moc.union( MOC.from_polygon_skycoord(self.vertices, max_depth=self.max_depth))
def load_ztf(): log.info('downloading and reading ZTF field list') url = 'https://github.com/ZwickyTransientFacility/ztf_information/raw/master/field_grid/ZTF_Fields.txt' with get_readable_fileobj(url, show_progress=False) as f: first_line, *lines = f names = re.split(r'\s\s+', first_line.lstrip('%').strip()) table = Table.read(lines, format='ascii', names=names) log.info('building footprint polygons') lon, lat = get_ztf_footprint_corners() centers = SkyCoord(table['RA'] * u.deg, table['Dec'] * u.deg) vertices = get_footprints_grid(lon, lat, centers) log.info('building MOCs') mocs = [MOC.from_polygon_skycoord(verts) for verts in vertices] log.info('creating ORM records') telescope = Telescope(telescope_name='ZTF', fields=[ Field.from_moc(moc, field_id=field_id) for field_id, moc in zip(table['ID'], mocs) ]) log.info('saving') db.session.add(telescope) db.session.commit() log.info('done')
def test_polygon_issue_50(): from mocpy import MOC from astropy.coordinates import SkyCoord from astropy import units as u coords = SkyCoord([(353.8156714, -56.33202193), (6.1843286, -56.33202193), (5.27558041, -49.49378172), (354.72441959, -49.49378172)], unit=u.deg) moc = MOC.from_polygon_skycoord(coords) assert not moc.empty()
def from_polygon(cls, polygon, *args, **kwargs): """Create a new instance from a polygon. Parameters ---------- polygon : astropy.coordinates.SkyCoord The vertices of the polygon. Returns ------- list A list of Tile instances. """ moc = MOC.from_polygon_skycoord(polygon) return cls.from_moc(moc, *args, **kwargs)
def handler(payload, root): run_test = False if run_test: s3path = 'test' else: s3path = 'fit' role = root.attrib['role'] print("ROLE is ", role) params = { elem.attrib['name']: elem.attrib['value'] for elem in root.iterfind('.//Param') } for key, value in params.items(): print(key, '=', value) keys = params.keys() notices = [150, 151, 152, 153, 164] if int(params['Packet_Type']) in notices: #lag for 5 minutes ingesttime_low = datetime.datetime.now() time.sleep(60 * 5) ingesttime_high = datetime.datetime.now() #query for the same graceid and alerttype made within the last 5 minutes #if it exists: return 0 # if it doesn't, download procedure alpha go filter = [ gw_alert.graceid == gwa.graceid, gw_alert.alert_type == gwa.alert_type, gw_alert.datecreated > ingesttime_low, gw_alert.datecreated < ingesttime_high ] already_ingested = db.session.query(gw_alert).filter(*filter).all() if len(already_ingested): print('This has already been ingested. Backup is working') return gwa = gw_alert( graceid=params['GraceID'] if 'GraceID' in keys else 'ERROR', packet_type=params['Packet_Type'] if 'Packet_Type' in keys else 0, alert_type=params['AlertType'] if 'AlertType' in keys else 'ERROR', detectors=params['Instruments'] if 'Instruments' in keys else '', far=params['FAR'] if 'FAR' in keys else 0.0, skymap_fits_url=params['skymap_fits'] if 'skymap_fits' in keys else '', prob_bns=params['BNS'] if 'BNS' in keys else 0.0, prob_nsbh=params['NSBH'] if 'NSBH' in keys else 0.0, prob_gap=params['MassGap'] if 'MassGap' in keys else 0.0, prob_bbh=params['BBH'] if 'BBH' in keys else 0.0, prob_terrestrial=params['Terrestrial'] if 'Terrestrial' in keys else 0.0, prob_hasns=params['HasNS'] if 'HasNS' in keys else 0.0, prob_hasremenant=params['HasRemnant'] if 'HasRemnant' in keys else 0.0, datecreated=datetime.datetime.now(), role=role, description="Not sure what to put here", ) path_info = gwa.graceid + '-' + gwa.alert_type filter = [ gw_alert.graceid == gwa.graceid, gw_alert.alert_type == gwa.alert_type ] alertinfo = db.session.query(gw_alert).filter(*filter).all() if len(alertinfo) > 0: path_info = path_info + str(len(alertinfo)) if 'skymap_fits' in params: print("downloading skymap_fits") s3 = boto3.client('s3') downloadpath = '{}/{}.fits.gz'.format(s3path, path_info) r = requests.get(params['skymap_fits']) with io.BytesIO() as f: f.write(r.content) f.seek(0) s3.upload_fileobj(f, Bucket=config.AWS_BUCKET, Key=downloadpath) print("download finished") skymap, header = hp.read_map(params['skymap_fits'], h=True, verbose=False) header = dict(header) hkeys = header.keys() gwa.time_of_signal = header[ 'DATE-OBS'] if 'DATE-OBS' in hkeys else '1991-12-23T19:15:00' gwa.distance = header[ 'DISTMEAN'] if 'DISTMEAN' in hkeys else "-999.9" gwa.distance_error = header[ 'DISTSTD'] if 'DISTSTD' in hkeys else "-999.9" gwa.timesent = header[ 'DATE'] if 'DATE' in hkeys else '1991-12-23T19:15:00' print('Creating 90/50 contours') prob, _ = ligo.skymap.io.fits.read_sky_map(gwa.skymap_fits_url, nest=None) prob = interpolate_nested(prob, nest=True) i = np.flipud(np.argsort(prob)) cumsum = np.cumsum(prob[i]) cls = np.empty_like(prob) cls[i] = cumsum * 100 paths = list( ligo.skymap.postprocess.contour(cls, [50, 90], nest=True, degrees=True, simplify=True)) contour_download_path = '{}/{}-contours-smooth.json'.format( s3path, path_info) with io.BytesIO() as cc: tt = json.dumps({ 'type': 'FeatureCollection', 'features': [{ 'type': 'Feature', 'properties': { 'credible_level': contour }, 'geometry': { 'type': 'MultiLineString', 'coordinates': path } } for contour, path in zip([50, 90], paths)] }) cc.write(tt.encode()) cc.seek(0) s3.upload_fileobj(cc, Bucket=config.AWS_BUCKET, Key=contour_download_path) #################### print('Creating Fermi and LAT MOC files') #################### tos = datetime.datetime.strptime(gwa.time_of_signal, "%Y-%m-%dT%H:%M:%S.%f") fermi_moc_upload_path = '{}/{}-Fermi.json'.format( s3path, gwa.graceid) try: s3.head_object(Bucket=config.AWS_BUCKET, Key=fermi_moc_upload_path) print('Fermi file already exists') except: #calculate try: earth_ra, earth_dec, earth_rad = function.getearthsatpos( tos) contour = function.makeEarthContour( earth_ra, earth_dec, earth_rad) skycoord = SkyCoord(contour, unit="deg", frame="icrs") inside = SkyCoord(ra=earth_ra + 180, dec=earth_dec, unit="deg", frame="icrs") moc = MOC.from_polygon_skycoord(skycoord, max_depth=9) moc = moc.complement() mocfootprint = moc.serialize(format='json') #store on S3 with io.BytesIO() as mm: moc_string = json.dumps(mocfootprint) mm.write(moc_string.encode()) mm.seek(0) s3.upload_fileobj(mm, Bucket=config.AWS_BUCKET, Key=fermi_moc_upload_path) print('Successfully Created Fermi MOC File for {}'.format( gwa.graceid)) except: print('ERROR in Fermi MOC creation for {}'.format( gwa.graceid)) ####LAT Creation##### lat_moc_upload_path = '{}/{}-LAT.json'.format(s3path, gwa.graceid) try: s3.head_object(Bucket=config.AWS_BUCKET, Key=lat_moc_upload_path) print('LAT file already exists') except: try: ra, dec = function.getFermiPointing(tos) pointing_footprint = function.makeLATFoV(ra, dec) skycoord = SkyCoord(pointing_footprint, unit="deg", frame="icrs") moc = MOC.from_polygon_skycoord(skycoord, max_depth=9) mocfootprint = moc.serialize(format='json') with io.BytesIO() as ll: moc_string = json.dumps(mocfootprint) ll.write(moc_string.encode()) ll.seek(0) s3.upload_fileobj(ll, Bucket=config.AWS_BUCKET, Key=lat_moc_upload_path) print('Successfully Created LAT MOC File for {}'.format( gwa.graceid)) except: print('ERROR in LAT MOC creation for {}'.format( gwa.graceid)) ################### if not run_test: db.session.add(gwa) print("commiting\n") db.session.commit() else: print('Sleeping, you should kill') time.sleep(20) else: print("\nNot Ligo, Don't Care\n")
import numpy as np # The set of points delimiting the polygon in deg vertices = np.array([[18.36490956, 5.], [15.7446692, 6.97214743], [16.80755056, 10.29852928], [13.36215502, 10.14616136], [12.05298691, 13.10706197], [9.54793022, 10.4556709], [8.7677627, 7.80921809], [9.71595962, 5.30855011], [7.32238541, 6.44905255], [0.807265, 6.53399616], [1.08855146, 3.51294225], [2.07615384, 0.7118289], [3.90690158, -1.61886929], [9.03727956, 2.80521847], [9.22274427, -4.38008174], [10.23563378, 4.06950324], [10.79931601, 3.77655576], [14.16533992, 1.7579858], [19.36243491, 1.78587203], [15.31732084, 5.]]) skycoord = SkyCoord(vertices, unit="deg", frame="icrs") # A point that we say it belongs to the inside of the MOC inside = SkyCoord(ra=10, dec=5, unit="deg", frame="icrs") moc = MOC.from_polygon_skycoord(skycoord, max_depth=9) moc.write('polygon_moc.fits', format='fits', overwrite=True) # 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.
[ 7.32238541, 6.44905255], [ 0.807265 , 6.53399616], [ 1.08855146, 3.51294225], [ 2.07615384, 0.7118289 ], [ 3.90690158, -1.61886929], [ 9.03727956, 2.80521847], [ 9.22274427, -4.38008174], [10.23563378, 4.06950324], [10.79931601, 3.77655576], [14.16533992, 1.7579858 ], [19.36243491, 1.78587203], [15.31732084, 5. ]]) skycoord = SkyCoord(vertices, unit="deg", frame="icrs") # A point that we say it belongs to the inside of the MOC inside = SkyCoord(ra=10, dec=5, unit="deg", frame="icrs") moc = MOC.from_polygon_skycoord(skycoord, max_depth=9, inside=inside) moc.write('polygon_moc.fits', format='fits', overwrite=True) # 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)
from mocpy import MOC from astropy.coordinates import SkyCoord from astropy import units as u coords = SkyCoord([(353.8156714, -56.33202193), (6.1843286, -56.33202193), (5.27558041, -49.49378172), (354.72441959, -49.49378172)], unit=u.deg) moc = MOC.from_polygon_skycoord(coords) assert not moc.empty()