1 | #! /usr/bin/env python |
---|
2 | |
---|
3 | ############################################ |
---|
4 | # Script to extract the date from a string formatted as xxxxYYYYMMDDnnnx.xxx, where x is any character, |
---|
5 | # YYYY is a 4-digit year, MM is a two-digit month, DD is a two-digit day of month and nnn is any digit |
---|
6 | # Intended for Trimble raw GPS data files. |
---|
7 | # |
---|
8 | # Author: Ben Taylor |
---|
9 | # |
---|
10 | # Change history: |
---|
11 | # 4th Dec. 2009: Created |
---|
12 | # |
---|
13 | # You may use or alter this script as you wish, but no warranty of any kind is given - it might be broken, it might |
---|
14 | # open horrible security holes if you use it in an insecure environment, and if it does it's your fault not mine. |
---|
15 | ############################################ |
---|
16 | |
---|
17 | import sys |
---|
18 | import os.path |
---|
19 | import optparse |
---|
20 | import re |
---|
21 | |
---|
22 | # Get command-line arguments |
---|
23 | usage = "\n%prog [-h] [-q] input_file [input_file] ...\n" |
---|
24 | usage = usage + "Extracts the file date from Trimble raw GPS filenames\n" |
---|
25 | parser = optparse.OptionParser(usage=usage) |
---|
26 | parser.add_option("-q", "--quiet", dest="quiet", action="store_true", default=False, help="If set suppresses error messages") |
---|
27 | |
---|
28 | (options, args) = parser.parse_args() |
---|
29 | |
---|
30 | # Check the user actually gave an argument |
---|
31 | if (len(args) < 1): |
---|
32 | print "Must give at least one filename" |
---|
33 | sys.exit(1) |
---|
34 | # end if |
---|
35 | |
---|
36 | # For each filename given |
---|
37 | for filename in args: |
---|
38 | |
---|
39 | # Get the filename without the path |
---|
40 | filebase = os.path.basename(filename) |
---|
41 | |
---|
42 | # Try and match the expected date string from the filename |
---|
43 | datematch = re.search(".*(20\d{2})(\d{2})(\d{2})\d{4}\w\..{3}$", filebase) |
---|
44 | |
---|
45 | # If it didn't match, this isn't a string formatted as expected, go on to the next one |
---|
46 | if (datematch == None): |
---|
47 | if (not options.quiet): |
---|
48 | print filename + " is not formatted correctly for date extraction, skipping." |
---|
49 | # end if |
---|
50 | continue |
---|
51 | # end if |
---|
52 | |
---|
53 | # Get the year, month and day from the match |
---|
54 | year = datematch.group(1) |
---|
55 | month = datematch.group(2) |
---|
56 | day = datematch.group (3) |
---|
57 | |
---|
58 | # Print output |
---|
59 | print day + " " + month + " " + year |
---|
60 | # end for |
---|