Package ivs :: Package catalogs :: Module hermes
[hide private]
[frames] | no frames]

Module hermes

source code

Interface the spectra from the Hermes spectrograph.

The most important function is search. This looks in SIMBAD for the coordinates of a given object, and finds all spectra matching those within a given radius. If the object's name is not recognised, it will look for correspondence between the given name and the contents of the FITS header's keyword object. search returns a record array, so that you can easily access all columns by their names.

Note that Hermes spectra retrieved in logscale should not be corrected for the barycentric velocity (the pipeline does it). The spectra retrieved in normal wavelength scale should be corrected. These two cases are outlined below.

Section 1. Data lookup and reading

Example usage: retrieve all data on HD170580

>>> mydata = search('HD170580')

Keep only those with a long enough exposure time:

>>> myselection = mydata[mydata['exptime']>500]

Now read in all the data, and plot the spectra. First, we need an extra module to read the FITS file and the plotting package.

>>> from ivs.inout import fits
>>> import pylab as pl

Then we can easily plot the relevant data:

>>> for fname in myselection['filename']:
...     wave,flux = fits.read_spectrum(fname)
...     p = pl.plot(wave,flux)
>>> p = pl.ylim(0,45000)
>>> p = pl.xlim(4511,4513.5)

]include figure]]ivs_catalogs_hermes_HD170580.png]

Note that you can easily shift them according to some radial velocity: as an extra example, we retrieve the data in wavelength scale, shift according to the barycentric velocity, convert to velocity space, and plot them:

First start a new figure and add extra modules:

>>> p = pl.figure()
>>> from ivs.spectra.tools import doppler_shift         # to apply doppler shift
>>> from ivs.units import conversions     # to convert to velocity space

Then get the spectra again, but now not in log scale. Make the same selection on exposure time.

>>> mydata = search('HD170580',data_type='cosmicsremoved_wavelength')
>>> myselection = mydata[mydata['exptime']>500]

Then read them all in and shift according to the barycentric velocity. Also, convert the spectra to velocity space afterwards for plotting purposes.

>>> for rv,fname in zip(myselection['bvcor'],myselection['filename']):
...     wave,flux = fits.read_spectrum(fname)
...     wave_shifted = model.doppler_shift(wave,rv)
...     velo_shifted = conversions.convert('angstrom','km/s',wave_shifted,wave=(4512.3,'angstrom'))
...     p = pl.plot(velo_shifted,flux)
>>> p = pl.ylim(0,45000)
>>> p = pl.xlim(-70,70)

]include figure]]ivs_catalogs_hermes_HD170580_velo.png]

Section 2. Extracting information

If you want to list the observers of a certain target, and the amount of spectra they took, you can do the following:

>>> data = search('HD50230')
>>> print [(i,list(data['observer']).count(i)) for i in set(data['observer'])]
[('Robin Lombaert', 4), ('Steven Bloemen', 6), ('Pieter Degroote', 25), ('Michel Hillen', 3)]

Section 3. Radial velocity computation with the HERMES DRS

Make sure you have sourced the Hermes DRS (source ~mercator/HermesDRS.rc) and that you make a backup of your config file before proceeding.

In this example, we first make a mask file:

>>> from ivs.spectra import linelists
>>> teff = 12500
>>> logg = 4.0
>>> ll = linelists.get_lines(teff,logg)
>>> mask_file = '%.0f_%.2f.fits'%(teff,logg)
>>> make_mask_file(ll['wavelength'],ll['depth'],filename=mask_file)

And run the DRS:

>>> CCFList('HD170200',mask_file=mask_file)

Section 4. Hermes overview file

To ensure a fast lookup of datafiles, an overview file HermesFullDataOverview.tsv is created via make_data_overview. The file resides in one of the IVSdata directories, and there should also exist a copy at /STER/mercator/hermes/.

The best strategy to keep the file up-to-date is by running this module in the background in a terminal, via (probably on pleiad22):

   $:> python hermes.py update

This script will look for a HermesFullDataOverview.tsv in one of the data directories (see config module), check until what date data files are stored, and add entries for HERMES data files created since the last update. If you want a complete update of all the HERMES data folders, you will need to remove the file and run the update. Indeed, if HermesFullDataOverview.tsv does not exist, it will create a new file in the current working directory from scratch, and copy it to /STER/mercator/hermes/. It is the user's responsibility to copy the file also to one of the IVSdata directories where the user has write permission, so that next time the update is performed, the script can start from the previous results.

