Skip to content

Glows Decom work#175

Merged
maxinelasp merged 29 commits into
IMAP-Science-Operations-Center:devfrom
maxinelasp:glows_l1
Oct 2, 2023
Merged

Glows Decom work#175
maxinelasp merged 29 commits into
IMAP-Science-Operations-Center:devfrom
maxinelasp:glows_l1

Conversation

@maxinelasp

Copy link
Copy Markdown
Contributor

Change Summary

Adding files for GLOWS L0 decom work, issue #104 and part of #105.

Overview

I created a data class for both direct event and histogram data in glows. This code will decommute the test packet provided by the GLOWS team, and return two lists of the two main science packets needed for processing.

This review includes both the separate XML files for both direct events and histograms, and a combined XML file for both of them. The final code uses only the combined file. These files were almost entirely generated from the xtce_generator. I am planning on going back and adding both the binary data type, which I manually changed for the histogram data field, and code for creating multi-APID xml files.

New Files

  • glows/l0/glows_decom
    • Provides a method for decoding GLOWS packets into histogram and direct events data
  • glows/l0/glows_l0_data
    • Provides data classes for GLOWS level 0 data, including methods for modifying that data (I expect to need some additional methods for processing before outputting to CDF files, so although they're overkill now, this is future-planning)
  • packet definitions
    • 3 packet defintions files: one each for histogram and direct events data, and one which combines them
  • test_glows_decom
    • tests for glows_decom.py
  • xtce_generation/xtce_generator_glows
    • code for automatically generating the separate histogram and direct events XTCE files

Updated Files

  • xtce_generator.py
    • Updating to fix a typo in the unsigned integer generation

Testing

I added some general decom unit tests. This uses a smaller piece of the whole packet file, since the entire thing is very large.

In addition, I was able to confirm the histogram data matches the expected value from the packets using a plot provided to me by the GLOWS team:

Expected result:

RMlj7rjNti7BZpxm

My results:
histogram_test_data_plot

@maxinelasp maxinelasp added the Ins: GLOWS Related to the GLOWS instrument label Sep 27, 2023
@maxinelasp maxinelasp requested review from a team September 27, 2023 21:24
@maxinelasp maxinelasp self-assigned this Sep 27, 2023
@maxinelasp maxinelasp requested review from GFMoraga, bourque, bryan-harter, greglucas, laspsandoval, sdhoyt and tech3371 and removed request for a team September 27, 2023 21:24
Comment thread imap_processing/glows/l0/glows_decom.py Outdated
de_l0 = GlowsDeL0(packet)
dedata.append(de_l0)

except ReadError as e:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is similar to the safe_packet_generator that Laura created in her Ultra L0 PR: #128. It does occur with the smaller packet that I created, because I ended up cutting it off in the middle of the last packet.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does occur with the smaller packet that I created, because I ended up cutting it off in the middle of the last packet

I think this was covering up a true error in Ultra, so I'd be hesitant to put this in here. Are you able to trim the packet to the exact binary length you want? Maybe also put the command you did to trim the file in a comment at the top of this file or somewhere so that we can remember how to recreate the data here if we get a new test data dump.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'll take a look at the packet and see if I can actually get a properly trimmed packet. I was using the code that the GLOWS team provided, which does byte by byte reading, but apparently failed to actually chop it at the right place. I'll try again, just wanted to get this PR up mostly

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with Greg. Glad he caught that for Ultra.

<xtce:Parameter name="HISTOGRAM_DATA" parameterTypeRef="BYTE28800">
<xtce:LongDescription>Histogram Counts</xtce:LongDescription>
</xtce:Parameter>
<!--Direct events sequence containers-->

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a name collision in the histogram and direct events packets - they both have a parameter named SEC. I could update the name (DE_SEC) but opted to just use the same SEC definition (line 81) because they were the same size and represent the same thing. This may come up again with other combined XTCE files.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you highlight a great observation here. Something to keep in mind for sure. The xtce looks great!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ok to use same parameter if they do same thing and same size but referred in different packets. I have similar thing in SWE.

<xtce:Parameter name="HISTOGRAM_DATA" parameterTypeRef="BYTE28800">
<xtce:LongDescription>Histogram Counts</xtce:LongDescription>
</xtce:Parameter>
<!--Direct events sequence containers-->

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ok to use same parameter if they do same thing and same size but referred in different packets. I have similar thing in SWE.

Comment thread tools/xtce_generation/telemetry_generator.py Outdated
Comment thread tools/xtce_generation/telemetry_generator.py Outdated
Comment thread tools/xtce_generation/telemetry_generator.py Outdated
Comment thread tools/xtce_generation/telemetry_generator.py Outdated
Comment thread imap_processing/glows/l0/glows_decom.py Outdated
Comment on lines +31 to +42
hist_packet_definition = xtcedef.XtcePacketDefinition(xtce_document)
histparser = parser.PacketParser(hist_packet_definition)

histdata = []
dedata = []

with open(packet_file_path, "rb") as binary_data:
try:
hist_packets = histparser.generator(
binary_data,
buffer_read_size_bytes=5790778,
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do lot of these in imap_processing/decom.py. We could update that function to take optional parameter buffer_read_size_bytes in case other needs to use it in the future.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't use that method because I wanted to use the generator directly and produce the data types as needed, rather than reading and converting the entire packet, converting that to a list, then looping through the list again to process it for glows.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see

def __init__(self, packet):
self.data = self.process_packet(packet)

def process_packet(self, packet) -> dict:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for this function? It seems to be doing duplicate work that may not be needed. What I mean is packet already has keys and value of HIST packet. You can reference it like this
Eg.

for key, value in packet.data.items():
        if key != "HISTOGRAM_DATA":
           # get key and value here
           print(key, value.derived_value)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great point. My one concern is, the expected list of keys does act as an additional check on the data - if one of the expected keys is missing, then the program raises an error. Is that something we would ever expect to happen? Or is that already checked by space_packet_parser?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went ahead and made the change, but I'm still curious about the answer to this question.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I see your point. I think that's where validation data comes to help. The way you validated with the plot. If the packet definition didn't have a key, validation data wouldn't match. But if we track that in here too, then it would add more things to track. That's my thought :)

raise ValueError(f"Missing data for {key} in {packet.data}")
return data

def _convert_histogram_data(self, binary_hist_data: str) -> list[int]:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we replace this function with this line? I did this in SWE. SWE also needed to get data in 8-bit chunks too.

binary_hist_data = packet.data["HISTOGRAM_DATA"].raw_value
byte_data = int(binary_hist_data, 2).to_bytes(3599, byteorder="big")
# convert bytes to numpy array of uint8
raw_counts = np.frombuffer(byte_data, dtype=np.uint8)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that doesn't work. I get an overflow error from trying to convert the binary_hist_data to an int.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bummer. ok

Data dictionary created from the packet, using L0_KEYS
"""
data = {}
for key in self.L0_KEYS:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above

@greglucas greglucas left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving modulo fixing the false error.

Comment thread imap_processing/glows/l0/decom_glows.py
)
parameter_type.attrib["name"] = parameter_type_ref_name
parameter_type.attrib["signed"] = "false"
parameter_type.attrib["signed"] = false

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
parameter_type.attrib["signed"] = false
parameter_type.attrib["signed"] = False

Is false defined above to be a string? If so it is confusing. Or do you want this to be the boolean and need the caps?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be a string, I'll update that.

@maxinelasp maxinelasp force-pushed the glows_l1 branch 4 times, most recently from 1646c51 to 6d7295f Compare September 29, 2023 16:26

@sdhoyt sdhoyt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me!

<xtce:BinaryParameterType name="BYTE28800">
<xtce:BinaryDataEncoding bitOrder="mostSignificantBitFirst">
<xtce:SizeInBits>
<xtce:FixedValue>28800</xtce:FixedValue>

@laspsandoval laspsandoval Oct 2, 2023

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if this might change and not be a FixedValue.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good, point, as far as I am aware, it is a fixed value. I have gotten some test data and this successfully decoms it. It's a standard size for the number of histogram buckets, so I think it will stay the same, but I will double-check when I meet with them next.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With Ultra there were some nuances that I didn't figure out until I was given the Flight Software Specifications Table. Even after that we had to walk through their L0 decom code in C for a couple of hours and only then it became clear what was going on. My suggested questions for them:

  1. Should the decom data be exactly the same as the simulator data (I do think that it should be)
  2. Should this be a variable length packet?
  3. Do they take only the first x values from the returned array? Ultra had hard-coded [0:48] into their C code to select only the first 48 values, which I never would have known about if I hadn't been walked through what they were doing.

I might just be paranoid from my own experience so maybe its all exactly as it should be :) I just recommend that you double check.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely will! Thank you for those suggested questions, that's helpful for understanding your concerns and what I should look into.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this is a good thing to discuss with them, but I would tend to agree that this is almost certainly a fixed value. I think this comes down to differences between whether instruments detect individual events, or if they are integrating over some bins. So, I think the event-based instruments will accumulate individual events and therefore we don't know how long those packets will be until we get them. But, for the other instruments, like GLOWS, they will be integrating over some specific bin and just adding up those counts within those bins, so the number of bins is fixed and they just report the number of events/bin, not individual events. That is my guess at the reason for the fixed vs variable-length differences between instruments.

SWVER : int
Version of SW used for generation
SEC : int
Block start time (IMAP), seconds<

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor typo <

@laspsandoval

Copy link
Copy Markdown
Contributor

Maybe it is my imagination, but the Expected Result and your result look ever so slightly different. I do wonder if the Fixed value parameter in the xtce may have something to do with it. Just for the sake of not missing anything have you considered adding more unit tests?

All the code looks good.

@maxinelasp

Copy link
Copy Markdown
Contributor Author

Maybe it is my imagination, but the Expected Result and your result look ever so slightly different. I do wonder if the Fixed value parameter in the xtce may have something to do with it. Just for the sake of not missing anything have you considered adding more unit tests?

Like I mentioned as a reply to one of the other comments, the GLOWS team and I have discussed having some statistical tests for this data, so that's my ultimate plan to test the data. I think the graph provided to me by the team is the data that went into the simulator, so I would expect some minor differences. I'll definitely talk about testing this process with them when I meet with them later this month.

Since the histogram value (with the FixedValue) should always have the same number of buckets, I think a FixedValue is the right way to go there. Unless part of the packet is missing, there should always be the same bucket count. I will confirm this with GLOWS.

@maxinelasp

Copy link
Copy Markdown
Contributor Author

I'm merging this with the code coverage check failing, because the uncovered lines do not need testing (pretty print methods in the two data classes)

@maxinelasp maxinelasp merged commit 6af8925 into IMAP-Science-Operations-Center:dev Oct 2, 2023
laspsandoval pushed a commit to laspsandoval/imap_processing that referenced this pull request Nov 15, 2023
* update telemetry_generator to be more generic

* first pass at glows xtce

* updated generator to use correct IntegerParameterType

* First pass at GLOWS decom

* Updating glows decom to properly process data into dictionary

* Updating decom to include direct events

* Finalizing code, adding smaller test data file and combined XML, updating documentation and tests

* Updating data classes to use attributes, and changing names

* Removing keys from data class and getting attribute names from the packet

* Move INT to SINT in telemetry_generator

Co-authored-by: Tenzin Choedon <36522642+tech3371@users.noreply.github.com>

* Addressing code review comments

---------

Co-authored-by: Tenzin Choedon <tenzin.choedon@lasp.colorado.edu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Ins: GLOWS Related to the GLOWS instrument

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants