ffile = fits.open(paths.dpath('w51_te_continuum_best.fits')) mywcs = wcs.WCS(ffile[0].header) img = np.isfinite(ffile[0].data) x, y = img.max(axis=0), img.max(axis=1) xlim = np.where(x)[0].min(), np.where(x)[0].max() ylim = np.where(y)[0].min(), np.where(y)[0].max() fig2 = pl.figure(2) fig2.clf() ax2 = fig2.add_subplot(1, 1, 1, projection=mywcs) ax2.imshow(ffile[0].data, cmap='gray_r', vmax=0.025, vmin=0.0001) not_freefree = ~(cores_merge['is_freefree'].astype('bool')) coords = coordinates.SkyCoord(cores_merge['RA'], cores_merge['Dec'], frame='fk5')[not_freefree] ax2.contour(img, levels=[0.5], colors='k') sc = ax2.scatter(coords.ra.deg, coords.dec.deg, marker='.', s=30 * cores_merge['T_corrected_peakmass'][not_freefree], transform=ax2.get_transform('fk5'), c=cores_merge['mean_velo'][not_freefree], edgecolors='k', alpha=0.5, cmap=pl.cm.jet) cb = pl.colorbar(mappable=sc, ax=ax2) cb.set_label('Velocity (km s$^{-1}$)') ax2.set_xlim(*xlim) ax2.set_ylim(*ylim)
def radec_hmstodd(ra, dec): """ Function to convert HMS values into decimal degrees. This function relies on the astropy.coordinates package to perform the conversion to decimal degrees. Parameters ---------- ra : list or array List or array of input RA positions dec : list or array List or array of input Dec positions Returns ------- pos : arr Array of RA,Dec positions in decimal degrees Notes ----- This function supports any specification of RA and Dec as HMS or DMS; specifically, the formats:: ["nn","nn","nn.nn"] "nn nn nn.nnn" "nn:nn:nn.nn" "nnH nnM nn.nnS" or "nnD nnM nn.nnS" See Also -------- astropy.coordinates """ hmstrans = string.maketrans(string.ascii_letters, ' ' * len(string.ascii_letters)) if isinstance(ra, list): rastr = ':'.join(ra) elif isinstance(ra, float): rastr = None pos_ra = ra elif ra.find(':') < 0: # convert any non-numeric characters to spaces (we already know the units) rastr = ra.translate(hmstrans).strip() rastr = rastr.replace(' ', ' ') # convert 'nn nn nn.nn' to final 'nn:nn:nn.nn' string rastr = rastr.replace(' ', ':') else: rastr = ra if isinstance(dec, list): decstr = ':'.join(dec) elif isinstance(dec, float): decstr = None pos_dec = dec elif dec.find(':') < 0: decstr = dec.translate(hmstrans).strip() decstr = decstr.replace(' ', ' ') decstr = decstr.replace(' ', ':') else: decstr = dec if rastr is None: pos = (pos_ra, pos_dec) else: #pos = coords.Position(rastr+' '+decstr,units='hours') #return pos.dd() pos_coord = coords.SkyCoord(rastr + ' ' + decstr, unit=(u.hourangle, u.deg)) pos = (pos_coord.ra.deg, pos_coord.dec.deg) return pos
import numpy as np from astroquery.sdss import SDSS from astroquery.ned import Ned from astropy import coordinates as coords import astropy.units as u from matplotlib import pyplot as plt from astropy.io import fits import wget from scipy.stats import scoreatpercentile import tarfile galaxy_name = 'NGC5012' result_table = Ned.query_object(galaxy_name) pos = coords.SkyCoord(ra=result_table['RA(deg)'][0] * u.deg, dec=result_table['DEC(deg)'][0] * u.deg, frame='icrs') baseurl = 'http://unwise.me/cutout_fits?version=allwise' ra = pos.ra.deg dec = pos.dec.deg wisefilter = '3' # 12 micron imsize = '100' # max size = 256 pixels bands = '34' #version=neo1&ra=41&dec=10&size=100&bands=12 imurl = baseurl + '&ra=%.5f&dec=%.5f&size=%s&bands=%s' % (ra, dec, imsize, bands) # this will download a tar file wisetar = wget.download(imurl) tartemp = tarfile.open(wisetar, mode='r:gz') #mode='r:gz'
def __init__(self, parts, indices): # Do RAJ and DECJ first posn = c.SkyCoord(parts[indices['RAJ']]+" "+parts[indices['DECJ']], frame=c.ICRS, unit=(u.hourangle, u.deg)) for param in params: part_index = indices[param] if param=="NAME": if not parts[part_index]=='*': self.name = parts[part_index][1:] else: self.name = "" elif param=="PSRJ": if not parts[part_index]=='*': self.jname = parts[part_index][1:] if self.name == self.jname: self.name = "" elif param=="RAJ": if not parts[part_index]=='*': self.rajstr = parts[part_index] self.ra = posn.ra.to(u.rad).value self.raerr = float(parts[part_index+1]) * pc.SECTORAD elif param=="DECJ": if not parts[part_index]=='*': self.decjstr = parts[part_index] self.dec = posn.dec.to(u.rad).value self.decerr = float(parts[part_index+1]) * pc.ARCSECTORAD elif param=="PMRA": if not parts[part_index]=='*': self.pmra, self.pmraerr = float(parts[part_index]), float(parts[part_index+1]) elif param=="PMDEC": if not parts[part_index]=='*': self.pmdec, self.pmdecerr = float(parts[part_index]), float(parts[part_index+1]) elif param=="PX": if not parts[part_index]=='*': self.px, self.pxerr = float(parts[part_index]), float(parts[part_index+1]) elif param=="POSEPOCH": if not parts[part_index]=='*': self.posepoch = float(parts[part_index]) elif param=="Gl": if not parts[part_index]=='*': self.l = float(parts[part_index]) elif param=="Gb": if not parts[part_index]=='*': self.b = float(parts[part_index]) elif param=="F0": if not parts[part_index]=='*': self.f, self.ferr = float(parts[part_index]), float(parts[part_index+1]) self.p, self.perr = pu.pferrs(self.f, self.ferr) else: self.f = self.ferr = self.p = self.perr = 0.0 self.fd = self.fdd = self.fddd = 0.0 self.pd = self.pdd = self.pddd = 0.0 self.fderr = self.fdderr = self.fddderr = 0.0 self.pderr = self.pdderr = self.pddderr = 0.0 elif param=="F1": if not parts[part_index]=='*': self.fd, self.fderr = float(parts[part_index]), float(parts[part_index+1]) self.p, self.perr, self.pd, self.pderr = pu.pferrs(self.f, self.ferr, self.fd, self.fderr) elif param=="F2": if not parts[part_index]=='*': self.fdd, self.fdderr = float(parts[part_index]), float(parts[part_index+1]) self.p, self.pd, self.pdd = presto.p_to_f(self.f, self.fd, self.fdd) elif param=="F3": if not parts[part_index]=='*': self.fddd, self.fddderr = float(parts[part_index]), float(parts[part_index+1]) elif param=="PEPOCH": if parts[part_index]=='*': self.pepoch = 51000.0 # Just to pick a reasonable value else: self.pepoch = float(parts[part_index]) elif param=="DM": if not parts[part_index]=='*': self.dm, self.dmerr = float(parts[part_index]), float(parts[part_index+1]) else: self.dm = self.dmerr = 0.0 elif param=="DM1": if not parts[part_index]=='*': self.ddm, self.ddmerr = float(parts[part_index]), float(parts[part_index+1]) elif param=="S400": if not parts[part_index]=='*': self.s400, self.s400err = float(parts[part_index]), float(parts[part_index+1]) else: self.s400 = None elif param=="S1400": if not parts[part_index]=='*': self.s1400, self.s1400err = float(parts[part_index]), float(parts[part_index+1]) else: self.s1400 = None elif param=="BINARY": if not parts[part_index]=='*': self.binary_model = parts[part_index] self.binary = 1 self.pb = self.x = self.w = self.To = self.e = None self.pberr = self.xerr = self.werr = self.Toerr =self.eerr = None else: self.binary = 0 elif param=="T0": if self.binary and not parts[part_index]=='*': self.To, self.Toerr = float(parts[part_index]), float(parts[part_index+1]) elif param=="PB": if self.binary and not parts[part_index]=='*': self.pb, self.pberr = float(parts[part_index]), float(parts[part_index+1]) elif param=="A1": if self.binary and not parts[part_index]=='*': self.x, self.xerr = float(parts[part_index]), float(parts[part_index+1]) elif param=="OM": if self.binary and not parts[part_index]=='*': self.w, self.werr = float(parts[part_index]), float(parts[part_index+1]) elif param=="ECC": if self.binary and not parts[part_index]=='*': self.e, self.eerr = float(parts[part_index]), float(parts[part_index+1]) elif param=="TASC": if self.binary and self.binary_model=="ELL1" and not parts[part_index]=='*': self.To, self.Toerr = float(parts[part_index]), float(parts[part_index+1]) elif param=="EPS1": if self.binary and self.binary_model=="ELL1" and not parts[part_index]=='*': self.eps1, self.eps1err = float(parts[part_index]), float(parts[part_index+1]) elif param=="EPS2": if self.binary and self.binary_model=="ELL1" and not parts[part_index]=='*': self.eps2, self.eps2err = float(parts[part_index]), float(parts[part_index+1]) if not hasattr(self, 'eps1'): self.eps1 = 0.0 self.e = math.sqrt(self.eps1*self.eps1 + self.eps2*self.eps2) self.eerr = 0.0001 # This needs fixing... self.w = pc.RADTODEG*math.atan2(self.eps1, self.eps2) if (self.w < 0.0): self.w += 360.0 self.werr = 1.0 # This needs fixing... elif param=="DIST": if not parts[part_index]=='*': self.dist = float(parts[part_index]) else: self.dist = None elif param=="ASSOC": if not parts[part_index]=='*': self.assoc = parts[part_index] else: self.assoc = None elif param=="SURVEY": if not parts[part_index]=='*': self.survey = parts[part_index] else: self.survey = None elif param=="PSR": if not parts[part_index]=='*': self.type = parts[part_index] else: self.type = None self.alias = ""
def lrsFieldSim(ra, dec, binComp=''): """ Produce a Grism Time Series field simulation for a target. Parameters ---------- ra : float The RA of the target. dec : float The Dec of the target. binComp : sequence The parameters of a binary companion. Returns ------- simuCube : np.ndarray The simulated data cube. Index 0 and 1 (axis=0) show the trace of the target for orders 1 and 2 (respectively). Index 2-362 show the trace of the target at every position angle (PA) of the instrument. """ # Calling the variables dimX = 55 dimY = 427 rad = 2.5 pixel_scale = 0.11 # arsec xval, yval = 38.5, 829.0 add_to_apa = 4.83425324 # stars in large field around target targetcrd = crd.SkyCoord(ra=ra, dec=dec, unit=(u.hour, u.deg)) targetRA = targetcrd.ra.value targetDEC = targetcrd.dec.value info = Irsa.query_region(targetcrd, catalog='fp_psc', spatial='Cone', radius=rad * u.arcmin) # Coordinates of all the stars in FOV, including target allRA = info['ra'].data.data allDEC = info['dec'].data.data Jmag = info['j_m'].data.data Hmag = info['h_m'].data.data Kmag = info['k_m'].data.data J_Hobs = Jmag - Hmag H_Kobs = Hmag - Kmag # Coordiniates of target aa = ((targetRA - allRA) * np.cos(targetDEC)) distance = np.sqrt(aa**2 + (targetDEC - allDEC)**2) targetIndex = np.argmin(distance) # the target # Add any missing companion if binComp != '': deg2rad = np.pi / 180 bb = binComp[0] / 3600 / np.cos(allDEC[targetIndex] * deg2rad) allRA = np.append(allRA, (allRA[targetIndex] + bb)) allDEC = np.append(allDEC, (allDEC[targetIndex] + binComp[1] / 3600)) Jmag = np.append(Jmag, binComp[2]) Hmag = np.append(Kmag, binComp[3]) Kmag = np.append(Kmag, binComp[4]) J_Hobs = Jmag - Hmag H_Kobs = Hmag - Kmag # Number of stars nStars = allRA.size # Restoring model parameters modelParam = readsav(os.path.join(TRACES_PATH, 'NIRISS', 'modelsInfo.sav'), verbose=False) models = modelParam['models'] modelPadX = modelParam['modelpadx'] modelPadY = modelParam['modelpady'] dimXmod = modelParam['dimxmod'] dimYmod = modelParam['dimymod'] jhMod = modelParam['jhmod'] hkMod = modelParam['hkmod'] #teffMod = modelParam['teffmod'] teffMod = np.linspace(2000, 6000, 41) # Find/assign Teff of each star starsT = np.empty(nStars) for j in range(nStars): color_separation = (J_Hobs[j] - jhMod)**2 + (H_Kobs[j] - hkMod)**2 min_separation_ind = np.argmin(color_separation) starsT[j] = teffMod[min_separation_ind] radeg = 180 / np.pi sweetSpot = dict(x=xval, y=yval, RA=allRA[targetIndex], DEC=allDEC[targetIndex], jmag=Jmag[targetIndex]) # Offset between all stars and target dRA = (allRA - sweetSpot['RA']) * np.cos(sweetSpot['DEC'] / radeg) * 3600 dDEC = (allDEC - sweetSpot['DEC']) * 3600 # Put field stars positions and magnitudes in structured array _ = dict(RA=allRA, DEC=allDEC, dRA=dRA, dDEC=dDEC, jmag=Jmag, T=starsT, x=np.empty(nStars), y=np.empty(nStars), dx=np.empty(nStars), dy=np.empty(nStars)) stars = np.empty(nStars, dtype=[(key, val.dtype) for key, val in _.items()]) for key, val in _.items(): stars[key] = val # Initialize final fits cube that contains the modelled traces # with contamination PAmin = 0 # instrument PA, degrees PAmax = 360 dPA = 1 # degrees # Set of IPA values to cover PAtab = np.arange(PAmin, PAmax, dPA) # degrees nPA = len(PAtab) # Cube of trace simulation at every degree of field rotation, # +target at O1 and O2 simuCube = np.zeros([nPA + 1, dimY + 1, dimX + 1]) fitsFiles = glob.glob(os.path.join(TRACES_PATH, 'MIRI', '_*.fits')) fitsFiles = np.sort(fitsFiles) # Big loop to generate a simulation at each instrument PA for kPA in range(PAtab.size): APA = PAtab[kPA] # Aperture Position Angle (PA of instrument) V3PA = APA + add_to_apa # from APT sindx = np.sin(np.pi / 2 + APA / radeg) * stars['dDEC'] cosdx = np.cos(np.pi / 2 + APA / radeg) * stars['dDEC'] ps = pixel_scale stars['dx'] = (np.cos(np.pi / 2 + APA / radeg) * stars['dRA'] - sindx) / ps stars['dy'] = (np.sin(np.pi / 2 + APA / radeg) * stars['dRA'] + cosdx) / ps stars['x'] = stars['dx'] + sweetSpot['x'] stars['y'] = stars['dy'] + sweetSpot['y'] # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~NOTE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Retain stars that are within the Direct Image NIRISS POM FOV # This extends the subarray edges to the detector edges. # It keeps the stars that fall out of the subarray but still # fall into the detector. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ind, = np.where((stars['x'] >= -8000) & (stars['x'] <= dimY + 8000) & (stars['y'] >= -8000) & (stars['y'] <= dimY + 8000)) starsInFOV = stars[ind] for i in range(len(ind)): intx = round(starsInFOV['dx'][i]) inty = round(starsInFOV['dy'][i]) # This indexing assumes that teffMod is # sorted the same way fitsFiles was sorted k = np.where(teffMod == starsInFOV['T'][i])[0][0] fluxscale = 10.0**(-0.4 * (starsInFOV['jmag'][i] - sweetSpot['jmag'])) # deal with subection sizes modelPadX = 0 modelPadY = 0 mx0 = int(modelPadX - intx) mx1 = int(modelPadX - intx + dimX) my0 = int(modelPadY - inty) my1 = int(modelPadY - inty + dimY) if (mx0 > dimX) or (my0 > dimY): continue if (mx1 < 0) or (my1 < 0): continue x0 = (mx0 < 0) * (-mx0) y0 = (my0 < 0) * (-my0) mx0 *= (mx0 >= 0) mx1 = dimX if mx1 > dimX else mx1 my0 *= (my0 >= 0) my1 = dimY if my1 > dimY else my1 # Fleshing out index 0 of the simulation cube (trace of target) if (intx == 0) & (inty == 0) & (kPA == 0): fNameModO12 = fitsFiles[k] modelO1 = fits.getdata(fNameModO12, 1) ord1 = modelO1[0, my0:my1, mx0:mx1] * fluxscale simuCube[0, y0:y0 + my1 - my0, x0:x0 + mx1 - mx0] = ord1 # Fleshing out indexes 1-361 of the simulation cube # (trace of neighboring stars at every position angle) if (intx != 0) or (inty != 0): fNameModO12 = fitsFiles[k] modelO12 = fits.getdata(fNameModO12) simuCube[kPA + 1, y0:y0 + my1 - my0, x0:x0 + mx1 - mx0] += modelO12[0, my0:my1, mx0:mx1] * fluxscale return simuCube
def update(self): """ Check the difference the exoplanet database (EU or NASA) and the SweetCat database INPUTS: self = exoplanet database in pandas dataframe OUTPUTS: names.txt = file with stars that are not in SweetCat but stars are in the exoplanet database (EU or NASA) fname_sc_day-month_hour:minute = new SWEET-Cat dabatase with updated database label Prints the stars that are in SweetCat and that are not in the exoplanet database (EU or NASA) """ # We have this already, but without the ' in the name. print('\n Matching database ...') if self.nasa: print(' NASA exoplanet archive') else: print(' Extrasolar Planets Encyclopaedia') NewStars = [] # Star corrdinates in the exoplanet database coordExo = coord.SkyCoord(ra=self.exoplanet['ra'].values, dec=self.exoplanet['dec'].values, unit=(u.deg, u.deg), frame='icrs') # Star coordinates in the SWEET-Cat database coordSC = coord.SkyCoord(ra=self.coordinates['ra'].values, dec=self.coordinates['dec'].values, unit=(u.hourangle, u.deg), frame='icrs') # ------------------------------------------------------- # Adding stars that are not # in Exoplanet.EU nor NASA exoplanet archive # For stars which already are in one of the database, # only the database label is updated by this script # ------------------------------------------------------- for i, exo_name in enumerate(self.exo_names): starName = exo_name # starIndex = self.exoplanet[self.exoplanet['pl_hostname'] == exo_name].index[0] # starName = self.exoplanet['star_name'].values[i] # Clean star name tmp = starName.lower().replace(' ', '').replace('-', '') # Check if the star in exoplanet database is in SWEET-Cat sep = coordExo[i].separation(coordSC).arcsecond ind = np.where(sep < 5.)[0] # Star is already in SWEET-Cat if len(ind) != 0.: ind_SC = ind[0] # Check if the name of the database (EU and/or NASA) # is written in the SWEET-CAT file, if not it's added if self.nasa: if 'NASA' in self.SC.loc[ind_SC].database: pass else: print('\nChecking star: ', starName, '(found by position)') # print(self.exoplanet.loc[starIndex][['pl_hostname', 'ra_str', 'dec_str']]) # print(self.SC.loc[ind_SC][['name', 'ra', 'dec', 'database', 'n3']]) print(' > adding NASA label') self.SC.at[ind_SC, 'database'] = self.SC.at[ind_SC, 'database'] + ',NASA' else: if 'EU' in self.SC.loc[ind_SC].database: pass else: print('\nChecking star: ', starName, '(found by position)') print(' > adding EU label') self.SC.at[ind_SC, 'database'] = self.SC.at[ind_SC, 'database'] + ',EU' if len(ind) == 0: try: # it didn't find by position but it finds by name position = self.sc_names.index(tmp) # Check the name of the database (EU and/or NASA) if self.nasa: if 'NASA' in self.SC.loc[position].database: pass else: print('\nChecking star: ', starName, i, '(found by name)') print(' > adding NASA label') self.SC.at[position, 'database'] = self.SC.at[position, 'database'] + ',NASA' else: if 'EU' in self.SC.loc[position].database: pass else: print('\nChecking star: ', starName, i, '(found by name)') print(' > adding EU label') self.SC.at[position, 'database'] = self.SC.at[position, 'database'] + ',EU' except: position = -1 # it didn't find by position and neither by name if (tmp not in self.blacklist): NewStars.append(starName) # REMOVE THE COMMENTS TO CHECK PROBLEMS IN POSITION # if position>=0: # result_table = Simbad.query_object(new) # try: # # check in Simbad the position and see where the coordinates are wrong # ra=str(result_table['RA'][0]) # dec=str(result_table['DEC'][0]) # coordS=coord.SkyCoord(ra, dec, unit=(u.hourangle, u.degree), frame='icrs') # sepES = coordExo[i].separation(coordS).arcsecond # sepSCS = coordSC[position].separation(coordS).arcsecond # if sepES>sepSCS: # # print new,': has wrong position in Exoplanet.eu, it is: ',coordExo[i].ra.deg, coordExo[i].dec.deg,', but should be: ',coordS.ra.deg,coordS.dec.deg # pass # else: # print new,': has wrong position in Sweet-Cat, it is: ',coordSC[position].ra.deg, coordSC[position].dec.deg,', but should be: ',coordS.ra.deg,coordS.dec.deg # except: # print 'Star not found in Simbad with this name ',new,'.\n Position in Exoplanet.eu:',coordExo[i].ra.deg, coordExo[i].dec.deg,'\n Position in SC: ',coordSC[position].ra.deg, coordSC[position].dec.deg # with open('starnotfoundinsimbad.list', 'a') as f: # f.write(new+'\n') # REMOVE THE COMMENTS TO CHECK PROBLEMS WITH THE NAMES # else: # position=ind[0] # if (tmp not in self.sc_names): # print new, ': has a different name in Sweet-Cat (',self.sc_names[position],')' NewStars = sorted(list(set(NewStars))) Nstars = len(NewStars) if Nstars: puts(' '+colored.green(str(Nstars) + " new exoplanet available!")) writeFile('names.txt', '\n'.join(NewStars)) updated = False else: puts(colored.clean(' No new updates available.')) updated = True # ------------------------------------------------------- # Removing planets that are not # in Exoplanet.EU and NASA exoplanet archive anymore # ------------------------------------------------------- print('\n Checking for stars to remove ...') NewStars = [] for i, scname in enumerate(self.sc_names_orig): # Clean star name tmp = starName.lower().replace(' ', '').replace('-', '') # Check if the star in SWEET-Cat is in exoplanet database sep = coordSC[i].separation(coordExo).arcsecond ind = np.where(sep < 5.)[0] if len(ind) == 0: try: # it didn't find by position but it finds by name position = self.exo_names_clean.index(tmp) except: # Star in Sweet-Cat is not found # in the exoplanet database (EU or NASA) position = -1 # Check if the star is not from the other database if self.nasa: if 'EU' in self.SC.loc[i].database: # Star is in EU # Star will not be removed from SWEET-Cat if 'NASA' in self.SC.loc[i].database and scname != 'Barnards': # Removing NASA label from database column print(' > Removing NASA label') self.SC.at[i, 'database'] = self.SC.at[i, 'database'].replace('NASA', '').replace(',', '') continue else: if 'NASA' in self.SC.loc[i].database: # Star is in NASA # Star will not be removed from SWEET-Cat if 'EU' in self.SC.loc[i].database and scname != 'Barnards': # Removing EU label from database column print(scname) print(' > Removing EU label') self.SC.at[i, 'database'] = self.SC.at[i, 'database'].replace('EU', '').replace(',', '') continue # it didn't find by position and neither by name # star is not from the other database if (tmp not in self.blacklist): NewStars.append(scname) NewStars = sorted(list(set(NewStars))) Nstars = len(NewStars) if Nstars: puts(colored.green(' '+str(Nstars) + " exoplanet has to be removed!")) print('\n '.join(NewStars)) else: puts(colored.clean('\n No star to remove.')) if updated: puts(colored.clean(' SWEET-Cat is up to date')) puts(colored.green(' Great job :)')) # Date and time timestr = time.strftime("%d-%m-%H:%M") filename = os.path.splitext(self.fname_sc)[0] + '_' + timestr + '.rdb' # Convert Tefferr column to integers self.SC['Tefferr'] = self.SC['Tefferr'].fillna('-111111') self.SC['Tefferr'] = self.SC['Tefferr'].astype(int).replace(-111111, 'NULL') # Replace NaN by NULL self.SC.fillna(value='NULL', inplace=True) # Write new SWEET-Cat database print('\n Writing the file: ', filename) self.SC.to_csv(filename, sep='\t', index=False, header=False)
not_found = False else: j+=1 newresults = np.append(results[int(matches[0])],results[int(matches[1])]) k = 2 while k <= len(matches)-1: newresults = np.append(newresults,results[int(matches[k])]) k+=1 results = newresults distances = 1000/results['parallax'] #Convert coordinates to galactic and then to cartesian. coordinates_ICRS = asc.SkyCoord(ra=results['ra']*u.degree, dec=results['dec']*u.degree, distance=distances*u.pc, pm_ra_cosdec=results['pmra']*u.mas/u.yr, pm_dec=results['pmdec']*u.mas/u.yr, frame='icrs', obstime='J2015.5') #coordinates_ICRS = asc.ICRS(ra=results['ra']*u.degree, dec=results['dec']*u.degree, distance=distances*u.pc, pm_ra_cosdec=results['pmra']*u.mas/u.yr, pm_dec=results['pmdec']*u.mas/u.yr) coordinates_galactic = coordinates_ICRS.galactic #coordinates_galactic = asc.SkyCoord(l=results['l']*u.degree, b=results['b']*u.degree, distance=distances*u.pc, pm_ra_cosdec=results['pmra']*u.mas/u.yr, pm_dec=results['pmdec']*u.mas/u.yr, radial_velocity=results['radial_velocity']*u.km/u.s, frame='galactic', obstime='J2015.5') #coordinates_galactic = asc.SkyCoord(l=results['l']*u.degree, b=results['b']*u.degree, distance=distances*u.pc, frame='galactic', obstime='J2015.5') coordinates_cartesian = np.column_stack((coordinates_galactic.cartesian.x.value, coordinates_galactic.cartesian.y.value, coordinates_galactic.cartesian.z.value)) x = coordinates_cartesian[:,0]#.filled(0) y = coordinates_cartesian[:,1]#.filled(0) z = coordinates_cartesian[:,2]#.filled(0) #counts,xbins,ybins,image = plt.hist2d(distances*(4.74 * 10**-3)*results['pmra'],distances*(4.74 * 10**-3)*results['pmdec'],bins=60,normed=True,norm=LogNorm(), cmap = 'Blues') #counts,xbins,ybins,image = plt.hist2d(results['pmra'],results['pmdec'],bins=40,normed=True,norm=LogNorm(),cmap = 'Blues')
result_dir = '../Tables/' outfile = result_dir + 'iracysosStats.dat' ysoStats = open(outfile, 'w') fmt = '%18s %6i \n' pfmt = '%6i %18s %6i %6.2f%% finished.' start = time.time() Irsa.ROW_LIMIT = 100000000 Irsa.TIMEOUT = 10000 for isou in range(len(sourList)): source = sourList['Name'][isou] ra = sourList['ra'][isou] dec = sourList['dec'][isou] radius = sourList['amaj'][isou] / 2.0 coor = coords.SkyCoord(ra, dec, unit=(u.degree, u.degree), frame="icrs") if (coor.galactic.l.deg > 10.8 and coor.galactic.l.deg < 349.2): raw_psc = Irsa.query_region(coor, catalog='glimpse_s07', spatial="Cone", radius=radius * u.arcsec) else: raw_psc = Irsa.query_region(coor, catalog='glimpse2_v2cat', spatial="Cone", radius=radius * u.arcsec) raw_psc['mag3_6'].mask[raw_psc['d3_6m'] >= 0.2] = True raw_psc['mag4_5'].mask[raw_psc['d4_5m'] >= 0.2] = True raw_psc['color36_45'].mask[raw_psc['d3_6m'] >= 0.2] = True raw_psc['color36_45'].mask[raw_psc['d4_5m'] >= 0.2] = True
def test_from_fiducial_sky(): sky = coord.SkyCoord(1.63 * u.radian, -72.4 * u.deg, frame='fk5') tan = models.Pix2Sky_TAN() w = wcs_from_fiducial(sky, projection=tan) assert isinstance(w.CelestialFrame.reference_frame, coord.FK5) assert_allclose(w(.1, .1), (93.7210280925364, -72.29972666307474))
def test_mast_service_request(patch_post): service = 'Mast.Caom.Cone' params = {'ra': 23.34086, 'dec': 60.658, 'radius': 0.2} result = mast.Mast.service_request(service, params) assert isinstance(result, Table) ########################### # ObservationsClass tests # ########################### regionCoords = coord.SkyCoord(23.34086, 60.658, unit=('deg', 'deg')) # query functions def test_observations_query_region_async(patch_post): responses = mast.Observations.query_region_async(regionCoords, radius=0.2) assert isinstance(responses, list) def test_observations_query_region(patch_post): result = mast.Observations.query_region(regionCoords, radius=0.2 * u.deg) assert isinstance(result, Table) def test_observations_query_object_async(patch_post):
import pylab as pl from spectral_cube import SpectralCube from astropy import units as u from astropy import coordinates from astropy import wcs from astropy.io import fits import pyregion import pvextractor from pvextractor import extract_pv_slice from pvextractor.geometry import Path from line_point_offset import offset_to_point e8mm = coordinates.SkyCoord(290.93289, 14.507833, frame='fk5', unit=(u.deg, u.deg)) e8blue_endpoint = coordinates.SkyCoord(290.93538, 14.506403, frame='fk5', unit=(u.deg, u.deg)) e8red_endpoint = coordinates.SkyCoord(290.92946, 14.509999, frame='fk5', unit=(u.deg, u.deg)) endpoints = coordinates.SkyCoord([e8blue_endpoint, e8red_endpoint]) e8flowxy = Path(endpoints, width=1.5 * u.arcsec) # e2 e2ereg = pyregion.open(paths.rpath('w51e2e.reg'))[0] w51e2e = coordinates.SkyCoord(e2ereg.coord_list[0] * u.deg,
if __name__ == "__main__": for sourcename,pfx,regfn in (('e2','e2e','e2_exclude_e2w.reg'), ('e8','e8','e8_core.reg'), ('north','north','north_core.reg')): #coordinate = coordinates.SkyCoord("19:23:43.961", # "+14:30:34.56", # frame='fk5', # unit=(u.hour, u.deg)) bins_ends_arcsec = np.linspace(0,2.25,7) bins_arcsec = np.array(list(zip(bins_ends_arcsec[:-1], bins_ends_arcsec[1:]))) reg = pyregion.open(paths.rpath(regfn)) coordinate = coordinates.SkyCoord(reg[0].coord_list[0], reg[0].coord_list[1], frame='fk5', unit=(u.deg, u.deg)) for spw in (0,1,2,3): for suffix in ('','_hires'): cubefn = paths.dpath('merge/fullcube_cutouts/{2}cutout_full_W51_7m12m_spw{0}{1}_lines.fits' .format(spw,suffix,sourcename)) print(cubefn) spectra = spectra_from_cubefn(cubefn, reg, bins_arcsec, coordinate) for bins, (key, spectrum) in zip(bins_arcsec, spectra.items()): if hasattr(spectrum, 'beams'): include = np.isfinite(spectrum) & np.array([(bm.major < 1*u.arcsec) &
mp.setattr(fermi.FermiLAT, '_request', post_mockreturn) mp.setattr(requests, 'post', post_mockreturn) return mp def post_mockreturn(method="POST", url=None, data=None, timeout=50, **kwargs): if data is not None: with open(data_path(DATA_FILES['async']), 'rb') as r: response = MockResponse(r.read(), **kwargs) else: with open(data_path(DATA_FILES['result']), 'rb') as r: response = MockResponse(r.read(), **kwargs) return response FK5_COORDINATES = coord.SkyCoord(10.68471, 41.26875, unit=('deg', 'deg')) # disable waiting so tests run fast fermi.core.get_fermilat_datafile.TIMEOUT = 1 fermi.core.get_fermilat_datafile.check_frequency = 0 def test_FermiLAT_query_async(patch_post): result = fermi.core.FermiLAT.query_object_async( FK5_COORDINATES, energyrange_MeV='1000, 100000', obsdates='2013-01-01 00:00:00, 2013-01-02 00:00:00') assert result == DATA_FILES['result_url'] def test_getfermilatdatafile(patch_post):
width=2 * u.arcmin) assert response is not None @pytest.mark.parametrize(("coordinates"), OBJ_LIST) def test_query_region_box(coordinates, patch_get): result = irsa.core.Irsa.query_region(coordinates, catalog='fp_psc', spatial='Box', width=2 * u.arcmin) assert isinstance(result, Table) poly1 = [ coord.SkyCoord(ra=10.1, dec=10.1, unit=(u.deg, u.deg)), coord.SkyCoord(ra=10.0, dec=10.1, unit=(u.deg, u.deg)), coord.SkyCoord(ra=10.0, dec=10.0, unit=(u.deg, u.deg)) ] poly2 = [(10.1 * u.deg, 10.1 * u.deg), (10.0 * u.deg, 10.1 * u.deg), (10.0 * u.deg, 10.0 * u.deg)] @pytest.mark.parametrize(("polygon"), [poly1, poly2]) def test_query_region_async_polygon(polygon, patch_get): response = irsa.core.Irsa.query_region_async("m31", catalog="fp_psc", spatial="Polygon", polygon=polygon, get_query_payload=True)
def pbcorr(modelname): import numpy as np import astropy.io.fits as fits from astropy import wcs from pyuvdata import UVBeam, utils as uvutils import os import sys import glob import argparse import shutil import copy import healpy import scipy.stats as stats from casa_imaging import casa_utils from scipy import interpolate from astropy.time import Time from astropy import coordinates as crd from astropy import units as u _fitsfiles = ["{}.fits".format(modelname)] # PB args _multiply = True _lon = p.longitude _lat = p.latitude _time = p.time # beam args _beamfile = p.beamfile _pols = -5, -6 _freq_interp_kind = 'cubic' # IO args _ext = '' _outdir = p.out_dir _overwrite = True _silence = False _spec_cube = False def echo(message, type=0): if verbose: if type == 0: print(message) elif type == 1: print('\n{}\n{}'.format(message, '-' * 40)) verbose = _silence == False # load pb echo("...loading beamfile {}".format(_beamfile)) # load beam uvb = UVBeam() uvb.read_beamfits(_beamfile) if uvb.pixel_coordinate_system == 'healpix': uvb.interpolation_function = 'healpix_simple' else: uvb.interpolation_function = 'az_za_simple' uvb.freq_interp_kind = _freq_interp_kind # get beam models and beam parameters beam_freqs = uvb.freq_array.squeeze() / 1e6 Nbeam_freqs = len(beam_freqs) # iterate over FITS files for i, ffile in enumerate(_fitsfiles): # create output filename if _outdir is None: output_dir = os.path.dirname(ffile) else: output_dir = _outdir output_fname = os.path.basename(ffile) output_fname = os.path.splitext(output_fname) if _ext is not None: output_fname = output_fname[0] + '.pbcorr{}'.format( _ext) + output_fname[1] else: output_fname = output_fname[0] + '.pbcorr' + output_fname[1] output_fname = os.path.join(output_dir, output_fname) # check for overwrite if os.path.exists(output_fname) and _overwrite is False: raise IOError("{} exists, not overwriting".format(output_fname)) # load hdu echo("...loading {}".format(ffile)) hdu = fits.open(ffile) # get header and data head = hdu[0].header data = hdu[0].data # get polarization info ra, dec, pol_arr, data_freqs, stok_ax, freq_ax = casa_utils.get_hdu_info( hdu) Ndata_freqs = len(data_freqs) # get axes info npix1 = head["NAXIS1"] npix2 = head["NAXIS2"] nstok = head["NAXIS{}".format(stok_ax)] nfreq = head["NAXIS{}".format(freq_ax)] # replace with forced polarization if provided if _pols is not None: pol_arr = np.asarray(_pols, dtype=np.int) pols = [uvutils.polnum2str(pol) for pol in pol_arr] # make sure required pols exist in maps if not np.all([p in uvb.polarization_array for p in pol_arr]): raise ValueError( "Required polarizationns {} not found in Beam polarization array" .format(pol_arr)) # convert from equatorial to spherical coordinates loc = crd.EarthLocation(lat=_lat * u.degree, lon=_lon * u.degree) time = Time(_time, format='jd', scale='utc') equatorial = crd.SkyCoord(ra=ra * u.degree, dec=dec * u.degree, frame='fk5', location=loc, obstime=time) altaz = equatorial.transform_to('altaz') theta = np.abs(altaz.alt.value - 90.0) phi = altaz.az.value # convert to radians theta *= np.pi / 180 phi *= np.pi / 180 if i == 0 or _spec_cube is False: # evaluate primary beam echo("...evaluating PB") pb, _ = uvb.interp(phi.ravel(), theta.ravel(), polarizations=pols, reuse_spline=True) pb = np.abs(pb.reshape((len(pols), Nbeam_freqs) + phi.shape)) # interpolate primary beam onto data frequencies echo("...interpolating PB") pb_shape = (pb.shape[1], pb.shape[2]) pb_interp = interpolate.interp1d(beam_freqs, pb, axis=1, kind=_freq_interp_kind, fill_value='extrapolate')(data_freqs / 1e6) # data shape is [naxis4, naxis3, naxis2, naxis1] if freq_ax == 4: pb_interp = np.moveaxis(pb_interp, 0, 1) # divide or multiply by primary beam if _multiply is True: echo("...multiplying PB into image") data_pbcorr = data * pb_interp else: echo("...dividing PB into image") data_pbcorr = data / pb_interp # change polarization to interpolated beam pols head["CRVAL{}".format(stok_ax)] = pol_arr[0] if len(pol_arr) == 1: step = 1 else: step = np.diff(pol_arr)[0] head["CDELT{}".format(stok_ax)] = step head["NAXIS{}".format(stok_ax)] = len(pol_arr) echo("...saving {}".format(output_fname)) fits.writeto(output_fname, data_pbcorr, head, overwrite=True) output_pb = output_fname.replace(".pbcorr.", ".pb.") echo("...saving {}".format(output_pb)) fits.writeto(output_pb, pb_interp, head, overwrite=True) return
def test_query_region_async(self): response = nrao.core.Nrao.query_region_async( coord.SkyCoord("04h33m11.1s 05d21m15.5s"), retry=5) assert response is not None assert response.content
return tmap, Nmap if __name__ == "__main__": import pylab as pl pl.matplotlib.rc_file('pubfiguresrc') # sigma ~0.055 - 0.065 detection_threshold_jykms = 0.065 * 2 approximate_jytok = 221 sources = { 'e2': coordinates.SkyCoord('19:23:43.963', '+14:30:34.53', frame='fk5', unit=(u.hour, u.deg)), 'e8': coordinates.SkyCoord('19:23:43.891', '+14:30:28.13', frame='fk5', unit=(u.hour, u.deg)), 'ALMAmm14': coordinates.SkyCoord('19:23:38.571', '+14:30:41.80', frame='fk5', unit=(u.hour, u.deg)), 'north': coordinates.SkyCoord('19:23:39.906', '+14:31:05.33', frame='fk5',
def test_query_region(self): result = nrao.core.Nrao.query_region( coord.SkyCoord("04h33m11.1s 05d21m15.5s"), retry=5) assert isinstance(result, Table) # I don't know why this is byte-typed assert b'0430+052' in result['Source']
print('Smoothing') gaussian = gf(a, sigma=sigma, truncate=3) b = 2*(expit(spread*gaussian)-0.5) from mayavi import mlab import moviepy.editor as mpy import astropy.units as u from astropy.coordinates.sky_coordinate import SkyCoord from astropy.units import Quantity from astropy.table import Table import astropy.coordinates as asc #gammavelorum_ICRS = asc.SkyCoord(ra='08h09m31.95013s', dec=(-47-(20/60)-(11.7108/3600))*u.degree, distance=336*u.pc, frame='icrs') #gammavelorum_galactic = gammavelorum_ICRS.galactic gammavelorum_galactic = asc.SkyCoord(l=262.8025*u.degree, b=-7.6858*u.degree, distance=336*u.pc, frame='galactic') #NGC2547_ICRS = asc.SkyCoord(ra=360*(8/24 + 10.7/(24*60))*u.degree,dec=-(49+16/60)*u.degree, distance=410*u.pc, frame='icrs') #NGC2547_galactic = NGC2547_ICRS.galactic NGC2547_galactic = asc.SkyCoord(l=264.465*u.degree, b=-8.597*u.degree, distance=410*u.pc, frame='galactic') #trumpler10_ICRS = asc.SkyCoord(ra=360*(8/24+47.8/(24*60))*u.degree, dec=-(42+29/60)*u.degree, distance=366*u.pc, frame='icrs') #trumpler10_galactic = trumpler10_ICRS.galactic trumpler10_galactic = asc.SkyCoord(l=262.791*u.degree, b=0.674*u.degree, distance=366*u.pc, frame='galactic') #NGC2516_ICRS = asc.SkyCoord(ra=360*(7/24+58/(24*60)+20/(24*3600))*u.degree, dec=-(60+52/60)*u.degree, distance=373*u.pc, frame='icrs') #NGC2516_galactic = NGC2516_ICRS.galactic NGC2516_galactic = asc.SkyCoord(l=273.816*u.degree, b=-15.856*u.degree, distance=373*u.pc, frame='galactic') velaOB2_galactic = asc.SkyCoord(l=263*u.degree, b=-7*u.degree, distance=410*u.pc, frame='galactic') OriOB1_galactic = asc.SkyCoord(l=206.91*u.degree, b=-17.59*u.degree, distance=400*u.pc, frame='galactic') #HIP22931_ICRS = asc.SkyCoord(ra=360*(4/24+55/(24*60)+14/(24*3600))*u.degree, dec=-(5+9.8/60)*u.degree, distance=180*u.pc, frame='icrs') #HIP22931_galactic = HIP22931_ICRS.galactic
def write_inf_file(datfn, hdr, hdrlen): """Write a PRESTO .inf file given a .dat file and a dictionary of SIGPROC-style header values. Inputs: datfn: The PRESTO .dat file to write a .inf file for. hdr: A dictionary of SIGPROC header values, as produced by PRESTO's sigproc.read_header. hdrlen: Length (in bytes) of SIGPROC file's header. Output: inffn: The corresponding .inf file that is created. """ if not datfn.endswith(".dat"): raise ValueError("Was expecting a file name ending with '.dat'. " "Got: %s" % datfn) size = os.path.getsize(datfn) if size % 4: raise ValueError("Bad size (%d bytes) for PRESTO .dat file (%s)" "Should be multiple of 4 because samples are " "32-bit floats." % (size, datfn)) N = size / 4 # Number of samples pos = coords.SkyCoord(sigproc.ra2radians(hdr['src_raj']), sigproc.dec2radians(hdr['src_dej']), frame='icrs', unit='rad') rastr, decstr = pos.to_string('hmsdms', sep=':', precision=4, pad=True).split() inffn = datfn[:-4] + ".inf" with open(inffn, 'w') as ff: ff.write(" Data file name without suffix = %s\n" % os.path.basename(datfn)) ff.write(" Telescope used = %s\n" % sigproc.ids_to_telescope[hdr['telescope_id']]) ff.write(" Instrument used = %s\n" % sigproc.ids_to_machine.get('machine_id', 'UNKNOWN')) ff.write(" Object being observed = %s\n" % hdr['source_name']) ff.write(" J2000 Right Ascension (hh:mm:ss.ssss) = %s\n" % rastr) ff.write(" J2000 Declination (dd:mm:ss.ssss) = %s\n" % decstr) ff.write(" Data observed by = UNKNOWN\n") ff.write(" Epoch of observation (MJD) = %05.15f\n" % hdr['tstart']) ff.write(" Barycentered? (1=yes, 0=no) = %d\n" % hdr['barycentric']) ff.write(" Number of bins in the time series = %d\n" % N) ff.write(" Width of each time series bin (sec) = %.15g\n" % hdr['tsamp']) ff.write(" Any breaks in the data? (1 yes, 0 no) = 0\n") if hdr.has_key('pulsarcentric'): ff.write(" Orbit removed? (1=yes, 0=no) = %d\n" % hdr['pulsarcentric']) ff.write(" Dispersion measure (cm-3 pc) = %f\n" % hdr['refdm']) ff.write(" Central freq of low channel (Mhz) = %f\n" % hdr['fch1']) if hdr.has_key('foff'): ff.write(" Total bandwidth (Mhz) = %f\n" % (hdr['nchans'] * hdr['foff'])) else: # what else can we do? ff.write(" Total bandwidth (Mhz) = %f\n" % 100.0) ff.write(" Number of channels = %d\n" % hdr['nchans']) if hdr.has_key('foff'): ff.write(" Channel bandwidth (Mhz) = %d\n" % hdr['foff']) else: # what else can we do? ff.write(" Channel bandwidth (Mhz) = %d\n" % 100.0) ff.write(" Data analyzed by = %s\n" % getpass.getuser()) ff.write(" Any additional notes:\n" " File converted from SIGPROC .tim time series\n" " with PRESTO's tim2dat.py, written by Patrick Lazarus\n") return inffn
def main(args, comm=None): """ finds the best models of all standard stars in the frame and normlize the model flux. Output is written to a file and will be called for calibration. """ log = get_logger() log.info("mag delta %s = %f (for the pre-selection of stellar models)" % (args.color, args.delta_color)) if args.mpi or comm is not None: from mpi4py import MPI if comm is None: comm = MPI.COMM_WORLD size = comm.Get_size() rank = comm.Get_rank() if rank == 0: log.info('mpi parallelizing with {} ranks'.format(size)) else: comm = None rank = 0 size = 1 # disable multiprocess by forcing ncpu = 1 when using MPI if comm is not None: ncpu = 1 if rank == 0: log.info('disabling multiprocess (forcing ncpu = 1)') else: ncpu = args.ncpu if ncpu > 1: if rank == 0: log.info( 'multiprocess parallelizing with {} processes'.format(ncpu)) if args.ignore_gpu and desispec.fluxcalibration.use_gpu: # Opt-out of GPU usage desispec.fluxcalibration.use_gpu = False if rank == 0: log.info('ignoring GPU') elif desispec.fluxcalibration.use_gpu: # Nothing to do here, GPU is used by default if available if rank == 0: log.info('using GPU') else: if rank == 0: log.info('GPU not available') std_targetids = None if args.std_targetids is not None: std_targetids = args.std_targetids # READ DATA ############################################ # First loop through and group by exposure and spectrograph frames_by_expid = {} rows = list() for filename in args.frames: log.info("reading %s" % filename) frame = io.read_frame(filename) night = safe_read_key(frame.meta, "NIGHT") expid = safe_read_key(frame.meta, "EXPID") camera = safe_read_key(frame.meta, "CAMERA").strip().lower() rows.append((night, expid, camera)) spec = camera[1] uniq_key = (expid, spec) if uniq_key in frames_by_expid.keys(): frames_by_expid[uniq_key][camera] = frame else: frames_by_expid[uniq_key] = {camera: frame} input_frames_table = Table(rows=rows, names=('NIGHT', 'EXPID', 'TILEID')) frames = {} flats = {} skies = {} spectrograph = None starfibers = None starindices = None fibermap = None # For each unique expid,spec pair, get the logical OR of the FIBERSTATUS for all # cameras and then proceed with extracting the frame information # once we modify the fibermap FIBERSTATUS for (expid, spec), camdict in frames_by_expid.items(): fiberstatus = None for frame in camdict.values(): if fiberstatus is None: fiberstatus = frame.fibermap['FIBERSTATUS'].data.copy() else: fiberstatus |= frame.fibermap['FIBERSTATUS'] for camera, frame in camdict.items(): frame.fibermap['FIBERSTATUS'] |= fiberstatus # Set fibermask flagged spectra to have 0 flux and variance frame = get_fiberbitmasked_frame(frame, bitmask='stdstars', ivar_framemask=True) frame_fibermap = frame.fibermap if std_targetids is None: frame_starindices = np.where(isStdStar(frame_fibermap))[0] else: frame_starindices = np.nonzero( np.isin(frame_fibermap['TARGETID'], std_targetids))[0] #- Confirm that all fluxes have entries but trust targeting bits #- to get basic magnitude range correct keep_legacy = np.ones(len(frame_starindices), dtype=bool) for colname in ['FLUX_G', 'FLUX_R', 'FLUX_Z']: #- and W1 and W2? keep_legacy &= frame_fibermap[colname][ frame_starindices] > 10**((22.5 - 30) / 2.5) keep_legacy &= frame_fibermap[colname][ frame_starindices] < 10**((22.5 - 0) / 2.5) keep_gaia = np.ones(len(frame_starindices), dtype=bool) for colname in ['G', 'BP', 'RP']: #- and W1 and W2? keep_gaia &= frame_fibermap[ 'GAIA_PHOT_' + colname + '_MEAN_MAG'][frame_starindices] > 10 keep_gaia &= frame_fibermap[ 'GAIA_PHOT_' + colname + '_MEAN_MAG'][frame_starindices] < 20 n_legacy_std = keep_legacy.sum() n_gaia_std = keep_gaia.sum() keep = keep_legacy | keep_gaia # accept both types of standards for the time being # keep the indices for gaia/legacy subsets gaia_indices = keep_gaia[keep] legacy_indices = keep_legacy[keep] frame_starindices = frame_starindices[keep] if spectrograph is None: spectrograph = frame.spectrograph fibermap = frame_fibermap starindices = frame_starindices starfibers = fibermap["FIBER"][starindices] elif spectrograph != frame.spectrograph: log.error("incompatible spectrographs {} != {}".format( spectrograph, frame.spectrograph)) raise ValueError("incompatible spectrographs {} != {}".format( spectrograph, frame.spectrograph)) elif starindices.size != frame_starindices.size or np.sum( starindices != frame_starindices) > 0: log.error("incompatible fibermap") raise ValueError("incompatible fibermap") if not camera in frames: frames[camera] = [] frames[camera].append(frame) # possibly cleanup memory del frames_by_expid for filename in args.skymodels: log.info("reading %s" % filename) sky = io.read_sky(filename) camera = safe_read_key(sky.header, "CAMERA").strip().lower() if not camera in skies: skies[camera] = [] skies[camera].append(sky) for filename in args.fiberflats: log.info("reading %s" % filename) flat = io.read_fiberflat(filename) camera = safe_read_key(flat.header, "CAMERA").strip().lower() # NEED TO ADD MORE CHECKS if camera in flats: log.warning( "cannot handle several flats of same camera (%s), will use only the first one" % camera) #raise ValueError("cannot handle several flats of same camera (%s)"%camera) else: flats[camera] = flat # if color is not specified we decide on the fly color = args.color if color is not None: if color[:4] == 'GAIA': legacy_color = False gaia_color = True else: legacy_color = True gaia_color = False if n_legacy_std == 0 and legacy_color: raise Exception( 'Specified Legacy survey color, but no legacy standards') if n_gaia_std == 0 and gaia_color: raise Exception('Specified gaia color, but no gaia stds') if starindices.size == 0: log.error("no STD star found in fibermap") raise ValueError("no STD star found in fibermap") log.info("found %d STD stars" % starindices.size) if n_legacy_std == 0: gaia_std = True if color is None: color = 'GAIA-BP-RP' else: gaia_std = False if color is None: color = 'G-R' if n_gaia_std > 0: log.info('Gaia standards found but not used') if gaia_std: # The name of the reference filter to which we normalize the flux ref_mag_name = 'GAIA-G' color_band1, color_band2 = ['GAIA-' + _ for _ in color[5:].split('-')] log.info( "Using Gaia standards with color {} and normalizing to {}".format( color, ref_mag_name)) # select appropriate subset of standards starindices = starindices[gaia_indices] starfibers = starfibers[gaia_indices] else: ref_mag_name = 'R' color_band1, color_band2 = color.split('-') log.info("Using Legacy standards with color {} and normalizing to {}". format(color, ref_mag_name)) # select appropriate subset of standards starindices = starindices[legacy_indices] starfibers = starfibers[legacy_indices] # excessive check but just in case if not color in ['G-R', 'R-Z', 'GAIA-BP-RP', 'GAIA-G-RP']: raise ValueError('Unknown color {}'.format(color)) # log.warning("Not using flux errors for Standard Star fits!") # DIVIDE FLAT AND SUBTRACT SKY , TRIM DATA ############################################ # since poping dict, we need to copy keys to iterate over to avoid # RuntimeError due to changing dict frame_cams = list(frames.keys()) for cam in frame_cams: if not cam in skies: log.warning("Missing sky for %s" % cam) frames.pop(cam) continue if not cam in flats: log.warning("Missing flat for %s" % cam) frames.pop(cam) continue flat = flats[cam] for frame, sky in zip(frames[cam], skies[cam]): frame.flux = frame.flux[starindices] frame.ivar = frame.ivar[starindices] frame.ivar *= (frame.mask[starindices] == 0) frame.ivar *= (sky.ivar[starindices] != 0) frame.ivar *= (sky.mask[starindices] == 0) frame.ivar *= (flat.ivar[starindices] != 0) frame.ivar *= (flat.mask[starindices] == 0) frame.flux *= (frame.ivar > 0) # just for clean plots for star in range(frame.flux.shape[0]): ok = np.where((frame.ivar[star] > 0) & (flat.fiberflat[star] != 0))[0] if ok.size > 0: frame.flux[star] = frame.flux[star] / flat.fiberflat[ star] - sky.flux[star] frame.resolution_data = frame.resolution_data[starindices] nframes = len(frames[cam]) if nframes > 1: # optimal weights for the coaddition = ivar*throughput, not directly ivar, # we estimate the relative throughput with median fluxes at this stage medflux = np.zeros(nframes) for i, frame in enumerate(frames[cam]): if np.sum(frame.ivar > 0) == 0: log.error( "ivar=0 for all std star spectra in frame {}-{:08d}". format(cam, frame.meta["EXPID"])) else: medflux[i] = np.median(frame.flux[frame.ivar > 0]) log.debug("medflux = {}".format(medflux)) medflux *= (medflux > 0) if np.sum(medflux > 0) == 0: log.error( "mean median flux = 0, for all stars in fibers {}".format( list(frames[cam][0].fibermap["FIBER"][starindices]))) sys.exit(12) mmedflux = np.mean(medflux[medflux > 0]) weights = medflux / mmedflux log.info("coadding {} exposures in cam {}, w={}".format( nframes, cam, weights)) sw = np.zeros(frames[cam][0].flux.shape) swf = np.zeros(frames[cam][0].flux.shape) swr = np.zeros(frames[cam][0].resolution_data.shape) for i, frame in enumerate(frames[cam]): sw += weights[i] * frame.ivar swf += weights[i] * frame.ivar * frame.flux swr += weights[i] * frame.ivar[:, None, :] * frame.resolution_data coadded_frame = frames[cam][0] coadded_frame.ivar = sw coadded_frame.flux = swf / (sw + (sw == 0)) coadded_frame.resolution_data = swr / ((sw + (sw == 0))[:, None, :]) frames[cam] = [coadded_frame] # CHECK S/N ############################################ # for each band in 'brz', record quadratic sum of median S/N across wavelength snr = dict() for band in ['b', 'r', 'z']: snr[band] = np.zeros(starindices.size) for cam in frames: band = cam[0].lower() for frame in frames[cam]: msnr = np.median(frame.flux * np.sqrt(frame.ivar) / np.sqrt(np.gradient(frame.wave)), axis=1) # median SNR per sqrt(A.) msnr *= (msnr > 0) snr[band] = np.sqrt(snr[band]**2 + msnr**2) log.info("SNR(B) = {}".format(snr['b'])) ############################### max_number_of_stars = 50 min_blue_snr = 4. ############################### indices = np.argsort(snr['b'])[::-1][:max_number_of_stars] validstars = np.where(snr['b'][indices] > min_blue_snr)[0] #- TODO: later we filter on models based upon color, thus throwing #- away very blue stars for which we don't have good models. log.info("Number of stars with median stacked blue S/N > {} /sqrt(A) = {}". format(min_blue_snr, validstars.size)) if validstars.size == 0: log.error("No valid star") sys.exit(12) validstars = indices[validstars] for band in ['b', 'r', 'z']: snr[band] = snr[band][validstars] log.info("BLUE SNR of selected stars={}".format(snr['b'])) for cam in frames: for frame in frames[cam]: frame.flux = frame.flux[validstars] frame.ivar = frame.ivar[validstars] frame.resolution_data = frame.resolution_data[validstars] starindices = starindices[validstars] starfibers = starfibers[validstars] nstars = starindices.size fibermap = Table(fibermap[starindices]) # MASK OUT THROUGHPUT DIP REGION ############################################ mask_throughput_dip_region = True if mask_throughput_dip_region: wmin = 4300. wmax = 4500. log.warning( "Masking out the wavelength region [{},{}]A in the standard star fit" .format(wmin, wmax)) for cam in frames: for frame in frames[cam]: ii = np.where((frame.wave >= wmin) & (frame.wave <= wmax))[0] if ii.size > 0: frame.ivar[:, ii] = 0 # READ MODELS ############################################ log.info("reading star models in %s" % args.starmodels) stdwave, stdflux, templateid, teff, logg, feh = io.read_stdstar_templates( args.starmodels) # COMPUTE MAGS OF MODELS FOR EACH STD STAR MAG ############################################ #- Support older fibermaps if 'PHOTSYS' not in fibermap.colnames: log.warning('Old fibermap format; using defaults for missing columns') log.warning(" PHOTSYS = 'S'") log.warning(" EBV = 0.0") fibermap['PHOTSYS'] = 'S' fibermap['EBV'] = 0.0 if not np.in1d(np.unique(fibermap['PHOTSYS']), ['', 'N', 'S', 'G']).all(): log.error('Unknown PHOTSYS found') raise Exception('Unknown PHOTSYS found') # Fetching Filter curves model_filters = dict() for band in ["G", "R", "Z"]: for photsys in np.unique(fibermap['PHOTSYS']): if photsys in ['N', 'S']: model_filters[band + photsys] = load_legacy_survey_filter( band=band, photsys=photsys) if len(model_filters) == 0: log.info('No Legacy survey photometry identified in fibermap') # I will always load gaia data even if we are fitting LS standards only for band in ["G", "BP", "RP"]: model_filters["GAIA-" + band] = load_gaia_filter(band=band, dr=2) # Compute model mags on rank 0 and bcast result to other ranks # This sidesteps an OOM event on Cori Haswell with "-c 2" model_mags = None if rank == 0: log.info("computing model mags for %s" % sorted(model_filters.keys())) model_mags = dict() for filter_name in model_filters.keys(): model_mags[filter_name] = get_magnitude(stdwave, stdflux, model_filters, filter_name) log.info("done computing model mags") if comm is not None: model_mags = comm.bcast(model_mags, root=0) # LOOP ON STARS TO FIND BEST MODEL ############################################ star_mags = dict() star_unextincted_mags = dict() if gaia_std and (fibermap['EBV'] == 0).all(): log.info("Using E(B-V) from SFD rather than FIBERMAP") # when doing gaia standards, on old tiles the # EBV is not set so we fetch from SFD (in original SFD scaling) ebv = SFDMap(scaling=1).ebv( acoo.SkyCoord(ra=fibermap['TARGET_RA'] * units.deg, dec=fibermap['TARGET_DEC'] * units.deg)) else: ebv = fibermap['EBV'] photometric_systems = np.unique(fibermap['PHOTSYS']) if not gaia_std: for band in ['G', 'R', 'Z']: star_mags[band] = 22.5 - 2.5 * np.log10(fibermap['FLUX_' + band]) star_unextincted_mags[band] = np.zeros(star_mags[band].shape) for photsys in photometric_systems: r_band = extinction_total_to_selective_ratio( band, photsys) # dimensionless # r_band = a_band / E(B-V) # E(B-V) is a difference of magnitudes (dimensionless) # a_band = -2.5*log10(effective dust transmission) , dimensionless # effective dust transmission = # integral( SED(lambda) * filter_transmission(lambda,band) * dust_transmission(lambda,E(B-V)) dlamdba) # / integral( SED(lambda) * filter_transmission(lambda,band) dlamdba) selection = (fibermap['PHOTSYS'] == photsys) a_band = r_band * ebv[selection] # dimensionless star_unextincted_mags[band][selection] = 22.5 - 2.5 * np.log10( fibermap['FLUX_' + band][selection]) - a_band for band in ['G', 'BP', 'RP']: star_mags['GAIA-' + band] = fibermap['GAIA_PHOT_' + band + '_MEAN_MAG'] for band, extval in gaia_extinction(star_mags['GAIA-G'], star_mags['GAIA-BP'], star_mags['GAIA-RP'], ebv).items(): star_unextincted_mags['GAIA-' + band] = star_mags['GAIA-' + band] - extval star_colors = dict() star_unextincted_colors = dict() # compute the colors and define the unextincted colors # the unextincted colors are filled later if not gaia_std: for c1, c2 in ['GR', 'RZ']: star_colors[c1 + '-' + c2] = star_mags[c1] - star_mags[c2] star_unextincted_colors[c1 + '-' + c2] = (star_unextincted_mags[c1] - star_unextincted_mags[c2]) for c1, c2 in [('BP', 'RP'), ('G', 'RP')]: star_colors['GAIA-' + c1 + '-' + c2] = (star_mags['GAIA-' + c1] - star_mags['GAIA-' + c2]) star_unextincted_colors['GAIA-' + c1 + '-' + c2] = (star_unextincted_mags['GAIA-' + c1] - star_unextincted_mags['GAIA-' + c2]) linear_coefficients = np.zeros((nstars, stdflux.shape[0])) chi2dof = np.zeros((nstars)) redshift = np.zeros((nstars)) normflux = np.zeros((nstars, stdwave.size)) fitted_model_colors = np.zeros(nstars) local_comm, head_comm = None, None if comm is not None: # All ranks in local_comm work on the same stars local_comm = comm.Split(rank % nstars, rank) # The color 1 in head_comm contains all ranks that are have rank 0 in local_comm head_comm = comm.Split(rank < nstars, rank) for star in range(rank % nstars, nstars, size): log.info("rank %d: finding best model for observed star #%d" % (rank, star)) # np.array of wave,flux,ivar,resol wave = {} flux = {} ivar = {} resolution_data = {} for camera in frames: for i, frame in enumerate(frames[camera]): identifier = "%s-%d" % (camera, i) wave[identifier] = frame.wave flux[identifier] = frame.flux[star] ivar[identifier] = frame.ivar[star] resolution_data[identifier] = frame.resolution_data[star] # preselect models based on magnitudes photsys = fibermap['PHOTSYS'][star] if gaia_std: model_colors = model_mags[color_band1] - model_mags[color_band2] else: model_colors = model_mags[color_band1 + photsys] - model_mags[color_band2 + photsys] color_diff = model_colors - star_unextincted_colors[color][star] selection = np.abs(color_diff) < args.delta_color if np.sum(selection) == 0: log.warning("no model in the selected color range for this star") continue # smallest cube in parameter space including this selection (needed for interpolation) new_selection = (teff >= np.min(teff[selection])) & (teff <= np.max( teff[selection])) new_selection &= (logg >= np.min(logg[selection])) & (logg <= np.max( logg[selection])) new_selection &= (feh >= np.min(feh[selection])) & (feh <= np.max( feh[selection])) selection = np.where(new_selection)[0] log.info( "star#%d fiber #%d, %s = %f, number of pre-selected models = %d/%d" % (star, starfibers[star], color, star_unextincted_colors[color][star], selection.size, stdflux.shape[0])) # Match unextincted standard stars to data match_templates_result = match_templates( wave, flux, ivar, resolution_data, stdwave, stdflux[selection], teff[selection], logg[selection], feh[selection], ncpu=ncpu, z_max=args.z_max, z_res=args.z_res, template_error=args.template_error, comm=local_comm) # Only local rank 0 can perform the remaining work if local_comm is not None and local_comm.Get_rank() != 0: continue coefficients, redshift[star], chi2dof[star] = match_templates_result linear_coefficients[star, selection] = coefficients log.info( 'Star Fiber: {}; TEFF: {:.3f}; LOGG: {:.3f}; FEH: {:.3f}; Redshift: {:g}; Chisq/dof: {:.3f}' .format(starfibers[star], np.inner(teff, linear_coefficients[star]), np.inner(logg, linear_coefficients[star]), np.inner(feh, linear_coefficients[star]), redshift[star], chi2dof[star])) # Apply redshift to original spectrum at full resolution model = np.zeros(stdwave.size) redshifted_stdwave = stdwave * (1 + redshift[star]) for i, c in enumerate(linear_coefficients[star]): if c != 0: model += c * np.interp(stdwave, redshifted_stdwave, stdflux[i]) # Apply dust extinction to the model log.info("Applying MW dust extinction to star {} with EBV = {}".format( star, ebv[star])) model *= dust_transmission(stdwave, ebv[star]) # Compute final color of dust-extincted model photsys = fibermap['PHOTSYS'][star] if not gaia_std: model_mag1, model_mag2 = [ get_magnitude(stdwave, model, model_filters, _ + photsys) for _ in [color_band1, color_band2] ] else: model_mag1, model_mag2 = [ get_magnitude(stdwave, model, model_filters, _) for _ in [color_band1, color_band2] ] if color_band1 == ref_mag_name: model_magr = model_mag1 elif color_band2 == ref_mag_name: model_magr = model_mag2 else: # if the reference magnitude is not among colours # I'm fetching it separately. This will happen when # colour is BP-RP and ref magnitude is G if gaia_std: model_magr = get_magnitude(stdwave, model, model_filters, ref_mag_name) else: model_magr = get_magnitude(stdwave, model, model_filters, ref_mag_name + photsys) fitted_model_colors[star] = model_mag1 - model_mag2 #- TODO: move this back into normalize_templates, at the cost of #- recalculating a model magnitude? cur_refmag = star_mags[ref_mag_name][star] # Normalize the best model using reported magnitude scalefac = 10**((model_magr - cur_refmag) / 2.5) log.info('scaling {} mag {:.3f} to {:.3f} using scale {}'.format( ref_mag_name, model_magr, cur_refmag, scalefac)) normflux[star] = model * scalefac if head_comm is not None and rank < nstars: # head_comm color is 1 linear_coefficients = head_comm.reduce(linear_coefficients, op=MPI.SUM, root=0) redshift = head_comm.reduce(redshift, op=MPI.SUM, root=0) chi2dof = head_comm.reduce(chi2dof, op=MPI.SUM, root=0) fitted_model_colors = head_comm.reduce(fitted_model_colors, op=MPI.SUM, root=0) normflux = head_comm.reduce(normflux, op=MPI.SUM, root=0) # Check at least one star was fit. The check is peformed on rank 0 and # the result is bcast to other ranks so that all ranks exit together if # the check fails. atleastonestarfit = False if rank == 0: fitted_stars = np.where(chi2dof != 0)[0] atleastonestarfit = fitted_stars.size > 0 if comm is not None: atleastonestarfit = comm.bcast(atleastonestarfit, root=0) if not atleastonestarfit: log.error("No star has been fit.") sys.exit(12) # Now write the normalized flux for all best models to a file if rank == 0: # get the fibermap from any input frame for the standard stars fibermap = Table(frame.fibermap) keep = np.isin(fibermap['FIBER'], starfibers[fitted_stars]) fibermap = fibermap[keep] # drop fibermap columns specific to exposures instead of targets for col in [ 'DELTA_X', 'DELTA_Y', 'EXPTIME', 'NUM_ITER', 'FIBER_RA', 'FIBER_DEC', 'FIBER_X', 'FIBER_Y' ]: if col in fibermap.colnames: fibermap.remove_column(col) data = {} data['LOGG'] = linear_coefficients[fitted_stars, :].dot(logg) data['TEFF'] = linear_coefficients[fitted_stars, :].dot(teff) data['FEH'] = linear_coefficients[fitted_stars, :].dot(feh) data['CHI2DOF'] = chi2dof[fitted_stars] data['REDSHIFT'] = redshift[fitted_stars] data['COEFF'] = linear_coefficients[fitted_stars, :] data['DATA_%s' % color] = star_colors[color][fitted_stars] data['MODEL_%s' % color] = fitted_model_colors[fitted_stars] data['BLUE_SNR'] = snr['b'][fitted_stars] data['RED_SNR'] = snr['r'][fitted_stars] data['NIR_SNR'] = snr['z'][fitted_stars] io.write_stdstar_models(args.outfile, normflux, stdwave, starfibers[fitted_stars], data, fibermap, input_frames_table)
def sossFieldSim(ra, dec, binComp=None): # binComp=[deltaRA,deltaDEC,J,H,K] # stars in large field around target targetcrd = crd.SkyCoord(ra=ra, dec=dec, unit=(u.hour, u.deg)) targetRA = targetcrd.ra.value targetDEC = targetcrd.dec.value info = Irsa.query_region(targetcrd, catalog='fp_psc', spatial='Box', width=4 * u.arcmin) # coordinates of possible contaminants in degrees contamRA = info['ra'].data.data contamDEC = info['dec'].data.data Jmag = info['j_m'].data.data Hmag = info['h_m'].data.data Kmag = info['k_m'].data.data J_Hobs = (Jmag - Hmag) H_Kobs = (Hmag - Kmag) # target coords distance = np.sqrt((targetRA - contamRA)**2 + (targetDEC - contamDEC)**2) targetIndex = np.argmin(distance) # the target # add any missing companion cubeNameSuf = '' if binComp is not None: #contamRA = [contamRA,contamRA[targetIndex]+bincomp[0]/3600/cos(contamDEC[targetIndex]*!dtor)] (JF) deg2rad = np.pi / 180 contamRA = np.append( contamRA, (contamRA[targetIndex] + binComp[0] / 3600 / cos(contamDEC[targetIndex] * deg2rad))) contamDEC = np.append(contamDEC, (contamDEC[targetIndex] + binComp[1] / 3600)) Jmag = np.append(Jmag, binComp[2]) Hmag = np.append(Kmag, binComp[3]) Kmag = np.append(Kmag, binComp[4]) J_Hobs = (Jmag - Hmag) H_Kobs = (Hmag - Kmag) cubeNameSuf = '_custom' #Joe's coordinate conversion code def deg2HMS(ra='', dec='', round=False): RA, DEC, rs, ds = '', '', '', '' if dec: if str(dec)[0] == '-': ds, dec = '-', abs(dec) deg = int(dec) decM = abs(int((dec - deg) * 60)) if round: decS = int((abs((dec - deg) * 60) - decM) * 60) else: decS = (abs((dec - deg) * 60) - decM) * 60 DEC = '{0}{1} {2} {3}'.format(ds, deg, decM, decS) if ra: if str(ra)[0] == '-': rs, ra = '-', abs(ra) raH = int(ra / 15) raM = int(((ra / 15) - raH) * 60) if round: raS = int(((((ra / 15) - raH) * 60) - raM) * 60) else: raS = ((((ra / 15) - raH) * 60) - raM) * 60 RA = '{0}{1} {2} {3}'.format(rs, raH, raM, raS) if ra and dec: return (RA, DEC) else: return RA or DEC cubeName = 'cubes/cube_RA' + deg2HMS(ra = contamRA[targetIndex], round = True) + \ 'DEC' + deg2HMS(ra = contamDEC[targetIndex], round = True) + cubeNameSuf + '.fits' print('Cube name:') print(cubeName) print('cubNameSuf:') print(cubeNameSuf) print('Target coordinates:') print(deg2HMS(ra=contamRA[targetIndex])) print(deg2HMS(ra=contamDEC[targetIndex])) if os.path.exists(cubeName) and (binComp is not None): return """ # the trace models if 0: modelDir = '/Users/david/Documents/work/jwst/a_GTO/SOSS-WG/contamination/modelTraces' # synthetic mag & colors of models readcol,modelDir+'/'+'magnitude_modele_atm.txt',Ttheo,logg,MH,aH,Jtheo,Htheo,Ktheo,jhtheo,jktheo,hktheo,format='f,f,f,f,f,f,f,f,f,f' remove,where(logg ne 4.50 or Ttheo lt 2800),Ttheo,logg,jhtheo,jktheo,hktheo # load model traces fNameMod=file_search(modelDir,'simu_lte*-4.5-0.0.BT-Settl_LLNL_UDEMPSF_cropped.fits.gz',count=nMod) teffMod=fix(strmid(fNameMod,strpos(fNameMod[0],'simu_lte')+8,3))*100 isort=sort(teffMod) teffMod=teffMod[isort] fNameMod=fNameMod[isort] # cropped model traces are cubes whose third axis are: 0->sum of all orders, 1->order 1, 2->order 2 h0=headfits(fNameMod[0]) dimXmod=sxpar(h0,'naxis1') dimYmod=sxpar(h0,'naxis2') models=fltarr(dimXmod,dimYmod,nMod) jhMod=fltarr(nMod) hkMod=fltarr(nMod) for i=0,nMod-1 do begin tmp=readfits(fNameMod[i]) models[*,*,i]=tmp[*,*,0] # keep only the sum of all orders modelO12=tmp[*,*,1:2] fNameModO12='modelOrder12_teff'+strtrim(teffMod[i],2)+'.sav' save,modelO12,file=fNameModO12 j=where(Ttheo eq teffMod[i]) jhMod[i]=jhTheo[j] hkMod[i]=hkTheo[j] endfor modelPadX = 2000-1900 modelPadY = 2000 # save the model traces save,models,fNameModO12,teffMod,jhMod,hkMod,dimXmod,dimYmod,modelPadX,modelPadY,Ttheo,logg,jhtheo,jktheo,hktheo,file='modelsInfo.sav' endif else begin restore,'modelsInfo.sav' endelse """ # Initialize final fits cube that contains the modelled traces with contamination PAmin = 0 PAmax = 360 angle_step = 1 # degrees # Set of fov angles to cover angle_set = np.arange(PAmin, PAmax + angle_step, angle_step) # degrees nsteps = len(angle_set) simucube = np.zeros( (256, 2048, nsteps + 2) ) # cube of trace simulation at every degree of field rotation,+target at O1 and O2 #pos_dict = dict(x=99.9,y=99.9,contamRA=99.9,contamDEC=99.9,jmag=99.9) niriss_pixel_scale = 0.065 # arcsec sweetspot = dict(x=99.9, y=99.9, contamRA=99.9, contamDEC=99.9, jmag=99.9) sweetspot[ 'x'] = 859 # position on the detector of the target in direct images sweetspot['y'] = 107 sweetspot['contamRA'] = contamRA[targetIndex] sweetspot['contamDEC'] = contamDEC[targetIndex] sweetspot['jmag'] = Jmag[targetIndex] # Put field stars position and magnitudes in a dictture nstars = len(contamRA) stars = dict(x=np.empty(nstars), y=np.empty(nstars), contamRA=np.empty(nstars), contamDEC=np.empty(nstars), jmag=np.empty(nstars)) stars['contamRA'] = contamRA stars['contamDEC'] = contamDEC stars['jmag'] = Jmag # find Teff of each star T = np.zeros(nstars) jhMod, hkMod, teffMod = colors.colorMod() #Question JF: what if len(nstars)!=len(jhMod)? for j in range(nstars): color_seperation = (J_Hobs[j] - jhMod)**2 + (H_Kobs[j] - hkMod)**2 min_seperation_ind = np.argmin(color_seperation) T[j] = teffMod[min_seperation_ind] # load8colors # Big loop to generate a simulation at each Field-of-view (FOV) rotation radeg = 180 / np.pi for angle in angle_set: fieldrotation = angle # print('Angle:',fieldrotation pixelsep = 3600 * np.sqrt((np.cos(sweetspot['contamDEC'] / radeg) * ( (stars['contamRA'] - sweetspot['contamRA']))**2) + ( (stars['contamDEC'] - sweetspot['contamDEC'])**2)) xo = -np.cos(sweetspot['contamDEC'] / radeg) * ( stars['contamRA'] - sweetspot['contamRA']) * 3600 / niriss_pixel_scale yo = (stars['contamDEC'] - sweetspot['contamDEC']) * 3600 / niriss_pixel_scale dx = xo * np.cos(fieldrotation / radeg) - yo * np.sin( fieldrotation / radeg) dy = xo * np.sin(fieldrotation / radeg) + yo * np.cos( fieldrotation / radeg) stars['x'] = dx + sweetspot['x'] stars['y'] = dy + sweetspot['y'] # email from Michael Maszkiewicz on 2016 May 17: # POM edge is: # 1.67-2.01 mm left of detector in x # 2.15-2.22 mm above detector in y # 2.22-2.43 mm right of detector in x # 1.49-1.87 mm below detector in y # we take the larger value in each direction and add a 50 pixels margin # given the uncertainty of the POM location wrt to detector FOV # Retain stars that are within the Direct Image NIRISS POM FOV ind = np.where((stars['x'] >= -162) & (stars['x'] <= 2047 + 185) & (stars['y'] >= -154) & (stars['y'] <= 2047 + 174)) starsInFOV = dict(x=stars['x'][ind], y=stars['y'][ind], contamRA=stars['contamRA'][ind], contamDEC=stars['contamDEC'][ind], jmag=stars['jmag'][ind]) # *** pour In Field Of View dx = dx[ind] dy = dy[ind] Tboucle = T[ind] # print(' Number of stars in FOV:',nstars # *** # print(' J magnitude of those stars:', starsInFOV.jmag # *** # print(' Temperature of those stars:', Tboucle # print # printline,starsInFOV.x,starsInFOV.y,starsInFOV.jmag if 0 & nstars > 1: # Display the star field and sub array location in red plt.plot(starsInFOV['x'], starsInFOV['y'], 'o') plt.xlim(-50, 2047 + 50) plt.ylim(-50, 2047 + 50) plt.plot([0, 2047, 2047, 0, 0], [0, 0, 2047, 2047, 0], 'r') plt.plot([0, 255, 255, 0, 0], [0, 0, 2047, 2047, 0], 'g') plt.plot(sweetspot['x'], sweetspot['y'], 'ro') plt.show() saveFiles = glob.glob('idlSaveFiles/*.sav')[:-1] modelPadX = 2000 - 1900 modelPadY = 2000 dimXmod = 256 dimYmod = 2048 for i in range(len(ind)): intx = round(dx[i]) inty = round(dy[i]) # print(intx,inty k = np.where(teffMod == Tboucle[i]) fluxscale = 10.0**(-0.4 * (starsInFOV['jmag'][i] - sweetspot['jmag'])) # if target and first angle, add target traces of order 1 and 2 in output cube if (intx == 0) and (inty == 0) and (ang == 0): # modelO1O2=readfits(fNameMod[k]) # read the order 1 and order 2 trace fNameModO12 = saveFiles[i] modelO12 = idlsave.read(fNameModO12) simucube[0, :, :] = modelO12[0, modelPadY:modelPadY + 2047, modelPadX:modelPadX + 255] * fluxscale # order 1 simucube[1, :, :] = modelO12[1, modelPadY:modelPadY + 2047, modelPadX:modelPadX + 255] * fluxscale # order 2 # if a field star if (intx != 0) or (inty != 0): mx0 = modelPadX - intx mx1 = modelPadX - intx + 255 my0 = modelPadY - inty my1 = modelPadY - inty + 2047 #pdb.set_trace() if (mx0 > dimXmod - 1) or (my0 > dimYmod - 1): continue if (mx1 < 0) or (my1 < 0): continue x0 = (-mx0) > 0 y0 = (-my0) > 0 mx0 >= 0 mx1 <= (dimXmod - 1) my0 >= 0 my1 <= (dimYmod - 1) simucube[angle_set + 2, y0:y0 + my1 - my0, x0:x0 + mx1 - mx0] += models[mx0:mx1, my0:my1, k] * fluxscale fits.writeto(cubeName, simucube, overwrite=True)
def sossFieldSim(ra, dec, binComp='', dimX=256): """ Produce a SOSS field simulation for a target. Parameters ---------- ra: float The RA of the target. dec: float The Dec of the target. binComp: sequence The parameters of a binary companion. dimX: int The subarray size. Returns ------- simuCub : np.ndarray The simulated data cube. """ # stars in large field around target targetcrd = crd.SkyCoord(ra=ra, dec=dec, unit=(u.hour, u.deg)) targetRA = targetcrd.ra.value targetDEC = targetcrd.dec.value info = Irsa.query_region(targetcrd, catalog='fp_psc', spatial='Cone', radius=2.5 * u.arcmin) # coordinates of all stars in FOV, including target allRA = info['ra'].data.data allDEC = info['dec'].data.data Jmag = info['j_m'].data.data Hmag = info['h_m'].data.data Kmag = info['k_m'].data.data J_Hobs = Jmag - Hmag H_Kobs = Hmag - Kmag # target coords aa = ((targetRA - allRA) * np.cos(targetDEC)) distance = np.sqrt(aa**2 + (targetDEC - allDEC)**2) targetIndex = np.argmin(distance) # the target # add any missing companion if binComp != '': deg2rad = np.pi / 180 bb = binComp[0] / 3600 / np.cos(allDEC[targetIndex] * deg2rad) allRA = np.append(allRA, (allRA[targetIndex] + bb)) allDEC = np.append(allDEC, (allDEC[targetIndex] + binComp[1] / 3600)) Jmag = np.append(Jmag, binComp[2]) Hmag = np.append(Kmag, binComp[3]) Kmag = np.append(Kmag, binComp[4]) J_Hobs = Jmag - Hmag H_Kobs = Hmag - Kmag # number of stars nStars = allRA.size # Restoring model parameters modelParam = readsav(os.path.join(TRACES_PATH, 'NIRISS', 'modelsInfo.sav'), verbose=False) models = modelParam['models'] modelPadX = modelParam['modelpadx'] modelPadY = modelParam['modelpady'] dimXmod = modelParam['dimxmod'] dimYmod = modelParam['dimymod'] jhMod = modelParam['jhmod'] hkMod = modelParam['hkmod'] teffMod = modelParam['teffmod'] # find/assign Teff of each star starsT = np.empty(nStars) for j in range(nStars): color_separation = (J_Hobs[j] - jhMod)**2 + (H_Kobs[j] - hkMod)**2 min_separation_ind = np.argmin(color_separation) starsT[j] = teffMod[min_separation_ind] radeg = 180 / np.pi niriss_pixel_scale = 0.065 # arcsec sweetSpot = dict(x=856, y=107, RA=allRA[targetIndex], DEC=allDEC[targetIndex], jmag=Jmag[targetIndex]) # offset between all stars and target dRA = (allRA - sweetSpot['RA']) * np.cos(sweetSpot['DEC'] / radeg) * 3600 dDEC = (allDEC - sweetSpot['DEC']) * 3600 # Put field stars positions and magnitudes in structured array _ = dict(RA=allRA, DEC=allDEC, dRA=dRA, dDEC=dDEC, jmag=Jmag, T=starsT, x=np.empty(nStars), y=np.empty(nStars), dx=np.empty(nStars), dy=np.empty(nStars)) stars = np.empty(nStars, dtype=[(key, val.dtype) for key, val in _.items()]) for key, val in _.items(): stars[key] = val # Initialize final fits cube that contains the modelled traces # with contamination PAmin = 0 # instrument PA, degrees PAmax = 360 dPA = 1 # degrees # Set of IPA values to cover PAtab = np.arange(PAmin, PAmax, dPA) # degrees nPA = len(PAtab) # dimX=256 #2048 #########now as argument, with default to 256 dimY = 2048 # cube of trace simulation at every degree of field rotation, # +target at O1 and O2 simuCube = np.zeros([nPA + 2, dimY, dimX]) # saveFiles = glob.glob('idlSaveFiles/*.sav')[:-1] saveFiles = glob.glob(os.path.join(TRACES_PATH, 'NIRISS', '*.sav'))[:-1] # pdb.set_trace() # Big loop to generate a simulation at each instrument PA for kPA in range(PAtab.size): APA = PAtab[kPA] V3PA = APA + 0.57 # from APT sindx = np.sin(np.pi / 2 + APA / radeg) * stars['dDEC'] cosdx = np.cos(np.pi / 2 + APA / radeg) * stars['dDEC'] nps = niriss_pixel_scale stars['dx'] = (np.cos(np.pi / 2 + APA / radeg) * stars['dRA'] - sindx) / nps stars['dy'] = (np.sin(np.pi / 2 + APA / radeg) * stars['dRA'] + cosdx) / nps stars['x'] = stars['dx'] + sweetSpot['x'] stars['y'] = stars['dy'] + sweetSpot['y'] # Display the star field (blue), target (red), subarray (green), # full array (blue), and axes if (kPA == 0 and nStars > 1) and False: plt.plot([0, 2047, 2047, 0, 0], [0, 0, 2047, 2047, 0], 'b') plt.plot([0, 255, 255, 0, 0], [0, 0, 2047, 2047, 0], 'g') # the order 1 & 2 traces path = '/Users/david/Documents/work/jwst/niriss/soss/data/' t1 = np.loadtxt(path + 'trace_order1.txt', unpack=True) plt.plot(t1[0], t1[1], 'r') t2 = np.loadtxt(path + 'trace_order2.txt', unpack=True) plt.plot(t2[0], t2[1], 'r') plt.plot(stars['x'], stars['y'], 'b*') plt.plot(sweetSpot['x'], sweetSpot['y'], 'r*') plt.title("APA= {} (V3PA={})".format(APA, V3PA)) ax = plt.gca() # add V2 & V3 axes l, hw, hl = 250, 50, 50 adx, ady = -l * np.cos(-0.57 / radeg), -l * np.sin(-0.57 / radeg) ax.arrow(2500, 1800, adx, ady, head_width=hw, head_length=hl, length_includes_head=True, fc='k') # V3 plt.text(2500 + 1.4 * adx, 1800 + 1.4 * ady, "V3", va='center', ha='center') adx, ady = -l * np.cos((-0.57 - 90) / radeg), -l * np.sin( (-0.57 - 90) / radeg) ax.arrow(2500, 1800, adx, ady, head_width=hw, head_length=hl, length_includes_head=True, fc='k') # V2 plt.text(2500 + 1.4 * adx, 1800 + 1.4 * ady, "V2", va='center', ha='center') # add North and East adx, ady = -l * np.cos(APA / radeg), -l * np.sin(APA / radeg) ax.arrow(2500, 1300, adx, ady, head_width=hw, head_length=hl, length_includes_head=True, fc='k') # N plt.text(2500 + 1.4 * adx, 1300 + 1.4 * ady, "N", va='center', ha='center') adx, ady = -l * np.cos((APA - 90) / radeg), -l * np.sin( (APA - 90) / radeg) ax.arrow(2500, 1300, adx, ady, head_width=hw, head_length=hl, length_includes_head=True, fc='k') # E plt.text(2500 + 1.4 * adx, 1300 + 1.4 * ady, "E", va='center', ha='center') ax.set_xlim(-400, 2047 + 800) ax.set_ylim(-400, 2047 + 400) ax.set_aspect('equal') plt.show() # Retain stars that are within the Direct Image NIRISS POM FOV ind, = np.where((stars['x'] >= -162) & (stars['x'] <= 2047 + 185) & (stars['y'] >= -154) & (stars['y'] <= 2047 + 174)) starsInFOV = stars[ind] for i in range(len(ind)): intx = round(starsInFOV['dx'][i]) inty = round(starsInFOV['dy'][i]) k = np.where(teffMod == starsInFOV['T'][i])[0][0] fluxscale = 10.0**(-0.4 * (starsInFOV['jmag'][i] - sweetSpot['jmag'])) # deal with subection sizes mx0 = int(modelPadX - intx) mx1 = int(modelPadX - intx + dimX) my0 = int(modelPadY - inty) my1 = int(modelPadY - inty + dimY) if (mx0 > dimXmod) or (my0 > dimYmod): continue if (mx1 < 0) or (my1 < 0): continue x0 = (mx0 < 0) * (-mx0) y0 = (my0 < 0) * (-my0) mx0 *= (mx0 >= 0) mx1 = dimXmod if mx1 > dimXmod else mx1 my0 *= (my0 >= 0) my1 = dimYmod if my1 > dimYmod else my1 # if target and first kPA, add target traces of order 1 and 2 # in output cube if (intx == 0) & (inty == 0) & (kPA == 0): fNameModO12 = saveFiles[k] modelO12 = readsav(fNameModO12, verbose=False)['modelo12'] ord1 = modelO12[0, my0:my1, mx0:mx1] * fluxscale ord2 = modelO12[1, my0:my1, mx0:mx1] * fluxscale simuCube[0, y0:y0 + my1 - my0, x0:x0 + mx1 - mx0] = ord1 simuCube[1, y0:y0 + my1 - my0, x0:x0 + mx1 - mx0] = ord2 if (intx != 0) or (inty != 0): mod = models[k, my0:my1, mx0:mx1] simuCube[kPA + 2, y0:y0 + my1 - my0, x0:x0 + mx1 - mx0] += mod * fluxscale #mod = models[k, 1500:1500+2048, 100:100+256] #simuCube[kPA+2, 0:dimY, 0:dimX] += mod*fluxscale return simuCube
def main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='Pull metadata from FITS for PLATE/MJD combos, output.') parser.add_argument('--obs_metadata', type=str, default=None, metavar='OBS_METADATA', required=True, help='File containing observation metadata.') parser.add_argument('--lunar_metadata', type=str, default=None, metavar='LUNAR_METADATA', required=True, help='File containing lunar ephemeris metadata.') parser.add_argument('--solar_metadata', type=str, default=None, metavar='SOLAR_METADATA', required=True, help='File containing solar ephemeris metadata.') parser.add_argument('--sunspot_metadata', type=str, default=None, metavar='SUNSPOT_METADATA', required=True, help='File containing sunspot metadata.') parser.add_argument( '--output', type=str, default='FITS', metavar='OUTPUT', help='Output format, either of FITS or CSV, defaults to FITS.') args = parser.parse_args() if args.output.upper() == 'CSV': obs_md_table = Table.read(args.obs_metadata, format="ascii.csv") elif args.output.upper == 'FITS': obs_md_table = Table.read(args.obs_metadata, format="fits") else: obs_md_table = Table.read(args.obs_metadata) lunar_md_table = Table.read(args.lunar_metadata, format="ascii.csv") lunar_md_table.rename_column('UTC', 'EPHEM_DATE') solar_md_table = Table.read(args.solar_metadata, format="ascii.csv") solar_md_table.rename_column('UTC', 'EPHEM_DATE') sunspot_md_table = Table.read(args.sunspot_metadata, format="ascii.csv") print "Table has {} entries".format(len(obs_md_table)) lookup_date, obs_md_table = find_ephemeris_lookup_date( obs_md_table['TAI-BEG'], obs_md_table['TAI-END'], obs_md_table) print "Successfully got {} ephemeris date entries".format(len(lookup_date)) ephem_date_col = Column(lookup_date, name="EPHEM_DATE") obs_md_table.add_column(ephem_date_col) sunspot_count, sunspot_area = find_sunspot_data(ephem_date_col, sunspot_md_table) sunspot_count_col = Column(sunspot_count, name="SS_COUNT") sunspot_area_col = Column(sunspot_area, name="SS_AREA") obs_md_table.add_column(sunspot_count_col) obs_md_table.add_column(sunspot_area_col) galactic_core = ascoord.SkyCoord(l=0.0, b=0.0, unit='deg', frame='galactic') #Join lunar data to the table obs_md_table = join( obs_md_table, lunar_md_table['EPHEM_DATE', 'RA_APP', 'DEC_APP', 'MG_APP', 'ELV_APP']) lunar_ra_dec = ascoord.SkyCoord(ra=obs_md_table['RA_APP'], dec=obs_md_table['DEC_APP'], unit='deg', frame='icrs') boresight_ra_dec = ascoord.SkyCoord(ra=obs_md_table['RA'], dec=obs_md_table['DEC'], unit='deg', frame='fk5') lunar_seps = boresight_ra_dec.separation(lunar_ra_dec).degree obs_md_table.add_column(Column(lunar_seps, dtype=float, name="LUNAR_SEP")) obs_md_table.rename_column("MG_APP", "LUNAR_MAGNITUDE") obs_md_table.rename_column("ELV_APP", "LUNAR_ELV") obs_md_table.remove_columns(['RA_APP', 'DEC_APP']) #Join solar data to the table obs_md_table = join( obs_md_table, solar_md_table['EPHEM_DATE', 'RA_APP', 'DEC_APP', 'ELV_APP']) solar_ra_dec = ascoord.SkyCoord(ra=obs_md_table['RA_APP'], dec=obs_md_table['DEC_APP'], unit='deg', frame='icrs') boresight_ra_dec = ascoord.SkyCoord(ra=obs_md_table['RA'], dec=obs_md_table['DEC'], unit='deg', frame='fk5') solar_seps = boresight_ra_dec.separation(solar_ra_dec).degree obs_md_table.add_column(Column(solar_seps, dtype=float, name="SOLAR_SEP")) obs_md_table.rename_column("ELV_APP", "SOLAR_ELV") obs_md_table.remove_columns(['RA_APP', 'DEC_APP']) #Add in galactic data boresight_ra_dec = ascoord.SkyCoord(ra=obs_md_table['RA'], dec=obs_md_table['DEC'], unit='deg', frame='fk5') obs_md_table.add_column( Column(boresight_ra_dec.separation(galactic_core).degree, dtype=float, name="GALACTIC_CORE_SEP")) boresight_ra_dec = ascoord.SkyCoord(ra=obs_md_table['RA'], dec=obs_md_table['DEC'], unit='deg', frame='fk5') obs_md_table.add_column( Column(boresight_ra_dec.transform_to('galactic').b.degree, dtype=float, name="GALACTIC_PLANE_SEP")) #print obs_md_table if args.output == 'CSV': obs_md_table.write("annotated_metadata.csv", format="ascii.csv") elif args.output == 'FITS': obs_md_table.write("annotated_metadata.fits", format="fits")
df = pd.read_table('AS_observations_cat_Sept2018.txt') df = df.drop(df.columns[[0]], axis = 1, inplace = False) df.columns = ['Date_Observed', 'Proposal_ID', 'Target_ID', 'ra', 'dec', 'Observation_ID', 'Instrument'] df['Time_Observed'] = np.zeros(len(df)) for i in range(len(df)): data = df['Date_Observed'][i].split(" ") df['Date_Observed'][i] = data[0] df['Time_Observed'][i] = data[1] ra = df['ra'] dec = df['dec'] df['SIMBAD_Name'] = np.zeros(len(ra)) df['Source_Type'] = np.zeros(len(ra)) for i in range(len(ra)): print(i, ra[i]) result_table = Simbad.query_region(coord.SkyCoord(ra[i], dec[i], unit=(u.deg, u.deg), frame='icrs'), radius='0d1m0s') if result_table: print(result_table['MAIN_ID'][0]) df['SIMBAD_Name'][i] = result_table['MAIN_ID'][0].decode("utf-8") df['Source_Type'][i] = result_table['OTYPE'][0].decode("utf-8") df.to_csv('Astrosat.csv', index=None) print(df)
sys.exit(1) ### Required INPUTS # lofar source catalogue path = '/data2/wwilliams/projects/lofar_surveys/LoTSS-DR2-Feb2020/' lofarcat_file = path + 'LoTSS_DR2_v100.srl_{h}.lr-full.presort.hdf5'.format( h=h) # Source catalogue lofarcat = Table.read(lofarcat_file) # ## nearest neighbour separation # get nearest neighbour for all sources c = ac.SkyCoord(lofarcat['RA'], lofarcat['DEC'], unit="deg") f_nn_idx, f_nn_sep2d, _ = ac.match_coordinates_sky(c, c, nthneighbor=2) #f_nn3_idx,f_nn3_sep2d,_ = ac.match_coordinates_sky(c,c,nthneighbor=3) f_nn4_idx, f_nn4_sep2d, _ = ac.match_coordinates_sky(c, c, nthneighbor=4) f_nn5_idx, f_nn5_sep2d, _ = ac.match_coordinates_sky(c, c, nthneighbor=5) #f_nn6_idx,f_nn6_sep2d,_ = ac.match_coordinates_sky(c,c,nthneighbor=6) if 'NN_sep' not in lofarcat.colnames: lofarcat.add_column(Column(lofarcat['LR'][f_nn_idx], 'NN_LR')) lofarcat.add_column(Column(f_nn_sep2d.to(u.arcsec).value, 'NN_sep')) lofarcat.add_column(Column(f_nn_idx, 'NN_idx')) lofarcat.add_column(Column(f_nn5_sep2d.to(u.arcsec).value, 'NN5_sep')) lofarcat.add_column(Column(f_nn4_sep2d.to(u.arcsec).value, 'NN4_sep')) lofarcat.add_column( Column(lofarcat['Total_flux'][f_nn_idx], 'NN_Total_flux'))
def downloadsdss(_ra, _dec, _band, _radius=20, force=False): from astroquery.sdss import SDSS from astropy import coordinates as coords import astropy.units as u from astropy.io import fits import os import sys import string import numpy as np from scipy import interpolate pos = coords.SkyCoord(ra=float(_ra) * u.deg, dec=float(_dec) * u.deg) print 'pos =', pos xid = SDSS.query_region(pos, spectro=False, radius=_radius * u.arcsec) print xid if xid: pointing = [] for i in xid: if i['run'] > 300: if (i['run'], i['camcol'], i['field']) not in pointing: pointing.append((i['run'], i['camcol'], i['field'])) # if too many pointing, take only first 40 if len(pointing) > 50: nn = 50 else: nn = len(pointing) filevec = [] print len(pointing) for run, camcol, field in pointing[:nn]: # naomaggie image output1 = _band + '_SDSS_' + str(run) + '_' + str( camcol) + '_' + str(field) + '.fits' # image in count output2 = _band + '_SDSS_' + str(run) + '_' + str( camcol) + '_' + str(field) + 'c.fits' # weight image output3 = _band + '_SDSS_' + str(run) + '_' + str( camcol) + '_' + str(field) + '.weight.fits' # sky image output4 = _band + '_SDSS_' + str(run) + '_' + str( camcol) + '_' + str(field) + '.sky.fits' if os.path.isfile(output1) and not force: print 'already downloaded', output1 filevec.append(output2) filevec.append(output3) continue im = SDSS.get_images(run=run, camcol=camcol, field=field, band=_band, cache=True) if os.path.isfile(output1): os.system('rm ' + output1) if os.path.isfile(output2): os.system('rm ' + output2) if os.path.isfile(output3): os.system('rm ' + output3) if os.path.isfile(output4): os.system('rm ' + output4) im[0].writeto(output1) # im[0][0].writeto(output2) FITS_file = fits.open(output1) new_header = FITS_file[0].header camcol = FITS_file[0].header['CAMCOL'] # camcol ugriz = FITS_file[0].header['FILTER'] # ugriz filter run1 = FITS_file[0].header['RUN'] # run gain, dark_var = SDSS_gain_dark(camcol, ugriz, run1) new_header['gain'] = gain new_header['dark'] = dark_var new_header['BUNIT'] = 'counts' new_header['rdnoise'] = 2 frame_image = FITS_file[0].data.transpose() allsky = FITS_file[2].data['ALLSKY'].transpose() allsky = allsky[:, :, 0] xinterp = FITS_file[2].data['XINTERP'].transpose() xinterp = xinterp[:, 0] yinterp = FITS_file[2].data['YINTERP'].transpose() yinterp = yinterp[:, 0] sky_function = interpolate.interp2d(np.arange(allsky.shape[1]),\ np.arange(allsky.shape[0]), allsky, kind='linear') sky_image = sky_function(yinterp, xinterp) # in counts calib = FITS_file[1].data # nanomaggies per count calib_image = np.empty_like(frame_image) for i in np.arange(calib_image.shape[1]): calib_image[:, i] = calib # Calculate the error in the frame image for use fitting algorithms later. dn_image = frame_image / calib_image # + sky_image # counts dn_err_image = np.sqrt((dn_image + sky_image) / gain + dark_var) # frame_image_err = dn_err_image * calib_image converts to nanomaggies frame_weight = 1 / ((dn_err_image)**2) new_header['SKYLEVEL'] = np.mean(sky_image) # save image in count fits.writeto(output2, dn_image.transpose(), new_header, overwrite=True) # save weight image fits.writeto(output3, frame_weight.transpose(), new_header, overwrite=True) # save sky image fits.writeto(output4, sky_image.transpose(), new_header, overwrite=True) filevec.append(output2) filevec.append(output3) return filevec else: return ''
from astropy import coordinates as c from astropy import time as t from astropy import units as u from astropy.table import Table # read the data table cat = Table.read("gll_psc_v14.fit") # select rows that have sufficient flux (note unit conversion is implicit!) maxflux = 0.5e-10 * u.TeV / u.cm**2 / u.s selected = cat[cat['Energy_Flux100'] > maxflux] # get the RA/Dec cols (you could also use the Gal L/B columns) RA = selected['RAJ2000'] Dec = selected['DEJ2000'] coord = c.SkyCoord(ra=RA, dec=Dec) # define the time and location paris = c.EarthLocation(lat=48.8567 * u.deg, lon=2.3508 * u.deg) tonight = t.Time("2015-04-09 00:00:00", format="iso", scale="utc") # calculate the horizontal coordinates altaz = coord.transform_to(c.AltAz(obstime=tonight, location=paris)) # select rows that are visible above 10 deg altitude visible = selected[altaz.alt > 10 * u.deg] # write those rows to a html file visible.write("visible.html") # or try an interactive viewer (here I show only 2 columns to make it
def make_rprof(regions, ploteach=False): names = [r.attr[1]['text'] for r in regions] center_positions = coordinates.SkyCoord([r.coord_list for r in regions], unit=(u.deg, u.deg), frame='fk5') size = u.Quantity([1.25, 1.25], u.arcsec) if ploteach: nplots = len(names) for ii in range(nplots): pl.figure(ii).clf() pl.figure(nplots + ii).clf() pl.figure(nplots * 2 + ii).clf() linestyles = { name: itertools.cycle(['-'] + ['--'] + [':'] + ['-.']) for name in names } for fn in ffiles: fh = fits.open(paths.dpath("12m/continuum/" + fn)) mywcs = wcs.WCS(fh[0].header) if 'BMAJ' not in fh[0].header: #print("File {0} does not have BMAJ".format(fn)) continue try: beam = radio_beam.Beam.from_fits_header(fh[0].header) except KeyError: #print("File {0} doesn't have beam info in the header".format(fn)) continue pixscale = (mywcs.pixel_scale_matrix.diagonal()**2).sum()**0.5 ppbeam = (beam.sr / (pixscale**2 * u.deg**2)).decompose().value / u.beam #print("fn {0} ppbeam={1:0.2f}".format(fn, ppbeam)) for ii, (name, position) in enumerate(zip(names, center_positions)): cutout = Cutout2D(fh[0].data, position, size, wcs=mywcs) nr, bins, rprof = azimuthalAverage(cutout.data, binsize=1.0, return_nr=True) linestyle = next(linestyles[name]) pl.figure(ii) pl.title(name) pl.plot(bins * pixscale * 3600., rprof / ppbeam, label=fn.split(".")[0], linestyle=linestyle) pl.ylabel("Azimuthally Averaged Flux (Jy)") pl.xlabel("Radius [arcsec]") cumul_rprof = np.nan_to_num(rprof * nr / ppbeam).cumsum() pl.figure(nplots + ii) pl.title(name) pl.plot(bins * pixscale * 3600., cumul_rprof, label=fn.split(".")[0], linestyle=linestyle) pl.ylabel("Cumulative Flux (Jy)") pl.xlabel("Radius [arcsec]") if ii == 0: ax = pl.gca() ax2 = ax.twiny() ax3 = ax.twinx() def tick_function(old_x): newx = (old_x * u.arcsec * masscalc.distance).to( u.pc, u.dimensionless_angles()).value return ["%.1f" % z for z in newx] new_tick_locations = [0.005, 0.01, 0.015, 0.02, 0.025 ] * u.pc new_tick_locs_as = (new_tick_locations / masscalc.distance).to( u.arcsec, u.dimensionless_angles()) ax2.set_xlim(ax.get_xlim()) ax2.set_xticks(new_tick_locs_as.value) ax2.set_xticklabels(tick_function(new_tick_locs_as.value)) ax2.set_xlabel(r"Radius (pc)") ax3.set_ylim(ax.get_ylim()) yticks_mass = np.arange(0, 6000, 1000) yticks_Jy = yticks_mass / masscalc.mass_conversion_factor( ).value ax3.set_yticks(yticks_Jy) ax3.set_yticklabels(yticks_mass) ax3.set_ylabel("Cumulative Mass (M$_\\odot$, $T=20$ K)") pl.figure(nplots * 2 + ii) pl.title(name) pl.plot(((bins * pixscale * u.deg) * masscalc.distance).to( u.pc, u.dimensionless_angles()), cumul_rprof * masscalc.mass_conversion_factor(), label=fn.split(".")[0], linestyle=linestyle) pl.ylabel("Cumulative Mass (M$_\\odot$, $T=20$ K)") pl.xlabel("Radius (pc)") for ii in range(nplots): for xtra in (0, nplots * 2): ax = pl.figure(ii + xtra).gca() box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.6, box.height]) # Put a legend to the right of the current axis ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) else: nplots = 0 pl.matplotlib.rc_file('pubfiguresrc') for jj in range(nplots * 3 + 1, nplots * 3 + 11): pl.figure(jj).clf() # $ find ~/work/w51/alma/FITS/ -samefile ~/work/w51/alma/FITS/W51_te_continuum_best.fits # /Users/adam/work/w51/alma/FITS//12m/continuum/selfcal_allspw_selfcal_3_mfs_deeper.image.pbcor.fits # /Users/adam/work/w51/alma/FITS//W51_te_continuum_best.fits fn = "selfcal_allspw_selfcal_3_mfs_deeper.image.pbcor.fits" fh = fits.open(paths.dpath("12m/continuum/" + fn)) mywcs = wcs.WCS(fh[0].header) beam = radio_beam.Beam.from_fits_header(fh[0].header) pixscale = (mywcs.pixel_scale_matrix.diagonal()**2).sum()**0.5 ppbeam = (beam.sr / (pixscale**2 * u.deg**2)).decompose().value / u.beam for ii, (name, position) in enumerate(zip(names, center_positions)): cutout = Cutout2D(fh[0].data, position, size, wcs=mywcs) nr, bins, rprof = azimuthalAverage(cutout.data, binsize=1.0, return_nr=True) pl.figure(nplots * 3 + 1) #pl.title(fn.replace(".image.pbcor.fits","")) pl.plot(bins * pixscale * 3600., rprof / ppbeam, label=name) pl.ylabel("Azimuthally Averaged Flux (Jy)") pl.xlabel("Radius [arcsec]") if len(names) < 5: pl.legend(loc='best') elif ii == len(names) - 1: ax = pl.gca() box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.6, box.height]) # Put a legend to the right of the current axis ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) cumul_rprof = np.nan_to_num(rprof * nr / ppbeam).cumsum() pl.figure(nplots * 3 + 2) #pl.title(fn.replace(".image.pbcor.fits","")) pl.plot(bins * pixscale * 3600., cumul_rprof, label=name) pl.ylabel("Cumulative Flux (Jy)") pl.xlabel("Radius [arcsec]") if len(names) < 5: pl.legend(loc='best') elif ii == len(names) - 1: ax = pl.gca() box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.6, box.height]) # Put a legend to the right of the current axis ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) if ii == len(names) - 1: ax = pl.gca() ax2 = ax.twiny() ax3 = ax.twinx() def tick_function(old_x): newx = (old_x * u.arcsec * masscalc.distance).to( u.au, u.dimensionless_angles()).value return ["%.1f" % z for z in newx] new_tick_locations = np.arange(0, 8000, 1000) * u.au new_tick_locs_as = (new_tick_locations / masscalc.distance).to( u.arcsec, u.dimensionless_angles()) ax2.set_xlim(ax.get_xlim()) ax2.set_xticks(new_tick_locs_as.value) ax2.set_xticklabels(tick_function(new_tick_locs_as.value)) ax2.set_xlabel(r"Radius [au]") ax3.set_ylim(ax.get_ylim()) yticks_mass = np.arange(0, 6000, 1000) yticks_Jy = yticks_mass / masscalc.mass_conversion_factor( TK=40).value ax3.set_yticks(yticks_Jy) ax3.set_yticklabels(yticks_mass) ax3.set_ylabel("Cumulative Mass (M$_\\odot$, $T=40$ K)") radii = ((bins * pixscale * u.deg) * masscalc.distance).to( u.pc, u.dimensionless_angles()) mass_40k_profile = (cumul_rprof * masscalc.mass_conversion_factor(TK=40) / u.beam).to(u.M_sun) pl.figure(nplots * 3 + 3) #pl.title(fn.replace(".image.pbcor.fits","")) pl.plot(radii, mass_40k_profile, label=name) pl.ylabel("Cumulative Mass (M$_\\odot$, $T=40$ K)") pl.xlabel("Radius (pc)") if len(names) < 5: pl.legend(loc='best') elif ii == len(names) - 1: ax = pl.gca() box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.6, box.height]) # Put a legend to the right of the current axis ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) density_40k_profile = (mass_40k_profile / (4 / 3. * np.pi * radii**3) / (2.8 * u.Da)).to(u.cm**-3) #print(density_40k_profile) #print(radii) pl.figure(nplots * 3 + 4) #pl.title(fn.replace(".image.pbcor.fits","")) pl.semilogy(radii, density_40k_profile, label=name) pl.ylabel("Cumulative Density [n(H$_2$), $T=40$ K]") pl.xlabel("Radius (pc)") if len(names) < 5: pl.legend(loc='best') elif ii == len(names) - 1: ax = pl.gca() box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.6, box.height]) # Put a legend to the right of the current axis ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) angular_radii = bins * pixscale * 3600. azimuthal_average_flux = rprof / ppbeam azimuthal_average_mass = azimuthal_average_flux * masscalc.mass_conversion_factor( TK=40) / u.beam bindiff_as = np.diff(bins).mean() * pixscale * 3600. bindiff_cm = bindiff_as * masscalc.distance / 206265. bins_cm = (angular_radii * masscalc.distance / 206265.).to(u.cm) sqdiffbins_cm = u.Quantity([bins_cm[0].to(u.cm).value**2] + (bins_cm.to(u.cm)[1:]**2 - bins_cm.to(u.cm)[:-1]**2).value.tolist(), u.cm**2) cudiffbins_cm = u.Quantity([bins_cm[0].to(u.cm).value**3] + (bins_cm.to(u.cm)[1:]**3 - bins_cm.to(u.cm)[:-1]**3).value.tolist(), u.cm**3) sqdiffbins_pix = [bins[0]**2] + (bins[1:]**2 - bins[:-1]**2).tolist() azimuthal_average_density = (azimuthal_average_mass * sqdiffbins_pix / (2.8 * u.Da) / (4 / 3. * np.pi * (cudiffbins_cm))).to( u.cm**-3) pl.figure(nplots * 3 + 5) pl.semilogy(angular_radii, azimuthal_average_density, label=name) pl.ylabel("Azimuthally Averaged Density [cm$^{-3}$]") pl.xlabel("Radius [arcsec]") if len(names) < 5: pl.legend(loc='best') elif ii == len(names) - 1: ax = pl.gca() box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.6, box.height]) # Put a legend to the right of the current axis ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) if ii == len(names) - 1: ax = pl.gca() ax.set_ylim(1e7, 5e10) ax2 = ax.twiny() #ax3 = ax.twinx() def tick_function(old_x): newx = (old_x * u.arcsec * masscalc.distance).to( u.au, u.dimensionless_angles()).value return ["%.1f" % z for z in newx] new_tick_locations = np.arange(0, 8000, 1000) * u.au new_tick_locs_as = (new_tick_locations / masscalc.distance).to( u.arcsec, u.dimensionless_angles()) ax2.set_xlim(ax.get_xlim()) ax2.set_xticks(new_tick_locs_as.value) ax2.set_xticklabels(tick_function(new_tick_locs_as.value)) ax2.set_xlabel(r"Radius [au]") #ax3.set_ylim(ax.get_ylim()) #yticks_mass = np.arange(0,6000,1000) #yticks_Jy = yticks_mass/masscalc.mass_conversion_factor(TK=40).value #ax3.set_yticks(yticks_Jy) #ax3.set_yticklabels(yticks_mass) #ax3.set_ylabel("Cumulative Mass (M$_\\odot$, $T=40$ K)") c_s_40k = ((constants.k_B * 40 * u.K / (2.4 * u.Da))**0.5).to(u.km / u.s) azimuthal_average_MJ_40k = ( np.pi / 6. * c_s_40k**3 / (constants.G**1.5 * (2.8 * u.Da * azimuthal_average_density)**0.5)).to(u.M_sun) pl.figure(nplots * 3 + 6) pl.semilogy(angular_radii, azimuthal_average_MJ_40k, label=name) pl.ylabel("Azimuthally Averaged $M_J$ at $T=40$K") pl.xlabel("Radius [arcsec]") if len(names) < 5: pl.legend(loc='best') elif ii == len(names) - 1: ax = pl.gca() box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.6, box.height]) # Put a legend to the right of the current axis ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) if ii == len(names) - 1: ax = pl.gca() #ax.set_ylim(1e7, 5e10) ax2 = ax.twiny() #ax3 = ax.twinx() def tick_function(old_x): newx = (old_x * u.arcsec * masscalc.distance).to( u.au, u.dimensionless_angles()).value return ["%.1f" % z for z in newx] new_tick_locations = np.arange(0, 8000, 1000) * u.au new_tick_locs_as = (new_tick_locations / masscalc.distance).to( u.arcsec, u.dimensionless_angles()) ax2.set_xlim(ax.get_xlim()) ax2.set_xticks(new_tick_locs_as.value) ax2.set_xticklabels(tick_function(new_tick_locs_as.value)) ax2.set_xlabel(r"Radius [au]") source = 'e2' if name == 'e2e' else name temperature_map_fn = paths.dpath( '12m/moments/CH3OH_{0}_cutout_temperaturemap.fits'.format(source)) temperature_map_fh = fits.open(temperature_map_fn) # this whole section is copied from overlay_contours_on_ch3oh ch3ohN_hdul = fits.open( paths.dpath( '12m/moments/CH3OH_{0}_cutout_columnmap.fits'.format(source))) ch3ohT_hdul = fits.open( paths.dpath( '12m/moments/CH3OH_{0}_cutout_temperaturemap.fits'.format( source))) bigwcs = wcs.WCS(ch3ohT_hdul[0].header) bigpixscale = (bigwcs.pixel_scale_matrix.diagonal()** 2).sum()**0.5 * u.deg ch3ohN = ch3ohN_hdul[0].data ch3ohT = ch3ohT_hdul[0].data dust_brightness, wts = reproject.reproject_interp( fits.open(paths.dpath('W51_te_continuum_best.fits')), ch3ohN_hdul[0].header) bm = radio_beam.Beam.from_fits_header( paths.dpath("W51_te_continuum_best.fits")) yy, xx = np.indices(ch3ohN.shape) if source == 'north': center = [84., 38.] else: center = [ch3ohN.shape[0] / 2., ch3ohN.shape[1] / 2.] yyc = (yy - center[0]) xxc = (xx - center[1]) rr = (yyc**2 + xxc**2)**0.5 rr_as = (rr * bigpixscale).to(u.arcsec) theta = np.arctan2(yyc, xxc) * u.rad dust_column = dust_emissivity.dust.colofsnu(225 * u.GHz, dust_brightness * u.Jy, beamomega=bm, temperature=ch3ohT * u.K) ch3oh_abundance = ch3ohN / dust_column.value mask = (ch3oh_abundance > 1e-10) & (ch3oh_abundance < 1e-5) if source == 'e2': mask = mask & (((theta > 15 * u.deg) & (theta < 345 * u.deg)) | (theta < -15 * u.deg)) mask = mask & np.isfinite(ch3oh_abundance) # exclude high-abundance, low-column regions: likely to be div-by-zero zones mask = mask & (~((ch3ohN < 1e18) & (ch3oh_abundance > 5e-6))) mask = mask & (~((dust_brightness < 1e-2) & (ch3ohT > 500) & (ch3oh_abundance > 1e-6))) #mask = mask & (~((ch3ohT > 250) & # (ch3ohN < 1e18) & # (rr_as>1.5*u.arcsec)) # )# these are low-column, temwcs = wcs.WCS(temperature_map_fh[0].header) temcutout = Cutout2D(temperature_map_fh[0].data, position, size, wcs=temwcs) maskcutout = Cutout2D(mask.astype('float'), position, size, wcs=bigwcs) tem_nr, tem_bins, tem_rprof = azimuthalAverage( temcutout.data, weights=(maskcutout.data > 0).astype('float'), binsize=1.0, return_nr=True, interpnan=True, ) mass_profile = (cumul_rprof * masscalc.mass_conversion_factor(TK=tem_rprof) / u.beam).to(u.M_sun) density_profile = (mass_profile / (4 / 3. * np.pi * radii**3) / (2.8 * u.Da)).to(u.cm**-3) c_s = ((constants.k_B * tem_rprof * u.K / (2.4 * u.Da))**0.5).to(u.km / u.s) azimuthal_average_MJ = ( np.pi / 6. * c_s**3 / (constants.G**1.5 * (2.8 * u.Da * azimuthal_average_density)**0.5)).to(u.M_sun) pl.figure(nplots * 3 + 7) pl.plot(angular_radii, azimuthal_average_MJ, label=name) pl.ylabel( "Azimuthally Averaged $M_J$ at $T(\\mathrm{CH}_3\\mathrm{OH})$") pl.xlabel("Radius [arcsec]") if len(names) < 5: pl.legend(loc='best') elif ii == len(names) - 1: ax = pl.gca() box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.6, box.height]) # Put a legend to the right of the current axis ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) if ii == len(names) - 1: ax = pl.gca() #ax.set_ylim(1e7, 5e10) ax2 = ax.twiny() #ax3 = ax.twinx() def tick_function(old_x): newx = (old_x * u.arcsec * masscalc.distance).to( u.au, u.dimensionless_angles()).value return ["%.1f" % z for z in newx] new_tick_locations = np.arange(0, 8000, 1000) * u.au new_tick_locs_as = (new_tick_locations / masscalc.distance).to( u.arcsec, u.dimensionless_angles()) ax2.set_xlim(ax.get_xlim()) ax2.set_xticks(new_tick_locs_as.value) ax2.set_xticklabels(tick_function(new_tick_locs_as.value)) ax2.set_xlabel(r"Radius [au]") pl.figure(nplots * 3 + 8) #pl.title(fn.replace(".image.pbcor.fits","")) pl.plot(bins * pixscale * 3600., mass_profile, label=name) pl.ylabel("Cumulative Mass at T(CH$_3$OH)") pl.xlabel("Radius [arcsec]") if len(names) < 5: pl.legend(loc='best') elif ii == len(names) - 1: ax = pl.gca() box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.6, box.height]) # Put a legend to the right of the current axis ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) if ii == len(names) - 1: ax = pl.gca() ax2 = ax.twiny() def tick_function(old_x): newx = (old_x * u.arcsec * masscalc.distance).to( u.au, u.dimensionless_angles()).value return ["%.1f" % z for z in newx] new_tick_locations = np.arange(0, 8000, 1000) * u.au new_tick_locs_as = (new_tick_locations / masscalc.distance).to( u.arcsec, u.dimensionless_angles()) ax2.set_xlim(ax.get_xlim()) ax2.set_xticks(new_tick_locs_as.value) ax2.set_xticklabels(tick_function(new_tick_locs_as.value)) ax2.set_xlabel(r"Radius [au]") pl.figure(nplots * 3 + 9) #pl.title(fn.replace(".image.pbcor.fits","")) pl.plot(bins * pixscale * 3600., tem_rprof, label=name) pl.legend(loc='best') pl.ylabel("CH$_3$OH temperature") pl.xlabel("Radius (as)") azimuthal_average_RJ = ( c_s**1 / (constants.G**0.5 * (2.8 * u.Da * azimuthal_average_density)**0.5)).to(u.au) pl.figure(nplots * 3 + 10) pl.plot(angular_radii, azimuthal_average_RJ, label=name) pl.plot([ 0, (7000 * u.au / masscalc.distance).to( u.arcsec, u.dimensionless_angles()).value ], [0, 7000], 'k--', alpha=0.5) pl.ylabel( "Azimuthally Averaged $R_J$ at $T(\\mathrm{CH}_3\\mathrm{OH})$ [au]" ) pl.xlabel("Radius [arcsec]") pl.ylim(0, 3000) if len(names) < 5: pl.legend(loc='best') elif ii == len(names) - 1: ax = pl.gca() box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.6, box.height]) # Put a legend to the right of the current axis ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) if ii == len(names) - 1: ax = pl.gca() #ax.set_ylim(1e7, 5e10) ax2 = ax.twiny() #ax3 = ax.twinx() def tick_function(old_x): newx = (old_x * u.arcsec * masscalc.distance).to( u.au, u.dimensionless_angles()).value return ["%.1f" % z for z in newx] new_tick_locations = np.arange(0, 8000, 1000) * u.au new_tick_locs_as = (new_tick_locations / masscalc.distance).to( u.arcsec, u.dimensionless_angles()) ax2.set_xlim(ax.get_xlim()) ax2.set_xticks(new_tick_locs_as.value) ax2.set_xticklabels(tick_function(new_tick_locs_as.value)) ax2.set_xlabel(r"Radius [au]")
def FK4CoordGenerator(*args, **kwargs): return coord.SkyCoord(*args, frame='fk4', **kwargs)