When running the above command in the terminal, you will notice that the script does not terminate until a manual CTRL+C is performed. Indeed, when left running the script will perform the update once a day. So once it runs, as long as the filepaths do not change or computers do not shut down, you don't need to run it again. If a computer does stop running, just restart again with:

   $:> python hermes.py update

and everything should be fine.

Classes [hide private]
  HermesCCF
Object that holds all information stored in the *_AllCCF.fits files written by hermesVR.
Functions [hide private]
    User functions
numpy rec array
search(ID=None, time_range=None, prog_ID=None, data_type='cosmicsremoved_log', radius=1., filename=None)
Retrieve datafiles from the Hermes catalogue.
source code
[array, array, dict]
merge_hermes_spectra(objlist, wscalelist=None, **kwargs)
Combines HERMES spectra with sigma clipping to remove cosmics.
source code
 
make_list_star(ID, direc=None)
Mimics HermesTool MakeListStar.
source code
 
make_mask_file(wavelength, depth, filename='mymask.fits')
Make a mask file for RV calculations with the Hermes pipeline.
source code
 
calibrate(wave, flux, header=None)
Rough calibration of Hermes spectrum.
source code
    Read / Write
record array
read_hermesVR_velocities(unique=True, return_latest=True, unseq=None, object=None, **kwargs)
Read the velocities.data file with the resuls from hermesVR.
source code
HermesCCF object
read_hermesVR_AllCCF(unseq)
Read the AllCCF.fits files written by hermesVR with the individual cross correlation functions of each order.
source code
dict
write_hermesConfig(**kwargs)
Update the hermesConfig.xml file with the specified keywords.
source code
    Hermes DRS wrappers
(int, str, int)
run_hermesVR(filename, mask_file=None, wvl_file=None, cosmic_clipping=True, flatfield=False, vrange='default', vrot=False, version='release', verbose=False, timeout=500, **kwargs)
Wrapper around hermesVR that will run hermesVR on any given filename.
source code
 
CCFList(ID, config_dir=None, out_dir=None, mask_file=None, cosmic_clipping=True, flatfield=False)
Calculate radial velocities for Hermes stars.
source code
    Administrator functions
 
make_data_overview()
Summarize all Hermes data in a file for easy data retrieval.
source code
 
_derive_filelocation_from_raw(rawfile, data_type)
Derive the location of a reduced file from the raw file.
source code
float
_timestamp2jd(timestamp)
Convert the time stamp from a HERMES FITS 'date-avg' to Julian Date.
source code
datetime
_timestamp2datetime(timestamp)
Convert the time stamp from a HERMES FITS 'date-avg' to a datetime object.
source code
dict
_etree_to_dict(t)
Convert a xml tree to a dictionary.
source code
 
_subprocess_execute(command, time_out=100)
executing the command with a watchdog
source code
Variables [hide private]
  hermesDir = os.path.expanduser('~/')
Path to the directory in which the hermes folders (hermesAnalysis, hermesRun, hermesDebug) are located.
  tempDir = '/scratch/%s/' %(getpass.getuser())
Path to temporary directory where files nessessary for hermesVR to run can be copied to.
  logger = logging.getLogger("CAT.HERMES")
Function Details [hide private]

search(ID=None, time_range=None, prog_ID=None, data_type='cosmicsremoved_log', radius=1., filename=None)

source code 

Retrieve datafiles from the Hermes catalogue.

If ID is given: A string search is performed to match the 'object' field in the FITS headers. The coordinates are pulled from SIMBAD. If the star ID is recognised by SIMBAD, an additional search is done based only on the coordinates. The union of both searches is the final result.

If time_range is given: The search is confined within the defined range. If you only give one day, the search is confined to the observations made during the night starting at that day. If ID is not given, all observations will be returned of the given datatype.

If prog_ID is given: The search is performed to match the number of the program. Individual stars are not queried in SIMBAD, so any information that is missing in the header will not be corrected.

