|
| 1 | +"""Data structures for MAG L2 and L1D processing.""" |
| 2 | + |
| 3 | +from dataclasses import dataclass, field |
| 4 | +from enum import Enum |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +import xarray as xr |
| 8 | + |
| 9 | +from imap_processing.cdf.imap_cdf_manager import ImapCdfAttributes |
| 10 | +from imap_processing.mag.constants import DataMode |
| 11 | + |
| 12 | + |
| 13 | +class ValidFrames(Enum): |
| 14 | + """SPICE reference frames for output.""" |
| 15 | + |
| 16 | + dsrf = "dsrf" |
| 17 | + srf = "srf" |
| 18 | + rtn = "rtn" |
| 19 | + gse = "gse" |
| 20 | + |
| 21 | + |
| 22 | +@dataclass |
| 23 | +class MagL2: |
| 24 | + """ |
| 25 | + Dataclass for MAG L2 data. |
| 26 | +
|
| 27 | + Since L2 and L1D should have the same structure, this can be used for either level. |
| 28 | +
|
| 29 | + Attributes |
| 30 | + ---------- |
| 31 | + vectors: np.ndarray |
| 32 | + Magnetic field vectors of size (n, 3) where n is the number of vectors. |
| 33 | + Describes (x, y, z) components of the magnetic field. |
| 34 | + epoch: np.ndarray |
| 35 | + Time of each vector in J2000 seconds. Should be of length n. |
| 36 | + range: np.ndarray |
| 37 | + Range of each vector. Should be of length n. |
| 38 | + global_attributes: dict |
| 39 | + Any global attributes we want to carry forward into the output CDF file. |
| 40 | + quality_flags: np.ndarray |
| 41 | + Quality flags for each vector. Should be of length n. |
| 42 | + quality_bitmask: np.ndarray |
| 43 | + Quality bitmask for each vector. Should be of length n. Copied from offset |
| 44 | + file in L2, marked as good always in L1D. |
| 45 | + magnitude: np.ndarray |
| 46 | + Magnitude of each vector. Should be of length n. Calculated from L2 vectors. |
| 47 | + is_l1d: bool |
| 48 | + Flag to indicate if the data is L1D. Defaults to False. |
| 49 | + """ |
| 50 | + |
| 51 | + vectors: np.ndarray |
| 52 | + epoch: np.ndarray |
| 53 | + range: np.ndarray |
| 54 | + global_attributes: dict |
| 55 | + quality_flags: np.ndarray |
| 56 | + quality_bitmask: np.ndarray |
| 57 | + data_mode: DataMode |
| 58 | + magnitude: np.ndarray = field(init=False) |
| 59 | + is_l1d: bool = False |
| 60 | + |
| 61 | + def __post_init__(self) -> None: |
| 62 | + """Calculate the magnitude of the vectors after initialization.""" |
| 63 | + self.magnitude = self.calculate_magnitude(self.vectors) |
| 64 | + |
| 65 | + @staticmethod |
| 66 | + def calculate_magnitude( |
| 67 | + vectors: np.ndarray, |
| 68 | + ) -> np.ndarray: |
| 69 | + """ |
| 70 | + Given a list of vectors (x, y, z), calculate the magnitude of each vector. |
| 71 | +
|
| 72 | + For an input list of vectors of size (n, 3) returns a list of magnitudes of |
| 73 | + size (n,). |
| 74 | +
|
| 75 | + Parameters |
| 76 | + ---------- |
| 77 | + vectors : np.ndarray |
| 78 | + Array of vectors to calculate the magnitude of. |
| 79 | +
|
| 80 | + Returns |
| 81 | + ------- |
| 82 | + np.ndarray |
| 83 | + Array of magnitudes of the input vectors. |
| 84 | + """ |
| 85 | + return np.zeros(vectors.shape[0]) # type: ignore |
| 86 | + |
| 87 | + def truncate_to_24h(self, timestamp: str) -> None: |
| 88 | + """ |
| 89 | + Truncate all data to a 24 hour period. |
| 90 | +
|
| 91 | + 24 hours is given by timestamp in the format YYYYmmdd. |
| 92 | +
|
| 93 | + Parameters |
| 94 | + ---------- |
| 95 | + timestamp : str |
| 96 | + Timestamp in the format YYYYMMDD. |
| 97 | + """ |
| 98 | + pass |
| 99 | + |
| 100 | + def generate_dataset( |
| 101 | + self, |
| 102 | + attribute_manager: ImapCdfAttributes, |
| 103 | + frame: ValidFrames = ValidFrames.dsrf, |
| 104 | + ) -> xr.Dataset: |
| 105 | + """ |
| 106 | + Generate an xarray dataset from the dataclass. |
| 107 | +
|
| 108 | + This method can be used for L2 and L1D, since they have extremely similar |
| 109 | + output. |
| 110 | +
|
| 111 | + Parameters |
| 112 | + ---------- |
| 113 | + attribute_manager : ImapCdfAttributes |
| 114 | + CDF attributes object for the correct level. |
| 115 | + frame : ValidFrames |
| 116 | + SPICE reference frame to rotate the data into. |
| 117 | +
|
| 118 | + Returns |
| 119 | + ------- |
| 120 | + xr.Dataset |
| 121 | + Complete dataset ready to write to CDF file. |
| 122 | + """ |
| 123 | + logical_source_id = f"imap_mag_l2_{self.data_mode.value.lower()}-{frame.name}" |
| 124 | + direction = xr.DataArray( |
| 125 | + np.arange(3), |
| 126 | + name="direction", |
| 127 | + dims=["direction"], |
| 128 | + attrs=attribute_manager.get_variable_attributes( |
| 129 | + "direction_attrs", check_schema=False |
| 130 | + ), |
| 131 | + ) |
| 132 | + |
| 133 | + direction_label = xr.DataArray( |
| 134 | + direction.values.astype(str), |
| 135 | + name="direction_label", |
| 136 | + dims=["direction_label"], |
| 137 | + attrs=attribute_manager.get_variable_attributes( |
| 138 | + "direction_label", check_schema=False |
| 139 | + ), |
| 140 | + ) |
| 141 | + |
| 142 | + epoch_time = xr.DataArray( |
| 143 | + self.epoch, |
| 144 | + name="epoch", |
| 145 | + dims=["epoch"], |
| 146 | + attrs=attribute_manager.get_variable_attributes("epoch"), |
| 147 | + ) |
| 148 | + |
| 149 | + vectors = xr.DataArray( |
| 150 | + self.vectors, |
| 151 | + name="vectors", |
| 152 | + dims=["epoch", "direction"], |
| 153 | + attrs=attribute_manager.get_variable_attributes("vector_attrs"), |
| 154 | + ) |
| 155 | + |
| 156 | + quality_flags = xr.DataArray( |
| 157 | + self.quality_flags, |
| 158 | + name="quality_flags", |
| 159 | + dims=["epoch"], |
| 160 | + attrs=attribute_manager.get_variable_attributes("compression"), |
| 161 | + ) |
| 162 | + |
| 163 | + quality_bitmask = xr.DataArray( |
| 164 | + self.quality_flags, |
| 165 | + name="quality_flags", |
| 166 | + dims=["epoch"], |
| 167 | + attrs=attribute_manager.get_variable_attributes("compression"), |
| 168 | + ) |
| 169 | + |
| 170 | + rng = xr.DataArray( |
| 171 | + self.range, |
| 172 | + name="range", |
| 173 | + dims=["epoch"], |
| 174 | + # TODO temp attrs |
| 175 | + attrs=attribute_manager.get_variable_attributes("compression_width"), |
| 176 | + ) |
| 177 | + |
| 178 | + magnitude = xr.DataArray( |
| 179 | + self.magnitude, |
| 180 | + name="magnitude", |
| 181 | + dims=["epoch"], |
| 182 | + attrs=attribute_manager.get_variable_attributes("compression_width"), |
| 183 | + ) |
| 184 | + |
| 185 | + global_attributes = ( |
| 186 | + attribute_manager.get_global_attributes(logical_source_id) |
| 187 | + | self.global_attributes |
| 188 | + ) |
| 189 | + |
| 190 | + output = xr.Dataset( |
| 191 | + coords={ |
| 192 | + "epoch": epoch_time, |
| 193 | + "direction": direction, |
| 194 | + "direction_label": direction_label, |
| 195 | + }, |
| 196 | + attrs=global_attributes, |
| 197 | + ) |
| 198 | + |
| 199 | + output["vectors"] = vectors |
| 200 | + output["quality_flags"] = quality_flags |
| 201 | + output["quality_bitmask"] = quality_bitmask |
| 202 | + output["range"] = rng |
| 203 | + output["magnitude"] = magnitude |
| 204 | + |
| 205 | + return output |
0 commit comments