NASA Logo
Ocean Color Science Software

ocssw V2022
__main__.py
Go to the documentation of this file.
1 """Main method of the module.
2 
3 This is called with `python -m ccsdspy`
4 """
5 
6 __author__ = "Daniel da Silva <mail@danieldasilva.org>"
7 
8 import argparse
9 import os
10 import sys
11 
12 from .utils import split_by_apid
13 
14 
15 def module_main(argv=sys.argv, cwd=os.getcwd()):
16  """Main method of the module, run with `python -m ccsdspy [..]"""
17  # Parse command line arguments
18  parser = argparse.ArgumentParser()
19 
20  subparser = parser.add_subparsers(dest="command")
21  split_parser = subparser.add_parser(
22  "split",
23  help=(
24  "Subcommand to run. Currently only split is supported. Use "
25  "split to split mixed APID stream and write files to the "
26  "current directory."
27  ),
28  )
29  split_parser.add_argument("file")
30  split_parser.add_argument("--valid-apids", help="Valid APIDs seperated by comma")
31 
32  args = parser.parse_args(argv[1:])
33 
34  # Implemention of split command, which splits a mixed stream and writes
35  # to the current directory.
36  if args.command == "split":
37  if args.valid_apids:
38  toks = args.valid_apids.split(",")
39  valid_apids = [int(apid) for apid in toks]
40  else:
41  valid_apids = None
42 
43  stream_by_apid = split_by_apid(args.file, valid_apids=valid_apids)
44 
45  print("Parsing done!")
46 
47  for apid in sorted(stream_by_apid):
48  if valid_apids and apid not in valid_apids:
49  continue
50 
51  out_file_name = f"{cwd}/apid{apid:05d}.tlm"
52  print(f"Writing {out_file_name}")
53 
54  with open(out_file_name, "wb") as file_out:
55  file_out.write(stream_by_apid[apid].read())
56 
57 
58 if __name__ == "__main__":
59  module_main()
def split_by_apid(mixed_file, valid_apids=None)
Definition: utils.py:143
void print(std::ostream &stream, const char *format)
Definition: PrintDebug.hpp:38
def module_main(argv=sys.argv, cwd=os.getcwd())
Definition: __main__.py:15