def scampastrom(inim): hdr = fits.getheader(inim) #ra, dec = wcscenter(inim) ra = hdr['ra'] dec = hdr['dec'] #ra= '208.3658234' #dec= '40.25456995' #ra = 2.081642579100E+02 #dec= 4.051871459365E+01 rad = coord.Angle(ra, unit=u.deg) radd = rad.degree decd = coord.Angle(dec, unit=u.deg) decdd = decd.degree xpix = hdr['NAXIS1'] / 2. ypix = hdr['NAXIS2'] / 2. #xpix = 364.16726 #ypix = 361.08691 #xpix = 6.0251172E+03 #pix = -1.9102533E+03 threshold, minarea = 3, 3 output_cat = 'astromtest.cat' sexcom = 'sex -c ' + sexconfig + ' ' + inim + ' -PARAMETERS_NAME ' + sexparam + ' -DETECT_THRESH ' + str( threshold ) + ' -DETECT_MINAREA ' + str( minarea ) + ' -CATALOG_NAME ' + output_cat + ' -FILTER_NAME ' + sexconv + ' -STARNNW_NAME ' + sexnnw + ' -CATALOG_TYPE FITS_LDAC' scampcom = 'scamp -c ' + scampconfig + ' astromtest.cat ' + '-ASTREF_CATALOG ' + refcat + ' -CHECKPLOT_DEV NULL -CROSSID_RADIUS 3.0 -PROJECTION_TYPE TAN' print(sexcom) print(scampcom) # iraf setting wcsreset(inim, 'physical') wcsreset(inim, 'world') hedit(inim, 'WAT0_001', 'system=image') hedit(inim, 'WAT1_001', 'wtype=tan axtype=ra') hedit(inim, 'WAT2_001', 'wtype=tan axtype=dec') hedit(inim, 'RADECSYS', 'FK5') hedit(inim, 'EQUINOX', '2000.0') hedit(inim, 'CTYPE1', 'RA---TAN') hedit(inim, 'CTYPE2', 'DEC--TAN') hedit(inim, 'CRVAL1', str(radd)) hedit(inim, 'CRVAL2', str(decdd)) hedit(inim, 'CRPIX1', str(xpix)) hedit(inim, 'CRPIX2', str(ypix)) hedit(inim, 'CD1_1', str(cd11)) hedit(inim, 'CD1_2', str(cd12)) hedit(inim, 'CD2_1', str(cd21)) hedit(inim, 'CD2_2', str(cd22)) os.system(sexcom) os.system('ldactoasc ' + output_cat + ' > tmp.cat') tmpcat = ascii.read('tmp.cat') dolist = [] nolist = [] os.system('mkdir bad_align') if len(tmpcat['NUMBER']) < 5: print('Matched star number = ' + str(len(tmpcat['NUMBER']))) print('I will not touch this, it has stars less than 5. \n') nolist.append(inim + '\n') os.system('mv ' + inim + ' bad_ailgn/') else: print('Scamp will do this!! \n') dolist.append(inim + '\n') os.system(scampcom) hdr1 = hdr hdr1.extend(fits.Header.fromtextfile('astromtest.head'), update=True, update_first=True) fits.writeto('a' + inim, fits.getdata(inim), hdr1) print(inim, 'astrometry work using SCAMP')
"\n") print str return ##check command line parameters #if len(sys.argv) > 3: # print 'usage: fixpoint <RA true (HH:MM:SS.SS)> <DEC true (DD:MM:SS.SS)>' # sys.exit(1) #get target ra and dec from command line, if available ra_target = None dec_target = None if len(sys.argv) >= 3: #convert to decimal degrees ra_target = coord.Angle(sys.argv[1], unit=u.hour).degree dec_target = coord.Angle(sys.argv[2], unit=u.deg).degree image_parms = '' if len(sys.argv) >= 4: #get any parameters passed for image command, e.g. notel image_parms = sys.argv[3] #MODIFY THESE FIELDS AS NEEDED! base_path = '/tmp/' + datetime.datetime.now().strftime( "%Y%m%d.%H%M%S%f.fixpoint.") #log file name log_fname = '/tmp/' + getpass.getuser() + '.fixpoint.log' #path to astrometry.net solve_field executable solve_field_path = '/home/mcnowinski/astrometry/bin/solve-field' #astrometry parameters
def psocolor(source_name,keepfiles=False,allcolor='#FFFF00',rejcolor='b',tm_color='r',plot=False,savepdf=True,secondary='',skipdownloads=False,circle_radius=0.0015,size=2.0,override_directory=None,primarypos_label=None,title=None,filename=None,buffer=False): # Set $FINDER_PATH in your bash_profile if you would like to control where the finder charts are output # size: arcmin # allwise: overplot AllWISE catalog positions # rejallwise = overplot AllWISE reject positions # tmass = overplot 2MASS psc positions # keepfiles = keep fits, tbl, and xml files # allcolor = color of allwise symbols # rejcolor = color of allwise reject symbols # tm_color = color of tmass symbols # plot = show plot (otherwise, finder is just made) # savepdf = save a pdf of the finder circle_width = 2.5 circle_alpha = 0.8 fig_xsize = 11 fig_ysize = 8.5 #Convert sexagecimal if source_name.find(' ') == -1: symbol = '+' if source_name.find('+') == -1: symbol = '-' radec = re.split('[\+ | -]',source_name) ras = radec[0][0:2]+' '+radec[0][2:4]+' '+radec[0][4:] decs = radec[1][0:2]+' '+radec[1][2:4]+' '+radec[1][4:] ra = coord.Angle(ras, unit=u.hour) dec = coord.Angle(symbol+decs,unit=u.degree) source_name = str(ra.degree)+' '+str(dec.degree) #Use buffer if needed if buffer: import matplotlib matplotlib.use('Agg') plot=False import pylab,pyfits,aplpy #Verify whether a working directory is set in the bash profile main_dir = None if override_directory: main_dir = override_directory else: proc = subprocess.Popen(["echo $FINDER_PATH"], stdout=subprocess.PIPE,shell=True) (out, err) = proc.communicate() if out != '\n': main_dir = out.split('\n')[0] if main_dir: initial_dir = os.getcwd() if not os.path.exists(main_dir): os.makedirs(main_dir) os.chdir(main_dir) #List of colors color_blue = '#377eb8'#RGB=[55,126,184] color_red = '#e41a1c'#RGB=[228,26,28] color_purple = '#b27bba'#RGB=[178,123,186] color_green = '#4daf4a'#RGB = [77,175,74] t1 = datetime.now() ra,de = simbad(source_name) ra2 = None de2 = None if secondary: ra2,de2 = simbad(secondary) if filename is None: filename = source_name if skipdownloads is not True: print("Downloading Pan-Starrs data") #Remove previous data os.system("rm *_PSO_TMP.fits*") query_pso_fits(ra,de,size=size,output_file='PSO_TMP.fits') #If no PSO data could be downloaded, return if len(glob.glob('*_PSO_TMP.fits*')) == 0: print("No PSO data !") return if plot: pylab.ion() fig = pylab.figure(figsize=(fig_xsize,fig_ysize)) pylab.rcParams['font.family'] = 'serif' #Create and plot RGB PSO image files = ['y_PSO_TMP.fits','i_PSO_TMP.fits','g_PSO_TMP.fits'] aplpy.make_rgb_cube(files,'PSO_rgb.fits') impsoc = aplpy.FITSFigure('PSO_rgb_2d.fits',figure=fig) impsoc.add_label(0.05,0.9,'PSO $y$/$i$/$g$',relative=True,size='medium',color='k',bbox=dict(facecolor='white', alpha=0.5),horizontalalignment='left') meds = [] mads = [] devs = [] for filei in files: datai = pyfits.getdata(filei) medi = np.nanmedian(datai) madi = np.nanmedian(abs(datai - np.nanmedian(datai))) devi = np.nanpercentile(datai,95) - np.nanpercentile(datai,5) meds.append(medi) mads.append(madi) devs.append(devi) mins = [] maxs = [] for i in range(0,len(files)): mini = (meds[i] - 2.0*mads[i]) #maxi = (meds[i] + 10.0*mads[i]) maxi = meds[i] + 2.0*devs[i] mins.append(mini) maxs.append(maxi) aplpy.make_rgb_image('PSO_rgb.fits','PSO_rgb.png',vmin_r=mins[0],vmin_g=mins[1],vmin_b=mins[2],vmax_r=maxs[0],vmax_g=maxs[1],vmax_b=maxs[2]) impsoc.show_rgb('PSO_rgb.png') impsoc.hide_tick_labels() impsoc.ticks.hide() impsoc.hide_xaxis_label() impsoc.hide_yaxis_label() impsoc.recenter(ra,de,width=(size/60.0),height=(size/60.0)) impsoc.show_circles(ra,de,edgecolor=color_red,linewidth=circle_width,facecolor='none',radius=circle_radius,alpha=circle_alpha) if secondary: impsoc.show_circles(ra2,de2,edgecolor=color_blue,linewidth=circle_width,facecolor='none', radius=circle_radius,alpha=circle_alpha) # Remove files (or not) if keepfiles: pass else: print("Removing files...") cmdrm1 = "rm PSO_rgb.png PSO_rgb*.fits" os.system(cmdrm1) if savepdf: pylab.savefig(filename+'_pso_color.pdf') #Return to initial directory if main_dir: os.chdir(initial_dir) t2 = datetime.now() tdiff = (t2 - t1) print("PSO image creation took %s seconds" % (round(tdiff.total_seconds(),0)))
hb = ax.hexbin(color,GMag,gridsize=500,cmap='inferno',bins='log',mincnt=1) # configure plots: ax.set_title("CMD of all Gaia targets") ax.set_ylabel('M$_{G}$') ax.set_xlabel('G$_{BP}$-G$_{RP}$') ax.invert_yaxis() cb = fig.colorbar(hb, ax=ax) cb.set_label('log10(count)') plt.annotate('{0} stars'.format(color.shape[0]),xy=(0.55,0.85),xycoords='figure fraction') # write out plot: plt.savefig('master_cmd.png',format='png') plt.close(fig) print 'Making Mollweide' # Make Sky Coord objects: pra = coord.Angle(f['ra'].values*u.degree) pra = pra.wrap_at(180*u.degree) pdec = coord.Angle(f['decl'].values*u.degree) # Plot 2d histogram of objects on Mollweide projection: fig2 = plt.figure(2,figsize=(8,6)) ax = fig2.add_subplot(111, projection="mollweide") hb = ax.hexbin(pra.radian, pdec.radian,gridsize=500,cmap='inferno',bins='log',mincnt=1) ax.set_xticklabels(['14h','16h','18h','20h','22h','0h','2h','4h','6h','8h','10h'],color='grey') ax.set_title('All Gaia targets \n') cb = fig2.colorbar(hb, ax=ax,orientation="horizontal", pad=0.1) cb.set_label('log10(count)') ax.grid(True) plt.savefig('master_mollweide.png',format='png') plt.close(fig2)
def plot_neos(date, site_code, asteroids, alt_limit=30.0, plot_filename=None): # Determine local sidereal time (site_name, site_long, site_lat, site_hgt) = get_sitepos(site_code) local_sidereal_time = compute_local_st(date, site_long, site_lat, site_hgt, dbg=False) # Normalize into range 0..PI for plot local_sidereal_time = S.sla_drange(local_sidereal_time) (moon_app_ra, moon_app_dec, diam) = moon_ra_dec(date, site_long, site_lat, site_hgt) moon_app_ra = S.sla_drange(moon_app_ra) ra_list = [] dec_list = [] for rock in asteroids: emp_line = rock.values()[0] if emp_line[4] >= alt_limit: ra = degrees(emp_line[0]) dec = degrees(emp_line[1]) ra_list.append(ra) dec_list.append(dec) ra = coord.Angle(ra_list*u.degree) ra = ra.wrap_at(180*u.degree) dec = coord.Angle(dec_list*u.degree) area, min_ra, max_ra, min_dec, max_dec = compute_area(asteroids, alt_limit) # width = max_ra - min_ra # height = max_dec - min_dec width = height = radians(sqrt(400.0)) center_ra = (max_ra + min_ra)/2.0 center_ra = S.sla_drange(center_ra) center_dec = (max_dec + min_dec)/2.0 radius = radians(90-alt_limit) # print("Center", center_ra, center_dec, radius) # Setup plot with Mollweide projection fig = plt.figure(figsize=(8,6)) ax = fig.add_subplot(111, projection="mollweide") # The following line makes it so that the zoom level no longer changes, # otherwise Matplotlib has a tendency to zoom out when adding overlays. ax.set_autoscale_on(False) # Plot NEOs as a scatterplot ax.scatter(ra.radian, dec.radian, 10) # Plot a rectangle of the BlackGEM survey area centered on the LST and Declination=latitude r = Rectangle((local_sidereal_time, site_lat), width, height, edgecolor='black', facecolor='none') ax.add_patch(r) # Plot a circle at the mean RA,Dec c = Circle((center_ra, center_dec), radius, edgecolor='red', facecolor='none') ax.add_patch(c) ax.set_xticklabels(['14h','16h','18h','20h','22h','0h','2h','4h','6h','8h','10h']) # Plot ecliptic. Plot in two parts because otherwise there is a distracting line # across the equator. ecliptic_ra, ecliptic_dec = compute_ecliptic_location(date, site_code) ecliptic_ra = ecliptic_ra.wrap_at(180*u.degree) # find breakpoint in the RA array when it wraps ra_wrap = ecliptic_ra.argmax() ax.plot(ecliptic_ra[0:ra_wrap].radian, ecliptic_dec[0:ra_wrap].radian, '--b') ax.plot(ecliptic_ra[ra_wrap+1:].radian, ecliptic_dec[ra_wrap+1:].radian, '--b') # Plot a circle at the Moon's position ax.scatter(moon_app_ra, moon_app_dec, 75, color='w', edgecolors='k') ax.set_title("%s #asteroids: %4d" % (date.strftime("%Y-%m-%d"), len(asteroids))) ax.grid(True) fig.tight_layout() if plot_filename: fig_file = default_storage.open(plot_filename,"wb+") fig.savefig(fig_file, format='png') fig_file.close() else: fig.show() plt.close('all') return
def compute_angles(frames_info, true_north, logger=_log): ''' Compute the various angles associated to frames: RA, DEC, parang, pupil offset, final derotation angle Parameters ---------- frames_info : dataframe The data frame with all the information on science frames true_north : float True North offset correction, in degrees logger : logHandler object Log handler for the reduction. Default is root logger ''' logger.debug('> compute angles') # get instrument instrument = frames_info['SEQ ARM'].unique() # derotator drift check and correction date_fix = Time('2016-07-12') if np.any(frames_info['MJD'].values <= date_fix.mjd): try: alt = frames_info['TEL ALT'].values.astype(np.float) drot2 = frames_info['INS4 DROT2 BEGIN'].values.astype(np.float) pa_correction = np.degrees( np.arctan(np.tan(np.radians(alt - 2. * drot2)))) except KeyError: pa_correction = 0 else: pa_correction = 0 # RA/DEC ra_drot = frames_info['INS4 DROT2 RA'].values.astype(np.float) ra_drot_h = np.floor(ra_drot / 1e4) ra_drot_m = np.floor((ra_drot - ra_drot_h * 1e4) / 1e2) ra_drot_s = ra_drot - ra_drot_h * 1e4 - ra_drot_m * 1e2 ra_hour = coordinates.Angle((ra_drot_h, ra_drot_m, ra_drot_s), units.hour) ra_deg = ra_hour * 15 frames_info['RA'] = ra_deg dec_drot = frames_info['INS4 DROT2 DEC'].values.astype(np.float) sign = np.sign(dec_drot) udec_drot = np.abs(dec_drot) dec_drot_d = np.floor(udec_drot / 1e4) dec_drot_m = np.floor((udec_drot - dec_drot_d * 1e4) / 1e2) dec_drot_s = udec_drot - dec_drot_d * 1e4 - dec_drot_m * 1e2 dec_drot_d *= sign dec = coordinates.Angle((dec_drot_d, dec_drot_m, dec_drot_s), units.degree) frames_info['DEC'] = dec # calculate parallactic angles utc = Time(frames_info['TIME'].values.astype(str), scale='utc', location=sphere.location) lst = utc.sidereal_time('apparent') ha = lst - ra_hour pa = parallatic_angle(ha, dec[0], sphere.latitude) frames_info['PARANG'] = pa.value + pa_correction frames_info['HOUR ANGLE'] = ha frames_info['LST'] = lst # Altitude and airmass j2000 = coordinates.SkyCoord(ra=ra_hour, dec=dec, frame='icrs', obstime=utc) altaz = j2000.transform_to(coordinates.AltAz(location=sphere.location)) frames_info['ALTITUDE'] = altaz.alt.value frames_info['AZIMUTH'] = altaz.az.value frames_info['AIRMASS'] = altaz.secz.value # START/END only applicable for IRDIFS data if (instrument == 'IRDIS') or (instrument == 'IFS'): utc = Time(frames_info['TIME START'].values.astype(str), scale='utc', location=sphere.location) lst = utc.sidereal_time('apparent') ha = lst - ra_hour pa = parallatic_angle(ha, dec[0], sphere.latitude) frames_info['PARANG START'] = pa.value + pa_correction frames_info['HOUR ANGLE START'] = ha frames_info['LST START'] = lst utc = Time(frames_info['TIME END'].values.astype(str), scale='utc', location=sphere.location) lst = utc.sidereal_time('apparent') ha = lst - ra_hour pa = parallatic_angle(ha, dec[0], sphere.latitude) frames_info['PARANG END'] = pa.value + pa_correction frames_info['HOUR ANGLE END'] = ha frames_info['LST END'] = lst # # Derotation angles # # PA_on-sky = PA_detector + PARANGLE + True_North + PUP_OFFSET + INSTRUMENT_OFFSET + TRUE_NORTH # # PUP_OFFSET = +135.99 ± 0.11 # INSTRUMENT_OFFSET # * IFS = -100.48 ± 0.10 # * IRD = 0.00 ± 0.00 # TRUE_NORTH = -1.75 ± 0.08 # if len(instrument) != 1: logger.error( 'Sequence is mixing different instruments: {0}'.format(instrument)) return sphere.ERROR if instrument == 'IFS': instru_offset = -100.48 elif instrument == 'IRDIS': instru_offset = 0.0 elif instrument == 'SPARTA': instru_offset = 0.0 else: logger.error('Unkown instrument {0}'.format(instrument)) return sphere.ERROR drot_mode = frames_info['INS4 DROT2 MODE'].unique() if len(drot_mode) != 1: logger.error('Derotator mode has several values in the sequence') return sphere.ERROR if drot_mode == 'ELEV': pupoff = 135.99 elif drot_mode == 'SKY': pupoff = -100.48 + frames_info['INS4 DROT2 POSANG'] elif drot_mode == 'STAT': pupoff = -100.48 else: logger.error('Unknown derotator mode {0}'.format(drot_mode)) return sphere.ERROR frames_info['PUPIL OFFSET'] = pupoff + instru_offset # final derotation value frames_info['DEROT ANGLE'] = frames_info[ 'PARANG'] + pupoff + instru_offset + true_north return sphere.SUCCESS
def get_image_list(self, coordinates, waveband='all', frame_type='stack', image_width=1 * u.arcmin, image_height=None, radius=None, database=None, programme_id=None, get_query_payload=False): """ Function that returns a list of urls from which to download the FITS images. Parameters ---------- coordinates : str or `astropy.coordinates` object The target around which to search. It may be specified as a string in which case it is resolved using online services or as the appropriate `astropy.coordinates` object. ICRS coordinates may also be entered as strings as specified in the `astropy.coordinates` module. waveband : str The color filter to download. Must be one of ``'all'``, ``'J'``, ``'H'``, ``'K'``, ``'H2'``, ``'Z'``, ``'Y'``, ``'Br'``]. frame_type : str The type of image. Must be one of ``'stack'``, ``'normal'``, ``'interleave'``, ``'deep_stack'``, ``'confidence'``, ``'difference'``, ``'leavstack'``, ``'all'``] image_width : str or `~astropy.units.Quantity` object, optional The image size (along X). Cannot exceed 15 arcmin. If missing, defaults to 1 arcmin. image_height : str or `~astropy.units.Quantity` object, optional The image size (along Y). Cannot exceed 90 arcmin. If missing, same as image_width. radius : str or `~astropy.units.Quantity` object, optional The string must be parsable by `~astropy.coordinates.Angle`. The appropriate `~astropy.units.Quantity` object from `astropy.units` may also be used. When missing only image around the given position rather than multi-frames are retrieved. programme_id : str The survey or programme in which to search for. See `list_catalogs`. database : str The WFAU database to use. verbose : bool Defaults to `True`. When `True` prints additional messages. get_query_payload : bool, optional If `True` then returns the dictionary sent as the HTTP request. Defaults to `False`. Returns ------- url_list : list of image urls """ if frame_type not in self.frame_types: raise ValueError( "Invalid frame type. Valid frame types are: {!s}".format( self.frame_types)) if waveband not in self.filters: raise ValueError( "Invalid waveband. Valid wavebands are: {!s}".format( self.filters.keys())) if database is None: database = self.database if programme_id is None: programme_id = self.programme_id request_payload = self._args_to_payload(coordinates, database=database, programme_id=programme_id, query_type='image') request_payload['filterID'] = self.filters[waveband] request_payload['obsType'] = 'object' request_payload['frameType'] = self.frame_types[frame_type] request_payload['mfid'] = '' if radius is None: request_payload['xsize'] = _parse_dimension(image_width) if image_height is None: request_payload['ysize'] = _parse_dimension(image_width) else: request_payload['ysize'] = _parse_dimension(image_height) query_url = self.IMAGE_URL else: query_url = self.ARCHIVE_URL ra = request_payload.pop('ra') dec = request_payload.pop('dec') radius = coord.Angle(radius).degree del request_payload['sys'] request_payload['userSelect'] = 'default' request_payload['minRA'] = str( round(ra - radius / cos(radians(dec)), 2)) request_payload['maxRA'] = str( round(ra + radius / cos(radians(dec)), 2)) request_payload['formatRA'] = 'degrees' request_payload['minDec'] = str(dec - radius) request_payload['maxDec'] = str(dec + radius) request_payload['formatDec'] = 'degrees' request_payload['startDay'] = 0 request_payload['startMonth'] = 0 request_payload['startYear'] = 0 request_payload['endDay'] = 0 request_payload['endMonth'] = 0 request_payload['endYear'] = 0 request_payload['dep'] = 0 request_payload['lmfid'] = '' request_payload['fsid'] = '' request_payload['rows'] = 1000 if get_query_payload: return request_payload response = self._wfau_send_request(query_url, request_payload) response = self._check_page(response.url, "row") image_urls = self.extract_urls(response.text) # different links for radius queries and simple ones if radius is not None: image_urls = [ link for link in image_urls if ('fits_download' in link and '_cat.fits' not in link and '_two.fit' not in link) ] else: image_urls = [ link.replace("getImage", "getFImage") for link in image_urls ] return image_urls
def setUp(self): self.t = t.Time("1988-03-20", scale='utc') self.location = c.EarthLocation.from_geodetic(-71.0833, 42.3333) self.up = sun.Uptime(Ex15aVenus_pt1, self.t, self.location) # replace calculated sidereal time with given time self.up.sidereal_greenwich = c.Angle((11, 50, 58.10), unit=u.hour)
def test_geometric_mean_lon(self): """tests geometric mean lon calculation""" L0 = self.pos.get_mean_lon() self.assertLess(np.abs(L0 - c.Angle(-2318.19280 * u.deg)), 0.5e-5 * u.deg)
def adjust_yse_pointings_base(field_name, snid): # cases # 1. SN is already being observed # 2. SN is not being observed, can test 4 candidate pointings - SN 0.75 deg N/S/E/W of pointing center # the first of these that doesn't overlap with any other blocks can be used # 3. if all of them overlap, this is a very weird edge case that I don't really want to address right now # report SNe w/i last 3 months - both all and "not Ignore" in each block # report best location for new block field_name = field_name.split('.')[0] surveyfields = SurveyFieldMSB.objects.filter( name=field_name)[0].survey_fields.all() #surveyfields = SurveyField.objects.filter(ztf_field_id = field_name).filter(~Q(obs_group__name='ZTF')) pointings, pointing_names = [], [] for s in surveyfields: pointings += [SkyCoord(s.ra_cen, s.dec_cen, unit=u.deg)] pointing_names += [s.field_id] transient = Transient.objects.filter(name=snid)[0] sc_transient = cd.SkyCoord(transient.ra, transient.dec, unit=u.deg) good_pointing_exists = False ra_adj = None for p in pointings: dra, ddec = sc_transient.spherical_offsets_to(p) if np.abs(dra.deg) < 1.55 and np.abs(ddec.deg) < 1.55: # SN is already getting observed good_pointing_exists = True if good_pointing_exists: pointings_for_html = () for p, n in zip(pointings, pointing_names): dra, ddec = sc_transient.spherical_offsets_to(p) pointings_for_html += ((n, GetSexigesimalString( p.ra.deg, p.dec.deg)[0], GetSexigesimalString(p.ra.deg, p.dec.deg)[1], '%.2f' % dra.deg, '%.2f' % ddec.deg, p.ra.deg, p.dec.deg), ) return None, pointings, pointing_names, pointings_for_html, None, transient, surveyfields[ 0].ztf_field_id # hand off the suggested IDs, RAs, Decs #context = {'pointings':pointings_for_html, # 'transient':transient, # 'ztf_field':sf, # 'pointings_good_flag': True #} #return render(request, 'YSE_App/yse_pointings.html', context) # test 4 candidate positions for new SN block d = sc_transient.dec.deg * np.pi / 180 #self.coord.dec.radian width_corr = 0.75 / np.abs(np.cos(d)) # Define the tile offsets: ra_offset = cd.Angle(width_corr, unit=u.deg) dec_offset = cd.Angle(0.75, unit=u.deg) newPS_S = SkyCoord(sc_transient.ra.deg, sc_transient.dec.deg - dec_offset.deg, unit=u.deg) newPS_N = SkyCoord(sc_transient.ra.deg, sc_transient.dec.deg + dec_offset.deg, unit=u.deg) newPS_E = SkyCoord(sc_transient.ra.deg + ra_offset.deg, sc_transient.dec.deg, unit=u.deg) newPS_W = SkyCoord(sc_transient.ra.deg - ra_offset.deg, sc_transient.dec.deg, unit=u.deg) good_new_pointings = [] min_seps = [] for new_pointing in [newPS_S, newPS_N, newPS_E, newPS_W]: good = True sep_list = [] for p in pointings: sep = new_pointing.separation(p).deg dra, ddec = new_pointing.spherical_offsets_to(p) sep_list += [sep] if np.abs(dra.deg) < 3.1 and np.abs(ddec.deg) < 3.1: good = False break if good: good_new_pointings += [new_pointing] min_seps += [min(sep_list)] #import pdb; pdb.set_trace() if not len(good_new_pointings): new_pointing = None #raise Http404('can\'t find a good pointing') else: new_pointing = np.array(good_new_pointings)[np.array(min_seps) == np.min(min_seps)][0] # formatting stuff pointings_for_html = () for p, n in zip(pointings, pointing_names): dra, ddec = sc_transient.spherical_offsets_to(p) pointings_for_html += ((n, GetSexigesimalString(p.ra.deg, p.dec.deg)[0], GetSexigesimalString(p.ra.deg, p.dec.deg)[1], '%.2f' % dra.deg, '%.2f' % ddec.deg, p.ra.deg, p.dec.deg), ) if new_pointing is not None: for p in [new_pointing]: dra, ddec = sc_transient.spherical_offsets_to(p) new_pointing_for_html = ( '%s.%s' % (field_name.split('.')[0], transient.name), GetSexigesimalString(p.ra.deg, p.dec.deg)[0], GetSexigesimalString(p.ra.deg, p.dec.deg)[1], '%.2f' % dra.deg, '%.2f' % ddec.deg, p.ra.deg, p.dec.deg) else: new_pointing_for_html = None return new_pointing, pointings, pointing_names, pointings_for_html, new_pointing_for_html, transient, surveyfields[ 0].ztf_field_id
45, delta=0.01), max_depth=10).complement() m_13hr45.write('mocs/dr2_13h45.moc', format='fits', overwrite=True) m_13hr60 = mocpy.MOC.from_polygon_skycoord(make_polygon(120, 240, 59, 65.5), max_depth=10) m_13hr60.write('mocs/dr2_13h60.moc', format='fits', overwrite=True) fig = plt.figure(figsize=(15, 10)) # Define a astropy WCS easily with World2ScreenMPL(fig, fov=360 * u.deg, center=ac.SkyCoord(180, 0, unit='deg', frame='icrs'), coordsys="icrs", rotation=ac.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. m_13hr60.fill(ax=ax, wcs=wcs, alpha=0.75, fill=True, color="C0", label='60 strip') m_13hr60.border(ax=ax, wcs=wcs, alpha=0.5, color="black") m_13hr45.fill(ax=ax, wcs=wcs, alpha=0.75,
def get_yse_pointings_base(field_name, snid): sf = SurveyField.objects.filter(field_id=field_name)[0] sc = cd.SkyCoord(sf.ra_cen, sf.dec_cen, unit=u.deg) transient = Transient.objects.filter(name=snid)[0] sc_transient = cd.SkyCoord(transient.ra, transient.dec, unit=u.deg) d = sc.dec.deg * np.pi / 180 #self.coord.dec.radian width_corr = 1.55 / np.abs(np.cos(d)) # Define the tile offsets: ra_offset = cd.Angle(width_corr, unit=u.deg) dec_offset = cd.Angle(3.1 / 2., unit=u.deg) # 6 pointings, spaced in usual way PS_SW = cd.SkyCoord(sc.ra - ra_offset, sc.dec - dec_offset) PS_NW = cd.SkyCoord(sc.ra - ra_offset, sc.dec + dec_offset) PS_SE = cd.SkyCoord(sc.ra + ra_offset, sc.dec - dec_offset) PS_NE = cd.SkyCoord(sc.ra + ra_offset, sc.dec + dec_offset) # last two are further over in RA PS_SE2 = cd.SkyCoord(sc.ra + ra_offset * 3, sc.dec - dec_offset) PS_NE2 = cd.SkyCoord(sc.ra + ra_offset * 3, sc.dec + dec_offset) #import pdb; pdb.set_trace() # try to move a field up/down to accommodate SN suggested_pointings = [] suggested_pointing_names = [] ra_adj, dec_adj = None, 0 sep_list = [] for pointing in [PS_SW, PS_NW, PS_SE, PS_NE, PS_SE2, PS_NE2]: sep_list += [sc_transient.separation(pointing).deg] sep_list = np.array(sep_list) for pointing, name, pointing_name, sep in zip( [PS_SW, PS_NW, PS_SE, PS_NE, PS_SE2, PS_NE2], ['PS_SW', 'PS_NW', 'PS_SE', 'PS_NE', 'PS_SE2', 'PS_NE2'], ['A', 'B', 'C', 'D', 'E', 'F'], sep_list): #print(sep) #if abs(sep - np.min(sep_list)) > 1e-4: # suggested_pointings += [pointing] # suggested_pointing_names += [field_name.replace('ZTF.','')+'.%s'%pointing_name] # continue #if sep < 1.55: dra, ddec = sc_transient.spherical_offsets_to(pointing) print(dra.deg, ddec.deg) if abs(dra.deg) < 1.45 and abs(ddec.deg) < 1.45: if sep > 0.4: # pointing is good as is suggested_pointings += [pointing] suggested_pointing_names += [ field_name.replace('ZTF.', '') + '.%s' % pointing_name ] continue else: # can we move up or down to put this at a # reasonable radius from center? #ra_offset = (sc_transient.ra.deg-pointing.ra.deg)/np.abs(np.cos(sc_transient.dec.deg)) #dec_offset = np.sqrt(0.75**2.-ra_offset**2.) if 'PS_N' in name: dec_offset = 0.75 - ddec.deg new_pointing = cd.SkyCoord(pointing.ra.deg, pointing.dec.deg + dec_offset, unit=u.deg) if 'PS_S' in name: dec_offset = 0.75 + ddec.deg new_pointing = cd.SkyCoord(pointing.ra.deg, pointing.dec.deg - dec_offset, unit=u.deg) # verify that this worked if sc_transient.separation( new_pointing).deg > 0.4 and sc_transient.separation( new_pointing).deg < 1: suggested_pointings += [new_pointing] suggested_pointing_names += [ field_name.replace('ZTF.', '') + '.%s' % pointing_name ] else: raise Http404('Something went wrong!') #import pdb; pdb.set_trace() elif abs(dra.deg) > 1.45 and abs(dra.deg) < 1.55: # really don't want SNe right near the edge ra_offset = dra.deg - 1.35 #import pdb; pdb.set_trace() if ra_adj is None: if dra.deg > 1.45: ra_adj = -0.4 elif dra.deg < -1.45: ra_adj = 0.4 suggested_pointings += [pointing] suggested_pointing_names += [ field_name.replace('ZTF.', '') + '.%s' % pointing_name ] elif abs(ddec.deg) > 1.45 and abs(ddec.deg) < 1.55: ra_offset = (sc_transient.ra.deg - pointing.ra.deg) / np.abs( np.cos(sc_transient.dec.deg)) dec_offset = np.sqrt(0.75**2. - ra_offset**2.) if 'PS_N' in name: new_pointing = cd.SkyCoord(pointing.ra.deg, pointing.dec.deg + dec_offset, unit=u.deg) if 'PS_S' in name: new_pointing = cd.SkyCoord(pointing.ra.deg, pointing.dec.deg - dec_offset, unit=u.deg) # verify that this worked if sc_transient.separation( new_pointing).deg > 0.4 and sc_transient.separation( new_pointing).deg < 1: suggested_pointings += [new_pointing] suggested_pointing_names += [ field_name.replace('ZTF.', '') + '.%s' % pointing_name ] else: raise Http404('Something went wrong!') else: suggested_pointings += [pointing] suggested_pointing_names += [ field_name.replace('ZTF.', '') + '.%s' % pointing_name ] #print('hi',sep,dra.deg,ddec.deg) if ((abs(dra.deg) < 1.55 and abs(ddec.deg) < 1.55) or abs(sep - np.min(sep_list)) < 1e-4) \ and (abs(dra.deg) > 1.05 or abs(ddec.deg) > 1.05): # four corners at +0.75,+0.75 deg, +0.75,-0.75, etc etc d = pointing.dec.deg * np.pi / 180 #self.coord.dec.radian width_corr = 0.75 / np.abs(np.cos(d)) ra_offset = cd.Angle(width_corr, unit=u.deg) dec_offset = cd.Angle(0.75, unit=u.deg) # 6 pointings, spaced in usual way SW = cd.SkyCoord(sc_transient.ra - ra_offset, sc_transient.dec - dec_offset) NW = cd.SkyCoord(sc_transient.ra - ra_offset, sc_transient.dec + dec_offset) SE = cd.SkyCoord(sc_transient.ra + ra_offset, sc_transient.dec - dec_offset) NE = cd.SkyCoord(sc_transient.ra + ra_offset, sc_transient.dec + dec_offset) sep1 = pointing.separation(SW).deg sep2 = pointing.separation(NW).deg sep3 = pointing.separation(SE).deg sep4 = pointing.separation(NE).deg if sep1 == np.min([sep1, sep2, sep3, sep4]): ddra, dddec = pointing.spherical_offsets_to(SW) elif sep2 == np.min([sep1, sep2, sep3, sep4]): ddra, dddec = pointing.spherical_offsets_to(NW) elif sep3 == np.min([sep1, sep2, sep3, sep4]): ddra, dddec = pointing.spherical_offsets_to(SE) elif sep4 == np.min([sep1, sep2, sep3, sep4]): ddra, dddec = pointing.spherical_offsets_to(NE) ra_adj, dec_adj = ddra.deg, dddec.deg #import pdb; pdb.set_trace() print(ddec.deg) # if necessary, move all fields right or left # to accomodate SN if ra_adj: new_suggested_pointings = [] new_suggested_pointing_names = [] for p, n in zip(suggested_pointings, ['A', 'B', 'C', 'D', 'E', 'F']): new_suggested_pointings += [ cd.SkyCoord(p.ra.deg + ra_adj, p.dec.deg + dec_adj, unit=u.deg) ] new_suggested_pointing_names += [ field_name.replace('ZTF.', '') + '.%s' % n ] suggested_pointings = new_suggested_pointings suggested_pointing_names = new_suggested_pointing_names # if necessary, move all fields up or down # to accomodate SN #if dec_adj: # new_suggested_pointings = [] # new_suggested_pointing_names = [] # for p,n in zip(suggested_pointings,['A','B','C','D','E','F']): # new_suggested_pointings += [cd.SkyCoord(p.ra.deg,p.dec.deg_dec_adj,unit=u.deg)] # new_suggested_pointing_names += [field_name.replace('ZTF.','')+'.%s'%n] # suggested_pointings = new_suggested_pointings # suggested_pointing_names = new_suggested_pointing_names # check that new pointings don't overlap # with any existing pointings for p in suggested_pointings: d = p.dec.deg * np.pi / 180 width_corr = 3.1 / np.abs(np.cos(d)) # Define the tile offsets: ra_offset = cd.Angle(width_corr / 2., unit=u.deg) dec_offset = cd.Angle(3.1 / 2., unit=u.deg) sf_test = SurveyField.objects.filter(~Q(obs_group__name='ZTF')).\ filter((Q(ra_cen__gt = p.ra.deg-ra_offset.degree) & Q(ra_cen__lt = p.ra.deg+ra_offset.degree) & Q(dec_cen__gt = p.dec.deg-dec_offset.degree) & Q(dec_cen__lt = p.dec.deg+dec_offset.degree))) if len(sf_test): raise Http404('Suggested pointing overlaps with field %s' % sf_test[0].field_id) # check that one pointings is at the right position good_pointing_exists = False for p in suggested_pointings: dra, ddec = sc_transient.spherical_offsets_to(p) if np.abs(dra.deg) < 1.45 and np.abs(ddec.deg) < 1.45: good_pointing_exists = True if not good_pointing_exists: raise Http404('can\'t find the right pointings for this transient') # generate a plot of what the pointings look like? # this can be done in template # formatting stuff pointings_for_html = () for p, n in zip(suggested_pointings, suggested_pointing_names): dra, ddec = sc_transient.spherical_offsets_to(p) pointings_for_html += ((n, GetSexigesimalString(p.ra.deg, p.dec.deg)[0], GetSexigesimalString(p.ra.deg, p.dec.deg)[1], '%.2f' % dra.deg, '%.2f' % ddec.deg), ) return suggested_pointings, suggested_pointing_names, pointings_for_html, transient, sf
def get_roll(icrs, R, x0=0.): res = minimize(obj_func, x0=x0, args=(icrs, R)) return coord.Angle(res.x[0] * u.deg)
def whirc_findstars(obj_id): from astropy.convolution import convolve, Tophat2DKernel, AiryDisk2DKernel, MexicanHat2DKernel, Box2DKernel import astropy.coordinates as coord import astropy.units as u import scipy.ndimage as snd from scipy.ndimage import interpolation as interp import pyfits import numpy as np import pdb import pickle #READ IN DATA AND OBSERVING LOG #Read in data from "whirc_info.dat" (the observation log) im1 = open('whirc_info.dat', 'r') data1 = im1.readlines() im1.close() filename = [] dateobs = [] objname = [] imgtype = [] ra = [] dec = [] exptime = [] usable = [] rotangle = [] raoffset = [] decoffset = [] for line in data1: p = line.split() filename.append(p[0]) dateobs.append(p[1]) objname.append(p[2]) imgtype.append(p[3]) ra.append(p[4]) dec.append(p[5]) exptime.append(p[6]) usable.append(p[7]) rotangle.append(p[8]) raoffset.append(p[9]) decoffset.append(p[10]) #Rewrite in a more convenient array with format array[line][element] alldata = [] for line in range(len(usable)): alldata.append([ filename[line], dateobs[line], objname[line], imgtype[line], ra[line], dec[line], exptime[line], usable[line], rotangle[line], raoffset[line], decoffset[line] ]) #Find junk files and good files: junk = [] good = [] for line in range(len(alldata)): if "no" in alldata[line][7]: junk.append(alldata[line]) if "yes" in alldata[line][7]: good.append(alldata[line]) #Find files related to obj_id in good files aobj_id = [] for line in range(len(good)): if str(obj_id) in good[line][2]: aobj_id.append(good[line]) print str(len(aobj_id)), "files for " + str(obj_id) # Change RA and Dec stuff to degrees for line in range(len(aobj_id)): raoff = aobj_id[line][9] decoff = aobj_id[line][10] roff = coord.Angle(raoff, unit=u.hour) doff = coord.Angle(decoff, unit=u.degree) raoff = (roff.degree) decoff = doff.degree if np.abs(raoff) > 1: raoff = -(360 - raoff) aobj_id[line][9] = raoff aobj_id[line][10] = decoff #print aobj_id[line][9], aobj_id[line][10] #Find files through J_filter aobj_idJ = [] for line in range(len(aobj_id)): if "J" in aobj_id[line][2]: aobj_idJ.append(aobj_id[line]) print str(len(aobj_idJ)), "J files for " + str(obj_id) #Find files through K_filter aobj_idK = [] for line in range(len(aobj_id)): if "K" in aobj_id[line][2]: aobj_idK.append(aobj_id[line]) print str(len(aobj_idK)), "K files for " + str(obj_id) #Find J files from Night 1 aobj_idJN1 = [] for line in range(len(aobj_idJ)): if "N1" in aobj_idJ[line][0]: aobj_idJN1.append(aobj_idJ[line]) print str(len(aobj_idJN1)), "J files in N1" #Fink K files from Night 1 aobj_idKN1 = [] for line in range(len(aobj_idK)): if "N1" in aobj_idK[line][0]: aobj_idKN1.append(aobj_idK[line]) print str(len(aobj_idKN1)), "K files in N1" #Find J files from Night 2 aobj_idJN2 = [] for line in range(len(aobj_idJ)): if "N2" in aobj_idJ[line][0]: aobj_idJN2.append(aobj_idJ[line]) print str(len(aobj_idJN2)), "J files in N2" #Find K files from Night 2 aobj_idKN2 = [] for line in range(len(aobj_idK)): if "N2" in aobj_idK[line][0]: aobj_idKN2.append(aobj_idK[line]) print str(len(aobj_idKN2)), "K files in N2" #Find J files from Night 3 aobj_idJN3 = [] for line in range(len(aobj_idJ)): if "N3" in aobj_idJ[line][0]: aobj_idJN3.append(aobj_idJ[line]) print str(len(aobj_idJN3)), "J files in N3" #Find K files from Night 3 aobj_idKN3 = [] for line in range(len(aobj_idK)): if "N3" in aobj_idK[line][0]: aobj_idKN3.append(aobj_idK[line]) print str(len(aobj_idKN3)), "K files in N3" #WRITE PATH TO J_FILES_N1 Jobj_idN1_locs = [] for line in range(len(aobj_idJN1)): Jobj_idN1_locs.append('Calibs/reduced/' + str(obj_id) + '_' + 'JN1_cos' + str(line + 1) + '.fits') #WRITE PATH TO K_FILES_N1 Kobj_idN1_locs = [] for line in range(len(aobj_idKN1)): Kobj_idN1_locs.append('Calibs/reduced/' + str(obj_id) + '_' + 'KN1_cos' + str(line + 1) + '.fits') #WRITE PATH OF J_FILES_N2 Jobj_idN2_locs = [] for line in range(len(aobj_idJN2)): Jobj_idN2_locs.append('Calibs/reduced/' + str(obj_id) + '_' + 'JN2_cos' + str(line + 1) + '.fits') #WRITE PATH OF K_FILES_N2 Kobj_idN2_locs = [] for line in range(len(aobj_idKN2)): Kobj_idN2_locs.append('Calibs/reduced/' + str(obj_id) + '_' + 'KN2_cos' + str(line + 1) + '.fits') #WRITE PATH OF J_FILES_N3 Jobj_idN3_locs = [] for line in range(len(aobj_idJN3)): Jobj_idN3_locs.append('Calibs/reduced/' + str(obj_id) + '_' + 'JN3_cos' + str(line + 1) + '.fits') #WRITE PATH OF K_FILES_N3 Kobj_idN3_locs = [] for line in range(len(aobj_idKN3)): Kobj_idN3_locs.append('Calibs/reduced/' + str(obj_id) + '_' + 'KN3_cos' + str(line + 1) + '.fits') #TELL FINDSTARS HOW MANY STARS TO LOOK FOR: if obj_id == 55500: num_star = 13 elif obj_id == 77610: num_star = 6 elif obj_id == 54655: num_star = 10 elif obj_id == 119887: num_star = 10 elif obj_id == 67565: num_star = 11 elif obj_id == 36363: num_star = 10 elif obj_id == 120659: num_star = 11 elif obj_id == 37836: #But this is useless anyway... num_star = 7 elif obj_id == 51306: num_star = 8 elif obj_id == 122277: num_star = 8 elif obj_id == 20700: num_star = 16 elif obj_id == 57476: num_star = 6 elif obj_id == 38329: num_star = 7 elif obj_id == 121130: num_star = 13 else: num_star = 12 #J_filter, Night 1 if len(aobj_idJN1) > 0: print "processing J_N1..." tophat = Tophat2DKernel(5) #Radius can be changed to increase sharpess star_coords = [] centrals = [] #### MORE TESTING #### # FIND REFERENCE IMAGE centrals = [] for line in range(len(aobj_idJN1)): if aobj_idJN1[line][9] == 0 and aobj_idJN1[line][10] == 0: centrals.append(line) if len(centrals) == 0: centrals.append( 4 ) #in case no object has zero offset, align all objects to fifth object print 'centrals: ', centrals #### /MORE TESTING ###### for line in range(len(aobj_idJN1)): if line in centrals: img = pyfits.getdata(Jobj_idN1_locs[line]) #Smoothing smooth = convolve(img, tophat) threshold = np.mean(smooth) + 20 # bit arbitrary, but it works print line, threshold labels, num = snd.label(smooth > threshold, np.ones((3, 3))) #find centroids of peaks centers = snd.center_of_mass(smooth, labels, range(1, num + 1)) #print centers cens = [] badcens = [] for line in range(len(centers)): if centers[line][0] > 2047 or centers[line][1] > 2047: badcens.append(line) if len(badcens) > 0: for line in range(len(badcens) - 1, -1, -1): #looping backwards centers.pop(badcens[line]) for line in range(len(centers)): cens.append( np.array([[centers[line][0], centers[line][1]], smooth[centers[line][0], centers[line][1]]])) badpix = [] for line in range(len(cens)): if 256 < cens[line][0][0] < 276 and 326 < cens[line][0][ 1] < 346: badpix.append(line) if len(badpix) > 0: del cens[badpix[0]] cens = np.array(cens) #sort in descending order according to 3rd column cens = cens[cens[:, 1].argsort()[::-1]] #only include central stars (that will be in all exposures) good_cens = [] for line in range(len(cens)): #remove edges #if 50 < cens[line][0][0] < 2000 and 50 < cens[line][0][1] < 2000: good_cens.append(cens[line]) cens = np.array(good_cens)[0:num_star] #remove fainter stars cens = cens[:, 0] print cens cens = cens.tolist() star_coords.append(cens) print np.shape(star_coords) #if line =/= center else: img = pyfits.getdata(Jobj_idN1_locs[line]) #Smoothing smooth = convolve(img, tophat) threshold = np.mean(smooth) + 20 # bit arbitrary, but it works print line, threshold labels, num = snd.label(smooth > threshold, np.ones((3, 3))) #find centroids of peaks centers = snd.center_of_mass(smooth, labels, range(1, num + 1)) #print centers cens = [] badcens = [] for line in range(len(centers)): if centers[line][0] > 2047 or centers[line][1] > 2047: badcens.append(line) if len(badcens) > 0: for line in range(len(badcens) - 1, -1, -1): #looping backwards centers.pop(badcens[line]) for line in range(len(centers)): cens.append( np.array([[centers[line][0], centers[line][1]], smooth[centers[line][0], centers[line][1]]])) badpix = [] for line in range(len(cens)): if 256 < cens[line][0][0] < 276 and 326 < cens[line][0][ 1] < 346: badpix.append(line) if len(badpix) > 0: del cens[badpix[0]] cens = np.array(cens) #sort in descending order according to 3rd column cens = cens[cens[:, 1].argsort()[::-1]] #only include central stars (that will be in all exposures) good_cens = [] for line in range(len(cens)): #remove edges #if 320 < cens[line][0][0] < 1740 and 320 < cens[line][0][1] < 1740: good_cens.append(cens[line]) cens = np.array(good_cens)[0:num_star] #remove fainter stars cens = cens[:, 0] print cens cens = cens.tolist() star_coords.append(cens) print np.shape(star_coords) #print star_coords #print star_coords #if aobj_idJN1[line][9] == 0 and aobj_idJN1[line][10] == 0: # print line # centrals.append(line) pickle.dump( star_coords, open("Calibs/starfields/star_coords" + str(obj_id) + 'JN1.p', "wb")) #pdb.set_trace() #centrals.append(4) #in case no object has zero offset, align all objects to fifth object #center = centrals[0] #print star_coords #xy_offsets_JN1 = [] #for line in range(len(star_coords)): # offset = [star_coords[center][0][0] - star_coords[line][0][0], star_coords[center][0][1] - star_coords[line][0][1]] # xy_offsets_JN1.append(offset) #print xy_offsets_JN1 #The next bit is currently not useful but might be later. ''' print "reading in JN1..." if len(aobj_idJN1) > 0: shifteds = [] for line in range(len(aobj_idJN1)): sci = pyfits.getdata(Jobj_idN1_locs[line]) shifted = interp.shift(sci, xy_offsets_JN1[line] ,order = 0) shifted = shifted - np.median(shifted) shifteds.append(shifted) combined = sum(shifteds) median = np.median(shifteds, axis = 0) file = pyfits.PrimaryHDU(combined) mfile = pyfits.PrimaryHDU(median) file.writeto('Calibs/shifted/'+str(obj_id)+'_JN1_combined.fits',clobber=True) mfile.writeto('Calibs/shifted/'+str(obj_id)+'_JN1_median.fits',clobber=True) #K_filter, Night 1 print "processing K_N1..." tophat = Tophat2DKernel(5) #Radius can be changed to increase sharpess star_coords = [] centrals = [] for line in range(len(aobj_idKN1)): img = pyfits.getdata(Kobj_idN1_locs[line]) #Smoothing smooth = convolve(img, tophat) threshold = 0.99*np.max(smooth) # bit arbitrary, but it works print threshold labels, num = snd.label(smooth > threshold, np.ones((3,3))) #find centroids of peaks centers = snd.center_of_mass(smooth, labels, range(1, num+1)) star_coords.append(centers) if aobj_idKN1[line][9] == 0 and aobj_idKN1[line][10] == 0: print line, centers centrals.append(line) centrals.append(4) #in case no object has zero offset, align all objects to fifth object center = centrals[0] print star_coords xy_offsets_KN1 = [] for line in range(len(star_coords)): offset = [star_coords[center][0][0] - star_coords[line][0][0], star_coords[center][0][1] - star_coords[line][0][1]] xy_offsets_KN1.append(offset) print xy_offsets_KN1 print "reading in KN1..." if len(aobj_idKN1) > 0: shifteds = [] for line in range(len(aobj_idKN1)): sci = pyfits.getdata(Kobj_idN1_locs[line]) shifted = interp.shift(sci, xy_offsets_KN1[line] ,order = 0) shifted = shifted - np.median(shifted) shifteds.append(shifted) combined = sum(shifteds) median = np.median(shifteds, axis = 0) file = pyfits.PrimaryHDU(combined) mfile = pyfits.PrimaryHDU(median) file.writeto('Calibs/shifted/'+str(obj_id)+'_KN1_combined.fits',clobber=True) mfile.writeto('Calibs/shifted/'+str(obj_id)+'_KN1_median.fits',clobber=True) #J_filter, Night 2 print "processing J_N2..." tophat = Tophat2DKernel(4) #Radius can be changed to increase sharpess star_coords = [] centrals = [] for line in range(len(aobj_idJN2)): img = pyfits.getdata(Jobj_idN2_locs[line]) #Smoothing smooth = convolve(img, tophat) threshold = 0.99*np.max(smooth) # bit arbitrary, but it works print threshold labels, num = snd.label(smooth > threshold, np.ones((3,3))) #find centroids of peaks centers = snd.center_of_mass(smooth, labels, range(1, num+1)) star_coords.append(centers) if aobj_idJN2[line][9] == 0 and aobj_idJN2[line][10] == 0: print line centrals.append(line) centrals.append(4) #in case no object has zero offset, align all objects to fifth object center = centrals[0] print star_coords xy_offsets_JN2 = [] for line in range(len(star_coords)): offset = [star_coords[center][0][0] - star_coords[line][0][0], star_coords[center][0][1] - star_coords[line][0][1]] xy_offsets_JN2.append(offset) print xy_offsets_JN2 print "reading in JN2..." if len(aobj_idJN2) > 0: shifteds = [] for line in range(len(aobj_idJN2)): sci = pyfits.getdata(Jobj_idN2_locs[line]) shifted = interp.shift(sci, xy_offsets_JN2[line] ,order = 0) shifted = shifted - np.median(shifted) shifteds.append(shifted) combined = sum(shifteds) median = np.median(shifteds, axis = 0) file = pyfits.PrimaryHDU(combined) mfile = pyfits.PrimaryHDU(median) file.writeto('Calibs/shifted/'+str(obj_id)+'_JN2_combined.fits',clobber=True) mfile.writeto('Calibs/shifted/'+str(obj_id)+'_JN2_median.fits',clobber=True) #K_filter, Night 2 print "processing K_N2..." tophat = Tophat2DKernel(5) #Radius can be changed to increase sharpess star_coords = [] centrals = [] for line in range(len(aobj_idKN2)): img = pyfits.getdata(Kobj_idN2_locs[line]) #Smoothing smooth = convolve(img, tophat) threshold = 0.99*np.max(smooth) # bit arbitrary, but it works print threshold labels, num = snd.label(smooth > threshold, np.ones((3,3))) #find centroids of peaks centers = snd.center_of_mass(smooth, labels, range(1, num+1)) star_coords.append(centers) if aobj_idKN2[line][9] == 0 and aobj_idKN2[line][10] == 0: print line centrals.append(line) centrals.append(4) #in case no object has zero offset, align all objects to fifth object center = centrals[0] print star_coords xy_offsets_KN2 = [] for line in range(len(star_coords)): offset = [star_coords[center][0][0] - star_coords[line][0][0], star_coords[center][0][1] - star_coords[line][0][1]] xy_offsets_KN2.append(offset) print xy_offsets_KN2 print "reading in KN2..." if len(aobj_idKN2) > 0: shifteds = [] for line in range(len(aobj_idKN2)): sci = pyfits.getdata(Kobj_idN2_locs[line]) shifted = interp.shift(sci, xy_offsets_KN2[line] ,order = 0) shifted = shifted - np.median(shifted) shifteds.append(shifted) combined = sum(shifteds) median = np.median(shifteds, axis = 0) file = pyfits.PrimaryHDU(combined) mfile = pyfits.PrimaryHDU(median) file.writeto('Calibs/shifted/'+str(obj_id)+'_KN2_combined.fits',clobber=True) mfile.writeto('Calibs/shifted/'+str(obj_id)+'_KN2_median.fits',clobber=True) #J_filter, Night 3 print "processing J_N3..." tophat = Tophat2DKernel(5) #Radius can be changed to increase sharpess star_coords = [] centrals = [] for line in range(len(aobj_idJN3)): img = pyfits.getdata(Jobj_idN3_locs[line]) #Smoothing smooth = convolve(img, tophat) threshold = 0.99*np.max(smooth) # bit arbitrary, but it works print threshold labels, num = snd.label(smooth > threshold, np.ones((3,3))) #find centroids of peaks centers = snd.center_of_mass(smooth, labels, range(1, num+1)) print centers star_coords.append(centers) if aobj_idJN3[line][9] == 0 and aobj_idJN3[line][10] == 0: print line centrals.append(line) centrals.append(5) #in case no object has zero offset, align all objects to fifth object center = centrals[0] print star_coords xy_offsets_JN3 = [] for line in range(len(star_coords)): offset = [star_coords[center][0][0] - star_coords[line][0][0], star_coords[center][0][1] - star_coords[line][0][1]] xy_offsets_JN3.append(offset) print xy_offsets_JN3 print "reading in JN3..." if len(aobj_idJN3) > 0: shifteds = [] for line in range(len(aobj_idJN3)): sci = pyfits.getdata(Jobj_idN3_locs[line]) shifted = interp.shift(sci, xy_offsets_JN3[line] ,order = 0) shifted = shifted - np.median(shifted) shifteds.append(shifted) combined = sum(shifteds) median = np.median(shifteds, axis = 0) file = pyfits.PrimaryHDU(combined) mfile = pyfits.PrimaryHDU(median) file.writeto('Calibs/shifted/'+str(obj_id)+'_JN3_combined.fits',clobber=True) mfile.writeto('Calibs/shifted/'+str(obj_id)+'_JN3_median.fits',clobber=True) #K_filter, Night 3 print "processing K_N3..." tophat = Tophat2DKernel(5) #Radius can be changed to increase sharpess star_coords = [] centrals = [] for line in range(len(aobj_idKN3)): img = pyfits.getdata(Kobj_idN3_locs[line]) #Smoothing smooth = convolve(img, tophat) threshold = 0.99*np.max(smooth) # bit arbitrary, but it works print threshold labels, num = snd.label(smooth > threshold, np.ones((3,3))) #find centroids of peaks centers = snd.center_of_mass(smooth, labels, range(1, num+1)) star_coords.append(centers) if aobj_idKN3[line][9] == 0 and aobj_idKN3[line][10] == 0: print line centrals.append(line) centrals.append(4) #in case no object has zero offset, align all objects to fifth object center = centrals[0] print star_coords xy_offsets_KN3 = [] for line in range(len(star_coords)): offset = [star_coords[center][0][0] - star_coords[line][0][0], star_coords[center][0][1] - star_coords[line][0][1]] xy_offsets_KN3.append(offset) print xy_offsets_KN3 print "reading in KN3..." if len(aobj_idKN3) > 0: shifteds = [] for line in range(len(aobj_idKN3)): sci = pyfits.getdata(Kobj_idN3_locs[line]) shifted = interp.shift(sci, xy_offsets_KN3[line] ,order = 0) shifted = shifted - np.median(shifted) shifteds.append(shifted) combined = sum(shifteds) median = np.median(shifteds, axis = 0) file = pyfits.PrimaryHDU(combined) mfile = pyfits.PrimaryHDU(median) file.writeto('Calibs/shifted/'+str(obj_id)+'_KN3_combined.fits', clobber=True) mfile.writeto('Calibs/shifted/'+str(obj_id)+'_KN3_median.fits', clobber=True) ''' #Here I continue the useful code: #K_filter, Night 1 if len(aobj_idKN1) > 0: print "processing K_N1..." if obj_id == 35979: num_star = 5 tophat = Tophat2DKernel(5) #Radius can be changed to increase sharpess star_coords = [] centrals = [] for line in range(len(aobj_idKN1)): img = pyfits.getdata(Kobj_idN1_locs[line]) #Smoothing smooth = convolve(img, tophat) threshold = np.mean(smooth) + 20 # bit arbitrary, but it works print line, threshold labels, num = snd.label(smooth > threshold, np.ones((3, 3))) #find centroids of peaks centers = snd.center_of_mass(smooth, labels, range(1, num + 1)) #print centers cens = [] badcens = [] for line in range(len(centers)): if centers[line][0] > 2047 or centers[line][1] > 2047: badcens.append(line) if len(badcens) > 0: for line in range(len(badcens) - 1, -1, -1): #looping backwards centers.pop(badcens[line]) for line in range(len(centers)): cens.append( np.array([[centers[line][0], centers[line][1]], smooth[centers[line][0], centers[line][1]]])) cens = np.array(cens) #sort in descending order according to 3rd column cens = cens[cens[:, 1].argsort()[::-1]] #only include central stars (that will be in all exposures) good_cens = [] for line in range(len(cens)): if 100 < cens[line][0][0] < 1950 and 100 < cens[line][0][ 1] < 1950: good_cens.append(cens[line]) cens = np.array(good_cens)[0:num_star] #remove fainter stars cens = cens[:, 0] print "stars", cens cens = cens.tolist() star_coords.append(cens) print np.shape(star_coords) #print star_coords #if aobj_idJN1[line][9] == 0 and aobj_idJN1[line][10] == 0: # print line # centrals.append(line) pickle.dump( star_coords, open("Calibs/starfields/star_coords" + str(obj_id) + 'KN1.p', "wb")) #J_filter, Night 2 if len(aobj_idJN2) > 0: print "processing J_N2..." tophat = Tophat2DKernel(5) #Radius can be changed to increase sharpess star_coords = [] centrals = [] for line in range(len(aobj_idJN2)): img = pyfits.getdata(Jobj_idN2_locs[line]) #Smoothing smooth = convolve(img, tophat) threshold = np.mean(smooth) + 20 # bit arbitrary, but it works print line, threshold labels, num = snd.label(smooth > threshold, np.ones((3, 3))) #find centroids of peaks centers = snd.center_of_mass(smooth, labels, range(1, num + 1)) #print centers cens = [] badcens = [] for line in range(len(centers)): if centers[line][0] > 2047 or centers[line][1] > 2047: badcens.append(line) if len(badcens) > 0: if len(badcens) > 0: for line in range(len(badcens) - 1, -1, -1): #looping backwards centers.pop(badcens[line]) for line in range(len(centers)): cens.append( np.array([[centers[line][0], centers[line][1]], smooth[centers[line][0], centers[line][1]]])) cens = np.array(cens) #sort in descending order according to 3rd column cens = cens[cens[:, 1].argsort()[::-1]] #only include central stars (that will be in all exposures) good_cens = [] for line in range(len(cens)): if 100 < cens[line][0][0] < 1950 and 100 < cens[line][0][ 1] < 1950: good_cens.append(cens[line]) cens = np.array(good_cens)[0:num_star] #remove fainter stars cens = cens[:, 0] print 'stars: ', cens cens = cens.tolist() star_coords.append(cens) print np.shape(star_coords) #print star_coords #print star_coords #if aobj_idJN1[line][9] == 0 and aobj_idJN1[line][10] == 0: # print line # centrals.append(line) pickle.dump( star_coords, open("Calibs/starfields/star_coords" + str(obj_id) + 'JN2.p', "wb")) #K_filter, Night 2 if len(aobj_idKN2) > 0: print "processing K_N2..." if obj_id == 55500: num_star = 7 if obj_id == 35979: num_star = 5 if obj_id == 77610: num_star = 4 tophat = Tophat2DKernel(5) #Radius can be changed to increase sharpess star_coords = [] centrals = [] for line in range(len(aobj_idKN2)): img = pyfits.getdata(Kobj_idN2_locs[line]) #Smoothing smooth = convolve(img, tophat) threshold = np.mean(smooth) + 20 # bit arbitrary, but it works print line, threshold labels, num = snd.label(smooth > threshold, np.ones((3, 3))) #find centroids of peaks centers = snd.center_of_mass(smooth, labels, range(1, num + 1)) #print centers cens = [] badcens = [] for line in range(len(centers)): if centers[line][0] > 2047 or centers[line][1] > 2047: badcens.append(line) if len(badcens) > 0: if len(badcens) > 0: for line in range(len(badcens) - 1, -1, -1): #looping backwards centers.pop(badcens[line]) for line in range(len(centers)): cens.append( np.array([[centers[line][0], centers[line][1]], smooth[centers[line][0], centers[line][1]]])) cens = np.array(cens) #sort in descending order according to 3rd column cens = cens[cens[:, 1].argsort()[::-1]] #only include central stars (that will be in all exposures) good_cens = [] for line in range(len(cens)): if 100 < cens[line][0][0] < 1950 and 100 < cens[line][0][ 1] < 1950: good_cens.append(cens[line]) cens = np.array(good_cens)[0:num_star] #remove fainter stars cens = cens[:, 0] print 'stars: ', cens cens = cens.tolist() star_coords.append(cens) print np.shape(star_coords) #print star_coords #print star_coords #if aobj_idJN1[line][9] == 0 and aobj_idJN1[line][10] == 0: # print line # centrals.append(line) pickle.dump( star_coords, open("Calibs/starfields/star_coords" + str(obj_id) + 'KN2.p', "wb")) #J_filter, Night 3 if len(aobj_idJN3) > 0: print "processing J_N3..." tophat = Tophat2DKernel(5) #Radius can be changed to increase sharpess star_coords = [] centrals = [] for line in range(len(aobj_idJN3)): img = pyfits.getdata(Jobj_idN3_locs[line]) #Smoothing smooth = convolve(img, tophat) threshold = np.mean(smooth) + 20 # bit arbitrary, but it works print line, threshold labels, num = snd.label(smooth > threshold, np.ones((3, 3))) #find centroids of peaks centers = snd.center_of_mass(smooth, labels, range(1, num + 1)) #print centers cens = [] badcens = [] for line in range(len(centers)): if centers[line][0] > 2047 or centers[line][1] > 2047: badcens.append(line) if len(badcens) > 0: if len(badcens) > 0: for line in range(len(badcens) - 1, -1, -1): #looping backwards centers.pop(badcens[line]) for line in range(len(centers)): cens.append( np.array([[centers[line][0], centers[line][1]], smooth[centers[line][0], centers[line][1]]])) cens = np.array(cens) #sort in descending order according to 3rd column cens = cens[cens[:, 1].argsort()[::-1]] #only include central stars (that will be in all exposures) good_cens = [] for line in range(len(cens)): if 100 < cens[line][0][0] < 1950 and 100 < cens[line][0][ 1] < 1950: good_cens.append(cens[line]) cens = np.array(good_cens)[0:num_star] #remove fainter stars cens = cens[:, 0] print 'stars: ', cens cens = cens.tolist() star_coords.append(cens) print np.shape(star_coords) #print star_coords #print star_coords #if aobj_idJN1[line][9] == 0 and aobj_idJN1[line][10] == 0: # print line # centrals.append(line) pickle.dump( star_coords, open("Calibs/starfields/star_coords" + str(obj_id) + 'JN3.p', "wb")) #K_filter, Night 3 if len(aobj_idKN3) > 0: print "processing K_N3..." tophat = Tophat2DKernel(5) #Radius can be changed to increase sharpess star_coords = [] centrals = [] for line in range(len(aobj_idKN3)): img = pyfits.getdata(Kobj_idN3_locs[line]) #Smoothing smooth = convolve(img, tophat) threshold = np.mean(smooth) + 20 # bit arbitrary, but it works print line, threshold labels, num = snd.label(smooth > threshold, np.ones((3, 3))) #find centroids of peaks centers = snd.center_of_mass(smooth, labels, range(1, num + 1)) #print centers cens = [] badcens = [] for line in range(len(centers)): if centers[line][0] > 2047 or centers[line][0] < 0 or centers[ line][1] < 0 or centers[line][1] > 2047: badcens.append(line) if len(badcens) > 0: if len(badcens) > 0: for line in range(len(badcens) - 1, -1, -1): #looping backwards centers.pop(badcens[line]) for line in range(len(centers)): cens.append( np.array([[centers[line][0], centers[line][1]], smooth[centers[line][0], centers[line][1]]])) cens = np.array(cens) #sort in descending order according to 3rd column cens = cens[cens[:, 1].argsort()[::-1]] #only include central stars (that will be in all exposures) good_cens = [] for line in range(len(cens)): if 100 < cens[line][0][0] < 1950 and 100 < cens[line][0][ 1] < 1950: good_cens.append(cens[line]) cens = np.array(good_cens)[0:num_star] #remove fainter stars cens = cens[:, 0] print 'stars: ', cens cens = cens.tolist() star_coords.append(cens) print np.shape(star_coords) #print star_coords #print star_coords #if aobj_idJN1[line][9] == 0 and aobj_idJN1[line][10] == 0: # print line # centrals.append(line) pickle.dump( star_coords, open("Calibs/starfields/star_coords" + str(obj_id) + 'KN3.p', "wb"))
waveBand = [] HWPang = [] binType = [] expTime = [] night = [] fileCounter = 0 percentage = 0 #Loop through each file in the fileList variable for file in fileList: # Read in the image tmpImg = AstroImage(file) # Grab the RA and Dec from the header # Parse the pointing for this file tmpRA = coord.Angle(tmpImg.header['TELRA'], unit=u.hour) tmpDec = coord.Angle(tmpImg.header['TELDEC'], unit=u.degree) telRA.append(tmpRA.degree) telDec.append(tmpDec.degree) # Classify each file type and binning tmpName = tmpImg.header['OBJECT'] if len(tmpName) < 1: tmpName = 'blank' name.append(tmpName) # Parse the HWP number tmpHWP = round(100*tmpImg.header['HWP_ANG']) HWPdiff = np.abs(HWPlist - tmpHWP) tmpHWP = (np.where(HWPdiff == np.min(HWPdiff)))[0][0] + 1 HWPang.append(tmpHWP)
def test_mean_anomaly(self): """tests the geometric mean anomaly calculation""" M = self.pos.get_mean_anomaly() self.assertLess(np.abs(M - c.Angle(-2241.00603 * u.deg)), 0.5e-5 * u.deg)
import astropy.coordinates as coord import astropy.units as u from mpl_toolkits.mplot3d import Axes3D #Ler a tabela de dados com RA e Dec de cada RRLyr AB # a estrela OGLE-LMC-RRLYR-15485 (posição 11013 no arquivo) não possui DatabaseNumber, estava dando problema para ler os dados. Coloquei manualmente o valor 0000 from astropy.io import ascii tbl = ascii.read( '/home/glauffer/Dropbox/FURG/final_project/data/identification_onlyRRAB.dat', guess=False, Reader=ascii.Tab, header_start=0, data_start=1) #Converter RA de horas para graus ra = coord.Angle(tbl['RA'], unit=u.hour) #Converter Dec em graus dec = coord.Angle(tbl['Dec'], unit=u.degree) #Converter RA e Dec em X e Y de acordo com o paper do Pejcha (2009) x = (ra.degree - 80.35) * np.cos(dec.degree) y = dec.degree + 69.68 ra_dec = np.around(np.column_stack( (x, y)), decimals=1) #arrendondamento para o primeiro decimal apos a virgula #Ler os dados do extinction map ext_map = np.array( np.loadtxt( '/home/glauffer/Dropbox/FURG/final_project/data/extinction_map/lmcextmap.dat',
def test_eoc(self): """tests the equation of center calculations""" eoc = self.pos.get_eoc() self.assertLess(np.abs(eoc - c.Angle(-1.89732 * u.deg)), 0.5e-5 * u.deg)
def _args_to_payload(self, coordinates=None, radius=2. * u.arcsec, fields=None, spectro=False, plate=None, mjd=None, fiberID=None, run=None, rerun=301, camcol=None, field=None, photoobj_fields=None, specobj_fields=None, field_help=None, obj_names=None): """ Construct the SQL query from the arguments. Parameters ---------- coordinates : str or `astropy.coordinates` object or list of coordinates or `~astropy.table.Column` or coordinates The target around which to search. It may be specified as a string in which case it is resolved using online services or as the appropriate `astropy.coordinates` object. ICRS coordinates may also be entered as strings as specified in the `astropy.coordinates` module. radius : str or `~astropy.units.Quantity` object, optional The string must be parsable by `~astropy.coordinates.Angle`. The appropriate `~astropy.units.Quantity` object from `astropy.units` may also be used. Defaults to 2 arcsec. fields : list, optional SDSS PhotoObj or SpecObj quantities to return. If None, defaults to quantities required to find corresponding spectra and images of matched objects (e.g. plate, fiberID, mjd, etc.). spectro : bool, optional Look for spectroscopic match in addition to photometric match? If True, objects will only count as a match if photometry *and* spectroscopy exist. If False, will look for photometric matches only. If ``spectro`` is True, it is possible to let coordinates undefined and set at least one of ``plate``, ``mjd`` or ``fiberID`` to search using these fields. plate : integer, optional Plate number. mjd : integer, optional Modified Julian Date indicating the date a given piece of SDSS data was taken. fiberID : integer, optional Fiber number. run : integer, optional Length of a strip observed in a single continuous image observing scan. rerun : integer, optional Reprocessing of an imaging run. Defaults to 301 which is the most recent rerun. camcol : integer, optional Output of one camera column of CCDs. field : integer, optional Part of a camcol of size 2048 by 1489 pixels. photoobj_fields: float, optional PhotoObj quantities to return. If photoobj_fields is None and specobj_fields is None then the value of fields is used specobj_fields: float, optional SpecObj quantities to return. If photoobj_fields is None and specobj_fields is None then the value of fields is used field_help: str or bool, optional Field name to check whether it is a valid PhotoObjAll or SpecObjAll field name. If `True` or it is an invalid field name all the valid field names are returned as a dict. obj_names : str, or list or `~astropy.table.Column`, optional Target names. If given, every coordinate should have a corresponding name, and it gets repeated in the query result Returns ------- request_payload : dict """ if field_help: ret = 0 if field_help in photoobj_all: print("{0} is a valid 'photoobj_field'".format(field_help)) ret += 1 elif field_help in specobj_all: print("{0} is a valid 'specobj_field'".format(field_help)) ret += 1 if ret > 0: return else: if field_help is not True: warnings.warn("{0} isn't a valid 'photobj_field' or " "'specobj_field' field, valid fields are" "returned.".format(field_help)) return { 'photoobj_all': photoobj_all, 'specobj_all': specobj_all } # Construct SQL query q_select = 'SELECT DISTINCT ' q_select_field = [] if photoobj_fields is None and specobj_fields is None: # Fields to return if fields is None: photoobj_fields = photoobj_defs if spectro: specobj_fields = specobj_defs else: for sql_field in fields: if sql_field.lower() in photoobj_all: q_select_field.append('p.{0}'.format(sql_field)) elif sql_field.lower() in specobj_all: q_select_field.append('s.{0}'.format(sql_field)) if photoobj_fields is not None: for sql_field in photoobj_fields: q_select_field.append('p.{0}'.format(sql_field)) if specobj_fields is not None: for sql_field in specobj_fields: q_select_field.append('s.{0}'.format(sql_field)) q_select += ', '.join(q_select_field) q_from = 'FROM PhotoObjAll AS p ' if spectro: q_join = 'JOIN SpecObjAll s ON p.objID = s.bestObjID ' else: q_join = '' q_where = 'WHERE ' if coordinates is not None: if (not isinstance(coordinates, list) and not isinstance(coordinates, Column) and not (isinstance(coordinates, commons.CoordClasses) and not coordinates.isscalar)): coordinates = [coordinates] for n, target in enumerate(coordinates): # Query for a region target = commons.parse_coordinates(target).transform_to('fk5') ra = target.ra.degree dec = target.dec.degree dr = coord.Angle(radius).to('degree').value if n > 0: q_where += ' or ' q_where += ('((p.ra between %g and %g) and ' '(p.dec between %g and %g))' % (ra - dr, ra + dr, dec - dr, dec + dr)) elif spectro: # Spectra: query for specified plate, mjd, fiberid s_fields = [ 's.%s=%d' % (key, val) for (key, val) in [('plate', plate), ('mjd', mjd), ('fiberid', fiberID)] if val is not None ] if s_fields: q_where = 'WHERE (' + ' AND '.join(s_fields) + ')' elif run or camcol or field: # Imaging: query for specified run, rerun, camcol, field p_fields = [ 'p.%s=%d' % (key, val) for (key, val) in [('run', run), ('camcol', camcol), ('field', field)] if val is not None ] if p_fields: p_fields.append('p.rerun=%d' % rerun) q_where = 'WHERE (' + ' AND '.join(p_fields) + ')' if not q_where: if spectro: raise ValueError('must specify at least one of `coordinates`, ' '`plate`, `mjd` or `fiberID`') else: raise ValueError('must specify at least one of `coordinates`, ' '`run`, `camcol` or `field`') sql = "{0} {1} {2} {3}".format(q_select, q_from, q_join, q_where) request_payload = dict(cmd=sql, format='csv') return request_payload
def test_true_lon(self): """tests calculation of sun's true longitude""" lon = self.pos.get_true_lon() self.assertLess(np.abs(lon - c.Angle(199.90988 * u.deg)), 1.0e-5 * u.deg)
def __init__(self, filenames): self.filenames = filenames self.num_files = len(filenames) self.N = 0 self.user_poln = 0 self.default_poln = 0 # Initialise a few arrays self.start_MJD = np.empty(self.num_files) self.num_subint = np.empty(self.num_files) self.start_subint = np.empty(self.num_files) self.start_spec = np.empty(self.num_files) self.num_pad = np.empty(self.num_files) self.num_spec = np.empty(self.num_files) # The following should default to False self.need_scale = False self.need_offset = False self.need_weight = False self.need_flipband = False for ii, fn in enumerate(filenames): if not is_PSRFITS(fn): raise ValueError("File '%s' does not appear to be PSRFITS!" % fn) # Open the PSRFITS file hdus = pyfits.open(fn, mode='readonly', memmap=True) if ii == 0: self.hdu_names = [hdu.name for hdu in hdus] primary = hdus['PRIMARY'].header if 'TELESCOP' not in primary.keys(): telescope = "" else: telescope = primary['TELESCOP'] # Quick fix for MockSpec data... if telescope == "ARECIBO 305m": telescope = "Arecibo" if ii == 0: self.telescope = telescope else: if telescope != self.telescope[0]: warnings.warn( "'TELESCOP' values don't match for files 0 and %d!" % ii) self.observer = primary['OBSERVER'] self.source = primary['SRC_NAME'] self.frontend = primary['FRONTEND'] self.backend = primary['BACKEND'] self.project_id = primary['PROJID'] self.date_obs = primary['DATE-OBS'] self.poln_type = primary['FD_POLN'] self.ra_str = primary['RA'] self.dec_str = primary['DEC'] self.fctr = primary['OBSFREQ'] self.orig_num_chan = primary['OBSNCHAN'] self.orig_df = primary['OBSBW'] self.beam_FWHM = primary['BMIN'] # CHAN_DM card is not in earlier versions of PSRFITS if 'CHAN_DM' not in primary.keys(): self.chan_dm = 0.0 else: self.chan_dm = primary['CHAN_DM'] self.start_MJD[ii] = primary['STT_IMJD'] + (primary['STT_SMJD'] + \ primary['STT_OFFS'])/psr_utils.SECPERDAY # Are we tracking track = (primary['TRK_MODE'] == "TRACK") if ii == 0: self.tracking = track else: if track != self.tracking: warnings.warn( "'TRK_MODE' values don't match for files 0 and %d" % ii) # Now switch to the subint HDU header subint = hdus['SUBINT'].header self.dt = subint['TBIN'] self.num_channels = subint['NCHAN'] self.num_polns = subint['NPOL'] # PRESTO's 'psrfits.c' has some settings based on environ variables envval = os.getenv("PSRFITS_POLN") if envval is not None: ival = int(envval) if ((ival > -1) and (ival < self.num_polns)): print "Using polarisation %d (from 0-%d) from PSRFITS_POLN." % \ (ival, self.num_polns-1) self.default_poln = ival self.user_poln = 1 self.poln_order = subint['POL_TYPE'] if subint['NCHNOFFS'] > 0: warnings.warn("first freq channel is not 0 in file %d" % ii) self.spectra_per_subint = subint['NSBLK'] self.bits_per_sample = subint['NBITS'] self.num_subint[ii] = subint['NAXIS2'] self.start_subint[ii] = subint['NSUBOFFS'] self.time_per_subint = self.dt * self.spectra_per_subint # This is the MJD offset based on the starting subint number MJDf = (self.time_per_subint * self.start_subint[ii]) / psr_utils.SECPERDAY # The start_MJD values should always be correct self.start_MJD[ii] += MJDf # Compute the starting spectra from the times MJDf = self.start_MJD[ii] - self.start_MJD[0] if MJDf < 0.0: raise ValueError("File %d seems to be from before file 0!" % ii) self.start_spec[ii] = (MJDf * psr_utils.SECPERDAY / self.dt + 0.5) # Now pull stuff from the columns subint_hdu = hdus['SUBINT'] # Identify the OFFS_SUB column number if 'OFFS_SUB' not in subint_hdu.columns.names: warnings.warn("Can't find the 'OFFS_SUB' column!") else: colnum = subint_hdu.columns.names.index('OFFS_SUB') if ii == 0: self.offs_sub_col = colnum elif self.offs_sub_col != colnum: warnings.warn( "'OFFS_SUB' column changes between files 0 and %d!" % ii) # Identify the data column and the data type if 'DATA' not in subint_hdu.columns.names: warnings.warn("Can't find the 'DATA' column!") else: colnum = subint_hdu.columns.names.index('DATA') if ii == 0: self.data_col = colnum self.FITS_typecode = subint_hdu.columns[ self.data_col].format[-1] elif self.data_col != colnum: warnings.warn( "'DATA' column changes between files 0 and %d!" % ii) # Telescope azimuth if 'TEL_AZ' not in subint_hdu.columns.names: self.azimuth = 0.0 else: colnum = subint_hdu.columns.names.index('TEL_AZ') if ii == 0: self.tel_az_col = colnum self.azimuth = subint_hdu.data[0]['TEL_AZ'] # Telescope zenith angle if 'TEL_ZEN' not in subint_hdu.columns.names: self.zenith_ang = 0.0 else: colnum = subint_hdu.columns.names.index('TEL_ZEN') if ii == 0: self.tel_zen_col = colnum self.zenith_ang = subint_hdu.data[0]['TEL_ZEN'] # Observing frequencies if 'DAT_FREQ' not in subint_hdu.columns.names: warnings.warn( "Can't find the channel freq column, 'DAT_FREQ'!") else: colnum = subint_hdu.columns.names.index('DAT_FREQ') freqs = subint_hdu.data[0]['DAT_FREQ'] if ii == 0: self.freqs_col = colnum self.df = freqs[1] - freqs[0] self.lo_freq = freqs[0] self.hi_freq = freqs[-1] # Now check that the channel spacing is the same throughout ftmp = freqs[1:] - freqs[:-1] if np.any((ftmp - self.df)) > 1e-7: warnings.warn("Channel spacing changes in file %d!" % ii) else: ftmp = np.abs(self.df - (freqs[1] - freqs[0])) if ftmp > 1e-7: warnings.warn( "Channel spacing between files 0 and %d!" % ii) ftmp = np.abs(self.lo_freq - freqs[0]) if ftmp > 1e-7: warnings.warn( "Low channel changes between files 0 and %d!" % ii) ftmp = np.abs(self.hi_freq - freqs[-1]) if ftmp > 1e-7: warnings.warn( "High channel changes between files 0 and %d!" % ii) # Data weights if 'DAT_WTS' not in subint_hdu.columns.names: warnings.warn( "Can't find the channel weights column, 'DAT_WTS'!") else: colnum = subint_hdu.columns.names.index('DAT_WTS') if ii == 0: self.dat_wts_col = colnum elif self.dat_wts_col != colnum: warnings.warn( "'DAT_WTS column changes between files 0 and %d!" % ii) if np.any(subint_hdu.data[0]['DAT_WTS'] != 1.0): self.need_weight = True # Data offsets if 'DAT_OFFS' not in subint_hdu.columns.names: warnings.warn( "Can't find the channel offsets column, 'DAT_OFFS'!") else: colnum = subint_hdu.columns.names.index('DAT_OFFS') if ii == 0: self.dat_offs_col = colnum elif self.dat_offs_col != colnum: warnings.warn( "'DAT_OFFS column changes between files 0 and %d!" % ii) if np.any(subint_hdu.data[0]['DAT_OFFS'] != 0.0): self.need_offset = True # Data scalings if 'DAT_SCL' not in subint_hdu.columns.names: warnings.warn( "Can't find the channel scalings column, 'DAT_SCL'!") else: colnum = subint_hdu.columns.names.index('DAT_SCL') if ii == 0: self.dat_scl_col = colnum elif self.dat_scl_col != colnum: warnings.warn( "'DAT_SCL' column changes between files 0 and %d!" % ii) if np.any(subint_hdu.data[0]['DAT_SCL'] != 1.0): self.need_scale = True # Comute the samples per file and the amount of padding # that the _previous_ file has self.num_pad[ii] = 0 self.num_spec[ii] = self.spectra_per_subint * self.num_subint[ii] if ii > 0: if self.start_spec[ii] > self.N: # Need padding self.num_pad[ii - 1] = self.start_spec[ii] - self.N self.N += self.num_pad[ii - 1] self.N += self.num_spec[ii] # Finished looping through PSRFITS files. Finalise a few things. # Convert the position strings into degrees self.ra2000 = coordinates.Angle(self.ra_str, unit=units.hourangle).deg self.dec2000 = coordinates.Angle(self.dec_str, unit=units.deg).deg # Are the polarisations summed? if (self.poln_order == "AA+BB") or (self.poln_order == "INTEN"): self.summed_polns = True else: self.summed_polns = False # Calculate some others self.T = self.N * self.dt self.orig_df /= float(self.orig_num_chan) self.samples_per_spectra = self.num_polns * self.num_channels # Note: the following is the number of bytes that will be in # the returned array. if self.bits_per_sample < 8: self.bytes_per_spectra = self.samples_per_spectra else: self.bytes_per_spectra = (self.bits_per_sample * self.samples_per_spectra) / 8 self.samples_per_subint = self.samples_per_spectra * self.spectra_per_subint self.bytes_per_subint = self.bytes_per_spectra * self.spectra_per_subint # Flip the band? if self.hi_freq < self.lo_freq: tmp = self.hi_freq self.hi_freq = self.lo_freq self.lo_freq = tmp self.df *= -1.0 self.need_flipband = True # Compute the bandwidth self.BW = self.num_channels * self.df self.mjd = int(self.start_MJD[0]) self.secs = (self.start_MJD[0] % 1) * psr_utils.SECPERDAY
def test_apparent_lon(self): """tests calculation of sun's apparent longitude""" lon = self.pos.get_apparent_lon().wrap_at(360 * u.deg) self.assertLess(np.abs(lon - c.Angle(199.90895 * u.deg)), 1.0e-5 * u.deg)
def _args_to_payload(self, **kwargs): """ Queries the NRAO data archive and fetches table of observation summaries. Parameters ---------- coordinates : str or `astropy.coordinates` object The target around which to search. It may be specified as a string in which case it is resolved using online services or as the appropriate `astropy.coordinates` object. ICRS coordinates may also be entered as a string. radius : str or `~astropy.units.Quantity` object, optional The string must be parsable by `astropy.coordinates.Angle`. The appropriate `~astropy.units.Quantity` object may also be used. Defaults to 1 arcminute. equinox : str, optional One of 'J2000' or 'B1950'. Defaults to 'J2000'. telescope : str, optional The telescope that produced the data. Defaults to 'all'. Valid values are: ['gbt', 'all', 'historical_vla', 'vlba', 'jansky_vla'] start_date : str, optional The starting date and time of the observations , e.g. 2010-06-21 14:20:30 Decimal seconds are not allowed. Defaults to `None` for no constraints. end_date : str, optional The ending date and time of the observations , e.g. 2010-06-21 14:20:30 Decimal seconds are not allowed. Defaults to `None` for no constraints. freq_low : `~astropy.units.Quantity` object, optional The lower frequency of the observations in proper units of frequency via `astropy.units`. Defaults to `None` for no constraints. freq_up : `~astropy.units.Quantity` object, optional The upper frequency of the observations in proper units of frequency via `astropy.units`. Defaults to `None` for no constraints. telescope_config : str, optional Select the telescope configuration (only valid for VLA array). Defaults to 'all'. Valid values are ['all', 'A', 'AB', 'BnA', 'B', 'BC', 'CnB', 'C', 'CD', 'DnC', 'D', 'DA'] obs_band : str, optional The frequency bands for the observation. Defaults to 'all'. Valid values are ['all', '4', 'P', 'L', 'S', 'C', 'X', 'U', 'K', 'Ka', 'Q', 'W']. sub_array : str, number, optional VLA subarray designations, may be set to an integer from 1 to 5. Defaults to 'all'. project_code : str, optional A string indicating the project code. Examples:: * GBT: AGBT12A_055 * JVLA: 12A-256 querytype : str The type of query to perform. "OBSSUMMARY" is the default, but it is only valid for VLA/VLBA observations. ARCHIVE will give the list of files available for download. OBSERVATION will provide full details of the sources observed and under what configurations. source_id : str, optional A source name (to be parsed by SIMBAD or NED) protocol : 'VOTable-XML' or 'HTML' The type of table to return. In theory, this should not matter, but in practice the different table types actually have different content. For ``querytype='ARCHIVE'``, the protocol will be force to HTML because the archive doesn't support votable returns for archive queries. get_query_payload : bool, optional if set to `True` then returns the dictionary sent as the HTTP request. Defaults to `False` cache : bool Cache the query results retry : bool or int The number of times to retry querying the server if it doesn't raise an exception but returns a null result (this sort of behavior seems unique to the NRAO archive) Returns ------- request_payload : dict The dictionary of parameters to send via HTTP GET request. """ lower_frequency = kwargs.get('freq_low', None) upper_frequency = kwargs.get('freq_up', None) if lower_frequency is not None and upper_frequency is not None: freq_str = (str(lower_frequency.to(u.MHz).value) + '-' + str(upper_frequency.to(u.MHz).value)) else: freq_str = "" obs_bands = kwargs.get('obs_band', 'all') if isinstance(obs_bands, six.string_types): obs_bands = obs_bands.upper() elif isinstance(obs_bands, (list, tuple)): obs_bands = [x.upper() for x in obs_bands] telescope_config = kwargs.get('telescope_config', 'all') if isinstance(telescope_config, six.string_types): telescope_config = telescope_config.upper() elif isinstance(telescope_config, (list, tuple)): telescope_config = [x.upper() for x in telescope_config] telescope_ = kwargs.get('telescope', 'all') if isinstance(telescope_, six.string_types): telescope = Nrao.telescope_code[telescope_] elif isinstance(telescope, (list, tuple)): telescope = [Nrao.telescope_code[telescope_] for x in telescope_] request_payload = dict( QUERYTYPE=kwargs.get('querytype', "OBSSUMMARY"), PROTOCOL=kwargs.get('protocol', "VOTable-XML"), MAX_ROWS="NO LIMIT", SORT_PARM="Starttime", SORT_ORDER="Asc", SORT_PARM2="Starttime", SORT_ORDER2="Asc", QUERY_ID=9999, QUERY_MODE="AAT_TOOL", LOCKMODE="PROJECT", SITE_CODE="AOC", DBHOST="CHEWBACCA", WRITELOG=0, TELESCOPE=telescope, PROJECT_CODE=kwargs.get('project_code', ''), SEGMENT="", MIN_EXPOSURE='', TIMERANGE1=kwargs.get('start_date', ''), OBSERVER="", ARCHIVE_VOLUME="", TIMERANGE2=kwargs.get('end_date', ''), EQUINOX=kwargs.get('equinox', 'J2000'), CENTER_RA='', CENTER_DEC='', SRAD=str( coordinates.Angle(kwargs.get('radius', "1.0m")).deg) + 'd', TELESCOPE_CONFIG=telescope_config, OBS_BANDS=obs_bands, SUBARRAY=kwargs.get('subarray', 'all').upper(), SOURCE_ID=kwargs.get('source_id', ''), SRC_SEARCH_TYPE='SIMBAD or NED', OBSFREQ1=freq_str, OBS_POLAR="ALL", RECEIVER_ID="ALL", BACKEND_ID="ALL", DATATYPE="ALL", PASSWD="", # TODO: implement login... SUBMIT="Submit Query") if ((request_payload['QUERYTYPE'] == "ARCHIVE" and request_payload['PROTOCOL'] != 'HTML')): warnings.warn("Changing protocol to HTML: ARCHIVE queries do not" " support votable returns") request_payload['PROTOCOL'] = 'HTML' if request_payload['PROTOCOL'] not in ('HTML', 'VOTable-XML'): raise ValueError("Only HTML and VOTable-XML returns are supported") if request_payload['QUERYTYPE'] not in ('ARCHIVE', 'OBSSUMMARY', 'OBSERVATION'): raise ValueError("Only ARCHIVE, OBSSUMMARY, and OBSERVATION " "querytypes are supported") if 'coordinates' in kwargs: c = commons.parse_coordinates( kwargs['coordinates']).transform_to(coordinates.ICRS) request_payload['CENTER_RA'] = str(c.ra.degree) + 'd' request_payload['CENTER_DEC'] = str(c.dec.degree) + 'd' return request_payload
def __init__(self, P=None, a=None, e=0, omega=None, i=None, Omega=None, M0=None, t0=None, units=None): """Base class for Keplerian orbital elements. Parameters ---------- P : quantity_like [time] Orbital period. a : quantity_like [length] (optional) Semi-major axis. If unspecified, computed orbits will be unscaled. e : numeric (optional) Orbital eccentricity. Default is circular, ``e=0``. omega : quantity_like, `~astropy.coordinates.Angle` [angle] Argument of pericenter. i : quantity_like, `~astropy.coordinates.Angle` [angle] Inclination of the orbit. Omega : quantity_like, `~astropy.coordinates.Angle` [angle] Longitude of the ascending node. M0 : quantity_like, `~astropy.coordinates.Angle` [angle] (optional) Mean anomaly at epoch ``t0``. Default is 0º if not specified. t0 : numeric, `~astropy.coordinates.Time` (optional) Reference epoch. If a number is passed in, it is assumed to be a solar system barycentric modified julian date (BMJD). The default is J2000 if not specified. units : `~twobody.units.UnitSystem`, iterable (optional) The unit system to represent quantities in. The default unit system is accessible as `KeplerElements.default_units`. """ if M0 is None: # Default phase at reference epoch is 0º M0 = 0 * u.degree if t0 is None: # Default reference epoch is J2000 t0 = Time('J2000') t0 = _parse_time(t0) # Now check that required elements are defined: _required = ['P', 'omega'] for name in _required: if eval(name) is None: raise ValueError("You must specify {0}.".format(name)) # Value validation: if P < 0 * u.day: raise ValueError("Period `P` must be positive.") if a is not None and a < 0 * u.au: raise ValueError("Semi-major axis `a` must be positive.") if e < 0 or e >= 1: raise ValueError("Eccentricity `e` must be: 0 <= e < 1") if i is not None and (i < 0 * u.deg or i > 180 * u.deg): raise ValueError( "Inclination `i` must be between 0º and 180º, you " "passed in i={:.3f}".format(i.to(u.degree))) if i is None: i = np.nan * u.deg if Omega is None: Omega = np.nan * u.deg # Set object attributes, but make them read-only self._a = a if a is not None else 1. * u.dimensionless_unscaled self._P = P self._e = float(e) * u.dimensionless_unscaled self._omega = coord.Angle(omega).wrap_at(360 * u.deg) self._i = coord.Angle(i) self._Omega = coord.Angle(Omega).wrap_at(360 * u.deg) self._M0 = coord.Angle(M0) self.t0 = t0 # Must happen at the end because it validates that all element names # have been set properly: super().__init__(units=units)
def query_region_async(self, coordinate=None, where=None, mission=None, dataset=None, table=None, columns=None, width=None, height=None, action='search', intersect='OVERLAPS', most_centered=False): """ For certain missions, this function can be used to search for image and catalog files based on a point, a box (bounded by great circles) and/or an SQL-like ``where`` clause. If ``coordinates`` is specified, then the optional ``width`` and ``height`` arguments control the width and height of the search box. If neither ``width`` nor ``height`` are provided, then the search area is a point. If only one of ``width`` or ``height`` are specified, then the search area is a square with that side length centered at the coordinate. Parameters ---------- coordinate : str, `astropy.coordinates` object Gives the position of the center of the box if performing a box search. If it is a string, then it must be a valid argument to `~astropy.coordinates.SkyCoord`. Required if ``where`` is absent. where : str SQL-like query string. Required if ``coordinates`` is absent. mission : str The mission to be used (if not the default mission). dataset : str The dataset to be used (if not the default dataset). table : str The table to be queried (if not the default table). columns : str, list A space-separated string or a list of strings of the names of the columns to return. width : str or `~astropy.units.Quantity` object Width of the search box if ``coordinates`` is present. The string must be parsable by `~astropy.coordinates.Angle`. The appropriate `~astropy.units.Quantity` object from `astropy.units` may also be used. height : str, `~astropy.units.Quantity` object Height of the search box if ``coordinates`` is present. The string must be parsable by `~astropy.coordinates.Angle`. The appropriate `~astropy.units.Quantity` object from `astropy.units` may also be used. intersect : ``'COVERS'``, ``'ENCLOSED'``, ``'CENTER'``, ``'OVERLAPS'`` Spatial relationship between search box and image footprint. ``'COVERS'``: X must completely contain S. Equivalent to ``'CENTER'`` and ``'OVERLAPS'`` if S is a point. ``'ENCLOSED'``: S must completely contain X. If S is a point, the query will always return an empty image table. ``'CENTER'``: X must contain the center of S. If S is a point, this is equivalent to ``'COVERS'`` and ``'OVERLAPS'``. ``'OVERLAPS'``: The intersection of S and X is non-empty. If S is a point, this is equivalent to ``'CENTER'`` and ``'COVERS'``. most_centered : bool If True, then only the most centered image is returned. action : ``'search'``, ``'data'``, or ``'sia'`` The action to perform at the server. The default is ``'search'``, which returns a table of the available data. ``'data'`` requires advanced path construction that is not yet supported. ``'sia'`` provides access to the 'simple image access' IVOA protocol Returns ------- response : `~requests.Response` The HTTP response returned from the service """ if coordinate is None and where is None: raise InvalidQueryError( 'At least one of `coordinate` or `where` is required') intersect = intersect.upper() if intersect not in ('COVERS', 'ENCLOSED', 'CENTER', 'OVERLAPS'): raise InvalidQueryError( "Invalid value for `intersects` " + "(must be 'COVERS', 'ENCLOSED', 'CENTER', or 'OVERLAPS')") if action not in ('sia', 'data', 'search'): raise InvalidQueryError("Valid actions are: sia, data, search.") if action == 'data': raise NotImplementedError( "The action='data' option is a placeholder for future " + "functionality.") args = {'INTERSECT': intersect} # Note: in IBE, if 'mcen' argument is present, it is true. # If absent, it is false. if most_centered: args['mcen'] = '1' if coordinate is not None: c = commons.parse_coordinates(coordinate).transform_to(coord.ICRS) args['POS'] = '{0},{1}'.format(c.ra.deg, c.dec.deg) if width and height: args['SIZE'] = '{0},{1}'.format( coord.Angle(width).value, coord.Angle(height).value) elif width or height: args['SIZE'] = str(coord.Angle(width or height).value) if where: args['where'] = where if columns: if isinstance(columns, six.string_types): columns = columns.split() args['columns'] = ','.join(columns) url = "{URL}{action}/{mission}/{dataset}/{table}".format( URL=self.URL, action=action, mission=mission or self.MISSION, dataset=dataset or self.DATASET, table=table or self.TABLE) return self._request('GET', url, args, timeout=self.TIMEOUT)
def query_region_async(self, table, coordinates, radius, *, get_query_payload=False, cache=None, **criteria): """ Filter a table using a cone search around specified coordinates Parameters ---------- table : str The name of the table to query. A list of the tables on the Exoplanet Archive can be found on the documentation pages [1]_, [2]_. coordinates : str or `~astropy.coordinates` The coordinates around which to query. radius : str or `~astropy.units.Quantity` The radius of the cone search. Assumed to be have units of degrees if not provided as a ``Quantity``. get_query_payload : bool, optional Just return the dict of HTTP request parameters. Defaults to ``False``. cache : bool, optional Should the request result be cached? This can be useful for large repeated queries, but since the data in the archive is updated regularly, this defaults to ``False``. **criteria Any other filtering criteria to apply. These are described in detail in the archive documentation [1]_,[2]_ but some examples include ``select="*"`` to return all columns of the queried table or ``where=pl_name='K2-18 b'`` to filter a specific column. Returns ------- response : `requests.Response` The HTTP response returned from the service. References ---------- .. [1] `NASA Exoplanet Archive TAP Documentation <https://exoplanetarchive.ipac.caltech.edu/docs/TAP/usingTAP.html>`_ .. [2] `NASA Exoplanet Archive API Documentation <https://exoplanetarchive.ipac.caltech.edu/docs/program_interfaces.html>`_ """ # Checks if coordinate strings is parsable as an astropy.coordinates object coordinates = commons.parse_coordinates(coordinates) # if radius is just a number we assume degrees if isinstance(radius, (int, float)): radius = radius * u.deg radius = coord.Angle(radius) criteria["ra"] = coordinates.ra.deg criteria["dec"] = coordinates.dec.deg criteria["radius"] = "{0} degree".format(radius.deg) # Runs the query method defined above, but with added region filter return self.query_criteria_async( table, get_query_payload=get_query_payload, cache=cache, **criteria, )
def query_region_async(self, coordinates, radius=None, inner_radius=None, width=None, height=None, catalog=None, get_query_payload=False, cache=True, return_type='votable', column_filters={}, frame='fk5'): """ Serves the same purpose as `query_region` but only returns the HTTP response rather than the parsed result. Parameters ---------- coordinates : str, `astropy.coordinates` object, or `~astropy.table.Table` The target around which to search. It may be specified as a string in which case it is resolved using online services or as the appropriate `astropy.coordinates` object. ICRS coordinates may also be entered as a string. If a table is used, each of its rows will be queried, as long as it contains two columns named ``_RAJ2000`` and ``_DEJ2000`` with proper angular units. radius : convertible to `~astropy.coordinates.Angle` The radius of the circular region to query. inner_radius : convertible to `~astropy.coordinates.Angle` When set in addition to ``radius``, the queried region becomes annular, with outer radius ``radius`` and inner radius ``inner_radius``. width : convertible to `~astropy.coordinates.Angle` The width of the square region to query. height : convertible to `~astropy.coordinates.Angle` When set in addition to ``width``, the queried region becomes rectangular, with the specified ``width`` and ``height``. catalog : str or list, optional The catalog(s) which must be searched for this identifier. If not specified, all matching catalogs will be searched. column_filters: dict, optional Constraints on columns of the result. The dictionary contains the column name as keys, and the constraints as values. frame : str, optional The frame to use for the request. It should be 'fk5', 'icrs', or 'galactic'. This choice influences the the orientation of box requests. Returns ------- response : `requests.Response` The response of the HTTP request. """ if frame not in ('galactic', 'fk5', 'icrs'): raise ValueError("Only the 'galactic', 'icrs', and 'fk5' frames are supported by VizieR") catalog = VizierClass._schema_catalog.validate(catalog) center = {} columns = [] # Process coordinates if isinstance(coordinates, (commons.CoordClasses,) + six.string_types): target = commons.parse_coordinates(coordinates).transform_to(frame) if not target.isscalar: center["-c"] = [] for pos in target: if frame == 'galactic': glon_deg = pos.l.to_string(unit="deg", decimal=True, precision=8) glat_deg = pos.b.to_string(unit="deg", decimal=True, precision=8, alwayssign=True) center["-c"] += ["G{}{}".format(glon_deg, glat_deg)] else: ra_deg = pos.ra.to_string(unit="deg", decimal=True, precision=8) dec_deg = pos.dec.to_string(unit="deg", decimal=True, precision=8, alwayssign=True) center["-c"] += ["{}{}".format(ra_deg, dec_deg)] columns += ["_q"] # Always request reference to input table else: if frame == 'galactic': glon = target.l.to_string(unit='deg', decimal=True, precision=8) glat = target.b.to_string(unit="deg", decimal=True, precision=8, alwayssign=True) center["-c"] = "G{glon}{glat}".format(glon=glon, glat=glat) else: ra = target.ra.to_string(unit='deg', decimal=True, precision=8) dec = target.dec.to_string(unit="deg", decimal=True, precision=8, alwayssign=True) center["-c"] = "{ra}{dec}".format(ra=ra, dec=dec) elif isinstance(coordinates, tbl.Table): if (("_RAJ2000" in coordinates.keys()) and ("_DEJ2000" in coordinates.keys())): center["-c"] = [] sky_coord = coord.SkyCoord(coordinates["_RAJ2000"], coordinates["_DEJ2000"], unit=(coordinates["_RAJ2000"].unit, coordinates["_DEJ2000"].unit)) for (ra, dec) in zip(sky_coord.ra, sky_coord.dec): ra_deg = ra.to_string(unit="deg", decimal=True, precision=8) dec_deg = dec.to_string(unit="deg", decimal=True, precision=8, alwayssign=True) center["-c"] += ["{}{}".format(ra_deg, dec_deg)] columns += ["_q"] # Always request reference to input table else: raise ValueError("Table must contain '_RAJ2000' and " "'_DEJ2000' columns!") else: raise TypeError("Coordinates must be one of: string, astropy " "coordinates, or table containing coordinates!") # decide whether box or radius if radius is not None: # is radius a disk or an annulus? if inner_radius is None: radius = coord.Angle(radius) unit, value = _parse_angle(radius) key = "-c.r" + unit center[key] = value else: i_radius = coord.Angle(inner_radius) o_radius = coord.Angle(radius) if i_radius.unit != o_radius.unit: o_radius = o_radius.to(i_radius.unit) i_unit, i_value = _parse_angle(i_radius) o_unit, o_value = _parse_angle(o_radius) key = "-c.r" + i_unit center[key] = ",".join([str(i_value), str(o_value)]) elif width is not None: # is box a rectangle or square? if height is None: width = coord.Angle(width) unit, value = _parse_angle(width) key = "-c.b" + unit center[key] = "x".join([str(value)] * 2) else: w_box = coord.Angle(width) h_box = coord.Angle(height) if w_box.unit != h_box.unit: h_box = h_box.to(w_box.unit) w_unit, w_value = _parse_angle(w_box) h_unit, h_value = _parse_angle(h_box) key = "-c.b" + w_unit center[key] = "x".join([str(w_value), str(h_value)]) else: raise Exception( "At least one of radius, width/height must be specified") # Prepare payload data_payload = self._args_to_payload(center=center, columns=columns, catalog=catalog, column_filters=column_filters) if get_query_payload: return data_payload response = self._request( method='POST', url=self._server_to_url(return_type=return_type), data=data_payload, timeout=self.TIMEOUT, cache=cache) return response
stretch=astropy.visualization.LinearStretch()) ax2.imshow(data, cmap='gist_gray_r', origin='lower', interpolation='none', norm=norm) conts = ax2.contour(var, levels=[1 * FCF_arcsec / FCF_beam], colors='black') ax2.set_xlim(117.38899509591401, 433.24228632982658) ax2.set_ylim(70.910809319165651, 384.07598743619394) uhtab = Table.read(uhcat) # Get decimal degree ra and dec dec = uhtab['DEJ2000'] dec = [i.decode() for i in dec] dec = coord.Angle(dec, unit=u.degree) ra = uhtab['RAJ2000'] ra = [i.decode() for i in ra] ra = coord.Angle(ra, unit=u.hour) ra = ra.to(unit=u.degree) coll = ax2.scatter(ra.value, dec.value, s=45, transform=ax2.get_transform('fk5'), label='UH', marker='o', color='black') coll.set_edgecolor(coll.get_facecolor()) coll.set_facecolor('none')
def query_region_async(self, coordinates, radius=1 * u.arcmin, equinox='J2000.0', get_query_payload=False): """ Serves the same purpose as `~NedClass.query_region` but returns the raw HTTP response rather than the `astropy.table.Table` object. Parameters ---------- coordinates : str or `astropy.coordinates` object The target around which to search. It may be specified as a string in which case it is resolved using online services or as the appropriate `astropy.coordinates` object. ICRS coordinates may also be entered as strings as specified in the `astropy.coordinates` module. radius : str or `~astropy.units.Quantity` object, optional The string must be parsable by `astropy.coordinates.Angle`. The appropriate `~astropy.units.Quantity` object from `astropy.units` may also be used. Defaults to 1 arcmin. equinox : str, optional The equinox may be either J2000.0 or B1950.0. Defaults to J2000.0 get_query_payload : bool, optional if set to `True` then returns the dictionary sent as the HTTP request. Defaults to `False`. Returns ------- response : `requests.Response` The HTTP response returned from the service """ request_payload = self._request_payload_init() self._set_input_options(request_payload) self._set_output_options(request_payload) # if its a name then query near name if not commons._is_coordinate(coordinates): request_payload['objname'] = coordinates request_payload['search_type'] = 'Near Name Search' request_payload['radius'] = coord.Angle(radius).arcmin else: try: c = commons.parse_coordinates(coordinates) if c.frame.name == 'galactic': request_payload['in_csys'] = 'Galactic' request_payload['lon'] = c.l.degree request_payload['lat'] = c.b.degree # for any other, convert to ICRS and send else: request_payload['in_csys'] = 'Equatorial' ra, dec = commons.coord_to_radec(c) request_payload['lon'] = ra request_payload['lat'] = dec request_payload['search_type'] = 'Near Position Search' request_payload['in_equinox'] = equinox request_payload['radius'] = coord.Angle(radius).arcmin except (u.UnitsError, TypeError): raise TypeError("Coordinates not specified correctly") if get_query_payload: return request_payload response = self._request("GET", url=Ned.OBJ_SEARCH_URL, params=request_payload, timeout=Ned.TIMEOUT) return response
def plot_est_and_err(map_size): """ PPlot the KK estimated angle maps and errors using the Gaussian errors. Here we plot the significance of the detection at each position. Input: map_size = 3,5,10. Output - 6 maps saved in SkyPolMaps/ directory""" import numpy as np # Read in data data = np.load('MCestimates' + str(map_size) + 'deg.npy') N = len(data) # no. patches # Estimator values est_str = [d[0][0] for d in data] # polarisation strength est_ang = [d[1][0] for d in data] # polarisation angle est_A = [d[2][0] for d in data] # monopole amplitude est_fs = [d[3][0] for d in data] # f_s anisotropy est_fc = [d[4][0] for d in data] # f_c anisotropy est_Afs = [d[5][0] for d in data] # Af_s anisotropy est_Afc = [d[6][0] for d in data] # Gaussian errors err_str = [d[0][1] for d in data] # strength gaussian error err_ang = [d[1][1] for d in data] # angle gaussian error err_A = [d[2][1] for d in data] # Amplitude gaussian error err_fs = [d[3][1] for d in data] # fs gaussian error -> unbiased? err_fc = [d[4][1] for d in data] # fc gaussian error err_Afs = [d[5][1] for d in data] # Afs Gaussian error -> unbiased? err_Afc = [d[6][1] for d in data] # Afc Gaussian error # Gaussian means mean_str = [d[0][2] for d in data] mean_ang = [d[1][2] for d in data] mean_A = [d[2][2] for d in data] mean_fs = [d[3][2] for d in data] mean_fc = [d[4][2] for d in data] mean_Afs = [d[5][2] for d in data] mean_Afc = [d[6][2] for d in data] # log data est_log_A = [np.log10(A) for A in est_A] # Compute estimate of strength without zero error unbiased_str = [est_str[i] - mean_str[i] for i in range(len(est_str))] # Compute significances for Afs,Afc data sig_Afs = [est_Afs[i] / err_Afs[i] for i in range(N)] sig_Afc = [est_Afc[i] / err_Afc[i] for i in range(N)] # Compute unbiased errors in fs,fc err_fs_from_Afs = [ np.sqrt((est_fs[i] * err_Afs[i] / est_Afs[i])**2 + (est_fs[i] * err_A[i] / est_A[i])**2) for i in range(N) ] err_fc_from_Afc = [ np.sqrt((est_fc[i] * err_Afc[i] / est_Afc[i])**2 + (est_fc[i] * err_A[i] / est_A[i])**2) for i in range(N) ] # Compute significances for fs, fc data sig_fs = [est_fs[i] / err_fs[i] for i in range(N)] sig_fc = [est_fc[i] / err_fc[i] for i in range(N)] # Next compute errors on angle and unbiased strength def amplitude_err(fs, fc, sig_fs, sig_fc): # error in amplitude return np.sqrt(((sig_fs * fs)**2 + (sig_fc * fc)**2) / (fs**2 + fc**2)) def angle_err(fs, fc, sig_fs, sig_fc): # error in angle in degrees return 1. / 4 * np.sqrt(((fs * sig_fc)**2 + (fc * sig_fs)**2) / (fs**2 + fc**2)) * 180. / np.pi # These are the unbiased estimates unbiased_err_str = [ amplitude_err(est_fs[i], est_fc[i], err_fs[i], err_fc[i]) for i in range(N) ] unbiased_err_ang = [ angle_err(est_fs[i], est_fc[i], err_fs[i], err_fc[i]) for i in range(N) ] # Compute significance of strength and angle sig_unbiased_str = [est_str[i] / unbiased_err_str[i] for i in range(N)] sig_unbiased_ang = [est_ang[i] / unbiased_err_ang[i] for i in range(N)] # Also read in coordinates of patches if map_size == 10: coords = np.loadtxt('/data/ohep2/sims/simdata/fvsmapRAsDecs.txt') # Create lists of patch centres ras = np.zeros(N) decs = np.zeros(N) for i in range(N): ras[i] = coords[i][1] decs[i] = coords[i][2] elif map_size == 5 or map_size == 3: import pickle ras = pickle.load( open('/data/ohep2/sims/' + str(map_size) + 'deg/fvsmapRas.pkl', 'rb')) decs = pickle.load( open('/data/ohep2/sims/' + str(map_size) + 'deg/fvsmapDecs.pkl', 'rb')) ras = ras[:N] decs = decs[:N] # remove any extras else: return Exception('Invalid map_size') # Now plot on grid import astropy.coordinates as coords import astropy.units as u import matplotlib.pyplot as plt ra_deg = coords.Angle(ras * u.degree) # convert to correct format ra_deg = ra_deg.wrap_at(180 * u.degree) dec_deg = coords.Angle(decs * u.degree) # Make output directory import os outDir = '/data/ohep2/SkyPolMapsUnbiased/' if not os.path.exists(outDir): os.mkdir(outDir) # Plot maps datSet=[est_str,est_ang,est_A,est_fs,est_fc,\ sig_fs,sig_fc,\ sig_unbiased_str,sig_unbiased_ang,unbiased_err_str,unbiased_err_ang,\ unbiased_str,mean_str,est_log_A] #datSet=[pol_str,pol_ang,str_err,ang_err,sig_str,sig_ang] # all datasets names=['Polarisation Strength','Polarisation Angle','Monopole Amplitude',\ 'f_s','f_c','f_s Significance','f_c Significance',\ 'Unbiased Strength Significance','Unbiased Angle Significance',\ 'Unbiased Strength Error','Unbiased Angle Error',\ 'Strength - mean(isotropic strength)','Mean Isotropic Strength','log Amplitude Estimation'] #names=['Polarisation Strength', 'Polarisation Angle', \ #'Strength Gaussian Error','Angle Gaussian Error', #'Polarisation Strength Significance','Polarisation Angle Significance'] fileStr=['est_str','est_ang','est_A','est_fs','est_fc','sig_fs','sig_fc',\ 'sig_unbiased_str','sig_unbiased_ang','unbiased_err_str','unbiased_err_ang',\ 'str_mean_sub','mean_iso_str','est_log_A'] #fileStr=['pol_str','pol_ang','str_err','ang_err','sig_str','sig_ang'] if map_size == 5: s_dot = 50 # size of point in plot elif map_size == 3: s_dot = 30 for i in range(len(datSet)): fig = plt.figure() fig.add_subplot(111, projection='mollweide') #if i == 2: # plt.scatter(ra_deg.radian,dec_deg.radian,c=datSet[i],marker='o',vmax=0.5,s=s_dot) #elif i ==4: # plt.scatter(ra_deg.radian,dec_deg.radian,c=datSet[i],marker='o',vmax=10,s=s_dot) #else: plt.scatter(ra_deg.radian, dec_deg.radian, c=datSet[i], marker='o', s=s_dot) plt.colorbar() plt.axis('off') plt.title(names[i]) plt.savefig(outDir + str(map_size) + 'deg_' + fileStr[i] + '.png', bbox_inches='tight') plt.clf() plt.close()