If you don't give either ID or time_range, the info on all data will be returned. This is a huge amount of data, so it can take a while before it is returned. Remember that the header of each spectrum is read in and checked.

Data type can be any of:

  1. cosmicsremoved_log: return log merged without cosmics
  2. cosmicsremoved_wavelength: return wavelength merged without cosmics
  3. ext_log: return log merged with cosmics
  4. ext_wavelength: return wavelength merged with cosmics
  5. raw: raw files (also TECH..., i.e. any file in the raw directory)

This functions needs a HermesFullDataOverview.tsv file located in one of the datadirectories from config.py, and subdirectory catalogs/hermes.

If this file does not exist, you can create it with make_data_overview.

If you want a summary file with the data you search for, you can give filename as an extra keyword argument. The results will be saved to that file.

The columns in the returned record array are listed in make_data_overview, but are repeated here (capital letters are directly retrieved from the fits header, small letters are calculated values. The real header strings are all small capitals):

  1. UNSEQ
  2. PROG_ID
  3. OBSMODE
  4. BVCOR
  5. OBSERVER
  6. OBJECT
  7. RA
  8. DEC
  9. BJD
  10. EXPTIME
  11. PMTOTAL
  12. DATE-AVG
  13. OBJECT
  14. airmass
  15. filename

The column filename contains a string with the absolute location of the file. If you need any extra information from the header, you can easily retrieve it.

If BVCOR or BJD are not available from the FITS header, this function will attempt to calculate it. It will not succeed if the object's name is not recognised by SIMBAD.

Example usage: retrieve all data on HD50230

>>> mydata = search('HD50230')

Keep only those with a long enough exposure time:

>>> myselection = mydata[mydata['exptime']>500]

Look up the 'telalt' value in the FITS headers of all these files via a fast list comprehension:

>>> telalts = [pf.getheader(fname)['telalt'] for fname in myselection['filename']]

Search for all data of HD50230 taken in the night of 22 September 2009:

>>> data = hermes.search('HD50230',time_range='2009-9-22')

Or within an interval of a few days:

>>> data = hermes.search('HD50230',time_range=('2009-9-23','2009-9-30'))

Search for all data observed in a given night:

>>> data = hermes.search(time_range='2009-9-22')

Warning: the heliocentric correction is not calculated when no ID is given, so make sure it is present in the header if you need it, or calculate it yourself.

Parameters:
  • ID (str) - ID of the star, understandable by SIMBAD
  • time_range (tuple strings of type '2009-09-23T04:24:35.712556' or '2009-09-23') - range of dates to confine the search to
  • data_type (str) - if None, all data will be returned. Otherwise, subset 'cosmicsremoved', 'merged' or 'raw'
  • radius (float) - search radius around the coordinates (arcminutes)
  • filename (str) - write summary to outputfile if not None
Returns: numpy rec array
record array with summary information on the observations, as well as their location (column 'filename')

merge_hermes_spectra(objlist, wscalelist=None, **kwargs)

source code 

Combines HERMES spectra with sigma clipping to remove cosmics. The input spectra are given as a list of filenames for the objects and for the wavelength scales. The output is the wavelength scale of the first object, the merged and sigma clipped flux, and the adapted header of the object. In this adapted header, the exposure time is the sum of all individual exposure times, and the observing time is averaged. Uses the ivs.spectra.tools.merge_cosmic_clipping method to merge the spectra.

>>> data = search('KIC9540226')
>>> objlist = data['filename']
>>> wave, flux, header = merge_hermes_spectra(objlist, wscalelist=None)
Parameters:
  • objlist - list of OBJ or order merged filenames
  • wscalelist - list of wavelengthscale filenames
  • vrads - list of radial velocities (optional)
  • vrad_units - units of the radial velocities
  • sigma - value used for sigma clipping
  • window - window size used in median filter
  • runs - number of iterations through the spectra
Returns: [array, array, dict]
The combined spectrum, cosmic clipped. (wave, flux, header)

make_list_star(ID, direc=None)

source code 

Mimics HermesTool MakeListStar.

This should work as input for HermesTool CCFList.py

The result is a file in the current working directory with name ID.list. If you have specified the direc keyword, the file will be written inside that directory. Make sure you have write permission.

The contents of the file is:

