Skip to content

Commit

Permalink
Replace use of CdfAttributeManager with ImapCdfAttributes in Hi files (
Browse files Browse the repository at this point in the history
  • Loading branch information
subagonsouth authored Jul 23, 2024
1 parent 1e5692b commit bf31f33
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 37 deletions.
15 changes: 7 additions & 8 deletions imap_processing/hi/l1a/science_direct_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import numpy as np
import xarray as xr

from imap_processing import imap_module_directory
from imap_processing.cdf.cdf_attribute_manager import CdfAttributeManager
from imap_processing.cdf.imap_cdf_manager import ImapCdfAttributes
from imap_processing.cdf.utils import met_to_j2000ns

# TODO: read LOOKED_UP_DURATION_OF_TICK from
Expand Down Expand Up @@ -261,27 +260,27 @@ def create_dataset(de_data_list: list, packet_met_time: list) -> xr.Dataset:
data_dict["ccsds_met"].append(packet_met_time[index])

# Load the CDF attributes
cdf_manager = CdfAttributeManager(imap_module_directory / "cdf" / "config")
cdf_manager.load_global_attributes("imap_hi_global_cdf_attrs.yaml")
cdf_manager.load_variable_attributes("imap_hi_variable_attrs.yaml")
attr_mgr = ImapCdfAttributes()
attr_mgr.add_instrument_global_attrs("hi")
attr_mgr.load_variable_attributes("imap_hi_variable_attrs.yaml")
# uncomment this once Maxine's PR is merged
# attr_mgr.add_global_attribute("Data_version", data_version)

epoch_time = xr.DataArray(
data_dict.pop("epoch"),
name="epoch",
dims=["epoch"],
attrs=cdf_manager.get_variable_attributes("hi_de_epoch"),
attrs=attr_mgr.get_variable_attributes("hi_de_epoch"),
)

de_global_attrs = cdf_manager.get_global_attributes("imap_hi_l1a_de_attrs")
de_global_attrs = attr_mgr.get_global_attributes("imap_hi_l1a_de_attrs")
dataset = xr.Dataset(
coords={"epoch": epoch_time},
attrs=de_global_attrs,
)

for var_name, data in data_dict.items():
attrs = cdf_manager.get_variable_attributes(
attrs = attr_mgr.get_variable_attributes(
f"hi_de_{var_name}", check_schema=False
).copy()
dtype = attrs.pop("dtype")
Expand Down
16 changes: 7 additions & 9 deletions imap_processing/hi/l1b/hi_l1b.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import xarray as xr

from imap_processing import imap_module_directory
from imap_processing.cdf.cdf_attribute_manager import CdfAttributeManager
from imap_processing.cdf.imap_cdf_manager import ImapCdfAttributes
from imap_processing.hi.utils import HIAPID
from imap_processing.utils import convert_raw_to_eu

logger = logging.getLogger(__name__)
CDF_MANAGER = CdfAttributeManager(imap_module_directory / "cdf" / "config")
CDF_MANAGER.load_global_attributes("imap_hi_global_cdf_attrs.yaml")
CDF_MANAGER.load_variable_attributes("imap_hi_variable_attrs.yaml")
ATTR_MGR = ImapCdfAttributes()
ATTR_MGR.add_instrument_global_attrs("hi")
ATTR_MGR.load_variable_attributes("imap_hi_variable_attrs.yaml")


def hi_l1b(l1a_dataset: xr.Dataset, data_version: str) -> xr.Dataset:
Expand Down Expand Up @@ -56,9 +56,7 @@ def hi_l1b(l1a_dataset: xr.Dataset, data_version: str) -> xr.Dataset:
converters={"mnemonic": str.lower},
)

l1b_dataset.attrs.update(
CDF_MANAGER.get_global_attributes("imap_hi_l1b_hk_attrs")
)
l1b_dataset.attrs.update(ATTR_MGR.get_global_attributes("imap_hi_l1b_hk_attrs"))
elif logical_source_parts[-1].endswith("de"):
l1b_dataset = annotate_direct_events(l1a_dataset)
else:
Expand Down Expand Up @@ -108,7 +106,7 @@ def annotate_direct_events(l1a_dataset: xr.Dataset) -> xr.Dataset:
"quality_flag",
"nominal_bin",
]:
attrs = CDF_MANAGER.get_variable_attributes(
attrs = ATTR_MGR.get_variable_attributes(
f"hi_de_{var}", check_schema=False
).copy()
dtype = attrs.pop("dtype")
Expand All @@ -124,6 +122,6 @@ def annotate_direct_events(l1a_dataset: xr.Dataset) -> xr.Dataset:
["tof_1", "tof_2", "tof_3", "de_tag", "ccsds_met", "meta_event_met"]
)

