def nearest_source(band, skypos, radius=0.01, maglimit=20.0, verbose=0, catalog="MCAT", retries=20): """Return targeting parameters for the nearest MCAT source to a position. """ out = np.array( gQuery.getArray( gQuery.mcat_sources(band, skypos[0], skypos[1], radius, maglimit=maglimit), verbose=verbose, retries=retries ) ) if not len(out) and band == "FUV": if verbose: print "No nearby MCAT source found in FUV. Trying NUV..." band = "NUV" out = np.array( gQuery.getArray( gQuery.mcat_sources(band, skypos[0], skypos[1], radius, maglimit=maglimit), verbose=verbose, retries=retries, ) ) if not len(out) and band == "NUV": if verbose: print "No nearby MCAT source found. Using input sky position." return skypos[0], skypos[1], 0.01 # dist=np.sqrt( (out[:,0]-skypos[0])**2 + (out[:,1]-skypos[1])**2) dist = angularSeparation(out[:, 0], out[:, 1], skypos[0], skypos[1]) if verbose > 1: print "Finding nearest among " + str(len(dist)) + " nearby sources." # Note that this doesn't cope with multiple entries for the same source. s = out[np.where(dist == dist.min())][0] # RA, Dec, NUV mag, FUV mag, NUV fwhm, FUV fwhm return avg_sources(band, [s[0], s[1]], verbose=verbose, retries=retries)
def nearest_distinct_source(band,skypos,radius=0.1,maglimit=20.0,verbose=0, catalog='MCAT',retries=20): """Return parameters for the nearest non-targeted source.""" out = np.array(gQuery.getArray(gQuery.mcat_sources(band,skypos[0],skypos[1],radius,maglimit=maglimit),verbose=verbose,retries=retries)) #dist=np.sqrt( (out[:,0]-skypos[0])**2 + (out[:,1]-skypos[1])**2) dist = angularSeparation(out[:,0],out[:,1],skypos[0],skypos[1]) ix = np.where(dist>0.005) return np.array(out)[ix][np.where(dist[ix]==dist[ix].min())][0]
def suggest_bg_radius(band, skypos, radius=0.1, maglimit=20.0, verbose=0, catalog="MCAT", retries=20): """Returns a recommended background radius based upon the positions and FWHM of nearby sources in the MCAT. """ nearest = nearest_distinct_source(band, skypos, verbose=verbose, retries=retries) # dist = np.sqrt( (nearest[0]-skypos[0])**2 + (nearest[1]-skypos[1])**2 ) dist = angularSeparation(nearest[0], nearest[1], skypos[0], skypos[1]) return round(dist - 3 * nearest[-2 if band == "NUV" else -1], 4)
def parse_unique_sources(ras,decs,fmags,nmags,margin=0.005): """Iteratively returns unique sources based upon a _margin_ within which two sources should be considered the same sources. Is a little bit sensitive to the first entry and could probably be written to be more robust, but works well enough. """ skypos = zip(ras,decs) for i,pos in enumerate(skypos): ix = np.where(angularSeparation(pos[0],pos[1],ras,decs)<=margin) skypos[i]=[ras[ix].mean(),decs[ix].mean()] a = skypos #unique_sources(data['ra'],data['dec']) b = [] for i in a: if not (i in b): b+=[i] return b
def get_aspect(band,skypos,trange=[6e8,11e8],verbose=0, detsize=1.25): """Get aspect solution in a dict() for given time range.""" asp = np.array(gQuery.getArray(gQuery.aspect_skypos(skypos[0],skypos[1], detsize=detsize),verbose=verbose)) data = {'eclipse':np.array(asp[:,0],dtype='int16'),'filename':asp[:,1], 't':np.array(asp[:,2],dtype='float64')/tscale, 'ra':np.array(asp[:,3],dtype='float64'), 'dec':np.array(asp[:,4],dtype='float64'), 'twist':np.array(asp[:,5],dtype='float64'), 'flag':np.array(asp[:,6],dtype='int8'), 'ra0':np.array(asp[:,7],dtype='float64'), 'dec0':np.array(asp[:,8],dtype='float64'), 'twist0':np.array(asp[:,9],dtype='float64')} ix = np.where((data['t']>trange[0]) & (data['t']<trange[1]) & (angularSeparation(skypos[0],skypos[1], data['ra'],data['dec'])<=detsize/2.)) for key in data.keys(): data[key] = data[key][ix] return data
def get_aspect(band, skypos, trange=[6e8, 11e8], verbose=0, detsize=1.25): """Get aspect solution in a dict() for given time range.""" asp = np.array(gQuery.getArray(gQuery.aspect_skypos(skypos[0], skypos[1], detsize=detsize), verbose=verbose)) data = { "eclipse": np.array(asp[:, 0], dtype="int16"), "filename": asp[:, 1], "t": np.array(asp[:, 2], dtype="float64") / tscale, "ra": np.array(asp[:, 3], dtype="float64"), "dec": np.array(asp[:, 4], dtype="float64"), "twist": np.array(asp[:, 5], dtype="float64"), "flag": np.array(asp[:, 6], dtype="int8"), "ra0": np.array(asp[:, 7], dtype="float64"), "dec0": np.array(asp[:, 8], dtype="float64"), "twist0": np.array(asp[:, 9], dtype="float64"), } ix = np.where( (data["t"] > trange[0]) & (data["t"] < trange[1]) & (angularSeparation(skypos[0], skypos[1], data["ra"], data["dec"]) <= detsize / 2.0) ) for key in data.keys(): data[key] = data[key][ix] return data