def s_name(rat, det): #Find the number of records num_obj = len(rat) #Convert the Decimal RA/DEC to Sexegessimal. c = sc(ra=rat * u.degree, dec=det * u.degree) c_hms = c.ra.hms c_dms = c.dec.dms #Placeholder arrays for the sign in the name, and for the names themselves. sig = np.chararray(num_obj, itemsize=1, unicode=True) sname_array = np.chararray(num_obj, itemsize=18, unicode=True) #To get the correct sign in the SDSS_NAME check the leading sign on the #sexegessimal dec. #Check the degree sign-- wdp = np.where(c_dms.d > 0.0)[0] sig[wdp] = '+' wdm = np.where(c_dms.d < 0.0)[0] sig[wdm] = '-' #If the degree is 0, check the minutes sign wd0 = np.where(c_dms.d == 0.0)[0] wmp = np.where(c_dms.m[wd0] > 0.0)[0] sig[wd0[wmp]] = '+' wmn = np.where(c_dms.m[wd0] < 0.0)[0] sig[wd0[wmn]] = '-' #If both the degree and minutes are 0, check the seconds sign. If seconds #are 0, then assign a +. This should only happen if DEC is 00:00:00.0 wm0 = np.where(c_dms.m[wd0] == 0.0)[0] wsp = np.where(c_dms.s[wd0[wm0]] >= 0.0)[0] sig[wd0[wm0[wsp]]] = '+' wsm = np.where(c_dms.s[wd0[wm0]] < 0.0)[0] sig[wd0[wm0[wsm]]] = '-' #Need to truncate the decimal places in seconds without rounding. #Converting to a string and then cutting string characters does this. #First convert the seconds to a string for both RA and DEC. c_htemp = np.array(['%07.4f' % X for X in abs(c_hms.s)]) c_dtemp = np.array(['%07.4f' % Y for Y in abs(c_dms.s)]) #Cut down the number of characters in the string. The :-2 means all but the #last two. For an SDSS name, the RA seconds have 2 decimal places, the DEC #seconds have 1 decimal place. c_hsec = np.array([obj[:-2] for obj in c_htemp]) c_dsec = np.array([obj[:-3] for obj in c_dtemp]) #Create the string for the RA part. snr = np.array([ '%02d%02d%s' % (Hh, mm, ss) for Hh, mm, ss in zip(abs(c_hms.h), abs(c_hms.m), c_hsec) ]) #Create the string for the DEC part. snd = np.array([ '%02d%02d%s' % (Dd, mm, ss) for Dd, mm, ss in zip(abs(c_dms.d), abs(c_dms.m), c_dsec) ]) #Create the total string, with sign, and without J for updating fields. If #a J is needed, then add it in the parent function calling this one. sname_array = np.array( ['%s%s%s' % (rs, sigs, ds) for rs, sigs, ds in zip(snr, sig, snd)]) #Output the final SDSS name. return sname_array
def get_name(in_ra, in_dec): coords = sc(ra=in_ra * u.degree, dec=in_dec * u.degree) c_hms = coords.ra.hms c_dms = coords.dec.dms c_raS, c_decS = str(c_hms.s)[0:5], str(c_dms.s)[0:4] if in_dec >= 0: name_sign = '+' else: name_sign = '-' ra_str = '{:02d}{:02d}{:5s}'.format(int(c_hms.h), int(c_hms.m), c_raS) dec_str = '{:02d}{:02d}{:4s}'.format(int(c_dms.d), int(c_dms.m), c_decS) sdss_name = 'SDSS J{}{}{}'.format(ra_str, name_sign, dec_str) return sdss_name
def matchdf(df1, df2, ra1, de1, ra2, de2): if len(df1)<len(df2): prim=df1 primname=[ra1,de1] sec=df2 secname=[ra2,de2] else: prim=df2 primname=[ra2,de2] sec=df1 secname=[ra1,de1] colnames=prim.columns.values.tolist()+sec.columns.values.tolist() new_df=df(columns=colnames) coord=sc(prim[primname[0]], prim[primname[1]], unit='deg') for i in sec.index.values: t=sc(sec[secname[0]].values[i],sec[secname[1]].values[i], unit='deg') matches, dist = mc(t,coord,3.0,0) print matches if matches and len(matches)>0: tempdf=df([prim.values[int(matches[0])].tolist()+sec.values[i].tolist()],columns=colnames) #print tempdf[[ra1,de1,ra2,de2]] matches=None new_df=new_df.append(tempdf,ignore_index=True) return new_df
def GalInABox(ra, dec, ra_unit, dec_unit, catalog='GLADE', all=False): """ Dati gli intervalli RA e DEC (pensati come gli estremi di una forma qualunque), la funzione restituisce un DataFrame contenente tutte le galassie contenute in CATALOG entro il rettangolo definito dagli intervalli Parameters ---------- ra, dec: list Intervalli di coordinate angolari entro le quali è interamente contenuta la regione desiderata ra_unit, dec_unit: astropy.units.core.Unit Unità di misura in cui sono espressi RA e DEC. Default: deg catalog: string, optional. Catalogo dal quale estrarre i dati. Default: GLADE2 (pensato per coll. LIGO-Virgo). all: boolean, optional Se all = True restituisce tutte le colonne scaricate dal catalogo. Se all = False, solamente quelle corrispondenti a RA, DEC e z. Returns ------- df: Pandas DataFrame DataFrame Pandas contenente gli oggetti selezionati dal catalogo. """ if all: v = Vizier() else: v = Vizier(columns=['RAJ2000', 'DEJ2000', 'z', 'Bmag']) v.ROW_LIMIT = -1 ra = np.array(ra) dec = np.array(dec) center = sc(ra.mean(), dec.mean(), unit=(ra_unit, dec_unit)) width = (ra.max() - ra.min()) / 2. * ra_unit height = (dec.max() - dec.min()) / 2. * dec_unit table = v.query_region(center, width=width, height=height, catalog=catalog) data = pd.DataFrame() for tablei in table: data = data.append(tablei.to_pandas(), ignore_index=True) return data.dropna()
def s_name(ra, dec): #Convert the Decimal RA/DEC to Sexegessimal. c = sc(ra=ra * u.degree, dec=dec * u.degree) c_hms = c.ra.hms c_dms = c.dec.dms #To get the correct sign in the SDSS_NAME check the leading sign on the #sexegessimal dec. if c_dms[0] > 0: sig = '+' elif c_dms[0] < 0: sig = '-' elif c_dms[0] == 0: if c_dms[1] > 0: sig = '+' elif c_dms[1] < 0: sig = '-' elif c_dms[1] == 0: if c_dms[2] > 0: sig = '+' elif c_dms[2] < 0: sig = '-' else: sig = '+' #Need to truncate the decimal places in seconds without rounding. Converting #to a string and then cutting string characters does this. c_htemp = '{0:07.4f}'.format(abs(c_hms[2][0])) c_dtemp = '{0:07.4f}'.format(abs(c_dms[2][0])) #Cut down the number of characters in the string. The :-2 means all but the #last two. c_hsec = c_htemp[:-2] c_dsec = c_dtemp[:-3] #Create the string for the RA part. snr = '{0:02d}{1:02d}{2}'.format(abs(int(c_hms[0])), abs(int(c_hms[1])), c_hsec) #Create the string for the DEC part. snd = '{0:02d}{1:02d}{2}'.format(abs(int(c_dms[0])), abs(int(c_dms[1])), c_dsec) #Create the total string, with sign, and without J for updating fields. If #a J is needed, then add it in the parent function calling this one. sntest = '{0}{1}{2}'.format(snr, sig, snd) #Output the final sdss_name. return sntest
def get_data(self): with fits.open(self.filename, mode='readonly') as hdulist: bjd = hdulist[1].data['TIME'] bjd_nan_mask = np.isnan(bjd) self.time_unit = u.Unit(hdulist[1].header['TIMEUNIT']) flux = hdulist[1].data['SAP_FLUX'] flux_nan_mask = np.isnan(flux) err = hdulist[1].data['SAP_FLUX_ERR'] err_nan_mask = np.isnan(err) flags = hdulist[1].data['QUALITY'] self.ra = hdulist[1].header['RA_OBJ'] self.dec = hdulist[1].header['DEC_OBJ'] self.id = str(hdulist[0].header['TICID']) self.exposure_time = hdulist[1].header['TIMEDEL'] # days s = sc(ra=self.ra * u.degree, dec=self.dec * u.degree).to_string('hmsdms') self.ra_hms = (s.split(' ')[0].split('h')[0] + ':' + s.split(' ')[0].split('h')[1].split('m')[0] + ':' + s.split(' ')[0].split('m')[1].split('s')[0]) self.dec_dms = (s.split(' ')[1].split('d')[0] + ':' + s.split(' ')[1].split('d')[1].split('m')[0] + ':' + s.split(' ')[1].split('m')[1].split('s')[0]) nan_mask = np.invert( np.logical_or(bjd_nan_mask, flux_nan_mask, err_nan_mask)) self.bjd = bjd[nan_mask] self.flux = flux[nan_mask] self.err = err[nan_mask] self.flags = flags[nan_mask] self.data = Table([self.bjd, self.flux, self.err, self.flags], names=['bjd', 'flux', 'err', 'flags'], masked=True)
def get_data(self): performance_star_ids = np.loadtxt(self.datapath + "performance.txt", dtype=np.int64, delimiter=' ', usecols=1) performance_data = np.loadtxt(self.datapath + "performance.txt", dtype=np.float64, delimiter=' ', usecols=(3, 4, 5)) self.data = np.loadtxt(self.filename, dtype=np.float64, delimiter=' ', usecols=(0, 1, 2)) try: row_index = np.where(performance_star_ids == self.id)[0][0] except IndexError: warnings.warn('No performance data found for this star. Skipping.') self.data = None return self.apass_gmag = performance_data[row_index][0] self.ra = performance_data[row_index][1] self.dec = performance_data[row_index][2] s = sc(ra=self.ra * u.degree, dec=self.dec * u.degree).to_string('hmsdms') self.ra_hms = s[0:2] + ':' + s[3:5] + ':' + s[6:13] self.dec_dms = s[15:18] + ':' + s[19:21] + ':' + s[22:29] sort = self.data[:, 0].argsort() mjd = self.data[:, 0][sort] mag = self.data[:, 1][sort] err = self.data[:, 2][sort] self.data = Table([mjd, mag, err], names=['mjd', 'mag', 'err'], masked=True)
## if i[0] == "yr8_1285e07": ## print i[0] ##models=df(d, index=d.get) ##print d['yr8_1285e07'] ##d=None ##print models[0] cur.close() con.close() ucoldefs=['ra','de','urat1', 'J','e_J','K','e_K','H','e_H'] con=p2.connect("dbname='stars' user='******' host='localhost'") cur=con.cursor() cur.execute("select ra, dec, mem from alejandroslist where mem > 50") ajl=df(cur.fetchall(), columns=['ra','de','mem']) cur.execute("select raj2000, dej2000, urat1, jmag, e_jmag, kmag, e_kmag, hmag, e_hmag from urat where jmag-kmag<0.6") urat=df(cur.fetchall(),columns=ucoldefs) usc=sc(urat['ra'],urat['de'],unit='deg') asc=sc(ajl['ra'],ajl['de'],unit='deg') matches, dist = mc(asc, usc, 3.0, 0) match_df=df(columns=ucoldefs+['mem','col']) for m in matches: tmp=sc(urat['ra'][m],urat['de'][m],unit='deg') temp_match,na=mc(tmp, asc, 3.0, 0) if temp_match and len(temp_match)>0: tdf=df([urat[ucoldefs].values[m]],columns=ucoldefs) mem=ajl['mem'].values[temp_match[0]] col=['#%02x%02x%02x' % (0,int(255*(.01*mem)),0)] tdf['mem']=mem tdf['col']=col match_df=match_df.append(tdf) match_df=match_df.set_index('urat1',drop = False) yrsplit=re.compile(r'yr(\d)_(\d{4})e(\d{2})')
# [ { "ra": 0.0, "dec": 0.0 }, { "ra": 0.5, "dec": 0.0 } ], # [ { "ra": 0.0, "dec": 10.0 }, { "ra": 0.5, "dec": 10.0 } ], # [ { "ra": 0.0, "dec": 20.0 }, { "ra": 0.5, "dec": 20.0 } ], # [ { "ra": 0.0, "dec": 30.0 }, { "ra": 0.5, "dec": 30.0 } ], # [ { "ra": 0.0, "dec": 40.0 }, { "ra": 0.5, "dec": 40.0 } ], # [ { "ra": 0.0, "dec": 50.0 }, { "ra": 0.5, "dec": 50.0 } ], # [ { "ra": 0.0, "dec": -50.0 }, { "ra": 0.5, "dec": -50.0 } ], # [ { "ra": 0.0, "dec": 60.0 }, { "ra": 0.5, "dec": 60.0 } ], # [ { "ra": 0.0, "dec": 70.0 }, { "ra": 0.5, "dec": 70.0 } ], # [ { "ra": 0.0, "dec": 80.0 }, { "ra": 0.5, "dec": 80.0 } ], ] for dat in data: # https://docs.astropy.org/en/stable/api/astropy.coordinates.SkyCoord.html#astropy.coordinates.SkyCoord star = [ sc(ra=d["ra"], dec=d["dec"], unit="deg", frame="icrs") for d in dat ] print("s0:", star[0].spherical) print("s1:", star[1].spherical) print(" Separation [deg]: %.4f" % star[1].separation(star[0]).deg) for i in range(len(star)): print(" star", i + 1, ":", star[i].to_string("hmsdms"), "||", star[1].ra.hour, star[1].dec.deg, "||", star[1].ra.radian, star[1].dec.radian) # longitude // theta [0, 2pi] lon = [s.ra.radian for s in star] # latitude // phi [0, pi] lat = [s.dec.radian for s in star]
def update_position(self): skycoordinates = sc(self.ra, self.dec, unit=(self.u_ra, self.u_dec)) err = sc(self.dra, self.ddec, unit=(self.u_ra, self.u_dec)) self.coord = skycoordinates self.err_coord = err