de_global_attrs = CDF_MANAGER.get_global_attributes("imap_hi_l1b_de_attrs")
de_global_attrs = ATTR_MGR.get_global_attributes("imap_hi_l1b_de_attrs")
l1b_dataset.attrs.update(**de_global_attrs)
return l1b_dataset
33 changes: 13 additions & 20 deletions imap_processing/hi/l1c/hi_l1c.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import numpy as np
import xarray as xr

from imap_processing import imap_module_directory
from imap_processing.cdf.cdf_attribute_manager import CdfAttributeManager
from imap_processing.cdf.imap_cdf_manager import ImapCdfAttributes

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -93,24 +92,22 @@ def allocate_pset_dataset(n_esa_steps: int, sensor_str: str) -> xr.Dataset:
dataset : xarray.Dataset
Empty xarray.Dataset ready to be filled with data.
"""
cdf_manager = CdfAttributeManager(imap_module_directory / "cdf" / "config")
cdf_manager.load_global_attributes("imap_hi_global_cdf_attrs.yaml")
cdf_manager.load_variable_attributes("imap_hi_variable_attrs.yaml")
attr_mgr = ImapCdfAttributes()
attr_mgr.add_instrument_global_attrs("hi")
attr_mgr.load_variable_attributes("imap_hi_variable_attrs.yaml")

# preallocate coordinates xr.DataArrays
coords = dict()
# epoch coordinate has only 1 entry for pointing set
attrs = cdf_manager.get_variable_attributes(
"hi_pset_epoch", check_schema=False
).copy()
attrs = attr_mgr.get_variable_attributes("hi_pset_epoch", check_schema=False).copy()
dtype = attrs.pop("dtype")
coords["epoch"] = xr.DataArray(
np.empty(1, dtype=dtype),
name="epoch",
dims=["epoch"],
attrs=attrs,
)
attrs = cdf_manager.get_variable_attributes(
attrs = attr_mgr.get_variable_attributes(
"hi_pset_esa_step", check_schema=False
).copy()
dtype = attrs.pop("dtype")
Expand All @@ -121,7 +118,7 @@ def allocate_pset_dataset(n_esa_steps: int, sensor_str: str) -> xr.Dataset:
attrs=attrs,
)
# spin angle bins are 0.1 degree bins for full 360 degree spin
attrs = cdf_manager.get_variable_attributes(
attrs = attr_mgr.get_variable_attributes(
"hi_pset_spin_angle_bin", check_schema=False
).copy()
dtype = attrs.pop("dtype")
Expand All @@ -148,9 +145,7 @@ def allocate_pset_dataset(n_esa_steps: int, sensor_str: str) -> xr.Dataset:
]:
data_vars[var_name] = full_dataarray(
var_name,
cdf_manager.get_variable_attributes(
f"hi_pset_{var_name}", check_schema=False
),
attr_mgr.get_variable_attributes(f"hi_pset_{var_name}", check_schema=False),
coords,
shape=var_shapes.get(var_name, None),
)
Expand All @@ -160,30 +155,28 @@ def allocate_pset_dataset(n_esa_steps: int, sensor_str: str) -> xr.Dataset:
coords["esa_step"].values.astype(str),
name="esa_step_label",
dims=["esa_step"],
attrs=cdf_manager.get_variable_attributes(
attrs=attr_mgr.get_variable_attributes(
"hi_pset_esa_step_label", check_schema=False
),
)
data_vars["spin_bin_label"] = xr.DataArray(
coords["spin_angle_bin"].values.astype(str),
name="spin_bin_label",
dims=["spin_angle_bin"],
attrs=cdf_manager.get_variable_attributes(
attrs=attr_mgr.get_variable_attributes(
"hi_pset_spin_bin_label", check_schema=False
),
)
data_vars["label_vector_HAE"] = xr.DataArray(
np.array(["x HAE", "y HAE", "z HAE"], dtype=str),
name="label_vector_HAE",
dims=[" "],
attrs=cdf_manager.get_variable_attributes(
attrs=attr_mgr.get_variable_attributes(
"hi_pset_label_vector_HAE", check_schema=False
),
)

pset_global_attrs = cdf_manager.get_global_attributes(
"imap_hi_l1c_pset_attrs"
).copy()
pset_global_attrs = attr_mgr.get_global_attributes("imap_hi_l1c_pset_attrs").copy()
pset_global_attrs["Logical_source"] = pset_global_attrs["Logical_source"].format(
sensor=sensor_str
)
Expand All @@ -198,7 +191,7 @@ def full_dataarray(
Generate an empty xarray.DataArray with appropriate attributes.
Data in DataArray are filled with FILLVAL defined in attributes
retrieved from CDF_MANAGER with shape matching coordinates defined by
retrieved from ATTR_MGR with shape matching coordinates defined by
dims or overridden by optional `shape` input.
Parameters
Expand Down

0 comments on commit bf31f33

Please sign in to comment.