unseq, date-avg, ID, bjd, bvcor, prog_id, exptime, airmass, pmtotal

Parameters:
  • ID (string) - name of the star, understandable by SIMBAD.
  • direc (string) - directory to write the file into (defaults to current working directory)

make_mask_file(wavelength, depth, filename='mymask.fits')

source code 

Make a mask file for RV calculations with the Hermes pipeline.

See ivs.units.linelists to select appropriate masks. You readily use the columns wavelength and depth as input for this function.

Parameters:
  • wavelength (1D numpy array) - wavelength in angstrom
  • depth (1D numpy array) - strenght of the line (1-normalised flux minimum)

calibrate(wave, flux, header=None)

source code 

Rough calibration of Hermes spectrum.

Thanks to Roy Ostensen for computing the response function via detailed modeling of Feige 66.

read_hermesVR_velocities(unique=True, return_latest=True, unseq=None, object=None, **kwargs)

source code 

Read the velocities.data file with the resuls from hermesVR. The results are returned as a numpy record array. By setting unique to True, the method will only return a unique set of sequence numbers. When return_latest is True it will pick the latest added version, otherwise the first added is picked.

You can supply a list of unseq numbers, and then only those will be returned, same goes for object. In both cases the output is ordered in the same way as the list of unseq or object that you provided. These lists can be used together with the unique option.

This method will search for 'velocities.data' in 'hermesDir/hermesAnalyses/'. You can provide another file location by using the keyword filename.

The field in the recored array are:

  • unseq: unique number
  • object: object name
  • hjd: HJD
  • exptime: exposure time
  • bvcorr: bary_corr
  • rvdrift: RV drift (2F frames only)
  • telloff: telluric offset
  • tellofferr: error on telluric offset
  • telloffwidth: width telluric fit
  • vrad: Vrad(55-74) (bvcorr applied)
  • vraderr: err on Vrad
  • nlines: number of lines used
  • ccfdepth: depth_CCF
  • ccfdeptherr: error on depth_CCF
  • ccfsigma: sigma_CCF
  • ccfsigmaerr: err on sigma_CCF
  • sn: signal to noise in spectrum (orders 55-74)
  • gaussoc: O-C of Gaussian fit
  • wvlfile: wavelength calibration file
  • maskfile: mask file
  • fffile: Flatfield file
Parameters:
  • unique (bool) - True is only return unique sequence numbers
  • return_latest (bool) - True to pick the last added file
  • unseq (list) - list of sequence numbers to return
  • object (list) - list of object names to return
  • filename (str) - Path to the velocities.data file
Returns: record array
array with the requested result

read_hermesVR_AllCCF(unseq)

source code 

Read the AllCCF.fits files written by hermesVR with the individual cross correlation functions of each order. The cross-correlation functions are stored in a HermesCCF object from which they can be requested.

You can read *_AllCCF.fits file by providing only the unique sequence number as an integer. In this case the file will be searched in the hermesDir/hermesAnalyses/ folder. You can also provide the complete filename instead as a string fx.

>>> read_hermesVR_AllCCF(457640)
>>> read_hermesVR_AllCCF('/home/jorisv/hermesAnalysis/00457640_AllCCF.fits')
Parameters:
  • unseq (int or str) - unique sequence number or filename to read
Returns: HermesCCF object
The cross correlation functions

write_hermesConfig(**kwargs)

source code 

Update the hermesConfig.xml file with the specified keywords. This method will return the original content of the file as a dictionary. You can use that dict as an argument to this function if you want to restore the hermesConfig file to the original state.

write_hermesConfig will search for hermesConfig.xml in hermesDir/hermesRun/ unless a path is specified in the kwarg filename.

example use:

>>> old = write_hermesConfig(CurrentNight="/STER/mercator/hermes/20101214")
>>> " Do some stuff "
>>> write_hermesConfig(**old)
Parameters:
  • filename (str) - complete path to the hermesConfig.xml file
Returns: dict
The original content of hermesConfig.xml before modification.

Attention: This function does not check the validity of the keywords that you provide, that is your own responsibility.

run_hermesVR(filename, mask_file=None, wvl_file=None, cosmic_clipping=True, flatfield=False, vrange='default', vrot=False, version='release', verbose=False, timeout=500, **kwargs)

source code 

Wrapper around hermesVR that will run hermesVR on any given filename. Thus not on sequence number. This can be usefull if you first merge together 2 back-to-back spectra and then want to calculate the radial velocity of the merged spectrum.

The original file will not be modified, it is coppied to the tempDir, and the name will be changed according to the supplied flags. During the process hermesConfig.xml is adapted, but after hermesVR is finished, it is restored to its old state.

When providing a wavelength scale file, provide the full path, but cut off the '_HRF_TH_ext_wavelengthScale.fits' part of the filename. Thus if you want to use: '/folder/2451577_HRF_TH_ext_wavelengthScale.fits', use:

>>> run_hermesVR(filename, wvl_file = '/folder/2451577')

The results of hermesVR are saved to the standard paths, as provided in the hermesConfig file. This method only returns the unseq number of the filename so you can find the corresponding output file, the output of hermesVR as a string, and a unix return signal of running hermesVR. The returncode is 0 if no errors are found. Any negative number means hermesVR failed (codes can be consulted here: unix signals). If returncode = -35, then hermesVR completed but didn't write any output. In this case consult the logs or string output to find out what went wrong.

Parameters:
  • filename (str) - complete path to the input spectrum for hermesVR
  • mask_file (str) - complete path to the mask file to use or None
  • wvl_file (str) - path to wavelengthscale file or None
  • cosmic_clipping (bool) - Doesn't do anything for now
  • flatfield (bool) - Use flatfield devision or not
  • vrange (str) - Size of velocity window ('default', 'large' (x2), 'xlarge' (x5))
  • vrot (bool) - Fit CCF with rotationaly broadend profile instead of gaussian
  • version (str) - which version of the pipeline to use: 'release' or 'trunk'
  • verbose (bool) - print output of hermesVR
  • timeout (int) - Maximum time hermesVR is allowed to run in seconds
Returns: (int, str, int)
unseq used, text output of hermesVR, unix return code

CCFList(ID, config_dir=None, out_dir=None, mask_file=None, cosmic_clipping=True, flatfield=False)

source code 

Calculate radial velocities for Hermes stars.

Warnings:
  • you have to have the DRS installed locally!
  • this function changes your hermesConfig.xml file, you'd better backup it before use

make_data_overview()

source code 

Summarize all Hermes data in a file for easy data retrieval.

The file is located in one of date data directories (see config.py), in subdirectories catalogs/hermes/HermesFullDataOverview.tsv. If it doesn't exist, it will be created. It contains the following columns, which are extracted from the Hermes FITS headers (except filename:

  1. UNSEQ
  2. PROG_ID
  3. OBSMODE
  4. BVCOR
  5. OBSERVER
  6. OBJECT
  7. RA
  8. DEC
  9. BJD
  10. EXPTIME
  11. PMTOTAL
  12. DATE-AVG
  13. OBJECT
  14. airmass
  15. filename

This file can most easily be read with the ivs.inout.ascii module and the command:

>>> hermes_file = config.get_datafile(os.path.join('catalogs','hermes'),'HermesFullDataOverview.tsv')
>>> data = ascii.read2recarray(hermes_file,splitchar='\t')

_timestamp2jd(timestamp)

source code 

Convert the time stamp from a HERMES FITS 'date-avg' to Julian Date.

Parameters:
  • timestamp (string) - string from 'date-avg'
Returns: float
julian date

_timestamp2datetime(timestamp)

source code 

Convert the time stamp from a HERMES FITS 'date-avg' to a datetime object.

Parameters:
  • timestamp (string) - string from 'date-avg'
Returns: datetime
datetime object

_etree_to_dict(t)

source code 

Convert a xml tree to a dictionary.

Parameters:
  • t (ElementTree root) - the xml tree
Returns: dict
the xml tree as a dictionary

Variables Details [hide private]

hermesDir

Path to the directory in which the hermes folders (hermesAnalysis, hermesRun, hermesDebug) are located. By default this is set to '/home/user/'.
Value:
os.path.expanduser('~/')

tempDir

Path to temporary directory where files nessessary for hermesVR to run can be copied to. You need write permission in this directory. The default is set to '/scratch/user/'
Value:
'/scratch/%s/' %(getpass.getuser())