本文整理汇总了Python中sunpy.io.fits.extract_waveunit函数的典型用法代码示例。如果您正苦于以下问题:Python extract_waveunit函数的具体用法?Python extract_waveunit怎么用?Python extract_waveunit使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了extract_waveunit函数的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_extract_waveunit_wavelnthcomment_parentheses
def test_extract_waveunit_wavelnthcomment_parentheses():
# WAVELNTH comment is: "Observed wavelength (nm)"
waveunit = extract_waveunit(get_header(SVSM_IMAGE)[0])
assert waveunit == 'nm'
示例2: entries_from_file
def entries_from_file(file, default_waveunit=None):
"""Use the headers of a FITS file to generate an iterator of
:class:`sunpy.database.tables.DatabaseEntry` instances. Gathered
information will be saved in the attribute `fits_header_entries`. If the
key INSTRUME, WAVELNTH or DATE-OBS / DATE_OBS is available, the attribute
`instrument`, `wavemin` and `wavemax` or `observation_time_start` is set,
respectively. If the wavelength unit can be read, the values of `wavemin`
and `wavemax` are converted to nm (nanometres). The value of the `file`
parameter is used to set the attribute `path` of each generated database
entry.
Parameters
----------
file : str or file-like object
Either a path pointing to a FITS file or a an opened file-like object.
If an opened file object, its mode must be one of the following rb,
rb+, or ab+.
default_waveunit : str, optional
The wavelength unit that is used for a header if it cannot be
found.
Raises
------
sunpy.database.WaveunitNotFoundError
If `default_waveunit` is not given and the wavelength unit cannot
be found in one of the FITS headers
sunpy.WaveunitNotConvertibleError
If a wavelength unit could be found but cannot be used to create an
instance of the type ``astropy.units.Unit``. This can be the case
for example if a FITS header has the key `WAVEUNIT` with the value
`nonsense`.
Examples
--------
>>> entries = list(entries_from_file(sunpy.data.sample.SWAP_LEVEL1_IMAGE))
>>> len(entries)
1
>>> entry = entries.pop()
>>> entry.instrument
'SWAP'
>>> entry.observation_time_start, entry.observation_time_end
(datetime.datetime(2012, 1, 1, 0, 16, 7, 836000), None)
>>> entry.wavemin, entry.wavemax
(17.400000000000002, 17.400000000000002)
>>> len(entry.fits_header_entries)
112
"""
headers = fits.get_header(file)
if isinstance(file, (str, unicode)):
filename = file
else:
filename = getattr(file, "name", None)
for header in headers:
entry = DatabaseEntry(path=filename)
for key, value in header.iteritems():
# Yes, it is possible to have an empty key in a FITS file.
# Example: sunpy.data.sample.EIT_195_IMAGE
# Don't ask me why this could be a good idea.
if key == "":
value = str(value)
elif key == "KEYCOMMENTS":
for k, v in value.iteritems():
entry.fits_key_comments.append(FitsKeyComment(k, v))
continue
entry.fits_header_entries.append(FitsHeaderEntry(key, value))
waveunit = fits.extract_waveunit(header)
if waveunit is None:
waveunit = default_waveunit
unit = None
if waveunit is not None:
try:
unit = Unit(waveunit)
except ValueError:
raise WaveunitNotConvertibleError(waveunit)
for header_entry in entry.fits_header_entries:
key, value = header_entry.key, header_entry.value
if key == "INSTRUME":
entry.instrument = value
elif key == "WAVELNTH":
if unit is None:
raise WaveunitNotFoundError(file)
# use the value of `unit` to convert the wavelength to nm
entry.wavemin = entry.wavemax = unit.to(nm, value, equivalencies.spectral())
# NOTE: the key DATE-END or DATE_END is not part of the official
# FITS standard, but many FITS files use it in their header
elif key in ("DATE-END", "DATE_END"):
entry.observation_time_end = parse_time(value)
elif key in ("DATE-OBS", "DATE_OBS"):
entry.observation_time_start = parse_time(value)
yield entry
示例3: test_extract_waveunit_waveunitcomment
def test_extract_waveunit_waveunitcomment():
# comment of WAVEUNIT is: "in meters"
waveunit = extract_waveunit(get_header(NA_IMAGE)[0])
assert waveunit == 'm'
示例4: test_extract_waveunit_wavelnthcomment_brackets
def test_extract_waveunit_wavelnthcomment_brackets():
# WAVELNTH comment is: "[Angstrom] bandpass peak response"
waveunit = extract_waveunit(get_header(SWAP_LEVEL1_IMAGE)[0])
assert waveunit == 'angstrom'
示例5: test_extract_waveunit_minus9
def test_extract_waveunit_minus9():
# value of WAVEUNIT is -9
waveunit = extract_waveunit(get_header(MEDN_IMAGE)[0])
assert waveunit == 'nm'
示例6: test_extract_waveunit_minus10
def test_extract_waveunit_minus10():
# value of WAVEUNIT is -10
waveunit = extract_waveunit(get_header(MQ_IMAGE)[0])
assert waveunit == 'angstrom'
示例7: test_missing_waveunit_in_wavelnth_comment
def test_missing_waveunit_in_wavelnth_comment():
# the comment of the key WAVELNTH has the value
# '171 = Fe IX/X, 195 = Fe XII,' which contains no unit information
waveunit = extract_waveunit(get_header(EIT_195_IMAGE)[0])
assert waveunit is None
示例8: test_extract_waveunit_from_waveunit_key
def test_extract_waveunit_from_waveunit_key():
# the key WAVEUNIT can be accessed and returned directly
waveunit = extract_waveunit(get_header(AIA_171_IMAGE)[0])
assert waveunit == 'angstrom'
示例9: test_extract_waveunit_missing_waveunit_key_and_missing_wavelnth_comment
def test_extract_waveunit_missing_waveunit_key_and_missing_wavelnth_comment():
waveunit = extract_waveunit(get_header(RHESSI_IMAGE)[0])
assert waveunit is None
示例10: test_extract_waveunit_wavelnthcomment_parentheses
def test_extract_waveunit_wavelnthcomment_parentheses():
# WAVELNTH comment is: "Observed wavelength (nm)"
with pytest.warns(AstropyUserWarning, match='File may have been truncated'):
waveunit = extract_waveunit(get_header(SVSM_IMAGE)[0])
assert waveunit == 'nm'
示例11: test_extract_waveunit_waveunitcomment
def test_extract_waveunit_waveunitcomment():
# comment of WAVEUNIT is: "in meters"
with pytest.warns(AstropyUserWarning, match='File may have been truncated'):
waveunit = extract_waveunit(get_header(NA_IMAGE)[0])
assert waveunit == 'm'
示例12: test_extract_waveunit_minus10
def test_extract_waveunit_minus10():
# value of WAVEUNIT is -10
with pytest.warns(AstropyUserWarning, match='File may have been truncated'):
waveunit = extract_waveunit(get_header(MQ_IMAGE)[0])
assert waveunit == 'angstrom'
示例13: entries_from_file
def entries_from_file(file, default_waveunit=None,
time_string_parse_format=''):
# Note: time_string_parse_format='' so that None won't be passed to Time.strptime
# (which would make strptime freak out, if I remember correctly).
"""Use the headers of a FITS file to generate an iterator of
:class:`sunpy.database.tables.DatabaseEntry` instances. Gathered
information will be saved in the attribute `fits_header_entries`. If the
key INSTRUME, WAVELNTH or DATE-OBS / DATE_OBS is available, the attribute
`instrument`, `wavemin` and `wavemax` or `observation_time_start` is set,
respectively. If the wavelength unit can be read, the values of `wavemin`
and `wavemax` are converted to nm (nanometres). The value of the `file`
parameter is used to set the attribute `path` of each generated database
entry.
Parameters
----------
file : str or file-like object
Either a path pointing to a FITS file or a an opened file-like object.
If an opened file object, its mode must be one of the following rb,
rb+, or ab+.
default_waveunit : str, optional
The wavelength unit that is used for a header if it cannot be
found.
time_string_parse_format : str, optional
Fallback timestamp format which will be passed to
`~astropy.time.Time.strptime` if `sunpy.time.parse_time` is unable to
automatically read the `date-obs` metadata.
Raises
------
sunpy.database.WaveunitNotFoundError
If `default_waveunit` is not given and the wavelength unit cannot
be found in one of the FITS headers
sunpy.WaveunitNotConvertibleError
If a wavelength unit could be found but cannot be used to create an
instance of the type ``astropy.units.Unit``. This can be the case
for example if a FITS header has the key `WAVEUNIT` with the value
`nonsense`.
Examples
--------
>>> from sunpy.database.tables import entries_from_file
>>> import sunpy.data.sample # doctest: +REMOTE_DATA
>>> entries = list(entries_from_file(sunpy.data.sample.SWAP_LEVEL1_IMAGE)) # doctest: +REMOTE_DATA
>>> len(entries) # doctest: +REMOTE_DATA
1
>>> entry = entries.pop() # doctest: +REMOTE_DATA
>>> entry.instrument # doctest: +REMOTE_DATA
'SWAP'
>>> entry.observation_time_start, entry.observation_time_end # doctest: +REMOTE_DATA
(datetime.datetime(2011, 6, 7, 6, 33, 29, 759000), None)
>>> entry.wavemin, entry.wavemax # doctest: +REMOTE_DATA
(17.400000000000002, 17.400000000000002)
>>> len(entry.fits_header_entries) # doctest: +REMOTE_DATA
111
"""
headers = fits.get_header(file)
# This just checks for blank default headers
# due to compression.
for header in headers:
if header == DEFAULT_HEADER:
headers.remove(header)
if isinstance(file, str):
filename = file
else:
filename = getattr(file, 'name', None)
for header in headers:
entry = DatabaseEntry(path=filename)
for key, value in header.items():
# Yes, it is possible to have an empty key in a FITS file.
# Example: sunpy.data.sample.EIT_195_IMAGE
# Don't ask me why this could be a good idea.
if key == '':
value = str(value)
elif key == 'KEYCOMMENTS':
for k, v in value.items():
entry.fits_key_comments.append(FitsKeyComment(k, v))
continue
entry.fits_header_entries.append(FitsHeaderEntry(key, value))
waveunit = fits.extract_waveunit(header)
entry.hdu_index = headers.index(header)
if waveunit is None:
waveunit = default_waveunit
unit = None
if waveunit is not None:
try:
unit = Unit(waveunit)
except ValueError:
raise WaveunitNotConvertibleError(waveunit)
for header_entry in entry.fits_header_entries:
key, value = header_entry.key, header_entry.value
if key == 'INSTRUME':
entry.instrument = value
elif key == 'WAVELNTH':
#.........这里部分代码省略.........