def ned_query(ra, dec): # getting skycoordinate object co = coordinates.SkyCoord(ra=ra, dec=dec, unit=(u.deg, u.deg), frame='icrs') search_radius = 0.01 * u.deg # this search radius is smaller than Fermi LAT resolution (please check) # get table with all objects inside the search radius result_table = Ned.query_region(co, radius=search_radius, equinox='J2000.0') result_table = Ned.query_object("NGC 224") print('names of the columns are ', result_table.colnames) print('to get an impression, here the whole table:') print(result_table) # get all the object names to get speific data object_names = result_table['Object Name'] print('identified objects = ', object_names) # get table with positions of the first object as an example: position_table = Ned.get_table(object_names[0], table='positions') # position table is something you normally don't need but might be interesting # This should always work' # print('position table: ', position_table) spectra = Ned.get_spectra("3c 273") return result_table, spectra
def getNEDSpectra(df, path, verbose=0): hostNames = np.array(df.dropna(subset=['NED_name'])['NED_name']) transientNames = np.array(df.dropna(subset=['NED_name'])['TransientName']) for j in np.arange(len(hostNames)): try: spectra = Ned.get_spectra(hostNames[j]) except: continue if spectra: for i in np.arange(len(spectra)): fn = re.sub(r"^\s+", "", hostNames[j]) a = find_all("%s_%.02i.fits" % (fn, i), path) sp = spectra[i] if not a: if verbose: print("Saving spectrum %i for %s, host of %s." % (i, hostNames[j], transientNames[j])) try: sp.writeto(path + "/%s_%.02i.fits" % (fn, i), output_verify='ignore') except: if verbose: print("Error in saving host spectrum for %s." % transientNames[j]) elif verbose: # if verbose: print("host spectrum for %s already downloaded!" % transientNames[j])
def load_spectra(self): """Load reference spectra from Pysynphot database or NED database. If the object redshift is >0.2, the LAMBDA_MIN and LAMBDA_MAX parameters are redshifted accordingly. Examples -------- >>> s = Star('3C273') >>> print(s.spectra[0][:4]) [0.0000000e+00 2.5048577e-14 2.4238061e-14 2.4088789e-14] >>> s = Star('HD111980') >>> print(s.spectra[0][:4]) [2.16890002e-13 2.66480010e-13 2.03540011e-13 2.38780004e-13] >>> s = Star('PKS1510-089') >>> print(s.redshift) 0.36 >>> print(f'{parameters.LAMBDA_MIN:.1f}, {parameters.LAMBDA_MAX:.1f}') 408.0, 1496.0 >>> print(s.spectra[0][:4]) [117.34012 139.27621 87.38032 143.0816 ] """ self.wavelengths = [] # in nm self.spectra = [] # first try with pysynphot file_names = [] is_calspec = False if os.getenv("PYSYN_CDBS") is not None: dirname = os.path.expandvars('$PYSYN_CDBS/calspec/') for fname in os.listdir(dirname): if os.path.isfile(os.path.join(dirname, fname)): if self.label.lower().replace(' ', '') in fname.lower(): file_names.append(os.path.join(dirname, fname)) if len(file_names) > 0: is_calspec = True self.emission_spectrum = False self.hydrogen_only = False self.lines = Lines(HYDROGEN_LINES + ATMOSPHERIC_LINES + STELLAR_LINES, redshift=self.redshift, emission_spectrum=self.emission_spectrum, hydrogen_only=self.hydrogen_only) for k, f in enumerate(file_names): if '_mod_' in f: continue if self.verbose: self.my_logger.info('\n\tLoading %s' % f) data = S.FileSpectrum(f, keepneg=True) if isinstance(data.waveunits, S.units.Angstrom): self.wavelengths.append(data.wave / 10.) self.spectra.append(data.flux * 10.) else: self.wavelengths.append(data.wave) self.spectra.append(data.flux) # TODO DM-33731: the use of self.label in parameters.STAR_NAMES: # below works for running but breaks a test so needs fixing for DM elif 'HD' in self.label: # or self.label in parameters.STAR_NAMES: # it is a star self.emission_spectrum = False self.hydrogen_only = False self.lines = Lines(ATMOSPHERIC_LINES + HYDROGEN_LINES + STELLAR_LINES, redshift=self.redshift, emission_spectrum=self.emission_spectrum, hydrogen_only=self.hydrogen_only) elif 'PNG' in self.label: self.emission_spectrum = True self.lines = Lines(ATMOSPHERIC_LINES + ISM_LINES + HYDROGEN_LINES, redshift=self.redshift, emission_spectrum=self.emission_spectrum, hydrogen_only=self.hydrogen_only) else: # maybe a quasar, try with NED query from astroquery.ned import Ned hdulists = Ned.get_spectra(self.label, show_progress=False) if len(hdulists) > 0: self.emission_spectrum = True self.hydrogen_only = False if self.redshift > 0.2: self.hydrogen_only = True parameters.LAMBDA_MIN *= 1 + self.redshift parameters.LAMBDA_MAX *= 1 + self.redshift self.lines = Lines(ATMOSPHERIC_LINES + ISM_LINES + HYDROGEN_LINES, redshift=self.redshift, emission_spectrum=self.emission_spectrum, hydrogen_only=self.hydrogen_only) for k, h in enumerate(hdulists): if h[0].header['NAXIS'] == 1: self.spectra.append(h[0].data) else: for d in h[0].data: self.spectra.append(d) wave_n = len(h[0].data) if h[0].header['NAXIS'] == 2: wave_n = len(h[0].data.T) wave_step = h[0].header['CDELT1'] wave_start = h[0].header['CRVAL1'] - ( h[0].header['CRPIX1'] - 1) * wave_step wave_end = wave_start + wave_n * wave_step waves = np.linspace(wave_start, wave_end, wave_n) is_angstrom = False for key in list(h[0].header.keys()): if 'angstrom' in str(h[0].header[key]).lower(): is_angstrom = True if is_angstrom: waves *= 0.1 if h[0].header['NAXIS'] > 1: for i in range(h[0].header['NAXIS'] + 1): self.wavelengths.append(waves) else: self.wavelengths.append(waves) self.build_sed() self.my_logger.debug( f"\n\tTarget label: {self.label}" f"\n\tCalspec? {is_calspec}" f"\n\tNumber of spectra: {len(self.spectra)}" f"\n\tRedshift: {self.redshift}" f"\n\tEmission spectrum ? {self.emission_spectrum}" f"\n\tLines: {[l.label for l in self.lines.lines]}")
def load_spectra(self): self.wavelengths = [] # in nm self.spectra = [] # first try with pysynphot file_names = [] if os.getenv("PYSYN_CDBS") is not None: dirname = os.path.expandvars('$PYSYN_CDBS/calspec/') for fname in os.listdir(dirname): if os.path.isfile(dirname + fname): if self.label.lower() in fname.lower(): file_names.append(dirname + fname) if len(file_names) > 0: self.emission_spectrum = False self.hydrogen_only = True for k, f in enumerate(file_names): if '_mod_' in f: continue if self.verbose: print 'Loading %s' % f data = S.FileSpectrum(f, keepneg=True) if isinstance(data.waveunits, S.units.Angstrom): self.wavelengths.append(data.wave / 10.) self.spectra.append(data.flux * 10.) else: self.wavelengths.append(data.wave) self.spectra.append(data.flux) else: if 'PNG' not in self.label: # Try with NED query # print 'Loading target %s from NED...' % self.label ned = Ned.query_object(self.label) hdulists = Ned.get_spectra(self.label) self.redshift = ned['Redshift'][0] self.emission_spectrum = True self.hydrogen_only = False if self.redshift > 0.2: self.hydrogen_only = True parameters.LAMBDA_MIN *= 1 + self.redshift parameters.LAMBDA_MAX *= 1 + self.redshift for k, h in enumerate(hdulists): if h[0].header['NAXIS'] == 1: self.spectra.append(h[0].data) else: for d in h[0].data: self.spectra.append(d) wave_n = len(h[0].data) if h[0].header['NAXIS'] == 2: wave_n = len(h[0].data.T) wave_step = h[0].header['CDELT1'] wave_start = h[0].header['CRVAL1'] - ( h[0].header['CRPIX1'] - 1) * wave_step wave_end = wave_start + wave_n * wave_step waves = np.linspace(wave_start, wave_end, wave_n) is_angstrom = False for key in h[0].header.keys(): if 'angstrom' in str(h[0].header[key]).lower(): is_angstrom = True if is_angstrom: waves *= 0.1 if h[0].header['NAXIS'] > 1: for i in range(h[0].header['NAXIS'] + 1): self.wavelengths.append(waves) else: self.wavelengths.append(waves) else: self.emission_spectrum = True self.build_sed()
def test6(): spectra = Ned.get_spectra("3c 273") print spectra