parser.add_option('-e', '--end',
action='store', dest='sdt_end', metavar="'YYYYMMDD HHMMSS'",
help='End data processing on that GMT date time.'
- 'Default is now.'
- 'If a date is provided without time, time defaults to 235959.')
+ ' Default is now.'
+ ' If a date is provided without time, time defaults to 235959.')
parser.add_option('-s', '--start',
action='store', dest='sdt_start', metavar="'YYYYMMDD HHMMSS'",
help='Start data processing on that date.'
- 'Using that option enables multiple output of the same boat.'
- 'Disabled by default.'
- 'If a date is provided without time, time default to 000000.'
- 'If other options enable multiple output, default to 1 day before'
+ ' Using that option enables multiple output of the same boat.'
+ ' Disabled by default.'
+ ' If a date is provided without time, time default to 000000.'
+ ' If other options enable multiple output, default to 1 day before'
' --end date/time.')
+ parser.add_option('--duration',
+ action='store', dest='sdt_duration', metavar="DURATION",
+ help='Duration of reference period.'
+ ' Last character may be S for seconds, M(inutes), D(ays), W(eeks)'
+ ' Default is seconds.'
+ ' This is the time length bewteen --start and --end above.'
+ ' If you want multiple output of the same boat, you may use '
+ ' --start, --end or --duration, 2 of them, but not 3 of them.')
parser.add_option('-g', '--granularity',
action='store', type='int', dest='granularity', metavar='SECONDS',
help='Dump only one position every granularity seconds.'
# Dates selections
#
+ if options.sdt_start:
+ # remove non digit characters
+ options.sdt_start = "".join([ c for c in options.sdt_start if c.isdigit()])
+ if len(options.sdt_start)==14:
+ options.sdt_start = datetime.strptime(options.sdt_start, '%Y%m%d%H%M%S')
+ elif len(options.sdt_start)==8:
+ options.sdt_start = datetime.strptime(options.sdt_start, '%Y%m%d')
+ else:
+ print >> sys.stderr, "Invalid format for --start option"
+ sys.exit(1)
+
if options.sdt_end:
# remove non digit characters
options.sdt_end = "".join([ c for c in options.sdt_end if c.isdigit()])
if len(options.sdt_end)==14:
- dt_end = datetime.strptime(options.sdt_end, '%Y%m%d%H%M%S')
+ options.sdt_end = datetime.strptime(options.sdt_end, '%Y%m%d%H%M%S')
elif len(options.sdt_end)==8:
- dt_end = datetime.strptime(options.sdt_end, '%Y%m%d')
- dt_end = datetime.combine(dt_end.date(), time(23,59,59))
+ options.sdt_end = datetime.strptime(options.sdt_end, '%Y%m%d')
+ options.sdt_end = datetime.combine(options.sdt_end.date(), time(23, 59, 59))
else:
print >> sys.stderr, "Invalid format for --end option"
sys.exit(1)
- else:
- dt_end = datetime.utcnow()
- logging.debug('--end is %s', dt_end)
-
- if options.sdt_start or options.granularity is not None or options.max_count or options.format in ('animation', 'track'):
- # time period is enabled
- if options.sdt_start:
- options.sdt_start = "".join([ c for c in options.sdt_start if c.isdigit()])
- if len(options.sdt_start)==14:
- dt_start = datetime.strptime(options.sdt_start, '%Y%m%d%H%M%S')
- elif len(options.sdt_start)==8:
- dt_start = datetime.strptime(options.sdt_start, '%Y%m%d')
- else:
- print >> sys.stderr, "Invalid format for --start option"
- sys.exit(1)
+
+ if options.sdt_duration:
+ # remove spaces
+ options.sdt_duration = options.sdt_duration.replace(' ', '')
+ # use upercase
+ options.sdt_duration = options.sdt_duration.upper()
+ if options.sdt_duration[-1] == 'S':
+ options.sdt_duration = options.sdt_duration[:-1]
+ duration_unit = 1
+ elif options.sdt_duration[-1] == 'M':
+ options.sdt_duration = options.sdt_duration[:-1]
+ duration_unit = 60
+ elif options.sdt_duration[-1] == 'H':
+ options.sdt_duration = options.sdt_duration[:-1]
+ duration_unit = 60*60
+ elif options.sdt_duration[-1] == 'D':
+ options.sdt_duration = options.sdt_duration[:-1]
+ duration_unit = 24*60*60
+ elif options.sdt_duration[-1] == 'W':
+ options.sdt_duration = options.sdt_duration[:-1]
+ duration_unit = 7*24*60*60
else:
- dt_start = dt_end - timedelta(1)
+ duration_unit = 1
+ try:
+ options.sdt_duration = long(options.sdt_duration)
+ except ValueError:
+ print >> sys.stderr, "Can't parse duration"
+ sys.exit(1)
+ options.sdt_duration = timedelta(0, options.sdt_duration * duration_unit)
+
+ if options.sdt_start or options.sdt_duration or options.granularity is not None or options.max_count:
+ # Time period is enabled (note that date_end only defaults to one day archives ending then)
+ if not options.sdt_start and not options.sdt_end and not options.sdt_duration:
+ options.sdt_duration = timedelta(1) # One day
+ # continue without else
+ if not options.sdt_start and not options.sdt_end and options.sdt_duration:
+ dt_end = datetime.utcnow()
+ dt_start = dt_end - options.sdt_duration
+ #elif not options.sdt_start and options.sdt_end and not options.sdt_duration:
+ # never reached
+ elif not options.sdt_start and options.sdt_end and options.sdt_duration:
+ dt_end = options.sdt_end
+ dt_start = dt_end - options.sdt_duration
+ elif options.sdt_start and not options.sdt_end and not options.sdt_duration:
+ dt_start = options.sdt_start
+ dt_end = datetime.utcnow()
+ elif options.sdt_start and not options.sdt_end and options.sdt_duration:
+ dt_start = options.sdt_start
+ dt_end = dt_start + options.sdt_duration
+ elif options.sdt_start and options.sdt_end and not options.sdt_duration:
+ dt_start = options.sdt_start
+ dt_end = options.sdt_end
+ else:
+ assert options.sdt_start and options.sdt_end and options.sdt_duration, 'Internal error'
+ print >> sys.stderr, "You can't have all 3 --start --end and --duration"
+ sys.exit(1)
if options.granularity is None:
options.granularity = 600
else:
+ # Only get one position
dt_start = None
+ if options.sdt_end:
+ dt_end = options.sdt_end
+ else:
+ dt_end = datetime.utcnow()
options.max_count = 1
if options.granularity is None:
options.granularity = 600
+
logging.debug('--start is %s', dt_start)
-
+ logging.debug('--end is %s', dt_end)
#
# Filters