1 | #! /usr/bin/env python |
---|
2 | |
---|
3 | ######################### |
---|
4 | # Script to truncate BIL files to the correct size from the associated header |
---|
5 | # Intended for files affected by the issue with azspec adding lines to the bottom of level 1 files |
---|
6 | # |
---|
7 | # Author: Ben Taylor |
---|
8 | # |
---|
9 | # Change history |
---|
10 | # 20 Nov 2009: Created |
---|
11 | ######################### |
---|
12 | |
---|
13 | import sys |
---|
14 | import optparse |
---|
15 | import os.path |
---|
16 | import shutil |
---|
17 | |
---|
18 | try: |
---|
19 | import data_handler |
---|
20 | except: |
---|
21 | print "data_handler.py not found in library path. Please get the latest version from subversion." |
---|
22 | sys.exit(1) |
---|
23 | # end try |
---|
24 | |
---|
25 | # Command-line option handling |
---|
26 | usage = "\n%prog -c <input_file> [<input_file> ...]\n" |
---|
27 | usage = usage + "Fixes BIL data with extra lines on the bottom (truncates file to match header dimensions)" |
---|
28 | parser = optparse.OptionParser(usage=usage) |
---|
29 | parser.add_option("-c", "--copy", dest="copyfile", action="store_true", default=False, help="If set, makes a backup copy of each input file before truncating") |
---|
30 | |
---|
31 | (options, args) = parser.parse_args() |
---|
32 | |
---|
33 | # Check at least one input file was given |
---|
34 | if (len(args) < 1): |
---|
35 | print "No input file specified" |
---|
36 | parser.print_help() |
---|
37 | sys.exit(1) |
---|
38 | # end if |
---|
39 | |
---|
40 | # For each file given |
---|
41 | for filepath in args: |
---|
42 | |
---|
43 | # Check the file exists |
---|
44 | if (not os.path.isfile(filepath)): |
---|
45 | print "Could not find file " + filepath |
---|
46 | continue |
---|
47 | # end if |
---|
48 | |
---|
49 | # Get the filename without path or extension |
---|
50 | filename = os.path.basename(filepath) |
---|
51 | filesplit = os.path.splitext(filename) |
---|
52 | filebase = filesplit[0] |
---|
53 | |
---|
54 | # See if we can find the header file to use |
---|
55 | if (os.path.isfile(filename + ".hdr")): |
---|
56 | hdrfile = filename + ".hdr" |
---|
57 | elif (os.path.isfile(filebase + ".hdr")): |
---|
58 | hdrfile = filebase + ".hdr" |
---|
59 | else: |
---|
60 | print "Could not find header file for " + filepath |
---|
61 | continue |
---|
62 | # end if |
---|
63 | |
---|
64 | # Read the header file |
---|
65 | try: |
---|
66 | hdrdata = data_handler.readHdrFile(hdrfile) |
---|
67 | except: |
---|
68 | print "Could not read header file " + hdrfile + ". Reason: " + str(sys.exc_info()[1]) |
---|
69 | continue |
---|
70 | # end try |
---|
71 | |
---|
72 | # Check the header file has appropriate dimensions |
---|
73 | if (not (("samples" in hdrdata) and ("lines" in hdrdata) and ("bands" in hdrdata))): |
---|
74 | print "Dimension info missing from header file " + hdrfile |
---|
75 | continue |
---|
76 | # end if |
---|
77 | |
---|
78 | # Work out what the new size should be |
---|
79 | newsize = int(hdrdata["samples"]) * int(hdrdata["lines"]) * int(hdrdata["bands"]) * 2 |
---|
80 | |
---|
81 | # Create a backup copy if requested |
---|
82 | if (options.copyfile): |
---|
83 | try: |
---|
84 | shutil.copy(filepath, filepath + "-original") |
---|
85 | except: |
---|
86 | print "Could not make backup copy of " + filepath + ". Reason: " + str(sys.exc_info()[1]) |
---|
87 | continue |
---|
88 | # end try |
---|
89 | # end if |
---|
90 | |
---|
91 | # Open the file in append mode (just using write mode results in it zero-filling when it's truncated) |
---|
92 | try: |
---|
93 | datafile = open(filepath, "ab") |
---|
94 | except: |
---|
95 | print "Could not open data file " + filepath + ". Reason: " + str(sys.exc_info()[1]) |
---|
96 | continue |
---|
97 | # end try |
---|
98 | |
---|
99 | # Truncate the file to the right size |
---|
100 | try: |
---|
101 | datafile.truncate(newsize) |
---|
102 | except: |
---|
103 | print "Could not truncate data file " + filepath + ". Reason: " + str(sys.exc_info()[1]) |
---|
104 | datafile.close() |
---|
105 | continue |
---|
106 | # end try |
---|
107 | |
---|
108 | # Close the file |
---|
109 | try: |
---|
110 | datafile.close() |
---|
111 | except: |
---|
112 | pass |
---|
113 | # end try |
---|
114 | |
---|
115 | print "Successfully truncated " + filepath |
---|
116 | # end for |
---|
117 | |
---|
118 | print "Finished" |
---|