1#!/usr/bin/env python3
2
3"""Utilities for reading DDS files main attributes."""
4
5import os
6import re
7import logging
8from datetime import datetime
9
10from chart.common.args import ArgumentParser
11from chart.common.exceptions import BadFileError
12from chart.project import SID
13
14logger = logging.getLogger()
15
16class DDSFilename:
17 """Decode a DDS standard filename into SID, sensing start.
18
19 >> d = DDSFilename('NSS.MHSX.NN.D09267.S1920.E2105.B2240001.GC')
20 >> print('Sensing start: {start}, sid: {sid}'.format(start=d.sensing_start, sid=d.sid))
21 Sensing start: 2009-09-24 19:05:00, sid: N18
22 """
23
24 class NotDDSFilename(BadFileError):
25 """Invalid DDS filename."""
26
27 def __init__(self, filename):
28 super(DDSFilename.NotDDSFilename, self).__init__(None, filename)
29
30 def __str__(self):
31 return '{path} is not an DDS filename'.format(path=self.filename)
32
33 matcher = re.compile(
34 r'NSS\.MHSX\.(?P<satid>[A-Z]{2})\.D(?P<year>\d{2})(?P<day_of_year>\d{3})'
35 r'\.S(?P<start_hour>\d{2})(?P<start_minute>\d{2})\.E(?P<stop_hour>\d{2})'
36 r'(?P<stop_minute>\d{2})\.B(?P<start_orbit>\d{5})(?P<stop_orbit>\d{2})\.'
37 r'\w{2}'
38 )
39
40 scids = {'NN': 'N18',
41 'NP': 'N19'}
42
43 def __init__(self, filename):
44 match = DDSFilename.matcher.match(str(filename.name))
45 if match is None:
46 raise DDSFilename.NotDDSFilename(filename)
47 year = '20{year}'.format(year=match.group('year'))
48 self.scid = DDSFilename.scids[match.group('satid')]
49 self.sid = SID(self.scid)
50 self.sensing_start = datetime.strptime('{year}{day_of_year}{hour}{minute}'.format(
51 year=year,
52 day_of_year=match.group('day_of_year'),
53 hour=match.group('start_hour'),
54 minute=match.group('stop_minute')),
55 '%Y%j%H%M')
56 self.sensing_stop = datetime.strptime('{year}{day_of_year}{hour}{minute}'.format(
57 year=year,
58 day_of_year=match.group('day_of_year'),
59 hour=match.group('stop_hour'),
60 minute=match.group('stop_minute')),
61 '%Y%j%H%M')
62
63
64def main():
65 """Command line entry point."""
66 parser = ArgumentParser()
67 parser.add_argument('--filename', '-f',
68 metavar='NAME',
69 help='Read file NAME')
70 parser.add_argument('--stats',
71 action='store_true')
72 args = parser.parse_args()
73
74 if args.stats:
75 dds = DDSFilename(args.filename)
76 print('Start ', dds.sensing_start)
77 print('Stop ', dds.sensing_stop)
78 parser.exit()
79
80 parser.error('No actions specified')
81
82if __name__ == '__main__':
83 main()