Skip to content

KeyError: 'nsamples' #53

@gsleap

Description

@gsleap

When reading a filterbank file which already has nsamples populated, I get a KeyError exception as it appears that sigpyproc3 wants to calculate this value?
see:

header["nsamples"] = (
8 * int(header["datalen"]) // int(header["nbits"]) // int(header["nchans"])
)

nsamples isn't in the list of valid keyword:

header_keys = {
"signed": "b",
"telescope_id": "I",
"ibeam": "I",
"nbeams": "I",
"refdm": "d",
"nifs": "I",
"nchans": "I",
"foff": "d",
"fch1": "d",
"nbits": "I",
"tsamp": "d",
"tstart": "d",
"src_dej": "d",
"src_raj": "d",
"za_start": "d",
"az_start": "d",
"source_name": "str",
"rawdatafile": "str",
"data_type": "I",
"machine_id": "I",
"barycentric": "I",
"pulsarcentric": "I",
}
"""Header keys recognised by the sigproc package."""

I think that if nsamples is provided by the filterbank file (as it is a valid keyword), it should either be ignored (maybe with a warning?) and the calculated value used, or should be read/used instead of the calculated value.

Example of error message:

$ spp_header print my_test_file.fil
Traceback (most recent call last):
  File "/home/gsleap/work/github/sigpyproc3/.venv/bin/spp_header", line 10, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/gsleap/work/github/sigpyproc3/.venv/lib/python3.12/site-packages/rich_click/rich_command.py", line 402, in __call__
    return super().__call__(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/gsleap/work/github/sigpyproc3/.venv/lib/python3.12/site-packages/click/core.py", line 1485, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/gsleap/work/github/sigpyproc3/.venv/lib/python3.12/site-packages/rich_click/rich_command.py", line 216, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/home/gsleap/work/github/sigpyproc3/.venv/lib/python3.12/site-packages/click/core.py", line 1873, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/gsleap/work/github/sigpyproc3/.venv/lib/python3.12/site-packages/click/core.py", line 1269, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/gsleap/work/github/sigpyproc3/.venv/lib/python3.12/site-packages/click/core.py", line 824, in invoke
    return callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/gsleap/work/github/sigpyproc3/sigpyproc/apps/spp_header.py", line 54, in print
    header = hdr_reader(filename)
             ^^^^^^^^^^^^^^^^^^^^
  File "/home/gsleap/work/github/sigpyproc3/sigpyproc/header.py", line 710, in from_sigproc
    header = sigproc.parse_header_multi(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/gsleap/work/github/sigpyproc3/sigpyproc/io/sigproc.py", line 225, in parse_header_multi
    header = parse_header(filenames[0])
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/gsleap/work/github/sigpyproc3/sigpyproc/io/sigproc.py", line 283, in parse_header
    key_fmt = header_keys[key]
              ~~~~~~~~~~~^^^^^
KeyError: 'nsamples'

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions