This notebook is available at astrodbtoolkit/astrodb_utils

Add a FITS header to a spectrum file#

[1]:
import sys

import astropy.units as u
import numpy as np

sys.path.append('..')
from astrodb_utils.fits import add_missing_keywords, add_observation_date, add_wavelength_keywords, check_header

/Users/kelle/miniforge3/envs/template-dev/lib/python3.13/site-packages/requests/__init__.py:86: RequestsDependencyWarning: Unable to find acceptable character detection dependency (chardet or charset_normalizer).
  warnings.warn(

Make a new header#

Use the astrodb_utils function add_missing_keywords to make a header object called header with the needed keywords set to None:

[ ]:
header = add_missing_keywords()

COPY AND PASTE THE FOLLOWING COMMANDS INTO YOUR SCRIPT
Replace <value> with the appropriate value for your dataset
If you're not sure of the correct value, use None
If you started with a header object not called `header`, replace 'header' with the name of your header object
Use the `astrodb_utils.fits.add_wavelength_keywords` function to add the SPEC_VAL, SPEC_BW, and SPECBAND keywords


header.set('OBJECT', "<value>")
header.set('RA_TARG', "<value>")
header.set('DEC_TARG', "<value>")
header.set('DATE-OBS', "<value>")
header.set('INSTRUME', "<value>")
header.set('TELESCOP', "<value>")
header.set('TELAPSE', "<value>")
header.set('APERTURE', "<value>")
header.set('AUTHOR', "<value>")
header.set('TITLE', "<value>")
header.set('VOREF', "<value>")
header.set('VOPUB', "<value>")
header.set('CONTRIB1', "<value>")
header.set('SPEC_VAL', "<value>")
header.set('SPEC_BW', "<value>")
header.set('SPECBAND', "<value>")

Inspect the new header.#

The header created with add_missing_keywords has several keywords but they are all set to None

[3]:
header
[3]:
OBJECT  =  / Name of observed object
RA_TARG =  / [deg] target position
DEC_TARG=  / [deg] target position
DATE-OBS=  / Date of observation
INSTRUME=  / Instrument name
TELESCOP=  / Telescope name
TELAPSE =  / [s] Total elapsed time (s)
APERTURE=  / [arcsec] slit width
AUTHOR  =  / Authors of original dataset
TITLE   =  / Dataset title
VOREF   =  / URL, DOI, or bibcode of original publication
VOPUB   =  / Publisher
CONTRIB1=  / Contributor who generated this header
SPEC_VAL=  / [angstrom] Characteristic spectral coordinate
SPEC_BW =  / [angstrom] width of spectrum
SPECBAND=  / SED.bandpass

Add the wavelength keywords#

Use astrodb_utils add_wavelength_keywords function to generate and add the wavelength header keywords using the wavelength Quantity array.

[4]:
wavelength = np.arange(5100, 5300)*u.AA
add_wavelength_keywords(header, wavelength)
header  #  Notice the new keywords and values added to the header
[4]:
OBJECT  =  / Name of observed object
RA_TARG =  / [deg] target position
DEC_TARG=  / [deg] target position
DATE-OBS=  / Date of observation
INSTRUME=  / Instrument name
TELESCOP=  / Telescope name
TELAPSE =  / [s] Total elapsed time (s)
APERTURE=  / [arcsec] slit width
AUTHOR  =  / Authors of original dataset
TITLE   =  / Dataset title
VOREF   =  / URL, DOI, or bibcode of original publication
VOPUB   =  / Publisher
CONTRIB1=  / Contributor who generated this header
SPEC_VAL=               5199.5 / [Angstrom] Characteristic spec coord
SPEC_BW =                199.0 / [Angstrom] Width of spectrum
SPECBAND= 'em.opt.V'           / SED.bandpass
TDMIN1  =               5100.0 / [Angstrom] Starting wavelength
TDMAX1  =               5299.0 / [Angstrom] Ending wavelength
HISTORY Wavelength keywords added by astrodb_utils.fits.add_wavelength_keywords

Add the observation date#

Use the astrodb_utils function add_observation_date to add the observation date to the header.

[5]:
add_observation_date(header, '1/1/21')
Date of observation: Jan 01, 2021
DATE-OBS set to : 2021-01-01.
/var/folders/b3/lj1wwttj1wn62jczjjrcp8h80000gn/T/ipykernel_29950/383763279.py:1: DeprecationWarning: Parsing dates involving a day of month without a year specified is ambiguious
and fails to parse leap day. The default behavior will change in Python 3.15
to either always raise an exception or to use a different default year (TBD).
To avoid trouble, add a specific year to the input & format.
See https://github.com/python/cpython/issues/70647.
  add_observation_date(header, '1/1/21')

Check for any keywords that are still missing.#

Run add_missing_keywords, this time with header as input, to see what keywords are still missing.

[6]:
add_missing_keywords(header=header)

COPY AND PASTE THE FOLLOWING COMMANDS INTO YOUR SCRIPT
Replace <value> with the appropriate value for your dataset
If you're not sure of the correct value, use None
If you started with a header object not called `header`, replace 'header' with the name of your header object
Use the `astrodb_utils.fits.add_wavelength_keywords` function to add the SPEC_VAL, SPEC_BW, and SPECBAND keywords


header.set('OBJECT', "<value>")
header.set('RA_TARG', "<value>")
header.set('DEC_TARG', "<value>")
header.set('INSTRUME', "<value>")
header.set('TELESCOP', "<value>")
header.set('TELAPSE', "<value>")
header.set('APERTURE', "<value>")
header.set('AUTHOR', "<value>")
header.set('TITLE', "<value>")
header.set('VOREF', "<value>")
header.set('VOPUB', "<value>")
header.set('CONTRIB1', "<value>")
[6]:
OBJECT  =  / Name of observed object
RA_TARG =  / [deg] target position
DEC_TARG=  / [deg] target position
DATE-OBS= '2021-01-01'         / date of the observation
INSTRUME=  / Instrument name
TELESCOP=  / Telescope name
TELAPSE =  / [s] Total elapsed time (s)
APERTURE=  / [arcsec] slit width
AUTHOR  =  / Authors of original dataset
TITLE   =  / Dataset title
VOREF   =  / URL, DOI, or bibcode of original publication
VOPUB   =  / Publisher
CONTRIB1=  / Contributor who generated this header
SPEC_VAL=               5199.5 / [Angstrom] Characteristic spec coord
SPEC_BW =                199.0 / [Angstrom] Width of spectrum
SPECBAND= 'em.opt.V'           / SED.bandpass
TDMIN1  =               5100.0 / [Angstrom] Starting wavelength
TDMAX1  =               5299.0 / [Angstrom] Ending wavelength
HISTORY Wavelength keywords added by astrodb_utils.fits.add_wavelength_keywords

Set the remaining keywords by hand#

Set any remaining keywords by hand using header.set. If the keyword is not known, assign None. The below values are just made up and not real.

[7]:
header.set('OBJECT', "SIMP J013656.5+093347.3")
header.set('RA_TARG', 24.2356689249292 )
header.set('DEC_TARG', 9.5631422127692)
header.set('INSTRUME', "NIRSPEC")
header.set('TELESCOP', "JWST")
header.set('TELAPSE', None)
header.set('APERTURE', None)
header.set('AUTHOR', "McCarthy et al.")
header.set('TITLE', "An amazing paper")
header.set('VOREF', "10.3847/1538-3881/aa9d8b")
header.set('VOPUB', "SIMPLE Archive https://simple-bd-archive.org/")
header.set('CONTRIB1', "Kelle Cruz")

Run check_header#

Use astrodb_utils check_header function to double check everything is as you expect it to be. If all mandatory keywords are set, this function will return True.

[8]:
check_header(header)
The following keywords are not set in the header:
TELAPSE : [s] Total elapsed time (s)
APERTURE : [arcsec] slit width
coordinates converted to sexagesimal: 01h36m56.56054198s +09d33m47.31196597s
SIMBAD results for object name SIMP J013656.5+093347.3:         main_id                 ra        ...        matched_id
                               deg        ...
----------------------- ----------------- ... -----------------------
SIMP J013656.5+093347.3 24.23566892492917 ... SIMP J013656.5+093347.3
coordinates converted to sexagesimal: 01h36m56.56054198s +09d33m47.31196597s
Object name SIMP J013656.5+093347.3 found in SIMBAD
        main_id                 ra        ...        matched_id
                               deg        ...
----------------------- ----------------- ... -----------------------
SIMP J013656.5+093347.3 24.23566892492917 ... SIMP J013656.5+093347.3
make sure SIMBAD coords match header coords
DATE-OBS set to : 2021-01-01.
Date of observation: Jan 01, 2021
/var/folders/b3/lj1wwttj1wn62jczjjrcp8h80000gn/T/ipykernel_29950/2868379340.py:1: DeprecationWarning: Parsing dates involving a day of month without a year specified is ambiguious
and fails to parse leap day. The default behavior will change in Python 3.15
to either always raise an exception or to use a different default year (TBD).
To avoid trouble, add a specific year to the input & format.
See https://github.com/python/cpython/issues/70647.
  check_header(header)
[8]:
True