Skip to content

Commit c049ab5

Browse files
authored
Add function for interpreting quality flag (#350)
The `QUALITY` keyword in the AIA headers is a 32-bit integer that denotes the quality of an observation. Flags associated with the observation quality are encoded bit-wise, with a different message associated with each bit flag. See [section 7.7.6 of the SDO user guide](https://www.lmsal.com/sdodocs/doc/dcur/SDOD0060.zip/zip/entry/) for more details. This PR adds a function that takes in the value of the `QUALITY` key and returns a list of messages associated with any quality flags that are set.
1 parent f6e8f74 commit c049ab5

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

aiapy/util/tests/test_util.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,24 @@ def test_sdo_location_raises_error() -> None:
1818
# Confirm that an error is raised for a time without records
1919
with pytest.raises(RuntimeError, match="No data found for this query"):
2020
aiapy.util.sdo_location("2001-01-01")
21+
22+
23+
@pytest.mark.parametrize(
24+
"bits",
25+
[
26+
[], # nominal
27+
[16], # single message
28+
[12, 13, 14, 17, 21], # multiple messages
29+
[4, 5], # empty bits
30+
],
31+
)
32+
def test_check_quality_flag(bits):
33+
quality = 0
34+
for b in bits:
35+
quality = quality | (1 << b)
36+
messages = ["nominal"]
37+
if bits:
38+
from aiapy.util.util import _QUALITY_FLAG_MESSAGES
39+
40+
messages = [_QUALITY_FLAG_MESSAGES.get(b, "(empty)") for b in bits]
41+
assert messages == aiapy.util.check_quality_flag(quality)

aiapy/util/util.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,64 @@
1313
from aiapy.util.decorators import validate_channel
1414
from aiapy.util.net import _get_data_from_jsoc
1515

16-
__all__ = ["sdo_location", "telescope_number"]
16+
__all__ = ["check_quality_flag", "sdo_location", "telescope_number"]
17+
18+
# This comes from Table 2 in Section 7.7.6 of the SDO User Guide
19+
_QUALITY_FLAG_MESSAGES = {
20+
0: "Flatfield data are not available",
21+
1: "Orbit data are not available",
22+
2: "Ancillary science data are not available",
23+
3: "Master pointing data are not available",
24+
4: "Limb-fit data are not available (generally not applicable to AIA images)",
25+
8: "Value of MISSVALS keyword is nonzero",
26+
9: "Value of MISSVALS keyword is more than 1% of the value of the TOTVALS keyword",
27+
10: "Value of MISSVALS keyword is more than 5% of the value of the TOTVALS keyword",
28+
11: "Value of MISSVALS keyword is more than 25% of the value of the TOTVALS keyword",
29+
12: "Spacecraft is not in science pointing mode (coincides with ACS_MODE keyword set to a value other than SCIENCE)",
30+
13: "Spacecraft eclipse flag is set (coincides with ACS_ECLP keyword set to YES)",
31+
14: "Spacecraft sun presence flag is not set (coincides with ACS_SUNP keyword set to NO)",
32+
15: "Spacecraft safe mode flag is set (coincides with ACS_SAFE keyword set to YES)",
33+
16: "Dark image flag is set (coincides with IMG_TYPE keyword set to DARK)",
34+
17: "Image Stabilization System (ISS) loop is open (coincides with AISTATE keyword set to OPEN)",
35+
18: "Calibration image",
36+
20: "Focus is out of range",
37+
21: "Register flag is set",
38+
30: "Quicklook image",
39+
31: "Image is not available",
40+
}
41+
42+
43+
def check_quality_flag(quality):
44+
"""
45+
Interpret the ``QUALITY`` flag in the header of an AIA observation.
46+
47+
AIA images are occasionally affected by operations associated with
48+
calibration maneuvers or are missing data e.g. due to eclipses. For
49+
these operating periods, the ``QUALITY`` keyword in the header,
50+
a 32-bit integer, records the reason for the operation not being
51+
nominal. Various flags for different operating modes are encoded
52+
bitwise such that ``QUALITY`` can indicate multiple non-nominal
53+
operating modes. For more information, see
54+
`section 7.7.6 of the SDO user guide <https://www.lmsal.com/sdodocs/doc/dcur/SDOD0060.zip/zip/entry/>`__.
55+
This function decodes the ``QUALITY`` flag and returns a string
56+
indicating the reason(s) for the flags being set. For a nominal
57+
operation (``QUALITY==0``), "nominal" is returned.
58+
59+
Parameters
60+
----------
61+
quality: `int`
62+
Quality flag encoded as an integer. This is typically found in
63+
the ``QUALITY`` keyword in the FITS header.
64+
65+
Returns
66+
-------
67+
: `list`
68+
Messages associated with quality flags that are set.
69+
"""
70+
flags = [i for i in range(32) if (quality & (1 << i))]
71+
if flags:
72+
return [_QUALITY_FLAG_MESSAGES.get(f, "(empty)") for f in flags]
73+
return ["nominal"]
1774

1875

1976
def sdo_location(time):

changelog/350.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added a function `~aiapy.util.check_quality_flag` to interpret quality flags from ``QUALITY`` keyword.

0 commit comments

Comments
 (0)